Skip to content

Commit b95b0ef

Browse files
author
Evgueni Naverniouk
committed
Documentation
1 parent b4a4b95 commit b95b0ef

File tree

3 files changed

+71
-1
lines changed

3 files changed

+71
-1
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ The plugin has a [recommended configuration](#user-content-recommended-configura
9595
* [self-closing-comp](docs/rules/self-closing-comp.md): Prevent extra closing tags for components without children
9696
* [sort-comp](docs/rules/sort-comp.md): Enforce component methods order
9797
* [sort-prop-types](docs/rules/sort-prop-types.md): Enforce propTypes declarations alphabetical sorting
98+
* [unused-prop-types](docs/rules/unused-prop-types.md): Prevent definitions of unused prop types
9899
* [wrap-multilines](docs/rules/wrap-multilines.md): Prevent missing parentheses around multilines JSX (fixable)
99100

100101
## JSX-specific rules

docs/rules/unused-prop-types.md

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
61+
## About component detection
62+
63+
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.
64+
65+
For now we should detect components created with:
66+
67+
* `React.createClass()`
68+
* an ES6 class that inherit from `React.Component` or `Component`
69+
* a stateless function that return JSX or the result of a `React.createElement` call.

lib/rules/unused-prop-types.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* @fileoverview Warn about unused PropType definitions in React components
2+
* @fileoverview Prevent definitions of unused prop types
33
* @author Evgueni Naverniouk
44
*/
55
'use strict';

0 commit comments

Comments
 (0)