Skip to content

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

Merged
jtkiesel merged 3 commits into
jhipster:mainfrom
officialasishkumar:fix/prettier-ignore-after-annotation-890
Nov 20, 2026
Merged

jtkiesel merged 3 commits into
jhipster:mainfrom
officialasishkumar:fix/prettier-ignore-after-annotation-890

Conversation

@officialasishkumar

Copy link
Copy Markdown
Contributor

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 Resolved
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.
@jtkiesel
jtkiesel merged commit a3c284c into jhipster:main Nov 20, 2026
6 checks passed
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