Skip to content

Commit

Permalink
Respect named tuple arguments in type with constraints. Fixes fsproje…
Browse files Browse the repository at this point in the history
  • Loading branch information
nojaf authored and jindraivanek committed Mar 30, 2022
1 parent c5fb3ef commit 93a020b
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 11 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## [Unreleased]

### Fixed
* Option parameter name is lost in tuple. [#2144](https://github.com/fsprojects/fantomas/issues/2144)

## [4.7.2] - 2022-03-11

### Fixed
Expand Down
30 changes: 30 additions & 0 deletions src/Fantomas.Tests/SignatureTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1884,3 +1884,33 @@ val SampleTupledFunction:
arg4: int ->
int list
"""

[<Test>]
let ``optional parameter in static type member, 2144`` () =
formatSourceString
true
"""
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.
namespace Microsoft.FSharp.Control
[<Sealed>]
[<CompiledName("FSharpAsync")>]
type Async =
static member AwaitEvent: event:IEvent<'Del,'T> * ?cancelAction : (unit -> unit) -> Async<'T> when 'Del : delegate<'T,unit> and 'Del :> System.Delegate
"""
config
|> prepend newline
|> should
equal
"""
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.
namespace Microsoft.FSharp.Control
[<Sealed>]
[<CompiledName("FSharpAsync")>]
type Async =
static member AwaitEvent: event: IEvent<'Del, 'T> * ?cancelAction: (unit -> unit) -> Async<'T>
when 'Del: delegate<'T, unit> and 'Del :> System.Delegate
"""
37 changes: 26 additions & 11 deletions src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -4097,17 +4097,32 @@ and genConstraints astContext (t: SynType) (vi: SynValInfo) =
match ti, vi with
| TFuns ts, SynValInfo (curriedArgInfos, returnType) ->
let namedArgInfos =
(List.map List.head curriedArgInfos)
@ [ returnType ]
|> List.map (fun (SynArgInfo (_, _, i)) -> i)

coli sepArrow ts (fun i t ->
let genNamedArg =
List.tryItem i namedArgInfos
|> Option.bind id
|> optSingle (fun (Ident s) -> !-s +> sepColon)

genNamedArg +> genType astContext false t)
[ yield! curriedArgInfos
yield [ returnType ] ]

let args = List.zip namedArgInfos ts

col sepArrow args (fun (argInfo, t) ->
match argInfo, t with
| [], _ -> genType astContext false t
| [ SynArgInfo (_, isOptional, Some (Ident s)) ], _ ->
onlyIf isOptional (!- "?")
+> !-s
+> sepColon
+> genType astContext false t
| [ SynArgInfo _ ], _ -> genType astContext false t
| multipleArgInfo, TTuple ts ->
let combined = List.zip multipleArgInfo ts

col sepStar combined (fun (argInfo, (_, t)) ->
let genNamed =
match argInfo with
| SynArgInfo (_, isOptional, Some (Ident s)) ->
onlyIf isOptional (!- "?") +> !-s +> sepColon
| _ -> sepNone

genNamed +> genType astContext false t)
| _ -> sepNone)
| _ -> genType astContext false ti

genType
Expand Down

0 comments on commit 93a020b

Please sign in to comment.