fsprojects / fantomas

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

Insertion of a newline changes precedence of the `||>` operator #2866

Closed ljb closed 3 months ago

ljb commented 1 year ago

Issue created from fantomas-online

Code

module Test

let createTuple (a: string) (b: string) = a, b

let value =
    match "string" with
    | "value" -> "1", "2"
    | _ -> "111111111111111111111111111111111111111111111111111111111111111111111", "22222222222222222222222222222222222"
    ||> createTuple

Result

module Test

let createTuple (a: string) (b: string) = a, b

let value =
    match "string" with
    | "value" -> "1", "2"
    | _ ->
        "111111111111111111111111111111111111111111111111111111111111111111111",
        "22222222222222222222222222222222222" ||> createTuple

Problem description

Running version 6.0.1 of Fantomas locally results in a slighly different formating compared to the online version:

module Test

let createTuple (a: string) (b: string) = a, b

let value =
    match "string" with
    | "value" -> "1", "2"
    | _ ->
        "111111111111111111111111111111111111111111111111111111111111111111111", "22222222222222222222222222222222222"
    ||> createTuple

Both the code generated by Fantomas online and localy results in the same compilation error. Running dotnet build with the .NET SDK 7.0.203 will give the following error:

C:\repos\test\test.fs(10,9): error FS0001: This expression was expected to have type↔    ''a * 'b'    ↔but here has type↔    'string' [C:\repos\test\test.fsproj]
C:\repos\test\test.fs(10,51): error FS0001: The type 'string' does not match the type 'string * string' [C:\repos\test\test.fsproj]

The problem is that the call to ||> createTuple will be run on the entire match-expression when there isn't a newline. When there is a newline, it will only be run against the line above.

Adding parenthesis around the entire match expression before calling createTuple is a workaround.

Extra information

Options

Fantomas main branch at 04/26/2023

Default Fantomas configuration

Did you know that you can ignore files when formatting from fantomas-tool or the FAKE targets by using a .fantomasignore file?

nojaf commented 1 year ago

Hello, thank you for reporting this issue.

We have seen this problem pop up as well when the indent_size is 2. As a workaround there we have wrapped the match expression in parentheses (as a last resort).

https://github.com/nojaf/fantomas/blob/f144756d7849a29438c84872f61d043be95fcc67/src/Fantomas.Core/CodePrinter.fs#L2083-L2103

I think we could extend this check to ctx.Config.IndentSize - 1 (as seems to be the case for IsIfThenElse _).

Are you interested in submitting a PR for this?