Skip to content

Commit

Permalink
Fix/lint constructor overloads (#3231)
Browse files Browse the repository at this point in the history
  • Loading branch information
ahoisl committed Dec 20, 2021
1 parent 48bf222 commit 0aaf183
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/gentle-weeks-rescue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-plugin-mobx": patch
---

fix(lint): fix 'missing-make-observable' rule with constructor overloads
13 changes: 13 additions & 0 deletions packages/eslint-plugin-mobx/__tests__/missing-make-observable.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,18 @@ class C {
}
`).map(code => ({ code }))

const valid4 = fields.map(field => `
class C {
${field}
constructor(aString: string);
constructor(aNum: number);
constructor(stringOrNum: string | number) {
makeObservable(this, null, { name: 'foo' })
}
}
`).map(code => ({ code }))

const invalid1 = fields.map(field => ({
code: `
class C {
Expand Down Expand Up @@ -152,6 +164,7 @@ tester.run("missing-make-observable", rule, {
...valid1,
valid2,
...valid3,
...valid4,
],
invalid: [
...invalid1,
Expand Down
3 changes: 2 additions & 1 deletion packages/eslint-plugin-mobx/src/missing-make-observable.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ function create(context) {
const clazz = findAncestor(decorator, node => node.type === 'ClassDeclaration' || node.type === 'ClassExpression');
if (!clazz) return;
// ClassDeclaration > ClassBody > []
const constructor = clazz.body.body.find(node => node.kind === 'constructor');
const constructor = clazz.body.body.find(node => node.kind === 'constructor' && node.value.type === 'FunctionExpression') ??
clazz.body.body.find(node => node.kind === 'constructor');
// MethodDefinition > FunctionExpression > BlockStatement > []
const isMakeObservable = node => node.expression?.callee?.name === 'makeObservable' && node.expression?.arguments[0]?.type === 'ThisExpression';
const makeObservable = constructor?.value.body?.body.find(isMakeObservable)?.expression;
Expand Down

0 comments on commit 0aaf183

Please sign in to comment.