Skip to content

Commit

Permalink
Add support for intersectionConstraints in SynTyparDecl. (fsprojects#…
Browse files Browse the repository at this point in the history
  • Loading branch information
nojaf authored and Josh DeGraw committed Nov 22, 2023
1 parent 522bfd1 commit b3664d0
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 4 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.0-alpha-003 - 2023-11-15

### Fixed
* Reformatting code with flexible type syntax remove hash symbol. [#2984](https://github.com/fsprojects/fantomas/issues/2984)

## 6.3.0-alpha-002 - 2023-11-07

### Changed
Expand Down
30 changes: 30 additions & 0 deletions src/Fantomas.Core.Tests/TypeDeclarationTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3701,3 +3701,33 @@ type MyArray3() =
with get (x: int, y: int, z: int) = ()
and set (x: int, y: int, z: int) v = ()
"""

[<Test>]
let ``intersection constraint, 2984`` () =
formatSourceString
false
"""
let typographyLabel<'msg, 'marker & #IFabLabel>() = ()
"""
config
|> prepend newline
|> should
equal
"""
let typographyLabel<'msg, 'marker & #IFabLabel> () = ()
"""

[<Test>]
let ``multiple intersection constraint, 2984`` () =
formatSourceString
false
"""
let typographyLabel<'msg, 'marker & #IFabLabel & #IFoo & #Bar>() = ()
"""
config
|> prepend newline
|> should
equal
"""
let typographyLabel<'msg, 'marker & #IFabLabel & #IFoo & #Bar> () = ()
"""
15 changes: 13 additions & 2 deletions src/Fantomas.Core/ASTTransformer.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1997,13 +1997,24 @@ let mkModuleDecl (creationAide: CreationAide) (decl: SynModuleDecl) =
|> ModuleDecl.NestedModule
| decl -> failwithf $"Failed to create ModuleDecl for %A{decl}"

let mkSynTyparDecl (creationAide: CreationAide) (SynTyparDecl(attributes = attrs; typar = typar)) =
let mkSynTyparDecl
(creationAide: CreationAide)
(SynTyparDecl(attributes = attrs; typar = typar; intersectionConstraints = intersectionConstraints; trivia = trivia))
=
let m =
match List.tryHead attrs with
| None -> typar.Range
| Some a -> unionRanges a.Range typar.Range

TyparDeclNode(mkAttributes creationAide attrs, mkSynTypar typar, m)
let intersectionConstraintNodes =
if intersectionConstraints.Length <> trivia.AmpersandRanges.Length then
failwith "Unexpected mismatch in SynTyparDecl between intersectionConstraints and AmpersandRanges"
else
(trivia.AmpersandRanges, intersectionConstraints)
||> List.zip
|> List.collect (fun (amp, t) -> [ Choice2Of2(stn "&" amp); Choice1Of2(mkType creationAide t) ])

TyparDeclNode(mkAttributes creationAide attrs, mkSynTypar typar, intersectionConstraintNodes, m)

let mkSynTyparDecls (creationAide: CreationAide) (tds: SynTyparDecls) : TyparDecls =
match tds with
Expand Down
3 changes: 3 additions & 0 deletions src/Fantomas.Core/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2555,6 +2555,9 @@ let genTyparDecl (isFirstTypeParam: bool) (td: TyparDeclNode) =
genOnelinerAttributes td.Attributes
+> onlyIf (isFirstTypeParam && String.startsWithOrdinal "^" td.TypeParameter.Text) sepSpace
+> genSingleTextNode td.TypeParameter
+> colPre sepSpace sepSpace td.IntersectionConstraints (function
| Choice1Of2 t -> genType t
| Choice2Of2 amp -> genSingleTextNode amp)
|> genNode td

let genTyparDecls (td: TyparDecls) =
Expand Down
21 changes: 19 additions & 2 deletions src/Fantomas.Core/SyntaxOak.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2680,11 +2680,28 @@ type Constant =
| Unit n -> n
| Measure n -> n

type TyparDeclNode(attributes: MultipleAttributeListNode option, typar: SingleTextNode, range) =
type TyparDeclNode
(
attributes: MultipleAttributeListNode option,
typar: SingleTextNode,
intersectionConstraints: Choice<Type, SingleTextNode> list,
range
) =
inherit NodeBase(range)
override val Children: Node array = [| yield! noa attributes; yield typar |]

override val Children: Node array =
[| yield! noa attributes
yield typar
yield!
List.map
(function
| Choice1Of2 t -> Type.Node t
| Choice2Of2 amp -> amp :> Node)
intersectionConstraints |]

member val Attributes = attributes
member val TypeParameter = typar
member val IntersectionConstraints = intersectionConstraints

type TyparDeclsPostfixListNode
(
Expand Down

0 comments on commit b3664d0

Please sign in to comment.