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

Adds new no-unused-prop-types rule #611

Merged
merged 1 commit into from Aug 25, 2016

Conversation

EvHaus
Copy link
Collaborator

@EvHaus EvHaus commented May 23, 2016

Fixes #226 by adding an new unused-prop-types rule.

The rule is based very heavily on the prop-types rule and shares a lot of the same code. For the time being, I have not made any attempts at breaking out the shared code into a reusable utility file. Please advise on the best way to do this. I was simply thinking of dropping a proptypes.js file into the utils directory with all the shared functions. If that's acceptable, let me know and I will do that.

Have tested against several React projects with great success so far. No false positive problems yet, but it needs a lot more testing against other repos. I'm hoping other users will report issues as they try it. For my needs, this rule is working great.

Can be tested by following these steps:

npm install EvNaverniouk/eslint-plugin-react#6d309e13a65a1032577ae0c98081d778f85d515a

Then enable the rule in your eslintrc:

"rules": {
    "react/unused-prop-types": 2
}

@coveralls
Copy link

coveralls commented May 23, 2016

Coverage Status

Coverage decreased (-0.08%) to 96.686% when pulling b95b0ef on EvNaverniouk:226-unused-prop-types into 82b3aa9 on yannickcr:master.

@EvHaus
Copy link
Collaborator Author

EvHaus commented May 23, 2016

I found one condition where this reports a false positive:

class Hello extends Component {
    static propTypes = {
        params: PropTypes.shape({
            id: PropTypes.string
        })
    }
    render () {
        const {params} = this.props;
        const id = (params || {}).id;
        return <span>{id}</span>;
    }
}

Not sure yet how to get (params || {}).id to get detected properly. Any advise would be welcome. I'll keep looking into it.

EDIT: I think trying to resolve it will be a losing battle of whack-a-mole. As such, I have added an ignoreShapeProp option to the rule to disable checking of shape proptypes.

@coveralls
Copy link

coveralls commented May 23, 2016

Coverage Status

Coverage decreased (-0.08%) to 96.686% when pulling 0fce5d1 on EvNaverniouk:226-unused-prop-types into 82b3aa9 on yannickcr:master.

@coveralls
Copy link

coveralls commented May 23, 2016

Coverage Status

Coverage decreased (-0.08%) to 96.686% when pulling 0fce5d1 on EvNaverniouk:226-unused-prop-types into 82b3aa9 on yannickcr:master.

@coveralls
Copy link

coveralls commented May 23, 2016

Coverage Status

Coverage decreased (-0.07%) to 96.699% when pulling a2faeb6 on EvNaverniouk:226-unused-prop-types into 82b3aa9 on yannickcr:master.

@mattdell
Copy link

mattdell commented Jun 7, 2016

I'm going to have a look at this now but want to flag to @EvNaverniouk that the PR has conflicts. Can you fix @EvNaverniouk ?

I think the issue using the pattern const {params} = this.props is something we can live with. It would be better to find unused instances of this.props.params as a first pass and we can work on extending this for the former pattern as well.

@EvHaus
Copy link
Collaborator Author

EvHaus commented Jun 7, 2016

Merged master into this PR to resolve conflicts. Thanks @mattdell

@ljharb
Copy link
Member

ljharb commented Jun 7, 2016

@EvNaverniouk it'd be nice if you could rebase master instead, so there's no merge commits (and to squash all the "wip" commits)

@EvHaus EvHaus force-pushed the 226-unused-prop-types branch 3 times, most recently from 46462cc to 74f0cbe Compare June 7, 2016 17:57
@EvHaus
Copy link
Collaborator Author

EvHaus commented Jun 7, 2016

@ljharb Rebased.

@mattdell
Copy link

What needs to happen to get this merged?

@antialias
Copy link

looks like it needs another merge / rebase

@ljharb
Copy link
Member

ljharb commented Jul 13, 2016

@EvNaverniouk would you mind rebasing this?

@EvHaus
Copy link
Collaborator Author

EvHaus commented Jul 14, 2016

@ljharb Rebase completed.

@ljharb
Copy link
Member

ljharb commented Jul 14, 2016

ping @yannickcr :-)

@Faradey27
Copy link

@EvNaverniouk do you plan to fix conflicts?

@antialias
Copy link

I resolved the conflicts between this branch and master and pushed them to my fork: antialias@dffa1f3

There were no ambiguities, i.e. I'm 100% confident that I resolved them correctly.

@yannickcr, I could submit a replacement PR or @EvNaverniouk, you could hard reset this branch to the commit from my branch and the PR would show as resolved.

@EvHaus
Copy link
Collaborator Author

EvHaus commented Jul 26, 2016

Looks like some of my tests are failing after merging from latest master. Investigating...

@EvHaus
Copy link
Collaborator Author

EvHaus commented Jul 26, 2016

Ok. Fixed the conflicts and rebased again. Also added a new commit to fix a bug in the Component.js utility method. I left that as a separate commit so you can clearly see it: fcd9d59

// Rule Definition
// ------------------------------------------------------------------------------

module.exports = Components.detect(function(context, components, utils) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This should use the new eslint rule format

@EvHaus
Copy link
Collaborator Author

EvHaus commented Aug 2, 2016

@petersendidit Updated PR to use new ESLint rule format.

@dhruska
Copy link

dhruska commented Aug 8, 2016

I pulled down this branch into one of my projects to give it a try, since I'm really excited about this - it worked great, except for in one component where it did not detect that I was only using a prop in componentWillReceiveProps on the nextProps object. Granted, it's probably a poor design pattern in my project that I am doing this, but the linter threw an error when I was indeed using the prop.

@EvHaus
Copy link
Collaborator Author

EvHaus commented Aug 8, 2016

@dhruska Can you send me a copy of the code that reproduces that?

@dhruska
Copy link

dhruska commented Aug 8, 2016

@EvNaverniouk Let me work on getting you an example. I tried to create a simple component that would reproduce the issue and was not able to.

@burabure
Copy link
Contributor

this will be very useful! I'll try to test this on our codebase (1M+ lines) and report back

}
});

function Hello({ name }) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't understand why this is included in the examples--this component has no propTypes. Did you mean to include something like this?

Hello.propTypes = {
  name: React.PropTypes.string.isRequired,
};

@EvHaus
Copy link
Collaborator Author

EvHaus commented Aug 21, 2016

@lencioni Updated PR to clean up the docs as per your recommendations, fixed merge conflicts and rebased.

@lencioni
Copy link
Collaborator

I think this looks good, thanks for all of your work on this rule. If this code is based off of the prop-types rule code, we currently have a bug in that rule where nested destructured props are not marked as used. (#296) It would be good to include some examples in your tests that demonstrate this.

@EvHaus
Copy link
Collaborator Author

EvHaus commented Aug 22, 2016

@lencioni Not sure if that's what you had in mind, but I added a commented-out test at f083a13#diff-655c668b4b1964c5be4ff486d879ef45R1980 which can be enabled once #296 is fixed.

@lencioni
Copy link
Collaborator

@EvNaverniouk Thanks for doing that! That's not quite what I had in mind. My point was more that if we are introducing code that has bugs that we are aware of, it would be nice to fix those bugs before merging. And actually, I think the bug might be the inverse in this rule (e.g. nested destructuring of a prop in propTypes is incorrectly flagged).

 function Foo(props) {
   const { foo: { bar } } = props;
   return <div test={bar} />;
 }

 Foo.propTypes = {
   foo: PropTypes.shape({
     bar: PropTypes.number,
   }),
 };

Any chance you can add a test case for this, and if it fails, make it pass?

@yannickcr
Copy link
Member

I think this rule already adds a lot of value even if this bug is not fixed right now. So I am not against merging this rule with the bug and fix it later on (actually I still need to figure out how to fix it in the prop-types rule).

Also I'd prefer to name this rule no-unused-prop-types to match ESLint Rule Naming Conventions.

@yannickcr
Copy link
Member

yannickcr commented Aug 23, 2016

Ho, and thank you for this @EvNaverniouk, this is really great work ❤️

@EvHaus EvHaus changed the title Adds new unused-prop-types rule Adds new no-unused-prop-types rule Aug 24, 2016
@EvHaus
Copy link
Collaborator Author

EvHaus commented Aug 24, 2016

@lencioni To be honest with you, fixing that bug is a bit beyond my level of understanding of how the component and proptype detection works. However, I'm happy to merge the fix in into this rule once someone solves that bug for the prop-types rule. I've subscribed to it and will keep a close eye on it once a fix is found.

@yannickcr I've renamed the rule to no-unused-prop-types and rebased again.

@lencioni
Copy link
Collaborator

@EvNaverniouk Perfect is the enemy of the good, so that sounds good to me. Thanks for your work on this!

@yannickcr yannickcr merged commit ed1f168 into jsx-eslint:master Aug 25, 2016
@yannickcr
Copy link
Member

Merged, thanks!

@Faradey27
Copy link

thank you guys, nice rule and nice job

@tristanbbq
Copy link

tristanbbq commented Sep 15, 2016

@yannickcr: I updated to 6.2.2 and added rule as following:
"react/no-unused-prop-types": ["warn", { "customValidators": [], "skipShapeProps": true }],
But I keep getting warnings PropType is defined but props is never used for a PropType.shape.

Something I'm missing here?

@EvHaus
Copy link
Collaborator Author

EvHaus commented Sep 15, 2016

@tristanbbq Can you please open a new issue with a code sample I can use to reproduce?

@tristanbbq
Copy link

tristanbbq commented Sep 15, 2016

@EvNaverniouk: just created #837

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.

None yet