Skip to content

Commit 1a1bb4b

Browse files
authored
feat: update one-var for class static blocks (#15317)
Fixes false positives of the `one-var` rule with option `"always"` where the rule suggests combining declarations from a class static block with declarations from the upper scope. Also enables autofixing declarations at the top level of class static blocks with option `"never"`. Refs #15016
1 parent 9b666e0 commit 1a1bb4b

File tree

3 files changed

+343
-8
lines changed

3 files changed

+343
-8
lines changed

docs/rules/one-var.md

Lines changed: 78 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ Examples of **incorrect** code for this rule with the default `"always"` option:
6666

6767
```js
6868
/*eslint one-var: ["error", "always"]*/
69-
/*eslint-env es6*/
7069

7170
function foo() {
7271
var bar;
@@ -89,13 +88,31 @@ function foo() {
8988
var qux = true;
9089
}
9190
}
91+
92+
class C {
93+
static {
94+
var foo;
95+
var bar;
96+
}
97+
98+
static {
99+
var foo;
100+
if (bar) {
101+
var baz = true;
102+
}
103+
}
104+
105+
static {
106+
let foo;
107+
let bar;
108+
}
109+
}
92110
```
93111

94112
Examples of **correct** code for this rule with the default `"always"` option:
95113

96114
```js
97115
/*eslint one-var: ["error", "always"]*/
98-
/*eslint-env es6*/
99116

100117
function foo() {
101118
var bar,
@@ -127,6 +144,30 @@ function foo(){
127144
let qux;
128145
}
129146
}
147+
148+
class C {
149+
static {
150+
var foo, bar;
151+
}
152+
153+
static {
154+
var foo, baz;
155+
if (bar) {
156+
baz = true;
157+
}
158+
}
159+
160+
static {
161+
let foo, bar;
162+
}
163+
164+
static {
165+
let foo;
166+
if (bar) {
167+
let baz;
168+
}
169+
}
170+
}
130171
```
131172

132173
### never
@@ -135,7 +176,6 @@ Examples of **incorrect** code for this rule with the `"never"` option:
135176

136177
```js
137178
/*eslint one-var: ["error", "never"]*/
138-
/*eslint-env es6*/
139179

140180
function foo() {
141181
var bar,
@@ -157,13 +197,19 @@ function foo(){
157197
let bar = true,
158198
baz = false;
159199
}
200+
201+
class C {
202+
static {
203+
var foo, bar;
204+
let baz, qux;
205+
}
206+
}
160207
```
161208

162209
Examples of **correct** code for this rule with the `"never"` option:
163210

164211
```js
165212
/*eslint one-var: ["error", "never"]*/
166-
/*eslint-env es6*/
167213

168214
function foo() {
169215
var bar;
@@ -185,6 +231,15 @@ function foo() {
185231
let qux = true;
186232
}
187233
}
234+
235+
class C {
236+
static {
237+
var foo;
238+
var bar;
239+
let baz;
240+
let qux;
241+
}
242+
}
188243
```
189244

190245
### consecutive
@@ -193,7 +248,6 @@ Examples of **incorrect** code for this rule with the `"consecutive"` option:
193248

194249
```js
195250
/*eslint one-var: ["error", "consecutive"]*/
196-
/*eslint-env es6*/
197251

198252
function foo() {
199253
var bar;
@@ -209,14 +263,21 @@ function foo(){
209263
var qux = 3;
210264
var quux;
211265
}
266+
267+
class C {
268+
static {
269+
var foo;
270+
var bar;
271+
let baz;
272+
let qux;
273+
}
274+
}
212275
```
213276

214277
Examples of **correct** code for this rule with the `"consecutive"` option:
215278

216279
```js
217280
/*eslint one-var: ["error", "consecutive"]*/
218-
/*eslint-env es6*/
219-
220281

221282
function foo() {
222283
var bar,
@@ -232,6 +293,16 @@ function foo(){
232293
var qux = 3,
233294
quux;
234295
}
296+
297+
class C {
298+
static {
299+
var foo, bar;
300+
let baz, qux;
301+
doSomething();
302+
let quux;
303+
var quuux;
304+
}
305+
}
235306
```
236307

237308
### var, let, and const

lib/rules/one-var.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,8 @@ module.exports = {
541541
FunctionDeclaration: startFunction,
542542
FunctionExpression: startFunction,
543543
ArrowFunctionExpression: startFunction,
544+
StaticBlock: startFunction, // StaticBlock creates a new scope for `var` variables
545+
544546
BlockStatement: startBlock,
545547
ForStatement: startBlock,
546548
ForInStatement: startBlock,
@@ -552,10 +554,12 @@ module.exports = {
552554
"ForInStatement:exit": endBlock,
553555
"SwitchStatement:exit": endBlock,
554556
"BlockStatement:exit": endBlock,
557+
555558
"Program:exit": endFunction,
556559
"FunctionDeclaration:exit": endFunction,
557560
"FunctionExpression:exit": endFunction,
558-
"ArrowFunctionExpression:exit": endFunction
561+
"ArrowFunctionExpression:exit": endFunction,
562+
"StaticBlock:exit": endFunction
559563
};
560564

561565
}

0 commit comments

Comments
 (0)