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

[new] Add prefer-read-only-props rule #2110

Merged
merged 6 commits into from Jan 21, 2019
Merged

[new] Add prefer-read-only-props rule #2110

merged 6 commits into from Jan 21, 2019

Conversation

drx
Copy link
Contributor

@drx drx commented Jan 3, 2019

This adds a rule to enforce that flow types for props are read-only (covariant). This was requested in #1454 and #1869.

I added a fixer for the rule, which I tested on a decently large codebase at Flexport (5000+ files) with thousands of violations.

One interesting note is that declaredPropTypes doesn't recognize foo in props: $ReadOnly<{foo: bar}> as a prop. In this case that's OK, because that wouldn't give a warning anyway.

Fixes #1454. Fixes #1869.

@ljharb ljharb added the new rule label Jan 3, 2019
@ljharb
Copy link
Member

ljharb commented Jan 3, 2019

It'd be great if we could separate out the fixes into a separate PR from the new rule.

}

Object.keys(component.declaredPropTypes).forEach(propName => {
const prop = component.declaredPropTypes[propName];
Copy link
Contributor

Choose a reason for hiding this comment

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

So cool to see the propTypes detection in use 👍

@alexzherdev
Copy link
Contributor

@drx to clarify this bit:

Worse yet, because declaredPropTypes is an object, those helper properties can overwrite actual props.

Do you mean that if we wrongly detect something as a prop, it can overwrite the metadata for an actual prop with the same name?

@drx
Copy link
Contributor Author

drx commented Jan 3, 2019

It'd be great if we could separate out the fixes into a separate PR from the new rule.

Done! #2111

@alexzherdev Yeah, if you have something like:

type Props = {
  foo: string,
}

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

The metadata (including the loc) might be overwritten by the renderHelper foo, I have had this happen while running fixes which is how I discovered this.

@ljharb ljharb added the flow label Jan 3, 2019
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.

Looks great!

(I'm going to hold off merging it for awhile while the 7.12 release settles down)

@drx
Copy link
Contributor Author

drx commented Jan 3, 2019

Thanks!

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. I'm looking forward to using this one.

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.

Rule proposal: Require read-only props Add a rule prefer-covariant-props
4 participants