@live-codes/prettier-plugin-rust 0.1.9
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 +21 -0
- package/README.md +290 -0
- package/index.cjs +4281 -0
- package/index.d.ts +18 -0
- package/index.global.js +44 -0
- package/index.js +4272 -0
- package/package.json +75 -0
package/index.cjs
ADDED
|
@@ -0,0 +1,4281 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var jinxRust = require('jinx-rust');
|
|
6
|
+
var utils = require('jinx-rust/utils');
|
|
7
|
+
var doc = require('prettier/doc');
|
|
8
|
+
|
|
9
|
+
// src/format/plugin.ts
|
|
10
|
+
|
|
11
|
+
// src/utils/debug.ts
|
|
12
|
+
var cwd = (
|
|
13
|
+
// @ts-expect-error
|
|
14
|
+
typeof process === "object" && typeof process?.cwd === "function" ? /* @__PURE__ */ normPath(/* @__PURE__ */ process.cwd() ?? "") : ""
|
|
15
|
+
);
|
|
16
|
+
function normPath_strip_cwd(filepath) {
|
|
17
|
+
let normFilePath = normPath(filepath);
|
|
18
|
+
return normFilePath.startsWith(cwd) ? normFilePath.slice(cwd.length + 1) : normFilePath;
|
|
19
|
+
}
|
|
20
|
+
var StackLine = class {
|
|
21
|
+
constructor(raw) {
|
|
22
|
+
({
|
|
23
|
+
1: this.callee = "",
|
|
24
|
+
2: this.filepath = "",
|
|
25
|
+
3: this.line = "",
|
|
26
|
+
4: this.col = "",
|
|
27
|
+
5: this.other = ""
|
|
28
|
+
} = (this.raw = raw).match(/at (?:(.+?)\s+\()?(?:(.+?):([0-9]+)(?::([0-9]+))?|([^)]+))\)?/) ?? ["", "", "", "", "", ""]);
|
|
29
|
+
this.url = this.filepath ? normPath_strip_cwd(this.filepath) + (this.line && this.col && `:${this.line}:${this.col}`) : this.other === "native" ? "<native>" : "";
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
function getPrintWidth() {
|
|
33
|
+
return clamp(0, getTerminalWidth(128), 200) - 4;
|
|
34
|
+
}
|
|
35
|
+
var StackItem = class extends StackLine {
|
|
36
|
+
constructor(stack, i, raw) {
|
|
37
|
+
super(raw);
|
|
38
|
+
this.stack = stack;
|
|
39
|
+
this.i = i;
|
|
40
|
+
this.hidden = false;
|
|
41
|
+
}
|
|
42
|
+
hide() {
|
|
43
|
+
this.hidden = true;
|
|
44
|
+
return this;
|
|
45
|
+
}
|
|
46
|
+
hideNext(n) {
|
|
47
|
+
for (let i = 0; i < n; i++)
|
|
48
|
+
this.at(i)?.hide();
|
|
49
|
+
}
|
|
50
|
+
hideWhileTrue(test) {
|
|
51
|
+
let line2 = this;
|
|
52
|
+
while (line2 && test(line2))
|
|
53
|
+
line2 = line2.hide().next();
|
|
54
|
+
}
|
|
55
|
+
at(relIndex) {
|
|
56
|
+
return this.i + relIndex >= this.stack.length || this.i + relIndex < 0 ? void 0 : this.stack[this.i + relIndex];
|
|
57
|
+
}
|
|
58
|
+
next() {
|
|
59
|
+
return this.at(1);
|
|
60
|
+
}
|
|
61
|
+
toString() {
|
|
62
|
+
const url = this.url;
|
|
63
|
+
const calleeColor = this.stack.style?.callee?.(this.callee, this) ?? color.cyan;
|
|
64
|
+
const urlColor = this.stack.style?.url?.(url, this) ?? color.grey;
|
|
65
|
+
return compose2Cols(" at " + calleeColor(this.callee), urlColor(url), getPrintWidth());
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
function createStack(message, Error_stack, style) {
|
|
69
|
+
for (var STACK = [], i = 0, stack = Error_stack.split("\n").slice(2); i < stack.length; i++)
|
|
70
|
+
STACK[i] = new StackItem(STACK, i, stack[i]);
|
|
71
|
+
return STACK.message = message, STACK.style = style, STACK;
|
|
72
|
+
}
|
|
73
|
+
function composeStack(stack) {
|
|
74
|
+
var hidden = 0;
|
|
75
|
+
var str = stack.message;
|
|
76
|
+
for (var item of stack)
|
|
77
|
+
item.hidden ? ++hidden : str += "\n" + item.toString();
|
|
78
|
+
return str + (hidden > 0 ? "\n" + color.grey(compose2Cols("", `...filtered ${hidden} lines`, getPrintWidth())) : "");
|
|
79
|
+
}
|
|
80
|
+
function createCustomError({
|
|
81
|
+
message = "Unknown Error",
|
|
82
|
+
editStack = (stack) => {
|
|
83
|
+
},
|
|
84
|
+
style = void 0,
|
|
85
|
+
stackTraceLimit = 20
|
|
86
|
+
}) {
|
|
87
|
+
const _stackTraceLimit = Error.stackTraceLimit;
|
|
88
|
+
const _prepareStackTrace = Error.prepareStackTrace;
|
|
89
|
+
Error.stackTraceLimit = stackTraceLimit;
|
|
90
|
+
const _ctx = {};
|
|
91
|
+
Error.captureStackTrace(_ctx, createCustomError);
|
|
92
|
+
const stack = createStack(message, _ctx.stack, style);
|
|
93
|
+
Error.prepareStackTrace = function(err2, calls) {
|
|
94
|
+
editStack(stack);
|
|
95
|
+
return composeStack(stack);
|
|
96
|
+
};
|
|
97
|
+
const err = new Error(message);
|
|
98
|
+
err.stack = err.stack;
|
|
99
|
+
Error.stackTraceLimit = _stackTraceLimit;
|
|
100
|
+
Error.prepareStackTrace = _prepareStackTrace;
|
|
101
|
+
return err;
|
|
102
|
+
}
|
|
103
|
+
function compose2Cols(left, right, len = 64, min = 1) {
|
|
104
|
+
return left + " ".repeat(clamp(min, len, len - (color.unstyledLength(left) + color.unstyledLength(right)))) + right;
|
|
105
|
+
}
|
|
106
|
+
function exit(message, ...ctx2) {
|
|
107
|
+
if (ctx2.length > 0)
|
|
108
|
+
console.log("Error context:", { ...ctx2 });
|
|
109
|
+
throw createCustomError({ message });
|
|
110
|
+
}
|
|
111
|
+
exit.never = function never(...ctx2) {
|
|
112
|
+
exit("Reached unreachable code", ...ctx2);
|
|
113
|
+
};
|
|
114
|
+
function assert(predicate, err, ...ctx2) {
|
|
115
|
+
if (false === predicate)
|
|
116
|
+
exit(err ?? "Assertion failed", ...ctx2);
|
|
117
|
+
}
|
|
118
|
+
function Identity(v) {
|
|
119
|
+
return v;
|
|
120
|
+
}
|
|
121
|
+
function last_of(arr) {
|
|
122
|
+
return arr[arr.length - 1];
|
|
123
|
+
}
|
|
124
|
+
function normPath(filepath) {
|
|
125
|
+
return filepath.replace(/^file:\/\/\//, "").replace(/\\\\?/g, "/");
|
|
126
|
+
}
|
|
127
|
+
function binarySearchIn(array, target, toValue) {
|
|
128
|
+
if (isEmpty(array))
|
|
129
|
+
return -1;
|
|
130
|
+
let i = 0;
|
|
131
|
+
let low = 0;
|
|
132
|
+
let high = array.length - 1;
|
|
133
|
+
let value = toValue(array[high]);
|
|
134
|
+
if (target >= value)
|
|
135
|
+
return high;
|
|
136
|
+
else
|
|
137
|
+
high--;
|
|
138
|
+
while (low <= high) {
|
|
139
|
+
i = low + (high - low >> 1);
|
|
140
|
+
value = toValue(array[i]);
|
|
141
|
+
if (target === value)
|
|
142
|
+
return i;
|
|
143
|
+
if (target > value)
|
|
144
|
+
low = i + 1;
|
|
145
|
+
else
|
|
146
|
+
high = i - 1;
|
|
147
|
+
}
|
|
148
|
+
return low - 1;
|
|
149
|
+
}
|
|
150
|
+
function getTerminalWidth(fallbackWidth = 200) {
|
|
151
|
+
return globalThis?.process?.stdout?.columns ?? fallbackWidth;
|
|
152
|
+
}
|
|
153
|
+
var isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined";
|
|
154
|
+
var color = ((cfn, mfn) => ({
|
|
155
|
+
black: cfn(30),
|
|
156
|
+
red: cfn(31),
|
|
157
|
+
green: cfn(32),
|
|
158
|
+
yellow: cfn(33),
|
|
159
|
+
blue: cfn(34),
|
|
160
|
+
magenta: cfn(35),
|
|
161
|
+
cyan: cfn(36),
|
|
162
|
+
white: cfn(37),
|
|
163
|
+
grey: cfn(90),
|
|
164
|
+
bold: mfn(1, 22),
|
|
165
|
+
italic: mfn(3, 23),
|
|
166
|
+
underline: mfn(4, 24),
|
|
167
|
+
hidden: mfn(8, 28),
|
|
168
|
+
hiddenCursor: (str) => `\x1B[?25l${str}\x1B[?25h`,
|
|
169
|
+
unstyle: (str) => str.replace(/\x1B\[[0-9][0-9]?m/g, ""),
|
|
170
|
+
unstyledLength: (str) => str.replace(/\x1B\[[0-9][0-9]?m/g, "").length,
|
|
171
|
+
link: (str) => color.underline(color.blue(str))
|
|
172
|
+
}))(
|
|
173
|
+
(c1) => isBrowser ? Identity : (str) => `\x1B[${c1}m${str.replace(/\x1B\[39m/g, `\x1B[${c1}m`)}\x1B[39m`,
|
|
174
|
+
(c1, c2) => isBrowser ? Identity : (str) => `\x1B[${c1}m${str}\x1B[${c2}m`
|
|
175
|
+
);
|
|
176
|
+
function Map_get(map, key, init) {
|
|
177
|
+
if (!map.has(key))
|
|
178
|
+
map.set(key, init(key));
|
|
179
|
+
return map.get(key);
|
|
180
|
+
}
|
|
181
|
+
function isEmpty(array) {
|
|
182
|
+
return 0 === array.length;
|
|
183
|
+
}
|
|
184
|
+
function Array_splice(array, target, index = array.indexOf(target)) {
|
|
185
|
+
array.splice(index, 1);
|
|
186
|
+
}
|
|
187
|
+
function Array_replace(array, target, ...replacements) {
|
|
188
|
+
array.indexOf(target);
|
|
189
|
+
array.splice(array.indexOf(target), 1, ...replacements);
|
|
190
|
+
}
|
|
191
|
+
function has_key_defined(o, k) {
|
|
192
|
+
return k in o && void 0 !== o[k];
|
|
193
|
+
}
|
|
194
|
+
function is_array(data) {
|
|
195
|
+
return Array.isArray(data);
|
|
196
|
+
}
|
|
197
|
+
function each(data, callback) {
|
|
198
|
+
switch (data.constructor) {
|
|
199
|
+
case Array: {
|
|
200
|
+
let i = 0;
|
|
201
|
+
for (; i < data.length; i++)
|
|
202
|
+
callback(data[i], i);
|
|
203
|
+
return;
|
|
204
|
+
}
|
|
205
|
+
case Object: {
|
|
206
|
+
let k;
|
|
207
|
+
for (k in data)
|
|
208
|
+
callback(data[k], k);
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
case Set: {
|
|
212
|
+
let d;
|
|
213
|
+
for (d of data)
|
|
214
|
+
callback(d, void 0);
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
case Map: {
|
|
218
|
+
let e;
|
|
219
|
+
for (e of data)
|
|
220
|
+
callback(e[1], e[0]);
|
|
221
|
+
return;
|
|
222
|
+
}
|
|
223
|
+
default: {
|
|
224
|
+
let x;
|
|
225
|
+
for (x of data)
|
|
226
|
+
callback(x, void 0);
|
|
227
|
+
return;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
function iLast(index, array) {
|
|
232
|
+
return 1 + index === array.length;
|
|
233
|
+
}
|
|
234
|
+
function try_eval(fn) {
|
|
235
|
+
try {
|
|
236
|
+
return fn();
|
|
237
|
+
} catch (e) {
|
|
238
|
+
return void 0;
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
function clamp(min, max, value) {
|
|
242
|
+
return value > min ? value < max ? value : max : min;
|
|
243
|
+
}
|
|
244
|
+
function flat(arr) {
|
|
245
|
+
return arr.flat(Infinity);
|
|
246
|
+
}
|
|
247
|
+
function map_tagged_template(args, map) {
|
|
248
|
+
const arr = [args[0][0]];
|
|
249
|
+
for (var i = 1; i < args.length; i++)
|
|
250
|
+
arr.push(map(args[i]), args[0][i]);
|
|
251
|
+
return arr;
|
|
252
|
+
}
|
|
253
|
+
function spliceAll(array) {
|
|
254
|
+
const r = [...array];
|
|
255
|
+
array.length = 0;
|
|
256
|
+
return r;
|
|
257
|
+
}
|
|
258
|
+
function spread(fn) {
|
|
259
|
+
return [...fn()];
|
|
260
|
+
}
|
|
261
|
+
var {
|
|
262
|
+
join,
|
|
263
|
+
line,
|
|
264
|
+
softline,
|
|
265
|
+
hardline,
|
|
266
|
+
literalline,
|
|
267
|
+
group,
|
|
268
|
+
conditionalGroup,
|
|
269
|
+
fill,
|
|
270
|
+
lineSuffix,
|
|
271
|
+
lineSuffixBoundary,
|
|
272
|
+
cursor,
|
|
273
|
+
breakParent,
|
|
274
|
+
ifBreak,
|
|
275
|
+
trim,
|
|
276
|
+
indent,
|
|
277
|
+
indentIfBreak,
|
|
278
|
+
align,
|
|
279
|
+
addAlignmentToDoc,
|
|
280
|
+
markAsRoot,
|
|
281
|
+
dedentToRoot,
|
|
282
|
+
dedent,
|
|
283
|
+
hardlineWithoutBreakParent,
|
|
284
|
+
literallineWithoutBreakParent,
|
|
285
|
+
label
|
|
286
|
+
} = doc.builders;
|
|
287
|
+
var { willBreak, traverseDoc, findInDoc, mapDoc, removeLines, stripTrailingHardline, canBreak } = doc.utils;
|
|
288
|
+
function cleanDoc(doc) {
|
|
289
|
+
if (!Array.isArray(doc))
|
|
290
|
+
return doc;
|
|
291
|
+
const parts = [];
|
|
292
|
+
for (const part of doc) {
|
|
293
|
+
if (!part)
|
|
294
|
+
continue;
|
|
295
|
+
const cleaned = cleanDoc(part);
|
|
296
|
+
const items = Array.isArray(cleaned) ? cleaned : [cleaned];
|
|
297
|
+
for (const item of items) {
|
|
298
|
+
if (typeof item === "string" && typeof parts[parts.length - 1] === "string") {
|
|
299
|
+
parts[parts.length - 1] = parts[parts.length - 1] + item;
|
|
300
|
+
} else {
|
|
301
|
+
parts.push(item);
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
return parts;
|
|
306
|
+
}
|
|
307
|
+
var Symbol_comments = Symbol.for("comments");
|
|
308
|
+
var DCM = /* @__PURE__ */ ((DCM2) => {
|
|
309
|
+
DCM2["arguments"] = "arguments";
|
|
310
|
+
DCM2["parameters"] = "parameters";
|
|
311
|
+
DCM2["items"] = "items";
|
|
312
|
+
DCM2["properties"] = "properties";
|
|
313
|
+
DCM2["members"] = "members";
|
|
314
|
+
DCM2["body"] = "body";
|
|
315
|
+
DCM2["cases"] = "cases";
|
|
316
|
+
DCM2["typeArguments"] = "typeArguments";
|
|
317
|
+
DCM2["ltParameters"] = "ltParameters";
|
|
318
|
+
DCM2["generics"] = "generics";
|
|
319
|
+
DCM2["specifiers"] = "specifiers";
|
|
320
|
+
DCM2["rules"] = "rules";
|
|
321
|
+
DCM2["match"] = "match";
|
|
322
|
+
DCM2["transform"] = "transform";
|
|
323
|
+
DCM2["segments"] = "segments";
|
|
324
|
+
return DCM2;
|
|
325
|
+
})(DCM || {});
|
|
326
|
+
|
|
327
|
+
// src/format/complexity.ts
|
|
328
|
+
var DEPTH = 0;
|
|
329
|
+
var ANCESTRY = [];
|
|
330
|
+
var LONE_SHORT_ARGUMENT_THRESHOLD_RATE = 0.25;
|
|
331
|
+
function withCheckContext(fn) {
|
|
332
|
+
if (0 === DEPTH) {
|
|
333
|
+
return fn();
|
|
334
|
+
} else {
|
|
335
|
+
DEPTH = 0;
|
|
336
|
+
const prev = spliceAll(ANCESTRY);
|
|
337
|
+
try {
|
|
338
|
+
return fn();
|
|
339
|
+
} finally {
|
|
340
|
+
DEPTH = ANCESTRY.push(...prev);
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
function is_short(str) {
|
|
345
|
+
return str.length <= LONE_SHORT_ARGUMENT_THRESHOLD_RATE * getOptions().printWidth;
|
|
346
|
+
}
|
|
347
|
+
function print(target) {
|
|
348
|
+
const current = getNode();
|
|
349
|
+
const keys = [...utils.getAstPath(ANCESTRY[0], getNode())];
|
|
350
|
+
for (let i = 1; i < ANCESTRY.length; i++)
|
|
351
|
+
keys.push(...utils.getOwnChildAstPath(ANCESTRY[i - 1], ANCESTRY[i]));
|
|
352
|
+
keys.push(...utils.getOwnChildAstPath(last_of(ANCESTRY), target));
|
|
353
|
+
try {
|
|
354
|
+
return getContext().path.call(() => getPrintFn()(), ...keys);
|
|
355
|
+
} catch (e) {
|
|
356
|
+
console.log({ current, target, keys, ANCESTRY });
|
|
357
|
+
throw e;
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
function IsSimpleFunction(fn) {
|
|
361
|
+
return function(node) {
|
|
362
|
+
if (0 !== DEPTH && node === ANCESTRY[DEPTH - 1]) {
|
|
363
|
+
return fn(node);
|
|
364
|
+
}
|
|
365
|
+
if (DEPTH >= 2) {
|
|
366
|
+
return isShortBasic(node);
|
|
367
|
+
}
|
|
368
|
+
try {
|
|
369
|
+
return fn(ANCESTRY[DEPTH++] = node);
|
|
370
|
+
} finally {
|
|
371
|
+
ANCESTRY.length = --DEPTH;
|
|
372
|
+
}
|
|
373
|
+
};
|
|
374
|
+
}
|
|
375
|
+
function HasComplexFunction(fn) {
|
|
376
|
+
return function(node) {
|
|
377
|
+
if (0 !== DEPTH && node === ANCESTRY[DEPTH - 1]) {
|
|
378
|
+
return fn(node);
|
|
379
|
+
}
|
|
380
|
+
if (DEPTH >= 2) {
|
|
381
|
+
return !isShortBasic(node);
|
|
382
|
+
}
|
|
383
|
+
try {
|
|
384
|
+
return fn(ANCESTRY[DEPTH++] = node);
|
|
385
|
+
} finally {
|
|
386
|
+
ANCESTRY.length = --DEPTH;
|
|
387
|
+
}
|
|
388
|
+
};
|
|
389
|
+
}
|
|
390
|
+
var isShortBasic = (node) => {
|
|
391
|
+
switch (node.nodeType) {
|
|
392
|
+
case jinxRust.NodeType.MissingNode:
|
|
393
|
+
return true;
|
|
394
|
+
case jinxRust.NodeType.Identifier:
|
|
395
|
+
case jinxRust.NodeType.Index:
|
|
396
|
+
case jinxRust.NodeType.LtIdentifier:
|
|
397
|
+
case jinxRust.NodeType.LbIdentifier:
|
|
398
|
+
case jinxRust.NodeType.McIdentifier:
|
|
399
|
+
return is_short(node.name);
|
|
400
|
+
case jinxRust.NodeType.Literal:
|
|
401
|
+
return is_short(node.value) && !/\n/.test(node.value);
|
|
402
|
+
}
|
|
403
|
+
return false;
|
|
404
|
+
};
|
|
405
|
+
var isSimpleType = IsSimpleFunction((node) => {
|
|
406
|
+
switch (node.nodeType) {
|
|
407
|
+
case jinxRust.NodeType.MissingNode:
|
|
408
|
+
case jinxRust.NodeType.FunctionSpread:
|
|
409
|
+
return true;
|
|
410
|
+
case jinxRust.NodeType.MacroInvocation:
|
|
411
|
+
return false;
|
|
412
|
+
case jinxRust.NodeType.Identifier:
|
|
413
|
+
case jinxRust.NodeType.TypeNever:
|
|
414
|
+
case jinxRust.NodeType.TypeInferred:
|
|
415
|
+
return true;
|
|
416
|
+
case jinxRust.NodeType.TypePath:
|
|
417
|
+
return isShortBasic(node.segment) && (!node.namespace || isSimpleType(node.namespace));
|
|
418
|
+
case jinxRust.NodeType.TypeCall:
|
|
419
|
+
return isSimpleType(node.typeCallee) && !hasComplexTypeArguments(node);
|
|
420
|
+
case jinxRust.NodeType.ExpressionTypeSelector:
|
|
421
|
+
return isSimpleType(node.typeTarget) && (!node.typeExpression || isSimpleType(node.typeExpression));
|
|
422
|
+
case jinxRust.NodeType.TypeDynBounds:
|
|
423
|
+
return !hasComplexTypeBounds(node);
|
|
424
|
+
case jinxRust.NodeType.TypeImplBounds:
|
|
425
|
+
return !hasComplexTypeBounds(node);
|
|
426
|
+
case jinxRust.NodeType.TypeFnPointer: {
|
|
427
|
+
const param = node.parameters[0];
|
|
428
|
+
return (!node.extern || !node.extern.abi || isShortBasic(node.extern.abi)) && !hasComplexLtParameters(node) && (node.parameters.length === 0 || node.parameters.length === 1 && (utils.is_FunctionSpread(param) || !utils.is_TypeFunctionNode(param.typeAnnotation) && isSimpleType(param.typeAnnotation))) && (!node.returnType || isSimpleType(node.returnType));
|
|
429
|
+
}
|
|
430
|
+
case jinxRust.NodeType.TypeFunction:
|
|
431
|
+
return isSimpleType(node.callee) && node.parameters.every(isSimpleType) && (!node.returnType || isSimpleType(node.returnType));
|
|
432
|
+
case jinxRust.NodeType.TypeSizedArray:
|
|
433
|
+
return isSimpleType(node.typeExpression) && isShortBasic(node.sizeExpression);
|
|
434
|
+
case jinxRust.NodeType.TypeSlice:
|
|
435
|
+
return isSimpleType(node.typeExpression);
|
|
436
|
+
case jinxRust.NodeType.TypeTuple:
|
|
437
|
+
return node.items.length === 0 || node.items.length === 1 && isSimpleType(node.items[0]);
|
|
438
|
+
case jinxRust.NodeType.TypeReference:
|
|
439
|
+
case jinxRust.NodeType.TypeDereferenceMut:
|
|
440
|
+
case jinxRust.NodeType.TypeDereferenceConst:
|
|
441
|
+
case jinxRust.NodeType.TypeParenthesized:
|
|
442
|
+
return isSimpleType(node.typeExpression);
|
|
443
|
+
default:
|
|
444
|
+
return false;
|
|
445
|
+
}
|
|
446
|
+
});
|
|
447
|
+
var hasComplexTypeBounds = HasComplexFunction((node) => {
|
|
448
|
+
return !!node.typeBounds && node.typeBounds.length > 1 && !node.typeBounds.every(isSimpleTypeBound);
|
|
449
|
+
});
|
|
450
|
+
var isSimpleTypeBound = (node) => {
|
|
451
|
+
switch (node.nodeType) {
|
|
452
|
+
case jinxRust.NodeType.TypeParenthesized:
|
|
453
|
+
return isSimpleTypeBound(node.typeExpression);
|
|
454
|
+
case jinxRust.NodeType.LtIdentifier:
|
|
455
|
+
case jinxRust.NodeType.LtElided:
|
|
456
|
+
case jinxRust.NodeType.LtStatic:
|
|
457
|
+
return true;
|
|
458
|
+
case jinxRust.NodeType.TypeTraitBound:
|
|
459
|
+
return utils.is_BareTypeTraitBound(node) && isSimpleTypeNamespaceTargetNoSelector(node.typeExpression);
|
|
460
|
+
default:
|
|
461
|
+
return false;
|
|
462
|
+
}
|
|
463
|
+
function isSimpleTypeNamespaceTargetNoSelector(node2) {
|
|
464
|
+
switch (node2.nodeType) {
|
|
465
|
+
case jinxRust.NodeType.Identifier:
|
|
466
|
+
return true;
|
|
467
|
+
case jinxRust.NodeType.TypePath:
|
|
468
|
+
return void 0 === node2.namespace || isSimpleTypeNamespaceTargetNoSelector(node2.namespace);
|
|
469
|
+
case jinxRust.NodeType.TypeCall:
|
|
470
|
+
return false;
|
|
471
|
+
case jinxRust.NodeType.TypeFunction:
|
|
472
|
+
return isSimpleTypeNamespaceTargetNoSelector(node2.callee) && node2.parameters.length === 0 && !node2.returnType;
|
|
473
|
+
default:
|
|
474
|
+
return false;
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
};
|
|
478
|
+
var hasComplexTypeArguments = HasComplexFunction(
|
|
479
|
+
(node) => !node.typeArguments || node.typeArguments.length === 0 ? false : node.typeArguments.length === 1 ? (() => {
|
|
480
|
+
const arg = node.typeArguments[0];
|
|
481
|
+
return utils.is_TypeBoundsStandaloneNode(arg) || canBreak(print(arg));
|
|
482
|
+
})() : true
|
|
483
|
+
);
|
|
484
|
+
var hasComplexLtParameters = HasComplexFunction((node) => {
|
|
485
|
+
const ltParameters = node.ltParameters;
|
|
486
|
+
if (!ltParameters || ltParameters.length === 0) {
|
|
487
|
+
return false;
|
|
488
|
+
}
|
|
489
|
+
if (ltParameters.length === 1) {
|
|
490
|
+
const arg = ltParameters[0];
|
|
491
|
+
if (arg.ltBounds && arg.ltBounds.length > 1) {
|
|
492
|
+
return true;
|
|
493
|
+
}
|
|
494
|
+
return false;
|
|
495
|
+
}
|
|
496
|
+
return true;
|
|
497
|
+
});
|
|
498
|
+
var isShortGenericParameterDeclaration = IsSimpleFunction((node) => {
|
|
499
|
+
switch (node.nodeType) {
|
|
500
|
+
case jinxRust.NodeType.GenericTypeParameterDeclaration:
|
|
501
|
+
return !node.typeBounds && !node.typeDefault;
|
|
502
|
+
case jinxRust.NodeType.ConstTypeParameterDeclaration:
|
|
503
|
+
return (!node.typeAnnotation || utils.is_MissingNode(node)) && !node.typeDefault;
|
|
504
|
+
case jinxRust.NodeType.GenericLtParameterDeclaration:
|
|
505
|
+
return !node.ltBounds;
|
|
506
|
+
default:
|
|
507
|
+
exit.never();
|
|
508
|
+
}
|
|
509
|
+
});
|
|
510
|
+
var hasComplexGenerics = HasComplexFunction((node) => {
|
|
511
|
+
return has_key_defined(node, "generics") && node.generics.length > 0 && !node.generics.every(isShortGenericParameterDeclaration);
|
|
512
|
+
});
|
|
513
|
+
var hasComplexTypeAnnotation = HasComplexFunction((node) => {
|
|
514
|
+
if (utils.is_VariableDeclarationNode(node) && !utils.is_LetScrutinee(node)) {
|
|
515
|
+
const { typeAnnotation } = node;
|
|
516
|
+
return !!typeAnnotation && !utils.is_MissingNode(typeAnnotation) && !isSimpleType(typeAnnotation);
|
|
517
|
+
} else {
|
|
518
|
+
return false;
|
|
519
|
+
}
|
|
520
|
+
});
|
|
521
|
+
function needsOuterSoftbreakParens(node) {
|
|
522
|
+
const parent = getParentNode();
|
|
523
|
+
if (!parent)
|
|
524
|
+
return false;
|
|
525
|
+
if (utils.is_ExpressionAsTypeCast(node)) {
|
|
526
|
+
return precedenceNeedsParens(node, parent);
|
|
527
|
+
}
|
|
528
|
+
if (utils.is_FlowControlMaybeValueExpression(parent) && //
|
|
529
|
+
parent.expression === node && flowControlExpressionNeedsOuterParens(parent)) {
|
|
530
|
+
return true;
|
|
531
|
+
}
|
|
532
|
+
if (utils.is_ExpressionWithBodyOrCases_or_BlockLikeMacroInvocation(node) && (utils.is_MemberExpression(parent) && parent.expression === node || utils.is_ExpressionWithBodyOrCases_or_BlockLikeMacroInvocation(parent) && !utils.is_ElseBlock(node, parent))) {
|
|
533
|
+
return true;
|
|
534
|
+
}
|
|
535
|
+
if (utils.is_UnionPattern(node) && utils.is_NodeWithMaybePatternNoUnionBody(parent)) {
|
|
536
|
+
return true;
|
|
537
|
+
}
|
|
538
|
+
if (hasComment(node)) {
|
|
539
|
+
if (utils.is_UnaryExpression(parent)) {
|
|
540
|
+
return true;
|
|
541
|
+
}
|
|
542
|
+
if (hasComment(node, 32 /* Line */)) {
|
|
543
|
+
if (utils.is_ReturnExpression(parent) || utils.is_YieldExpression(parent) && parent.expression === node) {
|
|
544
|
+
return true;
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
if (hasComment(node, 2 /* Leading */, (comment) => utils.is_Attribute(comment) && !comment.inner) && !utils.can_have_OuterAttributes(node, parent, true)) {
|
|
548
|
+
return true;
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
return false;
|
|
552
|
+
}
|
|
553
|
+
function needsInnerParens(node) {
|
|
554
|
+
if (needsOuterSoftbreakParens(node)) {
|
|
555
|
+
return false;
|
|
556
|
+
}
|
|
557
|
+
const parent = getParentNode();
|
|
558
|
+
if (!parent) {
|
|
559
|
+
return false;
|
|
560
|
+
}
|
|
561
|
+
if (utils.is_Identifier(node)) {
|
|
562
|
+
return false;
|
|
563
|
+
}
|
|
564
|
+
if (utils.is_Literal(node)) {
|
|
565
|
+
return utils.is_LiteralNumberLike(node) && utils.is_MemberExpression(parent) && node === parent.expression;
|
|
566
|
+
}
|
|
567
|
+
if (utils.is_CallExpression(parent) && parent.callee === node && utils.is_MemberExpression(node)) {
|
|
568
|
+
return !getOptions().actuallyMethodNodes.has(node);
|
|
569
|
+
}
|
|
570
|
+
if (utils.is_ReassignmentNode(node)) {
|
|
571
|
+
if (is_printing_macro()) {
|
|
572
|
+
return false;
|
|
573
|
+
}
|
|
574
|
+
if (utils.is_ClosureFunctionExpression(parent) && node === parent.expression) {
|
|
575
|
+
return true;
|
|
576
|
+
}
|
|
577
|
+
if (utils.is_ExpressionStatement(parent)) {
|
|
578
|
+
return utils.is_StructLiteral(node.left);
|
|
579
|
+
}
|
|
580
|
+
if (utils.is_ReassignmentNode(parent)) {
|
|
581
|
+
return false;
|
|
582
|
+
}
|
|
583
|
+
return true;
|
|
584
|
+
}
|
|
585
|
+
if (utils.is_ParenthesizedNode(parent)) {
|
|
586
|
+
return false;
|
|
587
|
+
}
|
|
588
|
+
if (utils.is_ExpressionStatement(parent)) {
|
|
589
|
+
return false;
|
|
590
|
+
}
|
|
591
|
+
if (utils.is_RangeLiteral(node)) {
|
|
592
|
+
return utils.is_ExpressionAsTypeCast(parent) || utils.is_LogicalExpression(parent) || utils.is_UnaryExpression(parent) || utils.is_PostfixExpression(parent) || utils.is_MemberExpression(parent) && node === parent.expression || utils.is_CallExpression(parent) && node === parent.callee || utils.is_OperationExpression(parent) || utils.is_ComparisonExpression(parent);
|
|
593
|
+
}
|
|
594
|
+
if (utils.is_LetScrutinee(parent) && utils.is_LogicalExpression(node) && parent.expression === node) {
|
|
595
|
+
return true;
|
|
596
|
+
}
|
|
597
|
+
if (utils.is_UnaryExpression(node)) {
|
|
598
|
+
switch (parent.nodeType) {
|
|
599
|
+
case jinxRust.NodeType.MemberExpression:
|
|
600
|
+
case jinxRust.NodeType.AwaitExpression:
|
|
601
|
+
return node === parent.expression;
|
|
602
|
+
case jinxRust.NodeType.CallExpression:
|
|
603
|
+
return node === parent.callee;
|
|
604
|
+
default:
|
|
605
|
+
return false;
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
if (utils.is_ExpressionWithBodyOrCases_or_BlockLikeMacroInvocation(node)) {
|
|
609
|
+
if (utils.is_ExpressionWithBodyOrCases(parent)) {
|
|
610
|
+
return !utils.is_ElseBlock(node, parent);
|
|
611
|
+
}
|
|
612
|
+
if (utils.is_LetScrutinee(parent) && parent.expression === node && utils.is_ExpressionWithBodyOrCases(getGrandParentNode())) {
|
|
613
|
+
return true;
|
|
614
|
+
}
|
|
615
|
+
return utils.is_ExpressionAsTypeCast(parent) || utils.is_LogicalExpression(parent) || utils.is_UnaryExpression(parent) || utils.is_PostfixExpression(parent) || utils.is_MemberExpression(parent) && node === parent.expression || utils.is_CallExpression(parent) && node === parent.callee || utils.is_OperationExpression(parent) || utils.is_ComparisonExpression(parent) || utils.is_RangeLiteral(parent);
|
|
616
|
+
}
|
|
617
|
+
if (utils.is_StructLiteral(node)) {
|
|
618
|
+
if (utils.is_ExpressionWithBodyOrCases(parent)) {
|
|
619
|
+
return true;
|
|
620
|
+
}
|
|
621
|
+
if (utils.is_LetScrutinee(parent) && parent.expression === node && utils.is_ExpressionWithBodyOrCases(getGrandParentNode())) {
|
|
622
|
+
return true;
|
|
623
|
+
}
|
|
624
|
+
if (utils.is_UnaryExpression(parent) || utils.is_PostfixExpression(parent) || utils.is_MemberExpression(parent)) {
|
|
625
|
+
return parent.expression === node;
|
|
626
|
+
}
|
|
627
|
+
if (utils.is_CallExpression(parent)) {
|
|
628
|
+
return parent.callee === node;
|
|
629
|
+
}
|
|
630
|
+
}
|
|
631
|
+
if (utils.is_LogicalExpression(node) || utils.is_OperationExpression(node) || utils.is_ComparisonExpression(node) || utils.is_ClosureFunctionExpression(node)) {
|
|
632
|
+
return precedenceNeedsParens(node, parent);
|
|
633
|
+
}
|
|
634
|
+
if (utils.is_TypeFunctionNode(node)) {
|
|
635
|
+
const gp = getGrandParentNode();
|
|
636
|
+
if (node.returnType && utils.is_TypeTraitBound(parent) && utils.is_TypeBoundsStandaloneNode(gp) && last_of(gp.typeBounds) !== parent) {
|
|
637
|
+
return true;
|
|
638
|
+
}
|
|
639
|
+
}
|
|
640
|
+
if (utils.is_TypeBoundsStandaloneNode(node)) {
|
|
641
|
+
return utils.is_UnaryType(parent) && node.typeBounds.length > 1 || utils.is_TypeBoundsStandaloneNode(parent) || utils.is_TypeTraitBound(parent) || utils.is_TypeFunctionNode(parent) && parent.returnType === node;
|
|
642
|
+
}
|
|
643
|
+
if (utils.is_PatternVariableDeclaration(parent)) {
|
|
644
|
+
return utils.is_UnionPattern(node);
|
|
645
|
+
}
|
|
646
|
+
return false;
|
|
647
|
+
}
|
|
648
|
+
function precedenceNeedsParens(node, parent) {
|
|
649
|
+
if (utils.is_UnaryExpression(parent) || utils.is_PostfixExpression(parent))
|
|
650
|
+
return true;
|
|
651
|
+
if (utils.is_ReassignmentNode(parent))
|
|
652
|
+
return parent.left === node;
|
|
653
|
+
if (utils.is_MemberExpression(parent))
|
|
654
|
+
return parent.expression === node;
|
|
655
|
+
if (utils.is_CallExpression(parent))
|
|
656
|
+
return parent.callee === node;
|
|
657
|
+
if (utils.is_ExpressionAsTypeCast(parent))
|
|
658
|
+
return !utils.is_ExpressionAsTypeCast(node);
|
|
659
|
+
if (utils.is_LogicalExpression(parent))
|
|
660
|
+
return utils.is_LogicalExpression(node) ? parent.nodeType !== node.nodeType : evalPrecedence(node, parent);
|
|
661
|
+
if (utils.is_OperationExpression(parent) || utils.is_ComparisonExpression(parent))
|
|
662
|
+
return evalPrecedence(node, parent);
|
|
663
|
+
return false;
|
|
664
|
+
function evalPrecedence(child, parent2) {
|
|
665
|
+
if (utils.is_ExpressionAsTypeCast(child) || utils.is_ClosureFunctionExpression(child)) {
|
|
666
|
+
return true;
|
|
667
|
+
}
|
|
668
|
+
function getPrec(node2, bool) {
|
|
669
|
+
return utils.getPrecedence(node2, bool);
|
|
670
|
+
}
|
|
671
|
+
const childPRCD = getPrec(child, is_insideScrutinee(child));
|
|
672
|
+
const parentPRCD = getPrec(parent2, is_insideScrutinee(parent2));
|
|
673
|
+
if (parentPRCD > childPRCD) {
|
|
674
|
+
return true;
|
|
675
|
+
}
|
|
676
|
+
if (parentPRCD === childPRCD && parent2.right === child) {
|
|
677
|
+
return true;
|
|
678
|
+
}
|
|
679
|
+
if (parentPRCD === childPRCD && !shouldFlatten(parent2, child)) {
|
|
680
|
+
return true;
|
|
681
|
+
}
|
|
682
|
+
if (parentPRCD < childPRCD && child.tk === jinxRust.TK["%"]) {
|
|
683
|
+
return parentPRCD === jinxRust.PRCD["+-"];
|
|
684
|
+
}
|
|
685
|
+
if (utils.is_BitwiseOperator(parent2.tk) || utils.is_BitwiseOperator(child.tk) && utils.is_EqualityOperator(parent2.tk)) {
|
|
686
|
+
return true;
|
|
687
|
+
}
|
|
688
|
+
return false;
|
|
689
|
+
}
|
|
690
|
+
}
|
|
691
|
+
function shouldFlatten(parent, node) {
|
|
692
|
+
if (utils.getPrecedence(node, is_insideScrutinee(node)) !== utils.getPrecedence(parent, is_insideScrutinee(parent)))
|
|
693
|
+
return false;
|
|
694
|
+
if (utils.is_ComparisonExpression(parent) && utils.is_ComparisonExpression(node))
|
|
695
|
+
return false;
|
|
696
|
+
if (utils.is_OperationExpression(parent) && utils.is_OperationExpression(node)) {
|
|
697
|
+
if (node.tk === jinxRust.TK["%"] && utils.is_multiplicativeOperator(parent.tk) || parent.tk === jinxRust.TK["%"] && utils.is_multiplicativeOperator(node.tk) || node.tk !== parent.tk && utils.is_multiplicativeOperator(node.tk) && utils.is_multiplicativeOperator(parent.tk) || utils.is_bitshiftOperator(node.tk) && utils.is_bitshiftOperator(parent.tk))
|
|
698
|
+
return false;
|
|
699
|
+
}
|
|
700
|
+
return true;
|
|
701
|
+
}
|
|
702
|
+
function needsParens(node) {
|
|
703
|
+
return needsOuterSoftbreakParens(node) || needsInnerParens(node);
|
|
704
|
+
}
|
|
705
|
+
function stmtNeedsSemi(stmt, disregardExprType = false) {
|
|
706
|
+
return pathCallParentOf(stmt, (parent) => needsSemi(parent, stmt, disregardExprType));
|
|
707
|
+
}
|
|
708
|
+
var NoNode = { nodeType: 0 };
|
|
709
|
+
function needsSemi(parent, stmt, disregardExprType = false) {
|
|
710
|
+
const expr = disregardExprType ? NoNode : stmt.expression;
|
|
711
|
+
const hadSemi = !disregardExprType && stmt.semi;
|
|
712
|
+
return !!expr && (forcePreserveSemi() ? true : shouldNeverSemi() ? false : shouldPreserveSemi() ? hadSemi || shouldAlwaysSemi() || canAutoCompleteSemi() : true);
|
|
713
|
+
function forcePreserveSemi() {
|
|
714
|
+
return hadSemi && stmt === last_of(parent.body) && (utils.is_IfBlockExpression(expr) && utils.hasLetScrutineeCondition(expr) && !(utils.is_LetScrutinee(expr.condition) && utils.is_Identifier(expr.condition.expression)) || utils.is_MatchExpression(expr) && !utils.is_Identifier(expr.expression));
|
|
715
|
+
}
|
|
716
|
+
function shouldNeverSemi() {
|
|
717
|
+
return utils.is_ExpressionWithBodyOrCases_or_BlockLikeMacroInvocation(expr);
|
|
718
|
+
}
|
|
719
|
+
function shouldPreserveSemi() {
|
|
720
|
+
return stmt === last_of(parent.body) && (utils.is_ImplicitReturnAbleNode(parent) || is_BlockLikeMacroInvocation(parent));
|
|
721
|
+
}
|
|
722
|
+
function shouldAlwaysSemi() {
|
|
723
|
+
return utils.is_FlowControlExpression(expr) || utils.is_ReassignmentNode(expr);
|
|
724
|
+
}
|
|
725
|
+
function canAutoCompleteSemi() {
|
|
726
|
+
return withPathAt(parent, function checkParent(child) {
|
|
727
|
+
return pathCallParentOf(child, (parent2) => {
|
|
728
|
+
if (utils.is_IfBlockExpression(parent2) && parent2.else === child) {
|
|
729
|
+
return checkParent(parent2);
|
|
730
|
+
}
|
|
731
|
+
if (utils.is_ExpressionStatement(parent2)) {
|
|
732
|
+
if (utils.hasOuterAttributes(parent2))
|
|
733
|
+
return false;
|
|
734
|
+
return stmtNeedsSemi(parent2, true);
|
|
735
|
+
}
|
|
736
|
+
if (utils.is_MatchExpressionCase(parent2) && parent2.expression === child) {
|
|
737
|
+
return pathCallParentOf(parent2, checkParent);
|
|
738
|
+
}
|
|
739
|
+
return false;
|
|
740
|
+
});
|
|
741
|
+
});
|
|
742
|
+
}
|
|
743
|
+
}
|
|
744
|
+
function canInlineBlockBody(node) {
|
|
745
|
+
if (!utils.is_ExpressionWithBody(node)) {
|
|
746
|
+
return false;
|
|
747
|
+
}
|
|
748
|
+
const body = node.body;
|
|
749
|
+
if (body.length === 0) {
|
|
750
|
+
return canInlineInlineable(node);
|
|
751
|
+
}
|
|
752
|
+
if (body.length === 1) {
|
|
753
|
+
const stmt = body[0];
|
|
754
|
+
if (utils.is_AttributeOrDocComment(stmt)) {
|
|
755
|
+
return true;
|
|
756
|
+
}
|
|
757
|
+
if (utils.is_ExpressionStatement(stmt) && !needsSemi(node, stmt)) {
|
|
758
|
+
const expr = stmt.expression;
|
|
759
|
+
if (utils.is_FlowControlExpression(expr) || //
|
|
760
|
+
utils.is_ClosureFunctionExpression(expr) || utils.is_ExpressionWithBodyOrCases_or_BlockLikeMacroInvocation(expr)) {
|
|
761
|
+
return false;
|
|
762
|
+
}
|
|
763
|
+
return canInlineInlineable(node);
|
|
764
|
+
}
|
|
765
|
+
}
|
|
766
|
+
return false;
|
|
767
|
+
}
|
|
768
|
+
function canInlineInlineable(node) {
|
|
769
|
+
if (utils.is_ForInBlockExpression(node) || utils.is_LoopBlockExpression(node)) {
|
|
770
|
+
return false;
|
|
771
|
+
}
|
|
772
|
+
if (utils.is_WhileBlockExpression(node)) {
|
|
773
|
+
return true;
|
|
774
|
+
}
|
|
775
|
+
const parent = getParentNode();
|
|
776
|
+
if (utils.is_ExpressionStatement(parent) && (!utils.is_ImplicitReturnAbleNode(node) || pathCallAtParent(parent, (parent2) => stmtNeedsSemi(parent2, true)))) {
|
|
777
|
+
return false;
|
|
778
|
+
}
|
|
779
|
+
if (utils.is_ElseBlock(node, parent)) {
|
|
780
|
+
return pathCallAtParent(parent, canInlineBlockBody);
|
|
781
|
+
}
|
|
782
|
+
if (utils.is_IfBlockExpression(node)) {
|
|
783
|
+
if (!node.else || // hasLetScrutineeCondition(node) ||
|
|
784
|
+
utils.is_ExpressionWithBodyOrCases_or_BlockLikeMacroInvocation(node.condition) || willBreak(getPrintFn()("condition"))) {
|
|
785
|
+
return false;
|
|
786
|
+
}
|
|
787
|
+
const grandparent = getGrandParentNode();
|
|
788
|
+
if (utils.is_ExpressionStatement(parent) && utils.hasBody(grandparent) && grandparent.body.length > 1) {
|
|
789
|
+
return false;
|
|
790
|
+
}
|
|
791
|
+
}
|
|
792
|
+
return true;
|
|
793
|
+
}
|
|
794
|
+
function emptyContent(node) {
|
|
795
|
+
switch (node.nodeType) {
|
|
796
|
+
case jinxRust.NodeType.Program:
|
|
797
|
+
case jinxRust.NodeType.MacroRulesDeclaration:
|
|
798
|
+
case jinxRust.NodeType.MacroDeclaration:
|
|
799
|
+
case jinxRust.NodeType.ExternBlockDeclaration:
|
|
800
|
+
case jinxRust.NodeType.ModuleDeclaration:
|
|
801
|
+
case jinxRust.NodeType.TraitDeclaration:
|
|
802
|
+
case jinxRust.NodeType.StructDeclaration:
|
|
803
|
+
case jinxRust.NodeType.MacroInvocation:
|
|
804
|
+
case jinxRust.NodeType.FunctionDeclaration:
|
|
805
|
+
case jinxRust.NodeType.ImplDeclaration:
|
|
806
|
+
case jinxRust.NodeType.UnionDeclaration:
|
|
807
|
+
case jinxRust.NodeType.EnumDeclaration:
|
|
808
|
+
case jinxRust.NodeType.EnumMemberStructDeclaration:
|
|
809
|
+
case jinxRust.NodeType.StructLiteral:
|
|
810
|
+
case jinxRust.NodeType.StructPattern:
|
|
811
|
+
return "";
|
|
812
|
+
case jinxRust.NodeType.BlockExpression:
|
|
813
|
+
case jinxRust.NodeType.WhileBlockExpression:
|
|
814
|
+
case jinxRust.NodeType.ForInBlockExpression:
|
|
815
|
+
case jinxRust.NodeType.TryBlockExpression:
|
|
816
|
+
case jinxRust.NodeType.IfBlockExpression:
|
|
817
|
+
return canInlineInlineable(node) ? utils.is_IfBlockExpression(node) || utils.is_ElseBlock(node, getParentNode()) ? softline : "" : hardline;
|
|
818
|
+
case jinxRust.NodeType.LoopBlockExpression:
|
|
819
|
+
case jinxRust.NodeType.MatchExpression:
|
|
820
|
+
return hardline;
|
|
821
|
+
default:
|
|
822
|
+
if (utils.is_NodeWithBodyNoBody(node)) {
|
|
823
|
+
return "";
|
|
824
|
+
}
|
|
825
|
+
return "";
|
|
826
|
+
}
|
|
827
|
+
}
|
|
828
|
+
function is_insideScrutinee(target) {
|
|
829
|
+
return withPathAt(target, (n) => stackIncludes("condition") && r(n));
|
|
830
|
+
function r(CHILD) {
|
|
831
|
+
switch (CHILD.nodeType) {
|
|
832
|
+
case jinxRust.NodeType.OrExpression:
|
|
833
|
+
case jinxRust.NodeType.AndExpression:
|
|
834
|
+
return pathCallParentOf(
|
|
835
|
+
CHILD,
|
|
836
|
+
(PARENT) => utils.hasCondition(PARENT) && PARENT.condition === CHILD ? utils.hasLetScrutineeCondition(PARENT) : r(PARENT)
|
|
837
|
+
);
|
|
838
|
+
case jinxRust.NodeType.LetScrutinee:
|
|
839
|
+
return true;
|
|
840
|
+
default:
|
|
841
|
+
return false;
|
|
842
|
+
}
|
|
843
|
+
}
|
|
844
|
+
}
|
|
845
|
+
function withPathAt(target, callback) {
|
|
846
|
+
if (target === getNode())
|
|
847
|
+
return callback(target);
|
|
848
|
+
if (target === getParentNode())
|
|
849
|
+
return pathCallAtParent(target, () => callback(target));
|
|
850
|
+
if (stackIncludes(target))
|
|
851
|
+
return pathCallAtParent(getParentNode(), () => withPathAt(target, callback));
|
|
852
|
+
return getContext().path.call(() => {
|
|
853
|
+
return callback(target);
|
|
854
|
+
}, ...utils.getAstPath(getNode(), target));
|
|
855
|
+
}
|
|
856
|
+
function shouldPrintOuterAttributesAbove(node) {
|
|
857
|
+
return utils.is_StatementNode(node) || utils.is_MatchExpressionCase(node) || utils.hasAttributes(node) && node.attributes.some(
|
|
858
|
+
canInlineOuterAttribute(node) ? (attr) => utils.is_DocCommentAttribute(attr) || hasBreaklineAfter(attr) : utils.is_DocCommentAttribute
|
|
859
|
+
);
|
|
860
|
+
function canInlineOuterAttribute(node2) {
|
|
861
|
+
return utils.is_EnumMemberDeclaration(node2) || utils.is_StructPropertyDeclaration(node2) || utils.is_StructLiteralProperty(node2) || utils.is_StructPatternProperty(node2);
|
|
862
|
+
}
|
|
863
|
+
}
|
|
864
|
+
|
|
865
|
+
// src/format/core.ts
|
|
866
|
+
function isNoopExpressionStatement(node) {
|
|
867
|
+
return utils.is_ExpressionStatement(node) && void 0 === node.expression && !utils.hasAttributes(node) && !hasComment(node);
|
|
868
|
+
}
|
|
869
|
+
function is_xVariableEqualishLike(node) {
|
|
870
|
+
switch (node.nodeType) {
|
|
871
|
+
case jinxRust.NodeType.LetScrutinee:
|
|
872
|
+
case jinxRust.NodeType.LetVariableDeclaration:
|
|
873
|
+
case jinxRust.NodeType.ConstVariableDeclaration:
|
|
874
|
+
case jinxRust.NodeType.StaticVariableDeclaration:
|
|
875
|
+
case jinxRust.NodeType.TypeAliasDeclaration:
|
|
876
|
+
case jinxRust.NodeType.TraitAliasDeclaration:
|
|
877
|
+
return true;
|
|
878
|
+
default:
|
|
879
|
+
return false;
|
|
880
|
+
}
|
|
881
|
+
}
|
|
882
|
+
function is_BinaryishExpression(node) {
|
|
883
|
+
switch (node.nodeType) {
|
|
884
|
+
case jinxRust.NodeType.OrExpression:
|
|
885
|
+
case jinxRust.NodeType.AndExpression:
|
|
886
|
+
case jinxRust.NodeType.OperationExpression:
|
|
887
|
+
case jinxRust.NodeType.ComparisonExpression:
|
|
888
|
+
return true;
|
|
889
|
+
default:
|
|
890
|
+
return false;
|
|
891
|
+
}
|
|
892
|
+
}
|
|
893
|
+
function is_StructSpread(node) {
|
|
894
|
+
switch (node.nodeType) {
|
|
895
|
+
case jinxRust.NodeType.StructLiteralPropertySpread:
|
|
896
|
+
case jinxRust.NodeType.StructLiteralRestUnassigned:
|
|
897
|
+
case jinxRust.NodeType.RestPattern:
|
|
898
|
+
return true;
|
|
899
|
+
default:
|
|
900
|
+
return false;
|
|
901
|
+
}
|
|
902
|
+
}
|
|
903
|
+
function isConciselyPrintedArray(node) {
|
|
904
|
+
return node.items.length > 1 && node.items.every(
|
|
905
|
+
(element) => (utils.is_LiteralNumberLike(element) || utils.is_MinusExpression(element) && utils.is_LiteralNumberLike(element.expression) && !hasComment(element.expression)) && !hasComment(element, 4 /* Trailing */ | 32 /* Line */, (comment) => !hasBreaklineBefore(comment))
|
|
906
|
+
);
|
|
907
|
+
}
|
|
908
|
+
function printNumber(rawNumber) {
|
|
909
|
+
return rawNumber.toLowerCase().replace(/^([\d.]+e)(?:\+|(-))?0*(\d)/, "$1$2$3").replace(/^(\d+)e[+-]?0+$/, "$1.0").replace(/^([\d.]+)e[+-]?0+$/, "$1").replace(/\.(\d+?)0+(?=e|$)/, ".$1").replace(/\.(?=e|$)/, ".0");
|
|
910
|
+
}
|
|
911
|
+
function printOnOwnLine(node, printed) {
|
|
912
|
+
return [printed, maybeEmptyLine(node)];
|
|
913
|
+
}
|
|
914
|
+
function maybeEmptyLine(node) {
|
|
915
|
+
return isNextLineEmpty(node) ? [hardline, hardline] : hardline;
|
|
916
|
+
}
|
|
917
|
+
function printBodyOrCases(print4, node) {
|
|
918
|
+
const p = [];
|
|
919
|
+
if (utils.is_MatchExpression(node)) {
|
|
920
|
+
pathCallEach(node, "cases", (mCase) => {
|
|
921
|
+
p.push({
|
|
922
|
+
node: mCase,
|
|
923
|
+
doc: utils.is_MatchExpressionCase(mCase) && !utils.is_ExpressionWithBodyOrCases(mCase.expression) ? [print4(), ","] : print4()
|
|
924
|
+
});
|
|
925
|
+
});
|
|
926
|
+
} else {
|
|
927
|
+
pathCallEach(node, "body", (stmt) => {
|
|
928
|
+
if (!isNoopExpressionStatement(stmt)) {
|
|
929
|
+
p.push({ node: stmt, doc: print4() });
|
|
930
|
+
}
|
|
931
|
+
});
|
|
932
|
+
}
|
|
933
|
+
const printed = bumpInnerAttributes(p).map(
|
|
934
|
+
({ doc, node: node2 }, i, a) => iLast(i, a) ? group(doc) : printOnOwnLine(node2, group(doc))
|
|
935
|
+
);
|
|
936
|
+
const comments = printDanglingCommentsForInline(node, "body" /* body */);
|
|
937
|
+
if (comments)
|
|
938
|
+
printed.push(comments);
|
|
939
|
+
const ccomments = printDanglingCommentsForInline(node, "cases" /* cases */);
|
|
940
|
+
if (ccomments)
|
|
941
|
+
printed.push(ccomments);
|
|
942
|
+
if (utils.is_Program(node) && utils.is_SourceFile(getParentNode()) && printed.length > 0 && !comments) {
|
|
943
|
+
printed.push(hardline);
|
|
944
|
+
}
|
|
945
|
+
return printed;
|
|
946
|
+
function bumpInnerAttributes(arr) {
|
|
947
|
+
return arr.sort((a, b) => utils.ownStart(a.node) - utils.ownStart(b.node));
|
|
948
|
+
}
|
|
949
|
+
}
|
|
950
|
+
function printMacroRules(print4, node) {
|
|
951
|
+
return !Array.isArray(node.rules) ? print4("rules") : node.rules.length > 0 ? [" {", indent([hardline, ...print4.join("rules", (rule) => maybeEmptyLine(rule))]), hardline, "}"] : [" {", printDanglingCommentsForInline(node, "rules" /* rules */) || emptyContent(node), "}"];
|
|
952
|
+
}
|
|
953
|
+
function is_unary_token(item) {
|
|
954
|
+
switch (item && utils.is_PunctuationToken(item) ? item.tk : jinxRust.TK.None) {
|
|
955
|
+
case jinxRust.TK["-"]:
|
|
956
|
+
case jinxRust.TK["*"]:
|
|
957
|
+
case jinxRust.TK["&"]:
|
|
958
|
+
case jinxRust.TK["#"]:
|
|
959
|
+
case jinxRust.TK["!"]:
|
|
960
|
+
case jinxRust.TK["~"]:
|
|
961
|
+
return true;
|
|
962
|
+
case jinxRust.TK["?"]:
|
|
963
|
+
return !/\s/.test(getOptions().originalText.charAt(utils.end(item)));
|
|
964
|
+
default:
|
|
965
|
+
return false;
|
|
966
|
+
}
|
|
967
|
+
}
|
|
968
|
+
function can_unary(node) {
|
|
969
|
+
return (!utils.is_PunctuationToken(node) || is_unary_token(node)) && (!utils.is_MacroGroup(node) || is_optional_unary(node));
|
|
970
|
+
}
|
|
971
|
+
function is_optional_token(item) {
|
|
972
|
+
return !!item && utils.is_MacroGroup(item) && item.kind === "?" && item.segments.length === 1 && utils.is_PunctuationToken(item.segments[0]);
|
|
973
|
+
}
|
|
974
|
+
function is_optional_unary(item) {
|
|
975
|
+
return is_optional_token(item) && is_unary_token(item.segments[0]);
|
|
976
|
+
}
|
|
977
|
+
function printRuleMatch(print4, rule) {
|
|
978
|
+
return print_map(rule, "match");
|
|
979
|
+
function print_map(node, property) {
|
|
980
|
+
const arr = node[property];
|
|
981
|
+
const shouldHug = should_hug(arr);
|
|
982
|
+
const dline = arr.dk === jinxRust.DelimKind["{}"] ? line : shouldHug ? "" : softline;
|
|
983
|
+
const isParamsLike = is_params_like(arr);
|
|
984
|
+
const shouldBreak = should_break(arr);
|
|
985
|
+
const d = utils.getDelimChars(arr);
|
|
986
|
+
if (arr.length === 0)
|
|
987
|
+
return [d.left, printDanglingCommentsForInline(node, DCM[property]), d.right];
|
|
988
|
+
const printed = flat(print4.map_join(property, print_item, join_item));
|
|
989
|
+
return group([d.left, !dline ? printed : [indent([dline, printed]), dline], d.right], {
|
|
990
|
+
shouldBreak,
|
|
991
|
+
id: getMacroGroupId(node)
|
|
992
|
+
});
|
|
993
|
+
function should_hug(arr2) {
|
|
994
|
+
if (node === rule)
|
|
995
|
+
return false;
|
|
996
|
+
let has_nonToken = false;
|
|
997
|
+
return arr2.every((item) => !utils.is_MacroGroup(item) && (utils.is_PunctuationToken(item) || has_nonToken !== (has_nonToken = true)));
|
|
998
|
+
}
|
|
999
|
+
function should_break(arr2) {
|
|
1000
|
+
let has_decl = false;
|
|
1001
|
+
return arr2.some(
|
|
1002
|
+
(item, i, a) => is_match_any(item) && arr2.length !== 1 || !iLast(i, a) && isDeclStart(item, a[i + 1]) && has_decl === (has_decl = true)
|
|
1003
|
+
);
|
|
1004
|
+
}
|
|
1005
|
+
function print_item(item, index, arr2) {
|
|
1006
|
+
switch (item.nodeType) {
|
|
1007
|
+
case jinxRust.NodeType.Identifier:
|
|
1008
|
+
case jinxRust.NodeType.LtIdentifier:
|
|
1009
|
+
case jinxRust.NodeType.Literal:
|
|
1010
|
+
case jinxRust.NodeType.PunctuationToken:
|
|
1011
|
+
case jinxRust.NodeType.MacroParameterDeclaration:
|
|
1012
|
+
return print4();
|
|
1013
|
+
case jinxRust.NodeType.MacroGroup:
|
|
1014
|
+
return printComments(["$", print_map(item, "segments"), print4("sep"), item.kind]);
|
|
1015
|
+
case jinxRust.NodeType.DelimGroup:
|
|
1016
|
+
return printComments(print_map(item, "segments"));
|
|
1017
|
+
}
|
|
1018
|
+
function printComments(doc) {
|
|
1019
|
+
const printed2 = withComments(item, doc);
|
|
1020
|
+
const comment = getFirstComment(item, 2 /* Leading */ | 32 /* Line */);
|
|
1021
|
+
return comment && index !== 0 ? isPreviousLineEmpty(comment) && typeof join_item(arr2[index - 1], item, index === 1 ? void 0 : arr2[index - 2]) === "string" ? [hardline, hardline, printed2] : [hardline, printed2] : printed2;
|
|
1022
|
+
}
|
|
1023
|
+
}
|
|
1024
|
+
function is_params_like(arr2) {
|
|
1025
|
+
return arr2.some(function isComma(item) {
|
|
1026
|
+
switch (item.nodeType) {
|
|
1027
|
+
case jinxRust.NodeType.PunctuationToken:
|
|
1028
|
+
return jinxRust.TK[","] === item.tk;
|
|
1029
|
+
case jinxRust.NodeType.MacroGroup:
|
|
1030
|
+
return !!item.sep && isComma(item.sep) || is_params_like(item.segments);
|
|
1031
|
+
}
|
|
1032
|
+
});
|
|
1033
|
+
}
|
|
1034
|
+
function join_item(item, next, prev) {
|
|
1035
|
+
if (utils.is_PunctuationToken(item)) {
|
|
1036
|
+
switch (item.tk) {
|
|
1037
|
+
case jinxRust.TK[","]:
|
|
1038
|
+
case jinxRust.TK[";"]:
|
|
1039
|
+
return line;
|
|
1040
|
+
case jinxRust.TK["::"]:
|
|
1041
|
+
case jinxRust.TK[".."]:
|
|
1042
|
+
case jinxRust.TK["..."]:
|
|
1043
|
+
case jinxRust.TK["."]:
|
|
1044
|
+
case jinxRust.TK["#"]:
|
|
1045
|
+
return "";
|
|
1046
|
+
case jinxRust.TK["!"]:
|
|
1047
|
+
if (prev && is_ident(prev) && utils.is_DelimGroup(next)) {
|
|
1048
|
+
return next.segments.dk === jinxRust.DelimKind["{}"] ? " " : "";
|
|
1049
|
+
}
|
|
1050
|
+
break;
|
|
1051
|
+
case jinxRust.TK["@"]:
|
|
1052
|
+
return is_ident(next) && (!prev || utils.is_MacroGroup(prev) || utils.is_DelimGroup(prev)) ? "" : " ";
|
|
1053
|
+
}
|
|
1054
|
+
return is_unary_token(item) && //
|
|
1055
|
+
(!prev || !is_ident(prev)) && can_unary(next) ? "" : " ";
|
|
1056
|
+
}
|
|
1057
|
+
switch (utils.is_PunctuationToken(next) ? next.tk : jinxRust.TK.None) {
|
|
1058
|
+
case jinxRust.TK[","]:
|
|
1059
|
+
case jinxRust.TK[";"]:
|
|
1060
|
+
case jinxRust.TK[":"]:
|
|
1061
|
+
case jinxRust.TK["::"]:
|
|
1062
|
+
case jinxRust.TK[".."]:
|
|
1063
|
+
case jinxRust.TK["..."]:
|
|
1064
|
+
case jinxRust.TK["."]:
|
|
1065
|
+
return "";
|
|
1066
|
+
case jinxRust.TK["!"]:
|
|
1067
|
+
if (is_ident(item)) {
|
|
1068
|
+
return "";
|
|
1069
|
+
}
|
|
1070
|
+
}
|
|
1071
|
+
if (is_match_any(item)) {
|
|
1072
|
+
return line;
|
|
1073
|
+
}
|
|
1074
|
+
{
|
|
1075
|
+
const sep_tk = utils.is_MacroGroup(item) && item.sep && utils.is_PunctuationToken(item.sep) ? item.sep.tk : jinxRust.TK.None;
|
|
1076
|
+
switch (sep_tk) {
|
|
1077
|
+
case jinxRust.TK["::"]:
|
|
1078
|
+
case jinxRust.TK["."]:
|
|
1079
|
+
return "";
|
|
1080
|
+
case jinxRust.TK[","]:
|
|
1081
|
+
case jinxRust.TK[";"]:
|
|
1082
|
+
return sep_tk === maybe_tk(next) ? ifBreak(line, " ", { groupId: getMacroGroupId(item) }) : line;
|
|
1083
|
+
}
|
|
1084
|
+
}
|
|
1085
|
+
if (is_optional_token(item)) {
|
|
1086
|
+
switch (item.segments[0].tk) {
|
|
1087
|
+
case jinxRust.TK["+"]:
|
|
1088
|
+
case jinxRust.TK["|"]:
|
|
1089
|
+
return " ";
|
|
1090
|
+
case jinxRust.TK["::"]:
|
|
1091
|
+
return "";
|
|
1092
|
+
}
|
|
1093
|
+
if (is_unary_token(item.segments[0])) {
|
|
1094
|
+
return "";
|
|
1095
|
+
}
|
|
1096
|
+
}
|
|
1097
|
+
if (utils.is_DelimGroup(item) || utils.is_MacroGroup(item)) {
|
|
1098
|
+
if (item.segments.dk === jinxRust.DelimKind["{}"]) {
|
|
1099
|
+
return line;
|
|
1100
|
+
}
|
|
1101
|
+
if (utils.is_MacroGroup(item) && item.segments.length === 2) {
|
|
1102
|
+
const { 0: left, 1: right } = item.segments;
|
|
1103
|
+
if (utils.is_PunctuationToken(left) && utils.is_DelimGroup(right) && left.tk === jinxRust.TK["#"] && right.segments.dk === jinxRust.DelimKind["[]"]) {
|
|
1104
|
+
return hardline;
|
|
1105
|
+
}
|
|
1106
|
+
}
|
|
1107
|
+
return isParamsLike || is_tk(next) ? " " : line;
|
|
1108
|
+
}
|
|
1109
|
+
const next_1 = next !== last_of(arr) && arr[arr.indexOf(next) + 1];
|
|
1110
|
+
if (is_ident(item) && utils.is_DelimGroup(next) && next.segments.dk === jinxRust.DelimKind["()"]) {
|
|
1111
|
+
if (!next_1 || !is_match_any(next_1)) {
|
|
1112
|
+
return "";
|
|
1113
|
+
}
|
|
1114
|
+
}
|
|
1115
|
+
if (is_match_any(next) && (!utils.is_DelimGroup(next) || next_1 && is_match_any(next_1))) {
|
|
1116
|
+
return line;
|
|
1117
|
+
}
|
|
1118
|
+
return " ";
|
|
1119
|
+
}
|
|
1120
|
+
}
|
|
1121
|
+
function is_ident(item) {
|
|
1122
|
+
switch (item.nodeType) {
|
|
1123
|
+
case jinxRust.NodeType.Identifier:
|
|
1124
|
+
return true;
|
|
1125
|
+
case jinxRust.NodeType.MacroParameterDeclaration:
|
|
1126
|
+
return item.ty.name === "ident";
|
|
1127
|
+
default:
|
|
1128
|
+
return false;
|
|
1129
|
+
}
|
|
1130
|
+
}
|
|
1131
|
+
function is_tk(item) {
|
|
1132
|
+
return utils.is_PunctuationToken(item) || is_optional_token(item);
|
|
1133
|
+
}
|
|
1134
|
+
function maybe_tk(item) {
|
|
1135
|
+
switch (item.nodeType) {
|
|
1136
|
+
case jinxRust.NodeType.PunctuationToken:
|
|
1137
|
+
return item.tk;
|
|
1138
|
+
case jinxRust.NodeType.MacroGroup:
|
|
1139
|
+
return is_optional_token(item) ? item.segments[0].tk : jinxRust.TK.None;
|
|
1140
|
+
default:
|
|
1141
|
+
return jinxRust.TK.None;
|
|
1142
|
+
}
|
|
1143
|
+
}
|
|
1144
|
+
function isDeclStart(item, next) {
|
|
1145
|
+
if (utils.is_Identifier(item)) {
|
|
1146
|
+
switch (item.name) {
|
|
1147
|
+
case "fn":
|
|
1148
|
+
case "mod":
|
|
1149
|
+
case "use":
|
|
1150
|
+
case "struct":
|
|
1151
|
+
case "trait":
|
|
1152
|
+
case "union":
|
|
1153
|
+
case "enum":
|
|
1154
|
+
case "impl":
|
|
1155
|
+
case "type":
|
|
1156
|
+
case "let":
|
|
1157
|
+
case "static":
|
|
1158
|
+
case "const":
|
|
1159
|
+
if (is_ident(next)) {
|
|
1160
|
+
return true;
|
|
1161
|
+
}
|
|
1162
|
+
}
|
|
1163
|
+
}
|
|
1164
|
+
return false;
|
|
1165
|
+
}
|
|
1166
|
+
function is_match_any(item) {
|
|
1167
|
+
return !!item && (utils.is_MacroGroup(item) && !item.sep && (item.kind === "*" || item.kind === "+") && item.segments.length === 1 && utils.is_MacroParameterDeclaration(item.segments[0]) && item.segments[0].ty.name === "tt" || utils.is_DelimGroup(item) && item.segments.length === 1 && is_match_any(item.segments[0]));
|
|
1168
|
+
}
|
|
1169
|
+
}
|
|
1170
|
+
function printRuleTransform(print4, node, t = utils.getDelimChars(node.transform)) {
|
|
1171
|
+
const text = node.transform.loc.sliceText();
|
|
1172
|
+
const fline = utils.is_MacroInlineRuleDeclaration(node) ? hardline : line;
|
|
1173
|
+
if (/^. *\n/.test(text)) {
|
|
1174
|
+
return [
|
|
1175
|
+
dedentToRoot([
|
|
1176
|
+
t.left,
|
|
1177
|
+
fline,
|
|
1178
|
+
text.slice(1, -1).replace(/^ *\n|\n\s*$/g, "")
|
|
1179
|
+
]),
|
|
1180
|
+
fline,
|
|
1181
|
+
t.right
|
|
1182
|
+
];
|
|
1183
|
+
} else if (/\n/.test(text) && node.transform.length === 1) {
|
|
1184
|
+
const segment = node.transform[0];
|
|
1185
|
+
if (utils.is_DelimGroup(segment) || utils.is_MacroGroup(segment)) {
|
|
1186
|
+
const inner = utils.is_DelimGroup(segment) ? utils.getDelimChars(segment.segments) : { left: "$(", right: `)${segment.sep?.loc.getOwnText() ?? ""}${segment.kind}` };
|
|
1187
|
+
return [
|
|
1188
|
+
dedentToRoot([
|
|
1189
|
+
t.left,
|
|
1190
|
+
[
|
|
1191
|
+
indent(indent([fline, inner.left])),
|
|
1192
|
+
line,
|
|
1193
|
+
segment.segments.loc.sliceText(1, -1).replace(/^ *\n|\n\s*$/g, ""),
|
|
1194
|
+
indent(indent([line, inner.right]))
|
|
1195
|
+
]
|
|
1196
|
+
]),
|
|
1197
|
+
fline,
|
|
1198
|
+
t.right
|
|
1199
|
+
];
|
|
1200
|
+
}
|
|
1201
|
+
}
|
|
1202
|
+
return text;
|
|
1203
|
+
}
|
|
1204
|
+
function is_AssignmentOrVariableDeclarator(node) {
|
|
1205
|
+
return utils.is_ReassignmentNode(node) || utils.is_VariableDeclarationNode(node);
|
|
1206
|
+
}
|
|
1207
|
+
function hasLeadingOwnLineComment(node) {
|
|
1208
|
+
if (utils.is_NodeWithBodyOrCases(node) && hasComment(node, 2 /* Leading */, utils.is_Attribute)) {
|
|
1209
|
+
return true;
|
|
1210
|
+
}
|
|
1211
|
+
return hasComment(
|
|
1212
|
+
node,
|
|
1213
|
+
2 /* Leading */,
|
|
1214
|
+
(comment) => hasNewline(utils.end(comment)) && !getContext().options.danglingAttributes.includes(comment)
|
|
1215
|
+
);
|
|
1216
|
+
}
|
|
1217
|
+
function isComplexDestructuring(node) {
|
|
1218
|
+
if (utils.is_ReassignmentExpression(node)) {
|
|
1219
|
+
const leftNode = node.left;
|
|
1220
|
+
return utils.is_StructLiteral(leftNode) && //
|
|
1221
|
+
leftNode.properties.length > 2 && leftNode.properties.some((property) => utils.is_StructLiteralProperty(property) || utils.is_StructLiteralPropertySpread(property));
|
|
1222
|
+
}
|
|
1223
|
+
if (utils.is_VariableDeclarationNode(node) || utils.is_MatchExpressionCase(node) || utils.is_LetScrutinee(node)) {
|
|
1224
|
+
const leftNode = node.pattern;
|
|
1225
|
+
return utils.is_StructPattern(leftNode) && //
|
|
1226
|
+
leftNode.properties.length > 2 && leftNode.properties.some((property) => utils.is_StructPatternPropertyDestructured(property));
|
|
1227
|
+
}
|
|
1228
|
+
return false;
|
|
1229
|
+
}
|
|
1230
|
+
function isArrowFunctionVariableDeclarator(node) {
|
|
1231
|
+
return utils.is_VariableDeclarationNode(node) && node.expression && utils.is_ClosureFunctionExpression(node.expression);
|
|
1232
|
+
}
|
|
1233
|
+
function isObjectPropertyWithShortKey(node, keyDoc) {
|
|
1234
|
+
if (!utils.is_StructProperty(node))
|
|
1235
|
+
return false;
|
|
1236
|
+
keyDoc = cleanDoc(keyDoc);
|
|
1237
|
+
const MIN_OVERLAP_FOR_BREAK = 3;
|
|
1238
|
+
return typeof keyDoc === "string" && keyDoc.length < getContext().options.tabWidth + MIN_OVERLAP_FOR_BREAK;
|
|
1239
|
+
}
|
|
1240
|
+
function print_CallExpression_end(print4, node) {
|
|
1241
|
+
return [f`::${printTypeArguments(print4, node)}`, printCallArguments(print4, node)];
|
|
1242
|
+
}
|
|
1243
|
+
function printCallExpression(print4, node) {
|
|
1244
|
+
if (shouldPrint_CallExpression_chain(node) && !pathCall(node, "callee", (node2) => needsParens(node2))) {
|
|
1245
|
+
return printMemberChain(print4, node);
|
|
1246
|
+
}
|
|
1247
|
+
const contents = [print4("callee"), ...print_CallExpression_end(print4, node)];
|
|
1248
|
+
if (is_CallExpression_or_CallLikeMacroInvocation(node.callee)) {
|
|
1249
|
+
return group(contents);
|
|
1250
|
+
}
|
|
1251
|
+
return contents;
|
|
1252
|
+
}
|
|
1253
|
+
function printTypeAnnotation(print4, node) {
|
|
1254
|
+
return node.typeAnnotation && !utils.is_MissingNode(node.typeAnnotation) ? [": ", print4("typeAnnotation")] : "";
|
|
1255
|
+
}
|
|
1256
|
+
function printAnnotatedPattern(print4, node) {
|
|
1257
|
+
return [print4("pattern"), printTypeAnnotation(print4, node)];
|
|
1258
|
+
}
|
|
1259
|
+
function isLoneShortArgument(node) {
|
|
1260
|
+
if (hasComment(node)) {
|
|
1261
|
+
return false;
|
|
1262
|
+
}
|
|
1263
|
+
if (utils.is_Identifier(node) && is_short(node.name) || utils.is_LiteralNumberLike(node) && !hasComment(node)) {
|
|
1264
|
+
return true;
|
|
1265
|
+
}
|
|
1266
|
+
if (utils.is_LiteralStringLike(node)) {
|
|
1267
|
+
return is_short(node.value) && !node.value.includes("\n");
|
|
1268
|
+
}
|
|
1269
|
+
return utils.is_LiteralBooleanLike(node);
|
|
1270
|
+
}
|
|
1271
|
+
var toLayout = ["break-after-operator", "never-break-after-operator", "fluid", "break-lhs", "chain", "chain-tail", "chain-tail-arrow-chain", "only-left"];
|
|
1272
|
+
function printMemberExpression(print4, node) {
|
|
1273
|
+
const objectDoc = print4("expression");
|
|
1274
|
+
const lookupDoc = printMemberLookup(print4, node);
|
|
1275
|
+
const shouldInline = shouldInlineMemberExpression(node, objectDoc);
|
|
1276
|
+
return label(objectDoc.label === "member-chain" ? "member-chain" : "member", [
|
|
1277
|
+
objectDoc,
|
|
1278
|
+
shouldInline ? lookupDoc : group(indent([softline, lookupDoc]))
|
|
1279
|
+
]);
|
|
1280
|
+
}
|
|
1281
|
+
function shouldInlineMemberExpression(node, objectDoc) {
|
|
1282
|
+
const { path } = getContext();
|
|
1283
|
+
const parent = getParentNode();
|
|
1284
|
+
let i = 0;
|
|
1285
|
+
let nmparent = parent;
|
|
1286
|
+
while (nmparent && (utils.is_MemberExpression(nmparent) || utils.is_PostfixExpression(nmparent))) {
|
|
1287
|
+
nmparent = path.getParentNode(i++);
|
|
1288
|
+
}
|
|
1289
|
+
const shouldInline = nmparent && (utils.is_ExpressionPath(nmparent) || utils.is_ReassignmentNode(nmparent) && !utils.is_Identifier(nmparent.left)) || !node.computed || utils.is_Identifier(node.expression) && utils.is_Identifier(node.property) && !utils.is_MemberExpression(parent) || is_AssignmentOrVariableDeclarator(parent) && (is_CallExpression_or_CallLikeMacroInvocation(node.expression) && node.expression.arguments.length > 0 || utils.is_PostfixExpression(node.expression) && is_CallExpression_or_CallLikeMacroInvocation(node.expression.expression) && node.expression.expression.arguments.length > 0 || objectDoc.label === "member-chain");
|
|
1290
|
+
return shouldInline;
|
|
1291
|
+
}
|
|
1292
|
+
function printAssignment(leftDoc, operator, rightPropertyName) {
|
|
1293
|
+
const assignmentNode = getNode();
|
|
1294
|
+
const rightNode = assignmentNode[rightPropertyName];
|
|
1295
|
+
if (!rightNode)
|
|
1296
|
+
return group(leftDoc);
|
|
1297
|
+
const layout = chooseLayout();
|
|
1298
|
+
const rightDoc = getPrintFn()(rightPropertyName, { assignmentLayout: layout });
|
|
1299
|
+
const res = function() {
|
|
1300
|
+
switch (layout) {
|
|
1301
|
+
case 0 /* break-after-operator */:
|
|
1302
|
+
return group([group(leftDoc), operator, group(indent([line, rightDoc]))]);
|
|
1303
|
+
case 1 /* never-break-after-operator */:
|
|
1304
|
+
return group([group(leftDoc), operator, " ", rightDoc]);
|
|
1305
|
+
case 2 /* fluid */: {
|
|
1306
|
+
const groupId = Symbol("assignment");
|
|
1307
|
+
return group([
|
|
1308
|
+
group(leftDoc),
|
|
1309
|
+
operator,
|
|
1310
|
+
//
|
|
1311
|
+
group(indent(line), { id: groupId }),
|
|
1312
|
+
lineSuffixBoundary,
|
|
1313
|
+
indentIfBreak(rightDoc, { groupId })
|
|
1314
|
+
]);
|
|
1315
|
+
}
|
|
1316
|
+
case 3 /* break-lhs */:
|
|
1317
|
+
return group([leftDoc, operator, " ", group(rightDoc)]);
|
|
1318
|
+
case 4 /* chain */:
|
|
1319
|
+
return [group(leftDoc), operator, line, rightDoc];
|
|
1320
|
+
case 5 /* chain-tail */:
|
|
1321
|
+
return [group(leftDoc), operator, indent([line, rightDoc])];
|
|
1322
|
+
case 6 /* chain-tail-arrow-chain */:
|
|
1323
|
+
return [group(leftDoc), operator, rightDoc];
|
|
1324
|
+
default:
|
|
1325
|
+
exit.never();
|
|
1326
|
+
}
|
|
1327
|
+
}();
|
|
1328
|
+
return label(toLayout[layout], res);
|
|
1329
|
+
function chooseLayout() {
|
|
1330
|
+
if (utils.is_ReassignmentExpression(assignmentNode) && is_printing_macro() || utils.is_GenericParameterDeclaration(assignmentNode) || utils.is_TypeCallNamedArgument(assignmentNode)) {
|
|
1331
|
+
return 1 /* never-break-after-operator */;
|
|
1332
|
+
}
|
|
1333
|
+
const isTail = !utils.is_ReassignmentNode(rightNode);
|
|
1334
|
+
const shouldUseChainFormatting = getContext().path.match(
|
|
1335
|
+
utils.is_ReassignmentNode,
|
|
1336
|
+
is_AssignmentOrVariableDeclarator,
|
|
1337
|
+
(node) => !isTail || !utils.is_ExpressionStatement(node) && !utils.is_VariableDeclarationNode(node)
|
|
1338
|
+
);
|
|
1339
|
+
if (shouldUseChainFormatting) {
|
|
1340
|
+
return !isTail ? 4 /* chain */ : utils.is_ClosureFunctionExpression(rightNode) && utils.is_ClosureFunctionExpression(rightNode.expression) ? 6 /* chain-tail-arrow-chain */ : 5 /* chain-tail */;
|
|
1341
|
+
}
|
|
1342
|
+
const isHeadOfLongChain = !isTail && utils.is_ReassignmentNode(rightNode.right);
|
|
1343
|
+
if (isHeadOfLongChain || hasLeadingOwnLineComment(rightNode)) {
|
|
1344
|
+
return 0 /* break-after-operator */;
|
|
1345
|
+
}
|
|
1346
|
+
if (isComplexDestructuring(assignmentNode) || hasComplexGenerics(assignmentNode) || hasComplexTypeAnnotation(assignmentNode) || isArrowFunctionVariableDeclarator(assignmentNode) && canBreak(leftDoc)) {
|
|
1347
|
+
return 3 /* break-lhs */;
|
|
1348
|
+
}
|
|
1349
|
+
const hasShortKey = isObjectPropertyWithShortKey(assignmentNode, leftDoc);
|
|
1350
|
+
if (pathCall(assignmentNode, rightPropertyName, (rightNode2) => shouldBreakAfterOperator(rightNode2, hasShortKey))) {
|
|
1351
|
+
return 0 /* break-after-operator */;
|
|
1352
|
+
}
|
|
1353
|
+
if (hasShortKey || utils.is_Literal(rightNode)) {
|
|
1354
|
+
return 1 /* never-break-after-operator */;
|
|
1355
|
+
}
|
|
1356
|
+
return 2 /* fluid */;
|
|
1357
|
+
}
|
|
1358
|
+
function shouldBreakAfterOperator(rightNode2, hasShortKey) {
|
|
1359
|
+
if (utils.is_MemberExpression(rightNode2) && shouldInlineMemberExpression(rightNode2, getPrintFn()("expression"))) {
|
|
1360
|
+
return false;
|
|
1361
|
+
}
|
|
1362
|
+
if (is_BinaryishExpression(rightNode2) && !shouldInlineLogicalExpression(rightNode2)) {
|
|
1363
|
+
return true;
|
|
1364
|
+
}
|
|
1365
|
+
if (utils.is_IfBlockExpression(rightNode2)) {
|
|
1366
|
+
return false;
|
|
1367
|
+
}
|
|
1368
|
+
if (hasShortKey) {
|
|
1369
|
+
return false;
|
|
1370
|
+
}
|
|
1371
|
+
return function unwrap(node) {
|
|
1372
|
+
if (utils.is_UnaryExpression(node) || utils.is_PostfixExpression(node)) {
|
|
1373
|
+
return pathCall(node, "expression", unwrap);
|
|
1374
|
+
}
|
|
1375
|
+
if (utils.is_LiteralStringLike(node)) {
|
|
1376
|
+
return true;
|
|
1377
|
+
}
|
|
1378
|
+
return isPoorlyBreakableMemberOrCallChain(node);
|
|
1379
|
+
}(rightNode2);
|
|
1380
|
+
function isPoorlyBreakableMemberOrCallChain(topNode) {
|
|
1381
|
+
return function unwrap(node) {
|
|
1382
|
+
if (utils.is_MemberExpression(node) || utils.is_PostfixExpression(node) || utils.is_UnaryExpression(node)) {
|
|
1383
|
+
return pathCall(node, "expression", unwrap);
|
|
1384
|
+
}
|
|
1385
|
+
if (utils.is_ExpressionPath(node)) {
|
|
1386
|
+
return pathCall(node, "namespace", (namespace) => !namespace || unwrap(namespace));
|
|
1387
|
+
}
|
|
1388
|
+
if (is_CallExpression_or_CallLikeMacroInvocation(node)) {
|
|
1389
|
+
const doc = printCallExpression(getPrintFn(), node);
|
|
1390
|
+
if (doc.label === "member-chain") {
|
|
1391
|
+
return false;
|
|
1392
|
+
}
|
|
1393
|
+
const args = node.arguments;
|
|
1394
|
+
const isPoorlyBreakableCall = args.length === 0 || args.length === 1 && isLoneShortArgument(args[0]);
|
|
1395
|
+
if (!isPoorlyBreakableCall) {
|
|
1396
|
+
return false;
|
|
1397
|
+
}
|
|
1398
|
+
if (hasComplexTypeArguments(node)) {
|
|
1399
|
+
return false;
|
|
1400
|
+
}
|
|
1401
|
+
return pathCall(node, "callee", unwrap);
|
|
1402
|
+
}
|
|
1403
|
+
return topNode === node ? false : utils.is_Identifier(node);
|
|
1404
|
+
}(topNode);
|
|
1405
|
+
}
|
|
1406
|
+
}
|
|
1407
|
+
}
|
|
1408
|
+
function is_MemberExpression_with_RangeOrLiteral_Property(node) {
|
|
1409
|
+
return !!node && utils.is_MemberExpression(node) && (node.computed ? is_Literal_or_SimpleRangeLiteral(node.property) : utils.is_Literal(node.property));
|
|
1410
|
+
}
|
|
1411
|
+
function is_Literal_or_SimpleRangeLiteral(node) {
|
|
1412
|
+
return utils.is_Literal(node) ? true : utils.is_RangeLiteral(node) ? (!node.lower || utils.is_Literal(node.lower)) && (!node.upper || utils.is_Literal(node.upper)) : false;
|
|
1413
|
+
}
|
|
1414
|
+
function printMemberLookup(print4, node) {
|
|
1415
|
+
return !node.computed ? [".", print4("property")] : is_Literal_or_SimpleRangeLiteral(node.property) ? ["[", print4("property"), "]"] : group(["[", indent([softline, print4("property")]), softline, "]"]);
|
|
1416
|
+
}
|
|
1417
|
+
function shouldPrint_CallExpression_chain(node) {
|
|
1418
|
+
return is_MemberAccessLike(node.callee) || is_CallExpression_or_CallLikeMacroInvocation(node.callee);
|
|
1419
|
+
}
|
|
1420
|
+
function is_MemberAccessLike(node) {
|
|
1421
|
+
switch (node.nodeType) {
|
|
1422
|
+
case jinxRust.NodeType.ExpressionPath:
|
|
1423
|
+
case jinxRust.NodeType.MemberExpression:
|
|
1424
|
+
return true;
|
|
1425
|
+
default:
|
|
1426
|
+
return false;
|
|
1427
|
+
}
|
|
1428
|
+
}
|
|
1429
|
+
function printMemberChain(print4, node) {
|
|
1430
|
+
const parent = getParentNode();
|
|
1431
|
+
const isExpressionStatement = !parent || utils.is_ExpressionStatement(parent);
|
|
1432
|
+
const { printedNodes, groups } = splitCallChains(node);
|
|
1433
|
+
const shouldMerge = groups.length >= 2 && !hasComment(groups[1][0].node) && shouldNotWrap(groups);
|
|
1434
|
+
const printedGroups = groups.map(printGroup);
|
|
1435
|
+
const oneLine = printedGroups;
|
|
1436
|
+
const cutoff = shouldMerge ? 3 : 2;
|
|
1437
|
+
const nodeHasComment = printedNodes.slice(1, -1).some(({ node: node2 }) => hasComment(node2, 2 /* Leading */)) || printedNodes.slice(0, -1).some(({ node: node2 }) => hasComment(node2, 4 /* Trailing */)) || groups[cutoff] && hasComment(groups[cutoff][0].node, 2 /* Leading */);
|
|
1438
|
+
if (groups.length <= cutoff && !nodeHasComment) {
|
|
1439
|
+
return isLongCurriedCallExpression(node) ? oneLine : group(oneLine);
|
|
1440
|
+
}
|
|
1441
|
+
const lastNodeBeforeIndent = last_of(groups[shouldMerge ? 1 : 0]).node;
|
|
1442
|
+
const shouldHaveEmptyLineBeforeIndent = !is_CallExpression_or_CallLikeMacroInvocation(lastNodeBeforeIndent) && shouldInsertEmptyLineAfter(lastNodeBeforeIndent);
|
|
1443
|
+
const expanded = [
|
|
1444
|
+
printGroup(groups[0]),
|
|
1445
|
+
shouldMerge ? groups.slice(1, 2).map(printGroup) : "",
|
|
1446
|
+
shouldHaveEmptyLineBeforeIndent ? hardline : "",
|
|
1447
|
+
printIndentedGroup(groups.slice(shouldMerge ? 2 : 1))
|
|
1448
|
+
];
|
|
1449
|
+
const callExpressions = printedNodes.map(({ node: node2 }) => node2).filter(is_CallExpression_or_CallLikeMacroInvocation);
|
|
1450
|
+
const result = nodeHasComment || callExpressions.length > 2 && callExpressions.some((expr) => expr.arguments.some((arg) => !isSimpleCallArgument(arg, 0))) || printedGroups.slice(0, -1).some(willBreak) || lastGroupWillBreakAndOtherCallsHaveFunctionArguments() ? group(expanded) : [shouldHaveEmptyLineBeforeIndent || willBreak(oneLine) ? breakParent : "", conditionalGroup([oneLine, expanded])];
|
|
1451
|
+
return label("member-chain", result);
|
|
1452
|
+
function shouldInsertEmptyLineAfter(node2) {
|
|
1453
|
+
let start8 = utils.end(node2);
|
|
1454
|
+
const last = getNextNonSpaceNonCommentCharacterIndex(node2);
|
|
1455
|
+
const { originalText } = getContext().options;
|
|
1456
|
+
while (start8 < last) {
|
|
1457
|
+
if (originalText.charAt(start8) === ")") {
|
|
1458
|
+
return isNextLineEmptyAfterIndex(start8 + 1);
|
|
1459
|
+
}
|
|
1460
|
+
start8++;
|
|
1461
|
+
}
|
|
1462
|
+
return isNextLineEmpty(node2);
|
|
1463
|
+
}
|
|
1464
|
+
function isFactory(name) {
|
|
1465
|
+
return /^[A-Z]|^[$_]+$/.test(name);
|
|
1466
|
+
}
|
|
1467
|
+
function isShort(name) {
|
|
1468
|
+
return name.length <= getContext().options.tabWidth;
|
|
1469
|
+
}
|
|
1470
|
+
function shouldNotWrap(groups2) {
|
|
1471
|
+
const hasComputed = groups2[1].length > 0 && utils.is_MemberExpression(groups2[1][0].node) && groups2[1][0].node.computed;
|
|
1472
|
+
if (groups2[0].length === 1) {
|
|
1473
|
+
const firstNode = groups2[0][0].node;
|
|
1474
|
+
return utils.is_Identifier(firstNode) && (isFactory(firstNode.name) || isExpressionStatement && isShort(firstNode.name) || hasComputed);
|
|
1475
|
+
}
|
|
1476
|
+
const lastNode = last_of(groups2[0]).node;
|
|
1477
|
+
const lastNodeLeft = utils.is_ExpressionPath(lastNode) ? lastNode.namespace : utils.is_MemberExpression(lastNode) ? lastNode.expression : void 0;
|
|
1478
|
+
return lastNodeLeft && utils.is_Identifier(lastNodeLeft) && (isFactory(lastNodeLeft.name) || hasComputed);
|
|
1479
|
+
}
|
|
1480
|
+
function printGroup(g) {
|
|
1481
|
+
const printed = [];
|
|
1482
|
+
if (printedNodes[0] === g[0]) {
|
|
1483
|
+
for (const item of printedNodes) {
|
|
1484
|
+
if (item.needsParens)
|
|
1485
|
+
printed.unshift("(");
|
|
1486
|
+
}
|
|
1487
|
+
}
|
|
1488
|
+
for (const item of g) {
|
|
1489
|
+
printed.push(item.printed);
|
|
1490
|
+
if (item.needsParens)
|
|
1491
|
+
printed.push(")");
|
|
1492
|
+
}
|
|
1493
|
+
return printed;
|
|
1494
|
+
}
|
|
1495
|
+
function printIndentedGroup(groups2) {
|
|
1496
|
+
if (groups2.length === 0)
|
|
1497
|
+
return "";
|
|
1498
|
+
return indent(group([hardline, join(hardline, groups2.map(printGroup))]));
|
|
1499
|
+
}
|
|
1500
|
+
function lastGroupWillBreakAndOtherCallsHaveFunctionArguments() {
|
|
1501
|
+
const lastGroupNode = last_of(last_of(groups)).node;
|
|
1502
|
+
const lastGroupDoc = last_of(printedGroups);
|
|
1503
|
+
return is_CallExpression_or_CallLikeMacroInvocation(lastGroupNode) && willBreak(lastGroupDoc) && callExpressions.slice(0, -1).some((node2) => node2.arguments.some(utils.is_ClosureFunctionExpression));
|
|
1504
|
+
}
|
|
1505
|
+
function splitCallChains(topNode) {
|
|
1506
|
+
const printedNodes2 = [
|
|
1507
|
+
{
|
|
1508
|
+
node: topNode,
|
|
1509
|
+
needsParens: false,
|
|
1510
|
+
printed: print_CallExpression_end(print4, node)
|
|
1511
|
+
}
|
|
1512
|
+
];
|
|
1513
|
+
pathCall(topNode, "callee", function READ_LEFT(node2) {
|
|
1514
|
+
if (is_CallExpression_or_CallLikeMacroInvocation(node2) && shouldPrint_CallExpression_chain(node2)) {
|
|
1515
|
+
unshift(print_CallExpression_end(print4, node2), shouldInsertEmptyLineAfter(node2));
|
|
1516
|
+
pathCall(node2, "callee", READ_LEFT);
|
|
1517
|
+
} else if (utils.is_MemberExpression(node2)) {
|
|
1518
|
+
unshift(printMemberLookup(print4, node2));
|
|
1519
|
+
pathCall(node2, "expression", READ_LEFT);
|
|
1520
|
+
} else if (utils.is_ExpressionPath(node2)) {
|
|
1521
|
+
unshift(["::", print4("segment")]);
|
|
1522
|
+
if (node2.namespace) {
|
|
1523
|
+
pathCall(node2, "namespace", READ_LEFT);
|
|
1524
|
+
}
|
|
1525
|
+
} else if (utils.is_PostfixExpression(node2)) {
|
|
1526
|
+
unshift(utils.is_UnwrapExpression(node2) ? "?" : ".await");
|
|
1527
|
+
pathCall(node2, "expression", READ_LEFT);
|
|
1528
|
+
} else {
|
|
1529
|
+
printedNodes2.unshift({ node: node2, needsParens: false, printed: print4() });
|
|
1530
|
+
}
|
|
1531
|
+
function unshift(printed, needsHardlineAfter = false) {
|
|
1532
|
+
printedNodes2.unshift({
|
|
1533
|
+
node: node2,
|
|
1534
|
+
needsParens: is_MemberAccessLike(node2) && needsParens(node2),
|
|
1535
|
+
printed: [withComments(node2, printed), needsHardlineAfter ? hardline : ""]
|
|
1536
|
+
});
|
|
1537
|
+
}
|
|
1538
|
+
});
|
|
1539
|
+
const groups2 = spread(function* () {
|
|
1540
|
+
let i = 0;
|
|
1541
|
+
let currentItem = printedNodes2[i];
|
|
1542
|
+
function testNextItem(fn) {
|
|
1543
|
+
return i + 1 < printedNodes2.length && fn(printedNodes2[i + 1]);
|
|
1544
|
+
}
|
|
1545
|
+
function readGroup(fn) {
|
|
1546
|
+
return spread(function* () {
|
|
1547
|
+
for (var _item of fn()) {
|
|
1548
|
+
yield currentItem;
|
|
1549
|
+
if (++i < printedNodes2.length)
|
|
1550
|
+
currentItem = printedNodes2[i];
|
|
1551
|
+
else
|
|
1552
|
+
break;
|
|
1553
|
+
}
|
|
1554
|
+
});
|
|
1555
|
+
}
|
|
1556
|
+
function* loop(condition) {
|
|
1557
|
+
while (condition(currentItem))
|
|
1558
|
+
yield currentItem;
|
|
1559
|
+
}
|
|
1560
|
+
function* until(condition) {
|
|
1561
|
+
while (!condition(currentItem))
|
|
1562
|
+
yield currentItem;
|
|
1563
|
+
}
|
|
1564
|
+
yield readGroup(function* () {
|
|
1565
|
+
const isCallExpression = is_CallExpression_or_CallLikeMacroInvocation(currentItem.node);
|
|
1566
|
+
yield currentItem;
|
|
1567
|
+
yield* loop(
|
|
1568
|
+
({ node: node2, needsParens: needsParens2 }) => utils.is_PostfixExpression(node2) || is_CallExpression_or_CallLikeMacroInvocation(node2) || is_MemberExpression_with_RangeOrLiteral_Property(node2) || needsParens2
|
|
1569
|
+
);
|
|
1570
|
+
if (!isCallExpression) {
|
|
1571
|
+
yield* loop(
|
|
1572
|
+
({ node: node2, needsParens: needsParens2 }) => is_MemberAccessLike(node2) && //
|
|
1573
|
+
testNextItem(({ node: node3 }) => is_MemberAccessLike(node3))
|
|
1574
|
+
);
|
|
1575
|
+
}
|
|
1576
|
+
});
|
|
1577
|
+
while (i < printedNodes2.length) {
|
|
1578
|
+
yield readGroup(function* () {
|
|
1579
|
+
let isCallExpression = false;
|
|
1580
|
+
yield* until(
|
|
1581
|
+
({ node: node2 }) => (isCallExpression = is_CallExpression_or_CallLikeMacroInvocation(node2)) || //
|
|
1582
|
+
hasComment(node2, 4 /* Trailing */)
|
|
1583
|
+
);
|
|
1584
|
+
yield currentItem;
|
|
1585
|
+
if (isCallExpression) {
|
|
1586
|
+
yield* loop(({ node: node2 }) => is_MemberExpression_with_RangeOrLiteral_Property(node2));
|
|
1587
|
+
yield* until(
|
|
1588
|
+
({ node: node2 }) => is_MemberAccessLike(node2) || //
|
|
1589
|
+
hasComment(node2, 4 /* Trailing */)
|
|
1590
|
+
);
|
|
1591
|
+
}
|
|
1592
|
+
});
|
|
1593
|
+
}
|
|
1594
|
+
});
|
|
1595
|
+
return { printedNodes: printedNodes2, groups: groups2 };
|
|
1596
|
+
}
|
|
1597
|
+
}
|
|
1598
|
+
function isSimpleCallArgument(node, depth) {
|
|
1599
|
+
if (depth >= 2)
|
|
1600
|
+
return false;
|
|
1601
|
+
if (utils.is_IdentifierOrIndex(node)) {
|
|
1602
|
+
return true;
|
|
1603
|
+
}
|
|
1604
|
+
if (utils.is_Literal(node)) {
|
|
1605
|
+
return !utils.is_LiteralStringLike(node) || !node.value.includes("\n");
|
|
1606
|
+
}
|
|
1607
|
+
if (utils.is_ArrayOrTupleLiteral(node)) {
|
|
1608
|
+
return node.items.every(isChildSimple);
|
|
1609
|
+
}
|
|
1610
|
+
if (utils.is_StructLiteral(node)) {
|
|
1611
|
+
return isSimpleCallArgument(node.struct, depth) && node.properties.every(
|
|
1612
|
+
(prop) => utils.is_StructLiteralPropertySpread(prop) ? isChildSimple(prop.expression) : utils.is_StructLiteralProperty(prop) ? isChildSimple(prop.value) : true
|
|
1613
|
+
);
|
|
1614
|
+
}
|
|
1615
|
+
if (is_CallExpression_or_CallLikeMacroInvocation(node)) {
|
|
1616
|
+
return isSimpleCallArgument(node.callee, depth) && (node.typeArguments ?? []).every(isChildSimple) && node.arguments.every(isChildSimple);
|
|
1617
|
+
}
|
|
1618
|
+
if (utils.is_MemberExpression(node)) {
|
|
1619
|
+
return isSimpleCallArgument(node.expression, depth) && isSimpleCallArgument(node.property, depth);
|
|
1620
|
+
}
|
|
1621
|
+
if (utils.is_ExpressionTypeCast(node)) {
|
|
1622
|
+
return isSimpleCallArgument(node.typeCallee, depth) && node.typeArguments.every(isChildSimple);
|
|
1623
|
+
}
|
|
1624
|
+
if (utils.is_ExpressionPath(node)) {
|
|
1625
|
+
const namespace = node.namespace;
|
|
1626
|
+
return !namespace || isSimpleCallArgument(namespace, depth);
|
|
1627
|
+
}
|
|
1628
|
+
if (utils.is_UnaryExpression(node) || utils.is_PostfixExpression(node)) {
|
|
1629
|
+
return isSimpleCallArgument(node.expression, depth);
|
|
1630
|
+
}
|
|
1631
|
+
return false;
|
|
1632
|
+
function isChildSimple(child) {
|
|
1633
|
+
return isSimpleCallArgument(child, depth + 1);
|
|
1634
|
+
}
|
|
1635
|
+
}
|
|
1636
|
+
function isLongCurriedCallExpression(node) {
|
|
1637
|
+
const parent = getParentNode();
|
|
1638
|
+
return is_CallExpression_or_CallLikeMacroInvocation(node) && is_CallExpression_or_CallLikeMacroInvocation(parent) && parent.callee === node && node.arguments.length > parent.arguments.length && parent.arguments.length > 0;
|
|
1639
|
+
}
|
|
1640
|
+
function printTypeArguments(print4, node) {
|
|
1641
|
+
return !node.typeArguments ? "" : node.typeArguments.length === 0 ? ["<", printDanglingCommentsForInline(node, "typeArguments" /* typeArguments */), ">"] : hasComplexTypeArguments(node) ? group(
|
|
1642
|
+
[
|
|
1643
|
+
"<",
|
|
1644
|
+
//
|
|
1645
|
+
indent([softline, print4.join("typeArguments", [",", line])]),
|
|
1646
|
+
softline,
|
|
1647
|
+
">"
|
|
1648
|
+
],
|
|
1649
|
+
{ id: getTypeParametersGroupId(node) }
|
|
1650
|
+
) : ["<", print4.join("typeArguments", ", "), ">"];
|
|
1651
|
+
}
|
|
1652
|
+
function printLtParameters(print4, node) {
|
|
1653
|
+
return !node.ltParameters ? "" : node.ltParameters.length === 0 ? ["for<", printDanglingCommentsForInline(node, "ltParameters" /* ltParameters */), "> "] : hasComplexLtParameters(node) ? group(
|
|
1654
|
+
[
|
|
1655
|
+
"for<",
|
|
1656
|
+
//
|
|
1657
|
+
indent([softline, print4.join("ltParameters", [",", line])]),
|
|
1658
|
+
softline,
|
|
1659
|
+
"> "
|
|
1660
|
+
],
|
|
1661
|
+
{ id: getTypeParametersGroupId(node) }
|
|
1662
|
+
) : ["for<", print4.join("ltParameters", ", "), "> "];
|
|
1663
|
+
}
|
|
1664
|
+
function printGenerics(print4, node) {
|
|
1665
|
+
return group(
|
|
1666
|
+
!node.generics ? "" : hasComplexGenerics(node) ? [
|
|
1667
|
+
"<",
|
|
1668
|
+
indent([softline, print4.join("generics", [",", line])]),
|
|
1669
|
+
//
|
|
1670
|
+
hasMultipleHeritage(node) ? indent([softline, ">"]) : [softline, ">"]
|
|
1671
|
+
] : [
|
|
1672
|
+
"<",
|
|
1673
|
+
print4.join("generics", ", "),
|
|
1674
|
+
//
|
|
1675
|
+
printDanglingCommentsForInline(node, "generics" /* generics */),
|
|
1676
|
+
">"
|
|
1677
|
+
]
|
|
1678
|
+
);
|
|
1679
|
+
}
|
|
1680
|
+
function getPrintedTypeBounds(print4, node) {
|
|
1681
|
+
if (!utils.hasTypeBounds(node) || node.typeBounds.length === 0)
|
|
1682
|
+
return "";
|
|
1683
|
+
if (node.typeBounds.length === 1)
|
|
1684
|
+
return print4.map("typeBounds");
|
|
1685
|
+
const printed = print4.join("typeBounds", (_, __, prev) => !prev ? " +" : [" +", line]);
|
|
1686
|
+
return [printed.shift(), indent([line, printed])];
|
|
1687
|
+
}
|
|
1688
|
+
function printTypeBounds(operator, print4, node) {
|
|
1689
|
+
if (!utils.hasTypeBounds(node))
|
|
1690
|
+
return "";
|
|
1691
|
+
const printed = getPrintedTypeBounds(print4, node);
|
|
1692
|
+
return printed ? group([operator, " ", printed]) : operator;
|
|
1693
|
+
}
|
|
1694
|
+
function printLtBounds(left, print4, node) {
|
|
1695
|
+
return group(
|
|
1696
|
+
!node.ltBounds ? "" : node.ltBounds.length === 0 ? [left, " "] : [left, " ", print4.map("ltBounds", (typeBound, i) => i === 0 ? print4() : indent([line, "+ ", print4()]))]
|
|
1697
|
+
);
|
|
1698
|
+
}
|
|
1699
|
+
function printWhereBounds(print4, node) {
|
|
1700
|
+
if (!node.whereBounds || node.whereBounds.length === 0)
|
|
1701
|
+
return "";
|
|
1702
|
+
return adjustDeclarationClause(
|
|
1703
|
+
node,
|
|
1704
|
+
//
|
|
1705
|
+
"where",
|
|
1706
|
+
print4.join("whereBounds", [",", line])
|
|
1707
|
+
);
|
|
1708
|
+
}
|
|
1709
|
+
function printDeclarationTypeBounds(print4, node, operator) {
|
|
1710
|
+
return utils.hasTypeBounds(node) ? adjustDeclarationClause(node, operator, getPrintedTypeBounds(print4, node)) : "";
|
|
1711
|
+
}
|
|
1712
|
+
function printImplTraitForType(print4, node) {
|
|
1713
|
+
return node.trait ? [print4("trait"), adjustDeclarationClause(node, "for", print4("typeTarget"))] : print4("typeTarget");
|
|
1714
|
+
}
|
|
1715
|
+
function adjustDeclarationClause(node, clause, content) {
|
|
1716
|
+
const isTypeBoundsClause = clause === ":" || clause === " =";
|
|
1717
|
+
return (clause === "where" || hasMultipleHeritage(node) ? indent : Identity)([
|
|
1718
|
+
clause === "->" ? hasMultipleHeritage(node) && node.whereBounds.length > 1 ? line : " " : clause === "where" ? line : isTypeBoundsClause ? hasMultipleHeritage(node) ? softline : "" : line,
|
|
1719
|
+
clause,
|
|
1720
|
+
content && group(
|
|
1721
|
+
clause === "where" ? indent([line, content]) : clause === "->" || isTypeBoundsClause ? [" ", content] : [line, content]
|
|
1722
|
+
)
|
|
1723
|
+
]);
|
|
1724
|
+
}
|
|
1725
|
+
function hasNonWhereHeritageClause(node) {
|
|
1726
|
+
switch (node.nodeType) {
|
|
1727
|
+
case jinxRust.NodeType.FunctionDeclaration:
|
|
1728
|
+
return !!node.returnType;
|
|
1729
|
+
case jinxRust.NodeType.StructDeclaration:
|
|
1730
|
+
case jinxRust.NodeType.TupleStructDeclaration:
|
|
1731
|
+
case jinxRust.NodeType.UnionDeclaration:
|
|
1732
|
+
case jinxRust.NodeType.EnumDeclaration:
|
|
1733
|
+
return false;
|
|
1734
|
+
case jinxRust.NodeType.TypeAliasDeclaration:
|
|
1735
|
+
case jinxRust.NodeType.TraitDeclaration:
|
|
1736
|
+
case jinxRust.NodeType.TraitAliasDeclaration:
|
|
1737
|
+
return utils.hasTypeBounds(node);
|
|
1738
|
+
case jinxRust.NodeType.ImplDeclaration:
|
|
1739
|
+
case jinxRust.NodeType.NegativeImplDeclaration:
|
|
1740
|
+
return !!node.trait;
|
|
1741
|
+
}
|
|
1742
|
+
}
|
|
1743
|
+
function hasMultipleHeritage(node) {
|
|
1744
|
+
return !!node.whereBounds && hasNonWhereHeritageClause(node);
|
|
1745
|
+
}
|
|
1746
|
+
var getMacroGroupId = createGroupIdMapper("MacroGroup");
|
|
1747
|
+
var getHeritageGroupId = createGroupIdMapper("heritageGroup");
|
|
1748
|
+
var getTypeParametersGroupId = createGroupIdMapper("typeParameters");
|
|
1749
|
+
function createGroupIdMapper(description) {
|
|
1750
|
+
const groupIds = /* @__PURE__ */ new WeakMap();
|
|
1751
|
+
return (node) => Map_get(groupIds, node, () => Symbol(description));
|
|
1752
|
+
}
|
|
1753
|
+
function printDanglingCommentsForInline(node, marker) {
|
|
1754
|
+
const hasOnlyBlockComments = !hasComment(node, 32 /* Line */ | 8 /* Dangling */, (comment) => !marker || comment.marker === marker) || utils.is_Program(node);
|
|
1755
|
+
const printed = printDanglingComments(node, hasOnlyBlockComments, marker);
|
|
1756
|
+
return printed && (hasOnlyBlockComments && !utils.is_Program(node) ? willBreak(printed) ? [indent([hardline, printed]), hardline] : [printed] : [printed, hardline]);
|
|
1757
|
+
}
|
|
1758
|
+
function isFormatLikeCall(node) {
|
|
1759
|
+
if (utils.is_Identifier(node.callee) && !node.typeArguments) {
|
|
1760
|
+
const [first, ...rest] = node.arguments;
|
|
1761
|
+
if (utils.is_Literal(first) && utils.is_LiteralStringLike(first) && first.value.includes("{}") && rest.every(utils.is_Identifier)) {
|
|
1762
|
+
return true;
|
|
1763
|
+
}
|
|
1764
|
+
}
|
|
1765
|
+
return false;
|
|
1766
|
+
}
|
|
1767
|
+
var ArgExpansionBailout = class extends Error {
|
|
1768
|
+
};
|
|
1769
|
+
function printCallArguments(print4, node) {
|
|
1770
|
+
const args = node.arguments;
|
|
1771
|
+
const { left: LEFT, right: RIGHT } = utils.getDelimChars(args);
|
|
1772
|
+
if (args.length === 0)
|
|
1773
|
+
return [LEFT, printDanglingCommentsForInline(node, "arguments" /* arguments */), RIGHT];
|
|
1774
|
+
if (args.length === 2 && isFormatLikeCall(node)) {
|
|
1775
|
+
return [LEFT, print4(["arguments", 0]), ", ", print4(["arguments", 1]), RIGHT];
|
|
1776
|
+
}
|
|
1777
|
+
let anyArgEmptyLine = false;
|
|
1778
|
+
let hasEmptyLineFollowingFirstArg = false;
|
|
1779
|
+
const lastArgIndex = args.length - 1;
|
|
1780
|
+
const trailingComma = "";
|
|
1781
|
+
const printedArguments = print4.map("arguments", (arg, index, arr) => {
|
|
1782
|
+
if (index === lastArgIndex) {
|
|
1783
|
+
return [print4()];
|
|
1784
|
+
} else if (isNextLineEmpty(arg)) {
|
|
1785
|
+
if (index === 0)
|
|
1786
|
+
hasEmptyLineFollowingFirstArg = true;
|
|
1787
|
+
anyArgEmptyLine = true;
|
|
1788
|
+
return [print4(), ",", hardline, hardline];
|
|
1789
|
+
} else {
|
|
1790
|
+
return [print4(), ",", line];
|
|
1791
|
+
}
|
|
1792
|
+
});
|
|
1793
|
+
if (anyArgEmptyLine || isFunctionCompositionArgs(args)) {
|
|
1794
|
+
return allArgsBrokenOut();
|
|
1795
|
+
}
|
|
1796
|
+
const shouldGroupFirst = shouldGroupFirstArg(args);
|
|
1797
|
+
const shouldGroupLast = shouldGroupLastArg(args);
|
|
1798
|
+
if (shouldGroupFirst || shouldGroupLast) {
|
|
1799
|
+
if (shouldGroupFirst ? printedArguments.slice(1).some(willBreak) : printedArguments.slice(0, -1).some(willBreak)) {
|
|
1800
|
+
return allArgsBrokenOut();
|
|
1801
|
+
}
|
|
1802
|
+
let printedExpanded = [];
|
|
1803
|
+
const { path } = getContext();
|
|
1804
|
+
const stackBackup = [...path.stack];
|
|
1805
|
+
try {
|
|
1806
|
+
path_try(() => {
|
|
1807
|
+
getContext().path.each((p, i) => {
|
|
1808
|
+
if (shouldGroupFirst && i === 0) {
|
|
1809
|
+
printedExpanded = [
|
|
1810
|
+
[
|
|
1811
|
+
print4([], { expandFirstArg: true }),
|
|
1812
|
+
printedArguments.length > 1 ? "," : "",
|
|
1813
|
+
hasEmptyLineFollowingFirstArg ? hardline : line,
|
|
1814
|
+
hasEmptyLineFollowingFirstArg ? hardline : ""
|
|
1815
|
+
],
|
|
1816
|
+
...printedArguments.slice(1)
|
|
1817
|
+
];
|
|
1818
|
+
}
|
|
1819
|
+
if (shouldGroupLast && i === lastArgIndex) {
|
|
1820
|
+
printedExpanded = [...printedArguments.slice(0, -1), print4([], { expandLastArg: true })];
|
|
1821
|
+
}
|
|
1822
|
+
}, "arguments");
|
|
1823
|
+
});
|
|
1824
|
+
} catch (caught) {
|
|
1825
|
+
path.stack.length = 0;
|
|
1826
|
+
path.stack.push(...stackBackup);
|
|
1827
|
+
if (caught instanceof ArgExpansionBailout)
|
|
1828
|
+
return allArgsBrokenOut();
|
|
1829
|
+
throw caught;
|
|
1830
|
+
}
|
|
1831
|
+
return [
|
|
1832
|
+
printedArguments.some(willBreak) ? breakParent : "",
|
|
1833
|
+
conditionalGroup([
|
|
1834
|
+
[LEFT, ...printedExpanded, RIGHT],
|
|
1835
|
+
shouldGroupFirst ? [LEFT, group(printedExpanded[0], { shouldBreak: true }), ...printedExpanded.slice(1), RIGHT] : [LEFT, ...printedArguments.slice(0, -1), group(printedExpanded[lastArgIndex], { shouldBreak: true }), RIGHT],
|
|
1836
|
+
allArgsBrokenOut()
|
|
1837
|
+
])
|
|
1838
|
+
];
|
|
1839
|
+
}
|
|
1840
|
+
const contents = [LEFT, indent([softline, ...printedArguments]), ifBreak(trailingComma), softline, RIGHT];
|
|
1841
|
+
return isLongCurriedCallExpression(node) ? contents : group(contents, { shouldBreak: anyArgEmptyLine || printedArguments.some(willBreak) });
|
|
1842
|
+
function allArgsBrokenOut() {
|
|
1843
|
+
return group([LEFT, indent([line, ...printedArguments]), trailingComma, line, RIGHT], { shouldBreak: true });
|
|
1844
|
+
}
|
|
1845
|
+
}
|
|
1846
|
+
function shouldHugFunctionParameters(node) {
|
|
1847
|
+
if (!node)
|
|
1848
|
+
return false;
|
|
1849
|
+
const parameters = utils.getParameters(node);
|
|
1850
|
+
if (parameters.length !== 1)
|
|
1851
|
+
return false;
|
|
1852
|
+
const param = parameters[0];
|
|
1853
|
+
if (hasComment(param))
|
|
1854
|
+
return false;
|
|
1855
|
+
switch (param.nodeType) {
|
|
1856
|
+
case jinxRust.NodeType.FunctionSelfParameterDeclaration:
|
|
1857
|
+
case jinxRust.NodeType.FunctionSpread:
|
|
1858
|
+
case jinxRust.NodeType.TypeFnPointerParameter:
|
|
1859
|
+
default:
|
|
1860
|
+
return false;
|
|
1861
|
+
case jinxRust.NodeType.FunctionParameterDeclaration:
|
|
1862
|
+
case jinxRust.NodeType.ClosureFunctionParameterDeclaration:
|
|
1863
|
+
return "items" in param.pattern || "properties" in param.pattern;
|
|
1864
|
+
}
|
|
1865
|
+
}
|
|
1866
|
+
function shouldGroupFunctionParameters(functionNode, returnTypeDoc) {
|
|
1867
|
+
const returnType = functionNode.returnType;
|
|
1868
|
+
const generics = functionNode.generics;
|
|
1869
|
+
const whereBounds = functionNode.whereBounds;
|
|
1870
|
+
if (!returnType)
|
|
1871
|
+
return false;
|
|
1872
|
+
if (generics) {
|
|
1873
|
+
if (generics.length > 1)
|
|
1874
|
+
return false;
|
|
1875
|
+
if (generics.length === 1 && !isShortGenericParameterDeclaration(generics[0]))
|
|
1876
|
+
return false;
|
|
1877
|
+
}
|
|
1878
|
+
if (whereBounds) {
|
|
1879
|
+
if (whereBounds.length > 1)
|
|
1880
|
+
return false;
|
|
1881
|
+
}
|
|
1882
|
+
return utils.getParameters(functionNode).length === 1 && (willBreak(returnTypeDoc) || willBreak(printWhereBounds(getPrintFn(), functionNode)));
|
|
1883
|
+
}
|
|
1884
|
+
function printBlockBody(print4, node) {
|
|
1885
|
+
const body = printBodyOrCases(print4, node);
|
|
1886
|
+
return [
|
|
1887
|
+
"{",
|
|
1888
|
+
body.length > 0 ? utils.getBodyOrCases(node)?.length ? canInlineBlockBody(node) ? [indent([line, body]), line] : group([indent([line, body]), line], { shouldBreak: true }) : body : emptyContent(node),
|
|
1889
|
+
"}"
|
|
1890
|
+
];
|
|
1891
|
+
}
|
|
1892
|
+
function printMaybeBlockBody(print4, node) {
|
|
1893
|
+
return utils.hasSemiNoBody(node) ? ";" : adjustClause(node, printBlockBody(print4, node));
|
|
1894
|
+
}
|
|
1895
|
+
function printArrowFunction(print4, node) {
|
|
1896
|
+
const signatures = [];
|
|
1897
|
+
const body = [];
|
|
1898
|
+
const { args, path } = getContext();
|
|
1899
|
+
let chainShouldBreak = false;
|
|
1900
|
+
let tailNode = node;
|
|
1901
|
+
(function rec(node2) {
|
|
1902
|
+
tailNode = node2;
|
|
1903
|
+
const doc = printArrowFunctionSignature(print4, node2);
|
|
1904
|
+
if (signatures.length === 0) {
|
|
1905
|
+
signatures.push(doc);
|
|
1906
|
+
} else {
|
|
1907
|
+
const { leading, trailing } = printCommentsSeparately();
|
|
1908
|
+
signatures.push([leading, doc]);
|
|
1909
|
+
body.unshift(trailing);
|
|
1910
|
+
}
|
|
1911
|
+
chainShouldBreak || (chainShouldBreak = !!node2.returnType || !node2.parameters.every((param) => isSimplePattern(param.pattern)));
|
|
1912
|
+
if (!utils.is_ClosureFunctionExpression(node2.expression) || args && args.expandLastArg) {
|
|
1913
|
+
body.unshift(print4("expression", args));
|
|
1914
|
+
} else {
|
|
1915
|
+
pathCall(node2, "expression", rec);
|
|
1916
|
+
}
|
|
1917
|
+
})(node);
|
|
1918
|
+
if (signatures.length > 1) {
|
|
1919
|
+
return printArrowChain(signatures, chainShouldBreak, body, tailNode);
|
|
1920
|
+
} else {
|
|
1921
|
+
const printed = signatures[0];
|
|
1922
|
+
if (!hasLeadingOwnLineComment(node.expression) && (utils.is_ArrayOrTupleLiteral(node.expression) || utils.is_StructLiteral(node.expression) || utils.is_ExpressionWithBodyOrCases(node.expression) || utils.is_ClosureFunctionExpression(node.expression))) {
|
|
1923
|
+
return group([printed, " ", body]);
|
|
1924
|
+
}
|
|
1925
|
+
const shouldAddSoftLine = args && args.expandLastArg && !hasComment(node);
|
|
1926
|
+
const printTrailingComma = args && args.expandLastArg && false;
|
|
1927
|
+
const shouldAddParens = utils.is_OrExpression(node.expression);
|
|
1928
|
+
return group([
|
|
1929
|
+
printed,
|
|
1930
|
+
group([
|
|
1931
|
+
indent(shouldAddParens ? [line, ifBreak("", "("), body, ifBreak("", ")")] : [line, body]),
|
|
1932
|
+
shouldAddSoftLine ? [ifBreak(printTrailingComma ? "," : ""), softline] : ""
|
|
1933
|
+
])
|
|
1934
|
+
]);
|
|
1935
|
+
}
|
|
1936
|
+
}
|
|
1937
|
+
function printArrowChain(signatures, shouldBreak, bodyDoc, tailNode) {
|
|
1938
|
+
const { args } = getContext();
|
|
1939
|
+
const parent = getParentNode();
|
|
1940
|
+
const isCallee = is_CallExpression_or_CallLikeMacroInvocation(parent) && parent.callee === getNode();
|
|
1941
|
+
const isAssignmentRhs = !!(args && args.assignmentLayout);
|
|
1942
|
+
const shouldPutBodyOnSeparateLine = !utils.is_ExpressionWithBodyOrCases(tailNode.expression) && !utils.is_StructLiteral(tailNode.expression);
|
|
1943
|
+
const shouldBreakBeforeChain = isCallee && shouldPutBodyOnSeparateLine || args && args.assignmentLayout === 6 /* chain-tail-arrow-chain */;
|
|
1944
|
+
const groupId = Symbol("arrow-chain");
|
|
1945
|
+
return group([
|
|
1946
|
+
group(
|
|
1947
|
+
indent([isCallee || isAssignmentRhs ? softline : "", group(join(line, signatures), { shouldBreak })]),
|
|
1948
|
+
//
|
|
1949
|
+
{ id: groupId, shouldBreak: shouldBreakBeforeChain }
|
|
1950
|
+
),
|
|
1951
|
+
indentIfBreak(shouldPutBodyOnSeparateLine ? indent([line, bodyDoc]) : [" ", bodyDoc], { groupId }),
|
|
1952
|
+
isCallee ? ifBreak(softline, "", { groupId }) : ""
|
|
1953
|
+
]);
|
|
1954
|
+
}
|
|
1955
|
+
function printArrowFunctionSignature(print4, node) {
|
|
1956
|
+
const { args } = getContext();
|
|
1957
|
+
const expandArg = args && (args.expandLastArg || args.expandFirstArg);
|
|
1958
|
+
let returnTypeDoc = printReturnType(print4, node);
|
|
1959
|
+
if (expandArg) {
|
|
1960
|
+
if (willBreak(returnTypeDoc))
|
|
1961
|
+
throw new ArgExpansionBailout();
|
|
1962
|
+
else
|
|
1963
|
+
returnTypeDoc = group(removeLines(returnTypeDoc));
|
|
1964
|
+
}
|
|
1965
|
+
return [
|
|
1966
|
+
print4.b("static"),
|
|
1967
|
+
print4.b("async"),
|
|
1968
|
+
print4.b("move"),
|
|
1969
|
+
//
|
|
1970
|
+
group([printFunctionParameters(print4, node, expandArg), returnTypeDoc])
|
|
1971
|
+
];
|
|
1972
|
+
}
|
|
1973
|
+
function printGenerics_x_whereBounds(print4, node, xDoc) {
|
|
1974
|
+
const generics = utils.is_ImplDeclarationNode(node) ? [printGenerics(print4, node), " "] : [" ", print4("id"), printGenerics(print4, node)];
|
|
1975
|
+
const whereBoundsDoc = printWhereBounds(print4, node);
|
|
1976
|
+
return utils.is_TupleStructDeclaration(node) ? [...generics, xDoc, group(whereBoundsDoc, { id: getHeritageGroupId(node) })] : [...generics, group([xDoc, whereBoundsDoc], { id: getHeritageGroupId(node) })];
|
|
1977
|
+
}
|
|
1978
|
+
function adjustClause(node, doc) {
|
|
1979
|
+
return [
|
|
1980
|
+
"whereBounds" in node && (!!node.whereBounds || utils.hasTypeBounds(node) && node.typeBounds.length > 1) && willBreak(doc) ? ifBreak(hardline, " ", { groupId: getHeritageGroupId(node) }) : " ",
|
|
1981
|
+
doc
|
|
1982
|
+
];
|
|
1983
|
+
}
|
|
1984
|
+
function printParametersAndReturnType(node) {
|
|
1985
|
+
const parametersDoc = printFunctionParameters(getPrintFn(), node);
|
|
1986
|
+
const returnTypeDoc = printReturnType(getPrintFn(), node);
|
|
1987
|
+
return utils.is_FunctionDeclaration(node) && shouldGroupFunctionParameters(node, returnTypeDoc) ? group([group(parametersDoc), returnTypeDoc]) : group([parametersDoc, returnTypeDoc]);
|
|
1988
|
+
}
|
|
1989
|
+
function printFlowControlExpression(print4, node) {
|
|
1990
|
+
return !node.expression ? "" : (
|
|
1991
|
+
// : hasLeadingComment(node.expression)
|
|
1992
|
+
// ? [" (", indent([hardline, print("expression")]), hardline, ")"]
|
|
1993
|
+
is_BinaryishExpression(node.expression) && !flowControlExpressionNeedsOuterParens(node) ? group([" ", ifBreak("("), indent([softline, print4("expression")]), softline, ifBreak(")")]) : [" ", print4("expression")]
|
|
1994
|
+
);
|
|
1995
|
+
}
|
|
1996
|
+
function flowControlExpressionNeedsOuterParens(flow) {
|
|
1997
|
+
return flow.expression && function hasLeadingComment(node) {
|
|
1998
|
+
if (hasLeadingOwnLineComment(node))
|
|
1999
|
+
return true;
|
|
2000
|
+
if (hasNakedLeftSide(node)) {
|
|
2001
|
+
let leftMost = node;
|
|
2002
|
+
while (leftMost = getLeftSide(leftMost)) {
|
|
2003
|
+
if (hasLeadingOwnLineComment(leftMost))
|
|
2004
|
+
return true;
|
|
2005
|
+
}
|
|
2006
|
+
}
|
|
2007
|
+
return false;
|
|
2008
|
+
}(flow.expression);
|
|
2009
|
+
}
|
|
2010
|
+
function getLeftSide(node, includeAttributes = false) {
|
|
2011
|
+
let target = node.left ?? node.callee ?? node.namespace ?? node.label ?? node.lower ?? node.struct ?? node.condition ?? node.expression;
|
|
2012
|
+
if (target && includeAttributes && utils.hasAttributes(node)) {
|
|
2013
|
+
node.attributes.forEach((attr) => {
|
|
2014
|
+
if (utils.start(attr) < utils.start(target))
|
|
2015
|
+
target = attr;
|
|
2016
|
+
});
|
|
2017
|
+
}
|
|
2018
|
+
return target;
|
|
2019
|
+
}
|
|
2020
|
+
function hasNakedLeftSide(node) {
|
|
2021
|
+
return is_BinaryishExpression(node) || utils.is_ReassignmentNode(node) || is_CallExpression_or_CallLikeMacroInvocation(node) || is_MemberAccessLike(node) || utils.is_PostfixExpression(node) || utils.is_ExpressionAsTypeCast(node);
|
|
2022
|
+
}
|
|
2023
|
+
function printReturnType(print4, node) {
|
|
2024
|
+
return node.returnType ? utils.is_FunctionDeclaration(node) ? adjustDeclarationClause(node, "->", print4("returnType")) : [" -> ", print4("returnType")] : "";
|
|
2025
|
+
}
|
|
2026
|
+
function printFunctionParameters(print4, node, expandArg = false, printTypeParams = false) {
|
|
2027
|
+
const { left: leftDelim, right: rightDelim } = utils.getDelimChars(node.parameters);
|
|
2028
|
+
const generics = printTypeParams && utils.is_FunctionDeclaration(node) ? printGenerics(print4, node) : "";
|
|
2029
|
+
if (!utils.hasParameters(node)) {
|
|
2030
|
+
return [
|
|
2031
|
+
generics,
|
|
2032
|
+
//
|
|
2033
|
+
leftDelim,
|
|
2034
|
+
printDanglingCommentsForInline(node, "parameters" /* parameters */),
|
|
2035
|
+
rightDelim
|
|
2036
|
+
];
|
|
2037
|
+
}
|
|
2038
|
+
const isParametersInTestCall = false;
|
|
2039
|
+
const shouldHugParameters = shouldHugFunctionParameters(node);
|
|
2040
|
+
const printed = print4.join("parameters", sepFn);
|
|
2041
|
+
if (utils.hasSelfParameter(node)) {
|
|
2042
|
+
printed.unshift(getContext().path.call(() => [print4(), printed.length ? sepFn(node.parameters.self) : ""], "parameters", "self"));
|
|
2043
|
+
}
|
|
2044
|
+
if (expandArg) {
|
|
2045
|
+
if (willBreak(generics) || willBreak(printed))
|
|
2046
|
+
throw new ArgExpansionBailout();
|
|
2047
|
+
return group([removeLines(generics), leftDelim, removeLines(printed), rightDelim]);
|
|
2048
|
+
} else if (shouldHugParameters || isParametersInTestCall) {
|
|
2049
|
+
return [generics, leftDelim, ...printed, rightDelim];
|
|
2050
|
+
} else {
|
|
2051
|
+
return [generics, leftDelim, indent([softline, ...printed]), softline, rightDelim];
|
|
2052
|
+
}
|
|
2053
|
+
function sepFn(param) {
|
|
2054
|
+
return shouldHugParameters || isParametersInTestCall ? ", " : isNextLineEmpty(param) ? [",", hardline, hardline] : [",", line];
|
|
2055
|
+
}
|
|
2056
|
+
}
|
|
2057
|
+
function path_try(callback) {
|
|
2058
|
+
const { stack } = getContext().path;
|
|
2059
|
+
const stackBackup = [...stack];
|
|
2060
|
+
try {
|
|
2061
|
+
return callback();
|
|
2062
|
+
} finally {
|
|
2063
|
+
stack.length = 0;
|
|
2064
|
+
stack.push(...stackBackup);
|
|
2065
|
+
}
|
|
2066
|
+
}
|
|
2067
|
+
function shouldGroupFirstArg(args) {
|
|
2068
|
+
if (args.length !== 2)
|
|
2069
|
+
return false;
|
|
2070
|
+
const [firstArg, secondArg] = args;
|
|
2071
|
+
return !hasComment(firstArg) && utils.is_ClosureFunctionExpression(firstArg) && utils.is_ExpressionWithBodyOrCases(firstArg.expression) && !utils.is_ClosureFunctionExpression(secondArg) && !couldGroupArg(secondArg);
|
|
2072
|
+
}
|
|
2073
|
+
function shouldGroupLastArg(args) {
|
|
2074
|
+
const lastArg = last_of(args);
|
|
2075
|
+
const preLastArg = args[args.length - 2];
|
|
2076
|
+
return !hasComment(lastArg, 2 /* Leading */) && !hasComment(lastArg, 4 /* Trailing */) && couldGroupArg(lastArg) && (!preLastArg || preLastArg.nodeType !== lastArg.nodeType) && (args.length !== 2 || !utils.is_ClosureFunctionExpression(preLastArg) || !utils.is_ArrayOrTupleLiteral(lastArg)) && !(args.length > 1 && utils.is_ArrayOrTupleLiteral(lastArg) && isConciselyPrintedArray(lastArg)) && (args.length !== 1 || !utils.is_IfBlockExpression(lastArg));
|
|
2077
|
+
}
|
|
2078
|
+
function couldGroupArg(arg, arrowChainRecursion = false) {
|
|
2079
|
+
return utils.is_StructLiteral(arg) && (arg.properties.length > 0 || hasComment(arg)) || utils.is_ArrayOrTupleLiteral(arg) && (arg.items.length > 0 || hasComment(arg)) || utils.is_ExpressionAsTypeCast(arg) && couldGroupArg(arg.expression) || utils.is_ClosureFunctionExpression(arg) && (!arg.returnType || utils.is_Identifier(arg.returnType) || !isNonEmptyBlockStatement(arg.expression)) && (isNonEmptyBlockStatement(arg.expression) || utils.is_ClosureFunctionExpression(arg.expression) && couldGroupArg(arg.expression, true) || utils.is_StructLiteral(arg.expression) || utils.is_ArrayOrTupleLiteral(arg.expression) || !arrowChainRecursion && is_CallExpression_or_CallLikeMacroInvocation(arg.expression)) || utils.is_ExpressionWithBodyOrCases(arg);
|
|
2080
|
+
}
|
|
2081
|
+
function isNonEmptyBlockStatement(node) {
|
|
2082
|
+
if (utils.is_MatchExpression(node))
|
|
2083
|
+
return node.cases.length > 0;
|
|
2084
|
+
return utils.is_ExpressionWithBodyOrCases(node) && node.body.length > 0;
|
|
2085
|
+
}
|
|
2086
|
+
function isFunctionCompositionArgs(args) {
|
|
2087
|
+
if (args.length <= 1) {
|
|
2088
|
+
return false;
|
|
2089
|
+
}
|
|
2090
|
+
let count = 0;
|
|
2091
|
+
for (const arg of args) {
|
|
2092
|
+
if (utils.is_ClosureFunctionExpression(arg)) {
|
|
2093
|
+
if (++count > 1)
|
|
2094
|
+
return true;
|
|
2095
|
+
} else if (is_CallExpression_or_CallLikeMacroInvocation(arg)) {
|
|
2096
|
+
for (const childArg of arg.arguments) {
|
|
2097
|
+
if (utils.is_ClosureFunctionExpression(childArg)) {
|
|
2098
|
+
return true;
|
|
2099
|
+
}
|
|
2100
|
+
}
|
|
2101
|
+
}
|
|
2102
|
+
}
|
|
2103
|
+
return false;
|
|
2104
|
+
}
|
|
2105
|
+
function printBinaryishExpression(print4, node) {
|
|
2106
|
+
const parent = getParentNode();
|
|
2107
|
+
const grandParent = getGrandParentNode();
|
|
2108
|
+
const isInsideParenthesis = "condition" in parent && parent.condition === node || utils.is_MatchExpression(parent);
|
|
2109
|
+
const parts = printBinaryishExpressions(false, isInsideParenthesis);
|
|
2110
|
+
if (isInsideParenthesis)
|
|
2111
|
+
return parts;
|
|
2112
|
+
if (is_CallExpression_or_CallLikeMacroInvocation(parent) && parent.callee === node || //
|
|
2113
|
+
utils.is_UnaryExpression(parent) || utils.is_MemberExpression(parent)) {
|
|
2114
|
+
return group([indent([softline, ...parts]), softline]);
|
|
2115
|
+
}
|
|
2116
|
+
const shouldNotIndent = utils.is_FlowControlExpression(parent) || utils.is_ClosureFunctionExpression(parent) && parent.expression === node || utils.is_ExpressionWithBodyOrCases(parent);
|
|
2117
|
+
const shouldIndentIfInlining = utils.is_ReassignmentNode(parent) || utils.is_VariableDeclarationNode(parent) || utils.is_StructLiteral(parent) || utils.is_StructLiteral(grandParent);
|
|
2118
|
+
const samePrecedenceSubExpression = is_BinaryishExpression(node.left) && shouldFlatten(node, node.left);
|
|
2119
|
+
if (shouldNotIndent || shouldInlineLogicalExpression(node) && !samePrecedenceSubExpression || !shouldInlineLogicalExpression(node) && shouldIndentIfInlining) {
|
|
2120
|
+
return group(parts);
|
|
2121
|
+
}
|
|
2122
|
+
if (parts.length === 0)
|
|
2123
|
+
return "";
|
|
2124
|
+
const firstGroupIndex = parts.findIndex((part) => typeof part !== "string" && !Array.isArray(part) && part.type === "group");
|
|
2125
|
+
const leading = parts.slice(0, firstGroupIndex === -1 ? 1 : firstGroupIndex + 1);
|
|
2126
|
+
return group([...leading, indent(parts.slice(leading.length))], { id: Symbol("logicalChain") });
|
|
2127
|
+
function printBinaryishExpressions(isNested, isInsideParenthesis2) {
|
|
2128
|
+
const { path, print: print5, options: options2 } = getContext();
|
|
2129
|
+
const node2 = path.getValue();
|
|
2130
|
+
if (!is_BinaryishExpression(node2)) {
|
|
2131
|
+
return [group(print5())];
|
|
2132
|
+
}
|
|
2133
|
+
const parts2 = [];
|
|
2134
|
+
if (shouldFlatten(node2, node2.left)) {
|
|
2135
|
+
parts2.push(...pathCall(node2, "left", () => printBinaryishExpressions(true, isInsideParenthesis2)));
|
|
2136
|
+
} else {
|
|
2137
|
+
parts2.push(group(print5("left")));
|
|
2138
|
+
}
|
|
2139
|
+
const shouldInline = shouldInlineLogicalExpression(node2);
|
|
2140
|
+
const operator = node2.kind;
|
|
2141
|
+
const right = [
|
|
2142
|
+
operator,
|
|
2143
|
+
shouldInline ? " " : line,
|
|
2144
|
+
// this is a hack (should always be 'print("right")')
|
|
2145
|
+
!shouldInline && utils.is_LogicalExpression(node2.right) && shouldFlatten(node2.right, node2) ? pathCall(node2, "right", () => printBinaryishExpressions(true, isInsideParenthesis2)) : print5("right")
|
|
2146
|
+
];
|
|
2147
|
+
const shouldBreak = hasComment(node2.left, 4 /* Trailing */ | 32 /* Line */);
|
|
2148
|
+
const shouldGroup = shouldBreak || !(isInsideParenthesis2 && utils.is_LogicalExpression(node2)) && path.getParentNode().nodeType !== node2.nodeType && node2.left.nodeType !== node2.nodeType && node2.right.nodeType !== node2.nodeType;
|
|
2149
|
+
parts2.push(" ", shouldGroup ? group(right, { shouldBreak }) : right);
|
|
2150
|
+
if (isNested && hasComment(node2)) {
|
|
2151
|
+
const printed = cleanDoc(withComments(node2, parts2));
|
|
2152
|
+
if (Array.isArray(printed))
|
|
2153
|
+
return printed;
|
|
2154
|
+
if (printed.type === "fill")
|
|
2155
|
+
return printed.parts;
|
|
2156
|
+
return [printed];
|
|
2157
|
+
}
|
|
2158
|
+
return parts2;
|
|
2159
|
+
}
|
|
2160
|
+
}
|
|
2161
|
+
function shouldInlineLogicalExpression(node) {
|
|
2162
|
+
if (utils.is_LogicalExpression(node)) {
|
|
2163
|
+
if (utils.is_StructLiteral(node.right))
|
|
2164
|
+
return node.right.properties.length > 0;
|
|
2165
|
+
if (utils.is_ArrayOrTupleLiteral(node.right))
|
|
2166
|
+
return node.right.items.length > 0;
|
|
2167
|
+
}
|
|
2168
|
+
return false;
|
|
2169
|
+
}
|
|
2170
|
+
function printUnaryExpression(leftDoc, node) {
|
|
2171
|
+
const printed = getPrintFn()("expression");
|
|
2172
|
+
return group([leftDoc, printed]);
|
|
2173
|
+
}
|
|
2174
|
+
function printIfBlock(print4, node) {
|
|
2175
|
+
let printed = [
|
|
2176
|
+
printIfBlockCondition(print4, node),
|
|
2177
|
+
//
|
|
2178
|
+
printBlockBody(print4, node),
|
|
2179
|
+
f` else ${print4("else")}`
|
|
2180
|
+
];
|
|
2181
|
+
const parent = getParentNode();
|
|
2182
|
+
if (utils.is_ClosureBlock(node, parent)) {
|
|
2183
|
+
printed = parenthesize_if_break([indent([softline, printed]), softline]);
|
|
2184
|
+
} else if (!utils.is_ElseBlock(node, parent)) ;
|
|
2185
|
+
return printed;
|
|
2186
|
+
}
|
|
2187
|
+
function printIfBlockCondition(print4, node) {
|
|
2188
|
+
if (!utils.hasCondition(node))
|
|
2189
|
+
return "";
|
|
2190
|
+
return f`if ${printCondition(print4, node)}`;
|
|
2191
|
+
}
|
|
2192
|
+
function printCondition(print4, node) {
|
|
2193
|
+
return pathCall(node, "condition", (condition) => {
|
|
2194
|
+
if (!condition)
|
|
2195
|
+
return "";
|
|
2196
|
+
if (needsParens(condition))
|
|
2197
|
+
return [print4(), " "];
|
|
2198
|
+
const id = Symbol("condition");
|
|
2199
|
+
const printed = [indent([softline , print4()]), softline];
|
|
2200
|
+
return [group(printed, { id }), ifBreak("", " ", { groupId: id })];
|
|
2201
|
+
});
|
|
2202
|
+
}
|
|
2203
|
+
function parenthesize_if_break(doc) {
|
|
2204
|
+
return conditionalGroup([doc, ["(", doc, ")"]], { shouldBreak: willBreak(doc) });
|
|
2205
|
+
}
|
|
2206
|
+
function isSimplePattern(node) {
|
|
2207
|
+
if (!node)
|
|
2208
|
+
return false;
|
|
2209
|
+
switch (node.nodeType) {
|
|
2210
|
+
case jinxRust.NodeType.MacroInvocation:
|
|
2211
|
+
return false;
|
|
2212
|
+
case jinxRust.NodeType.ExpressionTypeCast:
|
|
2213
|
+
return isSimplePattern(node.typeCallee) && !hasComplexTypeArguments(node);
|
|
2214
|
+
case jinxRust.NodeType.ExpressionTypeSelector:
|
|
2215
|
+
return utils.is_Identifier(node.typeTarget) && (!node.typeExpression || utils.is_Identifier(node.typeExpression));
|
|
2216
|
+
case jinxRust.NodeType.ExpressionPath:
|
|
2217
|
+
return !node.namespace || isSimplePattern(node.namespace);
|
|
2218
|
+
case jinxRust.NodeType.RangePattern:
|
|
2219
|
+
return (!node.lower || isSimplePattern(node.lower)) && (!node.upper || isSimplePattern(node.upper));
|
|
2220
|
+
case jinxRust.NodeType.PatternVariableDeclaration:
|
|
2221
|
+
case jinxRust.NodeType.ReferencePattern:
|
|
2222
|
+
case jinxRust.NodeType.BoxPattern:
|
|
2223
|
+
case jinxRust.NodeType.MinusPattern:
|
|
2224
|
+
return isSimplePattern(node.pattern);
|
|
2225
|
+
case jinxRust.NodeType.Identifier:
|
|
2226
|
+
case jinxRust.NodeType.Literal:
|
|
2227
|
+
case jinxRust.NodeType.RestPattern:
|
|
2228
|
+
case jinxRust.NodeType.WildcardPattern:
|
|
2229
|
+
return true;
|
|
2230
|
+
default:
|
|
2231
|
+
return false;
|
|
2232
|
+
}
|
|
2233
|
+
}
|
|
2234
|
+
function printUnionPattern(print4, node) {
|
|
2235
|
+
if (node.patterns.length === 1)
|
|
2236
|
+
return print4.map("patterns");
|
|
2237
|
+
const parent = getParentNode();
|
|
2238
|
+
const prebreak = parent && (utils.is_VariableDeclarationNode(parent) || utils.is_LetScrutinee(parent)) && !needsParens(node);
|
|
2239
|
+
return group([
|
|
2240
|
+
prebreak ? softline : "",
|
|
2241
|
+
print4.map("patterns", (node2, i, arr) => [
|
|
2242
|
+
withComments(node2, [
|
|
2243
|
+
i === 0 ? ifBreak("| ") : "| ",
|
|
2244
|
+
align(2, print4())
|
|
2245
|
+
]),
|
|
2246
|
+
i === arr.length - 1 ? "" : line
|
|
2247
|
+
])
|
|
2248
|
+
]);
|
|
2249
|
+
}
|
|
2250
|
+
function printArrayLike(print4, node) {
|
|
2251
|
+
const delims = utils.getDelimChars(node.items);
|
|
2252
|
+
if (node.items.length === 0) {
|
|
2253
|
+
const comments = printDanglingCommentsForInline(node, "items" /* items */);
|
|
2254
|
+
return comments ? group([delims.left, comments, delims.right]) : delims.left + delims.right;
|
|
2255
|
+
}
|
|
2256
|
+
const groupId = Symbol("array");
|
|
2257
|
+
const shouldBreak = (
|
|
2258
|
+
// is_TupleStructDeclaration(node) ||
|
|
2259
|
+
!utils.is_TupleNode(node) && node.items.length > 1 && node.items.every((item, i) => {
|
|
2260
|
+
const next = node.items[i + 1];
|
|
2261
|
+
return (utils.hasProperties(item) && item.properties.length > 1 || utils.hasItems(item) && item.items.length > 1) && (!next || item.nodeType === next.nodeType);
|
|
2262
|
+
})
|
|
2263
|
+
);
|
|
2264
|
+
const shouldUseConciseFormatting = isConciselyPrintedArray(node);
|
|
2265
|
+
const parent = getParentNode();
|
|
2266
|
+
const needsForcedTrailingComma = node.items.length === 1 ? utils.is_TupleLiteral(node) ? utils.is_RangeLiteral(node.items[0]) ? !(utils.is_ReassignmentExpression(parent) && parent.left === node) : true : utils.is_TuplePattern(node) ? !node.struct && !utils.is_RangePattern(node.items[0]) && !utils.is_RestPattern(node.items[0]) : utils.is_TypeTuple(node) ? true : false : false;
|
|
2267
|
+
const trailingComma = needsForcedTrailingComma ? "," : shouldUseConciseFormatting ? ifBreak(",", "", { groupId }) : ifBreak(",");
|
|
2268
|
+
const printed = shouldUseConciseFormatting ? fill(
|
|
2269
|
+
print4.join(
|
|
2270
|
+
"items",
|
|
2271
|
+
(item, next) => isNextLineEmpty(item) ? [",", hardline, hardline] : hasComment(next, 2 /* Leading */, (comment) => utils.is_LineCommentNode(comment) || comment.placement === "ownLine") ? [",", hardline] : [",", line],
|
|
2272
|
+
trailingComma
|
|
2273
|
+
)
|
|
2274
|
+
) : print4.map_join(
|
|
2275
|
+
"items",
|
|
2276
|
+
() => group(print4()),
|
|
2277
|
+
(item) => isNextLineEmpty(item) ? [",", line, softline] : [",", line],
|
|
2278
|
+
trailingComma
|
|
2279
|
+
);
|
|
2280
|
+
return group([delims.left, indent([softline, printed]), printDanglingComments(node, true, "items" /* items */), softline, delims.right], {
|
|
2281
|
+
shouldBreak,
|
|
2282
|
+
id: groupId
|
|
2283
|
+
});
|
|
2284
|
+
}
|
|
2285
|
+
function printObject(print4, node) {
|
|
2286
|
+
if (utils.hasSemiNoProperties(node)) {
|
|
2287
|
+
return ";";
|
|
2288
|
+
}
|
|
2289
|
+
if (!utils.hasProperties(node)) {
|
|
2290
|
+
return [" {", printDanglingCommentsForInline(node, "properties" /* properties */) || emptyContent(node), "}"];
|
|
2291
|
+
}
|
|
2292
|
+
const firstProperty = node.properties[0];
|
|
2293
|
+
const parent = getParentNode();
|
|
2294
|
+
const shouldBreak = utils.is_StructPattern(node) ? false : utils.is_UnionDeclaration(node) || utils.is_StructDeclaration(node) || utils.is_EnumMemberStructDeclaration(node) || hasNewlineInRange(utils.start(node), utils.start(firstProperty));
|
|
2295
|
+
const content = [
|
|
2296
|
+
" {",
|
|
2297
|
+
indent([
|
|
2298
|
+
line,
|
|
2299
|
+
...print4.join(
|
|
2300
|
+
"properties",
|
|
2301
|
+
//
|
|
2302
|
+
(node2) => isNextLineEmpty(node2) ? [",", hardline, hardline] : [",", line],
|
|
2303
|
+
(node2) => is_StructSpread(node2) ? "" : ifBreak(",")
|
|
2304
|
+
)
|
|
2305
|
+
]),
|
|
2306
|
+
line,
|
|
2307
|
+
"}"
|
|
2308
|
+
];
|
|
2309
|
+
const grandparent = getGrandParentNode();
|
|
2310
|
+
if (grandparent && (utils.is_FunctionDeclaration(grandparent) || utils.is_ClosureFunctionExpression(grandparent)) && utils.getParameters(grandparent)[0] === parent) {
|
|
2311
|
+
return content;
|
|
2312
|
+
}
|
|
2313
|
+
if (utils.is_StructLiteral(node) && utils.is_ReassignmentNode(parent) && parent.left === node || utils.is_StructPattern(node) && (utils.is_VariableDeclarationNode(parent) || utils.is_MatchExpressionCase(parent) || utils.is_FunctionParameterDeclaration(parent)) && parent.pattern === node) {
|
|
2314
|
+
return content;
|
|
2315
|
+
}
|
|
2316
|
+
return group(content, { shouldBreak });
|
|
2317
|
+
}
|
|
2318
|
+
function printEnumBody(print4, node) {
|
|
2319
|
+
const printed = print4.join("members", (member) => [",", maybeEmptyLine(member)], ",");
|
|
2320
|
+
return [
|
|
2321
|
+
" {",
|
|
2322
|
+
printed.length === 0 ? printDanglingCommentsForInline(node, "members" /* members */) || emptyContent(node) : [indent([hardline, ...printed]), hardline],
|
|
2323
|
+
"}"
|
|
2324
|
+
];
|
|
2325
|
+
}
|
|
2326
|
+
|
|
2327
|
+
// src/format/comments.ts
|
|
2328
|
+
function addCommentHelper(node, comment, leading = false, trailing = false) {
|
|
2329
|
+
(node.comments ?? (node.comments = [])).push(comment);
|
|
2330
|
+
comment.leading = leading, comment.trailing = trailing, comment.printed = false;
|
|
2331
|
+
}
|
|
2332
|
+
function addLeadingComment(node, comment) {
|
|
2333
|
+
addCommentHelper(node, comment, true);
|
|
2334
|
+
}
|
|
2335
|
+
function addDanglingComment(node, comment, marker) {
|
|
2336
|
+
addCommentHelper(node, comment);
|
|
2337
|
+
comment.marker = marker;
|
|
2338
|
+
}
|
|
2339
|
+
function addTrailingComment(node, comment) {
|
|
2340
|
+
addCommentHelper(node, comment, false, true);
|
|
2341
|
+
}
|
|
2342
|
+
function setPrettierIgnoreTarget(node, comment) {
|
|
2343
|
+
comment.unignore = true;
|
|
2344
|
+
node.prettierIgnore = true;
|
|
2345
|
+
}
|
|
2346
|
+
function hasComments(node) {
|
|
2347
|
+
return "comments" in node && node.comments.length > 0;
|
|
2348
|
+
}
|
|
2349
|
+
function printDanglingComments(enclosingNode, sameIndent, marker) {
|
|
2350
|
+
if (hasComments(enclosingNode)) {
|
|
2351
|
+
const printed = [];
|
|
2352
|
+
pathCallEach(enclosingNode, "comments", (comment) => {
|
|
2353
|
+
if (isDangling(comment) && (!marker || comment.marker === marker)) {
|
|
2354
|
+
printed.push(printComment(comment));
|
|
2355
|
+
}
|
|
2356
|
+
});
|
|
2357
|
+
if (printed.length > 0) {
|
|
2358
|
+
return sameIndent ? join(hardline, printed) : indent([hardline, join(hardline, printed)]);
|
|
2359
|
+
}
|
|
2360
|
+
}
|
|
2361
|
+
return "";
|
|
2362
|
+
}
|
|
2363
|
+
function setDidPrintComment(comment) {
|
|
2364
|
+
comment.printed = true;
|
|
2365
|
+
}
|
|
2366
|
+
function printComment(comment) {
|
|
2367
|
+
setDidPrintComment(comment);
|
|
2368
|
+
return getContext().options.printer.printComment(getContext().path, getOptions());
|
|
2369
|
+
}
|
|
2370
|
+
function isPreviousLineEmpty(node) {
|
|
2371
|
+
let index = utils.start(node) - 1;
|
|
2372
|
+
index = skipSpaces(index, true);
|
|
2373
|
+
index = skipNewline(index, true);
|
|
2374
|
+
index = skipSpaces(index, true);
|
|
2375
|
+
return index !== skipNewline(index, true);
|
|
2376
|
+
}
|
|
2377
|
+
function hasBreaklineBefore(node) {
|
|
2378
|
+
return hasNewline(utils.start(node) - 1, true);
|
|
2379
|
+
}
|
|
2380
|
+
function hasBreaklineAfter(node) {
|
|
2381
|
+
return hasNewline(utils.end(node));
|
|
2382
|
+
}
|
|
2383
|
+
function printCommentsSeparately(ignored) {
|
|
2384
|
+
const node = getNode();
|
|
2385
|
+
const leading = [];
|
|
2386
|
+
const trailing = [];
|
|
2387
|
+
let hasTrailingLineComment = false;
|
|
2388
|
+
let hadLeadingBlockComment = false;
|
|
2389
|
+
if ("comments" in node) {
|
|
2390
|
+
pathCallEach(node, "comments", (comment) => {
|
|
2391
|
+
if (ignored?.has(comment)) {
|
|
2392
|
+
return;
|
|
2393
|
+
} else if (isLeading(comment)) {
|
|
2394
|
+
leading.push(printLeadingComment(comment));
|
|
2395
|
+
} else if (isTrailing(comment)) {
|
|
2396
|
+
trailing.push(printTrailingComment(comment));
|
|
2397
|
+
}
|
|
2398
|
+
});
|
|
2399
|
+
}
|
|
2400
|
+
return (leading.length | trailing.length) > 0 ? { leading, trailing } : { leading: "", trailing: "" };
|
|
2401
|
+
function printLeadingComment(comment) {
|
|
2402
|
+
if (utils.is_Attribute(comment) && !comment.inner) {
|
|
2403
|
+
const printed = printComment(comment);
|
|
2404
|
+
return [printed, " "];
|
|
2405
|
+
}
|
|
2406
|
+
hadLeadingBlockComment || (hadLeadingBlockComment = utils.is_BlockCommentKind(comment) && hasBreaklineBefore(comment));
|
|
2407
|
+
return [
|
|
2408
|
+
printComment(comment),
|
|
2409
|
+
utils.is_BlockCommentKind(comment) ? hasBreaklineAfter(comment) ? hadLeadingBlockComment ? hardline : line : " " : hardline,
|
|
2410
|
+
hasNewline(skipNewline(skipSpaces(utils.end(comment)))) ? hardline : ""
|
|
2411
|
+
];
|
|
2412
|
+
}
|
|
2413
|
+
function printTrailingComment(comment) {
|
|
2414
|
+
const printed = printComment(comment);
|
|
2415
|
+
return hasBreaklineBefore(comment) ? lineSuffix([hardline, isPreviousLineEmpty(comment) ? hardline : "", printed]) : utils.is_BlockCommentNode(comment) ? [" ", printed] : lineSuffix([" ", printed, hasTrailingLineComment === (hasTrailingLineComment = true) ? hardline : breakParent]);
|
|
2416
|
+
}
|
|
2417
|
+
}
|
|
2418
|
+
function getPostLeadingComment(comment) {
|
|
2419
|
+
return hasNewline(skipNewline(skipSpaces(utils.end(comment)))) ? hardline : "";
|
|
2420
|
+
}
|
|
2421
|
+
function withComments(node, printed, ignored) {
|
|
2422
|
+
const { leading, trailing } = printCommentsSeparately(ignored);
|
|
2423
|
+
return leading || trailing ? [...leading, printed, ...trailing] : printed;
|
|
2424
|
+
}
|
|
2425
|
+
function getComments(node, ...args) {
|
|
2426
|
+
return node && node.comments ? args.length > 0 ? node.comments.filter(getCommentTestFunction(...args)) : node.comments : [];
|
|
2427
|
+
}
|
|
2428
|
+
function getFirstComment(node, flags, fn) {
|
|
2429
|
+
const r = getComments(node, flags | 128 /* First */, fn);
|
|
2430
|
+
return r.length === 0 ? void 0 : r[0];
|
|
2431
|
+
}
|
|
2432
|
+
function escapeComments(flags, fn) {
|
|
2433
|
+
const comments = getAllComments().filter(getCommentTestFunction(flags, fn));
|
|
2434
|
+
comments.forEach(setDidPrintComment);
|
|
2435
|
+
return new Set(comments);
|
|
2436
|
+
}
|
|
2437
|
+
function isPrettierIgnoreComment(comment) {
|
|
2438
|
+
return utils.is_Comment(comment) && /^\s*prettier-ignore\s*/.test(comment.value) && !comment.unignore;
|
|
2439
|
+
}
|
|
2440
|
+
function isPrettierIgnoreAttribute(node) {
|
|
2441
|
+
return utils.is_Attribute(node) && /^\s*rustfmt::skip\s*$/.test(node.value);
|
|
2442
|
+
}
|
|
2443
|
+
function getCommentTestFunction(flags, fn) {
|
|
2444
|
+
return function(comment, index, comments) {
|
|
2445
|
+
return !(flags & 2 /* Leading */ && !isLeading(comment) || flags & 4 /* Trailing */ && !isTrailing(comment) || flags & 8 /* Dangling */ && !isDangling(comment) || flags & 16 /* Block */ && !utils.is_BlockCommentKind(comment) || flags & 32 /* Line */ && !utils.is_LineCommentKind(comment) || flags & 128 /* First */ && index !== 0 || flags & 256 /* Last */ && !iLast(index, comments) || flags & 64 /* PrettierIgnore */ && !(isPrettierIgnoreComment(comment) || isPrettierIgnoreAttribute(comment)) || fn && !fn(comment));
|
|
2446
|
+
};
|
|
2447
|
+
}
|
|
2448
|
+
function hasComment(node, flags = 0, fn) {
|
|
2449
|
+
if ("comments" in node && node.comments.length > 0) {
|
|
2450
|
+
return flags || fn ? node.comments.some(getCommentTestFunction(flags, fn)) : true;
|
|
2451
|
+
}
|
|
2452
|
+
return false;
|
|
2453
|
+
}
|
|
2454
|
+
function hasNewlineInRange(leftIndex, rightIndex) {
|
|
2455
|
+
const text = getContext().options.originalText;
|
|
2456
|
+
for (var i = leftIndex; i < rightIndex; ++i)
|
|
2457
|
+
if (text.charCodeAt(i) === 10)
|
|
2458
|
+
return true;
|
|
2459
|
+
return false;
|
|
2460
|
+
}
|
|
2461
|
+
function isNextLineEmpty(node) {
|
|
2462
|
+
return isNextLineEmptyAfterIndex(utils.end(node));
|
|
2463
|
+
}
|
|
2464
|
+
function isNextLineEmptyAfterIndex(index) {
|
|
2465
|
+
let oldIdx = -1;
|
|
2466
|
+
let idx = index;
|
|
2467
|
+
while (idx !== oldIdx) {
|
|
2468
|
+
oldIdx = idx;
|
|
2469
|
+
idx = skipToLineEnd(idx);
|
|
2470
|
+
idx = skipBlockComment(idx);
|
|
2471
|
+
idx = skipSpaces(idx);
|
|
2472
|
+
idx = skipParens(idx);
|
|
2473
|
+
}
|
|
2474
|
+
idx = skipLineComment(idx);
|
|
2475
|
+
idx = skipParens(idx);
|
|
2476
|
+
idx = skipNewline(idx);
|
|
2477
|
+
idx = skipParens(idx);
|
|
2478
|
+
return idx !== false && hasNewline(idx);
|
|
2479
|
+
}
|
|
2480
|
+
function hasNewline(index, backwards = false) {
|
|
2481
|
+
if (index === false)
|
|
2482
|
+
return false;
|
|
2483
|
+
const i = skipSpaces(index, backwards);
|
|
2484
|
+
return i !== false && i !== skipNewline(i, backwards);
|
|
2485
|
+
}
|
|
2486
|
+
function skipLineComment(index) {
|
|
2487
|
+
if (index === false)
|
|
2488
|
+
return false;
|
|
2489
|
+
const { commentSpans, originalText } = getContext().options;
|
|
2490
|
+
if (commentSpans.has(index) && originalText.charCodeAt(index + 1) === 47)
|
|
2491
|
+
return skipEverythingButNewLine(commentSpans.get(index));
|
|
2492
|
+
return index;
|
|
2493
|
+
}
|
|
2494
|
+
function skipBlockComment(index) {
|
|
2495
|
+
if (index === false)
|
|
2496
|
+
return false;
|
|
2497
|
+
const { commentSpans, originalText } = getContext().options;
|
|
2498
|
+
if (commentSpans.has(index) && originalText.charCodeAt(index + 1) === 42)
|
|
2499
|
+
return commentSpans.get(index);
|
|
2500
|
+
return index;
|
|
2501
|
+
}
|
|
2502
|
+
var [skipSpaces, skipToLineEnd, skipEverythingButNewLine] = [/[ \t]/, /[,; \t]/, /[^\r\n]/].map(function(re) {
|
|
2503
|
+
return function(index, backwards = false) {
|
|
2504
|
+
if (index === false)
|
|
2505
|
+
return false;
|
|
2506
|
+
const { originalText: text } = getContext().options;
|
|
2507
|
+
let cursor2 = index;
|
|
2508
|
+
while (cursor2 >= 0 && cursor2 < text.length) {
|
|
2509
|
+
if (re.test(text.charAt(cursor2)))
|
|
2510
|
+
backwards ? cursor2-- : cursor2++;
|
|
2511
|
+
else
|
|
2512
|
+
return cursor2;
|
|
2513
|
+
}
|
|
2514
|
+
return cursor2 === -1 || cursor2 === text.length ? cursor2 : false;
|
|
2515
|
+
};
|
|
2516
|
+
});
|
|
2517
|
+
function skipNewline(index, backwards = false) {
|
|
2518
|
+
if (index === false)
|
|
2519
|
+
return false;
|
|
2520
|
+
const { originalText } = getContext().options;
|
|
2521
|
+
const atIndex = originalText.charCodeAt(index);
|
|
2522
|
+
if (backwards) {
|
|
2523
|
+
if (originalText.charCodeAt(index - 1) === 13 && atIndex === 10)
|
|
2524
|
+
return index - 2;
|
|
2525
|
+
if (atIndex === 10)
|
|
2526
|
+
return index - 1;
|
|
2527
|
+
} else {
|
|
2528
|
+
if (atIndex === 13 && originalText.charCodeAt(index + 1) === 10)
|
|
2529
|
+
return index + 2;
|
|
2530
|
+
if (atIndex === 10)
|
|
2531
|
+
return index + 1;
|
|
2532
|
+
}
|
|
2533
|
+
return index;
|
|
2534
|
+
}
|
|
2535
|
+
function skipParens(index, backwards = false) {
|
|
2536
|
+
return index;
|
|
2537
|
+
}
|
|
2538
|
+
function getNextNonSpaceNonCommentCharacterIndex(node) {
|
|
2539
|
+
return getNextNonSpaceNonCommentCharacterIndexWithStartIndex(utils.end(node));
|
|
2540
|
+
}
|
|
2541
|
+
function getNextNonSpaceNonCommentCharacterIndexWithStartIndex(i) {
|
|
2542
|
+
let oldIdx = -1;
|
|
2543
|
+
let nextIdx = i;
|
|
2544
|
+
while (nextIdx !== oldIdx) {
|
|
2545
|
+
oldIdx = nextIdx;
|
|
2546
|
+
nextIdx = skipSpaces(nextIdx);
|
|
2547
|
+
nextIdx = skipBlockComment(nextIdx);
|
|
2548
|
+
nextIdx = skipLineComment(nextIdx);
|
|
2549
|
+
nextIdx = skipNewline(nextIdx);
|
|
2550
|
+
nextIdx = skipParens(nextIdx);
|
|
2551
|
+
}
|
|
2552
|
+
return nextIdx;
|
|
2553
|
+
}
|
|
2554
|
+
function handled(comment) {
|
|
2555
|
+
return "printed" in comment;
|
|
2556
|
+
}
|
|
2557
|
+
function handleCommon(ctx2) {
|
|
2558
|
+
{
|
|
2559
|
+
const { comment: comment2, precedingNode: precedingNode2, enclosingNode, followingNode: followingNode2 } = ctx2;
|
|
2560
|
+
if (!enclosingNode) {
|
|
2561
|
+
ctx2.enclosingNode = ctx2.comment.loc.src.program;
|
|
2562
|
+
} else if (enclosingNode && utils.is_NodeWithBodyOrCases(enclosingNode)) {
|
|
2563
|
+
const body = utils.getBodyOrCases(enclosingNode);
|
|
2564
|
+
if (body) {
|
|
2565
|
+
if (utils.is_ExpressionWithBodyOrCases(enclosingNode) && enclosingNode.label) {
|
|
2566
|
+
if (ctx2.precedingNode === enclosingNode.label) {
|
|
2567
|
+
ctx2.precedingNode = void 0;
|
|
2568
|
+
}
|
|
2569
|
+
if (followingNode2 === enclosingNode.label) {
|
|
2570
|
+
ctx2.followingNode = void 0;
|
|
2571
|
+
}
|
|
2572
|
+
}
|
|
2573
|
+
if (comment2.loc.isBefore(body)) {
|
|
2574
|
+
if (followingNode2 && body.loc.contains(followingNode2)) {
|
|
2575
|
+
ctx2.followingNode = void 0;
|
|
2576
|
+
}
|
|
2577
|
+
if (!ctx2.precedingNode && !ctx2.followingNode) {
|
|
2578
|
+
addLeadingComment(enclosingNode, comment2);
|
|
2579
|
+
return true;
|
|
2580
|
+
}
|
|
2581
|
+
} else if (comment2.loc.isAfter(body)) {
|
|
2582
|
+
if (precedingNode2 && body.loc.contains(precedingNode2)) {
|
|
2583
|
+
ctx2.precedingNode = void 0;
|
|
2584
|
+
}
|
|
2585
|
+
if (!ctx2.precedingNode && !ctx2.followingNode) {
|
|
2586
|
+
addTrailingComment(enclosingNode, comment2);
|
|
2587
|
+
return true;
|
|
2588
|
+
}
|
|
2589
|
+
} else if (body.loc.contains(comment2)) {
|
|
2590
|
+
if (precedingNode2 && !body.loc.contains(precedingNode2)) {
|
|
2591
|
+
ctx2.precedingNode = void 0;
|
|
2592
|
+
}
|
|
2593
|
+
if (followingNode2 && !body.loc.contains(followingNode2)) {
|
|
2594
|
+
ctx2.followingNode = void 0;
|
|
2595
|
+
}
|
|
2596
|
+
}
|
|
2597
|
+
}
|
|
2598
|
+
}
|
|
2599
|
+
}
|
|
2600
|
+
for (const fn of [
|
|
2601
|
+
handleMixedInOuterAttributeComments,
|
|
2602
|
+
handleAttributeComments,
|
|
2603
|
+
handleDanglingComments,
|
|
2604
|
+
handleFunctionComments,
|
|
2605
|
+
handleMacroRuleComments,
|
|
2606
|
+
handleStructLiteralComments,
|
|
2607
|
+
handleVariableDeclaratorComments,
|
|
2608
|
+
handleIfBlockExpressionComments,
|
|
2609
|
+
handleMemberExpressionComments,
|
|
2610
|
+
handleStatementComments,
|
|
2611
|
+
handleFlowControlComments,
|
|
2612
|
+
handleBadComments
|
|
2613
|
+
]) {
|
|
2614
|
+
fn(ctx2);
|
|
2615
|
+
if (handled(ctx2.comment)) {
|
|
2616
|
+
return true;
|
|
2617
|
+
}
|
|
2618
|
+
}
|
|
2619
|
+
const { precedingNode, followingNode, comment } = ctx2;
|
|
2620
|
+
if (isStartOfLine(comment)) {
|
|
2621
|
+
if (followingNode) {
|
|
2622
|
+
addLeadingComment(followingNode, comment);
|
|
2623
|
+
} else if (precedingNode) {
|
|
2624
|
+
addTrailingComment(precedingNode, comment);
|
|
2625
|
+
} else {
|
|
2626
|
+
exit.never(ctx2);
|
|
2627
|
+
}
|
|
2628
|
+
} else if (isEndOfLine(comment)) {
|
|
2629
|
+
if (precedingNode) {
|
|
2630
|
+
addTrailingComment(precedingNode, comment);
|
|
2631
|
+
} else if (followingNode) {
|
|
2632
|
+
addLeadingComment(followingNode, comment);
|
|
2633
|
+
} else {
|
|
2634
|
+
exit.never(ctx2);
|
|
2635
|
+
}
|
|
2636
|
+
} else {
|
|
2637
|
+
if (precedingNode && followingNode) {
|
|
2638
|
+
return false;
|
|
2639
|
+
} else if (precedingNode) {
|
|
2640
|
+
addTrailingComment(precedingNode, comment);
|
|
2641
|
+
} else if (followingNode) {
|
|
2642
|
+
addLeadingComment(followingNode, comment);
|
|
2643
|
+
} else {
|
|
2644
|
+
exit.never(ctx2);
|
|
2645
|
+
}
|
|
2646
|
+
}
|
|
2647
|
+
return handled(ctx2.comment);
|
|
2648
|
+
}
|
|
2649
|
+
function handleOwnLineComment(ctx2) {
|
|
2650
|
+
return handleCommon(ctx2);
|
|
2651
|
+
}
|
|
2652
|
+
function handleEndOfLineComment(ctx2) {
|
|
2653
|
+
const { precedingNode, enclosingNode, comment } = ctx2;
|
|
2654
|
+
if (
|
|
2655
|
+
// handleCallExpressionComments
|
|
2656
|
+
precedingNode && enclosingNode && is_CallExpression_or_CallLikeMacroInvocation(enclosingNode) && enclosingNode.arguments.length > 0 && precedingNode === (enclosingNode.typeArguments ? last_of(enclosingNode.typeArguments) : enclosingNode.callee)
|
|
2657
|
+
) {
|
|
2658
|
+
addLeadingComment(enclosingNode.arguments[0], comment);
|
|
2659
|
+
return true;
|
|
2660
|
+
} else if (
|
|
2661
|
+
// handlePropertyComments
|
|
2662
|
+
enclosingNode && utils.is_StructLiteralProperty(enclosingNode)
|
|
2663
|
+
) {
|
|
2664
|
+
addLeadingComment(enclosingNode, comment);
|
|
2665
|
+
return true;
|
|
2666
|
+
} else {
|
|
2667
|
+
return handleCommon(ctx2);
|
|
2668
|
+
}
|
|
2669
|
+
}
|
|
2670
|
+
function handleRemainingComment(ctx2) {
|
|
2671
|
+
return handleCommon(ctx2);
|
|
2672
|
+
}
|
|
2673
|
+
function handleStructLiteralComments({ enclosingNode, followingNode, comment }) {
|
|
2674
|
+
if (enclosingNode && utils.is_StructLiteralPropertySpread(enclosingNode) && followingNode === enclosingNode.expression) {
|
|
2675
|
+
addLeadingComment(enclosingNode, comment);
|
|
2676
|
+
}
|
|
2677
|
+
}
|
|
2678
|
+
function handleVariableDeclaratorComments({ enclosingNode, followingNode, comment }) {
|
|
2679
|
+
if (enclosingNode && (is_xVariableEqualishLike(enclosingNode) || utils.is_ReassignmentNode(enclosingNode)) && followingNode && (utils.is_BlockCommentKind(comment) || utils.nisAnyOf(followingNode, [
|
|
2680
|
+
jinxRust.NodeType.StructLiteral,
|
|
2681
|
+
jinxRust.NodeType.StructPattern,
|
|
2682
|
+
jinxRust.NodeType.TupleLiteral,
|
|
2683
|
+
jinxRust.NodeType.TypeTuple,
|
|
2684
|
+
jinxRust.NodeType.TuplePattern,
|
|
2685
|
+
jinxRust.NodeType.ArrayLiteral,
|
|
2686
|
+
jinxRust.NodeType.ArrayPattern,
|
|
2687
|
+
jinxRust.NodeType.SizedArrayLiteral,
|
|
2688
|
+
jinxRust.NodeType.TypeSizedArray
|
|
2689
|
+
]))) {
|
|
2690
|
+
addLeadingComment(followingNode, comment);
|
|
2691
|
+
}
|
|
2692
|
+
}
|
|
2693
|
+
function handleMixedInOuterAttributeComments({ precedingNode, enclosingNode, followingNode, comment }) {
|
|
2694
|
+
if (enclosingNode && utils.hasOuterAttributes(enclosingNode) && utils.end(comment) <= utils.ownStart(enclosingNode)) {
|
|
2695
|
+
if (isPrettierIgnoreComment(comment) || isPrettierIgnoreAttribute(comment)) {
|
|
2696
|
+
setPrettierIgnoreTarget(enclosingNode, comment);
|
|
2697
|
+
}
|
|
2698
|
+
if (isEndOfLine(comment)) {
|
|
2699
|
+
if (shouldPrintOuterAttributesAbove(enclosingNode)) {
|
|
2700
|
+
addTrailingComment(precedingNode, comment);
|
|
2701
|
+
} else {
|
|
2702
|
+
addLeadingComment(followingNode || enclosingNode, comment);
|
|
2703
|
+
}
|
|
2704
|
+
} else {
|
|
2705
|
+
if (followingNode && utils.end(followingNode) <= utils.ownStart(enclosingNode)) {
|
|
2706
|
+
addLeadingComment(followingNode, comment);
|
|
2707
|
+
} else if (precedingNode && enclosingNode.loc.contains(precedingNode)) {
|
|
2708
|
+
addTrailingComment(precedingNode, comment);
|
|
2709
|
+
} else {
|
|
2710
|
+
addLeadingComment(enclosingNode, comment);
|
|
2711
|
+
}
|
|
2712
|
+
}
|
|
2713
|
+
}
|
|
2714
|
+
}
|
|
2715
|
+
function handleAttributeComments({ precedingNode, enclosingNode, followingNode, comment, ast }) {
|
|
2716
|
+
if (utils.is_AttributeOrDocComment(comment)) {
|
|
2717
|
+
if (comment.inner && enclosingNode && utils.is_FunctionDeclaration(enclosingNode) && (!followingNode || !utils.is_StatementNode(followingNode)) && (!precedingNode || !utils.is_StatementNode(precedingNode))) {
|
|
2718
|
+
if (enclosingNode.body) {
|
|
2719
|
+
if (canAttachCommentInLocArray(enclosingNode.body)) {
|
|
2720
|
+
addDanglingComment(enclosingNode, comment, "body" /* body */);
|
|
2721
|
+
} else {
|
|
2722
|
+
addLeadingComment(enclosingNode.body[0], comment);
|
|
2723
|
+
}
|
|
2724
|
+
} else {
|
|
2725
|
+
addLeadingComment(enclosingNode, comment);
|
|
2726
|
+
}
|
|
2727
|
+
} else {
|
|
2728
|
+
if (followingNode) {
|
|
2729
|
+
addLeadingComment(followingNode, comment);
|
|
2730
|
+
} else if (enclosingNode) {
|
|
2731
|
+
for (var key in DCM)
|
|
2732
|
+
if (key in enclosingNode) {
|
|
2733
|
+
addDanglingComment(enclosingNode, comment, key);
|
|
2734
|
+
return;
|
|
2735
|
+
}
|
|
2736
|
+
} else {
|
|
2737
|
+
addDanglingComment(ast, comment, "body" /* body */);
|
|
2738
|
+
}
|
|
2739
|
+
}
|
|
2740
|
+
}
|
|
2741
|
+
}
|
|
2742
|
+
function handleBadComments({ precedingNode, enclosingNode, followingNode, ast, comment }) {
|
|
2743
|
+
if (!enclosingNode) {
|
|
2744
|
+
if (followingNode) {
|
|
2745
|
+
addLeadingComment(followingNode, comment);
|
|
2746
|
+
} else if (precedingNode) {
|
|
2747
|
+
addTrailingComment(precedingNode, comment);
|
|
2748
|
+
} else {
|
|
2749
|
+
addDanglingComment(enclosingNode || ast, comment, "body" /* body */);
|
|
2750
|
+
}
|
|
2751
|
+
} else if (!precedingNode && !followingNode) {
|
|
2752
|
+
if (enclosingNode && enclosingNode !== ast) {
|
|
2753
|
+
addLeadingComment(enclosingNode, comment);
|
|
2754
|
+
} else {
|
|
2755
|
+
addDanglingComment(ast, comment, "body" /* body */);
|
|
2756
|
+
}
|
|
2757
|
+
}
|
|
2758
|
+
}
|
|
2759
|
+
function is_ABI_Comment({ precedingNode, enclosingNode, comment }) {
|
|
2760
|
+
return utils.is_CommentOrDocComment(comment) && (precedingNode && utils.is_ExternSpecifier(precedingNode) || enclosingNode && utils.is_ExternSpecifier(enclosingNode));
|
|
2761
|
+
}
|
|
2762
|
+
function handleFlowControlComments({ precedingNode, enclosingNode, followingNode, comment }) {
|
|
2763
|
+
if (enclosingNode && utils.is_FlowControlExpression(enclosingNode)) {
|
|
2764
|
+
if (!precedingNode && (isOwnLine(comment) || isEndOfLine(comment)) && !followingNode) {
|
|
2765
|
+
addLeadingComment(enclosingNode, comment);
|
|
2766
|
+
}
|
|
2767
|
+
}
|
|
2768
|
+
}
|
|
2769
|
+
function handleFunctionComments(ctx2) {
|
|
2770
|
+
const { precedingNode, enclosingNode, followingNode, comment } = ctx2;
|
|
2771
|
+
if (enclosingNode && utils.is_FunctionNode(enclosingNode)) {
|
|
2772
|
+
if (utils.is_FunctionDeclaration(enclosingNode) && (!is_ABI_Comment(ctx2) && comment.loc.isBefore(enclosingNode.generics || enclosingNode.id) || enclosingNode.generics && comment.loc.isBetween(enclosingNode.generics, enclosingNode.parameters))) {
|
|
2773
|
+
addLeadingComment(enclosingNode, comment);
|
|
2774
|
+
} else if (!enclosingNode.returnType && comment.loc.isBetween(
|
|
2775
|
+
enclosingNode.parameters,
|
|
2776
|
+
utils.is_FunctionDeclaration(enclosingNode) ? enclosingNode.body : enclosingNode.expression
|
|
2777
|
+
)) {
|
|
2778
|
+
if (utils.is_FunctionDeclaration(enclosingNode)) {
|
|
2779
|
+
addCommentToBlock(enclosingNode, comment);
|
|
2780
|
+
} else {
|
|
2781
|
+
addLeadingComment(enclosingNode.expression, comment);
|
|
2782
|
+
}
|
|
2783
|
+
} else if (precedingNode && //
|
|
2784
|
+
enclosingNode.parameters.loc.contains(comment)) {
|
|
2785
|
+
if (precedingNode === utils.getLastParameter(enclosingNode)) {
|
|
2786
|
+
addTrailingComment(precedingNode, comment);
|
|
2787
|
+
}
|
|
2788
|
+
} else if (followingNode && isStartOfLine(comment) && comment.loc.isAfter(enclosingNode.parameters) && (!utils.is_FunctionDeclaration(enclosingNode) || !enclosingNode.whereBounds || comment.loc.isAfter(enclosingNode.whereBounds)) && (!enclosingNode.returnType || comment.loc.isAfter(enclosingNode.returnType)) && followingNode === (utils.is_FunctionDeclaration(enclosingNode) ? enclosingNode.body?.[0] : enclosingNode.expression)) {
|
|
2789
|
+
addLeadingComment(followingNode, comment);
|
|
2790
|
+
}
|
|
2791
|
+
}
|
|
2792
|
+
}
|
|
2793
|
+
function handleMacroRuleComments(ctx2) {
|
|
2794
|
+
const { precedingNode, enclosingNode, followingNode, comment } = ctx2;
|
|
2795
|
+
if (enclosingNode && utils.is_MacroRule(enclosingNode)) {
|
|
2796
|
+
if (enclosingNode.transform.loc.contains(comment)) {
|
|
2797
|
+
if (!precedingNode || !enclosingNode.transform.loc.contains(precedingNode)) {
|
|
2798
|
+
addLeadingComment(followingNode, comment);
|
|
2799
|
+
}
|
|
2800
|
+
} else if (enclosingNode.match.loc.contains(comment)) {
|
|
2801
|
+
if (!followingNode || !enclosingNode.match.loc.contains(followingNode)) {
|
|
2802
|
+
addTrailingComment(precedingNode, comment);
|
|
2803
|
+
}
|
|
2804
|
+
}
|
|
2805
|
+
}
|
|
2806
|
+
}
|
|
2807
|
+
function handleStatementComments(ctx2) {
|
|
2808
|
+
const { precedingNode, comment } = ctx2;
|
|
2809
|
+
if (isEndOfLine(comment) && precedingNode && (utils.is_StatementNode(precedingNode) || precedingNode.loc.sliceText().endsWith(";"))) {
|
|
2810
|
+
addTrailingComment(precedingNode, comment);
|
|
2811
|
+
}
|
|
2812
|
+
}
|
|
2813
|
+
function addCommentToBlock(block, comment) {
|
|
2814
|
+
const body = utils.getBodyOrCases(block);
|
|
2815
|
+
if (body.length > 0) {
|
|
2816
|
+
addLeadingComment(body[0], comment);
|
|
2817
|
+
} else {
|
|
2818
|
+
addDanglingComment(block, comment, "body" /* body */);
|
|
2819
|
+
}
|
|
2820
|
+
}
|
|
2821
|
+
function handleIfBlockExpressionComments(ctx2) {
|
|
2822
|
+
const { comment, enclosingNode } = ctx2;
|
|
2823
|
+
if (enclosingNode && utils.is_IfBlockExpression(enclosingNode)) {
|
|
2824
|
+
const { condition, body, else: else_ } = enclosingNode;
|
|
2825
|
+
if (comment.loc.isBefore(condition)) {
|
|
2826
|
+
addLeadingComment(condition, comment);
|
|
2827
|
+
} else if (comment.loc.isBetween(condition, body)) {
|
|
2828
|
+
addTrailingComment(condition, comment);
|
|
2829
|
+
} else if (else_ && comment.loc.isBetween(body, else_)) {
|
|
2830
|
+
if (utils.is_IfBlockExpression(else_)) {
|
|
2831
|
+
addLeadingComment(else_.condition, comment);
|
|
2832
|
+
} else {
|
|
2833
|
+
addCommentToBlock(else_, comment);
|
|
2834
|
+
}
|
|
2835
|
+
}
|
|
2836
|
+
}
|
|
2837
|
+
}
|
|
2838
|
+
function handleMemberExpressionComments({ comment, precedingNode, enclosingNode }) {
|
|
2839
|
+
if (enclosingNode && is_MemberAccessLike(enclosingNode)) {
|
|
2840
|
+
if (isStartOfLine(comment) || !precedingNode)
|
|
2841
|
+
addLeadingComment(enclosingNode, comment);
|
|
2842
|
+
else
|
|
2843
|
+
addTrailingComment(precedingNode, comment);
|
|
2844
|
+
return true;
|
|
2845
|
+
}
|
|
2846
|
+
return false;
|
|
2847
|
+
}
|
|
2848
|
+
function handleDanglingComments({ comment, enclosingNode }) {
|
|
2849
|
+
if (enclosingNode) {
|
|
2850
|
+
for (var key in DCM) {
|
|
2851
|
+
if (key in enclosingNode) {
|
|
2852
|
+
var arr = enclosingNode[key];
|
|
2853
|
+
if (utils.is_LocArray(arr) && canAttachCommentInLocArray(arr) && arr.loc.contains(comment)) {
|
|
2854
|
+
addDanglingComment(enclosingNode, comment, key);
|
|
2855
|
+
return;
|
|
2856
|
+
}
|
|
2857
|
+
}
|
|
2858
|
+
}
|
|
2859
|
+
}
|
|
2860
|
+
}
|
|
2861
|
+
function canAttachCommentInLocArray(arr) {
|
|
2862
|
+
return arr.length === 0 || arr.every((node) => !canAttachComment(node));
|
|
2863
|
+
}
|
|
2864
|
+
function isOwnLine(comment) {
|
|
2865
|
+
return isStartOfLine(comment) && hasBreaklineAfter(comment);
|
|
2866
|
+
}
|
|
2867
|
+
function isStartOfLine(comment) {
|
|
2868
|
+
return comment.placement === "ownLine";
|
|
2869
|
+
}
|
|
2870
|
+
function isEndOfLine(comment) {
|
|
2871
|
+
return comment.placement === "endOfLine";
|
|
2872
|
+
}
|
|
2873
|
+
function isDangling(comment) {
|
|
2874
|
+
return !comment.leading && !comment.trailing;
|
|
2875
|
+
}
|
|
2876
|
+
function isLeading(comment) {
|
|
2877
|
+
return comment.leading && !comment.trailing;
|
|
2878
|
+
}
|
|
2879
|
+
function isTrailing(comment) {
|
|
2880
|
+
return !comment.leading && comment.trailing;
|
|
2881
|
+
}
|
|
2882
|
+
function print_comment(comment) {
|
|
2883
|
+
const doc = utils.is_BlockCommentNode(comment) ? isIndentableBlockComment(comment.value) ? [
|
|
2884
|
+
(!handled(comment) || isTrailing(comment)) && !hasBreaklineBefore(comment) ? hardline : "",
|
|
2885
|
+
getCommentStart(comment),
|
|
2886
|
+
...comment.value.split(/\n/g).map(
|
|
2887
|
+
(line2, i, a) => i === 0 ? [line2.trimEnd(), hardline] : !iLast(i, a) ? [" " + line2.trim(), hardline] : " " + line2.trimStart()
|
|
2888
|
+
),
|
|
2889
|
+
"*/"
|
|
2890
|
+
] : [
|
|
2891
|
+
getCommentStart(comment),
|
|
2892
|
+
//
|
|
2893
|
+
join(literalline, comment.value.split(/\n/g)),
|
|
2894
|
+
"*/"
|
|
2895
|
+
] : [getCommentStart(comment), comment.value.trimEnd()];
|
|
2896
|
+
return handled(comment) && isDangling(comment) ? [doc, getPostLeadingComment(comment)] : doc;
|
|
2897
|
+
function getCommentStart(comment2) {
|
|
2898
|
+
return utils.is_Comment(comment2) ? utils.is_BlockCommentKind(comment2) ? "/*" : "//" : utils.is_BlockCommentKind(comment2) ? utils.isInner(comment2) ? "/*!" : "/**" : utils.isInner(comment2) ? "//!" : "///";
|
|
2899
|
+
}
|
|
2900
|
+
function isIndentableBlockComment(value) {
|
|
2901
|
+
const lines = `*${value}*`.split(/\n/g);
|
|
2902
|
+
return lines.length > 1 && lines.every((line2) => /^\s*\*/.test(line2));
|
|
2903
|
+
}
|
|
2904
|
+
}
|
|
2905
|
+
function isIdent(node, name) {
|
|
2906
|
+
return !!node && utils.is_Identifier(node) && (null == name || node.name === name);
|
|
2907
|
+
}
|
|
2908
|
+
function isToken(node, tk) {
|
|
2909
|
+
return !!node && (null == tk ? utils.is_PunctuationToken(node) : utils.isTK(node, tk));
|
|
2910
|
+
}
|
|
2911
|
+
function isGroup(node, dk) {
|
|
2912
|
+
return !!node && utils.is_DelimGroup(node) && (null == dk || node.segments.dk === dk);
|
|
2913
|
+
}
|
|
2914
|
+
|
|
2915
|
+
// src/transform/custom/attribute.ts
|
|
2916
|
+
function transform_simpleAttrSyntax(segments) {
|
|
2917
|
+
assert(segments.length !== 0, segments.loc.url());
|
|
2918
|
+
return transform_segments(segments, false);
|
|
2919
|
+
function transform_segments(seq, nestedCall) {
|
|
2920
|
+
let i = 0;
|
|
2921
|
+
if (nestedCall) {
|
|
2922
|
+
const args = jinxRust.rs.createLocArray(jinxRust.DelimKind["()"], seq.loc.clone());
|
|
2923
|
+
while (i !== seq.length) {
|
|
2924
|
+
args.push(read(true));
|
|
2925
|
+
if (i === seq.length)
|
|
2926
|
+
break;
|
|
2927
|
+
assert(utils.isTK(seq[i++], jinxRust.TK[","]));
|
|
2928
|
+
}
|
|
2929
|
+
return args;
|
|
2930
|
+
} else {
|
|
2931
|
+
const res = read(true);
|
|
2932
|
+
assert(i === seq.length, res.loc.url());
|
|
2933
|
+
return res;
|
|
2934
|
+
}
|
|
2935
|
+
function read(allowEq) {
|
|
2936
|
+
let lhs;
|
|
2937
|
+
switch (seq[i].nodeType) {
|
|
2938
|
+
case jinxRust.NodeType.Literal:
|
|
2939
|
+
return seq[i++];
|
|
2940
|
+
case jinxRust.NodeType.Identifier:
|
|
2941
|
+
lhs = seq[i++];
|
|
2942
|
+
break;
|
|
2943
|
+
case jinxRust.NodeType.PunctuationToken:
|
|
2944
|
+
assert(seq[i].tk === jinxRust.TK["::"], seq[i].loc.url());
|
|
2945
|
+
lhs = eatPathSegment(void 0);
|
|
2946
|
+
break;
|
|
2947
|
+
default:
|
|
2948
|
+
exit.never();
|
|
2949
|
+
}
|
|
2950
|
+
while (true) {
|
|
2951
|
+
if (i === seq.length)
|
|
2952
|
+
return lhs;
|
|
2953
|
+
const seg = seq[i];
|
|
2954
|
+
switch (seg.nodeType) {
|
|
2955
|
+
case jinxRust.NodeType.PunctuationToken:
|
|
2956
|
+
switch (seg.tk) {
|
|
2957
|
+
case jinxRust.TK[","]:
|
|
2958
|
+
assert(nestedCall);
|
|
2959
|
+
return lhs;
|
|
2960
|
+
case jinxRust.TK["="]: {
|
|
2961
|
+
assert(allowEq);
|
|
2962
|
+
const right = (i++, read(false));
|
|
2963
|
+
return jinxRust.rs.mockNode(jinxRust.NodeType.ReassignmentExpression, right.loc.cloneFrom(utils.start(lhs)), {
|
|
2964
|
+
tk: jinxRust.TK["="],
|
|
2965
|
+
kind: jinxRust.DelimKind["="],
|
|
2966
|
+
left: lhs,
|
|
2967
|
+
right
|
|
2968
|
+
});
|
|
2969
|
+
}
|
|
2970
|
+
case jinxRust.TK["::"]:
|
|
2971
|
+
lhs = eatPathSegment(lhs);
|
|
2972
|
+
continue;
|
|
2973
|
+
default:
|
|
2974
|
+
exit.never();
|
|
2975
|
+
}
|
|
2976
|
+
case jinxRust.NodeType.DelimGroup:
|
|
2977
|
+
assert(seg.segments.dk === jinxRust.DelimKind["()"]);
|
|
2978
|
+
return jinxRust.rs.mockNode(jinxRust.NodeType.CallExpression, seq[i++].loc.cloneFrom(utils.start(lhs)), {
|
|
2979
|
+
callee: lhs,
|
|
2980
|
+
typeArguments: void 0,
|
|
2981
|
+
method: void 0,
|
|
2982
|
+
arguments: transform_segments(seg.segments, true)
|
|
2983
|
+
});
|
|
2984
|
+
default:
|
|
2985
|
+
exit.never();
|
|
2986
|
+
}
|
|
2987
|
+
}
|
|
2988
|
+
}
|
|
2989
|
+
function eatPathSegment(left) {
|
|
2990
|
+
const segment = seq[i + 1];
|
|
2991
|
+
assert(isIdent(segment));
|
|
2992
|
+
const res = jinxRust.rs.mockNode(jinxRust.NodeType.ExpressionPath, segment.loc.cloneFrom(utils.start(left ?? seq[i])), { namespace: left, segment });
|
|
2993
|
+
i += 2;
|
|
2994
|
+
return res;
|
|
2995
|
+
}
|
|
2996
|
+
}
|
|
2997
|
+
}
|
|
2998
|
+
function transform_macro_cfg_if(segments) {
|
|
2999
|
+
const danglingAttributes = [];
|
|
3000
|
+
const comments = [];
|
|
3001
|
+
const block = function create_if_block(i) {
|
|
3002
|
+
if (i >= segments.length)
|
|
3003
|
+
return void 0;
|
|
3004
|
+
const _if = segments[i];
|
|
3005
|
+
const pound = segments[i + 1];
|
|
3006
|
+
const grp = segments[i + 2];
|
|
3007
|
+
const block2 = segments[i + 3];
|
|
3008
|
+
const _else = segments[i + 4];
|
|
3009
|
+
assert(
|
|
3010
|
+
isIdent(_if, "if") && isToken(pound, jinxRust.TK["#"]) && isGroup(grp, jinxRust.DelimKind["[]"]) && isGroup(block2, jinxRust.DelimKind["{}"]) && (!_else || isIdent(_else, "else"))
|
|
3011
|
+
);
|
|
3012
|
+
return create_block(
|
|
3013
|
+
block2,
|
|
3014
|
+
(body) => jinxRust.rs.mockNode(jinxRust.NodeType.IfBlockExpression, block2.loc.cloneFrom(utils.start(_if)), {
|
|
3015
|
+
label: void 0,
|
|
3016
|
+
condition: jinxRust.rs.mockNode(jinxRust.NodeType.Attribute, grp.loc.cloneFrom(utils.start(pound)), {
|
|
3017
|
+
segments: grp.segments,
|
|
3018
|
+
value: grp.segments.loc.sliceText(),
|
|
3019
|
+
line: false,
|
|
3020
|
+
inner: false
|
|
3021
|
+
}),
|
|
3022
|
+
body,
|
|
3023
|
+
else: (_else && iLast(i + 5, segments) ? function create_else_block(i2) {
|
|
3024
|
+
const block3 = segments[i2];
|
|
3025
|
+
assert(isGroup(block3, jinxRust.DelimKind["{}"]));
|
|
3026
|
+
return create_block(
|
|
3027
|
+
block3,
|
|
3028
|
+
(body2) => jinxRust.rs.mockNode(jinxRust.NodeType.BlockExpression, body2.loc.clone(), {
|
|
3029
|
+
label: void 0,
|
|
3030
|
+
body: body2
|
|
3031
|
+
})
|
|
3032
|
+
);
|
|
3033
|
+
} : create_if_block)(i + 5)
|
|
3034
|
+
})
|
|
3035
|
+
);
|
|
3036
|
+
}(0);
|
|
3037
|
+
const ast = jinxRust.rs.createLocArray(
|
|
3038
|
+
segments.dk,
|
|
3039
|
+
segments.loc,
|
|
3040
|
+
block && [
|
|
3041
|
+
jinxRust.rs.mockNode(jinxRust.NodeType.ExpressionStatement, block.loc.clone(), {
|
|
3042
|
+
expression: block,
|
|
3043
|
+
semi: false
|
|
3044
|
+
})
|
|
3045
|
+
]
|
|
3046
|
+
);
|
|
3047
|
+
return jinxRust.rs.mockNode(jinxRust.NodeType.Snippet, segments.loc.clone(), { ast, danglingAttributes, comments });
|
|
3048
|
+
function create_block(group2, fn) {
|
|
3049
|
+
const snippet = jinxRust.rs.toBlockBody(group2.segments);
|
|
3050
|
+
utils.insertNodes(danglingAttributes, snippet.danglingAttributes);
|
|
3051
|
+
utils.insertNodes(comments, snippet.comments);
|
|
3052
|
+
const block2 = fn(snippet.ast);
|
|
3053
|
+
utils.transferAttributes(snippet, block2);
|
|
3054
|
+
return block2;
|
|
3055
|
+
}
|
|
3056
|
+
}
|
|
3057
|
+
|
|
3058
|
+
// src/transform/index.ts
|
|
3059
|
+
function is_CallLikeMacroInvocation(node) {
|
|
3060
|
+
return utils.is_MacroInvocation(node) && "arguments" in node;
|
|
3061
|
+
}
|
|
3062
|
+
function is_BlockLikeMacroInvocation(node) {
|
|
3063
|
+
return utils.is_MacroInvocation(node) && "body" in node;
|
|
3064
|
+
}
|
|
3065
|
+
function is_CallExpression_or_CallLikeMacroInvocation(node) {
|
|
3066
|
+
return utils.is_CallExpression(node) || is_CallLikeMacroInvocation(node);
|
|
3067
|
+
}
|
|
3068
|
+
var IGNORED_MACROS = /* @__PURE__ */ new Set([
|
|
3069
|
+
// std
|
|
3070
|
+
// crates
|
|
3071
|
+
"quote"
|
|
3072
|
+
]);
|
|
3073
|
+
var HARDCODED_MACRO_DELIMS = /* @__PURE__ */ new Map();
|
|
3074
|
+
each(
|
|
3075
|
+
{
|
|
3076
|
+
[jinxRust.DelimKind["{}"]]: [
|
|
3077
|
+
// std
|
|
3078
|
+
"thread_local",
|
|
3079
|
+
// crates
|
|
3080
|
+
"cfg_if"
|
|
3081
|
+
],
|
|
3082
|
+
[jinxRust.DelimKind["()"]]: [
|
|
3083
|
+
// std
|
|
3084
|
+
"assert_eq",
|
|
3085
|
+
"assert_ne",
|
|
3086
|
+
"assert",
|
|
3087
|
+
"cfg",
|
|
3088
|
+
"concat_bytes",
|
|
3089
|
+
"concat_idents",
|
|
3090
|
+
"concat",
|
|
3091
|
+
"debug_assert_eq",
|
|
3092
|
+
"debug_assert_ne",
|
|
3093
|
+
"debug_assert",
|
|
3094
|
+
"eprint",
|
|
3095
|
+
"eprintln",
|
|
3096
|
+
"format_args_nl",
|
|
3097
|
+
"format_args",
|
|
3098
|
+
"format",
|
|
3099
|
+
"matches",
|
|
3100
|
+
"panic",
|
|
3101
|
+
"print",
|
|
3102
|
+
"println",
|
|
3103
|
+
"try",
|
|
3104
|
+
"unimplemented",
|
|
3105
|
+
"unreachable",
|
|
3106
|
+
"write",
|
|
3107
|
+
"writeln"
|
|
3108
|
+
// crates
|
|
3109
|
+
],
|
|
3110
|
+
[jinxRust.DelimKind["[]"]]: [
|
|
3111
|
+
// std
|
|
3112
|
+
"vec"
|
|
3113
|
+
// crates
|
|
3114
|
+
]
|
|
3115
|
+
},
|
|
3116
|
+
(names, tk) => each(names, (name) => {
|
|
3117
|
+
HARDCODED_MACRO_DELIMS.set(name, +tk);
|
|
3118
|
+
})
|
|
3119
|
+
);
|
|
3120
|
+
var _COMMENTS = void 0;
|
|
3121
|
+
var _DANGLING_ATTRIBUTES = void 0;
|
|
3122
|
+
function transform_ast(options2) {
|
|
3123
|
+
try {
|
|
3124
|
+
_COMMENTS = options2.comments;
|
|
3125
|
+
_DANGLING_ATTRIBUTES = options2.danglingAttributes;
|
|
3126
|
+
transformNode(options2.rsParsedFile);
|
|
3127
|
+
} finally {
|
|
3128
|
+
_depth = 0;
|
|
3129
|
+
_COMMENTS = void 0;
|
|
3130
|
+
_DANGLING_ATTRIBUTES = void 0;
|
|
3131
|
+
}
|
|
3132
|
+
}
|
|
3133
|
+
var _depth = 0;
|
|
3134
|
+
var isReadingSnippet = () => 0 !== _depth;
|
|
3135
|
+
function maybe_transform_node(node, read_snippet, fn) {
|
|
3136
|
+
const snippet = try_eval(read_snippet);
|
|
3137
|
+
if (snippet) {
|
|
3138
|
+
++_depth;
|
|
3139
|
+
transformNode(snippet);
|
|
3140
|
+
--_depth;
|
|
3141
|
+
fn(node, snippet);
|
|
3142
|
+
transformed.add(node);
|
|
3143
|
+
return node;
|
|
3144
|
+
}
|
|
3145
|
+
}
|
|
3146
|
+
var transformed = /* @__PURE__ */ new WeakSet();
|
|
3147
|
+
function isTransformed(node) {
|
|
3148
|
+
return transformed.has(node);
|
|
3149
|
+
}
|
|
3150
|
+
var transform = {
|
|
3151
|
+
[jinxRust.NodeType.Attribute](node) {
|
|
3152
|
+
try_eval(() => {
|
|
3153
|
+
node.segments = jinxRust.rs.createLocArray(node.segments.dk, node.segments.loc.clone(), [
|
|
3154
|
+
transform_simpleAttrSyntax(node.segments)
|
|
3155
|
+
]);
|
|
3156
|
+
transformed.add(node);
|
|
3157
|
+
});
|
|
3158
|
+
},
|
|
3159
|
+
[jinxRust.NodeType.MacroInlineRuleDeclaration](node) {
|
|
3160
|
+
node.match.dk = jinxRust.DelimKind["()"];
|
|
3161
|
+
node.transform.dk = jinxRust.DelimKind["{}"];
|
|
3162
|
+
},
|
|
3163
|
+
[jinxRust.NodeType.MacroInvocation](node) {
|
|
3164
|
+
const name = utils.getMacroName(node);
|
|
3165
|
+
if (IGNORED_MACROS.has(name) || node.segments.length === 0 || node.segments.length === 1 && utils.is_PunctuationToken(node.segments[0])) {
|
|
3166
|
+
return;
|
|
3167
|
+
}
|
|
3168
|
+
const tk = transformMacroDelim(name, node);
|
|
3169
|
+
if (name === "cfg_if") {
|
|
3170
|
+
transformBlockLike(() => transform_macro_cfg_if(node.segments));
|
|
3171
|
+
} else if (tk === jinxRust.DelimKind["{}"]) {
|
|
3172
|
+
transformBlockLike();
|
|
3173
|
+
} else {
|
|
3174
|
+
transformCallLike();
|
|
3175
|
+
}
|
|
3176
|
+
function transformBlockLike(transform2 = () => jinxRust.rs.toBlockBody(node.segments)) {
|
|
3177
|
+
return maybe_transform_node(node, transform2, (node2, snippet) => {
|
|
3178
|
+
const _body = snippet.ast;
|
|
3179
|
+
_body.dk = tk;
|
|
3180
|
+
node2.body = _body;
|
|
3181
|
+
node2.segments = _body;
|
|
3182
|
+
utils.transferAttributes(snippet, node2);
|
|
3183
|
+
});
|
|
3184
|
+
}
|
|
3185
|
+
function transformCallLike() {
|
|
3186
|
+
return maybe_transform_node(
|
|
3187
|
+
node,
|
|
3188
|
+
() => jinxRust.rs.toCallExpressionArguments(node.segments),
|
|
3189
|
+
(node2, snippet) => {
|
|
3190
|
+
const _arguments = snippet.ast;
|
|
3191
|
+
_arguments.dk = tk;
|
|
3192
|
+
node2.method = void 0;
|
|
3193
|
+
node2.typeArguments = void 0;
|
|
3194
|
+
node2.arguments = _arguments;
|
|
3195
|
+
node2.segments = _arguments;
|
|
3196
|
+
}
|
|
3197
|
+
);
|
|
3198
|
+
}
|
|
3199
|
+
},
|
|
3200
|
+
[jinxRust.NodeType.CallExpression](node) {
|
|
3201
|
+
if (utils.hasMethod(node)) {
|
|
3202
|
+
node.callee = jinxRust.rs.mockNode(jinxRust.NodeType.MemberExpression, node.method.loc.cloneFrom(utils.start(node.callee)), {
|
|
3203
|
+
expression: node.callee,
|
|
3204
|
+
property: node.method,
|
|
3205
|
+
computed: false
|
|
3206
|
+
});
|
|
3207
|
+
node.method = void 0;
|
|
3208
|
+
getOptions().actuallyMethodNodes.add(node.callee);
|
|
3209
|
+
}
|
|
3210
|
+
},
|
|
3211
|
+
[jinxRust.NodeType.AutoTraitDeclaration](node) {
|
|
3212
|
+
mockBodyNoBody(node);
|
|
3213
|
+
},
|
|
3214
|
+
[jinxRust.NodeType.NegativeImplDeclaration](node) {
|
|
3215
|
+
mockBodyNoBody(node);
|
|
3216
|
+
},
|
|
3217
|
+
[jinxRust.NodeType.StructLiteral](node) {
|
|
3218
|
+
moveSpreadsToEnd(node);
|
|
3219
|
+
},
|
|
3220
|
+
[jinxRust.NodeType.StructPattern](node) {
|
|
3221
|
+
moveSpreadsToEnd(node);
|
|
3222
|
+
}
|
|
3223
|
+
};
|
|
3224
|
+
function moveSpreadsToEnd(node) {
|
|
3225
|
+
const props = node.properties;
|
|
3226
|
+
if (props.some((p, i, a) => is_StructSpread(p) && !iLast(i, a))) {
|
|
3227
|
+
const spreads = [];
|
|
3228
|
+
for (let i = 0; i < props.length; i++) {
|
|
3229
|
+
const prop = props[i];
|
|
3230
|
+
if (is_StructSpread(prop)) {
|
|
3231
|
+
Array_splice(props, prop, i--);
|
|
3232
|
+
spreads.push(prop);
|
|
3233
|
+
}
|
|
3234
|
+
}
|
|
3235
|
+
props.push(...spreads);
|
|
3236
|
+
}
|
|
3237
|
+
}
|
|
3238
|
+
function mockBodyNoBody(node) {
|
|
3239
|
+
node.body = jinxRust.rs.createLocArray(last_of(jinxRust.rs.toTokens(node).ast).loc.clone(), jinxRust.DelimKind["{}"]);
|
|
3240
|
+
}
|
|
3241
|
+
function transformMacroDelim(name, node) {
|
|
3242
|
+
if (HARDCODED_MACRO_DELIMS.has(name)) {
|
|
3243
|
+
return HARDCODED_MACRO_DELIMS.get(name);
|
|
3244
|
+
}
|
|
3245
|
+
if (node.segments.dk === jinxRust.DelimKind["{}"] && utils.includesTK(node, jinxRust.TK[","])) {
|
|
3246
|
+
return jinxRust.DelimKind["()"];
|
|
3247
|
+
}
|
|
3248
|
+
if (node.segments.dk === jinxRust.DelimKind["()"] && utils.includesTK(node, jinxRust.TK[";"])) {
|
|
3249
|
+
return jinxRust.DelimKind["{}"];
|
|
3250
|
+
}
|
|
3251
|
+
return node.segments.dk;
|
|
3252
|
+
}
|
|
3253
|
+
var seen = /* @__PURE__ */ new WeakSet();
|
|
3254
|
+
function transformNode(node, parent, key, index) {
|
|
3255
|
+
if (!seen.has(node)) {
|
|
3256
|
+
seen.add(node);
|
|
3257
|
+
if (utils.is_Snippet(node) || utils.is_Program(node)) {
|
|
3258
|
+
registerPogramLike(node);
|
|
3259
|
+
}
|
|
3260
|
+
utils.each_childNode(node, transformNode);
|
|
3261
|
+
insert_blocks(node, parent, key, index);
|
|
3262
|
+
transform[node.nodeType]?.(node);
|
|
3263
|
+
flatten_typeBounds(node);
|
|
3264
|
+
transform_nodeAttributes(node);
|
|
3265
|
+
}
|
|
3266
|
+
return node;
|
|
3267
|
+
}
|
|
3268
|
+
function insert_blocks(node, parent, key, index) {
|
|
3269
|
+
if (parent && key) {
|
|
3270
|
+
if (!utils.is_ExpressionStatement(parent) && (// "1 + break" -> "1 + { break; }"
|
|
3271
|
+
utils.is_FlowControlExpression(node) || // "1 + a = b" -> "1 + { a = b; }"
|
|
3272
|
+
!isReadingSnippet() && utils.is_ReassignmentNode(node) && !(utils.is_ReassignmentNode(parent) && parent.left === node))) {
|
|
3273
|
+
utils.reassignNodeProperty(blockify(node), parent, key, index);
|
|
3274
|
+
} else if (utils.is_ClosureFunctionExpression(node) && (// "|| -> T x" -> "|| -> T { x }"
|
|
3275
|
+
!!node.returnType && !utils.is_BlockExpression(node.expression) || // "|| match x {}" -> "|| { match x {} }"
|
|
3276
|
+
utils.is_ExpressionWithBodyOrCases(node.expression) && !utils.is_BlockExpression(node.expression) && !utils.is_IfBlockExpression(node.expression))) {
|
|
3277
|
+
node.expression = blockify(node.expression);
|
|
3278
|
+
}
|
|
3279
|
+
}
|
|
3280
|
+
function blockify(node2) {
|
|
3281
|
+
const block = jinxRust.rs.mockNode(jinxRust.NodeType.BlockExpression, node2.loc.clone(), {
|
|
3282
|
+
label: void 0,
|
|
3283
|
+
body: jinxRust.rs.createLocArray(jinxRust.DelimKind["{}"], node2.loc.clone(), [
|
|
3284
|
+
jinxRust.rs.mockNode(jinxRust.NodeType.ExpressionStatement, node2.loc.clone(), { semi: false, expression: node2 })
|
|
3285
|
+
])
|
|
3286
|
+
});
|
|
3287
|
+
utils.transferAttributes(node2, block);
|
|
3288
|
+
return block;
|
|
3289
|
+
}
|
|
3290
|
+
}
|
|
3291
|
+
function flatten_typeBounds(topNode) {
|
|
3292
|
+
if (utils.hasTypeBounds(topNode)) {
|
|
3293
|
+
const nestedBounds = topNode.typeBounds.filter(isBoundWithNestedBounds);
|
|
3294
|
+
const [first, ...subsequent] = nestedBounds;
|
|
3295
|
+
const flatten = (bound) => Array_replace(topNode.typeBounds, bound, ...bound.typeExpression.typeBounds);
|
|
3296
|
+
if (nestedBounds.every(isBareBoundWithNestedBoundsNoPrefix)) {
|
|
3297
|
+
each(nestedBounds, flatten);
|
|
3298
|
+
} else if (!hasDefinedPrefix(topNode) && first === topNode.typeBounds[0] && !isBareBoundWithNestedBoundsNoPrefix(first) && subsequent.every(isBareBoundWithNestedBoundsNoPrefix)) {
|
|
3299
|
+
if (utils.is_TypeDynBounds(topNode)) {
|
|
3300
|
+
if (utils.is_TypeImplBounds(first.typeExpression)) {
|
|
3301
|
+
utils.unsafe_set_nodeType(topNode, jinxRust.NodeType.TypeImplBounds);
|
|
3302
|
+
} else {
|
|
3303
|
+
topNode.dyn = true;
|
|
3304
|
+
}
|
|
3305
|
+
each(nestedBounds, flatten);
|
|
3306
|
+
} else {
|
|
3307
|
+
each(subsequent, flatten);
|
|
3308
|
+
first.typeExpression.typeBounds.push(...topNode.typeBounds.slice(1));
|
|
3309
|
+
topNode.typeBounds.length = 1;
|
|
3310
|
+
}
|
|
3311
|
+
}
|
|
3312
|
+
}
|
|
3313
|
+
function isBoundWithNestedBounds(bound) {
|
|
3314
|
+
return utils.is_TypeTraitBound(bound) && utils.is_TypeBoundsStandaloneNode(bound.typeExpression);
|
|
3315
|
+
}
|
|
3316
|
+
function isBareBoundWithNestedBounds(bound) {
|
|
3317
|
+
return isBoundWithNestedBounds(bound) && utils.is_BareTypeTraitBound(bound);
|
|
3318
|
+
}
|
|
3319
|
+
function isBareBoundWithNestedBoundsNoPrefix(bound) {
|
|
3320
|
+
return isBareBoundWithNestedBounds(bound) && !hasDefinedPrefix(bound.typeExpression);
|
|
3321
|
+
}
|
|
3322
|
+
function hasDefinedPrefix(node) {
|
|
3323
|
+
return utils.is_TypeDynBounds(node) && node.dyn || utils.is_TypeImplBounds(node);
|
|
3324
|
+
}
|
|
3325
|
+
}
|
|
3326
|
+
function transform_nodeAttributes(node) {
|
|
3327
|
+
if (utils.hasAttributes(node)) {
|
|
3328
|
+
const attrs = node.attributes;
|
|
3329
|
+
for (let i = 0; i < attrs.length; i++) {
|
|
3330
|
+
const attr = attrs[i];
|
|
3331
|
+
if (isReadingSnippet() && utils.is_DocCommentAttribute(attr)) {
|
|
3332
|
+
const index = binarySearchIn(_COMMENTS, utils.start(attr), utils.start);
|
|
3333
|
+
_COMMENTS.splice(index, 1);
|
|
3334
|
+
}
|
|
3335
|
+
if (attr.inner) {
|
|
3336
|
+
if (isPrettierIgnoreAttribute(attr)) {
|
|
3337
|
+
setPrettierIgnoreTarget(utils.is_Program(node) ? node.loc.src : node, attr);
|
|
3338
|
+
}
|
|
3339
|
+
utils.insertNode(utils.is_Snippet(node) ? node.ast : utils.getBodyOrCases(node), attr);
|
|
3340
|
+
Array_splice(attrs, attr, i--);
|
|
3341
|
+
}
|
|
3342
|
+
}
|
|
3343
|
+
if (attrs.length === 0) {
|
|
3344
|
+
utils.deleteAttributes(node);
|
|
3345
|
+
}
|
|
3346
|
+
}
|
|
3347
|
+
}
|
|
3348
|
+
function registerPogramLike(program) {
|
|
3349
|
+
const comments = spliceAll(program.comments);
|
|
3350
|
+
const danglingAttributes = spliceAll(program.danglingAttributes);
|
|
3351
|
+
for (let i = 0; i < danglingAttributes.length; i++) {
|
|
3352
|
+
const attr = danglingAttributes[i];
|
|
3353
|
+
if (utils.is_DocCommentAttribute(attr)) {
|
|
3354
|
+
if (isReadingSnippet()) {
|
|
3355
|
+
const index = binarySearchIn(_COMMENTS, utils.start(attr), utils.start);
|
|
3356
|
+
_COMMENTS.splice(index, 1);
|
|
3357
|
+
}
|
|
3358
|
+
} else {
|
|
3359
|
+
transformNode(danglingAttributes[i], program, "danglingAttributes", i);
|
|
3360
|
+
}
|
|
3361
|
+
}
|
|
3362
|
+
if (!isReadingSnippet())
|
|
3363
|
+
utils.insertNodes(_COMMENTS, comments);
|
|
3364
|
+
utils.insertNodes(_DANGLING_ATTRIBUTES, danglingAttributes);
|
|
3365
|
+
}
|
|
3366
|
+
var CommentChildNodes = /* @__PURE__ */ new WeakMap();
|
|
3367
|
+
function getVisitorKeys(node, nonTraversableKeys) {
|
|
3368
|
+
if (!utils.is_Node(node))
|
|
3369
|
+
return [];
|
|
3370
|
+
const keys = [];
|
|
3371
|
+
for (const key of Object.keys(node)) {
|
|
3372
|
+
if (nonTraversableKeys.has(key))
|
|
3373
|
+
continue;
|
|
3374
|
+
const value = node[key];
|
|
3375
|
+
if (utils.is_Node(value) || Array.isArray(value) && value.some(utils.is_Node))
|
|
3376
|
+
keys.push(key);
|
|
3377
|
+
}
|
|
3378
|
+
return keys;
|
|
3379
|
+
}
|
|
3380
|
+
function getCommentChildNodes(n) {
|
|
3381
|
+
if (!utils.is_Node(n))
|
|
3382
|
+
return [];
|
|
3383
|
+
const children = Map_get(CommentChildNodes, n, getTransformedNodeChildren);
|
|
3384
|
+
if (utils.is_NodeWithBodyOrCases(n) || is_BlockLikeMacroInvocation(n)) {
|
|
3385
|
+
for (let i = 0; i < children.length; i++) {
|
|
3386
|
+
const attr = children[i];
|
|
3387
|
+
if (utils.is_AttributeOrDocComment(attr)) {
|
|
3388
|
+
const target = children.find((n2) => utils.start(n2) <= utils.start(attr) && utils.ownStart(n2) >= utils.end(attr));
|
|
3389
|
+
if (target) {
|
|
3390
|
+
children.splice(i--, 1);
|
|
3391
|
+
utils.insertNode(Map_get(CommentChildNodes, target, getTransformedNodeChildren), attr);
|
|
3392
|
+
}
|
|
3393
|
+
}
|
|
3394
|
+
}
|
|
3395
|
+
}
|
|
3396
|
+
return children;
|
|
3397
|
+
function getTransformedNodeChildren(node) {
|
|
3398
|
+
if (utils.is_Program(node))
|
|
3399
|
+
node.comments ?? (node.comments = []);
|
|
3400
|
+
const children2 = utils.getNodeChildren(node);
|
|
3401
|
+
if (utils.is_NodeWithBodyNoBody(node)) {
|
|
3402
|
+
utils.insertNodes(children2, node.body);
|
|
3403
|
+
}
|
|
3404
|
+
return children2;
|
|
3405
|
+
}
|
|
3406
|
+
}
|
|
3407
|
+
var printer = {
|
|
3408
|
+
[jinxRust.NodeType.MissingNode](print4, node) {
|
|
3409
|
+
return "";
|
|
3410
|
+
},
|
|
3411
|
+
[jinxRust.NodeType.SourceFile](print4, node) {
|
|
3412
|
+
return [
|
|
3413
|
+
print4.b("UTF8BOM", "\uFEFF"),
|
|
3414
|
+
//
|
|
3415
|
+
print4("shebang"),
|
|
3416
|
+
print4("program")
|
|
3417
|
+
];
|
|
3418
|
+
},
|
|
3419
|
+
[jinxRust.NodeType.Shebang](print4, node) {
|
|
3420
|
+
return [`#!${node.value}`, hardline];
|
|
3421
|
+
},
|
|
3422
|
+
[jinxRust.NodeType.Program](print4, node) {
|
|
3423
|
+
return printBodyOrCases(print4, node);
|
|
3424
|
+
},
|
|
3425
|
+
[jinxRust.NodeType.Snippet](print4, node) {
|
|
3426
|
+
exit.never();
|
|
3427
|
+
},
|
|
3428
|
+
[jinxRust.NodeType.Identifier](print4, node) {
|
|
3429
|
+
return node.name;
|
|
3430
|
+
},
|
|
3431
|
+
[jinxRust.NodeType.Index](print4, node) {
|
|
3432
|
+
return node.name;
|
|
3433
|
+
},
|
|
3434
|
+
[jinxRust.NodeType.LbIdentifier](print4, node) {
|
|
3435
|
+
return node.name;
|
|
3436
|
+
},
|
|
3437
|
+
[jinxRust.NodeType.McIdentifier](print4, node) {
|
|
3438
|
+
return node.name;
|
|
3439
|
+
},
|
|
3440
|
+
[jinxRust.NodeType.LtIdentifier](print4, node) {
|
|
3441
|
+
return node.name;
|
|
3442
|
+
},
|
|
3443
|
+
[jinxRust.NodeType.PunctuationToken](print4, node) {
|
|
3444
|
+
return node.token;
|
|
3445
|
+
},
|
|
3446
|
+
[jinxRust.NodeType.DelimGroup](print4, node) {
|
|
3447
|
+
return node.loc.getOwnText();
|
|
3448
|
+
},
|
|
3449
|
+
[jinxRust.NodeType.Literal](print4, node) {
|
|
3450
|
+
let { value } = node;
|
|
3451
|
+
if (utils.is_LiteralNumberLike(node))
|
|
3452
|
+
value = printNumber(value);
|
|
3453
|
+
return utils.hasSuffix(node) ? [value, print4("suffix")] : value;
|
|
3454
|
+
},
|
|
3455
|
+
[jinxRust.NodeType.ItemPath](print4, node) {
|
|
3456
|
+
return [print4("namespace"), "::", print4("segment")];
|
|
3457
|
+
},
|
|
3458
|
+
[jinxRust.NodeType.ExpressionPath](print4, node) {
|
|
3459
|
+
return [print4("namespace"), "::", print4("segment")];
|
|
3460
|
+
},
|
|
3461
|
+
[jinxRust.NodeType.TypePath](print4, node) {
|
|
3462
|
+
return [print4("namespace"), "::", print4("segment")];
|
|
3463
|
+
},
|
|
3464
|
+
[jinxRust.NodeType.Comment](print4, node) {
|
|
3465
|
+
return print_comment(node);
|
|
3466
|
+
},
|
|
3467
|
+
[jinxRust.NodeType.DocCommentAttribute](print4, node) {
|
|
3468
|
+
return print_comment(node);
|
|
3469
|
+
},
|
|
3470
|
+
[jinxRust.NodeType.Attribute](print4, node) {
|
|
3471
|
+
return [
|
|
3472
|
+
node.inner ? "#![" : "#[",
|
|
3473
|
+
isTransformed(node) ? [print4("segments"), printDanglingCommentsForInline(node)] : node.segments.loc.sliceText(1, -1).trim(),
|
|
3474
|
+
"]"
|
|
3475
|
+
];
|
|
3476
|
+
},
|
|
3477
|
+
[jinxRust.NodeType.MacroInvocation](print4, node) {
|
|
3478
|
+
const hasCurlyBrackets = node.segments.dk === jinxRust.DelimKind["{}"];
|
|
3479
|
+
const delim = utils.getDelimChars(node.segments);
|
|
3480
|
+
if (node.segments.length === 0) {
|
|
3481
|
+
return [print4("callee"), "!", hasCurlyBrackets ? " " : "", delim.left, printDanglingCommentsForInline(node), delim.right];
|
|
3482
|
+
}
|
|
3483
|
+
if (isTransformed(node)) {
|
|
3484
|
+
if (is_CallLikeMacroInvocation(node)) {
|
|
3485
|
+
return [print4("callee"), "!", printCallArguments(print4, node)];
|
|
3486
|
+
}
|
|
3487
|
+
if (is_BlockLikeMacroInvocation(node)) {
|
|
3488
|
+
return [print4("callee"), "!", " ", printBlockBody(print4, node)];
|
|
3489
|
+
}
|
|
3490
|
+
}
|
|
3491
|
+
let content = node.segments.loc.sliceText(1, -1);
|
|
3492
|
+
if (content.trim().length === 0) {
|
|
3493
|
+
content = "";
|
|
3494
|
+
} else if (!content.includes("\n")) {
|
|
3495
|
+
content = content.trim();
|
|
3496
|
+
if (hasCurlyBrackets)
|
|
3497
|
+
content = " " + content + " ";
|
|
3498
|
+
}
|
|
3499
|
+
return [print4("callee"), "!", hasCurlyBrackets ? " " : "", delim.left, content, delim.right];
|
|
3500
|
+
},
|
|
3501
|
+
[jinxRust.NodeType.MacroRulesDeclaration](print4, node) {
|
|
3502
|
+
return ["macro_rules! ", print4("id"), printMacroRules(print4, node)];
|
|
3503
|
+
},
|
|
3504
|
+
[jinxRust.NodeType.MacroRuleDeclaration](print4, node) {
|
|
3505
|
+
return [printRuleMatch(print4, node), " => ", printRuleTransform(print4, node), ";"];
|
|
3506
|
+
},
|
|
3507
|
+
[jinxRust.NodeType.MacroDeclaration](print4, node) {
|
|
3508
|
+
return [print4("pub"), "macro ", print4("id"), printMacroRules(print4, node)];
|
|
3509
|
+
},
|
|
3510
|
+
[jinxRust.NodeType.MacroInlineRuleDeclaration](print4, node) {
|
|
3511
|
+
return [printRuleMatch(print4, node), " ", printRuleTransform(print4, node)];
|
|
3512
|
+
},
|
|
3513
|
+
[jinxRust.NodeType.MacroGroup](print4, node) {
|
|
3514
|
+
return node.loc.getOwnText();
|
|
3515
|
+
},
|
|
3516
|
+
[jinxRust.NodeType.MacroParameterDeclaration](print4, node) {
|
|
3517
|
+
return [print4("id"), ":", print4("ty")];
|
|
3518
|
+
},
|
|
3519
|
+
[jinxRust.NodeType.PubSpecifier](print4, node) {
|
|
3520
|
+
if (!node.location)
|
|
3521
|
+
return "pub ";
|
|
3522
|
+
if (utils.is_Identifier(node.location)) {
|
|
3523
|
+
switch (node.location.name) {
|
|
3524
|
+
case "crate":
|
|
3525
|
+
if (utils.start(node) === utils.start(node.location)) {
|
|
3526
|
+
return "crate ";
|
|
3527
|
+
} else {
|
|
3528
|
+
return ["pub(", print4("location"), ") "];
|
|
3529
|
+
}
|
|
3530
|
+
case "self":
|
|
3531
|
+
case "super":
|
|
3532
|
+
return ["pub(", print4("location"), ") "];
|
|
3533
|
+
}
|
|
3534
|
+
}
|
|
3535
|
+
return ["pub(in ", print4("location"), ") "];
|
|
3536
|
+
},
|
|
3537
|
+
[jinxRust.NodeType.ExternSpecifier](print4, node) {
|
|
3538
|
+
return ["extern ", f`${print4("abi")} `];
|
|
3539
|
+
},
|
|
3540
|
+
[jinxRust.NodeType.ExpressionStatement](print4, node) {
|
|
3541
|
+
return [print4("expression"), stmtNeedsSemi(node) ? ";" : ""];
|
|
3542
|
+
},
|
|
3543
|
+
[jinxRust.NodeType.UseStatement](print4, node) {
|
|
3544
|
+
return [print4("pub"), "use ", print4("import"), ";"];
|
|
3545
|
+
},
|
|
3546
|
+
[jinxRust.NodeType.DestructuredImport](print4, node) {
|
|
3547
|
+
if (node.specifiers.length === 0)
|
|
3548
|
+
return [print4("source"), "::{", printDanglingCommentsForInline(node, "specifiers" /* specifiers */), "}"];
|
|
3549
|
+
return [
|
|
3550
|
+
print4("source"),
|
|
3551
|
+
group([
|
|
3552
|
+
"::{",
|
|
3553
|
+
indent([line , join([",", line], print4("specifiers")), ifBreak(",")]),
|
|
3554
|
+
line ,
|
|
3555
|
+
"}"
|
|
3556
|
+
])
|
|
3557
|
+
];
|
|
3558
|
+
},
|
|
3559
|
+
[jinxRust.NodeType.AmbientImport](print4, node) {
|
|
3560
|
+
return f`${print4("source")}::*` || "*";
|
|
3561
|
+
},
|
|
3562
|
+
[jinxRust.NodeType.AnonymousImport](print4, node) {
|
|
3563
|
+
return [print4("source"), " as ", "_"];
|
|
3564
|
+
},
|
|
3565
|
+
[jinxRust.NodeType.NamedImport](print4, node) {
|
|
3566
|
+
return [print4("source"), f` as ${print4("local")}`];
|
|
3567
|
+
},
|
|
3568
|
+
[jinxRust.NodeType.ExternCrateStatement](print4, node) {
|
|
3569
|
+
return [print4("pub"), "extern crate ", print4("import"), ";"];
|
|
3570
|
+
},
|
|
3571
|
+
[jinxRust.NodeType.TypeAliasDeclaration](print4, node) {
|
|
3572
|
+
return [
|
|
3573
|
+
print4("pub"),
|
|
3574
|
+
"type",
|
|
3575
|
+
printAssignment(
|
|
3576
|
+
printGenerics_x_whereBounds(print4, node, printDeclarationTypeBounds(print4, node, ":")),
|
|
3577
|
+
//
|
|
3578
|
+
" =",
|
|
3579
|
+
"typeExpression"
|
|
3580
|
+
),
|
|
3581
|
+
";"
|
|
3582
|
+
];
|
|
3583
|
+
},
|
|
3584
|
+
[jinxRust.NodeType.LetVariableDeclaration](print4, node) {
|
|
3585
|
+
return [
|
|
3586
|
+
"let ",
|
|
3587
|
+
printAssignment(
|
|
3588
|
+
printAnnotatedPattern(print4, node),
|
|
3589
|
+
//
|
|
3590
|
+
" =",
|
|
3591
|
+
"expression"
|
|
3592
|
+
),
|
|
3593
|
+
f` else ${print4("else")}`,
|
|
3594
|
+
";"
|
|
3595
|
+
];
|
|
3596
|
+
},
|
|
3597
|
+
[jinxRust.NodeType.ConstVariableDeclaration](print4, node) {
|
|
3598
|
+
return [
|
|
3599
|
+
print4("pub"),
|
|
3600
|
+
"const ",
|
|
3601
|
+
printAssignment(
|
|
3602
|
+
printAnnotatedPattern(print4, node),
|
|
3603
|
+
//
|
|
3604
|
+
" =",
|
|
3605
|
+
"expression"
|
|
3606
|
+
),
|
|
3607
|
+
";"
|
|
3608
|
+
];
|
|
3609
|
+
},
|
|
3610
|
+
[jinxRust.NodeType.StaticVariableDeclaration](print4, node) {
|
|
3611
|
+
return [
|
|
3612
|
+
print4("pub"),
|
|
3613
|
+
"static ",
|
|
3614
|
+
printAssignment(
|
|
3615
|
+
printAnnotatedPattern(print4, node),
|
|
3616
|
+
//
|
|
3617
|
+
" =",
|
|
3618
|
+
"expression"
|
|
3619
|
+
),
|
|
3620
|
+
";"
|
|
3621
|
+
];
|
|
3622
|
+
},
|
|
3623
|
+
[jinxRust.NodeType.ModuleDeclaration](print4, node) {
|
|
3624
|
+
return [
|
|
3625
|
+
print4("pub"),
|
|
3626
|
+
//
|
|
3627
|
+
print4.b("unsafe"),
|
|
3628
|
+
"mod ",
|
|
3629
|
+
print4("id"),
|
|
3630
|
+
printMaybeBlockBody(print4, node)
|
|
3631
|
+
];
|
|
3632
|
+
},
|
|
3633
|
+
[jinxRust.NodeType.ExternBlockDeclaration](print4, node) {
|
|
3634
|
+
return [
|
|
3635
|
+
print4("pub"),
|
|
3636
|
+
//
|
|
3637
|
+
print4.b("unsafe"),
|
|
3638
|
+
"extern ",
|
|
3639
|
+
f`${print4("abi")} `,
|
|
3640
|
+
printBlockBody(print4, node)
|
|
3641
|
+
];
|
|
3642
|
+
},
|
|
3643
|
+
[jinxRust.NodeType.FunctionDeclaration](print4, node) {
|
|
3644
|
+
return [
|
|
3645
|
+
print4("pub"),
|
|
3646
|
+
print4.b("const"),
|
|
3647
|
+
print4.b("async"),
|
|
3648
|
+
print4.b("unsafe"),
|
|
3649
|
+
print4("extern"),
|
|
3650
|
+
"fn",
|
|
3651
|
+
printGenerics_x_whereBounds(print4, node, printParametersAndReturnType(node)),
|
|
3652
|
+
printMaybeBlockBody(print4, node)
|
|
3653
|
+
];
|
|
3654
|
+
},
|
|
3655
|
+
[jinxRust.NodeType.FunctionSelfParameterDeclaration](print4, node) {
|
|
3656
|
+
return group([print4.b("ref", "&"), f`${print4("lt")} `, print4.b("mut"), "self", printTypeAnnotation(print4, node)]);
|
|
3657
|
+
},
|
|
3658
|
+
[jinxRust.NodeType.FunctionParameterDeclaration](print4, node) {
|
|
3659
|
+
return group(printAnnotatedPattern(print4, node));
|
|
3660
|
+
},
|
|
3661
|
+
[jinxRust.NodeType.FunctionSpread](print4, node) {
|
|
3662
|
+
return "...";
|
|
3663
|
+
},
|
|
3664
|
+
[jinxRust.NodeType.StructDeclaration](print4, node) {
|
|
3665
|
+
return [print4("pub"), "struct", printGenerics_x_whereBounds(print4, node, ""), printObject(print4, node)];
|
|
3666
|
+
},
|
|
3667
|
+
[jinxRust.NodeType.StructPropertyDeclaration](print4, node) {
|
|
3668
|
+
return [print4("pub"), print4("id"), printTypeAnnotation(print4, node)];
|
|
3669
|
+
},
|
|
3670
|
+
[jinxRust.NodeType.TupleStructDeclaration](print4, node) {
|
|
3671
|
+
return [print4("pub"), "struct", printGenerics_x_whereBounds(print4, node, printArrayLike(print4, node)), ";"];
|
|
3672
|
+
},
|
|
3673
|
+
[jinxRust.NodeType.TupleStructItemDeclaration](print4, node) {
|
|
3674
|
+
return [print4("pub"), print4("typeAnnotation")];
|
|
3675
|
+
},
|
|
3676
|
+
[jinxRust.NodeType.UnionDeclaration](print4, node) {
|
|
3677
|
+
return [print4("pub"), "union", printGenerics_x_whereBounds(print4, node, ""), printObject(print4, node)];
|
|
3678
|
+
},
|
|
3679
|
+
[jinxRust.NodeType.EnumDeclaration](print4, node) {
|
|
3680
|
+
return [print4("pub"), "enum", printGenerics_x_whereBounds(print4, node, ""), printEnumBody(print4, node)];
|
|
3681
|
+
},
|
|
3682
|
+
[jinxRust.NodeType.EnumMemberDeclaration](print4, node) {
|
|
3683
|
+
return [
|
|
3684
|
+
print4("pub"),
|
|
3685
|
+
printAssignment(
|
|
3686
|
+
print4("id"),
|
|
3687
|
+
//
|
|
3688
|
+
" =",
|
|
3689
|
+
"value"
|
|
3690
|
+
)
|
|
3691
|
+
];
|
|
3692
|
+
},
|
|
3693
|
+
[jinxRust.NodeType.EnumMemberTupleDeclaration](print4, node) {
|
|
3694
|
+
return [
|
|
3695
|
+
print4("pub"),
|
|
3696
|
+
printAssignment(
|
|
3697
|
+
[print4("id"), printArrayLike(print4, node)],
|
|
3698
|
+
//
|
|
3699
|
+
" =",
|
|
3700
|
+
"value"
|
|
3701
|
+
)
|
|
3702
|
+
];
|
|
3703
|
+
},
|
|
3704
|
+
[jinxRust.NodeType.EnumMemberStructDeclaration](print4, node) {
|
|
3705
|
+
return [
|
|
3706
|
+
print4("pub"),
|
|
3707
|
+
printAssignment(
|
|
3708
|
+
[print4("id"), printObject(print4, node)],
|
|
3709
|
+
//
|
|
3710
|
+
" =",
|
|
3711
|
+
"value"
|
|
3712
|
+
)
|
|
3713
|
+
];
|
|
3714
|
+
},
|
|
3715
|
+
[jinxRust.NodeType.TraitDeclaration](print4, node) {
|
|
3716
|
+
return [
|
|
3717
|
+
print4("pub"),
|
|
3718
|
+
print4.b("unsafe"),
|
|
3719
|
+
"trait",
|
|
3720
|
+
printGenerics_x_whereBounds(print4, node, printDeclarationTypeBounds(print4, node, ":")),
|
|
3721
|
+
adjustClause(node, printBlockBody(print4, node))
|
|
3722
|
+
];
|
|
3723
|
+
},
|
|
3724
|
+
[jinxRust.NodeType.AutoTraitDeclaration](print4, node) {
|
|
3725
|
+
return [
|
|
3726
|
+
print4("pub"),
|
|
3727
|
+
print4.b("unsafe"),
|
|
3728
|
+
"auto trait ",
|
|
3729
|
+
print4("id"),
|
|
3730
|
+
" ",
|
|
3731
|
+
printBlockBody(print4, node)
|
|
3732
|
+
// see "transform.ts"
|
|
3733
|
+
];
|
|
3734
|
+
},
|
|
3735
|
+
[jinxRust.NodeType.TraitAliasDeclaration](print4, node) {
|
|
3736
|
+
return [
|
|
3737
|
+
print4("pub"),
|
|
3738
|
+
print4.b("unsafe"),
|
|
3739
|
+
"trait",
|
|
3740
|
+
printGenerics_x_whereBounds(print4, node, printDeclarationTypeBounds(print4, node, " =")),
|
|
3741
|
+
";"
|
|
3742
|
+
];
|
|
3743
|
+
},
|
|
3744
|
+
[jinxRust.NodeType.ImplDeclaration](print4, node) {
|
|
3745
|
+
return [
|
|
3746
|
+
print4("pub"),
|
|
3747
|
+
print4.b("unsafe"),
|
|
3748
|
+
"impl",
|
|
3749
|
+
printGenerics_x_whereBounds(print4, node, [print4.b("const"), printImplTraitForType(print4, node)]),
|
|
3750
|
+
adjustClause(node, printBlockBody(print4, node))
|
|
3751
|
+
];
|
|
3752
|
+
},
|
|
3753
|
+
[jinxRust.NodeType.NegativeImplDeclaration](print4, node) {
|
|
3754
|
+
return [
|
|
3755
|
+
print4("pub"),
|
|
3756
|
+
"impl",
|
|
3757
|
+
printGenerics_x_whereBounds(print4, node, ["!", printImplTraitForType(print4, node)]),
|
|
3758
|
+
" ",
|
|
3759
|
+
printBlockBody(print4, node)
|
|
3760
|
+
// see "transform.ts"
|
|
3761
|
+
];
|
|
3762
|
+
},
|
|
3763
|
+
[jinxRust.NodeType.ExpressionTypeSelector](print4, node) {
|
|
3764
|
+
return group(["<", print4("typeTarget"), f` as ${print4("typeExpression")}`, ">"]);
|
|
3765
|
+
},
|
|
3766
|
+
[jinxRust.NodeType.ExpressionTypeCast](print4, node) {
|
|
3767
|
+
return [print4("typeCallee"), f`::${printTypeArguments(print4, node)}`];
|
|
3768
|
+
},
|
|
3769
|
+
[jinxRust.NodeType.ExpressionAsTypeCast](print4, node) {
|
|
3770
|
+
return [print4("expression"), " as ", print4("typeExpression")];
|
|
3771
|
+
},
|
|
3772
|
+
[jinxRust.NodeType.ReturnExpression](print4, node) {
|
|
3773
|
+
return ["return", printFlowControlExpression(print4, node)];
|
|
3774
|
+
},
|
|
3775
|
+
[jinxRust.NodeType.BreakExpression](print4, node) {
|
|
3776
|
+
return ["break", f` ${print4("label")}`, printFlowControlExpression(print4, node)];
|
|
3777
|
+
},
|
|
3778
|
+
[jinxRust.NodeType.ContinueExpression](print4, node) {
|
|
3779
|
+
return ["continue", f` ${print4("label")}`];
|
|
3780
|
+
},
|
|
3781
|
+
[jinxRust.NodeType.YieldExpression](print4, node) {
|
|
3782
|
+
return ["yield", printFlowControlExpression(print4, node)];
|
|
3783
|
+
},
|
|
3784
|
+
[jinxRust.NodeType.RangeLiteral](print4, node) {
|
|
3785
|
+
return [print4("lower"), "..", print4.b("last", "="), print4("upper")];
|
|
3786
|
+
},
|
|
3787
|
+
[jinxRust.NodeType.CallExpression](print4, node) {
|
|
3788
|
+
return printCallExpression(print4, node);
|
|
3789
|
+
},
|
|
3790
|
+
[jinxRust.NodeType.MemberExpression](print4, node) {
|
|
3791
|
+
return printMemberExpression(print4, node);
|
|
3792
|
+
},
|
|
3793
|
+
[jinxRust.NodeType.AwaitExpression](print4, node) {
|
|
3794
|
+
return [print4("expression"), ".await"];
|
|
3795
|
+
},
|
|
3796
|
+
[jinxRust.NodeType.UnwrapExpression](print4, node) {
|
|
3797
|
+
return [print4("expression"), "?"];
|
|
3798
|
+
},
|
|
3799
|
+
[jinxRust.NodeType.ParenthesizedExpression](print4, node) {
|
|
3800
|
+
exit.never();
|
|
3801
|
+
const shouldHug = !hasComment(node.expression) && (utils.is_ArrayOrTupleLiteral(node.expression) || utils.is_StructLiteral(node.expression));
|
|
3802
|
+
if (shouldHug)
|
|
3803
|
+
return ["(", print4("expression"), ")"];
|
|
3804
|
+
return group(["(", indent([softline, print4("expression")]), softline, ")"]);
|
|
3805
|
+
},
|
|
3806
|
+
[jinxRust.NodeType.MinusExpression](print4, node) {
|
|
3807
|
+
return printUnaryExpression("-");
|
|
3808
|
+
},
|
|
3809
|
+
[jinxRust.NodeType.NotExpression](print4, node) {
|
|
3810
|
+
return printUnaryExpression("!");
|
|
3811
|
+
},
|
|
3812
|
+
[jinxRust.NodeType.OrExpression](print4, node) {
|
|
3813
|
+
return printBinaryishExpression(print4, node);
|
|
3814
|
+
},
|
|
3815
|
+
[jinxRust.NodeType.AndExpression](print4, node) {
|
|
3816
|
+
return printBinaryishExpression(print4, node);
|
|
3817
|
+
},
|
|
3818
|
+
[jinxRust.NodeType.ReassignmentExpression](print4, node) {
|
|
3819
|
+
return printAssignment(print4("left"), " =", "right");
|
|
3820
|
+
},
|
|
3821
|
+
[jinxRust.NodeType.UnassignedExpression](print4, node) {
|
|
3822
|
+
return "_";
|
|
3823
|
+
},
|
|
3824
|
+
[jinxRust.NodeType.OperationExpression](print4, node) {
|
|
3825
|
+
return printBinaryishExpression(print4, node);
|
|
3826
|
+
},
|
|
3827
|
+
[jinxRust.NodeType.ReassignmentOperationExpression](print4, node) {
|
|
3828
|
+
return printAssignment(print4("left"), " " + node.kind, "right");
|
|
3829
|
+
},
|
|
3830
|
+
[jinxRust.NodeType.ComparisonExpression](print4, node) {
|
|
3831
|
+
return printBinaryishExpression(print4, node);
|
|
3832
|
+
},
|
|
3833
|
+
[jinxRust.NodeType.LetScrutinee](print4, node) {
|
|
3834
|
+
return ["let ", printAssignment(print4("pattern"), " =", "expression")];
|
|
3835
|
+
},
|
|
3836
|
+
[jinxRust.NodeType.ClosureFunctionExpression](print4, node) {
|
|
3837
|
+
return printArrowFunction(print4, node);
|
|
3838
|
+
},
|
|
3839
|
+
[jinxRust.NodeType.ClosureFunctionParameterDeclaration](print4, node) {
|
|
3840
|
+
return group(printAnnotatedPattern(print4, node));
|
|
3841
|
+
},
|
|
3842
|
+
[jinxRust.NodeType.BlockExpression](print4, node) {
|
|
3843
|
+
return [
|
|
3844
|
+
f`${print4("label")}: `,
|
|
3845
|
+
print4.b("const"),
|
|
3846
|
+
print4.b("async"),
|
|
3847
|
+
print4.b("move"),
|
|
3848
|
+
print4.b("unsafe"),
|
|
3849
|
+
printBlockBody(print4, node)
|
|
3850
|
+
];
|
|
3851
|
+
},
|
|
3852
|
+
[jinxRust.NodeType.LoopBlockExpression](print4, node) {
|
|
3853
|
+
return [f`${print4("label")}: `, "loop ", printBlockBody(print4, node)];
|
|
3854
|
+
},
|
|
3855
|
+
[jinxRust.NodeType.WhileBlockExpression](print4, node) {
|
|
3856
|
+
return [f`${print4("label")}: `, "while ", printCondition(print4, node), printBlockBody(print4, node)];
|
|
3857
|
+
},
|
|
3858
|
+
[jinxRust.NodeType.ForInBlockExpression](print4, node) {
|
|
3859
|
+
return [f`${print4("label")}: `, "for ", print4("pattern"), " in ", print4("expression"), " ", printBlockBody(print4, node)];
|
|
3860
|
+
},
|
|
3861
|
+
[jinxRust.NodeType.IfBlockExpression](print4, node) {
|
|
3862
|
+
return [f`${print4("label")}: `, printIfBlock(print4, node)];
|
|
3863
|
+
},
|
|
3864
|
+
[jinxRust.NodeType.TryBlockExpression](print4, node) {
|
|
3865
|
+
return [f`${print4("label")}: `, "try ", printBlockBody(print4, node)];
|
|
3866
|
+
},
|
|
3867
|
+
[jinxRust.NodeType.MatchExpression](print4, node) {
|
|
3868
|
+
const id = Symbol("match");
|
|
3869
|
+
const expr = print4("expression");
|
|
3870
|
+
const needs_parens = pathCall(node, "expression", needsParens);
|
|
3871
|
+
let printed = [
|
|
3872
|
+
f`${print4("label")}: `,
|
|
3873
|
+
"match ",
|
|
3874
|
+
needs_parens ? expr : group([indent([softline, expr]), softline], { id }),
|
|
3875
|
+
needs_parens ? " " : !willBreak(expr) ? ifBreak("", " ", { groupId: id }) : "",
|
|
3876
|
+
printBlockBody(print4, node)
|
|
3877
|
+
];
|
|
3878
|
+
const parent = getParentNode();
|
|
3879
|
+
if (utils.is_ClosureFunctionExpression(parent) && parent.expression === node) {
|
|
3880
|
+
printed = parenthesize_if_break([indent([softline, printed]), softline]);
|
|
3881
|
+
}
|
|
3882
|
+
return printed;
|
|
3883
|
+
},
|
|
3884
|
+
[jinxRust.NodeType.MatchExpressionCase](print4, node) {
|
|
3885
|
+
return group([
|
|
3886
|
+
group(print4("pattern")),
|
|
3887
|
+
" ",
|
|
3888
|
+
printIfBlockCondition(print4, node),
|
|
3889
|
+
"=>",
|
|
3890
|
+
//
|
|
3891
|
+
(utils.is_BlockExpression(node.expression) || utils.is_IfBlockExpression(node.expression)) && !hasComment(node.expression, 0, (comment) => getOptions().danglingAttributes.includes(comment)) ? [" ", print4("expression")] : group(indent([line, print4("expression")]))
|
|
3892
|
+
]);
|
|
3893
|
+
},
|
|
3894
|
+
[jinxRust.NodeType.StructLiteral](print4, node) {
|
|
3895
|
+
return [print4("struct"), printObject(print4, node)];
|
|
3896
|
+
},
|
|
3897
|
+
[jinxRust.NodeType.StructLiteralPropertyShorthand](print4, node) {
|
|
3898
|
+
return print4("value");
|
|
3899
|
+
},
|
|
3900
|
+
[jinxRust.NodeType.StructLiteralProperty](print4, node) {
|
|
3901
|
+
return [print4("key"), ": ", print4("value")];
|
|
3902
|
+
},
|
|
3903
|
+
[jinxRust.NodeType.StructLiteralPropertySpread](print4, node) {
|
|
3904
|
+
return ["..", print4("expression")];
|
|
3905
|
+
},
|
|
3906
|
+
[jinxRust.NodeType.StructLiteralRestUnassigned](print4, node) {
|
|
3907
|
+
return "..";
|
|
3908
|
+
},
|
|
3909
|
+
[jinxRust.NodeType.ArrayLiteral](print4, node) {
|
|
3910
|
+
return printArrayLike(print4, node);
|
|
3911
|
+
},
|
|
3912
|
+
[jinxRust.NodeType.SizedArrayLiteral](print4, node) {
|
|
3913
|
+
return sg_duo`[${print4("initExpression")};${print4("sizeExpression")}]`;
|
|
3914
|
+
},
|
|
3915
|
+
[jinxRust.NodeType.TupleLiteral](print4, node) {
|
|
3916
|
+
return printArrayLike(print4, node);
|
|
3917
|
+
},
|
|
3918
|
+
[jinxRust.NodeType.ReferenceExpression](print4, node) {
|
|
3919
|
+
return printUnaryExpression(["&", print4.b("mut")]);
|
|
3920
|
+
},
|
|
3921
|
+
[jinxRust.NodeType.RawReferenceExpression](print4, node) {
|
|
3922
|
+
return printUnaryExpression(`&raw ${node.kind} `);
|
|
3923
|
+
},
|
|
3924
|
+
[jinxRust.NodeType.DereferenceExpression](print4, node) {
|
|
3925
|
+
return printUnaryExpression("*");
|
|
3926
|
+
},
|
|
3927
|
+
[jinxRust.NodeType.BoxExpression](print4, node) {
|
|
3928
|
+
return printUnaryExpression("box ");
|
|
3929
|
+
},
|
|
3930
|
+
[jinxRust.NodeType.UnionPattern](print4, node) {
|
|
3931
|
+
return printUnionPattern(print4, node);
|
|
3932
|
+
},
|
|
3933
|
+
[jinxRust.NodeType.ParenthesizedPattern](print4, node) {
|
|
3934
|
+
exit.never();
|
|
3935
|
+
return sg_single`(${print4("pattern")})`;
|
|
3936
|
+
},
|
|
3937
|
+
[jinxRust.NodeType.RestPattern](print4, node) {
|
|
3938
|
+
return "..";
|
|
3939
|
+
},
|
|
3940
|
+
[jinxRust.NodeType.WildcardPattern](print4, node) {
|
|
3941
|
+
return "_";
|
|
3942
|
+
},
|
|
3943
|
+
[jinxRust.NodeType.PatternVariableDeclaration](print4, node) {
|
|
3944
|
+
return [print4.b("ref"), print4.b("mut"), printAssignment(print4("id"), " @", "pattern")];
|
|
3945
|
+
},
|
|
3946
|
+
[jinxRust.NodeType.StructPattern](print4, node) {
|
|
3947
|
+
return [print4("struct"), printObject(print4, node)];
|
|
3948
|
+
},
|
|
3949
|
+
[jinxRust.NodeType.StructPatternPropertyDestructured](print4, node) {
|
|
3950
|
+
return [print4("key"), ": ", print4("pattern")];
|
|
3951
|
+
},
|
|
3952
|
+
[jinxRust.NodeType.StructPatternPropertyShorthand](print4, node) {
|
|
3953
|
+
return [print4.b("box"), print4.b("ref"), print4.b("mut"), print4("id")];
|
|
3954
|
+
},
|
|
3955
|
+
[jinxRust.NodeType.TuplePattern](print4, node) {
|
|
3956
|
+
return [print4("struct"), printArrayLike(print4, node)];
|
|
3957
|
+
},
|
|
3958
|
+
[jinxRust.NodeType.ArrayPattern](print4, node) {
|
|
3959
|
+
return printArrayLike(print4, node);
|
|
3960
|
+
},
|
|
3961
|
+
[jinxRust.NodeType.ReferencePattern](print4, node) {
|
|
3962
|
+
return ["&", print4.b("mut"), print4("pattern")];
|
|
3963
|
+
},
|
|
3964
|
+
[jinxRust.NodeType.BoxPattern](print4, node) {
|
|
3965
|
+
return ["box ", print4("pattern")];
|
|
3966
|
+
},
|
|
3967
|
+
[jinxRust.NodeType.MinusPattern](print4, node) {
|
|
3968
|
+
return ["-", print4("pattern")];
|
|
3969
|
+
},
|
|
3970
|
+
[jinxRust.NodeType.RangePattern](print4, node) {
|
|
3971
|
+
return [print4("lower"), "..", print4.b("last", "="), print4("upper")];
|
|
3972
|
+
},
|
|
3973
|
+
[jinxRust.NodeType.TypeCall](print4, node) {
|
|
3974
|
+
return [print4("typeCallee"), printTypeArguments(print4, node)];
|
|
3975
|
+
},
|
|
3976
|
+
[jinxRust.NodeType.TypeCallNamedArgument](print4, node) {
|
|
3977
|
+
return printAssignment(print4("target"), " =", "typeExpression");
|
|
3978
|
+
},
|
|
3979
|
+
[jinxRust.NodeType.TypeCallNamedBound](print4, node) {
|
|
3980
|
+
return [print4("typeTarget"), printTypeBounds(":", print4, node)];
|
|
3981
|
+
},
|
|
3982
|
+
[jinxRust.NodeType.LtElided](print4, node) {
|
|
3983
|
+
return "'_";
|
|
3984
|
+
},
|
|
3985
|
+
[jinxRust.NodeType.LtStatic](print4, node) {
|
|
3986
|
+
return "'static";
|
|
3987
|
+
},
|
|
3988
|
+
[jinxRust.NodeType.TypeNever](print4, node) {
|
|
3989
|
+
return "!";
|
|
3990
|
+
},
|
|
3991
|
+
[jinxRust.NodeType.TypeInferred](print4, node) {
|
|
3992
|
+
return "_";
|
|
3993
|
+
},
|
|
3994
|
+
[jinxRust.NodeType.GenericTypeParameterDeclaration](print4, node) {
|
|
3995
|
+
return printAssignment(
|
|
3996
|
+
[print4("id"), printTypeBounds(":", print4, node)],
|
|
3997
|
+
//
|
|
3998
|
+
" =",
|
|
3999
|
+
"typeDefault"
|
|
4000
|
+
);
|
|
4001
|
+
},
|
|
4002
|
+
[jinxRust.NodeType.ConstTypeParameterDeclaration](print4, node) {
|
|
4003
|
+
return [
|
|
4004
|
+
"const ",
|
|
4005
|
+
printAssignment(
|
|
4006
|
+
[print4("id"), printTypeAnnotation(print4, node)],
|
|
4007
|
+
//
|
|
4008
|
+
" =",
|
|
4009
|
+
"typeDefault"
|
|
4010
|
+
)
|
|
4011
|
+
];
|
|
4012
|
+
},
|
|
4013
|
+
[jinxRust.NodeType.GenericLtParameterDeclaration](print4, node) {
|
|
4014
|
+
return [print4("id"), printLtBounds(":", print4, node)];
|
|
4015
|
+
},
|
|
4016
|
+
[jinxRust.NodeType.WhereTypeBoundDeclaration](print4, node) {
|
|
4017
|
+
return [printLtParameters(print4, node), print4("typeTarget"), printTypeBounds(":", print4, node)];
|
|
4018
|
+
},
|
|
4019
|
+
[jinxRust.NodeType.WhereLtBoundDeclaration](print4, node) {
|
|
4020
|
+
return [print4("ltTarget"), printLtBounds(":", print4, node)];
|
|
4021
|
+
},
|
|
4022
|
+
[jinxRust.NodeType.TypeTraitBound](print4, node) {
|
|
4023
|
+
return [print4.b("maybeConst", "~const "), print4.b("optional", "?"), printLtParameters(print4, node), print4("typeExpression")];
|
|
4024
|
+
},
|
|
4025
|
+
[jinxRust.NodeType.TypeDynBounds](print4, node) {
|
|
4026
|
+
return printTypeBounds("dyn", print4, node);
|
|
4027
|
+
},
|
|
4028
|
+
[jinxRust.NodeType.TypeImplBounds](print4, node) {
|
|
4029
|
+
return printTypeBounds("impl", print4, node);
|
|
4030
|
+
},
|
|
4031
|
+
[jinxRust.NodeType.TypeFnPointer](print4, node) {
|
|
4032
|
+
return [printLtParameters(print4, node), print4.b("unsafe"), print4("extern"), "fn", printParametersAndReturnType(node)];
|
|
4033
|
+
},
|
|
4034
|
+
[jinxRust.NodeType.TypeFnPointerParameter](print4, node) {
|
|
4035
|
+
return [f`${print4("id")}: `, print4("typeAnnotation")];
|
|
4036
|
+
},
|
|
4037
|
+
[jinxRust.NodeType.TypeFunction](print4, node) {
|
|
4038
|
+
return [print4("callee"), printParametersAndReturnType(node)];
|
|
4039
|
+
},
|
|
4040
|
+
[jinxRust.NodeType.TypeTuple](print4, node) {
|
|
4041
|
+
return printArrayLike(print4, node);
|
|
4042
|
+
},
|
|
4043
|
+
[jinxRust.NodeType.TypeSizedArray](print4, node) {
|
|
4044
|
+
return sg_duo`[${print4("typeExpression")};${print4("sizeExpression")}]`;
|
|
4045
|
+
},
|
|
4046
|
+
[jinxRust.NodeType.TypeSlice](print4, node) {
|
|
4047
|
+
if (isSimpleType(node))
|
|
4048
|
+
return ["[", print4("typeExpression"), "]"];
|
|
4049
|
+
return sg_single`[${print4("typeExpression")}]`;
|
|
4050
|
+
},
|
|
4051
|
+
[jinxRust.NodeType.TypeReference](print4, node) {
|
|
4052
|
+
return ["&", f`${print4("lt")} `, print4.b("mut"), print4("typeExpression")];
|
|
4053
|
+
},
|
|
4054
|
+
[jinxRust.NodeType.TypeDereferenceConst](print4, node) {
|
|
4055
|
+
return ["*const ", print4("typeExpression")];
|
|
4056
|
+
},
|
|
4057
|
+
[jinxRust.NodeType.TypeDereferenceMut](print4, node) {
|
|
4058
|
+
return ["*mut ", print4("typeExpression")];
|
|
4059
|
+
},
|
|
4060
|
+
[jinxRust.NodeType.TypeParenthesized](print4, node) {
|
|
4061
|
+
exit.never();
|
|
4062
|
+
return sg_single`(${print4("typeExpression")})`;
|
|
4063
|
+
}
|
|
4064
|
+
};
|
|
4065
|
+
|
|
4066
|
+
// src/format/plugin.ts
|
|
4067
|
+
function is_printing_macro() {
|
|
4068
|
+
return getContext().path.stack.some((node) => utils.is_Node(node) && (utils.is_MacroInvocation(node) || utils.is_Attribute(node)));
|
|
4069
|
+
}
|
|
4070
|
+
function f(...args) {
|
|
4071
|
+
let cancel = false;
|
|
4072
|
+
const res = map_tagged_template(args, (doc) => {
|
|
4073
|
+
cancel || (cancel = !doc || is_array(doc) && doc.length === 0);
|
|
4074
|
+
return doc;
|
|
4075
|
+
});
|
|
4076
|
+
return cancel ? "" : res;
|
|
4077
|
+
}
|
|
4078
|
+
function sg_single(s, v_0) {
|
|
4079
|
+
return group([s[0], indent([softline, v_0]), softline, s[1]]);
|
|
4080
|
+
}
|
|
4081
|
+
function sg_duo(s, v_0, v_1) {
|
|
4082
|
+
return group([s[0], indent([softline, v_0, s[1], line, v_1]), softline, s[2]]);
|
|
4083
|
+
}
|
|
4084
|
+
var ctx;
|
|
4085
|
+
var getNode = () => ctx.path.stack[ctx.path.stack.length - 1];
|
|
4086
|
+
var stackIncludes = (x) => ctx.path.stack.includes(x);
|
|
4087
|
+
var getContext = () => ctx;
|
|
4088
|
+
var getOptions = () => ctx.options;
|
|
4089
|
+
var getAllComments = () => ctx.options[Symbol_comments];
|
|
4090
|
+
var getParentNode = (child) => {
|
|
4091
|
+
return ctx.path.getParentNode();
|
|
4092
|
+
};
|
|
4093
|
+
var getGrandParentNode = () => ctx.path.getParentNode(1);
|
|
4094
|
+
var getPrintFn = (forNode) => {
|
|
4095
|
+
return print3;
|
|
4096
|
+
};
|
|
4097
|
+
var get = (property) => getNode()[property];
|
|
4098
|
+
var has = (property) => !!get(property);
|
|
4099
|
+
function pathCall(node, key, fn) {
|
|
4100
|
+
return ctx.path.call(() => fn(getNode()), key);
|
|
4101
|
+
}
|
|
4102
|
+
function pathCallEach(node, key, fn) {
|
|
4103
|
+
ctx.path.each((_, i) => fn(getNode(), i), key);
|
|
4104
|
+
}
|
|
4105
|
+
function pathCallAtParent(parent, fn) {
|
|
4106
|
+
return ctx.path.callParent(() => {
|
|
4107
|
+
return fn(parent);
|
|
4108
|
+
});
|
|
4109
|
+
}
|
|
4110
|
+
function pathCallParentOf(child, fn) {
|
|
4111
|
+
return ctx.path.callParent((p) => fn(getNode()));
|
|
4112
|
+
}
|
|
4113
|
+
function print3(property, args) {
|
|
4114
|
+
if (!property)
|
|
4115
|
+
return ctx.print(void 0, args);
|
|
4116
|
+
if (Array.isArray(property))
|
|
4117
|
+
return ctx.print(property, args);
|
|
4118
|
+
const value = get(property);
|
|
4119
|
+
return !!value ? Array.isArray(value) ? ctx.path.map(ctx.print, property) : ctx.print(property, args) : "";
|
|
4120
|
+
}
|
|
4121
|
+
((print4) => {
|
|
4122
|
+
function b(property, res = `${property} `) {
|
|
4123
|
+
return has(property) ? res : "";
|
|
4124
|
+
}
|
|
4125
|
+
print4.b = b;
|
|
4126
|
+
function map(property, mapItem) {
|
|
4127
|
+
return !has(property) ? [] : ctx.path.map(mapItem ? (p, i, a) => mapItem(a[i], i, a) : () => ctx.print(), property);
|
|
4128
|
+
}
|
|
4129
|
+
print4.map = map;
|
|
4130
|
+
function join2(property, sep, trailingSep = "") {
|
|
4131
|
+
return map_join(property, () => ctx.print(), sep, trailingSep);
|
|
4132
|
+
}
|
|
4133
|
+
print4.join = join2;
|
|
4134
|
+
function map_join(property, mapFn, sep, sepTrailing = "") {
|
|
4135
|
+
const sepFn = typeof sep === "function" ? sep : () => sep;
|
|
4136
|
+
return map(property, (v, i, a) => [
|
|
4137
|
+
mapFn(v, i, a),
|
|
4138
|
+
iLast(i, a) ? typeof sepTrailing === "function" ? sepTrailing(v) : sepTrailing : sepFn(v, a[i + 1], i === 0 ? void 0 : a[i - 1])
|
|
4139
|
+
]);
|
|
4140
|
+
}
|
|
4141
|
+
print4.map_join = map_join;
|
|
4142
|
+
})(print3 || (print3 = {}));
|
|
4143
|
+
function genericPrint() {
|
|
4144
|
+
return withCheckContext(() => {
|
|
4145
|
+
const node = getNode();
|
|
4146
|
+
let printed = hasPrettierIgnore(node) ? node.loc.getOwnText() : printer[node.nodeType](print3, node);
|
|
4147
|
+
const inner_parens = needsInnerParens(node);
|
|
4148
|
+
if (inner_parens) {
|
|
4149
|
+
printed = group(["(", printed, ")"]);
|
|
4150
|
+
}
|
|
4151
|
+
if (utils.hasAttributes(node)) {
|
|
4152
|
+
const print_above = shouldPrintOuterAttributesAbove(node);
|
|
4153
|
+
printed = [
|
|
4154
|
+
...print3.join(
|
|
4155
|
+
"attributes",
|
|
4156
|
+
(attr) => print_above ? maybeEmptyLine(attr) : utils.is_LineCommentNode(attr) || utils.is_BlockCommentNode(attr) && hasBreaklineAfter(attr) ? hardline : " ",
|
|
4157
|
+
(attr) => print_above && utils.is_DocCommentAttribute(attr) ? maybeEmptyLine(attr) : print_above || utils.is_LineCommentNode(attr) || utils.is_BlockCommentNode(attr) && hasBreaklineAfter(attr) ? hardline : " "
|
|
4158
|
+
),
|
|
4159
|
+
printed
|
|
4160
|
+
];
|
|
4161
|
+
}
|
|
4162
|
+
printed = withComments(
|
|
4163
|
+
node,
|
|
4164
|
+
printed,
|
|
4165
|
+
hasPrettierIgnore(node) || (utils.is_Attribute(node) || utils.is_MacroInvocation(node)) && !isTransformed(node) ? escapeComments(0, (comment) => node.loc.ownContains(comment)) : utils.is_MacroRule(node) ? escapeComments(0, (comment) => node.transform.loc.contains(comment)) : utils.is_UnionPattern(getParentNode() ?? { nodeType: 0 }) ? new Set(getComments(node, 0, (comment) => !isDangling(comment))) : void 0
|
|
4166
|
+
);
|
|
4167
|
+
if (!inner_parens && needsOuterSoftbreakParens(node)) {
|
|
4168
|
+
printed = [group(["(", indent([softline, printed]), softline, ")"])];
|
|
4169
|
+
}
|
|
4170
|
+
return printed;
|
|
4171
|
+
});
|
|
4172
|
+
function hasPrettierIgnore(node) {
|
|
4173
|
+
return node.prettierIgnore || hasComment(node, 64 /* PrettierIgnore */) || utils.hasAttributes(node) && node.attributes.some(isPrettierIgnoreAttribute);
|
|
4174
|
+
}
|
|
4175
|
+
}
|
|
4176
|
+
function canAttachComment(n) {
|
|
4177
|
+
return utils.is_Node(n) && !utils.is_Comment(n) && !isNoopExpressionStatement(n) && !utils.is_MissingNode(n) && !utils.is_PunctuationToken(n);
|
|
4178
|
+
}
|
|
4179
|
+
var rustParser = {
|
|
4180
|
+
astFormat: "jinx-rust",
|
|
4181
|
+
locStart: utils.start,
|
|
4182
|
+
locEnd: utils.end,
|
|
4183
|
+
parse(code, options2) {
|
|
4184
|
+
ctx = { options: options2 };
|
|
4185
|
+
options2.rsParsedFile = jinxRust.rs.parseFile(options2.originalText = code, { filepath: options2.filepath });
|
|
4186
|
+
options2.actuallyMethodNodes = /* @__PURE__ */ new WeakSet();
|
|
4187
|
+
options2.danglingAttributes = [];
|
|
4188
|
+
options2.comments = [];
|
|
4189
|
+
transform_ast(options2);
|
|
4190
|
+
const comments = [];
|
|
4191
|
+
utils.insertNodes(comments, options2.comments);
|
|
4192
|
+
utils.insertNodes(comments, options2.danglingAttributes);
|
|
4193
|
+
options2.rsParsedFile.program.comments = comments;
|
|
4194
|
+
options2.commentSpans = new Map(comments.map((n) => [utils.start(n), utils.end(n)]));
|
|
4195
|
+
return options2.rsParsedFile.program;
|
|
4196
|
+
}
|
|
4197
|
+
};
|
|
4198
|
+
var plugin = {
|
|
4199
|
+
languages: [
|
|
4200
|
+
{
|
|
4201
|
+
name: "Rust",
|
|
4202
|
+
aliases: ["rs"],
|
|
4203
|
+
parsers: ["jinx-rust", "rust"],
|
|
4204
|
+
extensions: [".rs", ".rs.in"],
|
|
4205
|
+
linguistLanguageId: 327,
|
|
4206
|
+
vscodeLanguageIds: ["rust"],
|
|
4207
|
+
tmScope: "source.rust",
|
|
4208
|
+
aceMode: "rust",
|
|
4209
|
+
codemirrorMode: "rust",
|
|
4210
|
+
codemirrorMimeType: "text/x-rustsrc"
|
|
4211
|
+
}
|
|
4212
|
+
],
|
|
4213
|
+
parsers: {
|
|
4214
|
+
"jinx-rust": rustParser,
|
|
4215
|
+
rust: rustParser
|
|
4216
|
+
},
|
|
4217
|
+
printers: {
|
|
4218
|
+
"jinx-rust": {
|
|
4219
|
+
// Prettier v3 moved `handleComments.avoidAstMutation` to this feature flag.
|
|
4220
|
+
// @ts-expect-error internal printer feature, not part of the public typings
|
|
4221
|
+
features: { experimental_avoidAstMutation: true },
|
|
4222
|
+
preprocess: (node) => node.loc.src,
|
|
4223
|
+
print(path, options2, print4, args) {
|
|
4224
|
+
if (path.stack.length === 1) {
|
|
4225
|
+
ctx = { path, options: options2, print: print4, args };
|
|
4226
|
+
try {
|
|
4227
|
+
const printed = genericPrint();
|
|
4228
|
+
return printed;
|
|
4229
|
+
} finally {
|
|
4230
|
+
ctx = void 0;
|
|
4231
|
+
}
|
|
4232
|
+
} else if (args || ctx.args) {
|
|
4233
|
+
const prev_args = ctx.args;
|
|
4234
|
+
try {
|
|
4235
|
+
ctx.args = args;
|
|
4236
|
+
return genericPrint();
|
|
4237
|
+
} finally {
|
|
4238
|
+
ctx.args = prev_args;
|
|
4239
|
+
}
|
|
4240
|
+
} else {
|
|
4241
|
+
return genericPrint();
|
|
4242
|
+
}
|
|
4243
|
+
},
|
|
4244
|
+
hasPrettierIgnore: () => false,
|
|
4245
|
+
willPrintOwnComments: () => true,
|
|
4246
|
+
isBlockComment: utils.is_BlockCommentKind,
|
|
4247
|
+
canAttachComment,
|
|
4248
|
+
getCommentChildNodes,
|
|
4249
|
+
getVisitorKeys,
|
|
4250
|
+
printComment: genericPrint,
|
|
4251
|
+
handleComments: {
|
|
4252
|
+
ownLine: handleOwnLineComment,
|
|
4253
|
+
endOfLine: handleEndOfLineComment,
|
|
4254
|
+
remaining: handleRemainingComment
|
|
4255
|
+
}
|
|
4256
|
+
}
|
|
4257
|
+
},
|
|
4258
|
+
options: {},
|
|
4259
|
+
defaultOptions: {
|
|
4260
|
+
// default prettier (2) -> rustfmt (4)
|
|
4261
|
+
tabWidth: 4,
|
|
4262
|
+
// default prettier (80) -> rustfmt (100)
|
|
4263
|
+
printWidth: 100
|
|
4264
|
+
}
|
|
4265
|
+
};
|
|
4266
|
+
|
|
4267
|
+
// src/index.ts
|
|
4268
|
+
var src_default = plugin;
|
|
4269
|
+
var languages = plugin.languages;
|
|
4270
|
+
var parsers = plugin.parsers;
|
|
4271
|
+
var printers = plugin.printers;
|
|
4272
|
+
var options = plugin.options;
|
|
4273
|
+
var defaultOptions = plugin.defaultOptions;
|
|
4274
|
+
//!is_LetScrutinee(condition); //!is_LetScrutinee(getLeftMostCondition(condition));
|
|
4275
|
+
|
|
4276
|
+
exports.default = src_default;
|
|
4277
|
+
exports.defaultOptions = defaultOptions;
|
|
4278
|
+
exports.languages = languages;
|
|
4279
|
+
exports.options = options;
|
|
4280
|
+
exports.parsers = parsers;
|
|
4281
|
+
exports.printers = printers;
|