diff --git a/js/js_test.go b/js/js_test.go index 7f2fcf9cf1..eeda5cbaa3 100644 --- a/js/js_test.go +++ b/js/js_test.go @@ -627,6 +627,7 @@ func TestJS(t *testing.T) { {`!!(a===b||c===d)`, `a===b||c===d`}, {`!(a!==null)`, `a===null`}, {`a==void 0`, `a==null`}, + {`a?b(c):b(d)`, `b(a?c:d)`}, //{`if(a!==null&&a!==undefined)a.b()`, `a?.b()`}, // returns undefined instead of false {`(a===null||a===undefined)?undefined:a()`, `a?.()`}, {`(a===null||a===undefined)?undefined:a[0]`, `a?.[0]`}, diff --git a/js/util.go b/js/util.go index 7d1619d434..5472f3c871 100644 --- a/js/util.go +++ b/js/util.go @@ -829,6 +829,14 @@ func (m *jsMinifier) optimizeCondExpr(expr *js.CondExpr, prec js.OpPrec) js.IExp // no need to check whether left/right need to add groups, as the space saving is always more return nullishExpr } else { + callX, isCallX := expr.X.(*js.CallExpr) + callY, isCallY := expr.Y.(*js.CallExpr) + if isCallX && isCallY && len(callX.Args.List) == 1 && len(callY.Args.List) == 1 && !callX.Args.List[0].Rest && !callY.Args.List[0].Rest && isEqualExpr(callX.X, callY.X) { + expr.X = callX.Args.List[0].Value + expr.Y = callY.Args.List[0].Value + return &js.CallExpr{callX.X, js.Args{[]js.Arg{{expr, false}}}, false} // recompress the conditional expression inside + } + // shorten when true and false bodies are true and false trueX, falseX := isTrue(expr.X), isFalse(expr.X) trueY, falseY := isTrue(expr.Y), isFalse(expr.Y) @@ -860,7 +868,7 @@ func (m *jsMinifier) optimizeCondExpr(expr *js.CondExpr, prec js.OpPrec) js.IExp if comma, ok := group.X.(*js.CommaExpr); ok && js.OpCoalesce <= exprPrec(comma.List[len(comma.List)-1]) { expr.Cond = comma.List[len(comma.List)-1] comma.List[len(comma.List)-1] = expr - return comma + return comma // recompress the conditional expression inside } } }