Skip to content

Commit

Permalink
Merge pull request #1232 from jseminck/no-unused-props-scu-2
Browse files Browse the repository at this point in the history
Support lifecycle methods with nextProps/prevProps in no-unused-prop-types
  • Loading branch information
ljharb committed Jun 2, 2017
2 parents 2748421 + 309d37c commit 11117a4
Show file tree
Hide file tree
Showing 2 changed files with 365 additions and 12 deletions.
25 changes: 14 additions & 11 deletions lib/rules/no-unused-prop-types.js
Expand Up @@ -16,8 +16,10 @@ var annotations = require('../util/annotations');
// Constants
// ------------------------------------------------------------------------------

var DIRECT_PROPS_REGEX = /^props\s*(\.|\[)/;
var DIRECT_NEXT_PROPS_REGEX = /^nextProps\s*(\.|\[)/;
const DIRECT_PROPS_REGEX = /^props\s*(\.|\[)/;
const DIRECT_NEXT_PROPS_REGEX = /^nextProps\s*(\.|\[)/;
const DIRECT_PREV_PROPS_REGEX = /^prevProps\s*(\.|\[)/;
const LIFE_CYCLE_METHODS = ['componentWillReceiveProps', 'shouldComponentUpdate', 'componentWillUpdate', 'componentDidUpdate'];

// ------------------------------------------------------------------------------
// Rule Definition
Expand Down Expand Up @@ -78,15 +80,16 @@ module.exports = {
}

/**
* Check if we are in a class constructor
* Check if we are in a lifecycle method
* @return {boolean} true if we are in a class constructor, false if not
**/
function inComponentWillReceiveProps() {
function inLifeCycleMethod() {
var scope = context.getScope();
while (scope) {
if (
scope.block && scope.block.parent &&
scope.block.parent.key && scope.block.parent.key.name === 'componentWillReceiveProps'
scope.block.parent.key &&
LIFE_CYCLE_METHODS.indexOf(scope.block.parent.key.name) >= 0
) {
return true;
}
Expand All @@ -106,8 +109,7 @@ module.exports = {
node.object.type === 'ThisExpression' && node.property.name === 'props'
);
var isStatelessFunctionUsage = node.object.name === 'props';
var isNextPropsUsage = node.object.name === 'nextProps' && inComponentWillReceiveProps();
return isClassUsage || isStatelessFunctionUsage || isNextPropsUsage;
return isClassUsage || isStatelessFunctionUsage || inLifeCycleMethod();
}

/**
Expand Down Expand Up @@ -511,16 +513,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 isDirectPrevProp = DIRECT_PREV_PROPS_REGEX.test(sourceCode.getText(node));
var isInClassComponent = utils.getParentES6Component() || utils.getParentES5Component();
var isNotInConstructor = !inConstructor(node);
var isNotInComponentWillReceiveProps = !inComponentWillReceiveProps();
if ((isDirectProp || isDirectNextProp)
var isNotInLifeCycleMethod = !inLifeCycleMethod();
if ((isDirectProp || isDirectNextProp || isDirectPrevProp)
&& isInClassComponent
&& isNotInConstructor
&& isNotInComponentWillReceiveProps) {
&& isNotInLifeCycleMethod) {
return void 0;
}
if (!isDirectProp && !isDirectNextProp) {
if (!isDirectProp && !isDirectNextProp && !isDirectPrevProp) {
node = node.parent;
}
var property = node.property;
Expand Down

0 comments on commit 11117a4

Please sign in to comment.