Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Respect named tuple arguments in type with constraints. #2146

Merged
merged 1 commit into from
Mar 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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