From b67ae16fc677cda82bb1c7cd1252426c6e0c48d5 Mon Sep 17 00:00:00 2001 From: Jordan Kiesel Date: Wed, 9 Oct 2024 22:28:23 -0600 Subject: [PATCH] fix: print comment before first binary operator without failing --- .../src/printers/comments/handle-comments.ts | 4 ++-- packages/prettier-plugin-java/test/test-utils.ts | 3 +-- .../test/unit-test/comments/comments-spec.ts | 1 + .../test/unit-test/comments/expression/_input.java | 7 +++++++ .../test/unit-test/comments/expression/_output.java | 7 +++++++ 5 files changed, 18 insertions(+), 4 deletions(-) create mode 100644 packages/prettier-plugin-java/test/unit-test/comments/expression/_input.java create mode 100644 packages/prettier-plugin-java/test/unit-test/comments/expression/_output.java diff --git a/packages/prettier-plugin-java/src/printers/comments/handle-comments.ts b/packages/prettier-plugin-java/src/printers/comments/handle-comments.ts index bb6ac85f..faf02314 100644 --- a/packages/prettier-plugin-java/src/printers/comments/handle-comments.ts +++ b/packages/prettier-plugin-java/src/printers/comments/handle-comments.ts @@ -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 diff --git a/packages/prettier-plugin-java/test/test-utils.ts b/packages/prettier-plugin-java/test/test-utils.ts index 8c902b4e..1fafc456 100644 --- a/packages/prettier-plugin-java/test/test-utils.ts +++ b/packages/prettier-plugin-java/test/test-utils.ts @@ -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}` ); } }); diff --git a/packages/prettier-plugin-java/test/unit-test/comments/comments-spec.ts b/packages/prettier-plugin-java/test/unit-test/comments/comments-spec.ts index d4f68be1..942999e3 100644 --- a/packages/prettier-plugin-java/test/unit-test/comments/comments-spec.ts +++ b/packages/prettier-plugin-java/test/unit-test/comments/comments-spec.ts @@ -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( diff --git a/packages/prettier-plugin-java/test/unit-test/comments/expression/_input.java b/packages/prettier-plugin-java/test/unit-test/comments/expression/_input.java new file mode 100644 index 00000000..75d5180f --- /dev/null +++ b/packages/prettier-plugin-java/test/unit-test/comments/expression/_input.java @@ -0,0 +1,7 @@ +class Example { + + void example() { + 0 // + + 1; + } +} diff --git a/packages/prettier-plugin-java/test/unit-test/comments/expression/_output.java b/packages/prettier-plugin-java/test/unit-test/comments/expression/_output.java new file mode 100644 index 00000000..c3a9f526 --- /dev/null +++ b/packages/prettier-plugin-java/test/unit-test/comments/expression/_output.java @@ -0,0 +1,7 @@ +class Example { + + void example() { + 0 + // + 1; + } +}