diff --git a/CHANGELOG.md b/CHANGELOG.md index 891db0e0e..aab8e3059 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## 6.3.11 - 2024-08-16 + +### Fixed +* F# Range Operator (\.\.) with negative numbers produces invalid F# code and idempotent. [#3105](https://github.com/fsprojects/fantomas/issues/3105) + ## 6.3.10 - 2024-07-12 ### Changed diff --git a/src/Fantomas.Core.Tests/ListTests.fs b/src/Fantomas.Core.Tests/ListTests.fs index a60a107ec..9c9704fde 100644 --- a/src/Fantomas.Core.Tests/ListTests.fs +++ b/src/Fantomas.Core.Tests/ListTests.fs @@ -2292,3 +2292,17 @@ let fns = Functions[Checked false OnChange(fun _ -> s |> updateSettings)] """ + +[] +let ``negative numbers in range operator, 3105`` () = + formatSourceString + """ +[|-24.0 .. -1.0 .. -30.0|] +""" + config + |> prepend newline + |> should + equal + """ +[| -24.0 .. -1.0 .. -30.0 |] +""" diff --git a/src/Fantomas.Core/CodePrinter.fs b/src/Fantomas.Core/CodePrinter.fs index f1b58cbda..d78a975fd 100644 --- a/src/Fantomas.Core/CodePrinter.fs +++ b/src/Fantomas.Core/CodePrinter.fs @@ -1546,10 +1546,19 @@ let genExpr (e: Expr) = |> genNode node | Expr.IndexRangeWildcard node -> genSingleTextNode node | Expr.TripleNumberIndexRange node -> + let isNegativeFloat (text: string) = + text.StartsWith("-", StringComparison.Ordinal) && text.Contains(".") + + let genDots (before: SingleTextNode) (dots: SingleTextNode) (after: SingleTextNode) = + if not (isNegativeFloat before.Text) && not (isNegativeFloat after.Text) then + genSingleTextNode dots + else + sepSpace +> genSingleTextNode dots +> sepSpace + genSingleTextNode node.Start - +> genSingleTextNode node.StartDots + +> genDots node.Start node.StartDots node.Center +> genSingleTextNode node.Center - +> genSingleTextNode node.EndDots + +> genDots node.Center node.EndDots node.EndDots +> genSingleTextNode node.End |> genNode node | Expr.IndexRange node ->