Skip to content

Commit

Permalink
Fix: require-atomic-updates false positives in loops (fixes #14208)
Browse files Browse the repository at this point in the history
  • Loading branch information
patriscus committed Mar 31, 2021
1 parent cd2e584 commit 88da7f0
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lib/rules/require-atomic-updates.js
Expand Up @@ -224,13 +224,16 @@ module.exports = {
segmentInfo.markAsRead(codePath.currentSegments, name);
}

const isVariableDeclaration = isLocalVariableWithoutEscape(variable, isMemberAccess) ||
node.parent.type === "VariableDeclarator";

/*
* Register the variable to verify after ESLint traversed the `writeExpr` node
* if this reference is an assignment to a variable which is referred from other closure.
*/
if (writeExpr &&
writeExpr.parent.right === writeExpr && // ← exclude variable declarations.
!isLocalVariableWithoutEscape(variable, isMemberAccess)
writeExpr.parent.right === writeExpr &&
!isVariableDeclaration
) {
let refs = assignmentReferences.get(writeExpr);

Expand Down
24 changes: 24 additions & 0 deletions tests/lib/rules/require-atomic-updates.js
Expand Up @@ -162,6 +162,30 @@ ruleTester.run("require-atomic-updates", rule, {
count -= 1
return
}
`,

// https://github.com/eslint/eslint/issues/14208
`
async function foo(e) {
}
async function run() {
const input = [];
const props = [];
for(const entry of input) {
const prop = props.find(a => a.id === entry.id) || null;
await foo(entry);
}
for(const entry of input) {
const prop = props.find(a => a.id === entry.id) || null;
}
for(const entry2 of input) {
const prop = props.find(a => a.id === entry2.id) || null;
}
}
`
],

Expand Down

0 comments on commit 88da7f0

Please sign in to comment.