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

[no-unused-state] Add ability to handle arrow function class method #1411

Conversation

jackyho112
Copy link
Contributor

@jackyho112 jackyho112 commented Aug 31, 2017

For #1363, so that state property usage by object destructuring assignment in a class method defined with an arrow function gets recorded. Examples:

class ArrowFunctionClassMethodTest extends React.Component {
  constructor() {
    this.state = { foo: 0 };
  }

  doSomething = () => {
    const { state: { foo } } = this;

    return foo;
  }

  render() {
    return <SomeComponent />;
  }
}

should not be warned.

Let me know if there is anything I can do to improve this PR. Also, just to confirm, this rule doesn't care about whether the class method is used or not, right?

@jackyho112 jackyho112 closed this Aug 31, 2017
@jackyho112 jackyho112 reopened this Aug 31, 2017
@jackyho112 jackyho112 changed the title [no-used-state] Add ability to handle arrow function class method [no-unused-state] Add ability to handle arrow function class method Aug 31, 2017
node.value.type === 'ArrowFunctionExpression'
) {
// Create a new set for this.state aliases local to this function.
classInfo.aliases = new Set();
Copy link
Member

Choose a reason for hiding this comment

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

i'm not seeing anything that adds to this set?

If it otherwise crashes because this doesn't exist; then let's initialize it in every case.

@jackyho112
Copy link
Contributor Author

jackyho112 commented Sep 1, 2017

@ljharb

I think the set, classInfo.aliases, will only be added to if the property name gets assigned to a different variable name.

For instance,

class App extends React.Component {
        state = { foo: 0 };

        doSomething = () => {
          const { state: { foo: bar }} = this;

          return bar;
        }

        render() {
          return <SomeComponent />;
        }
      }

will add 'bar' to the set. I have added one more test regarding this.

I also refactored how the code deals with classInfo.aliases according to your feedback. We still need to clear the set after each method definition though because each alias added is local to the class method. Let me know if this looks better.

ljharb
ljharb previously requested changes Sep 5, 2017
classInfo.aliases = null;

// Remove the current set of aliases local to the previous class method
classInfo.aliases.clear();
Copy link
Member

Choose a reason for hiding this comment

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

rather than storing across files and clearing; we may want to instead assign classInfo.aliases = new Set() at program entry?

@jackyho112
Copy link
Contributor Author

jackyho112 commented Sep 5, 2017

@ljharb

Based on your suggestion, I have restored the implementation back to using classInfo.aliases = new Set() which is a part of the original code.

However, we still need to do classInfo.aliases = new Set() for every class method definition because the aliases in classInfo.aliases are local to each class method.

Let me know if that's ok. If not, there are alternative but potentially more complicated ways to implement this, such as making classInfo.aliases an object and setting a property for each class method's aliases.

@ljharb ljharb dismissed their stale review September 5, 2017 23:44

This seems fine to me; I'm deferring to someone more familiar with this code.

@jackyho112
Copy link
Contributor Author

jackyho112 commented Sep 6, 2017

@ljharb

Thanks for the review! After this is merged, I can take a look at documenting no-unused-state as I saw there is an issue open for it.

@jackyho112 jackyho112 closed this Sep 16, 2017
@jackyho112 jackyho112 force-pushed the make-no-unused-state-rule-handle-arrow-function-class-method branch from c35c5ea to ddd05b1 Compare September 16, 2017 22:29
@jackyho112 jackyho112 reopened this Sep 16, 2017
@jackyho112
Copy link
Contributor Author

jackyho112 commented Sep 17, 2017

@jseminck

I think this test is failing on master. Any clue? Maybe this test should report an error in the first place?

@jseminck
Copy link
Contributor

Thanks @jackyho112

This is because of the new ESLint release. They changed some internal process and a case that we didn't handle before will now work.

I can update the case to be valid, but it would only work with the latest ESLint. With older cases it will still fail. Even more: We had to write some work-around to make sure ESLint did not crash but I can't remove that code unless we update the peerDependency for ESLint.

I also commented here: #1375 (comment)

But I think I can start a new PR and we can start/continue a discussion there.

@jseminck
Copy link
Contributor

Created a PR to move that test: #1437

@jackyho112 jackyho112 force-pushed the make-no-unused-state-rule-handle-arrow-function-class-method branch from 393467f to e616d7f Compare October 1, 2017 22:01
@jackyho112
Copy link
Contributor Author

jackyho112 commented Oct 1, 2017

@ljharb, @EvHaus, @yannickcr, @lencioni

Decided to adhere to the original implementation instead because it is the safest approach, it is easier to review and if there is a new way of defining a class method in the future, the rule won't break immediately.

Now, this PR just merely adds the support for a class method defined by an arrow function in a way how the original code intends to and does nothing else. I hope that is ok and should at least make this PR more mergable. 😄

Copy link
Member

@ljharb ljharb left a comment

Choose a reason for hiding this comment

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

This does seem much simpler. Test changes LGTM; implementation feems fine.

Copy link
Collaborator

@EvHaus EvHaus left a comment

Choose a reason for hiding this comment

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

LGTM

@jackyho112
Copy link
Contributor Author

@EvHaus, @ljharb

Thanks for the approvals! 😄

@ljharb ljharb merged commit 952da85 into jsx-eslint:master Oct 18, 2017
@jackyho112 jackyho112 deleted the make-no-unused-state-rule-handle-arrow-function-class-method branch October 18, 2017 05:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

4 participants