Skip to content

Commit 1b29ac7

Browse files
committed
fold equality checks after cross-module inlining
1 parent d7a8bf3 commit 1b29ac7

File tree

5 files changed

+38
-16
lines changed

5 files changed

+38
-16
lines changed

internal/bundler_tests/bundler_dce_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -3138,7 +3138,7 @@ func TestConstValueInliningDirectEval(t *testing.T) {
31383138
})
31393139
}
31403140

3141-
func TestCrossModuleConstantFolding(t *testing.T) {
3141+
func TestCrossModuleConstantFoldingNumber(t *testing.T) {
31423142
dce_suite.expectBundled(t, bundled{
31433143
files: map[string]string{
31443144
"/enum-constants.ts": `

internal/bundler_tests/snapshots/snapshots_dce.txt

+9-9
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ function foo() {
257257
}
258258

259259
================================================================================
260-
TestCrossModuleConstantFolding
260+
TestCrossModuleConstantFoldingNumber
261261
---------- /out/enum-entry.js ----------
262262
// enum-entry.ts
263263
console.log([
@@ -278,10 +278,10 @@ console.log([
278278
!1,
279279
!0,
280280
!1,
281-
3 /* a */ == 6 /* b */,
282-
3 /* a */ != 6 /* b */,
283-
3 /* a */ === 6 /* b */,
284-
3 /* a */ !== 6 /* b */
281+
!1,
282+
!0,
283+
!1,
284+
!0
285285
], [
286286
12,
287287
3,
@@ -316,10 +316,10 @@ console.log([
316316
!1,
317317
!0,
318318
!1,
319-
3 == 6,
320-
3 != 6,
321-
3 === 6,
322-
3 !== 6
319+
!1,
320+
!0,
321+
!1,
322+
!0
323323
], [
324324
12,
325325
3,

internal/js_ast/js_ast_helpers.go

+24-2
Original file line numberDiff line numberDiff line change
@@ -1133,9 +1133,15 @@ func approximatePrintedIntCharCount(intValue float64) int {
11331133
return count
11341134
}
11351135

1136-
func ShouldFoldBinaryArithmeticWhenMinifying(binary *EBinary) bool {
1136+
func ShouldFoldBinaryOperatorWhenMinifying(binary *EBinary) bool {
11371137
switch binary.Op {
11381138
case
1139+
// Equality tests should always result in smaller code when folded
1140+
BinOpLooseEq,
1141+
BinOpLooseNe,
1142+
BinOpStrictEq,
1143+
BinOpStrictNe,
1144+
11391145
// Minification always folds right signed shift operations since they are
11401146
// unlikely to result in larger output. Note: ">>>" could result in
11411147
// bigger output such as "-1 >>> 0" becoming "4294967295".
@@ -1203,7 +1209,7 @@ func ShouldFoldBinaryArithmeticWhenMinifying(binary *EBinary) bool {
12031209

12041210
// This function intentionally avoids mutating the input AST so it can be
12051211
// called after the AST has been frozen (i.e. after parsing ends).
1206-
func FoldBinaryArithmetic(loc logger.Loc, e *EBinary) Expr {
1212+
func FoldBinaryOperator(loc logger.Loc, e *EBinary) Expr {
12071213
switch e.Op {
12081214
case BinOpAdd:
12091215
if left, right, ok := extractNumericValues(e.Left, e.Right); ok {
@@ -1296,6 +1302,22 @@ func FoldBinaryArithmetic(loc logger.Loc, e *EBinary) Expr {
12961302
if left, right, ok := extractStringValues(e.Left, e.Right); ok {
12971303
return Expr{Loc: loc, Data: &EBoolean{Value: stringCompareUCS2(left, right) >= 0}}
12981304
}
1305+
1306+
case BinOpLooseEq, BinOpStrictEq:
1307+
if left, right, ok := extractNumericValues(e.Left, e.Right); ok {
1308+
return Expr{Loc: loc, Data: &EBoolean{Value: left == right}}
1309+
}
1310+
if left, right, ok := extractStringValues(e.Left, e.Right); ok {
1311+
return Expr{Loc: loc, Data: &EBoolean{Value: stringCompareUCS2(left, right) == 0}}
1312+
}
1313+
1314+
case BinOpLooseNe, BinOpStrictNe:
1315+
if left, right, ok := extractNumericValues(e.Left, e.Right); ok {
1316+
return Expr{Loc: loc, Data: &EBoolean{Value: left != right}}
1317+
}
1318+
if left, right, ok := extractStringValues(e.Left, e.Right); ok {
1319+
return Expr{Loc: loc, Data: &EBoolean{Value: stringCompareUCS2(left, right) != 0}}
1320+
}
12991321
}
13001322

13011323
return Expr{}

internal/js_parser/js_parser.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -15319,8 +15319,8 @@ func (v *binaryExprVisitor) visitRightAndFinish(p *parser) js_ast.Expr {
1531915319
}
1532015320
}
1532115321

15322-
if p.shouldFoldTypeScriptConstantExpressions || (p.options.minifySyntax && js_ast.ShouldFoldBinaryArithmeticWhenMinifying(e)) {
15323-
if result := js_ast.FoldBinaryArithmetic(v.loc, e); result.Data != nil {
15322+
if p.shouldFoldTypeScriptConstantExpressions || (p.options.minifySyntax && js_ast.ShouldFoldBinaryOperatorWhenMinifying(e)) {
15323+
if result := js_ast.FoldBinaryOperator(v.loc, e); result.Data != nil {
1532415324
return result
1532515325
}
1532615326
}

internal/js_printer/js_printer.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1805,8 +1805,8 @@ func (p *printer) lateConstantFoldUnaryOrBinaryExpr(expr js_ast.Expr) js_ast.Exp
18051805
binary := &js_ast.EBinary{Op: e.Op, Left: left, Right: right}
18061806

18071807
// Only fold certain operations (just like the parser)
1808-
if js_ast.ShouldFoldBinaryArithmeticWhenMinifying(binary) {
1809-
if result := js_ast.FoldBinaryArithmetic(expr.Loc, binary); result.Data != nil {
1808+
if js_ast.ShouldFoldBinaryOperatorWhenMinifying(binary) {
1809+
if result := js_ast.FoldBinaryOperator(expr.Loc, binary); result.Data != nil {
18101810
return result
18111811
}
18121812
}

0 commit comments

Comments
 (0)