Skip to content

Commit 216af61

Browse files
committed
Add ignore option to prop-types rule
1 parent 74f1130 commit 216af61

File tree

3 files changed

+46
-8
lines changed

3 files changed

+46
-8
lines changed

docs/rules/prop-types.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,16 @@ var Hello = React.createClass({
5151
}
5252
});
5353
```
54+
55+
## Rule Options
56+
57+
This rule can take one argument to ignore some specific props during validation.
58+
59+
```
60+
...
61+
"prop-types": [<enabled>, { ignore: <ignore> }]
62+
...
63+
```
64+
65+
* `enabled`: for enabling the rule. 0=off, 1=warn, 2=error. Defaults to 0.
66+
* `ignore`: optional array of props name to ignore during validation.

lib/rules/prop-types.js

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ var ComponentList = componentUtil.List;
1313

1414
module.exports = function(context) {
1515

16+
var configuration = context.options[0] || {};
17+
var ignored = configuration.ignore || [];
18+
1619
var componentList = new ComponentList();
1720

1821
var MISSING_MESSAGE = '\'{{name}}\' is missing in props validation';
@@ -42,6 +45,28 @@ module.exports = function(context) {
4245
);
4346
}
4447

48+
/**
49+
* Checks if the prop is ignored
50+
* @param {String} name Name of the prop to check.
51+
* @returns {Boolean} True if the prop is ignored, false if not.
52+
*/
53+
function isIgnored(name) {
54+
return ignored.indexOf(name) !== -1;
55+
}
56+
57+
/**
58+
* Checks if the prop is declared
59+
* @param {String} name Name of the prop to check.
60+
* @param {Object} component The component to process
61+
* @returns {Boolean} True if the prop is declared, false if not.
62+
*/
63+
function isDeclaredInComponent(component, name) {
64+
return (
65+
component.declaredPropTypes &&
66+
component.declaredPropTypes.indexOf(name) !== -1
67+
);
68+
}
69+
4570
/**
4671
* Mark a prop type as used
4772
* @param {ASTNode} node The AST node being marked.
@@ -117,25 +142,22 @@ module.exports = function(context) {
117142

118143
/**
119144
* Reports undeclared proptypes for a given component
120-
* @param {Object} id The component to process
145+
* @param {Object} component The component to process
121146
*/
122147
function reportUndeclaredPropTypes(component) {
123148
if (!component || !component.usedPropTypes || component.ignorePropsValidation === true) {
124149
return;
125150
}
151+
var name;
126152
for (var i = 0, j = component.usedPropTypes.length; i < j; i++) {
127-
var isDeclared =
128-
component.declaredPropTypes &&
129-
component.declaredPropTypes.indexOf(component.usedPropTypes[i].name) !== -1
130-
;
131-
var isChildren = component.usedPropTypes[i].name === 'children';
132-
if (isDeclared || isChildren) {
153+
name = component.usedPropTypes[i].name;
154+
if (isDeclaredInComponent(component, name) || isIgnored(name)) {
133155
continue;
134156
}
135157
context.report(
136158
component.usedPropTypes[i].node,
137159
component.name === componentUtil.DEFAULT_COMPONENT_NAME ? MISSING_MESSAGE : MISSING_MESSAGE_NAMED_COMP, {
138-
name: component.usedPropTypes[i].name,
160+
name: name,
139161
component: component.name
140162
}
141163
);

tests/lib/rules/prop-types.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ eslintTester.addRuleTest('lib/rules/prop-types', {
6666
' }',
6767
'});'
6868
].join('\n'),
69+
args: [1, {
70+
ignore: ['children']
71+
}],
6972
ecmaFeatures: {
7073
jsx: true
7174
}

0 commit comments

Comments
 (0)