eslint-plugin-mobx 0.0.13 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1,550 +1,55 @@
1
- 'use strict';
2
-
3
- var require$$0 = require('fs');
4
- var require$$1 = require('path');
5
-
6
- function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
7
-
8
- var require$$0__default = /*#__PURE__*/_interopDefaultLegacy(require$$0);
9
- var require$$1__default = /*#__PURE__*/_interopDefaultLegacy(require$$1);
10
-
11
- function _arrayLikeToArray(r, a) {
12
- (null == a || a > r.length) && (a = r.length);
13
- for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e];
14
- return n;
15
- }
16
- function _arrayWithHoles(r) {
17
- if (Array.isArray(r)) return r;
18
- }
19
- function _arrayWithoutHoles(r) {
20
- if (Array.isArray(r)) return _arrayLikeToArray(r);
21
- }
22
- function _defineProperty(e, r, t) {
23
- return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
24
- value: t,
25
- enumerable: !0,
26
- configurable: !0,
27
- writable: !0
28
- }) : e[r] = t, e;
29
- }
30
- function _iterableToArray(r) {
31
- if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r);
32
- }
33
- function _iterableToArrayLimit(r, l) {
34
- var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"];
35
- if (null != t) {
36
- var e,
37
- n,
38
- i,
39
- u,
40
- a = [],
41
- f = !0,
42
- o = !1;
43
- try {
44
- if (i = (t = t.call(r)).next, 0 === l) {
45
- if (Object(t) !== t) return;
46
- f = !1;
47
- } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0);
48
- } catch (r) {
49
- o = !0, n = r;
50
- } finally {
51
- try {
52
- if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return;
53
- } finally {
54
- if (o) throw n;
55
- }
56
- }
57
- return a;
58
- }
59
- }
60
- function _nonIterableRest() {
61
- throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
62
- }
63
- function _nonIterableSpread() {
64
- throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
65
- }
66
- function ownKeys(e, r) {
67
- var t = Object.keys(e);
68
- if (Object.getOwnPropertySymbols) {
69
- var o = Object.getOwnPropertySymbols(e);
70
- r && (o = o.filter(function (r) {
71
- return Object.getOwnPropertyDescriptor(e, r).enumerable;
72
- })), t.push.apply(t, o);
73
- }
74
- return t;
75
- }
76
- function _objectSpread2(e) {
77
- for (var r = 1; r < arguments.length; r++) {
78
- var t = null != arguments[r] ? arguments[r] : {};
79
- r % 2 ? ownKeys(Object(t), !0).forEach(function (r) {
80
- _defineProperty(e, r, t[r]);
81
- }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) {
82
- Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r));
83
- });
84
- }
85
- return e;
86
- }
87
- function _slicedToArray(r, e) {
88
- return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest();
89
- }
90
- function _toConsumableArray(r) {
91
- return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread();
92
- }
93
- function _toPrimitive(t, r) {
94
- if ("object" != typeof t || !t) return t;
95
- var e = t[Symbol.toPrimitive];
96
- if (void 0 !== e) {
97
- var i = e.call(t, r || "default");
98
- if ("object" != typeof i) return i;
99
- throw new TypeError("@@toPrimitive must return a primitive value.");
100
- }
101
- return ("string" === r ? String : Number)(t);
102
- }
103
- function _toPropertyKey(t) {
104
- var i = _toPrimitive(t, "string");
105
- return "symbol" == typeof i ? i : i + "";
106
- }
107
- function _unsupportedIterableToArray(r, a) {
108
- if (r) {
109
- if ("string" == typeof r) return _arrayLikeToArray(r, a);
110
- var t = {}.toString.call(r).slice(8, -1);
111
- return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0;
112
- }
113
- }
114
-
115
- var mobxDecorators = new Set(['observable', 'computed', 'action', 'flow', 'override']);
116
- function isMobxDecorator$2(decorator) {
117
- var _decorator$expression, _decorator$expression2;
118
- return mobxDecorators.has(decorator.expression.name) // @foo
119
- || mobxDecorators.has((_decorator$expression = decorator.expression.callee) === null || _decorator$expression === void 0 ? void 0 : _decorator$expression.name) // @foo()
120
- || mobxDecorators.has((_decorator$expression2 = decorator.expression.object) === null || _decorator$expression2 === void 0 ? void 0 : _decorator$expression2.name); // @foo.bar
121
- }
122
- function findAncestor$3(node, match) {
123
- var parent = node.parent;
124
- if (!parent) return;
125
- if (match(parent)) return parent;
126
- return findAncestor$3(parent, match);
127
- }
128
- var utils = {
129
- findAncestor: findAncestor$3,
130
- isMobxDecorator: isMobxDecorator$2
131
- };
132
-
133
- var findAncestor$2 = utils.findAncestor,
134
- isMobxDecorator$1 = utils.isMobxDecorator;
135
-
136
- // TODO support this.foo = 5; in constructor
137
- // TODO? report on field as well
138
- function create$4(context) {
139
- var _context$options$0$au, _context$options$;
140
- var sourceCode = context.getSourceCode();
141
- var autofixAnnotation = (_context$options$0$au = (_context$options$ = context.options[0]) === null || _context$options$ === void 0 ? void 0 : _context$options$.autofixAnnotation) !== null && _context$options$0$au !== void 0 ? _context$options$0$au : true;
142
- function fieldToKey(field) {
143
- // TODO cache on field?
144
- var key = sourceCode.getText(field.key);
145
- return field.computed ? "[".concat(key, "]") : key;
146
- }
147
- return {
148
- 'CallExpression[callee.name="makeObservable"]': function CallExpressionCalleeNameMakeObservable(makeObservable) {
149
- // Only interested about makeObservable(this, ...) in constructor or makeObservable({}, ...)
150
- // ClassDeclaration
151
- // ClassBody
152
- // MethodDefinition[kind="constructor"]
153
- // FunctionExpression
154
- // BlockStatement
155
- // ExpressionStatement
156
- // CallExpression[callee.name="makeObservable"]
157
- var _makeObservable$argum = _slicedToArray(makeObservable.arguments, 2),
158
- firstArg = _makeObservable$argum[0],
159
- secondArg = _makeObservable$argum[1];
160
- if (!firstArg) return;
161
- var members;
162
- if (firstArg.type === "ThisExpression") {
163
- var _closestFunction$pare;
164
- var closestFunction = findAncestor$2(makeObservable, function (node) {
165
- return node.type === "FunctionExpression" || node.type === "FunctionDeclaration";
166
- });
167
- if ((closestFunction === null || closestFunction === void 0 || (_closestFunction$pare = closestFunction.parent) === null || _closestFunction$pare === void 0 ? void 0 : _closestFunction$pare.kind) !== "constructor") return;
168
- members = closestFunction.parent.parent.parent.body.body;
169
- } else if (firstArg.type === "ObjectExpression") {
170
- members = firstArg.properties;
171
- } else {
172
- return;
173
- }
174
- var annotationProps = (secondArg === null || secondArg === void 0 ? void 0 : secondArg.properties) || [];
175
- var nonAnnotatedMembers = [];
176
- var hasAnyDecorator = false;
177
- members.forEach(function (member) {
178
- var _member$decorators;
179
- if (member["static"]) return;
180
- if (member.kind === "constructor") return;
181
- //if (member.type !== 'MethodDefinition' && member.type !== 'ClassProperty') return;
182
- hasAnyDecorator = hasAnyDecorator || ((_member$decorators = member.decorators) === null || _member$decorators === void 0 ? void 0 : _member$decorators.some(isMobxDecorator$1)) || false;
183
- if (!annotationProps.some(function (prop) {
184
- return fieldToKey(prop) === fieldToKey(member);
185
- })) {
186
- // TODO optimize?
187
- nonAnnotatedMembers.push(member);
188
- }
189
- });
190
- /*
191
- // With decorators, second arg must be null/undefined or not provided
192
- if (hasAnyDecorator && secondArg && secondArg.name !== "undefined" && secondArg.value !== null) {
193
- context.report({
194
- node: makeObservable,
195
- message: 'When using decorators, second arg must be `null`, `undefined` or not provided.',
196
- })
197
- }
198
- // Without decorators, in constructor, second arg must be object literal
199
- if (!hasAnyDecorator && firstArg.type === 'ThisExpression' && (!secondArg || secondArg.type !== 'ObjectExpression')) {
200
- context.report({
201
- node: makeObservable,
202
- message: 'Second argument must be object in form of `{ key: annotation }`.',
203
- })
204
- }
205
- */
206
-
207
- if (!hasAnyDecorator && nonAnnotatedMembers.length) {
208
- // Set avoids reporting twice for setter+getter pair or actual duplicates
209
- var keys = _toConsumableArray(new Set(nonAnnotatedMembers.map(fieldToKey)));
210
- var keyList = keys.map(function (key) {
211
- return "`".concat(key, "`");
212
- }).join(", ");
213
- var fix = function fix(fixer) {
214
- var annotationList = keys.map(function (key) {
215
- return "".concat(key, ": ").concat(autofixAnnotation);
216
- }).join(", ") + ",";
217
- if (!secondArg) {
218
- return fixer.insertTextAfter(firstArg, ", { ".concat(annotationList, " }"));
219
- } else if (secondArg.type !== "ObjectExpression") {
220
- return fixer.replaceText(secondArg, "{ ".concat(annotationList, " }"));
221
- } else {
222
- var openingBracket = sourceCode.getFirstToken(secondArg);
223
- return fixer.insertTextAfter(openingBracket, " ".concat(annotationList, " "));
224
- }
225
- };
226
- context.report({
227
- node: makeObservable,
228
- messageId: "missingAnnotation",
229
- data: {
230
- keyList: keyList
231
- },
232
- fix: fix
233
- });
234
- }
235
- }
236
- };
237
- }
238
- var exhaustiveMakeObservable$1 = {
239
- meta: {
240
- type: "suggestion",
241
- fixable: "code",
242
- schema: [{
243
- type: "object",
244
- properties: {
245
- autofixAnnotation: {
246
- type: "boolean"
247
- }
248
- },
249
- additionalProperties: false
250
- }],
251
- docs: {
252
- description: "enforce all fields being listen in `makeObservable`",
253
- recommended: true,
254
- suggestion: false
255
- },
256
- messages: {
257
- missingAnnotation: "Missing annotation for {{ keyList }}. To exclude a field, use `false` as annotation."
258
- }
259
- },
260
- create: create$4
261
- };
262
-
263
- var findAncestor$1 = utils.findAncestor;
264
- function create$3(context) {
265
- return {
266
- 'CallExpression[callee.name=/(makeObservable|makeAutoObservable)/]': function CallExpressionCalleeNameMakeObservableMakeAutoObservable(makeObservable) {
267
- var _closestFunction$pare;
268
- // Only iterested about makeObservable(this, ...) inside constructor and not inside nested bindable function
269
- var _makeObservable$argum = _slicedToArray(makeObservable.arguments, 1),
270
- firstArg = _makeObservable$argum[0];
271
- if (!firstArg) return;
272
- if (firstArg.type !== 'ThisExpression') return;
273
- // MethodDefinition[key.name="constructor"][kind="constructor"]
274
- // FunctionExpression
275
- // BlockStatement
276
- // ExpressionStatement
277
- // CallExpression[callee.name="makeObservable"]
278
- var closestFunction = findAncestor$1(makeObservable, function (node) {
279
- return node.type === 'FunctionExpression' || node.type === 'FunctionDeclaration';
280
- });
281
- if ((closestFunction === null || closestFunction === void 0 || (_closestFunction$pare = closestFunction.parent) === null || _closestFunction$pare === void 0 ? void 0 : _closestFunction$pare.kind) !== 'constructor') return;
282
- if (makeObservable.parent.parent.parent !== closestFunction) {
283
- context.report({
284
- node: makeObservable,
285
- messageId: 'mustCallUnconditionally',
286
- data: {
287
- name: makeObservable.callee.name
288
- }
289
- });
290
- }
291
- }
292
- };
293
- }
294
- var unconditionalMakeObservable$1 = {
295
- meta: {
296
- type: 'problem',
297
- docs: {
298
- description: 'disallows calling `makeObservable(this)` conditionally inside constructors',
299
- recommended: true
300
- },
301
- messages: {
302
- mustCallUnconditionally: '`{{ name }}` must be called unconditionally inside constructor.'
303
- }
304
- },
305
- create: create$3
306
- };
307
-
308
- var findAncestor = utils.findAncestor,
309
- isMobxDecorator = utils.isMobxDecorator;
310
- function create$2(context) {
311
- var sourceCode = context.getSourceCode();
312
- return {
313
- 'Decorator': function Decorator(decorator) {
314
- var _clazz$body$body$find, _constructor$value$bo;
315
- if (!isMobxDecorator(decorator)) return;
316
- var clazz = findAncestor(decorator, function (node) {
317
- return node.type === 'ClassDeclaration' || node.type === 'ClassExpression';
318
- });
319
- if (!clazz) return;
320
- // ClassDeclaration > ClassBody > []
321
- var constructor = (_clazz$body$body$find = clazz.body.body.find(function (node) {
322
- return node.kind === 'constructor' && node.value.type === 'FunctionExpression';
323
- })) !== null && _clazz$body$body$find !== void 0 ? _clazz$body$body$find : clazz.body.body.find(function (node) {
324
- return node.kind === 'constructor';
325
- });
326
- // MethodDefinition > FunctionExpression > BlockStatement > []
327
- var isMakeObservable = function isMakeObservable(node) {
328
- var _node$expression, _node$expression2;
329
- return ((_node$expression = node.expression) === null || _node$expression === void 0 || (_node$expression = _node$expression.callee) === null || _node$expression === void 0 ? void 0 : _node$expression.name) === 'makeObservable' && ((_node$expression2 = node.expression) === null || _node$expression2 === void 0 || (_node$expression2 = _node$expression2.arguments[0]) === null || _node$expression2 === void 0 ? void 0 : _node$expression2.type) === 'ThisExpression';
330
- };
331
- var makeObservable = constructor === null || constructor === void 0 || (_constructor$value$bo = constructor.value.body) === null || _constructor$value$bo === void 0 || (_constructor$value$bo = _constructor$value$bo.body.find(isMakeObservable)) === null || _constructor$value$bo === void 0 ? void 0 : _constructor$value$bo.expression;
332
- if (makeObservable) {
333
- // make sure second arg is nullish
334
- var secondArg = makeObservable.arguments[1];
335
- if (secondArg && secondArg.value !== null && secondArg.name !== 'undefined') {
336
- context.report({
337
- node: makeObservable,
338
- messageId: 'secondArgMustBeNullish'
339
- });
340
- }
341
- } else {
342
- var fix = function fix(fixer) {
343
- if ((constructor === null || constructor === void 0 ? void 0 : constructor.value.type) === 'TSEmptyBodyFunctionExpression') {
344
- // constructor() - yes this a thing
345
- var closingBracket = sourceCode.getLastToken(constructor.value);
346
- return fixer.insertTextAfter(closingBracket, ' { makeObservable(this); }');
347
- } else if (constructor) {
348
- // constructor() {}
349
- var _closingBracket = sourceCode.getLastToken(constructor.value.body);
350
- return fixer.insertTextBefore(_closingBracket, ';makeObservable(this);');
351
- } else {
352
- // class C {}
353
- var openingBracket = sourceCode.getFirstToken(clazz.body);
354
- return fixer.insertTextAfter(openingBracket, '\nconstructor() { makeObservable(this); }');
355
- }
356
- };
357
- context.report({
358
- node: clazz,
359
- messageId: 'missingMakeObservable',
360
- fix: fix
361
- });
362
- }
363
- }
364
- };
365
- }
366
- var missingMakeObservable$1 = {
367
- meta: {
368
- type: 'problem',
369
- fixable: 'code',
370
- docs: {
371
- description: 'prevents missing `makeObservable(this)` when using decorators',
372
- recommended: true,
373
- suggestion: false
374
- },
375
- messages: {
376
- missingMakeObservable: "Constructor is missing `makeObservable(this)`.",
377
- secondArgMustBeNullish: "`makeObservable`'s second argument must be nullish or not provided when using decorators."
378
- }
379
- },
380
- create: create$2
381
- };
382
-
383
- function create$1(context) {
384
- var sourceCode = context.getSourceCode();
385
- return {
386
- "FunctionDeclaration,FunctionExpression,ArrowFunctionExpression,ClassDeclaration,ClassExpression": function FunctionDeclarationFunctionExpressionArrowFunctionExpressionClassDeclarationClassExpression(cmp) {
387
- var _cmp$id, _cmpOrForwardRef$pare;
388
- if (cmp.parent && cmp.parent.type === "CallExpression" && cmp.parent.callee.name === "observer") {
389
- // observer(...)
390
- return;
391
- }
392
- var forwardRef = cmp.parent && cmp.parent.type === "CallExpression" && cmp.parent.callee.name === "forwardRef" ? cmp.parent : undefined;
393
- if (forwardRef && forwardRef.parent && forwardRef.parent.type === "CallExpression" && forwardRef.parent.callee.name === "observer") {
394
- // forwardRef(observer(...))
395
- return;
396
- }
397
- var cmpOrForwardRef = forwardRef || cmp;
398
- var name = (_cmp$id = cmp.id) === null || _cmp$id === void 0 ? void 0 : _cmp$id.name;
399
- // If anonymous try to infer name from variable declaration
400
- if (!name && ((_cmpOrForwardRef$pare = cmpOrForwardRef.parent) === null || _cmpOrForwardRef$pare === void 0 ? void 0 : _cmpOrForwardRef$pare.type) === "VariableDeclarator") {
401
- name = cmpOrForwardRef.parent.id.name;
402
- }
403
- if (cmp.type.startsWith("Class")) {
404
- // Must extend Component or React.Component
405
- var superClass = cmp.superClass;
406
- if (!superClass) {
407
- // not a component
408
- return;
409
- }
410
- var superClassText = sourceCode.getText(superClass);
411
- if (superClassText !== "Component" && superClassText !== "React.Component") {
412
- // not a component
413
- return;
414
- }
415
- } else {
416
- var _name;
417
- // Name must start with uppercase letter
418
- if (!((_name = name) !== null && _name !== void 0 && _name.charAt(0).match(/^[A-Z]$/))) {
419
- // not a component
420
- return;
421
- }
422
- }
423
- var fix = function fix(fixer) {
424
- return [fixer.insertTextBefore(sourceCode.getFirstToken(cmpOrForwardRef), (name && cmp.type.endsWith("Declaration") ? "const ".concat(name, " = ") : "") + "observer("), fixer.insertTextAfter(sourceCode.getLastToken(cmpOrForwardRef), ")")];
425
- };
426
- context.report({
427
- node: cmp,
428
- messageId: "missingObserver",
429
- data: {
430
- name: name || "<anonymous>"
431
- },
432
- fix: fix
433
- });
434
- }
435
- };
436
- }
437
- var missingObserver$1 = {
438
- meta: {
439
- type: "problem",
440
- fixable: "code",
441
- docs: {
442
- description: "prevents missing `observer` on react component",
443
- recommended: true
444
- },
445
- messages: {
446
- missingObserver: "Component `{{ name }}` is missing `observer`."
447
- }
448
- },
449
- create: create$1
450
- };
451
-
452
- function create(context) {
453
- var sourceCode = context.getSourceCode();
454
- return {
455
- 'CallExpression[callee.name="observer"]': function CallExpressionCalleeNameObserver(observer) {
456
- var _cmp$id;
457
- var cmp = observer.arguments[0];
458
- if (!cmp) return;
459
- if (cmp !== null && cmp !== void 0 && (_cmp$id = cmp.id) !== null && _cmp$id !== void 0 && _cmp$id.name) return;
460
- var fix = function fix(fixer) {
461
- var _observer$parent;
462
- // Use name from variable for autofix
463
- var name = ((_observer$parent = observer.parent) === null || _observer$parent === void 0 ? void 0 : _observer$parent.type) === "VariableDeclarator" ? observer.parent.id.name : undefined;
464
- if (!name) return;
465
- if (cmp.type === "ArrowFunctionExpression") {
466
- var arrowToken = sourceCode.getTokenBefore(cmp.body);
467
- var fixes = [fixer.replaceText(arrowToken, ""), fixer.insertTextBefore(cmp, "function ".concat(name))];
468
- if (cmp.body.type !== "BlockStatement") {
469
- fixes.push(fixer.insertTextBefore(cmp.body, "{ return "), fixer.insertTextAfter(cmp.body, " }"));
470
- }
471
- return fixes;
472
- }
473
- if (cmp.type === "FunctionExpression") {
474
- var functionToken = sourceCode.getFirstToken(cmp);
475
- return fixer.replaceText(functionToken, "function ".concat(name));
476
- }
477
- if (cmp.type === "ClassExpression") {
478
- var classToken = sourceCode.getFirstToken(cmp);
479
- return fixer.replaceText(classToken, "class ".concat(name));
480
- }
481
- };
482
- context.report({
483
- node: cmp,
484
- messageId: "observerComponentMustHaveName",
485
- fix: fix
486
- });
487
- }
488
- };
489
- }
490
- var noAnonymousObserver$1 = {
491
- meta: {
492
- type: "problem",
493
- fixable: "code",
494
- docs: {
495
- description: "forbids anonymous functions or classes as `observer` components",
496
- recommended: true
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ var __importDefault = (this && this.__importDefault) || function (mod) {
14
+ return (mod && mod.__esModule) ? mod : { "default": mod };
15
+ };
16
+ var fs_1 = __importDefault(require("fs"));
17
+ var path_1 = __importDefault(require("path"));
18
+ var exhaustive_make_observable_1 = __importDefault(require("./exhaustive-make-observable"));
19
+ var unconditional_make_observable_1 = __importDefault(require("./unconditional-make-observable"));
20
+ var missing_make_observable_1 = __importDefault(require("./missing-make-observable"));
21
+ var missing_observer_1 = __importDefault(require("./missing-observer"));
22
+ var no_anonymous_observer_1 = __importDefault(require("./no-anonymous-observer"));
23
+ var pkg = JSON.parse(fs_1.default.readFileSync(path_1.default.join(__dirname, "..", "package.json"), "utf8"));
24
+ var pluginMobx = {
25
+ meta: {
26
+ name: pkg.name,
27
+ version: pkg.version
497
28
  },
498
- messages: {
499
- observerComponentMustHaveName: "`observer` component must have a name."
29
+ rules: {
30
+ "exhaustive-make-observable": exhaustive_make_observable_1.default,
31
+ "unconditional-make-observable": unconditional_make_observable_1.default,
32
+ "missing-make-observable": missing_make_observable_1.default,
33
+ "missing-observer": missing_observer_1.default,
34
+ "no-anonymous-observer": no_anonymous_observer_1.default
500
35
  }
501
- },
502
- create: create
503
- };
504
-
505
- var fs = require$$0__default["default"];
506
- var path = require$$1__default["default"];
507
- var exhaustiveMakeObservable = exhaustiveMakeObservable$1;
508
- var unconditionalMakeObservable = unconditionalMakeObservable$1;
509
- var missingMakeObservable = missingMakeObservable$1;
510
- var missingObserver = missingObserver$1;
511
- var noAnonymousObserver = noAnonymousObserver$1;
512
- var pkg = JSON.parse(fs.readFileSync(path.join(__dirname, "..", "package.json"), "utf8"));
513
- var pluginMobx = {
514
- meta: {
515
- name: pkg.name,
516
- version: pkg.version
517
- },
518
- rules: {
519
- "exhaustive-make-observable": exhaustiveMakeObservable,
520
- "unconditional-make-observable": unconditionalMakeObservable,
521
- "missing-make-observable": missingMakeObservable,
522
- "missing-observer": missingObserver,
523
- "no-anonymous-observer": noAnonymousObserver
524
- }
525
36
  };
526
37
  var recommendedRules = {
527
- "mobx/exhaustive-make-observable": "warn",
528
- "mobx/unconditional-make-observable": "error",
529
- "mobx/missing-make-observable": "error",
530
- "mobx/missing-observer": "warn"
531
- };
532
- var src = _objectSpread2(_objectSpread2({}, pluginMobx), {}, {
533
- configs: {
534
- recommended: {
535
- plugins: ["mobx"],
536
- rules: recommendedRules
537
- }
538
- },
539
- flatConfigs: {
540
- recommended: {
541
- name: "mobx/recommended",
542
- plugins: {
543
- mobx: pluginMobx
544
- },
545
- rules: recommendedRules
546
- }
547
- }
548
- });
549
-
550
- module.exports = src;
38
+ "mobx/exhaustive-make-observable": "warn",
39
+ "mobx/unconditional-make-observable": "error",
40
+ "mobx/missing-make-observable": "error",
41
+ "mobx/missing-observer": "warn"
42
+ };
43
+ var plugin = __assign(__assign({}, pluginMobx), { configs: {
44
+ recommended: {
45
+ plugins: ["mobx"],
46
+ rules: recommendedRules
47
+ }
48
+ }, flatConfigs: {
49
+ recommended: {
50
+ name: "mobx/recommended",
51
+ plugins: { mobx: pluginMobx },
52
+ rules: recommendedRules
53
+ }
54
+ } });
55
+ module.exports = plugin;
@@ -0,0 +1,3 @@
1
+ import type { Rule } from 'eslint';
2
+ declare const rule: Rule.RuleModule;
3
+ export default rule;
@@ -0,0 +1,72 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ var utils_1 = require("./utils");
4
+ function create(context) {
5
+ var _a;
6
+ var sourceCode = (_a = context.sourceCode) !== null && _a !== void 0 ? _a : context.getSourceCode();
7
+ return {
8
+ 'Decorator': function (decorator) {
9
+ var _a, _b, _c;
10
+ if (!(0, utils_1.isMobxDecorator)(decorator))
11
+ return;
12
+ var clazz = (0, utils_1.findAncestor)(decorator, function (node) { return node.type === 'ClassDeclaration' || node.type === 'ClassExpression'; });
13
+ if (!clazz)
14
+ return;
15
+ // ClassDeclaration > ClassBody > []
16
+ var constructor = (_a = clazz.body.body.find(function (node) { return node.kind === 'constructor' && node.value.type === 'FunctionExpression'; })) !== null && _a !== void 0 ? _a : clazz.body.body.find(function (node) { return node.kind === 'constructor'; });
17
+ // MethodDefinition > FunctionExpression > BlockStatement > []
18
+ var isMakeObservable = function (node) { var _a, _b, _c, _d; return ((_b = (_a = node.expression) === null || _a === void 0 ? void 0 : _a.callee) === null || _b === void 0 ? void 0 : _b.name) === 'makeObservable' && ((_d = (_c = node.expression) === null || _c === void 0 ? void 0 : _c.arguments[0]) === null || _d === void 0 ? void 0 : _d.type) === 'ThisExpression'; };
19
+ var makeObservable = (_c = (_b = constructor === null || constructor === void 0 ? void 0 : constructor.value.body) === null || _b === void 0 ? void 0 : _b.body.find(isMakeObservable)) === null || _c === void 0 ? void 0 : _c.expression;
20
+ if (makeObservable) {
21
+ // make sure second arg is nullish
22
+ var secondArg = makeObservable.arguments[1];
23
+ if (secondArg && secondArg.value !== null && secondArg.name !== 'undefined') {
24
+ context.report({
25
+ node: makeObservable,
26
+ messageId: 'secondArgMustBeNullish',
27
+ });
28
+ }
29
+ }
30
+ else {
31
+ var fix = function (fixer) {
32
+ if ((constructor === null || constructor === void 0 ? void 0 : constructor.value.type) === 'TSEmptyBodyFunctionExpression') {
33
+ // constructor() - yes this a thing
34
+ var closingBracket = sourceCode.getLastToken(constructor.value);
35
+ return fixer.insertTextAfter(closingBracket, ' { makeObservable(this); }');
36
+ }
37
+ else if (constructor) {
38
+ // constructor() {}
39
+ var closingBracket = sourceCode.getLastToken(constructor.value.body);
40
+ return fixer.insertTextBefore(closingBracket, ';makeObservable(this);');
41
+ }
42
+ else {
43
+ // class C {}
44
+ var openingBracket = sourceCode.getFirstToken(clazz.body);
45
+ return fixer.insertTextAfter(openingBracket, '\nconstructor() { makeObservable(this); }');
46
+ }
47
+ };
48
+ context.report({
49
+ node: clazz,
50
+ messageId: 'missingMakeObservable',
51
+ fix: fix,
52
+ });
53
+ }
54
+ },
55
+ };
56
+ }
57
+ var rule = {
58
+ meta: {
59
+ type: 'problem',
60
+ fixable: 'code',
61
+ docs: {
62
+ description: 'prevents missing `makeObservable(this)` when using decorators',
63
+ recommended: true,
64
+ },
65
+ messages: {
66
+ missingMakeObservable: "Constructor is missing `makeObservable(this)`.",
67
+ secondArgMustBeNullish: "`makeObservable`'s second argument must be nullish or not provided when using decorators."
68
+ },
69
+ },
70
+ create: create,
71
+ };
72
+ exports.default = rule;