You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Feature request] Option to do sanity checks on output #1353
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.
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
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:
Validate the output in the daemon, as the CLI already does, and answer with an error instead of the broken code.
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.
Activity
[-][Feature request] Need an option to do sanity checks on transforms[/-][+][Feature request] Option to do sanity checks on output[/+]nojaf commentedon Jan 8, 2021
Hey @reinux, thank you for your suggestion.
Perhaps as a first step something like
fantomas/src/Fantomas.Tests/TestHelpers.fs
Line 25 in d5f4d9f
Could be added (behind a setting) after formatting.
Are you interested in submitting a PR?
reinux commentedon Jan 8, 2021
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.
reinux commentedon Jan 8, 2021
That said, I can see if I can fit something in my schedule sometime in the coming weeks, along with #1179 .
nojaf commentedon Oct 6, 2026
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:
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.CodeFormatter.FormatDocumentAsyncreturns is sent back to the editor.Two things are still left to do here:
Fantomas.Core.Idempotency stays a
fantomas doctorcheck, 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
FormatResultin the public API ofFantomas.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, andfantomas doctorwould do the full comparison. Both items above stay on the list for then.Check that formatted output keeps every comment
Check that formatted output keeps every comment
Check that formatted output keeps every comment
Check that formatted output keeps every comment
Check that formatted output keeps every comment
Check that formatted output keeps every comment
Check that formatted output keeps every comment
Check that formatted output keeps every comment
Check that formatted output keeps every comment
Check that formatted output keeps every comment
Check that formatted output keeps every comment
Check that formatted output keeps every comment
Check that formatted output keeps every comment
Check that formatted output keeps every comment
Check that formatted output keeps every comment
Check that formatted output keeps every comment
Check that formatted output keeps every comment
Check that formatted output keeps every comment
Check that formatted output keeps every comment
Check that formatted output keeps every comment
Check that formatted output keeps every comment