Skip to content

Commit

Permalink
remove redundant IsPrimitiveWithSideEffects code
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Feb 6, 2023
1 parent e124cc4 commit 22354e4
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 61 deletions.
70 changes: 9 additions & 61 deletions internal/js_ast/js_ast_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,16 +194,23 @@ const (
// This can be used when the returned type is either one or the other
func MergedKnownPrimitiveTypes(a Expr, b Expr) PrimitiveType {
x := KnownPrimitiveType(a.Data)
if x == PrimitiveUnknown {
return PrimitiveUnknown
}

y := KnownPrimitiveType(b.Data)
if x == PrimitiveUnknown || y == PrimitiveUnknown {
if y == PrimitiveUnknown {
return PrimitiveUnknown
}

if x == y {
return x
}
return PrimitiveMixed // Definitely some kind of primitive
}

// Note: This function does not say whether the expression is side-effect free
// or not. For example, the expression "++x" always returns a primitive.
func KnownPrimitiveType(expr E) PrimitiveType {
switch e := expr.(type) {
case *EInlinedEnum:
Expand Down Expand Up @@ -481,65 +488,6 @@ func ConvertBindingToExpr(binding Binding, wrapIdentifier func(logger.Loc, Ref)
}
}

// Returns true if this expression is known to result in a primitive value (i.e.
// null, undefined, boolean, number, bigint, or string), even if the expression
// cannot be removed due to side effects.
func IsPrimitiveWithSideEffects(data E) bool {
switch e := data.(type) {
case *EInlinedEnum:
return IsPrimitiveWithSideEffects(e.Value.Data)

case *ENull, *EUndefined, *EBoolean, *ENumber, *EBigInt, *EString:
return true

case *EUnary:
switch e.Op {
case
// Number or bigint
UnOpPos, UnOpNeg, UnOpCpl,
UnOpPreDec, UnOpPreInc, UnOpPostDec, UnOpPostInc,
// Boolean
UnOpNot, UnOpDelete,
// Undefined
UnOpVoid,
// String
UnOpTypeof:
return true
}

case *EBinary:
switch e.Op {
case
// Boolean
BinOpLt, BinOpLe, BinOpGt, BinOpGe, BinOpIn,
BinOpInstanceof, BinOpLooseEq, BinOpLooseNe, BinOpStrictEq, BinOpStrictNe,
// String, number, or bigint
BinOpAdd, BinOpAddAssign,
// Number or bigint
BinOpSub, BinOpMul, BinOpDiv, BinOpRem, BinOpPow,
BinOpSubAssign, BinOpMulAssign, BinOpDivAssign, BinOpRemAssign, BinOpPowAssign,
BinOpShl, BinOpShr, BinOpUShr,
BinOpShlAssign, BinOpShrAssign, BinOpUShrAssign,
BinOpBitwiseOr, BinOpBitwiseAnd, BinOpBitwiseXor,
BinOpBitwiseOrAssign, BinOpBitwiseAndAssign, BinOpBitwiseXorAssign:
return true

// These always return one of the arguments unmodified
case BinOpLogicalAnd, BinOpLogicalOr, BinOpNullishCoalescing,
BinOpLogicalAndAssign, BinOpLogicalOrAssign, BinOpNullishCoalescingAssign:
return IsPrimitiveWithSideEffects(e.Left.Data) && IsPrimitiveWithSideEffects(e.Right.Data)

case BinOpComma:
return IsPrimitiveWithSideEffects(e.Right.Data)
}

case *EIf:
return IsPrimitiveWithSideEffects(e.Yes.Data) && IsPrimitiveWithSideEffects(e.No.Data)
}

return false
}

// This will return a nil expression if the expression can be totally removed.
//
// This function intentionally avoids mutating the input AST so it can be
Expand Down Expand Up @@ -729,7 +677,7 @@ func SimplifyUnusedExpr(expr Expr, unsupportedFeatures compat.JSFeature, isUnbou
// primitives. In that case there won't be any chance for user-defined
// "toString" and/or "valueOf" to be called.
case BinOpLooseEq, BinOpLooseNe:
if IsPrimitiveWithSideEffects(left.Data) && IsPrimitiveWithSideEffects(right.Data) {
if MergedKnownPrimitiveTypes(left, right) != PrimitiveUnknown {
return JoinWithComma(SimplifyUnusedExpr(left, unsupportedFeatures, isUnbound), SimplifyUnusedExpr(right, unsupportedFeatures, isUnbound))
}

Expand Down
2 changes: 2 additions & 0 deletions internal/js_parser/js_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4279,6 +4279,8 @@ func TestMangleUnused(t *testing.T) {
expectPrintedNormalAndMangle(t, "a + '' != b", "a + \"\" != b;\n", "a + \"\" != b;\n")
expectPrintedNormalAndMangle(t, "a + '' == b + ''", "a + \"\" == b + \"\";\n", "a + \"\", b + \"\";\n")
expectPrintedNormalAndMangle(t, "a + '' != b + ''", "a + \"\" != b + \"\";\n", "a + \"\", b + \"\";\n")
expectPrintedNormalAndMangle(t, "a + '' == (b | c)", "a + \"\" == (b | c);\n", "a + \"\", b | c;\n")
expectPrintedNormalAndMangle(t, "a + '' != (b | c)", "a + \"\" != (b | c);\n", "a + \"\", b | c;\n")
expectPrintedNormalAndMangle(t, "typeof a == b + ''", "typeof a == b + \"\";\n", "b + \"\";\n")
expectPrintedNormalAndMangle(t, "typeof a != b + ''", "typeof a != b + \"\";\n", "b + \"\";\n")
expectPrintedNormalAndMangle(t, "typeof a == 'b'", "typeof a == \"b\";\n", "")
Expand Down

0 comments on commit 22354e4

Please sign in to comment.