From cb7a8e0ac7548b8d9f97577fcce91803d46de393 Mon Sep 17 00:00:00 2001 From: dawe Date: Thu, 30 May 2024 14:14:20 +0200 Subject: [PATCH] Check if we just wrote a DotLambda before deciding on space before parens, fix 3050 (#3051) * fix an idempotency issue with dot lambdas, 3050 * add changelog entry * fix warning from analysers * fix by using Oak knowledge and altering the config for the dotlambda construction * instead of using a potentially wrong config for argument nodes, check if the node.Expr is an AppSingleParenArg and just be always atomic in that case when writing the FunctionExpr * release notes for 6.3.5 --- CHANGELOG.md | 5 ++ src/Fantomas.Core.Tests/DotLambdaTests.fs | 72 +++++++++++++++++++++++ src/Fantomas.Core/CodePrinter.fs | 7 ++- 3 files changed, 83 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0f7dd2ef31..f957a1d03f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## 6.3.5 - 2024-05-30 + +### Fixed +* Idempotency problem when _.Property shorthand. [#3050](https://github.com/fsprojects/fantomas/issues/3050) + ## 6.3.4 - 2024-04-16 ### Fixed diff --git a/src/Fantomas.Core.Tests/DotLambdaTests.fs b/src/Fantomas.Core.Tests/DotLambdaTests.fs index 376a4b92a6..45285c3e32 100644 --- a/src/Fantomas.Core.Tests/DotLambdaTests.fs +++ b/src/Fantomas.Core.Tests/DotLambdaTests.fs @@ -59,3 +59,75 @@ let c = _.foo().Length """ let c = _.foo().Length """ + +[] +let ``idempotency problem when _.Property shorthand, 3050`` () = + formatSourceString + """ +"ABC" |> _.ToLower() +""" + { config with + SpaceBeforeUppercaseInvocation = true } + |> prepend newline + |> should + equal + """ +"ABC" |> _.ToLower() +""" + +[] +let ``idempotency problem when _.property shorthand lowercase, 3050`` () = + formatSourceString + """ +"ABC" |> _.toLower() +""" + { config with + SpaceBeforeLowercaseInvocation = true } + |> prepend newline + |> should + equal + """ +"ABC" |> _.toLower() +""" + +[] +let ``idempotency problem when _.property shorthand quoted, 3050`` () = + formatSourceString + """ +"ABC" |> _.``to Lower``() +""" + { config with + SpaceBeforeLowercaseInvocation = true } + |> prepend newline + |> should + equal + """ +"ABC" |> _.``to Lower``() +""" + +[] +let ``idempotency problem when _.Property shorthand with app arg, 3050`` () = + formatSourceString + """ +let Meh () = 1 + +type Bar() = + member this.Foo(v:int):int = v + 1 + +let b = Bar() +b |> _.Foo(Meh ()) +""" + { config with + SpaceBeforeUppercaseInvocation = true } + |> prepend newline + |> should + equal + """ +let Meh () = 1 + +type Bar() = + member this.Foo(v: int) : int = v + 1 + +let b = Bar () +b |> _.Foo(Meh ()) +""" diff --git a/src/Fantomas.Core/CodePrinter.fs b/src/Fantomas.Core/CodePrinter.fs index 2491ef4f17..658f5fcdd1 100644 --- a/src/Fantomas.Core/CodePrinter.fs +++ b/src/Fantomas.Core/CodePrinter.fs @@ -1553,9 +1553,14 @@ let genExpr (e: Expr) = | Expr.IndexFromEnd node -> !- "^" +> genExpr node.Expr |> genNode node | Expr.Typar node -> genSingleTextNode node | Expr.DotLambda node -> + let genDotLambdaExpr expr = + match expr with + | Expr.AppSingleParenArg p -> genExpr p.FunctionExpr +> genExpr p.ArgExpr // be always atomic, see 3050 + | _ -> genExpr expr + genSingleTextNode node.Underscore +> genSingleTextNode node.Dot - +> genExpr node.Expr + +> genDotLambdaExpr node.Expr |> genNode node | Expr.BeginEnd node -> let short =