]> git.proxmox.com Git - pve-eslint.git/blobdiff - eslint/tests/lib/rules/strict.js
import 8.3.0 source
[pve-eslint.git] / eslint / tests / lib / rules / strict.js
index 92abffda16ccc9b41152b564ebb4b752de05e2a3..e3ea705fe7ef60df0019068d2b48081cd6f1d02b 100644 (file)
 const rule = require("../../../lib/rules/strict"),
     { RuleTester } = require("../../../lib/rule-tester");
 
+//------------------------------------------------------------------------------
+// Tests
+//------------------------------------------------------------------------------
+
 const ruleTester = new RuleTester();
 
 ruleTester.run("strict", rule, {
@@ -84,7 +88,20 @@ ruleTester.run("strict", rule, {
         "function foo() { 'use strict'; return; }",
         { code: "'use strict'; function foo() { return; }", parserOptions: { ecmaFeatures: { globalReturn: true } } },
         { code: "function foo() { return; }", parserOptions: { ecmaVersion: 6, sourceType: "module" } },
-        { code: "function foo() { return; }", parserOptions: { ecmaFeatures: { impliedStrict: true } } }
+        { code: "function foo() { return; }", parserOptions: { ecmaFeatures: { impliedStrict: true } } },
+
+        // class static blocks do not have directive prologues, therefore this rule should never require od disallow "use strict" statement in them.
+        { code: "'use strict'; class C { static { foo; } }", options: ["global"], parserOptions: { ecmaVersion: 2022 } },
+        { code: "'use strict'; class C { static { 'use strict'; } }", options: ["global"], parserOptions: { ecmaVersion: 2022 } },
+        { code: "'use strict'; class C { static { 'use strict'; 'use strict'; } }", options: ["global"], parserOptions: { ecmaVersion: 2022 } },
+        { code: "class C { static { foo; } }", options: ["function"], parserOptions: { ecmaVersion: 2022 } },
+        { code: "class C { static { 'use strict'; } }", options: ["function"], parserOptions: { ecmaVersion: 2022 } },
+        { code: "class C { static { 'use strict'; 'use strict'; } }", options: ["function"], parserOptions: { ecmaVersion: 2022 } },
+        { code: "class C { static { foo; } }", options: ["never"], parserOptions: { ecmaVersion: 2022 } },
+        { code: "class C { static { 'use strict'; } }", options: ["never"], parserOptions: { ecmaVersion: 2022 } },
+        { code: "class C { static { 'use strict'; 'use strict'; } }", options: ["never"], parserOptions: { ecmaVersion: 2022 } },
+        { code: "class C { static { 'use strict'; } }", options: ["safe"], parserOptions: { ecmaVersion: 2022, sourceType: "module" } },
+        { code: "class C { static { 'use strict'; } }", options: ["safe"], parserOptions: { ecmaVersion: 2022, ecmaFeatures: { impliedStrict: true } } }
 
     ],
     invalid: [
@@ -404,7 +421,20 @@ ruleTester.run("strict", rule, {
             parserOptions: { ecmaVersion: 6 },
             errors: [{ messageId: "unnecessaryInClasses", type: "ExpressionStatement" }]
         },
-
+        {
+            code: "class A { field = () => { \"use strict\"; } }",
+            output: "class A { field = () => {  } }",
+            options: ["function"],
+            parserOptions: { ecmaVersion: 2022 },
+            errors: [{ messageId: "unnecessaryInClasses", type: "ExpressionStatement" }]
+        },
+        {
+            code: "class A { field = function() { \"use strict\"; } }",
+            output: "class A { field = function() {  } }",
+            options: ["function"],
+            parserOptions: { ecmaVersion: 2022 },
+            errors: [{ messageId: "unnecessaryInClasses", type: "ExpressionStatement" }]
+        },
 
         // "safe" mode corresponds to "global" if ecmaFeatures.globalReturn is true, otherwise "function"
         {
@@ -572,7 +602,60 @@ ruleTester.run("strict", rule, {
             options: ["function"],
             parserOptions: { ecmaVersion: 6 },
             errors: ["Use the function form of 'use strict'."]
-        }
+        },
 
+        // functions inside class static blocks should be checked
+        {
+            code: "'use strict'; class C { static { function foo() { \n'use strict'; } } }",
+            output: null,
+            options: ["global"],
+            parserOptions: { ecmaVersion: 2022 },
+            errors: [{ messageId: "global", line: 2 }]
+        },
+        {
+            code: "class C { static { function foo() { \n'use strict'; } } }",
+            output: null,
+            options: ["never"],
+            parserOptions: { ecmaVersion: 2022 },
+            errors: [{ messageId: "never", line: 2 }]
+        },
+        {
+            code: "class C { static { function foo() { \n'use strict'; } } }",
+            output: "class C { static { function foo() { \n } } }",
+            options: ["safe"],
+            parserOptions: { ecmaVersion: 2022, sourceType: "module" },
+            errors: [{ messageId: "module", line: 2 }]
+        },
+        {
+            code: "class C { static { function foo() { \n'use strict'; } } }",
+            output: "class C { static { function foo() { \n } } }",
+            options: ["safe"],
+            parserOptions: { ecmaVersion: 2022, ecmaFeatures: { impliedStrict: true } },
+            errors: [{ messageId: "implied", line: 2 }]
+        },
+        {
+            code: "function foo() {'use strict'; class C { static { function foo() { \n'use strict'; } } } }",
+            output: "function foo() {'use strict'; class C { static { function foo() { \n } } } }",
+            options: ["function"],
+            parserOptions: { ecmaVersion: 2022 },
+            errors: [{ messageId: "unnecessary", line: 2 }]
+        },
+        {
+            code: "class C { static { function foo() { \n'use strict'; } } }",
+            output: "class C { static { function foo() { \n } } }",
+            options: ["function"],
+            parserOptions: { ecmaVersion: 2022 },
+            errors: [{ messageId: "unnecessaryInClasses", line: 2 }]
+        },
+        {
+            code: "class C { static { function foo() { \n'use strict';\n'use strict'; } } }",
+            output: "class C { static { function foo() { \n\n } } }",
+            options: ["function"],
+            parserOptions: { ecmaVersion: 2022 },
+            errors: [
+                { messageId: "unnecessaryInClasses", line: 2 },
+                { messageId: "multiple", line: 3 }
+            ]
+        }
     ]
 });