Skip to content

Commit ed1f168

Browse files
Evgueni Navernioukyannickcr
authored andcommitted
Adds no-unused-prop-types rule (fixes #226)
1 parent 466bb5e commit ed1f168

File tree

6 files changed

+2945
-2
lines changed

6 files changed

+2945
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ Finally, enable all of the rules that you would like to use. Use [our preset](#
9696
* [react/no-set-state](docs/rules/no-set-state.md): Prevent usage of `setState`
9797
* [react/no-string-refs](docs/rules/no-string-refs.md): Prevent using string references in `ref` attribute.
9898
* [react/no-unknown-property](docs/rules/no-unknown-property.md): Prevent usage of unknown DOM property (fixable)
99+
* [react/no-unused-prop-types](docs/rules/no-unused-prop-types.md): Prevent definitions of unused prop types
99100
* [react/prefer-es6-class](docs/rules/prefer-es6-class.md): Enforce ES5 or ES6 class for React Components
100101
* [react/prefer-stateless-function](docs/rules/prefer-stateless-function.md): Enforce stateless React Components to be written as a pure function
101102
* [react/prop-types](docs/rules/prop-types.md): Prevent missing props validation in a React component definition

docs/rules/no-unused-prop-types.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Prevent definitions of unused prop types (no-unused-prop-types)
2+
3+
Warns you if you have defined a prop type but it is never being used anywhere.
4+
5+
## Rule Details
6+
7+
The following patterns are considered warnings:
8+
9+
```jsx
10+
var Hello = React.createClass({
11+
propTypes: {
12+
name: React.PropTypes.string
13+
},
14+
render: function() {
15+
return <div>Hello Bob</div>;
16+
}
17+
});
18+
19+
var Hello = React.createClass({
20+
propTypes: {
21+
firstname: React.PropTypes.string.isRequired,
22+
middlename: React.PropTypes.string.isRequired, // middlename is never used below
23+
lastname: React.PropTypes.string.isRequired
24+
},
25+
render: function() {
26+
return <div>Hello {this.props.firstname} {this.props.lastname}</div>;
27+
}
28+
});
29+
```
30+
31+
The following patterns are not considered warnings:
32+
33+
```jsx
34+
var Hello = React.createClass({
35+
propTypes: {
36+
name: React.PropTypes.string
37+
},
38+
render: function() {
39+
return <div>Hello {this.props.name}</div>;
40+
}
41+
});
42+
```
43+
44+
## Rule Options
45+
46+
This rule can take one argument to ignore some specific props during validation.
47+
48+
```
49+
...
50+
"prop-types": [<enabled>, { customValidators: <customValidator> }]
51+
...
52+
```
53+
54+
* `enabled`: for enabling the rule. 0=off, 1=warn, 2=error. Defaults to 0.
55+
* `customValidators`: optional array of validators used for propTypes validation.
56+
* `skipShapeProps`: In some cases it is impossible to accurately detect whether or not a `React.PropTypes.shape`'s values are being used. Setting this option to `true` will skip validation of `PropTypes.shape`.
57+
58+
## About component detection
59+
60+
For this rule to work we need to detect React components, this could be very hard since components could be declared in a lot of ways.
61+
62+
For now we should detect components created with:
63+
64+
* `React.createClass()`
65+
* an ES6 class that inherit from `React.Component` or `Component`
66+
* a stateless function that return JSX or the result of a `React.createElement` call.

index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ var rules = {
5555
'require-optimization': require('./lib/rules/require-optimization'),
5656
'no-find-dom-node': require('./lib/rules/no-find-dom-node'),
5757
'no-danger-with-children': require('./lib/rules/no-danger-with-children'),
58-
'style-prop-object': require('./lib/rules/style-prop-object')
58+
'style-prop-object': require('./lib/rules/style-prop-object'),
59+
'no-unused-prop-types': require('./lib/rules/no-unused-prop-types')
5960
};
6061

6162
var ruleNames = Object.keys(rules);

0 commit comments

Comments
 (0)