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 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 |]