From ceb1fa27b695d27004705236a159e28f4476fec4 Mon Sep 17 00:00:00 2001 From: Joseph Price Date: Tue, 10 Sep 2024 10:17:17 -0400 Subject: [PATCH] fix idempotency for single case union w/o pipe (#3116) * fix idempotency for single case union w/o pipe * update changelog * Avoid indexed list access. * Add new version --------- Co-authored-by: nojaf --- CHANGELOG.md | 5 +++ .../TypeDeclarationTests.fs | 39 +++++++++++++++++++ src/Fantomas.Core/CodePrinter.fs | 20 +++++----- 3 files changed, 54 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 54bafec7a..dc1f31bd6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## 6.3.13 - 2024-09-10 + +### Fixed +* Idempotency problem when adding members to a single case union without a pipe [#3102](https://github.com/fsprojects/fantomas/issues/3102) + ## 6.3.12 - 2024-09-05 ### Fixed diff --git a/src/Fantomas.Core.Tests/TypeDeclarationTests.fs b/src/Fantomas.Core.Tests/TypeDeclarationTests.fs index 36b31dab0..f3210c601 100644 --- a/src/Fantomas.Core.Tests/TypeDeclarationTests.fs +++ b/src/Fantomas.Core.Tests/TypeDeclarationTests.fs @@ -3643,3 +3643,42 @@ type FSharpChecker with : Async<(FSharpParseFileResults * ParsedInput * FSharpCheckFileResults) option> = () """ + +[] +let ``single member union extensions without pipe, 3102`` () = + formatSourceString + """ +type X = X + with + static member x = 1 + """ + config + |> prepend newline + |> should + equal + """ +type X = X + with + + static member x = 1 +""" + +[] +let ``single member union extensions without pipe idempotent, 3102`` () = + formatSourceString + """ +type X = X + with + + static member x = 1 + """ + config + |> prepend newline + |> should + equal + """ +type X = X + with + + static member x = 1 +""" diff --git a/src/Fantomas.Core/CodePrinter.fs b/src/Fantomas.Core/CodePrinter.fs index 47931e4e7..6e68cf381 100644 --- a/src/Fantomas.Core/CodePrinter.fs +++ b/src/Fantomas.Core/CodePrinter.fs @@ -3585,16 +3585,16 @@ let genTypeDefn (td: TypeDefn) = | _ -> header +> sepSpaceOrIndentAndNlnIfExpressionExceedsPageWidth (genType node.Type) - +> onlyIf - hasMembers - (optSingle - (fun withNode -> - indentSepNlnUnindent ( - genSingleTextNode withNode - +> onlyIfCtx (fun ctx -> ctx.Config.NewlineBetweenTypeDefinitionAndMembers) sepNln - +> indentSepNlnUnindent (genMemberDefnList members) - )) - typeName.WithKeyword) + +> (match List.tryHead members, typeName.WithKeyword with + | Some firstMember, Some withNode -> + indentSepNlnUnindent ( + genSingleTextNode withNode + +> onlyIfCtx + (fun ctx -> ctx.Config.NewlineBetweenTypeDefinitionAndMembers) + (sepNlnUnlessContentBefore (MemberDefn.Node firstMember)) + +> indentSepNlnUnindent (genMemberDefnList members) + ) + | _ -> sepNone) |> genNode node) ctx | TypeDefn.Explicit node ->