diff --git a/package.json b/package.json index d860cd5b1f1..dae125a0dbf 100644 --- a/package.json +++ b/package.json @@ -143,9 +143,6 @@ "weak-napi": "^2.0.2", "yargs-parser": "^21.1.1" }, - "overrides": { - "d3": "7.8.0" - }, "files": [ "dist/**/*.js", "dist/*.d.ts", diff --git a/src/ast/nodes/SwitchStatement.ts b/src/ast/nodes/SwitchStatement.ts index b506a82110a..9dfb0154a4c 100644 --- a/src/ast/nodes/SwitchStatement.ts +++ b/src/ast/nodes/SwitchStatement.ts @@ -6,10 +6,12 @@ import { type InclusionContext } from '../ExecutionContext'; import BlockScope from '../scopes/BlockScope'; +import type ChildScope from '../scopes/ChildScope'; import type Scope from '../scopes/Scope'; import type * as NodeType from './NodeType'; import type SwitchCase from './SwitchCase'; -import { type ExpressionNode, type IncludeChildren, StatementBase } from './shared/Node'; +import type { ExpressionNode, GenericEsTreeNode, IncludeChildren } from './shared/Node'; +import { StatementBase } from './shared/Node'; export default class SwitchStatement extends StatementBase { declare cases: readonly SwitchCase[]; @@ -17,8 +19,10 @@ export default class SwitchStatement extends StatementBase { declare type: NodeType.tSwitchStatement; private declare defaultCase: number | null; + private declare parentScope: ChildScope; createScope(parentScope: Scope): void { + this.parentScope = parentScope as ChildScope; this.scope = new BlockScope(parentScope); } @@ -89,6 +93,15 @@ export default class SwitchStatement extends StatementBase { this.defaultCase = null; } + parseNode(esTreeNode: GenericEsTreeNode) { + this.discriminant = new (this.context.getNodeConstructor(esTreeNode.discriminant.type))( + esTreeNode.discriminant, + this, + this.parentScope + ); + super.parseNode(esTreeNode); + } + render(code: MagicString, options: RenderOptions): void { this.discriminant.render(code, options); if (this.cases.length > 0) { diff --git a/test/function/samples/switch-scope/_config.js b/test/function/samples/switch-scope/_config.js new file mode 100644 index 00000000000..deab99f886f --- /dev/null +++ b/test/function/samples/switch-scope/_config.js @@ -0,0 +1,3 @@ +module.exports = defineTest({ + description: 'uses correct scopes for switch statements' +}); diff --git a/test/function/samples/switch-scope/main.js b/test/function/samples/switch-scope/main.js new file mode 100644 index 00000000000..d2b441fe305 --- /dev/null +++ b/test/function/samples/switch-scope/main.js @@ -0,0 +1,10 @@ +const foo = 1; +let triggered = false; + +switch (foo) { + case 1: + const foo = 2; + triggered = true; +} + +assert.ok(triggered);