@@ -1133,9 +1133,15 @@ func approximatePrintedIntCharCount(intValue float64) int {
1133
1133
return count
1134
1134
}
1135
1135
1136
- func ShouldFoldBinaryArithmeticWhenMinifying (binary * EBinary ) bool {
1136
+ func ShouldFoldBinaryOperatorWhenMinifying (binary * EBinary ) bool {
1137
1137
switch binary .Op {
1138
1138
case
1139
+ // Equality tests should always result in smaller code when folded
1140
+ BinOpLooseEq ,
1141
+ BinOpLooseNe ,
1142
+ BinOpStrictEq ,
1143
+ BinOpStrictNe ,
1144
+
1139
1145
// Minification always folds right signed shift operations since they are
1140
1146
// unlikely to result in larger output. Note: ">>>" could result in
1141
1147
// bigger output such as "-1 >>> 0" becoming "4294967295".
@@ -1203,7 +1209,7 @@ func ShouldFoldBinaryArithmeticWhenMinifying(binary *EBinary) bool {
1203
1209
1204
1210
// This function intentionally avoids mutating the input AST so it can be
1205
1211
// 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 {
1207
1213
switch e .Op {
1208
1214
case BinOpAdd :
1209
1215
if left , right , ok := extractNumericValues (e .Left , e .Right ); ok {
@@ -1296,6 +1302,22 @@ func FoldBinaryArithmetic(loc logger.Loc, e *EBinary) Expr {
1296
1302
if left , right , ok := extractStringValues (e .Left , e .Right ); ok {
1297
1303
return Expr {Loc : loc , Data : & EBoolean {Value : stringCompareUCS2 (left , right ) >= 0 }}
1298
1304
}
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
+ }
1299
1321
}
1300
1322
1301
1323
return Expr {}
0 commit comments