Skip to content

Commit 5a8458e

Browse files
nojafdawedawe
andauthored
Properly transform indexer tuple in setter. (#2974)
* Properly transform indexer tuple in setter. * Update src/Fantomas.Core/ASTTransformer.fs Co-authored-by: dawe <[email protected]> --------- Co-authored-by: dawe <[email protected]>
1 parent 3f293e7 commit 5a8458e

File tree

3 files changed

+49
-18
lines changed

3 files changed

+49
-18
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## 6.2.3 - 2023-11-2
4+
5+
### Fixed
6+
* Crash when trying to format indexed property with three arguments. [#2971](https://github.com/fsprojects/fantomas/issues/2971)
7+
38
## 6.2.2 - 2023-10-18
49

510
### Fixed

src/Fantomas.Core.Tests/TypeDeclarationTests.fs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3642,3 +3642,26 @@ type Meh
36423642
class
36433643
end
36443644
"""
3645+
3646+
[<Test>]
3647+
let ``multi tuple setter with indexer, 2971`` () =
3648+
formatSourceString
3649+
false
3650+
"""
3651+
type MyArray3 () =
3652+
member _.Item
3653+
with get (x: int, y: int, z: int) =
3654+
()
3655+
and set (x: int, y: int, z: int) v =
3656+
()
3657+
"""
3658+
config
3659+
|> prepend newline
3660+
|> should
3661+
equal
3662+
"""
3663+
type MyArray3() =
3664+
member _.Item
3665+
with get (x: int, y: int, z: int) = ()
3666+
and set (x: int, y: int, z: int) v = ()
3667+
"""

src/Fantomas.Core/ASTTransformer.fs

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2661,24 +2661,27 @@ let mkPropertyGetSetBinding
26612661

26622662
let pats =
26632663
match ps with
2664-
| [ SynPat.Tuple(false, [ p1; p2; p3 ], [ comma ], _) ] ->
2665-
let mTuple = unionRanges p1.Range p2.Range
2666-
2667-
[ PatParenNode(
2668-
stn "(" Range.Zero,
2669-
Pattern.Tuple(
2670-
PatTupleNode(
2671-
[ Choice1Of2(mkPat creationAide p1)
2672-
Choice2Of2(stn "," comma)
2673-
Choice1Of2(mkPat creationAide p2) ],
2674-
mTuple
2675-
)
2676-
),
2677-
stn ")" Range.Zero,
2678-
mTuple
2679-
)
2680-
|> Pattern.Paren
2681-
mkPat creationAide p3 ]
2664+
| [ SynPat.Tuple(false, ps, commas, _) ] when
2665+
// This is the case for an indexer setter.
2666+
// The AST is weird in this case and doesn't properly reflect what the user wrote.
2667+
// It will represent `set (x: int, y: int, z: int) v` as a single tuple with 4 patterns and 2 commas.
2668+
ps.Length - 2 = commas.Length
2669+
->
2670+
2671+
let tuplePat =
2672+
let tuplePs = List.take (ps.Length - 1) ps
2673+
let mTuple = tuplePs |> List.map (fun p -> p.Range) |> List.reduce unionRanges
2674+
2675+
match tuplePs with
2676+
// If there is only a single element, it does not need any additional parentheses.
2677+
| [ singlePat ] -> singlePat
2678+
| _ -> SynPat.Paren(SynPat.Tuple(false, tuplePs, commas, mTuple), mTuple)
2679+
|> mkPat creationAide
2680+
2681+
[ tuplePat
2682+
match List.tryLast ps with
2683+
| None -> failwith ""
2684+
| Some indexerPat -> mkPat creationAide indexerPat ]
26822685
| [ SynPat.Tuple(false, [ p1; p2 ], _, _) ] -> [ mkPat creationAide p1; mkPat creationAide p2 ]
26832686
| ps -> List.map (mkPat creationAide) ps
26842687

0 commit comments

Comments
 (0)