Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Content after hash directive is lost #2293

Closed
3 tasks
nojaf opened this issue Jun 8, 2022 · 0 comments · Fixed by #2296
Closed
3 tasks

Content after hash directive is lost #2293

nojaf opened this issue Jun 8, 2022 · 0 comments · Fixed by #2296
Labels
bug (soundness) good first issue Long hanging fruit: easy issue to get your feet wet! help wanted

Comments

@nojaf
Copy link
Contributor

nojaf commented Jun 8, 2022

Issue created from fantomas-online

Code

#if FOO
#endif // FOO

Result

#if FOO
#endif

Problem description

#else and #endif are restored in a hardcoded fashion in

let internal collectTriviaFromDirectives
(source: ISourceText)
(directives: ConditionalDirectiveTrivia list)
: Trivia list =
directives
|> List.map (function
| ConditionalDirectiveTrivia.If (_, r) ->
let text = source.GetContentAt r
{ Item = Directive text; Range = r }
| ConditionalDirectiveTrivia.Else r -> { Item = Directive "#else"; Range = r }
| ConditionalDirectiveTrivia.EndIf r -> { Item = Directive "#endif"; Range = r })

The range of EndIf does appear to be spanning till the end of the line.
So text could be reused in this case to restore the code comment.
I haven't tested this, but it sure looks that way.
Calling TrimEnd() after source.GetContentAt probably makes sense as well.

A unit test can be added to https://github.com/fsprojects/fantomas/blob/master/src/Fantomas.Core.Tests/CompilerDirectivesTests.fs.

//cc @dsyme

Extra information

  • The formatted result breaks my code.
  • The formatted result gives compiler warnings.
  • I or my company would be willing to help fix this.

Options

Fantomas master branch at 2022-06-07T15:04:36Z - deffd73

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug (soundness) good first issue Long hanging fruit: easy issue to get your feet wet! help wanted
Projects
None yet
1 participant