Skip to content

Commit

Permalink
feat: add examples for block-scoped-var with class static blocks (#15302
Browse files Browse the repository at this point in the history
)

Documents examples for `block-scoped-var` with class static blocks.

The code change is only for clarity. It doesn't change the behavior of this rule.

Refs #15016
  • Loading branch information
mdjermanovic committed Nov 12, 2021
1 parent 9309841 commit abe740c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
18 changes: 18 additions & 0 deletions docs/rules/block-scoped-var.md
Expand Up @@ -41,6 +41,15 @@ function doFor() {
}
console.log(y);
}

class C {
static {
if (something) {
var build = true;
}
build = false;
}
}
```

Examples of **correct** code for this rule:
Expand Down Expand Up @@ -85,6 +94,15 @@ function doFor() {
console.log(y);
}
}

class C {
static {
var build = false;
if (something) {
build = true;
}
}
}
```

## Further Reading
Expand Down
2 changes: 2 additions & 0 deletions lib/rules/block-scoped-var.js
Expand Up @@ -112,6 +112,8 @@ module.exports = {
"SwitchStatement:exit": exitScope,
CatchClause: enterScope,
"CatchClause:exit": exitScope,
StaticBlock: enterScope,
"StaticBlock:exit": exitScope,

// Finds and reports references which are outside of valid scope.
VariableDeclaration: checkForVariables
Expand Down
15 changes: 14 additions & 1 deletion tests/lib/rules/block-scoped-var.js
Expand Up @@ -110,7 +110,15 @@ ruleTester.run("block-scoped-var", rule, {

// https://github.com/eslint/eslint/issues/2967
"(function () { foo(); })(); function foo() {}",
{ code: "(function () { foo(); })(); function foo() {}", parserOptions: { ecmaVersion: 6, sourceType: "module" } }
{ code: "(function () { foo(); })(); function foo() {}", parserOptions: { ecmaVersion: 6, sourceType: "module" } },

{ code: "class C { static { var foo; foo; } }", parserOptions: { ecmaVersion: 2022 } },
{ code: "class C { static { foo; var foo; } }", parserOptions: { ecmaVersion: 2022 } },
{ code: "class C { static { if (bar) { foo; } var foo; } }", parserOptions: { ecmaVersion: 2022 } },
{ code: "var foo; class C { static { foo; } } ", parserOptions: { ecmaVersion: 2022 } },
{ code: "class C { static { foo; } } var foo;", parserOptions: { ecmaVersion: 2022 } },
{ code: "var foo; class C { static {} [foo]; } ", parserOptions: { ecmaVersion: 2022 } },
{ code: "foo; class C { static {} } var foo; ", parserOptions: { ecmaVersion: 2022 } }
],
invalid: [
{ code: "function f(){ x; { var x; } }", errors: [{ messageId: "outOfScope", data: { name: "x" }, type: "Identifier" }] },
Expand Down Expand Up @@ -165,6 +173,11 @@ ruleTester.run("block-scoped-var", rule, {
{ messageId: "outOfScope", data: { name: "i" }, type: "Identifier" },
{ messageId: "outOfScope", data: { name: "i" }, type: "Identifier" }
]
},
{
code: "class C { static { if (bar) { var foo; } foo; } }",
parserOptions: { ecmaVersion: 2022 },
errors: [{ messageId: "outOfScope", data: { name: "foo" }, type: "Identifier" }]
}
]
});

0 comments on commit abe740c

Please sign in to comment.