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..95cdac755c6b --- /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..36f5e171d618 --- /dev/null +++ b/packages/babel-plugin-transform-block-scoping/test/fixtures/tdz/switch-shadow/output.js @@ -0,0 +1,5 @@ +var 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",