diff --git a/packages/java-parser/src/comments.js b/packages/java-parser/src/comments.js index cb3b5394..66397177 100644 --- a/packages/java-parser/src/comments.js +++ b/packages/java-parser/src/comments.js @@ -238,7 +238,12 @@ function attachComments( // prettier ignore support for (let i = 0; i < nodeLeadingComments.length; i++) { if (isPrettierIgnoreComment(nodeLeadingComments[i])) { - mostEnclosiveCstNodeByStartOffset[startOffset].ignore = true; + const node = mostEnclosiveCstNodeByStartOffset[startOffset]; + const ignoreNode = + node.name === "blockStatements" + ? node.children.blockStatement[0] + : node; + ignoreNode.ignore = true; break; } } diff --git a/packages/prettier-plugin-java/test/unit-test/prettier-ignore/block/_input.java b/packages/prettier-plugin-java/test/unit-test/prettier-ignore/block/_input.java new file mode 100644 index 00000000..b131513c --- /dev/null +++ b/packages/prettier-plugin-java/test/unit-test/prettier-ignore/block/_input.java @@ -0,0 +1,19 @@ +package tech.jhipster; + +import java.util.Map; + +public class StrangePrettierIgnore { + + private StrangePrettierIgnore() {} + + public static void drinkBeers() { + // prettier-ignore + Map beers = Map.of( + "beer1", "Gulden Draak", + "beer2", "Piraat", + "beer3", "Kapittel" + ); + + System.out.println(beers); // not well formated here + } +} diff --git a/packages/prettier-plugin-java/test/unit-test/prettier-ignore/block/_output.java b/packages/prettier-plugin-java/test/unit-test/prettier-ignore/block/_output.java new file mode 100644 index 00000000..6a0e7ae3 --- /dev/null +++ b/packages/prettier-plugin-java/test/unit-test/prettier-ignore/block/_output.java @@ -0,0 +1,19 @@ +package tech.jhipster; + +import java.util.Map; + +public class StrangePrettierIgnore { + + private StrangePrettierIgnore() {} + + public static void drinkBeers() { + // prettier-ignore + Map beers = Map.of( + "beer1", "Gulden Draak", + "beer2", "Piraat", + "beer3", "Kapittel" + ); + + System.out.println(beers); // not well formated here + } +} diff --git a/packages/prettier-plugin-java/test/unit-test/prettier-ignore/prettier-ignore-spec.ts b/packages/prettier-plugin-java/test/unit-test/prettier-ignore/prettier-ignore-spec.ts index 5e00de8b..0de49906 100644 --- a/packages/prettier-plugin-java/test/unit-test/prettier-ignore/prettier-ignore-spec.ts +++ b/packages/prettier-plugin-java/test/unit-test/prettier-ignore/prettier-ignore-spec.ts @@ -2,6 +2,7 @@ import { testSample } from "../../test-utils"; import * as path from "path"; describe("prettier-java: try catch", () => { + testSample(path.resolve(__dirname, "./block")); testSample(path.resolve(__dirname, "./classDeclaration")); testSample(path.resolve(__dirname, "./method")); testSample(path.resolve(__dirname, "./multiple-ignore"));