Skip to content

Support lifecycle methods with nextProps/prevProps in no-unused-prop-types #1232

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

Merged
merged 6 commits into from
Jun 2, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions lib/rules/no-unused-prop-types.js
Original file line number Diff line number Diff line change
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
Loading