diff --git a/lib/rules/no-access-state-in-setstate.js b/lib/rules/no-access-state-in-setstate.js index 975104d4a3..72e8081520 100644 --- a/lib/rules/no-access-state-in-setstate.js +++ b/lib/rules/no-access-state-in-setstate.js @@ -107,7 +107,8 @@ module.exports = { if (current.type === 'VariableDeclarator') { vars.push({ node: node, - scope: context.getScope() + scope: context.getScope(), + variableName: current.id.name }); break; } @@ -123,11 +124,14 @@ module.exports = { while (current.parent.type === 'BinaryExpression') { current = current.parent; } - if (current.parent.value === current) { + if ( + current.parent.value === current || + current.parent.object === current + ) { while (current.type !== 'Program') { if (isSetStateCall(current)) { vars - .filter(v => v.scope === context.getScope()) + .filter(v => v.scope === context.getScope() && v.variableName === node.name) .map(v => context.report( v.node, 'Use callback in setState when referencing the previous state.' @@ -136,6 +140,19 @@ module.exports = { current = current.parent; } } + }, + + ObjectPattern(node) { + const isDerivedFromThis = node.parent.init.type === 'ThisExpression'; + node.properties.forEach(property => { + if (property.key.name === 'state' && isDerivedFromThis) { + vars.push({ + node: property.key, + scope: context.getScope(), + variableName: property.key.name + }); + } + }); } }; }