From 87edc7d818dc2908fa877298c2defbca253ca0ea Mon Sep 17 00:00:00 2001 From: Jordan Kiesel Date: Sun, 7 Sep 2025 22:41:40 -0600 Subject: [PATCH] fix: eliminate repeated printer function execution from our naively-implemented 'call' helper --- .../src/printers/blocks-and-statements.ts | 5 ++- .../src/printers/expressions.ts | 10 ++--- .../src/printers/helpers.ts | 7 ++- .../test/unit-test/if/_input.java | 43 ++++++++++++++++++ .../test/unit-test/if/_output.java | 44 +++++++++++++++++++ 5 files changed, 98 insertions(+), 11 deletions(-) diff --git a/packages/prettier-plugin-java/src/printers/blocks-and-statements.ts b/packages/prettier-plugin-java/src/printers/blocks-and-statements.ts index 4bdc44766..cb61da2be 100644 --- a/packages/prettier-plugin-java/src/printers/blocks-and-statements.ts +++ b/packages/prettier-plugin-java/src/printers/blocks-and-statements.ts @@ -84,10 +84,11 @@ export default { ifStatement(path, print) { const { children } = path.node; const hasEmptyStatement = isEmptyStatement(children.statement[0]); + const statements = map(path, print, "statement"); const statement: Doc[] = [ "if ", indentInParentheses(call(path, print, "expression")), - hasEmptyStatement ? ";" : [" ", call(path, print, "statement", 0)] + hasEmptyStatement ? ";" : [" ", statements[0]] ]; if (children.Else) { const danglingComments = printDanglingComments(path); @@ -103,7 +104,7 @@ export default { const elseHasEmptyStatement = isEmptyStatement(children.statement[1]); statement.push( "else", - elseHasEmptyStatement ? ";" : [" ", call(path, print, "statement", 1)] + elseHasEmptyStatement ? ";" : [" ", statements[1]] ); } return statement; diff --git a/packages/prettier-plugin-java/src/printers/expressions.ts b/packages/prettier-plugin-java/src/printers/expressions.ts index 230e35aa2..92ffc3369 100644 --- a/packages/prettier-plugin-java/src/printers/expressions.ts +++ b/packages/prettier-plugin-java/src/printers/expressions.ts @@ -115,13 +115,14 @@ export default { conditionalExpression(path, print) { const binaryExpression = call(path, print, "binaryExpression"); + const expressions = map(path, print, "expression"); return path.node.children.QuestionMark ? group( indent( join(line, [ binaryExpression, - ["? ", call(path, print, "expression", 0)], - [": ", call(path, print, "expression", 1)] + ["? ", expressions[0]], + [": ", expressions[1]] ]) ) ) @@ -468,9 +469,9 @@ export default { return allArgsExpandable; } const headArgs = args.slice(0, -1); - const huggedLastArg = call( - path, + const huggedLastArg = path.call( argPath => print(argPath, { hug: true }), + "children", "expression", args.length - 1 ); @@ -756,7 +757,6 @@ function printTemplate< T extends StringTemplateCstNode | TextBlockTemplateCstNode, C extends Exclude, "embeddedExpression"> >(path: AstPath, print: JavaPrintFn, beginKey: C, midKey: C, endKey: C) { - const { children } = path.node; const begin = call(path, ({ node }) => node.image, beginKey); const mids = map(path, ({ node }) => node.image, midKey); const end = call(path, ({ node }) => node.image, endKey); diff --git a/packages/prettier-plugin-java/src/printers/helpers.ts b/packages/prettier-plugin-java/src/printers/helpers.ts index bf0ec40c7..c8001403a 100644 --- a/packages/prettier-plugin-java/src/printers/helpers.ts +++ b/packages/prettier-plugin-java/src/printers/helpers.ts @@ -122,10 +122,9 @@ export function call< >( path: AstPath, callback: MapCallback, P>, U>, - child: P, - index = 0 + child: P ) { - return path.map(callback, "children", child)[index]; + return path.map(callback, "children", child)[0]; } export function each< @@ -318,7 +317,7 @@ export function printClassType( const node = children[child]![childIndex]; const next = array.at(index + 1); const nextNode = next && children[next.child]![next.index]; - const docs = [call(path, print, child, childIndex)]; + const docs = [path.call(print, "children", child, childIndex)]; if (nextNode) { if (isNonTerminal(node)) { docs.push(node.name === "annotation" ? " " : "."); diff --git a/packages/prettier-plugin-java/test/unit-test/if/_input.java b/packages/prettier-plugin-java/test/unit-test/if/_input.java index d9b588eab..24672a0a9 100644 --- a/packages/prettier-plugin-java/test/unit-test/if/_input.java +++ b/packages/prettier-plugin-java/test/unit-test/if/_input.java @@ -40,4 +40,47 @@ public void ifElseIfElseIfElse(boolean one, boolean two, boolean three) { } } + void longIfElseChain() { + if (1) { + // 1 + } else if (2) { + // 2 + } else if (3) { + // 3 + } else if (4) { + // 4 + } else if (5) { + // 5 + } else if (6) { + // 6 + } else if (7) { + // 7 + } else if (8) { + // 8 + } else if (9) { + // 9 + } else if (10) { + // 10 + } else if (11) { + // 11 + } else if (12) { + // 12 + } else if (13) { + // 13 + } else if (14) { + // 14 + } else if (15) { + // 15 + } else if (16) { + // 16 + } else if (17) { + // 17 + } else if (18) { + // 18 + } else if (19) { + // 19 + } else if (20) { + // 20 + } + } } diff --git a/packages/prettier-plugin-java/test/unit-test/if/_output.java b/packages/prettier-plugin-java/test/unit-test/if/_output.java index a836f78f7..24672a0a9 100644 --- a/packages/prettier-plugin-java/test/unit-test/if/_output.java +++ b/packages/prettier-plugin-java/test/unit-test/if/_output.java @@ -39,4 +39,48 @@ public void ifElseIfElseIfElse(boolean one, boolean two, boolean three) { System.out.println("not on or two or three"); } } + + void longIfElseChain() { + if (1) { + // 1 + } else if (2) { + // 2 + } else if (3) { + // 3 + } else if (4) { + // 4 + } else if (5) { + // 5 + } else if (6) { + // 6 + } else if (7) { + // 7 + } else if (8) { + // 8 + } else if (9) { + // 9 + } else if (10) { + // 10 + } else if (11) { + // 11 + } else if (12) { + // 12 + } else if (13) { + // 13 + } else if (14) { + // 14 + } else if (15) { + // 15 + } else if (16) { + // 16 + } else if (17) { + // 17 + } else if (18) { + // 18 + } else if (19) { + // 19 + } else if (20) { + // 20 + } + } }