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

docs: add examples for block-scoped-var with class static blocks #15302

Merged
merged 1 commit into from Nov 12, 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
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" }]
}
]
});