prettier-java-next-line 2.10.3
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.
- package/LICENSE +204 -0
- package/NOTICE +5 -0
- package/README.md +100 -0
- package/dist/index.cjs +2558 -0
- package/dist/index.d.cts +1639 -0
- package/dist/index.d.mts +1640 -0
- package/dist/index.mjs +2558 -0
- package/dist/tree-sitter-java_orchard.wasm +0 -0
- package/package.json +89 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,2558 @@
|
|
|
1
|
+
let web_tree_sitter = require("web-tree-sitter");
|
|
2
|
+
let prettier = require("prettier");
|
|
3
|
+
let prettier_doc = require("prettier/doc");
|
|
4
|
+
//#region src/options.ts
|
|
5
|
+
var options_default = {
|
|
6
|
+
arrowParens: {
|
|
7
|
+
type: "choice",
|
|
8
|
+
category: "Java",
|
|
9
|
+
default: "always",
|
|
10
|
+
choices: [{
|
|
11
|
+
value: "always",
|
|
12
|
+
description: ""
|
|
13
|
+
}, {
|
|
14
|
+
value: "avoid",
|
|
15
|
+
description: ""
|
|
16
|
+
}],
|
|
17
|
+
description: "Include parentheses around a sole arrow function parameter."
|
|
18
|
+
},
|
|
19
|
+
trailingComma: {
|
|
20
|
+
type: "choice",
|
|
21
|
+
category: "Java",
|
|
22
|
+
default: "all",
|
|
23
|
+
choices: [
|
|
24
|
+
{
|
|
25
|
+
value: "all",
|
|
26
|
+
description: ""
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
value: "es5",
|
|
30
|
+
description: ""
|
|
31
|
+
},
|
|
32
|
+
{
|
|
33
|
+
value: "none",
|
|
34
|
+
description: ""
|
|
35
|
+
}
|
|
36
|
+
],
|
|
37
|
+
description: "Print trailing commas wherever possible when multi-line."
|
|
38
|
+
},
|
|
39
|
+
experimentalOperatorPosition: {
|
|
40
|
+
type: "choice",
|
|
41
|
+
category: "Java",
|
|
42
|
+
default: "end",
|
|
43
|
+
choices: [{
|
|
44
|
+
value: "start",
|
|
45
|
+
description: ""
|
|
46
|
+
}, {
|
|
47
|
+
value: "end",
|
|
48
|
+
description: ""
|
|
49
|
+
}],
|
|
50
|
+
description: "Where to print operators when binary expressions wrap lines."
|
|
51
|
+
},
|
|
52
|
+
braceStyle: {
|
|
53
|
+
type: "choice",
|
|
54
|
+
category: "Java",
|
|
55
|
+
default: "next-line",
|
|
56
|
+
choices: [{
|
|
57
|
+
value: "same-line",
|
|
58
|
+
description: "Put opening braces on same line (K&R style)"
|
|
59
|
+
}, {
|
|
60
|
+
value: "next-line",
|
|
61
|
+
description: "Put opening braces on new lines (Allman style)"
|
|
62
|
+
}],
|
|
63
|
+
description: "Placement of opening braces."
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
//#endregion
|
|
67
|
+
//#region src/node-types.ts
|
|
68
|
+
const multiFieldsByType = {
|
|
69
|
+
array_creation_expression: { dimensions: true },
|
|
70
|
+
cast_expression: { type: true },
|
|
71
|
+
constant_declaration: { declarator: true },
|
|
72
|
+
exports_module_directive: { modules: true },
|
|
73
|
+
field_declaration: { declarator: true },
|
|
74
|
+
for_statement: {
|
|
75
|
+
init: true,
|
|
76
|
+
update: true
|
|
77
|
+
},
|
|
78
|
+
local_variable_declaration: { declarator: true },
|
|
79
|
+
opens_module_directive: { modules: true },
|
|
80
|
+
provides_module_directive: { provider: true },
|
|
81
|
+
requires_module_directive: { modifiers: true },
|
|
82
|
+
spread_parameter: { annotations: true }
|
|
83
|
+
};
|
|
84
|
+
//#endregion
|
|
85
|
+
//#region src/printers/helpers.ts
|
|
86
|
+
const { group: group$7, hardline: hardline$7, ifBreak: ifBreak$2, indent: indent$7, indentIfBreak: indentIfBreak$2, join: join$7, line: line$7, lineSuffixBoundary: lineSuffixBoundary$1, softline: softline$4 } = prettier_doc.builders;
|
|
87
|
+
const { mapDoc } = prettier_doc.utils;
|
|
88
|
+
function hasType(path, type) {
|
|
89
|
+
return path.node.type === type;
|
|
90
|
+
}
|
|
91
|
+
function hasChild(path, fieldName) {
|
|
92
|
+
return path.node[fieldName] != null;
|
|
93
|
+
}
|
|
94
|
+
function definedKeys(obj, options) {
|
|
95
|
+
return (options ?? Object.keys(obj)).filter((key) => obj[key] !== void 0);
|
|
96
|
+
}
|
|
97
|
+
function printModifiers(path, print, annotationMode) {
|
|
98
|
+
const modifiersIndex = path.node.namedChildren.findIndex(({ type }) => type === "modifiers");
|
|
99
|
+
if (modifiersIndex === -1) return [];
|
|
100
|
+
const separator = annotationMode === "avoidBreak" ? line$7 : annotationMode === "noBreak" || path.node.namedChildren[modifiersIndex].children.some(({ type }) => type !== "annotation" && type !== "marker_annotation") ? " " : hardline$7;
|
|
101
|
+
return [path.call((modifiers) => print(modifiers, { annotationMode }), "namedChildren", modifiersIndex), separator];
|
|
102
|
+
}
|
|
103
|
+
function printValue(path) {
|
|
104
|
+
return path.node.value;
|
|
105
|
+
}
|
|
106
|
+
function lineStartWithComments(node) {
|
|
107
|
+
return node.comments?.length ? Math.min(node.start.row, node.comments[0].start.row) : node.start.row;
|
|
108
|
+
}
|
|
109
|
+
function lineEndWithComments(node) {
|
|
110
|
+
return node.comments?.length ? Math.max(node.end.row, node.comments.at(-1).end.row) : node.end.row;
|
|
111
|
+
}
|
|
112
|
+
function printDanglingComments(path, danglingCommentsPrintOptions = {}) {
|
|
113
|
+
const { indent: shouldIndent = false } = danglingCommentsPrintOptions;
|
|
114
|
+
const danglingComments = new Set(path.node.comments?.filter((comment) => !(comment.leading || comment.trailing)));
|
|
115
|
+
if (danglingComments.size === 0) return "";
|
|
116
|
+
const parts = path.map(({ node: comment }) => danglingComments.has(comment) ? printComment(comment) : "", "comments").filter(Boolean);
|
|
117
|
+
const doc = join$7(hardline$7, parts);
|
|
118
|
+
return shouldIndent ? indent$7([hardline$7, doc]) : doc;
|
|
119
|
+
}
|
|
120
|
+
function printComment(comment) {
|
|
121
|
+
comment.printed = true;
|
|
122
|
+
const lines = comment.value.split("\n").map((line) => line.trim());
|
|
123
|
+
return lines.length > 1 && lines[0].startsWith("/*") && lines.slice(1).every((line) => line.startsWith("*")) && lines.at(-1).endsWith("*/") ? join$7(hardline$7, lines.map((line, index) => index === 0 ? line : ` ${line}`)) : comment.value;
|
|
124
|
+
}
|
|
125
|
+
function hasLeadingComments(node) {
|
|
126
|
+
return node.comments?.some(({ leading }) => leading) ?? false;
|
|
127
|
+
}
|
|
128
|
+
function indentInParentheses(contents) {
|
|
129
|
+
return contents && !Array.isArray(contents) || contents.length ? [
|
|
130
|
+
"(",
|
|
131
|
+
indent$7([softline$4, contents]),
|
|
132
|
+
softline$4,
|
|
133
|
+
")"
|
|
134
|
+
] : "()";
|
|
135
|
+
}
|
|
136
|
+
function printArrayInitializer(path, print, options) {
|
|
137
|
+
if (!path.node.namedChildren.length) {
|
|
138
|
+
const danglingComments = printDanglingComments(path, { indent: true });
|
|
139
|
+
if (!danglingComments) return "{}";
|
|
140
|
+
const block = [
|
|
141
|
+
"{",
|
|
142
|
+
danglingComments,
|
|
143
|
+
hardline$7,
|
|
144
|
+
"}"
|
|
145
|
+
];
|
|
146
|
+
return options.braceStyle === "next-line" && !isNestedArray(path) ? [hardline$7, ...block] : block;
|
|
147
|
+
}
|
|
148
|
+
const list = join$7([",", line$7], path.map(print, "namedChildren"));
|
|
149
|
+
if (list.length && options.trailingComma !== "none") list.push(ifBreak$2(","));
|
|
150
|
+
if (options.braceStyle === "next-line" && path.node.type === "array_initializer") {
|
|
151
|
+
const block = [
|
|
152
|
+
"{",
|
|
153
|
+
indent$7([line$7, ...list]),
|
|
154
|
+
line$7,
|
|
155
|
+
"}"
|
|
156
|
+
];
|
|
157
|
+
return isNestedArray(path) ? block : [hardline$7, ...block];
|
|
158
|
+
}
|
|
159
|
+
return group$7([
|
|
160
|
+
"{",
|
|
161
|
+
indent$7([line$7, ...list]),
|
|
162
|
+
line$7,
|
|
163
|
+
"}"
|
|
164
|
+
]);
|
|
165
|
+
}
|
|
166
|
+
function isNestedArray(path) {
|
|
167
|
+
return path.parent?.type === "array_initializer" || path.parent?.type === "element_value_array_initializer";
|
|
168
|
+
}
|
|
169
|
+
function printBlock(path, contents, options) {
|
|
170
|
+
if (options?.braceStyle === "next-line") return printBlockNextLine(path, contents);
|
|
171
|
+
if (contents.length) return group$7([
|
|
172
|
+
"{",
|
|
173
|
+
indent$7([hardline$7, ...join$7(hardline$7, contents)]),
|
|
174
|
+
hardline$7,
|
|
175
|
+
"}"
|
|
176
|
+
]);
|
|
177
|
+
const danglingComments = printDanglingComments(path, { indent: true });
|
|
178
|
+
if (danglingComments) return [
|
|
179
|
+
"{",
|
|
180
|
+
danglingComments,
|
|
181
|
+
hardline$7,
|
|
182
|
+
"}"
|
|
183
|
+
];
|
|
184
|
+
const parent = path.parent;
|
|
185
|
+
const grandparent = path.grandparent;
|
|
186
|
+
return parent?.type === "catch_clause" && (grandparent?.type === "try_statement" || grandparent?.type === "try_with_resources_statement") && grandparent.namedChildren.filter(({ type }) => type === "catch_clause").length === 1 && !grandparent.namedChildren.some(({ type }) => type === "finally_clause") || parent && [
|
|
187
|
+
"for_statement",
|
|
188
|
+
"do_statement",
|
|
189
|
+
"enhanced_for_statement",
|
|
190
|
+
"while_statement"
|
|
191
|
+
].includes(parent.type) || [
|
|
192
|
+
"annotation_type_body",
|
|
193
|
+
"class_body",
|
|
194
|
+
"constructor_body",
|
|
195
|
+
"enum_body",
|
|
196
|
+
"interface_body",
|
|
197
|
+
"module_body",
|
|
198
|
+
"record_pattern_body"
|
|
199
|
+
].includes(path.node.type) || parent && [
|
|
200
|
+
"block",
|
|
201
|
+
"lambda_expression",
|
|
202
|
+
"method_declaration",
|
|
203
|
+
"static_initializer",
|
|
204
|
+
"synchronized_statement"
|
|
205
|
+
].includes(parent.type) ? "{}" : [
|
|
206
|
+
"{",
|
|
207
|
+
hardline$7,
|
|
208
|
+
"}"
|
|
209
|
+
];
|
|
210
|
+
}
|
|
211
|
+
function printBlockNextLine(path, contents) {
|
|
212
|
+
const parentType = path.parent?.type;
|
|
213
|
+
const prefix = parentType != null && [
|
|
214
|
+
"annotation_type_body",
|
|
215
|
+
"class_body",
|
|
216
|
+
"enum_body",
|
|
217
|
+
"interface_body"
|
|
218
|
+
].includes(parentType) ? [] : [hardline$7];
|
|
219
|
+
if (contents.length) return [
|
|
220
|
+
...prefix,
|
|
221
|
+
"{",
|
|
222
|
+
indent$7([hardline$7, ...join$7(hardline$7, contents)]),
|
|
223
|
+
hardline$7,
|
|
224
|
+
"}"
|
|
225
|
+
];
|
|
226
|
+
const danglingComments = printDanglingComments(path, { indent: true });
|
|
227
|
+
if (danglingComments) return [
|
|
228
|
+
...prefix,
|
|
229
|
+
"{",
|
|
230
|
+
danglingComments,
|
|
231
|
+
hardline$7,
|
|
232
|
+
"}"
|
|
233
|
+
];
|
|
234
|
+
return [
|
|
235
|
+
...prefix,
|
|
236
|
+
"{",
|
|
237
|
+
hardline$7,
|
|
238
|
+
"}"
|
|
239
|
+
];
|
|
240
|
+
}
|
|
241
|
+
function printBlockStatements(path, print) {
|
|
242
|
+
const parts = [];
|
|
243
|
+
path.each((child) => {
|
|
244
|
+
const { node, previous } = child;
|
|
245
|
+
if (node.type === "switch_label") return;
|
|
246
|
+
const blankLine = parts.length && previous && lineStartWithComments(node) > lineEndWithComments(previous) + 1;
|
|
247
|
+
const declaration = print(child);
|
|
248
|
+
parts.push(blankLine ? [hardline$7, declaration] : declaration);
|
|
249
|
+
}, "namedChildren");
|
|
250
|
+
return parts;
|
|
251
|
+
}
|
|
252
|
+
function printBodyDeclarations(path, print, padFirst = false) {
|
|
253
|
+
const isInterfaceBody = path.node.type === "interface_body";
|
|
254
|
+
const isFormalParameters = path.node.type === "formal_parameters";
|
|
255
|
+
const separator = isFormalParameters ? softline$4 : hardline$7;
|
|
256
|
+
let previousRequiresPadding = padFirst;
|
|
257
|
+
return path.map((child) => {
|
|
258
|
+
const { node, previous } = child;
|
|
259
|
+
const modifiers = node.namedChildren.find(({ type }) => type === "modifiers")?.children ?? [];
|
|
260
|
+
const firstAnnotationIndex = modifiers.findIndex(({ type }) => type === "annotation" || type === "marker_annotation");
|
|
261
|
+
const lastNonAnnotationIndex = modifiers.findLastIndex(({ type }) => type !== "annotation" && type !== "marker_annotation");
|
|
262
|
+
const currentRequiresPadding = firstAnnotationIndex !== -1 && (!isFormalParameters && lastNonAnnotationIndex === -1 || firstAnnotationIndex < lastNonAnnotationIndex) || !isFormalParameters && node.type !== "constant_declaration" && node.type !== "enum_constant" && node.type !== "field_declaration" && !(isInterfaceBody && node.type === "method_declaration" && !node.bodyNode);
|
|
263
|
+
const blankLine = previousRequiresPadding || previous && (currentRequiresPadding || lineStartWithComments(node) > lineEndWithComments(previous) + 1);
|
|
264
|
+
previousRequiresPadding = currentRequiresPadding;
|
|
265
|
+
const declaration = print(child);
|
|
266
|
+
return blankLine ? [separator, declaration] : declaration;
|
|
267
|
+
}, "namedChildren");
|
|
268
|
+
}
|
|
269
|
+
function printTypeParameters(path, print) {
|
|
270
|
+
const parameters = path.node.namedChildren;
|
|
271
|
+
if (parameters.length === 0 || parameters.length === 1 && isSimpleType(parameters[0]) && !parameters.some(({ comments }) => comments?.length && comments.some(({ type }) => type === "line_comment"))) return [
|
|
272
|
+
"<",
|
|
273
|
+
join$7(", ", path.map(print, "namedChildren")),
|
|
274
|
+
">"
|
|
275
|
+
];
|
|
276
|
+
const parts = [
|
|
277
|
+
"<",
|
|
278
|
+
indent$7([softline$4, join$7([",", line$7], path.map(print, "namedChildren"))]),
|
|
279
|
+
softline$4,
|
|
280
|
+
">"
|
|
281
|
+
];
|
|
282
|
+
return path.node.type === "type_arguments" ? group$7(parts) : parts;
|
|
283
|
+
}
|
|
284
|
+
function printVariableDeclaration(path, print) {
|
|
285
|
+
const declaration = printModifiers(path, print);
|
|
286
|
+
declaration.push(path.call(print, "typeNode"), " ");
|
|
287
|
+
const declarators = path.map(print, "declaratorNodes");
|
|
288
|
+
if (declarators.length > 1 && path.node.declaratorNodes.some(({ valueNode }) => valueNode)) declaration.push(group$7(indent$7(join$7([",", line$7], declarators)), { shouldBreak: path.parent?.type !== "for_statement" }));
|
|
289
|
+
else declaration.push(join$7(", ", declarators));
|
|
290
|
+
declaration.push(";");
|
|
291
|
+
return declaration;
|
|
292
|
+
}
|
|
293
|
+
function printAssignment(leftDoc, operator, rightDoc, rightNode) {
|
|
294
|
+
if (!rightDoc || !rightNode) return leftDoc;
|
|
295
|
+
if (rightNode.type === "binary_expression" || rightNode.type === "instanceof_expression" || rightNode.type === "ternary_expression" && (rightNode.conditionNode.type === "binary_expression" || rightNode.conditionNode.type === "instanceof_expression") || hasLeadingComments(rightNode)) return group$7([
|
|
296
|
+
leftDoc,
|
|
297
|
+
operator,
|
|
298
|
+
group$7(indent$7([line$7, rightDoc]))
|
|
299
|
+
]);
|
|
300
|
+
const groupId = Symbol("assignment");
|
|
301
|
+
return group$7([
|
|
302
|
+
leftDoc,
|
|
303
|
+
operator,
|
|
304
|
+
group$7(indent$7(line$7), { id: groupId }),
|
|
305
|
+
lineSuffixBoundary$1,
|
|
306
|
+
indentIfBreak$2(rightDoc, { groupId })
|
|
307
|
+
]);
|
|
308
|
+
}
|
|
309
|
+
function printTextBlock(contents) {
|
|
310
|
+
return [
|
|
311
|
+
"\"\"\"",
|
|
312
|
+
hardline$7,
|
|
313
|
+
contents,
|
|
314
|
+
"\"\"\""
|
|
315
|
+
];
|
|
316
|
+
}
|
|
317
|
+
function embedTextBlock(path) {
|
|
318
|
+
if (path.node.namedChildren.some(({ type }) => type === "string_interpolation") || path.node.children[0].value === "\"") return null;
|
|
319
|
+
const language = findEmbeddedLanguage(path);
|
|
320
|
+
if (!language) return null;
|
|
321
|
+
const text = unescapeTextBlockContents(textBlockContents(path.node));
|
|
322
|
+
return async (textToDoc) => {
|
|
323
|
+
return printTextBlock([escapeDocForTextBlock(await textToDoc(text, { parser: language })), hardline$7]);
|
|
324
|
+
};
|
|
325
|
+
}
|
|
326
|
+
function textBlockContents(node) {
|
|
327
|
+
const lines = node.value.split("\n").slice(1);
|
|
328
|
+
const baseIndent = findBaseIndent(lines);
|
|
329
|
+
return lines.map((line) => line.slice(baseIndent)).join("\n").slice(0, -3);
|
|
330
|
+
}
|
|
331
|
+
const PRECEDENCE = new Map([
|
|
332
|
+
["||"],
|
|
333
|
+
["&&"],
|
|
334
|
+
["|"],
|
|
335
|
+
["^"],
|
|
336
|
+
["&"],
|
|
337
|
+
["==", "!="],
|
|
338
|
+
[
|
|
339
|
+
"<",
|
|
340
|
+
">",
|
|
341
|
+
"<=",
|
|
342
|
+
">=",
|
|
343
|
+
"instanceof"
|
|
344
|
+
],
|
|
345
|
+
[
|
|
346
|
+
"<<",
|
|
347
|
+
">>",
|
|
348
|
+
">>>"
|
|
349
|
+
],
|
|
350
|
+
["+", "-"],
|
|
351
|
+
[
|
|
352
|
+
"*",
|
|
353
|
+
"/",
|
|
354
|
+
"%"
|
|
355
|
+
]
|
|
356
|
+
].flatMap((operators, index) => operators.map((operator) => [operator, index])));
|
|
357
|
+
function getPrecedence(operator) {
|
|
358
|
+
return PRECEDENCE.get(operator) ?? -1;
|
|
359
|
+
}
|
|
360
|
+
const equalityOperators = /* @__PURE__ */ new Set(["==", "!="]);
|
|
361
|
+
const multiplicativeOperators = /* @__PURE__ */ new Set([
|
|
362
|
+
"*",
|
|
363
|
+
"/",
|
|
364
|
+
"%"
|
|
365
|
+
]);
|
|
366
|
+
const bitshiftOperators = /* @__PURE__ */ new Set([
|
|
367
|
+
">>",
|
|
368
|
+
">>>",
|
|
369
|
+
"<<"
|
|
370
|
+
]);
|
|
371
|
+
function isBitwiseOperator(operator) {
|
|
372
|
+
return bitshiftOperators.has(operator) || operator === "|" || operator === "^" || operator === "&";
|
|
373
|
+
}
|
|
374
|
+
function shouldFlatten(parentOp, nodeOp) {
|
|
375
|
+
if (getPrecedence(nodeOp) !== getPrecedence(parentOp)) return false;
|
|
376
|
+
if (equalityOperators.has(parentOp) && equalityOperators.has(nodeOp)) return false;
|
|
377
|
+
if (nodeOp === "%" && multiplicativeOperators.has(parentOp) || parentOp === "%" && multiplicativeOperators.has(nodeOp)) return false;
|
|
378
|
+
if (nodeOp !== parentOp && multiplicativeOperators.has(nodeOp) && multiplicativeOperators.has(parentOp)) return false;
|
|
379
|
+
if (bitshiftOperators.has(parentOp) && bitshiftOperators.has(nodeOp)) return false;
|
|
380
|
+
return true;
|
|
381
|
+
}
|
|
382
|
+
function createTypeCheckFunction(typesArray) {
|
|
383
|
+
const types = new Set(typesArray);
|
|
384
|
+
return (node) => node != null && types.has(node.type);
|
|
385
|
+
}
|
|
386
|
+
const isMember = createTypeCheckFunction([
|
|
387
|
+
"array_access",
|
|
388
|
+
"field_access",
|
|
389
|
+
"method_invocation"
|
|
390
|
+
]);
|
|
391
|
+
function needsParentheses(path) {
|
|
392
|
+
if (path.isRoot) return false;
|
|
393
|
+
const { node, parent } = path;
|
|
394
|
+
const parentCheckResult = parentNeedsParentheses(path);
|
|
395
|
+
if (typeof parentCheckResult === "boolean") return parentCheckResult;
|
|
396
|
+
switch (node.type) {
|
|
397
|
+
case "switch_expression": return isMember(parent) || parent?.type === "explicit_constructor_invocation" || parent?.type === "method_reference" || parent?.type === "object_creation_expression";
|
|
398
|
+
case "update_expression": if (parent?.type === "unary_expression") return node.children[0].type.startsWith(parent.operatorNode.type);
|
|
399
|
+
case "unary_expression": switch (parent?.type) {
|
|
400
|
+
case "unary_expression": return node.type === "unary_expression" && node.operatorNode.type === parent.operatorNode.type && (node.operatorNode.type === "+" || node.operatorNode.type === "-");
|
|
401
|
+
case "instanceof_expression": return parent.leftNode === node && node.type === "unary_expression";
|
|
402
|
+
default: return false;
|
|
403
|
+
}
|
|
404
|
+
case "binary_expression":
|
|
405
|
+
case "instanceof_expression": if (parent?.type === "update_expression") return true;
|
|
406
|
+
case "cast_expression":
|
|
407
|
+
switch (parent?.type) {
|
|
408
|
+
case "cast_expression": return node.type !== "cast_expression";
|
|
409
|
+
case "method_reference":
|
|
410
|
+
case "object_creation_expression":
|
|
411
|
+
case "update_expression": return true;
|
|
412
|
+
case "unary_expression":
|
|
413
|
+
if (!node.comments) return true;
|
|
414
|
+
break;
|
|
415
|
+
case "explicit_constructor_invocation":
|
|
416
|
+
case "field_access":
|
|
417
|
+
case "method_invocation": return parent.objectNode === node;
|
|
418
|
+
case "array_access": return parent.arrayNode === node;
|
|
419
|
+
case "binary_expression":
|
|
420
|
+
case "instanceof_expression": {
|
|
421
|
+
if (node.type === "cast_expression") return false;
|
|
422
|
+
if (parent.type === "binary_expression" && isLogicalOperator(parent.operatorNode) && node.type === "binary_expression" && isLogicalOperator(node.operatorNode)) return parent.operatorNode.type !== node.operatorNode.type;
|
|
423
|
+
const operator = node.type === "instanceof_expression" ? "instanceof" : node.operatorNode.type;
|
|
424
|
+
const precedence = getPrecedence(operator);
|
|
425
|
+
const parentOperator = parent.type === "instanceof_expression" ? "instanceof" : parent.operatorNode.type;
|
|
426
|
+
const parentPrecedence = getPrecedence(parentOperator);
|
|
427
|
+
if (parentPrecedence > precedence) return true;
|
|
428
|
+
if (parent.rightNode === node && parentPrecedence === precedence) return true;
|
|
429
|
+
if (parentPrecedence === precedence && !shouldFlatten(parentOperator, operator)) return true;
|
|
430
|
+
if (parentPrecedence < precedence && operator === "%" && (parentOperator === "+" || parentOperator === "-")) return true;
|
|
431
|
+
if (isBitwiseOperator(parentOperator)) return true;
|
|
432
|
+
return false;
|
|
433
|
+
}
|
|
434
|
+
default: return false;
|
|
435
|
+
}
|
|
436
|
+
break;
|
|
437
|
+
case "assignment_expression":
|
|
438
|
+
if (parent?.type === "for_statement" && (parent.initNodes.includes(node) || parent.updateNodes.includes(node))) return false;
|
|
439
|
+
if (parent?.type === "expression_statement" && parent.namedChildren[0] === node) return false;
|
|
440
|
+
if (parent?.type === "assignment_expression" || parent?.type === "lambda_expression" && parent.bodyNode === node) return false;
|
|
441
|
+
return true;
|
|
442
|
+
case "ternary_expression": switch (parent?.type) {
|
|
443
|
+
case "unary_expression":
|
|
444
|
+
case "binary_expression":
|
|
445
|
+
case "cast_expression":
|
|
446
|
+
case "method_reference":
|
|
447
|
+
case "object_creation_expression": return true;
|
|
448
|
+
case "ternary_expression": return parent.conditionNode === node;
|
|
449
|
+
case "explicit_constructor_invocation":
|
|
450
|
+
case "field_access":
|
|
451
|
+
case "method_invocation": return parent.objectNode === node;
|
|
452
|
+
case "array_access": return parent.arrayNode === node;
|
|
453
|
+
default: return false;
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
return false;
|
|
457
|
+
}
|
|
458
|
+
function returnArgumentHasLeadingComment(node) {
|
|
459
|
+
return node.comments?.some((comment) => comment.leading && (comment.type === "line_comment" || comment.start < comment.end));
|
|
460
|
+
}
|
|
461
|
+
const isReturnOrThrowStatement = createTypeCheckFunction(["return_statement", "throw_statement"]);
|
|
462
|
+
function parentNeedsParentheses(path) {
|
|
463
|
+
const { parent } = path;
|
|
464
|
+
switch (parent?.type) {
|
|
465
|
+
case "return_statement":
|
|
466
|
+
case "throw_statement":
|
|
467
|
+
if (willReturnOrThrowStatementBreak(path)) return false;
|
|
468
|
+
break;
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
function willReturnOrThrowStatementBreak(path) {
|
|
472
|
+
const { parent } = path;
|
|
473
|
+
if (!isReturnOrThrowStatement(parent)) return false;
|
|
474
|
+
const { node } = path;
|
|
475
|
+
if (node.type === "assignment_expression" && returnArgumentHasLeadingComment(node)) return true;
|
|
476
|
+
return false;
|
|
477
|
+
}
|
|
478
|
+
const isLogicalOperator = createTypeCheckFunction(["||", "&&"]);
|
|
479
|
+
function isSimpleType(node) {
|
|
480
|
+
const { type, children, namedChildren } = node;
|
|
481
|
+
const lastNamedChild = namedChildren.at(-1);
|
|
482
|
+
return [
|
|
483
|
+
"boolean_type",
|
|
484
|
+
"floating_point_type",
|
|
485
|
+
"integral_type",
|
|
486
|
+
"type_identifier",
|
|
487
|
+
"void_type"
|
|
488
|
+
].includes(type) || type === "annotated_type" && lastNamedChild && isSimpleType(lastNamedChild) || type === "array_type" && isSimpleType(node.elementNode) || type === "scoped_type_identifier" && lastNamedChild && isSimpleType(namedChildren[0]) && isSimpleType(lastNamedChild) || type === "type_parameter" && (lastNamedChild?.type !== "type_bound" || lastNamedChild.namedChildren.length === 1) || type === "wildcard" && (children.at(-1).type === "?" || isSimpleType(namedChildren.at(-1)));
|
|
489
|
+
}
|
|
490
|
+
function findBaseIndent(lines) {
|
|
491
|
+
return Math.min(...lines.map((line) => line.search(/\S/)).filter((indent) => indent >= 0));
|
|
492
|
+
}
|
|
493
|
+
function findEmbeddedLanguage(path) {
|
|
494
|
+
return path.ancestors.find(({ type, comments }) => type === "block" || comments?.some(({ leading }) => leading))?.comments?.filter(({ leading }) => leading).map(({ value }) => value.match(/^(?:\/\/|\/\*)\s*language\s*=\s*(\S+)/)?.[1]).findLast((language) => language)?.toLowerCase();
|
|
495
|
+
}
|
|
496
|
+
function escapeDocForTextBlock(doc) {
|
|
497
|
+
return mapDoc(doc, (currentDoc) => typeof currentDoc === "string" ? currentDoc.replace(/\\|"""/g, (match) => match === "\\" ? "\\\\" : "\"\"\\\"") : currentDoc);
|
|
498
|
+
}
|
|
499
|
+
function unescapeTextBlockContents(text) {
|
|
500
|
+
return text.replace(/\\(?:([stnr"'\\])|\n|\r\n?)/g, (_, escaped) => {
|
|
501
|
+
switch (escaped) {
|
|
502
|
+
case "s": return " ";
|
|
503
|
+
case "t": return " ";
|
|
504
|
+
case "n": return "\n";
|
|
505
|
+
case "r": return "\r";
|
|
506
|
+
default: return escaped ?? "";
|
|
507
|
+
}
|
|
508
|
+
});
|
|
509
|
+
}
|
|
510
|
+
//#endregion
|
|
511
|
+
//#region src/printers/arrays.ts
|
|
512
|
+
var arrays_default = { array_initializer: printArrayInitializer };
|
|
513
|
+
//#endregion
|
|
514
|
+
//#region src/printers/blocks-and-statements.ts
|
|
515
|
+
const { group: group$6, hardline: hardline$6, ifBreak: ifBreak$1, indent: indent$6, indentIfBreak: indentIfBreak$1, join: join$6, line: line$6, softline: softline$3 } = prettier_doc.builders;
|
|
516
|
+
var blocks_and_statements_default = {
|
|
517
|
+
block(path, print, options) {
|
|
518
|
+
return printBlock(path, printBlockStatements(path, print), options);
|
|
519
|
+
},
|
|
520
|
+
local_variable_declaration: printVariableDeclaration,
|
|
521
|
+
labeled_statement(path, print) {
|
|
522
|
+
return join$6(": ", path.map(print, "namedChildren"));
|
|
523
|
+
},
|
|
524
|
+
expression_statement(path, print) {
|
|
525
|
+
const parentType = path.parent?.type;
|
|
526
|
+
const expressionType = path.node.namedChildren[0].type;
|
|
527
|
+
const expression = path.call(print, "namedChildren", 0);
|
|
528
|
+
return expressionType === "switch_expression" && parentType !== "assignment_expression" && parentType !== "switch_rule" ? expression : [expression, ";"];
|
|
529
|
+
},
|
|
530
|
+
if_statement(path, print, options) {
|
|
531
|
+
const statement = ["if ", path.call(print, "conditionNode")];
|
|
532
|
+
if (path.node.consequenceNode.type === ";") statement.push(";");
|
|
533
|
+
else statement.push(" ", path.call(print, "consequenceNode"));
|
|
534
|
+
if (!hasChild(path, "alternativeNode")) return statement;
|
|
535
|
+
const danglingComments = printDanglingComments(path);
|
|
536
|
+
if (danglingComments) statement.push(hardline$6, danglingComments, hardline$6);
|
|
537
|
+
else if (options?.braceStyle === "next-line") statement.push(hardline$6);
|
|
538
|
+
else {
|
|
539
|
+
const ifHasBlock = path.node.consequenceNode.type === "block";
|
|
540
|
+
statement.push(ifHasBlock ? " " : hardline$6);
|
|
541
|
+
}
|
|
542
|
+
statement.push("else");
|
|
543
|
+
if (path.node.alternativeNode.type === ";") statement.push(";");
|
|
544
|
+
else statement.push(" ", path.call(print, "alternativeNode"));
|
|
545
|
+
return statement;
|
|
546
|
+
},
|
|
547
|
+
assert_statement(path, print) {
|
|
548
|
+
return [
|
|
549
|
+
"assert ",
|
|
550
|
+
...join$6(" : ", path.map(print, "namedChildren")),
|
|
551
|
+
";"
|
|
552
|
+
];
|
|
553
|
+
},
|
|
554
|
+
switch_expression(path, print) {
|
|
555
|
+
return join$6(" ", [
|
|
556
|
+
"switch",
|
|
557
|
+
path.call(print, "conditionNode"),
|
|
558
|
+
path.call(print, "bodyNode")
|
|
559
|
+
]);
|
|
560
|
+
},
|
|
561
|
+
switch_block(path, print, options) {
|
|
562
|
+
return printBlock(path, path.map(print, "namedChildren"), options);
|
|
563
|
+
},
|
|
564
|
+
switch_block_statement_group(path, print) {
|
|
565
|
+
const parts = [];
|
|
566
|
+
path.each((child) => {
|
|
567
|
+
if (child.node.type === "switch_label") parts.push(print(child), ":");
|
|
568
|
+
}, "namedChildren");
|
|
569
|
+
const firstStatementIndex = path.node.namedChildren.findIndex(({ type }) => type !== "switch_label");
|
|
570
|
+
if (firstStatementIndex === path.node.namedChildren.length - 1 && path.node.namedChildren[firstStatementIndex].type === "block") parts.push(" ", path.call(print, "namedChildren", firstStatementIndex));
|
|
571
|
+
else if (firstStatementIndex !== -1) parts.push(indent$6([hardline$6, ...join$6(hardline$6, printBlockStatements(path, print))]));
|
|
572
|
+
return parts;
|
|
573
|
+
},
|
|
574
|
+
switch_label(path, print) {
|
|
575
|
+
if (!path.node.children.some(({ type }) => type === "case")) return "default";
|
|
576
|
+
const values = [];
|
|
577
|
+
path.each((child) => {
|
|
578
|
+
if (child.node.type !== "guard") values.push(print(child));
|
|
579
|
+
}, "namedChildren");
|
|
580
|
+
const hasMultipleValues = values.length > 1;
|
|
581
|
+
const label = hasMultipleValues ? ["case", indent$6([line$6, ...join$6([",", line$6], values)])] : ["case ", values[0]];
|
|
582
|
+
const guardIndex = path.node.namedChildren.findIndex(({ type }) => type === "guard");
|
|
583
|
+
return guardIndex !== -1 ? [group$6([...label, hasMultipleValues ? line$6 : " "]), path.call(print, "namedChildren", guardIndex)] : group$6(label);
|
|
584
|
+
},
|
|
585
|
+
switch_rule(path, print) {
|
|
586
|
+
const bodyIndex = path.node.namedChildren.findIndex(({ type }) => type === "block" || type === "expression_statement" || type === "throw_statement");
|
|
587
|
+
const body = path.call(print, "namedChildren", bodyIndex);
|
|
588
|
+
const switchLabelIndex = path.node.namedChildren.findIndex(({ type }) => type === "switch_label");
|
|
589
|
+
const parts = [path.call(print, "namedChildren", switchLabelIndex), " ->"];
|
|
590
|
+
const bodyNode = path.node.namedChildren[bodyIndex];
|
|
591
|
+
if (bodyNode.type !== "block" && hasLeadingComments(bodyNode)) parts.push(indent$6([hardline$6, body]));
|
|
592
|
+
else parts.push(" ", body);
|
|
593
|
+
return parts;
|
|
594
|
+
},
|
|
595
|
+
while_statement(path, print) {
|
|
596
|
+
const parts = ["while ", path.call(print, "conditionNode")];
|
|
597
|
+
const body = path.call(print, "bodyNode");
|
|
598
|
+
const bodyType = path.node.bodyNode.type;
|
|
599
|
+
if (bodyType === "block") {
|
|
600
|
+
parts.push(" ", body);
|
|
601
|
+
return parts;
|
|
602
|
+
} else if (bodyType === ";") {
|
|
603
|
+
parts.push(";");
|
|
604
|
+
return parts;
|
|
605
|
+
} else {
|
|
606
|
+
parts.push(line$6, body);
|
|
607
|
+
return group$6(indent$6(parts));
|
|
608
|
+
}
|
|
609
|
+
},
|
|
610
|
+
do_statement(path, print, options) {
|
|
611
|
+
const hasEmptyStatement = path.node.bodyNode.type === ";";
|
|
612
|
+
const isNextLine = options?.braceStyle === "next-line";
|
|
613
|
+
const whilePrefix = isNextLine ? [hardline$6, "while "] : " while ";
|
|
614
|
+
return [
|
|
615
|
+
"do",
|
|
616
|
+
hasEmptyStatement ? ";" : isNextLine ? path.call(print, "bodyNode") : [" ", path.call(print, "bodyNode")],
|
|
617
|
+
whilePrefix,
|
|
618
|
+
path.call(print, "conditionNode"),
|
|
619
|
+
";"
|
|
620
|
+
];
|
|
621
|
+
},
|
|
622
|
+
for_statement(path, print) {
|
|
623
|
+
const danglingComments = printDanglingComments(path);
|
|
624
|
+
const hasInit = path.node.initNodes.length > 0;
|
|
625
|
+
const hasCondition = hasChild(path, "conditionNode");
|
|
626
|
+
const hasUpdate = path.node.updateNodes.length > 0;
|
|
627
|
+
const expressions = [
|
|
628
|
+
!hasInit ? ";" : path.node.initNodes[0].type === "local_variable_declaration" ? path.call(print, "initNodes", 0) : [printExpressionList(path.map(print, "initNodes")), ";"],
|
|
629
|
+
hasCondition ? [path.call(print, "conditionNode"), ";"] : ";",
|
|
630
|
+
hasUpdate ? printExpressionList(path.map(print, "updateNodes")) : ""
|
|
631
|
+
];
|
|
632
|
+
const hasEmptyStatement = path.node.bodyNode.type === ";";
|
|
633
|
+
return [
|
|
634
|
+
danglingComments && [danglingComments, hardline$6],
|
|
635
|
+
"for ",
|
|
636
|
+
hasInit || hasCondition || hasUpdate ? group$6(indentInParentheses(join$6(line$6, expressions))) : "(;;)",
|
|
637
|
+
hasEmptyStatement ? ";" : [" ", path.call(print, "bodyNode")]
|
|
638
|
+
];
|
|
639
|
+
},
|
|
640
|
+
update_expression(path, print) {
|
|
641
|
+
return path.map(print, "children");
|
|
642
|
+
},
|
|
643
|
+
enhanced_for_statement(path, print) {
|
|
644
|
+
const danglingComments = printDanglingComments(path);
|
|
645
|
+
const forStatement = [
|
|
646
|
+
danglingComments && [danglingComments, hardline$6],
|
|
647
|
+
"for (",
|
|
648
|
+
...printModifiers(path, print),
|
|
649
|
+
path.call(print, "typeNode"),
|
|
650
|
+
" ",
|
|
651
|
+
path.call(print, "nameNode")
|
|
652
|
+
];
|
|
653
|
+
if (hasChild(path, "dimensionsNode")) forStatement.push(path.call(print, "dimensionsNode"));
|
|
654
|
+
forStatement.push(" : ", path.call(print, "valueNode"), ")");
|
|
655
|
+
const bodyType = path.node.bodyNode.type;
|
|
656
|
+
if (bodyType === ";") forStatement.push(";");
|
|
657
|
+
else {
|
|
658
|
+
const body = path.call(print, "bodyNode");
|
|
659
|
+
forStatement.push(bodyType === "block" ? [" ", body] : indent$6([line$6, body]));
|
|
660
|
+
}
|
|
661
|
+
return group$6(forStatement);
|
|
662
|
+
},
|
|
663
|
+
break_statement(path, print) {
|
|
664
|
+
const parts = ["break"];
|
|
665
|
+
const identifierIndex = path.node.namedChildren.findIndex(({ type }) => type === "identifier");
|
|
666
|
+
if (identifierIndex !== -1) parts.push(" ", path.call(print, "namedChildren", identifierIndex));
|
|
667
|
+
parts.push(";");
|
|
668
|
+
return parts;
|
|
669
|
+
},
|
|
670
|
+
continue_statement(path, print) {
|
|
671
|
+
const identifierIndex = path.node.namedChildren.findIndex(({ type }) => type === "identifier");
|
|
672
|
+
return identifierIndex !== -1 ? [
|
|
673
|
+
"continue ",
|
|
674
|
+
path.call(print, "namedChildren", identifierIndex),
|
|
675
|
+
";"
|
|
676
|
+
] : "continue;";
|
|
677
|
+
},
|
|
678
|
+
return_statement: printReturnOrThrowStatement,
|
|
679
|
+
throw_statement: printReturnOrThrowStatement,
|
|
680
|
+
synchronized_statement(path, print) {
|
|
681
|
+
const parenthesizedExpressionIndex = path.node.namedChildren.findIndex(({ type }) => type === "parenthesized_expression");
|
|
682
|
+
return [
|
|
683
|
+
"synchronized ",
|
|
684
|
+
path.call(print, "namedChildren", parenthesizedExpressionIndex),
|
|
685
|
+
" ",
|
|
686
|
+
path.call(print, "bodyNode")
|
|
687
|
+
];
|
|
688
|
+
},
|
|
689
|
+
try_statement(path, print, options) {
|
|
690
|
+
const parts = ["try", path.call(print, "bodyNode")];
|
|
691
|
+
path.each((child) => {
|
|
692
|
+
if (child.node.type === "catch_clause" || child.node.type === "finally_clause") parts.push(print(child));
|
|
693
|
+
}, "namedChildren");
|
|
694
|
+
if (options?.braceStyle === "next-line") return parts;
|
|
695
|
+
return join$6(" ", parts);
|
|
696
|
+
},
|
|
697
|
+
catch_clause(path, print, options) {
|
|
698
|
+
const catchFormalParameterIndex = path.node.namedChildren.findIndex(({ type }) => type === "catch_formal_parameter");
|
|
699
|
+
const params = group$6(indentInParentheses(path.call(print, "namedChildren", catchFormalParameterIndex)));
|
|
700
|
+
const body = path.call(print, "bodyNode");
|
|
701
|
+
if (options?.braceStyle === "next-line") return [
|
|
702
|
+
hardline$6,
|
|
703
|
+
"catch ",
|
|
704
|
+
params,
|
|
705
|
+
body
|
|
706
|
+
];
|
|
707
|
+
return [
|
|
708
|
+
"catch ",
|
|
709
|
+
params,
|
|
710
|
+
" ",
|
|
711
|
+
body
|
|
712
|
+
];
|
|
713
|
+
},
|
|
714
|
+
catch_formal_parameter(path, print) {
|
|
715
|
+
const parts = printModifiers(path, print, "noBreak");
|
|
716
|
+
const catchTypeIndex = path.node.namedChildren.findIndex(({ type }) => type === "catch_type");
|
|
717
|
+
parts.push(path.call(print, "namedChildren", catchTypeIndex), " ", path.call(print, "nameNode"));
|
|
718
|
+
if (hasChild(path, "dimensionsNode")) parts.push(path.call(print, "dimensionsNode"));
|
|
719
|
+
return parts;
|
|
720
|
+
},
|
|
721
|
+
catch_type(path, print) {
|
|
722
|
+
return join$6([line$6, "| "], path.map(print, "namedChildren"));
|
|
723
|
+
},
|
|
724
|
+
finally_clause(path, print, options) {
|
|
725
|
+
if (options?.braceStyle === "next-line") return [
|
|
726
|
+
hardline$6,
|
|
727
|
+
"finally",
|
|
728
|
+
path.call(print, "namedChildren", 0)
|
|
729
|
+
];
|
|
730
|
+
return ["finally ", path.call(print, "namedChildren", 0)];
|
|
731
|
+
},
|
|
732
|
+
try_with_resources_statement(path, print, options) {
|
|
733
|
+
const resources = path.call(print, "resourcesNode");
|
|
734
|
+
const body = path.call(print, "bodyNode");
|
|
735
|
+
const clauses = [];
|
|
736
|
+
path.each((child) => {
|
|
737
|
+
if (child.node.type === "catch_clause" || child.node.type === "finally_clause") clauses.push(print(child));
|
|
738
|
+
}, "namedChildren");
|
|
739
|
+
if (options?.braceStyle === "next-line") return [
|
|
740
|
+
"try ",
|
|
741
|
+
resources,
|
|
742
|
+
body,
|
|
743
|
+
...clauses
|
|
744
|
+
];
|
|
745
|
+
return join$6(" ", [
|
|
746
|
+
"try",
|
|
747
|
+
resources,
|
|
748
|
+
body,
|
|
749
|
+
...clauses
|
|
750
|
+
]);
|
|
751
|
+
},
|
|
752
|
+
resource_specification(path, print) {
|
|
753
|
+
const resources = [];
|
|
754
|
+
let hasTrailingSemicolon = false;
|
|
755
|
+
path.each((child) => {
|
|
756
|
+
if (child.node.type === "resource") {
|
|
757
|
+
resources.push(print(child));
|
|
758
|
+
hasTrailingSemicolon = false;
|
|
759
|
+
} else if (child.node.type === ";") hasTrailingSemicolon = true;
|
|
760
|
+
}, "children");
|
|
761
|
+
const parts = join$6([";", line$6], resources);
|
|
762
|
+
if (hasTrailingSemicolon) parts.push(ifBreak$1(";"));
|
|
763
|
+
return group$6(indentInParentheses(parts));
|
|
764
|
+
},
|
|
765
|
+
resource(path, print) {
|
|
766
|
+
if (hasChild(path, "typeNode") && hasChild(path, "nameNode") && hasChild(path, "valueNode")) {
|
|
767
|
+
const parts = printModifiers(path, print);
|
|
768
|
+
parts.push(path.call(print, "typeNode"), " ", path.call(print, "nameNode"));
|
|
769
|
+
if (hasChild(path, "dimensionsNode")) parts.push(path.call(print, "dimensionsNode"));
|
|
770
|
+
parts.push(" =");
|
|
771
|
+
const value = path.call(print, "valueNode");
|
|
772
|
+
if (path.node.valueNode.type === "binary_expression" || hasLeadingComments(path.node.valueNode)) parts.push(group$6(indent$6([line$6, value])));
|
|
773
|
+
else {
|
|
774
|
+
const groupId = Symbol("assignment");
|
|
775
|
+
parts.push(group$6(indent$6(line$6), { id: groupId }), indentIfBreak$1(value, { groupId }));
|
|
776
|
+
}
|
|
777
|
+
return parts;
|
|
778
|
+
}
|
|
779
|
+
const resourceIndex = path.node.namedChildren.findIndex(({ type }) => type === "identifier" || type === "field_access");
|
|
780
|
+
return path.call(print, "namedChildren", resourceIndex);
|
|
781
|
+
},
|
|
782
|
+
yield_statement(path, print) {
|
|
783
|
+
return [
|
|
784
|
+
"yield ",
|
|
785
|
+
path.call(print, "namedChildren", 0),
|
|
786
|
+
";"
|
|
787
|
+
];
|
|
788
|
+
}
|
|
789
|
+
};
|
|
790
|
+
function printExpressionList(expressions) {
|
|
791
|
+
return group$6(expressions.map((expression, index) => index === 0 ? expression : [",", indent$6([line$6, expression])]));
|
|
792
|
+
}
|
|
793
|
+
function printReturnOrThrowArgument(path, print) {
|
|
794
|
+
const { node } = path;
|
|
795
|
+
const argumentDoc = print(path);
|
|
796
|
+
if (returnArgumentHasLeadingComment(node)) return [
|
|
797
|
+
"(",
|
|
798
|
+
indent$6([hardline$6, argumentDoc]),
|
|
799
|
+
hardline$6,
|
|
800
|
+
")"
|
|
801
|
+
];
|
|
802
|
+
if (node.type === "binary_expression") return group$6([
|
|
803
|
+
ifBreak$1("("),
|
|
804
|
+
indent$6([softline$3, argumentDoc]),
|
|
805
|
+
softline$3,
|
|
806
|
+
ifBreak$1(")")
|
|
807
|
+
]);
|
|
808
|
+
return argumentDoc;
|
|
809
|
+
}
|
|
810
|
+
function printReturnOrThrowStatement(path, print) {
|
|
811
|
+
const { node } = path;
|
|
812
|
+
return [
|
|
813
|
+
node.type === "throw_statement" ? "throw" : "return",
|
|
814
|
+
node.namedChildren.length ? [" ", path.call(() => printReturnOrThrowArgument(path, print), "namedChildren", 0)] : "",
|
|
815
|
+
";"
|
|
816
|
+
];
|
|
817
|
+
}
|
|
818
|
+
//#endregion
|
|
819
|
+
//#region src/printers/classes.ts
|
|
820
|
+
const { group: group$5, hardline: hardline$5, indent: indent$5, join: join$5, line: line$5, softline: softline$2 } = prettier_doc.builders;
|
|
821
|
+
var classes_default = {
|
|
822
|
+
class_declaration(path, print, options) {
|
|
823
|
+
const parts = ["class ", path.call(print, "nameNode")];
|
|
824
|
+
const definedClauses = definedKeys(path.node, [
|
|
825
|
+
"superclassNode",
|
|
826
|
+
"interfacesNode",
|
|
827
|
+
"permitsNode"
|
|
828
|
+
]);
|
|
829
|
+
const hasMultipleClauses = definedClauses.length > 1;
|
|
830
|
+
if (hasChild(path, "type_parametersNode")) {
|
|
831
|
+
const typeParameters = path.call(print, "type_parametersNode");
|
|
832
|
+
parts.push(hasMultipleClauses ? group$5(indent$5(typeParameters)) : typeParameters);
|
|
833
|
+
}
|
|
834
|
+
if (definedClauses.length) {
|
|
835
|
+
const clauses = definedClauses.flatMap((clause) => {
|
|
836
|
+
if (!hasChild(path, clause)) return [];
|
|
837
|
+
return [clause === "superclassNode" || clause === "interfacesNode" ? hardline$5 : line$5, path.call(print, clause)];
|
|
838
|
+
});
|
|
839
|
+
const hasBody = path.node.bodyNode.namedChildren.length > 0;
|
|
840
|
+
const afterClauses = options?.braceStyle === "next-line" ? "" : hasBody ? line$5 : " ";
|
|
841
|
+
const clauseGroup = [indent$5(clauses), afterClauses];
|
|
842
|
+
parts.push(hasMultipleClauses ? clauseGroup : group$5(clauseGroup));
|
|
843
|
+
} else if (options?.braceStyle !== "next-line") parts.push(" ");
|
|
844
|
+
return [
|
|
845
|
+
...printModifiers(path, print, "declarationOnly"),
|
|
846
|
+
group$5(parts),
|
|
847
|
+
path.call(print, "bodyNode")
|
|
848
|
+
];
|
|
849
|
+
},
|
|
850
|
+
type_parameters: printTypeParameters,
|
|
851
|
+
superclass(path, print) {
|
|
852
|
+
return ["extends ", path.call(print, "namedChildren", 0)];
|
|
853
|
+
},
|
|
854
|
+
super_interfaces(path, print) {
|
|
855
|
+
return group$5(["implements", indent$5([line$5, path.call(print, "namedChildren", 0)])]);
|
|
856
|
+
},
|
|
857
|
+
permits(path, print) {
|
|
858
|
+
return group$5(["permits", indent$5([line$5, path.call(print, "namedChildren", 0)])]);
|
|
859
|
+
},
|
|
860
|
+
type_list(path, print) {
|
|
861
|
+
return join$5([",", line$5], path.map(print, "namedChildren"));
|
|
862
|
+
},
|
|
863
|
+
class_body(path, print, options) {
|
|
864
|
+
return printBlock(path, printBodyDeclarations(path, print, path.parent?.type === "class_declaration" && options?.braceStyle !== "next-line"), options);
|
|
865
|
+
},
|
|
866
|
+
field_declaration: printVariableDeclaration,
|
|
867
|
+
variable_declarator(path, print) {
|
|
868
|
+
const leftDoc = [path.call(print, "nameNode")];
|
|
869
|
+
if (hasChild(path, "dimensionsNode")) leftDoc.push(path.call(print, "dimensionsNode"));
|
|
870
|
+
return printAssignment(leftDoc, " =", hasChild(path, "valueNode") ? path.call(print, "valueNode") : void 0, path.node.valueNode);
|
|
871
|
+
},
|
|
872
|
+
method_declaration(path, print) {
|
|
873
|
+
const modifiers = printModifiers(path, print);
|
|
874
|
+
const declaration = [];
|
|
875
|
+
if (hasChild(path, "type_parametersNode")) declaration.push(group$5(path.call(print, "type_parametersNode")), " ");
|
|
876
|
+
path.each((child) => {
|
|
877
|
+
if (child.node.type === "annotation" || child.node.type === "marker_annotation") declaration.push(print(child), " ");
|
|
878
|
+
}, "children");
|
|
879
|
+
declaration.push(path.call(print, "typeNode"));
|
|
880
|
+
if (hasChild(path, "dimensionsNode")) declaration.push(path.call(print, "dimensionsNode"));
|
|
881
|
+
declaration.push(" ", path.call(print, "nameNode"), group$5(path.call(print, "parametersNode")));
|
|
882
|
+
const throwsIndex = path.node.namedChildren.findIndex(({ type }) => type === "throws");
|
|
883
|
+
if (throwsIndex !== -1) declaration.push(group$5(indent$5([hardline$5, path.call(print, "namedChildren", throwsIndex)])));
|
|
884
|
+
return hasChild(path, "bodyNode") ? [
|
|
885
|
+
modifiers,
|
|
886
|
+
group$5(declaration),
|
|
887
|
+
" ",
|
|
888
|
+
path.call(print, "bodyNode")
|
|
889
|
+
] : [
|
|
890
|
+
modifiers,
|
|
891
|
+
group$5(declaration),
|
|
892
|
+
";"
|
|
893
|
+
];
|
|
894
|
+
},
|
|
895
|
+
receiver_parameter(path, print) {
|
|
896
|
+
return path.map((child) => child.isLast ? print(child) : [print(child), child.node.type === "identifier" ? "." : " "], "namedChildren");
|
|
897
|
+
},
|
|
898
|
+
formal_parameters(path, print) {
|
|
899
|
+
const parameters = path.parent?.type === "record_declaration" ? printBodyDeclarations(path, print) : path.map(print, "namedChildren");
|
|
900
|
+
if (parameters.length === 0) return [
|
|
901
|
+
"(",
|
|
902
|
+
printDanglingCommentsInList(path),
|
|
903
|
+
")"
|
|
904
|
+
];
|
|
905
|
+
return indentInParentheses(join$5([",", line$5], parameters));
|
|
906
|
+
},
|
|
907
|
+
formal_parameter(path, print) {
|
|
908
|
+
const parameter = printModifiers(path, print, path.grandparent?.type === "record_declaration" ? "avoidBreak" : "noBreak");
|
|
909
|
+
parameter.push(path.call(print, "typeNode"));
|
|
910
|
+
if (hasChild(path, "dimensionsNode")) parameter.push(path.call(print, "dimensionsNode"));
|
|
911
|
+
parameter.push(" ", path.call(print, "nameNode"));
|
|
912
|
+
return group$5(parameter);
|
|
913
|
+
},
|
|
914
|
+
spread_parameter(path, print) {
|
|
915
|
+
const parts = printModifiers(path, print, "noBreak");
|
|
916
|
+
parts.push(path.call(print, "typeNode"));
|
|
917
|
+
if (hasChild(path, "annotationsNodes")) parts.push(...path.map((annotation) => [" ", print(annotation)], "annotationsNodes"));
|
|
918
|
+
parts.push("... ", path.call(print, "namedChildren", path.node.namedChildren.length - 1));
|
|
919
|
+
return parts;
|
|
920
|
+
},
|
|
921
|
+
throws(path, print) {
|
|
922
|
+
return ["throws ", ...join$5(", ", path.map(print, "namedChildren"))];
|
|
923
|
+
},
|
|
924
|
+
static_initializer(path, print) {
|
|
925
|
+
return ["static ", path.call(print, "namedChildren", 0)];
|
|
926
|
+
},
|
|
927
|
+
constructor_declaration(path, print) {
|
|
928
|
+
const modifiers = printModifiers(path, print, "declarationOnly");
|
|
929
|
+
const declaration = [];
|
|
930
|
+
if (hasChild(path, "type_parametersNode")) declaration.push(group$5(path.call(print, "type_parametersNode")), " ");
|
|
931
|
+
declaration.push(path.call(print, "nameNode"), group$5(path.call(print, "parametersNode")));
|
|
932
|
+
const throwsIndex = path.node.namedChildren.findIndex(({ type }) => type === "throws");
|
|
933
|
+
if (throwsIndex !== -1) declaration.push(group$5(indent$5([hardline$5, path.call(print, "namedChildren", throwsIndex)])));
|
|
934
|
+
return [
|
|
935
|
+
modifiers,
|
|
936
|
+
group$5(declaration),
|
|
937
|
+
" ",
|
|
938
|
+
path.call(print, "bodyNode")
|
|
939
|
+
];
|
|
940
|
+
},
|
|
941
|
+
constructor_body(path, print, options) {
|
|
942
|
+
return printBlock(path, printBlockStatements(path, print), options);
|
|
943
|
+
},
|
|
944
|
+
explicit_constructor_invocation(path, print) {
|
|
945
|
+
const invocation = [];
|
|
946
|
+
if (hasChild(path, "objectNode")) invocation.push(path.call(print, "objectNode"), ".");
|
|
947
|
+
if (hasChild(path, "type_argumentsNode")) invocation.push(path.call(print, "type_argumentsNode"));
|
|
948
|
+
invocation.push(path.call(print, "constructorNode"), path.call(print, "argumentsNode"), ";");
|
|
949
|
+
return invocation;
|
|
950
|
+
},
|
|
951
|
+
visibility: printValue,
|
|
952
|
+
modifier: printValue,
|
|
953
|
+
modifiers(path, print, _, args) {
|
|
954
|
+
const parts = [];
|
|
955
|
+
const modifiers = [];
|
|
956
|
+
const typeAnnotations = [];
|
|
957
|
+
path.each((child) => {
|
|
958
|
+
if (child.node.type === "annotation" || child.node.type === "marker_annotation") (modifiers.length ? typeAnnotations : parts).push(print(child));
|
|
959
|
+
else {
|
|
960
|
+
modifiers.push(child.node.value);
|
|
961
|
+
parts.push(...typeAnnotations);
|
|
962
|
+
typeAnnotations.length = 0;
|
|
963
|
+
}
|
|
964
|
+
}, "namedChildren");
|
|
965
|
+
const annotationMode = args && typeof args === "object" && "annotationMode" in args ? args.annotationMode : null;
|
|
966
|
+
if (annotationMode === "declarationOnly") {
|
|
967
|
+
parts.push(...typeAnnotations);
|
|
968
|
+
typeAnnotations.length = 0;
|
|
969
|
+
}
|
|
970
|
+
modifiers.sort((a, b) => (indexByModifier.get(a) ?? -1) - (indexByModifier.get(b) ?? -1));
|
|
971
|
+
if (modifiers.length || typeAnnotations.length) parts.push(join$5(" ", [...modifiers, ...typeAnnotations]));
|
|
972
|
+
return join$5(annotationMode === "avoidBreak" ? line$5 : annotationMode === "noBreak" ? " " : hardline$5, parts);
|
|
973
|
+
},
|
|
974
|
+
enum_declaration(path, print) {
|
|
975
|
+
const modifiers = printModifiers(path, print, "declarationOnly");
|
|
976
|
+
const parts = ["enum ", path.call(print, "nameNode")];
|
|
977
|
+
if (hasChild(path, "interfacesNode")) {
|
|
978
|
+
const hasBody = path.node.bodyNode.namedChildren.length > 0;
|
|
979
|
+
parts.push(indent$5([hardline$5, path.call(print, "interfacesNode")]), hasBody ? line$5 : " ");
|
|
980
|
+
} else parts.push(" ");
|
|
981
|
+
return [
|
|
982
|
+
...modifiers,
|
|
983
|
+
group$5(parts),
|
|
984
|
+
path.call(print, "bodyNode")
|
|
985
|
+
];
|
|
986
|
+
},
|
|
987
|
+
enum_body(path, print, options) {
|
|
988
|
+
const parts = printBodyDeclarations(path, print);
|
|
989
|
+
const enumBodyDeclarationsIndex = path.node.namedChildren.findIndex(({ type }) => type === "enum_body_declarations");
|
|
990
|
+
const declarations = [];
|
|
991
|
+
if (enumBodyDeclarationsIndex !== -1) {
|
|
992
|
+
const hasDeclarations = path.node.namedChildren[enumBodyDeclarationsIndex].namedChildren.length > 0;
|
|
993
|
+
const enumBodyDeclarations = parts.pop();
|
|
994
|
+
if (hasDeclarations) {
|
|
995
|
+
if (!parts.length) declarations.push(hardline$5);
|
|
996
|
+
declarations.push(enumBodyDeclarations);
|
|
997
|
+
}
|
|
998
|
+
}
|
|
999
|
+
const contents = [];
|
|
1000
|
+
if (parts.length) {
|
|
1001
|
+
contents.push(join$5([",", hardline$5], parts));
|
|
1002
|
+
if (!declarations.length && options.trailingComma !== "none") contents.push(",");
|
|
1003
|
+
}
|
|
1004
|
+
if (declarations.length) contents.push(";", hardline$5, ...declarations);
|
|
1005
|
+
return printBlock(path, contents.length ? [contents] : [], options);
|
|
1006
|
+
},
|
|
1007
|
+
enum_constant(path, print) {
|
|
1008
|
+
const initializer = printModifiers(path, print);
|
|
1009
|
+
initializer.push(path.call(print, "nameNode"));
|
|
1010
|
+
if (hasChild(path, "argumentsNode")) initializer.push(path.call(print, "argumentsNode"));
|
|
1011
|
+
if (hasChild(path, "bodyNode")) initializer.push(" ", path.call(print, "bodyNode"));
|
|
1012
|
+
return initializer;
|
|
1013
|
+
},
|
|
1014
|
+
enum_body_declarations(path, print) {
|
|
1015
|
+
return join$5(hardline$5, printBodyDeclarations(path, print));
|
|
1016
|
+
},
|
|
1017
|
+
record_declaration(path, print) {
|
|
1018
|
+
const modifiers = printModifiers(path, print, "declarationOnly");
|
|
1019
|
+
const parts = ["record ", path.call(print, "nameNode")];
|
|
1020
|
+
if (hasChild(path, "type_parametersNode")) parts.push(group$5(path.call(print, "type_parametersNode")));
|
|
1021
|
+
parts.push(group$5(path.call(print, "parametersNode")));
|
|
1022
|
+
if (hasChild(path, "interfacesNode")) {
|
|
1023
|
+
const hasBody = path.node.bodyNode.namedChildren.length > 0;
|
|
1024
|
+
const interfaces = [hardline$5, path.call(print, "interfacesNode")];
|
|
1025
|
+
parts.push(group$5([indent$5(interfaces), hasBody ? line$5 : " "]));
|
|
1026
|
+
} else parts.push(" ");
|
|
1027
|
+
return [
|
|
1028
|
+
...modifiers,
|
|
1029
|
+
group$5(parts),
|
|
1030
|
+
path.call(print, "bodyNode")
|
|
1031
|
+
];
|
|
1032
|
+
},
|
|
1033
|
+
compact_constructor_declaration(path, print) {
|
|
1034
|
+
const parts = printModifiers(path, print, "declarationOnly");
|
|
1035
|
+
parts.push(path.call(print, "nameNode"), " ", path.call(print, "bodyNode"));
|
|
1036
|
+
return parts;
|
|
1037
|
+
}
|
|
1038
|
+
};
|
|
1039
|
+
const indexByModifier = [
|
|
1040
|
+
"public",
|
|
1041
|
+
"protected",
|
|
1042
|
+
"private",
|
|
1043
|
+
"abstract",
|
|
1044
|
+
"default",
|
|
1045
|
+
"static",
|
|
1046
|
+
"final",
|
|
1047
|
+
"transient",
|
|
1048
|
+
"volatile",
|
|
1049
|
+
"synchronized",
|
|
1050
|
+
"native",
|
|
1051
|
+
"sealed",
|
|
1052
|
+
"non-sealed",
|
|
1053
|
+
"strictfp"
|
|
1054
|
+
].reduce((map, name, index) => map.set(name, index), /* @__PURE__ */ new Map());
|
|
1055
|
+
function printDanglingCommentsInList(path) {
|
|
1056
|
+
const { node } = path;
|
|
1057
|
+
return node.comments?.some((comment) => !(comment.leading || comment.trailing)) ? [indent$5([softline$2, printDanglingComments(path)]), node.comments.some(({ type, leading, trailing }) => !(leading || trailing) && type === "line_comment") ? hardline$5 : softline$2] : "";
|
|
1058
|
+
}
|
|
1059
|
+
//#endregion
|
|
1060
|
+
//#region src/printers/expressions.ts
|
|
1061
|
+
const { align, breakParent: breakParent$1, conditionalGroup, dedent, group: group$4, hardline: hardline$4, ifBreak, indent: indent$4, indentIfBreak, join: join$4, line: line$4, lineSuffixBoundary, softline: softline$1 } = prettier_doc.builders;
|
|
1062
|
+
const { getNextNonSpaceNonCommentCharacterIndex, hasNewline: hasNewline$1, isNextLineEmpty } = prettier.util;
|
|
1063
|
+
const { removeLines, willBreak } = prettier_doc.utils;
|
|
1064
|
+
var expressions_default = {
|
|
1065
|
+
lambda_expression(path, print, options, args) {
|
|
1066
|
+
const signatureDocs = [];
|
|
1067
|
+
let bodyDoc;
|
|
1068
|
+
const bodyComments = [];
|
|
1069
|
+
const shouldPrintAsChain = !(args && typeof args === "object" && "expandLastArg" in args && args.expandLastArg) && path.node.bodyNode.type === "lambda_expression";
|
|
1070
|
+
let functionBody;
|
|
1071
|
+
(function rec() {
|
|
1072
|
+
const { node } = path;
|
|
1073
|
+
const signatureDoc = printLambdaExpressionSignature(path, options, print, args);
|
|
1074
|
+
if (signatureDocs.length === 0) signatureDocs.push(signatureDoc);
|
|
1075
|
+
else {
|
|
1076
|
+
const { leading, trailing } = printCommentsSeparately(path);
|
|
1077
|
+
signatureDocs.push([leading, signatureDoc]);
|
|
1078
|
+
bodyComments.unshift(trailing);
|
|
1079
|
+
}
|
|
1080
|
+
if (!shouldPrintAsChain || node.bodyNode.type !== "lambda_expression") {
|
|
1081
|
+
bodyDoc = path.call((child) => print(child, args), "bodyNode");
|
|
1082
|
+
functionBody = node.bodyNode;
|
|
1083
|
+
} else path.call(rec, "bodyNode");
|
|
1084
|
+
})();
|
|
1085
|
+
const shouldPutBodyOnSameLine = !functionBody.comments?.some((comment) => comment.leading && hasNewline$1(options.originalText, comment.end.index)) && mayBreakAfterShortPrefix(functionBody);
|
|
1086
|
+
const chainGroupId = Symbol("arrow-chain");
|
|
1087
|
+
const signaturesDoc = printArrowFunctionSignatures(path, { signatureDocs });
|
|
1088
|
+
bodyDoc = printArrowFunctionBody(path, args, {
|
|
1089
|
+
bodyDoc,
|
|
1090
|
+
bodyComments,
|
|
1091
|
+
shouldPutBodyOnSameLine
|
|
1092
|
+
});
|
|
1093
|
+
return group$4([
|
|
1094
|
+
group$4(signaturesDoc, { id: chainGroupId }),
|
|
1095
|
+
" ->",
|
|
1096
|
+
shouldPrintAsChain ? indentIfBreak(bodyDoc, { groupId: chainGroupId }) : group$4(bodyDoc)
|
|
1097
|
+
]);
|
|
1098
|
+
},
|
|
1099
|
+
inferred_parameters(path, print, options) {
|
|
1100
|
+
const identifiers = [];
|
|
1101
|
+
path.each((child) => {
|
|
1102
|
+
if (child.node.type === "identifier") identifiers.push(print(child));
|
|
1103
|
+
}, "namedChildren");
|
|
1104
|
+
if (!identifiers.length) return "()";
|
|
1105
|
+
const parameters = join$4([",", line$4], identifiers);
|
|
1106
|
+
if (identifiers.length > 1 || willBreak(identifiers)) return group$4(indentInParentheses(parameters));
|
|
1107
|
+
return options.arrowParens === "avoid" ? parameters : [
|
|
1108
|
+
"(",
|
|
1109
|
+
...parameters,
|
|
1110
|
+
")"
|
|
1111
|
+
];
|
|
1112
|
+
},
|
|
1113
|
+
ternary_expression(path, print, options) {
|
|
1114
|
+
const { node } = path;
|
|
1115
|
+
const consequentNode = node.consequenceNode;
|
|
1116
|
+
const parts = [];
|
|
1117
|
+
const parent = path.parent;
|
|
1118
|
+
const isParentTest = parent.type === node.type && parent.conditionNode === node;
|
|
1119
|
+
const forceNoIndent = parent.type === node.type && !isParentTest;
|
|
1120
|
+
let currentParent;
|
|
1121
|
+
let previousParent;
|
|
1122
|
+
let i = 0;
|
|
1123
|
+
do {
|
|
1124
|
+
previousParent = currentParent || node;
|
|
1125
|
+
currentParent = path.getParentNode(i);
|
|
1126
|
+
i++;
|
|
1127
|
+
} while (currentParent && currentParent.type === node.type && currentParent.conditionNode !== previousParent);
|
|
1128
|
+
const firstNonConditionalParent = currentParent || parent;
|
|
1129
|
+
const printBranch = (nodePropertyName) => options.useTabs ? indent$4(path.call(print, nodePropertyName)) : align(2, path.call(print, nodePropertyName));
|
|
1130
|
+
const part = [
|
|
1131
|
+
line$4,
|
|
1132
|
+
"? ",
|
|
1133
|
+
consequentNode.type === node.type ? ifBreak("", "(") : "",
|
|
1134
|
+
printBranch("consequenceNode"),
|
|
1135
|
+
consequentNode.type === node.type ? ifBreak("", ")") : "",
|
|
1136
|
+
line$4,
|
|
1137
|
+
": ",
|
|
1138
|
+
printBranch("alternativeNode")
|
|
1139
|
+
];
|
|
1140
|
+
parts.push(parent.type !== node.type || parent.alternativeNode === node || isParentTest ? part : options.useTabs ? dedent(indent$4(part)) : align(Math.max(0, options.tabWidth - 2), part));
|
|
1141
|
+
const maybeGroup = (doc) => parent === firstNonConditionalParent ? group$4(doc) : doc;
|
|
1142
|
+
const breakClosingParen = parent.type === "explicit_constructor_invocation" || parent.type === "field_access" || parent.type === "method_invocation" || parent.type === "method_reference" || parent.type === "object_creation_expression";
|
|
1143
|
+
const shouldExtraIndent = shouldExtraIndentForTernaryExpression(path);
|
|
1144
|
+
const result = maybeGroup([
|
|
1145
|
+
printTernaryTest(path, print),
|
|
1146
|
+
forceNoIndent ? parts : indent$4(parts),
|
|
1147
|
+
breakClosingParen && !shouldExtraIndent ? softline$1 : ""
|
|
1148
|
+
]);
|
|
1149
|
+
return isParentTest || shouldExtraIndent ? group$4([indent$4([softline$1, result]), softline$1]) : result;
|
|
1150
|
+
},
|
|
1151
|
+
assignment_expression(path, print) {
|
|
1152
|
+
const { node } = path;
|
|
1153
|
+
return printAssignment(path.call(print, "leftNode"), [" ", node.operatorNode.type], path.call(print, "rightNode"), node.rightNode);
|
|
1154
|
+
},
|
|
1155
|
+
binary_expression(path, print, options) {
|
|
1156
|
+
const { node } = path;
|
|
1157
|
+
const parent = path.parent;
|
|
1158
|
+
const grandparent = path.grandparent;
|
|
1159
|
+
const isInsideParentheses = (grandparent?.type === "if_statement" || grandparent?.type === "while_statement" || grandparent?.type === "switch_expression" || grandparent?.type === "do_statement") && grandparent.conditionNode === parent || grandparent?.type === "synchronized_statement" && grandparent.bodyNode !== parent;
|
|
1160
|
+
const parts = printBinaryExpressions(path, print, options, isInsideParentheses);
|
|
1161
|
+
if (isInsideParentheses) return parts;
|
|
1162
|
+
if (parent?.type === "unary_expression" && !node.comments || (parent?.type === "explicit_constructor_invocation" || parent?.type === "field_access" || parent?.type === "method_invocation") && parent.objectNode === node || parent?.type === "method_reference" || parent?.type === "object_creation_expression") return group$4([indent$4([softline$1, ...parts]), softline$1]);
|
|
1163
|
+
if (isReturnOrThrowStatement(parent) || parent?.type === "assignment_expression" || parent?.type === "variable_declarator" || parent?.type === "guard" || parent?.type === "lambda_expression" && parent.bodyNode === node || parent?.type === "for_statement" && parent.conditionNode === node || parent?.type === "ternary_expression" && !isReturnOrThrowStatement(grandparent) && grandparent?.type !== "parenthesized_expression" && grandparent?.type !== "argument_list" || parent?.type === "unary_expression" && parent.operandNode === node) return group$4(parts);
|
|
1164
|
+
if (parts.length === 0) return "";
|
|
1165
|
+
const firstGroupIndex = parts.findIndex((part) => typeof part !== "string" && !Array.isArray(part) && part.type === "group");
|
|
1166
|
+
const headParts = parts.slice(0, firstGroupIndex === -1 ? 1 : firstGroupIndex + 1);
|
|
1167
|
+
const rest = parts.slice(headParts.length);
|
|
1168
|
+
return group$4([...headParts, indent$4(rest)]);
|
|
1169
|
+
},
|
|
1170
|
+
instanceof_expression(path, print, options) {
|
|
1171
|
+
return group$4(indent$4(path.map((child) => {
|
|
1172
|
+
const doc = print(child, { annotationMode: "noBreak" });
|
|
1173
|
+
if (!child.previous) return doc;
|
|
1174
|
+
return [(options.experimentalOperatorPosition === "start" ? child.node.type === "instanceof" : child.previous.type === "instanceof") ? line$4 : " ", doc];
|
|
1175
|
+
}, "children")));
|
|
1176
|
+
},
|
|
1177
|
+
unary_expression(path, print) {
|
|
1178
|
+
return path.map(print, "children");
|
|
1179
|
+
},
|
|
1180
|
+
field_access: printMemberChain,
|
|
1181
|
+
generic_type(path, print) {
|
|
1182
|
+
const typeIdentifierIndex = path.node.namedChildren.findIndex(({ type }) => type === "scoped_type_identifier" || type === "type_identifier");
|
|
1183
|
+
const typeArgumentsIndex = path.node.namedChildren.findIndex(({ type }) => type === "type_arguments");
|
|
1184
|
+
return [path.call(print, "namedChildren", typeIdentifierIndex), path.call(print, "namedChildren", typeArgumentsIndex)];
|
|
1185
|
+
},
|
|
1186
|
+
parenthesized_expression(path, print) {
|
|
1187
|
+
const expression = path.call(print, "namedChildren", 0);
|
|
1188
|
+
const parentType = path.parent?.type;
|
|
1189
|
+
const grandparentType = path.grandparent?.type;
|
|
1190
|
+
const expressionType = path.node.namedChildren[0].type;
|
|
1191
|
+
const hasLambda = expressionType === "lambda_expression";
|
|
1192
|
+
const hasTernary = expressionType === "ternary_expression";
|
|
1193
|
+
const hasSuffix = parentType && (parentType === "array_access" || parentType === "explicit_constructor_invocation" || parentType === "field_access" || parentType === "method_invocation" || parentType === "method_reference" || parentType === "object_creation_expression");
|
|
1194
|
+
const isAssignment = parentType && (parentType === "assignment_expression" || parentType === "variable_declarator") || hasSuffix && (grandparentType === "assignment_expression" || grandparentType === "variable_declarator");
|
|
1195
|
+
if (!hasLambda && hasSuffix && (!hasTernary || isAssignment)) return group$4(indentInParentheses(hasTernary ? group$4(expression) : expression));
|
|
1196
|
+
else if (parentType && (parentType === "guard" || parentType === "return_statement" || parentType === "unary_expression" && grandparentType === "return_statement" || path.node.fieldName === "condition" && (parentType === "do_statement" || parentType === "if_statement" || parentType === "switch_expression" || parentType === "while_statement") || path.node.fieldName !== "body" && parentType === "synchronized_statement")) return group$4(indentInParentheses(group$4(expression)));
|
|
1197
|
+
else if (hasTernary && hasSuffix && !isAssignment) return group$4([
|
|
1198
|
+
"(",
|
|
1199
|
+
expression,
|
|
1200
|
+
softline$1,
|
|
1201
|
+
")"
|
|
1202
|
+
]);
|
|
1203
|
+
else return group$4([
|
|
1204
|
+
"(",
|
|
1205
|
+
hasLambda || hasTernary ? expression : indent$4(expression),
|
|
1206
|
+
")"
|
|
1207
|
+
]);
|
|
1208
|
+
},
|
|
1209
|
+
cast_expression(path, print) {
|
|
1210
|
+
const { node } = path;
|
|
1211
|
+
const parts = [
|
|
1212
|
+
group$4(node.typeNodes.length === 1 ? [
|
|
1213
|
+
"(",
|
|
1214
|
+
path.call(print, "typeNodes", 0),
|
|
1215
|
+
")"
|
|
1216
|
+
] : [
|
|
1217
|
+
"(",
|
|
1218
|
+
indent$4([softline$1, join$4([" &", line$4], path.map(print, "typeNodes"))]),
|
|
1219
|
+
softline$1,
|
|
1220
|
+
")"
|
|
1221
|
+
]),
|
|
1222
|
+
" ",
|
|
1223
|
+
path.call(print, "valueNode")
|
|
1224
|
+
];
|
|
1225
|
+
const parent = path.parent;
|
|
1226
|
+
if (parent?.type === "unary_expression" && !node.comments || parent?.type === "array_access" && parent.arrayNode === node || (parent?.type === "explicit_constructor_invocation" || parent?.type === "field_access" || parent?.type === "method_invocation") && parent.objectNode === node || parent?.type === "method_reference" || parent?.type === "object_creation_expression") return group$4([indent$4([softline$1, ...parts]), softline$1]);
|
|
1227
|
+
return parts;
|
|
1228
|
+
},
|
|
1229
|
+
object_creation_expression(path, print) {
|
|
1230
|
+
const expression = [];
|
|
1231
|
+
path.each((child) => {
|
|
1232
|
+
if (child.node.type === "class_body") expression.push(" ");
|
|
1233
|
+
expression.push(print(child));
|
|
1234
|
+
if (child.node.type === "annotation" || child.node.type === "marker_annotation" || child.node.type === "new") expression.push(" ");
|
|
1235
|
+
}, "children");
|
|
1236
|
+
return expression;
|
|
1237
|
+
},
|
|
1238
|
+
method_invocation: printMemberChain,
|
|
1239
|
+
argument_list(path, print, options) {
|
|
1240
|
+
const args = path.node.namedChildren;
|
|
1241
|
+
if (args.length === 0) {
|
|
1242
|
+
const shouldBreak = path.node.comments?.some(({ type, leading, trailing }) => !leading && !trailing && type === "line_comment");
|
|
1243
|
+
return group$4(indentInParentheses(printDanglingComments(path)), { shouldBreak });
|
|
1244
|
+
}
|
|
1245
|
+
const lastArgIndex = args.length - 1;
|
|
1246
|
+
let anyArgEmptyLine = false;
|
|
1247
|
+
const printedArguments = [];
|
|
1248
|
+
path.each((arg, index) => {
|
|
1249
|
+
let argDoc = print(arg);
|
|
1250
|
+
if (index === lastArgIndex) {} else if (isNextLineEmpty(options.originalText, arg.node.end.index)) {
|
|
1251
|
+
anyArgEmptyLine = true;
|
|
1252
|
+
argDoc = [
|
|
1253
|
+
argDoc,
|
|
1254
|
+
",",
|
|
1255
|
+
hardline$4,
|
|
1256
|
+
hardline$4
|
|
1257
|
+
];
|
|
1258
|
+
} else argDoc = [
|
|
1259
|
+
argDoc,
|
|
1260
|
+
",",
|
|
1261
|
+
line$4
|
|
1262
|
+
];
|
|
1263
|
+
printedArguments.push(argDoc);
|
|
1264
|
+
}, "namedChildren");
|
|
1265
|
+
function allArgsBrokenOut() {
|
|
1266
|
+
return group$4([
|
|
1267
|
+
"(",
|
|
1268
|
+
indent$4([line$4, ...printedArguments]),
|
|
1269
|
+
line$4,
|
|
1270
|
+
")"
|
|
1271
|
+
], { shouldBreak: true });
|
|
1272
|
+
}
|
|
1273
|
+
if (anyArgEmptyLine) return allArgsBrokenOut();
|
|
1274
|
+
if (shouldExpandFirstArg(args)) {
|
|
1275
|
+
const tailArgs = printedArguments.slice(1);
|
|
1276
|
+
if (tailArgs.some(willBreak)) return allArgsBrokenOut();
|
|
1277
|
+
let firstArg;
|
|
1278
|
+
try {
|
|
1279
|
+
firstArg = path.call((arg) => print(arg, { expandFirstArg: true }), "namedChildren", 0);
|
|
1280
|
+
} catch (caught) {
|
|
1281
|
+
if (caught instanceof ArgExpansionBailout) return allArgsBrokenOut();
|
|
1282
|
+
throw caught;
|
|
1283
|
+
}
|
|
1284
|
+
if (willBreak(firstArg)) return [breakParent$1, conditionalGroup([[
|
|
1285
|
+
"(",
|
|
1286
|
+
group$4(firstArg, { shouldBreak: true }),
|
|
1287
|
+
", ",
|
|
1288
|
+
...tailArgs,
|
|
1289
|
+
")"
|
|
1290
|
+
], allArgsBrokenOut()])];
|
|
1291
|
+
return conditionalGroup([
|
|
1292
|
+
[
|
|
1293
|
+
"(",
|
|
1294
|
+
firstArg,
|
|
1295
|
+
", ",
|
|
1296
|
+
...tailArgs,
|
|
1297
|
+
")"
|
|
1298
|
+
],
|
|
1299
|
+
[
|
|
1300
|
+
"(",
|
|
1301
|
+
group$4(firstArg, { shouldBreak: true }),
|
|
1302
|
+
", ",
|
|
1303
|
+
...tailArgs,
|
|
1304
|
+
")"
|
|
1305
|
+
],
|
|
1306
|
+
allArgsBrokenOut()
|
|
1307
|
+
]);
|
|
1308
|
+
}
|
|
1309
|
+
if (shouldExpandLastArg(args)) {
|
|
1310
|
+
const headArgs = printedArguments.slice(0, -1);
|
|
1311
|
+
if (headArgs.some(willBreak)) return allArgsBrokenOut();
|
|
1312
|
+
let lastArg;
|
|
1313
|
+
try {
|
|
1314
|
+
lastArg = path.call((arg) => print(arg, { expandLastArg: true }), "namedChildren", lastArgIndex);
|
|
1315
|
+
} catch (caught) {
|
|
1316
|
+
if (caught instanceof ArgExpansionBailout) return allArgsBrokenOut();
|
|
1317
|
+
throw caught;
|
|
1318
|
+
}
|
|
1319
|
+
if (willBreak(lastArg)) return [breakParent$1, conditionalGroup([[
|
|
1320
|
+
"(",
|
|
1321
|
+
...headArgs,
|
|
1322
|
+
group$4(lastArg, { shouldBreak: true }),
|
|
1323
|
+
")"
|
|
1324
|
+
], allArgsBrokenOut()])];
|
|
1325
|
+
return conditionalGroup([
|
|
1326
|
+
[
|
|
1327
|
+
"(",
|
|
1328
|
+
...headArgs,
|
|
1329
|
+
lastArg,
|
|
1330
|
+
")"
|
|
1331
|
+
],
|
|
1332
|
+
[
|
|
1333
|
+
"(",
|
|
1334
|
+
...headArgs,
|
|
1335
|
+
group$4(lastArg, { shouldBreak: true }),
|
|
1336
|
+
")"
|
|
1337
|
+
],
|
|
1338
|
+
allArgsBrokenOut()
|
|
1339
|
+
]);
|
|
1340
|
+
}
|
|
1341
|
+
return group$4(indentInParentheses(printedArguments), { shouldBreak: printedArguments.some(willBreak) || anyArgEmptyLine });
|
|
1342
|
+
},
|
|
1343
|
+
array_creation_expression(path, print) {
|
|
1344
|
+
const parts = ["new "];
|
|
1345
|
+
path.each((child) => {
|
|
1346
|
+
if (child.node.type === "annotation" || child.node.type === "marker_annotation") parts.push(print(child), " ");
|
|
1347
|
+
}, "namedChildren");
|
|
1348
|
+
parts.push(path.call(print, "typeNode"), ...path.map(print, "dimensionsNodes"));
|
|
1349
|
+
if (hasChild(path, "valueNode")) parts.push(" ", path.call(print, "valueNode"));
|
|
1350
|
+
return parts;
|
|
1351
|
+
},
|
|
1352
|
+
dimensions_expr(path, print) {
|
|
1353
|
+
return path.map((child) => child.node.type === "annotation" || child.node.type === "marker_annotation" ? [print(child), " "] : [
|
|
1354
|
+
"[",
|
|
1355
|
+
print(child),
|
|
1356
|
+
"]"
|
|
1357
|
+
], "namedChildren");
|
|
1358
|
+
},
|
|
1359
|
+
class_literal(path, print) {
|
|
1360
|
+
return [path.call(print, "namedChildren", 0), ".class"];
|
|
1361
|
+
},
|
|
1362
|
+
array_access: printMemberChain,
|
|
1363
|
+
method_reference(path, print) {
|
|
1364
|
+
return group$4(path.map(print, "children"));
|
|
1365
|
+
},
|
|
1366
|
+
template_expression(path, print) {
|
|
1367
|
+
return [
|
|
1368
|
+
path.call(print, "template_processorNode"),
|
|
1369
|
+
".",
|
|
1370
|
+
path.call(print, "template_argumentNode")
|
|
1371
|
+
];
|
|
1372
|
+
},
|
|
1373
|
+
pattern(path, print) {
|
|
1374
|
+
return path.call(print, "namedChildren", 0);
|
|
1375
|
+
},
|
|
1376
|
+
type_pattern(path, print) {
|
|
1377
|
+
return join$4(" ", path.map(print, "children"));
|
|
1378
|
+
},
|
|
1379
|
+
record_pattern(path, print) {
|
|
1380
|
+
return path.map(print, "children");
|
|
1381
|
+
},
|
|
1382
|
+
record_pattern_body(path, print) {
|
|
1383
|
+
return group$4(indentInParentheses(join$4([",", line$4], path.map(print, "namedChildren"))));
|
|
1384
|
+
},
|
|
1385
|
+
record_pattern_component(path, print) {
|
|
1386
|
+
return join$4(" ", path.map(print, "children"));
|
|
1387
|
+
},
|
|
1388
|
+
guard(path, print) {
|
|
1389
|
+
return ["when ", group$4([
|
|
1390
|
+
ifBreak("("),
|
|
1391
|
+
indent$4([softline$1, path.call(print, "namedChildren", 0)]),
|
|
1392
|
+
softline$1,
|
|
1393
|
+
ifBreak(")")
|
|
1394
|
+
])];
|
|
1395
|
+
}
|
|
1396
|
+
};
|
|
1397
|
+
function printLambdaExpressionSignature(path, options, print, args) {
|
|
1398
|
+
const parts = [];
|
|
1399
|
+
const parameters = path.call(print, "parametersNode");
|
|
1400
|
+
if (shouldPrintParamsWithoutParens(path, options)) parts.push(parameters);
|
|
1401
|
+
else {
|
|
1402
|
+
if (args != null && typeof args === "object" && ("expandLastArg" in args && args.expandLastArg === true || "expandFirstArg" in args && args.expandFirstArg === true)) {
|
|
1403
|
+
if (willBreak(parameters)) throw new ArgExpansionBailout();
|
|
1404
|
+
parts.push(group$4(removeLines(parameters)));
|
|
1405
|
+
} else parts.push(parameters);
|
|
1406
|
+
if (path.node.parametersNode.type === "identifier") return path.node.parametersNode.comments ? group$4([
|
|
1407
|
+
"(",
|
|
1408
|
+
indent$4([softline$1, ...parts]),
|
|
1409
|
+
lineSuffixBoundary,
|
|
1410
|
+
")"
|
|
1411
|
+
]) : [
|
|
1412
|
+
"(",
|
|
1413
|
+
...parts,
|
|
1414
|
+
")"
|
|
1415
|
+
];
|
|
1416
|
+
}
|
|
1417
|
+
const dangling = printDanglingComments(path);
|
|
1418
|
+
if (dangling) parts.push(" ", dangling);
|
|
1419
|
+
return parts;
|
|
1420
|
+
}
|
|
1421
|
+
function mayBreakAfterShortPrefix(functionBody) {
|
|
1422
|
+
return functionBody.type === "array_creation_expression" || functionBody.type === "lambda_expression" || functionBody.type === "block";
|
|
1423
|
+
}
|
|
1424
|
+
function printArrowFunctionSignatures(path, { signatureDocs }) {
|
|
1425
|
+
if (signatureDocs.length === 1) return signatureDocs[0];
|
|
1426
|
+
const { node, parent } = path;
|
|
1427
|
+
if (node.fieldName !== "object" && parent?.type === "method_invocation" || parent?.type === "binary_expression") return group$4([
|
|
1428
|
+
signatureDocs[0],
|
|
1429
|
+
" ->",
|
|
1430
|
+
indent$4([line$4, join$4([" ->", line$4], signatureDocs.slice(1))])
|
|
1431
|
+
]);
|
|
1432
|
+
if (node.fieldName === "object" && parent?.type === "method_invocation") return group$4(join$4([" ->", line$4], signatureDocs));
|
|
1433
|
+
return group$4(indent$4(join$4([" ->", line$4], signatureDocs)));
|
|
1434
|
+
}
|
|
1435
|
+
function printArrowFunctionBody(path, args, { bodyDoc, bodyComments, shouldPutBodyOnSameLine }) {
|
|
1436
|
+
const trailingSpace = args && typeof args === "object" && "expandLastArg" in args && args.expandLastArg && !path.node.comments ? softline$1 : "";
|
|
1437
|
+
return shouldPutBodyOnSameLine ? [
|
|
1438
|
+
" ",
|
|
1439
|
+
bodyDoc,
|
|
1440
|
+
bodyComments
|
|
1441
|
+
] : [indent$4([
|
|
1442
|
+
line$4,
|
|
1443
|
+
bodyDoc,
|
|
1444
|
+
bodyComments
|
|
1445
|
+
]), trailingSpace];
|
|
1446
|
+
}
|
|
1447
|
+
function shouldPrintParamsWithoutParens(path, options) {
|
|
1448
|
+
if (options.arrowParens === "always") return false;
|
|
1449
|
+
if (options.arrowParens === "avoid") {
|
|
1450
|
+
const { node } = path;
|
|
1451
|
+
return canPrintParamsWithoutParens(node);
|
|
1452
|
+
}
|
|
1453
|
+
return false;
|
|
1454
|
+
}
|
|
1455
|
+
function canPrintParamsWithoutParens(node) {
|
|
1456
|
+
return node.parametersNode.type === "identifier" && !node.comments?.some(({ leading, trailing }) => !leading && !trailing) && !node.parametersNode.comments;
|
|
1457
|
+
}
|
|
1458
|
+
function printMemberChain(path, print, options) {
|
|
1459
|
+
const isExpressionStatement = path.parent?.type === "expression_statement";
|
|
1460
|
+
const printedNodes = [];
|
|
1461
|
+
function shouldInsertEmptyLineAfter(node) {
|
|
1462
|
+
const { originalText } = options;
|
|
1463
|
+
const nextCharIndex = getNextNonSpaceNonCommentCharacterIndex(originalText, node.end.index);
|
|
1464
|
+
if ((nextCharIndex ? originalText.charAt(nextCharIndex) : "") === ")") return nextCharIndex !== false && isNextLineEmpty(originalText, nextCharIndex + 1);
|
|
1465
|
+
return isNextLineEmpty(originalText, node.end.index);
|
|
1466
|
+
}
|
|
1467
|
+
function rec(path) {
|
|
1468
|
+
const { node } = path;
|
|
1469
|
+
if (hasType(path, "method_invocation") && hasChild(path, "objectNode")) {
|
|
1470
|
+
const hasTrailingEmptyLine = shouldInsertEmptyLineAfter(node);
|
|
1471
|
+
printedNodes.unshift({
|
|
1472
|
+
node,
|
|
1473
|
+
hasTrailingEmptyLine,
|
|
1474
|
+
printed: [printComments(path, printMethodInvocation(path, print)), hasTrailingEmptyLine ? hardline$4 : ""]
|
|
1475
|
+
});
|
|
1476
|
+
path.call(rec, "objectNode");
|
|
1477
|
+
} else if (hasType(path, "array_access")) {
|
|
1478
|
+
printedNodes.unshift({
|
|
1479
|
+
node,
|
|
1480
|
+
printed: printComments(path, printArrayAccess(path, print))
|
|
1481
|
+
});
|
|
1482
|
+
path.call(rec, "arrayNode");
|
|
1483
|
+
} else if (hasType(path, "field_access")) {
|
|
1484
|
+
printedNodes.unshift({
|
|
1485
|
+
node,
|
|
1486
|
+
printed: printComments(path, printFieldAccess(path, print))
|
|
1487
|
+
});
|
|
1488
|
+
path.call(rec, "objectNode");
|
|
1489
|
+
} else printedNodes.unshift({
|
|
1490
|
+
node,
|
|
1491
|
+
printed: print(path)
|
|
1492
|
+
});
|
|
1493
|
+
}
|
|
1494
|
+
const { node } = path;
|
|
1495
|
+
if (hasType(path, "method_invocation")) {
|
|
1496
|
+
printedNodes.unshift({
|
|
1497
|
+
node,
|
|
1498
|
+
printed: printComments(path, printMethodInvocation(path, print))
|
|
1499
|
+
});
|
|
1500
|
+
if (hasChild(path, "objectNode")) path.call(rec, "objectNode");
|
|
1501
|
+
} else if (hasType(path, "array_access")) {
|
|
1502
|
+
printedNodes.unshift({
|
|
1503
|
+
node,
|
|
1504
|
+
printed: printComments(path, printArrayAccess(path, print))
|
|
1505
|
+
});
|
|
1506
|
+
if (hasChild(path, "arrayNode")) path.call(rec, "arrayNode");
|
|
1507
|
+
} else if (hasType(path, "field_access")) {
|
|
1508
|
+
printedNodes.unshift({
|
|
1509
|
+
node,
|
|
1510
|
+
printed: printComments(path, printFieldAccess(path, print))
|
|
1511
|
+
});
|
|
1512
|
+
if (hasChild(path, "objectNode")) path.call(rec, "objectNode");
|
|
1513
|
+
}
|
|
1514
|
+
const danglingComments = printDanglingComments(path);
|
|
1515
|
+
if (danglingComments) printedNodes[0].printed = [
|
|
1516
|
+
danglingComments,
|
|
1517
|
+
hardline$4,
|
|
1518
|
+
printedNodes[0].printed
|
|
1519
|
+
];
|
|
1520
|
+
const groups = [];
|
|
1521
|
+
let currentGroup = [printedNodes[0]];
|
|
1522
|
+
let i = 1;
|
|
1523
|
+
for (; i < printedNodes.length; ++i) if (printedNodes[i].node.type === "array_access") currentGroup.push(printedNodes[i]);
|
|
1524
|
+
else break;
|
|
1525
|
+
if (printedNodes[0].node.type !== "method_invocation") for (; i + 1 < printedNodes.length; ++i) if (printedNodes[i].node.type !== "method_invocation") currentGroup.push(printedNodes[i]);
|
|
1526
|
+
else break;
|
|
1527
|
+
groups.push(currentGroup);
|
|
1528
|
+
currentGroup = [];
|
|
1529
|
+
let hasSeenMethodInvocation = false;
|
|
1530
|
+
for (; i < printedNodes.length; ++i) {
|
|
1531
|
+
if (hasSeenMethodInvocation) {
|
|
1532
|
+
if (printedNodes[i].node.type === "array_access") {
|
|
1533
|
+
currentGroup.push(printedNodes[i]);
|
|
1534
|
+
continue;
|
|
1535
|
+
}
|
|
1536
|
+
groups.push(currentGroup);
|
|
1537
|
+
currentGroup = [];
|
|
1538
|
+
hasSeenMethodInvocation = false;
|
|
1539
|
+
}
|
|
1540
|
+
if (printedNodes[i].node.type === "method_invocation") hasSeenMethodInvocation = true;
|
|
1541
|
+
currentGroup.push(printedNodes[i]);
|
|
1542
|
+
if (printedNodes[i].node.comments?.some(({ trailing }) => trailing)) {
|
|
1543
|
+
groups.push(currentGroup);
|
|
1544
|
+
currentGroup = [];
|
|
1545
|
+
hasSeenMethodInvocation = false;
|
|
1546
|
+
}
|
|
1547
|
+
}
|
|
1548
|
+
if (currentGroup.length > 0) groups.push(currentGroup);
|
|
1549
|
+
function isFactory(name) {
|
|
1550
|
+
return /^[A-Z]|^[$_]+$/.test(name);
|
|
1551
|
+
}
|
|
1552
|
+
function isShort(name) {
|
|
1553
|
+
return name.length <= options.tabWidth;
|
|
1554
|
+
}
|
|
1555
|
+
function shouldNotWrap(groups) {
|
|
1556
|
+
const hasArrayAccess = groups[1][0]?.node.type === "array_access";
|
|
1557
|
+
if (groups[0].length === 1) {
|
|
1558
|
+
const firstNode = groups[0][0].node;
|
|
1559
|
+
return firstNode.type === "this" || firstNode.type === "identifier" && (isFactory(firstNode.value) || isExpressionStatement && isShort(firstNode.value) || hasArrayAccess);
|
|
1560
|
+
}
|
|
1561
|
+
const lastNode = groups[0].at(-1).node;
|
|
1562
|
+
return lastNode.type === "field_access" && lastNode.fieldNode.type === "identifier" && (isFactory(lastNode.fieldNode.value) || hasArrayAccess);
|
|
1563
|
+
}
|
|
1564
|
+
const shouldMerge = groups.length >= 2 && !groups[1][0].node.comments?.length && shouldNotWrap(groups);
|
|
1565
|
+
function printGroup(printedGroup) {
|
|
1566
|
+
return printedGroup.map((tuple) => tuple.printed);
|
|
1567
|
+
}
|
|
1568
|
+
function printIndentedGroup(groups) {
|
|
1569
|
+
if (groups.length === 0) return "";
|
|
1570
|
+
return indent$4([hardline$4, join$4(hardline$4, groups.map(printGroup))]);
|
|
1571
|
+
}
|
|
1572
|
+
const printedGroups = groups.map(printGroup);
|
|
1573
|
+
const oneLine = printedGroups;
|
|
1574
|
+
const cutoff = shouldMerge ? 3 : 2;
|
|
1575
|
+
const flatGroups = groups.flat();
|
|
1576
|
+
const nodeHasComment = flatGroups.some((node) => node.node.comments?.some(({ leading }) => leading)) || flatGroups.slice(0, -1).some((node) => node.node.comments?.some(({ trailing }) => trailing));
|
|
1577
|
+
if (groups.length <= cutoff && !nodeHasComment && !groups.some((g) => g.at(-1).hasTrailingEmptyLine)) return group$4(oneLine);
|
|
1578
|
+
const lastNodeBeforeIndent = groups[shouldMerge ? 1 : 0].at(-1).node;
|
|
1579
|
+
const shouldHaveEmptyLineBeforeIndent = lastNodeBeforeIndent.type !== "method_invocation" && shouldInsertEmptyLineAfter(lastNodeBeforeIndent);
|
|
1580
|
+
const expanded = [
|
|
1581
|
+
printGroup(groups[0]),
|
|
1582
|
+
shouldMerge ? groups.slice(1, 2).map(printGroup) : "",
|
|
1583
|
+
shouldHaveEmptyLineBeforeIndent ? hardline$4 : "",
|
|
1584
|
+
printIndentedGroup(groups.slice(shouldMerge ? 2 : 1))
|
|
1585
|
+
];
|
|
1586
|
+
const methodInvocations = printedNodes.map(({ node }) => node).filter((node) => node.type === "method_invocation");
|
|
1587
|
+
function lastGroupWillBreakAndOtherCallsHaveFunctionArguments() {
|
|
1588
|
+
const lastGroupNode = groups.at(-1).at(-1).node;
|
|
1589
|
+
const lastGroupDoc = printedGroups.at(-1);
|
|
1590
|
+
return lastGroupNode.type === "method_invocation" && willBreak(lastGroupDoc) && methodInvocations.slice(0, -1).some((node) => node.argumentsNode.namedChildren.some(({ type }) => type === "lambda_expression"));
|
|
1591
|
+
}
|
|
1592
|
+
let result;
|
|
1593
|
+
if (nodeHasComment || methodInvocations.length > 2 && methodInvocations.some((inv) => !inv.argumentsNode.namedChildren.every((arg) => isSimpleCallArgument(arg))) || printedGroups.slice(0, -1).some(willBreak) || lastGroupWillBreakAndOtherCallsHaveFunctionArguments()) result = group$4(expanded);
|
|
1594
|
+
else result = [willBreak(oneLine) || shouldHaveEmptyLineBeforeIndent ? breakParent$1 : "", conditionalGroup([oneLine, expanded])];
|
|
1595
|
+
return result;
|
|
1596
|
+
}
|
|
1597
|
+
function printMethodInvocation(path, print) {
|
|
1598
|
+
const parts = [];
|
|
1599
|
+
if (hasChild(path, "objectNode")) parts.push(".");
|
|
1600
|
+
if (path.node.children.filter(({ type }) => type === ".").length === 2) parts.push("super", ".");
|
|
1601
|
+
if (hasChild(path, "type_argumentsNode")) parts.push(path.call(print, "type_argumentsNode"));
|
|
1602
|
+
parts.push(path.call(print, "nameNode"), path.call(print, "argumentsNode"));
|
|
1603
|
+
return parts;
|
|
1604
|
+
}
|
|
1605
|
+
function printArrayAccess(path, print) {
|
|
1606
|
+
const index = path.call(print, "indexNode");
|
|
1607
|
+
return path.node.indexNode.type === "decimal_integer_literal" ? [
|
|
1608
|
+
"[",
|
|
1609
|
+
index,
|
|
1610
|
+
"]"
|
|
1611
|
+
] : group$4([
|
|
1612
|
+
"[",
|
|
1613
|
+
indent$4([softline$1, index]),
|
|
1614
|
+
softline$1,
|
|
1615
|
+
"]"
|
|
1616
|
+
]);
|
|
1617
|
+
}
|
|
1618
|
+
function printFieldAccess(path, print) {
|
|
1619
|
+
const parts = ["."];
|
|
1620
|
+
if (path.node.children.filter(({ type }) => type === ".").length === 2) parts.push("super.");
|
|
1621
|
+
parts.push(path.call(print, "fieldNode"));
|
|
1622
|
+
return parts;
|
|
1623
|
+
}
|
|
1624
|
+
/**
|
|
1625
|
+
* For binary expressions to be consistent, we need to group
|
|
1626
|
+
* subsequent operators with the same precedence level under a single
|
|
1627
|
+
* group. Otherwise they will be nested such that some of them break
|
|
1628
|
+
* onto new lines but not all. Operators with the same precedence
|
|
1629
|
+
* level should either all break or not. Because we group them by
|
|
1630
|
+
* precedence level and the AST is structured based on precedence
|
|
1631
|
+
* level, things are naturally broken up correctly, i.e. `&&` is
|
|
1632
|
+
* broken before `+`.
|
|
1633
|
+
*/
|
|
1634
|
+
function printBinaryExpressions(path, print, options, isInsideParentheses) {
|
|
1635
|
+
if (!hasType(path, "binary_expression")) return [group$4(print(path))];
|
|
1636
|
+
const { node } = path;
|
|
1637
|
+
let parts = [];
|
|
1638
|
+
if (node.leftNode.type === "binary_expression" && shouldFlatten(node.operatorNode.type, node.leftNode.operatorNode.type)) parts = path.call(() => printBinaryExpressions(path, print, options, isInsideParentheses), "leftNode");
|
|
1639
|
+
else parts.push(group$4(path.call(print, "leftNode")));
|
|
1640
|
+
const operator = node.operatorNode.type;
|
|
1641
|
+
const operatorDoc = path.call(print, "operatorNode");
|
|
1642
|
+
const rightContent = path.call(print, "rightNode");
|
|
1643
|
+
let right = options.experimentalOperatorPosition === "start" ? [
|
|
1644
|
+
line$4,
|
|
1645
|
+
operatorDoc,
|
|
1646
|
+
" ",
|
|
1647
|
+
rightContent
|
|
1648
|
+
] : [
|
|
1649
|
+
" ",
|
|
1650
|
+
operatorDoc,
|
|
1651
|
+
line$4,
|
|
1652
|
+
rightContent
|
|
1653
|
+
];
|
|
1654
|
+
const { parent } = path;
|
|
1655
|
+
const shouldBreak = node.leftNode.comments?.some(({ trailing, type }) => trailing && type === "line_comment") ?? false;
|
|
1656
|
+
if (shouldBreak || !(isInsideParentheses && logicalOperators.has(operator)) && (parent?.type !== node.type || logicalOperators.has(parent.operatorNode.value) !== logicalOperators.has(operator)) && node.leftNode.type !== node.type && node.rightNode.type !== node.type) right = group$4(right, { shouldBreak });
|
|
1657
|
+
parts.push(right);
|
|
1658
|
+
return parts;
|
|
1659
|
+
}
|
|
1660
|
+
const logicalOperators = /* @__PURE__ */ new Set(["||", "&&"]);
|
|
1661
|
+
function isSimpleCallArgument(node, depth = 2) {
|
|
1662
|
+
if (depth <= 0) return false;
|
|
1663
|
+
const isChildSimple = (child) => isSimpleCallArgument(child, depth - 1);
|
|
1664
|
+
if (isLiteral(node) || isSingleWordType(node) || node.type === "class_literal") return true;
|
|
1665
|
+
if (node.type === "object_creation_expression" || node.type === "method_invocation") {
|
|
1666
|
+
if (node.type === "object_creation_expression" || !node.objectNode || isSimpleCallArgument(node.objectNode, depth)) {
|
|
1667
|
+
const args = node.argumentsNode.namedChildren;
|
|
1668
|
+
return args.length <= depth && args.every(isChildSimple);
|
|
1669
|
+
}
|
|
1670
|
+
return false;
|
|
1671
|
+
}
|
|
1672
|
+
if (node.type === "array_access") return isSimpleCallArgument(node.arrayNode, depth) && isSimpleCallArgument(node.indexNode, depth);
|
|
1673
|
+
if (node.type === "field_access") return isSimpleCallArgument(node.objectNode, depth) && isSimpleCallArgument(node.fieldNode, depth);
|
|
1674
|
+
if (node.type === "unary_expression") return isSimpleCallArgument(node.operandNode, depth);
|
|
1675
|
+
if (node.type === "method_reference" || node.type === "update_expression") return isSimpleCallArgument(node.namedChildren[0], depth);
|
|
1676
|
+
return false;
|
|
1677
|
+
}
|
|
1678
|
+
function isLiteral(node) {
|
|
1679
|
+
return [
|
|
1680
|
+
"true",
|
|
1681
|
+
"false",
|
|
1682
|
+
"null_literal",
|
|
1683
|
+
"binary_integer_literal",
|
|
1684
|
+
"decimal_floating_point_literal",
|
|
1685
|
+
"decimal_integer_literal",
|
|
1686
|
+
"hex_floating_point_literal",
|
|
1687
|
+
"hex_integer_literal",
|
|
1688
|
+
"octal_integer_literal",
|
|
1689
|
+
"character_literal"
|
|
1690
|
+
].includes(node.type) || isStringLiteral(node);
|
|
1691
|
+
}
|
|
1692
|
+
function isSingleWordType(node) {
|
|
1693
|
+
return [
|
|
1694
|
+
"identifier",
|
|
1695
|
+
"this",
|
|
1696
|
+
"super"
|
|
1697
|
+
].includes(node.type);
|
|
1698
|
+
}
|
|
1699
|
+
function couldExpandArg(arg, lambdaChainRecursion = false) {
|
|
1700
|
+
if (arg.type === "array_creation_expression" && arg.valueNode && (arg.valueNode.namedChildren.length > 0 || arg.valueNode.comments)) return true;
|
|
1701
|
+
if (arg.type === "lambda_expression") {
|
|
1702
|
+
const { bodyNode: body } = arg;
|
|
1703
|
+
if (body.type === "block" || body.type === "array_creation_expression") return true;
|
|
1704
|
+
if (body.type === "lambda_expression" && couldExpandArg(body, true)) return true;
|
|
1705
|
+
if (!lambdaChainRecursion) {
|
|
1706
|
+
if (body.type === "ternary_expression") return true;
|
|
1707
|
+
if (body.type === "method_invocation" || body.type === "object_creation_expression") return true;
|
|
1708
|
+
}
|
|
1709
|
+
}
|
|
1710
|
+
return false;
|
|
1711
|
+
}
|
|
1712
|
+
function shouldExpandLastArg(args) {
|
|
1713
|
+
const lastArg = args.at(-1);
|
|
1714
|
+
const penultimateArg = args.at(-2);
|
|
1715
|
+
return !lastArg.comments?.some(({ leading }) => leading) && !lastArg.comments?.some(({ trailing }) => trailing) && couldExpandArg(lastArg) && (!penultimateArg || penultimateArg.type !== lastArg.type) && (args.length !== 2 || penultimateArg.type !== "lambda_expression");
|
|
1716
|
+
}
|
|
1717
|
+
function shouldExpandFirstArg(args) {
|
|
1718
|
+
if (args.length !== 2) return false;
|
|
1719
|
+
const [firstArg, secondArg] = args;
|
|
1720
|
+
return !firstArg.comments && firstArg.type === "lambda_expression" && firstArg.bodyNode.type === "block" && secondArg.type !== "lambda_expression" && secondArg.type !== "ternary_expression" && isHopefullyShortCallArgument(secondArg) && !couldExpandArg(secondArg);
|
|
1721
|
+
}
|
|
1722
|
+
function isHopefullyShortCallArgument(node) {
|
|
1723
|
+
if ((node.type === "method_invocation" || node.type === "object_creation_expression") && node.argumentsNode.namedChildren.length > 1) return false;
|
|
1724
|
+
if (node.type === "binary_expression") return isSimpleCallArgument(node.leftNode, 1) && isSimpleCallArgument(node.rightNode, 1);
|
|
1725
|
+
return isSimpleCallArgument(node);
|
|
1726
|
+
}
|
|
1727
|
+
function printTernaryTest(path, print) {
|
|
1728
|
+
const { node, parent } = path;
|
|
1729
|
+
const printed = path.call(print, "conditionNode");
|
|
1730
|
+
/**
|
|
1731
|
+
* a
|
|
1732
|
+
* ? b
|
|
1733
|
+
* : multiline
|
|
1734
|
+
* test
|
|
1735
|
+
* node
|
|
1736
|
+
* ^^ align(2)
|
|
1737
|
+
* ? d
|
|
1738
|
+
* : e
|
|
1739
|
+
*/
|
|
1740
|
+
if (parent?.type === node.type && parent.alternativeNode === node) return align(2, printed);
|
|
1741
|
+
return printed;
|
|
1742
|
+
}
|
|
1743
|
+
function getExpressionChild(node) {
|
|
1744
|
+
switch (node.type) {
|
|
1745
|
+
case "assignment_expression": return node.rightNode;
|
|
1746
|
+
case "variable_declarator": return node.valueNode;
|
|
1747
|
+
case "unary_expression": return node.operandNode;
|
|
1748
|
+
case "return_statement":
|
|
1749
|
+
case "throw_statement":
|
|
1750
|
+
case "yield_statement": return node.namedChildren[0];
|
|
1751
|
+
default: return null;
|
|
1752
|
+
}
|
|
1753
|
+
}
|
|
1754
|
+
function shouldExtraIndentForTernaryExpression(path) {
|
|
1755
|
+
const { node } = path;
|
|
1756
|
+
let parent;
|
|
1757
|
+
let child = node;
|
|
1758
|
+
for (let ancestorCount = 0; !parent; ancestorCount++) {
|
|
1759
|
+
const node = path.getParentNode(ancestorCount);
|
|
1760
|
+
if (node.type === "array_access" && node.arrayNode === child || (node.type === "explicit_constructor_invocation" || node.type === "field_access" || node.type === "method_invocation") && node.objectNode === child || node.type === "method_reference" || node.type === "object_creation_expression") {
|
|
1761
|
+
child = node;
|
|
1762
|
+
continue;
|
|
1763
|
+
}
|
|
1764
|
+
if (node.type === "cast_expression" && node.valueNode === child) {
|
|
1765
|
+
parent = path.getParentNode(ancestorCount + 1);
|
|
1766
|
+
child = node;
|
|
1767
|
+
} else parent = node;
|
|
1768
|
+
}
|
|
1769
|
+
if (child === node) return false;
|
|
1770
|
+
return getExpressionChild(parent) === child;
|
|
1771
|
+
}
|
|
1772
|
+
function isStringLiteral(node) {
|
|
1773
|
+
return node.type === "string_literal" && node.children[0].value === "\"";
|
|
1774
|
+
}
|
|
1775
|
+
var ArgExpansionBailout = class extends Error {
|
|
1776
|
+
constructor(..._args) {
|
|
1777
|
+
super(..._args);
|
|
1778
|
+
this.name = "ArgExpansionBailout";
|
|
1779
|
+
}
|
|
1780
|
+
};
|
|
1781
|
+
//#endregion
|
|
1782
|
+
//#region src/printers/interfaces.ts
|
|
1783
|
+
const { group: group$3, hardline: hardline$3, indent: indent$3, join: join$3, line: line$3 } = prettier_doc.builders;
|
|
1784
|
+
var interfaces_default = {
|
|
1785
|
+
interface_declaration(path, print, options) {
|
|
1786
|
+
const parts = ["interface ", path.call(print, "nameNode")];
|
|
1787
|
+
const extendsInterfacesIndex = path.node.namedChildren.findIndex(({ type }) => type === "extends_interfaces");
|
|
1788
|
+
const hasExtendsInterfaces = extendsInterfacesIndex !== -1;
|
|
1789
|
+
const hasPermits = hasChild(path, "permitsNode");
|
|
1790
|
+
const hasMultipleClauses = hasExtendsInterfaces && hasPermits;
|
|
1791
|
+
if (hasChild(path, "type_parametersNode")) {
|
|
1792
|
+
const typeParameters = path.call(print, "type_parametersNode");
|
|
1793
|
+
parts.push(hasMultipleClauses ? group$3(indent$3(typeParameters)) : typeParameters);
|
|
1794
|
+
}
|
|
1795
|
+
if (hasExtendsInterfaces || hasPermits) {
|
|
1796
|
+
const clauses = [];
|
|
1797
|
+
if (hasExtendsInterfaces) clauses.push(hardline$3, path.call(print, "namedChildren", extendsInterfacesIndex));
|
|
1798
|
+
if (hasPermits) clauses.push(line$3, path.call(print, "permitsNode"));
|
|
1799
|
+
const hasBody = path.node.bodyNode.namedChildren.length > 0;
|
|
1800
|
+
const clauseGroup = [indent$3(clauses), options?.braceStyle === "next-line" ? "" : hasBody ? line$3 : " "];
|
|
1801
|
+
parts.push(hasMultipleClauses ? clauseGroup : group$3(clauseGroup));
|
|
1802
|
+
} else parts.push(" ");
|
|
1803
|
+
return [
|
|
1804
|
+
...printModifiers(path, print, "declarationOnly"),
|
|
1805
|
+
group$3(parts),
|
|
1806
|
+
path.call(print, "bodyNode")
|
|
1807
|
+
];
|
|
1808
|
+
},
|
|
1809
|
+
extends_interfaces(path, print) {
|
|
1810
|
+
const typeListIndex = path.node.namedChildren.findIndex(({ type }) => type === "type_list");
|
|
1811
|
+
return group$3(["extends", indent$3([line$3, path.call(print, "namedChildren", typeListIndex)])]);
|
|
1812
|
+
},
|
|
1813
|
+
interface_body(path, print, options) {
|
|
1814
|
+
return printBlock(path, printBodyDeclarations(path, print), options);
|
|
1815
|
+
},
|
|
1816
|
+
constant_declaration: printVariableDeclaration,
|
|
1817
|
+
annotation_type_declaration(path, print) {
|
|
1818
|
+
const parts = printModifiers(path, print);
|
|
1819
|
+
parts.push("@interface ", path.call(print, "nameNode"), " ", path.call(print, "bodyNode"));
|
|
1820
|
+
return parts;
|
|
1821
|
+
},
|
|
1822
|
+
annotation_type_body(path, print, options) {
|
|
1823
|
+
return printBlock(path, printBodyDeclarations(path, print), options);
|
|
1824
|
+
},
|
|
1825
|
+
annotation_type_element_declaration(path, print) {
|
|
1826
|
+
const parts = printModifiers(path, print);
|
|
1827
|
+
parts.push(path.call(print, "typeNode"), " ", path.call(print, "nameNode"), "()");
|
|
1828
|
+
if (hasChild(path, "dimensionsNode")) parts.push(path.call(print, "dimensionsNode"));
|
|
1829
|
+
if (hasChild(path, "valueNode")) parts.push(" default ", path.call(print, "valueNode"));
|
|
1830
|
+
parts.push(";");
|
|
1831
|
+
return parts;
|
|
1832
|
+
},
|
|
1833
|
+
annotation(path, print) {
|
|
1834
|
+
return [
|
|
1835
|
+
"@",
|
|
1836
|
+
path.call(print, "nameNode"),
|
|
1837
|
+
path.call(print, "argumentsNode")
|
|
1838
|
+
];
|
|
1839
|
+
},
|
|
1840
|
+
marker_annotation(path, print) {
|
|
1841
|
+
return ["@", path.call(print, "nameNode")];
|
|
1842
|
+
},
|
|
1843
|
+
annotation_argument_list(path, print) {
|
|
1844
|
+
const args = path.map(print, "namedChildren");
|
|
1845
|
+
return args.length === 1 && path.node.namedChildren[0].type === "element_value_array_initializer" ? [
|
|
1846
|
+
"(",
|
|
1847
|
+
args[0],
|
|
1848
|
+
")"
|
|
1849
|
+
] : group$3(indentInParentheses(join$3([",", line$3], args)));
|
|
1850
|
+
},
|
|
1851
|
+
element_value_pair(path, print) {
|
|
1852
|
+
return group$3([
|
|
1853
|
+
path.call(print, "keyNode"),
|
|
1854
|
+
" = ",
|
|
1855
|
+
path.call(print, "valueNode")
|
|
1856
|
+
]);
|
|
1857
|
+
},
|
|
1858
|
+
element_value_array_initializer: printArrayInitializer
|
|
1859
|
+
};
|
|
1860
|
+
//#endregion
|
|
1861
|
+
//#region src/printers/lexical-structure.ts
|
|
1862
|
+
const { group: group$2, hardline: hardline$2, indent: indent$2, join: join$2, softline } = prettier_doc.builders;
|
|
1863
|
+
var lexical_structure_default = {
|
|
1864
|
+
string_literal(path, print) {
|
|
1865
|
+
if (path.node.namedChildren.some(({ type }) => type === "string_interpolation") || path.node.children[0].value === "\"") return path.map(print, "children");
|
|
1866
|
+
return printTextBlock(join$2(hardline$2, textBlockContents(path.node).split("\n")));
|
|
1867
|
+
},
|
|
1868
|
+
string_fragment: printValue,
|
|
1869
|
+
multiline_string_fragment: printValue,
|
|
1870
|
+
string_interpolation(path, print) {
|
|
1871
|
+
const expressionType = path.node.namedChildren[0].type;
|
|
1872
|
+
const expression = path.call(print, "namedChildren", 0);
|
|
1873
|
+
return expressionType === "binary_expression" || expressionType === "ternary_expression" ? group$2([
|
|
1874
|
+
"\\{",
|
|
1875
|
+
indent$2([softline, expression]),
|
|
1876
|
+
softline,
|
|
1877
|
+
"}"
|
|
1878
|
+
]) : [
|
|
1879
|
+
"\\{",
|
|
1880
|
+
expression,
|
|
1881
|
+
"}"
|
|
1882
|
+
];
|
|
1883
|
+
},
|
|
1884
|
+
escape_sequence: printValue,
|
|
1885
|
+
character_literal: printValue,
|
|
1886
|
+
binary_integer_literal: printValue,
|
|
1887
|
+
decimal_integer_literal: printValue,
|
|
1888
|
+
hex_integer_literal: printValue,
|
|
1889
|
+
octal_integer_literal: printValue,
|
|
1890
|
+
decimal_floating_point_literal: printValue,
|
|
1891
|
+
hex_floating_point_literal: printValue,
|
|
1892
|
+
null_literal: printValue,
|
|
1893
|
+
true: printValue,
|
|
1894
|
+
false: printValue,
|
|
1895
|
+
this: printValue,
|
|
1896
|
+
super: printValue,
|
|
1897
|
+
underscore_pattern: printValue,
|
|
1898
|
+
asterisk: printValue
|
|
1899
|
+
};
|
|
1900
|
+
//#endregion
|
|
1901
|
+
//#region src/printers/names.ts
|
|
1902
|
+
var names_default = {
|
|
1903
|
+
identifier: printValue,
|
|
1904
|
+
type_identifier: printValue,
|
|
1905
|
+
scoped_identifier(path, print) {
|
|
1906
|
+
return [
|
|
1907
|
+
path.call(print, "scopeNode"),
|
|
1908
|
+
".",
|
|
1909
|
+
path.call(print, "nameNode")
|
|
1910
|
+
];
|
|
1911
|
+
},
|
|
1912
|
+
scoped_type_identifier(path, print) {
|
|
1913
|
+
return path.map((child) => child.node.type === "annotation" || child.node.type === "marker_annotation" ? [print(child), " "] : print(child), "children");
|
|
1914
|
+
}
|
|
1915
|
+
};
|
|
1916
|
+
//#endregion
|
|
1917
|
+
//#region src/printers/packages-and-modules.ts
|
|
1918
|
+
const { group: group$1, hardline: hardline$1, indent: indent$1, join: join$1, line: line$2 } = prettier_doc.builders;
|
|
1919
|
+
var packages_and_modules_default = {
|
|
1920
|
+
program(path, print) {
|
|
1921
|
+
if (!path.node.namedChildren.length) return [printDanglingComments(path), hardline$1];
|
|
1922
|
+
const parts = [];
|
|
1923
|
+
if (path.node.namedChildren[0].type !== "import_declaration") parts.push(path.call(print, "namedChildren", 0));
|
|
1924
|
+
const staticImports = [];
|
|
1925
|
+
const imports = [];
|
|
1926
|
+
const otherDeclarations = [];
|
|
1927
|
+
path.each((child) => {
|
|
1928
|
+
const doc = print(child);
|
|
1929
|
+
if (child.node.type === "import_declaration") {
|
|
1930
|
+
const names = extractNames(child.node.namedChildren[0]);
|
|
1931
|
+
if (child.node.namedChildren.some(({ type }) => type === "asterisk")) names.push("*");
|
|
1932
|
+
(child.node.children[1].type === "static" ? staticImports : imports).push({
|
|
1933
|
+
doc,
|
|
1934
|
+
names
|
|
1935
|
+
});
|
|
1936
|
+
} else if (!child.isFirst) otherDeclarations.push(doc);
|
|
1937
|
+
}, "namedChildren");
|
|
1938
|
+
parts.push(...[staticImports, imports].filter((imports) => imports.length).map((imports) => join$1(hardline$1, imports.sort(compareFqn).map(({ doc }) => doc))), ...otherDeclarations);
|
|
1939
|
+
return [...join$1([hardline$1, hardline$1], parts), hardline$1];
|
|
1940
|
+
},
|
|
1941
|
+
package_declaration(path, print) {
|
|
1942
|
+
const annotations = [];
|
|
1943
|
+
const identifier = [];
|
|
1944
|
+
path.each((child) => {
|
|
1945
|
+
switch (child.node.type) {
|
|
1946
|
+
case "annotation":
|
|
1947
|
+
case "marker_annotation":
|
|
1948
|
+
annotations.push(print(child));
|
|
1949
|
+
break;
|
|
1950
|
+
case "identifier":
|
|
1951
|
+
case "scoped_identifier":
|
|
1952
|
+
identifier.push(print(child));
|
|
1953
|
+
break;
|
|
1954
|
+
}
|
|
1955
|
+
}, "namedChildren");
|
|
1956
|
+
return join$1(hardline$1, [...annotations, [
|
|
1957
|
+
"package ",
|
|
1958
|
+
...identifier,
|
|
1959
|
+
";"
|
|
1960
|
+
]]);
|
|
1961
|
+
},
|
|
1962
|
+
import_declaration(path, print) {
|
|
1963
|
+
const declaration = ["import "];
|
|
1964
|
+
if (path.node.children.some(({ type }) => type === "static")) declaration.push("static ");
|
|
1965
|
+
const identifierIndex = path.node.namedChildren.findIndex(({ type }) => type === "identifier" || type === "scoped_identifier");
|
|
1966
|
+
declaration.push(path.call(print, "namedChildren", identifierIndex));
|
|
1967
|
+
if (path.node.namedChildren.some(({ type }) => type === "asterisk")) declaration.push(".*");
|
|
1968
|
+
declaration.push(";");
|
|
1969
|
+
return declaration;
|
|
1970
|
+
},
|
|
1971
|
+
module_declaration(path, print) {
|
|
1972
|
+
const parts = [];
|
|
1973
|
+
path.each((child) => {
|
|
1974
|
+
if (child.node.type === "annotation" || child.node.type === "marker_annotation") parts.push(print(child));
|
|
1975
|
+
}, "namedChildren");
|
|
1976
|
+
if (path.node.children.some(({ type }) => type === "open")) parts.push("open");
|
|
1977
|
+
parts.push("module", path.call(print, "nameNode"), path.call(print, "bodyNode"));
|
|
1978
|
+
return join$1(" ", parts);
|
|
1979
|
+
},
|
|
1980
|
+
module_body(path, print, options) {
|
|
1981
|
+
return printBlock(path, path.map((child) => child.previous && child.node.start.row > child.previous.end.row + 1 ? [hardline$1, print(child)] : print(child), "namedChildren"), options);
|
|
1982
|
+
},
|
|
1983
|
+
requires_module_directive(path, print) {
|
|
1984
|
+
const parts = ["requires"];
|
|
1985
|
+
path.each((child) => {
|
|
1986
|
+
if (child.node.type === "requires_modifier") parts.push(print(child));
|
|
1987
|
+
}, "namedChildren");
|
|
1988
|
+
parts.push(path.call(print, "moduleNode"));
|
|
1989
|
+
return [...join$1(" ", parts), ";"];
|
|
1990
|
+
},
|
|
1991
|
+
exports_module_directive: printToModuleNamesDirective,
|
|
1992
|
+
opens_module_directive: printToModuleNamesDirective,
|
|
1993
|
+
uses_module_directive(path, print) {
|
|
1994
|
+
return [
|
|
1995
|
+
"uses ",
|
|
1996
|
+
path.call(print, "typeNode"),
|
|
1997
|
+
";"
|
|
1998
|
+
];
|
|
1999
|
+
},
|
|
2000
|
+
provides_module_directive(path, print) {
|
|
2001
|
+
const [provided, ...providers] = path.map(print, "namedChildren");
|
|
2002
|
+
return [
|
|
2003
|
+
"provides ",
|
|
2004
|
+
provided,
|
|
2005
|
+
group$1(indent$1([line$2, group$1(indent$1([
|
|
2006
|
+
"with",
|
|
2007
|
+
line$2,
|
|
2008
|
+
...join$1([",", line$2], providers)
|
|
2009
|
+
]))])),
|
|
2010
|
+
";"
|
|
2011
|
+
];
|
|
2012
|
+
},
|
|
2013
|
+
requires_modifier: printValue
|
|
2014
|
+
};
|
|
2015
|
+
function extractNames(node) {
|
|
2016
|
+
return node.type === "identifier" ? [node.value] : [...extractNames(node.scopeNode), node.nameNode.value];
|
|
2017
|
+
}
|
|
2018
|
+
function compareFqn({ names: a }, { names: b }) {
|
|
2019
|
+
const minParts = Math.min(a.length, b.length);
|
|
2020
|
+
for (let i = 0; i < minParts; i++) {
|
|
2021
|
+
const imageA = a[i];
|
|
2022
|
+
const imageB = b[i];
|
|
2023
|
+
if (imageA < imageB) return -1;
|
|
2024
|
+
else if (imageA > imageB) return 1;
|
|
2025
|
+
}
|
|
2026
|
+
return a.length - b.length;
|
|
2027
|
+
}
|
|
2028
|
+
function printToModuleNamesDirective(path, print) {
|
|
2029
|
+
const directive = [
|
|
2030
|
+
path.node.type === "exports_module_directive" ? "exports" : "opens",
|
|
2031
|
+
" ",
|
|
2032
|
+
path.call(print, "packageNode")
|
|
2033
|
+
];
|
|
2034
|
+
if (path.node.modulesNodes.length) {
|
|
2035
|
+
const moduleNames = join$1([",", line$2], path.map(print, "modulesNodes"));
|
|
2036
|
+
directive.push(group$1(indent$1([line$2, group$1(indent$1([
|
|
2037
|
+
"to",
|
|
2038
|
+
line$2,
|
|
2039
|
+
...moduleNames
|
|
2040
|
+
]))])));
|
|
2041
|
+
}
|
|
2042
|
+
directive.push(";");
|
|
2043
|
+
return directive;
|
|
2044
|
+
}
|
|
2045
|
+
//#endregion
|
|
2046
|
+
//#region src/printers/types-values-and-variables.ts
|
|
2047
|
+
const { group, indent, join, line: line$1 } = prettier_doc.builders;
|
|
2048
|
+
var types_values_and_variables_default = {
|
|
2049
|
+
boolean_type: printValue,
|
|
2050
|
+
integral_type: printValue,
|
|
2051
|
+
floating_point_type: printValue,
|
|
2052
|
+
void_type: printValue,
|
|
2053
|
+
array_type(path, print) {
|
|
2054
|
+
return [path.call(print, "elementNode"), path.call(print, "dimensionsNode")];
|
|
2055
|
+
},
|
|
2056
|
+
annotated_type(path, print) {
|
|
2057
|
+
return join(" ", path.map(print, "children"));
|
|
2058
|
+
},
|
|
2059
|
+
dimensions(path, print) {
|
|
2060
|
+
return path.map((child) => {
|
|
2061
|
+
if (child.node.isNamed) return [
|
|
2062
|
+
...child.isFirst ? [" "] : [],
|
|
2063
|
+
print(child),
|
|
2064
|
+
" "
|
|
2065
|
+
];
|
|
2066
|
+
return child.node.value;
|
|
2067
|
+
}, "children");
|
|
2068
|
+
},
|
|
2069
|
+
type_parameter(path, print) {
|
|
2070
|
+
return join(" ", path.map(print, "children"));
|
|
2071
|
+
},
|
|
2072
|
+
type_bound(path, print) {
|
|
2073
|
+
const [firstType, ...restTypes] = path.map(print, "namedChildren");
|
|
2074
|
+
const bound = ["extends ", firstType];
|
|
2075
|
+
if (restTypes.length) bound.push(group(indent([
|
|
2076
|
+
line$1,
|
|
2077
|
+
"& ",
|
|
2078
|
+
...join([line$1, "& "], restTypes)
|
|
2079
|
+
])));
|
|
2080
|
+
return bound;
|
|
2081
|
+
},
|
|
2082
|
+
type_arguments: printTypeParameters,
|
|
2083
|
+
wildcard(path, print) {
|
|
2084
|
+
return join(" ", path.map(print, "children"));
|
|
2085
|
+
}
|
|
2086
|
+
};
|
|
2087
|
+
//#endregion
|
|
2088
|
+
//#region src/printers/index.ts
|
|
2089
|
+
const printersByNodeType = {
|
|
2090
|
+
ERROR(path) {
|
|
2091
|
+
throw new Error(`Failed to parse: "${printValue(path)}"`);
|
|
2092
|
+
},
|
|
2093
|
+
...arrays_default,
|
|
2094
|
+
...blocks_and_statements_default,
|
|
2095
|
+
...classes_default,
|
|
2096
|
+
...expressions_default,
|
|
2097
|
+
...interfaces_default,
|
|
2098
|
+
...lexical_structure_default,
|
|
2099
|
+
...names_default,
|
|
2100
|
+
...packages_and_modules_default,
|
|
2101
|
+
...types_values_and_variables_default
|
|
2102
|
+
};
|
|
2103
|
+
function printerForNodeType(type) {
|
|
2104
|
+
return printersByNodeType[type];
|
|
2105
|
+
}
|
|
2106
|
+
//#endregion
|
|
2107
|
+
//#region src/printer.ts
|
|
2108
|
+
var printer_default = {
|
|
2109
|
+
print(path, options, print, args) {
|
|
2110
|
+
if (!hasNamedNode(path)) return printValue(path);
|
|
2111
|
+
const doc = printerForNodeType(path.node.type)(path, print, options, args);
|
|
2112
|
+
return needsParentheses(path) ? [
|
|
2113
|
+
"(",
|
|
2114
|
+
doc,
|
|
2115
|
+
")"
|
|
2116
|
+
] : doc;
|
|
2117
|
+
},
|
|
2118
|
+
embed(path) {
|
|
2119
|
+
return hasType(path, "string_literal") ? embedTextBlock(path) : null;
|
|
2120
|
+
},
|
|
2121
|
+
hasPrettierIgnore(path) {
|
|
2122
|
+
return path.node.comments?.some(isPrettierIgnore) === true || canAttachComment(path.node, path.parent ? [path.parent] : []) && isFullyBetweenPrettierIgnore(path);
|
|
2123
|
+
},
|
|
2124
|
+
canAttachComment,
|
|
2125
|
+
isBlockComment(node) {
|
|
2126
|
+
return node.type === "block_comment";
|
|
2127
|
+
},
|
|
2128
|
+
willPrintOwnComments,
|
|
2129
|
+
printComment(commentPath) {
|
|
2130
|
+
return printComment(commentPath.node);
|
|
2131
|
+
},
|
|
2132
|
+
getCommentChildNodes(node) {
|
|
2133
|
+
return node.isNamed ? node.children : [];
|
|
2134
|
+
},
|
|
2135
|
+
handleComments: {
|
|
2136
|
+
ownLine: handleLineComment,
|
|
2137
|
+
endOfLine: handleLineComment,
|
|
2138
|
+
remaining: handleRemainingComment
|
|
2139
|
+
},
|
|
2140
|
+
getVisitorKeys() {
|
|
2141
|
+
return ["namedChildren"];
|
|
2142
|
+
}
|
|
2143
|
+
};
|
|
2144
|
+
function hasNamedNode(path) {
|
|
2145
|
+
return path.node.isNamed;
|
|
2146
|
+
}
|
|
2147
|
+
//#endregion
|
|
2148
|
+
//#region src/comments.ts
|
|
2149
|
+
const { hasNewline, isPreviousLineEmpty, skipNewline, skipSpaces } = prettier.util;
|
|
2150
|
+
const { breakParent, hardline, line, lineSuffix } = prettier_doc.builders;
|
|
2151
|
+
const prettierIgnoreRangesByTree = /* @__PURE__ */ new WeakMap();
|
|
2152
|
+
function determinePrettierIgnoreRanges(tree) {
|
|
2153
|
+
const { comments } = tree;
|
|
2154
|
+
if (!comments) return;
|
|
2155
|
+
const ranges = comments.filter(({ value }) => /^\/(?:\/\s*(?:prettier-ignore-(?:start|end)|@formatter:(?:off|on))\s*|\*\s*(?:prettier-ignore-(?:start|end)|@formatter:(?:off|on))\s*\*\/)$/.test(value)).reduce((ranges, { value, start }) => {
|
|
2156
|
+
const previous = ranges.at(-1);
|
|
2157
|
+
if (value.includes("start") || value.includes("off")) {
|
|
2158
|
+
if (previous?.end !== Infinity) ranges.push({
|
|
2159
|
+
start: start.index,
|
|
2160
|
+
end: Infinity
|
|
2161
|
+
});
|
|
2162
|
+
} else if (previous?.end === Infinity) previous.end = start.index;
|
|
2163
|
+
return ranges;
|
|
2164
|
+
}, new Array());
|
|
2165
|
+
prettierIgnoreRangesByTree.set(tree, ranges);
|
|
2166
|
+
}
|
|
2167
|
+
function isFullyBetweenPrettierIgnore(path) {
|
|
2168
|
+
const { node, root } = path;
|
|
2169
|
+
const start = parser_default.locStart(node);
|
|
2170
|
+
const end = parser_default.locEnd(node);
|
|
2171
|
+
return prettierIgnoreRangesByTree.get(root)?.some((range) => range.start < start && end < range.end) === true;
|
|
2172
|
+
}
|
|
2173
|
+
function isPrettierIgnore(comment) {
|
|
2174
|
+
return /^(\/\/\s*prettier-ignore|\/\*\s*prettier-ignore\s*\*\/)$/.test(comment.value);
|
|
2175
|
+
}
|
|
2176
|
+
function willPrintOwnComments(path) {
|
|
2177
|
+
return isMember(path.node) && !printer_default.hasPrettierIgnore(path);
|
|
2178
|
+
}
|
|
2179
|
+
function canAttachComment(node, ancestors) {
|
|
2180
|
+
if (!node.isNamed) return isBinaryOperator(node);
|
|
2181
|
+
switch (node.type) {
|
|
2182
|
+
case "enum_body_declarations":
|
|
2183
|
+
case "escape_sequence":
|
|
2184
|
+
case "modifier":
|
|
2185
|
+
case "multiline_string_fragment":
|
|
2186
|
+
case "program":
|
|
2187
|
+
case "string_fragment":
|
|
2188
|
+
case "visibility": return false;
|
|
2189
|
+
case "parenthesized_expression": {
|
|
2190
|
+
const [parent] = ancestors;
|
|
2191
|
+
return !(parent.isNamed && [
|
|
2192
|
+
"do_statement",
|
|
2193
|
+
"if_statement",
|
|
2194
|
+
"switch_expression",
|
|
2195
|
+
"synchronized_statement",
|
|
2196
|
+
"try_statement",
|
|
2197
|
+
"try_with_resources_statement",
|
|
2198
|
+
"while_statement"
|
|
2199
|
+
].includes(parent.type));
|
|
2200
|
+
}
|
|
2201
|
+
default: return true;
|
|
2202
|
+
}
|
|
2203
|
+
}
|
|
2204
|
+
function handleLineComment(commentNode, _, options) {
|
|
2205
|
+
return [
|
|
2206
|
+
handleBinaryExpressionComments,
|
|
2207
|
+
handleFormalParametersComments,
|
|
2208
|
+
handleFqnOrRefTypeComments,
|
|
2209
|
+
handleIfStatementComments,
|
|
2210
|
+
handleJumpStatementComments,
|
|
2211
|
+
handleLabeledStatementComments,
|
|
2212
|
+
handleLambdaExpressionComments,
|
|
2213
|
+
handleMemberChainComments,
|
|
2214
|
+
handleModifiersComments,
|
|
2215
|
+
handleNameComments,
|
|
2216
|
+
handleTernaryExpressionComments,
|
|
2217
|
+
handleTryStatementComments
|
|
2218
|
+
].some((fn) => fn(commentNode, options));
|
|
2219
|
+
}
|
|
2220
|
+
function handleRemainingComment(commentNode) {
|
|
2221
|
+
return [
|
|
2222
|
+
handleFqnOrRefTypeComments,
|
|
2223
|
+
handleNameComments,
|
|
2224
|
+
handleJumpStatementComments
|
|
2225
|
+
].some((fn) => fn(commentNode));
|
|
2226
|
+
}
|
|
2227
|
+
function handleBinaryExpressionComments(commentNode, options) {
|
|
2228
|
+
const { enclosingNode, precedingNode, followingNode } = commentNode;
|
|
2229
|
+
if (enclosingNode?.type === "binary_expression") {
|
|
2230
|
+
if (isBinaryOperator(followingNode)) {
|
|
2231
|
+
if (options.experimentalOperatorPosition === "start") prettier.util.addLeadingComment(followingNode, commentNode);
|
|
2232
|
+
else prettier.util.addTrailingComment(followingNode, commentNode);
|
|
2233
|
+
return true;
|
|
2234
|
+
} else if (options.experimentalOperatorPosition === "start" && isBinaryOperator(precedingNode)) {
|
|
2235
|
+
prettier.util.addLeadingComment(precedingNode, commentNode);
|
|
2236
|
+
return true;
|
|
2237
|
+
}
|
|
2238
|
+
}
|
|
2239
|
+
return false;
|
|
2240
|
+
}
|
|
2241
|
+
function handleFormalParametersComments(commentNode) {
|
|
2242
|
+
const { enclosingNode, precedingNode, followingNode } = commentNode;
|
|
2243
|
+
if (enclosingNode?.type === "formal_parameters" && !precedingNode && !followingNode) {
|
|
2244
|
+
prettier.util.addDanglingComment(enclosingNode, commentNode, void 0);
|
|
2245
|
+
return true;
|
|
2246
|
+
}
|
|
2247
|
+
return false;
|
|
2248
|
+
}
|
|
2249
|
+
function handleFqnOrRefTypeComments(commentNode) {
|
|
2250
|
+
const { enclosingNode, followingNode } = commentNode;
|
|
2251
|
+
if (enclosingNode?.type === "scoped_type_identifier" && followingNode) {
|
|
2252
|
+
prettier.util.addLeadingComment(followingNode, commentNode);
|
|
2253
|
+
return true;
|
|
2254
|
+
}
|
|
2255
|
+
return false;
|
|
2256
|
+
}
|
|
2257
|
+
function handleIfStatementComments(commentNode) {
|
|
2258
|
+
const { enclosingNode, precedingNode } = commentNode;
|
|
2259
|
+
if (enclosingNode?.type === "if_statement" && precedingNode?.fieldName === "consequence") {
|
|
2260
|
+
prettier.util.addDanglingComment(enclosingNode, commentNode, void 0);
|
|
2261
|
+
return true;
|
|
2262
|
+
}
|
|
2263
|
+
return false;
|
|
2264
|
+
}
|
|
2265
|
+
function handleJumpStatementComments(commentNode) {
|
|
2266
|
+
const { enclosingNode, precedingNode, followingNode } = commentNode;
|
|
2267
|
+
if (enclosingNode && !precedingNode && !followingNode && (enclosingNode.type === "break_statement" || enclosingNode.type === "continue_statement" || enclosingNode.type === "return_statement")) {
|
|
2268
|
+
prettier.util.addTrailingComment(enclosingNode, commentNode);
|
|
2269
|
+
return true;
|
|
2270
|
+
}
|
|
2271
|
+
return false;
|
|
2272
|
+
}
|
|
2273
|
+
function handleLabeledStatementComments(commentNode) {
|
|
2274
|
+
const { enclosingNode, precedingNode } = commentNode;
|
|
2275
|
+
if (enclosingNode?.type === "labeled_statement" && precedingNode?.type === "identifier") {
|
|
2276
|
+
prettier.util.addLeadingComment(precedingNode, commentNode);
|
|
2277
|
+
return true;
|
|
2278
|
+
}
|
|
2279
|
+
return false;
|
|
2280
|
+
}
|
|
2281
|
+
function handleLambdaExpressionComments(commentNode) {
|
|
2282
|
+
const { enclosingNode, precedingNode, followingNode } = commentNode;
|
|
2283
|
+
if (enclosingNode?.type === "lambda_expression" && precedingNode && followingNode && enclosingNode.children.find(({ type }) => type === "->").end.index < commentNode.start.index) {
|
|
2284
|
+
if (followingNode.type === "block") if (followingNode.namedChildren.length) prettier.util.addLeadingComment(followingNode.namedChildren[0], commentNode);
|
|
2285
|
+
else prettier.util.addDanglingComment(followingNode, commentNode, void 0);
|
|
2286
|
+
else prettier.util.addLeadingComment(followingNode, commentNode);
|
|
2287
|
+
return true;
|
|
2288
|
+
}
|
|
2289
|
+
}
|
|
2290
|
+
function handleMemberChainComments(commentNode) {
|
|
2291
|
+
const { enclosingNode, precedingNode, followingNode } = commentNode;
|
|
2292
|
+
if (precedingNode && (enclosingNode?.type === "field_access" || enclosingNode?.type === "method_invocation" && precedingNode.end.row < commentNode.start.row) && precedingNode === enclosingNode.objectNode) {
|
|
2293
|
+
prettier.util.addLeadingComment(enclosingNode, commentNode);
|
|
2294
|
+
return true;
|
|
2295
|
+
} else if (isMember(followingNode) && (!precedingNode || precedingNode !== getMemberObject(followingNode) && precedingNode.end.row < commentNode.start.row) && !isPrettierIgnore(commentNode)) {
|
|
2296
|
+
prettier.util.addDanglingComment(followingNode, commentNode, void 0);
|
|
2297
|
+
return true;
|
|
2298
|
+
}
|
|
2299
|
+
return false;
|
|
2300
|
+
}
|
|
2301
|
+
function handleModifiersComments(commentNode) {
|
|
2302
|
+
const { precedingNode } = commentNode;
|
|
2303
|
+
if (precedingNode?.type === "annotation" || precedingNode?.type === "marker_annotation" || precedingNode?.type === "modifiers") {
|
|
2304
|
+
prettier.util.addTrailingComment(precedingNode, commentNode);
|
|
2305
|
+
return true;
|
|
2306
|
+
}
|
|
2307
|
+
return false;
|
|
2308
|
+
}
|
|
2309
|
+
function handleNameComments(commentNode) {
|
|
2310
|
+
const { enclosingNode, precedingNode } = commentNode;
|
|
2311
|
+
if (enclosingNode && precedingNode?.type === "identifier" && (enclosingNode.type === "scoped_identifier" || enclosingNode.type === "module_declaration" || enclosingNode.type === "package_declaration" || enclosingNode.type === "scoped_type_identifier")) {
|
|
2312
|
+
prettier.util.addTrailingComment(precedingNode, commentNode);
|
|
2313
|
+
return true;
|
|
2314
|
+
}
|
|
2315
|
+
return false;
|
|
2316
|
+
}
|
|
2317
|
+
function handleTernaryExpressionComments(commentNode) {
|
|
2318
|
+
const { enclosingNode, precedingNode, followingNode } = commentNode;
|
|
2319
|
+
if (enclosingNode?.type === "ternary_expression" && precedingNode?.isNamed && followingNode?.isNamed && precedingNode.end.row < commentNode.start.row && commentNode.end.row < followingNode.start.row) {
|
|
2320
|
+
prettier.util.addLeadingComment(followingNode, commentNode);
|
|
2321
|
+
return true;
|
|
2322
|
+
}
|
|
2323
|
+
return false;
|
|
2324
|
+
}
|
|
2325
|
+
function handleTryStatementComments(commentNode) {
|
|
2326
|
+
const { enclosingNode, followingNode } = commentNode;
|
|
2327
|
+
if (enclosingNode && (enclosingNode.type === "catch_clause" || enclosingNode.type === "try_statement" || enclosingNode.type === "try_with_resources_statement") && followingNode?.isNamed) {
|
|
2328
|
+
const block = followingNode.type === "catch_clause" ? followingNode.bodyNode : followingNode.type === "finally_clause" ? followingNode.namedChildren[0] : null;
|
|
2329
|
+
if (!block) return false;
|
|
2330
|
+
const blockStatement = block.namedChildren.at(0);
|
|
2331
|
+
if (blockStatement) prettier.util.addLeadingComment(blockStatement, commentNode);
|
|
2332
|
+
else prettier.util.addDanglingComment(block, commentNode, void 0);
|
|
2333
|
+
return true;
|
|
2334
|
+
}
|
|
2335
|
+
return false;
|
|
2336
|
+
}
|
|
2337
|
+
function getMemberObject(node) {
|
|
2338
|
+
return node.type === "array_access" ? node.arrayNode : node.objectNode;
|
|
2339
|
+
}
|
|
2340
|
+
const binaryOperators = /* @__PURE__ */ new Set([
|
|
2341
|
+
"<<",
|
|
2342
|
+
">>",
|
|
2343
|
+
">>>",
|
|
2344
|
+
"instanceof",
|
|
2345
|
+
"<=",
|
|
2346
|
+
">=",
|
|
2347
|
+
"==",
|
|
2348
|
+
"-",
|
|
2349
|
+
"+",
|
|
2350
|
+
"&&",
|
|
2351
|
+
"&",
|
|
2352
|
+
"^",
|
|
2353
|
+
"!=",
|
|
2354
|
+
"||",
|
|
2355
|
+
"|",
|
|
2356
|
+
"*",
|
|
2357
|
+
"/",
|
|
2358
|
+
"%"
|
|
2359
|
+
]);
|
|
2360
|
+
function isBinaryOperator(node) {
|
|
2361
|
+
return node !== void 0 && binaryOperators.has(node.type);
|
|
2362
|
+
}
|
|
2363
|
+
function printLeadingComment(path) {
|
|
2364
|
+
const comment = path.node;
|
|
2365
|
+
const parts = [printComment(comment)];
|
|
2366
|
+
const originalText = path.root.value;
|
|
2367
|
+
if (comment.type === "block_comment") {
|
|
2368
|
+
let lineBreak = " ";
|
|
2369
|
+
if (hasNewline(originalText, parser_default.locEnd(comment))) if (hasNewline(originalText, parser_default.locStart(comment), { backwards: true })) lineBreak = hardline;
|
|
2370
|
+
else lineBreak = line;
|
|
2371
|
+
parts.push(lineBreak);
|
|
2372
|
+
} else parts.push(hardline);
|
|
2373
|
+
const index = skipNewline(originalText, skipSpaces(originalText, parser_default.locEnd(comment)));
|
|
2374
|
+
if (index !== false && hasNewline(originalText, index)) parts.push(hardline);
|
|
2375
|
+
return parts;
|
|
2376
|
+
}
|
|
2377
|
+
function printTrailingComment(path, previousComment) {
|
|
2378
|
+
const comment = path.node;
|
|
2379
|
+
const printed = printComment(comment);
|
|
2380
|
+
const originalText = path.root.value;
|
|
2381
|
+
const isBlock = comment.type === "block_comment";
|
|
2382
|
+
if (previousComment?.hasLineSuffix && !previousComment?.isBlock || hasNewline(originalText, parser_default.locStart(comment), { backwards: true })) {
|
|
2383
|
+
const isLineBeforeEmpty = isPreviousLineEmpty(originalText, parser_default.locStart(comment));
|
|
2384
|
+
return {
|
|
2385
|
+
doc: lineSuffix([
|
|
2386
|
+
hardline,
|
|
2387
|
+
isLineBeforeEmpty ? hardline : "",
|
|
2388
|
+
printed
|
|
2389
|
+
]),
|
|
2390
|
+
isBlock,
|
|
2391
|
+
hasLineSuffix: true
|
|
2392
|
+
};
|
|
2393
|
+
}
|
|
2394
|
+
if (!isBlock || previousComment?.hasLineSuffix) return {
|
|
2395
|
+
doc: [lineSuffix([" ", printed]), breakParent],
|
|
2396
|
+
isBlock,
|
|
2397
|
+
hasLineSuffix: true
|
|
2398
|
+
};
|
|
2399
|
+
return {
|
|
2400
|
+
doc: [" ", printed],
|
|
2401
|
+
isBlock,
|
|
2402
|
+
hasLineSuffix: false
|
|
2403
|
+
};
|
|
2404
|
+
}
|
|
2405
|
+
function printLeadingComments(path) {
|
|
2406
|
+
if (!hasChild(path, "comments")) return [];
|
|
2407
|
+
const docs = [];
|
|
2408
|
+
path.each((path) => {
|
|
2409
|
+
const { node: comment } = path;
|
|
2410
|
+
if (!comment.leading) return;
|
|
2411
|
+
docs.push(printLeadingComment(path));
|
|
2412
|
+
}, "comments");
|
|
2413
|
+
return docs;
|
|
2414
|
+
}
|
|
2415
|
+
function printTrailingComments(path) {
|
|
2416
|
+
if (!hasChild(path, "comments")) return [];
|
|
2417
|
+
const docs = [];
|
|
2418
|
+
let printedTrailingComment;
|
|
2419
|
+
path.each((path) => {
|
|
2420
|
+
const { node: comment } = path;
|
|
2421
|
+
if (!comment.trailing) return;
|
|
2422
|
+
printedTrailingComment = printTrailingComment(path, printedTrailingComment);
|
|
2423
|
+
docs.push(printedTrailingComment.doc);
|
|
2424
|
+
}, "comments");
|
|
2425
|
+
return docs;
|
|
2426
|
+
}
|
|
2427
|
+
function printCommentsSeparately(path) {
|
|
2428
|
+
return {
|
|
2429
|
+
leading: printLeadingComments(path),
|
|
2430
|
+
trailing: printTrailingComments(path)
|
|
2431
|
+
};
|
|
2432
|
+
}
|
|
2433
|
+
function printComments(path, doc) {
|
|
2434
|
+
const leading = printLeadingComments(path);
|
|
2435
|
+
const trailing = printTrailingComments(path);
|
|
2436
|
+
return leading.length || trailing.length ? [
|
|
2437
|
+
leading,
|
|
2438
|
+
doc,
|
|
2439
|
+
trailing
|
|
2440
|
+
] : doc;
|
|
2441
|
+
}
|
|
2442
|
+
//#endregion
|
|
2443
|
+
//#region src/parser.ts
|
|
2444
|
+
var parser_default = {
|
|
2445
|
+
async parse(text) {
|
|
2446
|
+
const tree = (await parser).parse(text);
|
|
2447
|
+
const { rootNode } = tree;
|
|
2448
|
+
if (rootNode.hasError) throw new Error("Failed to parse: " + rootNode);
|
|
2449
|
+
const ast = processTree(rootNode);
|
|
2450
|
+
determinePrettierIgnoreRanges(ast);
|
|
2451
|
+
tree.delete();
|
|
2452
|
+
return ast;
|
|
2453
|
+
},
|
|
2454
|
+
astFormat: "java",
|
|
2455
|
+
hasPragma(text) {
|
|
2456
|
+
return /^\/\*\*\n\s+\*\s@(format|prettier)\n\s+\*\//.test(text);
|
|
2457
|
+
},
|
|
2458
|
+
locStart(node) {
|
|
2459
|
+
return node.start.index;
|
|
2460
|
+
},
|
|
2461
|
+
locEnd(node) {
|
|
2462
|
+
return node.end.index;
|
|
2463
|
+
}
|
|
2464
|
+
};
|
|
2465
|
+
const parser = (async () => {
|
|
2466
|
+
await web_tree_sitter.Parser.init();
|
|
2467
|
+
const parser = new web_tree_sitter.Parser();
|
|
2468
|
+
const Java = await web_tree_sitter.Language.load(new URL("./tree-sitter-java_orchard.wasm", require("url").pathToFileURL(__filename).href));
|
|
2469
|
+
parser.setLanguage(Java);
|
|
2470
|
+
return parser;
|
|
2471
|
+
})();
|
|
2472
|
+
const isParenthesizedParent = createTypeCheckFunction([
|
|
2473
|
+
"do_statement",
|
|
2474
|
+
"if_statement",
|
|
2475
|
+
"switch_expression",
|
|
2476
|
+
"synchronized_statement",
|
|
2477
|
+
"try_statement",
|
|
2478
|
+
"try_with_resources_statement",
|
|
2479
|
+
"while_statement"
|
|
2480
|
+
]);
|
|
2481
|
+
function processTree(node, fieldName = null, comments) {
|
|
2482
|
+
const { type, isNamed, text: value, startPosition, endPosition } = node;
|
|
2483
|
+
const javaNode = {
|
|
2484
|
+
type,
|
|
2485
|
+
isNamed,
|
|
2486
|
+
value,
|
|
2487
|
+
start: {
|
|
2488
|
+
index: node.startIndex,
|
|
2489
|
+
row: startPosition.row,
|
|
2490
|
+
column: startPosition.column
|
|
2491
|
+
},
|
|
2492
|
+
end: {
|
|
2493
|
+
index: node.endIndex,
|
|
2494
|
+
row: endPosition.row,
|
|
2495
|
+
column: endPosition.column
|
|
2496
|
+
},
|
|
2497
|
+
children: [],
|
|
2498
|
+
namedChildren: [],
|
|
2499
|
+
fieldName
|
|
2500
|
+
};
|
|
2501
|
+
if (!comments) comments = javaNode.comments = [];
|
|
2502
|
+
if (!javaNode.isNamed) return javaNode;
|
|
2503
|
+
const multiFields = multiFieldsByType[node.type];
|
|
2504
|
+
if (multiFields) Object.keys(multiFields).forEach((name) => javaNode[`${name}Nodes`] = []);
|
|
2505
|
+
node.children.forEach((child, index) => {
|
|
2506
|
+
const fieldName = node.fieldNameForChild(index);
|
|
2507
|
+
(child.type === "parenthesized_expression" && !isParenthesizedParent(javaNode) ? child.namedChildren : [child]).forEach((child) => {
|
|
2508
|
+
const { type, text: value, startPosition, endPosition } = child;
|
|
2509
|
+
if (type === "block_comment" || type === "line_comment") comments.push({
|
|
2510
|
+
type,
|
|
2511
|
+
value,
|
|
2512
|
+
start: {
|
|
2513
|
+
index: child.startIndex,
|
|
2514
|
+
row: startPosition.row,
|
|
2515
|
+
column: startPosition.column
|
|
2516
|
+
},
|
|
2517
|
+
end: {
|
|
2518
|
+
index: child.endIndex,
|
|
2519
|
+
row: endPosition.row,
|
|
2520
|
+
column: endPosition.column
|
|
2521
|
+
},
|
|
2522
|
+
leading: false,
|
|
2523
|
+
trailing: false,
|
|
2524
|
+
printed: false
|
|
2525
|
+
});
|
|
2526
|
+
else {
|
|
2527
|
+
const javaChild = processTree(child, fieldName, comments);
|
|
2528
|
+
javaNode.children.push(javaChild);
|
|
2529
|
+
if (javaChild.isNamed) javaNode.namedChildren.push(javaChild);
|
|
2530
|
+
if (fieldName) if (multiFields?.[fieldName]) javaNode[`${fieldName}Nodes`].push(javaChild);
|
|
2531
|
+
else javaNode[`${fieldName}Node`] = javaChild;
|
|
2532
|
+
}
|
|
2533
|
+
});
|
|
2534
|
+
});
|
|
2535
|
+
return javaNode;
|
|
2536
|
+
}
|
|
2537
|
+
//#endregion
|
|
2538
|
+
//#region src/index.ts
|
|
2539
|
+
var src_default = {
|
|
2540
|
+
languages: [{
|
|
2541
|
+
name: "Java",
|
|
2542
|
+
parsers: ["java"],
|
|
2543
|
+
group: "Java",
|
|
2544
|
+
tmScope: "source.java",
|
|
2545
|
+
aceMode: "java",
|
|
2546
|
+
codemirrorMode: "clike",
|
|
2547
|
+
codemirrorMimeType: "text/x-java",
|
|
2548
|
+
extensions: [".java"],
|
|
2549
|
+
linguistLanguageId: 181,
|
|
2550
|
+
vscodeLanguageIds: ["java"]
|
|
2551
|
+
}],
|
|
2552
|
+
parsers: { java: parser_default },
|
|
2553
|
+
printers: { java: printer_default },
|
|
2554
|
+
options: options_default,
|
|
2555
|
+
defaultOptions: { arrowParens: "avoid" }
|
|
2556
|
+
};
|
|
2557
|
+
//#endregion
|
|
2558
|
+
module.exports = src_default;
|