Skip to content

Commit

Permalink
Make no-access-state-in-setstate find cases where state is destructur…
Browse files Browse the repository at this point in the history
…ed from this
  • Loading branch information
jaaberg committed Dec 11, 2017
1 parent 73f135a commit 4950623
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions lib/rules/no-access-state-in-setstate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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.'
Expand All @@ -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
});
}
});
}
};
}
Expand Down

0 comments on commit 4950623

Please sign in to comment.