eslint-plugin-mobx 0.0.0 → 0.0.5
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 +20 -0
- package/LICENSE +21 -0
- package/README.md +67 -33
- package/dist/index.js +505 -0
- package/package.json +50 -32
- package/src/exhaustive-make-observable.js +111 -0
- package/src/index.js +29 -0
- package/src/missing-make-observable.js +70 -0
- package/src/missing-observer.js +63 -0
- package/src/no-anonymous-observer.js +57 -0
- package/src/unconditional-make-observable.js +44 -0
- package/src/utils.js +29 -0
- package/.editorconfig +0 -15
- package/.eslintrc.js +0 -35
- package/husky.config.js +0 -5
- package/lint-staged.config.js +0 -4
- package/package-lock.json +0 -1293
- package/prettier.config.js +0 -7
- package/todo-rules.md +0 -115
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# eslint-plugin-mobx
|
|
2
|
+
|
|
3
|
+
## 0.0.5
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [`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),
|
|
8
|
+
[`mobx/no-anonymous-observer`](https://github.com/mobxjs/mobx/tree/main/packages/eslint-plugin-mobx#mobxno-anonymous-observer) rules,
|
|
9
|
+
|
|
10
|
+
## 0.0.4
|
|
11
|
+
|
|
12
|
+
### Patch Changes
|
|
13
|
+
|
|
14
|
+
- [`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
|
|
15
|
+
|
|
16
|
+
## 0.0.3
|
|
17
|
+
|
|
18
|
+
### Patch Changes
|
|
19
|
+
|
|
20
|
+
- [`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/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2015 Michel Weststrate
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,54 +1,88 @@
|
|
|
1
1
|
# eslint-plugin-mobx
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Mobx specific linting rules for `eslint`.
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
7
|
-
You'll first need to install [ESLint](http://eslint.org):
|
|
8
|
-
|
|
9
7
|
```
|
|
10
|
-
|
|
8
|
+
npm install --save-dev eslint @typescript-eslint/parser eslint-plugin-mobx
|
|
11
9
|
```
|
|
12
10
|
|
|
13
|
-
|
|
11
|
+
## Configuration
|
|
14
12
|
|
|
15
|
-
```
|
|
16
|
-
|
|
13
|
+
```javascript
|
|
14
|
+
// .eslintrc.js
|
|
15
|
+
module.exports = {
|
|
16
|
+
parser: "@typescript-eslint/parser",
|
|
17
|
+
// Include "mobx" in plugins array:
|
|
18
|
+
plugins: ["mobx"],
|
|
19
|
+
// Either extend our recommended configuration:
|
|
20
|
+
extends: "plugin:mobx/recommended",
|
|
21
|
+
// ...or specify and customize individual rules:
|
|
22
|
+
rules: {
|
|
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/no-missing-observer': 'warn',
|
|
28
|
+
'mobx/no-anonymous-observer': 'warn',
|
|
29
|
+
},
|
|
30
|
+
};
|
|
17
31
|
```
|
|
18
32
|
|
|
19
|
-
|
|
33
|
+
## Rules
|
|
20
34
|
|
|
21
|
-
|
|
35
|
+
### mobx/exhaustive-make-observable
|
|
22
36
|
|
|
23
|
-
|
|
37
|
+
Makes sure that `makeObservable` annotates all fields defined on class or object literal.<br>
|
|
38
|
+
**Autofix** adds `field: true` for each missing field.<br>
|
|
39
|
+
To exclude a field, annotate it using `field: false`.<br>
|
|
40
|
+
Does not support fields introduced by constructor (`this.foo = 5`).<br>
|
|
41
|
+
Does not warn about annotated non-existing fields (there is a runtime check, but the autofix removing the field could be handy...).
|
|
24
42
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
```
|
|
43
|
+
### mobx/missing-make-observable
|
|
44
|
+
|
|
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.
|
|
30
47
|
|
|
31
|
-
|
|
48
|
+
### mobx/unconditional-make-observable
|
|
32
49
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
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
|
+
```javascript
|
|
56
|
+
class Cmp extends React.Component { }
|
|
57
|
+
class Cmp extends Component { }
|
|
58
|
+
const Cmp = class extends React.Component { }
|
|
59
|
+
const Cmp = class extends Component { }
|
|
60
|
+
class extends Component { }
|
|
61
|
+
class extends React.Component { }
|
|
62
|
+
|
|
63
|
+
function Named() { }
|
|
64
|
+
const foo = function Named() { }
|
|
65
|
+
const Anonym = function () { };
|
|
66
|
+
const Arrow = () => { };
|
|
67
|
+
```
|
|
68
|
+
**Autofix** wraps the component with `observer` and if necessary declares a constant of the same name: `const Name = observer(function Name() {})`.
|
|
69
|
+
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:
|
|
70
|
+
```javascript
|
|
71
|
+
// .eslintrc.js
|
|
72
|
+
"overrides": [
|
|
73
|
+
{
|
|
74
|
+
"files": ["*.jsx"],
|
|
75
|
+
"rules": {
|
|
76
|
+
"mobx/missing-observer": "error"
|
|
77
|
+
}
|
|
37
78
|
}
|
|
38
|
-
|
|
79
|
+
]
|
|
39
80
|
```
|
|
40
81
|
|
|
41
|
-
|
|
82
|
+
### mobx/no-anonymous-observer
|
|
42
83
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
84
|
+
Forbids anonymous functions or classes as `observer` components.
|
|
85
|
+
Improves debugging experience and [avoids problem with inability to customize `displayName`](https://github.com/mobxjs/mobx/issues/2721).
|
|
86
|
+
Plays nice with `eslint-plugin-react-hooks` and `mobx/missing-observer` as both of these don't not recognize anonymous function as component.
|
|
87
|
+
**Autofix** infers the name from variable if possible.
|
|
46
88
|
|
|
47
|
-
| rule name | description | 👌 | 👍 | 🤘 |
|
|
48
|
-
| ---------------------------- | --------------------------------------------------------- | --- | --- | --- |
|
|
49
|
-
| action-name | Enforces name in action `action('name', () => {})` | | | |
|
|
50
|
-
| prefer-flow | Suggest using flow instead of action | | | |
|
|
51
|
-
| enforce-actions | Enforces rules same as mobx configure option | | | |
|
|
52
|
-
| no-to-js | Disallows use of toJS function | | | |
|
|
53
|
-
| reaction-requires-observable | Warns if reaction is created without any observable acess | | | |
|
|
54
|
-
| observable-requires-reaction | Warns about any unbserved observable access | | | |
|
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/no-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;
|