Skip to content

Commit 6935bd8

Browse files
committed
fix: eliminate repeated printer function execution from our naively-implemented 'call' helper
1 parent 75771a6 commit 6935bd8

File tree

5 files changed

+98
-11
lines changed

5 files changed

+98
-11
lines changed

packages/prettier-plugin-java/src/printers/blocks-and-statements.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,11 @@ export default {
8484
ifStatement(path, print) {
8585
const { children } = path.node;
8686
const hasEmptyStatement = isEmptyStatement(children.statement[0]);
87+
const statements = map(path, print, "statement");
8788
const statement: Doc[] = [
8889
"if ",
8990
indentInParentheses(call(path, print, "expression")),
90-
hasEmptyStatement ? ";" : [" ", call(path, print, "statement", 0)]
91+
hasEmptyStatement ? ";" : [" ", statements[0]]
9192
];
9293
if (children.Else) {
9394
const danglingComments = printDanglingComments(path);
@@ -103,7 +104,7 @@ export default {
103104
const elseHasEmptyStatement = isEmptyStatement(children.statement[1]);
104105
statement.push(
105106
"else",
106-
elseHasEmptyStatement ? ";" : [" ", call(path, print, "statement", 1)]
107+
elseHasEmptyStatement ? ";" : [" ", statements[1]]
107108
);
108109
}
109110
return statement;

packages/prettier-plugin-java/src/printers/expressions.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,14 @@ export default {
115115

116116
conditionalExpression(path, print) {
117117
const binaryExpression = call(path, print, "binaryExpression");
118+
const expressions = map(path, print, "expression");
118119
return path.node.children.QuestionMark
119120
? group(
120121
indent(
121122
join(line, [
122123
binaryExpression,
123-
["? ", call(path, print, "expression", 0)],
124-
[": ", call(path, print, "expression", 1)]
124+
["? ", expressions[0]],
125+
[": ", expressions[1]]
125126
])
126127
)
127128
)
@@ -468,9 +469,9 @@ export default {
468469
return allArgsExpandable;
469470
}
470471
const headArgs = args.slice(0, -1);
471-
const huggedLastArg = call(
472-
path,
472+
const huggedLastArg = path.call(
473473
argPath => print(argPath, { hug: true }),
474+
"children",
474475
"expression",
475476
args.length - 1
476477
);
@@ -756,7 +757,6 @@ function printTemplate<
756757
T extends StringTemplateCstNode | TextBlockTemplateCstNode,
757758
C extends Exclude<IterProperties<T["children"]>, "embeddedExpression">
758759
>(path: AstPath<T>, print: JavaPrintFn, beginKey: C, midKey: C, endKey: C) {
759-
const { children } = path.node;
760760
const begin = call(path, ({ node }) => node.image, beginKey);
761761
const mids = map(path, ({ node }) => node.image, midKey);
762762
const end = call(path, ({ node }) => node.image, endKey);

packages/prettier-plugin-java/src/printers/helpers.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,9 @@ export function call<
122122
>(
123123
path: AstPath<T>,
124124
callback: MapCallback<IndexValue<IndexValue<T, "children">, P>, U>,
125-
child: P,
126-
index = 0
125+
child: P
127126
) {
128-
return path.map(callback, "children", child)[index];
127+
return path.map(callback, "children", child)[0];
129128
}
130129

131130
export function each<
@@ -318,7 +317,7 @@ export function printClassType(
318317
const node = children[child]![childIndex];
319318
const next = array.at(index + 1);
320319
const nextNode = next && children[next.child]![next.index];
321-
const docs = [call(path, print, child, childIndex)];
320+
const docs = [path.call(print, "children", child, childIndex)];
322321
if (nextNode) {
323322
if (isNonTerminal(node)) {
324323
docs.push(node.name === "annotation" ? " " : ".");

packages/prettier-plugin-java/test/unit-test/if/_input.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,47 @@ public void ifElseIfElseIfElse(boolean one, boolean two, boolean three) {
4040
}
4141
}
4242

43+
void longIfElseChain() {
44+
if (1) {
45+
// 1
46+
} else if (2) {
47+
// 2
48+
} else if (3) {
49+
// 3
50+
} else if (4) {
51+
// 4
52+
} else if (5) {
53+
// 5
54+
} else if (6) {
55+
// 6
56+
} else if (7) {
57+
// 7
58+
} else if (8) {
59+
// 8
60+
} else if (9) {
61+
// 9
62+
} else if (10) {
63+
// 10
64+
} else if (11) {
65+
// 11
66+
} else if (12) {
67+
// 12
68+
} else if (13) {
69+
// 13
70+
} else if (14) {
71+
// 14
72+
} else if (15) {
73+
// 15
74+
} else if (16) {
75+
// 16
76+
} else if (17) {
77+
// 17
78+
} else if (18) {
79+
// 18
80+
} else if (19) {
81+
// 19
82+
} else if (20) {
83+
// 20
84+
}
85+
}
4386
}

packages/prettier-plugin-java/test/unit-test/if/_output.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,48 @@ public void ifElseIfElseIfElse(boolean one, boolean two, boolean three) {
3939
System.out.println("not on or two or three");
4040
}
4141
}
42+
43+
void longIfElseChain() {
44+
if (1) {
45+
// 1
46+
} else if (2) {
47+
// 2
48+
} else if (3) {
49+
// 3
50+
} else if (4) {
51+
// 4
52+
} else if (5) {
53+
// 5
54+
} else if (6) {
55+
// 6
56+
} else if (7) {
57+
// 7
58+
} else if (8) {
59+
// 8
60+
} else if (9) {
61+
// 9
62+
} else if (10) {
63+
// 10
64+
} else if (11) {
65+
// 11
66+
} else if (12) {
67+
// 12
68+
} else if (13) {
69+
// 13
70+
} else if (14) {
71+
// 14
72+
} else if (15) {
73+
// 15
74+
} else if (16) {
75+
// 16
76+
} else if (17) {
77+
// 17
78+
} else if (18) {
79+
// 18
80+
} else if (19) {
81+
// 19
82+
} else if (20) {
83+
// 20
84+
}
85+
}
4286
}

0 commit comments

Comments
 (0)