From abe740ce68dcc9e5413df93b3d80a2e3260f1c18 Mon Sep 17 00:00:00 2001 From: Milos Djermanovic Date: Fri, 12 Nov 2021 18:41:22 +0100 Subject: [PATCH] feat: add examples for block-scoped-var with class static blocks (#15302) 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 --- docs/rules/block-scoped-var.md | 18 ++++++++++++++++++ lib/rules/block-scoped-var.js | 2 ++ tests/lib/rules/block-scoped-var.js | 15 ++++++++++++++- 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/docs/rules/block-scoped-var.md b/docs/rules/block-scoped-var.md index 75c7db02580..9c308feb2b0 100644 --- a/docs/rules/block-scoped-var.md +++ b/docs/rules/block-scoped-var.md @@ -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: @@ -85,6 +94,15 @@ function doFor() { console.log(y); } } + +class C { + static { + var build = false; + if (something) { + build = true; + } + } +} ``` ## Further Reading diff --git a/lib/rules/block-scoped-var.js b/lib/rules/block-scoped-var.js index 10125e61fde..d98250b062b 100644 --- a/lib/rules/block-scoped-var.js +++ b/lib/rules/block-scoped-var.js @@ -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 diff --git a/tests/lib/rules/block-scoped-var.js b/tests/lib/rules/block-scoped-var.js index 596c3ba1cf9..f401332b116 100644 --- a/tests/lib/rules/block-scoped-var.js +++ b/tests/lib/rules/block-scoped-var.js @@ -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" }] }, @@ -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" }] } ] });