fsprojects / fantomas

FSharp source code formatter
https://fsprojects.github.io/fantomas
Other
770 stars 191 forks source link

Invalid location of comma when using named parameters for a class #2865

Closed MNie closed 3 months ago

MNie commented 1 year ago

Issue created from fantomas-online

Code

type MyCSharpClass() =
    member val Prop = 0.0m with get, set
    member val Prop2 = 0.0m with get, set

type FSharpType = {
    SomeValue: string option
}

let v = { SomeValue = Some "str" }

let instance = 
    MyClass(
        Prop = 
            match value.SomeValue with 
            | Some _ -> 0.0m
            | None -> 0.0m
        ,
        Prop2 =
            match value.SomeValue with
            | Some _ -> 0.0m
            | None -> 0.0m
    )

Result

type MyCSharpClass() =
    member val Prop = 0.0m with get, set
    member val Prop2 = 0.0m with get, set

type FSharpType = { SomeValue: string option }

let v = { SomeValue = Some "str" }

let instance =
    MyClass(
        Prop =
            match value.SomeValue with
            | Some _ -> 0.0m
            | None -> 0.0m,
        Prop2 =
            match value.SomeValue with
            | Some _ -> 0.0m
            | None -> 0.0m
    )

Problem description

It seems that when we have multiple match statements passed to named parameters for a class it places the , in the wrong place. That results in a dotnet fantomas error.

Known workarounds are:

  1. surround the match statement with (),
let instance = 
    MyClass(
        (Prop = 
            match value.SomeValue with 
            | Some _ -> 0.0m
            | None -> 0.0m)
        ,
        (Prop2 =
            match value.SomeValue with
            | Some _ -> 0.0m
            | None -> 0.0m)
    )
  1. extract the match statement to an explicit variable.
let instance = 
    let v1 =
            match value.SomeValue with 
            | Some _ -> 0.0m
            | None -> 0.0m
    let v2 =
            match value.SomeValue with 
            | Some _ -> 0.0m
            | None -> 0.0m
    MyClass(
        Prop = v1,
        Prop2 = v2
    )

Extra information

Options

Fantomas main branch at 2023-04-20T16:04:28Z - 4ed1e9d49b17d6045fc0188336c2a282ae245d6c

Default Fantomas configuration

nojaf commented 1 year ago

Hello,

Thank you for reporting this issue! I think you could extend this check to include your use-case: https://github.com/fsprojects/fantomas/blob/f144756d7849a29438c84872f61d043be95fcc67/src/Fantomas.Core/CodePrinter.fs#L1876-L1895

Are you interested in submitting a PR for this?

MNie commented 1 year ago

@nojaf will try to do so at the end of this week : )