Skip to content

Commit 535e609

Browse files
committed
super
1 parent 4c7d762 commit 535e609

File tree

2 files changed

+69
-2
lines changed

2 files changed

+69
-2
lines changed

packages/eslint-plugin-mobx/__tests__/missing-make-observable.js

+51
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,54 @@ constructor() { makeBanana(this); }
248248
}
249249
))
250250

251+
const invalid10 = fields.map(field => ({
252+
code: `
253+
class C extends Component {
254+
@${field}
255+
}
256+
`,
257+
errors: [
258+
{ messageId: 'missingMakeObservable' },
259+
],
260+
output: `
261+
class C extends Component {
262+
constructor(props) { super(props); makeObservable(this); }
263+
@${field}
264+
}
265+
`,
266+
}
267+
))
268+
269+
const invalid11 = fields.map(field => ({
270+
code: `
271+
class C extends React.Component<{ foo: string }> {
272+
@${field}
273+
}
274+
`,
275+
errors: [
276+
{ messageId: 'missingMakeObservable' },
277+
],
278+
output: `
279+
class C extends React.Component<{ foo: string }> {
280+
constructor(props: { foo: string }) { super(props); makeObservable(this); }
281+
@${field}
282+
}
283+
`,
284+
}
285+
))
286+
287+
const invalid12 = fields.map(field => ({
288+
code: `
289+
class C extends Banana {
290+
@${field}
291+
}
292+
`,
293+
errors: [
294+
{ messageId: 'missingMakeObservableSuper' },
295+
],
296+
}
297+
))
298+
251299
tester.run("missing-make-observable", rule, {
252300
valid: [
253301
...valid1,
@@ -265,5 +313,8 @@ tester.run("missing-make-observable", rule, {
265313
...invalid7,
266314
...invalid8,
267315
...invalid9,
316+
...invalid10,
317+
...invalid11,
318+
...invalid12,
268319
],
269320
});

packages/eslint-plugin-mobx/src/missing-make-observable.js

+18-2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ function create(context) {
3636
const constructor = clazz.body.body.find(node => node.kind === 'constructor' && node.value.type === 'FunctionExpression') ??
3737
clazz.body.body.find(node => node.kind === 'constructor');
3838
// MethodDefinition > FunctionExpression > BlockStatement > []
39+
const isReact = clazz.superClass?.name === 'Component'
40+
|| (clazz.superClass?.object?.name === 'React' && clazz.superClass?.property.name === 'Component');
41+
const unsupportedSuper = !isReact && !!clazz.superClass;
3942
const isMakeObservable = node => node.expression?.callee?.name === 'makeObservable' && node.expression?.arguments[0]?.type === 'ThisExpression';
4043
const makeObservable = constructor?.value.body?.body.find(isMakeObservable)?.expression;
4144

@@ -50,6 +53,10 @@ function create(context) {
5053
}
5154
} else {
5255
const fix = fixer => {
56+
// The class extends a another unknown class so we can not safely create a super call.
57+
if (unsupportedSuper && !constructor) {
58+
return;
59+
}
5360

5461
const fixes = [];
5562
let makeObservableExpr = 'makeObservable';
@@ -71,7 +78,15 @@ function create(context) {
7178
// constructor() {}
7279
const closingBracket = sourceCode.getLastToken(constructor.value.body);
7380
fixes.push(fixer.insertTextBefore(closingBracket, `;${makeObservableExpr}(this);`));
74-
} else {
81+
} else if (isReact) {
82+
// class C extends Component<{ foo: string }> {}
83+
let propsType = '';
84+
if (clazz.superTypeParameters?.params.length) {
85+
propsType = `: ${sourceCode.getText(clazz.superTypeParameters.params[0])}`;
86+
}
87+
const openingBracket = sourceCode.getFirstToken(clazz.body);
88+
fixes.push(fixer.insertTextAfter(openingBracket, `\nconstructor(props${propsType}) { super(props); ${makeObservableExpr}(this); }`));
89+
} else if (!unsupportedSuper) {
7590
// class C {}
7691
const openingBracket = sourceCode.getFirstToken(clazz.body);
7792
fixes.push(fixer.insertTextAfter(openingBracket, `\nconstructor() { ${makeObservableExpr}(this); }`));
@@ -82,7 +97,7 @@ function create(context) {
8297

8398
context.report({
8499
node: clazz,
85-
messageId: 'missingMakeObservable',
100+
messageId: unsupportedSuper ? 'missingMakeObservableSuper' : 'missingMakeObservable',
86101
fix,
87102
})
88103
}
@@ -101,6 +116,7 @@ module.exports = {
101116
},
102117
messages: {
103118
missingMakeObservable: "Constructor is missing `makeObservable(this)`.",
119+
missingMakeObservableSuper: "Constructor is missing `makeObservable(this)`. Can not fix because of missing super call.",
104120
secondArgMustBeNullish: "`makeObservable`'s second argument must be nullish or not provided when using decorators."
105121
},
106122
},

0 commit comments

Comments
 (0)