Skip to content

Commit

Permalink
docs: the strict rule does not apply to class static blocks (#15314)
Browse files Browse the repository at this point in the history
Documents that class static blocks do not have directives, and therefore the `strict` rule does not apply to them.

Also adds tests to confirm this behavior.

Refs #15016
  • Loading branch information
mdjermanovic committed Nov 16, 2021
1 parent f6ae7dc commit 3ae5258
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 2 deletions.
2 changes: 2 additions & 0 deletions docs/rules/strict.md
Expand Up @@ -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
Expand Down
70 changes: 68 additions & 2 deletions tests/lib/rules/strict.js
Expand Up @@ -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: [
Expand Down Expand Up @@ -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 }
]
}
]
});

0 comments on commit 3ae5258

Please sign in to comment.