Skip to content

Fix issue with propTypes misclassifying props #2111

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

Merged
merged 1 commit into from
Jan 3, 2019

Conversation

drx
Copy link
Contributor

@drx drx commented Jan 3, 2019

While testing #2110, I found a bug with declaredPropTypes and the no-unused-prop-types rule.

The propTypes module incorrectly detects function parameters inside a class property as props of the class component, which results in this code being considered a warning:

type Props = {
  used: string,
}

class Hello extends React.Component<Props> {
  renderHelper = ({unused}: {unused: string}) => {
    return <div />;
  }
  render() {
    return <div>{this.props.used}</div>;
  }
}

It thinks that unused is an unused prop of Hello.

I'm not a 100% if the fix is the correct fix, but I made markAnnotatedFunctionArgumentsAsDeclared check if the node has a ClassProperty ancestor, and if so, bail. I also added a test to no-unused-prop-types to check for this.

Having these kinds of class properties is arguably an anti-pattern, but it came up as an issue when I was testing legacy code. Worse yet, because declaredPropTypes is an object, those helper properties can overwrite actual props.

@ljharb ljharb force-pushed the prop_types_class_properties branch from 993c150 to ed1c787 Compare January 3, 2019 07:59
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.

Thanks!

@ljharb ljharb force-pushed the prop_types_class_properties branch from ed1c787 to e15bafa Compare January 3, 2019 07:59
@ljharb ljharb added the bug label Jan 3, 2019
@ljharb ljharb merged commit b48b479 into jsx-eslint:master Jan 3, 2019
This was referenced Jan 4, 2019
@ghost ghost mentioned this pull request Jan 12, 2019
1 task
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging this pull request may close these issues.

2 participants