Skip to content

Commit

Permalink
Restore condition with non-generic type parameter (#2173)
Browse files Browse the repository at this point in the history
* test

* add TLongIdent option to TyparSupportsMember

* format :)

* DRY - new helper function: colSurr

* format

* Remove blank lines

Co-authored-by: nojaf <florian.verdonck@outlook.com>
  • Loading branch information
jindraivanek and nojaf committed Apr 1, 2022
1 parent 8f2ab14 commit 7373a06
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 9 deletions.
19 changes: 18 additions & 1 deletion src/Fantomas.Tests/FunctionDefinitionTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -935,7 +935,7 @@ let ``space before ^ SRTP type is required in function call, 984`` () =
formatSourceString
false
"""
let inline deserialize< ^a when ( ^a or FromJsonDefaults) : (static member FromJson : ^a -> Json< ^a>)> json =
let inline deserialize< ^a when ^a: (static member FromJson: ^a -> Json< ^a >)> json =
json |> Json.parse |> Json.deserialize< ^a>
"""
config
Expand All @@ -947,6 +947,23 @@ let inline deserialize< ^a when ^a: (static member FromJson: ^a -> Json< ^a >)>
json |> Json.parse |> Json.deserialize< ^a>
"""

[<Test>]
let ``SRTP or condition with non-generic type, 2168`` () =
formatSourceString
false
"""
let inline deserialize< ^a when ( ^a or FromJsonDefaults) : (static member FromJson : ^a -> Json< ^a>)> json =
json |> Json.parse |> Json.deserialize< ^a>
"""
config
|> prepend newline
|> should
equal
"""
let inline deserialize< ^a when (^a or FromJsonDefaults): (static member FromJson: ^a -> Json< ^a >)> json =
json |> Json.parse |> Json.deserialize< ^a>
"""

[<Test>]
let ``equals sign between hash directives, 1218`` () =
formatSourceString
Expand Down
18 changes: 11 additions & 7 deletions src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -527,12 +527,16 @@ and genExprSepEqPrependType
+> sepSpaceOrIndentAndNlnIfExpressionExceedsPageWidth (genExpr astContext e)

and genTyparList astContext tps =
ifElse
(List.atMostOne tps)
(col wordOr tps (genTypar astContext))
(sepOpenT
+> col wordOr tps (genTypar astContext)
+> sepCloseT)
colSurr sepOpenT sepCloseT wordOr tps (genTypar astContext)

and genTypeSupportMember astContext st =
match st with
| SynType.Var (td, _) -> genTypar astContext td
| TLongIdent s -> !-s
| _ -> !- ""

and genTypeSupportMemberList astContext tps =
colSurr sepOpenT sepCloseT wordOr tps (genTypeSupportMember astContext)

and genTypeAndParam astContext typeName (tds: SynTyparDecls option) tcs =
let types openSep tds tcs closeSep =
Expand Down Expand Up @@ -4533,7 +4537,7 @@ and genTypeConstraint astContext node =
genTypar astContext tp -- " :> "
+> genType astContext false t
| TyparSupportsMember (tps, msg) ->
genTyparList astContext tps
genTypeSupportMemberList astContext tps
+> sepColon
+> sepOpenT
+> genMemberSig astContext msg
Expand Down
13 changes: 13 additions & 0 deletions src/Fantomas/Context.fs
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,19 @@ let internal colPreEx f2 f1 (c: seq<'T>) f (ctx: Context) =
else
colEx f1 c f (f2 ctx)

/// Similar to col, but apply two more functions fStart, fEnd at the beginning and the end if the input sequence is bigger thn one item
let internal colSurr fStart fEnd f1 (c: list<'T>) f (ctx: Context) =
if Seq.isEmpty c then
ctx
else
(col f1 c f
|> fun g ->
if (List.moreThanOne c) then
fStart +> g +> fEnd
else
g)
ctx

/// If there is a value, apply f and f' accordingly, otherwise do nothing
let internal opt (f': Context -> _) o f (ctx: Context) =
match o with
Expand Down
3 changes: 2 additions & 1 deletion src/Fantomas/SourceParser.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1605,7 +1605,8 @@ let (|TyparSingle|TyparDefaultsToType|TyparSubtypeOfType|TyparSupportsMember|Typ
TyparSupportsMember(
List.choose
(function
| SynType.Var (tp, _) -> Some tp
| SynType.Var _ as v -> Some v
| TLongIdent _ as i -> Some i
| _ -> None)
tps,
msg
Expand Down

0 comments on commit 7373a06

Please sign in to comment.