Skip to content

Commit 3ae5258

Browse files
authored
docs: the strict rule does not apply to class static blocks (#15314)
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
1 parent f6ae7dc commit 3ae5258

File tree

2 files changed

+70
-2
lines changed

2 files changed

+70
-2
lines changed

docs/rules/strict.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ This rule disallows strict mode directives, no matter which option is specified,
4949

5050
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.
5151

52+
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.
53+
5254
The `--fix` option on the command line does not insert new `"use strict"` statements, but only removes unneeded statements.
5355

5456
## Options

tests/lib/rules/strict.js

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,20 @@ ruleTester.run("strict", rule, {
8888
"function foo() { 'use strict'; return; }",
8989
{ code: "'use strict'; function foo() { return; }", parserOptions: { ecmaFeatures: { globalReturn: true } } },
9090
{ code: "function foo() { return; }", parserOptions: { ecmaVersion: 6, sourceType: "module" } },
91-
{ code: "function foo() { return; }", parserOptions: { ecmaFeatures: { impliedStrict: true } } }
91+
{ code: "function foo() { return; }", parserOptions: { ecmaFeatures: { impliedStrict: true } } },
92+
93+
// class static blocks do not have directive prologues, therefore this rule should never require od disallow "use strict" statement in them.
94+
{ code: "'use strict'; class C { static { foo; } }", options: ["global"], parserOptions: { ecmaVersion: 2022 } },
95+
{ code: "'use strict'; class C { static { 'use strict'; } }", options: ["global"], parserOptions: { ecmaVersion: 2022 } },
96+
{ code: "'use strict'; class C { static { 'use strict'; 'use strict'; } }", options: ["global"], parserOptions: { ecmaVersion: 2022 } },
97+
{ code: "class C { static { foo; } }", options: ["function"], parserOptions: { ecmaVersion: 2022 } },
98+
{ code: "class C { static { 'use strict'; } }", options: ["function"], parserOptions: { ecmaVersion: 2022 } },
99+
{ code: "class C { static { 'use strict'; 'use strict'; } }", options: ["function"], parserOptions: { ecmaVersion: 2022 } },
100+
{ code: "class C { static { foo; } }", options: ["never"], parserOptions: { ecmaVersion: 2022 } },
101+
{ code: "class C { static { 'use strict'; } }", options: ["never"], parserOptions: { ecmaVersion: 2022 } },
102+
{ code: "class C { static { 'use strict'; 'use strict'; } }", options: ["never"], parserOptions: { ecmaVersion: 2022 } },
103+
{ code: "class C { static { 'use strict'; } }", options: ["safe"], parserOptions: { ecmaVersion: 2022, sourceType: "module" } },
104+
{ code: "class C { static { 'use strict'; } }", options: ["safe"], parserOptions: { ecmaVersion: 2022, ecmaFeatures: { impliedStrict: true } } }
92105

93106
],
94107
invalid: [
@@ -589,7 +602,60 @@ ruleTester.run("strict", rule, {
589602
options: ["function"],
590603
parserOptions: { ecmaVersion: 6 },
591604
errors: ["Use the function form of 'use strict'."]
592-
}
605+
},
593606

607+
// functions inside class static blocks should be checked
608+
{
609+
code: "'use strict'; class C { static { function foo() { \n'use strict'; } } }",
610+
output: null,
611+
options: ["global"],
612+
parserOptions: { ecmaVersion: 2022 },
613+
errors: [{ messageId: "global", line: 2 }]
614+
},
615+
{
616+
code: "class C { static { function foo() { \n'use strict'; } } }",
617+
output: null,
618+
options: ["never"],
619+
parserOptions: { ecmaVersion: 2022 },
620+
errors: [{ messageId: "never", line: 2 }]
621+
},
622+
{
623+
code: "class C { static { function foo() { \n'use strict'; } } }",
624+
output: "class C { static { function foo() { \n } } }",
625+
options: ["safe"],
626+
parserOptions: { ecmaVersion: 2022, sourceType: "module" },
627+
errors: [{ messageId: "module", line: 2 }]
628+
},
629+
{
630+
code: "class C { static { function foo() { \n'use strict'; } } }",
631+
output: "class C { static { function foo() { \n } } }",
632+
options: ["safe"],
633+
parserOptions: { ecmaVersion: 2022, ecmaFeatures: { impliedStrict: true } },
634+
errors: [{ messageId: "implied", line: 2 }]
635+
},
636+
{
637+
code: "function foo() {'use strict'; class C { static { function foo() { \n'use strict'; } } } }",
638+
output: "function foo() {'use strict'; class C { static { function foo() { \n } } } }",
639+
options: ["function"],
640+
parserOptions: { ecmaVersion: 2022 },
641+
errors: [{ messageId: "unnecessary", line: 2 }]
642+
},
643+
{
644+
code: "class C { static { function foo() { \n'use strict'; } } }",
645+
output: "class C { static { function foo() { \n } } }",
646+
options: ["function"],
647+
parserOptions: { ecmaVersion: 2022 },
648+
errors: [{ messageId: "unnecessaryInClasses", line: 2 }]
649+
},
650+
{
651+
code: "class C { static { function foo() { \n'use strict';\n'use strict'; } } }",
652+
output: "class C { static { function foo() { \n\n } } }",
653+
options: ["function"],
654+
parserOptions: { ecmaVersion: 2022 },
655+
errors: [
656+
{ messageId: "unnecessaryInClasses", line: 2 },
657+
{ messageId: "multiple", line: 3 }
658+
]
659+
}
594660
]
595661
});

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy