Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: the strict rule does not apply to class static blocks #15314

Merged
merged 1 commit into from Nov 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 }
]
}
]
});