Skip to content

Commit

Permalink
Add space around dots when negative floats are inside Expr.TripleNumb…
Browse files Browse the repository at this point in the history
…erIndexRange (#3106)
  • Loading branch information
nojaf authored Aug 16, 2024
1 parent e2aa020 commit f4738e0
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
14 changes: 14 additions & 0 deletions src/Fantomas.Core.Tests/ListTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2292,3 +2292,17 @@ let fns =
Functions[Checked false
OnChange(fun _ -> s |> updateSettings)]
"""

[<Test>]
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 |]
"""
13 changes: 11 additions & 2 deletions src/Fantomas.Core/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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 ->
Expand Down

0 comments on commit f4738e0

Please sign in to comment.