Skip to content

Commit 1dfff04

Browse files
Yann PringaultKerumen
Yann Pringault
authored andcommitted
Fix nextProps false positive in no-unused-prop-types
1 parent b646485 commit 1dfff04

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

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

+23-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ var annotations = require('../util/annotations');
1717
// Constants
1818
// ------------------------------------------------------------------------------
1919

20-
var DIRECT_PROPS_REGEX = /^props\s*(\.|\[)/;
20+
var DIRECT_PROPS_REGEX = /^(props|nextProps)\s*(\.|\[)/;
2121

2222
// ------------------------------------------------------------------------------
2323
// Rule Definition
@@ -77,6 +77,24 @@ module.exports = {
7777
return value;
7878
}
7979

80+
/**
81+
* Check if we are in a class constructor
82+
* @return {boolean} true if we are in a class constructor, false if not
83+
**/
84+
function inComponentWillReceiveProps() {
85+
var scope = context.getScope();
86+
while (scope) {
87+
if (
88+
scope.block && scope.block.parent &&
89+
scope.block.parent.key && scope.block.parent.key.name === 'componentWillReceiveProps'
90+
) {
91+
return true;
92+
}
93+
scope = scope.upper;
94+
}
95+
return false;
96+
}
97+
8098
/**
8199
* Checks if we are using a prop
82100
* @param {ASTNode} node The AST node being checked.
@@ -88,7 +106,8 @@ module.exports = {
88106
node.object.type === 'ThisExpression' && node.property.name === 'props'
89107
);
90108
var isStatelessFunctionUsage = node.object.name === 'props';
91-
return isClassUsage || isStatelessFunctionUsage;
109+
var isNextPropsUsage = node.object.name === 'nextProps' && inComponentWillReceiveProps();
110+
return isClassUsage || isStatelessFunctionUsage || isNextPropsUsage;
92111
}
93112

94113
/**
@@ -493,7 +512,8 @@ module.exports = {
493512
var isDirectProp = DIRECT_PROPS_REGEX.test(sourceCode.getText(node));
494513
var isInClassComponent = utils.getParentES6Component() || utils.getParentES5Component();
495514
var isNotInConstructor = !inConstructor(node);
496-
if (isDirectProp && isInClassComponent && isNotInConstructor) {
515+
var isNotInComponentWillReceiveProps = !inComponentWillReceiveProps();
516+
if (isDirectProp && isInClassComponent && isNotInConstructor && isNotInComponentWillReceiveProps) {
497517
return void 0;
498518
}
499519
if (!isDirectProp) {

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

+16
Original file line numberDiff line numberDiff line change
@@ -1461,6 +1461,22 @@ ruleTester.run('no-unused-prop-types', rule, {
14611461
'};'
14621462
].join('\n'),
14631463
parserOptions: parserOptions
1464+
}, {
1465+
code: [
1466+
'class Hello extends Component {',
1467+
' componentWillReceiveProps (nextProps) {',
1468+
' if (nextProps.foo) {',
1469+
' doSomething(this.props.bar);',
1470+
' }',
1471+
' }',
1472+
'}',
1473+
1474+
'Hello.propTypes = {',
1475+
' foo: PropTypes.bool,',
1476+
' bar: PropTypes.bool',
1477+
'};'
1478+
].join('\n'),
1479+
parserOptions: parserOptions
14641480
}
14651481
],
14661482

0 commit comments

Comments
 (0)