Skip to content

Commit

Permalink
fix(printer): no longer fail when printing binary expression with com…
Browse files Browse the repository at this point in the history
…ment before first operator
  • Loading branch information
jtkiesel committed Oct 10, 2024
1 parent d18871d commit 6fa4b31
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { hasLeadingComments, hasTrailingComments } from "./comments-utils.js";
import {
BinaryExpressionCtx,
CstNode,
IToken,
UnaryExpressionCstNode
} from "java-parser";
import { hasLeadingComments, hasTrailingComments } from "./comments-utils.js";

export function handleCommentsBinaryExpression(ctx: BinaryExpressionCtx) {
moveOperatorLeadingCommentsToNextExpression(ctx);
Expand Down Expand Up @@ -88,12 +88,12 @@ function moveExpressionTrailingCommentsToNextOperator(
ctx: BinaryExpressionCtx
) {
const binaryOperators = ctx.BinaryOperator;
let binaryOperatorIndex = 1;
let binaryOperatorIndex = 0;
if (binaryOperators?.length) {
ctx.unaryExpression.forEach(unaryExpression => {
if (hasTrailingComments(unaryExpression)) {
while (
binaryOperatorIndex < binaryOperators.length &&
binaryOperatorIndex < binaryOperators.length - 1 &&
unaryExpression.location.endOffset &&
binaryOperators[binaryOperatorIndex].startOffset <
unaryExpression.location.endOffset
Expand Down
3 changes: 1 addition & 2 deletions packages/prettier-plugin-java/test/test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,7 @@ export function testRepositorySample(
});
if (code.status !== 0) {
expect.fail(
`Cannot build ${testFolder}, please check the output below:\n` +
code.error ?? code.stderr
`Cannot build ${testFolder}, please check the output below:\n${code.error ?? code.stderr}`
);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const __dirname = path.dirname(url.fileURLToPath(import.meta.url));
describe("prettier-java", () => {
testSample(path.resolve(__dirname, "./class"));
testSample(path.resolve(__dirname, "./edge"));
testSample(path.resolve(__dirname, "./expression"));
testSample(path.resolve(__dirname, "./interface"));
testSample(path.resolve(__dirname, "./package"));
testSample(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Example {

void example() {
0 //
+ 1;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Example {

void example() {
0 + //
1;
}
}

0 comments on commit 6fa4b31

Please sign in to comment.