Skip to content

Commit

Permalink
fix: stable format for if expression with trailing comment
Browse files Browse the repository at this point in the history
  • Loading branch information
jtkiesel committed Aug 6, 2023
1 parent b1b9e7c commit 4a36754
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { builders } from "prettier/doc";
import { concat, dedent, group, indent, join } from "./prettier-builder";
import { printTokenWithComments } from "./comments/format-comments";
import { handleCommentsIfStatement } from "./comments/handle-comments";
import {
hasLeadingLineComments,
hasTrailingLineComments
Expand Down Expand Up @@ -192,12 +193,19 @@ export class BlocksAndStatementPrettierVisitor extends BaseCstPrettierPrinter {
}

ifStatement(ctx: IfStatementCtx) {
handleCommentsIfStatement(ctx);

const expression = this.visit(ctx.expression);

const ifStatement = this.visit(ctx.statement[0], {
allowEmptyStatement: true
});
const ifSeparator = isStatementEmptyStatement(ifStatement) ? "" : " ";
const ifStatementCtx =
ctx.statement[0].children.statementWithoutTrailingSubstatement?.[0]
.children;
const emptyIfStatement = ifStatementCtx?.emptyStatement !== undefined;
const hasIfBlock = ifStatementCtx?.block !== undefined;
const ifSeparator = emptyIfStatement ? "" : hasIfBlock ? " " : indent(line);

let elsePart: Doc = "";
if (ctx.Else !== undefined) {
Expand All @@ -208,7 +216,9 @@ export class BlocksAndStatementPrettierVisitor extends BaseCstPrettierPrinter {

const elseOnSameLine =
hasTrailingLineComments(ctx.statement[0]) ||
hasLeadingLineComments(ctx.Else[0])
hasLeadingLineComments(ctx.Else[0]) ||
emptyIfStatement ||
!hasIfBlock
? hardline
: " ";

Expand All @@ -226,7 +236,7 @@ export class BlocksAndStatementPrettierVisitor extends BaseCstPrettierPrinter {
ifSeparator
])
]),
ifStatement,
hasIfBlock ? ifStatement : indent(ifStatement),
elsePart
]);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { hasLeadingComments } from "./comments-utils";
import { BinaryExpressionCtx, IToken } from "java-parser";
import { hasLeadingComments, hasTrailingComments } from "./comments-utils";
import { BinaryExpressionCtx, IToken, IfStatementCtx } from "java-parser";

export function handleCommentsBinaryExpression(ctx: BinaryExpressionCtx) {
let unaryExpressionIndex = 1;
Expand Down Expand Up @@ -43,3 +43,45 @@ export function handleCommentsBinaryExpression(ctx: BinaryExpressionCtx) {
});
}
}

export function handleCommentsIfStatement(ctx: IfStatementCtx) {
const rBrace = ctx.RBrace[0];
if (!hasTrailingComments(rBrace)) {
return;
}
const statement =
ctx.statement[0].children.statementWithoutTrailingSubstatement?.[0]
.children;
if (!statement) {
return;
}
const inner = [
statement.assertStatement,
statement.breakStatement,
statement.continueStatement,
statement.doStatement,
statement.emptyStatement,
statement.expressionStatement,
statement.expressionStatement,
statement.returnStatement,
statement.switchStatement,
statement.synchronizedStatement,
statement.throwStatement,
statement.tryStatement,
statement.yieldStatement
].find(s => s)?.[0];
const block = statement.block?.[0].children.blockStatements?.[0];
const rCurly = statement.block?.[0].children.RCurly[0];
const target = inner ?? block ?? rCurly;
if (!target) {
return;
}
if (block) {
block.location.startLine--;
}
if (!target.leadingComments) {
target.leadingComments = [];
}
target.leadingComments.unshift(...rBrace.trailingComments);
delete ctx.RBrace[0].trailingComments;
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@ private void myFunction(
/* axis y */int arg2,
/* axis z */int arg3
) {
if (arg1 == 0 && arg2 == 0 && arg == 3) throw new RuntimeException(
"X Y Z cannot be all 0"
);
if (arg1 == 0 && arg2 == 0 && arg == 3)
throw new RuntimeException("X Y Z cannot be all 0");

int /*variable name is of value var */var = arg1 + arg2 + arg3;
if /*true*/(var == 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,26 @@ void commentsIfLineComment() {
if ( // test
t) {
}

if (true) // comment
System.out.println("Oops");

if (true) {
// comment
}

if (true) // comment
{}

if (true) // comment
{
System.out.println("Oops");
}

if (true) // comment
{
if (true) {}
}
}

void commentsIfBlockComment() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,28 @@ void commentsIfLineComment() {
if ( // test
t
) {}

if (true)
// comment
System.out.println("Oops");

if (true) {
// comment
}

if (true) {
// comment
}

if (true) {
// comment
System.out.println("Oops");
}

if (true) {
// comment
if (true) {}
}
}

void commentsIfBlockComment() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,27 @@ public void forEachWithEmptyStatement(List<String> list) {
}

public void ifElseWithEmptyStatements() {
if (test); else {
if (test);
else {
System.out.println("one");
}

if (test) {
System.out.println("two");
} else;

if (test); else;
if (test);
else;
}

public void ifElseWithEmptyStatementsWithComments() {
if (test) /*test*/; else {
if (test)/*test*/;
else {
System.out.println("one");
}

if (test); /*test*/else {
if (test);
/*test*/else {
System.out.println("one");
}

Expand All @@ -65,9 +69,11 @@ public void ifElseWithEmptyStatementsWithComments() {
System.out.println("two");
} else;/*test*/

if (test); /*test*/else;/*test*/
if (test);
/*test*/else;/*test*/

if (test) /*test*/; else /*test*/;
if (test)/*test*/;
else /*test*/;
}

public void simpleWhileWithEmptyStatement(boolean one) {
Expand Down
11 changes: 6 additions & 5 deletions packages/prettier-plugin-java/test/unit-test/sealed/_output.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,12 @@ default Shape rotate(double angle) {
}

default String areaMessage() {
if (this instanceof Circle) return "Circle: " + area(); else if (
this instanceof Rectangle
) return "Rectangle: " + area(); else if (
this instanceof RightTriangle
) return "Triangle: " + area();
if (this instanceof Circle)
return "Circle: " + area();
else if (this instanceof Rectangle)
return "Rectangle: " + area();
else if (this instanceof RightTriangle)
return "Triangle: " + area();
// :(
throw new IllegalArgumentException();
}
Expand Down

0 comments on commit 4a36754

Please sign in to comment.