Skip to content

Commit 2174a6f

Browse files
authored
Fix: require-atomic-updates property assignment message (fixes #15076) (#15109)
1 parent f885fe0 commit 2174a6f

File tree

2 files changed

+57
-14
lines changed

2 files changed

+57
-14
lines changed

lib/rules/require-atomic-updates.js

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,8 @@ module.exports = {
179179
schema: [],
180180

181181
messages: {
182-
nonAtomicUpdate: "Possible race condition: `{{value}}` might be reassigned based on an outdated value of `{{value}}`."
182+
nonAtomicUpdate: "Possible race condition: `{{value}}` might be reassigned based on an outdated value of `{{value}}`.",
183+
nonAtomicObjectUpdate: "Possible race condition: `{{value}}` might be assigned based on an outdated state of `{{object}}`."
183184
}
184185
},
185186

@@ -275,13 +276,25 @@ module.exports = {
275276
const variable = reference.resolved;
276277

277278
if (segmentInfo.isOutdated(codePath.currentSegments, variable)) {
278-
context.report({
279-
node: node.parent,
280-
messageId: "nonAtomicUpdate",
281-
data: {
282-
value: sourceCode.getText(node.parent.left)
283-
}
284-
});
279+
if (node.parent.left === reference.identifier) {
280+
context.report({
281+
node: node.parent,
282+
messageId: "nonAtomicUpdate",
283+
data: {
284+
value: variable.name
285+
}
286+
});
287+
} else {
288+
context.report({
289+
node: node.parent,
290+
messageId: "nonAtomicObjectUpdate",
291+
data: {
292+
value: sourceCode.getText(node.parent.left),
293+
object: variable.name
294+
}
295+
});
296+
}
297+
285298
}
286299
}
287300
}

tests/lib/rules/require-atomic-updates.js

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,20 @@ const VARIABLE_ERROR = {
2424
};
2525

2626
const STATIC_PROPERTY_ERROR = {
27-
messageId: "nonAtomicUpdate",
28-
data: { value: "foo.bar" },
27+
messageId: "nonAtomicObjectUpdate",
28+
data: { value: "foo.bar", object: "foo" },
2929
type: "AssignmentExpression"
3030
};
3131

3232
const COMPUTED_PROPERTY_ERROR = {
33-
messageId: "nonAtomicUpdate",
34-
data: { value: "foo[bar].baz" },
33+
messageId: "nonAtomicObjectUpdate",
34+
data: { value: "foo[bar].baz", object: "foo" },
3535
type: "AssignmentExpression"
3636
};
3737

3838
const PRIVATE_PROPERTY_ERROR = {
39-
messageId: "nonAtomicUpdate",
40-
data: { value: "foo.#bar" },
39+
messageId: "nonAtomicObjectUpdate",
40+
data: { value: "foo.#bar", object: "foo" },
4141
type: "AssignmentExpression"
4242
};
4343

@@ -328,6 +328,36 @@ ruleTester.run("require-atomic-updates", rule, {
328328
}
329329
`,
330330
errors: [STATIC_PROPERTY_ERROR]
331+
},
332+
333+
// https://github.com/eslint/eslint/issues/15076
334+
{
335+
code: `
336+
async () => {
337+
opts.spec = process.stdin;
338+
try {
339+
const { exit_code } = await run(opts);
340+
process.exitCode = exit_code;
341+
} catch (e) {
342+
process.exitCode = 1;
343+
}
344+
};
345+
`,
346+
env: { node: true },
347+
errors: [
348+
{
349+
messageId: "nonAtomicObjectUpdate",
350+
data: { value: "process.exitCode", object: "process" },
351+
type: "AssignmentExpression",
352+
line: 6
353+
},
354+
{
355+
messageId: "nonAtomicObjectUpdate",
356+
data: { value: "process.exitCode", object: "process" },
357+
type: "AssignmentExpression",
358+
line: 8
359+
}
360+
]
331361
}
332362
]
333363
});

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy