From b56e69f961f976e65f3b3110ed14fb30171ab65b Mon Sep 17 00:00:00 2001 From: Jordan Kiesel Date: Tue, 5 Sep 2023 22:09:35 -0600 Subject: [PATCH] fix(parser): no longer ignore whole block when prettier-ignore at start closes #505 --- packages/java-parser/src/comments.js | 7 ++++++- .../prettier-ignore/block/_input.java | 19 +++++++++++++++++++ .../prettier-ignore/block/_output.java | 19 +++++++++++++++++++ .../prettier-ignore/prettier-ignore-spec.ts | 1 + 4 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 packages/prettier-plugin-java/test/unit-test/prettier-ignore/block/_input.java create mode 100644 packages/prettier-plugin-java/test/unit-test/prettier-ignore/block/_output.java 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"));