From 219201b737fc6607eec95c399f78a66bdd65586c Mon Sep 17 00:00:00 2001 From: Florian Verdonck Date: Fri, 25 Sep 2026 16:12:51 +0200 Subject: [PATCH 1/2] Keep a comment after the of keyword of a union case The syntax tree had no range for the `of` keyword of a union case or exception. The keyword was printed as plain text, so a block comment after it, as in `exception E of (*name*) string`, was attached to the case name and moved in front of `of`. Update FCS to 'Record the range of the `of` keyword in SynUnionCaseTrivia', commit 5e9647f158fe282462aba57ed83ada910a91b90e, which adds that range to SynUnionCaseTrivia. UnionCaseNode now has an OfKeyword node built from it, so a comment attaches to the side of the keyword it was written on. The wordOf helper is no longer used and is removed. Fixes #1959 --- Directory.Build.props | 2 +- src/Fantomas.Core.Tests/CommentTests.fs | 2 +- .../TypeDeclarationTests.fs | 14 ++++++++ src/Fantomas.Core.Tests/UnionTests.fs | 36 +++++++++++++++++++ src/Fantomas.Core/ASTTransformer.fs | 1 + src/Fantomas.Core/CodePrinter.fs | 6 ++-- src/Fantomas.Core/Context.fs | 1 - src/Fantomas.Core/Context.fsi | 1 - src/Fantomas.Core/SyntaxOak.fs | 3 ++ 9 files changed, 59 insertions(+), 7 deletions(-) diff --git a/Directory.Build.props b/Directory.Build.props index 8e1f8dc399..50ef7fb1dc 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -45,7 +45,7 @@ Some common use cases include: - 349d6aaf1e4685a72e43b7f4a0708205e4104b4c + 5e9647f158fe282462aba57ed83ada910a91b90e diff --git a/src/Fantomas.Core.Tests/CommentTests.fs b/src/Fantomas.Core.Tests/CommentTests.fs index 0d337c7345..fce64da222 100644 --- a/src/Fantomas.Core.Tests/CommentTests.fs +++ b/src/Fantomas.Core.Tests/CommentTests.fs @@ -2391,7 +2391,7 @@ module M module A = type ProviderGeneratedType = - | ProviderGeneratedType (*ilOrigTyRef*) of ILTypeRef (*ilRenamedTyRef*) * ILTypeRef * ProviderGeneratedType list + | ProviderGeneratedType of (*ilOrigTyRef*) ILTypeRef (*ilRenamedTyRef*) * ILTypeRef * ProviderGeneratedType list """ [] diff --git a/src/Fantomas.Core.Tests/TypeDeclarationTests.fs b/src/Fantomas.Core.Tests/TypeDeclarationTests.fs index 8ce55b9f1a..4e94a0933e 100644 --- a/src/Fantomas.Core.Tests/TypeDeclarationTests.fs +++ b/src/Fantomas.Core.Tests/TypeDeclarationTests.fs @@ -143,6 +143,20 @@ namespace Moon exception FooException of int """ +[] +let ``block comment after of keyword in exception, 1959`` () = + formatSourceString + """ +exception LoadedSourceNotFoundIgnoring of (*filename*) string * range +""" + config + |> prepend newline + |> should + equal + """ +exception LoadedSourceNotFoundIgnoring of (*filename*) string * range +""" + [] let ``type annotations`` () = formatSourceString diff --git a/src/Fantomas.Core.Tests/UnionTests.fs b/src/Fantomas.Core.Tests/UnionTests.fs index cc1ebc2016..b51a92e20b 100644 --- a/src/Fantomas.Core.Tests/UnionTests.fs +++ b/src/Fantomas.Core.Tests/UnionTests.fs @@ -1114,3 +1114,39 @@ type A = {| A: int LongerThanLengthDeclaration: string |} """ + +[] +let ``block comments around of keyword in union case`` () = + formatSourceString + """ +type A = + | A (* c *) of int + | B of (* c *) string * int +""" + config + |> prepend newline + |> should + equal + """ +type A = + | A (* c *) of int + | B of (* c *) string * int +""" + +[] +let ``line comment after of keyword in union case`` () = + formatSourceString + """ +type A = + | A of // comment + int +""" + config + |> prepend newline + |> should + equal + """ +type A = + | A of // comment + int +""" diff --git a/src/Fantomas.Core/ASTTransformer.fs b/src/Fantomas.Core/ASTTransformer.fs index c838c2cff3..68a62aed7c 100644 --- a/src/Fantomas.Core/ASTTransformer.fs +++ b/src/Fantomas.Core/ASTTransformer.fs @@ -2989,6 +2989,7 @@ let mkSynUnionCase mkAttributes creationAide attributes, Option.map (stn "|") trivia.BarRange, mkSynIdent creationAide ident, + Option.map (stn "of") trivia.OfKeyword, fields, fullRange ) diff --git a/src/Fantomas.Core/CodePrinter.fs b/src/Fantomas.Core/CodePrinter.fs index aacdc32892..8357fd9e08 100644 --- a/src/Fantomas.Core/CodePrinter.fs +++ b/src/Fantomas.Core/CodePrinter.fs @@ -4762,9 +4762,9 @@ let genField (node: FieldNode) = |> genNode node let genUnionCase (hasVerticalBar: bool) (node: UnionCaseNode) = - let shortExpr = col sepStar node.Fields genField + let shortExpr: Context -> Context = sepSpace +> col sepStar node.Fields genField - let longExpr = + let longExpr: Context -> Context = indentSepNlnUnindent (atCurrentColumn (col (sepStar +> sepNln) node.Fields genField)) let genBar = @@ -4779,7 +4779,7 @@ let genUnionCase (hasVerticalBar: bool) (node: UnionCaseNode) = sepNlnWhenWriteBeforeNewlineNotEmpty +> genOnelinerAttributes node.Attributes +> genSingleTextNode node.Identifier - +> onlyIf (List.isNotEmpty node.Fields) wordOf + +> optSingle (fun ofNode -> sepSpace +> genSingleTextNode ofNode) node.OfKeyword ) +> onlyIf (List.isNotEmpty node.Fields) (expressionFitsOnRestOfLine shortExpr longExpr) |> genNode node diff --git a/src/Fantomas.Core/Context.fs b/src/Fantomas.Core/Context.fs index 315f0f8643..56c9f4d337 100644 --- a/src/Fantomas.Core/Context.fs +++ b/src/Fantomas.Core/Context.fs @@ -618,7 +618,6 @@ let sepCloseT = !-")" let wordAnd = sepSpace +> !-"and " let wordAndFixed = !-"and" -let wordOf = sepSpace +> !-"of " let shortExpressionWithFallback (shortExpression: Context -> Context) diff --git a/src/Fantomas.Core/Context.fsi b/src/Fantomas.Core/Context.fsi index 5d3c6e0a14..4bb867352c 100644 --- a/src/Fantomas.Core/Context.fsi +++ b/src/Fantomas.Core/Context.fsi @@ -163,7 +163,6 @@ val sepOpenT: (Context -> Context) val sepCloseT: (Context -> Context) val wordAnd: (Context -> Context) val wordAndFixed: (Context -> Context) -val wordOf: (Context -> Context) val sepSpaceBeforeClassConstructor: ctx: Context -> Context val sepColon: ctx: Context -> Context val sepColonFixed: (Context -> Context) diff --git a/src/Fantomas.Core/SyntaxOak.fs b/src/Fantomas.Core/SyntaxOak.fs index 81d4d77e15..f818b5c097 100644 --- a/src/Fantomas.Core/SyntaxOak.fs +++ b/src/Fantomas.Core/SyntaxOak.fs @@ -2605,6 +2605,7 @@ type UnionCaseNode attributes: MultipleAttributeListNode option, bar: SingleTextNode option, identifier: SingleTextNode, + ofKeyword: SingleTextNode option, fields: FieldNode list, range ) @@ -2617,6 +2618,7 @@ type UnionCaseNode yield! noa bar yield! noa attributes yield identifier + yield! noa ofKeyword yield! nodes fields |] @@ -2624,6 +2626,7 @@ type UnionCaseNode member val Attributes = attributes member val Bar = bar member val Identifier = identifier + member val OfKeyword = ofKeyword member val Fields = fields /// The shared header of a type definition: `type` / `and` keyword, optional doc, attributes, name, type parameters, From d949f796f768997577bbdd7aac274ab91d5cb822 Mon Sep 17 00:00:00 2001 From: Florian Verdonck Date: Fri, 25 Sep 2026 16:16:04 +0200 Subject: [PATCH 2/2] Add changeog entry --- CHANGELOG.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d974233c9a..f9626c859e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,15 @@ # Changelog +## [Unreleased] + +### Changed + +- Update FCS to 'Record the range of the `of` keyword in SynUnionCaseTrivia', commit 5e9647f158fe282462aba57ed83ada910a91b90e [#3502](https://github.com/fsprojects/fantomas/pull/3502) + +### Fixed + +- Wrong change of comment's position. The syntax tree had no range for the `of` keyword of a union case or exception, so a block comment after it, as in `exception E of (*name*) string`, was attached to the case name and printed in front of `of`. The parser now records that range, and the comment stays after the keyword. [#1959](https://github.com/fsprojects/fantomas/issues/1959) + ## [8.0.5] - 2026-09-25 ### Changed