Skip to content

Commit

Permalink
ts enum: avoid merging siblings in nested blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Nov 24, 2021
1 parent 2816a99 commit 1084849
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 2 deletions.
2 changes: 1 addition & 1 deletion internal/js_parser/js_parser.go
Expand Up @@ -9335,7 +9335,6 @@ func (p *parser) visitAndAppendStmt(stmts []js_ast.Stmt, stmt js_ast.Stmt) []js_
case *js_ast.SEnum:
p.recordDeclaredSymbol(s.Name.Ref)
p.pushScopeForVisitPass(js_ast.ScopeEntry, stmt.Loc)
defer p.popScope()
p.recordDeclaredSymbol(s.Arg)

// Scan ahead for any variables inside this namespace. This must be done
Expand Down Expand Up @@ -9439,6 +9438,7 @@ func (p *parser) visitAndAppendStmt(stmts []js_ast.Stmt, stmt js_ast.Stmt) []js_
p.recordUsage(s.Arg)
}

p.popScope()
p.shouldFoldNumericConstants = oldShouldFoldNumericConstants

// Generate statements from expressions
Expand Down
2 changes: 1 addition & 1 deletion internal/js_parser/ts_parser.go
Expand Up @@ -1353,7 +1353,7 @@ func (p *parser) generateClosureForTypeScriptNamespaceOrEnum(
// can be multiple namespace blocks for the same namespace
if (symbol.Kind == js_ast.SymbolTSNamespace || symbol.Kind == js_ast.SymbolTSEnum) && !p.emittedNamespaceVars[nameRef] {
p.emittedNamespaceVars[nameRef] = true
if p.enclosingNamespaceArgRef == nil {
if p.currentScope == p.moduleScope {
// Top-level namespace
stmts = append(stmts, js_ast.Stmt{Loc: stmtLoc, Data: &js_ast.SLocal{
Kind: js_ast.LocalVar,
Expand Down
4 changes: 4 additions & 0 deletions internal/js_parser/ts_parser_test.go
Expand Up @@ -1099,6 +1099,10 @@ y = [0, Foo?.["A"], Foo?.["A"]()];
})(Foo || (Foo = {}));
`)

// Check top-level "var" and nested "let"
expectPrintedTS(t, "enum a { b = 1 }", "var a;\n(function(a) {\n a[a[\"b\"] = 1] = \"b\";\n})(a || (a = {}));\n")
expectPrintedTS(t, "{ enum a { b = 1 } }", "{\n let a;\n (function(a) {\n a[a[\"b\"] = 1] = \"b\";\n })(a || (a = {}));\n}\n")

// Check "await" and "yield"
expectPrintedTS(t, "enum x { await = 1, y = await }", `var x;
(function(x) {
Expand Down
22 changes: 22 additions & 0 deletions scripts/end-to-end-tests.js
Expand Up @@ -81,6 +81,28 @@
}),
)

// Test TypeScript enum scope merging
tests.push(
test(['entry.ts', '--bundle', '--outfile=node.js'], {
'entry.ts': `
enum a { b = 1 }
enum a { c = 2 }
if (a.c !== 2 || a[2] !== 'c' || a.b !== 1 || a[1] !== 'b') throw 'fail'
`,
}),
test(['entry.ts', '--bundle', '--outfile=node.js'], {
'entry.ts': `
{
enum a { b = 1 }
}
{
enum a { c = 2 }
if (a.c !== 2 || a[2] !== 'c' || a.b !== void 0 || a[1] !== void 0) throw 'fail'
}
`,
}),
)

// Test coverage for a special JSX error message
tests.push(
test(['example.jsx', '--outfile=node.js'], {
Expand Down

0 comments on commit 1084849

Please sign in to comment.