From cc30d826b3f55b7c0f6d495a924a7852c7529b3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Wed, 9 Nov 2022 13:55:32 +0100 Subject: [PATCH 1/2] Add failing test --- .../test/fixtures/general/switch-shadow/input.js | 3 +++ .../test/fixtures/general/switch-shadow/output.js | 4 ++++ .../test/fixtures/tdz/switch-shadow/input.js | 4 ++++ .../test/fixtures/tdz/switch-shadow/output.js | 5 +++++ 4 files changed, 16 insertions(+) create mode 100644 packages/babel-plugin-transform-block-scoping/test/fixtures/general/switch-shadow/input.js create mode 100644 packages/babel-plugin-transform-block-scoping/test/fixtures/general/switch-shadow/output.js create mode 100644 packages/babel-plugin-transform-block-scoping/test/fixtures/tdz/switch-shadow/input.js create mode 100644 packages/babel-plugin-transform-block-scoping/test/fixtures/tdz/switch-shadow/output.js diff --git a/packages/babel-plugin-transform-block-scoping/test/fixtures/general/switch-shadow/input.js b/packages/babel-plugin-transform-block-scoping/test/fixtures/general/switch-shadow/input.js new file mode 100644 index 000000000000..7a7a289aba67 --- /dev/null +++ b/packages/babel-plugin-transform-block-scoping/test/fixtures/general/switch-shadow/input.js @@ -0,0 +1,3 @@ +switch (x) { + default: let x; +} diff --git a/packages/babel-plugin-transform-block-scoping/test/fixtures/general/switch-shadow/output.js b/packages/babel-plugin-transform-block-scoping/test/fixtures/general/switch-shadow/output.js new file mode 100644 index 000000000000..65726dfc6ba1 --- /dev/null +++ b/packages/babel-plugin-transform-block-scoping/test/fixtures/general/switch-shadow/output.js @@ -0,0 +1,4 @@ +switch (x) { + default: + var x; +} diff --git a/packages/babel-plugin-transform-block-scoping/test/fixtures/tdz/switch-shadow/input.js b/packages/babel-plugin-transform-block-scoping/test/fixtures/tdz/switch-shadow/input.js new file mode 100644 index 000000000000..1ba4bd251964 --- /dev/null +++ b/packages/babel-plugin-transform-block-scoping/test/fixtures/tdz/switch-shadow/input.js @@ -0,0 +1,4 @@ +let x; +switch (x) { + default: let x; +} diff --git a/packages/babel-plugin-transform-block-scoping/test/fixtures/tdz/switch-shadow/output.js b/packages/babel-plugin-transform-block-scoping/test/fixtures/tdz/switch-shadow/output.js new file mode 100644 index 000000000000..f03f42298578 --- /dev/null +++ b/packages/babel-plugin-transform-block-scoping/test/fixtures/tdz/switch-shadow/output.js @@ -0,0 +1,5 @@ +var x; +switch (babelHelpers.tdz("x")) { + default: + var _x; +} From ba474c64b78f1097187434eb2afa571a44d4005f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Wed, 9 Nov 2022 14:08:25 +0100 Subject: [PATCH 2/2] Register `switch`'s `discriminant` in the outer scope --- .../test/fixtures/general/switch-shadow/output.js | 2 +- .../test/fixtures/tdz/switch-shadow/output.js | 2 +- packages/babel-traverse/src/path/context.ts | 8 +++++--- packages/babel-traverse/test/scope.js | 8 ++++++++ 4 files changed, 15 insertions(+), 5 deletions(-) diff --git a/packages/babel-plugin-transform-block-scoping/test/fixtures/general/switch-shadow/output.js b/packages/babel-plugin-transform-block-scoping/test/fixtures/general/switch-shadow/output.js index 65726dfc6ba1..95cdac755c6b 100644 --- a/packages/babel-plugin-transform-block-scoping/test/fixtures/general/switch-shadow/output.js +++ b/packages/babel-plugin-transform-block-scoping/test/fixtures/general/switch-shadow/output.js @@ -1,4 +1,4 @@ switch (x) { default: - var x; + var _x; } diff --git a/packages/babel-plugin-transform-block-scoping/test/fixtures/tdz/switch-shadow/output.js b/packages/babel-plugin-transform-block-scoping/test/fixtures/tdz/switch-shadow/output.js index f03f42298578..36f5e171d618 100644 --- a/packages/babel-plugin-transform-block-scoping/test/fixtures/tdz/switch-shadow/output.js +++ b/packages/babel-plugin-transform-block-scoping/test/fixtures/tdz/switch-shadow/output.js @@ -1,5 +1,5 @@ var x; -switch (babelHelpers.tdz("x")) { +switch (x) { default: var _x; } diff --git a/packages/babel-traverse/src/path/context.ts b/packages/babel-traverse/src/path/context.ts index de5d4bbc7d35..6fb9f5047ddc 100644 --- a/packages/babel-traverse/src/path/context.ts +++ b/packages/babel-traverse/src/path/context.ts @@ -133,10 +133,12 @@ export function setScope(this: NodePath) { let path = this.parentPath; - // Skip method scope if is computed method key or decorator expression if ( - (this.key === "key" || this.listKey === "decorators") && - path.isMethod() + // Skip method scope if is computed method key or decorator expression + ((this.key === "key" || this.listKey === "decorators") && + path.isMethod()) || + // Skip switch scope if for discriminant (`x` in `switch (x) {}`). + (this.key === "discriminant" && path.isSwitchStatement()) ) { path = path.parentPath; } diff --git a/packages/babel-traverse/test/scope.js b/packages/babel-traverse/test/scope.js index 612e7046b3d4..30aacd61566b 100644 --- a/packages/babel-traverse/test/scope.js +++ b/packages/babel-traverse/test/scope.js @@ -282,6 +282,14 @@ describe("scope", () => { }); }); + it("switch discriminant scope", () => { + expect( + getPath(`let a = "outside"; switch (a) { default: let a = "inside" }`) + .get("body.1.discriminant") + .scope.getBinding("a").path.node.init.value, + ).toBe("outside"); + }); + it("variable declaration", function () { expect(getPath("var foo = null;").scope.getBinding("foo").path.type).toBe( "VariableDeclarator",