From de77b2781286ef1b2b78346ae091299988bd4687 Mon Sep 17 00:00:00 2001 From: Florian Verdonck Date: Fri, 9 Oct 2026 17:17:18 +0200 Subject: [PATCH 1/2] Keep a comment after a when keyword on its own line The syntax tree had no range for the `when` of a match clause, so when `when` started its own line below the pattern, a line comment after it had no node on that line to attach to and was dropped. This happened in `match`, `function` and `try ... with`. FCS moves to 'Add range of the when keyword to SynMatchClauseTrivia', commit 409c7a62ea193ec060e87ccad78636feeb0883f1, which records that range. It also passes 'Build parser diagnostic messages without sprintf', commit 70b9afa3cf9a798fc82c8df8b7d5a95eeea73ede, which needed no change here. MatchClauseNode now pairs the guard with a `when` node, built from the new trivia, and the printer writes that node instead of a literal, so the comment stays after the keyword. Fixes #3521 --- Directory.Build.props | 2 +- .../3521-comment-after-when-on-its-own-line.fs | 4 ++++ ...521-comment-after-when-on-its-own-line.gold.fs | 5 +++++ src/Fantomas.Core/ASTTransformer.fs | 9 ++++++++- src/Fantomas.Core/CodePrinter.fs | 9 +++++---- src/Fantomas.Core/SyntaxOak.fs | 15 +++++++++++++-- 6 files changed, 36 insertions(+), 8 deletions(-) create mode 100644 src/Fantomas.Core.SnapshotTests/cases/oak/MatchClause/trivia/3521-comment-after-when-on-its-own-line.fs create mode 100644 src/Fantomas.Core.SnapshotTests/cases/oak/MatchClause/trivia/3521-comment-after-when-on-its-own-line.gold.fs diff --git a/Directory.Build.props b/Directory.Build.props index 50ef7fb1dc..1b058436db 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -45,7 +45,7 @@ Some common use cases include: - 5e9647f158fe282462aba57ed83ada910a91b90e + 409c7a62ea193ec060e87ccad78636feeb0883f1 diff --git a/src/Fantomas.Core.SnapshotTests/cases/oak/MatchClause/trivia/3521-comment-after-when-on-its-own-line.fs b/src/Fantomas.Core.SnapshotTests/cases/oak/MatchClause/trivia/3521-comment-after-when-on-its-own-line.fs new file mode 100644 index 0000000000..c8e0e10bb2 --- /dev/null +++ b/src/Fantomas.Core.SnapshotTests/cases/oak/MatchClause/trivia/3521-comment-after-when-on-its-own-line.fs @@ -0,0 +1,4 @@ +match x with +| _ + when // c + a -> b diff --git a/src/Fantomas.Core.SnapshotTests/cases/oak/MatchClause/trivia/3521-comment-after-when-on-its-own-line.gold.fs b/src/Fantomas.Core.SnapshotTests/cases/oak/MatchClause/trivia/3521-comment-after-when-on-its-own-line.gold.fs new file mode 100644 index 0000000000..dba44682be --- /dev/null +++ b/src/Fantomas.Core.SnapshotTests/cases/oak/MatchClause/trivia/3521-comment-after-when-on-its-own-line.gold.fs @@ -0,0 +1,5 @@ +match x with +| _ when // c + a + -> + b diff --git a/src/Fantomas.Core/ASTTransformer.fs b/src/Fantomas.Core/ASTTransformer.fs index b04cf0959a..5f5b59876f 100644 --- a/src/Fantomas.Core/ASTTransformer.fs +++ b/src/Fantomas.Core/ASTTransformer.fs @@ -476,10 +476,17 @@ let mkSynMatchClause creationAide (SynMatchClause(p, eo, e, range, _, trivia)) : | Some r -> r | None -> invariantViolation range $"unable to get the arrow range from trivia in %s{nameof mkSynMatchClause}" + let whenExpr: (SingleTextNode * Expr) option = + match eo, trivia.WhenKeyword with + | None, _ -> None + | Some e, Some mWhen -> Some(stn "when" mWhen, mkExpr creationAide e) + | Some _, None -> + invariantViolation range $"unable to get the when range from trivia in %s{nameof mkSynMatchClause}" + MatchClauseNode( Option.map (stn "|") trivia.BarRange, mkPat creationAide p, - Option.map (mkExpr creationAide) eo, + whenExpr, stn "->" arrowRange, mkExpr creationAide e, fullRange diff --git a/src/Fantomas.Core/CodePrinter.fs b/src/Fantomas.Core/CodePrinter.fs index 060c800f99..ee37e9dbfa 100644 --- a/src/Fantomas.Core/CodePrinter.fs +++ b/src/Fantomas.Core/CodePrinter.fs @@ -2052,8 +2052,9 @@ let genExpr (e: Expr) = ) +> genPatInClause clauseNode.Pattern +> optSingle - (fun e -> - !-" when" + (fun (whenNode, e) -> + sepSpace + +> genSingleTextNode whenNode +> expressionFitsOnRestOfLine (sepSpace +> genExpr e) (fun ctx -> @@ -2940,8 +2941,8 @@ let genClause (isLastItem: bool) (node: MatchClauseNode) = sepSpace +> leadingExpressionIsMultiline (optSingle - (fun e -> - !-"when" + (fun (whenNode, e) -> + genSingleTextNode whenNode +> sepSpaceOrIndentAndNlnIfExpressionExceedsPageWidth (genExpr e) +> sepSpace ) diff --git a/src/Fantomas.Core/SyntaxOak.fs b/src/Fantomas.Core/SyntaxOak.fs index 2a4a4a8534..35025cef6b 100644 --- a/src/Fantomas.Core/SyntaxOak.fs +++ b/src/Fantomas.Core/SyntaxOak.fs @@ -1361,7 +1361,14 @@ type ExprLambdaNode(funNode: SingleTextNode, parameters: Pattern list, arrow: Si /// Example: `| pat when guard -> body` — a single arm of a `match` or `try…with` expression. /// The leading bar, guard (`when` clause), and arrow are all optional depending on context. type MatchClauseNode - (bar: SingleTextNode option, pattern: Pattern, whenExpr: Expr option, arrow: SingleTextNode, bodyExpr: Expr, range) + ( + bar: SingleTextNode option, + pattern: Pattern, + whenExpr: (SingleTextNode * Expr) option, + arrow: SingleTextNode, + bodyExpr: Expr, + range: range + ) = inherit NodeBase(range) @@ -1369,7 +1376,11 @@ type MatchClauseNode [| yield! noa bar yield Pattern.Node pattern - yield! noa (Option.map Expr.Node whenExpr) + match whenExpr with + | None -> () + | Some(whenNode, whenExpr) -> + yield whenNode + yield Expr.Node whenExpr yield arrow yield Expr.Node bodyExpr |] From c6798dd10d126b18f41ea3b84924e2d0feee7bd5 Mon Sep 17 00:00:00 2001 From: Florian Verdonck Date: Fri, 9 Oct 2026 17:20:50 +0200 Subject: [PATCH 2/2] Add changelog entries for the FCS update and #3521 The Unreleased section becomes 8.0.8. --- CHANGELOG.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4852eceb48..9343f94860 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,15 @@ # Changelog +## [8.0.8] - 2026-10-09 + +### Changed + +- Update FCS to 'Add range of the when keyword to SynMatchClauseTrivia', commit 409c7a62ea193ec060e87ccad78636feeb0883f1 [#3531](https://github.com/fsprojects/fantomas/pull/3531) + +### Fixed + +- Comment after 'when' on its own line is lost. The syntax tree had no range for the `when` keyword of a match clause, so a comment after it had no node on its line to attach to. The parser now records that range, and the comment stays after the keyword. [#3521](https://github.com/fsprojects/fantomas/issues/3521) + ## [8.0.7] - 2026-10-05 ### Fixed