Skip to content

Commit

Permalink
Always yield list items on separate lines if a conditional is present (
Browse files Browse the repository at this point in the history
…fsprojects#3002)

* Always yield list items on separate lines if a conditional is present

* Format code

* Fix failing test

* Add changelog entry

* Update changelog to point to issue instead of PR

Co-authored-by: Florian Verdonck <florian.verdonck@outlook.com>

---------

Co-authored-by: Florian Verdonck <florian.verdonck@outlook.com>
  • Loading branch information
josh-degraw and nojaf committed Nov 29, 2023
1 parent 12f373e commit 10ea3a3
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 21 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## Unreleased

### Fixed
* Always yield list items on separate lines if a conditional is present. [#2972](https://github.com/fsprojects/fantomas/issues/2972)

## 6.3.0-alpha-003 - 2023-11-15

### Fixed
Expand Down
22 changes: 22 additions & 0 deletions src/Fantomas.Core.Tests/IfThenElseTests.fs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module Fantomas.Core.Tests.IfThenElseTests

open Fantomas.Core
open NUnit.Framework
open FsUnit
open Fantomas.Core.Tests.TestHelpers
Expand Down Expand Up @@ -2599,3 +2600,24 @@ else
let res = typesEqual (resT :: argTs) (haveResT :: haveArgTs)
res
"""

[<Test>]
let ``always force list items to be on separate lines in if/then/else, 2972`` () =
formatSourceString
"""
[
if 1 = 1 then 1 else 2
3
]
"""
{ config with
MultilineBracketStyle = Aligned }
|> prepend newline
|> should
equal
"""
[
if 1 = 1 then 1 else 2
3
]
"""
26 changes: 5 additions & 21 deletions src/Fantomas.Core/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1803,29 +1803,13 @@ let genArrayOrList (preferMultilineCramped: bool) (node: ExprArrayOrListNode) =

fun ctx ->
let alwaysMultiline =
let isIfThenElseWithYieldReturn e =
let (|YieldLikeExpr|_|) e =
match e with
| Expr.Single singleNode ->
if String.startsWithOrdinal "yield" singleNode.Leading.Text then
Some e
else
None
| _ -> None

match e with
| Expr.IfThen ifThenNode ->
match ifThenNode.ThenExpr with
| YieldLikeExpr _ -> true
| _ -> false
| Expr.IfThenElse ifThenElseNode ->
match ifThenElseNode.IfExpr, ifThenElseNode.ElseExpr with
| YieldLikeExpr _, _
| _, YieldLikeExpr _ -> true
| _ -> false
let isIfThenElse =
function
| Expr.IfThen _
| Expr.IfThenElse _ -> true
| _ -> false

List.exists isIfThenElseWithYieldReturn node.Elements
List.exists isIfThenElse node.Elements
|| List.forall isLambdaOrIfThenElse node.Elements

if alwaysMultiline then
Expand Down

0 comments on commit 10ea3a3

Please sign in to comment.