@danielx/civet 0.6.3 → 0.6.4
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/dist/browser.js +2148 -2137
- package/dist/main.js +2148 -2137
- package/dist/main.mjs +2148 -2137
- package/package.json +1 -1
package/dist/main.mjs
CHANGED
|
@@ -28,6 +28,119 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
28
28
|
var require_lib = __commonJS({
|
|
29
29
|
"source/lib.js"(exports, module) {
|
|
30
30
|
"use strict";
|
|
31
|
+
function addParentPointers(node, parent) {
|
|
32
|
+
if (node == null)
|
|
33
|
+
return;
|
|
34
|
+
if (typeof node !== "object")
|
|
35
|
+
return;
|
|
36
|
+
if (Array.isArray(node)) {
|
|
37
|
+
for (const child of node) {
|
|
38
|
+
addParentPointers(child, parent);
|
|
39
|
+
}
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
node.parent = parent;
|
|
43
|
+
if (node.children) {
|
|
44
|
+
for (const child of node.children) {
|
|
45
|
+
addParentPointers(child, node);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
function addPostfixStatement(statement, ws, post) {
|
|
50
|
+
let children, expressions;
|
|
51
|
+
if (post.blockPrefix?.length) {
|
|
52
|
+
let indent = post.blockPrefix[0][0];
|
|
53
|
+
expressions = [...post.blockPrefix, [indent, statement]];
|
|
54
|
+
children = [" {\n", ...expressions, "\n", indent?.slice?.(0, -2), "}"];
|
|
55
|
+
} else {
|
|
56
|
+
expressions = [["", statement]];
|
|
57
|
+
children = [" { ", ...expressions, " }"];
|
|
58
|
+
}
|
|
59
|
+
const block = {
|
|
60
|
+
type: "BlockStatement",
|
|
61
|
+
children,
|
|
62
|
+
expressions
|
|
63
|
+
};
|
|
64
|
+
children = [...post.children];
|
|
65
|
+
children.push(block);
|
|
66
|
+
if (!isWhitespaceOrEmpty(ws))
|
|
67
|
+
children.push(ws);
|
|
68
|
+
post = { ...post, children, block };
|
|
69
|
+
if (post.type === "IfStatement") {
|
|
70
|
+
post.then = block;
|
|
71
|
+
}
|
|
72
|
+
return post;
|
|
73
|
+
}
|
|
74
|
+
function adjustAtBindings(statements, asThis = false) {
|
|
75
|
+
gatherRecursiveAll(statements, (n) => n.type === "AtBindingProperty").forEach((binding) => {
|
|
76
|
+
const { ref } = binding;
|
|
77
|
+
if (asThis) {
|
|
78
|
+
const atBinding = binding.binding;
|
|
79
|
+
atBinding.children.pop();
|
|
80
|
+
atBinding.type = void 0;
|
|
81
|
+
binding.children.unshift(ref.id, ": this.", ref.base);
|
|
82
|
+
binding.type = "Property";
|
|
83
|
+
binding.ref = void 0;
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
if (ref.names[0] !== ref.base) {
|
|
87
|
+
binding.children.unshift(ref.base, ": ");
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
function adjustBindingElements(elements) {
|
|
92
|
+
const names = elements.flatMap((p) => p.names || []), { length } = elements;
|
|
93
|
+
let blockPrefix, restIndex = -1, restCount = 0;
|
|
94
|
+
elements.forEach(({ type }, i) => {
|
|
95
|
+
if (type === "BindingRestElement") {
|
|
96
|
+
if (restIndex < 0)
|
|
97
|
+
restIndex = i;
|
|
98
|
+
restCount++;
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
if (restCount === 0) {
|
|
102
|
+
return {
|
|
103
|
+
children: elements,
|
|
104
|
+
names,
|
|
105
|
+
blockPrefix,
|
|
106
|
+
length
|
|
107
|
+
};
|
|
108
|
+
} else if (restCount === 1) {
|
|
109
|
+
const rest = elements[restIndex];
|
|
110
|
+
const after = elements.slice(restIndex + 1);
|
|
111
|
+
const restIdentifier = rest.binding.ref || rest.binding;
|
|
112
|
+
names.push(...rest.names || []);
|
|
113
|
+
let l = after.length;
|
|
114
|
+
if (l) {
|
|
115
|
+
if (arrayElementHasTrailingComma(after[l - 1]))
|
|
116
|
+
l++;
|
|
117
|
+
blockPrefix = {
|
|
118
|
+
type: "PostRestBindingElements",
|
|
119
|
+
children: ["[", insertTrimmingSpace(after, ""), "] = ", restIdentifier, ".splice(-", l.toString(), ")"],
|
|
120
|
+
names: after.flatMap((p) => p.names)
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
return {
|
|
124
|
+
names,
|
|
125
|
+
children: [...elements.slice(0, restIndex), {
|
|
126
|
+
...rest,
|
|
127
|
+
children: rest.children.slice(0, -1)
|
|
128
|
+
}],
|
|
129
|
+
blockPrefix,
|
|
130
|
+
length
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
const err = {
|
|
134
|
+
type: "Error",
|
|
135
|
+
children: ["Multiple rest elements in array pattern"]
|
|
136
|
+
};
|
|
137
|
+
return {
|
|
138
|
+
names,
|
|
139
|
+
children: [...elements, err],
|
|
140
|
+
blockPrefix,
|
|
141
|
+
length
|
|
142
|
+
};
|
|
143
|
+
}
|
|
31
144
|
function aliasBinding(p, ref) {
|
|
32
145
|
if (p.type === "Identifier") {
|
|
33
146
|
p.children[0] = ref;
|
|
@@ -51,6 +164,13 @@ var require_lib = __commonJS({
|
|
|
51
164
|
}
|
|
52
165
|
return false;
|
|
53
166
|
}
|
|
167
|
+
var assert = {
|
|
168
|
+
equal(a, b, msg) {
|
|
169
|
+
if (a !== b) {
|
|
170
|
+
throw new Error(`Assertion failed [${msg}]: ${a} !== ${b}`);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
};
|
|
54
174
|
function blockWithPrefix(prefixStatements, block) {
|
|
55
175
|
if (prefixStatements && prefixStatements.length) {
|
|
56
176
|
const indent = getIndent(block.expressions[0]);
|
|
@@ -81,6 +201,126 @@ var require_lib = __commonJS({
|
|
|
81
201
|
removeParentPointers(node);
|
|
82
202
|
return deepCopy(node);
|
|
83
203
|
}
|
|
204
|
+
function constructInvocation(fn, arg) {
|
|
205
|
+
const fnArr = [fn.leadingComment, fn.expr, fn.trailingComment];
|
|
206
|
+
let expr = fn.expr;
|
|
207
|
+
while (expr.type === "ParenthesizedExpression") {
|
|
208
|
+
expr = expr.expression;
|
|
209
|
+
}
|
|
210
|
+
if (expr.ampersandBlock) {
|
|
211
|
+
const { ref, body } = expr;
|
|
212
|
+
ref.type = "PipedExpression";
|
|
213
|
+
ref.children = [makeLeftHandSideExpression(arg)];
|
|
214
|
+
return {
|
|
215
|
+
type: "UnwrappedExpression",
|
|
216
|
+
children: [skipIfOnlyWS(fn.leadingComment), ...body, skipIfOnlyWS(fn.trailingComment)]
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
expr = fn.expr;
|
|
220
|
+
const lhs = makeLeftHandSideExpression(expr);
|
|
221
|
+
let comment = skipIfOnlyWS(fn.trailingComment);
|
|
222
|
+
if (comment)
|
|
223
|
+
lhs.children.splice(2, 0, comment);
|
|
224
|
+
comment = skipIfOnlyWS(fn.leadingComment);
|
|
225
|
+
if (comment)
|
|
226
|
+
lhs.children.splice(1, 0, comment);
|
|
227
|
+
switch (arg.type) {
|
|
228
|
+
case "CommaExpression":
|
|
229
|
+
arg = makeLeftHandSideExpression(arg);
|
|
230
|
+
break;
|
|
231
|
+
}
|
|
232
|
+
return {
|
|
233
|
+
type: "CallExpression",
|
|
234
|
+
children: [lhs, "(", arg, ")"]
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
function constructPipeStep(fn, arg, returning) {
|
|
238
|
+
const children = [[fn.leadingComment, fn.expr, fn.trailingComment].map(skipIfOnlyWS), " ", arg];
|
|
239
|
+
switch (fn.expr.token) {
|
|
240
|
+
case "yield":
|
|
241
|
+
case "await":
|
|
242
|
+
if (returning) {
|
|
243
|
+
return [
|
|
244
|
+
children,
|
|
245
|
+
returning
|
|
246
|
+
];
|
|
247
|
+
}
|
|
248
|
+
return [
|
|
249
|
+
children,
|
|
250
|
+
null
|
|
251
|
+
];
|
|
252
|
+
case "return":
|
|
253
|
+
return [{
|
|
254
|
+
type: "ReturnStatement",
|
|
255
|
+
children
|
|
256
|
+
}, null];
|
|
257
|
+
}
|
|
258
|
+
if (returning) {
|
|
259
|
+
return [
|
|
260
|
+
constructInvocation(fn, arg),
|
|
261
|
+
returning
|
|
262
|
+
];
|
|
263
|
+
}
|
|
264
|
+
return [constructInvocation(fn, arg), null];
|
|
265
|
+
}
|
|
266
|
+
var initialSpacingRe = /^(?:\r?\n|\n)*((?:\r?\n|\n)\s+)/;
|
|
267
|
+
function dedentBlockString({ $loc, token: str }, spacing, trim = true) {
|
|
268
|
+
if (spacing == null)
|
|
269
|
+
spacing = str.match(initialSpacingRe);
|
|
270
|
+
if (spacing) {
|
|
271
|
+
str = str.replaceAll(spacing[1], "\n");
|
|
272
|
+
const l = spacing.length;
|
|
273
|
+
$loc.pos += l;
|
|
274
|
+
$loc.length -= l;
|
|
275
|
+
}
|
|
276
|
+
if (trim) {
|
|
277
|
+
str = str.replace(/^(\r?\n|\n)/, "").replace(/(\r?\n|\n)[ \t]*$/, "");
|
|
278
|
+
}
|
|
279
|
+
str = str.replace(/(\\.|`|\$\{)/g, (s) => {
|
|
280
|
+
if (s[0] === "\\") {
|
|
281
|
+
return s;
|
|
282
|
+
}
|
|
283
|
+
return `\\${s}`;
|
|
284
|
+
});
|
|
285
|
+
return {
|
|
286
|
+
$loc,
|
|
287
|
+
token: str
|
|
288
|
+
};
|
|
289
|
+
}
|
|
290
|
+
function dedentBlockSubstitutions($0) {
|
|
291
|
+
const [s, strWithSubstitutions, e] = $0;
|
|
292
|
+
if (strWithSubstitutions.length === 0) {
|
|
293
|
+
return $0;
|
|
294
|
+
}
|
|
295
|
+
let initialSpacing, i = 0, l = strWithSubstitutions.length, results = [s];
|
|
296
|
+
const { token } = strWithSubstitutions[0];
|
|
297
|
+
if (token) {
|
|
298
|
+
initialSpacing = token.match(initialSpacingRe);
|
|
299
|
+
} else {
|
|
300
|
+
initialSpacing = false;
|
|
301
|
+
}
|
|
302
|
+
while (i < l) {
|
|
303
|
+
let segment = strWithSubstitutions[i];
|
|
304
|
+
if (segment.token) {
|
|
305
|
+
segment = dedentBlockString(segment, initialSpacing, false);
|
|
306
|
+
if (i === 0) {
|
|
307
|
+
segment.token = segment.token.replace(/^(\r?\n|\n)/, "");
|
|
308
|
+
}
|
|
309
|
+
if (i === l - 1) {
|
|
310
|
+
segment.token = segment.token.replace(/(\r?\n|\n)[ \t]*$/, "");
|
|
311
|
+
}
|
|
312
|
+
results.push(segment);
|
|
313
|
+
} else {
|
|
314
|
+
results.push(segment);
|
|
315
|
+
}
|
|
316
|
+
i++;
|
|
317
|
+
}
|
|
318
|
+
results.push(e);
|
|
319
|
+
return {
|
|
320
|
+
type: "TemplateLiteral",
|
|
321
|
+
children: results
|
|
322
|
+
};
|
|
323
|
+
}
|
|
84
324
|
function deepCopy(node) {
|
|
85
325
|
if (node == null)
|
|
86
326
|
return node;
|
|
@@ -95,6 +335,356 @@ var require_lib = __commonJS({
|
|
|
95
335
|
})
|
|
96
336
|
);
|
|
97
337
|
}
|
|
338
|
+
function expressionizeIfClause(clause, b, e) {
|
|
339
|
+
const children = clause.children.slice(1);
|
|
340
|
+
children.push("?", b);
|
|
341
|
+
if (e) {
|
|
342
|
+
children.push(e[0], ":", ...e.slice(2));
|
|
343
|
+
} else {
|
|
344
|
+
children.push(":void 0");
|
|
345
|
+
}
|
|
346
|
+
return {
|
|
347
|
+
type: "IfExpression",
|
|
348
|
+
children
|
|
349
|
+
};
|
|
350
|
+
}
|
|
351
|
+
function expressionizeIteration(exp) {
|
|
352
|
+
const i = exp.children.indexOf(exp.block);
|
|
353
|
+
if (exp.subtype === "DoStatement") {
|
|
354
|
+
insertReturn(exp.block);
|
|
355
|
+
exp.children.splice(i, 1, ...wrapIIFE(exp.children, exp.async));
|
|
356
|
+
return;
|
|
357
|
+
}
|
|
358
|
+
const resultsRef = {
|
|
359
|
+
type: "Ref",
|
|
360
|
+
base: "results",
|
|
361
|
+
id: "results"
|
|
362
|
+
};
|
|
363
|
+
insertPush(exp.block, resultsRef);
|
|
364
|
+
exp.children.splice(
|
|
365
|
+
i,
|
|
366
|
+
1,
|
|
367
|
+
wrapIIFE([
|
|
368
|
+
"const ",
|
|
369
|
+
resultsRef,
|
|
370
|
+
"=[];",
|
|
371
|
+
...exp.children,
|
|
372
|
+
"; return ",
|
|
373
|
+
resultsRef
|
|
374
|
+
], exp.async)
|
|
375
|
+
);
|
|
376
|
+
}
|
|
377
|
+
function processBinaryOpExpression($0) {
|
|
378
|
+
const expandedOps = expandChainedComparisons($0);
|
|
379
|
+
let i = 2;
|
|
380
|
+
while (i < expandedOps.length) {
|
|
381
|
+
const op = expandedOps[i];
|
|
382
|
+
if (op.special) {
|
|
383
|
+
let [a, wsOp, op2, wsB, b] = expandedOps.slice(i - 2, i + 3);
|
|
384
|
+
if (op2.token === "instanceof" && b.type === "Literal" && b.children?.[0]?.type === "StringLiteral") {
|
|
385
|
+
a = ["typeof ", makeLeftHandSideExpression(a)];
|
|
386
|
+
if (op2.negated) {
|
|
387
|
+
op2 = { ...op2, token: "!==", negated: false };
|
|
388
|
+
} else {
|
|
389
|
+
op2 = { ...op2, token: "===" };
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
if (op2.asConst) {
|
|
393
|
+
a = makeAsConst(a);
|
|
394
|
+
b = makeAsConst(b);
|
|
395
|
+
}
|
|
396
|
+
let children;
|
|
397
|
+
if (op2.call) {
|
|
398
|
+
wsOp = insertTrimmingSpace(wsOp, "");
|
|
399
|
+
if (op2.reversed) {
|
|
400
|
+
wsB = insertTrimmingSpace(wsB, "");
|
|
401
|
+
children = [wsOp, op2.call, "(", wsB, b, ", ", a, ")", op2.suffix];
|
|
402
|
+
} else {
|
|
403
|
+
children = [wsOp, op2.call, "(", a, ",", wsB, b, ")", op2.suffix];
|
|
404
|
+
}
|
|
405
|
+
} else if (op2.method) {
|
|
406
|
+
wsOp = insertTrimmingSpace(wsOp, "");
|
|
407
|
+
wsB = insertTrimmingSpace(wsB, "");
|
|
408
|
+
if (op2.reversed) {
|
|
409
|
+
children = [wsB, b, wsOp, ".", op2.method, "(", a, ")"];
|
|
410
|
+
} else {
|
|
411
|
+
children = [a, wsOp, ".", op2.method, "(", wsB, b, ")"];
|
|
412
|
+
}
|
|
413
|
+
} else if (op2.token) {
|
|
414
|
+
children = [a, wsOp, op2, wsB, b];
|
|
415
|
+
if (op2.negated)
|
|
416
|
+
children = ["(", ...children, ")"];
|
|
417
|
+
} else {
|
|
418
|
+
throw new Error("Unknown operator: " + JSON.stringify(op2));
|
|
419
|
+
}
|
|
420
|
+
if (op2.negated)
|
|
421
|
+
children.unshift("!");
|
|
422
|
+
expandedOps.splice(i - 2, 5, {
|
|
423
|
+
children
|
|
424
|
+
});
|
|
425
|
+
} else {
|
|
426
|
+
i += 4;
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
return expandedOps;
|
|
430
|
+
}
|
|
431
|
+
function processCallMemberExpression(node) {
|
|
432
|
+
const { children } = node;
|
|
433
|
+
for (let i = 0; i < children.length; i++) {
|
|
434
|
+
const glob = children[i];
|
|
435
|
+
if (glob?.type === "PropertyGlob") {
|
|
436
|
+
const prefix = children.slice(0, i).concat(glob.dot);
|
|
437
|
+
const parts = [];
|
|
438
|
+
for (const part of glob.object.properties) {
|
|
439
|
+
if (part.type === "MethodDefinition") {
|
|
440
|
+
throw new Error("Glob pattern cannot have method definition");
|
|
441
|
+
}
|
|
442
|
+
if (part.value && !["CallExpression", "MemberExpression", "Identifier"].includes(part.value.type)) {
|
|
443
|
+
throw new Error("Glob pattern must have call or member expression value");
|
|
444
|
+
}
|
|
445
|
+
let value = part.value ?? part.name;
|
|
446
|
+
const wValue = getTrimmingSpace(part.value);
|
|
447
|
+
value = prefix.concat(insertTrimmingSpace(value, ""));
|
|
448
|
+
if (wValue)
|
|
449
|
+
value.unshift(wValue);
|
|
450
|
+
if (part.type === "SpreadProperty") {
|
|
451
|
+
parts.push({
|
|
452
|
+
type: part.type,
|
|
453
|
+
value,
|
|
454
|
+
dots: part.dots,
|
|
455
|
+
delim: part.delim,
|
|
456
|
+
names: part.names,
|
|
457
|
+
children: part.children.slice(0, 2).concat(value, part.delim)
|
|
458
|
+
});
|
|
459
|
+
} else {
|
|
460
|
+
parts.push({
|
|
461
|
+
type: part.type === "Identifier" ? "Property" : part.type,
|
|
462
|
+
name: part.name,
|
|
463
|
+
value,
|
|
464
|
+
delim: part.delim,
|
|
465
|
+
names: part.names,
|
|
466
|
+
children: [
|
|
467
|
+
isWhitespaceOrEmpty(part.children[0]) && part.children[0],
|
|
468
|
+
part.name,
|
|
469
|
+
isWhitespaceOrEmpty(part.children[2]) && part.children[2],
|
|
470
|
+
part.children[3]?.token === ":" ? part.children[3] : ":",
|
|
471
|
+
value,
|
|
472
|
+
part.delim
|
|
473
|
+
]
|
|
474
|
+
});
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
const object = {
|
|
478
|
+
type: "ObjectExpression",
|
|
479
|
+
children: [
|
|
480
|
+
glob.object.children[0],
|
|
481
|
+
...parts,
|
|
482
|
+
glob.object.children.at(-1)
|
|
483
|
+
],
|
|
484
|
+
properties: parts
|
|
485
|
+
};
|
|
486
|
+
if (i === children.length - 1)
|
|
487
|
+
return object;
|
|
488
|
+
return processCallMemberExpression({
|
|
489
|
+
...node,
|
|
490
|
+
children: [object, ...children.slice(i + 1)]
|
|
491
|
+
});
|
|
492
|
+
} else if (glob?.type === "PropertyBind") {
|
|
493
|
+
const prefix = children.slice(0, i);
|
|
494
|
+
return processCallMemberExpression({
|
|
495
|
+
...node,
|
|
496
|
+
children: [
|
|
497
|
+
prefix,
|
|
498
|
+
{
|
|
499
|
+
...glob,
|
|
500
|
+
type: "PropertyAccess",
|
|
501
|
+
children: [...glob.children, ".bind(", prefix, ")"]
|
|
502
|
+
},
|
|
503
|
+
...children.slice(i + 1)
|
|
504
|
+
]
|
|
505
|
+
});
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
return node;
|
|
509
|
+
}
|
|
510
|
+
function wrapIterationReturningResults(statement, outerRef) {
|
|
511
|
+
if (statement.type === "DoStatement") {
|
|
512
|
+
if (outerRef) {
|
|
513
|
+
insertPush(statement.block, outerRef);
|
|
514
|
+
} else {
|
|
515
|
+
insertReturn(statement.block);
|
|
516
|
+
}
|
|
517
|
+
return;
|
|
518
|
+
}
|
|
519
|
+
const resultsRef = {
|
|
520
|
+
type: "Ref",
|
|
521
|
+
base: "results",
|
|
522
|
+
id: "results"
|
|
523
|
+
};
|
|
524
|
+
const declaration = {
|
|
525
|
+
type: "Declaration",
|
|
526
|
+
children: ["const ", resultsRef, "=[];"]
|
|
527
|
+
};
|
|
528
|
+
insertPush(statement.block, resultsRef);
|
|
529
|
+
statement.children.unshift(declaration);
|
|
530
|
+
if (outerRef) {
|
|
531
|
+
statement.children.push(";", outerRef, ".push(", resultsRef, ");");
|
|
532
|
+
} else {
|
|
533
|
+
statement.children.push(";return ", resultsRef, ";");
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
function insertPush(node, ref) {
|
|
537
|
+
if (!node)
|
|
538
|
+
return;
|
|
539
|
+
switch (node.type) {
|
|
540
|
+
case "BlockStatement":
|
|
541
|
+
if (node.expressions.length) {
|
|
542
|
+
const last = node.expressions[node.expressions.length - 1];
|
|
543
|
+
insertPush(last, ref);
|
|
544
|
+
} else {
|
|
545
|
+
node.expressions.push([ref, ".push(void 0);"]);
|
|
546
|
+
}
|
|
547
|
+
return;
|
|
548
|
+
case "CaseBlock":
|
|
549
|
+
node.clauses.forEach((clause) => {
|
|
550
|
+
insertPush(clause, ref);
|
|
551
|
+
});
|
|
552
|
+
return;
|
|
553
|
+
case "WhenClause":
|
|
554
|
+
insertPush(node.block, ref);
|
|
555
|
+
return;
|
|
556
|
+
case "DefaultClause":
|
|
557
|
+
insertPush(node.block, ref);
|
|
558
|
+
return;
|
|
559
|
+
}
|
|
560
|
+
if (!Array.isArray(node))
|
|
561
|
+
return;
|
|
562
|
+
const [, exp] = node;
|
|
563
|
+
if (!exp)
|
|
564
|
+
return;
|
|
565
|
+
const indent = getIndent(node);
|
|
566
|
+
switch (exp.type) {
|
|
567
|
+
case "BreakStatement":
|
|
568
|
+
case "ContinueStatement":
|
|
569
|
+
case "DebuggerStatement":
|
|
570
|
+
case "EmptyStatement":
|
|
571
|
+
case "ReturnStatement":
|
|
572
|
+
case "ThrowStatement":
|
|
573
|
+
case "Declaration":
|
|
574
|
+
return;
|
|
575
|
+
case "ForStatement":
|
|
576
|
+
case "IterationStatement":
|
|
577
|
+
case "DoStatement":
|
|
578
|
+
wrapIterationReturningResults(exp, ref);
|
|
579
|
+
return;
|
|
580
|
+
case "BlockStatement":
|
|
581
|
+
insertPush(exp.expressions[exp.expressions.length - 1], ref);
|
|
582
|
+
return;
|
|
583
|
+
case "IfStatement":
|
|
584
|
+
insertPush(exp.then, ref);
|
|
585
|
+
if (exp.else)
|
|
586
|
+
insertPush(exp.else[2], ref);
|
|
587
|
+
else
|
|
588
|
+
exp.children.push([" else {\n", indent, ref, ".push(undefined)\n", indent, "}"]);
|
|
589
|
+
return;
|
|
590
|
+
case "PatternMatchingStatement":
|
|
591
|
+
insertPush(exp.children[0][0], ref);
|
|
592
|
+
return;
|
|
593
|
+
case "SwitchStatement":
|
|
594
|
+
insertPush(exp.children[2], ref);
|
|
595
|
+
return;
|
|
596
|
+
case "TryStatement":
|
|
597
|
+
exp.blocks.forEach((block) => insertPush(block, ref));
|
|
598
|
+
return;
|
|
599
|
+
}
|
|
600
|
+
if (node[node.length - 1]?.type === "SemicolonDelimiter")
|
|
601
|
+
return;
|
|
602
|
+
node.splice(1, 0, ref, ".push(");
|
|
603
|
+
node.push(")");
|
|
604
|
+
}
|
|
605
|
+
function wrapWithReturn(expression) {
|
|
606
|
+
const children = expression ? ["return ", expression] : ["return"];
|
|
607
|
+
return {
|
|
608
|
+
type: "ReturnStatement",
|
|
609
|
+
children
|
|
610
|
+
};
|
|
611
|
+
}
|
|
612
|
+
function expandChainedComparisons([first, binops]) {
|
|
613
|
+
const relationalOps = ["==", "===", "!=", "!==", "<", "<=", ">", ">=", "in"];
|
|
614
|
+
const lowerPrecedenceOps = ["??", "&&", "||", "&", "|", "^"];
|
|
615
|
+
let results = [];
|
|
616
|
+
let i = 0;
|
|
617
|
+
let l = binops.length;
|
|
618
|
+
let start = 0;
|
|
619
|
+
let chains = [];
|
|
620
|
+
while (i < l) {
|
|
621
|
+
const [, op] = binops[i];
|
|
622
|
+
if (relationalOps.includes(op.token) || op.relational) {
|
|
623
|
+
chains.push(i);
|
|
624
|
+
} else if (lowerPrecedenceOps.includes(op.token)) {
|
|
625
|
+
processChains();
|
|
626
|
+
first = [];
|
|
627
|
+
}
|
|
628
|
+
i++;
|
|
629
|
+
}
|
|
630
|
+
processChains();
|
|
631
|
+
return results;
|
|
632
|
+
function processChains() {
|
|
633
|
+
if (chains.length > 1) {
|
|
634
|
+
chains.forEach((index, k) => {
|
|
635
|
+
if (k > 0) {
|
|
636
|
+
results.push(" ", "&&", " ");
|
|
637
|
+
}
|
|
638
|
+
const [pre, op, post, exp] = binops[index];
|
|
639
|
+
let endIndex;
|
|
640
|
+
if (k < chains.length - 1) {
|
|
641
|
+
endIndex = chains[k + 1];
|
|
642
|
+
} else {
|
|
643
|
+
endIndex = i + 1;
|
|
644
|
+
}
|
|
645
|
+
results = results.concat(first, ...binops.slice(start, endIndex));
|
|
646
|
+
first = [exp].concat(binops.slice(index + 1, endIndex));
|
|
647
|
+
start = endIndex;
|
|
648
|
+
});
|
|
649
|
+
} else {
|
|
650
|
+
results = results.concat(first, ...binops.slice(start, i + 1));
|
|
651
|
+
start = i + 1;
|
|
652
|
+
}
|
|
653
|
+
chains.length = 0;
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
function processParams(f) {
|
|
657
|
+
const { type, parameters, block } = f;
|
|
658
|
+
if (type === "ArrowFunction" && parameters && parameters.tp && parameters.tp.parameters.length === 1) {
|
|
659
|
+
parameters.tp.parameters.push(",");
|
|
660
|
+
}
|
|
661
|
+
if (!block)
|
|
662
|
+
return;
|
|
663
|
+
const { expressions } = block;
|
|
664
|
+
if (!expressions)
|
|
665
|
+
return;
|
|
666
|
+
const { blockPrefix } = parameters;
|
|
667
|
+
let indent;
|
|
668
|
+
if (!expressions.length) {
|
|
669
|
+
indent = "";
|
|
670
|
+
} else {
|
|
671
|
+
indent = expressions[0][0];
|
|
672
|
+
}
|
|
673
|
+
const [splices, thisAssignments] = gatherBindingCode(parameters, {
|
|
674
|
+
injectParamProps: f.name === "constructor"
|
|
675
|
+
});
|
|
676
|
+
const delimiter = {
|
|
677
|
+
type: "SemicolonDelimiter",
|
|
678
|
+
children: [";"]
|
|
679
|
+
};
|
|
680
|
+
const prefix = splices.map((s) => ["let ", s]).concat(thisAssignments).map(
|
|
681
|
+
(s) => s.type ? {
|
|
682
|
+
...s,
|
|
683
|
+
children: [indent, ...s.children, delimiter]
|
|
684
|
+
} : [indent, s, delimiter]
|
|
685
|
+
);
|
|
686
|
+
expressions.unshift(...prefix);
|
|
687
|
+
}
|
|
98
688
|
function removeParentPointers(node) {
|
|
99
689
|
if (node == null)
|
|
100
690
|
return;
|
|
@@ -216,10 +806,123 @@ var require_lib = __commonJS({
|
|
|
216
806
|
node.hoistDec = null;
|
|
217
807
|
});
|
|
218
808
|
}
|
|
809
|
+
function insertReturn(node) {
|
|
810
|
+
if (!node)
|
|
811
|
+
return;
|
|
812
|
+
switch (node.type) {
|
|
813
|
+
case "BlockStatement":
|
|
814
|
+
if (node.expressions.length) {
|
|
815
|
+
const last = node.expressions[node.expressions.length - 1];
|
|
816
|
+
insertReturn(last);
|
|
817
|
+
} else {
|
|
818
|
+
if (node.parent.type === "CatchClause") {
|
|
819
|
+
node.expressions.push(["return"]);
|
|
820
|
+
}
|
|
821
|
+
}
|
|
822
|
+
return;
|
|
823
|
+
case "WhenClause":
|
|
824
|
+
node.children.splice(node.children.indexOf(node.break), 1);
|
|
825
|
+
if (node.block.expressions.length) {
|
|
826
|
+
insertReturn(node.block);
|
|
827
|
+
} else {
|
|
828
|
+
node.block.expressions.push(wrapWithReturn());
|
|
829
|
+
}
|
|
830
|
+
return;
|
|
831
|
+
case "DefaultClause":
|
|
832
|
+
insertReturn(node.block);
|
|
833
|
+
return;
|
|
834
|
+
}
|
|
835
|
+
if (!Array.isArray(node))
|
|
836
|
+
return;
|
|
837
|
+
const [, exp, semi] = node;
|
|
838
|
+
if (semi?.type === "SemicolonDelimiter")
|
|
839
|
+
return;
|
|
840
|
+
let indent = getIndent(node);
|
|
841
|
+
if (!exp)
|
|
842
|
+
return;
|
|
843
|
+
switch (exp.type) {
|
|
844
|
+
case "BreakStatement":
|
|
845
|
+
case "ContinueStatement":
|
|
846
|
+
case "DebuggerStatement":
|
|
847
|
+
case "EmptyStatement":
|
|
848
|
+
case "ReturnStatement":
|
|
849
|
+
case "ThrowStatement":
|
|
850
|
+
case "Declaration":
|
|
851
|
+
return;
|
|
852
|
+
case "ForStatement":
|
|
853
|
+
case "IterationStatement":
|
|
854
|
+
case "DoStatement":
|
|
855
|
+
wrapIterationReturningResults(exp);
|
|
856
|
+
return;
|
|
857
|
+
case "BlockStatement":
|
|
858
|
+
insertReturn(exp.expressions[exp.expressions.length - 1]);
|
|
859
|
+
return;
|
|
860
|
+
case "IfStatement":
|
|
861
|
+
insertReturn(exp.then);
|
|
862
|
+
if (exp.else)
|
|
863
|
+
insertReturn(exp.else[2]);
|
|
864
|
+
else
|
|
865
|
+
exp.children.push(["", {
|
|
866
|
+
type: "ReturnStatement",
|
|
867
|
+
children: [";return"]
|
|
868
|
+
}]);
|
|
869
|
+
return;
|
|
870
|
+
case "PatternMatchingStatement":
|
|
871
|
+
insertReturn(exp.children[0][0]);
|
|
872
|
+
return;
|
|
873
|
+
case "SwitchStatement":
|
|
874
|
+
insertSwitchReturns(exp);
|
|
875
|
+
return;
|
|
876
|
+
case "TryStatement":
|
|
877
|
+
exp.blocks.forEach((block) => insertReturn(block));
|
|
878
|
+
return;
|
|
879
|
+
}
|
|
880
|
+
if (node[node.length - 1]?.type === "SemicolonDelimiter")
|
|
881
|
+
return;
|
|
882
|
+
const returnStatement = wrapWithReturn(node[1]);
|
|
883
|
+
node.splice(1, 1, returnStatement);
|
|
884
|
+
}
|
|
885
|
+
function insertSwitchReturns(exp) {
|
|
886
|
+
switch (exp.type) {
|
|
887
|
+
case "SwitchStatement":
|
|
888
|
+
exp.caseBlock.clauses.forEach((clause) => {
|
|
889
|
+
insertReturn(clause);
|
|
890
|
+
});
|
|
891
|
+
return;
|
|
892
|
+
case "SwitchExpression":
|
|
893
|
+
exp.caseBlock.clauses.forEach(insertReturn);
|
|
894
|
+
return;
|
|
895
|
+
}
|
|
896
|
+
}
|
|
897
|
+
function isEmptyBareBlock(node) {
|
|
898
|
+
if (node?.type !== "BlockStatement")
|
|
899
|
+
return false;
|
|
900
|
+
const { bare, expressions } = node;
|
|
901
|
+
return bare && (expressions.length === 0 || expressions.length === 1 && expressions[0][1]?.type === "EmptyStatement");
|
|
902
|
+
}
|
|
219
903
|
function isFunction(node) {
|
|
220
904
|
const { type } = node;
|
|
221
905
|
return type === "FunctionExpression" || type === "ArrowFunction" || type === "MethodDefinition" || node.async;
|
|
222
906
|
}
|
|
907
|
+
function isVoidType(t) {
|
|
908
|
+
return t?.type === "LiteralType" && t.t.type === "VoidType";
|
|
909
|
+
}
|
|
910
|
+
function isWhitespaceOrEmpty(node) {
|
|
911
|
+
if (!node)
|
|
912
|
+
return true;
|
|
913
|
+
if (node.type === "Ref")
|
|
914
|
+
return false;
|
|
915
|
+
if (node.token)
|
|
916
|
+
return node.token.match(/^\s*$/);
|
|
917
|
+
if (node.children)
|
|
918
|
+
node = node.children;
|
|
919
|
+
if (!node.length)
|
|
920
|
+
return true;
|
|
921
|
+
if (typeof node === "string")
|
|
922
|
+
return node.match(/^\s*$/);
|
|
923
|
+
if (Array.isArray(node))
|
|
924
|
+
return node.every(isWhitespaceOrEmpty);
|
|
925
|
+
}
|
|
223
926
|
function gatherRecursiveWithinFunction(node, predicate) {
|
|
224
927
|
return gatherRecursive(node, predicate, isFunction);
|
|
225
928
|
}
|
|
@@ -256,62 +959,6 @@ var require_lib = __commonJS({
|
|
|
256
959
|
if (target.token)
|
|
257
960
|
return target.token.match(/^ ?/)[0];
|
|
258
961
|
}
|
|
259
|
-
function needsRef(exp) {
|
|
260
|
-
switch (exp.type) {
|
|
261
|
-
case "Identifier":
|
|
262
|
-
case "Literal":
|
|
263
|
-
case "Ref":
|
|
264
|
-
return false;
|
|
265
|
-
default:
|
|
266
|
-
return true;
|
|
267
|
-
}
|
|
268
|
-
}
|
|
269
|
-
function maybeRef(exp, base = "ref") {
|
|
270
|
-
if (!needsRef(exp))
|
|
271
|
-
return exp;
|
|
272
|
-
return {
|
|
273
|
-
type: "Ref",
|
|
274
|
-
base,
|
|
275
|
-
id: base
|
|
276
|
-
};
|
|
277
|
-
}
|
|
278
|
-
function literalValue(literal) {
|
|
279
|
-
let { raw } = literal;
|
|
280
|
-
switch (raw) {
|
|
281
|
-
case "null":
|
|
282
|
-
return null;
|
|
283
|
-
case "true":
|
|
284
|
-
return true;
|
|
285
|
-
case "false":
|
|
286
|
-
return false;
|
|
287
|
-
}
|
|
288
|
-
if (raw.startsWith('"') && raw.endsWith('"') || raw.startsWith("'") && raw.endsWith("'")) {
|
|
289
|
-
return raw.slice(1, -1);
|
|
290
|
-
}
|
|
291
|
-
const numeric = literal.children.find(
|
|
292
|
-
(child) => child.type === "NumericLiteral"
|
|
293
|
-
);
|
|
294
|
-
if (numeric) {
|
|
295
|
-
raw = raw.replace(/_/g, "");
|
|
296
|
-
const { token } = numeric;
|
|
297
|
-
if (token.endsWith("n")) {
|
|
298
|
-
return BigInt(raw.slice(0, -1));
|
|
299
|
-
} else if (token.match(/[\.eE]/)) {
|
|
300
|
-
return parseFloat(raw);
|
|
301
|
-
} else if (token.startsWith("0")) {
|
|
302
|
-
switch (token.charAt(1).toLowerCase()) {
|
|
303
|
-
case "x":
|
|
304
|
-
return parseInt(raw.replace(/0[xX]/, ""), 16);
|
|
305
|
-
case "b":
|
|
306
|
-
return parseInt(raw.replace(/0[bB]/, ""), 2);
|
|
307
|
-
case "o":
|
|
308
|
-
return parseInt(raw.replace(/0[oO]/, ""), 8);
|
|
309
|
-
}
|
|
310
|
-
}
|
|
311
|
-
return parseInt(raw, 10);
|
|
312
|
-
}
|
|
313
|
-
throw new Error("Unrecognized literal " + JSON.stringify(literal));
|
|
314
|
-
}
|
|
315
962
|
function forRange(open, forDeclaration, range, stepExp, close) {
|
|
316
963
|
const { start, end, inclusive } = range;
|
|
317
964
|
const counterRef = {
|
|
@@ -399,6 +1046,79 @@ var require_lib = __commonJS({
|
|
|
399
1046
|
insertRestSplices(statements, splices, thisAssignments);
|
|
400
1047
|
return [splices, thisAssignments];
|
|
401
1048
|
}
|
|
1049
|
+
function literalValue(literal) {
|
|
1050
|
+
let { raw } = literal;
|
|
1051
|
+
switch (raw) {
|
|
1052
|
+
case "null":
|
|
1053
|
+
return null;
|
|
1054
|
+
case "true":
|
|
1055
|
+
return true;
|
|
1056
|
+
case "false":
|
|
1057
|
+
return false;
|
|
1058
|
+
}
|
|
1059
|
+
if (raw.startsWith('"') && raw.endsWith('"') || raw.startsWith("'") && raw.endsWith("'")) {
|
|
1060
|
+
return raw.slice(1, -1);
|
|
1061
|
+
}
|
|
1062
|
+
const numeric = literal.children.find(
|
|
1063
|
+
(child) => child.type === "NumericLiteral"
|
|
1064
|
+
);
|
|
1065
|
+
if (numeric) {
|
|
1066
|
+
raw = raw.replace(/_/g, "");
|
|
1067
|
+
const { token } = numeric;
|
|
1068
|
+
if (token.endsWith("n")) {
|
|
1069
|
+
return BigInt(raw.slice(0, -1));
|
|
1070
|
+
} else if (token.match(/[\.eE]/)) {
|
|
1071
|
+
return parseFloat(raw);
|
|
1072
|
+
} else if (token.startsWith("0")) {
|
|
1073
|
+
switch (token.charAt(1).toLowerCase()) {
|
|
1074
|
+
case "x":
|
|
1075
|
+
return parseInt(raw.replace(/0[xX]/, ""), 16);
|
|
1076
|
+
case "b":
|
|
1077
|
+
return parseInt(raw.replace(/0[bB]/, ""), 2);
|
|
1078
|
+
case "o":
|
|
1079
|
+
return parseInt(raw.replace(/0[oO]/, ""), 8);
|
|
1080
|
+
}
|
|
1081
|
+
}
|
|
1082
|
+
return parseInt(raw, 10);
|
|
1083
|
+
}
|
|
1084
|
+
throw new Error("Unrecognized literal " + JSON.stringify(literal));
|
|
1085
|
+
}
|
|
1086
|
+
var asConst = {
|
|
1087
|
+
ts: true,
|
|
1088
|
+
children: [" as const"]
|
|
1089
|
+
};
|
|
1090
|
+
function makeAsConst(node) {
|
|
1091
|
+
if (node.type === "Literal" && node.raw !== "null" || node.type === "ArrayExpression" || node.type === "ObjectExpression") {
|
|
1092
|
+
return { ...node, children: [...node.children, asConst] };
|
|
1093
|
+
}
|
|
1094
|
+
return node;
|
|
1095
|
+
}
|
|
1096
|
+
function makeLeftHandSideExpression(expression) {
|
|
1097
|
+
switch (expression.type) {
|
|
1098
|
+
case "Ref":
|
|
1099
|
+
case "Identifier":
|
|
1100
|
+
case "Literal":
|
|
1101
|
+
case "CallExpression":
|
|
1102
|
+
case "MemberExpression":
|
|
1103
|
+
case "ParenthesizedExpression":
|
|
1104
|
+
return expression;
|
|
1105
|
+
default:
|
|
1106
|
+
return {
|
|
1107
|
+
type: "ParenthesizedExpression",
|
|
1108
|
+
children: ["(", expression, ")"],
|
|
1109
|
+
expression
|
|
1110
|
+
};
|
|
1111
|
+
}
|
|
1112
|
+
}
|
|
1113
|
+
function maybeRef(exp, base = "ref") {
|
|
1114
|
+
if (!needsRef(exp))
|
|
1115
|
+
return exp;
|
|
1116
|
+
return {
|
|
1117
|
+
type: "Ref",
|
|
1118
|
+
base,
|
|
1119
|
+
id: base
|
|
1120
|
+
};
|
|
1121
|
+
}
|
|
402
1122
|
function modifyString(str) {
|
|
403
1123
|
return str.replace(/(^.?|[^\\]{2})(\\\\)*\n/g, "$1$2\\n");
|
|
404
1124
|
}
|
|
@@ -486,6 +1206,20 @@ var require_lib = __commonJS({
|
|
|
486
1206
|
}
|
|
487
1207
|
return parts;
|
|
488
1208
|
}
|
|
1209
|
+
function needsRef(expression, base = "ref") {
|
|
1210
|
+
switch (expression.type) {
|
|
1211
|
+
case "Ref":
|
|
1212
|
+
case "Identifier":
|
|
1213
|
+
case "Literal":
|
|
1214
|
+
return;
|
|
1215
|
+
default:
|
|
1216
|
+
return {
|
|
1217
|
+
type: "Ref",
|
|
1218
|
+
base,
|
|
1219
|
+
id: base
|
|
1220
|
+
};
|
|
1221
|
+
}
|
|
1222
|
+
}
|
|
489
1223
|
function processCoffeeInterpolation(s, parts, e, $loc) {
|
|
490
1224
|
if (parts.length === 0 || parts.length === 1 && parts[0].token != null) {
|
|
491
1225
|
return {
|
|
@@ -574,55 +1308,914 @@ var require_lib = __commonJS({
|
|
|
574
1308
|
thisAssignments
|
|
575
1309
|
};
|
|
576
1310
|
}
|
|
577
|
-
function
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
1311
|
+
function processFunctions(statements, config) {
|
|
1312
|
+
gatherRecursiveAll(statements, ({ type }) => type === "FunctionExpression" || type === "ArrowFunction").forEach((f) => {
|
|
1313
|
+
processParams(f);
|
|
1314
|
+
if (!processReturnValue(f) && config.implicitReturns) {
|
|
1315
|
+
const { block, returnType } = f;
|
|
1316
|
+
const isVoid = isVoidType(returnType?.t);
|
|
1317
|
+
const isBlock = block?.type === "BlockStatement";
|
|
1318
|
+
if (!isVoid && isBlock) {
|
|
1319
|
+
insertReturn(block);
|
|
1320
|
+
}
|
|
1321
|
+
}
|
|
1322
|
+
});
|
|
1323
|
+
gatherRecursiveAll(statements, ({ type }) => type === "MethodDefinition").forEach((f) => {
|
|
1324
|
+
processParams(f);
|
|
1325
|
+
if (!processReturnValue(f) && config.implicitReturns) {
|
|
1326
|
+
const { signature, block } = f;
|
|
1327
|
+
const isConstructor = signature.name === "constructor";
|
|
1328
|
+
const isVoid = isVoidType(signature.returnType?.t);
|
|
1329
|
+
const isSet = signature.modifier?.set;
|
|
1330
|
+
if (!isConstructor && !isSet && !isVoid) {
|
|
1331
|
+
insertReturn(block);
|
|
1332
|
+
}
|
|
1333
|
+
}
|
|
1334
|
+
});
|
|
1335
|
+
}
|
|
1336
|
+
function processSwitchExpressions(statements) {
|
|
1337
|
+
gatherRecursiveAll(statements, (n) => n.type === "SwitchExpression").forEach(insertSwitchReturns);
|
|
1338
|
+
}
|
|
1339
|
+
function processTryExpressions(statements) {
|
|
1340
|
+
gatherRecursiveAll(statements, (n) => n.type === "TryExpression").forEach(({ blocks }) => {
|
|
1341
|
+
blocks.forEach(insertReturn);
|
|
1342
|
+
});
|
|
1343
|
+
}
|
|
1344
|
+
function processBindingPatternLHS(lhs, tail) {
|
|
1345
|
+
adjustAtBindings(lhs, true);
|
|
1346
|
+
const [splices, thisAssignments] = gatherBindingCode(lhs);
|
|
1347
|
+
tail.push(...splices.map((s) => [", ", s]), ...thisAssignments.map((a) => [", ", a]));
|
|
1348
|
+
}
|
|
1349
|
+
function processAssignments(statements) {
|
|
1350
|
+
gatherRecursiveAll(statements, (n) => n.type === "AssignmentExpression" && n.names === null).forEach((exp) => {
|
|
1351
|
+
let { lhs: $1, exp: $2 } = exp, tail = [], i = 0, len = $1.length;
|
|
1352
|
+
if ($1.some((left) => left[left.length - 1].special)) {
|
|
1353
|
+
if ($1.length !== 1) {
|
|
1354
|
+
throw new Error("Only one assignment with id= is allowed");
|
|
1355
|
+
}
|
|
1356
|
+
const [, lhs, , op] = $1[0];
|
|
1357
|
+
const { call } = op;
|
|
1358
|
+
op[op.length - 1] = "=";
|
|
1359
|
+
$2 = [call, "(", lhs, ", ", $2, ")"];
|
|
1360
|
+
}
|
|
1361
|
+
let wrapped = false;
|
|
1362
|
+
while (i < len) {
|
|
1363
|
+
const lastAssignment = $1[i++];
|
|
1364
|
+
const [, lhs, , op] = lastAssignment;
|
|
1365
|
+
if (op.token !== "=")
|
|
1366
|
+
continue;
|
|
1367
|
+
if (lhs.type === "ObjectExpression" || lhs.type === "ObjectBindingPattern") {
|
|
1368
|
+
if (!wrapped) {
|
|
1369
|
+
wrapped = true;
|
|
1370
|
+
lhs.children.splice(0, 0, "(");
|
|
1371
|
+
tail.push(")");
|
|
1372
|
+
}
|
|
1373
|
+
}
|
|
1374
|
+
}
|
|
1375
|
+
i = len - 1;
|
|
1376
|
+
while (i >= 0) {
|
|
1377
|
+
const lastAssignment = $1[i];
|
|
1378
|
+
if (lastAssignment[3].token === "=") {
|
|
1379
|
+
const lhs = lastAssignment[1];
|
|
1380
|
+
if (lhs.type === "MemberExpression") {
|
|
1381
|
+
const members = lhs.children;
|
|
1382
|
+
const lastMember = members[members.length - 1];
|
|
1383
|
+
if (lastMember.type === "SliceExpression") {
|
|
1384
|
+
const { start, end, children: c } = lastMember;
|
|
1385
|
+
c[0].token = ".splice(";
|
|
1386
|
+
c[1] = start;
|
|
1387
|
+
c[2] = ", ";
|
|
1388
|
+
if (end)
|
|
1389
|
+
c[3] = [end, " - ", start];
|
|
1390
|
+
else
|
|
1391
|
+
c[3] = ["1/0"];
|
|
1392
|
+
c[4] = [", ...", $2];
|
|
1393
|
+
c[5] = ")";
|
|
1394
|
+
lastAssignment.pop();
|
|
1395
|
+
if (isWhitespaceOrEmpty(lastAssignment[2]))
|
|
1396
|
+
lastAssignment.pop();
|
|
1397
|
+
if ($1.length > 1) {
|
|
1398
|
+
throw new Error("Not implemented yet! TODO: Handle multiple splice assignments");
|
|
1399
|
+
}
|
|
1400
|
+
exp.children = [$1];
|
|
1401
|
+
exp.names = [];
|
|
1402
|
+
return;
|
|
1403
|
+
}
|
|
1404
|
+
} else if (lhs.type === "ObjectBindingPattern" || lhs.type === "ArrayBindingPattern") {
|
|
1405
|
+
processBindingPatternLHS(lhs, tail);
|
|
1406
|
+
}
|
|
1407
|
+
}
|
|
1408
|
+
i--;
|
|
1409
|
+
}
|
|
1410
|
+
const names = $1.flatMap(([, l]) => l.names || []);
|
|
1411
|
+
exp.children = [$1, $2, ...tail];
|
|
1412
|
+
exp.names = names;
|
|
1413
|
+
});
|
|
1414
|
+
gatherRecursiveAll(statements, (n) => n.type === "AssignmentExpression" || n.type === "UpdateExpression").forEach((exp) => {
|
|
1415
|
+
function extractAssignment(lhs) {
|
|
1416
|
+
let expr = lhs;
|
|
1417
|
+
while (expr.type === "ParenthesizedExpression") {
|
|
1418
|
+
expr = expr.expression;
|
|
1419
|
+
}
|
|
1420
|
+
if (expr.type === "AssignmentExpression" || expr.type === "UpdateExpression") {
|
|
1421
|
+
if (expr.type === "UpdateExpression" && expr.children[0] === expr.assigned) {
|
|
1422
|
+
post.push([", ", lhs]);
|
|
1423
|
+
} else {
|
|
1424
|
+
pre.push([lhs, ", "]);
|
|
1425
|
+
}
|
|
1426
|
+
return expr.assigned;
|
|
1427
|
+
}
|
|
1428
|
+
}
|
|
1429
|
+
const pre = [], post = [];
|
|
585
1430
|
switch (exp.type) {
|
|
586
|
-
case "
|
|
1431
|
+
case "AssignmentExpression":
|
|
1432
|
+
if (!exp.lhs)
|
|
1433
|
+
return;
|
|
1434
|
+
exp.lhs.forEach((lhsPart, i) => {
|
|
1435
|
+
let newLhs2 = extractAssignment(lhsPart[1]);
|
|
1436
|
+
if (newLhs2) {
|
|
1437
|
+
lhsPart[1] = newLhs2;
|
|
1438
|
+
}
|
|
1439
|
+
});
|
|
1440
|
+
break;
|
|
1441
|
+
case "UpdateExpression":
|
|
1442
|
+
let newLhs = extractAssignment(exp.assigned);
|
|
1443
|
+
if (newLhs) {
|
|
1444
|
+
const i = exp.children.indexOf(exp.assigned);
|
|
1445
|
+
exp.assigned = exp.children[i] = newLhs;
|
|
1446
|
+
}
|
|
1447
|
+
break;
|
|
1448
|
+
}
|
|
1449
|
+
if (pre.length)
|
|
1450
|
+
exp.children.unshift(...pre);
|
|
1451
|
+
if (post.length)
|
|
1452
|
+
exp.children.push(...post);
|
|
1453
|
+
});
|
|
1454
|
+
}
|
|
1455
|
+
function attachPostfixStatementAsExpression(exp, post) {
|
|
1456
|
+
let clause;
|
|
1457
|
+
switch (post[1].type) {
|
|
1458
|
+
case "ForStatement":
|
|
1459
|
+
case "IterationStatement":
|
|
1460
|
+
case "DoStatement":
|
|
1461
|
+
clause = addPostfixStatement(exp, ...post);
|
|
1462
|
+
return {
|
|
1463
|
+
type: "IterationExpression",
|
|
1464
|
+
children: [clause],
|
|
1465
|
+
block: clause.block
|
|
1466
|
+
};
|
|
1467
|
+
case "IfStatement":
|
|
1468
|
+
clause = expressionizeIfClause(post[1], exp);
|
|
1469
|
+
return clause;
|
|
1470
|
+
default:
|
|
1471
|
+
throw new Error("Unknown postfix statement");
|
|
1472
|
+
}
|
|
1473
|
+
}
|
|
1474
|
+
function getPatternConditions(pattern, ref, conditions) {
|
|
1475
|
+
if (pattern.rest)
|
|
1476
|
+
return;
|
|
1477
|
+
switch (pattern.type) {
|
|
1478
|
+
case "ArrayBindingPattern": {
|
|
1479
|
+
const { elements, length } = pattern, hasRest = elements.some((e) => e.rest), comparator = hasRest ? " >= " : " === ", l = [comparator, (length - hasRest).toString()];
|
|
1480
|
+
conditions.push(
|
|
1481
|
+
["Array.isArray(", ref, ")"],
|
|
1482
|
+
[ref, ".length", l]
|
|
1483
|
+
);
|
|
1484
|
+
elements.forEach(({ children: [, e] }, i) => {
|
|
1485
|
+
const subRef = [ref, "[", i.toString(), "]"];
|
|
1486
|
+
getPatternConditions(e, subRef, conditions);
|
|
1487
|
+
});
|
|
1488
|
+
const postRest = pattern.children.find((c) => c?.blockPrefix);
|
|
1489
|
+
if (postRest) {
|
|
1490
|
+
const postElements = postRest.blockPrefix.children[1], { length: postLength } = postElements;
|
|
1491
|
+
postElements.forEach(({ children: [, e] }, i) => {
|
|
1492
|
+
const subRef = [ref, "[", ref, ".length - ", (postLength + i).toString(), "]"];
|
|
1493
|
+
getPatternConditions(e, subRef, conditions);
|
|
1494
|
+
});
|
|
1495
|
+
}
|
|
1496
|
+
break;
|
|
1497
|
+
}
|
|
1498
|
+
case "ObjectBindingPattern": {
|
|
1499
|
+
conditions.push(
|
|
1500
|
+
["typeof ", ref, " === 'object'"],
|
|
1501
|
+
[ref, " != null"]
|
|
1502
|
+
);
|
|
1503
|
+
pattern.properties.forEach((p) => {
|
|
1504
|
+
switch (p.type) {
|
|
1505
|
+
case "PinProperty":
|
|
1506
|
+
case "BindingProperty": {
|
|
1507
|
+
const { name, value } = p;
|
|
1508
|
+
let subRef;
|
|
1509
|
+
switch (name.type) {
|
|
1510
|
+
case "ComputedPropertyName":
|
|
1511
|
+
conditions.push([name.expression, " in ", ref]);
|
|
1512
|
+
subRef = [ref, name];
|
|
1513
|
+
break;
|
|
1514
|
+
case "Literal":
|
|
1515
|
+
case "StringLiteral":
|
|
1516
|
+
case "NumericLiteral":
|
|
1517
|
+
conditions.push([name, " in ", ref]);
|
|
1518
|
+
subRef = [ref, "[", name, "]"];
|
|
1519
|
+
break;
|
|
1520
|
+
default:
|
|
1521
|
+
conditions.push(["'", name, "' in ", ref]);
|
|
1522
|
+
subRef = [ref, ".", name];
|
|
1523
|
+
}
|
|
1524
|
+
if (value) {
|
|
1525
|
+
getPatternConditions(value, subRef, conditions);
|
|
1526
|
+
}
|
|
1527
|
+
break;
|
|
1528
|
+
}
|
|
1529
|
+
}
|
|
1530
|
+
});
|
|
1531
|
+
break;
|
|
1532
|
+
}
|
|
1533
|
+
case "ConditionFragment":
|
|
1534
|
+
conditions.push(
|
|
1535
|
+
[ref, " ", pattern.children]
|
|
1536
|
+
);
|
|
1537
|
+
break;
|
|
1538
|
+
case "RegularExpressionLiteral": {
|
|
1539
|
+
conditions.push(
|
|
1540
|
+
["typeof ", ref, " === 'string'"],
|
|
1541
|
+
[pattern, ".test(", ref, ")"]
|
|
1542
|
+
);
|
|
1543
|
+
break;
|
|
1544
|
+
}
|
|
1545
|
+
case "PinPattern":
|
|
1546
|
+
conditions.push([
|
|
1547
|
+
ref,
|
|
1548
|
+
" === ",
|
|
1549
|
+
pattern.identifier
|
|
1550
|
+
]);
|
|
1551
|
+
break;
|
|
1552
|
+
case "Literal":
|
|
1553
|
+
conditions.push([
|
|
1554
|
+
ref,
|
|
1555
|
+
" === ",
|
|
1556
|
+
pattern
|
|
1557
|
+
]);
|
|
1558
|
+
break;
|
|
1559
|
+
default:
|
|
1560
|
+
break;
|
|
1561
|
+
}
|
|
1562
|
+
}
|
|
1563
|
+
function elideMatchersFromArrayBindings(elements) {
|
|
1564
|
+
return elements.map((el) => {
|
|
1565
|
+
if (el.type === "BindingRestElement") {
|
|
1566
|
+
return ["", el, void 0];
|
|
1567
|
+
}
|
|
1568
|
+
const { children: [ws, e, sep] } = el;
|
|
1569
|
+
switch (e.type) {
|
|
587
1570
|
case "Literal":
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
1571
|
+
case "RegularExpressionLiteral":
|
|
1572
|
+
case "StringLiteral":
|
|
1573
|
+
case "PinPattern":
|
|
1574
|
+
return sep;
|
|
592
1575
|
default:
|
|
593
|
-
|
|
594
|
-
...exp,
|
|
595
|
-
children: [...pre, "(", exp.children, ")", post]
|
|
596
|
-
};
|
|
597
|
-
return {
|
|
598
|
-
type: "ParenthesizedExpression",
|
|
599
|
-
children: ["(", expression, ")"],
|
|
600
|
-
expression
|
|
601
|
-
};
|
|
1576
|
+
return [ws, nonMatcherBindings(e), sep];
|
|
602
1577
|
}
|
|
603
|
-
}
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
1578
|
+
});
|
|
1579
|
+
}
|
|
1580
|
+
function elideMatchersFromPropertyBindings(properties) {
|
|
1581
|
+
return properties.map((p) => {
|
|
1582
|
+
switch (p.type) {
|
|
1583
|
+
case "BindingProperty": {
|
|
1584
|
+
const { children, name, value } = p;
|
|
1585
|
+
const [ws] = children;
|
|
1586
|
+
const sep = children[children.length - 1];
|
|
1587
|
+
switch (value && value.type) {
|
|
1588
|
+
case "ArrayBindingPattern":
|
|
1589
|
+
case "ObjectBindingPattern":
|
|
1590
|
+
return {
|
|
1591
|
+
...p,
|
|
1592
|
+
children: [ws, name, ": ", nonMatcherBindings(value)]
|
|
1593
|
+
};
|
|
1594
|
+
case "Identifier":
|
|
1595
|
+
return p;
|
|
1596
|
+
case "Literal":
|
|
1597
|
+
case "RegularExpressionLiteral":
|
|
1598
|
+
case "StringLiteral":
|
|
1599
|
+
default:
|
|
1600
|
+
return {
|
|
1601
|
+
...p,
|
|
1602
|
+
children: [ws, name, sep]
|
|
1603
|
+
};
|
|
1604
|
+
}
|
|
1605
|
+
}
|
|
1606
|
+
case "PinProperty":
|
|
1607
|
+
case "BindingRestProperty":
|
|
1608
|
+
default:
|
|
1609
|
+
return p;
|
|
1610
|
+
}
|
|
1611
|
+
});
|
|
1612
|
+
}
|
|
1613
|
+
function nonMatcherBindings(pattern) {
|
|
1614
|
+
switch (pattern.type) {
|
|
1615
|
+
case "ArrayBindingPattern": {
|
|
1616
|
+
const elements = elideMatchersFromArrayBindings(pattern.elements);
|
|
1617
|
+
const children = ["[", elements, "]"];
|
|
609
1618
|
return {
|
|
610
|
-
|
|
1619
|
+
...pattern,
|
|
611
1620
|
children,
|
|
612
|
-
|
|
1621
|
+
elements
|
|
613
1622
|
};
|
|
614
1623
|
}
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
1624
|
+
case "PostRestBindingElements": {
|
|
1625
|
+
const els = elideMatchersFromArrayBindings(pattern.children[1]);
|
|
1626
|
+
return {
|
|
1627
|
+
...pattern,
|
|
1628
|
+
children: [
|
|
1629
|
+
pattern.children[0],
|
|
1630
|
+
els,
|
|
1631
|
+
...pattern.children.slice(2)
|
|
1632
|
+
]
|
|
1633
|
+
};
|
|
1634
|
+
}
|
|
1635
|
+
case "ObjectBindingPattern":
|
|
1636
|
+
return ["{", elideMatchersFromPropertyBindings(pattern.properties), "}"];
|
|
1637
|
+
default:
|
|
1638
|
+
return pattern;
|
|
1639
|
+
}
|
|
1640
|
+
}
|
|
1641
|
+
function aggregateDuplicateBindings(bindings, ReservedWord) {
|
|
1642
|
+
const props = gatherRecursiveAll(bindings, (n) => n.type === "BindingProperty");
|
|
1643
|
+
const arrayBindings = gatherRecursiveAll(bindings, (n) => n.type === "ArrayBindingPattern");
|
|
1644
|
+
arrayBindings.forEach((a) => {
|
|
1645
|
+
const { elements } = a;
|
|
1646
|
+
elements.forEach((element) => {
|
|
1647
|
+
if (Array.isArray(element)) {
|
|
1648
|
+
const [, e] = element;
|
|
1649
|
+
if (e.type === "Identifier") {
|
|
1650
|
+
props.push(e);
|
|
1651
|
+
} else if (e.type === "BindingRestElement") {
|
|
1652
|
+
props.push(e);
|
|
1653
|
+
}
|
|
1654
|
+
}
|
|
1655
|
+
});
|
|
1656
|
+
});
|
|
1657
|
+
const declarations = [];
|
|
1658
|
+
const propsGroupedByName = /* @__PURE__ */ new Map();
|
|
1659
|
+
for (const p of props) {
|
|
1660
|
+
const { name, value } = p;
|
|
1661
|
+
const key = value?.name || name?.name || name;
|
|
1662
|
+
if (propsGroupedByName.has(key)) {
|
|
1663
|
+
propsGroupedByName.get(key).push(p);
|
|
1664
|
+
} else {
|
|
1665
|
+
propsGroupedByName.set(key, [p]);
|
|
1666
|
+
}
|
|
1667
|
+
}
|
|
1668
|
+
propsGroupedByName.forEach((shared, key) => {
|
|
1669
|
+
if (!key)
|
|
1670
|
+
return;
|
|
1671
|
+
if (ReservedWord({
|
|
1672
|
+
pos: 0,
|
|
1673
|
+
input: key
|
|
1674
|
+
})) {
|
|
1675
|
+
shared.forEach((p) => {
|
|
1676
|
+
const ref = {
|
|
1677
|
+
type: "Ref",
|
|
1678
|
+
base: `_${key}`,
|
|
1679
|
+
id: key
|
|
1680
|
+
};
|
|
1681
|
+
aliasBinding(p, ref);
|
|
1682
|
+
});
|
|
1683
|
+
return;
|
|
1684
|
+
}
|
|
1685
|
+
if (shared.length === 1)
|
|
1686
|
+
return;
|
|
1687
|
+
const refs = shared.map((p) => {
|
|
1688
|
+
const ref = {
|
|
1689
|
+
type: "Ref",
|
|
1690
|
+
base: key,
|
|
1691
|
+
id: key
|
|
1692
|
+
};
|
|
1693
|
+
aliasBinding(p, ref);
|
|
1694
|
+
return ref;
|
|
1695
|
+
});
|
|
1696
|
+
declarations.push(["const ", key, " = [", ...refs.map((r, i) => {
|
|
1697
|
+
return i === 0 ? r : [", ", r];
|
|
1698
|
+
}), "]"]);
|
|
1699
|
+
});
|
|
1700
|
+
return declarations;
|
|
1701
|
+
}
|
|
1702
|
+
function processPatternMatching(statements, ReservedWord) {
|
|
1703
|
+
gatherRecursiveAll(statements, (n) => n.type === "SwitchStatement" || n.type === "SwitchExpression").forEach((s) => {
|
|
1704
|
+
const { caseBlock } = s;
|
|
1705
|
+
const { clauses } = caseBlock;
|
|
1706
|
+
let errors = false;
|
|
1707
|
+
let isPattern = false;
|
|
1708
|
+
if (clauses.some((c) => c.type === "PatternClause")) {
|
|
1709
|
+
isPattern = true;
|
|
1710
|
+
clauses.forEach((c) => {
|
|
1711
|
+
if (!(c.type === "PatternClause" || c.type === "DefaultClause")) {
|
|
1712
|
+
errors = true;
|
|
1713
|
+
c.children.push({
|
|
1714
|
+
type: "Error",
|
|
1715
|
+
message: "Can't mix pattern matching and non-pattern matching clauses"
|
|
1716
|
+
});
|
|
1717
|
+
}
|
|
1718
|
+
});
|
|
1719
|
+
}
|
|
1720
|
+
if (errors || !isPattern)
|
|
1721
|
+
return;
|
|
1722
|
+
let { expression } = s;
|
|
1723
|
+
if (expression.type === "ParenthesizedExpression") {
|
|
1724
|
+
expression = expression.expression;
|
|
1725
|
+
}
|
|
1726
|
+
let ref = needsRef(expression, "m") || expression;
|
|
1727
|
+
let hoistDec = ref !== expression ? [["", ["const ", ref, " = ", expression], ";"]] : void 0;
|
|
1728
|
+
let prev = [], root = prev;
|
|
1729
|
+
const l = clauses.length;
|
|
1730
|
+
clauses.forEach((c, i) => {
|
|
1731
|
+
if (c.type === "DefaultClause") {
|
|
1732
|
+
prev.push(c.block);
|
|
1733
|
+
return;
|
|
1734
|
+
}
|
|
1735
|
+
let { patterns, block } = c;
|
|
1736
|
+
let pattern = patterns[0];
|
|
1737
|
+
const indent = block.expressions?.[0]?.[0] || "";
|
|
1738
|
+
const alternativeConditions = patterns.map((pattern2, i2) => {
|
|
1739
|
+
const conditions = [];
|
|
1740
|
+
getPatternConditions(pattern2, ref, conditions);
|
|
1741
|
+
return conditions;
|
|
1742
|
+
});
|
|
1743
|
+
const conditionExpression = alternativeConditions.map((conditions, i2) => {
|
|
1744
|
+
const conditionArray = conditions.map((c2, i3) => {
|
|
1745
|
+
if (i3 === 0)
|
|
1746
|
+
return c2;
|
|
1747
|
+
return [" && ", ...c2];
|
|
1748
|
+
});
|
|
1749
|
+
if (i2 === 0)
|
|
1750
|
+
return conditionArray;
|
|
1751
|
+
return [" || ", ...conditionArray];
|
|
1752
|
+
});
|
|
1753
|
+
const condition = {
|
|
1754
|
+
type: "ParenthesizedExpression",
|
|
1755
|
+
children: ["(", conditionExpression, ")"],
|
|
1756
|
+
expression: conditionExpression
|
|
1757
|
+
};
|
|
1758
|
+
const prefix = [];
|
|
1759
|
+
switch (pattern.type) {
|
|
1760
|
+
case "ArrayBindingPattern":
|
|
1761
|
+
if (pattern.length === 0)
|
|
1762
|
+
break;
|
|
1763
|
+
case "ObjectBindingPattern": {
|
|
1764
|
+
if (pattern.properties?.length === 0)
|
|
1765
|
+
break;
|
|
1766
|
+
let [splices, thisAssignments] = gatherBindingCode(pattern);
|
|
1767
|
+
const patternBindings = nonMatcherBindings(pattern);
|
|
1768
|
+
splices = splices.map((s2) => [", ", nonMatcherBindings(s2)]);
|
|
1769
|
+
thisAssignments = thisAssignments.map((a) => [indent, a, ";"]);
|
|
1770
|
+
const duplicateDeclarations = aggregateDuplicateBindings([patternBindings, splices], ReservedWord);
|
|
1771
|
+
prefix.push([indent, "const ", patternBindings, " = ", ref, splices, ";"]);
|
|
1772
|
+
prefix.push(...thisAssignments);
|
|
1773
|
+
prefix.push(...duplicateDeclarations.map((d) => [indent, d, ";"]));
|
|
1774
|
+
break;
|
|
1775
|
+
}
|
|
1776
|
+
}
|
|
1777
|
+
block.expressions.unshift(...prefix);
|
|
1778
|
+
const next = [];
|
|
1779
|
+
if (block.bare) {
|
|
1780
|
+
block.children.unshift(" {");
|
|
1781
|
+
block.children.push("}");
|
|
1782
|
+
block.bare = false;
|
|
1783
|
+
}
|
|
1784
|
+
if (i < l - 1)
|
|
1785
|
+
next.push("\n", "else ");
|
|
1786
|
+
prev.push(["", {
|
|
1787
|
+
type: "IfStatement",
|
|
1788
|
+
children: ["if", condition, block, next],
|
|
1789
|
+
then: block,
|
|
1790
|
+
else: next,
|
|
1791
|
+
hoistDec
|
|
1792
|
+
}]);
|
|
1793
|
+
hoistDec = void 0;
|
|
1794
|
+
prev = next;
|
|
1795
|
+
});
|
|
1796
|
+
if (s.type === "SwitchExpression") {
|
|
1797
|
+
insertReturn(root[0]);
|
|
1798
|
+
root.splice(0, 1, wrapIIFE(root[0]));
|
|
1799
|
+
}
|
|
1800
|
+
s.type = "PatternMatchingStatement";
|
|
1801
|
+
s.children = [root];
|
|
1802
|
+
addParentPointers(s, s.parent);
|
|
1803
|
+
});
|
|
1804
|
+
}
|
|
1805
|
+
function processPipelineExpressions(statements) {
|
|
1806
|
+
gatherRecursiveAll(statements, (n) => n.type === "PipelineExpression").forEach((s) => {
|
|
1807
|
+
const [ws, , body] = s.children;
|
|
1808
|
+
let [, arg] = s.children;
|
|
1809
|
+
let i = 0, l = body.length;
|
|
1810
|
+
const refDec = [];
|
|
1811
|
+
const children = [ws, refDec];
|
|
1812
|
+
let usingRef = null;
|
|
1813
|
+
for (i = 0; i < l; i++) {
|
|
1814
|
+
const step = body[i];
|
|
1815
|
+
const [leadingComment, pipe, trailingComment, expr] = step;
|
|
1816
|
+
const returns = pipe.token === "||>";
|
|
1817
|
+
let ref, result, returning = returns ? arg : null;
|
|
1818
|
+
if (pipe.token === "|>=") {
|
|
1819
|
+
let initRef;
|
|
1820
|
+
if (i === 0) {
|
|
1821
|
+
outer:
|
|
1822
|
+
switch (arg.type) {
|
|
1823
|
+
case "MemberExpression":
|
|
1824
|
+
if (arg.children.length <= 2)
|
|
1825
|
+
break;
|
|
1826
|
+
case "CallExpression":
|
|
1827
|
+
const access = arg.children.pop();
|
|
1828
|
+
switch (access.type) {
|
|
1829
|
+
case "PropertyAccess":
|
|
1830
|
+
case "SliceExpression":
|
|
1831
|
+
break;
|
|
1832
|
+
default:
|
|
1833
|
+
children.unshift({
|
|
1834
|
+
type: "Error",
|
|
1835
|
+
$loc: pipe.token.$loc,
|
|
1836
|
+
message: `Can't assign to ${access.type}`
|
|
1837
|
+
});
|
|
1838
|
+
arg.children.push(access);
|
|
1839
|
+
break outer;
|
|
1840
|
+
}
|
|
1841
|
+
usingRef = needsRef({});
|
|
1842
|
+
initRef = {
|
|
1843
|
+
type: "AssignmentExpression",
|
|
1844
|
+
children: [usingRef, " = ", arg, ","]
|
|
1845
|
+
};
|
|
1846
|
+
arg = {
|
|
1847
|
+
type: "MemberExpression",
|
|
1848
|
+
children: [usingRef, access]
|
|
1849
|
+
};
|
|
1850
|
+
break;
|
|
1851
|
+
}
|
|
1852
|
+
children.pop();
|
|
1853
|
+
const lhs = [[
|
|
1854
|
+
[refDec, initRef],
|
|
1855
|
+
arg,
|
|
1856
|
+
[],
|
|
1857
|
+
{ token: "=", children: [" = "] }
|
|
1858
|
+
]];
|
|
1859
|
+
Object.assign(s, {
|
|
1860
|
+
type: "AssignmentExpression",
|
|
1861
|
+
children: [lhs, children],
|
|
1862
|
+
names: null,
|
|
1863
|
+
lhs,
|
|
1864
|
+
assigned: arg,
|
|
1865
|
+
exp: children
|
|
1866
|
+
});
|
|
1867
|
+
arg = clone(arg);
|
|
1868
|
+
if (arg.children[0].type === "Ref") {
|
|
1869
|
+
arg.children[0] = usingRef;
|
|
1870
|
+
}
|
|
1871
|
+
} else {
|
|
1872
|
+
children.unshift({
|
|
1873
|
+
type: "Error",
|
|
1874
|
+
$loc: pipe.token.$loc,
|
|
1875
|
+
message: "Can't use |>= in the middle of a pipeline"
|
|
1876
|
+
});
|
|
1877
|
+
}
|
|
1878
|
+
} else {
|
|
1879
|
+
s.children = children;
|
|
1880
|
+
}
|
|
1881
|
+
if (returns && (ref = needsRef(arg))) {
|
|
1882
|
+
usingRef = usingRef || ref;
|
|
1883
|
+
arg = {
|
|
1884
|
+
type: "ParenthesizedExpression",
|
|
1885
|
+
children: ["(", {
|
|
1886
|
+
type: "AssignmentExpression",
|
|
1887
|
+
children: [usingRef, " = ", arg]
|
|
1888
|
+
}, ")"]
|
|
1889
|
+
};
|
|
1890
|
+
returning = usingRef;
|
|
1891
|
+
}
|
|
1892
|
+
[result, returning] = constructPipeStep(
|
|
1893
|
+
{
|
|
1894
|
+
leadingComment: skipIfOnlyWS(leadingComment),
|
|
1895
|
+
trailingComment: skipIfOnlyWS(trailingComment),
|
|
1896
|
+
expr
|
|
1897
|
+
},
|
|
1898
|
+
arg,
|
|
1899
|
+
returning
|
|
1900
|
+
);
|
|
1901
|
+
if (result.type === "ReturnStatement") {
|
|
1902
|
+
if (i < l - 1) {
|
|
1903
|
+
result.children.push({
|
|
1904
|
+
type: "Error",
|
|
1905
|
+
message: "Can't continue a pipeline after returning"
|
|
1906
|
+
});
|
|
1907
|
+
}
|
|
1908
|
+
arg = result;
|
|
1909
|
+
if (children[children.length - 1] === ",") {
|
|
1910
|
+
children.pop();
|
|
1911
|
+
children.push(";");
|
|
1912
|
+
}
|
|
1913
|
+
break;
|
|
1914
|
+
}
|
|
1915
|
+
if (returning) {
|
|
1916
|
+
arg = returning;
|
|
1917
|
+
children.push(result, ",");
|
|
1918
|
+
} else {
|
|
1919
|
+
arg = result;
|
|
1920
|
+
}
|
|
1921
|
+
}
|
|
1922
|
+
if (usingRef) {
|
|
1923
|
+
refDec.unshift("let ", usingRef, ";");
|
|
1924
|
+
}
|
|
1925
|
+
children.push(arg);
|
|
1926
|
+
addParentPointers(s, s.parent);
|
|
1927
|
+
});
|
|
1928
|
+
}
|
|
1929
|
+
function processProgram(root, config, m, ReservedWord) {
|
|
1930
|
+
assert.equal(m.forbidClassImplicitCall.length, 1, "forbidClassImplicitCall");
|
|
1931
|
+
assert.equal(m.forbidIndentedApplication.length, 1, "forbidIndentedApplication");
|
|
1932
|
+
assert.equal(m.forbidTrailingMemberProperty.length, 1, "forbidTrailingMemberProperty");
|
|
1933
|
+
assert.equal(m.forbidMultiLineImplicitObjectLiteral.length, 1, "forbidMultiLineImplicitObjectLiteral");
|
|
1934
|
+
assert.equal(m.JSXTagStack.length, 0, "JSXTagStack should be empty");
|
|
1935
|
+
addParentPointers(root);
|
|
1936
|
+
const { expressions: statements } = root;
|
|
1937
|
+
processPipelineExpressions(statements);
|
|
1938
|
+
processAssignments(statements);
|
|
1939
|
+
processPatternMatching(statements, ReservedWord);
|
|
1940
|
+
processFunctions(statements, config);
|
|
1941
|
+
processSwitchExpressions(statements);
|
|
1942
|
+
processTryExpressions(statements);
|
|
1943
|
+
hoistRefDecs(statements);
|
|
1944
|
+
gatherRecursiveAll(statements, (n) => n.type === "IterationExpression").forEach((e) => expressionizeIteration(e));
|
|
1945
|
+
statements.unshift(...m.prelude);
|
|
1946
|
+
if (config.autoLet) {
|
|
1947
|
+
createLetDecs(statements, []);
|
|
1948
|
+
} else if (config.autoVar) {
|
|
1949
|
+
createVarDecs(statements, []);
|
|
1950
|
+
}
|
|
1951
|
+
populateRefs(statements);
|
|
1952
|
+
adjustAtBindings(statements);
|
|
1953
|
+
}
|
|
1954
|
+
function findDecs(statements) {
|
|
1955
|
+
const declarationNames = gatherNodes(statements, ({ type }) => type === "Declaration").flatMap((d) => d.names);
|
|
1956
|
+
return new Set(declarationNames);
|
|
1957
|
+
}
|
|
1958
|
+
function populateRefs(statements) {
|
|
1959
|
+
const refNodes = gatherRecursive(statements, ({ type }) => type === "Ref");
|
|
1960
|
+
if (refNodes.length) {
|
|
1961
|
+
const ids = gatherRecursive(statements, (s) => s.type === "Identifier");
|
|
1962
|
+
const names = new Set(ids.flatMap(({ names: names2 }) => names2 || []));
|
|
1963
|
+
refNodes.forEach((ref) => {
|
|
1964
|
+
const { type, base } = ref;
|
|
1965
|
+
if (type !== "Ref")
|
|
1966
|
+
return;
|
|
1967
|
+
ref.type = "Identifier";
|
|
1968
|
+
let n = 0;
|
|
1969
|
+
let name = base;
|
|
1970
|
+
while (names.has(name)) {
|
|
1971
|
+
n++;
|
|
1972
|
+
name = `${base}${n}`;
|
|
1973
|
+
}
|
|
1974
|
+
names.add(name);
|
|
1975
|
+
ref.children = ref.names = [name];
|
|
1976
|
+
});
|
|
1977
|
+
}
|
|
1978
|
+
}
|
|
1979
|
+
function createVarDecs(statements, scopes, pushVar) {
|
|
1980
|
+
function hasDec(name) {
|
|
1981
|
+
return scopes.some((s) => s.has(name));
|
|
1982
|
+
}
|
|
1983
|
+
function findAssignments(statements2, decs2) {
|
|
1984
|
+
let assignmentStatements2 = gatherNodes(statements2, (node) => {
|
|
1985
|
+
return node.type === "AssignmentExpression";
|
|
1986
|
+
});
|
|
1987
|
+
if (assignmentStatements2.length) {
|
|
1988
|
+
assignmentStatements2 = assignmentStatements2.concat(findAssignments(assignmentStatements2.map((s) => s.children), decs2));
|
|
1989
|
+
}
|
|
1990
|
+
return assignmentStatements2;
|
|
1991
|
+
}
|
|
1992
|
+
if (!pushVar) {
|
|
1993
|
+
pushVar = function(name) {
|
|
1994
|
+
varIds.push(name);
|
|
1995
|
+
decs.add(name);
|
|
1996
|
+
};
|
|
1997
|
+
}
|
|
1998
|
+
const decs = findDecs(statements);
|
|
1999
|
+
scopes.push(decs);
|
|
2000
|
+
const varIds = [];
|
|
2001
|
+
const assignmentStatements = findAssignments(statements, scopes);
|
|
2002
|
+
const undeclaredIdentifiers = assignmentStatements.flatMap((a) => a.names);
|
|
2003
|
+
undeclaredIdentifiers.filter((x, i, a) => {
|
|
2004
|
+
if (!hasDec(x))
|
|
2005
|
+
return a.indexOf(x) === i;
|
|
2006
|
+
}).forEach(pushVar);
|
|
2007
|
+
const fnNodes = gatherNodes(statements, (s) => s.type === "FunctionExpression");
|
|
2008
|
+
const forNodes = gatherNodes(statements, (s) => s.type === "ForStatement");
|
|
2009
|
+
const blockNodes = new Set(gatherNodes(statements, (s) => s.type === "BlockStatement"));
|
|
2010
|
+
fnNodes.forEach(({ block }) => blockNodes.delete(block));
|
|
2011
|
+
forNodes.forEach(({ block }) => blockNodes.delete(block));
|
|
2012
|
+
blockNodes.forEach((block) => {
|
|
2013
|
+
createVarDecs(block.expressions, scopes, pushVar);
|
|
2014
|
+
});
|
|
2015
|
+
forNodes.forEach(({ block, declaration }) => {
|
|
2016
|
+
scopes.push(new Set(declaration.names));
|
|
2017
|
+
createVarDecs(block.expressions, scopes, pushVar);
|
|
2018
|
+
scopes.pop();
|
|
2019
|
+
});
|
|
2020
|
+
fnNodes.forEach(({ block, parameters }) => {
|
|
2021
|
+
scopes.push(new Set(parameters.names));
|
|
2022
|
+
createVarDecs(block.expressions, scopes);
|
|
2023
|
+
scopes.pop();
|
|
2024
|
+
});
|
|
2025
|
+
if (varIds.length) {
|
|
2026
|
+
const indent = getIndent(statements[0]);
|
|
2027
|
+
let delimiter = ";";
|
|
2028
|
+
if (statements[0][1]?.parent?.root) {
|
|
2029
|
+
delimiter = ";\n";
|
|
2030
|
+
}
|
|
2031
|
+
statements.unshift([indent, "var ", varIds.join(", "), delimiter]);
|
|
2032
|
+
}
|
|
2033
|
+
scopes.pop();
|
|
2034
|
+
}
|
|
2035
|
+
function createLetDecs(statements, scopes) {
|
|
2036
|
+
function findVarDecs(statements2, decs) {
|
|
2037
|
+
const declarationNames = gatherRecursive(
|
|
2038
|
+
statements2,
|
|
2039
|
+
(node) => node.type === "Declaration" && node.children && node.children.length > 0 && node.children[0].token && node.children[0].token.startsWith("var") || node.type === "FunctionExpression"
|
|
2040
|
+
).filter((node) => node.type === "Declaration").flatMap((node) => node.names);
|
|
2041
|
+
return new Set(declarationNames);
|
|
2042
|
+
}
|
|
2043
|
+
let declaredIdentifiers = findVarDecs(statements);
|
|
2044
|
+
function hasDec(name) {
|
|
2045
|
+
return declaredIdentifiers.has(name) || scopes.some((s) => s.has(name));
|
|
2046
|
+
}
|
|
2047
|
+
function gatherBlockOrOther(statement) {
|
|
2048
|
+
return gatherNodes(statement, (s) => s.type === "BlockStatement" || s.type === "AssignmentExpression" || s.type === "Declaration").flatMap((node) => {
|
|
2049
|
+
if (node.type == "BlockStatement")
|
|
2050
|
+
return node.bare ? gatherBlockOrOther(node.expressions) : node;
|
|
2051
|
+
else if (node.children && node.children.length)
|
|
2052
|
+
return [...gatherBlockOrOther(node.children), node];
|
|
2053
|
+
else
|
|
2054
|
+
return [];
|
|
2055
|
+
});
|
|
2056
|
+
}
|
|
2057
|
+
let currentScope = /* @__PURE__ */ new Set();
|
|
2058
|
+
scopes.push(currentScope);
|
|
2059
|
+
const fnNodes = gatherNodes(statements, (s) => s.type === "FunctionExpression");
|
|
2060
|
+
const forNodes = gatherNodes(statements, (s) => s.type === "ForStatement");
|
|
2061
|
+
let targetStatements = [];
|
|
2062
|
+
for (const statement of statements) {
|
|
2063
|
+
const nodes = gatherBlockOrOther(statement);
|
|
2064
|
+
let undeclaredIdentifiers = [];
|
|
2065
|
+
for (const node of nodes) {
|
|
2066
|
+
if (node.type == "BlockStatement") {
|
|
2067
|
+
let block = node;
|
|
2068
|
+
let fnNode = fnNodes.find((fnNode2) => fnNode2.block === block);
|
|
2069
|
+
let forNode = forNodes.find((forNode2) => forNode2.block === block);
|
|
2070
|
+
if (fnNode != null) {
|
|
2071
|
+
scopes.push(new Set(fnNode.parameters.names));
|
|
2072
|
+
createLetDecs(block.expressions, scopes);
|
|
2073
|
+
scopes.pop();
|
|
2074
|
+
} else if (forNode != null) {
|
|
2075
|
+
scopes.push(new Set(forNode.declaration.names));
|
|
2076
|
+
createLetDecs(block.expressions, scopes);
|
|
2077
|
+
scopes.pop();
|
|
2078
|
+
} else
|
|
2079
|
+
createLetDecs(block.expressions, scopes);
|
|
2080
|
+
continue;
|
|
2081
|
+
}
|
|
2082
|
+
if (node.names == null)
|
|
2083
|
+
continue;
|
|
2084
|
+
let names = node.names.filter((name) => !hasDec(name));
|
|
2085
|
+
if (node.type == "AssignmentExpression")
|
|
2086
|
+
undeclaredIdentifiers.push(...names);
|
|
2087
|
+
names.forEach((name) => currentScope.add(name));
|
|
2088
|
+
}
|
|
2089
|
+
if (undeclaredIdentifiers.length > 0) {
|
|
2090
|
+
let indent = statement[0];
|
|
2091
|
+
let firstIdentifier = gatherNodes(statement[1], (node) => node.type == "Identifier")[0];
|
|
2092
|
+
if (undeclaredIdentifiers.length == 1 && statement[1].type == "AssignmentExpression" && statement[1].names.length == 1 && statement[1].names[0] == undeclaredIdentifiers[0] && firstIdentifier && firstIdentifier.names == undeclaredIdentifiers[0] && gatherNodes(statement[1], (node) => node.type === "ObjectBindingPattern").length == 0)
|
|
2093
|
+
statement[1].children.unshift(["let "]);
|
|
2094
|
+
else {
|
|
2095
|
+
let tail = "\n";
|
|
2096
|
+
if (gatherNodes(indent, (node) => node.token && node.token.endsWith("\n")).length > 0)
|
|
2097
|
+
tail = void 0;
|
|
2098
|
+
targetStatements.push([indent, "let ", undeclaredIdentifiers.join(", "), tail]);
|
|
2099
|
+
}
|
|
2100
|
+
}
|
|
2101
|
+
targetStatements.push(statement);
|
|
2102
|
+
}
|
|
2103
|
+
scopes.pop();
|
|
2104
|
+
statements.splice(0, statements.length, targetStatements);
|
|
2105
|
+
}
|
|
2106
|
+
function processReturnValue(func) {
|
|
2107
|
+
const { block } = func;
|
|
2108
|
+
const values = gatherRecursiveWithinFunction(
|
|
2109
|
+
block,
|
|
2110
|
+
({ type }) => type === "ReturnValue"
|
|
2111
|
+
);
|
|
2112
|
+
if (!values.length)
|
|
2113
|
+
return false;
|
|
2114
|
+
const ref = {
|
|
2115
|
+
type: "Ref",
|
|
2116
|
+
base: "ret",
|
|
2117
|
+
id: "ret"
|
|
2118
|
+
};
|
|
2119
|
+
let declared;
|
|
2120
|
+
values.forEach((value) => {
|
|
2121
|
+
value.children = [ref];
|
|
2122
|
+
const ancestor = findAncestor(
|
|
2123
|
+
value,
|
|
2124
|
+
({ type }) => type === "Declaration",
|
|
2125
|
+
isFunction
|
|
2126
|
+
);
|
|
2127
|
+
if (ancestor)
|
|
2128
|
+
declared = true;
|
|
2129
|
+
});
|
|
2130
|
+
if (!declared) {
|
|
2131
|
+
let returnType = func.returnType ?? func.signature?.returnType;
|
|
2132
|
+
if (returnType) {
|
|
2133
|
+
const { t } = returnType;
|
|
2134
|
+
if (t.type === "TypePredicate") {
|
|
2135
|
+
returnType = ": boolean";
|
|
2136
|
+
} else if (t.type === "AssertsType") {
|
|
2137
|
+
returnType = void 0;
|
|
2138
|
+
}
|
|
2139
|
+
}
|
|
2140
|
+
block.expressions.unshift([
|
|
2141
|
+
getIndent(block.expressions[0]),
|
|
2142
|
+
{
|
|
2143
|
+
type: "Declaration",
|
|
2144
|
+
children: ["let ", ref, returnType],
|
|
2145
|
+
names: []
|
|
2146
|
+
},
|
|
2147
|
+
";"
|
|
2148
|
+
]);
|
|
2149
|
+
}
|
|
2150
|
+
gatherRecursiveWithinFunction(
|
|
2151
|
+
block,
|
|
2152
|
+
(r) => r.type === "ReturnStatement" && !r.expression
|
|
2153
|
+
).forEach((r) => {
|
|
2154
|
+
r.expression = ref;
|
|
2155
|
+
r.children.splice(-1, 1, " ", ref);
|
|
2156
|
+
});
|
|
2157
|
+
if (block.children.at(-2)?.type !== "ReturnStatement") {
|
|
2158
|
+
block.expressions.push([
|
|
2159
|
+
[getIndent(block.expressions.at(-1))],
|
|
2160
|
+
{
|
|
2161
|
+
type: "ReturnStatement",
|
|
2162
|
+
expression: ref,
|
|
2163
|
+
children: ["return ", ref]
|
|
2164
|
+
}
|
|
2165
|
+
]);
|
|
2166
|
+
}
|
|
2167
|
+
return true;
|
|
2168
|
+
}
|
|
2169
|
+
function processUnaryExpression(pre, exp, post) {
|
|
2170
|
+
if (!(pre.length || post))
|
|
2171
|
+
return exp;
|
|
2172
|
+
if (post?.token === "?") {
|
|
2173
|
+
post = {
|
|
2174
|
+
$loc: post.$loc,
|
|
2175
|
+
token: " != null"
|
|
2176
|
+
};
|
|
2177
|
+
switch (exp.type) {
|
|
2178
|
+
case "Identifier":
|
|
2179
|
+
case "Literal":
|
|
2180
|
+
case "AmpersandRef":
|
|
2181
|
+
return {
|
|
2182
|
+
...exp,
|
|
2183
|
+
children: [...pre, ...exp.children, post]
|
|
2184
|
+
};
|
|
2185
|
+
default:
|
|
2186
|
+
const expression = {
|
|
2187
|
+
...exp,
|
|
2188
|
+
children: [...pre, "(", exp.children, ")", post]
|
|
2189
|
+
};
|
|
2190
|
+
return {
|
|
2191
|
+
type: "ParenthesizedExpression",
|
|
2192
|
+
children: ["(", expression, ")"],
|
|
2193
|
+
expression
|
|
2194
|
+
};
|
|
2195
|
+
}
|
|
2196
|
+
}
|
|
2197
|
+
if (exp.type === "Literal") {
|
|
2198
|
+
if (pre.length === 1 && pre[0].token === "-") {
|
|
2199
|
+
const children = [pre[0], ...exp.children];
|
|
2200
|
+
if (post)
|
|
2201
|
+
exp.children.push(post);
|
|
2202
|
+
return {
|
|
2203
|
+
type: "Literal",
|
|
2204
|
+
children,
|
|
2205
|
+
raw: `-${exp.raw}`
|
|
2206
|
+
};
|
|
2207
|
+
}
|
|
2208
|
+
}
|
|
2209
|
+
const l = pre.length;
|
|
2210
|
+
if (l) {
|
|
2211
|
+
const last = pre[l - 1];
|
|
2212
|
+
if (last.type === "Await" && last.op) {
|
|
2213
|
+
if (exp.type !== "ParenthesizedExpression") {
|
|
2214
|
+
exp = ["(", exp, ")"];
|
|
2215
|
+
}
|
|
2216
|
+
exp = {
|
|
2217
|
+
type: "CallExpression",
|
|
2218
|
+
children: [" Promise", last.op, exp]
|
|
626
2219
|
};
|
|
627
2220
|
}
|
|
628
2221
|
}
|
|
@@ -631,14 +2224,198 @@ var require_lib = __commonJS({
|
|
|
631
2224
|
children: [...pre, exp, post]
|
|
632
2225
|
};
|
|
633
2226
|
}
|
|
2227
|
+
function prune2(node) {
|
|
2228
|
+
if (node === null || node === void 0)
|
|
2229
|
+
return;
|
|
2230
|
+
if (node.length === 0)
|
|
2231
|
+
return;
|
|
2232
|
+
if (Array.isArray(node)) {
|
|
2233
|
+
const a = node.map((n) => prune2(n)).filter((n) => !!n);
|
|
2234
|
+
if (a.length > 1)
|
|
2235
|
+
return a;
|
|
2236
|
+
if (a.length === 1)
|
|
2237
|
+
return a[0];
|
|
2238
|
+
return;
|
|
2239
|
+
}
|
|
2240
|
+
if (node.children != null) {
|
|
2241
|
+
node.children = prune2(node.children);
|
|
2242
|
+
return node;
|
|
2243
|
+
}
|
|
2244
|
+
return node;
|
|
2245
|
+
}
|
|
2246
|
+
function reorderBindingRestProperty(props) {
|
|
2247
|
+
const names = props.flatMap((p) => p.names);
|
|
2248
|
+
let restIndex = -1;
|
|
2249
|
+
let restCount = 0;
|
|
2250
|
+
props.forEach(({ type }, i) => {
|
|
2251
|
+
if (type === "BindingRestProperty") {
|
|
2252
|
+
if (restIndex < 0)
|
|
2253
|
+
restIndex = i;
|
|
2254
|
+
restCount++;
|
|
2255
|
+
}
|
|
2256
|
+
});
|
|
2257
|
+
if (restCount === 0) {
|
|
2258
|
+
return {
|
|
2259
|
+
children: props,
|
|
2260
|
+
names
|
|
2261
|
+
};
|
|
2262
|
+
} else if (restCount === 1) {
|
|
2263
|
+
let after = props.slice(restIndex + 1);
|
|
2264
|
+
let rest = props[restIndex];
|
|
2265
|
+
props = props.slice(0, restIndex);
|
|
2266
|
+
if (after.length) {
|
|
2267
|
+
const [restDelim] = rest.children.slice(-1), lastAfterProp = after[after.length - 1], lastAfterChildren = lastAfterProp.children, [lastDelim] = lastAfterChildren.slice(-1);
|
|
2268
|
+
rest = { ...rest, children: [...rest.children.slice(0, -1), lastDelim] };
|
|
2269
|
+
after = [...after.slice(0, -1), { ...lastAfterProp, children: [...lastAfterChildren.slice(0, -1), restDelim] }];
|
|
2270
|
+
}
|
|
2271
|
+
const children = [...props, ...after, rest];
|
|
2272
|
+
return {
|
|
2273
|
+
children,
|
|
2274
|
+
names
|
|
2275
|
+
};
|
|
2276
|
+
}
|
|
2277
|
+
return {
|
|
2278
|
+
children: [{
|
|
2279
|
+
type: "Error",
|
|
2280
|
+
message: "Multiple rest properties in object pattern"
|
|
2281
|
+
}, props]
|
|
2282
|
+
};
|
|
2283
|
+
}
|
|
2284
|
+
function replaceNodes(root, predicate, replacer) {
|
|
2285
|
+
if (root == null)
|
|
2286
|
+
return root;
|
|
2287
|
+
const array = Array.isArray(root) ? root : root.children;
|
|
2288
|
+
if (!array)
|
|
2289
|
+
return root;
|
|
2290
|
+
array.forEach((node, i) => {
|
|
2291
|
+
if (node == null)
|
|
2292
|
+
return;
|
|
2293
|
+
if (predicate(node)) {
|
|
2294
|
+
array[i] = replacer(node, root);
|
|
2295
|
+
} else {
|
|
2296
|
+
replaceNodes(node, predicate, replacer);
|
|
2297
|
+
}
|
|
2298
|
+
});
|
|
2299
|
+
return root;
|
|
2300
|
+
}
|
|
2301
|
+
function skipIfOnlyWS(target) {
|
|
2302
|
+
if (!target)
|
|
2303
|
+
return target;
|
|
2304
|
+
if (Array.isArray(target)) {
|
|
2305
|
+
if (target.length === 1) {
|
|
2306
|
+
return skipIfOnlyWS(target[0]);
|
|
2307
|
+
} else if (target.every((e) => skipIfOnlyWS(e) === void 0)) {
|
|
2308
|
+
return void 0;
|
|
2309
|
+
}
|
|
2310
|
+
return target;
|
|
2311
|
+
}
|
|
2312
|
+
if (target.token != null && target.token.trim() === "") {
|
|
2313
|
+
return void 0;
|
|
2314
|
+
}
|
|
2315
|
+
return target;
|
|
2316
|
+
}
|
|
2317
|
+
function typeOfJSX(node, config, getRef) {
|
|
2318
|
+
switch (node.type) {
|
|
2319
|
+
case "JSXElement":
|
|
2320
|
+
return typeOfJSXElement(node, config, getRef);
|
|
2321
|
+
case "JSXFragment":
|
|
2322
|
+
return typeOfJSXFragment(node, config, getRef);
|
|
2323
|
+
}
|
|
2324
|
+
}
|
|
2325
|
+
function typeOfJSXElement(node, config, getRef) {
|
|
2326
|
+
if (config.solid) {
|
|
2327
|
+
if (config.server && !config.client) {
|
|
2328
|
+
return ["string"];
|
|
2329
|
+
}
|
|
2330
|
+
let { tag } = node;
|
|
2331
|
+
const clientType = tag[0] === tag[0].toLowerCase() ? [getRef("IntrinsicElements"), '<"', tag, '">'] : ["ReturnType<typeof ", tag, ">"];
|
|
2332
|
+
if (config.server) {
|
|
2333
|
+
return ["string", " | ", clientType];
|
|
2334
|
+
} else {
|
|
2335
|
+
return clientType;
|
|
2336
|
+
}
|
|
2337
|
+
}
|
|
2338
|
+
}
|
|
2339
|
+
function typeOfJSXFragment(node, config, getRef) {
|
|
2340
|
+
if (config.solid) {
|
|
2341
|
+
let type = [];
|
|
2342
|
+
let lastType;
|
|
2343
|
+
for (let child of node.jsxChildren) {
|
|
2344
|
+
switch (child.type) {
|
|
2345
|
+
case "JSXText":
|
|
2346
|
+
if (lastType !== "JSXText") {
|
|
2347
|
+
type.push("string");
|
|
2348
|
+
}
|
|
2349
|
+
break;
|
|
2350
|
+
case "JSXElement":
|
|
2351
|
+
type.push(typeOfJSXElement(child, config, getRef));
|
|
2352
|
+
break;
|
|
2353
|
+
case "JSXFragment":
|
|
2354
|
+
type.push(...typeOfJSXFragment(child, config, getRef));
|
|
2355
|
+
break;
|
|
2356
|
+
case "JSXChildExpression":
|
|
2357
|
+
if (child.expression) {
|
|
2358
|
+
type.push(["typeof ", child.expression]);
|
|
2359
|
+
}
|
|
2360
|
+
break;
|
|
2361
|
+
default:
|
|
2362
|
+
throw new Error(`unknown child in JSXFragment: ${JSON.stringify(child)}`);
|
|
2363
|
+
}
|
|
2364
|
+
lastType = child.type;
|
|
2365
|
+
}
|
|
2366
|
+
if (type.length === 1) {
|
|
2367
|
+
return type[0];
|
|
2368
|
+
} else {
|
|
2369
|
+
type = type.flatMap((t) => [t, ", "]);
|
|
2370
|
+
type.pop();
|
|
2371
|
+
return ["[", type, "]"];
|
|
2372
|
+
}
|
|
2373
|
+
}
|
|
2374
|
+
}
|
|
2375
|
+
function wrapIIFE(exp, async) {
|
|
2376
|
+
let prefix, suffix;
|
|
2377
|
+
if (async) {
|
|
2378
|
+
prefix = "(async ()=>";
|
|
2379
|
+
suffix = ")()";
|
|
2380
|
+
} else if (hasAwait(exp)) {
|
|
2381
|
+
prefix = "(await (async ()=>";
|
|
2382
|
+
suffix = ")())";
|
|
2383
|
+
} else {
|
|
2384
|
+
prefix = "(()=>";
|
|
2385
|
+
suffix = ")()";
|
|
2386
|
+
}
|
|
2387
|
+
const expressions = Array.isArray(exp) ? [[...exp]] : [exp];
|
|
2388
|
+
const block = {
|
|
2389
|
+
type: "BlockStatement",
|
|
2390
|
+
expressions,
|
|
2391
|
+
children: ["{", expressions, "}"],
|
|
2392
|
+
bare: false
|
|
2393
|
+
};
|
|
2394
|
+
return [
|
|
2395
|
+
prefix,
|
|
2396
|
+
block,
|
|
2397
|
+
suffix
|
|
2398
|
+
];
|
|
2399
|
+
}
|
|
634
2400
|
module.exports = {
|
|
2401
|
+
addParentPointers,
|
|
2402
|
+
addPostfixStatement,
|
|
2403
|
+
adjustAtBindings,
|
|
2404
|
+
adjustBindingElements,
|
|
635
2405
|
aliasBinding,
|
|
636
2406
|
arrayElementHasTrailingComma,
|
|
2407
|
+
attachPostfixStatementAsExpression,
|
|
637
2408
|
blockWithPrefix,
|
|
638
2409
|
clone,
|
|
2410
|
+
constructInvocation,
|
|
2411
|
+
constructPipeStep,
|
|
639
2412
|
convertMethodToFunction,
|
|
640
2413
|
convertObjectToJSXAttributes,
|
|
2414
|
+
dedentBlockString,
|
|
2415
|
+
dedentBlockSubstitutions,
|
|
641
2416
|
deepCopy,
|
|
2417
|
+
expressionizeIfClause,
|
|
2418
|
+
expressionizeIteration,
|
|
642
2419
|
findAncestor,
|
|
643
2420
|
forRange,
|
|
644
2421
|
gatherBindingCode,
|
|
@@ -651,17 +2428,36 @@ var require_lib = __commonJS({
|
|
|
651
2428
|
hasAwait,
|
|
652
2429
|
hasYield,
|
|
653
2430
|
hoistRefDecs,
|
|
2431
|
+
insertReturn,
|
|
2432
|
+
insertSwitchReturns,
|
|
654
2433
|
insertTrimmingSpace,
|
|
2434
|
+
isEmptyBareBlock,
|
|
655
2435
|
isFunction,
|
|
2436
|
+
isVoidType,
|
|
2437
|
+
isWhitespaceOrEmpty,
|
|
656
2438
|
lastAccessInCallExpression,
|
|
657
2439
|
literalValue,
|
|
2440
|
+
makeAsConst,
|
|
2441
|
+
makeLeftHandSideExpression,
|
|
658
2442
|
modifyString,
|
|
2443
|
+
needsRef,
|
|
2444
|
+
processBinaryOpExpression,
|
|
2445
|
+
processCallMemberExpression,
|
|
659
2446
|
processCoffeeInterpolation,
|
|
660
2447
|
processConstAssignmentDeclaration,
|
|
661
2448
|
processLetAssignmentDeclaration,
|
|
2449
|
+
processParams,
|
|
2450
|
+
processProgram,
|
|
2451
|
+
processReturnValue,
|
|
662
2452
|
processUnaryExpression,
|
|
2453
|
+
prune: prune2,
|
|
663
2454
|
quoteString,
|
|
664
|
-
removeParentPointers
|
|
2455
|
+
removeParentPointers,
|
|
2456
|
+
reorderBindingRestProperty,
|
|
2457
|
+
replaceNodes,
|
|
2458
|
+
skipIfOnlyWS,
|
|
2459
|
+
typeOfJSX,
|
|
2460
|
+
wrapIIFE
|
|
665
2461
|
};
|
|
666
2462
|
}
|
|
667
2463
|
});
|
|
@@ -1956,13 +3752,13 @@ ${input.slice(result.pos)}
|
|
|
1956
3752
|
var $R65 = $R(new RegExp("[ \\t]*", "suy"));
|
|
1957
3753
|
var Program$0 = $TS($S(Reset, Init, $E(EOS), TopLevelStatements, __), function($skip, $loc, $0, $1, $2, $3, $4, $5) {
|
|
1958
3754
|
var statements = $4;
|
|
1959
|
-
|
|
3755
|
+
processProgram({
|
|
1960
3756
|
type: "BlockStatement",
|
|
1961
3757
|
expressions: statements,
|
|
1962
3758
|
children: [statements],
|
|
1963
3759
|
bare: true,
|
|
1964
3760
|
root: true
|
|
1965
|
-
});
|
|
3761
|
+
}, module.config, module, ReservedWord);
|
|
1966
3762
|
return $0;
|
|
1967
3763
|
});
|
|
1968
3764
|
function Program(state) {
|
|
@@ -2337,7 +4133,7 @@ ${input.slice(result.pos)}
|
|
|
2337
4133
|
var ws = $4;
|
|
2338
4134
|
var args = $5;
|
|
2339
4135
|
var close = $6;
|
|
2340
|
-
if (args.length === 1 && args[0].type === "IterationExpression" && args[0].subtype !== "DoStatement" && !args[0].async &&
|
|
4136
|
+
if (args.length === 1 && args[0].type === "IterationExpression" && args[0].subtype !== "DoStatement" && !args[0].async && isEmptyBareBlock(args[0].block)) {
|
|
2341
4137
|
return $skip;
|
|
2342
4138
|
}
|
|
2343
4139
|
return [ta?.[0], open, insertTrimmingSpace(ws, ""), args, close];
|
|
@@ -2742,7 +4538,7 @@ ${input.slice(result.pos)}
|
|
|
2742
4538
|
}
|
|
2743
4539
|
var BinaryOpExpression$0 = $TS($S(UnaryExpression, $Q(BinaryOpRHS)), function($skip, $loc, $0, $1, $2) {
|
|
2744
4540
|
if ($2.length)
|
|
2745
|
-
return
|
|
4541
|
+
return processBinaryOpExpression($0);
|
|
2746
4542
|
return $1;
|
|
2747
4543
|
});
|
|
2748
4544
|
function BinaryOpExpression(state) {
|
|
@@ -3732,7 +5528,7 @@ ${input.slice(result.pos)}
|
|
|
3732
5528
|
var ExtendsTarget$0 = $TS($S(ExpressionWithIndentedApplicationForbidden, $E(TypeArguments)), function($skip, $loc, $0, $1, $2) {
|
|
3733
5529
|
var exp = $1;
|
|
3734
5530
|
var ta = $2;
|
|
3735
|
-
exp =
|
|
5531
|
+
exp = makeLeftHandSideExpression(exp);
|
|
3736
5532
|
if (ta)
|
|
3737
5533
|
return [exp, ta];
|
|
3738
5534
|
return exp;
|
|
@@ -4283,14 +6079,14 @@ ${input.slice(result.pos)}
|
|
|
4283
6079
|
}
|
|
4284
6080
|
var CallExpression$0 = $TS($S($EXPECT($L14, fail, 'CallExpression "super"'), ArgumentsWithTrailingMemberExpressions, $Q(CallExpressionRest)), function($skip, $loc, $0, $1, $2, $3) {
|
|
4285
6081
|
var rest = $3;
|
|
4286
|
-
return
|
|
6082
|
+
return processCallMemberExpression({
|
|
4287
6083
|
type: "CallExpression",
|
|
4288
6084
|
children: [$1, ...$2, ...rest.flat()]
|
|
4289
6085
|
});
|
|
4290
6086
|
});
|
|
4291
6087
|
var CallExpression$1 = $TS($S($EXPECT($L15, fail, 'CallExpression "import"'), ArgumentsWithTrailingMemberExpressions, $Q(CallExpressionRest)), function($skip, $loc, $0, $1, $2, $3) {
|
|
4292
6088
|
var rest = $3;
|
|
4293
|
-
return
|
|
6089
|
+
return processCallMemberExpression({
|
|
4294
6090
|
type: "CallExpression",
|
|
4295
6091
|
children: [$1, ...$2, ...rest.flat()]
|
|
4296
6092
|
});
|
|
@@ -4301,7 +6097,7 @@ ${input.slice(result.pos)}
|
|
|
4301
6097
|
var rest = $3;
|
|
4302
6098
|
if (rest.length || trailing.length) {
|
|
4303
6099
|
rest = rest.flat();
|
|
4304
|
-
return
|
|
6100
|
+
return processCallMemberExpression({
|
|
4305
6101
|
type: "CallExpression",
|
|
4306
6102
|
children: [member, ...trailing, ...rest]
|
|
4307
6103
|
});
|
|
@@ -4444,7 +6240,7 @@ ${input.slice(result.pos)}
|
|
|
4444
6240
|
var MemberExpression$0 = $TS($S($C(PrimaryExpression, SuperProperty, MetaProperty), $Q(MemberExpressionRest)), function($skip, $loc, $0, $1, $2) {
|
|
4445
6241
|
var rest = $2;
|
|
4446
6242
|
if (rest.length || Array.isArray($1)) {
|
|
4447
|
-
return
|
|
6243
|
+
return processCallMemberExpression({
|
|
4448
6244
|
type: "MemberExpression",
|
|
4449
6245
|
children: [$1, ...rest].flat()
|
|
4450
6246
|
});
|
|
@@ -5255,7 +7051,7 @@ ${input.slice(result.pos)}
|
|
|
5255
7051
|
var props = $0;
|
|
5256
7052
|
if (!props)
|
|
5257
7053
|
return { children: [], names: [] };
|
|
5258
|
-
return
|
|
7054
|
+
return reorderBindingRestProperty(props);
|
|
5259
7055
|
});
|
|
5260
7056
|
function ObjectBindingPatternContent(state) {
|
|
5261
7057
|
let eventData;
|
|
@@ -5347,7 +7143,7 @@ ${input.slice(result.pos)}
|
|
|
5347
7143
|
var elements = $0;
|
|
5348
7144
|
if (!elements)
|
|
5349
7145
|
return { children: [], names: [], length: 0 };
|
|
5350
|
-
return
|
|
7146
|
+
return adjustBindingElements(elements);
|
|
5351
7147
|
});
|
|
5352
7148
|
function ArrayBindingPatternContent(state) {
|
|
5353
7149
|
let eventData;
|
|
@@ -5463,7 +7259,7 @@ ${input.slice(result.pos)}
|
|
|
5463
7259
|
var props = $2;
|
|
5464
7260
|
if (!props.length)
|
|
5465
7261
|
return $skip;
|
|
5466
|
-
return
|
|
7262
|
+
return reorderBindingRestProperty(props.flat());
|
|
5467
7263
|
});
|
|
5468
7264
|
function NestedBindingProperties(state) {
|
|
5469
7265
|
let eventData;
|
|
@@ -5639,7 +7435,7 @@ ${input.slice(result.pos)}
|
|
|
5639
7435
|
var elements = $2;
|
|
5640
7436
|
if (!elements.length)
|
|
5641
7437
|
return $skip;
|
|
5642
|
-
return
|
|
7438
|
+
return adjustBindingElements(elements.flat());
|
|
5643
7439
|
});
|
|
5644
7440
|
function NestedBindingElements(state) {
|
|
5645
7441
|
let eventData;
|
|
@@ -5826,7 +7622,7 @@ ${input.slice(result.pos)}
|
|
|
5826
7622
|
var FunctionDeclaration$0 = $TS($S(FunctionExpression), function($skip, $loc, $0, $1) {
|
|
5827
7623
|
if ($1.id)
|
|
5828
7624
|
return $1;
|
|
5829
|
-
return
|
|
7625
|
+
return makeLeftHandSideExpression($1);
|
|
5830
7626
|
});
|
|
5831
7627
|
function FunctionDeclaration(state) {
|
|
5832
7628
|
let eventData;
|
|
@@ -6112,9 +7908,10 @@ ${input.slice(result.pos)}
|
|
|
6112
7908
|
return result;
|
|
6113
7909
|
}
|
|
6114
7910
|
}
|
|
6115
|
-
var AmpersandBlockRHSBody$0 = $TS($S($E($S($N(_), $P(CallExpressionRest))), $E($S($N($EXPECT($R3, fail, "AmpersandBlockRHSBody /[&]/")), $P(BinaryOpRHS)))), function($skip, $loc, $0, $1, $2) {
|
|
7911
|
+
var AmpersandBlockRHSBody$0 = $TS($S($E($S($N(_), $P(CallExpressionRest))), $E(QuestionMark), $E($S($N($EXPECT($R3, fail, "AmpersandBlockRHSBody /[&]/")), $P(BinaryOpRHS)))), function($skip, $loc, $0, $1, $2, $3) {
|
|
6116
7912
|
var callExpRest = $1;
|
|
6117
|
-
var
|
|
7913
|
+
var unaryPostfix = $2;
|
|
7914
|
+
var binopRHS = $3;
|
|
6118
7915
|
if (!callExpRest && !binopRHS)
|
|
6119
7916
|
return $skip;
|
|
6120
7917
|
const ref = {
|
|
@@ -6122,7 +7919,7 @@ ${input.slice(result.pos)}
|
|
|
6122
7919
|
base: "$",
|
|
6123
7920
|
id: "$"
|
|
6124
7921
|
};
|
|
6125
|
-
|
|
7922
|
+
let exp = {
|
|
6126
7923
|
type: "AmpersandRef",
|
|
6127
7924
|
children: [ref],
|
|
6128
7925
|
names: [],
|
|
@@ -6131,9 +7928,12 @@ ${input.slice(result.pos)}
|
|
|
6131
7928
|
if (callExpRest) {
|
|
6132
7929
|
exp.children.push(...callExpRest[1]);
|
|
6133
7930
|
}
|
|
7931
|
+
if (unaryPostfix) {
|
|
7932
|
+
exp = processUnaryExpression([], exp, unaryPostfix);
|
|
7933
|
+
}
|
|
6134
7934
|
if (binopRHS) {
|
|
6135
7935
|
return {
|
|
6136
|
-
children:
|
|
7936
|
+
children: processBinaryOpExpression([exp, binopRHS[1]]),
|
|
6137
7937
|
ref
|
|
6138
7938
|
};
|
|
6139
7939
|
}
|
|
@@ -8985,7 +10785,7 @@ ${input.slice(result.pos)}
|
|
|
8985
10785
|
var statement = $1;
|
|
8986
10786
|
var post = $2;
|
|
8987
10787
|
if (post)
|
|
8988
|
-
return
|
|
10788
|
+
return addPostfixStatement(statement, ...post);
|
|
8989
10789
|
return statement;
|
|
8990
10790
|
});
|
|
8991
10791
|
function PostfixedStatement(state) {
|
|
@@ -9014,7 +10814,7 @@ ${input.slice(result.pos)}
|
|
|
9014
10814
|
var expression = $1;
|
|
9015
10815
|
var post = $2;
|
|
9016
10816
|
if (post)
|
|
9017
|
-
return
|
|
10817
|
+
return attachPostfixStatementAsExpression(expression, post);
|
|
9018
10818
|
return expression;
|
|
9019
10819
|
});
|
|
9020
10820
|
function PostfixedExpression(state) {
|
|
@@ -9043,7 +10843,7 @@ ${input.slice(result.pos)}
|
|
|
9043
10843
|
var expression = $1;
|
|
9044
10844
|
var post = $2;
|
|
9045
10845
|
if (post)
|
|
9046
|
-
return
|
|
10846
|
+
return attachPostfixStatementAsExpression(expression, post);
|
|
9047
10847
|
return expression;
|
|
9048
10848
|
});
|
|
9049
10849
|
function NonPipelinePostfixedExpression(state) {
|
|
@@ -9105,7 +10905,7 @@ ${input.slice(result.pos)}
|
|
|
9105
10905
|
var Statement$7 = LabelledStatement;
|
|
9106
10906
|
var Statement$8 = $TS($S(ExpressionStatement), function($skip, $loc, $0, $1) {
|
|
9107
10907
|
if ($1.type === "ObjectExpression" || $1.type === "FunctionExpression" && !$1.id) {
|
|
9108
|
-
return
|
|
10908
|
+
return makeLeftHandSideExpression($1);
|
|
9109
10909
|
}
|
|
9110
10910
|
return $1;
|
|
9111
10911
|
});
|
|
@@ -9388,7 +11188,7 @@ ${input.slice(result.pos)}
|
|
|
9388
11188
|
var clause = $1;
|
|
9389
11189
|
var b = $2;
|
|
9390
11190
|
var e = $3;
|
|
9391
|
-
return
|
|
11191
|
+
return expressionizeIfClause(clause, b, e);
|
|
9392
11192
|
});
|
|
9393
11193
|
function IfExpression(state) {
|
|
9394
11194
|
let eventData;
|
|
@@ -9416,7 +11216,7 @@ ${input.slice(result.pos)}
|
|
|
9416
11216
|
var clause = $1;
|
|
9417
11217
|
var b = $2;
|
|
9418
11218
|
var e = $3;
|
|
9419
|
-
return
|
|
11219
|
+
return expressionizeIfClause(clause, b, e);
|
|
9420
11220
|
});
|
|
9421
11221
|
function UnlessExpression(state) {
|
|
9422
11222
|
let eventData;
|
|
@@ -10517,7 +12317,7 @@ ${input.slice(result.pos)}
|
|
|
10517
12317
|
var e = $0;
|
|
10518
12318
|
return {
|
|
10519
12319
|
type: "SwitchExpression",
|
|
10520
|
-
children:
|
|
12320
|
+
children: wrapIIFE(e.children),
|
|
10521
12321
|
expression: e.expression,
|
|
10522
12322
|
caseBlock: e.caseBlock
|
|
10523
12323
|
};
|
|
@@ -10884,7 +12684,7 @@ ${input.slice(result.pos)}
|
|
|
10884
12684
|
return {
|
|
10885
12685
|
type: "TryExpression",
|
|
10886
12686
|
blocks: t.blocks,
|
|
10887
|
-
children:
|
|
12687
|
+
children: wrapIIFE(t)
|
|
10888
12688
|
};
|
|
10889
12689
|
});
|
|
10890
12690
|
function TryExpression(state) {
|
|
@@ -11848,7 +13648,7 @@ ${input.slice(result.pos)}
|
|
|
11848
13648
|
var DebuggerExpression$0 = $TS($S(Debugger), function($skip, $loc, $0, $1) {
|
|
11849
13649
|
return {
|
|
11850
13650
|
type: "DebuggerExpression",
|
|
11851
|
-
children:
|
|
13651
|
+
children: wrapIIFE($1)
|
|
11852
13652
|
};
|
|
11853
13653
|
});
|
|
11854
13654
|
function DebuggerExpression(state) {
|
|
@@ -11876,7 +13676,7 @@ ${input.slice(result.pos)}
|
|
|
11876
13676
|
var ThrowExpression$0 = $TS($S(Throw, ExtendedExpression), function($skip, $loc, $0, $1, $2) {
|
|
11877
13677
|
return {
|
|
11878
13678
|
type: "ThrowExpression",
|
|
11879
|
-
children:
|
|
13679
|
+
children: wrapIIFE($0)
|
|
11880
13680
|
};
|
|
11881
13681
|
});
|
|
11882
13682
|
function ThrowExpression(state) {
|
|
@@ -13757,7 +15557,7 @@ ${input.slice(result.pos)}
|
|
|
13757
15557
|
}
|
|
13758
15558
|
}
|
|
13759
15559
|
var TemplateLiteral$0 = $TS($S(TripleTick, $Q($C(TemplateBlockCharacters, TemplateSubstitution)), TripleTick), function($skip, $loc, $0, $1, $2, $3) {
|
|
13760
|
-
return
|
|
15560
|
+
return dedentBlockSubstitutions($0);
|
|
13761
15561
|
});
|
|
13762
15562
|
var TemplateLiteral$1 = $TS($S(Backtick, $Q($C(TemplateCharacters, TemplateSubstitution)), Backtick), function($skip, $loc, $0, $1, $2, $3) {
|
|
13763
15563
|
return {
|
|
@@ -13766,7 +15566,7 @@ ${input.slice(result.pos)}
|
|
|
13766
15566
|
};
|
|
13767
15567
|
});
|
|
13768
15568
|
var TemplateLiteral$2 = $TS($S(TripleDoubleQuote, $Q($C(TripleDoubleStringCharacters, CoffeeStringSubstitution)), TripleDoubleQuote), function($skip, $loc, $0, $1, $2, $3) {
|
|
13769
|
-
return
|
|
15569
|
+
return dedentBlockSubstitutions($0);
|
|
13770
15570
|
});
|
|
13771
15571
|
var TemplateLiteral$3 = $TS($S(TripleSingleQuote, TripleSingleStringCharacters, TripleSingleQuote), function($skip, $loc, $0, $1, $2, $3) {
|
|
13772
15572
|
var s = $1;
|
|
@@ -13774,7 +15574,7 @@ ${input.slice(result.pos)}
|
|
|
13774
15574
|
var e = $3;
|
|
13775
15575
|
return {
|
|
13776
15576
|
type: "TemplateLiteral",
|
|
13777
|
-
children: [s,
|
|
15577
|
+
children: [s, dedentBlockString(str), e]
|
|
13778
15578
|
};
|
|
13779
15579
|
});
|
|
13780
15580
|
var TemplateLiteral$4 = CoffeeInterpolatedDoubleQuotedString;
|
|
@@ -16541,7 +18341,7 @@ ${input.slice(result.pos)}
|
|
|
16541
18341
|
],
|
|
16542
18342
|
jsxChildren: [$1].concat($2.map(([, tag]) => tag))
|
|
16543
18343
|
};
|
|
16544
|
-
const type =
|
|
18344
|
+
const type = typeOfJSX(jsx, module.config, module.getRef);
|
|
16545
18345
|
return type ? [
|
|
16546
18346
|
{ ts: true, children: ["("] },
|
|
16547
18347
|
jsx,
|
|
@@ -17021,7 +18821,7 @@ ${input.slice(result.pos)}
|
|
|
17021
18821
|
}
|
|
17022
18822
|
if (exprs.length === 1) {
|
|
17023
18823
|
let root = exprs[0];
|
|
17024
|
-
while (root.length &&
|
|
18824
|
+
while (root.length && isWhitespaceOrEmpty(root[root.length - 1])) {
|
|
17025
18825
|
root = root.slice(0, -1);
|
|
17026
18826
|
}
|
|
17027
18827
|
while (root?.length === 1)
|
|
@@ -17101,7 +18901,7 @@ ${input.slice(result.pos)}
|
|
|
17101
18901
|
children: [".", id],
|
|
17102
18902
|
name: id
|
|
17103
18903
|
};
|
|
17104
|
-
const expr =
|
|
18904
|
+
const expr = processCallMemberExpression({
|
|
17105
18905
|
type: "CallExpression",
|
|
17106
18906
|
children: [at, access, ...rest.flat()]
|
|
17107
18907
|
});
|
|
@@ -17125,7 +18925,7 @@ ${input.slice(result.pos)}
|
|
|
17125
18925
|
var JSXAttribute$4 = $TS($S(Identifier, $P(InlineJSXCallExpressionRest), $Y(JSXAttributeSpace)), function($skip, $loc, $0, $1, $2, $3) {
|
|
17126
18926
|
var id = $1;
|
|
17127
18927
|
var rest = $2;
|
|
17128
|
-
const expr =
|
|
18928
|
+
const expr = processCallMemberExpression({
|
|
17129
18929
|
type: "CallExpression",
|
|
17130
18930
|
children: [id, ...rest.flat()]
|
|
17131
18931
|
});
|
|
@@ -17323,7 +19123,7 @@ ${input.slice(result.pos)}
|
|
|
17323
19123
|
}
|
|
17324
19124
|
var InlineJSXAttributeValue$0 = $TS($S(InlineJSXUnaryExpression, $Q(InlineJSXBinaryOpRHS)), function($skip, $loc, $0, $1, $2) {
|
|
17325
19125
|
if ($2.length)
|
|
17326
|
-
return
|
|
19126
|
+
return processBinaryOpExpression($0);
|
|
17327
19127
|
return $1;
|
|
17328
19128
|
});
|
|
17329
19129
|
function InlineJSXAttributeValue(state) {
|
|
@@ -17482,7 +19282,7 @@ ${input.slice(result.pos)}
|
|
|
17482
19282
|
var InlineJSXCallExpression$0 = $TS($S($EXPECT($L14, fail, 'InlineJSXCallExpression "super"'), ExplicitArguments, $Q(InlineJSXCallExpressionRest)), function($skip, $loc, $0, $1, $2, $3) {
|
|
17483
19283
|
var args = $2;
|
|
17484
19284
|
var rest = $3;
|
|
17485
|
-
return
|
|
19285
|
+
return processCallMemberExpression({
|
|
17486
19286
|
type: "CallExpression",
|
|
17487
19287
|
children: [
|
|
17488
19288
|
$1,
|
|
@@ -17494,7 +19294,7 @@ ${input.slice(result.pos)}
|
|
|
17494
19294
|
var InlineJSXCallExpression$1 = $TS($S($EXPECT($L15, fail, 'InlineJSXCallExpression "import"'), ExplicitArguments, $Q(InlineJSXCallExpressionRest)), function($skip, $loc, $0, $1, $2, $3) {
|
|
17495
19295
|
var args = $2;
|
|
17496
19296
|
var rest = $3;
|
|
17497
|
-
return
|
|
19297
|
+
return processCallMemberExpression({
|
|
17498
19298
|
type: "CallExpression",
|
|
17499
19299
|
children: [
|
|
17500
19300
|
$1,
|
|
@@ -17508,7 +19308,7 @@ ${input.slice(result.pos)}
|
|
|
17508
19308
|
var rest = $2;
|
|
17509
19309
|
if (rest.length) {
|
|
17510
19310
|
rest = rest.flat();
|
|
17511
|
-
return
|
|
19311
|
+
return processCallMemberExpression({
|
|
17512
19312
|
type: "CallExpression",
|
|
17513
19313
|
children: [member, ...rest]
|
|
17514
19314
|
});
|
|
@@ -17576,7 +19376,7 @@ ${input.slice(result.pos)}
|
|
|
17576
19376
|
var InlineJSXMemberExpression$0 = $TS($S($C(InlineJSXPrimaryExpression, SuperProperty, MetaProperty), $Q(InlineJSXMemberExpressionRest)), function($skip, $loc, $0, $1, $2) {
|
|
17577
19377
|
var rest = $2;
|
|
17578
19378
|
if (rest.length || Array.isArray($1)) {
|
|
17579
|
-
return
|
|
19379
|
+
return processCallMemberExpression({
|
|
17580
19380
|
type: "MemberExpression",
|
|
17581
19381
|
children: [$1, ...rest].flat()
|
|
17582
19382
|
});
|
|
@@ -18669,7 +20469,7 @@ ${input.slice(result.pos)}
|
|
|
18669
20469
|
...block.properties.map((property, i) => {
|
|
18670
20470
|
let init, isString;
|
|
18671
20471
|
if (property.init) {
|
|
18672
|
-
init =
|
|
20472
|
+
init = replaceNodes(
|
|
18673
20473
|
deepCopy(property.init),
|
|
18674
20474
|
(n) => n.type === "Identifier" && names.has(n.name),
|
|
18675
20475
|
(n) => [id, '["', n.name, '"]']
|
|
@@ -21015,1999 +22815,214 @@ ${input.slice(result.pos)}
|
|
|
21015
22815
|
multiLineImplicitObjectLiteralForbidden: {
|
|
21016
22816
|
get() {
|
|
21017
22817
|
const { forbidMultiLineImplicitObjectLiteral: s } = module;
|
|
21018
|
-
return s[s.length - 1];
|
|
21019
|
-
}
|
|
21020
|
-
},
|
|
21021
|
-
newlineBinaryOpForbidden: {
|
|
21022
|
-
get() {
|
|
21023
|
-
const { forbidNewlineBinaryOp: s } = module;
|
|
21024
|
-
return s[s.length - 1];
|
|
21025
|
-
}
|
|
21026
|
-
},
|
|
21027
|
-
currentJSXTag: {
|
|
21028
|
-
get() {
|
|
21029
|
-
const { JSXTagStack: s } = module;
|
|
21030
|
-
return s[s.length - 1];
|
|
21031
|
-
}
|
|
21032
|
-
}
|
|
21033
|
-
});
|
|
21034
|
-
}
|
|
21035
|
-
module.config = parse2.config = {
|
|
21036
|
-
autoVar: false,
|
|
21037
|
-
autoLet: false,
|
|
21038
|
-
coffeeBinaryExistential: false,
|
|
21039
|
-
coffeeBooleans: false,
|
|
21040
|
-
coffeeClasses: false,
|
|
21041
|
-
coffeeComment: false,
|
|
21042
|
-
coffeeDo: false,
|
|
21043
|
-
coffeeEq: false,
|
|
21044
|
-
coffeeForLoops: false,
|
|
21045
|
-
coffeeInterpolation: false,
|
|
21046
|
-
coffeeIsnt: false,
|
|
21047
|
-
coffeeJSX: false,
|
|
21048
|
-
coffeeLineContinuation: false,
|
|
21049
|
-
coffeeNot: false,
|
|
21050
|
-
coffeeOf: false,
|
|
21051
|
-
coffeePrototype: false,
|
|
21052
|
-
defaultElement: "div",
|
|
21053
|
-
implicitReturns: true,
|
|
21054
|
-
objectIs: false,
|
|
21055
|
-
react: false,
|
|
21056
|
-
solid: false,
|
|
21057
|
-
client: false,
|
|
21058
|
-
rewriteTsImports: true,
|
|
21059
|
-
server: false,
|
|
21060
|
-
tab: void 0,
|
|
21061
|
-
verbose: false
|
|
21062
|
-
};
|
|
21063
|
-
module.asAny = {
|
|
21064
|
-
ts: true,
|
|
21065
|
-
children: [" as any"]
|
|
21066
|
-
};
|
|
21067
|
-
module.asConst = {
|
|
21068
|
-
ts: true,
|
|
21069
|
-
children: [" as const"]
|
|
21070
|
-
};
|
|
21071
|
-
module.prelude = [];
|
|
21072
|
-
const preludeVar = "var ";
|
|
21073
|
-
const declareRef = {
|
|
21074
|
-
indexOf(indexOfRef) {
|
|
21075
|
-
const typeSuffix = {
|
|
21076
|
-
ts: true,
|
|
21077
|
-
children: [": <T>(this: T[], searchElement: T) => boolean"]
|
|
21078
|
-
};
|
|
21079
|
-
module.prelude.push(["", [preludeVar, indexOfRef, typeSuffix, " = [].indexOf", module.asAny, ";\n"]]);
|
|
21080
|
-
},
|
|
21081
|
-
hasProp(hasPropRef) {
|
|
21082
|
-
const typeSuffix = {
|
|
21083
|
-
ts: true,
|
|
21084
|
-
children: [": <T>(this: T, prop: keyof T) => boolean"]
|
|
21085
|
-
};
|
|
21086
|
-
module.prelude.push(["", [preludeVar, hasPropRef, typeSuffix, " = {}.hasOwnProperty", module.asAny, ";\n"]]);
|
|
21087
|
-
},
|
|
21088
|
-
is(isRef) {
|
|
21089
|
-
const typeSuffix = {
|
|
21090
|
-
ts: true,
|
|
21091
|
-
children: [": { <B, A extends B> (a: A, b: B): b is A, <A, B> (a: A, b: B): a is A & B }"]
|
|
21092
|
-
};
|
|
21093
|
-
module.prelude.push(["", [preludeVar, isRef, typeSuffix, " = Object.is", module.asAny, ";\n"]]);
|
|
21094
|
-
},
|
|
21095
|
-
modulo(moduloRef) {
|
|
21096
|
-
const typeSuffix = {
|
|
21097
|
-
ts: true,
|
|
21098
|
-
children: [": (a: number, b: number) => number"]
|
|
21099
|
-
};
|
|
21100
|
-
module.prelude.push(["", [preludeVar, moduloRef, typeSuffix, " = (a, b) => (a % b + b) % b;", "\n"]]);
|
|
21101
|
-
},
|
|
21102
|
-
xor(xorRef) {
|
|
21103
|
-
const typeSuffix = {
|
|
21104
|
-
ts: true,
|
|
21105
|
-
children: [": (a: unknown, b: unknown) => boolean"]
|
|
21106
|
-
};
|
|
21107
|
-
module.prelude.push(["", [preludeVar, xorRef, typeSuffix, " = (a, b) => a ? !b && a : b;", "\n"]]);
|
|
21108
|
-
},
|
|
21109
|
-
xnor(xnorRef) {
|
|
21110
|
-
const typeSuffix = {
|
|
21111
|
-
ts: true,
|
|
21112
|
-
children: [": (a: unknown, b: unknown) => boolean"]
|
|
21113
|
-
};
|
|
21114
|
-
module.prelude.push(["", [preludeVar, xnorRef, typeSuffix, " = (a, b) => a ? b : !b || a;", "\n"]]);
|
|
21115
|
-
},
|
|
21116
|
-
returnSymbol(ref) {
|
|
21117
|
-
module.prelude.push({
|
|
21118
|
-
children: [
|
|
21119
|
-
preludeVar,
|
|
21120
|
-
ref,
|
|
21121
|
-
` = Symbol("return")';
|
|
21122
|
-
`
|
|
21123
|
-
]
|
|
21124
|
-
});
|
|
21125
|
-
},
|
|
21126
|
-
JSX(jsxRef) {
|
|
21127
|
-
module.prelude.push({
|
|
21128
|
-
ts: true,
|
|
21129
|
-
children: [
|
|
21130
|
-
"import type { JSX as ",
|
|
21131
|
-
jsxRef,
|
|
21132
|
-
" } from 'solid-js';\n"
|
|
21133
|
-
]
|
|
21134
|
-
});
|
|
21135
|
-
},
|
|
21136
|
-
IntrinsicElements(intrinsicElementsRef) {
|
|
21137
|
-
const JSX = module.getRef("JSX");
|
|
21138
|
-
module.prelude.push({
|
|
21139
|
-
ts: true,
|
|
21140
|
-
children: [
|
|
21141
|
-
"type ",
|
|
21142
|
-
intrinsicElementsRef,
|
|
21143
|
-
"<K extends keyof ",
|
|
21144
|
-
JSX,
|
|
21145
|
-
".IntrinsicElements> =\n",
|
|
21146
|
-
" ",
|
|
21147
|
-
JSX,
|
|
21148
|
-
".IntrinsicElements[K] extends ",
|
|
21149
|
-
JSX,
|
|
21150
|
-
".DOMAttributes<infer T> ? T : unknown;\n"
|
|
21151
|
-
]
|
|
21152
|
-
});
|
|
21153
|
-
}
|
|
21154
|
-
};
|
|
21155
|
-
const refs = {};
|
|
21156
|
-
module.getRef = function(base) {
|
|
21157
|
-
if (refs.hasOwnProperty(base))
|
|
21158
|
-
return refs[base];
|
|
21159
|
-
const ref = {
|
|
21160
|
-
type: "Ref",
|
|
21161
|
-
base,
|
|
21162
|
-
id: base
|
|
21163
|
-
};
|
|
21164
|
-
if (declareRef.hasOwnProperty(base))
|
|
21165
|
-
declareRef[base](ref);
|
|
21166
|
-
return refs[base] = ref;
|
|
21167
|
-
};
|
|
21168
|
-
module.makeAsConst = function(node) {
|
|
21169
|
-
if (node.type === "Literal" && node.raw !== "null" || node.type === "ArrayExpression" || node.type === "ObjectExpression") {
|
|
21170
|
-
return { ...node, children: [...node.children, module.asConst] };
|
|
21171
|
-
}
|
|
21172
|
-
return node;
|
|
21173
|
-
};
|
|
21174
|
-
module.typeOfJSX = function(node) {
|
|
21175
|
-
switch (node.type) {
|
|
21176
|
-
case "JSXElement":
|
|
21177
|
-
return module.typeOfJSXElement(node);
|
|
21178
|
-
case "JSXFragment":
|
|
21179
|
-
return module.typeOfJSXFragment(node);
|
|
21180
|
-
}
|
|
21181
|
-
};
|
|
21182
|
-
module.typeOfJSXElement = function(node) {
|
|
21183
|
-
if (module.config.solid) {
|
|
21184
|
-
if (module.config.server && !module.config.client) {
|
|
21185
|
-
return ["string"];
|
|
21186
|
-
}
|
|
21187
|
-
let { tag } = node;
|
|
21188
|
-
const clientType = tag[0] === tag[0].toLowerCase() ? [module.getRef("IntrinsicElements"), '<"', tag, '">'] : ["ReturnType<typeof ", tag, ">"];
|
|
21189
|
-
if (module.config.server) {
|
|
21190
|
-
return ["string", " | ", clientType];
|
|
21191
|
-
} else {
|
|
21192
|
-
return clientType;
|
|
21193
|
-
}
|
|
21194
|
-
}
|
|
21195
|
-
};
|
|
21196
|
-
module.typeOfJSXFragment = function(node) {
|
|
21197
|
-
if (module.config.solid) {
|
|
21198
|
-
let type = [];
|
|
21199
|
-
let lastType;
|
|
21200
|
-
for (let child of node.jsxChildren) {
|
|
21201
|
-
switch (child.type) {
|
|
21202
|
-
case "JSXText":
|
|
21203
|
-
if (lastType !== "JSXText") {
|
|
21204
|
-
type.push("string");
|
|
21205
|
-
}
|
|
21206
|
-
break;
|
|
21207
|
-
case "JSXElement":
|
|
21208
|
-
type.push(module.typeOfJSXElement(child));
|
|
21209
|
-
break;
|
|
21210
|
-
case "JSXFragment":
|
|
21211
|
-
type.push(...module.typeOfJSXFragment(child));
|
|
21212
|
-
break;
|
|
21213
|
-
case "JSXChildExpression":
|
|
21214
|
-
if (child.expression) {
|
|
21215
|
-
type.push(["typeof ", child.expression]);
|
|
21216
|
-
}
|
|
21217
|
-
break;
|
|
21218
|
-
default:
|
|
21219
|
-
throw new Error(`unknown child in JSXFragment: ${JSON.stringify(child)}`);
|
|
21220
|
-
}
|
|
21221
|
-
lastType = child.type;
|
|
21222
|
-
}
|
|
21223
|
-
if (type.length === 1) {
|
|
21224
|
-
return type[0];
|
|
21225
|
-
} else {
|
|
21226
|
-
type = type.flatMap((t) => [t, ", "]);
|
|
21227
|
-
type.pop();
|
|
21228
|
-
return ["[", type, "]"];
|
|
21229
|
-
}
|
|
21230
|
-
}
|
|
21231
|
-
};
|
|
21232
|
-
Object.defineProperty(module.config, "deno", {
|
|
21233
|
-
set(b) {
|
|
21234
|
-
module.config.rewriteTsImports = !b;
|
|
21235
|
-
}
|
|
21236
|
-
});
|
|
21237
|
-
module.config.deno = typeof Deno !== "undefined";
|
|
21238
|
-
Object.defineProperty(module.config, "coffeeCompat", {
|
|
21239
|
-
set(b) {
|
|
21240
|
-
for (const option of [
|
|
21241
|
-
"autoVar",
|
|
21242
|
-
"coffeeBinaryExistential",
|
|
21243
|
-
"coffeeBooleans",
|
|
21244
|
-
"coffeeClasses",
|
|
21245
|
-
"coffeeComment",
|
|
21246
|
-
"coffeeDo",
|
|
21247
|
-
"coffeeEq",
|
|
21248
|
-
"coffeeForLoops",
|
|
21249
|
-
"coffeeInterpolation",
|
|
21250
|
-
"coffeeIsnt",
|
|
21251
|
-
"coffeeJSX",
|
|
21252
|
-
"coffeeLineContinuation",
|
|
21253
|
-
"coffeeNot",
|
|
21254
|
-
"coffeeOf",
|
|
21255
|
-
"coffeePrototype"
|
|
21256
|
-
]) {
|
|
21257
|
-
module.config[option] = b;
|
|
21258
|
-
}
|
|
21259
|
-
if (b) {
|
|
21260
|
-
module.config.objectIs = false;
|
|
21261
|
-
}
|
|
21262
|
-
}
|
|
21263
|
-
});
|
|
21264
|
-
});
|
|
21265
|
-
function Reset(state) {
|
|
21266
|
-
let eventData;
|
|
21267
|
-
if (state.events) {
|
|
21268
|
-
const result = state.events.enter?.("Reset", state);
|
|
21269
|
-
if (result) {
|
|
21270
|
-
if (result.cache)
|
|
21271
|
-
return result.cache;
|
|
21272
|
-
eventData = result.data;
|
|
21273
|
-
}
|
|
21274
|
-
}
|
|
21275
|
-
if (state.tokenize) {
|
|
21276
|
-
const result = $TOKEN("Reset", state, Reset$0(state));
|
|
21277
|
-
if (state.events)
|
|
21278
|
-
state.events.exit?.("Reset", state, result, eventData);
|
|
21279
|
-
return result;
|
|
21280
|
-
} else {
|
|
21281
|
-
const result = Reset$0(state);
|
|
21282
|
-
if (state.events)
|
|
21283
|
-
state.events.exit?.("Reset", state, result, eventData);
|
|
21284
|
-
return result;
|
|
21285
|
-
}
|
|
21286
|
-
}
|
|
21287
|
-
var Init$0 = $TS($S($E(Shebang), $Q(DirectivePrologue), $EXPECT($L0, fail, 'Init ""')), function($skip, $loc, $0, $1, $2, $3) {
|
|
21288
|
-
var directives = $2;
|
|
21289
|
-
directives.forEach((directive) => {
|
|
21290
|
-
if (directive.type === "CivetPrologue") {
|
|
21291
|
-
Object.assign(module.config, directive.config);
|
|
21292
|
-
}
|
|
21293
|
-
});
|
|
21294
|
-
module.processCallMemberExpression = (node) => {
|
|
21295
|
-
const { children } = node;
|
|
21296
|
-
for (let i = 0; i < children.length; i++) {
|
|
21297
|
-
const glob = children[i];
|
|
21298
|
-
if (glob?.type === "PropertyGlob") {
|
|
21299
|
-
const prefix = children.slice(0, i).concat(glob.dot);
|
|
21300
|
-
const parts = [];
|
|
21301
|
-
for (const part of glob.object.properties) {
|
|
21302
|
-
if (part.type === "MethodDefinition") {
|
|
21303
|
-
throw new Error("Glob pattern cannot have method definition");
|
|
21304
|
-
}
|
|
21305
|
-
if (part.value && !["CallExpression", "MemberExpression", "Identifier"].includes(part.value.type)) {
|
|
21306
|
-
throw new Error("Glob pattern must have call or member expression value");
|
|
21307
|
-
}
|
|
21308
|
-
let value = part.value ?? part.name;
|
|
21309
|
-
const wValue = getTrimmingSpace(part.value);
|
|
21310
|
-
value = prefix.concat(insertTrimmingSpace(value, ""));
|
|
21311
|
-
if (wValue)
|
|
21312
|
-
value.unshift(wValue);
|
|
21313
|
-
if (part.type === "SpreadProperty") {
|
|
21314
|
-
parts.push({
|
|
21315
|
-
type: part.type,
|
|
21316
|
-
value,
|
|
21317
|
-
dots: part.dots,
|
|
21318
|
-
delim: part.delim,
|
|
21319
|
-
names: part.names,
|
|
21320
|
-
children: part.children.slice(0, 2).concat(value, part.delim)
|
|
21321
|
-
});
|
|
21322
|
-
} else {
|
|
21323
|
-
parts.push({
|
|
21324
|
-
type: part.type === "Identifier" ? "Property" : part.type,
|
|
21325
|
-
name: part.name,
|
|
21326
|
-
value,
|
|
21327
|
-
delim: part.delim,
|
|
21328
|
-
names: part.names,
|
|
21329
|
-
children: [
|
|
21330
|
-
module.isWhitespaceOrEmpty(part.children[0]) && part.children[0],
|
|
21331
|
-
part.name,
|
|
21332
|
-
module.isWhitespaceOrEmpty(part.children[2]) && part.children[2],
|
|
21333
|
-
part.children[3]?.token === ":" ? part.children[3] : ":",
|
|
21334
|
-
value,
|
|
21335
|
-
part.delim
|
|
21336
|
-
]
|
|
21337
|
-
});
|
|
21338
|
-
}
|
|
21339
|
-
}
|
|
21340
|
-
const object = {
|
|
21341
|
-
type: "ObjectExpression",
|
|
21342
|
-
children: [
|
|
21343
|
-
glob.object.children[0],
|
|
21344
|
-
...parts,
|
|
21345
|
-
glob.object.children.at(-1)
|
|
21346
|
-
],
|
|
21347
|
-
properties: parts
|
|
21348
|
-
};
|
|
21349
|
-
if (i === children.length - 1)
|
|
21350
|
-
return object;
|
|
21351
|
-
return module.processCallMemberExpression({
|
|
21352
|
-
...node,
|
|
21353
|
-
children: [object, ...children.slice(i + 1)]
|
|
21354
|
-
});
|
|
21355
|
-
} else if (glob?.type === "PropertyBind") {
|
|
21356
|
-
const prefix = children.slice(0, i);
|
|
21357
|
-
return module.processCallMemberExpression({
|
|
21358
|
-
...node,
|
|
21359
|
-
children: [
|
|
21360
|
-
prefix,
|
|
21361
|
-
{
|
|
21362
|
-
...glob,
|
|
21363
|
-
type: "PropertyAccess",
|
|
21364
|
-
children: [...glob.children, ".bind(", prefix, ")"]
|
|
21365
|
-
},
|
|
21366
|
-
...children.slice(i + 1)
|
|
21367
|
-
]
|
|
21368
|
-
});
|
|
21369
|
-
}
|
|
21370
|
-
}
|
|
21371
|
-
return node;
|
|
21372
|
-
};
|
|
21373
|
-
module.needsRef = function(expression, base = "ref") {
|
|
21374
|
-
switch (expression.type) {
|
|
21375
|
-
case "Ref":
|
|
21376
|
-
case "Identifier":
|
|
21377
|
-
case "Literal":
|
|
21378
|
-
return;
|
|
21379
|
-
default:
|
|
21380
|
-
return {
|
|
21381
|
-
type: "Ref",
|
|
21382
|
-
base,
|
|
21383
|
-
id: base
|
|
21384
|
-
};
|
|
21385
|
-
}
|
|
21386
|
-
};
|
|
21387
|
-
module.expressionizeIfClause = function(clause, b, e) {
|
|
21388
|
-
const children = clause.children.slice(1);
|
|
21389
|
-
children.push("?", b);
|
|
21390
|
-
if (e) {
|
|
21391
|
-
children.push(e[0], ":", ...e.slice(2));
|
|
21392
|
-
} else {
|
|
21393
|
-
children.push(":void 0");
|
|
21394
|
-
}
|
|
21395
|
-
return {
|
|
21396
|
-
type: "IfExpression",
|
|
21397
|
-
children
|
|
21398
|
-
};
|
|
21399
|
-
};
|
|
21400
|
-
module.addPostfixStatement = function(statement, ws, post) {
|
|
21401
|
-
let children, expressions;
|
|
21402
|
-
if (post.blockPrefix?.length) {
|
|
21403
|
-
let indent = post.blockPrefix[0][0];
|
|
21404
|
-
expressions = [...post.blockPrefix, [indent, statement]];
|
|
21405
|
-
children = [" {\n", ...expressions, "\n", indent?.slice?.(0, -2), "}"];
|
|
21406
|
-
} else {
|
|
21407
|
-
expressions = [["", statement]];
|
|
21408
|
-
children = [" { ", ...expressions, " }"];
|
|
21409
|
-
}
|
|
21410
|
-
const block = {
|
|
21411
|
-
type: "BlockStatement",
|
|
21412
|
-
children,
|
|
21413
|
-
expressions
|
|
21414
|
-
};
|
|
21415
|
-
children = [...post.children];
|
|
21416
|
-
children.push(block);
|
|
21417
|
-
if (!module.isWhitespaceOrEmpty(ws))
|
|
21418
|
-
children.push(ws);
|
|
21419
|
-
post = { ...post, children, block };
|
|
21420
|
-
if (post.type === "IfStatement") {
|
|
21421
|
-
post.then = block;
|
|
21422
|
-
}
|
|
21423
|
-
return post;
|
|
21424
|
-
};
|
|
21425
|
-
function expressionizeIteration(exp) {
|
|
21426
|
-
const i = exp.children.indexOf(exp.block);
|
|
21427
|
-
if (exp.subtype === "DoStatement") {
|
|
21428
|
-
insertReturn(exp.block);
|
|
21429
|
-
exp.children.splice(i, 1, ...module.wrapIIFE(exp.children, exp.async));
|
|
21430
|
-
return;
|
|
21431
|
-
}
|
|
21432
|
-
const resultsRef = {
|
|
21433
|
-
type: "Ref",
|
|
21434
|
-
base: "results",
|
|
21435
|
-
id: "results"
|
|
21436
|
-
};
|
|
21437
|
-
insertPush(exp.block, resultsRef);
|
|
21438
|
-
exp.children.splice(
|
|
21439
|
-
i,
|
|
21440
|
-
1,
|
|
21441
|
-
module.wrapIIFE([
|
|
21442
|
-
"const ",
|
|
21443
|
-
resultsRef,
|
|
21444
|
-
"=[];",
|
|
21445
|
-
...exp.children,
|
|
21446
|
-
"; return ",
|
|
21447
|
-
resultsRef
|
|
21448
|
-
], exp.async)
|
|
21449
|
-
);
|
|
21450
|
-
}
|
|
21451
|
-
module.wrapIIFE = (exp, async) => {
|
|
21452
|
-
let prefix, suffix;
|
|
21453
|
-
if (async) {
|
|
21454
|
-
prefix = "(async ()=>";
|
|
21455
|
-
suffix = ")()";
|
|
21456
|
-
} else if (hasAwait(exp)) {
|
|
21457
|
-
prefix = "(await (async ()=>";
|
|
21458
|
-
suffix = ")())";
|
|
21459
|
-
} else {
|
|
21460
|
-
prefix = "(()=>";
|
|
21461
|
-
suffix = ")()";
|
|
21462
|
-
}
|
|
21463
|
-
const expressions = Array.isArray(exp) ? [[...exp]] : [exp];
|
|
21464
|
-
const block = {
|
|
21465
|
-
type: "BlockStatement",
|
|
21466
|
-
expressions,
|
|
21467
|
-
children: ["{", expressions, "}"],
|
|
21468
|
-
bare: false
|
|
21469
|
-
};
|
|
21470
|
-
return [
|
|
21471
|
-
prefix,
|
|
21472
|
-
block,
|
|
21473
|
-
suffix
|
|
21474
|
-
];
|
|
21475
|
-
};
|
|
21476
|
-
function wrapIterationReturningResults(statement, outerRef) {
|
|
21477
|
-
if (statement.type === "DoStatement") {
|
|
21478
|
-
if (outerRef) {
|
|
21479
|
-
insertPush(statement.block, outerRef);
|
|
21480
|
-
} else {
|
|
21481
|
-
insertReturn(statement.block);
|
|
21482
|
-
}
|
|
21483
|
-
return;
|
|
21484
|
-
}
|
|
21485
|
-
const resultsRef = {
|
|
21486
|
-
type: "Ref",
|
|
21487
|
-
base: "results",
|
|
21488
|
-
id: "results"
|
|
21489
|
-
};
|
|
21490
|
-
const declaration = {
|
|
21491
|
-
type: "Declaration",
|
|
21492
|
-
children: ["const ", resultsRef, "=[];"]
|
|
21493
|
-
};
|
|
21494
|
-
insertPush(statement.block, resultsRef);
|
|
21495
|
-
statement.children.unshift(declaration);
|
|
21496
|
-
if (outerRef) {
|
|
21497
|
-
statement.children.push(";", outerRef, ".push(", resultsRef, ");");
|
|
21498
|
-
} else {
|
|
21499
|
-
statement.children.push(";return ", resultsRef, ";");
|
|
21500
|
-
}
|
|
21501
|
-
}
|
|
21502
|
-
function insertPush(node, ref) {
|
|
21503
|
-
if (!node)
|
|
21504
|
-
return;
|
|
21505
|
-
switch (node.type) {
|
|
21506
|
-
case "BlockStatement":
|
|
21507
|
-
if (node.expressions.length) {
|
|
21508
|
-
const last = node.expressions[node.expressions.length - 1];
|
|
21509
|
-
insertPush(last, ref);
|
|
21510
|
-
} else {
|
|
21511
|
-
node.expressions.push([ref, ".push(void 0);"]);
|
|
21512
|
-
}
|
|
21513
|
-
return;
|
|
21514
|
-
case "CaseBlock":
|
|
21515
|
-
node.clauses.forEach((clause) => {
|
|
21516
|
-
insertPush(clause, ref);
|
|
21517
|
-
});
|
|
21518
|
-
return;
|
|
21519
|
-
case "WhenClause":
|
|
21520
|
-
insertPush(node.block, ref);
|
|
21521
|
-
return;
|
|
21522
|
-
case "DefaultClause":
|
|
21523
|
-
insertPush(node.block, ref);
|
|
21524
|
-
return;
|
|
21525
|
-
}
|
|
21526
|
-
if (!Array.isArray(node))
|
|
21527
|
-
return;
|
|
21528
|
-
const [, exp] = node;
|
|
21529
|
-
if (!exp)
|
|
21530
|
-
return;
|
|
21531
|
-
const indent = getIndent(node);
|
|
21532
|
-
switch (exp.type) {
|
|
21533
|
-
case "BreakStatement":
|
|
21534
|
-
case "ContinueStatement":
|
|
21535
|
-
case "DebuggerStatement":
|
|
21536
|
-
case "EmptyStatement":
|
|
21537
|
-
case "ReturnStatement":
|
|
21538
|
-
case "ThrowStatement":
|
|
21539
|
-
case "Declaration":
|
|
21540
|
-
return;
|
|
21541
|
-
case "ForStatement":
|
|
21542
|
-
case "IterationStatement":
|
|
21543
|
-
case "DoStatement":
|
|
21544
|
-
wrapIterationReturningResults(exp, ref);
|
|
21545
|
-
return;
|
|
21546
|
-
case "BlockStatement":
|
|
21547
|
-
insertPush(exp.expressions[exp.expressions.length - 1], ref);
|
|
21548
|
-
return;
|
|
21549
|
-
case "IfStatement":
|
|
21550
|
-
insertPush(exp.then, ref);
|
|
21551
|
-
if (exp.else)
|
|
21552
|
-
insertPush(exp.else[2], ref);
|
|
21553
|
-
else
|
|
21554
|
-
exp.children.push([" else {\n", indent, ref, ".push(undefined)\n", indent, "}"]);
|
|
21555
|
-
return;
|
|
21556
|
-
case "PatternMatchingStatement":
|
|
21557
|
-
insertPush(exp.children[0][0], ref);
|
|
21558
|
-
return;
|
|
21559
|
-
case "SwitchStatement":
|
|
21560
|
-
insertPush(exp.children[2], ref);
|
|
21561
|
-
return;
|
|
21562
|
-
case "TryStatement":
|
|
21563
|
-
exp.blocks.forEach((block) => insertPush(block, ref));
|
|
21564
|
-
return;
|
|
21565
|
-
}
|
|
21566
|
-
if (node[node.length - 1]?.type === "SemicolonDelimiter")
|
|
21567
|
-
return;
|
|
21568
|
-
node.splice(1, 0, ref, ".push(");
|
|
21569
|
-
node.push(")");
|
|
21570
|
-
}
|
|
21571
|
-
function wrapWithReturn(expression) {
|
|
21572
|
-
const children = expression ? ["return ", expression] : ["return"];
|
|
21573
|
-
return {
|
|
21574
|
-
type: "ReturnStatement",
|
|
21575
|
-
children
|
|
21576
|
-
};
|
|
21577
|
-
}
|
|
21578
|
-
function insertSwitchReturns(exp) {
|
|
21579
|
-
switch (exp.type) {
|
|
21580
|
-
case "SwitchStatement":
|
|
21581
|
-
exp.caseBlock.clauses.forEach((clause) => {
|
|
21582
|
-
insertReturn(clause);
|
|
21583
|
-
});
|
|
21584
|
-
return;
|
|
21585
|
-
case "SwitchExpression":
|
|
21586
|
-
exp.caseBlock.clauses.forEach(insertReturn);
|
|
21587
|
-
return;
|
|
21588
|
-
}
|
|
21589
|
-
}
|
|
21590
|
-
function insertReturn(node) {
|
|
21591
|
-
if (!node)
|
|
21592
|
-
return;
|
|
21593
|
-
switch (node.type) {
|
|
21594
|
-
case "BlockStatement":
|
|
21595
|
-
if (node.expressions.length) {
|
|
21596
|
-
const last = node.expressions[node.expressions.length - 1];
|
|
21597
|
-
insertReturn(last);
|
|
21598
|
-
} else {
|
|
21599
|
-
if (node.parent.type === "CatchClause") {
|
|
21600
|
-
node.expressions.push(["return"]);
|
|
21601
|
-
}
|
|
21602
|
-
}
|
|
21603
|
-
return;
|
|
21604
|
-
case "WhenClause":
|
|
21605
|
-
node.children.splice(node.children.indexOf(node.break), 1);
|
|
21606
|
-
if (node.block.expressions.length) {
|
|
21607
|
-
insertReturn(node.block);
|
|
21608
|
-
} else {
|
|
21609
|
-
node.block.expressions.push(wrapWithReturn());
|
|
21610
|
-
}
|
|
21611
|
-
return;
|
|
21612
|
-
case "DefaultClause":
|
|
21613
|
-
insertReturn(node.block);
|
|
21614
|
-
return;
|
|
21615
|
-
}
|
|
21616
|
-
if (!Array.isArray(node))
|
|
21617
|
-
return;
|
|
21618
|
-
const [, exp, semi] = node;
|
|
21619
|
-
if (semi?.type === "SemicolonDelimiter")
|
|
21620
|
-
return;
|
|
21621
|
-
let indent = getIndent(node);
|
|
21622
|
-
if (!exp)
|
|
21623
|
-
return;
|
|
21624
|
-
switch (exp.type) {
|
|
21625
|
-
case "BreakStatement":
|
|
21626
|
-
case "ContinueStatement":
|
|
21627
|
-
case "DebuggerStatement":
|
|
21628
|
-
case "EmptyStatement":
|
|
21629
|
-
case "ReturnStatement":
|
|
21630
|
-
case "ThrowStatement":
|
|
21631
|
-
case "Declaration":
|
|
21632
|
-
return;
|
|
21633
|
-
case "ForStatement":
|
|
21634
|
-
case "IterationStatement":
|
|
21635
|
-
case "DoStatement":
|
|
21636
|
-
wrapIterationReturningResults(exp);
|
|
21637
|
-
return;
|
|
21638
|
-
case "BlockStatement":
|
|
21639
|
-
insertReturn(exp.expressions[exp.expressions.length - 1]);
|
|
21640
|
-
return;
|
|
21641
|
-
case "IfStatement":
|
|
21642
|
-
insertReturn(exp.then);
|
|
21643
|
-
if (exp.else)
|
|
21644
|
-
insertReturn(exp.else[2]);
|
|
21645
|
-
else
|
|
21646
|
-
exp.children.push(["", {
|
|
21647
|
-
type: "ReturnStatement",
|
|
21648
|
-
children: [";return"]
|
|
21649
|
-
}]);
|
|
21650
|
-
return;
|
|
21651
|
-
case "PatternMatchingStatement":
|
|
21652
|
-
insertReturn(exp.children[0][0]);
|
|
21653
|
-
return;
|
|
21654
|
-
case "SwitchStatement":
|
|
21655
|
-
insertSwitchReturns(exp);
|
|
21656
|
-
return;
|
|
21657
|
-
case "TryStatement":
|
|
21658
|
-
exp.blocks.forEach((block) => insertReturn(block));
|
|
21659
|
-
return;
|
|
21660
|
-
}
|
|
21661
|
-
if (node[node.length - 1]?.type === "SemicolonDelimiter")
|
|
21662
|
-
return;
|
|
21663
|
-
const returnStatement = wrapWithReturn(node[1]);
|
|
21664
|
-
node.splice(1, 1, returnStatement);
|
|
21665
|
-
}
|
|
21666
|
-
module.makeLeftHandSideExpression = function(expression) {
|
|
21667
|
-
switch (expression.type) {
|
|
21668
|
-
case "Ref":
|
|
21669
|
-
case "Identifier":
|
|
21670
|
-
case "Literal":
|
|
21671
|
-
case "CallExpression":
|
|
21672
|
-
case "MemberExpression":
|
|
21673
|
-
case "ParenthesizedExpression":
|
|
21674
|
-
return expression;
|
|
21675
|
-
default:
|
|
21676
|
-
return {
|
|
21677
|
-
type: "ParenthesizedExpression",
|
|
21678
|
-
children: ["(", expression, ")"],
|
|
21679
|
-
expression
|
|
21680
|
-
};
|
|
21681
|
-
}
|
|
21682
|
-
};
|
|
21683
|
-
module.isWhitespaceOrEmpty = function(node) {
|
|
21684
|
-
if (!node)
|
|
21685
|
-
return true;
|
|
21686
|
-
if (node.type === "Ref")
|
|
21687
|
-
return false;
|
|
21688
|
-
if (node.token)
|
|
21689
|
-
return node.token.match(/^\s*$/);
|
|
21690
|
-
if (node.children)
|
|
21691
|
-
node = node.children;
|
|
21692
|
-
if (!node.length)
|
|
21693
|
-
return true;
|
|
21694
|
-
if (typeof node === "string")
|
|
21695
|
-
return node.match(/^\s*$/);
|
|
21696
|
-
if (Array.isArray(node))
|
|
21697
|
-
return node.every(module.isWhitespaceOrEmpty);
|
|
21698
|
-
};
|
|
21699
|
-
module.isTemplateLiteral = function(node) {
|
|
21700
|
-
let s = node;
|
|
21701
|
-
while (s && s[0] && !s.token)
|
|
21702
|
-
s = s[0];
|
|
21703
|
-
return s.token?.startsWith?.("`");
|
|
21704
|
-
};
|
|
21705
|
-
module.isEmptyBareBlock = function(node) {
|
|
21706
|
-
if (node?.type !== "BlockStatement")
|
|
21707
|
-
return false;
|
|
21708
|
-
const { bare, expressions } = node;
|
|
21709
|
-
return bare && (expressions.length === 0 || expressions.length === 1 && expressions[0][1]?.type === "EmptyStatement");
|
|
21710
|
-
};
|
|
21711
|
-
module.processBinaryOpExpression = function($02) {
|
|
21712
|
-
const expandedOps = module.expandChainedComparisons($02);
|
|
21713
|
-
let i = 2;
|
|
21714
|
-
while (i < expandedOps.length) {
|
|
21715
|
-
const op = expandedOps[i];
|
|
21716
|
-
if (op.special) {
|
|
21717
|
-
let [a, wsOp, op2, wsB, b] = expandedOps.slice(i - 2, i + 3);
|
|
21718
|
-
if (op2.token === "instanceof" && b.type === "Literal" && b.children?.[0]?.type === "StringLiteral") {
|
|
21719
|
-
a = ["typeof ", module.makeLeftHandSideExpression(a)];
|
|
21720
|
-
if (op2.negated) {
|
|
21721
|
-
op2 = { ...op2, token: "!==", negated: false };
|
|
21722
|
-
} else {
|
|
21723
|
-
op2 = { ...op2, token: "===" };
|
|
21724
|
-
}
|
|
21725
|
-
}
|
|
21726
|
-
if (op2.asConst) {
|
|
21727
|
-
a = module.makeAsConst(a);
|
|
21728
|
-
b = module.makeAsConst(b);
|
|
21729
|
-
}
|
|
21730
|
-
let children;
|
|
21731
|
-
if (op2.call) {
|
|
21732
|
-
wsOp = insertTrimmingSpace(wsOp, "");
|
|
21733
|
-
if (op2.reversed) {
|
|
21734
|
-
wsB = insertTrimmingSpace(wsB, "");
|
|
21735
|
-
children = [wsOp, op2.call, "(", wsB, b, ", ", a, ")", op2.suffix];
|
|
21736
|
-
} else {
|
|
21737
|
-
children = [wsOp, op2.call, "(", a, ",", wsB, b, ")", op2.suffix];
|
|
21738
|
-
}
|
|
21739
|
-
} else if (op2.method) {
|
|
21740
|
-
wsOp = insertTrimmingSpace(wsOp, "");
|
|
21741
|
-
wsB = insertTrimmingSpace(wsB, "");
|
|
21742
|
-
if (op2.reversed) {
|
|
21743
|
-
children = [wsB, b, wsOp, ".", op2.method, "(", a, ")"];
|
|
21744
|
-
} else {
|
|
21745
|
-
children = [a, wsOp, ".", op2.method, "(", wsB, b, ")"];
|
|
21746
|
-
}
|
|
21747
|
-
} else if (op2.token) {
|
|
21748
|
-
children = [a, wsOp, op2, wsB, b];
|
|
21749
|
-
if (op2.negated)
|
|
21750
|
-
children = ["(", ...children, ")"];
|
|
21751
|
-
} else {
|
|
21752
|
-
throw new Error("Unknown operator: " + JSON.stringify(op2));
|
|
21753
|
-
}
|
|
21754
|
-
if (op2.negated)
|
|
21755
|
-
children.unshift("!");
|
|
21756
|
-
expandedOps.splice(i - 2, 5, {
|
|
21757
|
-
children
|
|
21758
|
-
});
|
|
21759
|
-
} else {
|
|
21760
|
-
i += 4;
|
|
21761
|
-
}
|
|
21762
|
-
}
|
|
21763
|
-
return expandedOps;
|
|
21764
|
-
};
|
|
21765
|
-
module.expandChainedComparisons = function([first, binops]) {
|
|
21766
|
-
const relationalOps = ["==", "===", "!=", "!==", "<", "<=", ">", ">=", "in"];
|
|
21767
|
-
const lowerPrecedenceOps = ["??", "&&", "||", "&", "|", "^"];
|
|
21768
|
-
let results = [];
|
|
21769
|
-
let i = 0;
|
|
21770
|
-
let l = binops.length;
|
|
21771
|
-
let start = 0;
|
|
21772
|
-
let chains = [];
|
|
21773
|
-
while (i < l) {
|
|
21774
|
-
const [, op] = binops[i];
|
|
21775
|
-
if (relationalOps.includes(op.token) || op.relational) {
|
|
21776
|
-
chains.push(i);
|
|
21777
|
-
} else if (lowerPrecedenceOps.includes(op.token)) {
|
|
21778
|
-
processChains();
|
|
21779
|
-
first = [];
|
|
21780
|
-
}
|
|
21781
|
-
i++;
|
|
21782
|
-
}
|
|
21783
|
-
processChains();
|
|
21784
|
-
return results;
|
|
21785
|
-
function processChains() {
|
|
21786
|
-
if (chains.length > 1) {
|
|
21787
|
-
chains.forEach((index, k) => {
|
|
21788
|
-
if (k > 0) {
|
|
21789
|
-
results.push(" ", "&&", " ");
|
|
21790
|
-
}
|
|
21791
|
-
const [pre, op, post, exp] = binops[index];
|
|
21792
|
-
let endIndex;
|
|
21793
|
-
if (k < chains.length - 1) {
|
|
21794
|
-
endIndex = chains[k + 1];
|
|
21795
|
-
} else {
|
|
21796
|
-
endIndex = i + 1;
|
|
21797
|
-
}
|
|
21798
|
-
results = results.concat(first, ...binops.slice(start, endIndex));
|
|
21799
|
-
first = [exp].concat(binops.slice(index + 1, endIndex));
|
|
21800
|
-
start = endIndex;
|
|
21801
|
-
});
|
|
21802
|
-
} else {
|
|
21803
|
-
results = results.concat(first, ...binops.slice(start, i + 1));
|
|
21804
|
-
start = i + 1;
|
|
21805
|
-
}
|
|
21806
|
-
chains.length = 0;
|
|
21807
|
-
}
|
|
21808
|
-
};
|
|
21809
|
-
module.parsePosition = function() {
|
|
21810
|
-
let s = Error().stack.split(/\n at /);
|
|
21811
|
-
s.shift();
|
|
21812
|
-
s = s.filter((e) => !e.match(/^eval/)).map((e) => e.split(" ")[0]);
|
|
21813
|
-
s = s.slice(1, s.indexOf("Program") + 1);
|
|
21814
|
-
return s;
|
|
21815
|
-
};
|
|
21816
|
-
module.prune = function(node) {
|
|
21817
|
-
if (node === null || node === void 0)
|
|
21818
|
-
return;
|
|
21819
|
-
if (node.length === 0)
|
|
21820
|
-
return;
|
|
21821
|
-
if (Array.isArray(node)) {
|
|
21822
|
-
const a = node.map((n) => module.prune(n)).filter((n) => !!n);
|
|
21823
|
-
if (a.length > 1)
|
|
21824
|
-
return a;
|
|
21825
|
-
if (a.length === 1)
|
|
21826
|
-
return a[0];
|
|
21827
|
-
return;
|
|
21828
|
-
}
|
|
21829
|
-
if (node.children != null) {
|
|
21830
|
-
node.children = module.prune(node.children);
|
|
21831
|
-
return node;
|
|
21832
|
-
}
|
|
21833
|
-
return node;
|
|
21834
|
-
};
|
|
21835
|
-
module.skipIfOnlyWS = function(target) {
|
|
21836
|
-
if (!target)
|
|
21837
|
-
return target;
|
|
21838
|
-
if (Array.isArray(target)) {
|
|
21839
|
-
if (target.length === 1) {
|
|
21840
|
-
return module.skipIfOnlyWS(target[0]);
|
|
21841
|
-
} else if (target.every((e) => module.skipIfOnlyWS(e) === void 0)) {
|
|
21842
|
-
return void 0;
|
|
21843
|
-
}
|
|
21844
|
-
return target;
|
|
21845
|
-
}
|
|
21846
|
-
if (target.token != null && target.token.trim() === "") {
|
|
21847
|
-
return void 0;
|
|
21848
|
-
}
|
|
21849
|
-
return target;
|
|
21850
|
-
};
|
|
21851
|
-
const initialSpacingRe = /^(?:\r?\n|\n)*((?:\r?\n|\n)\s+)/;
|
|
21852
|
-
module.dedentBlockSubstitutions = function($02) {
|
|
21853
|
-
const [s, strWithSubstitutions, e] = $02;
|
|
21854
|
-
if (strWithSubstitutions.length === 0) {
|
|
21855
|
-
return $02;
|
|
21856
|
-
}
|
|
21857
|
-
let initialSpacing, i = 0, l = strWithSubstitutions.length, results = [s];
|
|
21858
|
-
const { token } = strWithSubstitutions[0];
|
|
21859
|
-
if (token) {
|
|
21860
|
-
initialSpacing = token.match(initialSpacingRe);
|
|
21861
|
-
} else {
|
|
21862
|
-
initialSpacing = false;
|
|
21863
|
-
}
|
|
21864
|
-
while (i < l) {
|
|
21865
|
-
let segment = strWithSubstitutions[i];
|
|
21866
|
-
if (segment.token) {
|
|
21867
|
-
segment = module.dedentBlockString(segment, initialSpacing, false);
|
|
21868
|
-
if (i === 0) {
|
|
21869
|
-
segment.token = segment.token.replace(/^(\r?\n|\n)/, "");
|
|
21870
|
-
}
|
|
21871
|
-
if (i === l - 1) {
|
|
21872
|
-
segment.token = segment.token.replace(/(\r?\n|\n)[ \t]*$/, "");
|
|
21873
|
-
}
|
|
21874
|
-
results.push(segment);
|
|
21875
|
-
} else {
|
|
21876
|
-
results.push(segment);
|
|
21877
|
-
}
|
|
21878
|
-
i++;
|
|
21879
|
-
}
|
|
21880
|
-
results.push(e);
|
|
21881
|
-
return {
|
|
21882
|
-
type: "TemplateLiteral",
|
|
21883
|
-
children: results
|
|
21884
|
-
};
|
|
21885
|
-
};
|
|
21886
|
-
module.dedentBlockString = function({ $loc: $loc2, token: str }, spacing, trim = true) {
|
|
21887
|
-
if (spacing == null)
|
|
21888
|
-
spacing = str.match(initialSpacingRe);
|
|
21889
|
-
if (spacing) {
|
|
21890
|
-
str = str.replaceAll(spacing[1], "\n");
|
|
21891
|
-
const l = spacing.length;
|
|
21892
|
-
$loc2.pos += l;
|
|
21893
|
-
$loc2.length -= l;
|
|
21894
|
-
}
|
|
21895
|
-
if (trim) {
|
|
21896
|
-
str = str.replace(/^(\r?\n|\n)/, "").replace(/(\r?\n|\n)[ \t]*$/, "");
|
|
21897
|
-
}
|
|
21898
|
-
str = str.replace(/(\\.|`|\$\{)/g, (s) => {
|
|
21899
|
-
if (s[0] === "\\") {
|
|
21900
|
-
return s;
|
|
21901
|
-
}
|
|
21902
|
-
return `\\${s}`;
|
|
21903
|
-
});
|
|
21904
|
-
return {
|
|
21905
|
-
$loc: $loc2,
|
|
21906
|
-
token: str
|
|
21907
|
-
};
|
|
21908
|
-
};
|
|
21909
|
-
module.adjustBindingElements = function(elements) {
|
|
21910
|
-
const names = elements.flatMap((p) => p.names || []), { length } = elements;
|
|
21911
|
-
let blockPrefix, restIndex = -1, restCount = 0;
|
|
21912
|
-
elements.forEach(({ type }, i) => {
|
|
21913
|
-
if (type === "BindingRestElement") {
|
|
21914
|
-
if (restIndex < 0)
|
|
21915
|
-
restIndex = i;
|
|
21916
|
-
restCount++;
|
|
21917
|
-
}
|
|
21918
|
-
});
|
|
21919
|
-
if (restCount === 0) {
|
|
21920
|
-
return {
|
|
21921
|
-
children: elements,
|
|
21922
|
-
names,
|
|
21923
|
-
blockPrefix,
|
|
21924
|
-
length
|
|
21925
|
-
};
|
|
21926
|
-
} else if (restCount === 1) {
|
|
21927
|
-
const rest = elements[restIndex];
|
|
21928
|
-
const after = elements.slice(restIndex + 1);
|
|
21929
|
-
const restIdentifier = rest.binding.ref || rest.binding;
|
|
21930
|
-
names.push(...rest.names || []);
|
|
21931
|
-
let l = after.length;
|
|
21932
|
-
if (l) {
|
|
21933
|
-
if (arrayElementHasTrailingComma(after[l - 1]))
|
|
21934
|
-
l++;
|
|
21935
|
-
blockPrefix = {
|
|
21936
|
-
type: "PostRestBindingElements",
|
|
21937
|
-
children: ["[", insertTrimmingSpace(after, ""), "] = ", restIdentifier, ".splice(-", l.toString(), ")"],
|
|
21938
|
-
names: after.flatMap((p) => p.names)
|
|
21939
|
-
};
|
|
21940
|
-
}
|
|
21941
|
-
return {
|
|
21942
|
-
names,
|
|
21943
|
-
children: [...elements.slice(0, restIndex), {
|
|
21944
|
-
...rest,
|
|
21945
|
-
children: rest.children.slice(0, -1)
|
|
21946
|
-
}],
|
|
21947
|
-
blockPrefix,
|
|
21948
|
-
length
|
|
21949
|
-
};
|
|
21950
|
-
}
|
|
21951
|
-
const err = {
|
|
21952
|
-
type: "Error",
|
|
21953
|
-
children: ["Multiple rest elements in array pattern"]
|
|
21954
|
-
};
|
|
21955
|
-
return {
|
|
21956
|
-
names,
|
|
21957
|
-
children: [...elements, err],
|
|
21958
|
-
blockPrefix,
|
|
21959
|
-
length
|
|
21960
|
-
};
|
|
21961
|
-
};
|
|
21962
|
-
module.reorderBindingRestProperty = function(props) {
|
|
21963
|
-
const names = props.flatMap((p) => p.names);
|
|
21964
|
-
let restIndex = -1;
|
|
21965
|
-
let restCount = 0;
|
|
21966
|
-
props.forEach(({ type }, i) => {
|
|
21967
|
-
if (type === "BindingRestProperty") {
|
|
21968
|
-
if (restIndex < 0)
|
|
21969
|
-
restIndex = i;
|
|
21970
|
-
restCount++;
|
|
21971
|
-
}
|
|
21972
|
-
});
|
|
21973
|
-
if (restCount === 0) {
|
|
21974
|
-
return {
|
|
21975
|
-
children: props,
|
|
21976
|
-
names
|
|
21977
|
-
};
|
|
21978
|
-
} else if (restCount === 1) {
|
|
21979
|
-
let after = props.slice(restIndex + 1);
|
|
21980
|
-
let rest = props[restIndex];
|
|
21981
|
-
props = props.slice(0, restIndex);
|
|
21982
|
-
if (after.length) {
|
|
21983
|
-
const [restDelim] = rest.children.slice(-1), lastAfterProp = after[after.length - 1], lastAfterChildren = lastAfterProp.children, [lastDelim] = lastAfterChildren.slice(-1);
|
|
21984
|
-
rest = { ...rest, children: [...rest.children.slice(0, -1), lastDelim] };
|
|
21985
|
-
after = [...after.slice(0, -1), { ...lastAfterProp, children: [...lastAfterChildren.slice(0, -1), restDelim] }];
|
|
21986
|
-
}
|
|
21987
|
-
const children = [...props, ...after, rest];
|
|
21988
|
-
return {
|
|
21989
|
-
children,
|
|
21990
|
-
names
|
|
21991
|
-
};
|
|
21992
|
-
}
|
|
21993
|
-
return {
|
|
21994
|
-
children: [{
|
|
21995
|
-
type: "Error",
|
|
21996
|
-
message: "Multiple rest properties in object pattern"
|
|
21997
|
-
}, props]
|
|
21998
|
-
};
|
|
21999
|
-
};
|
|
22000
|
-
function addParentPointers(node, parent) {
|
|
22001
|
-
if (node == null)
|
|
22002
|
-
return;
|
|
22003
|
-
if (typeof node !== "object")
|
|
22004
|
-
return;
|
|
22005
|
-
if (Array.isArray(node)) {
|
|
22006
|
-
for (const child of node) {
|
|
22007
|
-
addParentPointers(child, parent);
|
|
22008
|
-
}
|
|
22009
|
-
return;
|
|
22010
|
-
}
|
|
22011
|
-
node.parent = parent;
|
|
22012
|
-
if (node.children) {
|
|
22013
|
-
for (const child of node.children) {
|
|
22014
|
-
addParentPointers(child, node);
|
|
22015
|
-
}
|
|
22016
|
-
}
|
|
22017
|
-
}
|
|
22018
|
-
module.replaceNodes = (root, predicate, replacer) => {
|
|
22019
|
-
if (root == null)
|
|
22020
|
-
return root;
|
|
22021
|
-
const array = Array.isArray(root) ? root : root.children;
|
|
22022
|
-
if (!array)
|
|
22023
|
-
return root;
|
|
22024
|
-
array.forEach((node, i) => {
|
|
22025
|
-
if (node == null)
|
|
22026
|
-
return;
|
|
22027
|
-
if (predicate(node)) {
|
|
22028
|
-
array[i] = replacer(node, root);
|
|
22029
|
-
} else {
|
|
22030
|
-
module.replaceNodes(node, predicate, replacer);
|
|
22031
|
-
}
|
|
22032
|
-
});
|
|
22033
|
-
return root;
|
|
22034
|
-
};
|
|
22035
|
-
function processParams(f) {
|
|
22036
|
-
const { type, parameters, block } = f;
|
|
22037
|
-
if (type === "ArrowFunction" && parameters && parameters.tp && parameters.tp.parameters.length === 1) {
|
|
22038
|
-
parameters.tp.parameters.push(",");
|
|
22039
|
-
}
|
|
22040
|
-
if (!block)
|
|
22041
|
-
return;
|
|
22042
|
-
const { expressions } = block;
|
|
22043
|
-
if (!expressions)
|
|
22044
|
-
return;
|
|
22045
|
-
const { blockPrefix } = parameters;
|
|
22046
|
-
let indent;
|
|
22047
|
-
if (!expressions.length) {
|
|
22048
|
-
indent = "";
|
|
22049
|
-
} else {
|
|
22050
|
-
indent = expressions[0][0];
|
|
22051
|
-
}
|
|
22052
|
-
const [splices, thisAssignments] = gatherBindingCode(parameters, {
|
|
22053
|
-
injectParamProps: f.name === "constructor"
|
|
22054
|
-
});
|
|
22055
|
-
const delimiter = {
|
|
22056
|
-
type: "SemicolonDelimiter",
|
|
22057
|
-
children: [";"]
|
|
22058
|
-
};
|
|
22059
|
-
const prefix = splices.map((s) => ["let ", s]).concat(thisAssignments).map(
|
|
22060
|
-
(s) => s.type ? {
|
|
22061
|
-
...s,
|
|
22062
|
-
children: [indent, ...s.children, delimiter]
|
|
22063
|
-
} : [indent, s, delimiter]
|
|
22064
|
-
);
|
|
22065
|
-
expressions.unshift(...prefix);
|
|
22066
|
-
}
|
|
22067
|
-
function adjustAtBindings(statements, asThis = false) {
|
|
22068
|
-
gatherRecursiveAll(statements, (n) => n.type === "AtBindingProperty").forEach((binding) => {
|
|
22069
|
-
const { ref } = binding;
|
|
22070
|
-
if (asThis) {
|
|
22071
|
-
const atBinding = binding.binding;
|
|
22072
|
-
atBinding.children.pop();
|
|
22073
|
-
atBinding.type = void 0;
|
|
22074
|
-
binding.children.unshift(ref.id, ": this.", ref.base);
|
|
22075
|
-
binding.type = "Property";
|
|
22076
|
-
binding.ref = void 0;
|
|
22077
|
-
return;
|
|
22078
|
-
}
|
|
22079
|
-
if (ref.names[0] !== ref.base) {
|
|
22080
|
-
binding.children.unshift(ref.base, ": ");
|
|
22081
|
-
}
|
|
22082
|
-
});
|
|
22083
|
-
}
|
|
22084
|
-
function processReturnValue(func) {
|
|
22085
|
-
const { block } = func;
|
|
22086
|
-
const values = gatherRecursiveWithinFunction(
|
|
22087
|
-
block,
|
|
22088
|
-
({ type }) => type === "ReturnValue"
|
|
22089
|
-
);
|
|
22090
|
-
if (!values.length)
|
|
22091
|
-
return false;
|
|
22092
|
-
const ref = {
|
|
22093
|
-
type: "Ref",
|
|
22094
|
-
base: "ret",
|
|
22095
|
-
id: "ret"
|
|
22096
|
-
};
|
|
22097
|
-
let declared;
|
|
22098
|
-
values.forEach((value) => {
|
|
22099
|
-
value.children = [ref];
|
|
22100
|
-
const ancestor = findAncestor(
|
|
22101
|
-
value,
|
|
22102
|
-
({ type }) => type === "Declaration",
|
|
22103
|
-
isFunction
|
|
22104
|
-
);
|
|
22105
|
-
if (ancestor)
|
|
22106
|
-
declared = true;
|
|
22107
|
-
});
|
|
22108
|
-
if (!declared) {
|
|
22109
|
-
let returnType = func.returnType ?? func.signature?.returnType;
|
|
22110
|
-
if (returnType) {
|
|
22111
|
-
const { t } = returnType;
|
|
22112
|
-
if (t.type === "TypePredicate") {
|
|
22113
|
-
returnType = ": boolean";
|
|
22114
|
-
} else if (t.type === "AssertsType") {
|
|
22115
|
-
returnType = void 0;
|
|
22116
|
-
}
|
|
22117
|
-
}
|
|
22118
|
-
block.expressions.unshift([
|
|
22119
|
-
getIndent(block.expressions[0]),
|
|
22120
|
-
{
|
|
22121
|
-
type: "Declaration",
|
|
22122
|
-
children: ["let ", ref, returnType],
|
|
22123
|
-
names: []
|
|
22124
|
-
},
|
|
22125
|
-
";"
|
|
22126
|
-
]);
|
|
22127
|
-
}
|
|
22128
|
-
gatherRecursiveWithinFunction(
|
|
22129
|
-
block,
|
|
22130
|
-
(r) => r.type === "ReturnStatement" && !r.expression
|
|
22131
|
-
).forEach((r) => {
|
|
22132
|
-
r.expression = ref;
|
|
22133
|
-
r.children.splice(-1, 1, " ", ref);
|
|
22134
|
-
});
|
|
22135
|
-
if (block.children.at(-2)?.type !== "ReturnStatement") {
|
|
22136
|
-
block.expressions.push([
|
|
22137
|
-
[getIndent(block.expressions.at(-1))],
|
|
22138
|
-
{
|
|
22139
|
-
type: "ReturnStatement",
|
|
22140
|
-
expression: ref,
|
|
22141
|
-
children: ["return ", ref]
|
|
22142
|
-
}
|
|
22143
|
-
]);
|
|
22144
|
-
}
|
|
22145
|
-
return true;
|
|
22146
|
-
}
|
|
22147
|
-
function isVoidType(t) {
|
|
22148
|
-
return t?.type === "LiteralType" && t.t.type === "VoidType";
|
|
22149
|
-
}
|
|
22150
|
-
function processFunctions(statements) {
|
|
22151
|
-
gatherRecursiveAll(statements, ({ type }) => type === "FunctionExpression" || type === "ArrowFunction").forEach((f) => {
|
|
22152
|
-
processParams(f);
|
|
22153
|
-
if (!processReturnValue(f) && module.config.implicitReturns) {
|
|
22154
|
-
const { block, returnType } = f;
|
|
22155
|
-
const isVoid = isVoidType(returnType?.t);
|
|
22156
|
-
const isBlock = block?.type === "BlockStatement";
|
|
22157
|
-
if (!isVoid && isBlock) {
|
|
22158
|
-
insertReturn(block);
|
|
22159
|
-
}
|
|
22160
|
-
}
|
|
22161
|
-
});
|
|
22162
|
-
gatherRecursiveAll(statements, ({ type }) => type === "MethodDefinition").forEach((f) => {
|
|
22163
|
-
processParams(f);
|
|
22164
|
-
if (!processReturnValue(f) && module.config.implicitReturns) {
|
|
22165
|
-
const { signature, block } = f;
|
|
22166
|
-
const isConstructor = signature.name === "constructor";
|
|
22167
|
-
const isVoid = isVoidType(signature.returnType?.t);
|
|
22168
|
-
const isSet = signature.modifier?.set;
|
|
22169
|
-
if (!isConstructor && !isSet && !isVoid) {
|
|
22170
|
-
insertReturn(block);
|
|
22171
|
-
}
|
|
22172
|
-
}
|
|
22173
|
-
});
|
|
22174
|
-
}
|
|
22175
|
-
function processSwitchExpressions(statements) {
|
|
22176
|
-
if (module.config.implicitReturns) {
|
|
22177
|
-
gatherRecursiveAll(statements, (n) => n.type === "SwitchExpression").forEach(insertSwitchReturns);
|
|
22178
|
-
}
|
|
22179
|
-
}
|
|
22180
|
-
function processTryExpressions(statements) {
|
|
22181
|
-
if (module.config.implicitReturns) {
|
|
22182
|
-
gatherRecursiveAll(statements, (n) => n.type === "TryExpression").forEach(({ blocks }) => {
|
|
22183
|
-
blocks.forEach(insertReturn);
|
|
22184
|
-
});
|
|
22185
|
-
}
|
|
22186
|
-
}
|
|
22187
|
-
function processBindingPatternLHS(lhs, tail) {
|
|
22188
|
-
adjustAtBindings(lhs, true);
|
|
22189
|
-
const [splices, thisAssignments] = gatherBindingCode(lhs);
|
|
22190
|
-
tail.push(...splices.map((s) => [", ", s]), ...thisAssignments.map((a) => [", ", a]));
|
|
22191
|
-
}
|
|
22192
|
-
function processAssignments(statements) {
|
|
22193
|
-
gatherRecursiveAll(statements, (n) => n.type === "AssignmentExpression" && n.names === null).forEach((exp) => {
|
|
22194
|
-
let { lhs: $12, exp: $22 } = exp, tail = [], i = 0, len = $12.length;
|
|
22195
|
-
if ($12.some((left) => left[left.length - 1].special)) {
|
|
22196
|
-
if ($12.length !== 1) {
|
|
22197
|
-
throw new Error("Only one assignment with id= is allowed");
|
|
22198
|
-
}
|
|
22199
|
-
const [, lhs, , op] = $12[0];
|
|
22200
|
-
const { call } = op;
|
|
22201
|
-
op[op.length - 1] = "=";
|
|
22202
|
-
$22 = [call, "(", lhs, ", ", $22, ")"];
|
|
22203
|
-
}
|
|
22204
|
-
let wrapped = false;
|
|
22205
|
-
while (i < len) {
|
|
22206
|
-
const lastAssignment = $12[i++];
|
|
22207
|
-
const [, lhs, , op] = lastAssignment;
|
|
22208
|
-
if (op.token !== "=")
|
|
22209
|
-
continue;
|
|
22210
|
-
if (lhs.type === "ObjectExpression" || lhs.type === "ObjectBindingPattern") {
|
|
22211
|
-
if (!wrapped) {
|
|
22212
|
-
wrapped = true;
|
|
22213
|
-
lhs.children.splice(0, 0, "(");
|
|
22214
|
-
tail.push(")");
|
|
22215
|
-
}
|
|
22216
|
-
}
|
|
22217
|
-
}
|
|
22218
|
-
i = len - 1;
|
|
22219
|
-
while (i >= 0) {
|
|
22220
|
-
const lastAssignment = $12[i];
|
|
22221
|
-
if (lastAssignment[3].token === "=") {
|
|
22222
|
-
const lhs = lastAssignment[1];
|
|
22223
|
-
if (lhs.type === "MemberExpression") {
|
|
22224
|
-
const members = lhs.children;
|
|
22225
|
-
const lastMember = members[members.length - 1];
|
|
22226
|
-
if (lastMember.type === "SliceExpression") {
|
|
22227
|
-
const { start, end, children: c } = lastMember;
|
|
22228
|
-
c[0].token = ".splice(";
|
|
22229
|
-
c[1] = start;
|
|
22230
|
-
c[2] = ", ";
|
|
22231
|
-
if (end)
|
|
22232
|
-
c[3] = [end, " - ", start];
|
|
22233
|
-
else
|
|
22234
|
-
c[3] = ["1/0"];
|
|
22235
|
-
c[4] = [", ...", $22];
|
|
22236
|
-
c[5] = ")";
|
|
22237
|
-
lastAssignment.pop();
|
|
22238
|
-
if (module.isWhitespaceOrEmpty(lastAssignment[2]))
|
|
22239
|
-
lastAssignment.pop();
|
|
22240
|
-
if ($12.length > 1) {
|
|
22241
|
-
throw new Error("Not implemented yet! TODO: Handle multiple splice assignments");
|
|
22242
|
-
}
|
|
22243
|
-
exp.children = [$12];
|
|
22244
|
-
exp.names = [];
|
|
22245
|
-
return;
|
|
22246
|
-
}
|
|
22247
|
-
} else if (lhs.type === "ObjectBindingPattern" || lhs.type === "ArrayBindingPattern") {
|
|
22248
|
-
processBindingPatternLHS(lhs, tail);
|
|
22249
|
-
}
|
|
22250
|
-
}
|
|
22251
|
-
i--;
|
|
22252
|
-
}
|
|
22253
|
-
const names = $12.flatMap(([, l]) => l.names || []);
|
|
22254
|
-
exp.children = [$12, $22, ...tail];
|
|
22255
|
-
exp.names = names;
|
|
22256
|
-
});
|
|
22257
|
-
gatherRecursiveAll(statements, (n) => n.type === "AssignmentExpression" || n.type === "UpdateExpression").forEach((exp) => {
|
|
22258
|
-
function extractAssignment(lhs) {
|
|
22259
|
-
let expr = lhs;
|
|
22260
|
-
while (expr.type === "ParenthesizedExpression") {
|
|
22261
|
-
expr = expr.expression;
|
|
22262
|
-
}
|
|
22263
|
-
if (expr.type === "AssignmentExpression" || expr.type === "UpdateExpression") {
|
|
22264
|
-
if (expr.type === "UpdateExpression" && expr.children[0] === expr.assigned) {
|
|
22265
|
-
post.push([", ", lhs]);
|
|
22266
|
-
} else {
|
|
22267
|
-
pre.push([lhs, ", "]);
|
|
22268
|
-
}
|
|
22269
|
-
return expr.assigned;
|
|
22270
|
-
}
|
|
22271
|
-
}
|
|
22272
|
-
const pre = [], post = [];
|
|
22273
|
-
switch (exp.type) {
|
|
22274
|
-
case "AssignmentExpression":
|
|
22275
|
-
if (!exp.lhs)
|
|
22276
|
-
return;
|
|
22277
|
-
exp.lhs.forEach((lhsPart, i) => {
|
|
22278
|
-
let newLhs2 = extractAssignment(lhsPart[1]);
|
|
22279
|
-
if (newLhs2) {
|
|
22280
|
-
lhsPart[1] = newLhs2;
|
|
22281
|
-
}
|
|
22282
|
-
});
|
|
22283
|
-
break;
|
|
22284
|
-
case "UpdateExpression":
|
|
22285
|
-
let newLhs = extractAssignment(exp.assigned);
|
|
22286
|
-
if (newLhs) {
|
|
22287
|
-
const i = exp.children.indexOf(exp.assigned);
|
|
22288
|
-
exp.assigned = exp.children[i] = newLhs;
|
|
22289
|
-
}
|
|
22290
|
-
break;
|
|
22291
|
-
}
|
|
22292
|
-
if (pre.length)
|
|
22293
|
-
exp.children.unshift(...pre);
|
|
22294
|
-
if (post.length)
|
|
22295
|
-
exp.children.push(...post);
|
|
22296
|
-
});
|
|
22297
|
-
}
|
|
22298
|
-
module.attachPostfixStatementAsExpression = function(exp, post) {
|
|
22299
|
-
let clause;
|
|
22300
|
-
switch (post[1].type) {
|
|
22301
|
-
case "ForStatement":
|
|
22302
|
-
case "IterationStatement":
|
|
22303
|
-
case "DoStatement":
|
|
22304
|
-
clause = module.addPostfixStatement(exp, ...post);
|
|
22305
|
-
return {
|
|
22306
|
-
type: "IterationExpression",
|
|
22307
|
-
children: [clause],
|
|
22308
|
-
block: clause.block
|
|
22309
|
-
};
|
|
22310
|
-
case "IfStatement":
|
|
22311
|
-
clause = module.expressionizeIfClause(post[1], exp);
|
|
22312
|
-
return clause;
|
|
22313
|
-
default:
|
|
22314
|
-
throw new Error("Unknown postfix statement");
|
|
22315
|
-
}
|
|
22316
|
-
};
|
|
22317
|
-
function getPatternConditions(pattern, ref, conditions) {
|
|
22318
|
-
if (pattern.rest)
|
|
22319
|
-
return;
|
|
22320
|
-
switch (pattern.type) {
|
|
22321
|
-
case "ArrayBindingPattern": {
|
|
22322
|
-
const { elements, length } = pattern, hasRest = elements.some((e) => e.rest), comparator = hasRest ? " >= " : " === ", l = [comparator, (length - hasRest).toString()];
|
|
22323
|
-
conditions.push(
|
|
22324
|
-
["Array.isArray(", ref, ")"],
|
|
22325
|
-
[ref, ".length", l]
|
|
22326
|
-
);
|
|
22327
|
-
elements.forEach(({ children: [, e] }, i) => {
|
|
22328
|
-
const subRef = [ref, "[", i.toString(), "]"];
|
|
22329
|
-
getPatternConditions(e, subRef, conditions);
|
|
22330
|
-
});
|
|
22331
|
-
const postRest = pattern.children.find((c) => c?.blockPrefix);
|
|
22332
|
-
if (postRest) {
|
|
22333
|
-
const postElements = postRest.blockPrefix.children[1], { length: postLength } = postElements;
|
|
22334
|
-
postElements.forEach(({ children: [, e] }, i) => {
|
|
22335
|
-
const subRef = [ref, "[", ref, ".length - ", (postLength + i).toString(), "]"];
|
|
22336
|
-
getPatternConditions(e, subRef, conditions);
|
|
22337
|
-
});
|
|
22338
|
-
}
|
|
22339
|
-
break;
|
|
22340
|
-
}
|
|
22341
|
-
case "ObjectBindingPattern": {
|
|
22342
|
-
conditions.push(
|
|
22343
|
-
["typeof ", ref, " === 'object'"],
|
|
22344
|
-
[ref, " != null"]
|
|
22345
|
-
);
|
|
22346
|
-
pattern.properties.forEach((p) => {
|
|
22347
|
-
switch (p.type) {
|
|
22348
|
-
case "PinProperty":
|
|
22349
|
-
case "BindingProperty": {
|
|
22350
|
-
const { name, value } = p;
|
|
22351
|
-
let subRef;
|
|
22352
|
-
switch (name.type) {
|
|
22353
|
-
case "ComputedPropertyName":
|
|
22354
|
-
conditions.push([name.expression, " in ", ref]);
|
|
22355
|
-
subRef = [ref, name];
|
|
22356
|
-
break;
|
|
22357
|
-
case "Literal":
|
|
22358
|
-
case "StringLiteral":
|
|
22359
|
-
case "NumericLiteral":
|
|
22360
|
-
conditions.push([name, " in ", ref]);
|
|
22361
|
-
subRef = [ref, "[", name, "]"];
|
|
22362
|
-
break;
|
|
22363
|
-
default:
|
|
22364
|
-
conditions.push(["'", name, "' in ", ref]);
|
|
22365
|
-
subRef = [ref, ".", name];
|
|
22366
|
-
}
|
|
22367
|
-
if (value) {
|
|
22368
|
-
getPatternConditions(value, subRef, conditions);
|
|
22369
|
-
}
|
|
22370
|
-
break;
|
|
22371
|
-
}
|
|
22372
|
-
}
|
|
22373
|
-
});
|
|
22374
|
-
break;
|
|
22375
|
-
}
|
|
22376
|
-
case "ConditionFragment":
|
|
22377
|
-
conditions.push(
|
|
22378
|
-
[ref, " ", pattern.children]
|
|
22379
|
-
);
|
|
22380
|
-
break;
|
|
22381
|
-
case "RegularExpressionLiteral": {
|
|
22382
|
-
conditions.push(
|
|
22383
|
-
["typeof ", ref, " === 'string'"],
|
|
22384
|
-
[pattern, ".test(", ref, ")"]
|
|
22385
|
-
);
|
|
22386
|
-
break;
|
|
22387
|
-
}
|
|
22388
|
-
case "PinPattern":
|
|
22389
|
-
conditions.push([
|
|
22390
|
-
ref,
|
|
22391
|
-
" === ",
|
|
22392
|
-
pattern.identifier
|
|
22393
|
-
]);
|
|
22394
|
-
break;
|
|
22395
|
-
case "Literal":
|
|
22396
|
-
conditions.push([
|
|
22397
|
-
ref,
|
|
22398
|
-
" === ",
|
|
22399
|
-
pattern
|
|
22400
|
-
]);
|
|
22401
|
-
break;
|
|
22402
|
-
default:
|
|
22403
|
-
break;
|
|
22404
|
-
}
|
|
22405
|
-
}
|
|
22406
|
-
function elideMatchersFromArrayBindings(elements) {
|
|
22407
|
-
return elements.map((el) => {
|
|
22408
|
-
if (el.type === "BindingRestElement") {
|
|
22409
|
-
return ["", el, void 0];
|
|
22410
|
-
}
|
|
22411
|
-
const { children: [ws, e, sep] } = el;
|
|
22412
|
-
switch (e.type) {
|
|
22413
|
-
case "Literal":
|
|
22414
|
-
case "RegularExpressionLiteral":
|
|
22415
|
-
case "StringLiteral":
|
|
22416
|
-
case "PinPattern":
|
|
22417
|
-
return sep;
|
|
22418
|
-
default:
|
|
22419
|
-
return [ws, nonMatcherBindings(e), sep];
|
|
22420
|
-
}
|
|
22421
|
-
});
|
|
22422
|
-
}
|
|
22423
|
-
function elideMatchersFromPropertyBindings(properties) {
|
|
22424
|
-
return properties.map((p) => {
|
|
22425
|
-
switch (p.type) {
|
|
22426
|
-
case "BindingProperty": {
|
|
22427
|
-
const { children, name, value } = p;
|
|
22428
|
-
const [ws] = children;
|
|
22429
|
-
const sep = children[children.length - 1];
|
|
22430
|
-
switch (value && value.type) {
|
|
22431
|
-
case "ArrayBindingPattern":
|
|
22432
|
-
case "ObjectBindingPattern":
|
|
22433
|
-
return {
|
|
22434
|
-
...p,
|
|
22435
|
-
children: [ws, name, ": ", nonMatcherBindings(value)]
|
|
22436
|
-
};
|
|
22437
|
-
case "Identifier":
|
|
22438
|
-
return p;
|
|
22439
|
-
case "Literal":
|
|
22440
|
-
case "RegularExpressionLiteral":
|
|
22441
|
-
case "StringLiteral":
|
|
22442
|
-
default:
|
|
22443
|
-
return {
|
|
22444
|
-
...p,
|
|
22445
|
-
children: [ws, name, sep]
|
|
22446
|
-
};
|
|
22447
|
-
}
|
|
22448
|
-
}
|
|
22449
|
-
case "PinProperty":
|
|
22450
|
-
case "BindingRestProperty":
|
|
22451
|
-
default:
|
|
22452
|
-
return p;
|
|
22453
|
-
}
|
|
22454
|
-
});
|
|
22455
|
-
}
|
|
22456
|
-
function nonMatcherBindings(pattern) {
|
|
22457
|
-
switch (pattern.type) {
|
|
22458
|
-
case "ArrayBindingPattern": {
|
|
22459
|
-
const elements = elideMatchersFromArrayBindings(pattern.elements);
|
|
22460
|
-
const children = ["[", elements, "]"];
|
|
22461
|
-
return {
|
|
22462
|
-
...pattern,
|
|
22463
|
-
children,
|
|
22464
|
-
elements
|
|
22465
|
-
};
|
|
22466
|
-
}
|
|
22467
|
-
case "PostRestBindingElements": {
|
|
22468
|
-
const els = elideMatchersFromArrayBindings(pattern.children[1]);
|
|
22469
|
-
return {
|
|
22470
|
-
...pattern,
|
|
22471
|
-
children: [
|
|
22472
|
-
pattern.children[0],
|
|
22473
|
-
els,
|
|
22474
|
-
...pattern.children.slice(2)
|
|
22475
|
-
]
|
|
22476
|
-
};
|
|
22477
|
-
}
|
|
22478
|
-
case "ObjectBindingPattern":
|
|
22479
|
-
return ["{", elideMatchersFromPropertyBindings(pattern.properties), "}"];
|
|
22480
|
-
default:
|
|
22481
|
-
return pattern;
|
|
22482
|
-
}
|
|
22483
|
-
}
|
|
22484
|
-
function aggregateDuplicateBindings(bindings) {
|
|
22485
|
-
const props = gatherRecursiveAll(bindings, (n) => n.type === "BindingProperty");
|
|
22486
|
-
const arrayBindings = gatherRecursiveAll(bindings, (n) => n.type === "ArrayBindingPattern");
|
|
22487
|
-
arrayBindings.forEach((a) => {
|
|
22488
|
-
const { elements } = a;
|
|
22489
|
-
elements.forEach((element) => {
|
|
22490
|
-
if (Array.isArray(element)) {
|
|
22491
|
-
const [, e] = element;
|
|
22492
|
-
if (e.type === "Identifier") {
|
|
22493
|
-
props.push(e);
|
|
22494
|
-
} else if (e.type === "BindingRestElement") {
|
|
22495
|
-
props.push(e);
|
|
22496
|
-
}
|
|
22497
|
-
}
|
|
22498
|
-
});
|
|
22499
|
-
});
|
|
22500
|
-
const declarations = [];
|
|
22501
|
-
const propsGroupedByName = /* @__PURE__ */ new Map();
|
|
22502
|
-
for (const p of props) {
|
|
22503
|
-
const { name, value } = p;
|
|
22504
|
-
const key = value?.name || name?.name || name;
|
|
22505
|
-
if (propsGroupedByName.has(key)) {
|
|
22506
|
-
propsGroupedByName.get(key).push(p);
|
|
22507
|
-
} else {
|
|
22508
|
-
propsGroupedByName.set(key, [p]);
|
|
22509
|
-
}
|
|
22510
|
-
}
|
|
22511
|
-
propsGroupedByName.forEach((shared, key) => {
|
|
22512
|
-
if (!key)
|
|
22513
|
-
return;
|
|
22514
|
-
if (ReservedWord({
|
|
22515
|
-
pos: 0,
|
|
22516
|
-
input: key
|
|
22517
|
-
})) {
|
|
22518
|
-
shared.forEach((p) => {
|
|
22519
|
-
const ref = {
|
|
22520
|
-
type: "Ref",
|
|
22521
|
-
base: `_${key}`,
|
|
22522
|
-
id: key
|
|
22523
|
-
};
|
|
22524
|
-
aliasBinding(p, ref);
|
|
22525
|
-
});
|
|
22526
|
-
return;
|
|
22527
|
-
}
|
|
22528
|
-
if (shared.length === 1)
|
|
22529
|
-
return;
|
|
22530
|
-
const refs = shared.map((p) => {
|
|
22531
|
-
const ref = {
|
|
22532
|
-
type: "Ref",
|
|
22533
|
-
base: key,
|
|
22534
|
-
id: key
|
|
22535
|
-
};
|
|
22536
|
-
aliasBinding(p, ref);
|
|
22537
|
-
return ref;
|
|
22538
|
-
});
|
|
22539
|
-
declarations.push(["const ", key, " = [", ...refs.map((r, i) => {
|
|
22540
|
-
return i === 0 ? r : [", ", r];
|
|
22541
|
-
}), "]"]);
|
|
22542
|
-
});
|
|
22543
|
-
return declarations;
|
|
22544
|
-
}
|
|
22545
|
-
function processPatternMatching(statements) {
|
|
22546
|
-
gatherRecursiveAll(statements, (n) => n.type === "SwitchStatement" || n.type === "SwitchExpression").forEach((s) => {
|
|
22547
|
-
const { caseBlock } = s;
|
|
22548
|
-
const { clauses } = caseBlock;
|
|
22549
|
-
let errors = false;
|
|
22550
|
-
let isPattern = false;
|
|
22551
|
-
if (clauses.some((c) => c.type === "PatternClause")) {
|
|
22552
|
-
isPattern = true;
|
|
22553
|
-
clauses.forEach((c) => {
|
|
22554
|
-
if (!(c.type === "PatternClause" || c.type === "DefaultClause")) {
|
|
22555
|
-
errors = true;
|
|
22556
|
-
c.children.push({
|
|
22557
|
-
type: "Error",
|
|
22558
|
-
message: "Can't mix pattern matching and non-pattern matching clauses"
|
|
22559
|
-
});
|
|
22560
|
-
}
|
|
22561
|
-
});
|
|
22562
|
-
}
|
|
22563
|
-
if (errors || !isPattern)
|
|
22564
|
-
return;
|
|
22565
|
-
let { expression } = s;
|
|
22566
|
-
if (expression.type === "ParenthesizedExpression") {
|
|
22567
|
-
expression = expression.expression;
|
|
22568
|
-
}
|
|
22569
|
-
let ref = module.needsRef(expression, "m") || expression;
|
|
22570
|
-
let hoistDec = ref !== expression ? [["", ["const ", ref, " = ", expression], ";"]] : void 0;
|
|
22571
|
-
let prev = [], root = prev;
|
|
22572
|
-
const l = clauses.length;
|
|
22573
|
-
clauses.forEach((c, i) => {
|
|
22574
|
-
if (c.type === "DefaultClause") {
|
|
22575
|
-
prev.push(c.block);
|
|
22576
|
-
return;
|
|
22577
|
-
}
|
|
22578
|
-
let { patterns, block } = c;
|
|
22579
|
-
let pattern = patterns[0];
|
|
22580
|
-
const indent = block.expressions?.[0]?.[0] || "";
|
|
22581
|
-
const alternativeConditions = patterns.map((pattern2, i2) => {
|
|
22582
|
-
const conditions = [];
|
|
22583
|
-
getPatternConditions(pattern2, ref, conditions);
|
|
22584
|
-
return conditions;
|
|
22585
|
-
});
|
|
22586
|
-
const conditionExpression = alternativeConditions.map((conditions, i2) => {
|
|
22587
|
-
const conditionArray = conditions.map((c2, i3) => {
|
|
22588
|
-
if (i3 === 0)
|
|
22589
|
-
return c2;
|
|
22590
|
-
return [" && ", ...c2];
|
|
22591
|
-
});
|
|
22592
|
-
if (i2 === 0)
|
|
22593
|
-
return conditionArray;
|
|
22594
|
-
return [" || ", ...conditionArray];
|
|
22595
|
-
});
|
|
22596
|
-
const condition = {
|
|
22597
|
-
type: "ParenthesizedExpression",
|
|
22598
|
-
children: ["(", conditionExpression, ")"],
|
|
22599
|
-
expression: conditionExpression
|
|
22600
|
-
};
|
|
22601
|
-
const prefix = [];
|
|
22602
|
-
switch (pattern.type) {
|
|
22603
|
-
case "ArrayBindingPattern":
|
|
22604
|
-
if (pattern.length === 0)
|
|
22605
|
-
break;
|
|
22606
|
-
case "ObjectBindingPattern": {
|
|
22607
|
-
if (pattern.properties?.length === 0)
|
|
22608
|
-
break;
|
|
22609
|
-
let [splices, thisAssignments] = gatherBindingCode(pattern);
|
|
22610
|
-
const patternBindings = nonMatcherBindings(pattern);
|
|
22611
|
-
splices = splices.map((s2) => [", ", nonMatcherBindings(s2)]);
|
|
22612
|
-
thisAssignments = thisAssignments.map((a) => [indent, a, ";"]);
|
|
22613
|
-
const duplicateDeclarations = aggregateDuplicateBindings([patternBindings, splices]);
|
|
22614
|
-
prefix.push([indent, "const ", patternBindings, " = ", ref, splices, ";"]);
|
|
22615
|
-
prefix.push(...thisAssignments);
|
|
22616
|
-
prefix.push(...duplicateDeclarations.map((d) => [indent, d, ";"]));
|
|
22617
|
-
break;
|
|
22618
|
-
}
|
|
22619
|
-
}
|
|
22620
|
-
block.expressions.unshift(...prefix);
|
|
22621
|
-
const next = [];
|
|
22622
|
-
if (block.bare) {
|
|
22623
|
-
block.children.unshift(" {");
|
|
22624
|
-
block.children.push("}");
|
|
22625
|
-
block.bare = false;
|
|
22626
|
-
}
|
|
22627
|
-
if (i < l - 1)
|
|
22628
|
-
next.push("\n", "else ");
|
|
22629
|
-
prev.push(["", {
|
|
22630
|
-
type: "IfStatement",
|
|
22631
|
-
children: ["if", condition, block, next],
|
|
22632
|
-
then: block,
|
|
22633
|
-
else: next,
|
|
22634
|
-
hoistDec
|
|
22635
|
-
}]);
|
|
22636
|
-
hoistDec = void 0;
|
|
22637
|
-
prev = next;
|
|
22638
|
-
});
|
|
22639
|
-
if (module.config.implicitReturns && s.type === "SwitchExpression") {
|
|
22640
|
-
insertReturn(root[0]);
|
|
22641
|
-
root.splice(0, 1, module.wrapIIFE(root[0]));
|
|
22642
|
-
}
|
|
22643
|
-
s.type = "PatternMatchingStatement";
|
|
22644
|
-
s.children = [root];
|
|
22645
|
-
addParentPointers(s, s.parent);
|
|
22646
|
-
});
|
|
22647
|
-
}
|
|
22648
|
-
function processPipelineExpressions(statements) {
|
|
22649
|
-
gatherRecursiveAll(statements, (n) => n.type === "PipelineExpression").forEach((s) => {
|
|
22650
|
-
const [ws, , body] = s.children;
|
|
22651
|
-
let [, arg] = s.children;
|
|
22652
|
-
let i = 0, l = body.length;
|
|
22653
|
-
const refDec = [];
|
|
22654
|
-
const children = [ws, refDec];
|
|
22655
|
-
let usingRef = null;
|
|
22656
|
-
for (i = 0; i < l; i++) {
|
|
22657
|
-
const step = body[i];
|
|
22658
|
-
const [leadingComment, pipe, trailingComment, expr] = step;
|
|
22659
|
-
const returns = pipe.token === "||>";
|
|
22660
|
-
let ref, result, returning = returns ? arg : null;
|
|
22661
|
-
if (pipe.token === "|>=") {
|
|
22662
|
-
let initRef;
|
|
22663
|
-
if (i === 0) {
|
|
22664
|
-
outer:
|
|
22665
|
-
switch (arg.type) {
|
|
22666
|
-
case "MemberExpression":
|
|
22667
|
-
if (arg.children.length <= 2)
|
|
22668
|
-
break;
|
|
22669
|
-
case "CallExpression":
|
|
22670
|
-
const access = arg.children.pop();
|
|
22671
|
-
switch (access.type) {
|
|
22672
|
-
case "PropertyAccess":
|
|
22673
|
-
case "SliceExpression":
|
|
22674
|
-
break;
|
|
22675
|
-
default:
|
|
22676
|
-
children.unshift({
|
|
22677
|
-
type: "Error",
|
|
22678
|
-
$loc: pipe.token.$loc,
|
|
22679
|
-
message: `Can't assign to ${access.type}`
|
|
22680
|
-
});
|
|
22681
|
-
arg.children.push(access);
|
|
22682
|
-
break outer;
|
|
22683
|
-
}
|
|
22684
|
-
usingRef = module.needsRef({});
|
|
22685
|
-
initRef = {
|
|
22686
|
-
type: "AssignmentExpression",
|
|
22687
|
-
children: [usingRef, " = ", arg, ","]
|
|
22688
|
-
};
|
|
22689
|
-
arg = {
|
|
22690
|
-
type: "MemberExpression",
|
|
22691
|
-
children: [usingRef, access]
|
|
22692
|
-
};
|
|
22693
|
-
break;
|
|
22694
|
-
}
|
|
22695
|
-
children.pop();
|
|
22696
|
-
const lhs = [[
|
|
22697
|
-
[refDec, initRef],
|
|
22698
|
-
arg,
|
|
22699
|
-
[],
|
|
22700
|
-
{ token: "=", children: [" = "] }
|
|
22701
|
-
]];
|
|
22702
|
-
Object.assign(s, {
|
|
22703
|
-
type: "AssignmentExpression",
|
|
22704
|
-
children: [lhs, children],
|
|
22705
|
-
names: null,
|
|
22706
|
-
lhs,
|
|
22707
|
-
assigned: arg,
|
|
22708
|
-
exp: children
|
|
22709
|
-
});
|
|
22710
|
-
arg = clone(arg);
|
|
22711
|
-
if (arg.children[0].type === "Ref") {
|
|
22712
|
-
arg.children[0] = usingRef;
|
|
22713
|
-
}
|
|
22714
|
-
} else {
|
|
22715
|
-
children.unshift({
|
|
22716
|
-
type: "Error",
|
|
22717
|
-
$loc: pipe.token.$loc,
|
|
22718
|
-
message: "Can't use |>= in the middle of a pipeline"
|
|
22719
|
-
});
|
|
22720
|
-
}
|
|
22721
|
-
} else {
|
|
22722
|
-
s.children = children;
|
|
22723
|
-
}
|
|
22724
|
-
if (returns && (ref = module.needsRef(arg))) {
|
|
22725
|
-
usingRef = usingRef || ref;
|
|
22726
|
-
arg = {
|
|
22727
|
-
type: "ParenthesizedExpression",
|
|
22728
|
-
children: ["(", {
|
|
22729
|
-
type: "AssignmentExpression",
|
|
22730
|
-
children: [usingRef, " = ", arg]
|
|
22731
|
-
}, ")"]
|
|
22732
|
-
};
|
|
22733
|
-
returning = usingRef;
|
|
22818
|
+
return s[s.length - 1];
|
|
22734
22819
|
}
|
|
22735
|
-
|
|
22736
|
-
|
|
22737
|
-
|
|
22738
|
-
|
|
22739
|
-
|
|
22740
|
-
},
|
|
22741
|
-
arg,
|
|
22742
|
-
returning
|
|
22743
|
-
);
|
|
22744
|
-
if (result.type === "ReturnStatement") {
|
|
22745
|
-
if (i < l - 1) {
|
|
22746
|
-
result.children.push({
|
|
22747
|
-
type: "Error",
|
|
22748
|
-
message: "Can't continue a pipeline after returning"
|
|
22749
|
-
});
|
|
22750
|
-
}
|
|
22751
|
-
arg = result;
|
|
22752
|
-
if (children[children.length - 1] === ",") {
|
|
22753
|
-
children.pop();
|
|
22754
|
-
children.push(";");
|
|
22755
|
-
}
|
|
22756
|
-
break;
|
|
22820
|
+
},
|
|
22821
|
+
newlineBinaryOpForbidden: {
|
|
22822
|
+
get() {
|
|
22823
|
+
const { forbidNewlineBinaryOp: s } = module;
|
|
22824
|
+
return s[s.length - 1];
|
|
22757
22825
|
}
|
|
22758
|
-
|
|
22759
|
-
|
|
22760
|
-
|
|
22761
|
-
|
|
22762
|
-
|
|
22826
|
+
},
|
|
22827
|
+
currentJSXTag: {
|
|
22828
|
+
get() {
|
|
22829
|
+
const { JSXTagStack: s } = module;
|
|
22830
|
+
return s[s.length - 1];
|
|
22763
22831
|
}
|
|
22764
22832
|
}
|
|
22765
|
-
if (usingRef) {
|
|
22766
|
-
refDec.unshift("let ", usingRef, ";");
|
|
22767
|
-
}
|
|
22768
|
-
children.push(arg);
|
|
22769
|
-
addParentPointers(s, s.parent);
|
|
22770
22833
|
});
|
|
22771
22834
|
}
|
|
22772
|
-
module.
|
|
22773
|
-
|
|
22774
|
-
|
|
22775
|
-
|
|
22776
|
-
|
|
22777
|
-
|
|
22778
|
-
|
|
22779
|
-
|
|
22780
|
-
|
|
22781
|
-
|
|
22782
|
-
|
|
22783
|
-
|
|
22784
|
-
|
|
22785
|
-
|
|
22786
|
-
|
|
22787
|
-
|
|
22788
|
-
|
|
22789
|
-
|
|
22790
|
-
|
|
22791
|
-
|
|
22792
|
-
|
|
22793
|
-
|
|
22794
|
-
|
|
22795
|
-
|
|
22796
|
-
|
|
22797
|
-
|
|
22798
|
-
|
|
22799
|
-
|
|
22800
|
-
|
|
22801
|
-
|
|
22802
|
-
|
|
22803
|
-
|
|
22804
|
-
|
|
22805
|
-
|
|
22806
|
-
|
|
22807
|
-
|
|
22808
|
-
|
|
22809
|
-
|
|
22810
|
-
|
|
22811
|
-
|
|
22812
|
-
|
|
22813
|
-
|
|
22814
|
-
|
|
22815
|
-
|
|
22816
|
-
|
|
22817
|
-
|
|
22818
|
-
|
|
22835
|
+
module.config = parse2.config = {
|
|
22836
|
+
autoVar: false,
|
|
22837
|
+
autoLet: false,
|
|
22838
|
+
coffeeBinaryExistential: false,
|
|
22839
|
+
coffeeBooleans: false,
|
|
22840
|
+
coffeeClasses: false,
|
|
22841
|
+
coffeeComment: false,
|
|
22842
|
+
coffeeDo: false,
|
|
22843
|
+
coffeeEq: false,
|
|
22844
|
+
coffeeForLoops: false,
|
|
22845
|
+
coffeeInterpolation: false,
|
|
22846
|
+
coffeeIsnt: false,
|
|
22847
|
+
coffeeJSX: false,
|
|
22848
|
+
coffeeLineContinuation: false,
|
|
22849
|
+
coffeeNot: false,
|
|
22850
|
+
coffeeOf: false,
|
|
22851
|
+
coffeePrototype: false,
|
|
22852
|
+
defaultElement: "div",
|
|
22853
|
+
implicitReturns: true,
|
|
22854
|
+
objectIs: false,
|
|
22855
|
+
react: false,
|
|
22856
|
+
solid: false,
|
|
22857
|
+
client: false,
|
|
22858
|
+
rewriteTsImports: true,
|
|
22859
|
+
server: false,
|
|
22860
|
+
tab: void 0,
|
|
22861
|
+
verbose: false
|
|
22862
|
+
};
|
|
22863
|
+
const asAny = {
|
|
22864
|
+
ts: true,
|
|
22865
|
+
children: [" as any"]
|
|
22866
|
+
};
|
|
22867
|
+
module.prelude = [];
|
|
22868
|
+
const preludeVar = "var ";
|
|
22869
|
+
const declareRef = {
|
|
22870
|
+
indexOf(indexOfRef) {
|
|
22871
|
+
const typeSuffix = {
|
|
22872
|
+
ts: true,
|
|
22873
|
+
children: [": <T>(this: T[], searchElement: T) => boolean"]
|
|
22874
|
+
};
|
|
22875
|
+
module.prelude.push(["", [preludeVar, indexOfRef, typeSuffix, " = [].indexOf", asAny, ";\n"]]);
|
|
22876
|
+
},
|
|
22877
|
+
hasProp(hasPropRef) {
|
|
22878
|
+
const typeSuffix = {
|
|
22879
|
+
ts: true,
|
|
22880
|
+
children: [": <T>(this: T, prop: keyof T) => boolean"]
|
|
22881
|
+
};
|
|
22882
|
+
module.prelude.push(["", [preludeVar, hasPropRef, typeSuffix, " = {}.hasOwnProperty", asAny, ";\n"]]);
|
|
22883
|
+
},
|
|
22884
|
+
is(isRef) {
|
|
22885
|
+
const typeSuffix = {
|
|
22886
|
+
ts: true,
|
|
22887
|
+
children: [": { <B, A extends B> (a: A, b: B): b is A, <A, B> (a: A, b: B): a is A & B }"]
|
|
22888
|
+
};
|
|
22889
|
+
module.prelude.push(["", [preludeVar, isRef, typeSuffix, " = Object.is", asAny, ";\n"]]);
|
|
22890
|
+
},
|
|
22891
|
+
modulo(moduloRef) {
|
|
22892
|
+
const typeSuffix = {
|
|
22893
|
+
ts: true,
|
|
22894
|
+
children: [": (a: number, b: number) => number"]
|
|
22895
|
+
};
|
|
22896
|
+
module.prelude.push(["", [preludeVar, moduloRef, typeSuffix, " = (a, b) => (a % b + b) % b;", "\n"]]);
|
|
22897
|
+
},
|
|
22898
|
+
xor(xorRef) {
|
|
22899
|
+
const typeSuffix = {
|
|
22900
|
+
ts: true,
|
|
22901
|
+
children: [": (a: unknown, b: unknown) => boolean"]
|
|
22902
|
+
};
|
|
22903
|
+
module.prelude.push(["", [preludeVar, xorRef, typeSuffix, " = (a, b) => a ? !b && a : b;", "\n"]]);
|
|
22904
|
+
},
|
|
22905
|
+
xnor(xnorRef) {
|
|
22906
|
+
const typeSuffix = {
|
|
22907
|
+
ts: true,
|
|
22908
|
+
children: [": (a: unknown, b: unknown) => boolean"]
|
|
22909
|
+
};
|
|
22910
|
+
module.prelude.push(["", [preludeVar, xnorRef, typeSuffix, " = (a, b) => a ? b : !b || a;", "\n"]]);
|
|
22911
|
+
},
|
|
22912
|
+
returnSymbol(ref) {
|
|
22913
|
+
module.prelude.push({
|
|
22914
|
+
children: [
|
|
22915
|
+
preludeVar,
|
|
22916
|
+
ref,
|
|
22917
|
+
` = Symbol("return")';
|
|
22918
|
+
`
|
|
22919
|
+
]
|
|
22819
22920
|
});
|
|
22820
|
-
}
|
|
22821
|
-
|
|
22822
|
-
|
|
22823
|
-
|
|
22824
|
-
|
|
22825
|
-
|
|
22826
|
-
|
|
22827
|
-
|
|
22828
|
-
|
|
22921
|
+
},
|
|
22922
|
+
JSX(jsxRef) {
|
|
22923
|
+
module.prelude.push({
|
|
22924
|
+
ts: true,
|
|
22925
|
+
children: [
|
|
22926
|
+
"import type { JSX as ",
|
|
22927
|
+
jsxRef,
|
|
22928
|
+
" } from 'solid-js';\n"
|
|
22929
|
+
]
|
|
22829
22930
|
});
|
|
22830
|
-
|
|
22831
|
-
|
|
22832
|
-
|
|
22833
|
-
|
|
22834
|
-
|
|
22835
|
-
|
|
22836
|
-
|
|
22837
|
-
|
|
22838
|
-
|
|
22839
|
-
|
|
22840
|
-
|
|
22841
|
-
|
|
22842
|
-
|
|
22843
|
-
|
|
22844
|
-
|
|
22845
|
-
|
|
22846
|
-
|
|
22847
|
-
if (!hasDec(x))
|
|
22848
|
-
return a.indexOf(x) === i;
|
|
22849
|
-
}).forEach(pushVar);
|
|
22850
|
-
const fnNodes = gatherNodes(statements, (s) => s.type === "FunctionExpression");
|
|
22851
|
-
const forNodes = gatherNodes(statements, (s) => s.type === "ForStatement");
|
|
22852
|
-
const blockNodes = new Set(gatherNodes(statements, (s) => s.type === "BlockStatement"));
|
|
22853
|
-
fnNodes.forEach(({ block }) => blockNodes.delete(block));
|
|
22854
|
-
forNodes.forEach(({ block }) => blockNodes.delete(block));
|
|
22855
|
-
blockNodes.forEach((block) => {
|
|
22856
|
-
createVarDecs(block.expressions, scopes, pushVar);
|
|
22857
|
-
});
|
|
22858
|
-
forNodes.forEach(({ block, declaration }) => {
|
|
22859
|
-
scopes.push(new Set(declaration.names));
|
|
22860
|
-
createVarDecs(block.expressions, scopes, pushVar);
|
|
22861
|
-
scopes.pop();
|
|
22862
|
-
});
|
|
22863
|
-
fnNodes.forEach(({ block, parameters }) => {
|
|
22864
|
-
scopes.push(new Set(parameters.names));
|
|
22865
|
-
createVarDecs(block.expressions, scopes);
|
|
22866
|
-
scopes.pop();
|
|
22867
|
-
});
|
|
22868
|
-
if (varIds.length) {
|
|
22869
|
-
const indent = getIndent(statements[0]);
|
|
22870
|
-
let delimiter = ";";
|
|
22871
|
-
if (statements[0][1]?.parent?.root) {
|
|
22872
|
-
delimiter = ";\n";
|
|
22873
|
-
}
|
|
22874
|
-
statements.unshift([indent, "var ", varIds.join(", "), delimiter]);
|
|
22875
|
-
}
|
|
22876
|
-
scopes.pop();
|
|
22877
|
-
}
|
|
22878
|
-
function createLetDecs(statements, scopes) {
|
|
22879
|
-
function findVarDecs(statements2, decs) {
|
|
22880
|
-
const declarationNames = gatherRecursive(
|
|
22881
|
-
statements2,
|
|
22882
|
-
(node) => node.type === "Declaration" && node.children && node.children.length > 0 && node.children[0].token && node.children[0].token.startsWith("var") || node.type === "FunctionExpression"
|
|
22883
|
-
).filter((node) => node.type === "Declaration").flatMap((node) => node.names);
|
|
22884
|
-
return new Set(declarationNames);
|
|
22885
|
-
}
|
|
22886
|
-
let declaredIdentifiers = findVarDecs(statements);
|
|
22887
|
-
function hasDec(name) {
|
|
22888
|
-
return declaredIdentifiers.has(name) || scopes.some((s) => s.has(name));
|
|
22889
|
-
}
|
|
22890
|
-
function gatherBlockOrOther(statement) {
|
|
22891
|
-
return gatherNodes(statement, (s) => s.type === "BlockStatement" || s.type === "AssignmentExpression" || s.type === "Declaration").flatMap((node) => {
|
|
22892
|
-
if (node.type == "BlockStatement")
|
|
22893
|
-
return node.bare ? gatherBlockOrOther(node.expressions) : node;
|
|
22894
|
-
else if (node.children && node.children.length)
|
|
22895
|
-
return [...gatherBlockOrOther(node.children), node];
|
|
22896
|
-
else
|
|
22897
|
-
return [];
|
|
22931
|
+
},
|
|
22932
|
+
IntrinsicElements(intrinsicElementsRef) {
|
|
22933
|
+
const JSX = module.getRef("JSX");
|
|
22934
|
+
module.prelude.push({
|
|
22935
|
+
ts: true,
|
|
22936
|
+
children: [
|
|
22937
|
+
"type ",
|
|
22938
|
+
intrinsicElementsRef,
|
|
22939
|
+
"<K extends keyof ",
|
|
22940
|
+
JSX,
|
|
22941
|
+
".IntrinsicElements> =\n",
|
|
22942
|
+
" ",
|
|
22943
|
+
JSX,
|
|
22944
|
+
".IntrinsicElements[K] extends ",
|
|
22945
|
+
JSX,
|
|
22946
|
+
".DOMAttributes<infer T> ? T : unknown;\n"
|
|
22947
|
+
]
|
|
22898
22948
|
});
|
|
22899
22949
|
}
|
|
22900
|
-
|
|
22901
|
-
|
|
22902
|
-
|
|
22903
|
-
|
|
22904
|
-
|
|
22905
|
-
|
|
22906
|
-
|
|
22907
|
-
|
|
22908
|
-
|
|
22909
|
-
|
|
22910
|
-
|
|
22911
|
-
|
|
22912
|
-
|
|
22913
|
-
|
|
22914
|
-
|
|
22915
|
-
|
|
22916
|
-
|
|
22917
|
-
|
|
22918
|
-
|
|
22919
|
-
|
|
22920
|
-
|
|
22921
|
-
|
|
22922
|
-
|
|
22923
|
-
|
|
22924
|
-
|
|
22925
|
-
|
|
22926
|
-
|
|
22927
|
-
|
|
22928
|
-
|
|
22929
|
-
|
|
22930
|
-
|
|
22950
|
+
};
|
|
22951
|
+
const refs = {};
|
|
22952
|
+
module.getRef = function(base) {
|
|
22953
|
+
if (refs.hasOwnProperty(base))
|
|
22954
|
+
return refs[base];
|
|
22955
|
+
const ref = {
|
|
22956
|
+
type: "Ref",
|
|
22957
|
+
base,
|
|
22958
|
+
id: base
|
|
22959
|
+
};
|
|
22960
|
+
if (declareRef.hasOwnProperty(base))
|
|
22961
|
+
declareRef[base](ref);
|
|
22962
|
+
return refs[base] = ref;
|
|
22963
|
+
};
|
|
22964
|
+
Object.defineProperty(module.config, "deno", {
|
|
22965
|
+
set(b) {
|
|
22966
|
+
module.config.rewriteTsImports = !b;
|
|
22967
|
+
}
|
|
22968
|
+
});
|
|
22969
|
+
module.config.deno = typeof Deno !== "undefined";
|
|
22970
|
+
Object.defineProperty(module.config, "coffeeCompat", {
|
|
22971
|
+
set(b) {
|
|
22972
|
+
for (const option of [
|
|
22973
|
+
"autoVar",
|
|
22974
|
+
"coffeeBinaryExistential",
|
|
22975
|
+
"coffeeBooleans",
|
|
22976
|
+
"coffeeClasses",
|
|
22977
|
+
"coffeeComment",
|
|
22978
|
+
"coffeeDo",
|
|
22979
|
+
"coffeeEq",
|
|
22980
|
+
"coffeeForLoops",
|
|
22981
|
+
"coffeeInterpolation",
|
|
22982
|
+
"coffeeIsnt",
|
|
22983
|
+
"coffeeJSX",
|
|
22984
|
+
"coffeeLineContinuation",
|
|
22985
|
+
"coffeeNot",
|
|
22986
|
+
"coffeeOf",
|
|
22987
|
+
"coffeePrototype"
|
|
22988
|
+
]) {
|
|
22989
|
+
module.config[option] = b;
|
|
22931
22990
|
}
|
|
22932
|
-
if (
|
|
22933
|
-
|
|
22934
|
-
let firstIdentifier = gatherNodes(statement[1], (node) => node.type == "Identifier")[0];
|
|
22935
|
-
if (undeclaredIdentifiers.length == 1 && statement[1].type == "AssignmentExpression" && statement[1].names.length == 1 && statement[1].names[0] == undeclaredIdentifiers[0] && firstIdentifier && firstIdentifier.names == undeclaredIdentifiers[0] && gatherNodes(statement[1], (node) => node.type === "ObjectBindingPattern").length == 0)
|
|
22936
|
-
statement[1].children.unshift(["let "]);
|
|
22937
|
-
else {
|
|
22938
|
-
let tail = "\n";
|
|
22939
|
-
if (gatherNodes(indent, (node) => node.token && node.token.endsWith("\n")).length > 0)
|
|
22940
|
-
tail = void 0;
|
|
22941
|
-
targetStatements.push([indent, "let ", undeclaredIdentifiers.join(", "), tail]);
|
|
22942
|
-
}
|
|
22991
|
+
if (b) {
|
|
22992
|
+
module.config.objectIs = false;
|
|
22943
22993
|
}
|
|
22944
|
-
targetStatements.push(statement);
|
|
22945
|
-
}
|
|
22946
|
-
scopes.pop();
|
|
22947
|
-
statements.splice(0, statements.length, targetStatements);
|
|
22948
|
-
}
|
|
22949
|
-
module.constructInvocation = function(fn, arg) {
|
|
22950
|
-
const fnArr = [fn.leadingComment, fn.expr, fn.trailingComment];
|
|
22951
|
-
let expr = fn.expr;
|
|
22952
|
-
while (expr.type === "ParenthesizedExpression") {
|
|
22953
|
-
expr = expr.expression;
|
|
22954
|
-
}
|
|
22955
|
-
if (expr.ampersandBlock) {
|
|
22956
|
-
const { ref, body } = expr;
|
|
22957
|
-
ref.type = "PipedExpression";
|
|
22958
|
-
ref.children = [module.makeLeftHandSideExpression(arg)];
|
|
22959
|
-
return {
|
|
22960
|
-
type: "UnwrappedExpression",
|
|
22961
|
-
children: [module.skipIfOnlyWS(fn.leadingComment), ...body, module.skipIfOnlyWS(fn.trailingComment)]
|
|
22962
|
-
};
|
|
22963
|
-
}
|
|
22964
|
-
expr = fn.expr;
|
|
22965
|
-
const lhs = module.makeLeftHandSideExpression(expr);
|
|
22966
|
-
let comment = module.skipIfOnlyWS(fn.trailingComment);
|
|
22967
|
-
if (comment)
|
|
22968
|
-
lhs.children.splice(2, 0, comment);
|
|
22969
|
-
comment = module.skipIfOnlyWS(fn.leadingComment);
|
|
22970
|
-
if (comment)
|
|
22971
|
-
lhs.children.splice(1, 0, comment);
|
|
22972
|
-
switch (arg.type) {
|
|
22973
|
-
case "CommaExpression":
|
|
22974
|
-
arg = module.makeLeftHandSideExpression(arg);
|
|
22975
|
-
break;
|
|
22976
22994
|
}
|
|
22977
|
-
|
|
22978
|
-
|
|
22979
|
-
|
|
22980
|
-
|
|
22981
|
-
|
|
22982
|
-
|
|
22983
|
-
|
|
22984
|
-
|
|
22985
|
-
|
|
22986
|
-
|
|
22987
|
-
if (returning) {
|
|
22988
|
-
return [
|
|
22989
|
-
children,
|
|
22990
|
-
returning
|
|
22991
|
-
];
|
|
22992
|
-
}
|
|
22993
|
-
return [
|
|
22994
|
-
children,
|
|
22995
|
-
null
|
|
22996
|
-
];
|
|
22997
|
-
case "return":
|
|
22998
|
-
return [{
|
|
22999
|
-
type: "ReturnStatement",
|
|
23000
|
-
children
|
|
23001
|
-
}, null];
|
|
22995
|
+
});
|
|
22996
|
+
});
|
|
22997
|
+
function Reset(state) {
|
|
22998
|
+
let eventData;
|
|
22999
|
+
if (state.events) {
|
|
23000
|
+
const result = state.events.enter?.("Reset", state);
|
|
23001
|
+
if (result) {
|
|
23002
|
+
if (result.cache)
|
|
23003
|
+
return result.cache;
|
|
23004
|
+
eventData = result.data;
|
|
23002
23005
|
}
|
|
23003
|
-
|
|
23004
|
-
|
|
23005
|
-
|
|
23006
|
-
|
|
23007
|
-
|
|
23006
|
+
}
|
|
23007
|
+
if (state.tokenize) {
|
|
23008
|
+
const result = $TOKEN("Reset", state, Reset$0(state));
|
|
23009
|
+
if (state.events)
|
|
23010
|
+
state.events.exit?.("Reset", state, result, eventData);
|
|
23011
|
+
return result;
|
|
23012
|
+
} else {
|
|
23013
|
+
const result = Reset$0(state);
|
|
23014
|
+
if (state.events)
|
|
23015
|
+
state.events.exit?.("Reset", state, result, eventData);
|
|
23016
|
+
return result;
|
|
23017
|
+
}
|
|
23018
|
+
}
|
|
23019
|
+
var Init$0 = $TS($S($E(Shebang), $Q(DirectivePrologue), $EXPECT($L0, fail, 'Init ""')), function($skip, $loc, $0, $1, $2, $3) {
|
|
23020
|
+
var directives = $2;
|
|
23021
|
+
directives.forEach((directive) => {
|
|
23022
|
+
if (directive.type === "CivetPrologue") {
|
|
23023
|
+
Object.assign(module.config, directive.config);
|
|
23008
23024
|
}
|
|
23009
|
-
|
|
23010
|
-
};
|
|
23025
|
+
});
|
|
23011
23026
|
return $0;
|
|
23012
23027
|
});
|
|
23013
23028
|
function Init(state) {
|
|
@@ -23312,44 +23327,40 @@ ${input.slice(result.pos)}
|
|
|
23312
23327
|
exports.parse = parse2;
|
|
23313
23328
|
exports.default = { parse: parse2 };
|
|
23314
23329
|
var {
|
|
23315
|
-
|
|
23316
|
-
|
|
23330
|
+
addPostfixStatement,
|
|
23331
|
+
adjustBindingElements,
|
|
23332
|
+
attachPostfixStatementAsExpression,
|
|
23317
23333
|
blockWithPrefix,
|
|
23318
|
-
clone,
|
|
23319
|
-
convertMethodToFunction,
|
|
23320
23334
|
convertObjectToJSXAttributes,
|
|
23321
23335
|
deepCopy,
|
|
23322
|
-
|
|
23336
|
+
dedentBlockString,
|
|
23337
|
+
dedentBlockSubstitutions,
|
|
23338
|
+
expressionizeIfClause,
|
|
23323
23339
|
forRange,
|
|
23324
23340
|
gatherBindingCode,
|
|
23325
|
-
gatherNodes,
|
|
23326
|
-
gatherRecursive,
|
|
23327
|
-
gatherRecursiveAll,
|
|
23328
|
-
gatherRecursiveWithinFunction,
|
|
23329
|
-
getIndent,
|
|
23330
23341
|
getTrimmingSpace,
|
|
23331
23342
|
hasAwait,
|
|
23332
23343
|
hasYield,
|
|
23333
|
-
hoistRefDecs,
|
|
23334
23344
|
insertTrimmingSpace,
|
|
23335
|
-
|
|
23345
|
+
isEmptyBareBlock,
|
|
23346
|
+
isWhitespaceOrEmpty,
|
|
23336
23347
|
lastAccessInCallExpression,
|
|
23337
23348
|
literalValue,
|
|
23349
|
+
makeLeftHandSideExpression,
|
|
23338
23350
|
modifyString,
|
|
23351
|
+
processBinaryOpExpression,
|
|
23352
|
+
processCallMemberExpression,
|
|
23339
23353
|
processCoffeeInterpolation,
|
|
23340
23354
|
processConstAssignmentDeclaration,
|
|
23341
23355
|
processLetAssignmentDeclaration,
|
|
23356
|
+
processProgram,
|
|
23342
23357
|
processUnaryExpression,
|
|
23343
23358
|
quoteString,
|
|
23344
|
-
|
|
23359
|
+
reorderBindingRestProperty,
|
|
23360
|
+
replaceNodes,
|
|
23361
|
+
typeOfJSX,
|
|
23362
|
+
wrapIIFE
|
|
23345
23363
|
} = require_lib();
|
|
23346
|
-
var assert = {
|
|
23347
|
-
equal(a, b, msg) {
|
|
23348
|
-
if (a !== b) {
|
|
23349
|
-
throw new Error(`Assertion failed [${msg}]: ${a} !== ${b}`);
|
|
23350
|
-
}
|
|
23351
|
-
}
|
|
23352
|
-
};
|
|
23353
23364
|
}
|
|
23354
23365
|
});
|
|
23355
23366
|
|