Skip to content

Commit ab64df0

Browse files
committed
don't unconditionally forward flags in printer
1 parent 2498aaf commit ab64df0

File tree

1 file changed

+24
-29
lines changed

1 file changed

+24
-29
lines changed

internal/js_printer/js_printer.go

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1292,18 +1292,6 @@ func (p *printer) printDotThenSuffix() {
12921292
}
12931293
}
12941294

1295-
type printExprFlags uint8
1296-
1297-
const (
1298-
forbidCall printExprFlags = 1 << iota
1299-
forbidIn
1300-
hasNonOptionalChainParent
1301-
exprResultIsUnused
1302-
didAlreadySimplifyUnusedExprs
1303-
isFollowedByOf
1304-
isInsideForAwait
1305-
)
1306-
13071295
func (p *printer) printUndefined(level js_ast.L) {
13081296
if level >= js_ast.LPrefix {
13091297
p.print("(void 0)")
@@ -1373,12 +1361,19 @@ func (p *printer) simplifyUnusedExpr(expr js_ast.Expr) js_ast.Expr {
13731361
return expr
13741362
}
13751363

1376-
func (p *printer) printExpr(expr js_ast.Expr, level js_ast.L, flags printExprFlags) {
1377-
originalFlags := flags
1364+
type printExprFlags uint8
13781365

1379-
// Turn these flags off so we don't unintentionally propagate them to child calls
1380-
flags &= ^(isFollowedByOf | exprResultIsUnused | didAlreadySimplifyUnusedExprs)
1366+
const (
1367+
forbidCall printExprFlags = 1 << iota
1368+
forbidIn
1369+
hasNonOptionalChainParent
1370+
exprResultIsUnused
1371+
didAlreadySimplifyUnusedExprs
1372+
isFollowedByOf
1373+
isInsideForAwait
1374+
)
13811375

1376+
func (p *printer) printExpr(expr js_ast.Expr, level js_ast.L, flags printExprFlags) {
13821377
p.addSourceMapping(expr.Loc)
13831378

13841379
switch e := expr.Data.(type) {
@@ -1560,20 +1555,20 @@ func (p *printer) printExpr(expr js_ast.Expr, level js_ast.L, flags printExprFla
15601555
for _, arg := range e.Args {
15611556
replacement = js_ast.JoinWithComma(replacement, js_ast.SimplifyUnusedExpr(arg, p.isUnbound))
15621557
}
1563-
if replacement.Data == nil || (originalFlags&exprResultIsUnused) == 0 {
1558+
if replacement.Data == nil || (flags&exprResultIsUnused) == 0 {
15641559
replacement = js_ast.JoinWithComma(replacement, js_ast.Expr{Loc: expr.Loc, Data: js_ast.EUndefinedShared})
15651560
}
1566-
p.printExpr(replacement, level, originalFlags)
1561+
p.printExpr(replacement, level, flags)
15671562
break
15681563
}
15691564

15701565
// Inline non-mutated identity functions at print time
15711566
if (symbolFlags&(js_ast.IsIdentityFunction|js_ast.CouldPotentiallyBeMutated)) == js_ast.IsIdentityFunction && len(e.Args) == 1 {
15721567
arg := e.Args[0]
1573-
if (originalFlags & exprResultIsUnused) != 0 {
1568+
if (flags & exprResultIsUnused) != 0 {
15741569
arg = js_ast.SimplifyUnusedExpr(arg, p.isUnbound)
15751570
}
1576-
p.printExpr(arg, level, originalFlags)
1571+
p.printExpr(arg, level, flags)
15771572
break
15781573
}
15791574
}
@@ -1634,7 +1629,7 @@ func (p *printer) printExpr(expr js_ast.Expr, level js_ast.L, flags printExprFla
16341629
}
16351630

16361631
case *js_ast.ERequireString:
1637-
p.printRequireOrImportExpr(e.ImportRecordIndex, nil, level, originalFlags)
1632+
p.printRequireOrImportExpr(e.ImportRecordIndex, nil, level, flags)
16381633

16391634
case *js_ast.ERequireResolveString:
16401635
wrap := level >= js_ast.LNew || (flags&forbidCall) != 0
@@ -1654,7 +1649,7 @@ func (p *printer) printExpr(expr js_ast.Expr, level js_ast.L, flags printExprFla
16541649
if !p.options.RemoveWhitespace {
16551650
leadingInteriorComments = e.LeadingInteriorComments
16561651
}
1657-
p.printRequireOrImportExpr(e.ImportRecordIndex, leadingInteriorComments, level, originalFlags)
1652+
p.printRequireOrImportExpr(e.ImportRecordIndex, leadingInteriorComments, level, flags)
16581653

16591654
case *js_ast.EImportCall:
16601655
var leadingInteriorComments []js_ast.Comment
@@ -1724,7 +1719,7 @@ func (p *printer) printExpr(expr js_ast.Expr, level js_ast.L, flags printExprFla
17241719
}
17251720
flags &= ^hasNonOptionalChainParent
17261721
}
1727-
p.printExpr(e.Target, js_ast.LPostfix, flags)
1722+
p.printExpr(e.Target, js_ast.LPostfix, flags&(forbidCall|hasNonOptionalChainParent))
17281723
if p.canPrintIdentifier(e.Name) {
17291724
if e.OptionalChain != js_ast.OptionalChainStart && p.prevNumEnd == len(p.js) {
17301725
// "1.toString" is a syntax error, so print "1 .toString" instead
@@ -1786,7 +1781,7 @@ func (p *printer) printExpr(expr js_ast.Expr, level js_ast.L, flags printExprFla
17861781
}
17871782
flags &= ^hasNonOptionalChainParent
17881783
}
1789-
p.printExpr(e.Target, js_ast.LPostfix, flags)
1784+
p.printExpr(e.Target, js_ast.LPostfix, flags&(forbidCall|hasNonOptionalChainParent))
17901785
if e.OptionalChain == js_ast.OptionalChainStart {
17911786
p.print("?.")
17921787
}
@@ -2072,7 +2067,7 @@ func (p *printer) printExpr(expr js_ast.Expr, level js_ast.L, flags printExprFla
20722067
case *js_ast.EIdentifier:
20732068
name := p.renamer.NameForSymbol(e.Ref)
20742069
wrap := len(p.js) == p.forOfInitStart && (name == "let" ||
2075-
((originalFlags&isFollowedByOf) != 0 && (flags&isInsideForAwait) == 0 && name == "async"))
2070+
((flags&isFollowedByOf) != 0 && (flags&isInsideForAwait) == 0 && name == "async"))
20762071

20772072
if wrap {
20782073
p.print("(")
@@ -2192,15 +2187,15 @@ func (p *printer) printExpr(expr js_ast.Expr, level js_ast.L, flags printExprFla
21922187
// should have already simplified unused expressions), or the result is used
21932188
// (and we can still simplify unused expressions inside the left operand)
21942189
if e.Op == js_ast.BinOpComma {
2195-
if (originalFlags & didAlreadySimplifyUnusedExprs) == 0 {
2190+
if (flags & didAlreadySimplifyUnusedExprs) == 0 {
21962191
left := p.simplifyUnusedExpr(e.Left)
21972192
right := e.Right
2198-
if (originalFlags & exprResultIsUnused) != 0 {
2193+
if (flags & exprResultIsUnused) != 0 {
21992194
right = p.simplifyUnusedExpr(right)
22002195
}
22012196
if left.Data != e.Left.Data || right.Data != e.Right.Data {
22022197
// Pass a flag so we don't needlessly re-simplify the same expression
2203-
p.printExpr(js_ast.JoinWithComma(left, e.Right), level, originalFlags|didAlreadySimplifyUnusedExprs)
2198+
p.printExpr(js_ast.JoinWithComma(left, e.Right), level, flags|didAlreadySimplifyUnusedExprs)
22042199
break
22052200
}
22062201
} else {
@@ -2292,7 +2287,7 @@ func (p *printer) printExpr(expr js_ast.Expr, level js_ast.L, flags printExprFla
22922287

22932288
if e.Op == js_ast.BinOpComma {
22942289
// The result of the right operand of the comma operator is unused if the caller doesn't use it
2295-
p.printExpr(e.Right, rightLevel, (flags&forbidIn)|(originalFlags&exprResultIsUnused))
2290+
p.printExpr(e.Right, rightLevel, (flags&forbidIn)|(flags&exprResultIsUnused))
22962291
} else {
22972292
p.printExpr(e.Right, rightLevel, flags&forbidIn)
22982293
}

0 commit comments

Comments
 (0)