eslint-plugin-mobx 0.0.2 → 0.0.6
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 +25 -0
- package/README.md +54 -12
- package/dist/index.js +505 -0
- package/package.json +15 -7
- package/src/index.js +25 -19
- package/src/missing-observer.js +63 -0
- package/src/no-anonymous-observer.js +57 -0
- package/src/index.ts +0 -3
package/CHANGELOG.md
CHANGED
|
@@ -1 +1,26 @@
|
|
|
1
1
|
# eslint-plugin-mobx
|
|
2
|
+
|
|
3
|
+
## 0.0.6
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [`4b1337ec`](https://github.com/mobxjs/mobx/commit/4b1337ecd64c7bfc904a04063bd1b07e62e392f1) [#3228](https://github.com/mobxjs/mobx/pull/3228) Thanks [@ahoisl](https://github.com/ahoisl)! - fix name for missing-observer rule in recommended
|
|
8
|
+
|
|
9
|
+
## 0.0.5
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- [`021f34ec`](https://github.com/mobxjs/mobx/commit/021f34ec81daed9e5b5ed8425b2f3e0fa85dfe5b) [#3219](https://github.com/mobxjs/mobx/pull/3219) Thanks [@urugator](https://github.com/urugator)! - Add [`mobx/missing-observer`](https://github.com/mobxjs/mobx/tree/main/packages/eslint-plugin-mobx#mobxmissing-observer),
|
|
14
|
+
[`mobx/no-anonymous-observer`](https://github.com/mobxjs/mobx/tree/main/packages/eslint-plugin-mobx#mobxno-anonymous-observer) rules,
|
|
15
|
+
|
|
16
|
+
## 0.0.4
|
|
17
|
+
|
|
18
|
+
### Patch Changes
|
|
19
|
+
|
|
20
|
+
- [`5b6f3001`](https://github.com/mobxjs/mobx/commit/5b6f30017939a2082f7d767a857e0189210a91a7) [#3204](https://github.com/mobxjs/mobx/pull/3204) Thanks [@urugator](https://github.com/urugator)! - changed build process
|
|
21
|
+
|
|
22
|
+
## 0.0.3
|
|
23
|
+
|
|
24
|
+
### Patch Changes
|
|
25
|
+
|
|
26
|
+
- [`cd6a6a68`](https://github.com/mobxjs/mobx/commit/cd6a6a68245f082bdc35a3109214a5449ef9818d) [#3200](https://github.com/mobxjs/mobx/pull/3200) Thanks [@urugator](https://github.com/urugator)! - fix package.json
|
package/README.md
CHANGED
|
@@ -5,7 +5,7 @@ Mobx specific linting rules for `eslint`.
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
7
7
|
```
|
|
8
|
-
npm install --save-dev eslint @typescript-eslint/parser eslint-plugin-mobx
|
|
8
|
+
npm install --save-dev eslint @typescript-eslint/parser eslint-plugin-mobx
|
|
9
9
|
```
|
|
10
10
|
|
|
11
11
|
## Configuration
|
|
@@ -15,17 +15,19 @@ npm install --save-dev eslint @typescript-eslint/parser eslint-plugin-mobx
|
|
|
15
15
|
module.exports = {
|
|
16
16
|
parser: "@typescript-eslint/parser",
|
|
17
17
|
// Include "mobx" in plugins array:
|
|
18
|
-
plugins: ["mobx"],
|
|
18
|
+
plugins: ["mobx"],
|
|
19
19
|
// Either extend our recommended configuration:
|
|
20
20
|
extends: "plugin:mobx/recommended",
|
|
21
21
|
// ...or specify and customize individual rules:
|
|
22
22
|
rules: {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
23
|
+
// these values are the same as recommended
|
|
24
|
+
"mobx/exhaustive-make-observable": "warn",
|
|
25
|
+
"mobx/unconditional-make-observable": "error",
|
|
26
|
+
"mobx/missing-make-observable": "error",
|
|
27
|
+
"mobx/missing-observer": "warn",
|
|
28
|
+
"mobx/no-anonymous-observer": "warn"
|
|
29
|
+
}
|
|
30
|
+
}
|
|
29
31
|
```
|
|
30
32
|
|
|
31
33
|
## Rules
|
|
@@ -33,16 +35,56 @@ module.exports = {
|
|
|
33
35
|
### mobx/exhaustive-make-observable
|
|
34
36
|
|
|
35
37
|
Makes sure that `makeObservable` annotates all fields defined on class or object literal.<br>
|
|
36
|
-
Autofix adds `field: true` for each missing field.<br>
|
|
38
|
+
**Autofix** adds `field: true` for each missing field.<br>
|
|
37
39
|
To exclude a field, annotate it using `field: false`.<br>
|
|
38
40
|
Does not support fields introduced by constructor (`this.foo = 5`).<br>
|
|
39
41
|
Does not warn about annotated non-existing fields (there is a runtime check, but the autofix removing the field could be handy...).
|
|
40
42
|
|
|
41
43
|
### mobx/missing-make-observable
|
|
42
44
|
|
|
43
|
-
|
|
44
|
-
Autofix creates a constructor if necessary and adds `makeObservable(this)` at it's end.
|
|
45
|
+
_When using decorators (eg `@observable foo = 5`)_, makes sure that `makeObservable(this)` is called in a constructor.<br>
|
|
46
|
+
**Autofix** creates a constructor if necessary and adds `makeObservable(this)` at it's end.
|
|
45
47
|
|
|
46
48
|
### mobx/unconditional-make-observable
|
|
47
49
|
|
|
48
|
-
Makes sure the `make(Auto)Observable(this)` is called unconditionally inside a constructor.
|
|
50
|
+
Makes sure the `make(Auto)Observable(this)` is called unconditionally inside a constructor.
|
|
51
|
+
|
|
52
|
+
### mobx/missing-observer
|
|
53
|
+
|
|
54
|
+
Makes sure every React component is wrapped with `observer`. A React component is considered to be any _class_ extending from `Component` or `React.Component` and any _function_ which name has the first letter capitalized (for anonymous functions the name is inferred from variable). These are all considered components:
|
|
55
|
+
|
|
56
|
+
```javascript
|
|
57
|
+
class Cmp extends React.Component { }
|
|
58
|
+
class Cmp extends Component { }
|
|
59
|
+
const Cmp = class extends React.Component { }
|
|
60
|
+
const Cmp = class extends Component { }
|
|
61
|
+
class extends Component { }
|
|
62
|
+
class extends React.Component { }
|
|
63
|
+
|
|
64
|
+
function Named() { }
|
|
65
|
+
const foo = function Named() { }
|
|
66
|
+
const Anonym = function () { };
|
|
67
|
+
const Arrow = () => { };
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
**Autofix** wraps the component with `observer` and if necessary declares a constant of the same name: `const Name = observer(function Name() {})`.
|
|
71
|
+
It's a bit opinionated and can lead to a lot of false positives depending on your conventions. You will probably want to combine this rule with `overrides` option, eg:
|
|
72
|
+
|
|
73
|
+
```javascript
|
|
74
|
+
// .eslintrc.js
|
|
75
|
+
"overrides": [
|
|
76
|
+
{
|
|
77
|
+
"files": ["*.jsx"],
|
|
78
|
+
"rules": {
|
|
79
|
+
"mobx/missing-observer": "error"
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
]
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
### mobx/no-anonymous-observer
|
|
86
|
+
|
|
87
|
+
Forbids anonymous functions or classes as `observer` components.
|
|
88
|
+
Improves debugging experience and [avoids problem with inability to customize `displayName`](https://github.com/mobxjs/mobx/issues/2721).
|
|
89
|
+
Plays nice with `eslint-plugin-react-hooks` and `mobx/missing-observer` as both of these don't not recognize anonymous function as component.
|
|
90
|
+
**Autofix** infers the name from variable if possible.
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,505 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
function _slicedToArray(arr, i) {
|
|
4
|
+
return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest();
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
function _toConsumableArray(arr) {
|
|
8
|
+
return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread();
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function _arrayWithoutHoles(arr) {
|
|
12
|
+
if (Array.isArray(arr)) return _arrayLikeToArray(arr);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function _arrayWithHoles(arr) {
|
|
16
|
+
if (Array.isArray(arr)) return arr;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function _iterableToArray(iter) {
|
|
20
|
+
if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function _iterableToArrayLimit(arr, i) {
|
|
24
|
+
var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
|
|
25
|
+
|
|
26
|
+
if (_i == null) return;
|
|
27
|
+
var _arr = [];
|
|
28
|
+
var _n = true;
|
|
29
|
+
var _d = false;
|
|
30
|
+
|
|
31
|
+
var _s, _e;
|
|
32
|
+
|
|
33
|
+
try {
|
|
34
|
+
for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) {
|
|
35
|
+
_arr.push(_s.value);
|
|
36
|
+
|
|
37
|
+
if (i && _arr.length === i) break;
|
|
38
|
+
}
|
|
39
|
+
} catch (err) {
|
|
40
|
+
_d = true;
|
|
41
|
+
_e = err;
|
|
42
|
+
} finally {
|
|
43
|
+
try {
|
|
44
|
+
if (!_n && _i["return"] != null) _i["return"]();
|
|
45
|
+
} finally {
|
|
46
|
+
if (_d) throw _e;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return _arr;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function _unsupportedIterableToArray(o, minLen) {
|
|
54
|
+
if (!o) return;
|
|
55
|
+
if (typeof o === "string") return _arrayLikeToArray(o, minLen);
|
|
56
|
+
var n = Object.prototype.toString.call(o).slice(8, -1);
|
|
57
|
+
if (n === "Object" && o.constructor) n = o.constructor.name;
|
|
58
|
+
if (n === "Map" || n === "Set") return Array.from(o);
|
|
59
|
+
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function _arrayLikeToArray(arr, len) {
|
|
63
|
+
if (len == null || len > arr.length) len = arr.length;
|
|
64
|
+
|
|
65
|
+
for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
|
|
66
|
+
|
|
67
|
+
return arr2;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function _nonIterableSpread() {
|
|
71
|
+
throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function _nonIterableRest() {
|
|
75
|
+
throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
var mobxDecorators = new Set(['observable', 'computed', 'action', 'flow', 'override']);
|
|
79
|
+
|
|
80
|
+
function isMobxDecorator$2(decorator) {
|
|
81
|
+
var _decorator$expression, _decorator$expression2;
|
|
82
|
+
|
|
83
|
+
return mobxDecorators.has(decorator.expression.name) // @foo
|
|
84
|
+
|| mobxDecorators.has((_decorator$expression = decorator.expression.callee) === null || _decorator$expression === void 0 ? void 0 : _decorator$expression.name) // @foo()
|
|
85
|
+
|| mobxDecorators.has((_decorator$expression2 = decorator.expression.object) === null || _decorator$expression2 === void 0 ? void 0 : _decorator$expression2.name); // @foo.bar
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function findAncestor$3(node, match) {
|
|
89
|
+
var parent = node.parent;
|
|
90
|
+
if (!parent) return;
|
|
91
|
+
if (match(parent)) return parent;
|
|
92
|
+
return findAncestor$3(parent, match);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
var utils = {
|
|
96
|
+
findAncestor: findAncestor$3,
|
|
97
|
+
isMobxDecorator: isMobxDecorator$2
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
var findAncestor$2 = utils.findAncestor,
|
|
101
|
+
isMobxDecorator$1 = utils.isMobxDecorator; // TODO support this.foo = 5; in constructor
|
|
102
|
+
// TODO? report on field as well
|
|
103
|
+
|
|
104
|
+
function create$4(context) {
|
|
105
|
+
var sourceCode = context.getSourceCode();
|
|
106
|
+
|
|
107
|
+
function fieldToKey(field) {
|
|
108
|
+
// TODO cache on field?
|
|
109
|
+
var key = sourceCode.getText(field.key);
|
|
110
|
+
return field.computed ? "[".concat(key, "]") : key;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
return {
|
|
114
|
+
'CallExpression[callee.name="makeObservable"]': function CallExpressionCalleeNameMakeObservable(makeObservable) {
|
|
115
|
+
// Only interested about makeObservable(this, ...) in constructor or makeObservable({}, ...)
|
|
116
|
+
// ClassDeclaration
|
|
117
|
+
// ClassBody
|
|
118
|
+
// MethodDefinition[kind="constructor"]
|
|
119
|
+
// FunctionExpression
|
|
120
|
+
// BlockStatement
|
|
121
|
+
// ExpressionStatement
|
|
122
|
+
// CallExpression[callee.name="makeObservable"]
|
|
123
|
+
var _makeObservable$argum = _slicedToArray(makeObservable.arguments, 2),
|
|
124
|
+
firstArg = _makeObservable$argum[0],
|
|
125
|
+
secondArg = _makeObservable$argum[1];
|
|
126
|
+
|
|
127
|
+
if (!firstArg) return;
|
|
128
|
+
var members;
|
|
129
|
+
|
|
130
|
+
if (firstArg.type === 'ThisExpression') {
|
|
131
|
+
var _closestFunction$pare;
|
|
132
|
+
|
|
133
|
+
var closestFunction = findAncestor$2(makeObservable, function (node) {
|
|
134
|
+
return node.type === 'FunctionExpression' || node.type === 'FunctionDeclaration';
|
|
135
|
+
});
|
|
136
|
+
if ((closestFunction === null || closestFunction === void 0 ? void 0 : (_closestFunction$pare = closestFunction.parent) === null || _closestFunction$pare === void 0 ? void 0 : _closestFunction$pare.kind) !== 'constructor') return;
|
|
137
|
+
members = closestFunction.parent.parent.parent.body.body;
|
|
138
|
+
} else if (firstArg.type === 'ObjectExpression') {
|
|
139
|
+
members = firstArg.properties;
|
|
140
|
+
} else {
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
var annotationProps = (secondArg === null || secondArg === void 0 ? void 0 : secondArg.properties) || [];
|
|
145
|
+
var nonAnnotatedMembers = [];
|
|
146
|
+
var hasAnyDecorator = false;
|
|
147
|
+
members.forEach(function (member) {
|
|
148
|
+
var _member$decorators;
|
|
149
|
+
|
|
150
|
+
if (member["static"]) return;
|
|
151
|
+
if (member.kind === "constructor") return; //if (member.type !== 'MethodDefinition' && member.type !== 'ClassProperty') return;
|
|
152
|
+
|
|
153
|
+
hasAnyDecorator = hasAnyDecorator || ((_member$decorators = member.decorators) === null || _member$decorators === void 0 ? void 0 : _member$decorators.some(isMobxDecorator$1)) || false;
|
|
154
|
+
|
|
155
|
+
if (!annotationProps.some(function (prop) {
|
|
156
|
+
return fieldToKey(prop) === fieldToKey(member);
|
|
157
|
+
})) {
|
|
158
|
+
// TODO optimize?
|
|
159
|
+
nonAnnotatedMembers.push(member);
|
|
160
|
+
}
|
|
161
|
+
});
|
|
162
|
+
/*
|
|
163
|
+
// With decorators, second arg must be null/undefined or not provided
|
|
164
|
+
if (hasAnyDecorator && secondArg && secondArg.name !== "undefined" && secondArg.value !== null) {
|
|
165
|
+
context.report({
|
|
166
|
+
node: makeObservable,
|
|
167
|
+
message: 'When using decorators, second arg must be `null`, `undefined` or not provided.',
|
|
168
|
+
})
|
|
169
|
+
}
|
|
170
|
+
// Without decorators, in constructor, second arg must be object literal
|
|
171
|
+
if (!hasAnyDecorator && firstArg.type === 'ThisExpression' && (!secondArg || secondArg.type !== 'ObjectExpression')) {
|
|
172
|
+
context.report({
|
|
173
|
+
node: makeObservable,
|
|
174
|
+
message: 'Second argument must be object in form of `{ key: annotation }`.',
|
|
175
|
+
})
|
|
176
|
+
}
|
|
177
|
+
*/
|
|
178
|
+
|
|
179
|
+
if (!hasAnyDecorator && nonAnnotatedMembers.length) {
|
|
180
|
+
// Set avoids reporting twice for setter+getter pair or actual duplicates
|
|
181
|
+
var keys = _toConsumableArray(new Set(nonAnnotatedMembers.map(fieldToKey)));
|
|
182
|
+
|
|
183
|
+
var keyList = keys.map(function (key) {
|
|
184
|
+
return "`".concat(key, "`");
|
|
185
|
+
}).join(', ');
|
|
186
|
+
|
|
187
|
+
var fix = function fix(fixer) {
|
|
188
|
+
var annotationList = keys.map(function (key) {
|
|
189
|
+
return "".concat(key, ": true");
|
|
190
|
+
}).join(', ') + ',';
|
|
191
|
+
|
|
192
|
+
if (!secondArg) {
|
|
193
|
+
return fixer.insertTextAfter(firstArg, ", { ".concat(annotationList, " }"));
|
|
194
|
+
} else if (secondArg.type !== 'ObjectExpression') {
|
|
195
|
+
return fixer.replaceText(secondArg, "{ ".concat(annotationList, " }"));
|
|
196
|
+
} else {
|
|
197
|
+
var openingBracket = sourceCode.getFirstToken(secondArg);
|
|
198
|
+
return fixer.insertTextAfter(openingBracket, " ".concat(annotationList, " "));
|
|
199
|
+
}
|
|
200
|
+
};
|
|
201
|
+
|
|
202
|
+
context.report({
|
|
203
|
+
node: makeObservable,
|
|
204
|
+
messageId: 'missingAnnotation',
|
|
205
|
+
data: {
|
|
206
|
+
keyList: keyList
|
|
207
|
+
},
|
|
208
|
+
fix: fix
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
var exhaustiveMakeObservable$1 = {
|
|
216
|
+
meta: {
|
|
217
|
+
type: 'suggestion',
|
|
218
|
+
fixable: 'code',
|
|
219
|
+
docs: {
|
|
220
|
+
description: 'enforce all fields being listen in `makeObservable`',
|
|
221
|
+
recommended: true,
|
|
222
|
+
suggestion: false
|
|
223
|
+
},
|
|
224
|
+
messages: {
|
|
225
|
+
'missingAnnotation': 'Missing annotation for {{ keyList }}. To exclude a field, use `false` as annotation.'
|
|
226
|
+
}
|
|
227
|
+
},
|
|
228
|
+
create: create$4
|
|
229
|
+
};
|
|
230
|
+
|
|
231
|
+
var findAncestor$1 = utils.findAncestor;
|
|
232
|
+
|
|
233
|
+
function create$3(context) {
|
|
234
|
+
return {
|
|
235
|
+
'CallExpression[callee.name=/(makeObservable|makeAutoObservable)/]': function CallExpressionCalleeNameMakeObservableMakeAutoObservable(makeObservable) {
|
|
236
|
+
var _closestFunction$pare;
|
|
237
|
+
|
|
238
|
+
// Only iterested about makeObservable(this, ...) inside constructor and not inside nested bindable function
|
|
239
|
+
var _makeObservable$argum = _slicedToArray(makeObservable.arguments, 1),
|
|
240
|
+
firstArg = _makeObservable$argum[0];
|
|
241
|
+
|
|
242
|
+
if (!firstArg) return;
|
|
243
|
+
if (firstArg.type !== 'ThisExpression') return; // MethodDefinition[key.name="constructor"][kind="constructor"]
|
|
244
|
+
// FunctionExpression
|
|
245
|
+
// BlockStatement
|
|
246
|
+
// ExpressionStatement
|
|
247
|
+
// CallExpression[callee.name="makeObservable"]
|
|
248
|
+
|
|
249
|
+
var closestFunction = findAncestor$1(makeObservable, function (node) {
|
|
250
|
+
return node.type === 'FunctionExpression' || node.type === 'FunctionDeclaration';
|
|
251
|
+
});
|
|
252
|
+
if ((closestFunction === null || closestFunction === void 0 ? void 0 : (_closestFunction$pare = closestFunction.parent) === null || _closestFunction$pare === void 0 ? void 0 : _closestFunction$pare.kind) !== 'constructor') return;
|
|
253
|
+
|
|
254
|
+
if (makeObservable.parent.parent.parent !== closestFunction) {
|
|
255
|
+
context.report({
|
|
256
|
+
node: makeObservable,
|
|
257
|
+
messageId: 'mustCallUnconditionally',
|
|
258
|
+
data: {
|
|
259
|
+
name: makeObservable.callee.name
|
|
260
|
+
}
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
};
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
var unconditionalMakeObservable$1 = {
|
|
268
|
+
meta: {
|
|
269
|
+
type: 'problem',
|
|
270
|
+
docs: {
|
|
271
|
+
description: 'disallows calling `makeObservable(this)` conditionally inside constructors',
|
|
272
|
+
recommended: true
|
|
273
|
+
},
|
|
274
|
+
messages: {
|
|
275
|
+
mustCallUnconditionally: '`{{ name }}` must be called unconditionally inside constructor.'
|
|
276
|
+
}
|
|
277
|
+
},
|
|
278
|
+
create: create$3
|
|
279
|
+
};
|
|
280
|
+
|
|
281
|
+
var findAncestor = utils.findAncestor,
|
|
282
|
+
isMobxDecorator = utils.isMobxDecorator;
|
|
283
|
+
|
|
284
|
+
function create$2(context) {
|
|
285
|
+
var sourceCode = context.getSourceCode();
|
|
286
|
+
return {
|
|
287
|
+
'Decorator': function Decorator(decorator) {
|
|
288
|
+
var _constructor$value$bo, _constructor$value$bo2;
|
|
289
|
+
|
|
290
|
+
if (!isMobxDecorator(decorator)) return;
|
|
291
|
+
var clazz = findAncestor(decorator, function (node) {
|
|
292
|
+
return node.type === 'ClassDeclaration' || node.type === 'ClassExpression';
|
|
293
|
+
});
|
|
294
|
+
if (!clazz) return; // ClassDeclaration > ClassBody > []
|
|
295
|
+
|
|
296
|
+
var constructor = clazz.body.body.find(function (node) {
|
|
297
|
+
return node.kind === 'constructor';
|
|
298
|
+
}); // MethodDefinition > FunctionExpression > BlockStatement > []
|
|
299
|
+
|
|
300
|
+
var isMakeObservable = function isMakeObservable(node) {
|
|
301
|
+
var _node$expression, _node$expression$call, _node$expression2, _node$expression2$arg;
|
|
302
|
+
|
|
303
|
+
return ((_node$expression = node.expression) === null || _node$expression === void 0 ? void 0 : (_node$expression$call = _node$expression.callee) === null || _node$expression$call === void 0 ? void 0 : _node$expression$call.name) === 'makeObservable' && ((_node$expression2 = node.expression) === null || _node$expression2 === void 0 ? void 0 : (_node$expression2$arg = _node$expression2.arguments[0]) === null || _node$expression2$arg === void 0 ? void 0 : _node$expression2$arg.type) === 'ThisExpression';
|
|
304
|
+
};
|
|
305
|
+
|
|
306
|
+
var makeObservable = constructor === null || constructor === void 0 ? void 0 : (_constructor$value$bo = constructor.value.body) === null || _constructor$value$bo === void 0 ? void 0 : (_constructor$value$bo2 = _constructor$value$bo.body.find(isMakeObservable)) === null || _constructor$value$bo2 === void 0 ? void 0 : _constructor$value$bo2.expression;
|
|
307
|
+
|
|
308
|
+
if (makeObservable) {
|
|
309
|
+
// make sure second arg is nullish
|
|
310
|
+
var secondArg = makeObservable.arguments[1];
|
|
311
|
+
|
|
312
|
+
if (secondArg && secondArg.value !== null && secondArg.name !== 'undefined') {
|
|
313
|
+
context.report({
|
|
314
|
+
node: makeObservable,
|
|
315
|
+
messageId: 'secondArgMustBeNullish'
|
|
316
|
+
});
|
|
317
|
+
}
|
|
318
|
+
} else {
|
|
319
|
+
var fix = function fix(fixer) {
|
|
320
|
+
if ((constructor === null || constructor === void 0 ? void 0 : constructor.value.type) === 'TSEmptyBodyFunctionExpression') {
|
|
321
|
+
// constructor() - yes this a thing
|
|
322
|
+
var closingBracket = sourceCode.getLastToken(constructor.value);
|
|
323
|
+
return fixer.insertTextAfter(closingBracket, ' { makeObservable(this); }');
|
|
324
|
+
} else if (constructor) {
|
|
325
|
+
// constructor() {}
|
|
326
|
+
var _closingBracket = sourceCode.getLastToken(constructor.value.body);
|
|
327
|
+
|
|
328
|
+
return fixer.insertTextBefore(_closingBracket, ';makeObservable(this);');
|
|
329
|
+
} else {
|
|
330
|
+
// class C {}
|
|
331
|
+
var openingBracket = sourceCode.getFirstToken(clazz.body);
|
|
332
|
+
return fixer.insertTextAfter(openingBracket, '\nconstructor() { makeObservable(this); }');
|
|
333
|
+
}
|
|
334
|
+
};
|
|
335
|
+
|
|
336
|
+
context.report({
|
|
337
|
+
node: clazz,
|
|
338
|
+
messageId: 'missingMakeObservable',
|
|
339
|
+
fix: fix
|
|
340
|
+
});
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
};
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
var missingMakeObservable$1 = {
|
|
347
|
+
meta: {
|
|
348
|
+
type: 'problem',
|
|
349
|
+
fixable: 'code',
|
|
350
|
+
docs: {
|
|
351
|
+
description: 'prevents missing `makeObservable(this)` when using decorators',
|
|
352
|
+
recommended: true,
|
|
353
|
+
suggestion: false
|
|
354
|
+
},
|
|
355
|
+
messages: {
|
|
356
|
+
missingMakeObservable: "Constructor is missing `makeObservable(this)`.",
|
|
357
|
+
secondArgMustBeNullish: "`makeObservable`'s second argument must be nullish or not provided when using decorators."
|
|
358
|
+
}
|
|
359
|
+
},
|
|
360
|
+
create: create$2
|
|
361
|
+
};
|
|
362
|
+
|
|
363
|
+
function create$1(context) {
|
|
364
|
+
var sourceCode = context.getSourceCode();
|
|
365
|
+
return {
|
|
366
|
+
'FunctionDeclaration,FunctionExpression,ArrowFunctionExpression,ClassDeclaration,ClassExpression': function FunctionDeclarationFunctionExpressionArrowFunctionExpressionClassDeclarationClassExpression(cmp) {
|
|
367
|
+
var _cmp$id, _cmp$parent;
|
|
368
|
+
|
|
369
|
+
// Already has observer
|
|
370
|
+
if (cmp.parent && cmp.parent.type === 'CallExpression' && cmp.parent.callee.name === 'observer') return;
|
|
371
|
+
var name = (_cmp$id = cmp.id) === null || _cmp$id === void 0 ? void 0 : _cmp$id.name; // If anonymous try to infer name from variable declaration
|
|
372
|
+
|
|
373
|
+
if (!name && ((_cmp$parent = cmp.parent) === null || _cmp$parent === void 0 ? void 0 : _cmp$parent.type) === 'VariableDeclarator') {
|
|
374
|
+
name = cmp.parent.id.name;
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
if (cmp.type.startsWith('Class')) {
|
|
378
|
+
// Must extend Component or React.Component
|
|
379
|
+
var superClass = cmp.superClass;
|
|
380
|
+
if (!superClass) return;
|
|
381
|
+
var superClassText = sourceCode.getText(superClass);
|
|
382
|
+
if (superClassText !== 'Component' && superClassText !== 'React.Component') return;
|
|
383
|
+
} else {
|
|
384
|
+
var _name;
|
|
385
|
+
|
|
386
|
+
// Name must start with uppercase letter
|
|
387
|
+
if (!((_name = name) !== null && _name !== void 0 && _name.charAt(0).match(/^[A-Z]$/))) return;
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
var fix = function fix(fixer) {
|
|
391
|
+
return [fixer.insertTextBefore(sourceCode.getFirstToken(cmp), (name && cmp.type.endsWith('Declaration') ? "const ".concat(name, " = ") : '') + 'observer('), fixer.insertTextAfter(sourceCode.getLastToken(cmp), ')')];
|
|
392
|
+
};
|
|
393
|
+
|
|
394
|
+
context.report({
|
|
395
|
+
node: cmp,
|
|
396
|
+
messageId: 'missingObserver',
|
|
397
|
+
data: {
|
|
398
|
+
name: name || '<anonymous>'
|
|
399
|
+
},
|
|
400
|
+
fix: fix
|
|
401
|
+
});
|
|
402
|
+
}
|
|
403
|
+
};
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
var missingObserver$1 = {
|
|
407
|
+
meta: {
|
|
408
|
+
type: 'problem',
|
|
409
|
+
fixable: 'code',
|
|
410
|
+
docs: {
|
|
411
|
+
description: 'prevents missing `observer` on react component',
|
|
412
|
+
recommended: true
|
|
413
|
+
},
|
|
414
|
+
messages: {
|
|
415
|
+
missingObserver: "Component `{{ name }}` is missing `observer`."
|
|
416
|
+
}
|
|
417
|
+
},
|
|
418
|
+
create: create$1
|
|
419
|
+
};
|
|
420
|
+
|
|
421
|
+
function create(context) {
|
|
422
|
+
var sourceCode = context.getSourceCode();
|
|
423
|
+
return {
|
|
424
|
+
'CallExpression[callee.name="observer"]': function CallExpressionCalleeNameObserver(observer) {
|
|
425
|
+
var _cmp$id;
|
|
426
|
+
|
|
427
|
+
var cmp = observer.arguments[0];
|
|
428
|
+
if (!cmp) return;
|
|
429
|
+
if (cmp !== null && cmp !== void 0 && (_cmp$id = cmp.id) !== null && _cmp$id !== void 0 && _cmp$id.name) return;
|
|
430
|
+
|
|
431
|
+
var fix = function fix(fixer) {
|
|
432
|
+
var _observer$parent;
|
|
433
|
+
|
|
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
|
+
if (!name) return;
|
|
437
|
+
|
|
438
|
+
if (cmp.type === 'ArrowFunctionExpression') {
|
|
439
|
+
var arrowToken = sourceCode.getTokenBefore(cmp.body);
|
|
440
|
+
return [fixer.replaceText(arrowToken, ''), fixer.insertTextBefore(cmp, "function ".concat(name))];
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
if (cmp.type === 'FunctionExpression') {
|
|
444
|
+
var functionToken = sourceCode.getFirstToken(cmp);
|
|
445
|
+
return fixer.replaceText(functionToken, "function ".concat(name));
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
if (cmp.type === 'ClassExpression') {
|
|
449
|
+
var classToken = sourceCode.getFirstToken(cmp);
|
|
450
|
+
return fixer.replaceText(classToken, "class ".concat(name));
|
|
451
|
+
}
|
|
452
|
+
};
|
|
453
|
+
|
|
454
|
+
context.report({
|
|
455
|
+
node: cmp,
|
|
456
|
+
messageId: 'observerComponentMustHaveName',
|
|
457
|
+
fix: fix
|
|
458
|
+
});
|
|
459
|
+
}
|
|
460
|
+
};
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
var noAnonymousObserver$1 = {
|
|
464
|
+
meta: {
|
|
465
|
+
type: 'problem',
|
|
466
|
+
fixable: 'code',
|
|
467
|
+
docs: {
|
|
468
|
+
description: 'forbids anonymous functions or classes as `observer` components',
|
|
469
|
+
recommended: true
|
|
470
|
+
},
|
|
471
|
+
messages: {
|
|
472
|
+
observerComponentMustHaveName: "`observer` component must have a name."
|
|
473
|
+
}
|
|
474
|
+
},
|
|
475
|
+
create: create
|
|
476
|
+
};
|
|
477
|
+
|
|
478
|
+
var exhaustiveMakeObservable = exhaustiveMakeObservable$1;
|
|
479
|
+
var unconditionalMakeObservable = unconditionalMakeObservable$1;
|
|
480
|
+
var missingMakeObservable = missingMakeObservable$1;
|
|
481
|
+
var missingObserver = missingObserver$1;
|
|
482
|
+
var noAnonymousObserver = noAnonymousObserver$1;
|
|
483
|
+
var src = {
|
|
484
|
+
configs: {
|
|
485
|
+
recommended: {
|
|
486
|
+
plugins: ["mobx"],
|
|
487
|
+
rules: {
|
|
488
|
+
"mobx/exhaustive-make-observable": "warn",
|
|
489
|
+
"mobx/unconditional-make-observable": "error",
|
|
490
|
+
"mobx/missing-make-observable": "error",
|
|
491
|
+
"mobx/missing-observer": "warn",
|
|
492
|
+
"mobx/no-anonymous-observer": "warn"
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
},
|
|
496
|
+
rules: {
|
|
497
|
+
"exhaustive-make-observable": exhaustiveMakeObservable,
|
|
498
|
+
"unconditional-make-observable": unconditionalMakeObservable,
|
|
499
|
+
"missing-make-observable": missingMakeObservable,
|
|
500
|
+
"missing-observer": missingObserver,
|
|
501
|
+
"no-anonymous-observer": noAnonymousObserver
|
|
502
|
+
}
|
|
503
|
+
};
|
|
504
|
+
|
|
505
|
+
module.exports = src;
|
package/package.json
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "eslint-plugin-mobx",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.6",
|
|
4
4
|
"description": "ESLint rules for MobX",
|
|
5
|
+
"main": "dist/index.js",
|
|
5
6
|
"repository": {
|
|
6
7
|
"type": "git",
|
|
7
8
|
"url": "https://github.com/mobxjs/mobx.git",
|
|
@@ -17,18 +18,25 @@
|
|
|
17
18
|
},
|
|
18
19
|
"files": [
|
|
19
20
|
"src",
|
|
21
|
+
"dist",
|
|
20
22
|
"LICENSE",
|
|
23
|
+
"CHANGELOG.md",
|
|
21
24
|
"README.md"
|
|
22
25
|
],
|
|
23
26
|
"homepage": "https://mobx.js.org/",
|
|
24
|
-
"dependencies": {},
|
|
25
27
|
"peerDependencies": {
|
|
26
|
-
"eslint": "^3.0.0 || ^4.0.0 || ^5.0.0 || ^6.0.0 || ^7.0.0"
|
|
28
|
+
"eslint": "^3.0.0 || ^4.0.0 || ^5.0.0 || ^6.0.0 || ^7.0.0 || ^8.0.0"
|
|
27
29
|
},
|
|
28
30
|
"devDependencies": {
|
|
29
|
-
"eslint": "^
|
|
31
|
+
"@typescript-eslint/eslint-plugin": "^4.29.3",
|
|
30
32
|
"@typescript-eslint/parser": "^4.0.0",
|
|
31
|
-
"
|
|
33
|
+
"eslint": "^7.0.0",
|
|
34
|
+
"@babel/core": "^7.16.0",
|
|
35
|
+
"@babel/preset-env": "^7.16.4",
|
|
36
|
+
"rollup": "^2.60.2",
|
|
37
|
+
"@rollup/plugin-babel": "^5.3.0",
|
|
38
|
+
"@rollup/plugin-commonjs": "^21.0.1",
|
|
39
|
+
"@rollup/plugin-node-resolve": "13.0.6"
|
|
32
40
|
},
|
|
33
41
|
"keywords": [
|
|
34
42
|
"eslint",
|
|
@@ -38,7 +46,7 @@
|
|
|
38
46
|
],
|
|
39
47
|
"scripts": {
|
|
40
48
|
"test": "jest",
|
|
41
|
-
"build": "
|
|
42
|
-
"prepublish": "yarn build
|
|
49
|
+
"build": "yarn rollup --config",
|
|
50
|
+
"prepublish": "yarn build"
|
|
43
51
|
}
|
|
44
52
|
}
|
package/src/index.js
CHANGED
|
@@ -1,23 +1,29 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict"
|
|
2
2
|
|
|
3
|
-
const exhaustiveMakeObservable = require(
|
|
4
|
-
const unconditionalMakeObservable = require(
|
|
5
|
-
const missingMakeObservable = require(
|
|
3
|
+
const exhaustiveMakeObservable = require("./exhaustive-make-observable.js")
|
|
4
|
+
const unconditionalMakeObservable = require("./unconditional-make-observable.js")
|
|
5
|
+
const missingMakeObservable = require("./missing-make-observable.js")
|
|
6
|
+
const missingObserver = require("./missing-observer")
|
|
7
|
+
const noAnonymousObserver = require("./no-anonymous-observer.js")
|
|
6
8
|
|
|
7
9
|
module.exports = {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
10
|
+
configs: {
|
|
11
|
+
recommended: {
|
|
12
|
+
plugins: ["mobx"],
|
|
13
|
+
rules: {
|
|
14
|
+
"mobx/exhaustive-make-observable": "warn",
|
|
15
|
+
"mobx/unconditional-make-observable": "error",
|
|
16
|
+
"mobx/missing-make-observable": "error",
|
|
17
|
+
"mobx/missing-observer": "warn",
|
|
18
|
+
"mobx/no-anonymous-observer": "warn"
|
|
19
|
+
}
|
|
20
|
+
}
|
|
16
21
|
},
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
}
|
|
22
|
+
rules: {
|
|
23
|
+
"exhaustive-make-observable": exhaustiveMakeObservable,
|
|
24
|
+
"unconditional-make-observable": unconditionalMakeObservable,
|
|
25
|
+
"missing-make-observable": missingMakeObservable,
|
|
26
|
+
"missing-observer": missingObserver,
|
|
27
|
+
"no-anonymous-observer": noAnonymousObserver
|
|
28
|
+
}
|
|
29
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
function create(context) {
|
|
4
|
+
const sourceCode = context.getSourceCode();
|
|
5
|
+
|
|
6
|
+
return {
|
|
7
|
+
'FunctionDeclaration,FunctionExpression,ArrowFunctionExpression,ClassDeclaration,ClassExpression': cmp => {
|
|
8
|
+
// Already has observer
|
|
9
|
+
if (cmp.parent && cmp.parent.type === 'CallExpression' && cmp.parent.callee.name === 'observer') return;
|
|
10
|
+
let name = cmp.id?.name;
|
|
11
|
+
// If anonymous try to infer name from variable declaration
|
|
12
|
+
if (!name && cmp.parent?.type === 'VariableDeclarator') {
|
|
13
|
+
name = cmp.parent.id.name;
|
|
14
|
+
}
|
|
15
|
+
if (cmp.type.startsWith('Class')) {
|
|
16
|
+
// Must extend Component or React.Component
|
|
17
|
+
const { superClass } = cmp;
|
|
18
|
+
if (!superClass) return;
|
|
19
|
+
const superClassText = sourceCode.getText(superClass);
|
|
20
|
+
if (superClassText !== 'Component' && superClassText !== 'React.Component') return;
|
|
21
|
+
} else {
|
|
22
|
+
// Name must start with uppercase letter
|
|
23
|
+
if (!name?.charAt(0).match(/^[A-Z]$/)) return;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const fix = fixer => {
|
|
27
|
+
return [
|
|
28
|
+
fixer.insertTextBefore(
|
|
29
|
+
sourceCode.getFirstToken(cmp),
|
|
30
|
+
(name && cmp.type.endsWith('Declaration') ? `const ${name} = ` : '') + 'observer(',
|
|
31
|
+
),
|
|
32
|
+
fixer.insertTextAfter(
|
|
33
|
+
sourceCode.getLastToken(cmp),
|
|
34
|
+
')',
|
|
35
|
+
),
|
|
36
|
+
]
|
|
37
|
+
}
|
|
38
|
+
context.report({
|
|
39
|
+
node: cmp,
|
|
40
|
+
messageId: 'missingObserver',
|
|
41
|
+
data: {
|
|
42
|
+
name: name || '<anonymous>',
|
|
43
|
+
},
|
|
44
|
+
fix,
|
|
45
|
+
})
|
|
46
|
+
},
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
module.exports = {
|
|
51
|
+
meta: {
|
|
52
|
+
type: 'problem',
|
|
53
|
+
fixable: 'code',
|
|
54
|
+
docs: {
|
|
55
|
+
description: 'prevents missing `observer` on react component',
|
|
56
|
+
recommended: true,
|
|
57
|
+
},
|
|
58
|
+
messages: {
|
|
59
|
+
missingObserver: "Component `{{ name }}` is missing `observer`.",
|
|
60
|
+
},
|
|
61
|
+
},
|
|
62
|
+
create,
|
|
63
|
+
};
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
function create(context) {
|
|
4
|
+
const sourceCode = context.getSourceCode();
|
|
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;
|
|
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;
|
|
17
|
+
|
|
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
|
+
]
|
|
25
|
+
}
|
|
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
|
+
};
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
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.",
|
|
54
|
+
},
|
|
55
|
+
},
|
|
56
|
+
create,
|
|
57
|
+
};
|
package/src/index.ts
DELETED