-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
[new] Add prefer-read-only-props rule #2110
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
Conversation
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]; |
There was a problem hiding this comment.
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 👍
@drx to clarify this bit:
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? |
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 |
There was a problem hiding this 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)
Thanks! |
There was a problem hiding this 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.
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 recognizefoo
inprops: $ReadOnly<{foo: bar}>
as a prop. In this case that's OK, because that wouldn't give a warning anyway.Fixes #1454. Fixes #1869.