@aljazmc/syntax 0.0.0-stage → 0.3.0

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/bin/index.js ADDED
@@ -0,0 +1,3099 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
+
4
+ // shared/syntax/src/scanner.ts
5
+ var ParseError = class extends SyntaxError {
6
+ static {
7
+ __name(this, "ParseError");
8
+ }
9
+ position;
10
+ constructor(message, position) {
11
+ super(message);
12
+ this.position = position;
13
+ }
14
+ };
15
+ var Scanner = class _Scanner {
16
+ static {
17
+ __name(this, "Scanner");
18
+ }
19
+ static #punctuators = [
20
+ ">>>=",
21
+ "...",
22
+ "===",
23
+ "!==",
24
+ "**=",
25
+ "&&=",
26
+ "||=",
27
+ "??=",
28
+ "=>",
29
+ "==",
30
+ "!=",
31
+ "<=",
32
+ "&&",
33
+ "||",
34
+ "??",
35
+ "?.",
36
+ "++",
37
+ "--",
38
+ "+=",
39
+ "-=",
40
+ "*=",
41
+ "/=",
42
+ "%=",
43
+ "&=",
44
+ "|=",
45
+ "^=",
46
+ "**"
47
+ ];
48
+ static #greater = [">>>=", ">>>", ">>=", ">>", ">=", ">"];
49
+ static #less = ["<<=", "<<", "<=", "<"];
50
+ source;
51
+ comments = [];
52
+ #position = 0;
53
+ constructor(source) {
54
+ this.source = source;
55
+ }
56
+ get position() {
57
+ return this.#position;
58
+ }
59
+ /** Moves back (or forward) and forgets comments scanned beyond. */
60
+ reset(position) {
61
+ this.#position = position;
62
+ while ((this.comments.at(-1)?.start ?? -1) >= position)
63
+ this.comments.pop();
64
+ }
65
+ next() {
66
+ const newline = this.#trivia();
67
+ const start = this.#position;
68
+ const char = this.source[start];
69
+ if (char === void 0) return this.#token("eof", start, newline);
70
+ if (_Scanner.#identifierStart(char) || char === "\\")
71
+ return this.#word("name", start, newline);
72
+ if (char === "#") return this.#word("private", start, newline);
73
+ if (/[0-9]/.test(char) || char === "." && /[0-9]/.test(this.source[start + 1] ?? ""))
74
+ return this.#number(start, newline);
75
+ if (char === '"' || char === "'") return this.#string(start, newline);
76
+ if (char === "`") return this.template(start, newline);
77
+ const punctuator = _Scanner.#punctuators.find(
78
+ (value) => this.source.startsWith(value, start) && !(value === "?." && /[0-9]/.test(this.source[start + 2] ?? ""))
79
+ ) ?? char;
80
+ this.#position = start + punctuator.length;
81
+ return this.#token("punct", start, newline);
82
+ }
83
+ /** Rescans a `/` or `/=` token as a regular expression literal. */
84
+ regex(token) {
85
+ let inClass = false;
86
+ for (let index = token.start + 1; ; index++) {
87
+ const char = this.source[index];
88
+ if (char === void 0 || char === "\n" || char === "\r")
89
+ return this.#invalid(token.start, "Unterminated regex");
90
+ if (char === "\\") index++;
91
+ else if (char === "[") inClass = true;
92
+ else if (char === "]") inClass = false;
93
+ else if (char === "/" && !inClass) {
94
+ this.#position = index + 1;
95
+ while (_Scanner.#identifierPart(this.source[this.#position] ?? ""))
96
+ this.#position++;
97
+ return this.#token("regex", token.start, token.newline);
98
+ }
99
+ }
100
+ }
101
+ /** Rescans `>` as the longest of `>>>=`, `>>>`, `>>=`, `>>`, `>=`. */
102
+ greater(token) {
103
+ return this.#operator(token, _Scanner.#greater);
104
+ }
105
+ /** Rescans `<` as the longest of `<<=`, `<<`, `<=`, since types nest `<<T>() => T>`. */
106
+ less(token) {
107
+ return this.#operator(token, _Scanner.#less);
108
+ }
109
+ #operator(token, candidates) {
110
+ const value = candidates.find(
111
+ (value2) => this.source.startsWith(value2, token.start)
112
+ ) ?? token.value;
113
+ this.reset(token.start + value.length);
114
+ return { ...token, value, end: token.start + value.length };
115
+ }
116
+ /** Scans a template part, from a backtick or a closing `}`, up to `${` or a backtick. */
117
+ template(start, newline = false) {
118
+ for (let index = start + 1; index < this.source.length; index++) {
119
+ const char = this.source[index];
120
+ if (char === "\\") index++;
121
+ else if (char === "`" || char === "$" && this.source[index + 1] === "{") {
122
+ this.#position = index + (char === "`" ? 1 : 2);
123
+ return this.#token("template", start, newline);
124
+ }
125
+ }
126
+ return this.#invalid(start, "Unterminated template");
127
+ }
128
+ /** Scans JSX text up to `{` or `<`. */
129
+ jsxText(start) {
130
+ let index = start;
131
+ while (index < this.source.length && !"{<".includes(this.source[index] ?? ""))
132
+ index++;
133
+ this.#position = index;
134
+ return this.#token("jsxText", start, false);
135
+ }
136
+ /** Rescans a name token to include JSX's `-` characters. */
137
+ jsxName(token) {
138
+ let index = token.end;
139
+ while (/[-\p{ID_Continue}$]/u.test(this.source[index] ?? "")) index++;
140
+ this.reset(index);
141
+ return {
142
+ ...token,
143
+ value: this.source.slice(token.start, index),
144
+ end: index
145
+ };
146
+ }
147
+ /** Rescans a quoted JSX attribute value, which has no escapes. */
148
+ jsxString(token) {
149
+ const quote = this.source[token.start] ?? "";
150
+ const end = this.source.indexOf(quote, token.start + 1);
151
+ if (end < 0) return this.#invalid(token.start, "Unterminated string");
152
+ this.reset(end + 1);
153
+ return {
154
+ ...token,
155
+ type: "string",
156
+ value: this.source.slice(token.start, end + 1),
157
+ end: end + 1
158
+ };
159
+ }
160
+ #token(type, start, newline) {
161
+ return {
162
+ type,
163
+ value: this.source.slice(start, this.#position),
164
+ start,
165
+ end: this.#position,
166
+ newline
167
+ };
168
+ }
169
+ #invalid(start, message) {
170
+ this.#position = this.source.length;
171
+ return {
172
+ type: "invalid",
173
+ value: message,
174
+ start,
175
+ end: start,
176
+ newline: false
177
+ };
178
+ }
179
+ /** Skips whitespace and comments, returns whether a line break was crossed. */
180
+ #trivia() {
181
+ let newline = false;
182
+ if (this.#position === 0 && this.source.startsWith("#!")) {
183
+ const end = this.#lineEnd(0);
184
+ this.comments.push({
185
+ type: "Line",
186
+ value: this.source.slice(0, end),
187
+ start: 0,
188
+ end
189
+ });
190
+ this.#position = end;
191
+ }
192
+ while (this.#position < this.source.length) {
193
+ const char = this.source[this.#position] ?? "";
194
+ const next = this.source[this.#position + 1];
195
+ if (char === "\n" || char === "\r" || char === "\u2028" || char === "\u2029") {
196
+ newline = true;
197
+ this.#position++;
198
+ } else if (/\s/.test(char)) this.#position++;
199
+ else if (char === "/" && next === "/") {
200
+ const end = this.#lineEnd(this.#position);
201
+ this.#comment("Line", end);
202
+ } else if (char === "/" && next === "*") {
203
+ const close = this.source.indexOf("*/", this.#position + 2);
204
+ const end = close < 0 ? this.source.length : close + 2;
205
+ if (/[\r\n\u2028\u2029]/.test(
206
+ this.source.slice(this.#position, end)
207
+ ))
208
+ newline = true;
209
+ this.#comment("Block", end);
210
+ } else break;
211
+ }
212
+ return newline;
213
+ }
214
+ #comment(type, end) {
215
+ const start = this.#position;
216
+ this.comments.push({
217
+ type,
218
+ value: this.source.slice(start, end),
219
+ start,
220
+ end
221
+ });
222
+ this.#position = end;
223
+ }
224
+ #lineEnd(start) {
225
+ const match = /[\r\n\u2028\u2029]/.exec(this.source.slice(start));
226
+ return match ? start + match.index : this.source.length;
227
+ }
228
+ #word(type, start, newline) {
229
+ let index = start + 1;
230
+ while (index < this.source.length) {
231
+ const char = this.source[index] ?? "";
232
+ if (char === "\\")
233
+ index += /^\\u\{/.test(this.source.slice(index, index + 3)) ? this.source.indexOf("}", index) - index + 1 : 6;
234
+ else if (_Scanner.#identifierPart(char)) index += char.length;
235
+ else {
236
+ const code = this.source.codePointAt(index) ?? 0;
237
+ const symbol = String.fromCodePoint(code);
238
+ if (code > 65535 && _Scanner.#identifierPart(symbol))
239
+ index += 2;
240
+ else break;
241
+ }
242
+ }
243
+ this.#position = index;
244
+ return this.#token(type, start, newline);
245
+ }
246
+ #number(start, newline) {
247
+ const pattern = /0[xX][\da-fA-F_]+n?|0[oO][0-7_]+n?|0[bB][01_]+n?|(?:\d[\d_]*\.?[\d_]*|\.\d[\d_]*)(?:[eE][+-]?\d[\d_]*)?n?/y;
248
+ pattern.lastIndex = start;
249
+ pattern.exec(this.source);
250
+ this.#position = pattern.lastIndex;
251
+ return this.#token("number", start, newline);
252
+ }
253
+ #string(start, newline) {
254
+ const quote = this.source[start];
255
+ for (let index = start + 1; index < this.source.length; index++) {
256
+ const char = this.source[index];
257
+ if (char === "\\")
258
+ index += this.source.startsWith("\r\n", index + 1) ? 2 : 1;
259
+ else if (char === quote) {
260
+ this.#position = index + 1;
261
+ return this.#token("string", start, newline);
262
+ } else if (char === "\n" || char === "\r") break;
263
+ }
264
+ return this.#invalid(start, "Unterminated string");
265
+ }
266
+ static #identifierStart(char) {
267
+ return /[\p{ID_Start}$_]/u.test(char) || /[\ud800-\udbff]/.test(char);
268
+ }
269
+ static #identifierPart(char) {
270
+ return /[\p{ID_Continue}$‌‍]/u.test(char);
271
+ }
272
+ };
273
+
274
+ // shared/syntax/src/parser/base.ts
275
+ var BaseParser = class {
276
+ static {
277
+ __name(this, "BaseParser");
278
+ }
279
+ source;
280
+ scanner;
281
+ options;
282
+ token;
283
+ /** End of the last consumed token. */
284
+ previous = 0;
285
+ /** Whether `in` is a binary operator here (not in a for-in head). */
286
+ allowIn = true;
287
+ constructor(source, options) {
288
+ this.source = source;
289
+ this.options = options;
290
+ this.scanner = new Scanner(source);
291
+ this.token = this.scanner.next();
292
+ }
293
+ get comments() {
294
+ return this.scanner.comments;
295
+ }
296
+ next() {
297
+ const token = this.token;
298
+ if (token.type === "invalid") this.raise(token.value, token.start);
299
+ this.previous = token.end;
300
+ this.token = this.scanner.next();
301
+ return token;
302
+ }
303
+ /** Replaces the current token with a rescanned one. */
304
+ replace(token) {
305
+ this.token = token;
306
+ }
307
+ /** Continues scanning from a position, discarding the current token. */
308
+ rescan(position) {
309
+ this.scanner.reset(position);
310
+ this.token = this.scanner.next();
311
+ }
312
+ is(value, token = this.token) {
313
+ return token.value === value && (token.type === "punct" || token.type === "name");
314
+ }
315
+ isName(token = this.token) {
316
+ return token.type === "name";
317
+ }
318
+ eat(value) {
319
+ if (!this.is(value)) return false;
320
+ this.next();
321
+ return true;
322
+ }
323
+ expect(value) {
324
+ if (!this.is(value)) this.unexpected(`Expected "${value}"`);
325
+ return this.next();
326
+ }
327
+ /** Peeks the token after the current one. */
328
+ peek() {
329
+ const snapshot = this.snapshot();
330
+ this.next();
331
+ const token = this.token;
332
+ this.restore(snapshot);
333
+ return token;
334
+ }
335
+ snapshot() {
336
+ return {
337
+ position: this.scanner.position,
338
+ token: this.token,
339
+ previous: this.previous
340
+ };
341
+ }
342
+ restore({ position, token, previous }) {
343
+ this.scanner.reset(position);
344
+ this.token = token;
345
+ this.previous = previous;
346
+ }
347
+ /** Runs a speculative parse, undoing it when it fails. */
348
+ attempt(parse2) {
349
+ const snapshot = this.snapshot();
350
+ try {
351
+ return parse2();
352
+ } catch (error) {
353
+ if (!(error instanceof ParseError)) throw error;
354
+ this.restore(snapshot);
355
+ return void 0;
356
+ }
357
+ }
358
+ finish(type, start, fields) {
359
+ return { type, start, end: this.previous, ...fields };
360
+ }
361
+ /** Consumes a semicolon or accepts automatic semicolon insertion. */
362
+ semicolon() {
363
+ if (this.eat(";")) return;
364
+ if (this.is("}") || this.token.type === "eof" || this.token.newline)
365
+ return;
366
+ this.unexpected('Expected ";"');
367
+ }
368
+ canInsertSemicolon() {
369
+ return this.is(";") || this.is("}") || this.token.type === "eof" || this.token.newline;
370
+ }
371
+ unexpected(message = "Unexpected token") {
372
+ const { type, value, start } = this.token;
373
+ this.raise(
374
+ `${message}, got ${type === "eof" ? "end of file" : `"${value}"`}`,
375
+ start
376
+ );
377
+ }
378
+ raise(message, position) {
379
+ const before = this.source.slice(0, position);
380
+ const line = before.split("\n").length;
381
+ const column = position - before.lastIndexOf("\n");
382
+ throw new ParseError(`${message} (${line}:${column})`, position);
383
+ }
384
+ };
385
+
386
+ // shared/syntax/src/parser/types.ts
387
+ var TypeParser = class _TypeParser extends BaseParser {
388
+ static {
389
+ __name(this, "TypeParser");
390
+ }
391
+ static #keywords = /* @__PURE__ */ new Set([
392
+ "any",
393
+ "bigint",
394
+ "boolean",
395
+ "intrinsic",
396
+ "never",
397
+ "null",
398
+ "number",
399
+ "object",
400
+ "string",
401
+ "symbol",
402
+ "undefined",
403
+ "unknown",
404
+ "void"
405
+ ]);
406
+ /** Whether a conditional type may start here (not in its extends clause). */
407
+ #conditional = true;
408
+ parseIdentifier() {
409
+ if (!this.isName()) this.unexpected("Expected an identifier");
410
+ const { start, value } = this.next();
411
+ return this.finish("Identifier", start, { name: value });
412
+ }
413
+ parseString() {
414
+ if (this.token.type !== "string") this.unexpected("Expected a string");
415
+ const { start, value } = this.next();
416
+ return this.finish("StringLiteral", start, { raw: value });
417
+ }
418
+ /** Parses a template, with parts parsed by the given parser. */
419
+ parseTemplateParts(part) {
420
+ const quasis = [];
421
+ const parts = [];
422
+ for (; ; ) {
423
+ const token = this.token;
424
+ if (token.type !== "template")
425
+ this.unexpected("Expected a template");
426
+ const tail = token.value.endsWith("`") && (token.value.length > 1 || quasis.length > 0);
427
+ const raw = token.value.slice(1, tail ? -1 : -2);
428
+ quasis.push({
429
+ type: "TemplateElement",
430
+ start: token.start,
431
+ end: token.end,
432
+ raw,
433
+ tail
434
+ });
435
+ this.next();
436
+ if (tail) return { quasis, parts };
437
+ parts.push(part());
438
+ if (!this.is("}")) this.unexpected('Expected "}" in template');
439
+ this.replace(
440
+ this.scanner.template(this.token.start, this.token.newline)
441
+ );
442
+ }
443
+ }
444
+ parseTypeAnnotation() {
445
+ if (!this.eat(":")) return null;
446
+ return this.parseType();
447
+ }
448
+ parseType() {
449
+ const start = this.token.start;
450
+ if (this.is("<")) return this.#functionType(start, false);
451
+ if (this.is("new") || this.is("abstract") && this.is("new", this.peek()))
452
+ return this.#functionType(start, true);
453
+ if (this.is("(")) {
454
+ const fn = this.attempt(() => this.#functionType(start, false));
455
+ if (fn) return fn;
456
+ }
457
+ const check = this.#union();
458
+ if (!this.#conditional || this.token.newline || !this.is("extends"))
459
+ return check;
460
+ this.next();
461
+ this.#conditional = false;
462
+ const extendsType = this.parseType();
463
+ this.#conditional = true;
464
+ this.expect("?");
465
+ const trueType = this.parseType();
466
+ this.expect(":");
467
+ const falseType = this.parseType();
468
+ return this.finish("TSConditionalType", start, {
469
+ checkType: check,
470
+ extendsType,
471
+ trueType,
472
+ falseType
473
+ });
474
+ }
475
+ parseReturnType() {
476
+ const start = this.token.start;
477
+ const asserts = this.is("asserts") && (this.isName(this.peek()) || this.is("this", this.peek())) && !this.peek().newline;
478
+ if (asserts) this.next();
479
+ if ((this.isName() || this.is("this")) && (asserts || this.is("is", this.peek()))) {
480
+ const parameter = this.is("this") ? this.#thisType() : this.parseIdentifier();
481
+ let typeAnnotation = null;
482
+ if (!this.token.newline && this.eat("is"))
483
+ typeAnnotation = this.parseType();
484
+ if (asserts || typeAnnotation)
485
+ return this.finish("TSTypePredicate", start, {
486
+ parameterName: parameter,
487
+ typeAnnotation,
488
+ asserts
489
+ });
490
+ return parameter;
491
+ }
492
+ return this.parseType();
493
+ }
494
+ parseTypeParameters() {
495
+ if (!this.is("<")) return null;
496
+ const start = this.next().start;
497
+ const params = [];
498
+ while (!this.is(">")) {
499
+ const paramStart = this.token.start;
500
+ const modifiers = { in: false, out: false, const: false };
501
+ while (["in", "out", "const"].includes(this.token.value) && this.isName(this.peek())) {
502
+ modifiers[this.token.value] = true;
503
+ this.next();
504
+ }
505
+ const { value: name } = this.next();
506
+ const constraint = this.eat("extends") ? this.parseType() : null;
507
+ const fallback = this.eat("=") ? this.parseType() : null;
508
+ params.push(
509
+ this.finish("TSTypeParameter", paramStart, {
510
+ name,
511
+ constraint,
512
+ default: fallback,
513
+ ...modifiers
514
+ })
515
+ );
516
+ if (!this.eat(",")) break;
517
+ }
518
+ this.expect(">");
519
+ return this.finish("TSTypeParameterDeclaration", start, { params });
520
+ }
521
+ parseTypeArguments() {
522
+ const start = this.expect("<").start;
523
+ const params = [];
524
+ while (!this.is(">")) {
525
+ params.push(this.parseType());
526
+ if (!this.eat(",")) break;
527
+ }
528
+ this.expect(">");
529
+ return this.finish("TSTypeParameterInstantiation", start, { params });
530
+ }
531
+ parseEntityName() {
532
+ const start = this.token.start;
533
+ let name = this.parseIdentifier();
534
+ while (this.is(".") && this.isName(this.peek())) {
535
+ this.next();
536
+ name = this.finish("TSQualifiedName", start, {
537
+ left: name,
538
+ right: this.parseIdentifier()
539
+ });
540
+ }
541
+ return name;
542
+ }
543
+ /** Parses `{ ... }` members of an interface or a type literal. */
544
+ parseTypeMembers() {
545
+ this.expect("{");
546
+ const members = [];
547
+ while (!this.is("}")) {
548
+ members.push(this.#member());
549
+ if (!this.eat(";") && !this.eat(",") && !this.is("}") && !this.token.newline)
550
+ this.unexpected();
551
+ }
552
+ this.expect("}");
553
+ return members;
554
+ }
555
+ #functionType(start, construct) {
556
+ const abstract = construct && this.eat("abstract");
557
+ if (construct) this.expect("new");
558
+ const typeParameters = this.parseTypeParameters();
559
+ const params = this.parseParameters();
560
+ this.expect("=>");
561
+ const returnType = this.parseReturnType();
562
+ return construct ? this.finish("TSConstructorType", start, {
563
+ typeParameters,
564
+ params,
565
+ returnType,
566
+ abstract
567
+ }) : this.finish("TSFunctionType", start, {
568
+ typeParameters,
569
+ params,
570
+ returnType
571
+ });
572
+ }
573
+ #union() {
574
+ return this.#list(
575
+ "|",
576
+ "TSUnionType",
577
+ () => this.#list("&", "TSIntersectionType", () => this.#operator())
578
+ );
579
+ }
580
+ #list(operator, type, parse2) {
581
+ const start = this.token.start;
582
+ const leading = this.eat(operator);
583
+ const types = [parse2()];
584
+ while (this.eat(operator)) types.push(parse2());
585
+ if (types.length === 1 && !leading) return types[0];
586
+ return this.finish(type, start, { types });
587
+ }
588
+ #operator() {
589
+ const start = this.token.start;
590
+ if (["keyof", "unique", "readonly"].includes(this.token.value) && this.isName() && !this.#ends(this.peek())) {
591
+ const operator = this.next().value;
592
+ return this.finish("TSTypeOperator", start, {
593
+ operator,
594
+ typeAnnotation: this.#operator()
595
+ });
596
+ }
597
+ if (this.is("infer") && this.isName(this.peek())) {
598
+ this.next();
599
+ const name = this.next().value;
600
+ const constraint = this.is("extends") ? this.attempt(() => {
601
+ this.next();
602
+ const conditional = this.#conditional;
603
+ this.#conditional = false;
604
+ const type2 = this.parseType();
605
+ this.#conditional = conditional;
606
+ if (conditional && this.is("?")) this.unexpected();
607
+ return type2;
608
+ }) ?? null : null;
609
+ return this.finish("TSInferType", start, { name, constraint });
610
+ }
611
+ let type = this.#primary();
612
+ while (!this.token.newline && this.is("[")) {
613
+ this.next();
614
+ if (this.eat("]"))
615
+ type = this.finish("TSArrayType", start, { elementType: type });
616
+ else {
617
+ const indexType = this.parseType();
618
+ this.expect("]");
619
+ type = this.finish("TSIndexedAccessType", start, {
620
+ objectType: type,
621
+ indexType
622
+ });
623
+ }
624
+ }
625
+ return type;
626
+ }
627
+ /** Whether the token ends a type, so a preceding keyword is a plain name. */
628
+ #ends(token) {
629
+ return [")", "]", ",", ";", ">", "=", "|", "&", "}", "?", ":"].includes(
630
+ token.value
631
+ ) || token.type === "eof";
632
+ }
633
+ /** Parses a primary type; brackets inside allow conditional types again. */
634
+ #primary() {
635
+ const conditional = this.#conditional;
636
+ this.#conditional = true;
637
+ try {
638
+ return this.#primaryType();
639
+ } finally {
640
+ this.#conditional = conditional;
641
+ }
642
+ }
643
+ #primaryType() {
644
+ const { start, type, value } = this.token;
645
+ if (type === "string" || type === "number")
646
+ return this.#literal(start, () => this.next());
647
+ if (type === "template") {
648
+ const { quasis, parts } = this.parseTemplateParts(
649
+ () => this.parseType()
650
+ );
651
+ return this.finish("TSTemplateLiteralType", start, {
652
+ quasis,
653
+ types: parts
654
+ });
655
+ }
656
+ if (this.is("-") && this.peek().type === "number")
657
+ return this.#literal(start, () => this.next());
658
+ switch (value) {
659
+ case "(": {
660
+ this.next();
661
+ const inner = this.parseType();
662
+ this.expect(")");
663
+ inner.parenthesized = true;
664
+ return inner;
665
+ }
666
+ case "[":
667
+ return this.#tuple(start);
668
+ case "{":
669
+ return this.#mapped(start) ?? this.finish("TSTypeLiteral", start, {
670
+ members: this.parseTypeMembers()
671
+ });
672
+ }
673
+ if (!this.isName()) this.unexpected("Expected a type");
674
+ if ((value === "true" || value === "false") && this.isName())
675
+ return this.#literal(start, () => this.next());
676
+ if (value === "this") return this.#thisType();
677
+ if (value === "typeof") {
678
+ this.next();
679
+ const exprName = this.is("import") ? this.#importType(this.token.start) : this.parseEntityName();
680
+ const typeArguments2 = !this.token.newline && this.is("<") ? this.parseTypeArguments() : null;
681
+ return this.finish("TSTypeQuery", start, {
682
+ exprName,
683
+ typeArguments: typeArguments2
684
+ });
685
+ }
686
+ if (value === "import") return this.#importType(start);
687
+ if (_TypeParser.#keywords.has(value) && !this.is(".", this.peek())) {
688
+ this.next();
689
+ return this.finish("TSKeyword", start, { name: value });
690
+ }
691
+ const typeName = this.parseEntityName();
692
+ const typeArguments = !this.token.newline && this.is("<") ? this.parseTypeArguments() : null;
693
+ return this.finish("TSTypeReference", start, {
694
+ typeName,
695
+ typeArguments
696
+ });
697
+ }
698
+ #literal(start, consume) {
699
+ const negative = this.is("-");
700
+ if (negative) this.next();
701
+ const token = this.token;
702
+ consume();
703
+ let literal = token.type === "string" ? this.finish("StringLiteral", token.start, {
704
+ raw: token.value
705
+ }) : token.type === "number" ? this.finish("NumericLiteral", token.start, {
706
+ raw: token.value
707
+ }) : this.finish("BooleanLiteral", token.start, {
708
+ value: token.value === "true"
709
+ });
710
+ if (negative)
711
+ literal = this.finish("UnaryExpression", start, {
712
+ operator: "-",
713
+ argument: literal
714
+ });
715
+ return this.finish("TSLiteralType", start, { literal });
716
+ }
717
+ #thisType() {
718
+ const { start } = this.expect("this");
719
+ return this.finish("TSThisType", start, {});
720
+ }
721
+ #importType(start) {
722
+ this.expect("import");
723
+ this.expect("(");
724
+ const argument = this.parseType();
725
+ const options = this.eat(",") && !this.is(")") ? this.parseAssignment() : null;
726
+ this.eat(",");
727
+ this.expect(")");
728
+ let qualifier = null;
729
+ if (this.eat(".")) qualifier = this.parseEntityName();
730
+ const typeArguments = this.is("<") ? this.parseTypeArguments() : null;
731
+ return this.finish("TSImportType", start, {
732
+ argument,
733
+ options,
734
+ qualifier,
735
+ typeArguments
736
+ });
737
+ }
738
+ #tuple(start) {
739
+ this.expect("[");
740
+ const elementTypes = [];
741
+ while (!this.is("]")) {
742
+ const elementStart = this.token.start;
743
+ const rest = this.eat("...");
744
+ let element;
745
+ if (this.isName() && (this.is(":", this.peek()) || this.is("?", this.peek()) && this.#labelled())) {
746
+ const label = this.parseIdentifier();
747
+ const optional = this.eat("?");
748
+ this.expect(":");
749
+ element = this.finish("TSNamedTupleMember", elementStart, {
750
+ label,
751
+ elementType: this.parseType(),
752
+ optional
753
+ });
754
+ } else {
755
+ element = this.parseType();
756
+ if (this.eat("?"))
757
+ element = this.finish("TSOptionalType", elementStart, {
758
+ typeAnnotation: element
759
+ });
760
+ }
761
+ if (rest)
762
+ element = this.finish("TSRestType", elementStart, {
763
+ typeAnnotation: element
764
+ });
765
+ elementTypes.push(element);
766
+ if (!this.eat(",")) break;
767
+ }
768
+ this.expect("]");
769
+ return this.finish("TSTupleType", start, { elementTypes });
770
+ }
771
+ /** Whether `name ?` starts a labelled tuple member `name?: T`. */
772
+ #labelled() {
773
+ const snapshot = this.snapshot();
774
+ this.next();
775
+ this.next();
776
+ const labelled = this.is(":");
777
+ this.restore(snapshot);
778
+ return labelled;
779
+ }
780
+ #mapped(start) {
781
+ return this.attempt(() => {
782
+ this.expect("{");
783
+ let readonly = null;
784
+ if (this.is("+") || this.is("-")) readonly = this.next().value;
785
+ if (this.is("readonly")) {
786
+ this.next();
787
+ readonly = readonly ? readonly : "true";
788
+ } else if (readonly) this.unexpected();
789
+ this.expect("[");
790
+ const { value: key } = this.next();
791
+ this.expect("in");
792
+ const constraint = this.parseType();
793
+ const nameType = this.eat("as") ? this.parseType() : null;
794
+ this.expect("]");
795
+ let optional = null;
796
+ if (this.is("+") || this.is("-")) {
797
+ optional = this.next().value;
798
+ this.expect("?");
799
+ } else if (this.eat("?")) optional = "true";
800
+ const typeAnnotation = this.parseTypeAnnotation();
801
+ this.eat(";") || this.eat(",");
802
+ this.expect("}");
803
+ return this.finish("TSMappedType", start, {
804
+ key,
805
+ constraint,
806
+ nameType,
807
+ typeAnnotation,
808
+ optional,
809
+ readonly
810
+ });
811
+ });
812
+ }
813
+ #member() {
814
+ const start = this.token.start;
815
+ if (this.is("(") || this.is("<"))
816
+ return this.#signature(start, "TSCallSignatureDeclaration");
817
+ if (this.is("new") && (this.is("(", this.peek()) || this.is("<", this.peek()))) {
818
+ this.next();
819
+ return this.#signature(start, "TSConstructSignatureDeclaration");
820
+ }
821
+ const readonly = this.is("readonly") && this.#modifies();
822
+ if (readonly) this.next();
823
+ if (this.is("[")) {
824
+ const index = this.attempt(
825
+ () => this.#index(start, readonly, false)
826
+ );
827
+ if (index) return index;
828
+ }
829
+ let kind = "method";
830
+ if ((this.is("get") || this.is("set")) && this.#modifies())
831
+ kind = this.next().value;
832
+ const { key, computed } = this.parsePropertyKey();
833
+ const optional = this.eat("?");
834
+ if (this.is("(") || this.is("<")) {
835
+ const typeParameters = this.parseTypeParameters();
836
+ const params = this.parseParameters();
837
+ const returnType = this.eat(":") ? this.parseReturnType() : null;
838
+ return this.finish("TSMethodSignature", start, {
839
+ key,
840
+ computed,
841
+ optional,
842
+ kind,
843
+ typeParameters,
844
+ params,
845
+ returnType
846
+ });
847
+ }
848
+ const typeAnnotation = this.parseTypeAnnotation();
849
+ return this.finish("TSPropertySignature", start, {
850
+ key,
851
+ computed,
852
+ optional,
853
+ readonly,
854
+ typeAnnotation
855
+ });
856
+ }
857
+ /** Whether the current word modifies a following member name. */
858
+ #modifies() {
859
+ const next = this.peek();
860
+ return !next.newline && !["(", ")", ":", "?", ";", ",", "}", "=", "<", "!"].includes(
861
+ next.value
862
+ );
863
+ }
864
+ #signature(start, type) {
865
+ const typeParameters = this.parseTypeParameters();
866
+ const params = this.parseParameters();
867
+ const returnType = this.eat(":") ? this.parseReturnType() : null;
868
+ return this.finish(type, start, { typeParameters, params, returnType });
869
+ }
870
+ /** Parses `[key: T]: U` after optional modifiers. */
871
+ parseIndexSignature(start, readonly, isStatic) {
872
+ return this.attempt(() => this.#index(start, readonly, isStatic));
873
+ }
874
+ #index(start, readonly, isStatic) {
875
+ this.expect("[");
876
+ const parameters = [];
877
+ do {
878
+ const paramStart = this.token.start;
879
+ const { value: name } = this.next();
880
+ this.expect(":");
881
+ const typeAnnotation2 = this.parseType();
882
+ parameters.push(
883
+ this.finish("Identifier", paramStart, { name, typeAnnotation: typeAnnotation2 })
884
+ );
885
+ } while (this.eat(","));
886
+ this.expect("]");
887
+ const typeAnnotation = this.parseTypeAnnotation();
888
+ return this.finish("TSIndexSignature", start, {
889
+ parameters,
890
+ typeAnnotation,
891
+ readonly,
892
+ static: isStatic
893
+ });
894
+ }
895
+ };
896
+
897
+ // shared/syntax/src/parser/expressions.ts
898
+ var ExpressionParser = class _ExpressionParser extends TypeParser {
899
+ static {
900
+ __name(this, "ExpressionParser");
901
+ }
902
+ static #precedence = {
903
+ "??": 1,
904
+ "||": 2,
905
+ "&&": 3,
906
+ "|": 4,
907
+ "^": 5,
908
+ "&": 6,
909
+ "==": 7,
910
+ "!=": 7,
911
+ "===": 7,
912
+ "!==": 7,
913
+ "<": 8,
914
+ ">": 8,
915
+ "<=": 8,
916
+ ">=": 8,
917
+ instanceof: 8,
918
+ in: 8,
919
+ as: 8,
920
+ satisfies: 8,
921
+ "<<": 9,
922
+ ">>": 9,
923
+ ">>>": 9,
924
+ "+": 10,
925
+ "-": 10,
926
+ "*": 11,
927
+ "/": 11,
928
+ "%": 11,
929
+ "**": 12
930
+ };
931
+ static #assignments = /* @__PURE__ */ new Set([
932
+ "=",
933
+ "+=",
934
+ "-=",
935
+ "*=",
936
+ "/=",
937
+ "%=",
938
+ "**=",
939
+ "<<=",
940
+ ">>=",
941
+ ">>>=",
942
+ "&=",
943
+ "|=",
944
+ "^=",
945
+ "&&=",
946
+ "||=",
947
+ "??="
948
+ ]);
949
+ static #modifiers = /* @__PURE__ */ new Set([
950
+ "abstract",
951
+ "accessor",
952
+ "async",
953
+ "declare",
954
+ "override",
955
+ "private",
956
+ "protected",
957
+ "public",
958
+ "readonly",
959
+ "static"
960
+ ]);
961
+ /** Tokens after `f<T>` that make it an instantiation, not a comparison. */
962
+ static #afterTypeArguments = /* @__PURE__ */ new Set([
963
+ ")",
964
+ "]",
965
+ "}",
966
+ ",",
967
+ ";",
968
+ ".",
969
+ "?.",
970
+ ":",
971
+ "=",
972
+ "==",
973
+ "===",
974
+ "!=",
975
+ "!==",
976
+ "&&",
977
+ "||",
978
+ "??",
979
+ "?",
980
+ "{",
981
+ "extends",
982
+ "implements"
983
+ ]);
984
+ parseExpression() {
985
+ const start = this.token.start;
986
+ const first = this.parseAssignment();
987
+ if (!this.is(",")) return first;
988
+ const expressions = [first];
989
+ while (this.eat(",")) expressions.push(this.parseAssignment());
990
+ return this.finish("SequenceExpression", start, { expressions });
991
+ }
992
+ parseAssignment() {
993
+ const start = this.token.start;
994
+ if (this.is("yield") && this.#yields()) return this.#yield(start);
995
+ const arrow = this.#tryArrow(start);
996
+ if (arrow) return arrow;
997
+ const left = this.#conditional(start);
998
+ if (this.token.type === "punct" && this.is(">"))
999
+ this.replace(this.scanner.greater(this.token));
1000
+ if (this.token.type === "punct" && this.is("<"))
1001
+ this.replace(this.scanner.less(this.token));
1002
+ if (this.token.type !== "punct" || !_ExpressionParser.#assignments.has(this.token.value))
1003
+ return left;
1004
+ const operator = this.next().value;
1005
+ const right = this.parseAssignment();
1006
+ return this.finish("AssignmentExpression", start, {
1007
+ operator,
1008
+ left: operator === "=" ? this.toPattern(left) : left,
1009
+ right
1010
+ });
1011
+ }
1012
+ /** Converts an object or array literal used as an assignment target into a pattern. */
1013
+ toPattern(node) {
1014
+ switch (node.type) {
1015
+ case "ObjectExpression":
1016
+ return {
1017
+ ...node,
1018
+ type: "ObjectPattern",
1019
+ typeAnnotation: null,
1020
+ properties: node.properties.map((property) => {
1021
+ if (property.type === "SpreadElement")
1022
+ return {
1023
+ ...property,
1024
+ type: "RestElement",
1025
+ argument: this.toPattern(property.argument),
1026
+ typeAnnotation: null
1027
+ };
1028
+ if (property.type === "Property" && !property.shorthand)
1029
+ return {
1030
+ ...property,
1031
+ value: this.toPattern(property.value)
1032
+ };
1033
+ return property;
1034
+ })
1035
+ };
1036
+ case "ArrayExpression":
1037
+ return {
1038
+ ...node,
1039
+ type: "ArrayPattern",
1040
+ typeAnnotation: null,
1041
+ elements: node.elements.map((element) => {
1042
+ if (!element) return element;
1043
+ if (element.type === "SpreadElement")
1044
+ return {
1045
+ ...element,
1046
+ type: "RestElement",
1047
+ argument: this.toPattern(element.argument),
1048
+ typeAnnotation: null
1049
+ };
1050
+ return this.toPattern(element);
1051
+ })
1052
+ };
1053
+ case "AssignmentExpression":
1054
+ return node.operator === "=" ? {
1055
+ ...node,
1056
+ type: "AssignmentPattern",
1057
+ left: this.toPattern(node.left)
1058
+ } : node;
1059
+ default:
1060
+ return node;
1061
+ }
1062
+ }
1063
+ /** Parses with `in` allowed or not, restoring the previous setting. */
1064
+ withIn(allow, parse2) {
1065
+ const allowIn = this.allowIn;
1066
+ this.allowIn = allow;
1067
+ try {
1068
+ return parse2();
1069
+ } finally {
1070
+ this.allowIn = allowIn;
1071
+ }
1072
+ }
1073
+ #yields() {
1074
+ const next = this.peek();
1075
+ return !next.newline || next.value === "*";
1076
+ }
1077
+ #yield(start) {
1078
+ this.next();
1079
+ const delegate = this.eat("*");
1080
+ const argument = delegate || !this.token.newline && !this.#ends() ? this.parseAssignment() : null;
1081
+ return this.finish("YieldExpression", start, { argument, delegate });
1082
+ }
1083
+ /** Whether the current token cannot start an expression. */
1084
+ #ends() {
1085
+ return [")", "]", "}", ",", ";", ":", "=>"].includes(this.token.value) || this.token.type === "eof";
1086
+ }
1087
+ #tryArrow(start) {
1088
+ const next = this.isName() || this.is("(") || this.is("<") ? this.peek() : void 0;
1089
+ if (!next) return;
1090
+ if (this.is("async") && !next.newline && (this.isName(next) || this.is("(", next) || this.is("<", next))) {
1091
+ const arrow = this.attempt(() => {
1092
+ this.next();
1093
+ return this.#arrow(start, true);
1094
+ });
1095
+ if (arrow) return arrow;
1096
+ }
1097
+ if (this.isName() && this.is("=>", next) && !next.newline)
1098
+ return this.#arrow(start, false);
1099
+ if (this.is("(") || this.is("<") && this.options.typescript)
1100
+ return this.attempt(() => this.#arrow(start, false));
1101
+ }
1102
+ #arrow(start, async) {
1103
+ let typeParameters = null;
1104
+ let params;
1105
+ let returnType = null;
1106
+ if (this.isName() && !this.is("(")) params = [this.parseIdentifier()];
1107
+ else {
1108
+ typeParameters = this.parseTypeParameters();
1109
+ params = this.parseParameters();
1110
+ if (this.eat(":")) returnType = this.parseReturnType();
1111
+ }
1112
+ if (this.token.newline || !this.is("=>"))
1113
+ this.unexpected('Expected "=>"');
1114
+ this.next();
1115
+ const body = this.is("{") ? this.parseBlock() : this.parseAssignment();
1116
+ return this.finish("ArrowFunctionExpression", start, {
1117
+ id: null,
1118
+ params,
1119
+ body,
1120
+ async,
1121
+ generator: false,
1122
+ typeParameters,
1123
+ returnType
1124
+ });
1125
+ }
1126
+ #conditional(start) {
1127
+ const test = this.#binary(0);
1128
+ if (!this.is("?")) return test;
1129
+ this.next();
1130
+ const consequent = this.withIn(true, () => this.parseAssignment());
1131
+ this.expect(":");
1132
+ const alternate = this.parseAssignment();
1133
+ return this.finish("ConditionalExpression", start, {
1134
+ test,
1135
+ consequent,
1136
+ alternate
1137
+ });
1138
+ }
1139
+ #binary(minimum) {
1140
+ const start = this.token.start;
1141
+ let left = this.#unary();
1142
+ for (; ; ) {
1143
+ if (this.is(">")) this.replace(this.scanner.greater(this.token));
1144
+ else if (this.is("<") && this.token.type === "punct")
1145
+ this.replace(this.scanner.less(this.token));
1146
+ const { value, type } = this.token;
1147
+ const precedence = _ExpressionParser.#precedence[value];
1148
+ if (precedence === void 0 || type !== "punct" && type !== "name")
1149
+ break;
1150
+ if (type === "name" && !["in", "instanceof", "as", "satisfies"].includes(value))
1151
+ break;
1152
+ if (value === "in" && !this.allowIn) break;
1153
+ if ((value === "as" || value === "satisfies") && (this.token.newline || !this.options.typescript))
1154
+ break;
1155
+ if (value === "**" ? precedence < minimum : precedence <= minimum)
1156
+ break;
1157
+ this.next();
1158
+ if (value === "as" || value === "satisfies") {
1159
+ const typeAnnotation = this.is("const") ? this.#constType() : this.parseType();
1160
+ left = this.finish(
1161
+ value === "as" ? "TSAsExpression" : "TSSatisfiesExpression",
1162
+ start,
1163
+ { expression: left, typeAnnotation }
1164
+ );
1165
+ continue;
1166
+ }
1167
+ const right = this.#binary(precedence);
1168
+ const logical = value === "&&" || value === "||" || value === "??";
1169
+ left = this.finish(
1170
+ logical ? "LogicalExpression" : "BinaryExpression",
1171
+ start,
1172
+ { operator: value, left, right }
1173
+ );
1174
+ }
1175
+ return left;
1176
+ }
1177
+ #constType() {
1178
+ const start = this.token.start;
1179
+ const typeName = this.parseIdentifier();
1180
+ return this.finish("TSTypeReference", start, {
1181
+ typeName,
1182
+ typeArguments: null
1183
+ });
1184
+ }
1185
+ #unary() {
1186
+ const start = this.token.start;
1187
+ const { value, type } = this.token;
1188
+ if (type === "punct" && ["!", "~", "+", "-"].includes(value)) {
1189
+ this.next();
1190
+ return this.finish("UnaryExpression", start, {
1191
+ operator: value,
1192
+ argument: this.#unary()
1193
+ });
1194
+ }
1195
+ if (type === "name" && ["typeof", "void", "delete"].includes(value) && !this.#keywordEnds()) {
1196
+ this.next();
1197
+ return this.finish("UnaryExpression", start, {
1198
+ operator: value,
1199
+ argument: this.#unary()
1200
+ });
1201
+ }
1202
+ if (type === "punct" && (value === "++" || value === "--")) {
1203
+ this.next();
1204
+ return this.finish("UpdateExpression", start, {
1205
+ operator: value,
1206
+ prefix: true,
1207
+ argument: this.#unary()
1208
+ });
1209
+ }
1210
+ if (this.is("await") && !this.#keywordEnds()) {
1211
+ this.next();
1212
+ return this.finish("AwaitExpression", start, {
1213
+ argument: this.#unary()
1214
+ });
1215
+ }
1216
+ if (this.is("<") && this.options.typescript && !this.options.jsx) {
1217
+ this.next();
1218
+ const typeAnnotation = this.is("const") ? this.#constType() : this.parseType();
1219
+ this.expect(">");
1220
+ return this.finish("TSTypeAssertion", start, {
1221
+ typeAnnotation,
1222
+ expression: this.#unary()
1223
+ });
1224
+ }
1225
+ const argument = this.parseLeftHandSide();
1226
+ if (!this.token.newline && (this.is("++") || this.is("--"))) {
1227
+ const operator = this.next().value;
1228
+ return this.finish("UpdateExpression", start, {
1229
+ operator,
1230
+ prefix: false,
1231
+ argument
1232
+ });
1233
+ }
1234
+ return argument;
1235
+ }
1236
+ /** Whether a keyword like `await` is used as a plain name here. */
1237
+ #keywordEnds() {
1238
+ const next = this.peek();
1239
+ return [")", "]", "}", ",", ";", ":", "=", ".", "?.", "=>", "?"].includes(
1240
+ next.value
1241
+ ) || next.type === "eof" || next.type === "punct" && _ExpressionParser.#assignments.has(next.value);
1242
+ }
1243
+ parseLeftHandSide() {
1244
+ const start = this.token.start;
1245
+ const expression = this.is("new") ? this.#new() : this.#primary();
1246
+ return this.#subscripts(expression, start, false);
1247
+ }
1248
+ #subscripts(object, start, noCall) {
1249
+ let expression = object;
1250
+ for (; ; ) {
1251
+ if (this.eat(".")) {
1252
+ expression = this.finish("MemberExpression", start, {
1253
+ object: expression,
1254
+ property: this.#memberName(),
1255
+ computed: false,
1256
+ optional: false
1257
+ });
1258
+ } else if (this.is("?.")) {
1259
+ if (noCall) break;
1260
+ this.next();
1261
+ if (this.is("("))
1262
+ expression = this.#call(start, expression, null, true);
1263
+ else if (this.eat("[")) {
1264
+ const property = this.withIn(
1265
+ true,
1266
+ () => this.parseExpression()
1267
+ );
1268
+ this.expect("]");
1269
+ expression = this.finish("MemberExpression", start, {
1270
+ object: expression,
1271
+ property,
1272
+ computed: true,
1273
+ optional: true
1274
+ });
1275
+ } else if (this.is("<")) {
1276
+ const typeArguments = this.parseTypeArguments();
1277
+ expression = this.#call(
1278
+ start,
1279
+ expression,
1280
+ typeArguments,
1281
+ true
1282
+ );
1283
+ } else
1284
+ expression = this.finish("MemberExpression", start, {
1285
+ object: expression,
1286
+ property: this.#memberName(),
1287
+ computed: false,
1288
+ optional: true
1289
+ });
1290
+ } else if (this.is("[")) {
1291
+ this.next();
1292
+ const property = this.withIn(
1293
+ true,
1294
+ () => this.parseExpression()
1295
+ );
1296
+ this.expect("]");
1297
+ expression = this.finish("MemberExpression", start, {
1298
+ object: expression,
1299
+ property,
1300
+ computed: true,
1301
+ optional: false
1302
+ });
1303
+ } else if (!noCall && this.is("(")) {
1304
+ expression = this.#call(start, expression, null, false);
1305
+ } else if (this.token.type === "template") {
1306
+ const quasi = this.#template();
1307
+ expression = this.finish("TaggedTemplateExpression", start, {
1308
+ tag: expression,
1309
+ quasi,
1310
+ typeArguments: null
1311
+ });
1312
+ } else if (this.options.typescript && this.is("!") && !this.token.newline) {
1313
+ this.next();
1314
+ expression = this.finish("TSNonNullExpression", start, {
1315
+ expression
1316
+ });
1317
+ } else if (this.options.typescript && this.is("<") && !this.token.newline) {
1318
+ const result = this.attempt(
1319
+ () => this.#typeArgumentsSubscript(start, expression, noCall)
1320
+ );
1321
+ if (!result) break;
1322
+ expression = result;
1323
+ } else break;
1324
+ }
1325
+ return expression;
1326
+ }
1327
+ #typeArgumentsSubscript(start, expression, noCall) {
1328
+ const typeArguments = this.parseTypeArguments();
1329
+ if (!noCall && this.is("("))
1330
+ return this.#call(start, expression, typeArguments, false);
1331
+ if (this.token.type === "template") {
1332
+ const quasi = this.#template();
1333
+ return this.finish("TaggedTemplateExpression", start, {
1334
+ tag: expression,
1335
+ quasi,
1336
+ typeArguments
1337
+ });
1338
+ }
1339
+ if (this.token.type === "eof" || this.token.newline || _ExpressionParser.#afterTypeArguments.has(this.token.value) && this.token.type !== "string")
1340
+ return this.finish("TSInstantiationExpression", start, {
1341
+ expression,
1342
+ typeArguments
1343
+ });
1344
+ return this.unexpected();
1345
+ }
1346
+ #memberName() {
1347
+ if (this.token.type === "private") {
1348
+ const { start, value } = this.next();
1349
+ return this.finish("PrivateName", start, { name: value.slice(1) });
1350
+ }
1351
+ return this.parseIdentifier();
1352
+ }
1353
+ #call(start, callee, typeArguments, optional) {
1354
+ const args = this.parseArguments();
1355
+ return this.finish("CallExpression", start, {
1356
+ callee,
1357
+ arguments: args,
1358
+ optional,
1359
+ typeArguments
1360
+ });
1361
+ }
1362
+ parseArguments() {
1363
+ this.expect("(");
1364
+ const args = [];
1365
+ while (!this.is(")")) {
1366
+ const start = this.token.start;
1367
+ if (this.eat("..."))
1368
+ args.push(
1369
+ this.finish("SpreadElement", start, {
1370
+ argument: this.withIn(
1371
+ true,
1372
+ () => this.parseAssignment()
1373
+ )
1374
+ })
1375
+ );
1376
+ else args.push(this.withIn(true, () => this.parseAssignment()));
1377
+ if (!this.eat(",")) break;
1378
+ }
1379
+ this.expect(")");
1380
+ return args;
1381
+ }
1382
+ #new() {
1383
+ const start = this.expect("new").start;
1384
+ if (this.eat(".")) {
1385
+ const property = this.parseIdentifier().name;
1386
+ return this.finish("MetaProperty", start, {
1387
+ meta: "new",
1388
+ property
1389
+ });
1390
+ }
1391
+ const calleeStart = this.token.start;
1392
+ const inner = this.is("new") ? this.#new() : this.#primary();
1393
+ let callee = this.#subscripts(inner, calleeStart, true);
1394
+ let typeArguments = null;
1395
+ if (callee.type === "TSInstantiationExpression") {
1396
+ typeArguments = callee.typeArguments;
1397
+ callee = callee.expression;
1398
+ } else if (this.options.typescript && this.is("<")) {
1399
+ typeArguments = this.attempt(() => this.parseTypeArguments()) ?? null;
1400
+ }
1401
+ const parentheses = this.is("(");
1402
+ const args = parentheses ? this.parseArguments() : [];
1403
+ return this.finish("NewExpression", start, {
1404
+ callee,
1405
+ arguments: args,
1406
+ typeArguments,
1407
+ parentheses
1408
+ });
1409
+ }
1410
+ #primary() {
1411
+ const token = this.token;
1412
+ const { start, type, value } = token;
1413
+ switch (type) {
1414
+ case "number":
1415
+ this.next();
1416
+ return this.finish("NumericLiteral", start, { raw: value });
1417
+ case "string":
1418
+ this.next();
1419
+ return this.finish("StringLiteral", start, { raw: value });
1420
+ case "template":
1421
+ return this.#template();
1422
+ case "private":
1423
+ this.next();
1424
+ return this.finish("PrivateName", start, {
1425
+ name: value.slice(1)
1426
+ });
1427
+ case "punct":
1428
+ return this.#punctuation(start, value);
1429
+ case "name":
1430
+ return this.#word(start, value);
1431
+ default:
1432
+ return this.unexpected();
1433
+ }
1434
+ }
1435
+ #punctuation(start, value) {
1436
+ switch (value) {
1437
+ case "(": {
1438
+ this.next();
1439
+ const expression = this.withIn(
1440
+ true,
1441
+ () => this.parseExpression()
1442
+ );
1443
+ this.expect(")");
1444
+ expression.parenthesized = true;
1445
+ return expression;
1446
+ }
1447
+ case "[":
1448
+ return this.#array(start);
1449
+ case "{":
1450
+ return this.#object(start);
1451
+ case "/":
1452
+ case "/=": {
1453
+ const regex = this.scanner.regex(this.token);
1454
+ this.replace(regex);
1455
+ this.next();
1456
+ return this.finish("RegExpLiteral", start, {
1457
+ raw: regex.value
1458
+ });
1459
+ }
1460
+ case "<":
1461
+ if (!this.options.jsx) return this.unexpected();
1462
+ return this.#jsxExpression();
1463
+ case "@": {
1464
+ const decorators = this.parseDecorators();
1465
+ return this.parseClass(start, decorators, true, false, false);
1466
+ }
1467
+ default:
1468
+ return this.unexpected();
1469
+ }
1470
+ }
1471
+ #word(start, value) {
1472
+ switch (value) {
1473
+ case "function":
1474
+ return this.parseFunction(start, false, true);
1475
+ case "class":
1476
+ return this.parseClass(start, [], true, false, false);
1477
+ case "this":
1478
+ this.next();
1479
+ return this.finish("ThisExpression", start, {});
1480
+ case "super":
1481
+ this.next();
1482
+ return this.finish("Super", start, {});
1483
+ case "null":
1484
+ this.next();
1485
+ return this.finish("NullLiteral", start, {});
1486
+ case "true":
1487
+ case "false":
1488
+ this.next();
1489
+ return this.finish("BooleanLiteral", start, {
1490
+ value: value === "true"
1491
+ });
1492
+ case "async": {
1493
+ const next = this.peek();
1494
+ if (this.is("function", next) && !next.newline) {
1495
+ this.next();
1496
+ return this.parseFunction(start, true, true);
1497
+ }
1498
+ break;
1499
+ }
1500
+ case "import":
1501
+ return this.#import(start);
1502
+ }
1503
+ return this.parseIdentifier();
1504
+ }
1505
+ #import(start) {
1506
+ const next = this.peek();
1507
+ if (this.is(".", next)) {
1508
+ this.next();
1509
+ this.next();
1510
+ const property = this.parseIdentifier().name;
1511
+ if (this.is("(")) {
1512
+ this.next();
1513
+ return this.#importCall(start, property);
1514
+ }
1515
+ return this.finish("MetaProperty", start, {
1516
+ meta: "import",
1517
+ property
1518
+ });
1519
+ }
1520
+ this.next();
1521
+ this.expect("(");
1522
+ return this.#importCall(start, null);
1523
+ }
1524
+ #importCall(start, phase) {
1525
+ const source = this.withIn(true, () => this.parseAssignment());
1526
+ const options = this.eat(",") && !this.is(")") ? this.parseAssignment() : null;
1527
+ this.eat(",");
1528
+ this.expect(")");
1529
+ return this.finish("ImportExpression", start, {
1530
+ source,
1531
+ options,
1532
+ phase
1533
+ });
1534
+ }
1535
+ #template() {
1536
+ const start = this.token.start;
1537
+ const { quasis, parts } = this.parseTemplateParts(
1538
+ () => this.withIn(true, () => this.parseExpression())
1539
+ );
1540
+ return this.finish("TemplateLiteral", start, {
1541
+ quasis,
1542
+ expressions: parts
1543
+ });
1544
+ }
1545
+ #array(start) {
1546
+ this.expect("[");
1547
+ const elements = [];
1548
+ while (!this.is("]")) {
1549
+ if (this.is(",")) {
1550
+ this.next();
1551
+ elements.push(null);
1552
+ continue;
1553
+ }
1554
+ const elementStart = this.token.start;
1555
+ if (this.eat("..."))
1556
+ elements.push(
1557
+ this.finish("SpreadElement", elementStart, {
1558
+ argument: this.withIn(
1559
+ true,
1560
+ () => this.parseAssignment()
1561
+ )
1562
+ })
1563
+ );
1564
+ else elements.push(this.withIn(true, () => this.parseAssignment()));
1565
+ if (!this.eat(",")) break;
1566
+ }
1567
+ this.expect("]");
1568
+ return this.finish("ArrayExpression", start, { elements });
1569
+ }
1570
+ #object(start) {
1571
+ this.expect("{");
1572
+ const properties = [];
1573
+ while (!this.is("}")) {
1574
+ properties.push(this.withIn(true, () => this.#property()));
1575
+ if (!this.eat(",")) break;
1576
+ }
1577
+ this.expect("}");
1578
+ return this.finish("ObjectExpression", start, { properties });
1579
+ }
1580
+ #property() {
1581
+ const start = this.token.start;
1582
+ if (this.eat("..."))
1583
+ return this.finish("SpreadElement", start, {
1584
+ argument: this.parseAssignment()
1585
+ });
1586
+ let async = false;
1587
+ let generator = false;
1588
+ let kind = "init";
1589
+ if (this.is("async") && this.#modifies()) {
1590
+ this.next();
1591
+ async = true;
1592
+ }
1593
+ if (this.eat("*")) generator = true;
1594
+ if ((this.is("get") || this.is("set")) && this.#modifies())
1595
+ kind = this.next().value;
1596
+ const { key, computed } = this.parsePropertyKey();
1597
+ if (this.is("(") || this.is("<")) {
1598
+ const value2 = this.#method(this.token.start, async, generator);
1599
+ return this.finish("Property", start, {
1600
+ key,
1601
+ value: value2,
1602
+ kind,
1603
+ method: kind === "init",
1604
+ shorthand: false,
1605
+ computed
1606
+ });
1607
+ }
1608
+ if (this.eat(":"))
1609
+ return this.finish("Property", start, {
1610
+ key,
1611
+ value: this.parseAssignment(),
1612
+ kind,
1613
+ method: false,
1614
+ shorthand: false,
1615
+ computed
1616
+ });
1617
+ let value = key;
1618
+ if (this.is("=")) {
1619
+ this.next();
1620
+ value = this.finish("AssignmentPattern", start, {
1621
+ left: key,
1622
+ right: this.parseAssignment()
1623
+ });
1624
+ }
1625
+ return this.finish("Property", start, {
1626
+ key,
1627
+ value,
1628
+ kind,
1629
+ method: false,
1630
+ shorthand: true,
1631
+ computed
1632
+ });
1633
+ }
1634
+ /** Whether a modifier word (async, get, set, static...) applies to a following name. */
1635
+ #modifies() {
1636
+ const next = this.peek();
1637
+ return !next.newline && !["(", ")", ":", ",", "}", "=", ";", "?", "!", "<", "."].includes(
1638
+ next.value
1639
+ ) && next.type !== "eof";
1640
+ }
1641
+ parsePropertyKey() {
1642
+ const { start, type, value } = this.token;
1643
+ if (this.is("[")) {
1644
+ this.next();
1645
+ const key = this.withIn(true, () => this.parseAssignment());
1646
+ this.expect("]");
1647
+ return { key, computed: true };
1648
+ }
1649
+ this.next();
1650
+ switch (type) {
1651
+ case "string":
1652
+ return {
1653
+ key: this.finish("StringLiteral", start, { raw: value }),
1654
+ computed: false
1655
+ };
1656
+ case "number":
1657
+ return {
1658
+ key: this.finish("NumericLiteral", start, { raw: value }),
1659
+ computed: false
1660
+ };
1661
+ case "private":
1662
+ return {
1663
+ key: this.finish("PrivateName", start, {
1664
+ name: value.slice(1)
1665
+ }),
1666
+ computed: false
1667
+ };
1668
+ case "name":
1669
+ return {
1670
+ key: this.finish("Identifier", start, { name: value }),
1671
+ computed: false
1672
+ };
1673
+ default:
1674
+ return this.raise("Expected a property name", start);
1675
+ }
1676
+ }
1677
+ #method(start, async, generator) {
1678
+ const typeParameters = this.parseTypeParameters();
1679
+ const params = this.parseParameters();
1680
+ const returnType = this.eat(":") ? this.parseReturnType() : null;
1681
+ const body = this.is("{") ? this.parseBlock() : null;
1682
+ return this.finish("FunctionExpression", start, {
1683
+ id: null,
1684
+ params,
1685
+ body,
1686
+ async,
1687
+ generator,
1688
+ typeParameters,
1689
+ returnType
1690
+ });
1691
+ }
1692
+ parseFunction(start, async, expression) {
1693
+ this.expect("function");
1694
+ const generator = this.eat("*");
1695
+ const id = this.isName() && !this.is("(") ? this.parseIdentifier() : null;
1696
+ const typeParameters = this.parseTypeParameters();
1697
+ const params = this.parseParameters();
1698
+ const returnType = this.eat(":") ? this.parseReturnType() : null;
1699
+ const parts = {
1700
+ id,
1701
+ params,
1702
+ async,
1703
+ generator,
1704
+ typeParameters,
1705
+ returnType
1706
+ };
1707
+ if (expression) {
1708
+ const body = this.is("{") || !this.options.typescript ? this.parseBlock() : null;
1709
+ return this.finish("FunctionExpression", start, { ...parts, body });
1710
+ }
1711
+ if (!this.is("{")) {
1712
+ this.semicolon();
1713
+ return this.finish("TSDeclareFunction", start, {
1714
+ ...parts,
1715
+ body: null,
1716
+ declare: false
1717
+ });
1718
+ }
1719
+ return this.finish("FunctionDeclaration", start, {
1720
+ ...parts,
1721
+ body: this.parseBlock(),
1722
+ declare: false
1723
+ });
1724
+ }
1725
+ parseParameters() {
1726
+ this.expect("(");
1727
+ const params = [];
1728
+ while (!this.is(")")) {
1729
+ params.push(this.#parameter());
1730
+ if (!this.eat(",")) break;
1731
+ }
1732
+ this.expect(")");
1733
+ return params;
1734
+ }
1735
+ #parameter() {
1736
+ const start = this.token.start;
1737
+ const decorators = this.parseDecorators();
1738
+ const modifiers = {};
1739
+ while (["public", "private", "protected", "readonly", "override"].includes(
1740
+ this.token.value
1741
+ ) && this.#modifies()) {
1742
+ const { value } = this.next();
1743
+ if (value === "readonly") modifiers.readonly = true;
1744
+ else if (value === "override") modifiers.override = true;
1745
+ else
1746
+ modifiers.accessibility = value;
1747
+ }
1748
+ const parameter = this.parseBindingElement();
1749
+ if (Object.keys(modifiers).length === 0 && decorators.length === 0)
1750
+ return parameter;
1751
+ return this.finish("TSParameterProperty", start, {
1752
+ ...modifiers,
1753
+ decorators,
1754
+ parameter
1755
+ });
1756
+ }
1757
+ /** Parses a binding target with optional `?`, type annotation and default. */
1758
+ parseBindingElement() {
1759
+ const start = this.token.start;
1760
+ if (this.eat("...")) {
1761
+ const argument = this.parseBindingTarget();
1762
+ const optional = this.eat("?");
1763
+ const typeAnnotation = this.parseTypeAnnotation();
1764
+ return this.finish("RestElement", start, {
1765
+ argument,
1766
+ typeAnnotation,
1767
+ optional
1768
+ });
1769
+ }
1770
+ const target = this.parseBindingTarget();
1771
+ if (this.eat("?")) target.optional = true;
1772
+ if (this.is(":")) {
1773
+ const typeAnnotation = this.parseTypeAnnotation();
1774
+ target.typeAnnotation = typeAnnotation;
1775
+ target.end = this.previous;
1776
+ }
1777
+ if (!this.eat("=")) return target;
1778
+ return this.finish("AssignmentPattern", start, {
1779
+ left: target,
1780
+ right: this.withIn(true, () => this.parseAssignment())
1781
+ });
1782
+ }
1783
+ parseBindingTarget() {
1784
+ const start = this.token.start;
1785
+ if (this.eat("[")) {
1786
+ const elements = [];
1787
+ while (!this.is("]")) {
1788
+ if (this.eat(",")) {
1789
+ elements.push(null);
1790
+ continue;
1791
+ }
1792
+ elements.push(this.parseBindingElement());
1793
+ if (!this.eat(",")) break;
1794
+ }
1795
+ this.expect("]");
1796
+ return this.finish("ArrayPattern", start, {
1797
+ elements,
1798
+ typeAnnotation: null
1799
+ });
1800
+ }
1801
+ if (this.eat("{")) {
1802
+ const properties = [];
1803
+ while (!this.is("}")) {
1804
+ const propertyStart = this.token.start;
1805
+ if (this.eat("...")) {
1806
+ properties.push(
1807
+ this.finish("RestElement", propertyStart, {
1808
+ argument: this.parseBindingTarget(),
1809
+ typeAnnotation: null
1810
+ })
1811
+ );
1812
+ } else {
1813
+ const { key, computed } = this.parsePropertyKey();
1814
+ if (this.eat(":")) {
1815
+ const value = this.parseBindingElement();
1816
+ properties.push(
1817
+ this.finish("Property", propertyStart, {
1818
+ key,
1819
+ value,
1820
+ kind: "init",
1821
+ method: false,
1822
+ shorthand: false,
1823
+ computed
1824
+ })
1825
+ );
1826
+ } else {
1827
+ const value = this.eat("=") ? this.finish("AssignmentPattern", propertyStart, {
1828
+ left: key,
1829
+ right: this.withIn(
1830
+ true,
1831
+ () => this.parseAssignment()
1832
+ )
1833
+ }) : key;
1834
+ properties.push(
1835
+ this.finish("Property", propertyStart, {
1836
+ key,
1837
+ value,
1838
+ kind: "init",
1839
+ method: false,
1840
+ shorthand: true,
1841
+ computed
1842
+ })
1843
+ );
1844
+ }
1845
+ }
1846
+ if (!this.eat(",")) break;
1847
+ }
1848
+ this.expect("}");
1849
+ return this.finish("ObjectPattern", start, {
1850
+ properties,
1851
+ typeAnnotation: null
1852
+ });
1853
+ }
1854
+ if (this.is("this")) {
1855
+ this.next();
1856
+ return this.finish("Identifier", start, { name: "this" });
1857
+ }
1858
+ return this.parseIdentifier();
1859
+ }
1860
+ parseDecorators() {
1861
+ const decorators = [];
1862
+ while (this.is("@")) {
1863
+ const start = this.next().start;
1864
+ let expression;
1865
+ if (this.is("(")) {
1866
+ this.next();
1867
+ expression = this.parseExpression();
1868
+ this.expect(")");
1869
+ expression.parenthesized = true;
1870
+ } else {
1871
+ const nameStart = this.token.start;
1872
+ expression = this.parseIdentifier();
1873
+ while (this.eat("."))
1874
+ expression = this.finish("MemberExpression", nameStart, {
1875
+ object: expression,
1876
+ property: this.#memberName(),
1877
+ computed: false,
1878
+ optional: false
1879
+ });
1880
+ let typeArguments = null;
1881
+ if (this.options.typescript && this.is("<"))
1882
+ typeArguments = this.parseTypeArguments();
1883
+ if (this.is("("))
1884
+ expression = this.#call(
1885
+ nameStart,
1886
+ expression,
1887
+ typeArguments,
1888
+ false
1889
+ );
1890
+ }
1891
+ decorators.push(this.finish("Decorator", start, { expression }));
1892
+ }
1893
+ return decorators;
1894
+ }
1895
+ parseClass(start, decorators, expression, abstract, declare) {
1896
+ this.expect("class");
1897
+ const id = this.isName() && !["extends", "implements"].includes(this.token.value) ? this.parseIdentifier() : null;
1898
+ const typeParameters = this.parseTypeParameters();
1899
+ let superClass = null;
1900
+ let superTypeArguments = null;
1901
+ if (this.eat("extends")) {
1902
+ superClass = this.parseLeftHandSide();
1903
+ if (superClass.type === "TSInstantiationExpression") {
1904
+ superTypeArguments = superClass.typeArguments;
1905
+ superClass = superClass.expression;
1906
+ }
1907
+ }
1908
+ const implemented = [];
1909
+ if (this.eat("implements")) {
1910
+ do
1911
+ implemented.push(this.parseType());
1912
+ while (this.eat(","));
1913
+ }
1914
+ const body = this.#classBody();
1915
+ const parts = {
1916
+ id,
1917
+ superClass,
1918
+ superTypeArguments,
1919
+ implements: implemented,
1920
+ body,
1921
+ decorators,
1922
+ typeParameters,
1923
+ abstract,
1924
+ declare
1925
+ };
1926
+ return expression ? this.finish("ClassExpression", start, parts) : this.finish("ClassDeclaration", start, parts);
1927
+ }
1928
+ #classBody() {
1929
+ const start = this.expect("{").start;
1930
+ const body = [];
1931
+ while (!this.is("}")) {
1932
+ if (this.eat(";")) continue;
1933
+ body.push(this.#member());
1934
+ }
1935
+ this.expect("}");
1936
+ return this.finish("ClassBody", start, { body });
1937
+ }
1938
+ #member() {
1939
+ const start = this.token.start;
1940
+ const decorators = this.parseDecorators();
1941
+ const modifiers = {};
1942
+ while (_ExpressionParser.#modifiers.has(this.token.value) && this.isName() && (this.#modifies() || this.is("static") && this.is("{", this.peek()))) {
1943
+ if (this.is("static") && this.is("{", this.peek())) break;
1944
+ const { value: value2 } = this.next();
1945
+ if (["private", "protected", "public"].includes(value2))
1946
+ modifiers.accessibility = value2;
1947
+ else modifiers[value2] = true;
1948
+ }
1949
+ if (this.is("static") && this.is("{", this.peek())) {
1950
+ this.next();
1951
+ const block = this.parseBlock();
1952
+ return this.finish("StaticBlock", start, { body: block.body });
1953
+ }
1954
+ const flags = {
1955
+ static: modifiers.static === true,
1956
+ ...typeof modifiers.accessibility === "string" ? {
1957
+ accessibility: modifiers.accessibility
1958
+ } : {},
1959
+ abstract: modifiers.abstract === true,
1960
+ declare: modifiers.declare === true,
1961
+ override: modifiers.override === true,
1962
+ readonly: modifiers.readonly === true
1963
+ };
1964
+ if (this.is("[")) {
1965
+ const index = this.parseIndexSignature(
1966
+ start,
1967
+ flags.readonly,
1968
+ flags.static
1969
+ );
1970
+ if (index) {
1971
+ this.eat(";") || this.eat(",");
1972
+ return index;
1973
+ }
1974
+ }
1975
+ const generator = this.eat("*");
1976
+ let kind = "method";
1977
+ if ((this.is("get") || this.is("set")) && this.#modifies())
1978
+ kind = this.next().value;
1979
+ const { key, computed } = this.parsePropertyKey();
1980
+ if (!computed && key.type === "Identifier" && key.name === "constructor" && (this.is("(") || this.is("<")))
1981
+ kind = "constructor";
1982
+ if (!computed && key.type === "StringLiteral" && key.raw.slice(1, -1) === "constructor")
1983
+ kind = "constructor";
1984
+ const optional = this.eat("?");
1985
+ if (this.is("(") || this.is("<")) {
1986
+ const value2 = this.#method(
1987
+ this.token.start,
1988
+ modifiers.async === true,
1989
+ generator
1990
+ );
1991
+ if (!value2.body) this.semicolon();
1992
+ return this.finish("MethodDefinition", start, {
1993
+ ...flags,
1994
+ key,
1995
+ value: value2,
1996
+ kind,
1997
+ computed,
1998
+ optional,
1999
+ decorators
2000
+ });
2001
+ }
2002
+ const definite = this.eat("!");
2003
+ const typeAnnotation = this.parseTypeAnnotation();
2004
+ const value = this.eat("=") ? this.withIn(true, () => this.parseAssignment()) : null;
2005
+ this.semicolon();
2006
+ return this.finish("PropertyDefinition", start, {
2007
+ ...flags,
2008
+ key,
2009
+ value,
2010
+ computed,
2011
+ optional,
2012
+ definite,
2013
+ accessor: modifiers.accessor === true,
2014
+ typeAnnotation,
2015
+ decorators
2016
+ });
2017
+ }
2018
+ // JSX
2019
+ #jsxExpression() {
2020
+ const element = this.#jsxElement();
2021
+ this.rescan(element.end);
2022
+ this.previous = element.end;
2023
+ return element;
2024
+ }
2025
+ /** Parses an element from `<`; leaves the scanner right after its last `>`. */
2026
+ #jsxElement() {
2027
+ const start = this.expect("<").start;
2028
+ if (this.is(">")) return this.#jsxChildren(start, null, [], null);
2029
+ const name = this.#jsxName();
2030
+ const typeArguments = this.options.typescript && this.is("<") ? this.parseTypeArguments() : null;
2031
+ const attributes = [];
2032
+ while (!this.is("/") && !this.is(">")) {
2033
+ const attributeStart = this.token.start;
2034
+ if (this.is("{")) {
2035
+ this.next();
2036
+ this.expect("...");
2037
+ const argument = this.parseAssignment();
2038
+ this.expect("}");
2039
+ attributes.push(
2040
+ this.finish("JSXSpreadAttribute", attributeStart, {
2041
+ argument
2042
+ })
2043
+ );
2044
+ continue;
2045
+ }
2046
+ const attributeName = this.#jsxName();
2047
+ let value = null;
2048
+ if (this.eat("=")) value = this.#jsxValue();
2049
+ attributes.push(
2050
+ this.finish("JSXAttribute", attributeStart, {
2051
+ name: attributeName,
2052
+ value
2053
+ })
2054
+ );
2055
+ }
2056
+ if (this.is("/")) {
2057
+ this.next();
2058
+ if (!this.is(">")) this.unexpected();
2059
+ this.previous = this.token.end;
2060
+ return this.finish("JSXElement", start, {
2061
+ name,
2062
+ attributes,
2063
+ children: [],
2064
+ selfClosing: true,
2065
+ typeArguments
2066
+ });
2067
+ }
2068
+ return this.#jsxChildren(start, name, attributes, typeArguments);
2069
+ }
2070
+ #jsxName() {
2071
+ if (!this.isName()) this.unexpected("Expected a JSX name");
2072
+ this.replace(this.scanner.jsxName(this.token));
2073
+ let name = this.next().value;
2074
+ while ((this.is(".") || this.is(":")) && this.isName(this.peek())) {
2075
+ name += this.next().value;
2076
+ this.replace(this.scanner.jsxName(this.token));
2077
+ name += this.next().value;
2078
+ }
2079
+ return name;
2080
+ }
2081
+ #jsxValue() {
2082
+ const start = this.token.start;
2083
+ if (this.token.type === "string" || this.token.type === "invalid" && /["']/.test(this.source[start] ?? "")) {
2084
+ const token = this.scanner.jsxString(this.token);
2085
+ this.replace(token);
2086
+ this.next();
2087
+ return this.finish("StringLiteral", start, { raw: token.value });
2088
+ }
2089
+ if (this.is("<")) {
2090
+ const element = this.#jsxElement();
2091
+ this.rescan(element.end);
2092
+ return element;
2093
+ }
2094
+ this.expect("{");
2095
+ const expression = this.is("}") ? null : this.parseAssignment();
2096
+ this.expect("}");
2097
+ return this.finish("JSXExpressionContainer", start, { expression });
2098
+ }
2099
+ /** Parses children after the opening `>` up to and including the closing tag. */
2100
+ #jsxChildren(start, name, attributes, typeArguments) {
2101
+ if (!this.is(">")) this.unexpected();
2102
+ let position = this.token.end;
2103
+ const children = [];
2104
+ for (; ; ) {
2105
+ const char = this.source[position];
2106
+ if (char === void 0)
2107
+ this.raise("Unterminated JSX element", start);
2108
+ if (char !== "{" && char !== "<") {
2109
+ const text = this.scanner.jsxText(position);
2110
+ children.push({
2111
+ type: "JSXText",
2112
+ start: text.start,
2113
+ end: text.end,
2114
+ raw: text.value
2115
+ });
2116
+ position = text.end;
2117
+ continue;
2118
+ }
2119
+ this.rescan(position);
2120
+ if (char === "{") {
2121
+ const childStart = this.next().start;
2122
+ let child2;
2123
+ if (this.is("}"))
2124
+ child2 = this.finish("JSXExpressionContainer", childStart, {
2125
+ expression: null
2126
+ });
2127
+ else if (this.eat("...")) {
2128
+ const expression = this.parseExpression();
2129
+ child2 = this.finish("JSXSpreadChild", childStart, {
2130
+ expression
2131
+ });
2132
+ } else {
2133
+ const expression = this.parseExpression();
2134
+ child2 = this.finish("JSXExpressionContainer", childStart, {
2135
+ expression
2136
+ });
2137
+ }
2138
+ if (!this.is("}")) this.unexpected();
2139
+ child2.end = this.token.end;
2140
+ children.push(child2);
2141
+ position = this.token.end;
2142
+ continue;
2143
+ }
2144
+ if (this.is("/", this.peek())) {
2145
+ this.next();
2146
+ this.next();
2147
+ const closing = this.is(">") ? null : this.#jsxName();
2148
+ if (closing !== name)
2149
+ this.raise(
2150
+ `Expected closing tag for ${name ?? "fragment"}`,
2151
+ this.token.start
2152
+ );
2153
+ if (!this.is(">")) this.unexpected();
2154
+ this.previous = this.token.end;
2155
+ return this.finish("JSXElement", start, {
2156
+ name,
2157
+ attributes,
2158
+ children,
2159
+ selfClosing: false,
2160
+ typeArguments
2161
+ });
2162
+ }
2163
+ const child = this.#jsxElement();
2164
+ children.push(child);
2165
+ position = child.end;
2166
+ }
2167
+ }
2168
+ };
2169
+
2170
+ // shared/syntax/src/parser/statements.ts
2171
+ var Parser = class extends ExpressionParser {
2172
+ static {
2173
+ __name(this, "Parser");
2174
+ }
2175
+ parseProgram() {
2176
+ const { body, directives } = this.#list(
2177
+ true,
2178
+ () => this.token.type === "eof"
2179
+ );
2180
+ if (this.token.type !== "eof") this.unexpected();
2181
+ return {
2182
+ type: "Program",
2183
+ start: 0,
2184
+ end: this.source.length,
2185
+ body,
2186
+ directives
2187
+ };
2188
+ }
2189
+ /** Parses a whole document as one expression, for JSON. */
2190
+ parseDocument() {
2191
+ const expression = this.parseAssignment();
2192
+ if (this.token.type !== "eof") this.unexpected();
2193
+ return expression;
2194
+ }
2195
+ parseBlock() {
2196
+ const start = this.expect("{").start;
2197
+ const { body, directives } = this.#list(true, () => this.is("}"));
2198
+ this.expect("}");
2199
+ return this.finish("BlockStatement", start, { body, directives });
2200
+ }
2201
+ #list(directives, done) {
2202
+ const body = [];
2203
+ const prologue = [];
2204
+ let inPrologue = directives;
2205
+ while (!done() && this.token.type !== "eof") {
2206
+ if (inPrologue && this.token.type === "string") {
2207
+ const directive = this.attempt(() => {
2208
+ const value = this.parseString();
2209
+ if (!this.canInsertSemicolon()) this.unexpected();
2210
+ this.semicolon();
2211
+ return this.finish("Directive", value.start, { value });
2212
+ });
2213
+ if (directive) {
2214
+ prologue.push(directive);
2215
+ continue;
2216
+ }
2217
+ }
2218
+ inPrologue = false;
2219
+ body.push(this.#statement());
2220
+ }
2221
+ return { body, directives: prologue };
2222
+ }
2223
+ #statement() {
2224
+ const start = this.token.start;
2225
+ const { type, value } = this.token;
2226
+ if (type === "punct") {
2227
+ if (value === "{") return this.parseBlock();
2228
+ if (value === ";") {
2229
+ this.next();
2230
+ return this.finish("EmptyStatement", start, {});
2231
+ }
2232
+ if (value === "@") {
2233
+ const decorators = this.parseDecorators();
2234
+ if (this.is("export")) return this.#export(start, decorators);
2235
+ const abstract = this.eat("abstract");
2236
+ return this.parseClass(
2237
+ start,
2238
+ decorators,
2239
+ false,
2240
+ abstract,
2241
+ false
2242
+ );
2243
+ }
2244
+ }
2245
+ if (type === "name") {
2246
+ const statement = this.#keyword(start, value);
2247
+ if (statement) return statement;
2248
+ if (this.is(":", this.peek())) {
2249
+ const label = this.parseIdentifier();
2250
+ this.next();
2251
+ return this.finish("LabeledStatement", start, {
2252
+ label,
2253
+ body: this.#statement()
2254
+ });
2255
+ }
2256
+ }
2257
+ const expression = this.parseExpression();
2258
+ this.semicolon();
2259
+ return this.finish("ExpressionStatement", start, { expression });
2260
+ }
2261
+ #keyword(start, value) {
2262
+ const next = this.peek();
2263
+ const followedByName = this.isName(next) && !next.newline;
2264
+ switch (value) {
2265
+ case "var":
2266
+ case "const":
2267
+ if (value === "const" && this.is("enum", next))
2268
+ return this.#declaration(start, false);
2269
+ return this.#variables(start, false);
2270
+ case "let":
2271
+ if (this.isName(next) || this.is("[", next) || this.is("{", next))
2272
+ return this.#variables(start, false);
2273
+ return;
2274
+ case "using":
2275
+ if (followedByName) return this.#variables(start, false);
2276
+ return;
2277
+ case "await": {
2278
+ if (!this.is("using", next) || next.newline) return;
2279
+ const snapshot = this.snapshot();
2280
+ this.next();
2281
+ const after = this.peek();
2282
+ this.restore(snapshot);
2283
+ if (!this.isName(after) || after.newline) return;
2284
+ this.next();
2285
+ const declaration = this.#variables(start, false);
2286
+ declaration.kind = "await using";
2287
+ return declaration;
2288
+ }
2289
+ case "function":
2290
+ return this.parseFunction(start, false, false);
2291
+ case "async":
2292
+ if (this.is("function", next) && !next.newline) {
2293
+ this.next();
2294
+ return this.parseFunction(start, true, false);
2295
+ }
2296
+ return;
2297
+ case "class":
2298
+ return this.parseClass(start, [], false, false, false);
2299
+ case "if":
2300
+ return this.#if(start);
2301
+ case "for":
2302
+ return this.#for(start);
2303
+ case "while": {
2304
+ this.next();
2305
+ const test = this.#parenthesized();
2306
+ return this.finish("WhileStatement", start, {
2307
+ test,
2308
+ body: this.#statement()
2309
+ });
2310
+ }
2311
+ case "do": {
2312
+ this.next();
2313
+ const body = this.#statement();
2314
+ this.expect("while");
2315
+ const test = this.#parenthesized();
2316
+ this.eat(";");
2317
+ return this.finish("DoWhileStatement", start, { body, test });
2318
+ }
2319
+ case "return": {
2320
+ this.next();
2321
+ const argument = this.canInsertSemicolon() ? null : this.parseExpression();
2322
+ this.semicolon();
2323
+ return this.finish("ReturnStatement", start, { argument });
2324
+ }
2325
+ case "throw": {
2326
+ this.next();
2327
+ const argument = this.parseExpression();
2328
+ this.semicolon();
2329
+ return this.finish("ThrowStatement", start, { argument });
2330
+ }
2331
+ case "break":
2332
+ case "continue": {
2333
+ this.next();
2334
+ const label = !this.canInsertSemicolon() && this.isName() ? this.parseIdentifier() : null;
2335
+ this.semicolon();
2336
+ return this.finish(
2337
+ value === "break" ? "BreakStatement" : "ContinueStatement",
2338
+ start,
2339
+ { label }
2340
+ );
2341
+ }
2342
+ case "debugger":
2343
+ this.next();
2344
+ this.semicolon();
2345
+ return this.finish("DebuggerStatement", start, {});
2346
+ case "try":
2347
+ return this.#try(start);
2348
+ case "switch":
2349
+ return this.#switch(start);
2350
+ case "with": {
2351
+ this.next();
2352
+ const object = this.#parenthesized();
2353
+ return this.finish("WithStatement", start, {
2354
+ object,
2355
+ body: this.#statement()
2356
+ });
2357
+ }
2358
+ case "import":
2359
+ if (this.is("(", next) || this.is(".", next)) return;
2360
+ return this.#import(start);
2361
+ case "export":
2362
+ return this.#export(start, []);
2363
+ }
2364
+ if (this.options.typescript) return this.#declaration(start, false);
2365
+ return void 0;
2366
+ }
2367
+ /** TypeScript declarations, undefined when the word is used as a name. */
2368
+ #declaration(start, declare) {
2369
+ const next = this.peek();
2370
+ const sameLine = !next.newline;
2371
+ switch (this.token.value) {
2372
+ case "interface":
2373
+ if (!this.isName(next) || !sameLine) return;
2374
+ return this.#interface(start, declare);
2375
+ case "type":
2376
+ if (!this.isName(next) || !sameLine) return;
2377
+ return this.#typeAlias(start, declare);
2378
+ case "enum":
2379
+ if (!this.isName(next)) return;
2380
+ return this.#enum(start, false, declare);
2381
+ case "const":
2382
+ if (!this.is("enum", next))
2383
+ return this.#variables(start, declare);
2384
+ this.next();
2385
+ return this.#enum(start, true, declare);
2386
+ case "namespace":
2387
+ case "module":
2388
+ if (!sameLine || !(this.isName(next) || this.token.value === "module" && next.type === "string"))
2389
+ return;
2390
+ return this.#module(start, declare);
2391
+ case "global":
2392
+ if (!this.is("{", next)) return;
2393
+ return this.#module(start, declare);
2394
+ case "abstract":
2395
+ if (!this.is("class", next) || !sameLine) return;
2396
+ this.next();
2397
+ return this.parseClass(start, [], false, true, declare);
2398
+ case "declare": {
2399
+ if (!sameLine || !(this.isName(next) || this.is("@", next)))
2400
+ return;
2401
+ this.next();
2402
+ const declared = this.#declared(start);
2403
+ return declared;
2404
+ }
2405
+ }
2406
+ return void 0;
2407
+ }
2408
+ #declared(start) {
2409
+ switch (this.token.value) {
2410
+ case "var":
2411
+ case "let":
2412
+ case "using":
2413
+ return this.#variables(start, true);
2414
+ case "function": {
2415
+ const fn = this.parseFunction(start, false, false);
2416
+ return {
2417
+ ...fn,
2418
+ type: "TSDeclareFunction",
2419
+ declare: true,
2420
+ body: null
2421
+ };
2422
+ }
2423
+ case "async": {
2424
+ this.next();
2425
+ const fn = this.parseFunction(start, true, false);
2426
+ return {
2427
+ ...fn,
2428
+ type: "TSDeclareFunction",
2429
+ declare: true,
2430
+ body: null
2431
+ };
2432
+ }
2433
+ case "class":
2434
+ return this.parseClass(start, [], false, false, true);
2435
+ }
2436
+ const declaration = this.#declaration(start, true);
2437
+ if (!declaration) this.unexpected("Expected a declaration");
2438
+ return declaration;
2439
+ }
2440
+ #variables(start, declare) {
2441
+ const declaration = this.parseVariables(start, declare);
2442
+ this.semicolon();
2443
+ declaration.end = this.previous;
2444
+ return declaration;
2445
+ }
2446
+ parseVariables(start, declare) {
2447
+ const kind = this.next().value;
2448
+ const declarations = [];
2449
+ do {
2450
+ const declaratorStart = this.token.start;
2451
+ const id = this.parseBindingTarget();
2452
+ const definite = this.eat("!");
2453
+ if (this.is(":")) {
2454
+ id.typeAnnotation = this.parseTypeAnnotation();
2455
+ id.end = this.previous;
2456
+ }
2457
+ const init = this.eat("=") ? this.parseAssignment() : null;
2458
+ declarations.push(
2459
+ this.finish("VariableDeclarator", declaratorStart, {
2460
+ id,
2461
+ init,
2462
+ definite
2463
+ })
2464
+ );
2465
+ } while (this.eat(","));
2466
+ return this.finish("VariableDeclaration", start, {
2467
+ kind,
2468
+ declarations,
2469
+ declare
2470
+ });
2471
+ }
2472
+ #parenthesized() {
2473
+ this.expect("(");
2474
+ const expression = this.withIn(true, () => this.parseExpression());
2475
+ this.expect(")");
2476
+ return expression;
2477
+ }
2478
+ #if(start) {
2479
+ this.next();
2480
+ const test = this.#parenthesized();
2481
+ const consequent = this.#statement();
2482
+ const alternate = this.eat("else") ? this.#statement() : null;
2483
+ return this.finish("IfStatement", start, {
2484
+ test,
2485
+ consequent,
2486
+ alternate
2487
+ });
2488
+ }
2489
+ #for(start) {
2490
+ this.next();
2491
+ const awaits = this.eat("await");
2492
+ this.expect("(");
2493
+ let init = null;
2494
+ if (!this.is(";")) {
2495
+ const initStart = this.token.start;
2496
+ const declares = ["var", "const"].includes(this.token.value) || ["let", "using"].includes(this.token.value) && (this.isName(this.peek()) || this.is("[", this.peek()) || this.is("{", this.peek()));
2497
+ init = this.withIn(
2498
+ false,
2499
+ () => declares ? this.parseVariables(initStart, false) : this.parseExpression()
2500
+ );
2501
+ }
2502
+ if (init && (this.is("of") || this.is("in"))) {
2503
+ if (init.type !== "VariableDeclaration")
2504
+ init = this.toPattern(init);
2505
+ const of = this.next().value === "of";
2506
+ const right = this.withIn(
2507
+ true,
2508
+ () => of ? this.parseAssignment() : this.parseExpression()
2509
+ );
2510
+ this.expect(")");
2511
+ const body = this.#statement();
2512
+ return of ? this.finish("ForOfStatement", start, {
2513
+ left: init,
2514
+ right,
2515
+ body,
2516
+ await: awaits
2517
+ }) : this.finish("ForInStatement", start, {
2518
+ left: init,
2519
+ right,
2520
+ body
2521
+ });
2522
+ }
2523
+ this.expect(";");
2524
+ const test = this.is(";") ? null : this.withIn(true, () => this.parseExpression());
2525
+ this.expect(";");
2526
+ const update = this.is(")") ? null : this.withIn(true, () => this.parseExpression());
2527
+ this.expect(")");
2528
+ return this.finish("ForStatement", start, {
2529
+ init,
2530
+ test,
2531
+ update,
2532
+ body: this.#statement()
2533
+ });
2534
+ }
2535
+ #try(start) {
2536
+ this.next();
2537
+ const block = this.parseBlock();
2538
+ let handler = null;
2539
+ if (this.is("catch")) {
2540
+ const handlerStart = this.next().start;
2541
+ let param = null;
2542
+ if (this.eat("(")) {
2543
+ const target = this.parseBindingTarget();
2544
+ if (this.is(":")) {
2545
+ target.typeAnnotation = this.parseTypeAnnotation();
2546
+ target.end = this.previous;
2547
+ }
2548
+ param = target;
2549
+ this.expect(")");
2550
+ }
2551
+ handler = this.finish("CatchClause", handlerStart, {
2552
+ param,
2553
+ body: this.parseBlock()
2554
+ });
2555
+ }
2556
+ const finalizer = this.eat("finally") ? this.parseBlock() : null;
2557
+ return this.finish("TryStatement", start, {
2558
+ block,
2559
+ handler,
2560
+ finalizer
2561
+ });
2562
+ }
2563
+ #switch(start) {
2564
+ this.next();
2565
+ const discriminant = this.#parenthesized();
2566
+ this.expect("{");
2567
+ const cases = [];
2568
+ while (!this.is("}")) {
2569
+ const caseStart = this.token.start;
2570
+ let test = null;
2571
+ if (this.eat("case"))
2572
+ test = this.withIn(true, () => this.parseExpression());
2573
+ else this.expect("default");
2574
+ this.expect(":");
2575
+ const consequent = [];
2576
+ while (!this.is("case") && !this.is("default") && !this.is("}"))
2577
+ consequent.push(this.#statement());
2578
+ cases.push(
2579
+ this.finish("SwitchCase", caseStart, { test, consequent })
2580
+ );
2581
+ }
2582
+ this.expect("}");
2583
+ return this.finish("SwitchStatement", start, { discriminant, cases });
2584
+ }
2585
+ // Modules
2586
+ #import(start) {
2587
+ this.expect("import");
2588
+ let importKind = "value";
2589
+ const next = this.peek();
2590
+ if (this.is("type") && (this.is("{", next) || this.is("*", next) || this.isName(next) && !this.is("from", next) || this.is("from", next) && this.isName(this.#peekTwo())))
2591
+ importKind = this.next().value;
2592
+ let phase = null;
2593
+ if ((this.is("defer") || this.is("source")) && (this.isName(this.peek()) || this.is("*", this.peek())) && !this.is("from", this.peek()))
2594
+ phase = this.next().value;
2595
+ if (this.token.type === "string") {
2596
+ const source2 = this.parseString();
2597
+ const attributes2 = this.#attributes();
2598
+ this.semicolon();
2599
+ return this.finish("ImportDeclaration", start, {
2600
+ specifiers: [],
2601
+ source: source2,
2602
+ attributes: attributes2,
2603
+ importKind,
2604
+ phase
2605
+ });
2606
+ }
2607
+ const specifiers = [];
2608
+ if (this.isName() && !this.is("from")) {
2609
+ const local = this.parseIdentifier();
2610
+ if (this.is("="))
2611
+ return this.#importEquals(start, local, importKind, false);
2612
+ specifiers.push(
2613
+ this.finish("ImportDefaultSpecifier", local.start, { local })
2614
+ );
2615
+ this.eat(",");
2616
+ } else if (this.isName() && this.is("from") && this.is("=", this.peek())) {
2617
+ const local = this.parseIdentifier();
2618
+ return this.#importEquals(start, local, importKind, false);
2619
+ } else if (this.isName() && this.is("from") && this.isName(this.peek())) {
2620
+ const local = this.parseIdentifier();
2621
+ specifiers.push(
2622
+ this.finish("ImportDefaultSpecifier", local.start, { local })
2623
+ );
2624
+ }
2625
+ if (this.is("*")) {
2626
+ const namespaceStart = this.next().start;
2627
+ this.expect("as");
2628
+ const local = this.parseIdentifier();
2629
+ specifiers.push(
2630
+ this.finish("ImportNamespaceSpecifier", namespaceStart, {
2631
+ local
2632
+ })
2633
+ );
2634
+ } else if (this.is("{")) {
2635
+ this.next();
2636
+ while (!this.is("}")) {
2637
+ const specifierStart = this.token.start;
2638
+ let kind = "value";
2639
+ if (this.#typeModifier()) {
2640
+ this.next();
2641
+ kind = "type";
2642
+ }
2643
+ const imported = this.#moduleName();
2644
+ const local = this.eat("as") ? this.parseIdentifier() : imported;
2645
+ specifiers.push(
2646
+ this.finish("ImportSpecifier", specifierStart, {
2647
+ imported,
2648
+ local,
2649
+ importKind: kind
2650
+ })
2651
+ );
2652
+ if (!this.eat(",")) break;
2653
+ }
2654
+ this.expect("}");
2655
+ }
2656
+ this.expect("from");
2657
+ const source = this.parseString();
2658
+ const attributes = this.#attributes();
2659
+ this.semicolon();
2660
+ return this.finish("ImportDeclaration", start, {
2661
+ specifiers,
2662
+ source,
2663
+ attributes,
2664
+ importKind,
2665
+ phase
2666
+ });
2667
+ }
2668
+ /** A module export name: an identifier or a string. */
2669
+ #moduleName() {
2670
+ return this.token.type === "string" ? this.parseString() : this.parseIdentifier();
2671
+ }
2672
+ /** Whether `type` modifies the following specifier, as in `{ type A }`. */
2673
+ #typeModifier() {
2674
+ const next = this.peek();
2675
+ return this.is("type") && (this.isName(next) || next.type === "string") && !this.is("as", next);
2676
+ }
2677
+ #peekTwo() {
2678
+ const snapshot = this.snapshot();
2679
+ this.next();
2680
+ this.next();
2681
+ const token = this.token;
2682
+ this.restore(snapshot);
2683
+ return token;
2684
+ }
2685
+ #importEquals(start, id, importKind, isExport) {
2686
+ this.expect("=");
2687
+ let moduleReference;
2688
+ if (this.is("require") && this.is("(", this.peek())) {
2689
+ const referenceStart = this.next().start;
2690
+ this.expect("(");
2691
+ const expression = this.parseString();
2692
+ this.expect(")");
2693
+ moduleReference = this.finish(
2694
+ "TSExternalModuleReference",
2695
+ referenceStart,
2696
+ { expression }
2697
+ );
2698
+ } else moduleReference = this.parseEntityName();
2699
+ this.semicolon();
2700
+ return this.finish("TSImportEqualsDeclaration", start, {
2701
+ id,
2702
+ moduleReference,
2703
+ importKind: importKind === "type" ? "type" : "value",
2704
+ isExport
2705
+ });
2706
+ }
2707
+ #attributes() {
2708
+ if (this.token.newline || !(this.is("with") || this.is("assert")))
2709
+ return [];
2710
+ this.next();
2711
+ this.expect("{");
2712
+ const attributes = [];
2713
+ while (!this.is("}")) {
2714
+ const start = this.token.start;
2715
+ const key = this.#moduleName();
2716
+ this.expect(":");
2717
+ const value = this.parseString();
2718
+ attributes.push(
2719
+ this.finish("ImportAttribute", start, { key, value })
2720
+ );
2721
+ if (!this.eat(",")) break;
2722
+ }
2723
+ this.expect("}");
2724
+ return attributes;
2725
+ }
2726
+ #export(start, decorators) {
2727
+ this.expect("export");
2728
+ if (this.is("=")) {
2729
+ this.next();
2730
+ const expression = this.parseExpression();
2731
+ this.semicolon();
2732
+ return this.finish("TSExportAssignment", start, { expression });
2733
+ }
2734
+ if (this.is("as") && this.is("namespace", this.peek())) {
2735
+ this.next();
2736
+ this.next();
2737
+ const id = this.parseIdentifier();
2738
+ this.semicolon();
2739
+ return this.finish("TSNamespaceExportDeclaration", start, { id });
2740
+ }
2741
+ if (this.is("import") && this.isName(this.peek())) {
2742
+ this.next();
2743
+ let kind = "value";
2744
+ if (this.is("type") && this.isName(this.peek()) && !this.is("=", this.peek())) {
2745
+ this.next();
2746
+ kind = "type";
2747
+ }
2748
+ const id = this.parseIdentifier();
2749
+ return this.#importEquals(start, id, kind, true);
2750
+ }
2751
+ if (this.is("default")) {
2752
+ this.next();
2753
+ const declarationStart2 = this.token.start;
2754
+ let declaration2;
2755
+ if (this.is("function"))
2756
+ declaration2 = this.#defaultFunction(declarationStart2, false);
2757
+ else if (this.is("async") && this.is("function", this.peek()) && !this.peek().newline) {
2758
+ this.next();
2759
+ declaration2 = this.#defaultFunction(declarationStart2, true);
2760
+ } else if (this.is("class"))
2761
+ declaration2 = this.parseClass(
2762
+ declarationStart2,
2763
+ decorators,
2764
+ false,
2765
+ false,
2766
+ false
2767
+ );
2768
+ else if (this.is("@")) {
2769
+ const classDecorators = this.parseDecorators();
2770
+ declaration2 = this.parseClass(
2771
+ declarationStart2,
2772
+ classDecorators,
2773
+ false,
2774
+ false,
2775
+ false
2776
+ );
2777
+ } else if (this.is("abstract") && this.is("class", this.peek())) {
2778
+ this.next();
2779
+ declaration2 = this.parseClass(
2780
+ declarationStart2,
2781
+ decorators,
2782
+ false,
2783
+ true,
2784
+ false
2785
+ );
2786
+ } else if (this.is("interface") && this.isName(this.peek())) {
2787
+ declaration2 = this.#interface(declarationStart2, false);
2788
+ } else {
2789
+ declaration2 = this.parseAssignment();
2790
+ this.semicolon();
2791
+ }
2792
+ return this.finish("ExportDefaultDeclaration", start, {
2793
+ declaration: declaration2
2794
+ });
2795
+ }
2796
+ let exportKind = "value";
2797
+ if (this.is("type") && (this.is("{", this.peek()) || this.is("*", this.peek()))) {
2798
+ this.next();
2799
+ exportKind = "type";
2800
+ }
2801
+ if (this.is("*")) {
2802
+ this.next();
2803
+ const exported = this.eat("as") ? this.#moduleName() : null;
2804
+ this.expect("from");
2805
+ const source = this.parseString();
2806
+ const attributes = this.#attributes();
2807
+ this.semicolon();
2808
+ return this.finish("ExportAllDeclaration", start, {
2809
+ exported,
2810
+ source,
2811
+ attributes,
2812
+ exportKind
2813
+ });
2814
+ }
2815
+ if (this.is("{")) {
2816
+ this.next();
2817
+ const specifiers = [];
2818
+ while (!this.is("}")) {
2819
+ const specifierStart = this.token.start;
2820
+ let kind = "value";
2821
+ if (this.#typeModifier()) {
2822
+ this.next();
2823
+ kind = "type";
2824
+ }
2825
+ const local = this.#moduleName();
2826
+ const exported = this.eat("as") ? this.#moduleName() : local;
2827
+ specifiers.push(
2828
+ this.finish("ExportSpecifier", specifierStart, {
2829
+ local,
2830
+ exported,
2831
+ exportKind: kind
2832
+ })
2833
+ );
2834
+ if (!this.eat(",")) break;
2835
+ }
2836
+ this.expect("}");
2837
+ let source = null;
2838
+ let attributes = [];
2839
+ if (this.eat("from")) {
2840
+ source = this.parseString();
2841
+ attributes = this.#attributes();
2842
+ }
2843
+ this.semicolon();
2844
+ return this.finish("ExportNamedDeclaration", start, {
2845
+ declaration: null,
2846
+ specifiers,
2847
+ source,
2848
+ attributes,
2849
+ exportKind
2850
+ });
2851
+ }
2852
+ const declarationStart = this.token.start;
2853
+ let declaration;
2854
+ if (this.is("@")) {
2855
+ const classDecorators = this.parseDecorators();
2856
+ declaration = this.parseClass(
2857
+ declarationStart,
2858
+ classDecorators,
2859
+ false,
2860
+ this.eat("abstract"),
2861
+ false
2862
+ );
2863
+ } else if (this.is("class"))
2864
+ declaration = this.parseClass(
2865
+ declarationStart,
2866
+ decorators,
2867
+ false,
2868
+ false,
2869
+ false
2870
+ );
2871
+ else
2872
+ declaration = this.#keyword(declarationStart, this.token.value) ?? this.#declaration(declarationStart, false);
2873
+ if (!declaration) this.unexpected("Expected a declaration");
2874
+ if (declaration.type === "ClassDeclaration" && decorators.length > 0 && declaration.decorators.length === 0)
2875
+ declaration.decorators = decorators;
2876
+ return this.finish("ExportNamedDeclaration", start, {
2877
+ declaration,
2878
+ specifiers: [],
2879
+ source: null,
2880
+ attributes: [],
2881
+ exportKind: "value"
2882
+ });
2883
+ }
2884
+ #defaultFunction(start, async) {
2885
+ this.expect("function");
2886
+ const generator = this.eat("*");
2887
+ const id = this.isName() && !this.is("(") ? this.parseIdentifier() : null;
2888
+ const typeParameters = this.parseTypeParameters();
2889
+ const params = this.parseParameters();
2890
+ const returnType = this.eat(":") ? this.parseReturnType() : null;
2891
+ const parts = {
2892
+ id,
2893
+ params,
2894
+ async,
2895
+ generator,
2896
+ typeParameters,
2897
+ returnType
2898
+ };
2899
+ if (!this.is("{")) {
2900
+ this.semicolon();
2901
+ return this.finish("TSDeclareFunction", start, {
2902
+ ...parts,
2903
+ body: null,
2904
+ declare: false
2905
+ });
2906
+ }
2907
+ return this.finish("FunctionDeclaration", start, {
2908
+ ...parts,
2909
+ body: this.parseBlock(),
2910
+ declare: false
2911
+ });
2912
+ }
2913
+ // TypeScript declarations
2914
+ #interface(start, declare) {
2915
+ this.expect("interface");
2916
+ const id = this.parseIdentifier();
2917
+ const typeParameters = this.parseTypeParameters();
2918
+ const extended = [];
2919
+ if (this.eat("extends")) {
2920
+ do
2921
+ extended.push(this.parseType());
2922
+ while (this.eat(","));
2923
+ }
2924
+ const bodyStart = this.token.start;
2925
+ const members = this.parseTypeMembers();
2926
+ const body = this.finish("TSInterfaceBody", bodyStart, {
2927
+ body: members
2928
+ });
2929
+ return this.finish("TSInterfaceDeclaration", start, {
2930
+ id,
2931
+ typeParameters,
2932
+ extends: extended,
2933
+ body,
2934
+ declare
2935
+ });
2936
+ }
2937
+ #typeAlias(start, declare) {
2938
+ this.expect("type");
2939
+ const id = this.parseIdentifier();
2940
+ const typeParameters = this.parseTypeParameters();
2941
+ this.expect("=");
2942
+ const typeAnnotation = this.parseType();
2943
+ this.semicolon();
2944
+ return this.finish("TSTypeAliasDeclaration", start, {
2945
+ id,
2946
+ typeParameters,
2947
+ typeAnnotation,
2948
+ declare
2949
+ });
2950
+ }
2951
+ #enum(start, isConst, declare) {
2952
+ this.expect("enum");
2953
+ const id = this.parseIdentifier();
2954
+ this.expect("{");
2955
+ const members = [];
2956
+ while (!this.is("}")) {
2957
+ const memberStart = this.token.start;
2958
+ const { key } = this.parsePropertyKey();
2959
+ const initializer = this.eat("=") ? this.parseAssignment() : null;
2960
+ members.push(
2961
+ this.finish("TSEnumMember", memberStart, {
2962
+ id: key,
2963
+ initializer
2964
+ })
2965
+ );
2966
+ if (!this.eat(",")) break;
2967
+ }
2968
+ this.expect("}");
2969
+ return this.finish("TSEnumDeclaration", start, {
2970
+ id,
2971
+ members,
2972
+ const: isConst,
2973
+ declare
2974
+ });
2975
+ }
2976
+ #module(start, declare) {
2977
+ const keyword = this.next().value;
2978
+ const kind = keyword === "global" ? "global" : keyword === "module" ? "module" : "namespace";
2979
+ let id;
2980
+ if (kind === "global")
2981
+ id = this.finish("Identifier", start, { name: "global" });
2982
+ else if (this.token.type === "string") id = this.parseString();
2983
+ else id = this.parseEntityName();
2984
+ if (!this.is("{")) {
2985
+ this.semicolon();
2986
+ return this.finish("TSModuleDeclaration", start, {
2987
+ id,
2988
+ body: null,
2989
+ kind,
2990
+ declare
2991
+ });
2992
+ }
2993
+ const bodyStart = this.expect("{").start;
2994
+ const { body } = this.#list(false, () => this.is("}"));
2995
+ this.expect("}");
2996
+ const block = this.finish("TSModuleBlock", bodyStart, { body });
2997
+ return this.finish("TSModuleDeclaration", start, {
2998
+ id,
2999
+ body: block,
3000
+ kind,
3001
+ declare
3002
+ });
3003
+ }
3004
+ };
3005
+
3006
+ // shared/syntax/src/lines.ts
3007
+ var Lines = class {
3008
+ static {
3009
+ __name(this, "Lines");
3010
+ }
3011
+ #starts = [0];
3012
+ constructor(text) {
3013
+ for (const match of text.matchAll(/\r\n|\r|\n/g))
3014
+ this.#starts.push(match.index + match[0].length);
3015
+ }
3016
+ line(offset) {
3017
+ let low = 0;
3018
+ let high = this.#starts.length - 1;
3019
+ while (low < high) {
3020
+ const middle = Math.ceil((low + high) / 2);
3021
+ if (this.#starts[middle] <= offset) low = middle;
3022
+ else high = middle - 1;
3023
+ }
3024
+ return low + 1;
3025
+ }
3026
+ column(offset) {
3027
+ return offset - this.#starts[this.line(offset) - 1] + 1;
3028
+ }
3029
+ };
3030
+
3031
+ // shared/syntax/src/walker.ts
3032
+ var Walker = class _Walker {
3033
+ static {
3034
+ __name(this, "Walker");
3035
+ }
3036
+ static isNode(value) {
3037
+ return typeof value === "object" && value !== null && typeof value.type === "string" && typeof value.start === "number";
3038
+ }
3039
+ /** Child nodes by start, outer nodes before inner ones starting with them. */
3040
+ static children(node) {
3041
+ const children = [];
3042
+ for (const [key, value] of Object.entries(node)) {
3043
+ if (key === "comments") continue;
3044
+ if (node.type === "Property" && node.shorthand && key === "key")
3045
+ continue;
3046
+ if (Array.isArray(value)) {
3047
+ for (const item of value)
3048
+ if (_Walker.isNode(item)) children.push(item);
3049
+ } else if (_Walker.isNode(value)) children.push(value);
3050
+ }
3051
+ return children.sort((a, b) => a.start - b.start || b.end - a.end);
3052
+ }
3053
+ };
3054
+
3055
+ // shared/syntax/src/index.ts
3056
+ function language(file) {
3057
+ if (/\.jsonc?$/.test(file))
3058
+ return { json: true, typescript: false, jsx: false };
3059
+ if (!/\.[cm]?[jt]sx?$/.test(file)) return;
3060
+ return {
3061
+ json: false,
3062
+ typescript: /\.[cm]?tsx?$/.test(file),
3063
+ jsx: !/\.[cm]?ts$/.test(file)
3064
+ };
3065
+ }
3066
+ __name(language, "language");
3067
+ function parse(source, options) {
3068
+ const parser = new Parser(source, options);
3069
+ const program = parser.parseProgram();
3070
+ return { program, comments: parser.comments };
3071
+ }
3072
+ __name(parse, "parse");
3073
+ function parseExpression(source) {
3074
+ const parser = new Parser(source, { typescript: false, jsx: false });
3075
+ const expression = parser.parseDocument();
3076
+ const statement = {
3077
+ type: "ExpressionStatement",
3078
+ start: expression.start,
3079
+ end: expression.end,
3080
+ expression
3081
+ };
3082
+ const program = {
3083
+ type: "Program",
3084
+ start: 0,
3085
+ end: source.length,
3086
+ body: [statement],
3087
+ directives: []
3088
+ };
3089
+ return { program, comments: parser.comments };
3090
+ }
3091
+ __name(parseExpression, "parseExpression");
3092
+ export {
3093
+ Lines,
3094
+ ParseError,
3095
+ Walker,
3096
+ language,
3097
+ parse,
3098
+ parseExpression
3099
+ };