diff --git a/docs/rules/strict.md b/docs/rules/strict.md index a6d87c20ac1..97285afc025 100644 --- a/docs/rules/strict.md +++ b/docs/rules/strict.md @@ -49,6 +49,8 @@ This rule disallows strict mode directives, no matter which option is specified, This rule disallows strict mode directives, no matter which option is specified, in functions with non-simple parameter lists (for example, parameter lists with default parameter values) because that is a syntax error in **ECMAScript 2016** and later. See the examples of the [function](#function) option. +This rule does not apply to class static blocks, no matter which option is specified, because class static blocks do not have directives. Therefore, a `"use strict"` statement in a class static block is not a directive, and will be reported by the [no-unused-expressions](no-unused-expressions.md) rule. + The `--fix` option on the command line does not insert new `"use strict"` statements, but only removes unneeded statements. ## Options diff --git a/tests/lib/rules/strict.js b/tests/lib/rules/strict.js index def56ace4d0..e3ea705fe7e 100644 --- a/tests/lib/rules/strict.js +++ b/tests/lib/rules/strict.js @@ -88,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: [ @@ -589,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 } + ] + } ] });