eslint-plugin-mobx 0.0.6 → 0.0.9

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/CHANGELOG.md CHANGED
@@ -1,5 +1,23 @@
1
1
  # eslint-plugin-mobx
2
2
 
3
+ ## 0.0.9
4
+
5
+ ### Patch Changes
6
+
7
+ - [`e63c2df0`](https://github.com/mobxjs/mobx/commit/e63c2df0ef166868675bce21892cd686a46db953) [#3443](https://github.com/mobxjs/mobx/pull/3443) Thanks [@urugator](https://github.com/urugator)! - changeset for PR #3423: deprecate no-anonymous-observer
8
+
9
+ ## 0.0.8
10
+
11
+ ### Patch Changes
12
+
13
+ - [`aafda613`](https://github.com/mobxjs/mobx/commit/aafda6136afd107c6cbdd73d9bab57e45a2eb9f5) [#3256](https://github.com/mobxjs/mobx/pull/3256) Thanks [@urugator](https://github.com/urugator)! - fix `no-anonymous-observer` autofix for arrow functions without BlockStatement body
14
+
15
+ ## 0.0.7
16
+
17
+ ### Patch Changes
18
+
19
+ - [`0aaf1831`](https://github.com/mobxjs/mobx/commit/0aaf183131f3eadd40c05ccc94139282bd8d7d56) [#3231](https://github.com/mobxjs/mobx/pull/3231) Thanks [@ahoisl](https://github.com/ahoisl)! - fix(lint): fix 'missing-make-observable' rule with constructor overloads
20
+
3
21
  ## 0.0.6
4
22
 
5
23
  ### Patch Changes
package/README.md CHANGED
@@ -82,7 +82,22 @@ It's a bit opinionated and can lead to a lot of false positives depending on you
82
82
  ]
83
83
  ```
84
84
 
85
- ### mobx/no-anonymous-observer
85
+ ### mobx/no-anonymous-observer (deprecated)
86
+
87
+ _Deprecated in favor of [react/display-name](https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/display-name.md) + [componentWrapperFunctions](https://github.com/jsx-eslint/eslint-plugin-react). Example of **.eslintrc**:_
88
+ ```
89
+ {
90
+ "rules": {
91
+ "react/display-name": "warn"
92
+ },
93
+ "settings": {
94
+ "componentWrapperFunctions": [
95
+ "observer"
96
+ ]
97
+ }
98
+ }
99
+ ```
100
+ ---
86
101
 
87
102
  Forbids anonymous functions or classes as `observer` components.
88
103
  Improves debugging experience and [avoids problem with inability to customize `displayName`](https://github.com/mobxjs/mobx/issues/2721).
package/dist/index.js CHANGED
@@ -285,7 +285,7 @@ function create$2(context) {
285
285
  var sourceCode = context.getSourceCode();
286
286
  return {
287
287
  'Decorator': function Decorator(decorator) {
288
- var _constructor$value$bo, _constructor$value$bo2;
288
+ var _clazz$body$body$find, _constructor$value$bo, _constructor$value$bo2;
289
289
 
290
290
  if (!isMobxDecorator(decorator)) return;
291
291
  var clazz = findAncestor(decorator, function (node) {
@@ -293,7 +293,9 @@ function create$2(context) {
293
293
  });
294
294
  if (!clazz) return; // ClassDeclaration > ClassBody > []
295
295
 
296
- var constructor = clazz.body.body.find(function (node) {
296
+ var constructor = (_clazz$body$body$find = clazz.body.body.find(function (node) {
297
+ return node.kind === 'constructor' && node.value.type === 'FunctionExpression';
298
+ })) !== null && _clazz$body$body$find !== void 0 ? _clazz$body$body$find : clazz.body.body.find(function (node) {
297
299
  return node.kind === 'constructor';
298
300
  }); // MethodDefinition > FunctionExpression > BlockStatement > []
299
301
 
@@ -431,21 +433,27 @@ function create(context) {
431
433
  var fix = function fix(fixer) {
432
434
  var _observer$parent;
433
435
 
434
- // Use name from variable for autofix
435
- var name = ((_observer$parent = observer.parent) === null || _observer$parent === void 0 ? void 0 : _observer$parent.type) === 'VariableDeclarator' ? observer.parent.id.name : undefined;
436
+ // Use name from variable for autofix
437
+ var name = ((_observer$parent = observer.parent) === null || _observer$parent === void 0 ? void 0 : _observer$parent.type) === "VariableDeclarator" ? observer.parent.id.name : undefined;
436
438
  if (!name) return;
437
439
 
438
- if (cmp.type === 'ArrowFunctionExpression') {
440
+ if (cmp.type === "ArrowFunctionExpression") {
439
441
  var arrowToken = sourceCode.getTokenBefore(cmp.body);
440
- return [fixer.replaceText(arrowToken, ''), fixer.insertTextBefore(cmp, "function ".concat(name))];
442
+ var fixes = [fixer.replaceText(arrowToken, ""), fixer.insertTextBefore(cmp, "function ".concat(name))];
443
+
444
+ if (cmp.body.type !== "BlockStatement") {
445
+ fixes.push(fixer.insertTextBefore(cmp.body, "{ return "), fixer.insertTextAfter(cmp.body, " }"));
446
+ }
447
+
448
+ return fixes;
441
449
  }
442
450
 
443
- if (cmp.type === 'FunctionExpression') {
451
+ if (cmp.type === "FunctionExpression") {
444
452
  var functionToken = sourceCode.getFirstToken(cmp);
445
453
  return fixer.replaceText(functionToken, "function ".concat(name));
446
454
  }
447
455
 
448
- if (cmp.type === 'ClassExpression') {
456
+ if (cmp.type === "ClassExpression") {
449
457
  var classToken = sourceCode.getFirstToken(cmp);
450
458
  return fixer.replaceText(classToken, "class ".concat(name));
451
459
  }
@@ -453,7 +461,7 @@ function create(context) {
453
461
 
454
462
  context.report({
455
463
  node: cmp,
456
- messageId: 'observerComponentMustHaveName',
464
+ messageId: "observerComponentMustHaveName",
457
465
  fix: fix
458
466
  });
459
467
  }
@@ -462,10 +470,10 @@ function create(context) {
462
470
 
463
471
  var noAnonymousObserver$1 = {
464
472
  meta: {
465
- type: 'problem',
466
- fixable: 'code',
473
+ type: "problem",
474
+ fixable: "code",
467
475
  docs: {
468
- description: 'forbids anonymous functions or classes as `observer` components',
476
+ description: "forbids anonymous functions or classes as `observer` components",
469
477
  recommended: true
470
478
  },
471
479
  messages: {
@@ -488,8 +496,7 @@ var src = {
488
496
  "mobx/exhaustive-make-observable": "warn",
489
497
  "mobx/unconditional-make-observable": "error",
490
498
  "mobx/missing-make-observable": "error",
491
- "mobx/missing-observer": "warn",
492
- "mobx/no-anonymous-observer": "warn"
499
+ "mobx/missing-observer": "warn"
493
500
  }
494
501
  }
495
502
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-mobx",
3
- "version": "0.0.6",
3
+ "version": "0.0.9",
4
4
  "description": "ESLint rules for MobX",
5
5
  "main": "dist/index.js",
6
6
  "repository": {
package/src/index.js CHANGED
@@ -14,8 +14,7 @@ module.exports = {
14
14
  "mobx/exhaustive-make-observable": "warn",
15
15
  "mobx/unconditional-make-observable": "error",
16
16
  "mobx/missing-make-observable": "error",
17
- "mobx/missing-observer": "warn",
18
- "mobx/no-anonymous-observer": "warn"
17
+ "mobx/missing-observer": "warn"
19
18
  }
20
19
  }
21
20
  },
@@ -11,7 +11,8 @@ function create(context) {
11
11
  const clazz = findAncestor(decorator, node => node.type === 'ClassDeclaration' || node.type === 'ClassExpression');
12
12
  if (!clazz) return;
13
13
  // ClassDeclaration > ClassBody > []
14
- const constructor = clazz.body.body.find(node => node.kind === 'constructor');
14
+ const constructor = clazz.body.body.find(node => node.kind === 'constructor' && node.value.type === 'FunctionExpression') ??
15
+ clazz.body.body.find(node => node.kind === 'constructor');
15
16
  // MethodDefinition > FunctionExpression > BlockStatement > []
16
17
  const isMakeObservable = node => node.expression?.callee?.name === 'makeObservable' && node.expression?.arguments[0]?.type === 'ThisExpression';
17
18
  const makeObservable = constructor?.value.body?.body.find(isMakeObservable)?.expression;
@@ -1,57 +1,65 @@
1
- 'use strict';
1
+ "use strict"
2
2
 
3
3
  function create(context) {
4
- const sourceCode = context.getSourceCode();
4
+ const sourceCode = context.getSourceCode()
5
5
 
6
- return {
7
- 'CallExpression[callee.name="observer"]': observer => {
8
- const cmp = observer.arguments[0];
9
- if (!cmp) return;
10
- if (cmp?.id?.name) return;
6
+ return {
7
+ 'CallExpression[callee.name="observer"]': observer => {
8
+ const cmp = observer.arguments[0]
9
+ if (!cmp) return
10
+ if (cmp?.id?.name) return
11
11
 
12
- const fix = fixer => {
13
- // Use name from variable for autofix
14
- const name = observer.parent?.type === 'VariableDeclarator'
15
- ? observer.parent.id.name
16
- : undefined;
12
+ const fix = fixer => {
13
+ // Use name from variable for autofix
14
+ const name =
15
+ observer.parent?.type === "VariableDeclarator"
16
+ ? observer.parent.id.name
17
+ : undefined
17
18
 
18
- if (!name) return;
19
- if (cmp.type === 'ArrowFunctionExpression') {
20
- const arrowToken = sourceCode.getTokenBefore(cmp.body);
21
- return [
22
- fixer.replaceText(arrowToken, ''),
23
- fixer.insertTextBefore(cmp, `function ${name}`),
24
- ]
19
+ if (!name) return
20
+ if (cmp.type === "ArrowFunctionExpression") {
21
+ const arrowToken = sourceCode.getTokenBefore(cmp.body)
22
+ const fixes = [
23
+ fixer.replaceText(arrowToken, ""),
24
+ fixer.insertTextBefore(cmp, `function ${name}`)
25
+ ]
26
+ if (cmp.body.type !== "BlockStatement") {
27
+ fixes.push(
28
+ fixer.insertTextBefore(cmp.body, `{ return `),
29
+ fixer.insertTextAfter(cmp.body, ` }`)
30
+ )
31
+ }
32
+ return fixes
33
+ }
34
+ if (cmp.type === "FunctionExpression") {
35
+ const functionToken = sourceCode.getFirstToken(cmp)
36
+ return fixer.replaceText(functionToken, `function ${name}`)
37
+ }
38
+ if (cmp.type === "ClassExpression") {
39
+ const classToken = sourceCode.getFirstToken(cmp)
40
+ return fixer.replaceText(classToken, `class ${name}`)
41
+ }
42
+ }
43
+ context.report({
44
+ node: cmp,
45
+ messageId: "observerComponentMustHaveName",
46
+ fix
47
+ })
25
48
  }
26
- if (cmp.type === 'FunctionExpression') {
27
- const functionToken = sourceCode.getFirstToken(cmp);
28
- return fixer.replaceText(functionToken, `function ${name}`);
29
- }
30
- if (cmp.type === 'ClassExpression') {
31
- const classToken = sourceCode.getFirstToken(cmp);
32
- return fixer.replaceText(classToken, `class ${name}`);
33
- }
34
- }
35
- context.report({
36
- node: cmp,
37
- messageId: 'observerComponentMustHaveName',
38
- fix,
39
- })
40
- },
41
- };
49
+ }
42
50
  }
43
51
 
44
52
  module.exports = {
45
- meta: {
46
- type: 'problem',
47
- fixable: 'code',
48
- docs: {
49
- description: 'forbids anonymous functions or classes as `observer` components',
50
- recommended: true,
51
- },
52
- messages: {
53
- observerComponentMustHaveName: "`observer` component must have a name.",
53
+ meta: {
54
+ type: "problem",
55
+ fixable: "code",
56
+ docs: {
57
+ description: "forbids anonymous functions or classes as `observer` components",
58
+ recommended: true
59
+ },
60
+ messages: {
61
+ observerComponentMustHaveName: "`observer` component must have a name."
62
+ }
54
63
  },
55
- },
56
- create,
57
- };
64
+ create
65
+ }