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

Comments get removed for method parameter with attribute #2585

Closed
3 tasks
stefan-schweiger opened this issue Oct 17, 2022 · 5 comments · Fixed by #2588
Closed
3 tasks

Comments get removed for method parameter with attribute #2585

stefan-schweiger opened this issue Oct 17, 2022 · 5 comments · Fixed by #2588

Comments

@stefan-schweiger
Copy link
Contributor

Issue created from fantomas-online

Code

type MyType =
    member _.MyMethod
        (
            [<MyAttribute>] inputA: string  // my input
        )
        =
        printf inputA

    member _.MyMethod2
        (
            inputA: string  // my input
        )
        =
        printf inputA

Result

type MyType =
    member _.MyMethod([<MyAttribute>] inputA: string) = printf inputA

    member _.MyMethod2
        (inputA: string) // my input
        =
        printf inputA

Problem description

If you have a type method with an attribute and a comment afterwards the comment gets removed. If you don't include an attribute the formatting does not remove the comment.

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 main branch at 2022-10-14T16:59:37Z - 35f093c

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?

@dawedawe
Copy link
Member

Hey Stefan,
thanks for raising this issue.
Are you interested in trying to fix this yourself? We have documentation which should help you to get a grip on the issue.

@stefan-schweiger
Copy link
Contributor Author

@dawedawe I just tried to take a look, but it's a bit unclear to me if the problem is with it choosing the wrong node to attach the trivia or if it's because the trivia didn't get printed in CodePrinter.

If I look at this code:

type MyType =
    member _.MyMethod
        (
            [<MyAttribute>] inputA: string,  // my comment 1
            inputB: string // my comment 2
        )
        =
        inputA

I see the trivia node for // my comment 2 is attached to the SynLongIdent_ (string) node, but for // my comment 1 it's attached to SynPat_Attrib ([<MyAttribute>] inputA: string).

Can you maybe give me any pointers in the right direction?

@stefan-schweiger
Copy link
Contributor Author

stefan-schweiger commented Oct 19, 2022

Ok, I think I found the problem. It is in Trivia.fs there should probably be this line in visitLastChildNode:

| SynPat_Attrib when not (Array.isEmpty node.Children) -> visitLastChildNode (Array.last node.Children)

When I add this the generated code contains the comment, but for whatever reason also an additional space. Maybe any idea why this happens?

EDIT: This seems to be a general problem as the space also gets added for this code:

Code

type MyType =
    member _.MyMethod
        (
            inputA: string, // my comment 1
            inputB: string // my comment 2
        ) =
        inputA

Result

type MyType =
    member _.MyMethod
        (
            inputA: string,  // my comment 1
            inputB: string // my comment 2
        ) =
        inputA

@nojaf
Copy link
Contributor

nojaf commented Oct 19, 2022

Hello Stefan, thank you for looking into this problem! Much appreciated.
You are right to add additional cases in visitLastChildNode.
In your case, you can add | SynPat_Attrib, | SynPat_Named, SynPat_Typed to the first case.
There is no need for the when not (Array.isEmpty node.Children) bit in this case. There will always be children for those nodes.

image

You actually want the comment to be a part of SynIdent here, that is the smallest node after to pick from. Adding those cases leads you there.

The result after all this will still not be a 100% what you originally had.

type MyType =
    member _.MyMethod
        ([<MyAttribute>] inputA: string) // my input
        =
        printf inputA

    member _.MyMethod2
        (inputA: string) // my input
        =
        printf inputA

The comments are printed outside the ( ... ), while being attached to the SynIdent node.
It would be a bit more complex to fix that case. I can elaborate if you like, but it is up to you go far you want to go with your PR.

@stefan-schweiger
Copy link
Contributor Author

@nojaf thanks, I've created a pull request with those changes and some tests. I will also open an issue about the double whitespace between ", //", I know it's just stylistic but it looks wrong in my opinion.

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.

3 participants