From 2174a6f0e5d18b673604d31e3ca7b790cdc9429b Mon Sep 17 00:00:00 2001 From: Milos Djermanovic Date: Sat, 9 Oct 2021 22:25:47 +0200 Subject: [PATCH] Fix: require-atomic-updates property assignment message (fixes #15076) (#15109) --- lib/rules/require-atomic-updates.js | 29 +++++++++++----- tests/lib/rules/require-atomic-updates.js | 42 +++++++++++++++++++---- 2 files changed, 57 insertions(+), 14 deletions(-) diff --git a/lib/rules/require-atomic-updates.js b/lib/rules/require-atomic-updates.js index b25f150f6e0..9eee4ca38bc 100644 --- a/lib/rules/require-atomic-updates.js +++ b/lib/rules/require-atomic-updates.js @@ -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}}`." } }, @@ -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 + } + }); + } + } } } diff --git a/tests/lib/rules/require-atomic-updates.js b/tests/lib/rules/require-atomic-updates.js index f3d8e1d76a6..be931b7e1d0 100644 --- a/tests/lib/rules/require-atomic-updates.js +++ b/tests/lib/rules/require-atomic-updates.js @@ -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" }; @@ -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 + } + ] } ] });