@@ -432,6 +432,7 @@ func (p *printer) printJSXTag(tagOrNil js_ast.Expr) {
432
432
433
433
type printer struct {
434
434
symbols js_ast.SymbolMap
435
+ isUnbound func (js_ast.Ref ) bool
435
436
renamer renamer.Renamer
436
437
importRecords []ast.ImportRecord
437
438
options Options
@@ -1313,8 +1314,10 @@ func (p *printer) printUndefined(level js_ast.L) {
1313
1314
}
1314
1315
1315
1316
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 )
1318
1321
1319
1322
p .addSourceMapping (expr .Loc )
1320
1323
@@ -1495,16 +1498,18 @@ func (p *printer) printExpr(expr js_ast.Expr, level js_ast.L, flags printExprFla
1495
1498
if (symbolFlags & (js_ast .IsEmptyFunction | js_ast .CouldPotentiallyBeMutated )) == js_ast .IsEmptyFunction {
1496
1499
var replacement js_ast.Expr
1497
1500
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 })
1499
1505
}
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 )
1502
1507
break
1503
1508
}
1504
1509
1505
1510
// Inline non-mutated identity functions at print time
1506
1511
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 )
1508
1513
break
1509
1514
}
1510
1515
}
@@ -1565,7 +1570,7 @@ func (p *printer) printExpr(expr js_ast.Expr, level js_ast.L, flags printExprFla
1565
1570
}
1566
1571
1567
1572
case * js_ast.ERequireString :
1568
- p .printRequireOrImportExpr (e .ImportRecordIndex , nil , level , flags )
1573
+ p .printRequireOrImportExpr (e .ImportRecordIndex , nil , level , originalFlags )
1569
1574
1570
1575
case * js_ast.ERequireResolveString :
1571
1576
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
1585
1590
if ! p .options .RemoveWhitespace {
1586
1591
leadingInteriorComments = e .LeadingInteriorComments
1587
1592
}
1588
- p .printRequireOrImportExpr (e .ImportRecordIndex , leadingInteriorComments , level , flags )
1593
+ p .printRequireOrImportExpr (e .ImportRecordIndex , leadingInteriorComments , level , originalFlags )
1589
1594
1590
1595
case * js_ast.EImportCall :
1591
1596
var leadingInteriorComments []js_ast.Comment
@@ -2003,7 +2008,7 @@ func (p *printer) printExpr(expr js_ast.Expr, level js_ast.L, flags printExprFla
2003
2008
case * js_ast.EIdentifier :
2004
2009
name := p .renamer .NameForSymbol (e .Ref )
2005
2010
wrap := len (p .js ) == p .forOfInitStart && (name == "let" ||
2006
- (wasFollowedByOf && (flags & isInsideForAwait ) == 0 && name == "async" ))
2011
+ (( originalFlags & isFollowedByOf ) != 0 && (flags & isInsideForAwait ) == 0 && name == "async" ))
2007
2012
2008
2013
if wrap {
2009
2014
p .print ("(" )
@@ -2998,7 +3003,7 @@ func (p *printer) printStmt(stmt js_ast.Stmt) {
2998
3003
p .printSpace ()
2999
3004
p .print ("(" )
3000
3005
if s .InitOrNil .Data != nil {
3001
- p .printForLoopInit (s .InitOrNil , forbidIn )
3006
+ p .printForLoopInit (s .InitOrNil , forbidIn | exprResultIsUnused )
3002
3007
}
3003
3008
p .print (";" )
3004
3009
p .printSpace ()
@@ -3008,7 +3013,7 @@ func (p *printer) printStmt(stmt js_ast.Stmt) {
3008
3013
p .print (";" )
3009
3014
p .printSpace ()
3010
3015
if s .UpdateOrNil .Data != nil {
3011
- p .printExpr (s .UpdateOrNil , js_ast .LLowest , 0 )
3016
+ p .printExpr (s .UpdateOrNil , js_ast .LLowest , exprResultIsUnused )
3012
3017
}
3013
3018
p .print (")" )
3014
3019
p .printBody (s .Body )
@@ -3273,6 +3278,11 @@ func Print(tree js_ast.AST, symbols js_ast.SymbolMap, r renamer.Renamer, options
3273
3278
builder : sourcemap .MakeChunkBuilder (options .InputSourceMap , options .LineOffsetTables ),
3274
3279
}
3275
3280
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
+
3276
3286
// Add the top-level directive if present
3277
3287
if tree .Directive != "" {
3278
3288
p .printIndent ()
0 commit comments