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

Conversation

WearyMonkey
Copy link

@WearyMonkey WearyMonkey commented Feb 9, 2022

Improves the missing-make-observable-rule to:

  • Support namespace imports e.g. @mobx.observable.ref
  • Insert imports if required
  • Adds a super call on classes that extend React.Component
  • Does not generate a constructor when extending an unknown class

For example:

import * as mobx from 'mobx';

class C {
   @mobx.observable.ref c = 1;
}

Will be fixed to:

import * as mobx from 'mobx';

class C {
  constructor() {
    mobx.makeObservable(this)
  }

   @mobx.observable.ref c = 1;
}

And

import { observable } from 'mobx';

class C {
   @observable.ref c = 1;
}

Will be fixed to:

import { observable, makeObservable  } from 'mobx';

class C {
  constructor() {
    makeObservable(this)
  }

   @observable.ref c = 1;
}

And

class C extends React.Component<{ foo: string }> {
   @observable.ref c = 1;
}

Will be fixed to:

class C extends React.Component<{ foo: string }> {
    constructor(props: { foo: string }) {
       super(props);
       makeObservable(this);
    }

   @observable.ref c = 1;
}

Code change checklist

  • Added/updated unit tests
  • Updated /docs. For new functionality, at least API.md should be updated
  • Verified that there is no significant performance drop (yarn mobx test:performance)

@changeset-bot
Copy link

changeset-bot bot commented Feb 9, 2022

⚠️ No Changeset found

Latest commit: 0fe7e04

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@WearyMonkey WearyMonkey force-pushed the toby-make-observable-import branch from d4af3bd to 4c7d762 Compare February 9, 2022 14:52
@iChenLei
Copy link
Member

iChenLei commented Feb 9, 2022

ci failed, bro

@WearyMonkey WearyMonkey force-pushed the toby-make-observable-import branch from 535e609 to 0fe7e04 Compare February 9, 2022 16:34
@iChenLei iChenLei requested a review from urugator February 10, 2022 02:24
@@ -28,31 +53,67 @@ function create(context) {
}
} else {
const fix = fixer => {
// The class extends a another unknown class so we can not safely create a super call.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't matter, create constructor without super and let another eslint rule handle missing super.
I think that handling React component specifically as part of this rule is unnecessary as well, but I am ok with it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants