diff --git a/src/Fantomas.Tests/FunctionDefinitionTests.fs b/src/Fantomas.Tests/FunctionDefinitionTests.fs index 3e093d97c7..e78d3417e9 100644 --- a/src/Fantomas.Tests/FunctionDefinitionTests.fs +++ b/src/Fantomas.Tests/FunctionDefinitionTests.fs @@ -267,8 +267,7 @@ let f = fun x -> match x with X (x) -> x """ config |> prepend newline |> should equal """ -type U = - | X of int +type U = X of int let f = fun x -> diff --git a/src/Fantomas.Tests/RecordTests.fs b/src/Fantomas.Tests/RecordTests.fs index af2ff48f13..ae58e8822d 100644 --- a/src/Fantomas.Tests/RecordTests.fs +++ b/src/Fantomas.Tests/RecordTests.fs @@ -190,8 +190,7 @@ type rate2 = Rate of float type rate = { Rate : float } -type rate2 = - | Rate of float +type rate2 = Rate of float """ [] diff --git a/src/Fantomas.Tests/TypeDeclarationTests.fs b/src/Fantomas.Tests/TypeDeclarationTests.fs index 344e4568fd..bb2f8d1b36 100644 --- a/src/Fantomas.Tests/TypeDeclarationTests.fs +++ b/src/Fantomas.Tests/TypeDeclarationTests.fs @@ -532,8 +532,7 @@ type Delegate3 = delegate of int -> (int -> int) type Delegate4 = delegate of int -> int -> int -type U = - | U of (int * int) +type U = U of (int * int) """ [] diff --git a/src/Fantomas.Tests/UnionTests.fs b/src/Fantomas.Tests/UnionTests.fs index e6dda61d18..590995b4ed 100644 --- a/src/Fantomas.Tests/UnionTests.fs +++ b/src/Fantomas.Tests/UnionTests.fs @@ -126,8 +126,7 @@ let main argv = | _ -> 0""" config |> prepend newline |> should equal """ -type TestUnion = - | Test of A : int * B : int +type TestUnion = Test of A : int * B : int [] let main argv = @@ -154,4 +153,42 @@ type uColor = let col3 = Microsoft.FSharp.Core.LanguagePrimitives.EnumOfValue(2u) +""" + +[] +let ``Single case DUs on same line`` () = + formatSourceString false """ +type CustomerId = + | CustomerId of int + """ config + |> prepend newline + |> should equal """ +type CustomerId = CustomerId of int +""" + +[] +let ``Single case DU with private access modifier`` () = + formatSourceString false """ +type CustomerId = + private + | CustomerId of int + """ config + |> prepend newline + |> should equal """ +type CustomerId = private CustomerId of int +""" + +[] +let ``Single case DU with member should be on a newline`` () = + formatSourceString false """ +type CustomerId = + | CustomerId of int + member this.Test() = + printfn "%A" this + """ config + |> prepend newline + |> should equal """ +type CustomerId = + | CustomerId of int + member this.Test() = printfn "%A" this """ \ No newline at end of file diff --git a/src/Fantomas/CodePrinter.fs b/src/Fantomas/CodePrinter.fs index 2baa58c40d..7c28399df2 100644 --- a/src/Fantomas/CodePrinter.fs +++ b/src/Fantomas/CodePrinter.fs @@ -690,9 +690,18 @@ and genTypeDefn astContext (TypeDef(ats, px, ao, tds, tcs, tdr, ms, s)) = +> unindent | Simple(TDSRUnion(ao', xs)) -> + let unionCases = + match xs with + | [] -> id + | [x] when List.isEmpty ms -> + indent +> sepSpace +> opt sepSpace ao' genAccess + +> genUnionCase { astContext with HasVerticalBar = false } x + | xs -> + indent +> sepNln +> opt sepNln ao' genAccess + +> col sepNln xs (genUnionCase { astContext with HasVerticalBar = true }) + typeName +> sepEq - +> indent +> sepNln +> opt sepNln ao' genAccess - +> col sepNln xs (genUnionCase { astContext with HasVerticalBar = true }) + +> unionCases +> genMemberDefnList { astContext with IsInterface = false } ms +> unindent