Skip to content

Commit

Permalink
fix: print comment before first binary operator without failing
Browse files Browse the repository at this point in the history
  • Loading branch information
jtkiesel committed Oct 11, 2024
1 parent d18871d commit 0e4370e
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 5 deletions.
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe("prettier-java", () => {
testRepositorySample(
resolve(__dirname, "../../samples/spring-boot"),
"./gradlew",
["clean", "build", "-Ddisable.checks", "-xtest", "--no-scan"]
["compileJava"]
);

jhipsterRepository.forEach(repository => {
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 0e4370e

Please sign in to comment.