Skip to content
Permalink
Browse files
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
  • Loading branch information
mdjermanovic committed Nov 17, 2021
1 parent 9b666e0 commit 1a1bb4b1ee87c1b33f2d86ef70b3d81e83377547
Showing with 343 additions and 8 deletions.
  1. +78 −7 docs/rules/one-var.md
  2. +5 −1 lib/rules/one-var.js
  3. +260 −0 tests/lib/rules/one-var.js
@@ -66,7 +66,6 @@ Examples of **incorrect** code for this rule with the default `"always"` option:

```js
/*eslint one-var: ["error", "always"]*/
/*eslint-env es6*/
function foo() {
var bar;
@@ -89,13 +88,31 @@ function foo() {
var qux = true;
}
}
class C {
static {
var foo;
var bar;
}
static {
var foo;
if (bar) {
var baz = true;
}
}
static {
let foo;
let bar;
}
}
```

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

```js
/*eslint one-var: ["error", "always"]*/
/*eslint-env es6*/
function foo() {
var bar,
@@ -127,6 +144,30 @@ function foo(){
let qux;
}
}
class C {
static {
var foo, bar;
}
static {
var foo, baz;
if (bar) {
baz = true;
}
}
static {
let foo, bar;
}
static {
let foo;
if (bar) {
let baz;
}
}
}
```

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

```js
/*eslint one-var: ["error", "never"]*/
/*eslint-env es6*/
function foo() {
var bar,
@@ -157,13 +197,19 @@ function foo(){
let bar = true,
baz = false;
}
class C {
static {
var foo, bar;
let baz, qux;
}
}
```

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

```js
/*eslint one-var: ["error", "never"]*/
/*eslint-env es6*/
function foo() {
var bar;
@@ -185,6 +231,15 @@ function foo() {
let qux = true;
}
}
class C {
static {
var foo;
var bar;
let baz;
let qux;
}
}
```

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

```js
/*eslint one-var: ["error", "consecutive"]*/
/*eslint-env es6*/
function foo() {
var bar;
@@ -209,14 +263,21 @@ function foo(){
var qux = 3;
var quux;
}
class C {
static {
var foo;
var bar;
let baz;
let qux;
}
}
```

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

```js
/*eslint one-var: ["error", "consecutive"]*/
/*eslint-env es6*/
function foo() {
var bar,
@@ -232,6 +293,16 @@ function foo(){
var qux = 3,
quux;
}
class C {
static {
var foo, bar;
let baz, qux;
doSomething();
let quux;
var quuux;
}
}
```

### var, let, and const
@@ -541,6 +541,8 @@ module.exports = {
FunctionDeclaration: startFunction,
FunctionExpression: startFunction,
ArrowFunctionExpression: startFunction,
StaticBlock: startFunction, // StaticBlock creates a new scope for `var` variables

BlockStatement: startBlock,
ForStatement: startBlock,
ForInStatement: startBlock,
@@ -552,10 +554,12 @@ module.exports = {
"ForInStatement:exit": endBlock,
"SwitchStatement:exit": endBlock,
"BlockStatement:exit": endBlock,

"Program:exit": endFunction,
"FunctionDeclaration:exit": endFunction,
"FunctionExpression:exit": endFunction,
"ArrowFunctionExpression:exit": endFunction
"ArrowFunctionExpression:exit": endFunction,
"StaticBlock:exit": endFunction
};

}

0 comments on commit 1a1bb4b

Please sign in to comment.