Skip to content

Commit 11117a4

Browse files
authored
Merge pull request jsx-eslint#1232 from jseminck/no-unused-props-scu-2
Support lifecycle methods with nextProps/prevProps in no-unused-prop-types
2 parents 2748421 + 309d37c commit 11117a4

File tree

2 files changed

+365
-12
lines changed

2 files changed

+365
-12
lines changed

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

+14-11
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@ var annotations = require('../util/annotations');
1616
// Constants
1717
// ------------------------------------------------------------------------------
1818

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

2224
// ------------------------------------------------------------------------------
2325
// Rule Definition
@@ -78,15 +80,16 @@ module.exports = {
7880
}
7981

8082
/**
81-
* Check if we are in a class constructor
83+
* Check if we are in a lifecycle method
8284
* @return {boolean} true if we are in a class constructor, false if not
8385
**/
84-
function inComponentWillReceiveProps() {
86+
function inLifeCycleMethod() {
8587
var scope = context.getScope();
8688
while (scope) {
8789
if (
8890
scope.block && scope.block.parent &&
89-
scope.block.parent.key && scope.block.parent.key.name === 'componentWillReceiveProps'
91+
scope.block.parent.key &&
92+
LIFE_CYCLE_METHODS.indexOf(scope.block.parent.key.name) >= 0
9093
) {
9194
return true;
9295
}
@@ -106,8 +109,7 @@ module.exports = {
106109
node.object.type === 'ThisExpression' && node.property.name === 'props'
107110
);
108111
var isStatelessFunctionUsage = node.object.name === 'props';
109-
var isNextPropsUsage = node.object.name === 'nextProps' && inComponentWillReceiveProps();
110-
return isClassUsage || isStatelessFunctionUsage || isNextPropsUsage;
112+
return isClassUsage || isStatelessFunctionUsage || inLifeCycleMethod();
111113
}
112114

113115
/**
@@ -511,16 +513,17 @@ module.exports = {
511513
function getPropertyName(node) {
512514
var isDirectProp = DIRECT_PROPS_REGEX.test(sourceCode.getText(node));
513515
var isDirectNextProp = DIRECT_NEXT_PROPS_REGEX.test(sourceCode.getText(node));
516+
var isDirectPrevProp = DIRECT_PREV_PROPS_REGEX.test(sourceCode.getText(node));
514517
var isInClassComponent = utils.getParentES6Component() || utils.getParentES5Component();
515518
var isNotInConstructor = !inConstructor(node);
516-
var isNotInComponentWillReceiveProps = !inComponentWillReceiveProps();
517-
if ((isDirectProp || isDirectNextProp)
519+
var isNotInLifeCycleMethod = !inLifeCycleMethod();
520+
if ((isDirectProp || isDirectNextProp || isDirectPrevProp)
518521
&& isInClassComponent
519522
&& isNotInConstructor
520-
&& isNotInComponentWillReceiveProps) {
523+
&& isNotInLifeCycleMethod) {
521524
return void 0;
522525
}
523-
if (!isDirectProp && !isDirectNextProp) {
526+
if (!isDirectProp && !isDirectNextProp && !isDirectPrevProp) {
524527
node = node.parent;
525528
}
526529
var property = node.property;

0 commit comments

Comments
 (0)