Skip to content

Commit c568d49

Browse files
authored
Merge pull request #1253 from dustinsoftware/forbidExtraProps
[New] `no-unused-prop-types`: Add support for propTypes wrapped in a function
2 parents ab77cc9 + 5770f29 commit c568d49

File tree

3 files changed

+61
-1
lines changed

3 files changed

+61
-1
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,14 @@ This rule can take one argument to ignore some specific props during validation.
4747

4848
```js
4949
...
50-
"react/no-unused-prop-types": [<enabled>, { customValidators: <customValidator>, skipShapeProps: <skipShapeProps> }]
50+
"react/no-unused-prop-types": [<enabled>, { customValidators: <customValidator>, skipShapeProps: <skipShapeProps>, propWrapperFunctions: <propWrapperFunctions> }]
5151
...
5252
```
5353

5454
* `enabled`: for enabling the rule. 0=off, 1=warn, 2=error. Defaults to 0.
5555
* `customValidators`: optional array of validators used for propTypes validation.
5656
* `skipShapeProps`: In some cases it is impossible to accurately detect whether or not a `PropTypes.shape`'s values are being used. Setting this option to `true` will skip validation of `PropTypes.shape` (`true` by default).
57+
* `propWrapperFunctions`: The names of any functions used to wrap the propTypes object, such as `forbidExtraProps`. If this isn't set, any propTypes wrapped in a function will be skipped.
5758

5859
## Caveats
5960

lib/rules/no-unused-prop-types.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ module.exports = {
4444
},
4545
skipShapeProps: {
4646
type: 'boolean'
47+
},
48+
propWrapperFunctions: {
49+
type: 'array',
50+
items: {
51+
type: 'string'
52+
},
53+
uniqueItems: true
4754
}
4855
},
4956
additionalProperties: false
@@ -56,6 +63,8 @@ module.exports = {
5663
var configuration = Object.assign({}, defaults, context.options[0] || {});
5764
var skipShapeProps = configuration.skipShapeProps;
5865
var customValidators = configuration.customValidators || [];
66+
var propWrapperFunctions = new Set(configuration.propWrapperFunctions || []);
67+
5968
// Used to track the type annotations in scope.
6069
// Necessary because babel's scopes do not track type annotations.
6170
var stack = null;
@@ -722,6 +731,15 @@ module.exports = {
722731
}
723732
ignorePropsValidation = true;
724733
break;
734+
case 'CallExpression':
735+
if (
736+
propWrapperFunctions.has(propTypes.callee.name) &&
737+
propTypes.arguments && propTypes.arguments[0]
738+
) {
739+
markPropTypesAsDeclared(node, propTypes.arguments[0]);
740+
return;
741+
}
742+
break;
725743
case null:
726744
break;
727745
default:

tests/lib/rules/no-unused-prop-types.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2779,6 +2779,47 @@ ruleTester.run('no-unused-prop-types', rule, {
27792779
line: 10,
27802780
column: 8
27812781
}]
2782+
}, {
2783+
code: [
2784+
'class Hello extends Component {',
2785+
' componentDidUpdate (nextProps) {',
2786+
' if (nextProps.foo) {',
2787+
' return true;',
2788+
' }',
2789+
' }',
2790+
'}',
2791+
'Hello.propTypes = forbidExtraProps({',
2792+
' foo: PropTypes.string,',
2793+
' bar: PropTypes.string,',
2794+
'});'
2795+
].join('\n'),
2796+
errors: [{
2797+
message: '\'bar\' PropType is defined but prop is never used',
2798+
line: 10,
2799+
column: 8
2800+
}],
2801+
options: [{propWrapperFunctions: ['forbidExtraProps']}]
2802+
}, {
2803+
code: [
2804+
'class Hello extends Component {',
2805+
' propTypes = forbidExtraProps({',
2806+
' foo: PropTypes.string,',
2807+
' bar: PropTypes.string',
2808+
' });',
2809+
' componentDidUpdate (nextProps) {',
2810+
' if (nextProps.foo) {',
2811+
' return true;',
2812+
' }',
2813+
' }',
2814+
'};'
2815+
].join('\n'),
2816+
parser: 'babel-eslint',
2817+
errors: [{
2818+
message: '\'bar\' PropType is defined but prop is never used',
2819+
line: 4,
2820+
column: 10
2821+
}],
2822+
options: [{propWrapperFunctions: ['forbidExtraProps']}]
27822823
}
27832824
/* , {
27842825
// Enable this when the following issue is fixed

0 commit comments

Comments
 (0)