-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
[New] add prefer-exact-props
rule
#1547
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
# Prefer exact proptype definitions (react/prefer-exact-props) | ||
|
||
Recommends options to ensure only exact prop definitions are used when writing components. This recommends solutions for PropTypes or for Flow types. | ||
|
||
In React, you can define prop types for components using propTypes. Such an example is below: | ||
|
||
```jsx | ||
class Foo extends React.Component { | ||
render() { | ||
return <p>{this.props.bar}</p>; | ||
} | ||
} | ||
|
||
Foo.propTypes = { | ||
bar: PropTypes.string | ||
}; | ||
``` | ||
|
||
The problem with this is that the consumer of the component could still pass in extra props. There could even be a typo for expected props. In order to prevent those situations, one could use the npm package [prop-types-exact](https://www.npmjs.com/package/prop-types-exact) to warn when unexpected props are passed to the component. | ||
|
||
One can also define props for a component using Flow types. Such an example is below: | ||
|
||
```jsx | ||
class Foo extends React.Component { | ||
props: { | ||
bar: string | ||
} | ||
|
||
render() { | ||
return <p>{this.props.bar}</p>; | ||
} | ||
} | ||
``` | ||
|
||
In this case, one could instead enforce only the exact props being used by using exact type objects, like below: | ||
|
||
```jsx | ||
class Foo extends React.Component { | ||
props: {| | ||
bar: string | ||
}| | ||
|
||
render() { | ||
return <p>{this.props.bar}</p>; | ||
} | ||
} | ||
``` | ||
|
||
See the [Flow docs](https://flow.org/en/docs/types/objects/#toc-exact-object-types) on exact object types for more information. | ||
|
||
## Rule Details | ||
|
||
This rule will only produce errors for prop types when combined with the appropriate entries in `propWrapperFunctions`. For example: | ||
|
||
```json | ||
{ | ||
"settings": { | ||
"propWrapperFunctions": [ | ||
{"property": "exact", "exact": true} | ||
] | ||
} | ||
} | ||
``` | ||
|
||
The following patterns are considered warnings: | ||
|
||
```jsx | ||
class Component extends React.Component { | ||
render() { | ||
return <div />; | ||
} | ||
} | ||
Component.propTypes = { | ||
foo: PropTypes.string | ||
}; | ||
``` | ||
|
||
```jsx | ||
class Component extends React.Component { | ||
static propTypes = { | ||
foo: PropTypes.string | ||
} | ||
render() { | ||
return <div />; | ||
} | ||
} | ||
``` | ||
|
||
```jsx | ||
class Component extends React.Component { | ||
props: { | ||
foo: string | ||
} | ||
render() { | ||
return <div />; | ||
} | ||
} | ||
``` | ||
|
||
```jsx | ||
function Component(props: { foo: string }) { | ||
return <div />; | ||
} | ||
``` | ||
|
||
```jsx | ||
type Props = { | ||
foo: string | ||
} | ||
function Component(props: Props) { | ||
return <div />; | ||
} | ||
``` | ||
|
||
The following patterns are **not** considered warnings: | ||
|
||
```jsx | ||
type Props = {| | ||
foo: string | ||
|} | ||
function Component(props: Props) { | ||
return <div />; | ||
} | ||
``` | ||
|
||
```jsx | ||
import exact from 'prop-types-exact'; | ||
class Component extends React.Component { | ||
render() { | ||
return <div />; | ||
} | ||
} | ||
Component.propTypes = exact({ | ||
foo: PropTypes.string | ||
}); | ||
``` | ||
|
||
## When Not To Use It | ||
|
||
If you aren't concerned about extra props being passed to a component or potential spelling errors for existing props aren't a common nuisance, then you can leave this rule off. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.