Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

eslint missing-make-observable-rule support for namespaces and fix imports #3284

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
190 changes: 167 additions & 23 deletions packages/eslint-plugin-mobx/__tests__/missing-make-observable.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,22 @@ const tester = new RuleTester({
});

const fields = [
'@observable o = 5',
'@observable.ref or = []',
'@observable.shallow os = []',
'@observable.deep od = {}',
'@computed get c() {}',
'@computed.struct get cs() {}',
'@computed({ equals }) get co() {}',
'@action a() {}',
'@action.bound ab() {}',
'@flow *f() {}',
'@flow.bound *fb() {}',
'observable o = 5',
'observable.ref or = []',
'observable.shallow os = []',
'observable.deep od = {}',
'computed get c() {}',
'computed.struct get cs() {}',
'computed({ equals }) get co() {}',
'action a() {}',
'action.bound ab() {}',
'flow *f() {}',
'flow.bound *fb() {}',
];

const valid1 = fields.map(field => `
class C {
${field}
@${field}

constructor() {
makeObservable(this)
Expand All @@ -48,7 +48,7 @@ class C {

const valid3 = fields.map(field => `
class C {
${field}
@${field}

constructor() {
makeObservable(this, null, { name: 'foo' })
Expand All @@ -58,7 +58,7 @@ class C {

const valid4 = fields.map(field => `
class C {
${field}
@${field}

constructor(aString: string);
constructor(aNum: number);
Expand All @@ -71,7 +71,7 @@ class C {
const invalid1 = fields.map(field => ({
code: `
class C {
${field}
@${field}
}
`,
errors: [
Expand All @@ -80,15 +80,15 @@ class C {
output: `
class C {
constructor() { makeObservable(this); }
${field}
@${field}
}
`
}))

const invalid2 = fields.map(field => ({
code: `
class C {
${field}
@${field}
constructor() {}
}
`,
Expand All @@ -97,7 +97,7 @@ class C {
],
output: `
class C {
${field}
@${field}
constructor() {;makeObservable(this);}
}
`,
Expand All @@ -106,7 +106,7 @@ class C {
const invalid3 = fields.map(field => ({
code: `
class C {
${field}
@${field}
constructor() {
makeObservable({ a: 5 });
}
Expand All @@ -117,7 +117,7 @@ class C {
],
output: `
class C {
${field}
@${field}
constructor() {
makeObservable({ a: 5 });
;makeObservable(this);}
Expand All @@ -128,7 +128,7 @@ class C {
const invalid4 = fields.map(field => ({
code: `
class C {
${field}
@${field}
constructor()
}
`,
Expand All @@ -137,7 +137,7 @@ class C {
],
output: `
class C {
${field}
@${field}
constructor() { makeObservable(this); }
}
`,
Expand All @@ -147,7 +147,7 @@ class C {
const invalid5 = fields.map(field => ({
code: `
class C {
${field}
@${field}
constructor() {
makeObservable(this, { o: observable.ref });
}
Expand All @@ -158,6 +158,143 @@ class C {
],
}))

const invalid6 = fields.map(field => ({
code: `
import * as mobx from 'mobx';

class C {
@mobx.${field}
}
`,
errors: [
{ messageId: 'missingMakeObservable' },
],
output: `
import * as mobx from 'mobx';

class C {
constructor() { mobx.makeObservable(this); }
@mobx.${field}
}
`,
}
))

const invalid7 = fields.map(field => ({
code: `
import { foo } from 'mobx';

class C {
@${field}
}
`,
errors: [
{ messageId: 'missingMakeObservable' },
],
output: `
import { foo, makeObservable } from 'mobx';

class C {
constructor() { makeObservable(this); }
@${field}
}
`,
}
))

const invalid8 = fields.map(field => ({
code: `
import { foo } from 'mobx';
import * as mobx from 'mobx';

class C {
@mobx.${field}
}
`,
errors: [
{ messageId: 'missingMakeObservable' },
],
output: `
import { foo } from 'mobx';
import * as mobx from 'mobx';

class C {
constructor() { mobx.makeObservable(this); }
@mobx.${field}
}
`,
}
))

const invalid9 = fields.map(field => ({
code: `
import { makeObservable as makeBanana } from 'mobx';

class C {
@${field}
}
`,
errors: [
{ messageId: 'missingMakeObservable' },
],
output: `
import { makeObservable as makeBanana } from 'mobx';

class C {
constructor() { makeBanana(this); }
@${field}
}
`,
}
))

const invalid10 = fields.map(field => ({
code: `
class C extends Component {
@${field}
}
`,
errors: [
{ messageId: 'missingMakeObservable' },
],
output: `
class C extends Component {
constructor(props) { super(props); makeObservable(this); }
@${field}
}
`,
}
))

const invalid11 = fields.map(field => ({
code: `
class C extends React.Component<{ foo: string }> {
@${field}
}
`,
errors: [
{ messageId: 'missingMakeObservable' },
],
output: `
class C extends React.Component<{ foo: string }> {
constructor(props: { foo: string }) { super(props); makeObservable(this); }
@${field}
}
`,
}
))

const invalid12 = fields.map(field => ({
code: `
class C extends Banana {
@${field}
}
`,
errors: [
{ messageId: 'missingMakeObservableSuper' },
],
}
))

tester.run("missing-make-observable", rule, {
valid: [
Expand All @@ -172,5 +309,12 @@ tester.run("missing-make-observable", rule, {
...invalid3,
...invalid4,
...invalid5,
...invalid6,
...invalid7,
...invalid8,
...invalid9,
...invalid10,
...invalid11,
...invalid12,
],
});
Loading