Skip to content

Commit 504bcd5

Browse files
author
Evgueni Naverniouk
committed
Adds new unused-prop-types rule
1 parent 4be546a commit 504bcd5

File tree

6 files changed

+2924
-2
lines changed

6 files changed

+2924
-2
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ Finally, enable all of the rules that you would like to use. Use [our preset](#
105105
* [react/self-closing-comp](docs/rules/self-closing-comp.md): Prevent extra closing tags for components without children (fixable)
106106
* [react/sort-comp](docs/rules/sort-comp.md): Enforce component methods order
107107
* [react/sort-prop-types](docs/rules/sort-prop-types.md): Enforce propTypes declarations alphabetical sorting
108+
* [react/unused-prop-types](docs/rules/unused-prop-types.md): Prevent definitions of unused prop types
108109

109110
## JSX-specific rules
110111

docs/rules/unused-prop-types.md

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Prevent definitions of unused prop types (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

+2-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ var rules = {
5454
'jsx-filename-extension': require('./lib/rules/jsx-filename-extension'),
5555
'require-optimization': require('./lib/rules/require-optimization'),
5656
'no-find-dom-node': require('./lib/rules/no-find-dom-node'),
57-
'no-danger-with-children': require('./lib/rules/no-danger-with-children')
57+
'no-danger-with-children': require('./lib/rules/no-danger-with-children'),
58+
'unused-prop-types': require('./lib/rules/unused-prop-types')
5859
};
5960

6061
var ruleNames = Object.keys(rules);

0 commit comments

Comments
 (0)