Skip to content

Commit de61886

Browse files
authored
Merge pull request #135 from dillonkearns/fix/pipeLeft-lambda-parens
Parenthesize lambdas on left side of operators
2 parents 65a250f + 25266b7 commit de61886

2 files changed

Lines changed: 26 additions & 4 deletions

File tree

src/Internal/Write.elm

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,12 +1060,18 @@ prettyOperatorApplication aliases indent symbol dir (Node _ exprl) (Node _ exprr
10601060
Right ->
10611061
( prec + 1, prec )
10621062

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

1066-
-- Lambda expressions on the right side of operators like |>
1067-
-- need explicit parentheses because `a |> \x -> b |> \y -> c`
1068-
-- is ambiguous — the second |> could be inside the lambda body.
10691075
( right, breakRight ) =
10701076
case exprr of
10711077
LambdaExpression _ ->

tests/OperatorPrecedence.elm

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,4 +133,20 @@ pipes =
133133
(Elm.fn (Elm.Arg.var "y") (\y -> y))
134134
|> Elm.Expect.renderedAs
135135
""""hello" |> (\\x -> x) |> (\\y -> y)"""
136+
, test "pipeLeft with lambda is parenthesized" <|
137+
\_ ->
138+
Elm.Op.pipeLeft
139+
(Elm.fn (Elm.Arg.var "x") (\x -> x))
140+
(Elm.string "hello")
141+
|> Elm.Expect.renderedAs
142+
"""(\\x -> x) <| "hello\""""
143+
, test "pipeLeft with lambda applied to complex expression" <|
144+
\_ ->
145+
Elm.Op.pipeLeft
146+
(Elm.fn (Elm.Arg.var "x")
147+
(\x -> Elm.Op.append x (Elm.string "!"))
148+
)
149+
(Elm.string "hello")
150+
|> Elm.Expect.renderedAs
151+
"""(\\x -> x ++ "!") <| "hello\""""
136152
]

0 commit comments

Comments
 (0)