From 5d7a37958b9103cde6c31129e45ed26bf0b58693 Mon Sep 17 00:00:00 2001 From: Florian Verdonck Date: Thu, 12 Dec 2024 11:07:31 +0100 Subject: [PATCH] Add active pattern for empty computation expression (#3141) * Add active pattern for empty computation expression * Comment out lex.fsi * Add changelog entry --- CHANGELOG.md | 5 +++++ .../ComputationExpressionTests.fs | 14 ++++++++++++++ src/Fantomas.Core/ASTTransformer.fs | 19 +++++++++++++++++++ src/Fantomas.FCS/Fantomas.FCS.fsproj | 6 +++--- 4 files changed, 41 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c5c2b31b7..af5acb1f1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## 7.0.0-alpha-004 - 2024-12-12 + +### Fixed +* (Empty/NoEmpty)-bodied named computation expression produces inconsistent formatting. [#3140](https://github.com/fsprojects/fantomas/issues/3140) + ## 7.0.0-alpha-003 - 2024-11-29 ### Removed diff --git a/src/Fantomas.Core.Tests/ComputationExpressionTests.fs b/src/Fantomas.Core.Tests/ComputationExpressionTests.fs index 31c1a96c7..1b7912be9 100644 --- a/src/Fantomas.Core.Tests/ComputationExpressionTests.fs +++ b/src/Fantomas.Core.Tests/ComputationExpressionTests.fs @@ -2453,3 +2453,17 @@ let zero = async { () } // foo |> ignore """ + +[] +let ``empty computation expression with application`` () = + formatSourceString + """ +A() {} +""" + config + |> prepend newline + |> should + equal + """ +A() { } +""" diff --git a/src/Fantomas.Core/ASTTransformer.fs b/src/Fantomas.Core/ASTTransformer.fs index 93240f743..f1c4d9784 100644 --- a/src/Fantomas.Core/ASTTransformer.fs +++ b/src/Fantomas.Core/ASTTransformer.fs @@ -455,6 +455,15 @@ let (|IndexWithoutDot|_|) expr = Some(identifierExpr, indexExpr) | _ -> None +let (|EmptyComputationExpr|_|) expr = + match expr with + | SynExpr.App(ExprAtomicFlag.NonAtomic, + false, + funcExpr, + SynExpr.Record(recordFields = []; range = StartEndRange 1 (mOpen, _, mClose)), + range) -> Some(funcExpr, mOpen, mClose, range) + | _ -> None + let (|MultipleConsInfixApps|_|) expr = let rec visit expr (headAndLastOperator: (SynExpr * SingleTextNode) option) (xs: Queue) = match expr with @@ -1252,6 +1261,16 @@ let mkExpr (creationAide: CreationAide) (e: SynExpr) : Expr = ExprIndexWithoutDotNode(mkExpr creationAide identifierExpr, mkExpr creationAide indexExpr, exprRange) |> Expr.IndexWithoutDot + | EmptyComputationExpr(expr, mOpen, mClose, m) -> + ExprNamedComputationNode( + mkExpr creationAide expr, + stn "{" mOpen, + Expr.Ident(stn "" Range.Zero), + stn "}" mClose, + m + ) + |> Expr.NamedComputation + | ChainExpr links -> let chainLinks = links diff --git a/src/Fantomas.FCS/Fantomas.FCS.fsproj b/src/Fantomas.FCS/Fantomas.FCS.fsproj index db012a03f..dc2b8f3d4 100644 --- a/src/Fantomas.FCS/Fantomas.FCS.fsproj +++ b/src/Fantomas.FCS/Fantomas.FCS.fsproj @@ -303,9 +303,9 @@ SyntaxTree\FsLexOutput\pplex.fs - - SyntaxTree\FsLexOutput\lex.fsi - + + + SyntaxTree\FsLexOutput\lex.fs