@@ -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 {}
0 commit comments