]> git.proxmox.com Git - pve-eslint.git/blobdiff - eslint/tests/lib/rules/require-atomic-updates.js
import 8.3.0 source
[pve-eslint.git] / eslint / tests / lib / rules / require-atomic-updates.js
index bd3738ac979dda0f23d378174f5a0ec8649cee64..53d85b752898af22f7740e5809fa52c8cf347ce3 100644 (file)
 const rule = require("../../../lib/rules/require-atomic-updates");
 const { RuleTester } = require("../../../lib/rule-tester");
 
-
 //------------------------------------------------------------------------------
 // Tests
 //------------------------------------------------------------------------------
 
-const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 2018 } });
+const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 2022 } });
 
 const VARIABLE_ERROR = {
     messageId: "nonAtomicUpdate",
@@ -25,14 +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: "nonAtomicObjectUpdate",
+    data: { value: "foo.#bar", object: "foo" },
     type: "AssignmentExpression"
 };
 
@@ -209,7 +214,29 @@ ruleTester.run("require-atomic-updates", rule, {
                 await a;
                 b = 1;
             }
-        `
+        `,
+
+        // allowProperties
+        {
+            code: `
+                async function a(foo) {
+                    if (foo.bar) {
+                        foo.bar = await something;
+                    }
+                }
+            `,
+            options: [{ allowProperties: true }]
+        },
+        {
+            code: `
+                function* g(foo) {
+                    baz = foo.bar;
+                    yield something;
+                    foo.bar = 1;
+                }
+            `,
+            options: [{ allowProperties: true }]
+        }
     ],
 
     invalid: [
@@ -269,6 +296,10 @@ ruleTester.run("require-atomic-updates", rule, {
             code: "const foo = []; async function x() { foo[bar].baz += await result;  }",
             errors: [COMPUTED_PROPERTY_ERROR]
         },
+        {
+            code: "const foo = {}; class C { #bar; async wrap() { foo.#bar += await baz } }",
+            errors: [PRIVATE_PROPERTY_ERROR]
+        },
         {
             code: "let foo; async function* x() { foo = (yield foo) + await bar; }",
             errors: [VARIABLE_ERROR]
@@ -319,6 +350,129 @@ 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
+                }
+            ]
+        },
+
+        // allowProperties
+        {
+            code: `
+                async function a(foo) {
+                    if (foo.bar) {
+                        foo.bar = await something;
+                    }
+                }
+            `,
+            errors: [STATIC_PROPERTY_ERROR]
+        },
+        {
+            code: `
+                function* g(foo) {
+                    baz = foo.bar;
+                    yield something;
+                    foo.bar = 1;
+                }
+            `,
+            errors: [STATIC_PROPERTY_ERROR]
+        },
+        {
+            code: `
+                async function a(foo) {
+                    if (foo.bar) {
+                        foo.bar = await something;
+                    }
+                }
+            `,
+            options: [{}],
+            errors: [STATIC_PROPERTY_ERROR]
+
+        },
+        {
+            code: `
+                function* g(foo) {
+                    baz = foo.bar;
+                    yield something;
+                    foo.bar = 1;
+                }
+            `,
+            options: [{}],
+            errors: [STATIC_PROPERTY_ERROR]
+        },
+        {
+            code: `
+                async function a(foo) {
+                    if (foo.bar) {
+                        foo.bar = await something;
+                    }
+                }
+            `,
+            options: [{ allowProperties: false }],
+            errors: [STATIC_PROPERTY_ERROR]
+
+        },
+        {
+            code: `
+                function* g(foo) {
+                    baz = foo.bar;
+                    yield something;
+                    foo.bar = 1;
+                }
+            `,
+            options: [{ allowProperties: false }],
+            errors: [STATIC_PROPERTY_ERROR]
+        },
+        {
+            code: `
+                let foo;
+                async function a() {
+                    if (foo) {
+                        foo = await something;
+                    }
+                }
+            `,
+            options: [{ allowProperties: true }],
+            errors: [VARIABLE_ERROR]
+
+        },
+        {
+            code: `
+                let foo;
+                function* g() {
+                    baz = foo;
+                    yield something;
+                    foo = 1;
+                }
+            `,
+            options: [{ allowProperties: true }],
+            errors: [VARIABLE_ERROR]
         }
     ]
 });