Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions src/Internal/Write.elm
Original file line number Diff line number Diff line change
Expand Up @@ -1060,12 +1060,18 @@ prettyOperatorApplication aliases indent symbol dir (Node _ exprl) (Node _ exprr
Right ->
( prec + 1, prec )

-- Lambda expressions need explicit parentheses on either side
-- of operators. On the right: `a |> \x -> b` is ambiguous.
-- On the left: `\x -> x <| "hello"` absorbs <| into the body.
( left, breakLeft ) =
prettyExpressionInner aliases { precedence = lprec } indent exprl
case exprl of
LambdaExpression _ ->
prettyExpressionInner aliases { precedence = lprec } indent exprl
|> Tuple.mapFirst Pretty.parens

_ ->
prettyExpressionInner aliases { precedence = lprec } indent exprl

-- Lambda expressions on the right side of operators like |>
-- need explicit parentheses because `a |> \x -> b |> \y -> c`
-- is ambiguous — the second |> could be inside the lambda body.
( right, breakRight ) =
case exprr of
LambdaExpression _ ->
Expand Down
16 changes: 16 additions & 0 deletions tests/OperatorPrecedence.elm
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,20 @@ pipes =
(Elm.fn (Elm.Arg.var "y") (\y -> y))
|> Elm.Expect.renderedAs
""""hello" |> (\\x -> x) |> (\\y -> y)"""
, test "pipeLeft with lambda is parenthesized" <|
\_ ->
Elm.Op.pipeLeft
(Elm.fn (Elm.Arg.var "x") (\x -> x))
(Elm.string "hello")
|> Elm.Expect.renderedAs
"""(\\x -> x) <| "hello\""""
, test "pipeLeft with lambda applied to complex expression" <|
\_ ->
Elm.Op.pipeLeft
(Elm.fn (Elm.Arg.var "x")
(\x -> Elm.Op.append x (Elm.string "!"))
)
(Elm.string "hello")
|> Elm.Expect.renderedAs
"""(\\x -> x ++ "!") <| "hello\""""
]
Loading