prettier-plugin-java 2.8.0 → 2.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. package/README.md +42 -102
  2. package/dist/index.cjs +2215 -0
  3. package/dist/index.d.cts +1630 -0
  4. package/dist/index.d.mts +1633 -0
  5. package/dist/index.mjs +2215 -0
  6. package/dist/tree-sitter-java_orchard.wasm +0 -0
  7. package/package.json +62 -24
  8. package/dist/comments.d.ts +0 -17
  9. package/dist/comments.js +0 -229
  10. package/dist/index.d.ts +0 -563
  11. package/dist/index.js +0 -29
  12. package/dist/options.d.ts +0 -43
  13. package/dist/options.js +0 -284
  14. package/dist/parser.d.ts +0 -9
  15. package/dist/parser.js +0 -24
  16. package/dist/printer.d.ts +0 -18
  17. package/dist/printer.js +0 -40
  18. package/dist/printers/arrays.d.ts +0 -9
  19. package/dist/printers/arrays.js +0 -9
  20. package/dist/printers/blocks-and-statements.d.ts +0 -117
  21. package/dist/printers/blocks-and-statements.js +0 -340
  22. package/dist/printers/classes.d.ts +0 -157
  23. package/dist/printers/classes.js +0 -485
  24. package/dist/printers/expressions.d.ts +0 -134
  25. package/dist/printers/expressions.js +0 -625
  26. package/dist/printers/helpers.d.ts +0 -73
  27. package/dist/printers/helpers.js +0 -273
  28. package/dist/printers/index.d.ts +0 -2
  29. package/dist/printers/index.js +0 -13
  30. package/dist/printers/interfaces.d.ts +0 -62
  31. package/dist/printers/interfaces.js +0 -175
  32. package/dist/printers/lexical-structure.d.ts +0 -14
  33. package/dist/printers/lexical-structure.js +0 -29
  34. package/dist/printers/names.d.ts +0 -12
  35. package/dist/printers/names.js +0 -11
  36. package/dist/printers/packages-and-modules.d.ts +0 -46
  37. package/dist/printers/packages-and-modules.js +0 -169
  38. package/dist/printers/types-values-and-variables.d.ts +0 -46
  39. package/dist/printers/types-values-and-variables.js +0 -90
@@ -1,625 +0,0 @@
1
- import { builders, utils } from "prettier/doc";
2
- import { call, definedKeys, each, findBaseIndent, flatMap, hasAssignmentOperators, hasLeadingComments, hasNonAssignmentOperators, indentInParentheses, isNonTerminal, isTerminal, map, onlyDefinedKey, printDanglingComments, printList, printName, printSingle } from "./helpers.js";
3
- const { align, breakParent, conditionalGroup, group, hardline, ifBreak, indent, indentIfBreak, join, line, lineSuffixBoundary, softline } = builders;
4
- const { removeLines, willBreak } = utils;
5
- export default {
6
- expression: printSingle,
7
- lambdaExpression(path, print, _, args = {}) {
8
- var _a;
9
- const hug = (_a = args.hug) !== null && _a !== void 0 ? _a : false;
10
- const parameters = call(path, print, "lambdaParameters");
11
- const expression = [hug ? removeLines(parameters) : parameters, " ->"];
12
- const lambdaExpression = path.node.children.lambdaBody[0].children.expression;
13
- const body = call(path, print, "lambdaBody");
14
- if (lambdaExpression) {
15
- const suffix = indent([line, body]);
16
- expression.push(group(hug ? [suffix, softline] : suffix));
17
- }
18
- else {
19
- expression.push(" ", body);
20
- }
21
- return expression;
22
- },
23
- lambdaParameters(path, print, options) {
24
- const parameters = printSingle(path, print);
25
- return !path.node.children.lambdaParametersWithBraces &&
26
- options.arrowParens === "always"
27
- ? ["(", parameters, ")"]
28
- : parameters;
29
- },
30
- lambdaParametersWithBraces(path, print, options) {
31
- var _a;
32
- const { lambdaParameterList } = path.node.children;
33
- if (!lambdaParameterList) {
34
- return "()";
35
- }
36
- const { conciseLambdaParameterList, normalLambdaParameterList } = lambdaParameterList[0].children;
37
- const parameterCount = ((_a = conciseLambdaParameterList === null || conciseLambdaParameterList === void 0 ? void 0 : conciseLambdaParameterList[0].children.conciseLambdaParameter) !== null && _a !== void 0 ? _a : normalLambdaParameterList === null || normalLambdaParameterList === void 0 ? void 0 : normalLambdaParameterList[0].children.normalLambdaParameter).length;
38
- const parameters = call(path, print, "lambdaParameterList");
39
- if (parameterCount > 1) {
40
- return indentInParentheses(parameters);
41
- }
42
- return conciseLambdaParameterList && options.arrowParens === "avoid"
43
- ? parameters
44
- : ["(", parameters, ")"];
45
- },
46
- lambdaParameterList: printSingle,
47
- conciseLambdaParameterList(path, print) {
48
- return printList(path, print, "conciseLambdaParameter");
49
- },
50
- normalLambdaParameterList(path, print) {
51
- return printList(path, print, "normalLambdaParameter");
52
- },
53
- normalLambdaParameter: printSingle,
54
- regularLambdaParameter(path, print) {
55
- return join(" ", [
56
- ...map(path, print, "variableModifier"),
57
- call(path, print, "lambdaParameterType"),
58
- call(path, print, "variableDeclaratorId")
59
- ]);
60
- },
61
- lambdaParameterType: printSingle,
62
- conciseLambdaParameter: printSingle,
63
- lambdaBody: printSingle,
64
- conditionalExpression(path, print, options) {
65
- var _a;
66
- const binaryExpression = call(path, print, "binaryExpression");
67
- const grandparentNodeName = (_a = path.getNode(4)) === null || _a === void 0 ? void 0 : _a.name;
68
- const isInParentheses = grandparentNodeName === "parenthesisExpression";
69
- if (!path.node.children.QuestionMark) {
70
- return isInParentheses ? binaryExpression : group(binaryExpression);
71
- }
72
- const [consequent, alternate] = map(path, print, "expression");
73
- const suffix = [
74
- line,
75
- ["? ", options.useTabs ? indent(consequent) : align(2, consequent)],
76
- line,
77
- [": ", options.useTabs ? indent(alternate) : align(2, alternate)]
78
- ];
79
- const isNestedTernary = grandparentNodeName === "conditionalExpression";
80
- const alignedSuffix = !isNestedTernary || options.useTabs
81
- ? suffix
82
- : align(Math.max(0, options.tabWidth - 2), suffix);
83
- if (isNestedTernary) {
84
- return [group(binaryExpression), alignedSuffix];
85
- }
86
- const parts = [group(binaryExpression), indent(alignedSuffix)];
87
- return isInParentheses ? parts : group(parts);
88
- },
89
- binaryExpression(path, print, options) {
90
- var _a, _b, _c, _d;
91
- const { children } = path.node;
92
- const operands = flatMap(path, print, definedKeys(children, [
93
- "expression",
94
- "pattern",
95
- "referenceType",
96
- "unaryExpression"
97
- ]));
98
- const operators = flatMap(path, operatorPath => {
99
- const { node } = operatorPath;
100
- let image;
101
- if (isTerminal(node)) {
102
- image = node.image;
103
- }
104
- else if (node.children.Less) {
105
- image = "<<";
106
- }
107
- else {
108
- image = node.children.Greater.length === 2 ? ">>" : ">>>";
109
- }
110
- return { image, doc: print(operatorPath) };
111
- }, definedKeys(children, [
112
- "AssignmentOperator",
113
- "BinaryOperator",
114
- "Instanceof",
115
- "shiftOperator"
116
- ]));
117
- const isInList = ((_a = path.getNode(4)) === null || _a === void 0 ? void 0 : _a.name) === "elementValue" ||
118
- ((_b = path.getNode(6)) === null || _b === void 0 ? void 0 : _b.name) === "argumentList";
119
- const binaryExpression = (_d = (_c = children.expression) === null || _c === void 0 ? void 0 : _c[0].children.conditionalExpression) === null || _d === void 0 ? void 0 : _d[0].children.binaryExpression[0];
120
- return binary(operands, operators, {
121
- hasNonAssignmentOperators: (operators.length > 0 && !children.AssignmentOperator) ||
122
- (binaryExpression && hasNonAssignmentOperators(binaryExpression)),
123
- isInList,
124
- isRoot: true,
125
- operatorPosition: options.experimentalOperatorPosition
126
- });
127
- },
128
- unaryExpression(path, print) {
129
- return [
130
- ...map(path, print, "UnaryPrefixOperator"),
131
- call(path, print, "primary"),
132
- ...map(path, print, "UnarySuffixOperator")
133
- ];
134
- },
135
- unaryExpressionNotPlusMinus(path, print) {
136
- const { children } = path.node;
137
- const expression = [];
138
- if (children.UnaryPrefixOperatorNotPlusMinus) {
139
- expression.push(...map(path, print, "UnaryPrefixOperatorNotPlusMinus"));
140
- }
141
- expression.push(call(path, print, "primary"));
142
- if (children.UnarySuffixOperator) {
143
- expression.push(...map(path, print, "UnarySuffixOperator"));
144
- }
145
- return join(" ", expression);
146
- },
147
- primary(path, print) {
148
- var _a, _b;
149
- const { children } = path.node;
150
- if (!children.primarySuffix) {
151
- return call(path, print, "primaryPrefix");
152
- }
153
- const methodInvocations = children.primarySuffix
154
- .filter(({ children }) => children.methodInvocationSuffix)
155
- .map(({ children }) => children.methodInvocationSuffix[0].children);
156
- const hasLambdaMethodParameter = methodInvocations.some(({ argumentList }) => argumentList === null || argumentList === void 0 ? void 0 : argumentList[0].children.expression.some(({ children }) => children.lambdaExpression));
157
- const prefixIsCallExpression = children.primaryPrefix[0].children.newExpression;
158
- const callExpressionCount = methodInvocations.length +
159
- (prefixIsCallExpression ? 1 : 0) +
160
- children.primarySuffix.filter(({ children }) => children.unqualifiedClassInstanceCreationExpression).length;
161
- const fqnOrRefType = (_a = children.primaryPrefix[0].children.fqnOrRefType) === null || _a === void 0 ? void 0 : _a[0].children;
162
- const prefixIsMethodInvocation = (fqnOrRefType === null || fqnOrRefType === void 0 ? void 0 : fqnOrRefType.fqnOrRefTypePartRest) !== undefined &&
163
- ((_b = children.primarySuffix) === null || _b === void 0 ? void 0 : _b[0].children.methodInvocationSuffix) !== undefined;
164
- const prefixIsStaticMethodInvocation = prefixIsMethodInvocation && isCapitalizedIdentifier(fqnOrRefType);
165
- const prefixIsInstanceMethodInvocation = prefixIsMethodInvocation && !prefixIsStaticMethodInvocation;
166
- const mustBreakForCallExpressions = methodInvocations.length > 2 && hasLambdaMethodParameter;
167
- const separator = mustBreakForCallExpressions ? hardline : softline;
168
- const prefix = [
169
- call(path, prefixPath => print(prefixPath, {
170
- lastSeparator: prefixIsStaticMethodInvocation ||
171
- (prefixIsInstanceMethodInvocation && callExpressionCount === 1)
172
- ? ""
173
- : separator
174
- }), "primaryPrefix")
175
- ];
176
- const canBreakForCallExpressions = callExpressionCount > 2 ||
177
- (callExpressionCount === 2 && prefixIsInstanceMethodInvocation) ||
178
- willBreak(prefix);
179
- const suffixes = [];
180
- each(path, suffixPath => {
181
- const { node, previous } = suffixPath;
182
- const suffix = print(suffixPath);
183
- if (node.children.Dot) {
184
- if ((canBreakForCallExpressions &&
185
- ((!previous && prefixIsCallExpression) ||
186
- (previous === null || previous === void 0 ? void 0 : previous.children.methodInvocationSuffix) ||
187
- (previous === null || previous === void 0 ? void 0 : previous.children.unqualifiedClassInstanceCreationExpression))) ||
188
- (!node.children.templateArgument && willBreak(suffix))) {
189
- suffixes.push(separator);
190
- }
191
- suffixes.push(suffix);
192
- }
193
- else if (previous) {
194
- suffixes.push(suffix);
195
- }
196
- else {
197
- prefix.push(prefixIsInstanceMethodInvocation && callExpressionCount >= 2
198
- ? indent(suffix)
199
- : suffix);
200
- }
201
- }, "primarySuffix");
202
- const hasSuffixComments = children.primarySuffix.some(suffix => hasLeadingComments(suffix));
203
- return group(canBreakForCallExpressions || hasSuffixComments
204
- ? [prefix, indent(suffixes)]
205
- : [prefix, ...suffixes]);
206
- },
207
- primaryPrefix: printSingle,
208
- primarySuffix(path, print) {
209
- const { children } = path.node;
210
- if (!children.Dot) {
211
- return printSingle(path, print);
212
- }
213
- const suffix = ["."];
214
- if (children.This) {
215
- suffix.push("this");
216
- }
217
- else if (children.Identifier) {
218
- if (children.typeArguments) {
219
- suffix.push(call(path, print, "typeArguments"));
220
- }
221
- suffix.push(call(path, print, "Identifier"));
222
- }
223
- else {
224
- const suffixKey = onlyDefinedKey(children, [
225
- "templateArgument",
226
- "unqualifiedClassInstanceCreationExpression"
227
- ]);
228
- suffix.push(call(path, print, suffixKey));
229
- }
230
- return suffix;
231
- },
232
- fqnOrRefType(path, print, _, args) {
233
- var _a;
234
- const lastSeparator = (_a = args.lastSeparator) !== null && _a !== void 0 ? _a : "";
235
- const fqnOrRefType = [
236
- call(path, print, "fqnOrRefTypePartFirst"),
237
- ...map(path, partPath => {
238
- const part = print(partPath);
239
- return partPath.isLast
240
- ? [willBreak(part) ? hardline : lastSeparator, part]
241
- : part;
242
- }, "fqnOrRefTypePartRest")
243
- ];
244
- fqnOrRefType.push(indent(fqnOrRefType.pop()));
245
- return path.node.children.dims
246
- ? [fqnOrRefType, call(path, print, "dims")]
247
- : fqnOrRefType;
248
- },
249
- fqnOrRefTypePartFirst(path, print) {
250
- return join(" ", [
251
- ...map(path, print, "annotation"),
252
- call(path, print, "fqnOrRefTypePartCommon")
253
- ]);
254
- },
255
- fqnOrRefTypePartRest(path, print) {
256
- const common = call(path, print, "fqnOrRefTypePartCommon");
257
- const type = path.node.children.typeArguments
258
- ? [call(path, print, "typeArguments"), common]
259
- : common;
260
- return [".", ...join(" ", [...map(path, print, "annotation"), type])];
261
- },
262
- fqnOrRefTypePartCommon(path, print) {
263
- const { children } = path.node;
264
- const keywordKey = onlyDefinedKey(children, ["Identifier", "Super"]);
265
- const keyword = call(path, print, keywordKey);
266
- return children.typeArguments
267
- ? [keyword, call(path, print, "typeArguments")]
268
- : keyword;
269
- },
270
- parenthesisExpression(path, print) {
271
- const expression = call(path, print, "expression");
272
- const primaryAncestor = path.getNode(4);
273
- const binaryExpressionAncestor = path.getNode(8);
274
- const outerAncestor = path.getNode(14);
275
- const { conditionalExpression, lambdaExpression } = path.node.children.expression[0].children;
276
- const hasLambda = lambdaExpression !== undefined;
277
- const hasTernary = (conditionalExpression === null || conditionalExpression === void 0 ? void 0 : conditionalExpression[0].children.QuestionMark) !== undefined;
278
- const hasSuffix = (primaryAncestor === null || primaryAncestor === void 0 ? void 0 : primaryAncestor.children.primarySuffix) !== undefined;
279
- const isAssignment = ((outerAncestor === null || outerAncestor === void 0 ? void 0 : outerAncestor.name) === "binaryExpression" &&
280
- hasAssignmentOperators(outerAncestor)) ||
281
- (outerAncestor === null || outerAncestor === void 0 ? void 0 : outerAncestor.name) === "variableInitializer";
282
- if (!hasLambda && hasSuffix && (!hasTernary || isAssignment)) {
283
- return indentInParentheses(hasTernary ? group(expression) : expression);
284
- }
285
- else if (binaryExpressionAncestor &&
286
- Object.keys(binaryExpressionAncestor.children).length === 1 &&
287
- outerAncestor &&
288
- ["guard", "returnStatement"].includes(outerAncestor.name)) {
289
- return indentInParentheses(group(expression));
290
- }
291
- else if (hasTernary && hasSuffix && !isAssignment) {
292
- return group(["(", expression, softline, ")"]);
293
- }
294
- else {
295
- return group([
296
- "(",
297
- hasLambda || hasTernary ? expression : indent(expression),
298
- ")"
299
- ]);
300
- }
301
- },
302
- castExpression: printSingle,
303
- primitiveCastExpression(path, print) {
304
- return [
305
- "(",
306
- call(path, print, "primitiveType"),
307
- ") ",
308
- call(path, print, "unaryExpression")
309
- ];
310
- },
311
- referenceTypeCastExpression(path, print) {
312
- const { children } = path.node;
313
- const type = call(path, print, "referenceType");
314
- const cast = children.additionalBound
315
- ? indentInParentheses(join(line, [type, ...map(path, print, "additionalBound")]))
316
- : ["(", type, ")"];
317
- const expressionKey = onlyDefinedKey(children, [
318
- "lambdaExpression",
319
- "unaryExpressionNotPlusMinus"
320
- ]);
321
- return [cast, " ", call(path, print, expressionKey)];
322
- },
323
- newExpression: printSingle,
324
- unqualifiedClassInstanceCreationExpression(path, print) {
325
- const { children } = path.node;
326
- const expression = ["new "];
327
- if (children.typeArguments) {
328
- expression.push(call(path, print, "typeArguments"));
329
- }
330
- expression.push(call(path, print, "classOrInterfaceTypeToInstantiate"), children.argumentList
331
- ? group(["(", call(path, print, "argumentList"), ")"])
332
- : "()");
333
- if (children.classBody) {
334
- expression.push(" ", call(path, print, "classBody"));
335
- }
336
- return expression;
337
- },
338
- classOrInterfaceTypeToInstantiate(path, print) {
339
- const { children } = path.node;
340
- const type = children.annotation
341
- ? flatMap(path, childPath => [
342
- print(childPath),
343
- isNonTerminal(childPath.node) ? " " : "."
344
- ], ["annotation", "Identifier"])
345
- : printName(path, print);
346
- if (children.typeArgumentsOrDiamond) {
347
- type.push(call(path, print, "typeArgumentsOrDiamond"));
348
- }
349
- return type;
350
- },
351
- typeArgumentsOrDiamond: printSingle,
352
- diamond() {
353
- return "<>";
354
- },
355
- methodInvocationSuffix(path, print) {
356
- return path.node.children.argumentList
357
- ? group(["(", call(path, print, "argumentList"), ")"])
358
- : indentInParentheses(printDanglingComments(path), { shouldBreak: true });
359
- },
360
- argumentList(path, print) {
361
- var _a, _b, _c, _d;
362
- const expressions = path.node.children.expression;
363
- const lastExpression = expressions.at(-1);
364
- const lastExpressionLambdaBodyExpression = (_b = (_a = lastExpression.children.lambdaExpression) === null || _a === void 0 ? void 0 : _a[0].children.lambdaBody[0].children.expression) === null || _b === void 0 ? void 0 : _b[0].children;
365
- const lastExpressionLambdaBodyTernaryExpression = (_c = lastExpressionLambdaBodyExpression === null || lastExpressionLambdaBodyExpression === void 0 ? void 0 : lastExpressionLambdaBodyExpression.conditionalExpression) === null || _c === void 0 ? void 0 : _c[0].children;
366
- const isHuggable = !lastExpression.comments &&
367
- (!lastExpressionLambdaBodyExpression ||
368
- (lastExpressionLambdaBodyTernaryExpression === null || lastExpressionLambdaBodyTernaryExpression === void 0 ? void 0 : lastExpressionLambdaBodyTernaryExpression.QuestionMark) !== undefined ||
369
- ((_d = lastExpressionLambdaBodyTernaryExpression === null || lastExpressionLambdaBodyTernaryExpression === void 0 ? void 0 : lastExpressionLambdaBodyTernaryExpression.binaryExpression) === null || _d === void 0 ? void 0 : _d[0].children.unaryExpression.length) === 1) &&
370
- expressions.findIndex(({ children }) => children.lambdaExpression) ===
371
- expressions.length - 1;
372
- const args = map(path, print, "expression");
373
- const allArgsExpandable = [
374
- indent([softline, ...join([",", line], args)]),
375
- softline
376
- ];
377
- if (!isHuggable || willBreak(args.at(-1)[0])) {
378
- return allArgsExpandable;
379
- }
380
- const headArgs = args.slice(0, -1);
381
- const huggedLastArg = path.call(argPath => print(argPath, { hug: true }), "children", "expression", args.length - 1);
382
- const lastArgExpanded = join(", ", [
383
- ...headArgs,
384
- group(huggedLastArg, { shouldBreak: true })
385
- ]);
386
- if (willBreak(huggedLastArg)) {
387
- return [
388
- breakParent,
389
- conditionalGroup([lastArgExpanded, allArgsExpandable])
390
- ];
391
- }
392
- return conditionalGroup([
393
- join(", ", [...headArgs, huggedLastArg]),
394
- lastArgExpanded,
395
- allArgsExpandable
396
- ]);
397
- },
398
- arrayCreationExpression(path, print) {
399
- const { children } = path.node;
400
- const typeKey = onlyDefinedKey(children, [
401
- "classOrInterfaceType",
402
- "primitiveType"
403
- ]);
404
- const suffixKey = onlyDefinedKey(children, [
405
- "arrayCreationExpressionWithoutInitializerSuffix",
406
- "arrayCreationWithInitializerSuffix"
407
- ]);
408
- return ["new ", call(path, print, typeKey), call(path, print, suffixKey)];
409
- },
410
- arrayCreationExpressionWithoutInitializerSuffix(path, print) {
411
- const expressions = call(path, print, "dimExprs");
412
- return path.node.children.dims
413
- ? [expressions, call(path, print, "dims")]
414
- : expressions;
415
- },
416
- arrayCreationWithInitializerSuffix(path, print) {
417
- return [
418
- call(path, print, "dims"),
419
- " ",
420
- call(path, print, "arrayInitializer")
421
- ];
422
- },
423
- dimExprs(path, print) {
424
- return map(path, print, "dimExpr");
425
- },
426
- dimExpr(path, print) {
427
- return join(" ", [
428
- ...map(path, print, "annotation"),
429
- ["[", call(path, print, "expression"), "]"]
430
- ]);
431
- },
432
- classLiteralSuffix(path, print) {
433
- const lSquares = map(path, print, "LSquare");
434
- const rSquares = map(path, print, "RSquare");
435
- return [
436
- ...lSquares.flatMap((lSquare, index) => [lSquare, rSquares[index]]),
437
- ".class"
438
- ];
439
- },
440
- arrayAccessSuffix(path, print) {
441
- return ["[", call(path, print, "expression"), "]"];
442
- },
443
- methodReferenceSuffix(path, print) {
444
- const { children } = path.node;
445
- const reference = ["::"];
446
- if (children.typeArguments) {
447
- reference.push(call(path, print, "typeArguments"));
448
- }
449
- reference.push(call(path, print, onlyDefinedKey(children, ["Identifier", "New"])));
450
- return reference;
451
- },
452
- templateArgument: printSingle,
453
- template: printSingle,
454
- stringTemplate(path, print) {
455
- return printTemplate(path, print, "StringTemplateBegin", "StringTemplateMid", "StringTemplateEnd");
456
- },
457
- textBlockTemplate(path, print) {
458
- return printTemplate(path, print, "TextBlockTemplateBegin", "TextBlockTemplateMid", "TextBlockTemplateEnd");
459
- },
460
- embeddedExpression: printSingle,
461
- pattern: printSingle,
462
- typePattern: printSingle,
463
- recordPattern(path, print) {
464
- const patterns = path.node.children.componentPatternList
465
- ? indentInParentheses(call(path, print, "componentPatternList"))
466
- : "()";
467
- return [call(path, print, "referenceType"), patterns];
468
- },
469
- componentPatternList(path, print) {
470
- return printList(path, print, "componentPattern");
471
- },
472
- componentPattern: printSingle,
473
- matchAllPattern: printSingle,
474
- guard(path, print) {
475
- var _a;
476
- const expression = call(path, print, "expression");
477
- const hasParentheses = ((_a = path.node.children.expression[0].children.conditionalExpression) === null || _a === void 0 ? void 0 : _a[0].children.binaryExpression[0].children.unaryExpression[0].children.primary[0].children.primaryPrefix[0].children.parenthesisExpression) !==
478
- undefined;
479
- return [
480
- "when ",
481
- hasParentheses
482
- ? expression
483
- : group([
484
- ifBreak("("),
485
- indent([softline, expression]),
486
- softline,
487
- ifBreak(")")
488
- ])
489
- ];
490
- }
491
- };
492
- function binary(operands, operators, { hasNonAssignmentOperators = false, isInList = false, isRoot = false, operatorPosition }) {
493
- let levelOperator;
494
- let levelPrecedence;
495
- let level = [];
496
- while (operators.length) {
497
- const nextOperator = operators[0].image;
498
- const nextPrecedence = getOperatorPrecedence(nextOperator);
499
- if (levelPrecedence === undefined || nextPrecedence === levelPrecedence) {
500
- const { image: operator, doc: operatorDoc } = operators.shift();
501
- level.push(operands.shift());
502
- if (levelOperator !== undefined &&
503
- needsParentheses(levelOperator, operator)) {
504
- level = [["(", group(indent(level)), ")"]];
505
- }
506
- const parts = [" ", operatorDoc, line];
507
- if (operatorPosition === "start" && !isAssignmentOperator(operator)) {
508
- parts.reverse();
509
- }
510
- level.push(parts);
511
- levelOperator = operator;
512
- levelPrecedence = nextPrecedence;
513
- }
514
- else if (nextPrecedence < levelPrecedence) {
515
- if (!isRoot) {
516
- break;
517
- }
518
- level.push(operands.shift());
519
- const content = group(indent(level));
520
- operands.unshift(levelOperator !== undefined &&
521
- needsParentheses(levelOperator, nextOperator)
522
- ? ["(", content, ")"]
523
- : content);
524
- level = [];
525
- levelOperator = undefined;
526
- levelPrecedence = undefined;
527
- }
528
- else {
529
- const content = binary(operands, operators, { operatorPosition });
530
- operands.unshift(levelOperator !== undefined &&
531
- needsParentheses(nextOperator, levelOperator)
532
- ? ["(", group(indent(content)), ")"]
533
- : group(content));
534
- }
535
- }
536
- level.push(operands.shift());
537
- if (!levelOperator ||
538
- (!isInList &&
539
- !isAssignmentOperator(levelOperator) &&
540
- levelOperator !== "instanceof")) {
541
- return level;
542
- }
543
- if (!isRoot || hasNonAssignmentOperators) {
544
- return indent(level);
545
- }
546
- const groupId = Symbol("assignment");
547
- return [
548
- level[0],
549
- group(indent(level[1]), { id: groupId }),
550
- indentIfBreak(level[2], { groupId })
551
- ];
552
- }
553
- const precedencesByOperator = new Map([
554
- ["||"],
555
- ["&&"],
556
- ["|"],
557
- ["^"],
558
- ["&"],
559
- ["==", "!="],
560
- ["<", ">", "<=", ">=", "instanceof"],
561
- ["<<", ">>", ">>>"],
562
- ["+", "-"],
563
- ["*", "/", "%"]
564
- ].flatMap((operators, index) => operators.map(operator => [operator, index])));
565
- function getOperatorPrecedence(operator) {
566
- var _a;
567
- return (_a = precedencesByOperator.get(operator)) !== null && _a !== void 0 ? _a : -1;
568
- }
569
- function needsParentheses(operator, parentOperator) {
570
- return ((operator === "&&" && parentOperator === "||") ||
571
- (["|", "^", "&", "<<", ">>", ">>>"].includes(parentOperator) &&
572
- getOperatorPrecedence(operator) >
573
- getOperatorPrecedence(parentOperator)) ||
574
- [operator, parentOperator].every(o => ["==", "!="].includes(o)) ||
575
- [operator, parentOperator].every(o => ["<<", ">>", ">>>"].includes(o)) ||
576
- (operator === "*" && parentOperator === "/") ||
577
- (operator === "/" && parentOperator === "*") ||
578
- (operator === "%" && ["+", "-", "*", "/"].includes(parentOperator)) ||
579
- (["*", "/"].includes(operator) && parentOperator === "%"));
580
- }
581
- const assignmentOperators = new Set([
582
- "=",
583
- "*=",
584
- "/=",
585
- "%=",
586
- "+=",
587
- "-=",
588
- "<<=",
589
- ">>=",
590
- ">>>=",
591
- "&=",
592
- "^=",
593
- "|="
594
- ]);
595
- function isAssignmentOperator(operator) {
596
- return assignmentOperators.has(operator);
597
- }
598
- function isCapitalizedIdentifier(fqnOrRefType) {
599
- var _a, _b, _c;
600
- const nextToLastIdentifier = (_c = (_b = [
601
- fqnOrRefType.fqnOrRefTypePartFirst[0],
602
- ...((_a = fqnOrRefType.fqnOrRefTypePartRest) !== null && _a !== void 0 ? _a : [])
603
- ].at(-2)) === null || _b === void 0 ? void 0 : _b.children.fqnOrRefTypePartCommon[0].children.Identifier) === null || _c === void 0 ? void 0 : _c[0].image;
604
- return /^\p{Uppercase_Letter}/u.test(nextToLastIdentifier !== null && nextToLastIdentifier !== void 0 ? nextToLastIdentifier : "");
605
- }
606
- function printTemplate(path, print, beginKey, midKey, endKey) {
607
- const begin = call(path, ({ node }) => node.image, beginKey);
608
- const mids = map(path, ({ node }) => node.image, midKey);
609
- const end = call(path, ({ node }) => node.image, endKey);
610
- const lines = [begin, ...mids, end].join("").split("\n").slice(1);
611
- const baseIndent = findBaseIndent(lines);
612
- const prefix = "\n" + " ".repeat(baseIndent);
613
- const parts = [begin, ...mids, end].map(image => join(hardline, image.split(prefix)));
614
- return indent([
615
- parts[0],
616
- ...map(path, (expressionPath, index) => {
617
- const expression = group([
618
- indent([softline, print(expressionPath), lineSuffixBoundary]),
619
- softline
620
- ]);
621
- return index === 0 ? expression : [parts[index], expression];
622
- }, "embeddedExpression"),
623
- parts.at(-1)
624
- ]);
625
- }