From a064abcbb539214ee5968f159cb37de2144d1748 Mon Sep 17 00:00:00 2001 From: Evan Wallace Date: Fri, 23 Feb 2024 11:33:18 -0800 Subject: [PATCH] fix bug with no-identifier `var` in dead branches --- internal/js_parser/js_parser.go | 3 +++ internal/js_parser/js_parser_test.go | 2 ++ 2 files changed, 5 insertions(+) diff --git a/internal/js_parser/js_parser.go b/internal/js_parser/js_parser.go index 601916a4f48..02faa2db90c 100644 --- a/internal/js_parser/js_parser.go +++ b/internal/js_parser/js_parser.go @@ -8355,6 +8355,9 @@ func shouldKeepStmtInDeadControlFlow(stmt js_ast.Stmt) bool { for _, decl := range s.Decls { identifiers = findIdentifiers(decl.Binding, identifiers) } + if len(identifiers) == 0 { + return false + } s.Decls = identifiers return true diff --git a/internal/js_parser/js_parser_test.go b/internal/js_parser/js_parser_test.go index 57bc7568e1e..15aba2e377c 100644 --- a/internal/js_parser/js_parser_test.go +++ b/internal/js_parser/js_parser_test.go @@ -5241,6 +5241,8 @@ func TestTrimCodeInDeadControlFlow(t *testing.T) { expectPrintedMangle(t, "if (1) a(); else { var a = b }", "if (1)\n a();\nelse\n var a;\n") expectPrintedMangle(t, "if (1) a(); else { var [a] = b }", "if (1)\n a();\nelse\n var a;\n") expectPrintedMangle(t, "if (1) a(); else { var {x: a} = b }", "if (1)\n a();\nelse\n var a;\n") + expectPrintedMangle(t, "if (1) a(); else { var [] = b }", "a();\n") + expectPrintedMangle(t, "if (1) a(); else { var {} = b }", "a();\n") 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")