Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix nextProps false positive in no-unused-prop-types #1106

Merged
merged 1 commit into from Mar 31, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 28 additions & 3 deletions lib/rules/no-unused-prop-types.js
Expand Up @@ -18,6 +18,7 @@ var annotations = require('../util/annotations');
// ------------------------------------------------------------------------------

var DIRECT_PROPS_REGEX = /^props\s*(\.|\[)/;
var DIRECT_NEXT_PROPS_REGEX = /^nextProps\s*(\.|\[)/;

// ------------------------------------------------------------------------------
// Rule Definition
Expand Down Expand Up @@ -77,6 +78,24 @@ module.exports = {
return value;
}

/**
* Check if we are in a class constructor
* @return {boolean} true if we are in a class constructor, false if not
**/
function inComponentWillReceiveProps() {
var scope = context.getScope();
while (scope) {
if (
scope.block && scope.block.parent &&
scope.block.parent.key && scope.block.parent.key.name === 'componentWillReceiveProps'
) {
return true;
}
scope = scope.upper;
}
return false;
}

/**
* Checks if we are using a prop
* @param {ASTNode} node The AST node being checked.
Expand All @@ -88,7 +107,8 @@ module.exports = {
node.object.type === 'ThisExpression' && node.property.name === 'props'
);
var isStatelessFunctionUsage = node.object.name === 'props';
return isClassUsage || isStatelessFunctionUsage;
var isNextPropsUsage = node.object.name === 'nextProps' && inComponentWillReceiveProps();
return isClassUsage || isStatelessFunctionUsage || isNextPropsUsage;
}

/**
Expand Down Expand Up @@ -491,12 +511,17 @@ module.exports = {
*/
function getPropertyName(node) {
var isDirectProp = DIRECT_PROPS_REGEX.test(sourceCode.getText(node));
var isDirectNextProp = DIRECT_NEXT_PROPS_REGEX.test(sourceCode.getText(node));
var isInClassComponent = utils.getParentES6Component() || utils.getParentES5Component();
var isNotInConstructor = !inConstructor(node);
if (isDirectProp && isInClassComponent && isNotInConstructor) {
var isNotInComponentWillReceiveProps = !inComponentWillReceiveProps();
if ((isDirectProp || isDirectNextProp)
&& isInClassComponent
&& isNotInConstructor
&& isNotInComponentWillReceiveProps) {
return void 0;
}
if (!isDirectProp) {
if (!isDirectProp && !isDirectNextProp) {
node = node.parent;
}
var property = node.property;
Expand Down
16 changes: 16 additions & 0 deletions tests/lib/rules/no-unused-prop-types.js
Expand Up @@ -1461,6 +1461,22 @@ ruleTester.run('no-unused-prop-types', rule, {
'};'
].join('\n'),
parserOptions: parserOptions
}, {
code: [
'class Hello extends Component {',
' componentWillReceiveProps (nextProps) {',
' if (nextProps.foo) {',
' doSomething(this.props.bar);',
' }',
' }',
'}',

'Hello.propTypes = {',
' foo: PropTypes.bool,',
' bar: PropTypes.bool',
'};'
].join('\n'),
parserOptions: parserOptions
}
],

Expand Down