Skip to content

Commit

Permalink
fix #2496: minify removes useless break statements
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Aug 29, 2022
1 parent 501abf7 commit af4d944
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
* `class-static-blocks=false`
* `class-static-field=false`

* Implement a small minification improvement ([#2496](https://github.com/evanw/esbuild/issues/2496))

Some people write code that contains a label with an immediate break such as `x: break x`. Previously this code was not removed during minification but it will now be removed during minification starting with this release.

## 0.15.5

* Fix issues with Yarn PnP and Yarn's workspaces feature ([#2476](https://github.com/evanw/esbuild/issues/2476))
Expand Down
7 changes: 7 additions & 0 deletions internal/js_parser/js_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -9454,6 +9454,13 @@ func (p *parser) visitAndAppendStmt(stmts []js_ast.Stmt, stmt js_ast.Stmt) []js_
s.Stmt = p.visitSingleStmt(s.Stmt, stmtsNormal)
p.popScope()

// Optimize "x: break x" which some people apparently write by hand
if p.options.minifySyntax {
if child, ok := s.Stmt.Data.(*js_ast.SBreak); ok && child.Label != nil && child.Label.Ref == s.Name.Ref {
return stmts
}
}

case *js_ast.SLocal:
// Local statements do not end the const local prefix
p.currentScope.IsAfterConstLocalPrefix = wasAfterAfterConstLocalPrefix
Expand Down
7 changes: 7 additions & 0 deletions internal/js_parser/js_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2031,6 +2031,13 @@ func TestLabels(t *testing.T) {
expectPrinted(t, "x: ({ f() { x: 1; } }).f()", "x:\n ({ f() {\n x:\n 1;\n } }).f();\n")
expectPrinted(t, "x: (function() { x: 1; })()", "x:\n (function() {\n x:\n 1;\n })();\n")
expectParseError(t, "x: y: x: 1", "<stdin>: ERROR: Duplicate label \"x\"\n<stdin>: NOTE: The original label \"x\" is here:\n")

expectPrinted(t, "x: break x", "x:\n break x;\n")
expectPrinted(t, "x: { break x; foo() }", "x: {\n break x;\n foo();\n}\n")
expectPrintedMangle(t, "x: break x", "")
expectPrintedMangle(t, "x: { break x; foo() }", "")
expectPrintedMangle(t, "y: while (foo()) x: { break x; foo() }", "y:\n for (; foo(); )\n ;\n")
expectPrintedMangle(t, "y: while (foo()) x: { break y; foo() }", "y:\n for (; foo(); )\n x:\n break y;\n")
}

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

0 comments on commit af4d944

Please sign in to comment.