Skip to content

[Feature request] Add additional trivia checks #2431

Open
@nojaf

Description

@nojaf

I propose we do some additional trivia checks when formatting the code.
Consider the following example:

let a = // some comment

     42

We detect two trivia in this case: TriviaItem.Comment and TriviaItem.Newline.
After formatting we expect the same trivia to be present in the result.
It might be possible to add two additional checks:

  • Were two TriviaInstruction created? (This means the trivia was assigned to an AST node).
  • Can we find two trivia in the formatted code?

The existing way of Fantomas deals with this problem is that you manually need to inspect if the result code contains these details. If not, you need to have caught it and report an issue via the online tool.

Pros and Cons

The advantages of making this adjustment to Fantomas are that we are more certain that no information was lost.

The disadvantages of making this adjustment to Fantomas are that it can take a performance hit to check this. And also lead to a very disruptive situation when it did spot a problem. If you are in the middle of some work this will be very inconvenient.

Maybe this needs to be added as a command line flag and could be more targeted at the initial formatting of a code base or an upgrade scenario to the next minor/major.

Examples

Block comments can occur after virtually any F# token. There is still a larger number of cases we just didn't cover. Proactively covering these is something nobody will ever volunteer for and will weigh on the performance without a use-case in the wild. The point is that there are still a lot of examples to be found where comments are lost, detecting these with some additional tooling could be beneficial.

Extra information

Estimated cost (XS, S, M, L, XL, XXL): M / L

Related suggestions: (put links to related suggestions here)

Affidavit (please submit!)

Please tick this by placing a cross in the box:

Please tick all that apply:

  • This is not a breaking change to Fantomas
  • I or my company would be willing to help implement and/or test this.
    (I might be at some point but would prefer someone to take the lead on this. I can provide pointers and mentor someone if there are volunteers).
  • This suggestion is part of the Microsoft style guide (please add a link to section if so)
  • This suggestion is part of the G-Research style guide (please add a link to section if so)

For readers:

Please leave:

👍if you consider this useful.
🎉if you want to participate in the development (if approved).
👎if you don't consider this useful.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions