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

FormatASTAsync loses single quotes for characters #3076

Closed
ErikSchierboom opened this issue Apr 12, 2024 · 4 comments · Fixed by #3077
Closed

FormatASTAsync loses single quotes for characters #3076

ErikSchierboom opened this issue Apr 12, 2024 · 4 comments · Fixed by #3077
Labels
good first issue Long hanging fruit: easy issue to get your feet wet!

Comments

@ErikSchierboom
Copy link
Contributor

I've been using FormatASTAsync to format my code, but I found out that it is removed the single quotes from characters.

let code = "let x = 'A'"
let tree, _ = parseFile false (SourceText.ofString code) []    
let formattedCode = CodeFormatter.FormatASTAsync(tree) |> Async.RunSynchronously

I've also tried:

let code = "let x = 'A'"
let [|(tree,_)|] = CodeFormatter.ParseAsync(false, code) |> Async.RunSynchronously
let formattedCode = CodeFormatter.FormatASTAsync(tree) |> Async.RunSynchronously

But that has the same issue.

I'm using:

  • Fantomas.Core: 6.3.2
  • Fantomas.FCS: 6.3.2
  • FSharp.Core: 8.0.200
@ErikSchierboom
Copy link
Contributor Author

I've just tried reproducing it from within a test in Fantomas, but that does work 🤔 I'll do some more digging.

@nojaf
Copy link
Contributor

nojaf commented Apr 12, 2024

Yeah, this is more or less to be expected.
We don't seem to add the single quotes in

| _ -> c.ToString()

In practice, the value of SynConst.Char is quite unreliable, so Fantomas will normally reuse the char content based on the original source code. This is why this works in the test.

let formattedCode = CodeFormatter.FormatASTAsync(tree, code) |> Async.RunSynchronously probably would be another workaround.

I would accept a PR fix for this.

@nojaf nojaf added the good first issue Long hanging fruit: easy issue to get your feet wet! label Apr 12, 2024
@ErikSchierboom
Copy link
Contributor Author

Thanks!

formatSourceString "let s = 'A'" config
|> should equal "let s = 'A'\n"

works, but

formatAST false "let s = 'A'" config
|> should equal "let s = 'A'\n"

doesn't.

I'll see if I can figure it out :)

@ErikSchierboom
Copy link
Contributor Author

ErikSchierboom commented Apr 12, 2024

I've opened a PR: #3077

ErikSchierboom added a commit to ErikSchierboom/fantomas that referenced this issue Apr 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Long hanging fruit: easy issue to get your feet wet!
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants