fsprojects / fantomas

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

Wrong formatting in this line At["serviceType"; "case"] #2955

Closed RicoSaupeBosch closed 10 months ago

RicoSaupeBosch commented 10 months ago

fantomas removes the ; and breaks the code of the decoder.

Issue created from fantomas-online

Code

let decode: Decoder<MigrationService> =
        Decode.object (fun fields ->
            { ServiceId = fields.Required.Field "serviceId" ServiceId.decode
              ServiceType =
                fields.Required.At["serviceType"; "case"]Decode.string
                |> fromString<ServiceType>
                |> Option.get
              Name = fields.Required.At["name"]Decode.string
              Server = fields.Required.At["server"]Decode.string
              MaxInstances = fields.Required.At["maxInstances"]Decode.int
              AvailableInstances = fields.Optional.At["maxInstances"]Decode.int
              AliveToken = fields.Required.At["aliveToken"]Decode.datetimeOffset })

Result

let decode: Decoder<MigrationService> =
    Decode.object (fun fields ->
        { ServiceId = fields.Required.Field "serviceId" ServiceId.decode
          ServiceType =
            fields.Required.At["serviceType" "case"]Decode.string
            |> fromString<ServiceType>
            |> Option.get
          Name = fields.Required.At["name"]Decode.string
          Server = fields.Required.At["server"]Decode.string
          MaxInstances = fields.Required.At["maxInstances"]Decode.int
          AvailableInstances = fields.Optional.At["maxInstances"]Decode.int
          AliveToken = fields.Required.At["aliveToken"]Decode.datetimeOffset })

Problem description

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

Extra information

Options

Fantomas main branch at 2023-08-31T06:37:49Z - 369faf81362e7ddb3fce3bd4505216055af35347

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 10 months ago

Hi there, this looks a lot like https://fsprojects.github.io/fantomas/docs/end-users/FAQ.html#Why-does-Fantomas-format-my-lists-strangely-when-I-pass-them-as-arguments

fields.Required.At["serviceType"; "case"]Decode.string needs spaces in order not to be confused with the new indexer syntax. fields.Required.At ["serviceType"; "case"] Decode.string formats correctly

RicoSaupeBosch commented 10 months ago

@nojaf your are right, with that it formats correctly and its even adding spaces inside the brackets. Then maybe fantomas could check for that and already format this correctly. Currently without any spaces after the At its not doing anything.

like that line

fields.Required.At["maxInstances"]Decode.int

to that

fields.Required.At [ "maxInstances" ] Decode.int

image

nojaf commented 10 months ago

Bad input will lead to a bad format result, this has always been the case for Fantomas. I'm actually surprised you are not getting warning FS3365 for fields.Required.At.

image

I don't think there is really anything actionable on Fantomas' side. I'll open an issue at the compiler for the missing warning. Thanks for understanding.