Skip to content

Commit

Permalink
fix #3659: trim code in dead switch statements
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Feb 23, 2024
1 parent a064abc commit cc74e60
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,21 @@

## Unreleased

* Improve dead code removal of `switch` statements ([#3659](https://github.com/evanw/esbuild/issues/3659))

With this release, esbuild will now remove `switch` statements in branches when minifying if they are known to never be evaluated:

```js
// Original code
if (true) foo(); else switch (bar) { case 1: baz(); break }

// Old output (with --minify)
if(1)foo();else switch(bar){case 1:}

// New output (with --minify)
foo();
```

* Empty enums should behave like an object literal ([#3657](https://github.com/evanw/esbuild/issues/3657))

TypeScript allows you to create an empty enum and add properties to it at run time. While people usually use an empty object literal for this instead of a TypeScript enum, esbuild's enum transform didn't anticipate this use case and generated `undefined` instead of `{}` for an empty enum. With this release, you can now use an empty enum to generate an empty object literal.
Expand Down
8 changes: 8 additions & 0 deletions internal/js_parser/js_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -10704,6 +10704,14 @@ func (p *parser) visitAndAppendStmt(stmts []js_ast.Stmt, stmt js_ast.Stmt) []js_
}
}

// Unwrap switch statements in dead code
if p.options.minifySyntax && p.isControlFlowDead {
for _, c := range s.Cases {
stmts = append(stmts, c.Body...)
}
return stmts
}

// "using" declarations inside switch statements must be special-cased
if lowered := p.maybeLowerUsingDeclarationsInSwitch(stmt.Loc, s); lowered != nil {
return append(stmts, lowered...)
Expand Down
2 changes: 2 additions & 0 deletions internal/js_parser/js_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5232,6 +5232,7 @@ func TestTrimCodeInDeadControlFlow(t *testing.T) {
expectPrintedMangle(t, "b: while (1) if (0) a(); else { continue b }", "b:\n for (; ; )\n continue b;\n")
expectPrintedMangle(t, "if (1) a(); else { class b {} }", "a();\n")
expectPrintedMangle(t, "if (1) a(); else { debugger }", "a();\n")
expectPrintedMangle(t, "if (1) a(); else { switch (1) { case 1: b() } }", "a();\n")

expectPrintedMangle(t, "if (0) {let a = 1} else a()", "a();\n")
expectPrintedMangle(t, "if (1) {let a = 1} else a()", "{\n let a = 1;\n}\n")
Expand All @@ -5246,6 +5247,7 @@ func TestTrimCodeInDeadControlFlow(t *testing.T) {
expectPrintedMangle(t, "if (1) a(); else { function a() {} }", "if (1)\n a();\nelse\n var a;\n")
expectPrintedMangle(t, "if (1) a(); else { for(;;){var a} }", "if (1)\n a();\nelse\n for (; ; )\n var a;\n")
expectPrintedMangle(t, "if (1) { a(); b() } else { var a; var b; }", "if (1)\n a(), b();\nelse\n var a, b;\n")
expectPrintedMangle(t, "if (1) a(); else { switch (1) { case 1: case 2: var a } }", "if (1)\n a();\nelse\n var a;\n")
}

func TestPreservedComments(t *testing.T) {
Expand Down

0 comments on commit cc74e60

Please sign in to comment.