Skip to content

Commit

Permalink
fix #844: edge case where return should cause dead code
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Feb 19, 2021
1 parent e790163 commit d7eb896
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 0 deletions.
3 changes: 3 additions & 0 deletions internal/js_parser/js_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -6776,6 +6776,9 @@ func (p *parser) mangleStmts(stmts []js_ast.Stmt, kind stmtsKind) []js_ast.Stmt
}
}
result = appendIfBodyPreservingScope(result, stmt)
if isJumpStatement(stmt.Data) {
isControlFlowDead = true
}
continue
}
}
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 @@ -2723,6 +2723,8 @@ func TestMangleReturn(t *testing.T) {
expectPrintedMangle(t, "if (!!a) return b(); return c()", "return a ? b() : c();\n")
expectPrintedMangle(t, "if (!!!a) return b(); return c()", "return a ? c() : b();\n")

expectPrintedMangle(t, "if (a) return b; else return c; return d;\n", "return a ? b : c;\n")

// Optimize implicit return
expectPrintedMangle(t, "function x() { if (y) return; z(); }", "function x() {\n y || z();\n}\n")
expectPrintedMangle(t, "function x() { if (y) return; else z(); w(); }", "function x() {\n y || (z(), w());\n}\n")
Expand Down

0 comments on commit d7eb896

Please sign in to comment.