Skip to content

Commit

Permalink
fix a dce issue identified by ci
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Dec 6, 2020
1 parent 179433f commit 2563f67
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 23 deletions.
8 changes: 4 additions & 4 deletions internal/bundler/bundler_dce_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1030,15 +1030,15 @@ func TestDeadCodeFollowingJump(t *testing.T) {
if (true) return y + z()
if (FAIL) return FAIL
if (x) { var y }
function z() {}
function z() { KEEP_ME() }
return FAIL
}
function testThrow() {
if (true) throw y + z()
if (FAIL) return FAIL
if (x) { var y }
function z() {}
function z() { KEEP_ME() }
return FAIL
}
Expand All @@ -1050,7 +1050,7 @@ func TestDeadCodeFollowingJump(t *testing.T) {
}
if (FAIL) return FAIL
if (x) { var y }
function z() {}
function z() { KEEP_ME() }
return FAIL
}
}
Expand All @@ -1063,7 +1063,7 @@ func TestDeadCodeFollowingJump(t *testing.T) {
}
if (FAIL) return FAIL
if (x) { var y }
function z() {}
function z() { KEEP_ME() }
return FAIL
}
}
Expand Down
4 changes: 4 additions & 0 deletions internal/bundler/snapshots/snapshots_dce.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ function testReturn() {
if (x)
var y;
function z() {
KEEP_ME();
}
}
function testThrow() {
throw y + z();
if (x)
var y;
function z() {
KEEP_ME();
}
}
function testBreak() {
Expand All @@ -34,6 +36,7 @@ function testBreak() {
if (x)
var y;
function z() {
KEEP_ME();
}
}
}
Expand All @@ -44,6 +47,7 @@ function testContinue() {
if (x)
var y;
function z() {
KEEP_ME();
}
}
}
Expand Down
29 changes: 10 additions & 19 deletions internal/js_parser/js_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -6022,6 +6022,14 @@ func shouldKeepStmtInDeadControlFlow(stmt js_ast.Stmt) bool {
s.Decls = identifiers
return true

case *js_ast.SBlock:
for _, child := range s.Stmts {
if shouldKeepStmtInDeadControlFlow(child) {
return true
}
}
return false

case *js_ast.SIf:
return shouldKeepStmtInDeadControlFlow(s.Yes) || (s.No != nil && shouldKeepStmtInDeadControlFlow(*s.No))

Expand Down Expand Up @@ -6113,30 +6121,13 @@ func (p *parser) visitStmts(stmts []js_ast.Stmt) []js_ast.Stmt {
visited := make([]js_ast.Stmt, 0, len(stmts))
var after []js_ast.Stmt
for _, stmt := range stmts {
var target *[]js_ast.Stmt
if _, ok := stmt.Data.(*js_ast.SExportEquals); ok {
// TypeScript "export = value;" becomes "module.exports = value;". This
// must happen at the end after everything is parsed because TypeScript
// moves this statement to the end when it generates code.
target = &after
after = p.visitAndAppendStmt(after, stmt)
} else {
target = &visited
}

// This call should only append new statements but never modify old ones
nextStmtIndex := len(*target)
*target = p.visitAndAppendStmt(*target, stmt)

// If there is a jump-like statement in the middle of this statement
// sequence, control flow is dead for all of the following statements.
// This improves dead-code elimination.
if p.options.mangleSyntax {
for _, stmt := range (*target)[nextStmtIndex:] {
if isJumpStatement(stmt.Data) {
p.isControlFlowDead = true
break
}
}
visited = p.visitAndAppendStmt(visited, stmt)
}
}
visited = append(visited, after...)
Expand Down

0 comments on commit 2563f67

Please sign in to comment.