Skip to content

Commit

Permalink
Fix false positive in no-unused-prop-types (fixes #801)
Browse files Browse the repository at this point in the history
  • Loading branch information
Evgueni Naverniouk authored and yannickcr committed Sep 12, 2016
1 parent bb5b1df commit 79b56b4
Show file tree
Hide file tree
Showing 2 changed files with 429 additions and 3 deletions.
71 changes: 68 additions & 3 deletions lib/rules/no-unused-prop-types.js
Expand Up @@ -174,6 +174,53 @@ module.exports = {
);
}

/**
* Returns true if the given node is a React Component lifecycle method
* @param {ASTNode} node The AST node being checked.
* @return {Boolean} True if the node is a lifecycle method
*/
function isNodeALifeCycleMethod(node) {
var nodeKeyName = (node.key || {}).name;
return (
node.kind === 'constructor' ||
nodeKeyName === 'componentWillReceiveProps' ||
nodeKeyName === 'shouldComponentUpdate' ||
nodeKeyName === 'componentWillUpdate' ||
nodeKeyName === 'componentDidUpdate'
);
}

/**
* Returns true if the given node is inside a React Component lifecycle
* method.
* @param {ASTNode} node The AST node being checked.
* @return {Boolean} True if the node is inside a lifecycle method
*/
function isInLifeCycleMethod(node) {
if (node.type === 'MethodDefinition' && isNodeALifeCycleMethod(node)) {
return true;
}

if (node.parent) {
return isInLifeCycleMethod(node.parent);
}

return false;
}

/**
* Checks if a prop init name matches common naming patterns
* @param {ASTNode} node The AST node being checked.
* @returns {Boolean} True if the prop name matches
*/
function isPropAttributeName (node) {
return (
node.init.name === 'props' ||
node.init.name === 'nextProps' ||
node.init.name === 'prevProps'
);
}

/**
* Checks if a prop is used
* @param {ASTNode} node The AST node being checked.
Expand Down Expand Up @@ -536,11 +583,14 @@ module.exports = {
node.id.properties[i].value.type === 'ObjectPattern'
);
// let {firstname} = props
var statelessDestructuring = node.init.name === 'props' && utils.getParentStatelessComponent();
var genericDestructuring = isPropAttributeName(node) && (
utils.getParentStatelessComponent() ||
isInLifeCycleMethod(node)
);

if (thisDestructuring) {
properties = node.id.properties[i].value.properties;
} else if (statelessDestructuring) {
} else if (genericDestructuring) {
properties = node.id.properties;
} else {
continue;
Expand Down Expand Up @@ -776,7 +826,10 @@ module.exports = {
// let {props: {firstname}} = this
var thisDestructuring = destructuring && node.init.type === 'ThisExpression';
// let {firstname} = props
var statelessDestructuring = destructuring && node.init.name === 'props' && utils.getParentStatelessComponent();
var statelessDestructuring = destructuring && isPropAttributeName(node) && (
utils.getParentStatelessComponent() ||
isInLifeCycleMethod(node)
);

if (!thisDestructuring && !statelessDestructuring) {
return;
Expand Down Expand Up @@ -831,6 +884,18 @@ module.exports = {
}
},

ObjectPattern: function(node) {
// If the object pattern is a destructured props object in a lifecycle
// method -- mark it for used props.
if (isNodeALifeCycleMethod(node.parent.parent)) {
node.properties.forEach(function(property, i) {
if (i === 0) {
markPropTypesAsUsed(node.parent);
}
});
}
},

ObjectExpression: function(node) {
// Search for the proptypes declaration
node.properties.forEach(function(property) {
Expand Down

0 comments on commit 79b56b4

Please sign in to comment.