Skip to content

Commit

Permalink
feat: update one-var for class static blocks (#15317)
Browse files Browse the repository at this point in the history
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 1a1bb4b
Show file tree
Hide file tree
Showing 3 changed files with 343 additions and 8 deletions.
85 changes: 78 additions & 7 deletions docs/rules/one-var.md
Expand Up @@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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;
Expand All @@ -185,6 +231,15 @@ function foo() {
let qux = true;
}
}

class C {
static {
var foo;
var bar;
let baz;
let qux;
}
}
```

### consecutive
Expand All @@ -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;
Expand All @@ -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,
Expand All @@ -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
Expand Down
6 changes: 5 additions & 1 deletion lib/rules/one-var.js
Expand Up @@ -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,
Expand All @@ -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
};

}
Expand Down

0 comments on commit 1a1bb4b

Please sign in to comment.