Repository navigation
fix: respect prettier-ignore comment placed between annotations - #1140
officialasishkumar wants to merge 3 commits into
Conversation
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
left a comment
There was a problem hiding this comment.
Thanks for the contribution! I've left a comment detailing an issue with this solution, and a potential alternative solution.
| 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 | ||
| ); | ||
| } | ||
|
|
There was a problem hiding this comment.
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;
}There was a problem hiding this comment.
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.
What changed with this PR:
A
// prettier-ignoreor/* 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.handleModifiersCommentsnow 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
Output
Relative issues or prs:
Fixes #890