fsprojects / fantomas

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

Non needed parentheses are added around lambda call from tuple/members #3082

Open MangelMaxime opened 2 months ago

MangelMaxime commented 2 months ago

Issue created from fantomas-online

Code

func ("/health", fun a b -> "")

Result

func ("/health", (fun a b -> ""))

Problem description

Please describe here the Fantomas problem you encountered. Check out our Contribution Guidelines.

Fantomas adds non needed () around the lambda declaration.

[!NOTE] This does not happen if you have a multi line lambda

func ("/health", fun a b -> 
    // Force new line
    "")

Extra information

Options

Fantomas main branch at 2024-04-16T07:11:28Z - 873d9d70d755f2c058f49b4712f3d22abb28dce4

Default Fantomas configuration

Did you know that you can ignore files when formatting by using a .fantomasignore file? PS: It's unlikely that someone else will solve your specific issue, as it's something that you have a personal stake in.

nojaf commented 2 months ago

Hi, thanks for the report. This is one of those things where we add them because it is the safe thing to do to avoid another related problem. I would accept a PR for this if you are interested.

MangelMaxime commented 2 months ago

This is one of those things where we add them because it is the safe thing to do to avoid another related problem.

That's what I supposed.

I would accept a PR for this if you are interested.

I suppose it should be a small changes, I can give it when I have some time for it. Any pointers, to nudge me in the right direction?

nojaf commented 2 months ago

Step one would be to add a new test in TupleTests.fs:

[<Test>]
let ``maxime thing`` () =
    formatSourceString
        """
func ("/health", fun a b -> "")
"""
        config
    |> prepend newline
    |> should
        equal
        """
func ("/health", fun a b -> "")
"""

Then take a look in the online tool what func ("/health", fun a b -> "") produces (Oak tab), the tuple is stored in ExprTupleNode.

Next look in CodePrinter.fs where that is being processed:

See let genTupleExpr (node: ExprTupleNode):

https://github.com/fsprojects/fantomas/blob/873d9d70d755f2c058f49b4712f3d22abb28dce4/src/Fantomas.Core/CodePrinter.fs#L1872

seems to add the parentheses.

Commenting that out will make the test pass and make 31 others fail.