Skip to content

[Feature request] Option to do sanity checks on output #1353

Open

Description

@reinux

I propose we ...

Do sanity checks to ensure the output matches the semantics of the input

The existing way of Fantomas deals with this problem is ...

If it breaks something, there isn't any guarantee that anyone will notice

Pros and Cons

The advantages of making this adjustment to Fantomas are ...

  • Ensure that Fantomas doesn't introduce bugs in code or delete code etc.
  • Confidence for use in production
  • More robust unit tests

The disadvantages of making this adjustment to Fantomas are ...

  • Slight performance hit
  • Need to consider situations where AST differs but behavior doesn't (e.g. superfluous brackets)

Examples

Please provide multiple examples (before and after scenarios) and explain what rules did apply to achieve the outcome.

A bug like #1352 provides an error message instead of producing incorrect code without warning.

Extra information

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

M

Related suggestions: (put links to related suggestions here)

I would prefer that Fantomas refuse to change code if it incorrectly thinks it may have changed the semantics, than for it to produce code that may or may not compile but is nonetheless wrong.

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
  • 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)

Activity

  1. changed the title [-][Feature request] Need an option to do sanity checks on transforms[/-] [+][Feature request] Option to do sanity checks on output[/+] on Jan 8, 2021
  2. nojaf commented on Jan 8, 2021

    @nojaf
    Contributor

    Hey @reinux, thank you for your suggestion.
    Perhaps as a first step something like

    let private isValidAndHasNoWarnings fileName source parsingOptions =

    Could be added (behind a setting) after formatting.
    Are you interested in submitting a PR?

  3. reinux commented on Jan 8, 2021

    @reinux
    Author

    Thanks, I think that would indeed be a good starting point.

    It seems easy enough to do, but unfortunately, I've got a lot on my plate right now, so I can't commit to anything for at least a couple months :(

    Sorry about that.

  4. reinux commented on Jan 8, 2021

    @reinux
    Author

    That said, I can see if I can fit something in my schedule sometime in the coming weeks, along with #1179 .

  5. nojaf commented on Oct 6, 2026

    @nojaf
    Contributor

    Revisiting this: Fantomas checks its own output these days, but not everywhere, and not as thoroughly as the snapshot tests do.

    Every test case checks that the result is valid F# under every define combination, that every comment of the input is still there (same text, same count), that every conditional and warn directive (#if, #else, #endif, #nowarn, #warnon) is still there in the same order, and that the result is idempotent.

    What users get today:

    • The CLI (fantomas <file>) only checks that the result parses, under every define combination. If it doesn't, the file is left unchanged and an error is reported.
    • The daemon, which editors use, checks nothing: whatever CodeFormatter.FormatDocumentAsync returns is sent back to the editor.

    Two things are still left to do here:

    1. Validate the output in the daemon, as the CLI already does, and answer with an error instead of the broken code.
    2. Check that comments and directives are preserved, in both the CLI and the daemon. This is the check that catches output that is valid F# but still wrong, which is what this issue is about. The test harness already has the logic, and it can move into Fantomas.Core.

    Idempotency stays a fantomas doctor check, as it doubles the formatting time. A full check that the meaning is unchanged (for example comparing syntax trees) is not planned: Fantomas changes the tree on purpose in places, so such a check would reject intended formatting.

    Update: this is easier to pick up in the next major version. Doing the comment check on every format cheaply means formatting has to report what it found, which changes FormatResult in the public API of Fantomas.Core. The full comparison (parsing the output again) roughly doubles what validating costs, so a format run would only search the output for the text of each comment, and fantomas doctor would do the full comparison. Both items above stay on the list for then.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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