Skip to content

fix: respect prettier-ignore comment placed between annotations - #1140

Open
officialasishkumar wants to merge 3 commits into
jhipster:mainfrom
officialasishkumar:fix/prettier-ignore-after-annotation-890
Open

officialasishkumar wants to merge 3 commits into
jhipster:mainfrom
officialasishkumar:fix/prettier-ignore-after-annotation-890

Conversation

@officialasishkumar

Copy link
Copy Markdown

What changed with this PR:

A // prettier-ignore or /* prettier-ignore */ comment placed between two annotations was attached by Prettier as a trailing comment of the preceding annotation, so the annotation that follows it was still reformatted. handleModifiersComments now attaches such a comment as a leading comment of the following annotation, so that annotation is printed verbatim. Other comments between annotations are handled as before. A unit test (prettier-ignore/annotation) covers line and block comments, marker annotations and a regular comment that must not ignore anything.

Example

Input

@What
// prettier-ignore
@SuppressFBWarnings({ "AFBR_ABNORMAL_FINALLY_BLOCK_RETURN", "LEST_LOST_EXCEPTION_STACK_TRACE", "BC_UNCONFIRMED_CAST_OF_RETURN_VALUE" })
@Another({ "a",   "b" })
void foo() {
}

Output

@What
// prettier-ignore
@SuppressFBWarnings({ "AFBR_ABNORMAL_FINALLY_BLOCK_RETURN", "LEST_LOST_EXCEPTION_STACK_TRACE", "BC_UNCONFIRMED_CAST_OF_RETURN_VALUE" })
@Another({ "a", "b" })
void foo() {}

Relative issues or prs:

Fixes #890

A `// prettier-ignore` (or `/* prettier-ignore */`) comment sitting between
two annotations in a modifier list was attached by Prettier's default
algorithm as a trailing comment of the preceding annotation, so the
following annotation was still reformatted:

    @what
    // prettier-ignore
    @SuppressFBWarnings({ "A", "B" })
    void foo() {}

handleModifiersComments now attaches such a comment as a leading comment of
the following annotation, which makes hasPrettierIgnore keep that annotation
verbatim. Other comments between annotations keep their existing behaviour.

Fixes jhipster#890

@jtkiesel jtkiesel left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution! I've left a comment detailing an issue with this solution, and a potential alternative solution.

Comment thread src/comments.ts Outdated
Comment on lines +277 to +304
function handleModifiersComments(commentNode: CommentNode) {
const { precedingNode } = commentNode;
const { precedingNode, followingNode } = commentNode;
if (
precedingNode?.type === SyntaxType.Annotation ||
precedingNode?.type === SyntaxType.MarkerAnnotation ||
precedingNode?.type === SyntaxType.Modifiers
isAnnotation(precedingNode) &&
followingNode &&
isAnnotation(followingNode) &&
isPrettierIgnore(commentNode)
) {
util.addLeadingComment(followingNode, commentNode);
return true;
}
if (
precedingNode &&
(isAnnotation(precedingNode) || precedingNode.type === SyntaxType.Modifiers)
) {
util.addTrailingComment(precedingNode, commentNode);
return true;
}
return false;
}

function isAnnotation(node?: SyntaxNode): boolean {
return (
node?.type === SyntaxType.Annotation ||
node?.type === SyntaxType.MarkerAnnotation
);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because this solution doesn't compare line numbers of the comment vs the preceding node, it makes the mistake of attaching this comment as a leading comment of its following node, while it should stay as a trailing comment on the preceding node:

@A({"a"
}) // prettier-ignore
@B({"b"
})
void f() {}

This not only moves the position of the comment, but also causes Prettier to ignore formatting of the 2nd annotation instead of the 1st:

@A({ "a" })
// prettier-ignore
@B({"b"
})
void f() {}

This could be fixed by comparing line numbers, but an even easier solution seems to be simply removing annotation handling from this handleModifiersComments function altogether, as it doesn't appear to be necessary any longer.

As far as I can tell, the only situation where this comment handler is actually needed is when a line comment is placed in between an annotation and class, for example:

@Marker
// comment
class Nested {}

Without this comment handler, the above would be printed like this:

@Marker
class // comment
Nested {}

Funny enough, we don't even have a test for this in our repo. It's only encountered in our E2E tests run agains the Spring source code.

I believe we could replace the current implementation with something like this, which solves the issue described in #890 without causing the issue I mentioned at the top of this comment:

function handleModifiersComments(commentNode: CommentNode) {
  const { precedingNode } = commentNode;
  if (precedingNode?.type === SyntaxType.Modifiers) {
    util.addTrailingComment(precedingNode, commentNode);
    return true;
  }
  return false;
}

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, you're right. I dropped the annotation handling and only keep the trailing comment for Modifiers, and added your same-line example plus the @Marker / comment / class case to the test.

Only attach trailing comments to the Modifiers node and drop the
annotation special case, so a prettier-ignore comment on the same line
as an annotation stays with that annotation.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[2.9.2] prettier-ignore not being respected after annotation

2 participants