From 2190c310754ebc5fa278e95bdd444e020df89867 Mon Sep 17 00:00:00 2001 From: Asish Kumar Date: Wed, 11 Nov 2026 11:11:42 +0000 Subject: [PATCH 1/2] fix: respect prettier-ignore comment placed between annotations 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 #890 --- src/comments.ts | 23 +++++++++++++--- .../prettier-ignore/annotation/_input.java | 26 +++++++++++++++++++ .../prettier-ignore/annotation/_output.java | 26 +++++++++++++++++++ .../prettier-ignore/prettier-ignore-spec.ts | 1 + 4 files changed, 72 insertions(+), 4 deletions(-) create mode 100644 test/unit-test/prettier-ignore/annotation/_input.java create mode 100644 test/unit-test/prettier-ignore/annotation/_output.java diff --git a/src/comments.ts b/src/comments.ts index 84dd1fcf..2a976684 100644 --- a/src/comments.ts +++ b/src/comments.ts @@ -275,11 +275,19 @@ function getMemberChainRoot(node: SyntaxNode): SyntaxNode { } 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; @@ -287,6 +295,13 @@ function handleModifiersComments(commentNode: CommentNode) { return false; } +function isAnnotation(node?: SyntaxNode): boolean { + return ( + node?.type === SyntaxType.Annotation || + node?.type === SyntaxType.MarkerAnnotation + ); +} + function handleNameComments(commentNode: CommentNode) { const { enclosingNode, precedingNode } = commentNode; if ( diff --git a/test/unit-test/prettier-ignore/annotation/_input.java b/test/unit-test/prettier-ignore/annotation/_input.java new file mode 100644 index 00000000..9be4f51d --- /dev/null +++ b/test/unit-test/prettier-ignore/annotation/_input.java @@ -0,0 +1,26 @@ +public class PrettierIgnoreAnnotation { + + @Marker + // prettier-ignore + @SuppressFBWarnings({ "AFBR_ABNORMAL_FINALLY_BLOCK_RETURN", "LEST_LOST_EXCEPTION_STACK_TRACE", "BC_UNCONFIRMED_CAST_OF_RETURN_VALUE" }) + @Another({ "a", "b" }) + void method() { + } + + @Marker + /* prettier-ignore */ + @SuppressFBWarnings({ "AFBR_ABNORMAL_FINALLY_BLOCK_RETURN", "LEST_LOST_EXCEPTION_STACK_TRACE", "BC_UNCONFIRMED_CAST_OF_RETURN_VALUE" }) + int field = 1; + + @Marker + // prettier-ignore + @Other + public static class Nested { + int x; + } + + @Marker + // just a comment + @Other({ "a", "b" }) + void other() {} +} diff --git a/test/unit-test/prettier-ignore/annotation/_output.java b/test/unit-test/prettier-ignore/annotation/_output.java new file mode 100644 index 00000000..ac84828d --- /dev/null +++ b/test/unit-test/prettier-ignore/annotation/_output.java @@ -0,0 +1,26 @@ +public class PrettierIgnoreAnnotation { + + @Marker + // prettier-ignore + @SuppressFBWarnings({ "AFBR_ABNORMAL_FINALLY_BLOCK_RETURN", "LEST_LOST_EXCEPTION_STACK_TRACE", "BC_UNCONFIRMED_CAST_OF_RETURN_VALUE" }) + @Another({ "a", "b" }) + void method() {} + + @Marker + /* prettier-ignore */ + @SuppressFBWarnings({ "AFBR_ABNORMAL_FINALLY_BLOCK_RETURN", "LEST_LOST_EXCEPTION_STACK_TRACE", "BC_UNCONFIRMED_CAST_OF_RETURN_VALUE" }) + int field = 1; + + @Marker + // prettier-ignore + @Other + public static class Nested { + + int x; + } + + @Marker + // just a comment + @Other({ "a", "b" }) + void other() {} +} diff --git a/test/unit-test/prettier-ignore/prettier-ignore-spec.ts b/test/unit-test/prettier-ignore/prettier-ignore-spec.ts index f28b4597..f6f8d463 100644 --- a/test/unit-test/prettier-ignore/prettier-ignore-spec.ts +++ b/test/unit-test/prettier-ignore/prettier-ignore-spec.ts @@ -5,6 +5,7 @@ import { testSample } from "../../test-utils.ts"; const __dirname = path.dirname(fileURLToPath(import.meta.url)); describe("prettier-java: try catch", () => { + testSample(path.resolve(__dirname, "./annotation")); testSample(path.resolve(__dirname, "./block")); testSample(path.resolve(__dirname, "./classDeclaration")); testSample(path.resolve(__dirname, "./method")); From 076f6bf73df97226bb34e03ccced7a8ce0e934d4 Mon Sep 17 00:00:00 2001 From: Asish Kumar Date: Mon, 16 Nov 2026 11:24:31 +0530 Subject: [PATCH 2/2] fix: attach comments after annotations to the preceding annotation 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. --- src/comments.ts | 23 ++----------------- .../prettier-ignore/annotation/_input.java | 10 ++++++++ .../prettier-ignore/annotation/_output.java | 9 ++++++++ 3 files changed, 21 insertions(+), 21 deletions(-) diff --git a/src/comments.ts b/src/comments.ts index b8367ff6..f7833251 100644 --- a/src/comments.ts +++ b/src/comments.ts @@ -278,33 +278,14 @@ function getMemberChainRoot(node: SyntaxNode): SyntaxNode { } function handleModifiersComments(commentNode: CommentNode) { - const { precedingNode, followingNode } = commentNode; - if ( - isAnnotation(precedingNode) && - followingNode && - isAnnotation(followingNode) && - isPrettierIgnore(commentNode) - ) { - util.addLeadingComment(followingNode, commentNode); - return true; - } - if ( - precedingNode && - (isAnnotation(precedingNode) || precedingNode.type === SyntaxType.Modifiers) - ) { + const { precedingNode } = commentNode; + if (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 - ); -} - function handleNameComments(commentNode: CommentNode) { const { enclosingNode, precedingNode } = commentNode; if ( diff --git a/test/unit-test/prettier-ignore/annotation/_input.java b/test/unit-test/prettier-ignore/annotation/_input.java index 9be4f51d..d7bca42e 100644 --- a/test/unit-test/prettier-ignore/annotation/_input.java +++ b/test/unit-test/prettier-ignore/annotation/_input.java @@ -23,4 +23,14 @@ public static class Nested { // just a comment @Other({ "a", "b" }) void other() {} + + @A({"a" + }) // prettier-ignore + @B({"b" + }) + void sameLine() {} + + @Marker + // comment + class Commented {} } diff --git a/test/unit-test/prettier-ignore/annotation/_output.java b/test/unit-test/prettier-ignore/annotation/_output.java index ac84828d..7ae93104 100644 --- a/test/unit-test/prettier-ignore/annotation/_output.java +++ b/test/unit-test/prettier-ignore/annotation/_output.java @@ -23,4 +23,13 @@ public static class Nested { // just a comment @Other({ "a", "b" }) void other() {} + + @A({"a" + }) // prettier-ignore + @B({ "b" }) + void sameLine() {} + + @Marker + // comment + class Commented {} }