From 61e8b343f6fa1a229dfd669213fbe4101654100a Mon Sep 17 00:00:00 2001 From: nojaf Date: Fri, 11 Mar 2022 14:57:41 +0100 Subject: [PATCH] Respect named tuple arguments in type with constraints. Fixes #2144. --- CHANGELOG.md | 5 ++++ src/Fantomas.Tests/SignatureTests.fs | 30 ++++++++++++++++++++++ src/Fantomas/CodePrinter.fs | 37 +++++++++++++++++++--------- 3 files changed, 61 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d0099efbc7..9b6dfdaf7a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/Fantomas.Tests/SignatureTests.fs b/src/Fantomas.Tests/SignatureTests.fs index 0d1fc96982..645116032c 100644 --- a/src/Fantomas.Tests/SignatureTests.fs +++ b/src/Fantomas.Tests/SignatureTests.fs @@ -1884,3 +1884,33 @@ val SampleTupledFunction: arg4: int -> int list """ + +[] +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 + + [] + [] + 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 + +[] +[] +type Async = + static member AwaitEvent: event: IEvent<'Del, 'T> * ?cancelAction: (unit -> unit) -> Async<'T> + when 'Del: delegate<'T, unit> and 'Del :> System.Delegate +""" diff --git a/src/Fantomas/CodePrinter.fs b/src/Fantomas/CodePrinter.fs index 3f6a331e36..ca2570ccd9 100644 --- a/src/Fantomas/CodePrinter.fs +++ b/src/Fantomas/CodePrinter.fs @@ -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