Skip to content

Commit 15c1397

Browse files
authoredNov 17, 2021
feat: update eslint-scope for class static blocks (#15321)
Updates `eslint-scope` dependency to v7.0.0, which supports class static blocks. Refs #15016
1 parent 1a1bb4b commit 15c1397

File tree

4 files changed

+302
-5
lines changed

4 files changed

+302
-5
lines changed
 

‎docs/developer-guide/scope-manager-interface.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ Those members are defined but not used in ESLint.
7777
#### type
7878

7979
* **Type:** `string`
80-
* **Description:** The type of this scope. This is one of `"block"`, `"catch"`, `"class"`, `"class-field-initializer"`, `"for"`, `"function"`, `"function-expression-name"`, `"global"`, `"module"`, `"switch"`, `"with"`.
80+
* **Description:** The type of this scope. This is one of `"block"`, `"catch"`, `"class"`, `"class-field-initializer"`, `"class-static-block"`, `"for"`, `"function"`, `"function-expression-name"`, `"global"`, `"module"`, `"switch"`, `"with"`.
8181

8282
#### isStrict
8383

@@ -97,9 +97,9 @@ Those members are defined but not used in ESLint.
9797
#### variableScope
9898

9999
* **Type:** `Scope`
100-
* **Description:** The nearest ancestor whose `type` is one of `"class-field-initializer"`, `"function"`, `"module"`, or `"program"` . For the aforementioned scopes this is a self-reference.
100+
* **Description:** The nearest ancestor whose `type` is one of `"class-field-initializer"`, `"class-static-block"`, `"function"`, `"global"`, or `"module"`. For the aforementioned scopes this is a self-reference.
101101

102-
> This represents the lowest enclosing function or top-level scope. Class field initializers are implicit functions. Historically, this was the scope which hosts variables that are defined by `var` declarations, and thus the name `variableScope`.
102+
> This represents the lowest enclosing function or top-level scope. Class field initializers and class static blocks are implicit functions. Historically, this was the scope which hosts variables that are defined by `var` declarations, and thus the name `variableScope`.
103103
104104
#### block
105105

‎package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
"doctrine": "^3.0.0",
5757
"enquirer": "^2.3.5",
5858
"escape-string-regexp": "^4.0.0",
59-
"eslint-scope": "^6.0.0",
59+
"eslint-scope": "^7.0.0",
6060
"eslint-utils": "^3.0.0",
6161
"eslint-visitor-keys": "^3.1.0",
6262
"espree": "^9.0.0",

‎tests/lib/rules/no-shadow.js

+144-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,12 @@ ruleTester.run("no-shadow", rule, {
5858
{ code: "var Object = 0;", options: [{ builtinGlobals: true }] },
5959
{ code: "var top = 0;", options: [{ builtinGlobals: true }], env: { browser: true } },
6060
{ code: "function foo(cb) { (function (cb) { cb(42); })(cb); }", options: [{ allow: ["cb"] }] },
61-
{ code: "class C { foo; foo() { let foo; } }", parserOptions: { ecmaVersion: 2022 } }
61+
{ code: "class C { foo; foo() { let foo; } }", parserOptions: { ecmaVersion: 2022 } },
62+
{ code: "class C { static { var x; } static { var x; } }", parserOptions: { ecmaVersion: 2022 } },
63+
{ code: "class C { static { let x; } static { let x; } }", parserOptions: { ecmaVersion: 2022 } },
64+
{ code: "class C { static { var x; { var x; /* redeclaration */ } } }", parserOptions: { ecmaVersion: 2022 } },
65+
{ code: "class C { static { { var x; } { var x; /* redeclaration */ } } }", parserOptions: { ecmaVersion: 2022 } },
66+
{ code: "class C { static { { let x; } { let x; } } }", parserOptions: { ecmaVersion: 2022 } }
6267
],
6368
invalid: [
6469
{
@@ -716,6 +721,144 @@ ruleTester.run("no-shadow", rule, {
716721
line: 1,
717722
column: 31
718723
}]
724+
},
725+
{
726+
code: "class C { static { let a; { let a; } } }",
727+
parserOptions: { ecmaVersion: 2022 },
728+
errors: [{
729+
messageId: "noShadow",
730+
data: {
731+
name: "a",
732+
shadowedLine: 1,
733+
shadowedColumn: 24
734+
},
735+
type: "Identifier",
736+
line: 1,
737+
column: 33
738+
}]
739+
},
740+
{
741+
code: "class C { static { var C; } }",
742+
parserOptions: { ecmaVersion: 2022 },
743+
errors: [{
744+
messageId: "noShadow",
745+
data: {
746+
name: "C",
747+
shadowedLine: 1,
748+
shadowedColumn: 7
749+
},
750+
type: "Identifier",
751+
line: 1,
752+
column: 24
753+
}]
754+
},
755+
{
756+
code: "class C { static { let C; } }",
757+
parserOptions: { ecmaVersion: 2022 },
758+
errors: [{
759+
messageId: "noShadow",
760+
data: {
761+
name: "C",
762+
shadowedLine: 1,
763+
shadowedColumn: 7
764+
},
765+
type: "Identifier",
766+
line: 1,
767+
column: 24
768+
}]
769+
},
770+
{
771+
code: "var a; class C { static { var a; } }",
772+
parserOptions: { ecmaVersion: 2022 },
773+
errors: [{
774+
messageId: "noShadow",
775+
data: {
776+
name: "a",
777+
shadowedLine: 1,
778+
shadowedColumn: 5
779+
},
780+
type: "Identifier",
781+
line: 1,
782+
column: 31
783+
}]
784+
},
785+
{
786+
code: "class C { static { var a; } } var a;",
787+
options: [{ hoist: "all" }],
788+
parserOptions: { ecmaVersion: 2022 },
789+
errors: [{
790+
messageId: "noShadow",
791+
data: {
792+
name: "a",
793+
shadowedLine: 1,
794+
shadowedColumn: 35
795+
},
796+
type: "Identifier",
797+
line: 1,
798+
column: 24
799+
}]
800+
},
801+
{
802+
code: "class C { static { let a; } } let a;",
803+
options: [{ hoist: "all" }],
804+
parserOptions: { ecmaVersion: 2022 },
805+
errors: [{
806+
messageId: "noShadow",
807+
data: {
808+
name: "a",
809+
shadowedLine: 1,
810+
shadowedColumn: 35
811+
},
812+
type: "Identifier",
813+
line: 1,
814+
column: 24
815+
}]
816+
},
817+
{
818+
code: "class C { static { var a; } } let a;",
819+
options: [{ hoist: "all" }],
820+
parserOptions: { ecmaVersion: 2022 },
821+
errors: [{
822+
messageId: "noShadow",
823+
data: {
824+
name: "a",
825+
shadowedLine: 1,
826+
shadowedColumn: 35
827+
},
828+
type: "Identifier",
829+
line: 1,
830+
column: 24
831+
}]
832+
},
833+
{
834+
code: "class C { static { var a; class D { static { var a; } } } }",
835+
parserOptions: { ecmaVersion: 2022 },
836+
errors: [{
837+
messageId: "noShadow",
838+
data: {
839+
name: "a",
840+
shadowedLine: 1,
841+
shadowedColumn: 24
842+
},
843+
type: "Identifier",
844+
line: 1,
845+
column: 50
846+
}]
847+
},
848+
{
849+
code: "class C { static { let a; class D { static { let a; } } } }",
850+
parserOptions: { ecmaVersion: 2022 },
851+
errors: [{
852+
messageId: "noShadow",
853+
data: {
854+
name: "a",
855+
shadowedLine: 1,
856+
shadowedColumn: 24
857+
},
858+
type: "Identifier",
859+
line: 1,
860+
column: 50
861+
}]
719862
}
720863
]
721864
});

‎tests/lib/rules/no-undef.js

+154
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,67 @@ ruleTester.run("no-undef", rule, {
9191
{
9292
code: "import.meta",
9393
parserOptions: { ecmaVersion: 2020, sourceType: "module" }
94+
},
95+
96+
// class static blocks
97+
{
98+
code: "let a; class C { static {} } a;",
99+
parserOptions: { ecmaVersion: 2022 },
100+
errors: [{ messageId: "undef", data: { name: "a" } }]
101+
},
102+
{
103+
code: "var a; class C { static {} } a;",
104+
parserOptions: { ecmaVersion: 2022 },
105+
errors: [{ messageId: "undef", data: { name: "a" } }]
106+
},
107+
{
108+
code: "a; class C { static {} } var a;",
109+
parserOptions: { ecmaVersion: 2022 },
110+
errors: [{ messageId: "undef", data: { name: "a" } }]
111+
},
112+
{
113+
code: "class C { static { C; } }",
114+
parserOptions: { ecmaVersion: 2022, sourceType: "module" }
115+
},
116+
{
117+
code: "const C = class { static { C; } }",
118+
parserOptions: { ecmaVersion: 2022, sourceType: "module" }
119+
},
120+
{
121+
code: "class C { static { a; } } var a;",
122+
parserOptions: { ecmaVersion: 2022, sourceType: "module" }
123+
},
124+
{
125+
code: "class C { static { a; } } let a;",
126+
parserOptions: { ecmaVersion: 2022, sourceType: "module" }
127+
},
128+
{
129+
code: "class C { static { var a; a; } }",
130+
parserOptions: { ecmaVersion: 2022, sourceType: "module" }
131+
},
132+
{
133+
code: "class C { static { a; var a; } }",
134+
parserOptions: { ecmaVersion: 2022, sourceType: "module" }
135+
},
136+
{
137+
code: "class C { static { a; { var a; } } }",
138+
parserOptions: { ecmaVersion: 2022, sourceType: "module" }
139+
},
140+
{
141+
code: "class C { static { let a; a; } }",
142+
parserOptions: { ecmaVersion: 2022, sourceType: "module" }
143+
},
144+
{
145+
code: "class C { static { a; let a; } }",
146+
parserOptions: { ecmaVersion: 2022, sourceType: "module" }
147+
},
148+
{
149+
code: "class C { static { function a() {} a; } }",
150+
parserOptions: { ecmaVersion: 2022, sourceType: "module" }
151+
},
152+
{
153+
code: "class C { static { a; function a() {} } }",
154+
parserOptions: { ecmaVersion: 2022, sourceType: "module" }
94155
}
95156
],
96157
invalid: [
@@ -114,6 +175,99 @@ ruleTester.run("no-undef", rule, {
114175
ecmaVersion: 2018
115176
},
116177
errors: [{ messageId: "undef", data: { name: "b" } }]
178+
},
179+
180+
// class static blocks
181+
{
182+
code: "class C { static { a; } }",
183+
parserOptions: {
184+
ecmaVersion: 2022
185+
},
186+
errors: [{ messageId: "undef", data: { name: "a" } }]
187+
},
188+
{
189+
code: "class C { static { { let a; } a; } }",
190+
parserOptions: {
191+
ecmaVersion: 2022
192+
},
193+
errors: [{ messageId: "undef", data: { name: "a" }, column: 31 }]
194+
},
195+
{
196+
code: "class C { static { { function a() {} } a; } }",
197+
parserOptions: {
198+
ecmaVersion: 2022
199+
},
200+
errors: [{ messageId: "undef", data: { name: "a" }, column: 40 }]
201+
},
202+
{
203+
code: "class C { static { function foo() { var a; } a; } }",
204+
parserOptions: {
205+
ecmaVersion: 2022
206+
},
207+
errors: [{ messageId: "undef", data: { name: "a" }, column: 47 }]
208+
},
209+
{
210+
code: "class C { static { var a; } static { a; } }",
211+
parserOptions: {
212+
ecmaVersion: 2022
213+
},
214+
errors: [{ messageId: "undef", data: { name: "a" }, column: 38 }]
215+
},
216+
{
217+
code: "class C { static { let a; } static { a; } }",
218+
parserOptions: {
219+
ecmaVersion: 2022
220+
},
221+
errors: [{ messageId: "undef", data: { name: "a" }, column: 38 }]
222+
},
223+
{
224+
code: "class C { static { function a(){} } static { a; } }",
225+
parserOptions: {
226+
ecmaVersion: 2022
227+
},
228+
errors: [{ messageId: "undef", data: { name: "a" }, column: 46 }]
229+
},
230+
{
231+
code: "class C { static { var a; } foo() { a; } }",
232+
parserOptions: {
233+
ecmaVersion: 2022
234+
},
235+
errors: [{ messageId: "undef", data: { name: "a" }, column: 37 }]
236+
},
237+
{
238+
code: "class C { static { let a; } foo() { a; } }",
239+
parserOptions: {
240+
ecmaVersion: 2022
241+
},
242+
errors: [{ messageId: "undef", data: { name: "a" }, column: 37 }]
243+
},
244+
{
245+
code: "class C { static { var a; } [a]; }",
246+
parserOptions: {
247+
ecmaVersion: 2022
248+
},
249+
errors: [{ messageId: "undef", data: { name: "a" }, column: 30 }]
250+
},
251+
{
252+
code: "class C { static { let a; } [a]; }",
253+
parserOptions: {
254+
ecmaVersion: 2022
255+
},
256+
errors: [{ messageId: "undef", data: { name: "a" }, column: 30 }]
257+
},
258+
{
259+
code: "class C { static { function a() {} } [a]; }",
260+
parserOptions: {
261+
ecmaVersion: 2022
262+
},
263+
errors: [{ messageId: "undef", data: { name: "a" }, column: 39 }]
264+
},
265+
{
266+
code: "class C { static { var a; } } a;",
267+
parserOptions: {
268+
ecmaVersion: 2022
269+
},
270+
errors: [{ messageId: "undef", data: { name: "a" }, column: 31 }]
117271
}
118272
]
119273
});

0 commit comments

Comments
 (0)
Please sign in to comment.