Skip to content

Commit 0e4370e

Browse files
committed
fix: print comment before first binary operator without failing
1 parent d18871d commit 0e4370e

File tree

6 files changed

+19
-5
lines changed

6 files changed

+19
-5
lines changed

packages/prettier-plugin-java/src/printers/comments/handle-comments.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,12 @@ function moveExpressionTrailingCommentsToNextOperator(
8888
ctx: BinaryExpressionCtx
8989
) {
9090
const binaryOperators = ctx.BinaryOperator;
91-
let binaryOperatorIndex = 1;
91+
let binaryOperatorIndex = 0;
9292
if (binaryOperators?.length) {
9393
ctx.unaryExpression.forEach(unaryExpression => {
9494
if (hasTrailingComments(unaryExpression)) {
9595
while (
96-
binaryOperatorIndex < binaryOperators.length &&
96+
binaryOperatorIndex < binaryOperators.length - 1 &&
9797
unaryExpression.location.endOffset &&
9898
binaryOperators[binaryOperatorIndex].startOffset <
9999
unaryExpression.location.endOffset

packages/prettier-plugin-java/test/repository-test/core-test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ describe("prettier-java", () => {
1515
testRepositorySample(
1616
resolve(__dirname, "../../samples/spring-boot"),
1717
"./gradlew",
18-
["clean", "build", "-Ddisable.checks", "-xtest", "--no-scan"]
18+
["compileJava"]
1919
);
2020

2121
jhipsterRepository.forEach(repository => {

packages/prettier-plugin-java/test/test-utils.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,7 @@ export function testRepositorySample(
118118
});
119119
if (code.status !== 0) {
120120
expect.fail(
121-
`Cannot build ${testFolder}, please check the output below:\n` +
122-
code.error ?? code.stderr
121+
`Cannot build ${testFolder}, please check the output below:\n${code.error ?? code.stderr}`
123122
);
124123
}
125124
});

packages/prettier-plugin-java/test/unit-test/comments/comments-spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const __dirname = path.dirname(url.fileURLToPath(import.meta.url));
77
describe("prettier-java", () => {
88
testSample(path.resolve(__dirname, "./class"));
99
testSample(path.resolve(__dirname, "./edge"));
10+
testSample(path.resolve(__dirname, "./expression"));
1011
testSample(path.resolve(__dirname, "./interface"));
1112
testSample(path.resolve(__dirname, "./package"));
1213
testSample(
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Example {
2+
3+
void example() {
4+
0 //
5+
+ 1;
6+
}
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Example {
2+
3+
void example() {
4+
0 + //
5+
1;
6+
}
7+
}

0 commit comments

Comments
 (0)