eslint-plugin-mobx 0.0.7 → 0.0.8
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 +6 -0
- package/dist/index.js +16 -10
- package/package.json +1 -1
- package/src/no-anonymous-observer.js +55 -47
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
# eslint-plugin-mobx
|
|
2
2
|
|
|
3
|
+
## 0.0.8
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [`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
|
|
8
|
+
|
|
3
9
|
## 0.0.7
|
|
4
10
|
|
|
5
11
|
### Patch Changes
|
package/dist/index.js
CHANGED
|
@@ -433,21 +433,27 @@ function create(context) {
|
|
|
433
433
|
var fix = function fix(fixer) {
|
|
434
434
|
var _observer$parent;
|
|
435
435
|
|
|
436
|
-
// Use name from variable for autofix
|
|
437
|
-
var name = ((_observer$parent = observer.parent) === null || _observer$parent === void 0 ? void 0 : _observer$parent.type) ===
|
|
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;
|
|
438
438
|
if (!name) return;
|
|
439
439
|
|
|
440
|
-
if (cmp.type ===
|
|
440
|
+
if (cmp.type === "ArrowFunctionExpression") {
|
|
441
441
|
var arrowToken = sourceCode.getTokenBefore(cmp.body);
|
|
442
|
-
|
|
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;
|
|
443
449
|
}
|
|
444
450
|
|
|
445
|
-
if (cmp.type ===
|
|
451
|
+
if (cmp.type === "FunctionExpression") {
|
|
446
452
|
var functionToken = sourceCode.getFirstToken(cmp);
|
|
447
453
|
return fixer.replaceText(functionToken, "function ".concat(name));
|
|
448
454
|
}
|
|
449
455
|
|
|
450
|
-
if (cmp.type ===
|
|
456
|
+
if (cmp.type === "ClassExpression") {
|
|
451
457
|
var classToken = sourceCode.getFirstToken(cmp);
|
|
452
458
|
return fixer.replaceText(classToken, "class ".concat(name));
|
|
453
459
|
}
|
|
@@ -455,7 +461,7 @@ function create(context) {
|
|
|
455
461
|
|
|
456
462
|
context.report({
|
|
457
463
|
node: cmp,
|
|
458
|
-
messageId:
|
|
464
|
+
messageId: "observerComponentMustHaveName",
|
|
459
465
|
fix: fix
|
|
460
466
|
});
|
|
461
467
|
}
|
|
@@ -464,10 +470,10 @@ function create(context) {
|
|
|
464
470
|
|
|
465
471
|
var noAnonymousObserver$1 = {
|
|
466
472
|
meta: {
|
|
467
|
-
type:
|
|
468
|
-
fixable:
|
|
473
|
+
type: "problem",
|
|
474
|
+
fixable: "code",
|
|
469
475
|
docs: {
|
|
470
|
-
description:
|
|
476
|
+
description: "forbids anonymous functions or classes as `observer` components",
|
|
471
477
|
recommended: true
|
|
472
478
|
},
|
|
473
479
|
messages: {
|
package/package.json
CHANGED
|
@@ -1,57 +1,65 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict"
|
|
2
2
|
|
|
3
3
|
function create(context) {
|
|
4
|
-
|
|
4
|
+
const sourceCode = context.getSourceCode()
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
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
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
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
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
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
|
-
|
|
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
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
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
|
-
|
|
57
|
-
};
|
|
64
|
+
create
|
|
65
|
+
}
|