Skip to content

Commit

Permalink
Fix: require-atomic-updates property assignment message (fixes #15076) (
Browse files Browse the repository at this point in the history
  • Loading branch information
mdjermanovic committed Oct 9, 2021
1 parent f885fe0 commit 2174a6f
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 14 deletions.
29 changes: 21 additions & 8 deletions lib/rules/require-atomic-updates.js
Expand Up @@ -179,7 +179,8 @@ module.exports = {
schema: [],

messages: {
nonAtomicUpdate: "Possible race condition: `{{value}}` might be reassigned based on an outdated value of `{{value}}`."
nonAtomicUpdate: "Possible race condition: `{{value}}` might be reassigned based on an outdated value of `{{value}}`.",
nonAtomicObjectUpdate: "Possible race condition: `{{value}}` might be assigned based on an outdated state of `{{object}}`."
}
},

Expand Down Expand Up @@ -275,13 +276,25 @@ module.exports = {
const variable = reference.resolved;

if (segmentInfo.isOutdated(codePath.currentSegments, variable)) {
context.report({
node: node.parent,
messageId: "nonAtomicUpdate",
data: {
value: sourceCode.getText(node.parent.left)
}
});
if (node.parent.left === reference.identifier) {
context.report({
node: node.parent,
messageId: "nonAtomicUpdate",
data: {
value: variable.name
}
});
} else {
context.report({
node: node.parent,
messageId: "nonAtomicObjectUpdate",
data: {
value: sourceCode.getText(node.parent.left),
object: variable.name
}
});
}

}
}
}
Expand Down
42 changes: 36 additions & 6 deletions tests/lib/rules/require-atomic-updates.js
Expand Up @@ -24,20 +24,20 @@ const VARIABLE_ERROR = {
};

const STATIC_PROPERTY_ERROR = {
messageId: "nonAtomicUpdate",
data: { value: "foo.bar" },
messageId: "nonAtomicObjectUpdate",
data: { value: "foo.bar", object: "foo" },
type: "AssignmentExpression"
};

const COMPUTED_PROPERTY_ERROR = {
messageId: "nonAtomicUpdate",
data: { value: "foo[bar].baz" },
messageId: "nonAtomicObjectUpdate",
data: { value: "foo[bar].baz", object: "foo" },
type: "AssignmentExpression"
};

const PRIVATE_PROPERTY_ERROR = {
messageId: "nonAtomicUpdate",
data: { value: "foo.#bar" },
messageId: "nonAtomicObjectUpdate",
data: { value: "foo.#bar", object: "foo" },
type: "AssignmentExpression"
};

Expand Down Expand Up @@ -328,6 +328,36 @@ ruleTester.run("require-atomic-updates", rule, {
}
`,
errors: [STATIC_PROPERTY_ERROR]
},

// https://github.com/eslint/eslint/issues/15076
{
code: `
async () => {
opts.spec = process.stdin;
try {
const { exit_code } = await run(opts);
process.exitCode = exit_code;
} catch (e) {
process.exitCode = 1;
}
};
`,
env: { node: true },
errors: [
{
messageId: "nonAtomicObjectUpdate",
data: { value: "process.exitCode", object: "process" },
type: "AssignmentExpression",
line: 6
},
{
messageId: "nonAtomicObjectUpdate",
data: { value: "process.exitCode", object: "process" },
type: "AssignmentExpression",
line: 8
}
]
}
]
});

0 comments on commit 2174a6f

Please sign in to comment.