Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update one-var for class static blocks #15317

Merged
merged 1 commit into from Nov 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
2 changes: 1 addition & 1 deletion lib/rules/utils/ast-utils.js
Expand Up @@ -35,7 +35,7 @@ const COMMENTS_IGNORE_PATTERN = /^\s*(?:eslint|jshint\s+|jslint\s+|istanbul\s+|g
const LINEBREAKS = new Set(["\r\n", "\r", "\n", "\u2028", "\u2029"]);

// A set of node types that can contain a list of statements
const STATEMENT_LIST_PARENTS = new Set(["Program", "BlockStatement", "SwitchCase"]);
const STATEMENT_LIST_PARENTS = new Set(["Program", "BlockStatement", "StaticBlock", "SwitchCase"]);

const DECIMAL_INTEGER_PATTERN = /^(?:0|0[0-7]*[89]\d*|[1-9](?:_?\d)*)$/u;

Expand Down