prettier-plugin-java 2.8.1 → 2.9.1

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