Skip to content

Commit dffa1f3

Browse files
Evgueni Navernioukantialias
Evgueni Naverniouk
authored andcommitted
Adds new unused-prop-types rule
1 parent 8b8eba7 commit dffa1f3

File tree

5 files changed

+2917
-1
lines changed

5 files changed

+2917
-1
lines changed

README.md

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

106107
## JSX-specific rules
107108

docs/rules/unused-prop-types.md

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
function Hello({ name }) {
31+
return <div>Hello Jeremy</div>;
32+
}
33+
```
34+
35+
The following patterns are not considered warnings:
36+
37+
```jsx
38+
var Hello = React.createClass({
39+
propTypes: {
40+
name: React.PropTypes.string
41+
},
42+
render: function() {
43+
return <div>Hello {this.props.name}</div>;
44+
}
45+
});
46+
```
47+
48+
## Rule Options
49+
50+
This rule can take one argument to ignore some specific props during validation.
51+
52+
```
53+
...
54+
"prop-types": [<enabled>, { customValidators: <customValidator> }]
55+
...
56+
```
57+
58+
* `enabled`: for enabling the rule. 0=off, 1=warn, 2=error. Defaults to 0.
59+
* `customValidators`: optional array of validators used for propTypes validation.
60+
* `skipShapeProps`: In some cases it is impossible to accurately detect whether or not a shape PropType's values are being used. Setting this option to true will skip validation of shape PropTypes.
61+
62+
## About component detection
63+
64+
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.
65+
66+
For now we should detect components created with:
67+
68+
* `React.createClass()`
69+
* an ES6 class that inherit from `React.Component` or `Component`
70+
* 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
@@ -50,7 +50,8 @@ var rules = {
5050
'jsx-no-target-blank': require('./lib/rules/jsx-no-target-blank'),
5151
'jsx-filename-extension': require('./lib/rules/jsx-filename-extension'),
5252
'require-optimization': require('./lib/rules/require-optimization'),
53-
'no-find-dom-node': require('./lib/rules/no-find-dom-node')
53+
'no-find-dom-node': require('./lib/rules/no-find-dom-node'),
54+
'unused-prop-types': require('./lib/rules/unused-prop-types')
5455
};
5556

5657
var ruleNames = Object.keys(rules);

0 commit comments

Comments
 (0)