fantomas
fantomas copied to clipboard
Wrong change of comment's position
Issue created from fantomas-online
Code
exception LoadedSourceNotFoundIgnoring of (*filename*) string * range
Result
exception LoadedSourceNotFoundIgnoring of string * range (*filename*)
Extra information
- [ ] The formatted result breaks by code.
- [ ] The formatted result gives compiler warnings.
- [ ] I or my company would be willing to help fix this.
Options
Fantomas 4.6 branch at 11/07/2021 14:33:13 - a4d21fe91323c394fdb2a0f97e0c980ff5378eaf
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?
Hello, thank you for reporting this issue.
The trivia is assigned to SynUnionCase_
while the first SynField_
might be the better candidate.
Are you interested in submitting a PR?
I saw that we are calling genField
inside genUnionCase
when the fields are not empty on CodePrinter.fs
, but I can't find why it's not generating the trivia for the field
We also have other similar cases happening here
The trivia is not assigned to SynField_
but to SynUnionCase_
, the main problem is that the Ident
of the exception (LoadedSourceNotFoundIgnoring
) is not picked up in ASTTransformer:
https://github.com/fsprojects/fantomas/blob/fe22e3107033dbe0dc70db05190d72919058cfd3/src/Fantomas/AstTransformer.fs#L1113-L1118
should be something like:
and visitSynUnionCase (uc: SynUnionCase) : TriviaNodeAssigner list =
match uc with
| SynUnionCase (attrs, ident, uct, _, _, range) ->
[ yield mkNode SynUnionCase_ range
yield visitIdent ident
yield! visitSynUnionCaseType uct
yield! (visitSynAttributeLists attrs) ]
that will lead to the comment being assigned to the right node.
However, for this to be processed correctly in CodePrinter, you would need to extend the active pattern in SourceParser:
https://github.com/fsprojects/fantomas/blob/18eda76ce035e4592cd58399c166274d0aa8ddc6/src/Fantomas/SourceParser.fs#L31-L44
to include the range and print the trivia in CodePrinter.
This is used in a lot of places so a helper function in CodePrinter might be a good idea.
We also have other similar cases happening here
Similar kind of bug, different solution, please open a new issue.