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