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] prop-types: false positive with setState updater #2359

Merged
merged 1 commit into from Jul 28, 2019
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
33 changes: 12 additions & 21 deletions lib/util/usedPropTypes.js
Expand Up @@ -135,25 +135,16 @@ function isInLifeCycleMethod(node, checkAsyncSafeLifeCycles) {
}

/**
* Check if the current node is in a setState updater method
* @return {boolean} true if we are in a setState updater, false if not
* Check if a function node is a setState updater
* @param {ASTNode} node a function node
* @return {boolean}
*/
function inSetStateUpdater(context) {
let scope = context.getScope();
while (scope) {
if (
scope.block && scope.block.parent &&
scope.block.parent.type === 'CallExpression' &&
scope.block.parent.callee.property &&
scope.block.parent.callee.property.name === 'setState' &&
// Make sure we are in the updater not the callback
scope.block.parent.arguments[0].start === scope.block.start
) {
return true;
}
scope = scope.upper;
}
return false;
function isSetStateUpdater(node) {
return node.parent.type === 'CallExpression' &&
node.parent.callee.property &&
node.parent.callee.property.name === 'setState' &&
// Make sure we are in the updater not the callback
node.parent.arguments[0] === node;
}

function isPropArgumentInSetStateUpdater(context, name) {
Expand Down Expand Up @@ -324,7 +315,7 @@ module.exports = function usedPropTypesInstructions(context, components, utils)
break;
}
type = 'destructuring';
const propParam = inSetStateUpdater(context) ? node.params[1] : node.params[0];
const propParam = isSetStateUpdater(node) ? node.params[1] : node.params[0];
properties = propParam.type === 'AssignmentPattern' ?
propParam.left.properties :
propParam.properties;
Expand Down Expand Up @@ -399,7 +390,7 @@ module.exports = function usedPropTypesInstructions(context, components, utils)
* FunctionDeclaration, or FunctionExpression
*/
function markDestructuredFunctionArgumentsAsUsed(node) {
const param = node.params && inSetStateUpdater(context) ? node.params[1] : node.params[0];
const param = node.params && isSetStateUpdater(node) ? node.params[1] : node.params[0];

const destructuring = param && (
param.type === 'ObjectPattern' ||
Expand All @@ -412,7 +403,7 @@ module.exports = function usedPropTypesInstructions(context, components, utils)
}

function handleSetStateUpdater(node) {
if (!node.params || node.params.length < 2 || !inSetStateUpdater(context)) {
if (!node.params || node.params.length < 2 || !isSetStateUpdater(node)) {
return;
}
markPropTypesAsUsed(node);
Expand Down
11 changes: 11 additions & 0 deletions tests/lib/rules/prop-types.js
Expand Up @@ -2065,6 +2065,17 @@ ruleTester.run('prop-types', rule, {
};
`
},
{
code: `
class Foo extends React.Component {
bar() {
this.setState((state, props) => {
function f(_, {aaaaaaa}) {}
});
}
}
`
},
{
code: `
class Foo extends React.Component {
Expand Down