eslint-plugin-mobx 0.0.14 → 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/CHANGELOG.md +6 -0
- package/dist/exhaustive-make-observable.d.ts +3 -0
- package/dist/exhaustive-make-observable.js +154 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +51 -549
- package/dist/missing-make-observable.d.ts +3 -0
- package/dist/missing-make-observable.js +72 -0
- package/dist/missing-observer.d.ts +3 -0
- package/dist/missing-observer.js +85 -0
- package/dist/no-anonymous-observer.d.ts +3 -0
- package/dist/no-anonymous-observer.js +64 -0
- package/dist/unconditional-make-observable.d.ts +3 -0
- package/dist/unconditional-make-observable.js +63 -0
- package/dist/utils.d.ts +3 -0
- package/dist/utils.js +27 -0
- package/package.json +12 -13
- package/src/{exhaustive-make-observable.js → exhaustive-make-observable.ts} +7 -6
- package/src/{index.js → index.ts} +16 -11
- package/src/{missing-make-observable.js → missing-make-observable.ts} +6 -5
- package/src/{missing-observer.js → missing-observer.ts} +4 -2
- package/src/{no-anonymous-observer.js → no-anonymous-observer.ts} +4 -2
- package/src/{unconditional-make-observable.js → unconditional-make-observable.ts} +7 -5
- package/src/{utils.js → utils.ts} +3 -10
package/dist/index.js
CHANGED
|
@@ -1,553 +1,55 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
var
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
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$sourceCode, _context$options$0$au, _context$options$;
|
|
140
|
-
var sourceCode = (_context$sourceCode = context.sourceCode) !== null && _context$sourceCode !== void 0 ? _context$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 _context$sourceCode;
|
|
312
|
-
var sourceCode = (_context$sourceCode = context.sourceCode) !== null && _context$sourceCode !== void 0 ? _context$sourceCode : context.getSourceCode();
|
|
313
|
-
return {
|
|
314
|
-
'Decorator': function Decorator(decorator) {
|
|
315
|
-
var _clazz$body$body$find, _constructor$value$bo;
|
|
316
|
-
if (!isMobxDecorator(decorator)) return;
|
|
317
|
-
var clazz = findAncestor(decorator, function (node) {
|
|
318
|
-
return node.type === 'ClassDeclaration' || node.type === 'ClassExpression';
|
|
319
|
-
});
|
|
320
|
-
if (!clazz) return;
|
|
321
|
-
// ClassDeclaration > ClassBody > []
|
|
322
|
-
var constructor = (_clazz$body$body$find = clazz.body.body.find(function (node) {
|
|
323
|
-
return node.kind === 'constructor' && node.value.type === 'FunctionExpression';
|
|
324
|
-
})) !== null && _clazz$body$body$find !== void 0 ? _clazz$body$body$find : clazz.body.body.find(function (node) {
|
|
325
|
-
return node.kind === 'constructor';
|
|
326
|
-
});
|
|
327
|
-
// MethodDefinition > FunctionExpression > BlockStatement > []
|
|
328
|
-
var isMakeObservable = function isMakeObservable(node) {
|
|
329
|
-
var _node$expression, _node$expression2;
|
|
330
|
-
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';
|
|
331
|
-
};
|
|
332
|
-
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;
|
|
333
|
-
if (makeObservable) {
|
|
334
|
-
// make sure second arg is nullish
|
|
335
|
-
var secondArg = makeObservable.arguments[1];
|
|
336
|
-
if (secondArg && secondArg.value !== null && secondArg.name !== 'undefined') {
|
|
337
|
-
context.report({
|
|
338
|
-
node: makeObservable,
|
|
339
|
-
messageId: 'secondArgMustBeNullish'
|
|
340
|
-
});
|
|
341
|
-
}
|
|
342
|
-
} else {
|
|
343
|
-
var fix = function fix(fixer) {
|
|
344
|
-
if ((constructor === null || constructor === void 0 ? void 0 : constructor.value.type) === 'TSEmptyBodyFunctionExpression') {
|
|
345
|
-
// constructor() - yes this a thing
|
|
346
|
-
var closingBracket = sourceCode.getLastToken(constructor.value);
|
|
347
|
-
return fixer.insertTextAfter(closingBracket, ' { makeObservable(this); }');
|
|
348
|
-
} else if (constructor) {
|
|
349
|
-
// constructor() {}
|
|
350
|
-
var _closingBracket = sourceCode.getLastToken(constructor.value.body);
|
|
351
|
-
return fixer.insertTextBefore(_closingBracket, ';makeObservable(this);');
|
|
352
|
-
} else {
|
|
353
|
-
// class C {}
|
|
354
|
-
var openingBracket = sourceCode.getFirstToken(clazz.body);
|
|
355
|
-
return fixer.insertTextAfter(openingBracket, '\nconstructor() { makeObservable(this); }');
|
|
356
|
-
}
|
|
357
|
-
};
|
|
358
|
-
context.report({
|
|
359
|
-
node: clazz,
|
|
360
|
-
messageId: 'missingMakeObservable',
|
|
361
|
-
fix: fix
|
|
362
|
-
});
|
|
363
|
-
}
|
|
364
|
-
}
|
|
365
|
-
};
|
|
366
|
-
}
|
|
367
|
-
var missingMakeObservable$1 = {
|
|
368
|
-
meta: {
|
|
369
|
-
type: 'problem',
|
|
370
|
-
fixable: 'code',
|
|
371
|
-
docs: {
|
|
372
|
-
description: 'prevents missing `makeObservable(this)` when using decorators',
|
|
373
|
-
recommended: true,
|
|
374
|
-
suggestion: false
|
|
375
|
-
},
|
|
376
|
-
messages: {
|
|
377
|
-
missingMakeObservable: "Constructor is missing `makeObservable(this)`.",
|
|
378
|
-
secondArgMustBeNullish: "`makeObservable`'s second argument must be nullish or not provided when using decorators."
|
|
379
|
-
}
|
|
380
|
-
},
|
|
381
|
-
create: create$2
|
|
382
|
-
};
|
|
383
|
-
|
|
384
|
-
function create$1(context) {
|
|
385
|
-
var _context$sourceCode;
|
|
386
|
-
var sourceCode = (_context$sourceCode = context.sourceCode) !== null && _context$sourceCode !== void 0 ? _context$sourceCode : context.getSourceCode();
|
|
387
|
-
return {
|
|
388
|
-
"FunctionDeclaration,FunctionExpression,ArrowFunctionExpression,ClassDeclaration,ClassExpression": function FunctionDeclarationFunctionExpressionArrowFunctionExpressionClassDeclarationClassExpression(cmp) {
|
|
389
|
-
var _cmp$id, _cmpOrForwardRef$pare;
|
|
390
|
-
if (cmp.parent && cmp.parent.type === "CallExpression" && cmp.parent.callee.name === "observer") {
|
|
391
|
-
// observer(...)
|
|
392
|
-
return;
|
|
393
|
-
}
|
|
394
|
-
var forwardRef = cmp.parent && cmp.parent.type === "CallExpression" && cmp.parent.callee.name === "forwardRef" ? cmp.parent : undefined;
|
|
395
|
-
if (forwardRef && forwardRef.parent && forwardRef.parent.type === "CallExpression" && forwardRef.parent.callee.name === "observer") {
|
|
396
|
-
// forwardRef(observer(...))
|
|
397
|
-
return;
|
|
398
|
-
}
|
|
399
|
-
var cmpOrForwardRef = forwardRef || cmp;
|
|
400
|
-
var name = (_cmp$id = cmp.id) === null || _cmp$id === void 0 ? void 0 : _cmp$id.name;
|
|
401
|
-
// If anonymous try to infer name from variable declaration
|
|
402
|
-
if (!name && ((_cmpOrForwardRef$pare = cmpOrForwardRef.parent) === null || _cmpOrForwardRef$pare === void 0 ? void 0 : _cmpOrForwardRef$pare.type) === "VariableDeclarator") {
|
|
403
|
-
name = cmpOrForwardRef.parent.id.name;
|
|
404
|
-
}
|
|
405
|
-
if (cmp.type.startsWith("Class")) {
|
|
406
|
-
// Must extend Component or React.Component
|
|
407
|
-
var superClass = cmp.superClass;
|
|
408
|
-
if (!superClass) {
|
|
409
|
-
// not a component
|
|
410
|
-
return;
|
|
411
|
-
}
|
|
412
|
-
var superClassText = sourceCode.getText(superClass);
|
|
413
|
-
if (superClassText !== "Component" && superClassText !== "React.Component") {
|
|
414
|
-
// not a component
|
|
415
|
-
return;
|
|
416
|
-
}
|
|
417
|
-
} else {
|
|
418
|
-
var _name;
|
|
419
|
-
// Name must start with uppercase letter
|
|
420
|
-
if (!((_name = name) !== null && _name !== void 0 && _name.charAt(0).match(/^[A-Z]$/))) {
|
|
421
|
-
// not a component
|
|
422
|
-
return;
|
|
423
|
-
}
|
|
424
|
-
}
|
|
425
|
-
var fix = function fix(fixer) {
|
|
426
|
-
return [fixer.insertTextBefore(sourceCode.getFirstToken(cmpOrForwardRef), (name && cmp.type.endsWith("Declaration") ? "const ".concat(name, " = ") : "") + "observer("), fixer.insertTextAfter(sourceCode.getLastToken(cmpOrForwardRef), ")")];
|
|
427
|
-
};
|
|
428
|
-
context.report({
|
|
429
|
-
node: cmp,
|
|
430
|
-
messageId: "missingObserver",
|
|
431
|
-
data: {
|
|
432
|
-
name: name || "<anonymous>"
|
|
433
|
-
},
|
|
434
|
-
fix: fix
|
|
435
|
-
});
|
|
436
|
-
}
|
|
437
|
-
};
|
|
438
|
-
}
|
|
439
|
-
var missingObserver$1 = {
|
|
440
|
-
meta: {
|
|
441
|
-
type: "problem",
|
|
442
|
-
fixable: "code",
|
|
443
|
-
docs: {
|
|
444
|
-
description: "prevents missing `observer` on react component",
|
|
445
|
-
recommended: true
|
|
446
|
-
},
|
|
447
|
-
messages: {
|
|
448
|
-
missingObserver: "Component `{{ name }}` is missing `observer`."
|
|
449
|
-
}
|
|
450
|
-
},
|
|
451
|
-
create: create$1
|
|
452
|
-
};
|
|
453
|
-
|
|
454
|
-
function create(context) {
|
|
455
|
-
var _context$sourceCode;
|
|
456
|
-
var sourceCode = (_context$sourceCode = context.sourceCode) !== null && _context$sourceCode !== void 0 ? _context$sourceCode : context.getSourceCode();
|
|
457
|
-
return {
|
|
458
|
-
'CallExpression[callee.name="observer"]': function CallExpressionCalleeNameObserver(observer) {
|
|
459
|
-
var _cmp$id;
|
|
460
|
-
var cmp = observer.arguments[0];
|
|
461
|
-
if (!cmp) return;
|
|
462
|
-
if (cmp !== null && cmp !== void 0 && (_cmp$id = cmp.id) !== null && _cmp$id !== void 0 && _cmp$id.name) return;
|
|
463
|
-
var fix = function fix(fixer) {
|
|
464
|
-
var _observer$parent;
|
|
465
|
-
// Use name from variable for autofix
|
|
466
|
-
var name = ((_observer$parent = observer.parent) === null || _observer$parent === void 0 ? void 0 : _observer$parent.type) === "VariableDeclarator" ? observer.parent.id.name : undefined;
|
|
467
|
-
if (!name) return;
|
|
468
|
-
if (cmp.type === "ArrowFunctionExpression") {
|
|
469
|
-
var arrowToken = sourceCode.getTokenBefore(cmp.body);
|
|
470
|
-
var fixes = [fixer.replaceText(arrowToken, ""), fixer.insertTextBefore(cmp, "function ".concat(name))];
|
|
471
|
-
if (cmp.body.type !== "BlockStatement") {
|
|
472
|
-
fixes.push(fixer.insertTextBefore(cmp.body, "{ return "), fixer.insertTextAfter(cmp.body, " }"));
|
|
473
|
-
}
|
|
474
|
-
return fixes;
|
|
475
|
-
}
|
|
476
|
-
if (cmp.type === "FunctionExpression") {
|
|
477
|
-
var functionToken = sourceCode.getFirstToken(cmp);
|
|
478
|
-
return fixer.replaceText(functionToken, "function ".concat(name));
|
|
479
|
-
}
|
|
480
|
-
if (cmp.type === "ClassExpression") {
|
|
481
|
-
var classToken = sourceCode.getFirstToken(cmp);
|
|
482
|
-
return fixer.replaceText(classToken, "class ".concat(name));
|
|
483
|
-
}
|
|
484
|
-
};
|
|
485
|
-
context.report({
|
|
486
|
-
node: cmp,
|
|
487
|
-
messageId: "observerComponentMustHaveName",
|
|
488
|
-
fix: fix
|
|
489
|
-
});
|
|
490
|
-
}
|
|
491
|
-
};
|
|
492
|
-
}
|
|
493
|
-
var noAnonymousObserver$1 = {
|
|
494
|
-
meta: {
|
|
495
|
-
type: "problem",
|
|
496
|
-
fixable: "code",
|
|
497
|
-
docs: {
|
|
498
|
-
description: "forbids anonymous functions or classes as `observer` components",
|
|
499
|
-
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
|
|
500
28
|
},
|
|
501
|
-
|
|
502
|
-
|
|
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
|
|
503
35
|
}
|
|
504
|
-
},
|
|
505
|
-
create: create
|
|
506
|
-
};
|
|
507
|
-
|
|
508
|
-
var fs = require$$0__default["default"];
|
|
509
|
-
var path = require$$1__default["default"];
|
|
510
|
-
var exhaustiveMakeObservable = exhaustiveMakeObservable$1;
|
|
511
|
-
var unconditionalMakeObservable = unconditionalMakeObservable$1;
|
|
512
|
-
var missingMakeObservable = missingMakeObservable$1;
|
|
513
|
-
var missingObserver = missingObserver$1;
|
|
514
|
-
var noAnonymousObserver = noAnonymousObserver$1;
|
|
515
|
-
var pkg = JSON.parse(fs.readFileSync(path.join(__dirname, "..", "package.json"), "utf8"));
|
|
516
|
-
var pluginMobx = {
|
|
517
|
-
meta: {
|
|
518
|
-
name: pkg.name,
|
|
519
|
-
version: pkg.version
|
|
520
|
-
},
|
|
521
|
-
rules: {
|
|
522
|
-
"exhaustive-make-observable": exhaustiveMakeObservable,
|
|
523
|
-
"unconditional-make-observable": unconditionalMakeObservable,
|
|
524
|
-
"missing-make-observable": missingMakeObservable,
|
|
525
|
-
"missing-observer": missingObserver,
|
|
526
|
-
"no-anonymous-observer": noAnonymousObserver
|
|
527
|
-
}
|
|
528
36
|
};
|
|
529
37
|
var recommendedRules = {
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
};
|
|
535
|
-
var
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
}
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
rules: recommendedRules
|
|
549
|
-
}
|
|
550
|
-
}
|
|
551
|
-
});
|
|
552
|
-
|
|
553
|
-
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,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;
|