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

Adding space after prefix operator breaks code #2796

Closed
1 of 3 tasks
ursenzler opened this issue Mar 17, 2023 · 3 comments · Fixed by #2815
Closed
1 of 3 tasks

Adding space after prefix operator breaks code #2796

ursenzler opened this issue Mar 17, 2023 · 3 comments · Fixed by #2815

Comments

@ursenzler
Copy link
Contributor

Issue created from fantomas-online

Code

let inline (~%%) id = int id

let f a b = a + b

let foo () = f %%"17" %%"42"

Result

let inline (~%%) id = int id

let f a b = a + b

let foo () = f %% "17" %% "42"

Problem description

Fantomas adds a space between the prefix operator and the string. Note that this only happens when the string is specified after the operator. If a value is used, then formatting does not add a space.

This works correctly:

    let inline (~%%) id = int id
     
    let f a b = a + b

    let v = 17
    let w = 42
    let foo () = f %%v %%w

The current workaround is to add ( ) around the operator and value.
We use this in our tests to setup data; that's why we use strings there, not values containing strings.
It would also be more consistent never to add a space (as far as I understand it, a space is only added when the prefix operator is followed by a string definition)

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 v5.2 branch at 1/1/1990

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?

@nojaf
Copy link
Contributor

nojaf commented Mar 18, 2023

Hello, thank you for reporting this issue.
There seems to be a difference when you start your prefix operator with ~ versus !.

https://learn.microsoft.com/en-us/dotnet/fsharp/language-reference/operator-overloading#prefix-and-infix-operators

as far as I understand it, a space is only added when the prefix operator is followed by a string definition

That faintly rings a bell, I believe we at some point added this because of the operator we use in Fantomas.
let (!-) (s: string) = ... where !- "foo" was preferred over !-"foo".

Short term, I would accept a fix that takes the % character into account when dealing with a prefix operator. Long term, there should be guidance from the style guide for this matter.

Are you interested in submitting a PR?

@ursenzler
Copy link
Contributor Author

I have no clue where and how to start :-(

@nojaf
Copy link
Contributor

nojaf commented Mar 22, 2023

We have a fair amount of documentation to get started.

The Oak viewer in the online tool gives a pretty good starting point:
image

Look where ExprPrefixAppNode is constructed (ASTTransformer.fs) and consumed (CodePrinter.fs).

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

Successfully merging a pull request may close this issue.

2 participants