Skip to content

Commit 3d66d2a

Browse files
committed
simplify unused empty function arguments
1 parent 0229eae commit 3d66d2a

File tree

3 files changed

+29
-19
lines changed

3 files changed

+29
-19
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@
9292
function assertFoo(o){}const val=getFoo();assertFoo(val),console.log(val.bar);
9393

9494
// New output (with --minify --define:window.DEBUG=false)
95-
function assertFoo(o){}const val=getFoo();val,void 0,console.log(val.bar);
95+
function assertFoo(o){}const val=getFoo();void 0,console.log(val.bar);
9696
```
9797

9898
## 0.14.9

internal/bundler/snapshots/snapshots_dce.txt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -272,14 +272,14 @@ TestInlineEmptyFunctionCalls
272272
function DROP() {
273273
}
274274
console.log((foo(), bar(), void 0));
275-
console.log((foo(), 1, void 0));
276-
console.log((1, foo(), void 0));
277-
console.log((1, void 0));
275+
console.log((foo(), void 0));
276+
console.log((foo(), void 0));
278277
console.log(void 0);
279-
foo(), bar(), void 0;
280-
foo(), 1, void 0;
281-
1, foo(), void 0;
282-
1, void 0;
278+
console.log(void 0);
279+
foo(), bar();
280+
foo();
281+
foo();
282+
void 0;
283283
void 0;
284284

285285
---------- /out/empty-comma.js ----------

internal/js_printer/js_printer.go

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,7 @@ func (p *printer) printJSXTag(tagOrNil js_ast.Expr) {
432432

433433
type printer struct {
434434
symbols js_ast.SymbolMap
435+
isUnbound func(js_ast.Ref) bool
435436
renamer renamer.Renamer
436437
importRecords []ast.ImportRecord
437438
options Options
@@ -1313,8 +1314,10 @@ func (p *printer) printUndefined(level js_ast.L) {
13131314
}
13141315

13151316
func (p *printer) printExpr(expr js_ast.Expr, level js_ast.L, flags printExprFlags) {
1316-
wasFollowedByOf := (flags & isFollowedByOf) != 0
1317-
flags &= ^isFollowedByOf
1317+
originalFlags := flags
1318+
1319+
// Turn these flags off so we don't unintentionally propagate them to child calls
1320+
flags &= ^(isFollowedByOf | exprResultIsUnused)
13181321

13191322
p.addSourceMapping(expr.Loc)
13201323

@@ -1495,16 +1498,18 @@ func (p *printer) printExpr(expr js_ast.Expr, level js_ast.L, flags printExprFla
14951498
if (symbolFlags & (js_ast.IsEmptyFunction | js_ast.CouldPotentiallyBeMutated)) == js_ast.IsEmptyFunction {
14961499
var replacement js_ast.Expr
14971500
for _, arg := range e.Args {
1498-
replacement = js_ast.JoinWithComma(replacement, arg)
1501+
replacement = js_ast.JoinWithComma(replacement, js_ast.SimplifyUnusedExpr(arg, p.isUnbound))
1502+
}
1503+
if replacement.Data == nil || (originalFlags&exprResultIsUnused) == 0 {
1504+
replacement = js_ast.JoinWithComma(replacement, js_ast.Expr{Loc: expr.Loc, Data: js_ast.EUndefinedShared})
14991505
}
1500-
replacement = js_ast.JoinWithComma(replacement, js_ast.Expr{Loc: expr.Loc, Data: js_ast.EUndefinedShared})
1501-
p.printExpr(replacement, level, flags)
1506+
p.printExpr(replacement, level, originalFlags)
15021507
break
15031508
}
15041509

15051510
// Inline non-mutated identity functions at print time
15061511
if (symbolFlags&(js_ast.IsIdentityFunction|js_ast.CouldPotentiallyBeMutated)) == js_ast.IsIdentityFunction && len(e.Args) == 1 {
1507-
p.printExpr(e.Args[0], level, flags)
1512+
p.printExpr(e.Args[0], level, originalFlags)
15081513
break
15091514
}
15101515
}
@@ -1565,7 +1570,7 @@ func (p *printer) printExpr(expr js_ast.Expr, level js_ast.L, flags printExprFla
15651570
}
15661571

15671572
case *js_ast.ERequireString:
1568-
p.printRequireOrImportExpr(e.ImportRecordIndex, nil, level, flags)
1573+
p.printRequireOrImportExpr(e.ImportRecordIndex, nil, level, originalFlags)
15691574

15701575
case *js_ast.ERequireResolveString:
15711576
wrap := level >= js_ast.LNew || (flags&forbidCall) != 0
@@ -1585,7 +1590,7 @@ func (p *printer) printExpr(expr js_ast.Expr, level js_ast.L, flags printExprFla
15851590
if !p.options.RemoveWhitespace {
15861591
leadingInteriorComments = e.LeadingInteriorComments
15871592
}
1588-
p.printRequireOrImportExpr(e.ImportRecordIndex, leadingInteriorComments, level, flags)
1593+
p.printRequireOrImportExpr(e.ImportRecordIndex, leadingInteriorComments, level, originalFlags)
15891594

15901595
case *js_ast.EImportCall:
15911596
var leadingInteriorComments []js_ast.Comment
@@ -2003,7 +2008,7 @@ func (p *printer) printExpr(expr js_ast.Expr, level js_ast.L, flags printExprFla
20032008
case *js_ast.EIdentifier:
20042009
name := p.renamer.NameForSymbol(e.Ref)
20052010
wrap := len(p.js) == p.forOfInitStart && (name == "let" ||
2006-
(wasFollowedByOf && (flags&isInsideForAwait) == 0 && name == "async"))
2011+
((originalFlags&isFollowedByOf) != 0 && (flags&isInsideForAwait) == 0 && name == "async"))
20072012

20082013
if wrap {
20092014
p.print("(")
@@ -2998,7 +3003,7 @@ func (p *printer) printStmt(stmt js_ast.Stmt) {
29983003
p.printSpace()
29993004
p.print("(")
30003005
if s.InitOrNil.Data != nil {
3001-
p.printForLoopInit(s.InitOrNil, forbidIn)
3006+
p.printForLoopInit(s.InitOrNil, forbidIn|exprResultIsUnused)
30023007
}
30033008
p.print(";")
30043009
p.printSpace()
@@ -3008,7 +3013,7 @@ func (p *printer) printStmt(stmt js_ast.Stmt) {
30083013
p.print(";")
30093014
p.printSpace()
30103015
if s.UpdateOrNil.Data != nil {
3011-
p.printExpr(s.UpdateOrNil, js_ast.LLowest, 0)
3016+
p.printExpr(s.UpdateOrNil, js_ast.LLowest, exprResultIsUnused)
30123017
}
30133018
p.print(")")
30143019
p.printBody(s.Body)
@@ -3273,6 +3278,11 @@ func Print(tree js_ast.AST, symbols js_ast.SymbolMap, r renamer.Renamer, options
32733278
builder: sourcemap.MakeChunkBuilder(options.InputSourceMap, options.LineOffsetTables),
32743279
}
32753280

3281+
p.isUnbound = func(ref js_ast.Ref) bool {
3282+
ref = js_ast.FollowSymbols(symbols, ref)
3283+
return symbols.Get(ref).Kind == js_ast.SymbolUnbound
3284+
}
3285+
32763286
// Add the top-level directive if present
32773287
if tree.Directive != "" {
32783288
p.printIndent()

0 commit comments

Comments
 (0)