Skip to content
Closed
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
26 changes: 26 additions & 0 deletions src/Fantomas.Core.Tests/LambdaTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1482,3 +1482,29 @@ type Rec = { A: int; B: int -> int; C: int }

let test () : Rec = { A = 1; B = (fun x -> x + 1); C = 3 }
"""

[<Test>]
let ``lambda in non-last tuple position in list stays multiline to preserve semantics, 3278`` () =
formatSourceString
"""
module A

let x =
[
1, fun () -> 1
1, fun () -> 1
]
"""
config
|> prepend newline
|> should
equal
"""
module A

let x =
[
1, fun () -> 1
1, fun () -> 1
]
"""
26 changes: 24 additions & 2 deletions src/Fantomas.Core/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1878,8 +1878,30 @@ let genArrayOrList (preferMultilineCramped: bool) (node: ExprArrayOrListNode) =
| Expr.IfThenElse _ -> true
| _ -> false

List.exists isIfThenElse node.Elements
|| List.forall isLambdaOrIfThenElse node.Elements
// Returns true when an expression, if placed as a non-last element
// of a single-line list (followed by ";"), would have its lambda or
// if/then/else body swallow the subsequent list items.
// E.g. `1, fun () -> 1` in `[1, fun () -> 1; 1, fun () -> 1]` β€”
// the lambda captures the ";" and the following element as its body,
// changing the list from two elements to one. See #3278.
let wouldSwallowNextListItem (e: Expr) =
match e with
| IsLambdaOrIfThenElse _ -> true
| Expr.Tuple tupleNode ->
tupleNode.Items
|> List.choose (function
| Choice1Of2 e -> Some e
| _ -> None)
|> List.tryLast
|> Option.exists isLambdaOrIfThenElse
| _ -> false

let elements = node.Elements

List.exists isIfThenElse elements
|| List.forall isLambdaOrIfThenElse elements
|| (elements.Length > 1
&& List.take (elements.Length - 1) elements |> List.exists wouldSwallowNextListItem)

if alwaysMultiline then
multilineExpression ctx
Expand Down