Skip to content

Commit

Permalink
fix(parser): no longer ignore whole block when prettier-ignore at start
Browse files Browse the repository at this point in the history
closes #505
  • Loading branch information
jtkiesel committed Sep 6, 2023
1 parent a7f9a5f commit 96cf586
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 1 deletion.
7 changes: 6 additions & 1 deletion packages/java-parser/src/comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package tech.jhipster;

import java.util.Map;

public class StrangePrettierIgnore {

private StrangePrettierIgnore() {}

public static void drinkBeers() {
// prettier-ignore
Map<String, String> beers = Map.of(
"beer1", "Gulden Draak",
"beer2", "Piraat",
"beer3", "Kapittel"
);

System.out.println(beers); // not well formated here
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package tech.jhipster;

import java.util.Map;

public class StrangePrettierIgnore {

private StrangePrettierIgnore() {}

public static void drinkBeers() {
// prettier-ignore
Map<String, String> beers = Map.of(
"beer1", "Gulden Draak",
"beer2", "Piraat",
"beer3", "Kapittel"
);

System.out.println(beers); // not well formated here
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand Down

0 comments on commit 96cf586

Please sign in to comment.