1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, '__esModule', { value: true });
|
4 |
|
5 | const beforeExpr = true;
|
6 | const startsExpr = true;
|
7 | const isLoop = true;
|
8 | const isAssign = true;
|
9 | const prefix = true;
|
10 | const postfix = true;
|
11 | class TokenType {
|
12 | constructor(label, conf = {}) {
|
13 | this.label = label;
|
14 | this.keyword = conf.keyword;
|
15 | this.beforeExpr = !!conf.beforeExpr;
|
16 | this.startsExpr = !!conf.startsExpr;
|
17 | this.rightAssociative = !!conf.rightAssociative;
|
18 | this.isLoop = !!conf.isLoop;
|
19 | this.isAssign = !!conf.isAssign;
|
20 | this.prefix = !!conf.prefix;
|
21 | this.postfix = !!conf.postfix;
|
22 | this.binop = conf.binop === 0 ? 0 : conf.binop || null;
|
23 | this.updateContext = null;
|
24 | }
|
25 |
|
26 | }
|
27 |
|
28 | function KeywordTokenType(keyword, options = {}) {
|
29 | return new TokenType(keyword, Object.assign({}, options, {
|
30 | keyword
|
31 | }));
|
32 | }
|
33 |
|
34 | function BinopTokenType(name, binop) {
|
35 | return new TokenType(name, {
|
36 | beforeExpr,
|
37 | binop
|
38 | });
|
39 | }
|
40 |
|
41 | const types = {
|
42 | num: new TokenType("num", {
|
43 | startsExpr
|
44 | }),
|
45 | bigint: new TokenType("bigint", {
|
46 | startsExpr
|
47 | }),
|
48 | regexp: new TokenType("regexp", {
|
49 | startsExpr
|
50 | }),
|
51 | string: new TokenType("string", {
|
52 | startsExpr
|
53 | }),
|
54 | name: new TokenType("name", {
|
55 | startsExpr
|
56 | }),
|
57 | eof: new TokenType("eof"),
|
58 | bracketL: new TokenType("[", {
|
59 | beforeExpr,
|
60 | startsExpr
|
61 | }),
|
62 | bracketR: new TokenType("]"),
|
63 | braceL: new TokenType("{", {
|
64 | beforeExpr,
|
65 | startsExpr
|
66 | }),
|
67 | braceBarL: new TokenType("{|", {
|
68 | beforeExpr,
|
69 | startsExpr
|
70 | }),
|
71 | braceR: new TokenType("}"),
|
72 | braceBarR: new TokenType("|}"),
|
73 | parenL: new TokenType("(", {
|
74 | beforeExpr,
|
75 | startsExpr
|
76 | }),
|
77 | parenR: new TokenType(")"),
|
78 | comma: new TokenType(",", {
|
79 | beforeExpr
|
80 | }),
|
81 | semi: new TokenType(";", {
|
82 | beforeExpr
|
83 | }),
|
84 | colon: new TokenType(":", {
|
85 | beforeExpr
|
86 | }),
|
87 | doubleColon: new TokenType("::", {
|
88 | beforeExpr
|
89 | }),
|
90 | dot: new TokenType("."),
|
91 | question: new TokenType("?", {
|
92 | beforeExpr
|
93 | }),
|
94 | questionDot: new TokenType("?."),
|
95 | arrow: new TokenType("=>", {
|
96 | beforeExpr
|
97 | }),
|
98 | template: new TokenType("template"),
|
99 | ellipsis: new TokenType("...", {
|
100 | beforeExpr
|
101 | }),
|
102 | backQuote: new TokenType("`", {
|
103 | startsExpr
|
104 | }),
|
105 | dollarBraceL: new TokenType("${", {
|
106 | beforeExpr,
|
107 | startsExpr
|
108 | }),
|
109 | at: new TokenType("@"),
|
110 | hash: new TokenType("#", {
|
111 | startsExpr
|
112 | }),
|
113 | interpreterDirective: new TokenType("#!..."),
|
114 | eq: new TokenType("=", {
|
115 | beforeExpr,
|
116 | isAssign
|
117 | }),
|
118 | assign: new TokenType("_=", {
|
119 | beforeExpr,
|
120 | isAssign
|
121 | }),
|
122 | incDec: new TokenType("++/--", {
|
123 | prefix,
|
124 | postfix,
|
125 | startsExpr
|
126 | }),
|
127 | bang: new TokenType("!", {
|
128 | beforeExpr,
|
129 | prefix,
|
130 | startsExpr
|
131 | }),
|
132 | tilde: new TokenType("~", {
|
133 | beforeExpr,
|
134 | prefix,
|
135 | startsExpr
|
136 | }),
|
137 | pipeline: BinopTokenType("|>", 0),
|
138 | nullishCoalescing: BinopTokenType("??", 1),
|
139 | logicalOR: BinopTokenType("||", 1),
|
140 | logicalAND: BinopTokenType("&&", 2),
|
141 | bitwiseOR: BinopTokenType("|", 3),
|
142 | bitwiseXOR: BinopTokenType("^", 4),
|
143 | bitwiseAND: BinopTokenType("&", 5),
|
144 | equality: BinopTokenType("==/!=", 6),
|
145 | relational: BinopTokenType("</>", 7),
|
146 | bitShift: BinopTokenType("<</>>", 8),
|
147 | plusMin: new TokenType("+/-", {
|
148 | beforeExpr,
|
149 | binop: 9,
|
150 | prefix,
|
151 | startsExpr
|
152 | }),
|
153 | modulo: BinopTokenType("%", 10),
|
154 | star: BinopTokenType("*", 10),
|
155 | slash: BinopTokenType("/", 10),
|
156 | exponent: new TokenType("**", {
|
157 | beforeExpr,
|
158 | binop: 11,
|
159 | rightAssociative: true
|
160 | })
|
161 | };
|
162 |
|
163 | function makeKeywordProps(name, conf) {
|
164 | return {
|
165 | value: KeywordTokenType(name, conf),
|
166 | enumerable: true
|
167 | };
|
168 | }
|
169 |
|
170 | const keywords = Object.create(null, {
|
171 | break: makeKeywordProps("break"),
|
172 | case: makeKeywordProps("case", {
|
173 | beforeExpr
|
174 | }),
|
175 | catch: makeKeywordProps("catch"),
|
176 | continue: makeKeywordProps("continue"),
|
177 | debugger: makeKeywordProps("debugger"),
|
178 | default: makeKeywordProps("default", {
|
179 | beforeExpr
|
180 | }),
|
181 | do: makeKeywordProps("do", {
|
182 | isLoop,
|
183 | beforeExpr
|
184 | }),
|
185 | else: makeKeywordProps("else", {
|
186 | beforeExpr
|
187 | }),
|
188 | finally: makeKeywordProps("finally"),
|
189 | for: makeKeywordProps("for", {
|
190 | isLoop
|
191 | }),
|
192 | function: makeKeywordProps("function", {
|
193 | startsExpr
|
194 | }),
|
195 | if: makeKeywordProps("if"),
|
196 | return: makeKeywordProps("return", {
|
197 | beforeExpr
|
198 | }),
|
199 | switch: makeKeywordProps("switch"),
|
200 | throw: makeKeywordProps("throw", {
|
201 | beforeExpr,
|
202 | prefix,
|
203 | startsExpr
|
204 | }),
|
205 | try: makeKeywordProps("try"),
|
206 | var: makeKeywordProps("var"),
|
207 | const: makeKeywordProps("const"),
|
208 | while: makeKeywordProps("while", {
|
209 | isLoop
|
210 | }),
|
211 | with: makeKeywordProps("with"),
|
212 | new: makeKeywordProps("new", {
|
213 | beforeExpr,
|
214 | startsExpr
|
215 | }),
|
216 | this: makeKeywordProps("this", {
|
217 | startsExpr
|
218 | }),
|
219 | super: makeKeywordProps("super", {
|
220 | startsExpr
|
221 | }),
|
222 | class: makeKeywordProps("class", {
|
223 | startsExpr
|
224 | }),
|
225 | extends: makeKeywordProps("extends", {
|
226 | beforeExpr
|
227 | }),
|
228 | export: makeKeywordProps("export"),
|
229 | import: makeKeywordProps("import", {
|
230 | startsExpr
|
231 | }),
|
232 | null: makeKeywordProps("null", {
|
233 | startsExpr
|
234 | }),
|
235 | true: makeKeywordProps("true", {
|
236 | startsExpr
|
237 | }),
|
238 | false: makeKeywordProps("false", {
|
239 | startsExpr
|
240 | }),
|
241 | in: makeKeywordProps("in", {
|
242 | beforeExpr,
|
243 | binop: 7
|
244 | }),
|
245 | instanceof: makeKeywordProps("instanceof", {
|
246 | beforeExpr,
|
247 | binop: 7
|
248 | }),
|
249 | typeof: makeKeywordProps("typeof", {
|
250 | beforeExpr,
|
251 | prefix,
|
252 | startsExpr
|
253 | }),
|
254 | void: makeKeywordProps("void", {
|
255 | beforeExpr,
|
256 | prefix,
|
257 | startsExpr
|
258 | }),
|
259 | delete: makeKeywordProps("delete", {
|
260 | beforeExpr,
|
261 | prefix,
|
262 | startsExpr
|
263 | })
|
264 | });
|
265 | Object.keys(keywords).forEach(name => {
|
266 | types["_" + name] = keywords[name];
|
267 | });
|
268 |
|
269 | function isSimpleProperty(node) {
|
270 | return node != null && node.type === "Property" && node.kind === "init" && node.method === false;
|
271 | }
|
272 |
|
273 | var estree = (superClass => class extends superClass {
|
274 | estreeParseRegExpLiteral({
|
275 | pattern,
|
276 | flags
|
277 | }) {
|
278 | let regex = null;
|
279 |
|
280 | try {
|
281 | regex = new RegExp(pattern, flags);
|
282 | } catch (e) {}
|
283 |
|
284 | const node = this.estreeParseLiteral(regex);
|
285 | node.regex = {
|
286 | pattern,
|
287 | flags
|
288 | };
|
289 | return node;
|
290 | }
|
291 |
|
292 | estreeParseLiteral(value) {
|
293 | return this.parseLiteral(value, "Literal");
|
294 | }
|
295 |
|
296 | directiveToStmt(directive) {
|
297 | const directiveLiteral = directive.value;
|
298 | const stmt = this.startNodeAt(directive.start, directive.loc.start);
|
299 | const expression = this.startNodeAt(directiveLiteral.start, directiveLiteral.loc.start);
|
300 | expression.value = directiveLiteral.value;
|
301 | expression.raw = directiveLiteral.extra.raw;
|
302 | stmt.expression = this.finishNodeAt(expression, "Literal", directiveLiteral.end, directiveLiteral.loc.end);
|
303 | stmt.directive = directiveLiteral.extra.raw.slice(1, -1);
|
304 | return this.finishNodeAt(stmt, "ExpressionStatement", directive.end, directive.loc.end);
|
305 | }
|
306 |
|
307 | initFunction(node, isAsync) {
|
308 | super.initFunction(node, isAsync);
|
309 | node.expression = false;
|
310 | }
|
311 |
|
312 | checkDeclaration(node) {
|
313 | if (isSimpleProperty(node)) {
|
314 | this.checkDeclaration(node.value);
|
315 | } else {
|
316 | super.checkDeclaration(node);
|
317 | }
|
318 | }
|
319 |
|
320 | checkGetterSetterParams(method) {
|
321 | const prop = method;
|
322 | const paramCount = prop.kind === "get" ? 0 : 1;
|
323 | const start = prop.start;
|
324 |
|
325 | if (prop.value.params.length !== paramCount) {
|
326 | if (prop.kind === "get") {
|
327 | this.raise(start, "getter must not have any formal parameters");
|
328 | } else {
|
329 | this.raise(start, "setter must have exactly one formal parameter");
|
330 | }
|
331 | }
|
332 |
|
333 | if (prop.kind === "set" && prop.value.params[0].type === "RestElement") {
|
334 | this.raise(start, "setter function argument must not be a rest parameter");
|
335 | }
|
336 | }
|
337 |
|
338 | checkLVal(expr, isBinding, checkClashes, contextDescription) {
|
339 | switch (expr.type) {
|
340 | case "ObjectPattern":
|
341 | expr.properties.forEach(prop => {
|
342 | this.checkLVal(prop.type === "Property" ? prop.value : prop, isBinding, checkClashes, "object destructuring pattern");
|
343 | });
|
344 | break;
|
345 |
|
346 | default:
|
347 | super.checkLVal(expr, isBinding, checkClashes, contextDescription);
|
348 | }
|
349 | }
|
350 |
|
351 | checkPropClash(prop, propHash) {
|
352 | if (prop.computed || !isSimpleProperty(prop)) return;
|
353 | const key = prop.key;
|
354 | const name = key.type === "Identifier" ? key.name : String(key.value);
|
355 |
|
356 | if (name === "__proto__") {
|
357 | if (propHash.proto) {
|
358 | this.raise(key.start, "Redefinition of __proto__ property");
|
359 | }
|
360 |
|
361 | propHash.proto = true;
|
362 | }
|
363 | }
|
364 |
|
365 | isStrictBody(node) {
|
366 | const isBlockStatement = node.body.type === "BlockStatement";
|
367 |
|
368 | if (isBlockStatement && node.body.body.length > 0) {
|
369 | for (let _i = 0, _node$body$body = node.body.body; _i < _node$body$body.length; _i++) {
|
370 | const directive = _node$body$body[_i];
|
371 |
|
372 | if (directive.type === "ExpressionStatement" && directive.expression.type === "Literal") {
|
373 | if (directive.expression.value === "use strict") return true;
|
374 | } else {
|
375 | break;
|
376 | }
|
377 | }
|
378 | }
|
379 |
|
380 | return false;
|
381 | }
|
382 |
|
383 | isValidDirective(stmt) {
|
384 | return stmt.type === "ExpressionStatement" && stmt.expression.type === "Literal" && typeof stmt.expression.value === "string" && (!stmt.expression.extra || !stmt.expression.extra.parenthesized);
|
385 | }
|
386 |
|
387 | stmtToDirective(stmt) {
|
388 | const directive = super.stmtToDirective(stmt);
|
389 | const value = stmt.expression.value;
|
390 | directive.value.value = value;
|
391 | return directive;
|
392 | }
|
393 |
|
394 | parseBlockBody(node, allowDirectives, topLevel, end) {
|
395 | super.parseBlockBody(node, allowDirectives, topLevel, end);
|
396 | const directiveStatements = node.directives.map(d => this.directiveToStmt(d));
|
397 | node.body = directiveStatements.concat(node.body);
|
398 | delete node.directives;
|
399 | }
|
400 |
|
401 | pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor) {
|
402 | this.parseMethod(method, isGenerator, isAsync, isConstructor, "MethodDefinition");
|
403 |
|
404 | if (method.typeParameters) {
|
405 | method.value.typeParameters = method.typeParameters;
|
406 | delete method.typeParameters;
|
407 | }
|
408 |
|
409 | classBody.body.push(method);
|
410 | }
|
411 |
|
412 | parseExprAtom(refShorthandDefaultPos) {
|
413 | switch (this.state.type) {
|
414 | case types.regexp:
|
415 | return this.estreeParseRegExpLiteral(this.state.value);
|
416 |
|
417 | case types.num:
|
418 | case types.string:
|
419 | return this.estreeParseLiteral(this.state.value);
|
420 |
|
421 | case types._null:
|
422 | return this.estreeParseLiteral(null);
|
423 |
|
424 | case types._true:
|
425 | return this.estreeParseLiteral(true);
|
426 |
|
427 | case types._false:
|
428 | return this.estreeParseLiteral(false);
|
429 |
|
430 | default:
|
431 | return super.parseExprAtom(refShorthandDefaultPos);
|
432 | }
|
433 | }
|
434 |
|
435 | parseLiteral(value, type, startPos, startLoc) {
|
436 | const node = super.parseLiteral(value, type, startPos, startLoc);
|
437 | node.raw = node.extra.raw;
|
438 | delete node.extra;
|
439 | return node;
|
440 | }
|
441 |
|
442 | parseFunctionBody(node, allowExpression) {
|
443 | super.parseFunctionBody(node, allowExpression);
|
444 | node.expression = node.body.type !== "BlockStatement";
|
445 | }
|
446 |
|
447 | parseMethod(node, isGenerator, isAsync, isConstructor, type) {
|
448 | let funcNode = this.startNode();
|
449 | funcNode.kind = node.kind;
|
450 | funcNode = super.parseMethod(funcNode, isGenerator, isAsync, isConstructor, "FunctionExpression");
|
451 | delete funcNode.kind;
|
452 | node.value = funcNode;
|
453 | return this.finishNode(node, type);
|
454 | }
|
455 |
|
456 | parseObjectMethod(prop, isGenerator, isAsync, isPattern, containsEsc) {
|
457 | const node = super.parseObjectMethod(prop, isGenerator, isAsync, isPattern, containsEsc);
|
458 |
|
459 | if (node) {
|
460 | node.type = "Property";
|
461 | if (node.kind === "method") node.kind = "init";
|
462 | node.shorthand = false;
|
463 | }
|
464 |
|
465 | return node;
|
466 | }
|
467 |
|
468 | parseObjectProperty(prop, startPos, startLoc, isPattern, refShorthandDefaultPos) {
|
469 | const node = super.parseObjectProperty(prop, startPos, startLoc, isPattern, refShorthandDefaultPos);
|
470 |
|
471 | if (node) {
|
472 | node.kind = "init";
|
473 | node.type = "Property";
|
474 | }
|
475 |
|
476 | return node;
|
477 | }
|
478 |
|
479 | toAssignable(node, isBinding, contextDescription) {
|
480 | if (isSimpleProperty(node)) {
|
481 | this.toAssignable(node.value, isBinding, contextDescription);
|
482 | return node;
|
483 | }
|
484 |
|
485 | return super.toAssignable(node, isBinding, contextDescription);
|
486 | }
|
487 |
|
488 | toAssignableObjectExpressionProp(prop, isBinding, isLast) {
|
489 | if (prop.kind === "get" || prop.kind === "set") {
|
490 | this.raise(prop.key.start, "Object pattern can't contain getter or setter");
|
491 | } else if (prop.method) {
|
492 | this.raise(prop.key.start, "Object pattern can't contain methods");
|
493 | } else {
|
494 | super.toAssignableObjectExpressionProp(prop, isBinding, isLast);
|
495 | }
|
496 | }
|
497 |
|
498 | });
|
499 |
|
500 | const lineBreak = /\r\n?|[\n\u2028\u2029]/;
|
501 | const lineBreakG = new RegExp(lineBreak.source, "g");
|
502 | function isNewLine(code) {
|
503 | switch (code) {
|
504 | case 10:
|
505 | case 13:
|
506 | case 8232:
|
507 | case 8233:
|
508 | return true;
|
509 |
|
510 | default:
|
511 | return false;
|
512 | }
|
513 | }
|
514 | const skipWhiteSpace = /(?:\s|\/\/.*|\/\*[^]*?\*\/)*/g;
|
515 | function isWhitespace(code) {
|
516 | switch (code) {
|
517 | case 0x0009:
|
518 | case 0x000b:
|
519 | case 0x000c:
|
520 | case 32:
|
521 | case 160:
|
522 | case 5760:
|
523 | case 0x2000:
|
524 | case 0x2001:
|
525 | case 0x2002:
|
526 | case 0x2003:
|
527 | case 0x2004:
|
528 | case 0x2005:
|
529 | case 0x2006:
|
530 | case 0x2007:
|
531 | case 0x2008:
|
532 | case 0x2009:
|
533 | case 0x200a:
|
534 | case 0x202f:
|
535 | case 0x205f:
|
536 | case 0x3000:
|
537 | case 0xfeff:
|
538 | return true;
|
539 |
|
540 | default:
|
541 | return false;
|
542 | }
|
543 | }
|
544 |
|
545 | class TokContext {
|
546 | constructor(token, isExpr, preserveSpace, override) {
|
547 | this.token = token;
|
548 | this.isExpr = !!isExpr;
|
549 | this.preserveSpace = !!preserveSpace;
|
550 | this.override = override;
|
551 | }
|
552 |
|
553 | }
|
554 | const types$1 = {
|
555 | braceStatement: new TokContext("{", false),
|
556 | braceExpression: new TokContext("{", true),
|
557 | templateQuasi: new TokContext("${", false),
|
558 | parenStatement: new TokContext("(", false),
|
559 | parenExpression: new TokContext("(", true),
|
560 | template: new TokContext("`", true, true, p => p.readTmplToken()),
|
561 | functionExpression: new TokContext("function", true),
|
562 | functionStatement: new TokContext("function", false)
|
563 | };
|
564 |
|
565 | types.parenR.updateContext = types.braceR.updateContext = function () {
|
566 | if (this.state.context.length === 1) {
|
567 | this.state.exprAllowed = true;
|
568 | return;
|
569 | }
|
570 |
|
571 | let out = this.state.context.pop();
|
572 |
|
573 | if (out === types$1.braceStatement && this.curContext().token === "function") {
|
574 | out = this.state.context.pop();
|
575 | }
|
576 |
|
577 | this.state.exprAllowed = !out.isExpr;
|
578 | };
|
579 |
|
580 | types.name.updateContext = function (prevType) {
|
581 | let allowed = false;
|
582 |
|
583 | if (prevType !== types.dot) {
|
584 | if (this.state.value === "of" && !this.state.exprAllowed || this.state.value === "yield" && this.state.inGenerator) {
|
585 | allowed = true;
|
586 | }
|
587 | }
|
588 |
|
589 | this.state.exprAllowed = allowed;
|
590 |
|
591 | if (this.state.isIterator) {
|
592 | this.state.isIterator = false;
|
593 | }
|
594 | };
|
595 |
|
596 | types.braceL.updateContext = function (prevType) {
|
597 | this.state.context.push(this.braceIsBlock(prevType) ? types$1.braceStatement : types$1.braceExpression);
|
598 | this.state.exprAllowed = true;
|
599 | };
|
600 |
|
601 | types.dollarBraceL.updateContext = function () {
|
602 | this.state.context.push(types$1.templateQuasi);
|
603 | this.state.exprAllowed = true;
|
604 | };
|
605 |
|
606 | types.parenL.updateContext = function (prevType) {
|
607 | const statementParens = prevType === types._if || prevType === types._for || prevType === types._with || prevType === types._while;
|
608 | this.state.context.push(statementParens ? types$1.parenStatement : types$1.parenExpression);
|
609 | this.state.exprAllowed = true;
|
610 | };
|
611 |
|
612 | types.incDec.updateContext = function () {};
|
613 |
|
614 | types._function.updateContext = types._class.updateContext = function (prevType) {
|
615 | if (prevType.beforeExpr && prevType !== types.semi && prevType !== types._else && !(prevType === types._return && lineBreak.test(this.state.input.slice(this.state.lastTokEnd, this.state.start))) && !((prevType === types.colon || prevType === types.braceL) && this.curContext() === types$1.b_stat)) {
|
616 | this.state.context.push(types$1.functionExpression);
|
617 | } else {
|
618 | this.state.context.push(types$1.functionStatement);
|
619 | }
|
620 |
|
621 | this.state.exprAllowed = false;
|
622 | };
|
623 |
|
624 | types.backQuote.updateContext = function () {
|
625 | if (this.curContext() === types$1.template) {
|
626 | this.state.context.pop();
|
627 | } else {
|
628 | this.state.context.push(types$1.template);
|
629 | }
|
630 |
|
631 | this.state.exprAllowed = false;
|
632 | };
|
633 |
|
634 | const reservedWords = {
|
635 | strict: ["implements", "interface", "let", "package", "private", "protected", "public", "static", "yield"],
|
636 | strictBind: ["eval", "arguments"]
|
637 | };
|
638 | const reservedWordsStrictSet = new Set(reservedWords.strict);
|
639 | const reservedWordsStrictBindSet = new Set(reservedWords.strict.concat(reservedWords.strictBind));
|
640 | const isReservedWord = (word, inModule) => {
|
641 | return inModule && word === "await" || word === "enum";
|
642 | };
|
643 | function isStrictReservedWord(word, inModule) {
|
644 | return isReservedWord(word, inModule) || reservedWordsStrictSet.has(word);
|
645 | }
|
646 | function isStrictBindReservedWord(word, inModule) {
|
647 | return isReservedWord(word, inModule) || reservedWordsStrictBindSet.has(word);
|
648 | }
|
649 | const keywords$1 = new Set(["break", "case", "catch", "continue", "debugger", "default", "do", "else", "finally", "for", "function", "if", "return", "switch", "throw", "try", "var", "while", "with", "null", "true", "false", "instanceof", "typeof", "void", "delete", "new", "in", "this", "const", "class", "extends", "export", "import", "super"]);
|
650 | function isKeyword(word) {
|
651 | return keywords$1.has(word);
|
652 | }
|
653 | const keywordRelationalOperator = /^in(stanceof)?$/;
|
654 | let nonASCIIidentifierStartChars = "\xaa\xb5\xba\xc0-\xd6\xd8-\xf6\xf8-\u02c1\u02c6-\u02d1\u02e0-\u02e4\u02ec\u02ee\u0370-\u0374\u0376\u0377\u037a-\u037d\u037f\u0386\u0388-\u038a\u038c\u038e-\u03a1\u03a3-\u03f5\u03f7-\u0481\u048a-\u052f\u0531-\u0556\u0559\u0560-\u0588\u05d0-\u05ea\u05ef-\u05f2\u0620-\u064a\u066e\u066f\u0671-\u06d3\u06d5\u06e5\u06e6\u06ee\u06ef\u06fa-\u06fc\u06ff\u0710\u0712-\u072f\u074d-\u07a5\u07b1\u07ca-\u07ea\u07f4\u07f5\u07fa\u0800-\u0815\u081a\u0824\u0828\u0840-\u0858\u0860-\u086a\u08a0-\u08b4\u08b6-\u08bd\u0904-\u0939\u093d\u0950\u0958-\u0961\u0971-\u0980\u0985-\u098c\u098f\u0990\u0993-\u09a8\u09aa-\u09b0\u09b2\u09b6-\u09b9\u09bd\u09ce\u09dc\u09dd\u09df-\u09e1\u09f0\u09f1\u09fc\u0a05-\u0a0a\u0a0f\u0a10\u0a13-\u0a28\u0a2a-\u0a30\u0a32\u0a33\u0a35\u0a36\u0a38\u0a39\u0a59-\u0a5c\u0a5e\u0a72-\u0a74\u0a85-\u0a8d\u0a8f-\u0a91\u0a93-\u0aa8\u0aaa-\u0ab0\u0ab2\u0ab3\u0ab5-\u0ab9\u0abd\u0ad0\u0ae0\u0ae1\u0af9\u0b05-\u0b0c\u0b0f\u0b10\u0b13-\u0b28\u0b2a-\u0b30\u0b32\u0b33\u0b35-\u0b39\u0b3d\u0b5c\u0b5d\u0b5f-\u0b61\u0b71\u0b83\u0b85-\u0b8a\u0b8e-\u0b90\u0b92-\u0b95\u0b99\u0b9a\u0b9c\u0b9e\u0b9f\u0ba3\u0ba4\u0ba8-\u0baa\u0bae-\u0bb9\u0bd0\u0c05-\u0c0c\u0c0e-\u0c10\u0c12-\u0c28\u0c2a-\u0c39\u0c3d\u0c58-\u0c5a\u0c60\u0c61\u0c80\u0c85-\u0c8c\u0c8e-\u0c90\u0c92-\u0ca8\u0caa-\u0cb3\u0cb5-\u0cb9\u0cbd\u0cde\u0ce0\u0ce1\u0cf1\u0cf2\u0d05-\u0d0c\u0d0e-\u0d10\u0d12-\u0d3a\u0d3d\u0d4e\u0d54-\u0d56\u0d5f-\u0d61\u0d7a-\u0d7f\u0d85-\u0d96\u0d9a-\u0db1\u0db3-\u0dbb\u0dbd\u0dc0-\u0dc6\u0e01-\u0e30\u0e32\u0e33\u0e40-\u0e46\u0e81\u0e82\u0e84\u0e87\u0e88\u0e8a\u0e8d\u0e94-\u0e97\u0e99-\u0e9f\u0ea1-\u0ea3\u0ea5\u0ea7\u0eaa\u0eab\u0ead-\u0eb0\u0eb2\u0eb3\u0ebd\u0ec0-\u0ec4\u0ec6\u0edc-\u0edf\u0f00\u0f40-\u0f47\u0f49-\u0f6c\u0f88-\u0f8c\u1000-\u102a\u103f\u1050-\u1055\u105a-\u105d\u1061\u1065\u1066\u106e-\u1070\u1075-\u1081\u108e\u10a0-\u10c5\u10c7\u10cd\u10d0-\u10fa\u10fc-\u1248\u124a-\u124d\u1250-\u1256\u1258\u125a-\u125d\u1260-\u1288\u128a-\u128d\u1290-\u12b0\u12b2-\u12b5\u12b8-\u12be\u12c0\u12c2-\u12c5\u12c8-\u12d6\u12d8-\u1310\u1312-\u1315\u1318-\u135a\u1380-\u138f\u13a0-\u13f5\u13f8-\u13fd\u1401-\u166c\u166f-\u167f\u1681-\u169a\u16a0-\u16ea\u16ee-\u16f8\u1700-\u170c\u170e-\u1711\u1720-\u1731\u1740-\u1751\u1760-\u176c\u176e-\u1770\u1780-\u17b3\u17d7\u17dc\u1820-\u1878\u1880-\u18a8\u18aa\u18b0-\u18f5\u1900-\u191e\u1950-\u196d\u1970-\u1974\u1980-\u19ab\u19b0-\u19c9\u1a00-\u1a16\u1a20-\u1a54\u1aa7\u1b05-\u1b33\u1b45-\u1b4b\u1b83-\u1ba0\u1bae\u1baf\u1bba-\u1be5\u1c00-\u1c23\u1c4d-\u1c4f\u1c5a-\u1c7d\u1c80-\u1c88\u1c90-\u1cba\u1cbd-\u1cbf\u1ce9-\u1cec\u1cee-\u1cf1\u1cf5\u1cf6\u1d00-\u1dbf\u1e00-\u1f15\u1f18-\u1f1d\u1f20-\u1f45\u1f48-\u1f4d\u1f50-\u1f57\u1f59\u1f5b\u1f5d\u1f5f-\u1f7d\u1f80-\u1fb4\u1fb6-\u1fbc\u1fbe\u1fc2-\u1fc4\u1fc6-\u1fcc\u1fd0-\u1fd3\u1fd6-\u1fdb\u1fe0-\u1fec\u1ff2-\u1ff4\u1ff6-\u1ffc\u2071\u207f\u2090-\u209c\u2102\u2107\u210a-\u2113\u2115\u2118-\u211d\u2124\u2126\u2128\u212a-\u2139\u213c-\u213f\u2145-\u2149\u214e\u2160-\u2188\u2c00-\u2c2e\u2c30-\u2c5e\u2c60-\u2ce4\u2ceb-\u2cee\u2cf2\u2cf3\u2d00-\u2d25\u2d27\u2d2d\u2d30-\u2d67\u2d6f\u2d80-\u2d96\u2da0-\u2da6\u2da8-\u2dae\u2db0-\u2db6\u2db8-\u2dbe\u2dc0-\u2dc6\u2dc8-\u2dce\u2dd0-\u2dd6\u2dd8-\u2dde\u3005-\u3007\u3021-\u3029\u3031-\u3035\u3038-\u303c\u3041-\u3096\u309b-\u309f\u30a1-\u30fa\u30fc-\u30ff\u3105-\u312f\u3131-\u318e\u31a0-\u31ba\u31f0-\u31ff\u3400-\u4db5\u4e00-\u9fef\ua000-\ua48c\ua4d0-\ua4fd\ua500-\ua60c\ua610-\ua61f\ua62a\ua62b\ua640-\ua66e\ua67f-\ua69d\ua6a0-\ua6ef\ua717-\ua71f\ua722-\ua788\ua78b-\ua7b9\ua7f7-\ua801\ua803-\ua805\ua807-\ua80a\ua80c-\ua822\ua840-\ua873\ua882-\ua8b3\ua8f2-\ua8f7\ua8fb\ua8fd\ua8fe\ua90a-\ua925\ua930-\ua946\ua960-\ua97c\ua984-\ua9b2\ua9cf\ua9e0-\ua9e4\ua9e6-\ua9ef\ua9fa-\ua9fe\uaa00-\uaa28\uaa40-\uaa42\uaa44-\uaa4b\uaa60-\uaa76\uaa7a\uaa7e-\uaaaf\uaab1\uaab5\uaab6\uaab9-\uaabd\uaac0\uaac2\uaadb-\uaadd\uaae0-\uaaea\uaaf2-\uaaf4\uab01-\uab06\uab09-\uab0e\uab11-\uab16\uab20-\uab26\uab28-\uab2e\uab30-\uab5a\uab5c-\uab65\uab70-\uabe2\uac00-\ud7a3\ud7b0-\ud7c6\ud7cb-\ud7fb\uf900-\ufa6d\ufa70-\ufad9\ufb00-\ufb06\ufb13-\ufb17\ufb1d\ufb1f-\ufb28\ufb2a-\ufb36\ufb38-\ufb3c\ufb3e\ufb40\ufb41\ufb43\ufb44\ufb46-\ufbb1\ufbd3-\ufd3d\ufd50-\ufd8f\ufd92-\ufdc7\ufdf0-\ufdfb\ufe70-\ufe74\ufe76-\ufefc\uff21-\uff3a\uff41-\uff5a\uff66-\uffbe\uffc2-\uffc7\uffca-\uffcf\uffd2-\uffd7\uffda-\uffdc";
|
655 | let nonASCIIidentifierChars = "\u200c\u200d\xb7\u0300-\u036f\u0387\u0483-\u0487\u0591-\u05bd\u05bf\u05c1\u05c2\u05c4\u05c5\u05c7\u0610-\u061a\u064b-\u0669\u0670\u06d6-\u06dc\u06df-\u06e4\u06e7\u06e8\u06ea-\u06ed\u06f0-\u06f9\u0711\u0730-\u074a\u07a6-\u07b0\u07c0-\u07c9\u07eb-\u07f3\u07fd\u0816-\u0819\u081b-\u0823\u0825-\u0827\u0829-\u082d\u0859-\u085b\u08d3-\u08e1\u08e3-\u0903\u093a-\u093c\u093e-\u094f\u0951-\u0957\u0962\u0963\u0966-\u096f\u0981-\u0983\u09bc\u09be-\u09c4\u09c7\u09c8\u09cb-\u09cd\u09d7\u09e2\u09e3\u09e6-\u09ef\u09fe\u0a01-\u0a03\u0a3c\u0a3e-\u0a42\u0a47\u0a48\u0a4b-\u0a4d\u0a51\u0a66-\u0a71\u0a75\u0a81-\u0a83\u0abc\u0abe-\u0ac5\u0ac7-\u0ac9\u0acb-\u0acd\u0ae2\u0ae3\u0ae6-\u0aef\u0afa-\u0aff\u0b01-\u0b03\u0b3c\u0b3e-\u0b44\u0b47\u0b48\u0b4b-\u0b4d\u0b56\u0b57\u0b62\u0b63\u0b66-\u0b6f\u0b82\u0bbe-\u0bc2\u0bc6-\u0bc8\u0bca-\u0bcd\u0bd7\u0be6-\u0bef\u0c00-\u0c04\u0c3e-\u0c44\u0c46-\u0c48\u0c4a-\u0c4d\u0c55\u0c56\u0c62\u0c63\u0c66-\u0c6f\u0c81-\u0c83\u0cbc\u0cbe-\u0cc4\u0cc6-\u0cc8\u0cca-\u0ccd\u0cd5\u0cd6\u0ce2\u0ce3\u0ce6-\u0cef\u0d00-\u0d03\u0d3b\u0d3c\u0d3e-\u0d44\u0d46-\u0d48\u0d4a-\u0d4d\u0d57\u0d62\u0d63\u0d66-\u0d6f\u0d82\u0d83\u0dca\u0dcf-\u0dd4\u0dd6\u0dd8-\u0ddf\u0de6-\u0def\u0df2\u0df3\u0e31\u0e34-\u0e3a\u0e47-\u0e4e\u0e50-\u0e59\u0eb1\u0eb4-\u0eb9\u0ebb\u0ebc\u0ec8-\u0ecd\u0ed0-\u0ed9\u0f18\u0f19\u0f20-\u0f29\u0f35\u0f37\u0f39\u0f3e\u0f3f\u0f71-\u0f84\u0f86\u0f87\u0f8d-\u0f97\u0f99-\u0fbc\u0fc6\u102b-\u103e\u1040-\u1049\u1056-\u1059\u105e-\u1060\u1062-\u1064\u1067-\u106d\u1071-\u1074\u1082-\u108d\u108f-\u109d\u135d-\u135f\u1369-\u1371\u1712-\u1714\u1732-\u1734\u1752\u1753\u1772\u1773\u17b4-\u17d3\u17dd\u17e0-\u17e9\u180b-\u180d\u1810-\u1819\u18a9\u1920-\u192b\u1930-\u193b\u1946-\u194f\u19d0-\u19da\u1a17-\u1a1b\u1a55-\u1a5e\u1a60-\u1a7c\u1a7f-\u1a89\u1a90-\u1a99\u1ab0-\u1abd\u1b00-\u1b04\u1b34-\u1b44\u1b50-\u1b59\u1b6b-\u1b73\u1b80-\u1b82\u1ba1-\u1bad\u1bb0-\u1bb9\u1be6-\u1bf3\u1c24-\u1c37\u1c40-\u1c49\u1c50-\u1c59\u1cd0-\u1cd2\u1cd4-\u1ce8\u1ced\u1cf2-\u1cf4\u1cf7-\u1cf9\u1dc0-\u1df9\u1dfb-\u1dff\u203f\u2040\u2054\u20d0-\u20dc\u20e1\u20e5-\u20f0\u2cef-\u2cf1\u2d7f\u2de0-\u2dff\u302a-\u302f\u3099\u309a\ua620-\ua629\ua66f\ua674-\ua67d\ua69e\ua69f\ua6f0\ua6f1\ua802\ua806\ua80b\ua823-\ua827\ua880\ua881\ua8b4-\ua8c5\ua8d0-\ua8d9\ua8e0-\ua8f1\ua8ff-\ua909\ua926-\ua92d\ua947-\ua953\ua980-\ua983\ua9b3-\ua9c0\ua9d0-\ua9d9\ua9e5\ua9f0-\ua9f9\uaa29-\uaa36\uaa43\uaa4c\uaa4d\uaa50-\uaa59\uaa7b-\uaa7d\uaab0\uaab2-\uaab4\uaab7\uaab8\uaabe\uaabf\uaac1\uaaeb-\uaaef\uaaf5\uaaf6\uabe3-\uabea\uabec\uabed\uabf0-\uabf9\ufb1e\ufe00-\ufe0f\ufe20-\ufe2f\ufe33\ufe34\ufe4d-\ufe4f\uff10-\uff19\uff3f";
|
656 | const nonASCIIidentifierStart = new RegExp("[" + nonASCIIidentifierStartChars + "]");
|
657 | const nonASCIIidentifier = new RegExp("[" + nonASCIIidentifierStartChars + nonASCIIidentifierChars + "]");
|
658 | nonASCIIidentifierStartChars = nonASCIIidentifierChars = null;
|
659 | const astralIdentifierStartCodes = [0, 11, 2, 25, 2, 18, 2, 1, 2, 14, 3, 13, 35, 122, 70, 52, 268, 28, 4, 48, 48, 31, 14, 29, 6, 37, 11, 29, 3, 35, 5, 7, 2, 4, 43, 157, 19, 35, 5, 35, 5, 39, 9, 51, 157, 310, 10, 21, 11, 7, 153, 5, 3, 0, 2, 43, 2, 1, 4, 0, 3, 22, 11, 22, 10, 30, 66, 18, 2, 1, 11, 21, 11, 25, 71, 55, 7, 1, 65, 0, 16, 3, 2, 2, 2, 28, 43, 28, 4, 28, 36, 7, 2, 27, 28, 53, 11, 21, 11, 18, 14, 17, 111, 72, 56, 50, 14, 50, 14, 35, 477, 28, 11, 0, 9, 21, 190, 52, 76, 44, 33, 24, 27, 35, 30, 0, 12, 34, 4, 0, 13, 47, 15, 3, 22, 0, 2, 0, 36, 17, 2, 24, 85, 6, 2, 0, 2, 3, 2, 14, 2, 9, 8, 46, 39, 7, 3, 1, 3, 21, 2, 6, 2, 1, 2, 4, 4, 0, 19, 0, 13, 4, 159, 52, 19, 3, 54, 47, 21, 1, 2, 0, 185, 46, 42, 3, 37, 47, 21, 0, 60, 42, 86, 26, 230, 43, 117, 63, 32, 0, 257, 0, 11, 39, 8, 0, 22, 0, 12, 39, 3, 3, 20, 0, 35, 56, 264, 8, 2, 36, 18, 0, 50, 29, 113, 6, 2, 1, 2, 37, 22, 0, 26, 5, 2, 1, 2, 31, 15, 0, 328, 18, 270, 921, 103, 110, 18, 195, 2749, 1070, 4050, 582, 8634, 568, 8, 30, 114, 29, 19, 47, 17, 3, 32, 20, 6, 18, 689, 63, 129, 68, 12, 0, 67, 12, 65, 1, 31, 6129, 15, 754, 9486, 286, 82, 395, 2309, 106, 6, 12, 4, 8, 8, 9, 5991, 84, 2, 70, 2, 1, 3, 0, 3, 1, 3, 3, 2, 11, 2, 0, 2, 6, 2, 64, 2, 3, 3, 7, 2, 6, 2, 27, 2, 3, 2, 4, 2, 0, 4, 6, 2, 339, 3, 24, 2, 24, 2, 30, 2, 24, 2, 30, 2, 24, 2, 30, 2, 24, 2, 30, 2, 24, 2, 7, 4149, 196, 60, 67, 1213, 3, 2, 26, 2, 1, 2, 0, 3, 0, 2, 9, 2, 3, 2, 0, 2, 0, 7, 0, 5, 0, 2, 0, 2, 0, 2, 2, 2, 1, 2, 0, 3, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 1, 2, 0, 3, 3, 2, 6, 2, 3, 2, 3, 2, 0, 2, 9, 2, 16, 6, 2, 2, 4, 2, 16, 4421, 42710, 42, 4148, 12, 221, 3, 5761, 15, 7472, 3104, 541];
|
660 | const astralIdentifierCodes = [509, 0, 227, 0, 150, 4, 294, 9, 1368, 2, 2, 1, 6, 3, 41, 2, 5, 0, 166, 1, 574, 3, 9, 9, 525, 10, 176, 2, 54, 14, 32, 9, 16, 3, 46, 10, 54, 9, 7, 2, 37, 13, 2, 9, 6, 1, 45, 0, 13, 2, 49, 13, 9, 3, 4, 9, 83, 11, 7, 0, 161, 11, 6, 9, 7, 3, 56, 1, 2, 6, 3, 1, 3, 2, 10, 0, 11, 1, 3, 6, 4, 4, 193, 17, 10, 9, 5, 0, 82, 19, 13, 9, 214, 6, 3, 8, 28, 1, 83, 16, 16, 9, 82, 12, 9, 9, 84, 14, 5, 9, 243, 14, 166, 9, 280, 9, 41, 6, 2, 3, 9, 0, 10, 10, 47, 15, 406, 7, 2, 7, 17, 9, 57, 21, 2, 13, 123, 5, 4, 0, 2, 1, 2, 6, 2, 0, 9, 9, 49, 4, 2, 1, 2, 4, 9, 9, 330, 3, 19306, 9, 135, 4, 60, 6, 26, 9, 1016, 45, 17, 3, 19723, 1, 5319, 4, 4, 5, 9, 7, 3, 6, 31, 3, 149, 2, 1418, 49, 513, 54, 5, 49, 9, 0, 15, 0, 23, 4, 2, 14, 1361, 6, 2, 16, 3, 6, 2, 1, 2, 4, 2214, 6, 110, 6, 6, 9, 792487, 239];
|
661 |
|
662 | function isInAstralSet(code, set) {
|
663 | let pos = 0x10000;
|
664 |
|
665 | for (let i = 0, length = set.length; i < length; i += 2) {
|
666 | pos += set[i];
|
667 | if (pos > code) return false;
|
668 | pos += set[i + 1];
|
669 | if (pos >= code) return true;
|
670 | }
|
671 |
|
672 | return false;
|
673 | }
|
674 |
|
675 | function isIdentifierStart(code) {
|
676 | if (code < 65) return code === 36;
|
677 | if (code <= 90) return true;
|
678 | if (code < 97) return code === 95;
|
679 | if (code <= 122) return true;
|
680 |
|
681 | if (code <= 0xffff) {
|
682 | return code >= 0xaa && nonASCIIidentifierStart.test(String.fromCharCode(code));
|
683 | }
|
684 |
|
685 | return isInAstralSet(code, astralIdentifierStartCodes);
|
686 | }
|
687 | function isIteratorStart(current, next) {
|
688 | return current === 64 && next === 64;
|
689 | }
|
690 | function isIdentifierChar(code) {
|
691 | if (code < 48) return code === 36;
|
692 | if (code < 58) return true;
|
693 | if (code < 65) return false;
|
694 | if (code <= 90) return true;
|
695 | if (code < 97) return code === 95;
|
696 | if (code <= 122) return true;
|
697 |
|
698 | if (code <= 0xffff) {
|
699 | return code >= 0xaa && nonASCIIidentifier.test(String.fromCharCode(code));
|
700 | }
|
701 |
|
702 | return isInAstralSet(code, astralIdentifierStartCodes) || isInAstralSet(code, astralIdentifierCodes);
|
703 | }
|
704 |
|
705 | const reservedTypes = ["any", "bool", "boolean", "empty", "false", "mixed", "null", "number", "static", "string", "true", "typeof", "void", "interface", "extends", "_"];
|
706 |
|
707 | function isEsModuleType(bodyElement) {
|
708 | return bodyElement.type === "DeclareExportAllDeclaration" || bodyElement.type === "DeclareExportDeclaration" && (!bodyElement.declaration || bodyElement.declaration.type !== "TypeAlias" && bodyElement.declaration.type !== "InterfaceDeclaration");
|
709 | }
|
710 |
|
711 | function hasTypeImportKind(node) {
|
712 | return node.importKind === "type" || node.importKind === "typeof";
|
713 | }
|
714 |
|
715 | function isMaybeDefaultImport(state) {
|
716 | return (state.type === types.name || !!state.type.keyword) && state.value !== "from";
|
717 | }
|
718 |
|
719 | const exportSuggestions = {
|
720 | const: "declare export var",
|
721 | let: "declare export var",
|
722 | type: "export type",
|
723 | interface: "export interface"
|
724 | };
|
725 |
|
726 | function partition(list, test) {
|
727 | const list1 = [];
|
728 | const list2 = [];
|
729 |
|
730 | for (let i = 0; i < list.length; i++) {
|
731 | (test(list[i], i, list) ? list1 : list2).push(list[i]);
|
732 | }
|
733 |
|
734 | return [list1, list2];
|
735 | }
|
736 |
|
737 | const FLOW_PRAGMA_REGEX = /\*?\s*@((?:no)?flow)\b/;
|
738 | var flow = (superClass => class extends superClass {
|
739 | constructor(options, input) {
|
740 | super(options, input);
|
741 | this.flowPragma = undefined;
|
742 | }
|
743 |
|
744 | shouldParseTypes() {
|
745 | return this.getPluginOption("flow", "all") || this.flowPragma === "flow";
|
746 | }
|
747 |
|
748 | addComment(comment) {
|
749 | if (this.flowPragma === undefined) {
|
750 | const matches = FLOW_PRAGMA_REGEX.exec(comment.value);
|
751 |
|
752 | if (!matches) {
|
753 | this.flowPragma = null;
|
754 | } else if (matches[1] === "flow") {
|
755 | this.flowPragma = "flow";
|
756 | } else if (matches[1] === "noflow") {
|
757 | this.flowPragma = "noflow";
|
758 | } else {
|
759 | throw new Error("Unexpected flow pragma");
|
760 | }
|
761 | }
|
762 |
|
763 | return super.addComment(comment);
|
764 | }
|
765 |
|
766 | flowParseTypeInitialiser(tok) {
|
767 | const oldInType = this.state.inType;
|
768 | this.state.inType = true;
|
769 | this.expect(tok || types.colon);
|
770 | const type = this.flowParseType();
|
771 | this.state.inType = oldInType;
|
772 | return type;
|
773 | }
|
774 |
|
775 | flowParsePredicate() {
|
776 | const node = this.startNode();
|
777 | const moduloLoc = this.state.startLoc;
|
778 | const moduloPos = this.state.start;
|
779 | this.expect(types.modulo);
|
780 | const checksLoc = this.state.startLoc;
|
781 | this.expectContextual("checks");
|
782 |
|
783 | if (moduloLoc.line !== checksLoc.line || moduloLoc.column !== checksLoc.column - 1) {
|
784 | this.raise(moduloPos, "Spaces between ´%´ and ´checks´ are not allowed here.");
|
785 | }
|
786 |
|
787 | if (this.eat(types.parenL)) {
|
788 | node.value = this.parseExpression();
|
789 | this.expect(types.parenR);
|
790 | return this.finishNode(node, "DeclaredPredicate");
|
791 | } else {
|
792 | return this.finishNode(node, "InferredPredicate");
|
793 | }
|
794 | }
|
795 |
|
796 | flowParseTypeAndPredicateInitialiser() {
|
797 | const oldInType = this.state.inType;
|
798 | this.state.inType = true;
|
799 | this.expect(types.colon);
|
800 | let type = null;
|
801 | let predicate = null;
|
802 |
|
803 | if (this.match(types.modulo)) {
|
804 | this.state.inType = oldInType;
|
805 | predicate = this.flowParsePredicate();
|
806 | } else {
|
807 | type = this.flowParseType();
|
808 | this.state.inType = oldInType;
|
809 |
|
810 | if (this.match(types.modulo)) {
|
811 | predicate = this.flowParsePredicate();
|
812 | }
|
813 | }
|
814 |
|
815 | return [type, predicate];
|
816 | }
|
817 |
|
818 | flowParseDeclareClass(node) {
|
819 | this.next();
|
820 | this.flowParseInterfaceish(node, true);
|
821 | return this.finishNode(node, "DeclareClass");
|
822 | }
|
823 |
|
824 | flowParseDeclareFunction(node) {
|
825 | this.next();
|
826 | const id = node.id = this.parseIdentifier();
|
827 | const typeNode = this.startNode();
|
828 | const typeContainer = this.startNode();
|
829 |
|
830 | if (this.isRelational("<")) {
|
831 | typeNode.typeParameters = this.flowParseTypeParameterDeclaration();
|
832 | } else {
|
833 | typeNode.typeParameters = null;
|
834 | }
|
835 |
|
836 | this.expect(types.parenL);
|
837 | const tmp = this.flowParseFunctionTypeParams();
|
838 | typeNode.params = tmp.params;
|
839 | typeNode.rest = tmp.rest;
|
840 | this.expect(types.parenR);
|
841 | [typeNode.returnType, node.predicate] = this.flowParseTypeAndPredicateInitialiser();
|
842 | typeContainer.typeAnnotation = this.finishNode(typeNode, "FunctionTypeAnnotation");
|
843 | id.typeAnnotation = this.finishNode(typeContainer, "TypeAnnotation");
|
844 | this.finishNode(id, id.type);
|
845 | this.semicolon();
|
846 | return this.finishNode(node, "DeclareFunction");
|
847 | }
|
848 |
|
849 | flowParseDeclare(node, insideModule) {
|
850 | if (this.match(types._class)) {
|
851 | return this.flowParseDeclareClass(node);
|
852 | } else if (this.match(types._function)) {
|
853 | return this.flowParseDeclareFunction(node);
|
854 | } else if (this.match(types._var)) {
|
855 | return this.flowParseDeclareVariable(node);
|
856 | } else if (this.isContextual("module")) {
|
857 | if (this.lookahead().type === types.dot) {
|
858 | return this.flowParseDeclareModuleExports(node);
|
859 | } else {
|
860 | if (insideModule) {
|
861 | this.unexpected(null, "`declare module` cannot be used inside another `declare module`");
|
862 | }
|
863 |
|
864 | return this.flowParseDeclareModule(node);
|
865 | }
|
866 | } else if (this.isContextual("type")) {
|
867 | return this.flowParseDeclareTypeAlias(node);
|
868 | } else if (this.isContextual("opaque")) {
|
869 | return this.flowParseDeclareOpaqueType(node);
|
870 | } else if (this.isContextual("interface")) {
|
871 | return this.flowParseDeclareInterface(node);
|
872 | } else if (this.match(types._export)) {
|
873 | return this.flowParseDeclareExportDeclaration(node, insideModule);
|
874 | } else {
|
875 | throw this.unexpected();
|
876 | }
|
877 | }
|
878 |
|
879 | flowParseDeclareVariable(node) {
|
880 | this.next();
|
881 | node.id = this.flowParseTypeAnnotatableIdentifier(true);
|
882 | this.semicolon();
|
883 | return this.finishNode(node, "DeclareVariable");
|
884 | }
|
885 |
|
886 | flowParseDeclareModule(node) {
|
887 | this.next();
|
888 |
|
889 | if (this.match(types.string)) {
|
890 | node.id = this.parseExprAtom();
|
891 | } else {
|
892 | node.id = this.parseIdentifier();
|
893 | }
|
894 |
|
895 | const bodyNode = node.body = this.startNode();
|
896 | const body = bodyNode.body = [];
|
897 | this.expect(types.braceL);
|
898 |
|
899 | while (!this.match(types.braceR)) {
|
900 | let bodyNode = this.startNode();
|
901 |
|
902 | if (this.match(types._import)) {
|
903 | const lookahead = this.lookahead();
|
904 |
|
905 | if (lookahead.value !== "type" && lookahead.value !== "typeof") {
|
906 | this.unexpected(null, "Imports within a `declare module` body must always be `import type` or `import typeof`");
|
907 | }
|
908 |
|
909 | this.next();
|
910 | this.parseImport(bodyNode);
|
911 | } else {
|
912 | this.expectContextual("declare", "Only declares and type imports are allowed inside declare module");
|
913 | bodyNode = this.flowParseDeclare(bodyNode, true);
|
914 | }
|
915 |
|
916 | body.push(bodyNode);
|
917 | }
|
918 |
|
919 | this.expect(types.braceR);
|
920 | this.finishNode(bodyNode, "BlockStatement");
|
921 | let kind = null;
|
922 | let hasModuleExport = false;
|
923 | const errorMessage = "Found both `declare module.exports` and `declare export` in the same module. " + "Modules can only have 1 since they are either an ES module or they are a CommonJS module";
|
924 | body.forEach(bodyElement => {
|
925 | if (isEsModuleType(bodyElement)) {
|
926 | if (kind === "CommonJS") {
|
927 | this.unexpected(bodyElement.start, errorMessage);
|
928 | }
|
929 |
|
930 | kind = "ES";
|
931 | } else if (bodyElement.type === "DeclareModuleExports") {
|
932 | if (hasModuleExport) {
|
933 | this.unexpected(bodyElement.start, "Duplicate `declare module.exports` statement");
|
934 | }
|
935 |
|
936 | if (kind === "ES") this.unexpected(bodyElement.start, errorMessage);
|
937 | kind = "CommonJS";
|
938 | hasModuleExport = true;
|
939 | }
|
940 | });
|
941 | node.kind = kind || "CommonJS";
|
942 | return this.finishNode(node, "DeclareModule");
|
943 | }
|
944 |
|
945 | flowParseDeclareExportDeclaration(node, insideModule) {
|
946 | this.expect(types._export);
|
947 |
|
948 | if (this.eat(types._default)) {
|
949 | if (this.match(types._function) || this.match(types._class)) {
|
950 | node.declaration = this.flowParseDeclare(this.startNode());
|
951 | } else {
|
952 | node.declaration = this.flowParseType();
|
953 | this.semicolon();
|
954 | }
|
955 |
|
956 | node.default = true;
|
957 | return this.finishNode(node, "DeclareExportDeclaration");
|
958 | } else {
|
959 | if (this.match(types._const) || this.isLet() || (this.isContextual("type") || this.isContextual("interface")) && !insideModule) {
|
960 | const label = this.state.value;
|
961 | const suggestion = exportSuggestions[label];
|
962 | this.unexpected(this.state.start, `\`declare export ${label}\` is not supported. Use \`${suggestion}\` instead`);
|
963 | }
|
964 |
|
965 | if (this.match(types._var) || this.match(types._function) || this.match(types._class) || this.isContextual("opaque")) {
|
966 | node.declaration = this.flowParseDeclare(this.startNode());
|
967 | node.default = false;
|
968 | return this.finishNode(node, "DeclareExportDeclaration");
|
969 | } else if (this.match(types.star) || this.match(types.braceL) || this.isContextual("interface") || this.isContextual("type") || this.isContextual("opaque")) {
|
970 | node = this.parseExport(node);
|
971 |
|
972 | if (node.type === "ExportNamedDeclaration") {
|
973 | node.type = "ExportDeclaration";
|
974 | node.default = false;
|
975 | delete node.exportKind;
|
976 | }
|
977 |
|
978 | node.type = "Declare" + node.type;
|
979 | return node;
|
980 | }
|
981 | }
|
982 |
|
983 | throw this.unexpected();
|
984 | }
|
985 |
|
986 | flowParseDeclareModuleExports(node) {
|
987 | this.expectContextual("module");
|
988 | this.expect(types.dot);
|
989 | this.expectContextual("exports");
|
990 | node.typeAnnotation = this.flowParseTypeAnnotation();
|
991 | this.semicolon();
|
992 | return this.finishNode(node, "DeclareModuleExports");
|
993 | }
|
994 |
|
995 | flowParseDeclareTypeAlias(node) {
|
996 | this.next();
|
997 | this.flowParseTypeAlias(node);
|
998 | return this.finishNode(node, "DeclareTypeAlias");
|
999 | }
|
1000 |
|
1001 | flowParseDeclareOpaqueType(node) {
|
1002 | this.next();
|
1003 | this.flowParseOpaqueType(node, true);
|
1004 | return this.finishNode(node, "DeclareOpaqueType");
|
1005 | }
|
1006 |
|
1007 | flowParseDeclareInterface(node) {
|
1008 | this.next();
|
1009 | this.flowParseInterfaceish(node);
|
1010 | return this.finishNode(node, "DeclareInterface");
|
1011 | }
|
1012 |
|
1013 | flowParseInterfaceish(node, isClass = false) {
|
1014 | node.id = this.flowParseRestrictedIdentifier(!isClass);
|
1015 |
|
1016 | if (this.isRelational("<")) {
|
1017 | node.typeParameters = this.flowParseTypeParameterDeclaration();
|
1018 | } else {
|
1019 | node.typeParameters = null;
|
1020 | }
|
1021 |
|
1022 | node.extends = [];
|
1023 | node.implements = [];
|
1024 | node.mixins = [];
|
1025 |
|
1026 | if (this.eat(types._extends)) {
|
1027 | do {
|
1028 | node.extends.push(this.flowParseInterfaceExtends());
|
1029 | } while (!isClass && this.eat(types.comma));
|
1030 | }
|
1031 |
|
1032 | if (this.isContextual("mixins")) {
|
1033 | this.next();
|
1034 |
|
1035 | do {
|
1036 | node.mixins.push(this.flowParseInterfaceExtends());
|
1037 | } while (this.eat(types.comma));
|
1038 | }
|
1039 |
|
1040 | if (this.isContextual("implements")) {
|
1041 | this.next();
|
1042 |
|
1043 | do {
|
1044 | node.implements.push(this.flowParseInterfaceExtends());
|
1045 | } while (this.eat(types.comma));
|
1046 | }
|
1047 |
|
1048 | node.body = this.flowParseObjectType({
|
1049 | allowStatic: isClass,
|
1050 | allowExact: false,
|
1051 | allowSpread: false,
|
1052 | allowProto: isClass,
|
1053 | allowInexact: false
|
1054 | });
|
1055 | }
|
1056 |
|
1057 | flowParseInterfaceExtends() {
|
1058 | const node = this.startNode();
|
1059 | node.id = this.flowParseQualifiedTypeIdentifier();
|
1060 |
|
1061 | if (this.isRelational("<")) {
|
1062 | node.typeParameters = this.flowParseTypeParameterInstantiation();
|
1063 | } else {
|
1064 | node.typeParameters = null;
|
1065 | }
|
1066 |
|
1067 | return this.finishNode(node, "InterfaceExtends");
|
1068 | }
|
1069 |
|
1070 | flowParseInterface(node) {
|
1071 | this.flowParseInterfaceish(node);
|
1072 | return this.finishNode(node, "InterfaceDeclaration");
|
1073 | }
|
1074 |
|
1075 | checkNotUnderscore(word) {
|
1076 | if (word === "_") {
|
1077 | throw this.unexpected(null, "`_` is only allowed as a type argument to call or new");
|
1078 | }
|
1079 | }
|
1080 |
|
1081 | checkReservedType(word, startLoc) {
|
1082 | if (reservedTypes.indexOf(word) > -1) {
|
1083 | this.raise(startLoc, `Cannot overwrite reserved type ${word}`);
|
1084 | }
|
1085 | }
|
1086 |
|
1087 | flowParseRestrictedIdentifier(liberal) {
|
1088 | this.checkReservedType(this.state.value, this.state.start);
|
1089 | return this.parseIdentifier(liberal);
|
1090 | }
|
1091 |
|
1092 | flowParseTypeAlias(node) {
|
1093 | node.id = this.flowParseRestrictedIdentifier();
|
1094 |
|
1095 | if (this.isRelational("<")) {
|
1096 | node.typeParameters = this.flowParseTypeParameterDeclaration();
|
1097 | } else {
|
1098 | node.typeParameters = null;
|
1099 | }
|
1100 |
|
1101 | node.right = this.flowParseTypeInitialiser(types.eq);
|
1102 | this.semicolon();
|
1103 | return this.finishNode(node, "TypeAlias");
|
1104 | }
|
1105 |
|
1106 | flowParseOpaqueType(node, declare) {
|
1107 | this.expectContextual("type");
|
1108 | node.id = this.flowParseRestrictedIdentifier(true);
|
1109 |
|
1110 | if (this.isRelational("<")) {
|
1111 | node.typeParameters = this.flowParseTypeParameterDeclaration();
|
1112 | } else {
|
1113 | node.typeParameters = null;
|
1114 | }
|
1115 |
|
1116 | node.supertype = null;
|
1117 |
|
1118 | if (this.match(types.colon)) {
|
1119 | node.supertype = this.flowParseTypeInitialiser(types.colon);
|
1120 | }
|
1121 |
|
1122 | node.impltype = null;
|
1123 |
|
1124 | if (!declare) {
|
1125 | node.impltype = this.flowParseTypeInitialiser(types.eq);
|
1126 | }
|
1127 |
|
1128 | this.semicolon();
|
1129 | return this.finishNode(node, "OpaqueType");
|
1130 | }
|
1131 |
|
1132 | flowParseTypeParameter(allowDefault = true, requireDefault = false) {
|
1133 | if (!allowDefault && requireDefault) {
|
1134 | throw new Error("Cannot disallow a default value (`allowDefault`) while also requiring it (`requireDefault`).");
|
1135 | }
|
1136 |
|
1137 | const nodeStart = this.state.start;
|
1138 | const node = this.startNode();
|
1139 | const variance = this.flowParseVariance();
|
1140 | const ident = this.flowParseTypeAnnotatableIdentifier();
|
1141 | node.name = ident.name;
|
1142 | node.variance = variance;
|
1143 | node.bound = ident.typeAnnotation;
|
1144 |
|
1145 | if (this.match(types.eq)) {
|
1146 | if (allowDefault) {
|
1147 | this.eat(types.eq);
|
1148 | node.default = this.flowParseType();
|
1149 | } else {
|
1150 | this.unexpected();
|
1151 | }
|
1152 | } else {
|
1153 | if (requireDefault) {
|
1154 | this.unexpected(nodeStart, "Type parameter declaration needs a default, since a preceding type parameter declaration has a default.");
|
1155 | }
|
1156 | }
|
1157 |
|
1158 | return this.finishNode(node, "TypeParameter");
|
1159 | }
|
1160 |
|
1161 | flowParseTypeParameterDeclaration(allowDefault = true) {
|
1162 | const oldInType = this.state.inType;
|
1163 | const node = this.startNode();
|
1164 | node.params = [];
|
1165 | this.state.inType = true;
|
1166 |
|
1167 | if (this.isRelational("<") || this.match(types.jsxTagStart)) {
|
1168 | this.next();
|
1169 | } else {
|
1170 | this.unexpected();
|
1171 | }
|
1172 |
|
1173 | let defaultRequired = false;
|
1174 |
|
1175 | do {
|
1176 | const typeParameter = this.flowParseTypeParameter(allowDefault, defaultRequired);
|
1177 | node.params.push(typeParameter);
|
1178 |
|
1179 | if (typeParameter.default) {
|
1180 | defaultRequired = true;
|
1181 | }
|
1182 |
|
1183 | if (!this.isRelational(">")) {
|
1184 | this.expect(types.comma);
|
1185 | }
|
1186 | } while (!this.isRelational(">"));
|
1187 |
|
1188 | this.expectRelational(">");
|
1189 | this.state.inType = oldInType;
|
1190 | return this.finishNode(node, "TypeParameterDeclaration");
|
1191 | }
|
1192 |
|
1193 | flowParseTypeParameterInstantiation() {
|
1194 | const node = this.startNode();
|
1195 | const oldInType = this.state.inType;
|
1196 | node.params = [];
|
1197 | this.state.inType = true;
|
1198 | this.expectRelational("<");
|
1199 | const oldNoAnonFunctionType = this.state.noAnonFunctionType;
|
1200 | this.state.noAnonFunctionType = false;
|
1201 |
|
1202 | while (!this.isRelational(">")) {
|
1203 | node.params.push(this.flowParseType());
|
1204 |
|
1205 | if (!this.isRelational(">")) {
|
1206 | this.expect(types.comma);
|
1207 | }
|
1208 | }
|
1209 |
|
1210 | this.state.noAnonFunctionType = oldNoAnonFunctionType;
|
1211 | this.expectRelational(">");
|
1212 | this.state.inType = oldInType;
|
1213 | return this.finishNode(node, "TypeParameterInstantiation");
|
1214 | }
|
1215 |
|
1216 | flowParseTypeParameterInstantiationCallOrNew() {
|
1217 | const node = this.startNode();
|
1218 | const oldInType = this.state.inType;
|
1219 | node.params = [];
|
1220 | this.state.inType = true;
|
1221 | this.expectRelational("<");
|
1222 |
|
1223 | while (!this.isRelational(">")) {
|
1224 | node.params.push(this.flowParseTypeOrImplicitInstantiation());
|
1225 |
|
1226 | if (!this.isRelational(">")) {
|
1227 | this.expect(types.comma);
|
1228 | }
|
1229 | }
|
1230 |
|
1231 | this.expectRelational(">");
|
1232 | this.state.inType = oldInType;
|
1233 | return this.finishNode(node, "TypeParameterInstantiation");
|
1234 | }
|
1235 |
|
1236 | flowParseInterfaceType() {
|
1237 | const node = this.startNode();
|
1238 | this.expectContextual("interface");
|
1239 | node.extends = [];
|
1240 |
|
1241 | if (this.eat(types._extends)) {
|
1242 | do {
|
1243 | node.extends.push(this.flowParseInterfaceExtends());
|
1244 | } while (this.eat(types.comma));
|
1245 | }
|
1246 |
|
1247 | node.body = this.flowParseObjectType({
|
1248 | allowStatic: false,
|
1249 | allowExact: false,
|
1250 | allowSpread: false,
|
1251 | allowProto: false,
|
1252 | allowInexact: false
|
1253 | });
|
1254 | return this.finishNode(node, "InterfaceTypeAnnotation");
|
1255 | }
|
1256 |
|
1257 | flowParseObjectPropertyKey() {
|
1258 | return this.match(types.num) || this.match(types.string) ? this.parseExprAtom() : this.parseIdentifier(true);
|
1259 | }
|
1260 |
|
1261 | flowParseObjectTypeIndexer(node, isStatic, variance) {
|
1262 | node.static = isStatic;
|
1263 |
|
1264 | if (this.lookahead().type === types.colon) {
|
1265 | node.id = this.flowParseObjectPropertyKey();
|
1266 | node.key = this.flowParseTypeInitialiser();
|
1267 | } else {
|
1268 | node.id = null;
|
1269 | node.key = this.flowParseType();
|
1270 | }
|
1271 |
|
1272 | this.expect(types.bracketR);
|
1273 | node.value = this.flowParseTypeInitialiser();
|
1274 | node.variance = variance;
|
1275 | return this.finishNode(node, "ObjectTypeIndexer");
|
1276 | }
|
1277 |
|
1278 | flowParseObjectTypeInternalSlot(node, isStatic) {
|
1279 | node.static = isStatic;
|
1280 | node.id = this.flowParseObjectPropertyKey();
|
1281 | this.expect(types.bracketR);
|
1282 | this.expect(types.bracketR);
|
1283 |
|
1284 | if (this.isRelational("<") || this.match(types.parenL)) {
|
1285 | node.method = true;
|
1286 | node.optional = false;
|
1287 | node.value = this.flowParseObjectTypeMethodish(this.startNodeAt(node.start, node.loc.start));
|
1288 | } else {
|
1289 | node.method = false;
|
1290 |
|
1291 | if (this.eat(types.question)) {
|
1292 | node.optional = true;
|
1293 | }
|
1294 |
|
1295 | node.value = this.flowParseTypeInitialiser();
|
1296 | }
|
1297 |
|
1298 | return this.finishNode(node, "ObjectTypeInternalSlot");
|
1299 | }
|
1300 |
|
1301 | flowParseObjectTypeMethodish(node) {
|
1302 | node.params = [];
|
1303 | node.rest = null;
|
1304 | node.typeParameters = null;
|
1305 |
|
1306 | if (this.isRelational("<")) {
|
1307 | node.typeParameters = this.flowParseTypeParameterDeclaration(false);
|
1308 | }
|
1309 |
|
1310 | this.expect(types.parenL);
|
1311 |
|
1312 | while (!this.match(types.parenR) && !this.match(types.ellipsis)) {
|
1313 | node.params.push(this.flowParseFunctionTypeParam());
|
1314 |
|
1315 | if (!this.match(types.parenR)) {
|
1316 | this.expect(types.comma);
|
1317 | }
|
1318 | }
|
1319 |
|
1320 | if (this.eat(types.ellipsis)) {
|
1321 | node.rest = this.flowParseFunctionTypeParam();
|
1322 | }
|
1323 |
|
1324 | this.expect(types.parenR);
|
1325 | node.returnType = this.flowParseTypeInitialiser();
|
1326 | return this.finishNode(node, "FunctionTypeAnnotation");
|
1327 | }
|
1328 |
|
1329 | flowParseObjectTypeCallProperty(node, isStatic) {
|
1330 | const valueNode = this.startNode();
|
1331 | node.static = isStatic;
|
1332 | node.value = this.flowParseObjectTypeMethodish(valueNode);
|
1333 | return this.finishNode(node, "ObjectTypeCallProperty");
|
1334 | }
|
1335 |
|
1336 | flowParseObjectType({
|
1337 | allowStatic,
|
1338 | allowExact,
|
1339 | allowSpread,
|
1340 | allowProto,
|
1341 | allowInexact
|
1342 | }) {
|
1343 | const oldInType = this.state.inType;
|
1344 | this.state.inType = true;
|
1345 | const nodeStart = this.startNode();
|
1346 | nodeStart.callProperties = [];
|
1347 | nodeStart.properties = [];
|
1348 | nodeStart.indexers = [];
|
1349 | nodeStart.internalSlots = [];
|
1350 | let endDelim;
|
1351 | let exact;
|
1352 | let inexact = false;
|
1353 |
|
1354 | if (allowExact && this.match(types.braceBarL)) {
|
1355 | this.expect(types.braceBarL);
|
1356 | endDelim = types.braceBarR;
|
1357 | exact = true;
|
1358 | } else {
|
1359 | this.expect(types.braceL);
|
1360 | endDelim = types.braceR;
|
1361 | exact = false;
|
1362 | }
|
1363 |
|
1364 | nodeStart.exact = exact;
|
1365 |
|
1366 | while (!this.match(endDelim)) {
|
1367 | let isStatic = false;
|
1368 | let protoStart = null;
|
1369 | const node = this.startNode();
|
1370 |
|
1371 | if (allowProto && this.isContextual("proto")) {
|
1372 | const lookahead = this.lookahead();
|
1373 |
|
1374 | if (lookahead.type !== types.colon && lookahead.type !== types.question) {
|
1375 | this.next();
|
1376 | protoStart = this.state.start;
|
1377 | allowStatic = false;
|
1378 | }
|
1379 | }
|
1380 |
|
1381 | if (allowStatic && this.isContextual("static")) {
|
1382 | const lookahead = this.lookahead();
|
1383 |
|
1384 | if (lookahead.type !== types.colon && lookahead.type !== types.question) {
|
1385 | this.next();
|
1386 | isStatic = true;
|
1387 | }
|
1388 | }
|
1389 |
|
1390 | const variance = this.flowParseVariance();
|
1391 |
|
1392 | if (this.eat(types.bracketL)) {
|
1393 | if (protoStart != null) {
|
1394 | this.unexpected(protoStart);
|
1395 | }
|
1396 |
|
1397 | if (this.eat(types.bracketL)) {
|
1398 | if (variance) {
|
1399 | this.unexpected(variance.start);
|
1400 | }
|
1401 |
|
1402 | nodeStart.internalSlots.push(this.flowParseObjectTypeInternalSlot(node, isStatic));
|
1403 | } else {
|
1404 | nodeStart.indexers.push(this.flowParseObjectTypeIndexer(node, isStatic, variance));
|
1405 | }
|
1406 | } else if (this.match(types.parenL) || this.isRelational("<")) {
|
1407 | if (protoStart != null) {
|
1408 | this.unexpected(protoStart);
|
1409 | }
|
1410 |
|
1411 | if (variance) {
|
1412 | this.unexpected(variance.start);
|
1413 | }
|
1414 |
|
1415 | nodeStart.callProperties.push(this.flowParseObjectTypeCallProperty(node, isStatic));
|
1416 | } else {
|
1417 | let kind = "init";
|
1418 |
|
1419 | if (this.isContextual("get") || this.isContextual("set")) {
|
1420 | const lookahead = this.lookahead();
|
1421 |
|
1422 | if (lookahead.type === types.name || lookahead.type === types.string || lookahead.type === types.num) {
|
1423 | kind = this.state.value;
|
1424 | this.next();
|
1425 | }
|
1426 | }
|
1427 |
|
1428 | const propOrInexact = this.flowParseObjectTypeProperty(node, isStatic, protoStart, variance, kind, allowSpread, allowInexact);
|
1429 |
|
1430 | if (propOrInexact === null) {
|
1431 | inexact = true;
|
1432 | } else {
|
1433 | nodeStart.properties.push(propOrInexact);
|
1434 | }
|
1435 | }
|
1436 |
|
1437 | this.flowObjectTypeSemicolon();
|
1438 | }
|
1439 |
|
1440 | this.expect(endDelim);
|
1441 |
|
1442 | if (allowSpread) {
|
1443 | nodeStart.inexact = inexact;
|
1444 | }
|
1445 |
|
1446 | const out = this.finishNode(nodeStart, "ObjectTypeAnnotation");
|
1447 | this.state.inType = oldInType;
|
1448 | return out;
|
1449 | }
|
1450 |
|
1451 | flowParseObjectTypeProperty(node, isStatic, protoStart, variance, kind, allowSpread, allowInexact) {
|
1452 | if (this.match(types.ellipsis)) {
|
1453 | if (!allowSpread) {
|
1454 | this.unexpected(null, "Spread operator cannot appear in class or interface definitions");
|
1455 | }
|
1456 |
|
1457 | if (protoStart != null) {
|
1458 | this.unexpected(protoStart);
|
1459 | }
|
1460 |
|
1461 | if (variance) {
|
1462 | this.unexpected(variance.start, "Spread properties cannot have variance");
|
1463 | }
|
1464 |
|
1465 | this.expect(types.ellipsis);
|
1466 | const isInexactToken = this.eat(types.comma) || this.eat(types.semi);
|
1467 |
|
1468 | if (this.match(types.braceR)) {
|
1469 | if (allowInexact) return null;
|
1470 | this.unexpected(null, "Explicit inexact syntax is only allowed inside inexact objects");
|
1471 | }
|
1472 |
|
1473 | if (this.match(types.braceBarR)) {
|
1474 | this.unexpected(null, "Explicit inexact syntax cannot appear inside an explicit exact object type");
|
1475 | }
|
1476 |
|
1477 | if (isInexactToken) {
|
1478 | this.unexpected(null, "Explicit inexact syntax must appear at the end of an inexact object");
|
1479 | }
|
1480 |
|
1481 | node.argument = this.flowParseType();
|
1482 | return this.finishNode(node, "ObjectTypeSpreadProperty");
|
1483 | } else {
|
1484 | node.key = this.flowParseObjectPropertyKey();
|
1485 | node.static = isStatic;
|
1486 | node.proto = protoStart != null;
|
1487 | node.kind = kind;
|
1488 | let optional = false;
|
1489 |
|
1490 | if (this.isRelational("<") || this.match(types.parenL)) {
|
1491 | node.method = true;
|
1492 |
|
1493 | if (protoStart != null) {
|
1494 | this.unexpected(protoStart);
|
1495 | }
|
1496 |
|
1497 | if (variance) {
|
1498 | this.unexpected(variance.start);
|
1499 | }
|
1500 |
|
1501 | node.value = this.flowParseObjectTypeMethodish(this.startNodeAt(node.start, node.loc.start));
|
1502 |
|
1503 | if (kind === "get" || kind === "set") {
|
1504 | this.flowCheckGetterSetterParams(node);
|
1505 | }
|
1506 | } else {
|
1507 | if (kind !== "init") this.unexpected();
|
1508 | node.method = false;
|
1509 |
|
1510 | if (this.eat(types.question)) {
|
1511 | optional = true;
|
1512 | }
|
1513 |
|
1514 | node.value = this.flowParseTypeInitialiser();
|
1515 | node.variance = variance;
|
1516 | }
|
1517 |
|
1518 | node.optional = optional;
|
1519 | return this.finishNode(node, "ObjectTypeProperty");
|
1520 | }
|
1521 | }
|
1522 |
|
1523 | flowCheckGetterSetterParams(property) {
|
1524 | const paramCount = property.kind === "get" ? 0 : 1;
|
1525 | const start = property.start;
|
1526 | const length = property.value.params.length + (property.value.rest ? 1 : 0);
|
1527 |
|
1528 | if (length !== paramCount) {
|
1529 | if (property.kind === "get") {
|
1530 | this.raise(start, "getter must not have any formal parameters");
|
1531 | } else {
|
1532 | this.raise(start, "setter must have exactly one formal parameter");
|
1533 | }
|
1534 | }
|
1535 |
|
1536 | if (property.kind === "set" && property.value.rest) {
|
1537 | this.raise(start, "setter function argument must not be a rest parameter");
|
1538 | }
|
1539 | }
|
1540 |
|
1541 | flowObjectTypeSemicolon() {
|
1542 | if (!this.eat(types.semi) && !this.eat(types.comma) && !this.match(types.braceR) && !this.match(types.braceBarR)) {
|
1543 | this.unexpected();
|
1544 | }
|
1545 | }
|
1546 |
|
1547 | flowParseQualifiedTypeIdentifier(startPos, startLoc, id) {
|
1548 | startPos = startPos || this.state.start;
|
1549 | startLoc = startLoc || this.state.startLoc;
|
1550 | let node = id || this.parseIdentifier();
|
1551 |
|
1552 | while (this.eat(types.dot)) {
|
1553 | const node2 = this.startNodeAt(startPos, startLoc);
|
1554 | node2.qualification = node;
|
1555 | node2.id = this.parseIdentifier();
|
1556 | node = this.finishNode(node2, "QualifiedTypeIdentifier");
|
1557 | }
|
1558 |
|
1559 | return node;
|
1560 | }
|
1561 |
|
1562 | flowParseGenericType(startPos, startLoc, id) {
|
1563 | const node = this.startNodeAt(startPos, startLoc);
|
1564 | node.typeParameters = null;
|
1565 | node.id = this.flowParseQualifiedTypeIdentifier(startPos, startLoc, id);
|
1566 |
|
1567 | if (this.isRelational("<")) {
|
1568 | node.typeParameters = this.flowParseTypeParameterInstantiation();
|
1569 | }
|
1570 |
|
1571 | return this.finishNode(node, "GenericTypeAnnotation");
|
1572 | }
|
1573 |
|
1574 | flowParseTypeofType() {
|
1575 | const node = this.startNode();
|
1576 | this.expect(types._typeof);
|
1577 | node.argument = this.flowParsePrimaryType();
|
1578 | return this.finishNode(node, "TypeofTypeAnnotation");
|
1579 | }
|
1580 |
|
1581 | flowParseTupleType() {
|
1582 | const node = this.startNode();
|
1583 | node.types = [];
|
1584 | this.expect(types.bracketL);
|
1585 |
|
1586 | while (this.state.pos < this.state.length && !this.match(types.bracketR)) {
|
1587 | node.types.push(this.flowParseType());
|
1588 | if (this.match(types.bracketR)) break;
|
1589 | this.expect(types.comma);
|
1590 | }
|
1591 |
|
1592 | this.expect(types.bracketR);
|
1593 | return this.finishNode(node, "TupleTypeAnnotation");
|
1594 | }
|
1595 |
|
1596 | flowParseFunctionTypeParam() {
|
1597 | let name = null;
|
1598 | let optional = false;
|
1599 | let typeAnnotation = null;
|
1600 | const node = this.startNode();
|
1601 | const lh = this.lookahead();
|
1602 |
|
1603 | if (lh.type === types.colon || lh.type === types.question) {
|
1604 | name = this.parseIdentifier();
|
1605 |
|
1606 | if (this.eat(types.question)) {
|
1607 | optional = true;
|
1608 | }
|
1609 |
|
1610 | typeAnnotation = this.flowParseTypeInitialiser();
|
1611 | } else {
|
1612 | typeAnnotation = this.flowParseType();
|
1613 | }
|
1614 |
|
1615 | node.name = name;
|
1616 | node.optional = optional;
|
1617 | node.typeAnnotation = typeAnnotation;
|
1618 | return this.finishNode(node, "FunctionTypeParam");
|
1619 | }
|
1620 |
|
1621 | reinterpretTypeAsFunctionTypeParam(type) {
|
1622 | const node = this.startNodeAt(type.start, type.loc.start);
|
1623 | node.name = null;
|
1624 | node.optional = false;
|
1625 | node.typeAnnotation = type;
|
1626 | return this.finishNode(node, "FunctionTypeParam");
|
1627 | }
|
1628 |
|
1629 | flowParseFunctionTypeParams(params = []) {
|
1630 | let rest = null;
|
1631 |
|
1632 | while (!this.match(types.parenR) && !this.match(types.ellipsis)) {
|
1633 | params.push(this.flowParseFunctionTypeParam());
|
1634 |
|
1635 | if (!this.match(types.parenR)) {
|
1636 | this.expect(types.comma);
|
1637 | }
|
1638 | }
|
1639 |
|
1640 | if (this.eat(types.ellipsis)) {
|
1641 | rest = this.flowParseFunctionTypeParam();
|
1642 | }
|
1643 |
|
1644 | return {
|
1645 | params,
|
1646 | rest
|
1647 | };
|
1648 | }
|
1649 |
|
1650 | flowIdentToTypeAnnotation(startPos, startLoc, node, id) {
|
1651 | switch (id.name) {
|
1652 | case "any":
|
1653 | return this.finishNode(node, "AnyTypeAnnotation");
|
1654 |
|
1655 | case "bool":
|
1656 | case "boolean":
|
1657 | return this.finishNode(node, "BooleanTypeAnnotation");
|
1658 |
|
1659 | case "mixed":
|
1660 | return this.finishNode(node, "MixedTypeAnnotation");
|
1661 |
|
1662 | case "empty":
|
1663 | return this.finishNode(node, "EmptyTypeAnnotation");
|
1664 |
|
1665 | case "number":
|
1666 | return this.finishNode(node, "NumberTypeAnnotation");
|
1667 |
|
1668 | case "string":
|
1669 | return this.finishNode(node, "StringTypeAnnotation");
|
1670 |
|
1671 | default:
|
1672 | this.checkNotUnderscore(id.name);
|
1673 | return this.flowParseGenericType(startPos, startLoc, id);
|
1674 | }
|
1675 | }
|
1676 |
|
1677 | flowParsePrimaryType() {
|
1678 | const startPos = this.state.start;
|
1679 | const startLoc = this.state.startLoc;
|
1680 | const node = this.startNode();
|
1681 | let tmp;
|
1682 | let type;
|
1683 | let isGroupedType = false;
|
1684 | const oldNoAnonFunctionType = this.state.noAnonFunctionType;
|
1685 |
|
1686 | switch (this.state.type) {
|
1687 | case types.name:
|
1688 | if (this.isContextual("interface")) {
|
1689 | return this.flowParseInterfaceType();
|
1690 | }
|
1691 |
|
1692 | return this.flowIdentToTypeAnnotation(startPos, startLoc, node, this.parseIdentifier());
|
1693 |
|
1694 | case types.braceL:
|
1695 | return this.flowParseObjectType({
|
1696 | allowStatic: false,
|
1697 | allowExact: false,
|
1698 | allowSpread: true,
|
1699 | allowProto: false,
|
1700 | allowInexact: true
|
1701 | });
|
1702 |
|
1703 | case types.braceBarL:
|
1704 | return this.flowParseObjectType({
|
1705 | allowStatic: false,
|
1706 | allowExact: true,
|
1707 | allowSpread: true,
|
1708 | allowProto: false,
|
1709 | allowInexact: false
|
1710 | });
|
1711 |
|
1712 | case types.bracketL:
|
1713 | return this.flowParseTupleType();
|
1714 |
|
1715 | case types.relational:
|
1716 | if (this.state.value === "<") {
|
1717 | node.typeParameters = this.flowParseTypeParameterDeclaration(false);
|
1718 | this.expect(types.parenL);
|
1719 | tmp = this.flowParseFunctionTypeParams();
|
1720 | node.params = tmp.params;
|
1721 | node.rest = tmp.rest;
|
1722 | this.expect(types.parenR);
|
1723 | this.expect(types.arrow);
|
1724 | node.returnType = this.flowParseType();
|
1725 | return this.finishNode(node, "FunctionTypeAnnotation");
|
1726 | }
|
1727 |
|
1728 | break;
|
1729 |
|
1730 | case types.parenL:
|
1731 | this.next();
|
1732 |
|
1733 | if (!this.match(types.parenR) && !this.match(types.ellipsis)) {
|
1734 | if (this.match(types.name)) {
|
1735 | const token = this.lookahead().type;
|
1736 | isGroupedType = token !== types.question && token !== types.colon;
|
1737 | } else {
|
1738 | isGroupedType = true;
|
1739 | }
|
1740 | }
|
1741 |
|
1742 | if (isGroupedType) {
|
1743 | this.state.noAnonFunctionType = false;
|
1744 | type = this.flowParseType();
|
1745 | this.state.noAnonFunctionType = oldNoAnonFunctionType;
|
1746 |
|
1747 | if (this.state.noAnonFunctionType || !(this.match(types.comma) || this.match(types.parenR) && this.lookahead().type === types.arrow)) {
|
1748 | this.expect(types.parenR);
|
1749 | return type;
|
1750 | } else {
|
1751 | this.eat(types.comma);
|
1752 | }
|
1753 | }
|
1754 |
|
1755 | if (type) {
|
1756 | tmp = this.flowParseFunctionTypeParams([this.reinterpretTypeAsFunctionTypeParam(type)]);
|
1757 | } else {
|
1758 | tmp = this.flowParseFunctionTypeParams();
|
1759 | }
|
1760 |
|
1761 | node.params = tmp.params;
|
1762 | node.rest = tmp.rest;
|
1763 | this.expect(types.parenR);
|
1764 | this.expect(types.arrow);
|
1765 | node.returnType = this.flowParseType();
|
1766 | node.typeParameters = null;
|
1767 | return this.finishNode(node, "FunctionTypeAnnotation");
|
1768 |
|
1769 | case types.string:
|
1770 | return this.parseLiteral(this.state.value, "StringLiteralTypeAnnotation");
|
1771 |
|
1772 | case types._true:
|
1773 | case types._false:
|
1774 | node.value = this.match(types._true);
|
1775 | this.next();
|
1776 | return this.finishNode(node, "BooleanLiteralTypeAnnotation");
|
1777 |
|
1778 | case types.plusMin:
|
1779 | if (this.state.value === "-") {
|
1780 | this.next();
|
1781 |
|
1782 | if (!this.match(types.num)) {
|
1783 | this.unexpected(null, `Unexpected token, expected "number"`);
|
1784 | }
|
1785 |
|
1786 | return this.parseLiteral(-this.state.value, "NumberLiteralTypeAnnotation", node.start, node.loc.start);
|
1787 | }
|
1788 |
|
1789 | this.unexpected();
|
1790 |
|
1791 | case types.num:
|
1792 | return this.parseLiteral(this.state.value, "NumberLiteralTypeAnnotation");
|
1793 |
|
1794 | case types._void:
|
1795 | this.next();
|
1796 | return this.finishNode(node, "VoidTypeAnnotation");
|
1797 |
|
1798 | case types._null:
|
1799 | this.next();
|
1800 | return this.finishNode(node, "NullLiteralTypeAnnotation");
|
1801 |
|
1802 | case types._this:
|
1803 | this.next();
|
1804 | return this.finishNode(node, "ThisTypeAnnotation");
|
1805 |
|
1806 | case types.star:
|
1807 | this.next();
|
1808 | return this.finishNode(node, "ExistsTypeAnnotation");
|
1809 |
|
1810 | default:
|
1811 | if (this.state.type.keyword === "typeof") {
|
1812 | return this.flowParseTypeofType();
|
1813 | } else if (this.state.type.keyword) {
|
1814 | const label = this.state.type.label;
|
1815 | this.next();
|
1816 | return super.createIdentifier(node, label);
|
1817 | }
|
1818 |
|
1819 | }
|
1820 |
|
1821 | throw this.unexpected();
|
1822 | }
|
1823 |
|
1824 | flowParsePostfixType() {
|
1825 | const startPos = this.state.start,
|
1826 | startLoc = this.state.startLoc;
|
1827 | let type = this.flowParsePrimaryType();
|
1828 |
|
1829 | while (this.match(types.bracketL) && !this.canInsertSemicolon()) {
|
1830 | const node = this.startNodeAt(startPos, startLoc);
|
1831 | node.elementType = type;
|
1832 | this.expect(types.bracketL);
|
1833 | this.expect(types.bracketR);
|
1834 | type = this.finishNode(node, "ArrayTypeAnnotation");
|
1835 | }
|
1836 |
|
1837 | return type;
|
1838 | }
|
1839 |
|
1840 | flowParsePrefixType() {
|
1841 | const node = this.startNode();
|
1842 |
|
1843 | if (this.eat(types.question)) {
|
1844 | node.typeAnnotation = this.flowParsePrefixType();
|
1845 | return this.finishNode(node, "NullableTypeAnnotation");
|
1846 | } else {
|
1847 | return this.flowParsePostfixType();
|
1848 | }
|
1849 | }
|
1850 |
|
1851 | flowParseAnonFunctionWithoutParens() {
|
1852 | const param = this.flowParsePrefixType();
|
1853 |
|
1854 | if (!this.state.noAnonFunctionType && this.eat(types.arrow)) {
|
1855 | const node = this.startNodeAt(param.start, param.loc.start);
|
1856 | node.params = [this.reinterpretTypeAsFunctionTypeParam(param)];
|
1857 | node.rest = null;
|
1858 | node.returnType = this.flowParseType();
|
1859 | node.typeParameters = null;
|
1860 | return this.finishNode(node, "FunctionTypeAnnotation");
|
1861 | }
|
1862 |
|
1863 | return param;
|
1864 | }
|
1865 |
|
1866 | flowParseIntersectionType() {
|
1867 | const node = this.startNode();
|
1868 | this.eat(types.bitwiseAND);
|
1869 | const type = this.flowParseAnonFunctionWithoutParens();
|
1870 | node.types = [type];
|
1871 |
|
1872 | while (this.eat(types.bitwiseAND)) {
|
1873 | node.types.push(this.flowParseAnonFunctionWithoutParens());
|
1874 | }
|
1875 |
|
1876 | return node.types.length === 1 ? type : this.finishNode(node, "IntersectionTypeAnnotation");
|
1877 | }
|
1878 |
|
1879 | flowParseUnionType() {
|
1880 | const node = this.startNode();
|
1881 | this.eat(types.bitwiseOR);
|
1882 | const type = this.flowParseIntersectionType();
|
1883 | node.types = [type];
|
1884 |
|
1885 | while (this.eat(types.bitwiseOR)) {
|
1886 | node.types.push(this.flowParseIntersectionType());
|
1887 | }
|
1888 |
|
1889 | return node.types.length === 1 ? type : this.finishNode(node, "UnionTypeAnnotation");
|
1890 | }
|
1891 |
|
1892 | flowParseType() {
|
1893 | const oldInType = this.state.inType;
|
1894 | this.state.inType = true;
|
1895 | const type = this.flowParseUnionType();
|
1896 | this.state.inType = oldInType;
|
1897 | this.state.exprAllowed = this.state.exprAllowed || this.state.noAnonFunctionType;
|
1898 | return type;
|
1899 | }
|
1900 |
|
1901 | flowParseTypeOrImplicitInstantiation() {
|
1902 | if (this.state.type === types.name && this.state.value === "_") {
|
1903 | const startPos = this.state.start;
|
1904 | const startLoc = this.state.startLoc;
|
1905 | const node = this.parseIdentifier();
|
1906 | return this.flowParseGenericType(startPos, startLoc, node);
|
1907 | } else {
|
1908 | return this.flowParseType();
|
1909 | }
|
1910 | }
|
1911 |
|
1912 | flowParseTypeAnnotation() {
|
1913 | const node = this.startNode();
|
1914 | node.typeAnnotation = this.flowParseTypeInitialiser();
|
1915 | return this.finishNode(node, "TypeAnnotation");
|
1916 | }
|
1917 |
|
1918 | flowParseTypeAnnotatableIdentifier(allowPrimitiveOverride) {
|
1919 | const ident = allowPrimitiveOverride ? this.parseIdentifier() : this.flowParseRestrictedIdentifier();
|
1920 |
|
1921 | if (this.match(types.colon)) {
|
1922 | ident.typeAnnotation = this.flowParseTypeAnnotation();
|
1923 | this.finishNode(ident, ident.type);
|
1924 | }
|
1925 |
|
1926 | return ident;
|
1927 | }
|
1928 |
|
1929 | typeCastToParameter(node) {
|
1930 | node.expression.typeAnnotation = node.typeAnnotation;
|
1931 | return this.finishNodeAt(node.expression, node.expression.type, node.typeAnnotation.end, node.typeAnnotation.loc.end);
|
1932 | }
|
1933 |
|
1934 | flowParseVariance() {
|
1935 | let variance = null;
|
1936 |
|
1937 | if (this.match(types.plusMin)) {
|
1938 | variance = this.startNode();
|
1939 |
|
1940 | if (this.state.value === "+") {
|
1941 | variance.kind = "plus";
|
1942 | } else {
|
1943 | variance.kind = "minus";
|
1944 | }
|
1945 |
|
1946 | this.next();
|
1947 | this.finishNode(variance, "Variance");
|
1948 | }
|
1949 |
|
1950 | return variance;
|
1951 | }
|
1952 |
|
1953 | parseFunctionBody(node, allowExpressionBody) {
|
1954 | if (allowExpressionBody) {
|
1955 | return this.forwardNoArrowParamsConversionAt(node, () => super.parseFunctionBody(node, true));
|
1956 | }
|
1957 |
|
1958 | return super.parseFunctionBody(node, false);
|
1959 | }
|
1960 |
|
1961 | parseFunctionBodyAndFinish(node, type, allowExpressionBody) {
|
1962 | if (!allowExpressionBody && this.match(types.colon)) {
|
1963 | const typeNode = this.startNode();
|
1964 | [typeNode.typeAnnotation, node.predicate] = this.flowParseTypeAndPredicateInitialiser();
|
1965 | node.returnType = typeNode.typeAnnotation ? this.finishNode(typeNode, "TypeAnnotation") : null;
|
1966 | }
|
1967 |
|
1968 | super.parseFunctionBodyAndFinish(node, type, allowExpressionBody);
|
1969 | }
|
1970 |
|
1971 | parseStatement(context, topLevel) {
|
1972 | if (this.state.strict && this.match(types.name) && this.state.value === "interface") {
|
1973 | const node = this.startNode();
|
1974 | this.next();
|
1975 | return this.flowParseInterface(node);
|
1976 | } else {
|
1977 | const stmt = super.parseStatement(context, topLevel);
|
1978 |
|
1979 | if (this.flowPragma === undefined && !this.isValidDirective(stmt)) {
|
1980 | this.flowPragma = null;
|
1981 | }
|
1982 |
|
1983 | return stmt;
|
1984 | }
|
1985 | }
|
1986 |
|
1987 | parseExpressionStatement(node, expr) {
|
1988 | if (expr.type === "Identifier") {
|
1989 | if (expr.name === "declare") {
|
1990 | if (this.match(types._class) || this.match(types.name) || this.match(types._function) || this.match(types._var) || this.match(types._export)) {
|
1991 | return this.flowParseDeclare(node);
|
1992 | }
|
1993 | } else if (this.match(types.name)) {
|
1994 | if (expr.name === "interface") {
|
1995 | return this.flowParseInterface(node);
|
1996 | } else if (expr.name === "type") {
|
1997 | return this.flowParseTypeAlias(node);
|
1998 | } else if (expr.name === "opaque") {
|
1999 | return this.flowParseOpaqueType(node, false);
|
2000 | }
|
2001 | }
|
2002 | }
|
2003 |
|
2004 | return super.parseExpressionStatement(node, expr);
|
2005 | }
|
2006 |
|
2007 | shouldParseExportDeclaration() {
|
2008 | return this.isContextual("type") || this.isContextual("interface") || this.isContextual("opaque") || super.shouldParseExportDeclaration();
|
2009 | }
|
2010 |
|
2011 | isExportDefaultSpecifier() {
|
2012 | if (this.match(types.name) && (this.state.value === "type" || this.state.value === "interface" || this.state.value === "opaque")) {
|
2013 | return false;
|
2014 | }
|
2015 |
|
2016 | return super.isExportDefaultSpecifier();
|
2017 | }
|
2018 |
|
2019 | parseConditional(expr, noIn, startPos, startLoc, refNeedsArrowPos) {
|
2020 | if (!this.match(types.question)) return expr;
|
2021 |
|
2022 | if (refNeedsArrowPos) {
|
2023 | const state = this.state.clone();
|
2024 |
|
2025 | try {
|
2026 | return super.parseConditional(expr, noIn, startPos, startLoc);
|
2027 | } catch (err) {
|
2028 | if (err instanceof SyntaxError) {
|
2029 | this.state = state;
|
2030 | refNeedsArrowPos.start = err.pos || this.state.start;
|
2031 | return expr;
|
2032 | } else {
|
2033 | throw err;
|
2034 | }
|
2035 | }
|
2036 | }
|
2037 |
|
2038 | this.expect(types.question);
|
2039 | const state = this.state.clone();
|
2040 | const originalNoArrowAt = this.state.noArrowAt;
|
2041 | const node = this.startNodeAt(startPos, startLoc);
|
2042 | let {
|
2043 | consequent,
|
2044 | failed
|
2045 | } = this.tryParseConditionalConsequent();
|
2046 | let [valid, invalid] = this.getArrowLikeExpressions(consequent);
|
2047 |
|
2048 | if (failed || invalid.length > 0) {
|
2049 | const noArrowAt = [...originalNoArrowAt];
|
2050 |
|
2051 | if (invalid.length > 0) {
|
2052 | this.state = state;
|
2053 | this.state.noArrowAt = noArrowAt;
|
2054 |
|
2055 | for (let i = 0; i < invalid.length; i++) {
|
2056 | noArrowAt.push(invalid[i].start);
|
2057 | }
|
2058 |
|
2059 | ({
|
2060 | consequent,
|
2061 | failed
|
2062 | } = this.tryParseConditionalConsequent());
|
2063 | [valid, invalid] = this.getArrowLikeExpressions(consequent);
|
2064 | }
|
2065 |
|
2066 | if (failed && valid.length > 1) {
|
2067 | this.raise(state.start, "Ambiguous expression: wrap the arrow functions in parentheses to disambiguate.");
|
2068 | }
|
2069 |
|
2070 | if (failed && valid.length === 1) {
|
2071 | this.state = state;
|
2072 | this.state.noArrowAt = noArrowAt.concat(valid[0].start);
|
2073 | ({
|
2074 | consequent,
|
2075 | failed
|
2076 | } = this.tryParseConditionalConsequent());
|
2077 | }
|
2078 |
|
2079 | this.getArrowLikeExpressions(consequent, true);
|
2080 | }
|
2081 |
|
2082 | this.state.noArrowAt = originalNoArrowAt;
|
2083 | this.expect(types.colon);
|
2084 | node.test = expr;
|
2085 | node.consequent = consequent;
|
2086 | node.alternate = this.forwardNoArrowParamsConversionAt(node, () => this.parseMaybeAssign(noIn, undefined, undefined, undefined));
|
2087 | return this.finishNode(node, "ConditionalExpression");
|
2088 | }
|
2089 |
|
2090 | tryParseConditionalConsequent() {
|
2091 | this.state.noArrowParamsConversionAt.push(this.state.start);
|
2092 | const consequent = this.parseMaybeAssign();
|
2093 | const failed = !this.match(types.colon);
|
2094 | this.state.noArrowParamsConversionAt.pop();
|
2095 | return {
|
2096 | consequent,
|
2097 | failed
|
2098 | };
|
2099 | }
|
2100 |
|
2101 | getArrowLikeExpressions(node, disallowInvalid) {
|
2102 | const stack = [node];
|
2103 | const arrows = [];
|
2104 |
|
2105 | while (stack.length !== 0) {
|
2106 | const node = stack.pop();
|
2107 |
|
2108 | if (node.type === "ArrowFunctionExpression") {
|
2109 | if (node.typeParameters || !node.returnType) {
|
2110 | this.toAssignableList(node.params, true, "arrow function parameters");
|
2111 | super.checkFunctionNameAndParams(node, true);
|
2112 | } else {
|
2113 | arrows.push(node);
|
2114 | }
|
2115 |
|
2116 | stack.push(node.body);
|
2117 | } else if (node.type === "ConditionalExpression") {
|
2118 | stack.push(node.consequent);
|
2119 | stack.push(node.alternate);
|
2120 | }
|
2121 | }
|
2122 |
|
2123 | if (disallowInvalid) {
|
2124 | for (let i = 0; i < arrows.length; i++) {
|
2125 | this.toAssignableList(node.params, true, "arrow function parameters");
|
2126 | }
|
2127 |
|
2128 | return [arrows, []];
|
2129 | }
|
2130 |
|
2131 | return partition(arrows, node => {
|
2132 | try {
|
2133 | this.toAssignableList(node.params, true, "arrow function parameters");
|
2134 | return true;
|
2135 | } catch (err) {
|
2136 | return false;
|
2137 | }
|
2138 | });
|
2139 | }
|
2140 |
|
2141 | forwardNoArrowParamsConversionAt(node, parse) {
|
2142 | let result;
|
2143 |
|
2144 | if (this.state.noArrowParamsConversionAt.indexOf(node.start) !== -1) {
|
2145 | this.state.noArrowParamsConversionAt.push(this.state.start);
|
2146 | result = parse();
|
2147 | this.state.noArrowParamsConversionAt.pop();
|
2148 | } else {
|
2149 | result = parse();
|
2150 | }
|
2151 |
|
2152 | return result;
|
2153 | }
|
2154 |
|
2155 | parseParenItem(node, startPos, startLoc) {
|
2156 | node = super.parseParenItem(node, startPos, startLoc);
|
2157 |
|
2158 | if (this.eat(types.question)) {
|
2159 | node.optional = true;
|
2160 | }
|
2161 |
|
2162 | if (this.match(types.colon)) {
|
2163 | const typeCastNode = this.startNodeAt(startPos, startLoc);
|
2164 | typeCastNode.expression = node;
|
2165 | typeCastNode.typeAnnotation = this.flowParseTypeAnnotation();
|
2166 | return this.finishNode(typeCastNode, "TypeCastExpression");
|
2167 | }
|
2168 |
|
2169 | return node;
|
2170 | }
|
2171 |
|
2172 | assertModuleNodeAllowed(node) {
|
2173 | if (node.type === "ImportDeclaration" && (node.importKind === "type" || node.importKind === "typeof") || node.type === "ExportNamedDeclaration" && node.exportKind === "type" || node.type === "ExportAllDeclaration" && node.exportKind === "type") {
|
2174 | return;
|
2175 | }
|
2176 |
|
2177 | super.assertModuleNodeAllowed(node);
|
2178 | }
|
2179 |
|
2180 | parseExport(node) {
|
2181 | const decl = super.parseExport(node);
|
2182 |
|
2183 | if (decl.type === "ExportNamedDeclaration" || decl.type === "ExportAllDeclaration") {
|
2184 | decl.exportKind = decl.exportKind || "value";
|
2185 | }
|
2186 |
|
2187 | return decl;
|
2188 | }
|
2189 |
|
2190 | parseExportDeclaration(node) {
|
2191 | if (this.isContextual("type")) {
|
2192 | node.exportKind = "type";
|
2193 | const declarationNode = this.startNode();
|
2194 | this.next();
|
2195 |
|
2196 | if (this.match(types.braceL)) {
|
2197 | node.specifiers = this.parseExportSpecifiers();
|
2198 | this.parseExportFrom(node);
|
2199 | return null;
|
2200 | } else {
|
2201 | return this.flowParseTypeAlias(declarationNode);
|
2202 | }
|
2203 | } else if (this.isContextual("opaque")) {
|
2204 | node.exportKind = "type";
|
2205 | const declarationNode = this.startNode();
|
2206 | this.next();
|
2207 | return this.flowParseOpaqueType(declarationNode, false);
|
2208 | } else if (this.isContextual("interface")) {
|
2209 | node.exportKind = "type";
|
2210 | const declarationNode = this.startNode();
|
2211 | this.next();
|
2212 | return this.flowParseInterface(declarationNode);
|
2213 | } else {
|
2214 | return super.parseExportDeclaration(node);
|
2215 | }
|
2216 | }
|
2217 |
|
2218 | eatExportStar(node) {
|
2219 | if (super.eatExportStar(...arguments)) return true;
|
2220 |
|
2221 | if (this.isContextual("type") && this.lookahead().type === types.star) {
|
2222 | node.exportKind = "type";
|
2223 | this.next();
|
2224 | this.next();
|
2225 | return true;
|
2226 | }
|
2227 |
|
2228 | return false;
|
2229 | }
|
2230 |
|
2231 | maybeParseExportNamespaceSpecifier(node) {
|
2232 | const pos = this.state.start;
|
2233 | const hasNamespace = super.maybeParseExportNamespaceSpecifier(node);
|
2234 |
|
2235 | if (hasNamespace && node.exportKind === "type") {
|
2236 | this.unexpected(pos);
|
2237 | }
|
2238 |
|
2239 | return hasNamespace;
|
2240 | }
|
2241 |
|
2242 | parseClassId(node, isStatement, optionalId) {
|
2243 | super.parseClassId(node, isStatement, optionalId);
|
2244 |
|
2245 | if (this.isRelational("<")) {
|
2246 | node.typeParameters = this.flowParseTypeParameterDeclaration();
|
2247 | }
|
2248 | }
|
2249 |
|
2250 | getTokenFromCode(code) {
|
2251 | const next = this.state.input.charCodeAt(this.state.pos + 1);
|
2252 |
|
2253 | if (code === 123 && next === 124) {
|
2254 | return this.finishOp(types.braceBarL, 2);
|
2255 | } else if (this.state.inType && (code === 62 || code === 60)) {
|
2256 | return this.finishOp(types.relational, 1);
|
2257 | } else if (isIteratorStart(code, next)) {
|
2258 | this.state.isIterator = true;
|
2259 | return super.readWord();
|
2260 | } else {
|
2261 | return super.getTokenFromCode(code);
|
2262 | }
|
2263 | }
|
2264 |
|
2265 | toAssignable(node, isBinding, contextDescription) {
|
2266 | if (node.type === "TypeCastExpression") {
|
2267 | return super.toAssignable(this.typeCastToParameter(node), isBinding, contextDescription);
|
2268 | } else {
|
2269 | return super.toAssignable(node, isBinding, contextDescription);
|
2270 | }
|
2271 | }
|
2272 |
|
2273 | toAssignableList(exprList, isBinding, contextDescription) {
|
2274 | for (let i = 0; i < exprList.length; i++) {
|
2275 | const expr = exprList[i];
|
2276 |
|
2277 | if (expr && expr.type === "TypeCastExpression") {
|
2278 | exprList[i] = this.typeCastToParameter(expr);
|
2279 | }
|
2280 | }
|
2281 |
|
2282 | return super.toAssignableList(exprList, isBinding, contextDescription);
|
2283 | }
|
2284 |
|
2285 | toReferencedList(exprList, isParenthesizedExpr) {
|
2286 | for (let i = 0; i < exprList.length; i++) {
|
2287 | const expr = exprList[i];
|
2288 |
|
2289 | if (expr && expr.type === "TypeCastExpression" && (!expr.extra || !expr.extra.parenthesized) && (exprList.length > 1 || !isParenthesizedExpr)) {
|
2290 | this.raise(expr.typeAnnotation.start, "The type cast expression is expected to be wrapped with parenthesis");
|
2291 | }
|
2292 | }
|
2293 |
|
2294 | return exprList;
|
2295 | }
|
2296 |
|
2297 | checkLVal(expr, isBinding, checkClashes, contextDescription) {
|
2298 | if (expr.type !== "TypeCastExpression") {
|
2299 | return super.checkLVal(expr, isBinding, checkClashes, contextDescription);
|
2300 | }
|
2301 | }
|
2302 |
|
2303 | parseClassProperty(node) {
|
2304 | if (this.match(types.colon)) {
|
2305 | node.typeAnnotation = this.flowParseTypeAnnotation();
|
2306 | }
|
2307 |
|
2308 | return super.parseClassProperty(node);
|
2309 | }
|
2310 |
|
2311 | parseClassPrivateProperty(node) {
|
2312 | if (this.match(types.colon)) {
|
2313 | node.typeAnnotation = this.flowParseTypeAnnotation();
|
2314 | }
|
2315 |
|
2316 | return super.parseClassPrivateProperty(node);
|
2317 | }
|
2318 |
|
2319 | isClassMethod() {
|
2320 | return this.isRelational("<") || super.isClassMethod();
|
2321 | }
|
2322 |
|
2323 | isClassProperty() {
|
2324 | return this.match(types.colon) || super.isClassProperty();
|
2325 | }
|
2326 |
|
2327 | isNonstaticConstructor(method) {
|
2328 | return !this.match(types.colon) && super.isNonstaticConstructor(method);
|
2329 | }
|
2330 |
|
2331 | pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor) {
|
2332 | if (method.variance) {
|
2333 | this.unexpected(method.variance.start);
|
2334 | }
|
2335 |
|
2336 | delete method.variance;
|
2337 |
|
2338 | if (this.isRelational("<")) {
|
2339 | method.typeParameters = this.flowParseTypeParameterDeclaration(false);
|
2340 | }
|
2341 |
|
2342 | super.pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor);
|
2343 | }
|
2344 |
|
2345 | pushClassPrivateMethod(classBody, method, isGenerator, isAsync) {
|
2346 | if (method.variance) {
|
2347 | this.unexpected(method.variance.start);
|
2348 | }
|
2349 |
|
2350 | delete method.variance;
|
2351 |
|
2352 | if (this.isRelational("<")) {
|
2353 | method.typeParameters = this.flowParseTypeParameterDeclaration();
|
2354 | }
|
2355 |
|
2356 | super.pushClassPrivateMethod(classBody, method, isGenerator, isAsync);
|
2357 | }
|
2358 |
|
2359 | parseClassSuper(node) {
|
2360 | super.parseClassSuper(node);
|
2361 |
|
2362 | if (node.superClass && this.isRelational("<")) {
|
2363 | node.superTypeParameters = this.flowParseTypeParameterInstantiation();
|
2364 | }
|
2365 |
|
2366 | if (this.isContextual("implements")) {
|
2367 | this.next();
|
2368 | const implemented = node.implements = [];
|
2369 |
|
2370 | do {
|
2371 | const node = this.startNode();
|
2372 | node.id = this.flowParseRestrictedIdentifier(true);
|
2373 |
|
2374 | if (this.isRelational("<")) {
|
2375 | node.typeParameters = this.flowParseTypeParameterInstantiation();
|
2376 | } else {
|
2377 | node.typeParameters = null;
|
2378 | }
|
2379 |
|
2380 | implemented.push(this.finishNode(node, "ClassImplements"));
|
2381 | } while (this.eat(types.comma));
|
2382 | }
|
2383 | }
|
2384 |
|
2385 | parsePropertyName(node) {
|
2386 | const variance = this.flowParseVariance();
|
2387 | const key = super.parsePropertyName(node);
|
2388 | node.variance = variance;
|
2389 | return key;
|
2390 | }
|
2391 |
|
2392 | parseObjPropValue(prop, startPos, startLoc, isGenerator, isAsync, isPattern, refShorthandDefaultPos, containsEsc) {
|
2393 | if (prop.variance) {
|
2394 | this.unexpected(prop.variance.start);
|
2395 | }
|
2396 |
|
2397 | delete prop.variance;
|
2398 | let typeParameters;
|
2399 |
|
2400 | if (this.isRelational("<")) {
|
2401 | typeParameters = this.flowParseTypeParameterDeclaration(false);
|
2402 | if (!this.match(types.parenL)) this.unexpected();
|
2403 | }
|
2404 |
|
2405 | super.parseObjPropValue(prop, startPos, startLoc, isGenerator, isAsync, isPattern, refShorthandDefaultPos, containsEsc);
|
2406 |
|
2407 | if (typeParameters) {
|
2408 | (prop.value || prop).typeParameters = typeParameters;
|
2409 | }
|
2410 | }
|
2411 |
|
2412 | parseAssignableListItemTypes(param) {
|
2413 | if (this.eat(types.question)) {
|
2414 | if (param.type !== "Identifier") {
|
2415 | throw this.raise(param.start, "A binding pattern parameter cannot be optional in an implementation signature.");
|
2416 | }
|
2417 |
|
2418 | param.optional = true;
|
2419 | }
|
2420 |
|
2421 | if (this.match(types.colon)) {
|
2422 | param.typeAnnotation = this.flowParseTypeAnnotation();
|
2423 | }
|
2424 |
|
2425 | this.finishNode(param, param.type);
|
2426 | return param;
|
2427 | }
|
2428 |
|
2429 | parseMaybeDefault(startPos, startLoc, left) {
|
2430 | const node = super.parseMaybeDefault(startPos, startLoc, left);
|
2431 |
|
2432 | if (node.type === "AssignmentPattern" && node.typeAnnotation && node.right.start < node.typeAnnotation.start) {
|
2433 | this.raise(node.typeAnnotation.start, "Type annotations must come before default assignments, " + "e.g. instead of `age = 25: number` use `age: number = 25`");
|
2434 | }
|
2435 |
|
2436 | return node;
|
2437 | }
|
2438 |
|
2439 | shouldParseDefaultImport(node) {
|
2440 | if (!hasTypeImportKind(node)) {
|
2441 | return super.shouldParseDefaultImport(node);
|
2442 | }
|
2443 |
|
2444 | return isMaybeDefaultImport(this.state);
|
2445 | }
|
2446 |
|
2447 | parseImportSpecifierLocal(node, specifier, type, contextDescription) {
|
2448 | specifier.local = hasTypeImportKind(node) ? this.flowParseRestrictedIdentifier(true) : this.parseIdentifier();
|
2449 | this.checkLVal(specifier.local, true, undefined, contextDescription);
|
2450 | node.specifiers.push(this.finishNode(specifier, type));
|
2451 | }
|
2452 |
|
2453 | maybeParseDefaultImportSpecifier(node) {
|
2454 | node.importKind = "value";
|
2455 | let kind = null;
|
2456 |
|
2457 | if (this.match(types._typeof)) {
|
2458 | kind = "typeof";
|
2459 | } else if (this.isContextual("type")) {
|
2460 | kind = "type";
|
2461 | }
|
2462 |
|
2463 | if (kind) {
|
2464 | const lh = this.lookahead();
|
2465 |
|
2466 | if (kind === "type" && lh.type === types.star) {
|
2467 | this.unexpected(lh.start);
|
2468 | }
|
2469 |
|
2470 | if (isMaybeDefaultImport(lh) || lh.type === types.braceL || lh.type === types.star) {
|
2471 | this.next();
|
2472 | node.importKind = kind;
|
2473 | }
|
2474 | }
|
2475 |
|
2476 | return super.maybeParseDefaultImportSpecifier(node);
|
2477 | }
|
2478 |
|
2479 | parseImportSpecifier(node) {
|
2480 | const specifier = this.startNode();
|
2481 | const firstIdentLoc = this.state.start;
|
2482 | const firstIdent = this.parseIdentifier(true);
|
2483 | let specifierTypeKind = null;
|
2484 |
|
2485 | if (firstIdent.name === "type") {
|
2486 | specifierTypeKind = "type";
|
2487 | } else if (firstIdent.name === "typeof") {
|
2488 | specifierTypeKind = "typeof";
|
2489 | }
|
2490 |
|
2491 | let isBinding = false;
|
2492 |
|
2493 | if (this.isContextual("as") && !this.isLookaheadContextual("as")) {
|
2494 | const as_ident = this.parseIdentifier(true);
|
2495 |
|
2496 | if (specifierTypeKind !== null && !this.match(types.name) && !this.state.type.keyword) {
|
2497 | specifier.imported = as_ident;
|
2498 | specifier.importKind = specifierTypeKind;
|
2499 | specifier.local = as_ident.__clone();
|
2500 | } else {
|
2501 | specifier.imported = firstIdent;
|
2502 | specifier.importKind = null;
|
2503 | specifier.local = this.parseIdentifier();
|
2504 | }
|
2505 | } else if (specifierTypeKind !== null && (this.match(types.name) || this.state.type.keyword)) {
|
2506 | specifier.imported = this.parseIdentifier(true);
|
2507 | specifier.importKind = specifierTypeKind;
|
2508 |
|
2509 | if (this.eatContextual("as")) {
|
2510 | specifier.local = this.parseIdentifier();
|
2511 | } else {
|
2512 | isBinding = true;
|
2513 | specifier.local = specifier.imported.__clone();
|
2514 | }
|
2515 | } else {
|
2516 | isBinding = true;
|
2517 | specifier.imported = firstIdent;
|
2518 | specifier.importKind = null;
|
2519 | specifier.local = specifier.imported.__clone();
|
2520 | }
|
2521 |
|
2522 | const nodeIsTypeImport = hasTypeImportKind(node);
|
2523 | const specifierIsTypeImport = hasTypeImportKind(specifier);
|
2524 |
|
2525 | if (nodeIsTypeImport && specifierIsTypeImport) {
|
2526 | this.raise(firstIdentLoc, "The `type` and `typeof` keywords on named imports can only be used on regular " + "`import` statements. It cannot be used with `import type` or `import typeof` statements");
|
2527 | }
|
2528 |
|
2529 | if (nodeIsTypeImport || specifierIsTypeImport) {
|
2530 | this.checkReservedType(specifier.local.name, specifier.local.start);
|
2531 | }
|
2532 |
|
2533 | if (isBinding && !nodeIsTypeImport && !specifierIsTypeImport) {
|
2534 | this.checkReservedWord(specifier.local.name, specifier.start, true, true);
|
2535 | }
|
2536 |
|
2537 | this.checkLVal(specifier.local, true, undefined, "import specifier");
|
2538 | node.specifiers.push(this.finishNode(specifier, "ImportSpecifier"));
|
2539 | }
|
2540 |
|
2541 | parseFunctionParams(node) {
|
2542 | const kind = node.kind;
|
2543 |
|
2544 | if (kind !== "get" && kind !== "set" && this.isRelational("<")) {
|
2545 | node.typeParameters = this.flowParseTypeParameterDeclaration(false);
|
2546 | }
|
2547 |
|
2548 | super.parseFunctionParams(node);
|
2549 | }
|
2550 |
|
2551 | parseVarId(decl, kind) {
|
2552 | super.parseVarId(decl, kind);
|
2553 |
|
2554 | if (this.match(types.colon)) {
|
2555 | decl.id.typeAnnotation = this.flowParseTypeAnnotation();
|
2556 | this.finishNode(decl.id, decl.id.type);
|
2557 | }
|
2558 | }
|
2559 |
|
2560 | parseAsyncArrowFromCallExpression(node, call) {
|
2561 | if (this.match(types.colon)) {
|
2562 | const oldNoAnonFunctionType = this.state.noAnonFunctionType;
|
2563 | this.state.noAnonFunctionType = true;
|
2564 | node.returnType = this.flowParseTypeAnnotation();
|
2565 | this.state.noAnonFunctionType = oldNoAnonFunctionType;
|
2566 | }
|
2567 |
|
2568 | return super.parseAsyncArrowFromCallExpression(node, call);
|
2569 | }
|
2570 |
|
2571 | shouldParseAsyncArrow() {
|
2572 | return this.match(types.colon) || super.shouldParseAsyncArrow();
|
2573 | }
|
2574 |
|
2575 | parseMaybeAssign(noIn, refShorthandDefaultPos, afterLeftParse, refNeedsArrowPos) {
|
2576 | let jsxError = null;
|
2577 |
|
2578 | if (this.hasPlugin("jsx") && (this.match(types.jsxTagStart) || this.isRelational("<"))) {
|
2579 | const state = this.state.clone();
|
2580 |
|
2581 | try {
|
2582 | return super.parseMaybeAssign(noIn, refShorthandDefaultPos, afterLeftParse, refNeedsArrowPos);
|
2583 | } catch (err) {
|
2584 | if (err instanceof SyntaxError) {
|
2585 | this.state = state;
|
2586 | const cLength = this.state.context.length;
|
2587 |
|
2588 | if (this.state.context[cLength - 1] === types$1.j_oTag) {
|
2589 | this.state.context.length -= 2;
|
2590 | }
|
2591 |
|
2592 | jsxError = err;
|
2593 | } else {
|
2594 | throw err;
|
2595 | }
|
2596 | }
|
2597 | }
|
2598 |
|
2599 | if (jsxError != null || this.isRelational("<")) {
|
2600 | let arrowExpression;
|
2601 | let typeParameters;
|
2602 |
|
2603 | try {
|
2604 | typeParameters = this.flowParseTypeParameterDeclaration();
|
2605 | arrowExpression = this.forwardNoArrowParamsConversionAt(typeParameters, () => super.parseMaybeAssign(noIn, refShorthandDefaultPos, afterLeftParse, refNeedsArrowPos));
|
2606 | arrowExpression.typeParameters = typeParameters;
|
2607 | this.resetStartLocationFromNode(arrowExpression, typeParameters);
|
2608 | } catch (err) {
|
2609 | throw jsxError || err;
|
2610 | }
|
2611 |
|
2612 | if (arrowExpression.type === "ArrowFunctionExpression") {
|
2613 | return arrowExpression;
|
2614 | } else if (jsxError != null) {
|
2615 | throw jsxError;
|
2616 | } else {
|
2617 | this.raise(typeParameters.start, "Expected an arrow function after this type parameter declaration");
|
2618 | }
|
2619 | }
|
2620 |
|
2621 | return super.parseMaybeAssign(noIn, refShorthandDefaultPos, afterLeftParse, refNeedsArrowPos);
|
2622 | }
|
2623 |
|
2624 | parseArrow(node) {
|
2625 | if (this.match(types.colon)) {
|
2626 | const state = this.state.clone();
|
2627 |
|
2628 | try {
|
2629 | const oldNoAnonFunctionType = this.state.noAnonFunctionType;
|
2630 | this.state.noAnonFunctionType = true;
|
2631 | const typeNode = this.startNode();
|
2632 | [typeNode.typeAnnotation, node.predicate] = this.flowParseTypeAndPredicateInitialiser();
|
2633 | this.state.noAnonFunctionType = oldNoAnonFunctionType;
|
2634 | if (this.canInsertSemicolon()) this.unexpected();
|
2635 | if (!this.match(types.arrow)) this.unexpected();
|
2636 | node.returnType = typeNode.typeAnnotation ? this.finishNode(typeNode, "TypeAnnotation") : null;
|
2637 | } catch (err) {
|
2638 | if (err instanceof SyntaxError) {
|
2639 | this.state = state;
|
2640 | } else {
|
2641 | throw err;
|
2642 | }
|
2643 | }
|
2644 | }
|
2645 |
|
2646 | return super.parseArrow(node);
|
2647 | }
|
2648 |
|
2649 | shouldParseArrow() {
|
2650 | return this.match(types.colon) || super.shouldParseArrow();
|
2651 | }
|
2652 |
|
2653 | setArrowFunctionParameters(node, params) {
|
2654 | if (this.state.noArrowParamsConversionAt.indexOf(node.start) !== -1) {
|
2655 | node.params = params;
|
2656 | } else {
|
2657 | super.setArrowFunctionParameters(node, params);
|
2658 | }
|
2659 | }
|
2660 |
|
2661 | checkFunctionNameAndParams(node, isArrowFunction) {
|
2662 | if (isArrowFunction && this.state.noArrowParamsConversionAt.indexOf(node.start) !== -1) {
|
2663 | return;
|
2664 | }
|
2665 |
|
2666 | return super.checkFunctionNameAndParams(node, isArrowFunction);
|
2667 | }
|
2668 |
|
2669 | parseParenAndDistinguishExpression(canBeArrow) {
|
2670 | return super.parseParenAndDistinguishExpression(canBeArrow && this.state.noArrowAt.indexOf(this.state.start) === -1);
|
2671 | }
|
2672 |
|
2673 | parseSubscripts(base, startPos, startLoc, noCalls) {
|
2674 | if (base.type === "Identifier" && base.name === "async" && this.state.noArrowAt.indexOf(startPos) !== -1) {
|
2675 | this.next();
|
2676 | const node = this.startNodeAt(startPos, startLoc);
|
2677 | node.callee = base;
|
2678 | node.arguments = this.parseCallExpressionArguments(types.parenR, false);
|
2679 | base = this.finishNode(node, "CallExpression");
|
2680 | } else if (base.type === "Identifier" && base.name === "async" && this.isRelational("<")) {
|
2681 | const state = this.state.clone();
|
2682 | let error;
|
2683 |
|
2684 | try {
|
2685 | const node = this.parseAsyncArrowWithTypeParameters(startPos, startLoc);
|
2686 | if (node) return node;
|
2687 | } catch (e) {
|
2688 | error = e;
|
2689 | }
|
2690 |
|
2691 | this.state = state;
|
2692 |
|
2693 | try {
|
2694 | return super.parseSubscripts(base, startPos, startLoc, noCalls);
|
2695 | } catch (e) {
|
2696 | throw error || e;
|
2697 | }
|
2698 | }
|
2699 |
|
2700 | return super.parseSubscripts(base, startPos, startLoc, noCalls);
|
2701 | }
|
2702 |
|
2703 | parseSubscript(base, startPos, startLoc, noCalls, subscriptState) {
|
2704 | if (this.match(types.questionDot) && this.isLookaheadRelational("<")) {
|
2705 | this.expectPlugin("optionalChaining");
|
2706 | subscriptState.optionalChainMember = true;
|
2707 |
|
2708 | if (noCalls) {
|
2709 | subscriptState.stop = true;
|
2710 | return base;
|
2711 | }
|
2712 |
|
2713 | this.next();
|
2714 | const node = this.startNodeAt(startPos, startLoc);
|
2715 | node.callee = base;
|
2716 | node.typeArguments = this.flowParseTypeParameterInstantiation();
|
2717 | this.expect(types.parenL);
|
2718 | node.arguments = this.parseCallExpressionArguments(types.parenR, false);
|
2719 | node.optional = true;
|
2720 | return this.finishNode(node, "OptionalCallExpression");
|
2721 | } else if (!noCalls && this.shouldParseTypes() && this.isRelational("<")) {
|
2722 | const node = this.startNodeAt(startPos, startLoc);
|
2723 | node.callee = base;
|
2724 | const state = this.state.clone();
|
2725 |
|
2726 | try {
|
2727 | node.typeArguments = this.flowParseTypeParameterInstantiationCallOrNew();
|
2728 | this.expect(types.parenL);
|
2729 | node.arguments = this.parseCallExpressionArguments(types.parenR, false);
|
2730 |
|
2731 | if (subscriptState.optionalChainMember) {
|
2732 | node.optional = false;
|
2733 | return this.finishNode(node, "OptionalCallExpression");
|
2734 | }
|
2735 |
|
2736 | return this.finishNode(node, "CallExpression");
|
2737 | } catch (e) {
|
2738 | if (e instanceof SyntaxError) {
|
2739 | this.state = state;
|
2740 | } else {
|
2741 | throw e;
|
2742 | }
|
2743 | }
|
2744 | }
|
2745 |
|
2746 | return super.parseSubscript(base, startPos, startLoc, noCalls, subscriptState);
|
2747 | }
|
2748 |
|
2749 | parseNewArguments(node) {
|
2750 | let targs = null;
|
2751 |
|
2752 | if (this.shouldParseTypes() && this.isRelational("<")) {
|
2753 | const state = this.state.clone();
|
2754 |
|
2755 | try {
|
2756 | targs = this.flowParseTypeParameterInstantiationCallOrNew();
|
2757 | } catch (e) {
|
2758 | if (e instanceof SyntaxError) {
|
2759 | this.state = state;
|
2760 | } else {
|
2761 | throw e;
|
2762 | }
|
2763 | }
|
2764 | }
|
2765 |
|
2766 | node.typeArguments = targs;
|
2767 | super.parseNewArguments(node);
|
2768 | }
|
2769 |
|
2770 | parseAsyncArrowWithTypeParameters(startPos, startLoc) {
|
2771 | const node = this.startNodeAt(startPos, startLoc);
|
2772 | this.parseFunctionParams(node);
|
2773 | if (!this.parseArrow(node)) return;
|
2774 | return this.parseArrowExpression(node, undefined, true);
|
2775 | }
|
2776 |
|
2777 | readToken_mult_modulo(code) {
|
2778 | const next = this.state.input.charCodeAt(this.state.pos + 1);
|
2779 |
|
2780 | if (code === 42 && next === 47 && this.state.hasFlowComment) {
|
2781 | this.state.hasFlowComment = false;
|
2782 | this.state.pos += 2;
|
2783 | this.nextToken();
|
2784 | return;
|
2785 | }
|
2786 |
|
2787 | super.readToken_mult_modulo(code);
|
2788 | }
|
2789 |
|
2790 | readToken_pipe_amp(code) {
|
2791 | const next = this.state.input.charCodeAt(this.state.pos + 1);
|
2792 |
|
2793 | if (code === 124 && next === 125) {
|
2794 | this.finishOp(types.braceBarR, 2);
|
2795 | return;
|
2796 | }
|
2797 |
|
2798 | super.readToken_pipe_amp(code);
|
2799 | }
|
2800 |
|
2801 | parseTopLevel(file, program) {
|
2802 | const fileNode = super.parseTopLevel(file, program);
|
2803 |
|
2804 | if (this.state.hasFlowComment) {
|
2805 | this.unexpected(null, "Unterminated flow-comment");
|
2806 | }
|
2807 |
|
2808 | return fileNode;
|
2809 | }
|
2810 |
|
2811 | skipBlockComment() {
|
2812 | if (this.hasPlugin("flowComments") && this.skipFlowComment()) {
|
2813 | if (this.state.hasFlowComment) {
|
2814 | this.unexpected(null, "Cannot have a flow comment inside another flow comment");
|
2815 | }
|
2816 |
|
2817 | this.hasFlowCommentCompletion();
|
2818 | this.state.pos += this.skipFlowComment();
|
2819 | this.state.hasFlowComment = true;
|
2820 | return;
|
2821 | }
|
2822 |
|
2823 | if (this.state.hasFlowComment) {
|
2824 | const end = this.state.input.indexOf("*-/", this.state.pos += 2);
|
2825 | if (end === -1) this.raise(this.state.pos - 2, "Unterminated comment");
|
2826 | this.state.pos = end + 3;
|
2827 | return;
|
2828 | }
|
2829 |
|
2830 | super.skipBlockComment();
|
2831 | }
|
2832 |
|
2833 | skipFlowComment() {
|
2834 | const {
|
2835 | pos
|
2836 | } = this.state;
|
2837 | let shiftToFirstNonWhiteSpace = 2;
|
2838 |
|
2839 | while ([32, 9].includes(this.state.input.charCodeAt(pos + shiftToFirstNonWhiteSpace))) {
|
2840 | shiftToFirstNonWhiteSpace++;
|
2841 | }
|
2842 |
|
2843 | const ch2 = this.state.input.charCodeAt(shiftToFirstNonWhiteSpace + pos);
|
2844 | const ch3 = this.state.input.charCodeAt(shiftToFirstNonWhiteSpace + pos + 1);
|
2845 |
|
2846 | if (ch2 === 58 && ch3 === 58) {
|
2847 | return shiftToFirstNonWhiteSpace + 2;
|
2848 | }
|
2849 |
|
2850 | if (this.state.input.slice(shiftToFirstNonWhiteSpace + pos, shiftToFirstNonWhiteSpace + pos + 12) === "flow-include") {
|
2851 | return shiftToFirstNonWhiteSpace + 12;
|
2852 | }
|
2853 |
|
2854 | if (ch2 === 58 && ch3 !== 58) {
|
2855 | return shiftToFirstNonWhiteSpace;
|
2856 | }
|
2857 |
|
2858 | return false;
|
2859 | }
|
2860 |
|
2861 | hasFlowCommentCompletion() {
|
2862 | const end = this.state.input.indexOf("*/", this.state.pos);
|
2863 |
|
2864 | if (end === -1) {
|
2865 | this.raise(this.state.pos, "Unterminated comment");
|
2866 | }
|
2867 | }
|
2868 |
|
2869 | });
|
2870 |
|
2871 | const entities = {
|
2872 | quot: "\u0022",
|
2873 | amp: "&",
|
2874 | apos: "\u0027",
|
2875 | lt: "<",
|
2876 | gt: ">",
|
2877 | nbsp: "\u00A0",
|
2878 | iexcl: "\u00A1",
|
2879 | cent: "\u00A2",
|
2880 | pound: "\u00A3",
|
2881 | curren: "\u00A4",
|
2882 | yen: "\u00A5",
|
2883 | brvbar: "\u00A6",
|
2884 | sect: "\u00A7",
|
2885 | uml: "\u00A8",
|
2886 | copy: "\u00A9",
|
2887 | ordf: "\u00AA",
|
2888 | laquo: "\u00AB",
|
2889 | not: "\u00AC",
|
2890 | shy: "\u00AD",
|
2891 | reg: "\u00AE",
|
2892 | macr: "\u00AF",
|
2893 | deg: "\u00B0",
|
2894 | plusmn: "\u00B1",
|
2895 | sup2: "\u00B2",
|
2896 | sup3: "\u00B3",
|
2897 | acute: "\u00B4",
|
2898 | micro: "\u00B5",
|
2899 | para: "\u00B6",
|
2900 | middot: "\u00B7",
|
2901 | cedil: "\u00B8",
|
2902 | sup1: "\u00B9",
|
2903 | ordm: "\u00BA",
|
2904 | raquo: "\u00BB",
|
2905 | frac14: "\u00BC",
|
2906 | frac12: "\u00BD",
|
2907 | frac34: "\u00BE",
|
2908 | iquest: "\u00BF",
|
2909 | Agrave: "\u00C0",
|
2910 | Aacute: "\u00C1",
|
2911 | Acirc: "\u00C2",
|
2912 | Atilde: "\u00C3",
|
2913 | Auml: "\u00C4",
|
2914 | Aring: "\u00C5",
|
2915 | AElig: "\u00C6",
|
2916 | Ccedil: "\u00C7",
|
2917 | Egrave: "\u00C8",
|
2918 | Eacute: "\u00C9",
|
2919 | Ecirc: "\u00CA",
|
2920 | Euml: "\u00CB",
|
2921 | Igrave: "\u00CC",
|
2922 | Iacute: "\u00CD",
|
2923 | Icirc: "\u00CE",
|
2924 | Iuml: "\u00CF",
|
2925 | ETH: "\u00D0",
|
2926 | Ntilde: "\u00D1",
|
2927 | Ograve: "\u00D2",
|
2928 | Oacute: "\u00D3",
|
2929 | Ocirc: "\u00D4",
|
2930 | Otilde: "\u00D5",
|
2931 | Ouml: "\u00D6",
|
2932 | times: "\u00D7",
|
2933 | Oslash: "\u00D8",
|
2934 | Ugrave: "\u00D9",
|
2935 | Uacute: "\u00DA",
|
2936 | Ucirc: "\u00DB",
|
2937 | Uuml: "\u00DC",
|
2938 | Yacute: "\u00DD",
|
2939 | THORN: "\u00DE",
|
2940 | szlig: "\u00DF",
|
2941 | agrave: "\u00E0",
|
2942 | aacute: "\u00E1",
|
2943 | acirc: "\u00E2",
|
2944 | atilde: "\u00E3",
|
2945 | auml: "\u00E4",
|
2946 | aring: "\u00E5",
|
2947 | aelig: "\u00E6",
|
2948 | ccedil: "\u00E7",
|
2949 | egrave: "\u00E8",
|
2950 | eacute: "\u00E9",
|
2951 | ecirc: "\u00EA",
|
2952 | euml: "\u00EB",
|
2953 | igrave: "\u00EC",
|
2954 | iacute: "\u00ED",
|
2955 | icirc: "\u00EE",
|
2956 | iuml: "\u00EF",
|
2957 | eth: "\u00F0",
|
2958 | ntilde: "\u00F1",
|
2959 | ograve: "\u00F2",
|
2960 | oacute: "\u00F3",
|
2961 | ocirc: "\u00F4",
|
2962 | otilde: "\u00F5",
|
2963 | ouml: "\u00F6",
|
2964 | divide: "\u00F7",
|
2965 | oslash: "\u00F8",
|
2966 | ugrave: "\u00F9",
|
2967 | uacute: "\u00FA",
|
2968 | ucirc: "\u00FB",
|
2969 | uuml: "\u00FC",
|
2970 | yacute: "\u00FD",
|
2971 | thorn: "\u00FE",
|
2972 | yuml: "\u00FF",
|
2973 | OElig: "\u0152",
|
2974 | oelig: "\u0153",
|
2975 | Scaron: "\u0160",
|
2976 | scaron: "\u0161",
|
2977 | Yuml: "\u0178",
|
2978 | fnof: "\u0192",
|
2979 | circ: "\u02C6",
|
2980 | tilde: "\u02DC",
|
2981 | Alpha: "\u0391",
|
2982 | Beta: "\u0392",
|
2983 | Gamma: "\u0393",
|
2984 | Delta: "\u0394",
|
2985 | Epsilon: "\u0395",
|
2986 | Zeta: "\u0396",
|
2987 | Eta: "\u0397",
|
2988 | Theta: "\u0398",
|
2989 | Iota: "\u0399",
|
2990 | Kappa: "\u039A",
|
2991 | Lambda: "\u039B",
|
2992 | Mu: "\u039C",
|
2993 | Nu: "\u039D",
|
2994 | Xi: "\u039E",
|
2995 | Omicron: "\u039F",
|
2996 | Pi: "\u03A0",
|
2997 | Rho: "\u03A1",
|
2998 | Sigma: "\u03A3",
|
2999 | Tau: "\u03A4",
|
3000 | Upsilon: "\u03A5",
|
3001 | Phi: "\u03A6",
|
3002 | Chi: "\u03A7",
|
3003 | Psi: "\u03A8",
|
3004 | Omega: "\u03A9",
|
3005 | alpha: "\u03B1",
|
3006 | beta: "\u03B2",
|
3007 | gamma: "\u03B3",
|
3008 | delta: "\u03B4",
|
3009 | epsilon: "\u03B5",
|
3010 | zeta: "\u03B6",
|
3011 | eta: "\u03B7",
|
3012 | theta: "\u03B8",
|
3013 | iota: "\u03B9",
|
3014 | kappa: "\u03BA",
|
3015 | lambda: "\u03BB",
|
3016 | mu: "\u03BC",
|
3017 | nu: "\u03BD",
|
3018 | xi: "\u03BE",
|
3019 | omicron: "\u03BF",
|
3020 | pi: "\u03C0",
|
3021 | rho: "\u03C1",
|
3022 | sigmaf: "\u03C2",
|
3023 | sigma: "\u03C3",
|
3024 | tau: "\u03C4",
|
3025 | upsilon: "\u03C5",
|
3026 | phi: "\u03C6",
|
3027 | chi: "\u03C7",
|
3028 | psi: "\u03C8",
|
3029 | omega: "\u03C9",
|
3030 | thetasym: "\u03D1",
|
3031 | upsih: "\u03D2",
|
3032 | piv: "\u03D6",
|
3033 | ensp: "\u2002",
|
3034 | emsp: "\u2003",
|
3035 | thinsp: "\u2009",
|
3036 | zwnj: "\u200C",
|
3037 | zwj: "\u200D",
|
3038 | lrm: "\u200E",
|
3039 | rlm: "\u200F",
|
3040 | ndash: "\u2013",
|
3041 | mdash: "\u2014",
|
3042 | lsquo: "\u2018",
|
3043 | rsquo: "\u2019",
|
3044 | sbquo: "\u201A",
|
3045 | ldquo: "\u201C",
|
3046 | rdquo: "\u201D",
|
3047 | bdquo: "\u201E",
|
3048 | dagger: "\u2020",
|
3049 | Dagger: "\u2021",
|
3050 | bull: "\u2022",
|
3051 | hellip: "\u2026",
|
3052 | permil: "\u2030",
|
3053 | prime: "\u2032",
|
3054 | Prime: "\u2033",
|
3055 | lsaquo: "\u2039",
|
3056 | rsaquo: "\u203A",
|
3057 | oline: "\u203E",
|
3058 | frasl: "\u2044",
|
3059 | euro: "\u20AC",
|
3060 | image: "\u2111",
|
3061 | weierp: "\u2118",
|
3062 | real: "\u211C",
|
3063 | trade: "\u2122",
|
3064 | alefsym: "\u2135",
|
3065 | larr: "\u2190",
|
3066 | uarr: "\u2191",
|
3067 | rarr: "\u2192",
|
3068 | darr: "\u2193",
|
3069 | harr: "\u2194",
|
3070 | crarr: "\u21B5",
|
3071 | lArr: "\u21D0",
|
3072 | uArr: "\u21D1",
|
3073 | rArr: "\u21D2",
|
3074 | dArr: "\u21D3",
|
3075 | hArr: "\u21D4",
|
3076 | forall: "\u2200",
|
3077 | part: "\u2202",
|
3078 | exist: "\u2203",
|
3079 | empty: "\u2205",
|
3080 | nabla: "\u2207",
|
3081 | isin: "\u2208",
|
3082 | notin: "\u2209",
|
3083 | ni: "\u220B",
|
3084 | prod: "\u220F",
|
3085 | sum: "\u2211",
|
3086 | minus: "\u2212",
|
3087 | lowast: "\u2217",
|
3088 | radic: "\u221A",
|
3089 | prop: "\u221D",
|
3090 | infin: "\u221E",
|
3091 | ang: "\u2220",
|
3092 | and: "\u2227",
|
3093 | or: "\u2228",
|
3094 | cap: "\u2229",
|
3095 | cup: "\u222A",
|
3096 | int: "\u222B",
|
3097 | there4: "\u2234",
|
3098 | sim: "\u223C",
|
3099 | cong: "\u2245",
|
3100 | asymp: "\u2248",
|
3101 | ne: "\u2260",
|
3102 | equiv: "\u2261",
|
3103 | le: "\u2264",
|
3104 | ge: "\u2265",
|
3105 | sub: "\u2282",
|
3106 | sup: "\u2283",
|
3107 | nsub: "\u2284",
|
3108 | sube: "\u2286",
|
3109 | supe: "\u2287",
|
3110 | oplus: "\u2295",
|
3111 | otimes: "\u2297",
|
3112 | perp: "\u22A5",
|
3113 | sdot: "\u22C5",
|
3114 | lceil: "\u2308",
|
3115 | rceil: "\u2309",
|
3116 | lfloor: "\u230A",
|
3117 | rfloor: "\u230B",
|
3118 | lang: "\u2329",
|
3119 | rang: "\u232A",
|
3120 | loz: "\u25CA",
|
3121 | spades: "\u2660",
|
3122 | clubs: "\u2663",
|
3123 | hearts: "\u2665",
|
3124 | diams: "\u2666"
|
3125 | };
|
3126 |
|
3127 | const HEX_NUMBER = /^[\da-fA-F]+$/;
|
3128 | const DECIMAL_NUMBER = /^\d+$/;
|
3129 | types$1.j_oTag = new TokContext("<tag", false);
|
3130 | types$1.j_cTag = new TokContext("</tag", false);
|
3131 | types$1.j_expr = new TokContext("<tag>...</tag>", true, true);
|
3132 | types.jsxName = new TokenType("jsxName");
|
3133 | types.jsxText = new TokenType("jsxText", {
|
3134 | beforeExpr: true
|
3135 | });
|
3136 | types.jsxTagStart = new TokenType("jsxTagStart", {
|
3137 | startsExpr: true
|
3138 | });
|
3139 | types.jsxTagEnd = new TokenType("jsxTagEnd");
|
3140 |
|
3141 | types.jsxTagStart.updateContext = function () {
|
3142 | this.state.context.push(types$1.j_expr);
|
3143 | this.state.context.push(types$1.j_oTag);
|
3144 | this.state.exprAllowed = false;
|
3145 | };
|
3146 |
|
3147 | types.jsxTagEnd.updateContext = function (prevType) {
|
3148 | const out = this.state.context.pop();
|
3149 |
|
3150 | if (out === types$1.j_oTag && prevType === types.slash || out === types$1.j_cTag) {
|
3151 | this.state.context.pop();
|
3152 | this.state.exprAllowed = this.curContext() === types$1.j_expr;
|
3153 | } else {
|
3154 | this.state.exprAllowed = true;
|
3155 | }
|
3156 | };
|
3157 |
|
3158 | function isFragment(object) {
|
3159 | return object ? object.type === "JSXOpeningFragment" || object.type === "JSXClosingFragment" : false;
|
3160 | }
|
3161 |
|
3162 | function getQualifiedJSXName(object) {
|
3163 | if (object.type === "JSXIdentifier") {
|
3164 | return object.name;
|
3165 | }
|
3166 |
|
3167 | if (object.type === "JSXNamespacedName") {
|
3168 | return object.namespace.name + ":" + object.name.name;
|
3169 | }
|
3170 |
|
3171 | if (object.type === "JSXMemberExpression") {
|
3172 | return getQualifiedJSXName(object.object) + "." + getQualifiedJSXName(object.property);
|
3173 | }
|
3174 |
|
3175 | throw new Error("Node had unexpected type: " + object.type);
|
3176 | }
|
3177 |
|
3178 | var jsx = (superClass => class extends superClass {
|
3179 | jsxReadToken() {
|
3180 | let out = "";
|
3181 | let chunkStart = this.state.pos;
|
3182 |
|
3183 | for (;;) {
|
3184 | if (this.state.pos >= this.state.length) {
|
3185 | this.raise(this.state.start, "Unterminated JSX contents");
|
3186 | }
|
3187 |
|
3188 | const ch = this.state.input.charCodeAt(this.state.pos);
|
3189 |
|
3190 | switch (ch) {
|
3191 | case 60:
|
3192 | case 123:
|
3193 | if (this.state.pos === this.state.start) {
|
3194 | if (ch === 60 && this.state.exprAllowed) {
|
3195 | ++this.state.pos;
|
3196 | return this.finishToken(types.jsxTagStart);
|
3197 | }
|
3198 |
|
3199 | return super.getTokenFromCode(ch);
|
3200 | }
|
3201 |
|
3202 | out += this.state.input.slice(chunkStart, this.state.pos);
|
3203 | return this.finishToken(types.jsxText, out);
|
3204 |
|
3205 | case 38:
|
3206 | out += this.state.input.slice(chunkStart, this.state.pos);
|
3207 | out += this.jsxReadEntity();
|
3208 | chunkStart = this.state.pos;
|
3209 | break;
|
3210 |
|
3211 | default:
|
3212 | if (isNewLine(ch)) {
|
3213 | out += this.state.input.slice(chunkStart, this.state.pos);
|
3214 | out += this.jsxReadNewLine(true);
|
3215 | chunkStart = this.state.pos;
|
3216 | } else {
|
3217 | ++this.state.pos;
|
3218 | }
|
3219 |
|
3220 | }
|
3221 | }
|
3222 | }
|
3223 |
|
3224 | jsxReadNewLine(normalizeCRLF) {
|
3225 | const ch = this.state.input.charCodeAt(this.state.pos);
|
3226 | let out;
|
3227 | ++this.state.pos;
|
3228 |
|
3229 | if (ch === 13 && this.state.input.charCodeAt(this.state.pos) === 10) {
|
3230 | ++this.state.pos;
|
3231 | out = normalizeCRLF ? "\n" : "\r\n";
|
3232 | } else {
|
3233 | out = String.fromCharCode(ch);
|
3234 | }
|
3235 |
|
3236 | ++this.state.curLine;
|
3237 | this.state.lineStart = this.state.pos;
|
3238 | return out;
|
3239 | }
|
3240 |
|
3241 | jsxReadString(quote) {
|
3242 | let out = "";
|
3243 | let chunkStart = ++this.state.pos;
|
3244 |
|
3245 | for (;;) {
|
3246 | if (this.state.pos >= this.state.length) {
|
3247 | this.raise(this.state.start, "Unterminated string constant");
|
3248 | }
|
3249 |
|
3250 | const ch = this.state.input.charCodeAt(this.state.pos);
|
3251 | if (ch === quote) break;
|
3252 |
|
3253 | if (ch === 38) {
|
3254 | out += this.state.input.slice(chunkStart, this.state.pos);
|
3255 | out += this.jsxReadEntity();
|
3256 | chunkStart = this.state.pos;
|
3257 | } else if (isNewLine(ch)) {
|
3258 | out += this.state.input.slice(chunkStart, this.state.pos);
|
3259 | out += this.jsxReadNewLine(false);
|
3260 | chunkStart = this.state.pos;
|
3261 | } else {
|
3262 | ++this.state.pos;
|
3263 | }
|
3264 | }
|
3265 |
|
3266 | out += this.state.input.slice(chunkStart, this.state.pos++);
|
3267 | return this.finishToken(types.string, out);
|
3268 | }
|
3269 |
|
3270 | jsxReadEntity() {
|
3271 | let str = "";
|
3272 | let count = 0;
|
3273 | let entity;
|
3274 | let ch = this.state.input[this.state.pos];
|
3275 | const startPos = ++this.state.pos;
|
3276 |
|
3277 | while (this.state.pos < this.state.length && count++ < 10) {
|
3278 | ch = this.state.input[this.state.pos++];
|
3279 |
|
3280 | if (ch === ";") {
|
3281 | if (str[0] === "#") {
|
3282 | if (str[1] === "x") {
|
3283 | str = str.substr(2);
|
3284 |
|
3285 | if (HEX_NUMBER.test(str)) {
|
3286 | entity = String.fromCodePoint(parseInt(str, 16));
|
3287 | }
|
3288 | } else {
|
3289 | str = str.substr(1);
|
3290 |
|
3291 | if (DECIMAL_NUMBER.test(str)) {
|
3292 | entity = String.fromCodePoint(parseInt(str, 10));
|
3293 | }
|
3294 | }
|
3295 | } else {
|
3296 | entity = entities[str];
|
3297 | }
|
3298 |
|
3299 | break;
|
3300 | }
|
3301 |
|
3302 | str += ch;
|
3303 | }
|
3304 |
|
3305 | if (!entity) {
|
3306 | this.state.pos = startPos;
|
3307 | return "&";
|
3308 | }
|
3309 |
|
3310 | return entity;
|
3311 | }
|
3312 |
|
3313 | jsxReadWord() {
|
3314 | let ch;
|
3315 | const start = this.state.pos;
|
3316 |
|
3317 | do {
|
3318 | ch = this.state.input.charCodeAt(++this.state.pos);
|
3319 | } while (isIdentifierChar(ch) || ch === 45);
|
3320 |
|
3321 | return this.finishToken(types.jsxName, this.state.input.slice(start, this.state.pos));
|
3322 | }
|
3323 |
|
3324 | jsxParseIdentifier() {
|
3325 | const node = this.startNode();
|
3326 |
|
3327 | if (this.match(types.jsxName)) {
|
3328 | node.name = this.state.value;
|
3329 | } else if (this.state.type.keyword) {
|
3330 | node.name = this.state.type.keyword;
|
3331 | } else {
|
3332 | this.unexpected();
|
3333 | }
|
3334 |
|
3335 | this.next();
|
3336 | return this.finishNode(node, "JSXIdentifier");
|
3337 | }
|
3338 |
|
3339 | jsxParseNamespacedName() {
|
3340 | const startPos = this.state.start;
|
3341 | const startLoc = this.state.startLoc;
|
3342 | const name = this.jsxParseIdentifier();
|
3343 | if (!this.eat(types.colon)) return name;
|
3344 | const node = this.startNodeAt(startPos, startLoc);
|
3345 | node.namespace = name;
|
3346 | node.name = this.jsxParseIdentifier();
|
3347 | return this.finishNode(node, "JSXNamespacedName");
|
3348 | }
|
3349 |
|
3350 | jsxParseElementName() {
|
3351 | const startPos = this.state.start;
|
3352 | const startLoc = this.state.startLoc;
|
3353 | let node = this.jsxParseNamespacedName();
|
3354 |
|
3355 | while (this.eat(types.dot)) {
|
3356 | const newNode = this.startNodeAt(startPos, startLoc);
|
3357 | newNode.object = node;
|
3358 | newNode.property = this.jsxParseIdentifier();
|
3359 | node = this.finishNode(newNode, "JSXMemberExpression");
|
3360 | }
|
3361 |
|
3362 | return node;
|
3363 | }
|
3364 |
|
3365 | jsxParseAttributeValue() {
|
3366 | let node;
|
3367 |
|
3368 | switch (this.state.type) {
|
3369 | case types.braceL:
|
3370 | node = this.jsxParseExpressionContainer();
|
3371 |
|
3372 | if (node.expression.type === "JSXEmptyExpression") {
|
3373 | throw this.raise(node.start, "JSX attributes must only be assigned a non-empty expression");
|
3374 | } else {
|
3375 | return node;
|
3376 | }
|
3377 |
|
3378 | case types.jsxTagStart:
|
3379 | case types.string:
|
3380 | return this.parseExprAtom();
|
3381 |
|
3382 | default:
|
3383 | throw this.raise(this.state.start, "JSX value should be either an expression or a quoted JSX text");
|
3384 | }
|
3385 | }
|
3386 |
|
3387 | jsxParseEmptyExpression() {
|
3388 | const node = this.startNodeAt(this.state.lastTokEnd, this.state.lastTokEndLoc);
|
3389 | return this.finishNodeAt(node, "JSXEmptyExpression", this.state.start, this.state.startLoc);
|
3390 | }
|
3391 |
|
3392 | jsxParseSpreadChild() {
|
3393 | const node = this.startNode();
|
3394 | this.expect(types.braceL);
|
3395 | this.expect(types.ellipsis);
|
3396 | node.expression = this.parseExpression();
|
3397 | this.expect(types.braceR);
|
3398 | return this.finishNode(node, "JSXSpreadChild");
|
3399 | }
|
3400 |
|
3401 | jsxParseExpressionContainer() {
|
3402 | const node = this.startNode();
|
3403 | this.next();
|
3404 |
|
3405 | if (this.match(types.braceR)) {
|
3406 | node.expression = this.jsxParseEmptyExpression();
|
3407 | } else {
|
3408 | node.expression = this.parseExpression();
|
3409 | }
|
3410 |
|
3411 | this.expect(types.braceR);
|
3412 | return this.finishNode(node, "JSXExpressionContainer");
|
3413 | }
|
3414 |
|
3415 | jsxParseAttribute() {
|
3416 | const node = this.startNode();
|
3417 |
|
3418 | if (this.eat(types.braceL)) {
|
3419 | this.expect(types.ellipsis);
|
3420 | node.argument = this.parseMaybeAssign();
|
3421 | this.expect(types.braceR);
|
3422 | return this.finishNode(node, "JSXSpreadAttribute");
|
3423 | }
|
3424 |
|
3425 | node.name = this.jsxParseNamespacedName();
|
3426 | node.value = this.eat(types.eq) ? this.jsxParseAttributeValue() : null;
|
3427 | return this.finishNode(node, "JSXAttribute");
|
3428 | }
|
3429 |
|
3430 | jsxParseOpeningElementAt(startPos, startLoc) {
|
3431 | const node = this.startNodeAt(startPos, startLoc);
|
3432 |
|
3433 | if (this.match(types.jsxTagEnd)) {
|
3434 | this.expect(types.jsxTagEnd);
|
3435 | return this.finishNode(node, "JSXOpeningFragment");
|
3436 | }
|
3437 |
|
3438 | node.name = this.jsxParseElementName();
|
3439 | return this.jsxParseOpeningElementAfterName(node);
|
3440 | }
|
3441 |
|
3442 | jsxParseOpeningElementAfterName(node) {
|
3443 | const attributes = [];
|
3444 |
|
3445 | while (!this.match(types.slash) && !this.match(types.jsxTagEnd)) {
|
3446 | attributes.push(this.jsxParseAttribute());
|
3447 | }
|
3448 |
|
3449 | node.attributes = attributes;
|
3450 | node.selfClosing = this.eat(types.slash);
|
3451 | this.expect(types.jsxTagEnd);
|
3452 | return this.finishNode(node, "JSXOpeningElement");
|
3453 | }
|
3454 |
|
3455 | jsxParseClosingElementAt(startPos, startLoc) {
|
3456 | const node = this.startNodeAt(startPos, startLoc);
|
3457 |
|
3458 | if (this.match(types.jsxTagEnd)) {
|
3459 | this.expect(types.jsxTagEnd);
|
3460 | return this.finishNode(node, "JSXClosingFragment");
|
3461 | }
|
3462 |
|
3463 | node.name = this.jsxParseElementName();
|
3464 | this.expect(types.jsxTagEnd);
|
3465 | return this.finishNode(node, "JSXClosingElement");
|
3466 | }
|
3467 |
|
3468 | jsxParseElementAt(startPos, startLoc) {
|
3469 | const node = this.startNodeAt(startPos, startLoc);
|
3470 | const children = [];
|
3471 | const openingElement = this.jsxParseOpeningElementAt(startPos, startLoc);
|
3472 | let closingElement = null;
|
3473 |
|
3474 | if (!openingElement.selfClosing) {
|
3475 | contents: for (;;) {
|
3476 | switch (this.state.type) {
|
3477 | case types.jsxTagStart:
|
3478 | startPos = this.state.start;
|
3479 | startLoc = this.state.startLoc;
|
3480 | this.next();
|
3481 |
|
3482 | if (this.eat(types.slash)) {
|
3483 | closingElement = this.jsxParseClosingElementAt(startPos, startLoc);
|
3484 | break contents;
|
3485 | }
|
3486 |
|
3487 | children.push(this.jsxParseElementAt(startPos, startLoc));
|
3488 | break;
|
3489 |
|
3490 | case types.jsxText:
|
3491 | children.push(this.parseExprAtom());
|
3492 | break;
|
3493 |
|
3494 | case types.braceL:
|
3495 | if (this.lookahead().type === types.ellipsis) {
|
3496 | children.push(this.jsxParseSpreadChild());
|
3497 | } else {
|
3498 | children.push(this.jsxParseExpressionContainer());
|
3499 | }
|
3500 |
|
3501 | break;
|
3502 |
|
3503 | default:
|
3504 | throw this.unexpected();
|
3505 | }
|
3506 | }
|
3507 |
|
3508 | if (isFragment(openingElement) && !isFragment(closingElement)) {
|
3509 | this.raise(closingElement.start, "Expected corresponding JSX closing tag for <>");
|
3510 | } else if (!isFragment(openingElement) && isFragment(closingElement)) {
|
3511 | this.raise(closingElement.start, "Expected corresponding JSX closing tag for <" + getQualifiedJSXName(openingElement.name) + ">");
|
3512 | } else if (!isFragment(openingElement) && !isFragment(closingElement)) {
|
3513 | if (getQualifiedJSXName(closingElement.name) !== getQualifiedJSXName(openingElement.name)) {
|
3514 | this.raise(closingElement.start, "Expected corresponding JSX closing tag for <" + getQualifiedJSXName(openingElement.name) + ">");
|
3515 | }
|
3516 | }
|
3517 | }
|
3518 |
|
3519 | if (isFragment(openingElement)) {
|
3520 | node.openingFragment = openingElement;
|
3521 | node.closingFragment = closingElement;
|
3522 | } else {
|
3523 | node.openingElement = openingElement;
|
3524 | node.closingElement = closingElement;
|
3525 | }
|
3526 |
|
3527 | node.children = children;
|
3528 |
|
3529 | if (this.match(types.relational) && this.state.value === "<") {
|
3530 | this.raise(this.state.start, "Adjacent JSX elements must be wrapped in an enclosing tag. " + "Did you want a JSX fragment <>...</>?");
|
3531 | }
|
3532 |
|
3533 | return isFragment(openingElement) ? this.finishNode(node, "JSXFragment") : this.finishNode(node, "JSXElement");
|
3534 | }
|
3535 |
|
3536 | jsxParseElement() {
|
3537 | const startPos = this.state.start;
|
3538 | const startLoc = this.state.startLoc;
|
3539 | this.next();
|
3540 | return this.jsxParseElementAt(startPos, startLoc);
|
3541 | }
|
3542 |
|
3543 | parseExprAtom(refShortHandDefaultPos) {
|
3544 | if (this.match(types.jsxText)) {
|
3545 | return this.parseLiteral(this.state.value, "JSXText");
|
3546 | } else if (this.match(types.jsxTagStart)) {
|
3547 | return this.jsxParseElement();
|
3548 | } else if (this.isRelational("<") && this.state.input.charCodeAt(this.state.pos) !== 33) {
|
3549 | this.finishToken(types.jsxTagStart);
|
3550 | return this.jsxParseElement();
|
3551 | } else {
|
3552 | return super.parseExprAtom(refShortHandDefaultPos);
|
3553 | }
|
3554 | }
|
3555 |
|
3556 | getTokenFromCode(code) {
|
3557 | if (this.state.inPropertyName) return super.getTokenFromCode(code);
|
3558 | const context = this.curContext();
|
3559 |
|
3560 | if (context === types$1.j_expr) {
|
3561 | return this.jsxReadToken();
|
3562 | }
|
3563 |
|
3564 | if (context === types$1.j_oTag || context === types$1.j_cTag) {
|
3565 | if (isIdentifierStart(code)) {
|
3566 | return this.jsxReadWord();
|
3567 | }
|
3568 |
|
3569 | if (code === 62) {
|
3570 | ++this.state.pos;
|
3571 | return this.finishToken(types.jsxTagEnd);
|
3572 | }
|
3573 |
|
3574 | if ((code === 34 || code === 39) && context === types$1.j_oTag) {
|
3575 | return this.jsxReadString(code);
|
3576 | }
|
3577 | }
|
3578 |
|
3579 | if (code === 60 && this.state.exprAllowed && this.state.input.charCodeAt(this.state.pos + 1) !== 33) {
|
3580 | ++this.state.pos;
|
3581 | return this.finishToken(types.jsxTagStart);
|
3582 | }
|
3583 |
|
3584 | return super.getTokenFromCode(code);
|
3585 | }
|
3586 |
|
3587 | updateContext(prevType) {
|
3588 | if (this.match(types.braceL)) {
|
3589 | const curContext = this.curContext();
|
3590 |
|
3591 | if (curContext === types$1.j_oTag) {
|
3592 | this.state.context.push(types$1.braceExpression);
|
3593 | } else if (curContext === types$1.j_expr) {
|
3594 | this.state.context.push(types$1.templateQuasi);
|
3595 | } else {
|
3596 | super.updateContext(prevType);
|
3597 | }
|
3598 |
|
3599 | this.state.exprAllowed = true;
|
3600 | } else if (this.match(types.slash) && prevType === types.jsxTagStart) {
|
3601 | this.state.context.length -= 2;
|
3602 | this.state.context.push(types$1.j_cTag);
|
3603 | this.state.exprAllowed = false;
|
3604 | } else {
|
3605 | return super.updateContext(prevType);
|
3606 | }
|
3607 | }
|
3608 |
|
3609 | });
|
3610 |
|
3611 | const defaultOptions = {
|
3612 | sourceType: "script",
|
3613 | sourceFilename: undefined,
|
3614 | startLine: 1,
|
3615 | allowAwaitOutsideFunction: false,
|
3616 | allowReturnOutsideFunction: false,
|
3617 | allowImportExportEverywhere: false,
|
3618 | allowSuperOutsideMethod: false,
|
3619 | plugins: [],
|
3620 | strictMode: null,
|
3621 | ranges: false,
|
3622 | tokens: false
|
3623 | };
|
3624 | function getOptions(opts) {
|
3625 | const options = {};
|
3626 |
|
3627 | for (const key in defaultOptions) {
|
3628 | options[key] = opts && opts[key] != null ? opts[key] : defaultOptions[key];
|
3629 | }
|
3630 |
|
3631 | return options;
|
3632 | }
|
3633 |
|
3634 | class Position {
|
3635 | constructor(line, col) {
|
3636 | this.line = line;
|
3637 | this.column = col;
|
3638 | }
|
3639 |
|
3640 | }
|
3641 | class SourceLocation {
|
3642 | constructor(start, end) {
|
3643 | this.start = start;
|
3644 | this.end = end;
|
3645 | }
|
3646 |
|
3647 | }
|
3648 | function getLineInfo(input, offset) {
|
3649 | let line = 1;
|
3650 | let lineStart = 0;
|
3651 | let match;
|
3652 | lineBreakG.lastIndex = 0;
|
3653 |
|
3654 | while ((match = lineBreakG.exec(input)) && match.index < offset) {
|
3655 | line++;
|
3656 | lineStart = lineBreakG.lastIndex;
|
3657 | }
|
3658 |
|
3659 | return new Position(line, offset - lineStart);
|
3660 | }
|
3661 |
|
3662 | class BaseParser {
|
3663 | constructor() {
|
3664 | this.sawUnambiguousESM = false;
|
3665 | }
|
3666 |
|
3667 | hasPlugin(name) {
|
3668 | return this.plugins.has(name);
|
3669 | }
|
3670 |
|
3671 | getPluginOption(plugin, name) {
|
3672 | if (this.hasPlugin(plugin)) return this.plugins.get(plugin)[name];
|
3673 | }
|
3674 |
|
3675 | }
|
3676 |
|
3677 | function last(stack) {
|
3678 | return stack[stack.length - 1];
|
3679 | }
|
3680 |
|
3681 | class CommentsParser extends BaseParser {
|
3682 | addComment(comment) {
|
3683 | if (this.filename) comment.loc.filename = this.filename;
|
3684 | this.state.trailingComments.push(comment);
|
3685 | this.state.leadingComments.push(comment);
|
3686 | }
|
3687 |
|
3688 | processComment(node) {
|
3689 | if (node.type === "Program" && node.body.length > 0) return;
|
3690 | const stack = this.state.commentStack;
|
3691 | let firstChild, lastChild, trailingComments, i, j;
|
3692 |
|
3693 | if (this.state.trailingComments.length > 0) {
|
3694 | if (this.state.trailingComments[0].start >= node.end) {
|
3695 | trailingComments = this.state.trailingComments;
|
3696 | this.state.trailingComments = [];
|
3697 | } else {
|
3698 | this.state.trailingComments.length = 0;
|
3699 | }
|
3700 | } else if (stack.length > 0) {
|
3701 | const lastInStack = last(stack);
|
3702 |
|
3703 | if (lastInStack.trailingComments && lastInStack.trailingComments[0].start >= node.end) {
|
3704 | trailingComments = lastInStack.trailingComments;
|
3705 | delete lastInStack.trailingComments;
|
3706 | }
|
3707 | }
|
3708 |
|
3709 | if (stack.length > 0 && last(stack).start >= node.start) {
|
3710 | firstChild = stack.pop();
|
3711 | }
|
3712 |
|
3713 | while (stack.length > 0 && last(stack).start >= node.start) {
|
3714 | lastChild = stack.pop();
|
3715 | }
|
3716 |
|
3717 | if (!lastChild && firstChild) lastChild = firstChild;
|
3718 |
|
3719 | if (firstChild && this.state.leadingComments.length > 0) {
|
3720 | const lastComment = last(this.state.leadingComments);
|
3721 |
|
3722 | if (firstChild.type === "ObjectProperty") {
|
3723 | if (lastComment.start >= node.start) {
|
3724 | if (this.state.commentPreviousNode) {
|
3725 | for (j = 0; j < this.state.leadingComments.length; j++) {
|
3726 | if (this.state.leadingComments[j].end < this.state.commentPreviousNode.end) {
|
3727 | this.state.leadingComments.splice(j, 1);
|
3728 | j--;
|
3729 | }
|
3730 | }
|
3731 |
|
3732 | if (this.state.leadingComments.length > 0) {
|
3733 | firstChild.trailingComments = this.state.leadingComments;
|
3734 | this.state.leadingComments = [];
|
3735 | }
|
3736 | }
|
3737 | }
|
3738 | } else if (node.type === "CallExpression" && node.arguments && node.arguments.length) {
|
3739 | const lastArg = last(node.arguments);
|
3740 |
|
3741 | if (lastArg && lastComment.start >= lastArg.start && lastComment.end <= node.end) {
|
3742 | if (this.state.commentPreviousNode) {
|
3743 | for (j = 0; j < this.state.leadingComments.length; j++) {
|
3744 | if (this.state.leadingComments[j].end < this.state.commentPreviousNode.end) {
|
3745 | this.state.leadingComments.splice(j, 1);
|
3746 | j--;
|
3747 | }
|
3748 | }
|
3749 |
|
3750 | if (this.state.leadingComments.length > 0) {
|
3751 | lastArg.trailingComments = this.state.leadingComments;
|
3752 | this.state.leadingComments = [];
|
3753 | }
|
3754 | }
|
3755 | }
|
3756 | }
|
3757 | }
|
3758 |
|
3759 | if (lastChild) {
|
3760 | if (lastChild.leadingComments) {
|
3761 | if (lastChild !== node && lastChild.leadingComments.length > 0 && last(lastChild.leadingComments).end <= node.start) {
|
3762 | node.leadingComments = lastChild.leadingComments;
|
3763 | delete lastChild.leadingComments;
|
3764 | } else {
|
3765 | for (i = lastChild.leadingComments.length - 2; i >= 0; --i) {
|
3766 | if (lastChild.leadingComments[i].end <= node.start) {
|
3767 | node.leadingComments = lastChild.leadingComments.splice(0, i + 1);
|
3768 | break;
|
3769 | }
|
3770 | }
|
3771 | }
|
3772 | }
|
3773 | } else if (this.state.leadingComments.length > 0) {
|
3774 | if (last(this.state.leadingComments).end <= node.start) {
|
3775 | if (this.state.commentPreviousNode) {
|
3776 | for (j = 0; j < this.state.leadingComments.length; j++) {
|
3777 | if (this.state.leadingComments[j].end < this.state.commentPreviousNode.end) {
|
3778 | this.state.leadingComments.splice(j, 1);
|
3779 | j--;
|
3780 | }
|
3781 | }
|
3782 | }
|
3783 |
|
3784 | if (this.state.leadingComments.length > 0) {
|
3785 | node.leadingComments = this.state.leadingComments;
|
3786 | this.state.leadingComments = [];
|
3787 | }
|
3788 | } else {
|
3789 | for (i = 0; i < this.state.leadingComments.length; i++) {
|
3790 | if (this.state.leadingComments[i].end > node.start) {
|
3791 | break;
|
3792 | }
|
3793 | }
|
3794 |
|
3795 | const leadingComments = this.state.leadingComments.slice(0, i);
|
3796 |
|
3797 | if (leadingComments.length) {
|
3798 | node.leadingComments = leadingComments;
|
3799 | }
|
3800 |
|
3801 | trailingComments = this.state.leadingComments.slice(i);
|
3802 |
|
3803 | if (trailingComments.length === 0) {
|
3804 | trailingComments = null;
|
3805 | }
|
3806 | }
|
3807 | }
|
3808 |
|
3809 | this.state.commentPreviousNode = node;
|
3810 |
|
3811 | if (trailingComments) {
|
3812 | if (trailingComments.length && trailingComments[0].start >= node.start && last(trailingComments).end <= node.end) {
|
3813 | node.innerComments = trailingComments;
|
3814 | } else {
|
3815 | node.trailingComments = trailingComments;
|
3816 | }
|
3817 | }
|
3818 |
|
3819 | stack.push(node);
|
3820 | }
|
3821 |
|
3822 | }
|
3823 |
|
3824 | class LocationParser extends CommentsParser {
|
3825 | raise(pos, message, {
|
3826 | missingPluginNames,
|
3827 | code
|
3828 | } = {}) {
|
3829 | const loc = getLineInfo(this.state.input, pos);
|
3830 | message += ` (${loc.line}:${loc.column})`;
|
3831 | const err = new SyntaxError(message);
|
3832 | err.pos = pos;
|
3833 | err.loc = loc;
|
3834 |
|
3835 | if (missingPluginNames) {
|
3836 | err.missingPlugin = missingPluginNames;
|
3837 | }
|
3838 |
|
3839 | if (code !== undefined) {
|
3840 | err.code = code;
|
3841 | }
|
3842 |
|
3843 | throw err;
|
3844 | }
|
3845 |
|
3846 | }
|
3847 |
|
3848 | class State {
|
3849 | constructor() {
|
3850 | this.potentialArrowAt = -1;
|
3851 | this.noArrowAt = [];
|
3852 | this.noArrowParamsConversionAt = [];
|
3853 | this.commaAfterSpreadAt = -1;
|
3854 | this.inFunction = false;
|
3855 | this.inParameters = false;
|
3856 | this.maybeInArrowParameters = false;
|
3857 | this.inGenerator = false;
|
3858 | this.inMethod = false;
|
3859 | this.inAsync = false;
|
3860 | this.inPipeline = false;
|
3861 | this.inType = false;
|
3862 | this.noAnonFunctionType = false;
|
3863 | this.inPropertyName = false;
|
3864 | this.inClassProperty = false;
|
3865 | this.hasFlowComment = false;
|
3866 | this.isIterator = false;
|
3867 | this.topicContext = {
|
3868 | maxNumOfResolvableTopics: 0,
|
3869 | maxTopicIndex: null
|
3870 | };
|
3871 | this.classLevel = 0;
|
3872 | this.labels = [];
|
3873 | this.decoratorStack = [[]];
|
3874 | this.yieldPos = 0;
|
3875 | this.awaitPos = 0;
|
3876 | this.tokens = [];
|
3877 | this.comments = [];
|
3878 | this.trailingComments = [];
|
3879 | this.leadingComments = [];
|
3880 | this.commentStack = [];
|
3881 | this.commentPreviousNode = null;
|
3882 | this.pos = 0;
|
3883 | this.lineStart = 0;
|
3884 | this.type = types.eof;
|
3885 | this.value = null;
|
3886 | this.start = 0;
|
3887 | this.end = 0;
|
3888 | this.lastTokEndLoc = null;
|
3889 | this.lastTokStartLoc = null;
|
3890 | this.lastTokStart = 0;
|
3891 | this.lastTokEnd = 0;
|
3892 | this.context = [types$1.braceStatement];
|
3893 | this.exprAllowed = true;
|
3894 | this.containsEsc = false;
|
3895 | this.containsOctal = false;
|
3896 | this.octalPosition = null;
|
3897 | this.exportedIdentifiers = [];
|
3898 | this.invalidTemplateEscapePosition = null;
|
3899 | }
|
3900 |
|
3901 | init(options, input) {
|
3902 | this.strict = options.strictMode === false ? false : options.sourceType === "module";
|
3903 | this.input = input;
|
3904 | this.length = input.length;
|
3905 | this.curLine = options.startLine;
|
3906 | this.startLoc = this.endLoc = this.curPosition();
|
3907 | }
|
3908 |
|
3909 | curPosition() {
|
3910 | return new Position(this.curLine, this.pos - this.lineStart);
|
3911 | }
|
3912 |
|
3913 | clone(skipArrays) {
|
3914 | const state = new State();
|
3915 | const keys = Object.keys(this);
|
3916 |
|
3917 | for (let i = 0, length = keys.length; i < length; i++) {
|
3918 | const key = keys[i];
|
3919 | let val = this[key];
|
3920 |
|
3921 | if ((!skipArrays || key === "context") && Array.isArray(val)) {
|
3922 | val = val.slice();
|
3923 | }
|
3924 |
|
3925 | state[key] = val;
|
3926 | }
|
3927 |
|
3928 | return state;
|
3929 | }
|
3930 |
|
3931 | }
|
3932 |
|
3933 | var _isDigit = function isDigit(code) {
|
3934 | return code >= 48 && code <= 57;
|
3935 | };
|
3936 |
|
3937 | const VALID_REGEX_FLAGS = new Set(["g", "m", "s", "i", "y", "u"]);
|
3938 | const forbiddenNumericSeparatorSiblings = {
|
3939 | decBinOct: [46, 66, 69, 79, 95, 98, 101, 111],
|
3940 | hex: [46, 88, 95, 120]
|
3941 | };
|
3942 | const allowedNumericSeparatorSiblings = {};
|
3943 | allowedNumericSeparatorSiblings.bin = [48, 49];
|
3944 | allowedNumericSeparatorSiblings.oct = [...allowedNumericSeparatorSiblings.bin, 50, 51, 52, 53, 54, 55];
|
3945 | allowedNumericSeparatorSiblings.dec = [...allowedNumericSeparatorSiblings.oct, 56, 57];
|
3946 | allowedNumericSeparatorSiblings.hex = [...allowedNumericSeparatorSiblings.dec, 65, 66, 67, 68, 69, 70, 97, 98, 99, 100, 101, 102];
|
3947 | class Token {
|
3948 | constructor(state) {
|
3949 | this.type = state.type;
|
3950 | this.value = state.value;
|
3951 | this.start = state.start;
|
3952 | this.end = state.end;
|
3953 | this.loc = new SourceLocation(state.startLoc, state.endLoc);
|
3954 | }
|
3955 |
|
3956 | }
|
3957 | class Tokenizer extends LocationParser {
|
3958 | constructor(options, input) {
|
3959 | super();
|
3960 | this.state = new State();
|
3961 | this.state.init(options, input);
|
3962 | this.isLookahead = false;
|
3963 | }
|
3964 |
|
3965 | next() {
|
3966 | if (this.options.tokens && !this.isLookahead) {
|
3967 | this.state.tokens.push(new Token(this.state));
|
3968 | }
|
3969 |
|
3970 | this.state.lastTokEnd = this.state.end;
|
3971 | this.state.lastTokStart = this.state.start;
|
3972 | this.state.lastTokEndLoc = this.state.endLoc;
|
3973 | this.state.lastTokStartLoc = this.state.startLoc;
|
3974 | this.nextToken();
|
3975 | }
|
3976 |
|
3977 | eat(type) {
|
3978 | if (this.match(type)) {
|
3979 | this.next();
|
3980 | return true;
|
3981 | } else {
|
3982 | return false;
|
3983 | }
|
3984 | }
|
3985 |
|
3986 | match(type) {
|
3987 | return this.state.type === type;
|
3988 | }
|
3989 |
|
3990 | lookahead() {
|
3991 | const old = this.state;
|
3992 | this.state = old.clone(true);
|
3993 | this.isLookahead = true;
|
3994 | this.next();
|
3995 | this.isLookahead = false;
|
3996 | const curr = this.state;
|
3997 | this.state = old;
|
3998 | return curr;
|
3999 | }
|
4000 |
|
4001 | setStrict(strict) {
|
4002 | this.state.strict = strict;
|
4003 | if (!this.match(types.num) && !this.match(types.string)) return;
|
4004 | this.state.pos = this.state.start;
|
4005 |
|
4006 | while (this.state.pos < this.state.lineStart) {
|
4007 | this.state.lineStart = this.state.input.lastIndexOf("\n", this.state.lineStart - 2) + 1;
|
4008 | --this.state.curLine;
|
4009 | }
|
4010 |
|
4011 | this.nextToken();
|
4012 | }
|
4013 |
|
4014 | curContext() {
|
4015 | return this.state.context[this.state.context.length - 1];
|
4016 | }
|
4017 |
|
4018 | nextToken() {
|
4019 | const curContext = this.curContext();
|
4020 | if (!curContext || !curContext.preserveSpace) this.skipSpace();
|
4021 | this.state.containsOctal = false;
|
4022 | this.state.octalPosition = null;
|
4023 | this.state.start = this.state.pos;
|
4024 | this.state.startLoc = this.state.curPosition();
|
4025 |
|
4026 | if (this.state.pos >= this.state.length) {
|
4027 | this.finishToken(types.eof);
|
4028 | return;
|
4029 | }
|
4030 |
|
4031 | if (curContext.override) {
|
4032 | curContext.override(this);
|
4033 | } else {
|
4034 | this.getTokenFromCode(this.state.input.codePointAt(this.state.pos));
|
4035 | }
|
4036 | }
|
4037 |
|
4038 | pushComment(block, text, start, end, startLoc, endLoc) {
|
4039 | const comment = {
|
4040 | type: block ? "CommentBlock" : "CommentLine",
|
4041 | value: text,
|
4042 | start: start,
|
4043 | end: end,
|
4044 | loc: new SourceLocation(startLoc, endLoc)
|
4045 | };
|
4046 |
|
4047 | if (!this.isLookahead) {
|
4048 | if (this.options.tokens) this.state.tokens.push(comment);
|
4049 | this.state.comments.push(comment);
|
4050 | this.addComment(comment);
|
4051 | }
|
4052 | }
|
4053 |
|
4054 | skipBlockComment() {
|
4055 | const startLoc = this.state.curPosition();
|
4056 | const start = this.state.pos;
|
4057 | const end = this.state.input.indexOf("*/", this.state.pos += 2);
|
4058 | if (end === -1) this.raise(this.state.pos - 2, "Unterminated comment");
|
4059 | this.state.pos = end + 2;
|
4060 | lineBreakG.lastIndex = start;
|
4061 | let match;
|
4062 |
|
4063 | while ((match = lineBreakG.exec(this.state.input)) && match.index < this.state.pos) {
|
4064 | ++this.state.curLine;
|
4065 | this.state.lineStart = match.index + match[0].length;
|
4066 | }
|
4067 |
|
4068 | this.pushComment(true, this.state.input.slice(start + 2, end), start, this.state.pos, startLoc, this.state.curPosition());
|
4069 | }
|
4070 |
|
4071 | skipLineComment(startSkip) {
|
4072 | const start = this.state.pos;
|
4073 | const startLoc = this.state.curPosition();
|
4074 | let ch = this.state.input.charCodeAt(this.state.pos += startSkip);
|
4075 |
|
4076 | if (this.state.pos < this.state.length) {
|
4077 | while (ch !== 10 && ch !== 13 && ch !== 8232 && ch !== 8233 && ++this.state.pos < this.state.length) {
|
4078 | ch = this.state.input.charCodeAt(this.state.pos);
|
4079 | }
|
4080 | }
|
4081 |
|
4082 | this.pushComment(false, this.state.input.slice(start + startSkip, this.state.pos), start, this.state.pos, startLoc, this.state.curPosition());
|
4083 | }
|
4084 |
|
4085 | skipSpace() {
|
4086 | loop: while (this.state.pos < this.state.length) {
|
4087 | const ch = this.state.input.charCodeAt(this.state.pos);
|
4088 |
|
4089 | switch (ch) {
|
4090 | case 32:
|
4091 | case 160:
|
4092 | case 9:
|
4093 | ++this.state.pos;
|
4094 | break;
|
4095 |
|
4096 | case 13:
|
4097 | if (this.state.input.charCodeAt(this.state.pos + 1) === 10) {
|
4098 | ++this.state.pos;
|
4099 | }
|
4100 |
|
4101 | case 10:
|
4102 | case 8232:
|
4103 | case 8233:
|
4104 | ++this.state.pos;
|
4105 | ++this.state.curLine;
|
4106 | this.state.lineStart = this.state.pos;
|
4107 | break;
|
4108 |
|
4109 | case 47:
|
4110 | switch (this.state.input.charCodeAt(this.state.pos + 1)) {
|
4111 | case 42:
|
4112 | this.skipBlockComment();
|
4113 | break;
|
4114 |
|
4115 | case 47:
|
4116 | this.skipLineComment(2);
|
4117 | break;
|
4118 |
|
4119 | default:
|
4120 | break loop;
|
4121 | }
|
4122 |
|
4123 | break;
|
4124 |
|
4125 | default:
|
4126 | if (isWhitespace(ch)) {
|
4127 | ++this.state.pos;
|
4128 | } else {
|
4129 | break loop;
|
4130 | }
|
4131 |
|
4132 | }
|
4133 | }
|
4134 | }
|
4135 |
|
4136 | finishToken(type, val) {
|
4137 | this.state.end = this.state.pos;
|
4138 | this.state.endLoc = this.state.curPosition();
|
4139 | const prevType = this.state.type;
|
4140 | this.state.type = type;
|
4141 | this.state.value = val;
|
4142 | this.updateContext(prevType);
|
4143 | }
|
4144 |
|
4145 | readToken_numberSign() {
|
4146 | if (this.state.pos === 0 && this.readToken_interpreter()) {
|
4147 | return;
|
4148 | }
|
4149 |
|
4150 | const nextPos = this.state.pos + 1;
|
4151 | const next = this.state.input.charCodeAt(nextPos);
|
4152 |
|
4153 | if (next >= 48 && next <= 57) {
|
4154 | this.raise(this.state.pos, "Unexpected digit after hash token");
|
4155 | }
|
4156 |
|
4157 | if ((this.hasPlugin("classPrivateProperties") || this.hasPlugin("classPrivateMethods")) && this.state.classLevel > 0) {
|
4158 | ++this.state.pos;
|
4159 | this.finishToken(types.hash);
|
4160 | return;
|
4161 | } else if (this.getPluginOption("pipelineOperator", "proposal") === "smart") {
|
4162 | this.finishOp(types.hash, 1);
|
4163 | } else {
|
4164 | this.raise(this.state.pos, "Unexpected character '#'");
|
4165 | }
|
4166 | }
|
4167 |
|
4168 | readToken_dot() {
|
4169 | const next = this.state.input.charCodeAt(this.state.pos + 1);
|
4170 |
|
4171 | if (next >= 48 && next <= 57) {
|
4172 | this.readNumber(true);
|
4173 | return;
|
4174 | }
|
4175 |
|
4176 | const next2 = this.state.input.charCodeAt(this.state.pos + 2);
|
4177 |
|
4178 | if (next === 46 && next2 === 46) {
|
4179 | this.state.pos += 3;
|
4180 | this.finishToken(types.ellipsis);
|
4181 | } else {
|
4182 | ++this.state.pos;
|
4183 | this.finishToken(types.dot);
|
4184 | }
|
4185 | }
|
4186 |
|
4187 | readToken_slash() {
|
4188 | if (this.state.exprAllowed && !this.state.inType) {
|
4189 | ++this.state.pos;
|
4190 | this.readRegexp();
|
4191 | return;
|
4192 | }
|
4193 |
|
4194 | const next = this.state.input.charCodeAt(this.state.pos + 1);
|
4195 |
|
4196 | if (next === 61) {
|
4197 | this.finishOp(types.assign, 2);
|
4198 | } else {
|
4199 | this.finishOp(types.slash, 1);
|
4200 | }
|
4201 | }
|
4202 |
|
4203 | readToken_interpreter() {
|
4204 | if (this.state.pos !== 0 || this.state.length < 2) return false;
|
4205 | const start = this.state.pos;
|
4206 | this.state.pos += 1;
|
4207 | let ch = this.state.input.charCodeAt(this.state.pos);
|
4208 | if (ch !== 33) return false;
|
4209 |
|
4210 | while (ch !== 10 && ch !== 13 && ch !== 8232 && ch !== 8233 && ++this.state.pos < this.state.length) {
|
4211 | ch = this.state.input.charCodeAt(this.state.pos);
|
4212 | }
|
4213 |
|
4214 | const value = this.state.input.slice(start + 2, this.state.pos);
|
4215 | this.finishToken(types.interpreterDirective, value);
|
4216 | return true;
|
4217 | }
|
4218 |
|
4219 | readToken_mult_modulo(code) {
|
4220 | let type = code === 42 ? types.star : types.modulo;
|
4221 | let width = 1;
|
4222 | let next = this.state.input.charCodeAt(this.state.pos + 1);
|
4223 | const exprAllowed = this.state.exprAllowed;
|
4224 |
|
4225 | if (code === 42 && next === 42) {
|
4226 | width++;
|
4227 | next = this.state.input.charCodeAt(this.state.pos + 2);
|
4228 | type = types.exponent;
|
4229 | }
|
4230 |
|
4231 | if (next === 61 && !exprAllowed) {
|
4232 | width++;
|
4233 | type = types.assign;
|
4234 | }
|
4235 |
|
4236 | this.finishOp(type, width);
|
4237 | }
|
4238 |
|
4239 | readToken_pipe_amp(code) {
|
4240 | const next = this.state.input.charCodeAt(this.state.pos + 1);
|
4241 |
|
4242 | if (next === code) {
|
4243 | if (this.state.input.charCodeAt(this.state.pos + 2) === 61) {
|
4244 | this.finishOp(types.assign, 3);
|
4245 | } else {
|
4246 | this.finishOp(code === 124 ? types.logicalOR : types.logicalAND, 2);
|
4247 | }
|
4248 |
|
4249 | return;
|
4250 | }
|
4251 |
|
4252 | if (code === 124) {
|
4253 | if (next === 62) {
|
4254 | this.finishOp(types.pipeline, 2);
|
4255 | return;
|
4256 | }
|
4257 | }
|
4258 |
|
4259 | if (next === 61) {
|
4260 | this.finishOp(types.assign, 2);
|
4261 | return;
|
4262 | }
|
4263 |
|
4264 | this.finishOp(code === 124 ? types.bitwiseOR : types.bitwiseAND, 1);
|
4265 | }
|
4266 |
|
4267 | readToken_caret() {
|
4268 | const next = this.state.input.charCodeAt(this.state.pos + 1);
|
4269 |
|
4270 | if (next === 61) {
|
4271 | this.finishOp(types.assign, 2);
|
4272 | } else {
|
4273 | this.finishOp(types.bitwiseXOR, 1);
|
4274 | }
|
4275 | }
|
4276 |
|
4277 | readToken_plus_min(code) {
|
4278 | const next = this.state.input.charCodeAt(this.state.pos + 1);
|
4279 |
|
4280 | if (next === code) {
|
4281 | if (next === 45 && !this.inModule && this.state.input.charCodeAt(this.state.pos + 2) === 62 && lineBreak.test(this.state.input.slice(this.state.lastTokEnd, this.state.pos))) {
|
4282 | this.skipLineComment(3);
|
4283 | this.skipSpace();
|
4284 | this.nextToken();
|
4285 | return;
|
4286 | }
|
4287 |
|
4288 | this.finishOp(types.incDec, 2);
|
4289 | return;
|
4290 | }
|
4291 |
|
4292 | if (next === 61) {
|
4293 | this.finishOp(types.assign, 2);
|
4294 | } else {
|
4295 | this.finishOp(types.plusMin, 1);
|
4296 | }
|
4297 | }
|
4298 |
|
4299 | readToken_lt_gt(code) {
|
4300 | const next = this.state.input.charCodeAt(this.state.pos + 1);
|
4301 | let size = 1;
|
4302 |
|
4303 | if (next === code) {
|
4304 | size = code === 62 && this.state.input.charCodeAt(this.state.pos + 2) === 62 ? 3 : 2;
|
4305 |
|
4306 | if (this.state.input.charCodeAt(this.state.pos + size) === 61) {
|
4307 | this.finishOp(types.assign, size + 1);
|
4308 | return;
|
4309 | }
|
4310 |
|
4311 | this.finishOp(types.bitShift, size);
|
4312 | return;
|
4313 | }
|
4314 |
|
4315 | if (next === 33 && code === 60 && !this.inModule && this.state.input.charCodeAt(this.state.pos + 2) === 45 && this.state.input.charCodeAt(this.state.pos + 3) === 45) {
|
4316 | this.skipLineComment(4);
|
4317 | this.skipSpace();
|
4318 | this.nextToken();
|
4319 | return;
|
4320 | }
|
4321 |
|
4322 | if (next === 61) {
|
4323 | size = 2;
|
4324 | }
|
4325 |
|
4326 | this.finishOp(types.relational, size);
|
4327 | }
|
4328 |
|
4329 | readToken_eq_excl(code) {
|
4330 | const next = this.state.input.charCodeAt(this.state.pos + 1);
|
4331 |
|
4332 | if (next === 61) {
|
4333 | this.finishOp(types.equality, this.state.input.charCodeAt(this.state.pos + 2) === 61 ? 3 : 2);
|
4334 | return;
|
4335 | }
|
4336 |
|
4337 | if (code === 61 && next === 62) {
|
4338 | this.state.pos += 2;
|
4339 | this.finishToken(types.arrow);
|
4340 | return;
|
4341 | }
|
4342 |
|
4343 | this.finishOp(code === 61 ? types.eq : types.bang, 1);
|
4344 | }
|
4345 |
|
4346 | readToken_question() {
|
4347 | const next = this.state.input.charCodeAt(this.state.pos + 1);
|
4348 | const next2 = this.state.input.charCodeAt(this.state.pos + 2);
|
4349 |
|
4350 | if (next === 63 && !this.state.inType) {
|
4351 | if (next2 === 61) {
|
4352 | this.finishOp(types.assign, 3);
|
4353 | } else {
|
4354 | this.finishOp(types.nullishCoalescing, 2);
|
4355 | }
|
4356 | } else if (next === 46 && !(next2 >= 48 && next2 <= 57)) {
|
4357 | this.state.pos += 2;
|
4358 | this.finishToken(types.questionDot);
|
4359 | } else {
|
4360 | ++this.state.pos;
|
4361 | this.finishToken(types.question);
|
4362 | }
|
4363 | }
|
4364 |
|
4365 | getTokenFromCode(code) {
|
4366 | switch (code) {
|
4367 | case 46:
|
4368 | this.readToken_dot();
|
4369 | return;
|
4370 |
|
4371 | case 40:
|
4372 | ++this.state.pos;
|
4373 | this.finishToken(types.parenL);
|
4374 | return;
|
4375 |
|
4376 | case 41:
|
4377 | ++this.state.pos;
|
4378 | this.finishToken(types.parenR);
|
4379 | return;
|
4380 |
|
4381 | case 59:
|
4382 | ++this.state.pos;
|
4383 | this.finishToken(types.semi);
|
4384 | return;
|
4385 |
|
4386 | case 44:
|
4387 | ++this.state.pos;
|
4388 | this.finishToken(types.comma);
|
4389 | return;
|
4390 |
|
4391 | case 91:
|
4392 | ++this.state.pos;
|
4393 | this.finishToken(types.bracketL);
|
4394 | return;
|
4395 |
|
4396 | case 93:
|
4397 | ++this.state.pos;
|
4398 | this.finishToken(types.bracketR);
|
4399 | return;
|
4400 |
|
4401 | case 123:
|
4402 | ++this.state.pos;
|
4403 | this.finishToken(types.braceL);
|
4404 | return;
|
4405 |
|
4406 | case 125:
|
4407 | ++this.state.pos;
|
4408 | this.finishToken(types.braceR);
|
4409 | return;
|
4410 |
|
4411 | case 58:
|
4412 | if (this.hasPlugin("functionBind") && this.state.input.charCodeAt(this.state.pos + 1) === 58) {
|
4413 | this.finishOp(types.doubleColon, 2);
|
4414 | } else {
|
4415 | ++this.state.pos;
|
4416 | this.finishToken(types.colon);
|
4417 | }
|
4418 |
|
4419 | return;
|
4420 |
|
4421 | case 63:
|
4422 | this.readToken_question();
|
4423 | return;
|
4424 |
|
4425 | case 96:
|
4426 | ++this.state.pos;
|
4427 | this.finishToken(types.backQuote);
|
4428 | return;
|
4429 |
|
4430 | case 48:
|
4431 | {
|
4432 | const next = this.state.input.charCodeAt(this.state.pos + 1);
|
4433 |
|
4434 | if (next === 120 || next === 88) {
|
4435 | this.readRadixNumber(16);
|
4436 | return;
|
4437 | }
|
4438 |
|
4439 | if (next === 111 || next === 79) {
|
4440 | this.readRadixNumber(8);
|
4441 | return;
|
4442 | }
|
4443 |
|
4444 | if (next === 98 || next === 66) {
|
4445 | this.readRadixNumber(2);
|
4446 | return;
|
4447 | }
|
4448 | }
|
4449 |
|
4450 | case 49:
|
4451 | case 50:
|
4452 | case 51:
|
4453 | case 52:
|
4454 | case 53:
|
4455 | case 54:
|
4456 | case 55:
|
4457 | case 56:
|
4458 | case 57:
|
4459 | this.readNumber(false);
|
4460 | return;
|
4461 |
|
4462 | case 34:
|
4463 | case 39:
|
4464 | this.readString(code);
|
4465 | return;
|
4466 |
|
4467 | case 47:
|
4468 | this.readToken_slash();
|
4469 | return;
|
4470 |
|
4471 | case 37:
|
4472 | case 42:
|
4473 | this.readToken_mult_modulo(code);
|
4474 | return;
|
4475 |
|
4476 | case 124:
|
4477 | case 38:
|
4478 | this.readToken_pipe_amp(code);
|
4479 | return;
|
4480 |
|
4481 | case 94:
|
4482 | this.readToken_caret();
|
4483 | return;
|
4484 |
|
4485 | case 43:
|
4486 | case 45:
|
4487 | this.readToken_plus_min(code);
|
4488 | return;
|
4489 |
|
4490 | case 60:
|
4491 | case 62:
|
4492 | this.readToken_lt_gt(code);
|
4493 | return;
|
4494 |
|
4495 | case 61:
|
4496 | case 33:
|
4497 | this.readToken_eq_excl(code);
|
4498 | return;
|
4499 |
|
4500 | case 126:
|
4501 | this.finishOp(types.tilde, 1);
|
4502 | return;
|
4503 |
|
4504 | case 64:
|
4505 | ++this.state.pos;
|
4506 | this.finishToken(types.at);
|
4507 | return;
|
4508 |
|
4509 | case 35:
|
4510 | this.readToken_numberSign();
|
4511 | return;
|
4512 |
|
4513 | case 92:
|
4514 | this.readWord();
|
4515 | return;
|
4516 |
|
4517 | default:
|
4518 | if (isIdentifierStart(code)) {
|
4519 | this.readWord();
|
4520 | return;
|
4521 | }
|
4522 |
|
4523 | }
|
4524 |
|
4525 | this.raise(this.state.pos, `Unexpected character '${String.fromCodePoint(code)}'`);
|
4526 | }
|
4527 |
|
4528 | finishOp(type, size) {
|
4529 | const str = this.state.input.slice(this.state.pos, this.state.pos + size);
|
4530 | this.state.pos += size;
|
4531 | this.finishToken(type, str);
|
4532 | }
|
4533 |
|
4534 | readRegexp() {
|
4535 | const start = this.state.pos;
|
4536 | let escaped, inClass;
|
4537 |
|
4538 | for (;;) {
|
4539 | if (this.state.pos >= this.state.length) {
|
4540 | this.raise(start, "Unterminated regular expression");
|
4541 | }
|
4542 |
|
4543 | const ch = this.state.input.charAt(this.state.pos);
|
4544 |
|
4545 | if (lineBreak.test(ch)) {
|
4546 | this.raise(start, "Unterminated regular expression");
|
4547 | }
|
4548 |
|
4549 | if (escaped) {
|
4550 | escaped = false;
|
4551 | } else {
|
4552 | if (ch === "[") {
|
4553 | inClass = true;
|
4554 | } else if (ch === "]" && inClass) {
|
4555 | inClass = false;
|
4556 | } else if (ch === "/" && !inClass) {
|
4557 | break;
|
4558 | }
|
4559 |
|
4560 | escaped = ch === "\\";
|
4561 | }
|
4562 |
|
4563 | ++this.state.pos;
|
4564 | }
|
4565 |
|
4566 | const content = this.state.input.slice(start, this.state.pos);
|
4567 | ++this.state.pos;
|
4568 | let mods = "";
|
4569 |
|
4570 | while (this.state.pos < this.state.length) {
|
4571 | const char = this.state.input[this.state.pos];
|
4572 | const charCode = this.state.input.codePointAt(this.state.pos);
|
4573 |
|
4574 | if (VALID_REGEX_FLAGS.has(char)) {
|
4575 | if (mods.indexOf(char) > -1) {
|
4576 | this.raise(this.state.pos + 1, "Duplicate regular expression flag");
|
4577 | }
|
4578 |
|
4579 | ++this.state.pos;
|
4580 | mods += char;
|
4581 | } else if (isIdentifierChar(charCode) || charCode === 92) {
|
4582 | this.raise(this.state.pos + 1, "Invalid regular expression flag");
|
4583 | } else {
|
4584 | break;
|
4585 | }
|
4586 | }
|
4587 |
|
4588 | this.finishToken(types.regexp, {
|
4589 | pattern: content,
|
4590 | flags: mods
|
4591 | });
|
4592 | }
|
4593 |
|
4594 | readInt(radix, len) {
|
4595 | const start = this.state.pos;
|
4596 | const forbiddenSiblings = radix === 16 ? forbiddenNumericSeparatorSiblings.hex : forbiddenNumericSeparatorSiblings.decBinOct;
|
4597 | const allowedSiblings = radix === 16 ? allowedNumericSeparatorSiblings.hex : radix === 10 ? allowedNumericSeparatorSiblings.dec : radix === 8 ? allowedNumericSeparatorSiblings.oct : allowedNumericSeparatorSiblings.bin;
|
4598 | let total = 0;
|
4599 |
|
4600 | for (let i = 0, e = len == null ? Infinity : len; i < e; ++i) {
|
4601 | const code = this.state.input.charCodeAt(this.state.pos);
|
4602 | let val;
|
4603 |
|
4604 | if (this.hasPlugin("numericSeparator")) {
|
4605 | const prev = this.state.input.charCodeAt(this.state.pos - 1);
|
4606 | const next = this.state.input.charCodeAt(this.state.pos + 1);
|
4607 |
|
4608 | if (code === 95) {
|
4609 | if (allowedSiblings.indexOf(next) === -1) {
|
4610 | this.raise(this.state.pos, "Invalid or unexpected token");
|
4611 | }
|
4612 |
|
4613 | if (forbiddenSiblings.indexOf(prev) > -1 || forbiddenSiblings.indexOf(next) > -1 || Number.isNaN(next)) {
|
4614 | this.raise(this.state.pos, "Invalid or unexpected token");
|
4615 | }
|
4616 |
|
4617 | ++this.state.pos;
|
4618 | continue;
|
4619 | }
|
4620 | }
|
4621 |
|
4622 | if (code >= 97) {
|
4623 | val = code - 97 + 10;
|
4624 | } else if (code >= 65) {
|
4625 | val = code - 65 + 10;
|
4626 | } else if (_isDigit(code)) {
|
4627 | val = code - 48;
|
4628 | } else {
|
4629 | val = Infinity;
|
4630 | }
|
4631 |
|
4632 | if (val >= radix) break;
|
4633 | ++this.state.pos;
|
4634 | total = total * radix + val;
|
4635 | }
|
4636 |
|
4637 | if (this.state.pos === start || len != null && this.state.pos - start !== len) {
|
4638 | return null;
|
4639 | }
|
4640 |
|
4641 | return total;
|
4642 | }
|
4643 |
|
4644 | readRadixNumber(radix) {
|
4645 | const start = this.state.pos;
|
4646 | let isBigInt = false;
|
4647 | this.state.pos += 2;
|
4648 | const val = this.readInt(radix);
|
4649 |
|
4650 | if (val == null) {
|
4651 | this.raise(this.state.start + 2, "Expected number in radix " + radix);
|
4652 | }
|
4653 |
|
4654 | if (this.hasPlugin("bigInt")) {
|
4655 | if (this.state.input.charCodeAt(this.state.pos) === 110) {
|
4656 | ++this.state.pos;
|
4657 | isBigInt = true;
|
4658 | }
|
4659 | }
|
4660 |
|
4661 | if (isIdentifierStart(this.state.input.codePointAt(this.state.pos))) {
|
4662 | this.raise(this.state.pos, "Identifier directly after number");
|
4663 | }
|
4664 |
|
4665 | if (isBigInt) {
|
4666 | const str = this.state.input.slice(start, this.state.pos).replace(/[_n]/g, "");
|
4667 | this.finishToken(types.bigint, str);
|
4668 | return;
|
4669 | }
|
4670 |
|
4671 | this.finishToken(types.num, val);
|
4672 | }
|
4673 |
|
4674 | readNumber(startsWithDot) {
|
4675 | const start = this.state.pos;
|
4676 | let isFloat = false;
|
4677 | let isBigInt = false;
|
4678 |
|
4679 | if (!startsWithDot && this.readInt(10) === null) {
|
4680 | this.raise(start, "Invalid number");
|
4681 | }
|
4682 |
|
4683 | let octal = this.state.pos - start >= 2 && this.state.input.charCodeAt(start) === 48;
|
4684 |
|
4685 | if (octal) {
|
4686 | if (this.state.strict) {
|
4687 | this.raise(start, "Legacy octal literals are not allowed in strict mode");
|
4688 | }
|
4689 |
|
4690 | if (/[89]/.test(this.state.input.slice(start, this.state.pos))) {
|
4691 | octal = false;
|
4692 | }
|
4693 | }
|
4694 |
|
4695 | let next = this.state.input.charCodeAt(this.state.pos);
|
4696 |
|
4697 | if (next === 46 && !octal) {
|
4698 | ++this.state.pos;
|
4699 | this.readInt(10);
|
4700 | isFloat = true;
|
4701 | next = this.state.input.charCodeAt(this.state.pos);
|
4702 | }
|
4703 |
|
4704 | if ((next === 69 || next === 101) && !octal) {
|
4705 | next = this.state.input.charCodeAt(++this.state.pos);
|
4706 |
|
4707 | if (next === 43 || next === 45) {
|
4708 | ++this.state.pos;
|
4709 | }
|
4710 |
|
4711 | if (this.readInt(10) === null) this.raise(start, "Invalid number");
|
4712 | isFloat = true;
|
4713 | next = this.state.input.charCodeAt(this.state.pos);
|
4714 | }
|
4715 |
|
4716 | if (this.hasPlugin("bigInt")) {
|
4717 | if (next === 110) {
|
4718 | if (isFloat || octal) this.raise(start, "Invalid BigIntLiteral");
|
4719 | ++this.state.pos;
|
4720 | isBigInt = true;
|
4721 | }
|
4722 | }
|
4723 |
|
4724 | if (isIdentifierStart(this.state.input.codePointAt(this.state.pos))) {
|
4725 | this.raise(this.state.pos, "Identifier directly after number");
|
4726 | }
|
4727 |
|
4728 | const str = this.state.input.slice(start, this.state.pos).replace(/[_n]/g, "");
|
4729 |
|
4730 | if (isBigInt) {
|
4731 | this.finishToken(types.bigint, str);
|
4732 | return;
|
4733 | }
|
4734 |
|
4735 | const val = octal ? parseInt(str, 8) : parseFloat(str);
|
4736 | this.finishToken(types.num, val);
|
4737 | }
|
4738 |
|
4739 | readCodePoint(throwOnInvalid) {
|
4740 | const ch = this.state.input.charCodeAt(this.state.pos);
|
4741 | let code;
|
4742 |
|
4743 | if (ch === 123) {
|
4744 | const codePos = ++this.state.pos;
|
4745 | code = this.readHexChar(this.state.input.indexOf("}", this.state.pos) - this.state.pos, throwOnInvalid);
|
4746 | ++this.state.pos;
|
4747 |
|
4748 | if (code === null) {
|
4749 | --this.state.invalidTemplateEscapePosition;
|
4750 | } else if (code > 0x10ffff) {
|
4751 | if (throwOnInvalid) {
|
4752 | this.raise(codePos, "Code point out of bounds");
|
4753 | } else {
|
4754 | this.state.invalidTemplateEscapePosition = codePos - 2;
|
4755 | return null;
|
4756 | }
|
4757 | }
|
4758 | } else {
|
4759 | code = this.readHexChar(4, throwOnInvalid);
|
4760 | }
|
4761 |
|
4762 | return code;
|
4763 | }
|
4764 |
|
4765 | readString(quote) {
|
4766 | let out = "",
|
4767 | chunkStart = ++this.state.pos;
|
4768 |
|
4769 | for (;;) {
|
4770 | if (this.state.pos >= this.state.length) {
|
4771 | this.raise(this.state.start, "Unterminated string constant");
|
4772 | }
|
4773 |
|
4774 | const ch = this.state.input.charCodeAt(this.state.pos);
|
4775 | if (ch === quote) break;
|
4776 |
|
4777 | if (ch === 92) {
|
4778 | out += this.state.input.slice(chunkStart, this.state.pos);
|
4779 | out += this.readEscapedChar(false);
|
4780 | chunkStart = this.state.pos;
|
4781 | } else if (ch === 8232 || ch === 8233) {
|
4782 | ++this.state.pos;
|
4783 | ++this.state.curLine;
|
4784 | } else if (isNewLine(ch)) {
|
4785 | this.raise(this.state.start, "Unterminated string constant");
|
4786 | } else {
|
4787 | ++this.state.pos;
|
4788 | }
|
4789 | }
|
4790 |
|
4791 | out += this.state.input.slice(chunkStart, this.state.pos++);
|
4792 | this.finishToken(types.string, out);
|
4793 | }
|
4794 |
|
4795 | readTmplToken() {
|
4796 | let out = "",
|
4797 | chunkStart = this.state.pos,
|
4798 | containsInvalid = false;
|
4799 |
|
4800 | for (;;) {
|
4801 | if (this.state.pos >= this.state.length) {
|
4802 | this.raise(this.state.start, "Unterminated template");
|
4803 | }
|
4804 |
|
4805 | const ch = this.state.input.charCodeAt(this.state.pos);
|
4806 |
|
4807 | if (ch === 96 || ch === 36 && this.state.input.charCodeAt(this.state.pos + 1) === 123) {
|
4808 | if (this.state.pos === this.state.start && this.match(types.template)) {
|
4809 | if (ch === 36) {
|
4810 | this.state.pos += 2;
|
4811 | this.finishToken(types.dollarBraceL);
|
4812 | return;
|
4813 | } else {
|
4814 | ++this.state.pos;
|
4815 | this.finishToken(types.backQuote);
|
4816 | return;
|
4817 | }
|
4818 | }
|
4819 |
|
4820 | out += this.state.input.slice(chunkStart, this.state.pos);
|
4821 | this.finishToken(types.template, containsInvalid ? null : out);
|
4822 | return;
|
4823 | }
|
4824 |
|
4825 | if (ch === 92) {
|
4826 | out += this.state.input.slice(chunkStart, this.state.pos);
|
4827 | const escaped = this.readEscapedChar(true);
|
4828 |
|
4829 | if (escaped === null) {
|
4830 | containsInvalid = true;
|
4831 | } else {
|
4832 | out += escaped;
|
4833 | }
|
4834 |
|
4835 | chunkStart = this.state.pos;
|
4836 | } else if (isNewLine(ch)) {
|
4837 | out += this.state.input.slice(chunkStart, this.state.pos);
|
4838 | ++this.state.pos;
|
4839 |
|
4840 | switch (ch) {
|
4841 | case 13:
|
4842 | if (this.state.input.charCodeAt(this.state.pos) === 10) {
|
4843 | ++this.state.pos;
|
4844 | }
|
4845 |
|
4846 | case 10:
|
4847 | out += "\n";
|
4848 | break;
|
4849 |
|
4850 | default:
|
4851 | out += String.fromCharCode(ch);
|
4852 | break;
|
4853 | }
|
4854 |
|
4855 | ++this.state.curLine;
|
4856 | this.state.lineStart = this.state.pos;
|
4857 | chunkStart = this.state.pos;
|
4858 | } else {
|
4859 | ++this.state.pos;
|
4860 | }
|
4861 | }
|
4862 | }
|
4863 |
|
4864 | readEscapedChar(inTemplate) {
|
4865 | const throwOnInvalid = !inTemplate;
|
4866 | const ch = this.state.input.charCodeAt(++this.state.pos);
|
4867 | ++this.state.pos;
|
4868 |
|
4869 | switch (ch) {
|
4870 | case 110:
|
4871 | return "\n";
|
4872 |
|
4873 | case 114:
|
4874 | return "\r";
|
4875 |
|
4876 | case 120:
|
4877 | {
|
4878 | const code = this.readHexChar(2, throwOnInvalid);
|
4879 | return code === null ? null : String.fromCharCode(code);
|
4880 | }
|
4881 |
|
4882 | case 117:
|
4883 | {
|
4884 | const code = this.readCodePoint(throwOnInvalid);
|
4885 | return code === null ? null : String.fromCodePoint(code);
|
4886 | }
|
4887 |
|
4888 | case 116:
|
4889 | return "\t";
|
4890 |
|
4891 | case 98:
|
4892 | return "\b";
|
4893 |
|
4894 | case 118:
|
4895 | return "\u000b";
|
4896 |
|
4897 | case 102:
|
4898 | return "\f";
|
4899 |
|
4900 | case 13:
|
4901 | if (this.state.input.charCodeAt(this.state.pos) === 10) {
|
4902 | ++this.state.pos;
|
4903 | }
|
4904 |
|
4905 | case 10:
|
4906 | this.state.lineStart = this.state.pos;
|
4907 | ++this.state.curLine;
|
4908 |
|
4909 | case 8232:
|
4910 | case 8233:
|
4911 | return "";
|
4912 |
|
4913 | default:
|
4914 | if (ch >= 48 && ch <= 55) {
|
4915 | const codePos = this.state.pos - 1;
|
4916 | let octalStr = this.state.input.substr(this.state.pos - 1, 3).match(/^[0-7]+/)[0];
|
4917 | let octal = parseInt(octalStr, 8);
|
4918 |
|
4919 | if (octal > 255) {
|
4920 | octalStr = octalStr.slice(0, -1);
|
4921 | octal = parseInt(octalStr, 8);
|
4922 | }
|
4923 |
|
4924 | if (octal > 0) {
|
4925 | if (inTemplate) {
|
4926 | this.state.invalidTemplateEscapePosition = codePos;
|
4927 | return null;
|
4928 | } else if (this.state.strict) {
|
4929 | this.raise(codePos, "Octal literal in strict mode");
|
4930 | } else if (!this.state.containsOctal) {
|
4931 | this.state.containsOctal = true;
|
4932 | this.state.octalPosition = codePos;
|
4933 | }
|
4934 | }
|
4935 |
|
4936 | this.state.pos += octalStr.length - 1;
|
4937 | return String.fromCharCode(octal);
|
4938 | }
|
4939 |
|
4940 | return String.fromCharCode(ch);
|
4941 | }
|
4942 | }
|
4943 |
|
4944 | readHexChar(len, throwOnInvalid) {
|
4945 | const codePos = this.state.pos;
|
4946 | const n = this.readInt(16, len);
|
4947 |
|
4948 | if (n === null) {
|
4949 | if (throwOnInvalid) {
|
4950 | this.raise(codePos, "Bad character escape sequence");
|
4951 | } else {
|
4952 | this.state.pos = codePos - 1;
|
4953 | this.state.invalidTemplateEscapePosition = codePos - 1;
|
4954 | }
|
4955 | }
|
4956 |
|
4957 | return n;
|
4958 | }
|
4959 |
|
4960 | readWord1() {
|
4961 | let word = "";
|
4962 | this.state.containsEsc = false;
|
4963 | const start = this.state.pos;
|
4964 | let chunkStart = this.state.pos;
|
4965 |
|
4966 | while (this.state.pos < this.state.length) {
|
4967 | const ch = this.state.input.codePointAt(this.state.pos);
|
4968 |
|
4969 | if (isIdentifierChar(ch)) {
|
4970 | this.state.pos += ch <= 0xffff ? 1 : 2;
|
4971 | } else if (this.state.isIterator && ch === 64) {
|
4972 | ++this.state.pos;
|
4973 | } else if (ch === 92) {
|
4974 | this.state.containsEsc = true;
|
4975 | word += this.state.input.slice(chunkStart, this.state.pos);
|
4976 | const escStart = this.state.pos;
|
4977 | const identifierCheck = this.state.pos === start ? isIdentifierStart : isIdentifierChar;
|
4978 |
|
4979 | if (this.state.input.charCodeAt(++this.state.pos) !== 117) {
|
4980 | this.raise(this.state.pos, "Expecting Unicode escape sequence \\uXXXX");
|
4981 | }
|
4982 |
|
4983 | ++this.state.pos;
|
4984 | const esc = this.readCodePoint(true);
|
4985 |
|
4986 | if (!identifierCheck(esc, true)) {
|
4987 | this.raise(escStart, "Invalid Unicode escape");
|
4988 | }
|
4989 |
|
4990 | word += String.fromCodePoint(esc);
|
4991 | chunkStart = this.state.pos;
|
4992 | } else {
|
4993 | break;
|
4994 | }
|
4995 | }
|
4996 |
|
4997 | return word + this.state.input.slice(chunkStart, this.state.pos);
|
4998 | }
|
4999 |
|
5000 | isIterator(word) {
|
5001 | return word === "@@iterator" || word === "@@asyncIterator";
|
5002 | }
|
5003 |
|
5004 | readWord() {
|
5005 | const word = this.readWord1();
|
5006 | const type = keywords[word] || types.name;
|
5007 |
|
5008 | if (type.keyword && this.state.containsEsc) {
|
5009 | this.raise(this.state.pos, `Escape sequence in keyword ${word}`);
|
5010 | }
|
5011 |
|
5012 | if (this.state.isIterator && (!this.isIterator(word) || !this.state.inType)) {
|
5013 | this.raise(this.state.pos, `Invalid identifier ${word}`);
|
5014 | }
|
5015 |
|
5016 | this.finishToken(type, word);
|
5017 | }
|
5018 |
|
5019 | braceIsBlock(prevType) {
|
5020 | const parent = this.curContext();
|
5021 |
|
5022 | if (parent === types$1.functionExpression || parent === types$1.functionStatement) {
|
5023 | return true;
|
5024 | }
|
5025 |
|
5026 | if (prevType === types.colon && (parent === types$1.braceStatement || parent === types$1.braceExpression)) {
|
5027 | return !parent.isExpr;
|
5028 | }
|
5029 |
|
5030 | if (prevType === types._return || prevType === types.name && this.state.exprAllowed) {
|
5031 | return lineBreak.test(this.state.input.slice(this.state.lastTokEnd, this.state.start));
|
5032 | }
|
5033 |
|
5034 | if (prevType === types._else || prevType === types.semi || prevType === types.eof || prevType === types.parenR || prevType === types.arrow) {
|
5035 | return true;
|
5036 | }
|
5037 |
|
5038 | if (prevType === types.braceL) {
|
5039 | return parent === types$1.braceStatement;
|
5040 | }
|
5041 |
|
5042 | if (prevType === types._var || prevType === types._const || prevType === types.name) {
|
5043 | return false;
|
5044 | }
|
5045 |
|
5046 | if (prevType === types.relational) {
|
5047 | return true;
|
5048 | }
|
5049 |
|
5050 | return !this.state.exprAllowed;
|
5051 | }
|
5052 |
|
5053 | updateContext(prevType) {
|
5054 | const type = this.state.type;
|
5055 | let update;
|
5056 |
|
5057 | if (type.keyword && (prevType === types.dot || prevType === types.questionDot)) {
|
5058 | this.state.exprAllowed = false;
|
5059 | } else if (update = type.updateContext) {
|
5060 | update.call(this, prevType);
|
5061 | } else {
|
5062 | this.state.exprAllowed = type.beforeExpr;
|
5063 | }
|
5064 | }
|
5065 |
|
5066 | }
|
5067 |
|
5068 | class UtilParser extends Tokenizer {
|
5069 | addExtra(node, key, val) {
|
5070 | if (!node) return;
|
5071 | const extra = node.extra = node.extra || {};
|
5072 | extra[key] = val;
|
5073 | }
|
5074 |
|
5075 | isRelational(op) {
|
5076 | return this.match(types.relational) && this.state.value === op;
|
5077 | }
|
5078 |
|
5079 | isLookaheadRelational(op) {
|
5080 | const l = this.lookahead();
|
5081 | return l.type === types.relational && l.value === op;
|
5082 | }
|
5083 |
|
5084 | expectRelational(op) {
|
5085 | if (this.isRelational(op)) {
|
5086 | this.next();
|
5087 | } else {
|
5088 | this.unexpected(null, types.relational);
|
5089 | }
|
5090 | }
|
5091 |
|
5092 | eatRelational(op) {
|
5093 | if (this.isRelational(op)) {
|
5094 | this.next();
|
5095 | return true;
|
5096 | }
|
5097 |
|
5098 | return false;
|
5099 | }
|
5100 |
|
5101 | isContextual(name) {
|
5102 | return this.match(types.name) && this.state.value === name && !this.state.containsEsc;
|
5103 | }
|
5104 |
|
5105 | isLookaheadContextual(name) {
|
5106 | const l = this.lookahead();
|
5107 | return l.type === types.name && l.value === name;
|
5108 | }
|
5109 |
|
5110 | eatContextual(name) {
|
5111 | return this.isContextual(name) && this.eat(types.name);
|
5112 | }
|
5113 |
|
5114 | expectContextual(name, message) {
|
5115 | if (!this.eatContextual(name)) this.unexpected(null, message);
|
5116 | }
|
5117 |
|
5118 | canInsertSemicolon() {
|
5119 | return this.match(types.eof) || this.match(types.braceR) || this.hasPrecedingLineBreak();
|
5120 | }
|
5121 |
|
5122 | hasPrecedingLineBreak() {
|
5123 | return lineBreak.test(this.state.input.slice(this.state.lastTokEnd, this.state.start));
|
5124 | }
|
5125 |
|
5126 | isLineTerminator() {
|
5127 | return this.eat(types.semi) || this.canInsertSemicolon();
|
5128 | }
|
5129 |
|
5130 | semicolon() {
|
5131 | if (!this.isLineTerminator()) this.unexpected(null, types.semi);
|
5132 | }
|
5133 |
|
5134 | expect(type, pos) {
|
5135 | this.eat(type) || this.unexpected(pos, type);
|
5136 | }
|
5137 |
|
5138 | unexpected(pos, messageOrType = "Unexpected token") {
|
5139 | if (typeof messageOrType !== "string") {
|
5140 | messageOrType = `Unexpected token, expected "${messageOrType.label}"`;
|
5141 | }
|
5142 |
|
5143 | throw this.raise(pos != null ? pos : this.state.start, messageOrType);
|
5144 | }
|
5145 |
|
5146 | expectPlugin(name, pos) {
|
5147 | if (!this.hasPlugin(name)) {
|
5148 | throw this.raise(pos != null ? pos : this.state.start, `This experimental syntax requires enabling the parser plugin: '${name}'`, {
|
5149 | missingPluginNames: [name]
|
5150 | });
|
5151 | }
|
5152 |
|
5153 | return true;
|
5154 | }
|
5155 |
|
5156 | expectOnePlugin(names, pos) {
|
5157 | if (!names.some(n => this.hasPlugin(n))) {
|
5158 | throw this.raise(pos != null ? pos : this.state.start, `This experimental syntax requires enabling one of the following parser plugin(s): '${names.join(", ")}'`, {
|
5159 | missingPluginNames: names
|
5160 | });
|
5161 | }
|
5162 | }
|
5163 |
|
5164 | checkYieldAwaitInDefaultParams() {
|
5165 | if (this.state.yieldPos && (!this.state.awaitPos || this.state.yieldPos < this.state.awaitPos)) {
|
5166 | this.raise(this.state.yieldPos, "Yield cannot be used as name inside a generator function");
|
5167 | }
|
5168 |
|
5169 | if (this.state.awaitPos) {
|
5170 | this.raise(this.state.awaitPos, "Await cannot be used as name inside an async function");
|
5171 | }
|
5172 | }
|
5173 |
|
5174 | }
|
5175 |
|
5176 | class Node {
|
5177 | constructor(parser, pos, loc) {
|
5178 | this.type = "";
|
5179 | this.start = pos;
|
5180 | this.end = 0;
|
5181 | this.loc = new SourceLocation(loc);
|
5182 | if (parser && parser.options.ranges) this.range = [pos, 0];
|
5183 | if (parser && parser.filename) this.loc.filename = parser.filename;
|
5184 | }
|
5185 |
|
5186 | __clone() {
|
5187 | const newNode = new Node();
|
5188 | const keys = Object.keys(this);
|
5189 |
|
5190 | for (let i = 0, length = keys.length; i < length; i++) {
|
5191 | const key = keys[i];
|
5192 |
|
5193 | if (key !== "leadingComments" && key !== "trailingComments" && key !== "innerComments") {
|
5194 | newNode[key] = this[key];
|
5195 | }
|
5196 | }
|
5197 |
|
5198 | return newNode;
|
5199 | }
|
5200 |
|
5201 | }
|
5202 |
|
5203 | class NodeUtils extends UtilParser {
|
5204 | startNode() {
|
5205 | return new Node(this, this.state.start, this.state.startLoc);
|
5206 | }
|
5207 |
|
5208 | startNodeAt(pos, loc) {
|
5209 | return new Node(this, pos, loc);
|
5210 | }
|
5211 |
|
5212 | startNodeAtNode(type) {
|
5213 | return this.startNodeAt(type.start, type.loc.start);
|
5214 | }
|
5215 |
|
5216 | finishNode(node, type) {
|
5217 | return this.finishNodeAt(node, type, this.state.lastTokEnd, this.state.lastTokEndLoc);
|
5218 | }
|
5219 |
|
5220 | finishNodeAt(node, type, pos, loc) {
|
5221 | node.type = type;
|
5222 | node.end = pos;
|
5223 | node.loc.end = loc;
|
5224 | if (this.options.ranges) node.range[1] = pos;
|
5225 | this.processComment(node);
|
5226 | return node;
|
5227 | }
|
5228 |
|
5229 | resetStartLocation(node, start, startLoc) {
|
5230 | node.start = start;
|
5231 | node.loc.start = startLoc;
|
5232 | if (this.options.ranges) node.range[0] = start;
|
5233 | }
|
5234 |
|
5235 | resetStartLocationFromNode(node, locationNode) {
|
5236 | this.resetStartLocation(node, locationNode.start, locationNode.loc.start);
|
5237 | }
|
5238 |
|
5239 | }
|
5240 |
|
5241 | class LValParser extends NodeUtils {
|
5242 | toAssignable(node, isBinding, contextDescription) {
|
5243 | if (node) {
|
5244 | switch (node.type) {
|
5245 | case "Identifier":
|
5246 | case "ObjectPattern":
|
5247 | case "ArrayPattern":
|
5248 | case "AssignmentPattern":
|
5249 | break;
|
5250 |
|
5251 | case "ObjectExpression":
|
5252 | node.type = "ObjectPattern";
|
5253 |
|
5254 | for (let i = 0, length = node.properties.length, last = length - 1; i < length; i++) {
|
5255 | const prop = node.properties[i];
|
5256 | const isLast = i === last;
|
5257 | this.toAssignableObjectExpressionProp(prop, isBinding, isLast);
|
5258 | }
|
5259 |
|
5260 | break;
|
5261 |
|
5262 | case "ObjectProperty":
|
5263 | this.toAssignable(node.value, isBinding, contextDescription);
|
5264 | break;
|
5265 |
|
5266 | case "SpreadElement":
|
5267 | {
|
5268 | this.checkToRestConversion(node);
|
5269 | node.type = "RestElement";
|
5270 | const arg = node.argument;
|
5271 | this.toAssignable(arg, isBinding, contextDescription);
|
5272 | break;
|
5273 | }
|
5274 |
|
5275 | case "ArrayExpression":
|
5276 | node.type = "ArrayPattern";
|
5277 | this.toAssignableList(node.elements, isBinding, contextDescription);
|
5278 | break;
|
5279 |
|
5280 | case "AssignmentExpression":
|
5281 | if (node.operator === "=") {
|
5282 | node.type = "AssignmentPattern";
|
5283 | delete node.operator;
|
5284 | } else {
|
5285 | this.raise(node.left.end, "Only '=' operator can be used for specifying default value.");
|
5286 | }
|
5287 |
|
5288 | break;
|
5289 |
|
5290 | case "MemberExpression":
|
5291 | if (!isBinding) break;
|
5292 |
|
5293 | default:
|
5294 | {
|
5295 | const message = "Invalid left-hand side" + (contextDescription ? " in " + contextDescription : "expression");
|
5296 | this.raise(node.start, message);
|
5297 | }
|
5298 | }
|
5299 | }
|
5300 |
|
5301 | return node;
|
5302 | }
|
5303 |
|
5304 | toAssignableObjectExpressionProp(prop, isBinding, isLast) {
|
5305 | if (prop.type === "ObjectMethod") {
|
5306 | const error = prop.kind === "get" || prop.kind === "set" ? "Object pattern can't contain getter or setter" : "Object pattern can't contain methods";
|
5307 | this.raise(prop.key.start, error);
|
5308 | } else if (prop.type === "SpreadElement" && !isLast) {
|
5309 | this.raiseRestNotLast(prop.start, "property");
|
5310 | } else {
|
5311 | this.toAssignable(prop, isBinding, "object destructuring pattern");
|
5312 | }
|
5313 | }
|
5314 |
|
5315 | toAssignableList(exprList, isBinding, contextDescription) {
|
5316 | let end = exprList.length;
|
5317 |
|
5318 | if (end) {
|
5319 | const last = exprList[end - 1];
|
5320 |
|
5321 | if (last && last.type === "RestElement") {
|
5322 | --end;
|
5323 | } else if (last && last.type === "SpreadElement") {
|
5324 | last.type = "RestElement";
|
5325 | const arg = last.argument;
|
5326 | this.toAssignable(arg, isBinding, contextDescription);
|
5327 |
|
5328 | if (arg.type !== "Identifier" && arg.type !== "MemberExpression" && arg.type !== "ArrayPattern" && arg.type !== "ObjectPattern") {
|
5329 | this.unexpected(arg.start);
|
5330 | }
|
5331 |
|
5332 | --end;
|
5333 | }
|
5334 | }
|
5335 |
|
5336 | for (let i = 0; i < end; i++) {
|
5337 | const elt = exprList[i];
|
5338 |
|
5339 | if (elt) {
|
5340 | this.toAssignable(elt, isBinding, contextDescription);
|
5341 |
|
5342 | if (elt.type === "RestElement") {
|
5343 | this.raiseRestNotLast(elt.start, "element");
|
5344 | }
|
5345 | }
|
5346 | }
|
5347 |
|
5348 | return exprList;
|
5349 | }
|
5350 |
|
5351 | toReferencedList(exprList, isParenthesizedExpr) {
|
5352 | return exprList;
|
5353 | }
|
5354 |
|
5355 | toReferencedListDeep(exprList, isParenthesizedExpr) {
|
5356 | this.toReferencedList(exprList, isParenthesizedExpr);
|
5357 |
|
5358 | for (let _i = 0; _i < exprList.length; _i++) {
|
5359 | const expr = exprList[_i];
|
5360 |
|
5361 | if (expr && expr.type === "ArrayExpression") {
|
5362 | this.toReferencedListDeep(expr.elements);
|
5363 | }
|
5364 | }
|
5365 |
|
5366 | return exprList;
|
5367 | }
|
5368 |
|
5369 | parseSpread(refShorthandDefaultPos, refNeedsArrowPos) {
|
5370 | const node = this.startNode();
|
5371 | this.next();
|
5372 | node.argument = this.parseMaybeAssign(false, refShorthandDefaultPos, undefined, refNeedsArrowPos);
|
5373 |
|
5374 | if (this.state.commaAfterSpreadAt === -1 && this.match(types.comma)) {
|
5375 | this.state.commaAfterSpreadAt = this.state.start;
|
5376 | }
|
5377 |
|
5378 | return this.finishNode(node, "SpreadElement");
|
5379 | }
|
5380 |
|
5381 | parseRest() {
|
5382 | const node = this.startNode();
|
5383 | this.next();
|
5384 | node.argument = this.parseBindingAtom();
|
5385 | return this.finishNode(node, "RestElement");
|
5386 | }
|
5387 |
|
5388 | parseBindingAtom() {
|
5389 | switch (this.state.type) {
|
5390 | case types.name:
|
5391 | return this.parseIdentifier();
|
5392 |
|
5393 | case types.bracketL:
|
5394 | {
|
5395 | const node = this.startNode();
|
5396 | this.next();
|
5397 | node.elements = this.parseBindingList(types.bracketR, true);
|
5398 | return this.finishNode(node, "ArrayPattern");
|
5399 | }
|
5400 |
|
5401 | case types.braceL:
|
5402 | return this.parseObj(true);
|
5403 |
|
5404 | default:
|
5405 | throw this.unexpected();
|
5406 | }
|
5407 | }
|
5408 |
|
5409 | parseBindingList(close, allowEmpty, allowModifiers) {
|
5410 | const elts = [];
|
5411 | let first = true;
|
5412 |
|
5413 | while (!this.eat(close)) {
|
5414 | if (first) {
|
5415 | first = false;
|
5416 | } else {
|
5417 | this.expect(types.comma);
|
5418 | }
|
5419 |
|
5420 | if (allowEmpty && this.match(types.comma)) {
|
5421 | elts.push(null);
|
5422 | } else if (this.eat(close)) {
|
5423 | break;
|
5424 | } else if (this.match(types.ellipsis)) {
|
5425 | elts.push(this.parseAssignableListItemTypes(this.parseRest()));
|
5426 | this.checkCommaAfterRest(close, this.state.inFunction && this.state.inParameters ? "parameter" : "element");
|
5427 | this.expect(close);
|
5428 | break;
|
5429 | } else {
|
5430 | const decorators = [];
|
5431 |
|
5432 | if (this.match(types.at) && this.hasPlugin("decorators")) {
|
5433 | this.raise(this.state.start, "Stage 2 decorators cannot be used to decorate parameters");
|
5434 | }
|
5435 |
|
5436 | while (this.match(types.at)) {
|
5437 | decorators.push(this.parseDecorator());
|
5438 | }
|
5439 |
|
5440 | elts.push(this.parseAssignableListItem(allowModifiers, decorators));
|
5441 | }
|
5442 | }
|
5443 |
|
5444 | return elts;
|
5445 | }
|
5446 |
|
5447 | parseAssignableListItem(allowModifiers, decorators) {
|
5448 | const left = this.parseMaybeDefault();
|
5449 | this.parseAssignableListItemTypes(left);
|
5450 | const elt = this.parseMaybeDefault(left.start, left.loc.start, left);
|
5451 |
|
5452 | if (decorators.length) {
|
5453 | left.decorators = decorators;
|
5454 | }
|
5455 |
|
5456 | return elt;
|
5457 | }
|
5458 |
|
5459 | parseAssignableListItemTypes(param) {
|
5460 | return param;
|
5461 | }
|
5462 |
|
5463 | parseMaybeDefault(startPos, startLoc, left) {
|
5464 | startLoc = startLoc || this.state.startLoc;
|
5465 | startPos = startPos || this.state.start;
|
5466 | left = left || this.parseBindingAtom();
|
5467 | if (!this.eat(types.eq)) return left;
|
5468 | const node = this.startNodeAt(startPos, startLoc);
|
5469 | node.left = left;
|
5470 | node.right = this.parseMaybeAssign();
|
5471 | return this.finishNode(node, "AssignmentPattern");
|
5472 | }
|
5473 |
|
5474 | checkLVal(expr, isBinding, checkClashes, contextDescription) {
|
5475 | switch (expr.type) {
|
5476 | case "Identifier":
|
5477 | if (this.state.strict && isStrictBindReservedWord(expr.name, this.inModule)) {
|
5478 | this.raise(expr.start, `${isBinding ? "Binding" : "Assigning to"} '${expr.name}' in strict mode`);
|
5479 | }
|
5480 |
|
5481 | if (checkClashes) {
|
5482 | const key = `_${expr.name}`;
|
5483 |
|
5484 | if (checkClashes[key]) {
|
5485 | this.raise(expr.start, "Argument name clash in strict mode");
|
5486 | } else {
|
5487 | checkClashes[key] = true;
|
5488 | }
|
5489 | }
|
5490 |
|
5491 | break;
|
5492 |
|
5493 | case "MemberExpression":
|
5494 | if (isBinding) this.raise(expr.start, "Binding member expression");
|
5495 | break;
|
5496 |
|
5497 | case "ObjectPattern":
|
5498 | for (let _i2 = 0, _expr$properties = expr.properties; _i2 < _expr$properties.length; _i2++) {
|
5499 | let prop = _expr$properties[_i2];
|
5500 | if (prop.type === "ObjectProperty") prop = prop.value;
|
5501 | this.checkLVal(prop, isBinding, checkClashes, "object destructuring pattern");
|
5502 | }
|
5503 |
|
5504 | break;
|
5505 |
|
5506 | case "ArrayPattern":
|
5507 | for (let _i3 = 0, _expr$elements = expr.elements; _i3 < _expr$elements.length; _i3++) {
|
5508 | const elem = _expr$elements[_i3];
|
5509 |
|
5510 | if (elem) {
|
5511 | this.checkLVal(elem, isBinding, checkClashes, "array destructuring pattern");
|
5512 | }
|
5513 | }
|
5514 |
|
5515 | break;
|
5516 |
|
5517 | case "AssignmentPattern":
|
5518 | this.checkLVal(expr.left, isBinding, checkClashes, "assignment pattern");
|
5519 | break;
|
5520 |
|
5521 | case "RestElement":
|
5522 | this.checkLVal(expr.argument, isBinding, checkClashes, "rest element");
|
5523 | break;
|
5524 |
|
5525 | default:
|
5526 | {
|
5527 | const message = (isBinding ? "Binding invalid" : "Invalid") + " left-hand side" + (contextDescription ? " in " + contextDescription : "expression");
|
5528 | this.raise(expr.start, message);
|
5529 | }
|
5530 | }
|
5531 | }
|
5532 |
|
5533 | checkToRestConversion(node) {
|
5534 | if (node.argument.type !== "Identifier" && node.argument.type !== "MemberExpression") {
|
5535 | this.raise(node.argument.start, "Invalid rest operator's argument");
|
5536 | }
|
5537 | }
|
5538 |
|
5539 | checkCommaAfterRest(close, kind) {
|
5540 | if (this.match(types.comma)) {
|
5541 | if (this.lookahead().type === close) {
|
5542 | this.raiseCommaAfterRest(this.state.start, kind);
|
5543 | } else {
|
5544 | this.raiseRestNotLast(this.state.start, kind);
|
5545 | }
|
5546 | }
|
5547 | }
|
5548 |
|
5549 | checkCommaAfterRestFromSpread(kind) {
|
5550 | if (this.state.commaAfterSpreadAt > -1) {
|
5551 | this.raiseCommaAfterRest(this.state.commaAfterSpreadAt, kind);
|
5552 | }
|
5553 | }
|
5554 |
|
5555 | raiseCommaAfterRest(pos, kind) {
|
5556 | this.raise(pos, `A trailing comma is not permitted after the rest ${kind}`);
|
5557 | }
|
5558 |
|
5559 | raiseRestNotLast(pos, kind) {
|
5560 | this.raise(pos, `The rest ${kind} must be the last ${kind}`);
|
5561 | }
|
5562 |
|
5563 | }
|
5564 |
|
5565 | class ExpressionParser extends LValParser {
|
5566 | checkPropClash(prop, propHash) {
|
5567 | if (prop.computed || prop.kind) return;
|
5568 | const key = prop.key;
|
5569 | const name = key.type === "Identifier" ? key.name : String(key.value);
|
5570 |
|
5571 | if (name === "__proto__") {
|
5572 | if (propHash.proto) {
|
5573 | this.raise(key.start, "Redefinition of __proto__ property");
|
5574 | }
|
5575 |
|
5576 | propHash.proto = true;
|
5577 | }
|
5578 | }
|
5579 |
|
5580 | getExpression() {
|
5581 | this.nextToken();
|
5582 | const expr = this.parseExpression();
|
5583 |
|
5584 | if (!this.match(types.eof)) {
|
5585 | this.unexpected();
|
5586 | }
|
5587 |
|
5588 | expr.comments = this.state.comments;
|
5589 | return expr;
|
5590 | }
|
5591 |
|
5592 | parseExpression(noIn, refShorthandDefaultPos) {
|
5593 | const startPos = this.state.start;
|
5594 | const startLoc = this.state.startLoc;
|
5595 | const expr = this.parseMaybeAssign(noIn, refShorthandDefaultPos);
|
5596 |
|
5597 | if (this.match(types.comma)) {
|
5598 | const node = this.startNodeAt(startPos, startLoc);
|
5599 | node.expressions = [expr];
|
5600 |
|
5601 | while (this.eat(types.comma)) {
|
5602 | node.expressions.push(this.parseMaybeAssign(noIn, refShorthandDefaultPos));
|
5603 | }
|
5604 |
|
5605 | this.toReferencedList(node.expressions);
|
5606 | return this.finishNode(node, "SequenceExpression");
|
5607 | }
|
5608 |
|
5609 | return expr;
|
5610 | }
|
5611 |
|
5612 | parseMaybeAssign(noIn, refShorthandDefaultPos, afterLeftParse, refNeedsArrowPos) {
|
5613 | const startPos = this.state.start;
|
5614 | const startLoc = this.state.startLoc;
|
5615 |
|
5616 | if (this.isContextual("yield")) {
|
5617 | if (this.state.inGenerator) {
|
5618 | let left = this.parseYield(noIn);
|
5619 |
|
5620 | if (afterLeftParse) {
|
5621 | left = afterLeftParse.call(this, left, startPos, startLoc);
|
5622 | }
|
5623 |
|
5624 | return left;
|
5625 | } else {
|
5626 | this.state.exprAllowed = false;
|
5627 | }
|
5628 | }
|
5629 |
|
5630 | const oldCommaAfterSpreadAt = this.state.commaAfterSpreadAt;
|
5631 | this.state.commaAfterSpreadAt = -1;
|
5632 | let failOnShorthandAssign;
|
5633 |
|
5634 | if (refShorthandDefaultPos) {
|
5635 | failOnShorthandAssign = false;
|
5636 | } else {
|
5637 | refShorthandDefaultPos = {
|
5638 | start: 0
|
5639 | };
|
5640 | failOnShorthandAssign = true;
|
5641 | }
|
5642 |
|
5643 | if (this.match(types.parenL) || this.match(types.name)) {
|
5644 | this.state.potentialArrowAt = this.state.start;
|
5645 | }
|
5646 |
|
5647 | let left = this.parseMaybeConditional(noIn, refShorthandDefaultPos, refNeedsArrowPos);
|
5648 |
|
5649 | if (afterLeftParse) {
|
5650 | left = afterLeftParse.call(this, left, startPos, startLoc);
|
5651 | }
|
5652 |
|
5653 | if (this.state.type.isAssign) {
|
5654 | const node = this.startNodeAt(startPos, startLoc);
|
5655 | const operator = this.state.value;
|
5656 | node.operator = operator;
|
5657 |
|
5658 | if (operator === "??=") {
|
5659 | this.expectPlugin("nullishCoalescingOperator");
|
5660 | this.expectPlugin("logicalAssignment");
|
5661 | }
|
5662 |
|
5663 | if (operator === "||=" || operator === "&&=") {
|
5664 | this.expectPlugin("logicalAssignment");
|
5665 | }
|
5666 |
|
5667 | node.left = this.match(types.eq) ? this.toAssignable(left, undefined, "assignment expression") : left;
|
5668 | refShorthandDefaultPos.start = 0;
|
5669 | this.checkLVal(left, undefined, undefined, "assignment expression");
|
5670 | let patternErrorMsg;
|
5671 | let elementName;
|
5672 |
|
5673 | if (left.type === "ObjectPattern") {
|
5674 | patternErrorMsg = "`({a}) = 0` use `({a} = 0)`";
|
5675 | elementName = "property";
|
5676 | } else if (left.type === "ArrayPattern") {
|
5677 | patternErrorMsg = "`([a]) = 0` use `([a] = 0)`";
|
5678 | elementName = "element";
|
5679 | }
|
5680 |
|
5681 | if (patternErrorMsg && left.extra && left.extra.parenthesized) {
|
5682 | this.raise(left.start, `You're trying to assign to a parenthesized expression, eg. instead of ${patternErrorMsg}`);
|
5683 | }
|
5684 |
|
5685 | if (elementName) this.checkCommaAfterRestFromSpread(elementName);
|
5686 | this.state.commaAfterSpreadAt = oldCommaAfterSpreadAt;
|
5687 | this.next();
|
5688 | node.right = this.parseMaybeAssign(noIn);
|
5689 | return this.finishNode(node, "AssignmentExpression");
|
5690 | } else if (failOnShorthandAssign && refShorthandDefaultPos.start) {
|
5691 | this.unexpected(refShorthandDefaultPos.start);
|
5692 | }
|
5693 |
|
5694 | this.state.commaAfterSpreadAt = oldCommaAfterSpreadAt;
|
5695 | return left;
|
5696 | }
|
5697 |
|
5698 | parseMaybeConditional(noIn, refShorthandDefaultPos, refNeedsArrowPos) {
|
5699 | const startPos = this.state.start;
|
5700 | const startLoc = this.state.startLoc;
|
5701 | const potentialArrowAt = this.state.potentialArrowAt;
|
5702 | const expr = this.parseExprOps(noIn, refShorthandDefaultPos);
|
5703 |
|
5704 | if (expr.type === "ArrowFunctionExpression" && expr.start === potentialArrowAt) {
|
5705 | return expr;
|
5706 | }
|
5707 |
|
5708 | if (refShorthandDefaultPos && refShorthandDefaultPos.start) return expr;
|
5709 | return this.parseConditional(expr, noIn, startPos, startLoc, refNeedsArrowPos);
|
5710 | }
|
5711 |
|
5712 | parseConditional(expr, noIn, startPos, startLoc, refNeedsArrowPos) {
|
5713 | if (this.eat(types.question)) {
|
5714 | const node = this.startNodeAt(startPos, startLoc);
|
5715 | node.test = expr;
|
5716 | node.consequent = this.parseMaybeAssign();
|
5717 | this.expect(types.colon);
|
5718 | node.alternate = this.parseMaybeAssign(noIn);
|
5719 | return this.finishNode(node, "ConditionalExpression");
|
5720 | }
|
5721 |
|
5722 | return expr;
|
5723 | }
|
5724 |
|
5725 | parseExprOps(noIn, refShorthandDefaultPos) {
|
5726 | const startPos = this.state.start;
|
5727 | const startLoc = this.state.startLoc;
|
5728 | const potentialArrowAt = this.state.potentialArrowAt;
|
5729 | const expr = this.parseMaybeUnary(refShorthandDefaultPos);
|
5730 |
|
5731 | if (expr.type === "ArrowFunctionExpression" && expr.start === potentialArrowAt) {
|
5732 | return expr;
|
5733 | }
|
5734 |
|
5735 | if (refShorthandDefaultPos && refShorthandDefaultPos.start) {
|
5736 | return expr;
|
5737 | }
|
5738 |
|
5739 | return this.parseExprOp(expr, startPos, startLoc, -1, noIn);
|
5740 | }
|
5741 |
|
5742 | parseExprOp(left, leftStartPos, leftStartLoc, minPrec, noIn) {
|
5743 | const prec = this.state.type.binop;
|
5744 |
|
5745 | if (prec != null && (!noIn || !this.match(types._in))) {
|
5746 | if (prec > minPrec) {
|
5747 | const node = this.startNodeAt(leftStartPos, leftStartLoc);
|
5748 | const operator = this.state.value;
|
5749 | node.left = left;
|
5750 | node.operator = operator;
|
5751 |
|
5752 | if (operator === "**" && left.type === "UnaryExpression" && !(left.extra && left.extra.parenthesized)) {
|
5753 | this.raise(left.argument.start, "Illegal expression. Wrap left hand side or entire exponentiation in parentheses.");
|
5754 | }
|
5755 |
|
5756 | const op = this.state.type;
|
5757 |
|
5758 | if (op === types.pipeline) {
|
5759 | this.expectPlugin("pipelineOperator");
|
5760 | this.state.inPipeline = true;
|
5761 | this.checkPipelineAtInfixOperator(left, leftStartPos);
|
5762 | } else if (op === types.nullishCoalescing) {
|
5763 | this.expectPlugin("nullishCoalescingOperator");
|
5764 | }
|
5765 |
|
5766 | this.next();
|
5767 |
|
5768 | if (op === types.pipeline && this.getPluginOption("pipelineOperator", "proposal") === "minimal") {
|
5769 | if (this.match(types.name) && this.state.value === "await" && this.state.inAsync) {
|
5770 | throw this.raise(this.state.start, `Unexpected "await" after pipeline body; await must have parentheses in minimal proposal`);
|
5771 | }
|
5772 | }
|
5773 |
|
5774 | node.right = this.parseExprOpRightExpr(op, prec, noIn);
|
5775 | this.finishNode(node, op === types.logicalOR || op === types.logicalAND || op === types.nullishCoalescing ? "LogicalExpression" : "BinaryExpression");
|
5776 | return this.parseExprOp(node, leftStartPos, leftStartLoc, minPrec, noIn);
|
5777 | }
|
5778 | }
|
5779 |
|
5780 | return left;
|
5781 | }
|
5782 |
|
5783 | parseExprOpRightExpr(op, prec, noIn) {
|
5784 | switch (op) {
|
5785 | case types.pipeline:
|
5786 | if (this.getPluginOption("pipelineOperator", "proposal") === "smart") {
|
5787 | const startPos = this.state.start;
|
5788 | const startLoc = this.state.startLoc;
|
5789 | return this.withTopicPermittingContext(() => {
|
5790 | return this.parseSmartPipelineBody(this.parseExprOpBaseRightExpr(op, prec, noIn), startPos, startLoc);
|
5791 | });
|
5792 | }
|
5793 |
|
5794 | default:
|
5795 | return this.parseExprOpBaseRightExpr(op, prec, noIn);
|
5796 | }
|
5797 | }
|
5798 |
|
5799 | parseExprOpBaseRightExpr(op, prec, noIn) {
|
5800 | const startPos = this.state.start;
|
5801 | const startLoc = this.state.startLoc;
|
5802 | return this.parseExprOp(this.parseMaybeUnary(), startPos, startLoc, op.rightAssociative ? prec - 1 : prec, noIn);
|
5803 | }
|
5804 |
|
5805 | parseMaybeUnary(refShorthandDefaultPos) {
|
5806 | if (this.isContextual("await") && (this.state.inAsync || !this.state.inFunction && this.options.allowAwaitOutsideFunction)) {
|
5807 | return this.parseAwait();
|
5808 | } else if (this.state.type.prefix) {
|
5809 | const node = this.startNode();
|
5810 | const update = this.match(types.incDec);
|
5811 | node.operator = this.state.value;
|
5812 | node.prefix = true;
|
5813 |
|
5814 | if (node.operator === "throw") {
|
5815 | this.expectPlugin("throwExpressions");
|
5816 | }
|
5817 |
|
5818 | this.next();
|
5819 | node.argument = this.parseMaybeUnary();
|
5820 |
|
5821 | if (refShorthandDefaultPos && refShorthandDefaultPos.start) {
|
5822 | this.unexpected(refShorthandDefaultPos.start);
|
5823 | }
|
5824 |
|
5825 | if (update) {
|
5826 | this.checkLVal(node.argument, undefined, undefined, "prefix operation");
|
5827 | } else if (this.state.strict && node.operator === "delete") {
|
5828 | const arg = node.argument;
|
5829 |
|
5830 | if (arg.type === "Identifier") {
|
5831 | this.raise(node.start, "Deleting local variable in strict mode");
|
5832 | } else if (arg.type === "MemberExpression" && arg.property.type === "PrivateName") {
|
5833 | this.raise(node.start, "Deleting a private field is not allowed");
|
5834 | }
|
5835 | }
|
5836 |
|
5837 | return this.finishNode(node, update ? "UpdateExpression" : "UnaryExpression");
|
5838 | }
|
5839 |
|
5840 | const startPos = this.state.start;
|
5841 | const startLoc = this.state.startLoc;
|
5842 | let expr = this.parseExprSubscripts(refShorthandDefaultPos);
|
5843 | if (refShorthandDefaultPos && refShorthandDefaultPos.start) return expr;
|
5844 |
|
5845 | while (this.state.type.postfix && !this.canInsertSemicolon()) {
|
5846 | const node = this.startNodeAt(startPos, startLoc);
|
5847 | node.operator = this.state.value;
|
5848 | node.prefix = false;
|
5849 | node.argument = expr;
|
5850 | this.checkLVal(expr, undefined, undefined, "postfix operation");
|
5851 | this.next();
|
5852 | expr = this.finishNode(node, "UpdateExpression");
|
5853 | }
|
5854 |
|
5855 | return expr;
|
5856 | }
|
5857 |
|
5858 | parseExprSubscripts(refShorthandDefaultPos) {
|
5859 | const startPos = this.state.start;
|
5860 | const startLoc = this.state.startLoc;
|
5861 | const potentialArrowAt = this.state.potentialArrowAt;
|
5862 | const expr = this.parseExprAtom(refShorthandDefaultPos);
|
5863 |
|
5864 | if (expr.type === "ArrowFunctionExpression" && expr.start === potentialArrowAt) {
|
5865 | return expr;
|
5866 | }
|
5867 |
|
5868 | if (refShorthandDefaultPos && refShorthandDefaultPos.start) {
|
5869 | return expr;
|
5870 | }
|
5871 |
|
5872 | return this.parseSubscripts(expr, startPos, startLoc);
|
5873 | }
|
5874 |
|
5875 | parseSubscripts(base, startPos, startLoc, noCalls) {
|
5876 | const state = {
|
5877 | optionalChainMember: false,
|
5878 | stop: false
|
5879 | };
|
5880 |
|
5881 | do {
|
5882 | base = this.parseSubscript(base, startPos, startLoc, noCalls, state);
|
5883 | } while (!state.stop);
|
5884 |
|
5885 | return base;
|
5886 | }
|
5887 |
|
5888 | parseSubscript(base, startPos, startLoc, noCalls, state) {
|
5889 | if (!noCalls && this.eat(types.doubleColon)) {
|
5890 | const node = this.startNodeAt(startPos, startLoc);
|
5891 | node.object = base;
|
5892 | node.callee = this.parseNoCallExpr();
|
5893 | state.stop = true;
|
5894 | return this.parseSubscripts(this.finishNode(node, "BindExpression"), startPos, startLoc, noCalls);
|
5895 | } else if (this.match(types.questionDot)) {
|
5896 | this.expectPlugin("optionalChaining");
|
5897 | state.optionalChainMember = true;
|
5898 |
|
5899 | if (noCalls && this.lookahead().type === types.parenL) {
|
5900 | state.stop = true;
|
5901 | return base;
|
5902 | }
|
5903 |
|
5904 | this.next();
|
5905 | const node = this.startNodeAt(startPos, startLoc);
|
5906 |
|
5907 | if (this.eat(types.bracketL)) {
|
5908 | node.object = base;
|
5909 | node.property = this.parseExpression();
|
5910 | node.computed = true;
|
5911 | node.optional = true;
|
5912 | this.expect(types.bracketR);
|
5913 | return this.finishNode(node, "OptionalMemberExpression");
|
5914 | } else if (this.eat(types.parenL)) {
|
5915 | const possibleAsync = this.atPossibleAsync(base);
|
5916 | node.callee = base;
|
5917 | node.arguments = this.parseCallExpressionArguments(types.parenR, possibleAsync);
|
5918 | node.optional = true;
|
5919 | return this.finishNode(node, "OptionalCallExpression");
|
5920 | } else {
|
5921 | node.object = base;
|
5922 | node.property = this.parseIdentifier(true);
|
5923 | node.computed = false;
|
5924 | node.optional = true;
|
5925 | return this.finishNode(node, "OptionalMemberExpression");
|
5926 | }
|
5927 | } else if (this.eat(types.dot)) {
|
5928 | const node = this.startNodeAt(startPos, startLoc);
|
5929 | node.object = base;
|
5930 | node.property = this.parseMaybePrivateName();
|
5931 | node.computed = false;
|
5932 |
|
5933 | if (state.optionalChainMember) {
|
5934 | node.optional = false;
|
5935 | return this.finishNode(node, "OptionalMemberExpression");
|
5936 | }
|
5937 |
|
5938 | return this.finishNode(node, "MemberExpression");
|
5939 | } else if (this.eat(types.bracketL)) {
|
5940 | const node = this.startNodeAt(startPos, startLoc);
|
5941 | node.object = base;
|
5942 | node.property = this.parseExpression();
|
5943 | node.computed = true;
|
5944 | this.expect(types.bracketR);
|
5945 |
|
5946 | if (state.optionalChainMember) {
|
5947 | node.optional = false;
|
5948 | return this.finishNode(node, "OptionalMemberExpression");
|
5949 | }
|
5950 |
|
5951 | return this.finishNode(node, "MemberExpression");
|
5952 | } else if (!noCalls && this.match(types.parenL)) {
|
5953 | const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
|
5954 | const oldYieldPos = this.state.yieldPos;
|
5955 | const oldAwaitPos = this.state.awaitPos;
|
5956 | this.state.maybeInArrowParameters = true;
|
5957 | this.state.yieldPos = 0;
|
5958 | this.state.awaitPos = 0;
|
5959 | const possibleAsync = this.atPossibleAsync(base);
|
5960 | this.next();
|
5961 | let node = this.startNodeAt(startPos, startLoc);
|
5962 | node.callee = base;
|
5963 | const oldCommaAfterSpreadAt = this.state.commaAfterSpreadAt;
|
5964 | this.state.commaAfterSpreadAt = -1;
|
5965 | node.arguments = this.parseCallExpressionArguments(types.parenR, possibleAsync, base.type === "Import");
|
5966 |
|
5967 | if (!state.optionalChainMember) {
|
5968 | this.finishCallExpression(node);
|
5969 | } else {
|
5970 | this.finishOptionalCallExpression(node);
|
5971 | }
|
5972 |
|
5973 | if (possibleAsync && this.shouldParseAsyncArrow()) {
|
5974 | state.stop = true;
|
5975 | this.checkCommaAfterRestFromSpread("parameter");
|
5976 | node = this.parseAsyncArrowFromCallExpression(this.startNodeAt(startPos, startLoc), node);
|
5977 | this.checkYieldAwaitInDefaultParams();
|
5978 | this.state.yieldPos = oldYieldPos;
|
5979 | this.state.awaitPos = oldAwaitPos;
|
5980 | } else {
|
5981 | this.toReferencedListDeep(node.arguments);
|
5982 | this.state.yieldPos = oldYieldPos || this.state.yieldPos;
|
5983 | this.state.awaitPos = oldAwaitPos || this.state.awaitPos;
|
5984 | }
|
5985 |
|
5986 | this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
|
5987 | this.state.commaAfterSpreadAt = oldCommaAfterSpreadAt;
|
5988 | return node;
|
5989 | } else if (this.match(types.backQuote)) {
|
5990 | return this.parseTaggedTemplateExpression(startPos, startLoc, base, state);
|
5991 | } else {
|
5992 | state.stop = true;
|
5993 | return base;
|
5994 | }
|
5995 | }
|
5996 |
|
5997 | parseTaggedTemplateExpression(startPos, startLoc, base, state, typeArguments) {
|
5998 | const node = this.startNodeAt(startPos, startLoc);
|
5999 | node.tag = base;
|
6000 | node.quasi = this.parseTemplate(true);
|
6001 | if (typeArguments) node.typeParameters = typeArguments;
|
6002 |
|
6003 | if (state.optionalChainMember) {
|
6004 | this.raise(startPos, "Tagged Template Literals are not allowed in optionalChain");
|
6005 | }
|
6006 |
|
6007 | return this.finishNode(node, "TaggedTemplateExpression");
|
6008 | }
|
6009 |
|
6010 | atPossibleAsync(base) {
|
6011 | return !this.state.containsEsc && this.state.potentialArrowAt === base.start && base.type === "Identifier" && base.name === "async" && !this.canInsertSemicolon();
|
6012 | }
|
6013 |
|
6014 | finishCallExpression(node) {
|
6015 | if (node.callee.type === "Import") {
|
6016 | if (node.arguments.length !== 1) {
|
6017 | this.raise(node.start, "import() requires exactly one argument");
|
6018 | }
|
6019 |
|
6020 | const importArg = node.arguments[0];
|
6021 |
|
6022 | if (importArg && importArg.type === "SpreadElement") {
|
6023 | this.raise(importArg.start, "... is not allowed in import()");
|
6024 | }
|
6025 | }
|
6026 |
|
6027 | return this.finishNode(node, "CallExpression");
|
6028 | }
|
6029 |
|
6030 | finishOptionalCallExpression(node) {
|
6031 | if (node.callee.type === "Import") {
|
6032 | if (node.arguments.length !== 1) {
|
6033 | this.raise(node.start, "import() requires exactly one argument");
|
6034 | }
|
6035 |
|
6036 | const importArg = node.arguments[0];
|
6037 |
|
6038 | if (importArg && importArg.type === "SpreadElement") {
|
6039 | this.raise(importArg.start, "... is not allowed in import()");
|
6040 | }
|
6041 | }
|
6042 |
|
6043 | return this.finishNode(node, "OptionalCallExpression");
|
6044 | }
|
6045 |
|
6046 | parseCallExpressionArguments(close, possibleAsyncArrow, dynamicImport) {
|
6047 | const elts = [];
|
6048 | let innerParenStart;
|
6049 | let first = true;
|
6050 |
|
6051 | while (!this.eat(close)) {
|
6052 | if (first) {
|
6053 | first = false;
|
6054 | } else {
|
6055 | this.expect(types.comma);
|
6056 |
|
6057 | if (this.eat(close)) {
|
6058 | if (dynamicImport) {
|
6059 | this.raise(this.state.lastTokStart, "Trailing comma is disallowed inside import(...) arguments");
|
6060 | }
|
6061 |
|
6062 | break;
|
6063 | }
|
6064 | }
|
6065 |
|
6066 | if (this.match(types.parenL) && !innerParenStart) {
|
6067 | innerParenStart = this.state.start;
|
6068 | }
|
6069 |
|
6070 | elts.push(this.parseExprListItem(false, possibleAsyncArrow ? {
|
6071 | start: 0
|
6072 | } : undefined, possibleAsyncArrow ? {
|
6073 | start: 0
|
6074 | } : undefined));
|
6075 | }
|
6076 |
|
6077 | if (possibleAsyncArrow && innerParenStart && this.shouldParseAsyncArrow()) {
|
6078 | this.unexpected();
|
6079 | }
|
6080 |
|
6081 | return elts;
|
6082 | }
|
6083 |
|
6084 | shouldParseAsyncArrow() {
|
6085 | return this.match(types.arrow);
|
6086 | }
|
6087 |
|
6088 | parseAsyncArrowFromCallExpression(node, call) {
|
6089 | this.expect(types.arrow);
|
6090 | this.parseArrowExpression(node, call.arguments, true);
|
6091 | return node;
|
6092 | }
|
6093 |
|
6094 | parseNoCallExpr() {
|
6095 | const startPos = this.state.start;
|
6096 | const startLoc = this.state.startLoc;
|
6097 | return this.parseSubscripts(this.parseExprAtom(), startPos, startLoc, true);
|
6098 | }
|
6099 |
|
6100 | parseExprAtom(refShorthandDefaultPos) {
|
6101 | if (this.state.type === types.slash) this.readRegexp();
|
6102 | const canBeArrow = this.state.potentialArrowAt === this.state.start;
|
6103 | let node;
|
6104 |
|
6105 | switch (this.state.type) {
|
6106 | case types._super:
|
6107 | if (!this.state.inMethod && !this.state.inClassProperty && !this.options.allowSuperOutsideMethod) {
|
6108 | this.raise(this.state.start, "super is only allowed in object methods and classes");
|
6109 | }
|
6110 |
|
6111 | node = this.startNode();
|
6112 | this.next();
|
6113 |
|
6114 | if (!this.match(types.parenL) && !this.match(types.bracketL) && !this.match(types.dot)) {
|
6115 | this.unexpected();
|
6116 | }
|
6117 |
|
6118 | if (this.match(types.parenL) && this.state.inMethod !== "constructor" && !this.options.allowSuperOutsideMethod) {
|
6119 | this.raise(node.start, "super() is only valid inside a class constructor. " + "Make sure the method name is spelled exactly as 'constructor'.");
|
6120 | }
|
6121 |
|
6122 | return this.finishNode(node, "Super");
|
6123 |
|
6124 | case types._import:
|
6125 | if (this.lookahead().type === types.dot) {
|
6126 | return this.parseImportMetaProperty();
|
6127 | }
|
6128 |
|
6129 | this.expectPlugin("dynamicImport");
|
6130 | node = this.startNode();
|
6131 | this.next();
|
6132 |
|
6133 | if (!this.match(types.parenL)) {
|
6134 | this.unexpected(null, types.parenL);
|
6135 | }
|
6136 |
|
6137 | return this.finishNode(node, "Import");
|
6138 |
|
6139 | case types._this:
|
6140 | node = this.startNode();
|
6141 | this.next();
|
6142 | return this.finishNode(node, "ThisExpression");
|
6143 |
|
6144 | case types.name:
|
6145 | {
|
6146 | node = this.startNode();
|
6147 | const containsEsc = this.state.containsEsc;
|
6148 | const id = this.parseIdentifier();
|
6149 |
|
6150 | if (!containsEsc && id.name === "async" && this.match(types._function) && !this.canInsertSemicolon()) {
|
6151 | this.next();
|
6152 | return this.parseFunction(node, false, false, true);
|
6153 | } else if (canBeArrow && id.name === "async" && this.match(types.name) && !this.canInsertSemicolon()) {
|
6154 | const oldInAsync = this.state.inAsync;
|
6155 | this.state.inAsync = true;
|
6156 | const params = [this.parseIdentifier()];
|
6157 | this.expect(types.arrow);
|
6158 | this.parseArrowExpression(node, params, true);
|
6159 | this.state.inAsync = oldInAsync;
|
6160 | return node;
|
6161 | }
|
6162 |
|
6163 | if (canBeArrow && this.match(types.arrow) && !this.canInsertSemicolon()) {
|
6164 | this.next();
|
6165 | this.parseArrowExpression(node, [id], false);
|
6166 | return node;
|
6167 | }
|
6168 |
|
6169 | return id;
|
6170 | }
|
6171 |
|
6172 | case types._do:
|
6173 | {
|
6174 | this.expectPlugin("doExpressions");
|
6175 | const node = this.startNode();
|
6176 | this.next();
|
6177 | const oldInFunction = this.state.inFunction;
|
6178 | const oldLabels = this.state.labels;
|
6179 | this.state.labels = [];
|
6180 | this.state.inFunction = false;
|
6181 | node.body = this.parseBlock(false);
|
6182 | this.state.inFunction = oldInFunction;
|
6183 | this.state.labels = oldLabels;
|
6184 | return this.finishNode(node, "DoExpression");
|
6185 | }
|
6186 |
|
6187 | case types.regexp:
|
6188 | {
|
6189 | const value = this.state.value;
|
6190 | node = this.parseLiteral(value.value, "RegExpLiteral");
|
6191 | node.pattern = value.pattern;
|
6192 | node.flags = value.flags;
|
6193 | return node;
|
6194 | }
|
6195 |
|
6196 | case types.num:
|
6197 | return this.parseLiteral(this.state.value, "NumericLiteral");
|
6198 |
|
6199 | case types.bigint:
|
6200 | return this.parseLiteral(this.state.value, "BigIntLiteral");
|
6201 |
|
6202 | case types.string:
|
6203 | return this.parseLiteral(this.state.value, "StringLiteral");
|
6204 |
|
6205 | case types._null:
|
6206 | node = this.startNode();
|
6207 | this.next();
|
6208 | return this.finishNode(node, "NullLiteral");
|
6209 |
|
6210 | case types._true:
|
6211 | case types._false:
|
6212 | return this.parseBooleanLiteral();
|
6213 |
|
6214 | case types.parenL:
|
6215 | return this.parseParenAndDistinguishExpression(canBeArrow);
|
6216 |
|
6217 | case types.bracketL:
|
6218 | node = this.startNode();
|
6219 | this.next();
|
6220 | node.elements = this.parseExprList(types.bracketR, true, refShorthandDefaultPos);
|
6221 |
|
6222 | if (!this.state.maybeInArrowParameters) {
|
6223 | this.toReferencedList(node.elements);
|
6224 | }
|
6225 |
|
6226 | return this.finishNode(node, "ArrayExpression");
|
6227 |
|
6228 | case types.braceL:
|
6229 | return this.parseObj(false, refShorthandDefaultPos);
|
6230 |
|
6231 | case types._function:
|
6232 | return this.parseFunctionExpression();
|
6233 |
|
6234 | case types.at:
|
6235 | this.parseDecorators();
|
6236 |
|
6237 | case types._class:
|
6238 | node = this.startNode();
|
6239 | this.takeDecorators(node);
|
6240 | return this.parseClass(node, false);
|
6241 |
|
6242 | case types._new:
|
6243 | return this.parseNew();
|
6244 |
|
6245 | case types.backQuote:
|
6246 | return this.parseTemplate(false);
|
6247 |
|
6248 | case types.doubleColon:
|
6249 | {
|
6250 | node = this.startNode();
|
6251 | this.next();
|
6252 | node.object = null;
|
6253 | const callee = node.callee = this.parseNoCallExpr();
|
6254 |
|
6255 | if (callee.type === "MemberExpression") {
|
6256 | return this.finishNode(node, "BindExpression");
|
6257 | } else {
|
6258 | throw this.raise(callee.start, "Binding should be performed on object property.");
|
6259 | }
|
6260 | }
|
6261 |
|
6262 | case types.hash:
|
6263 | {
|
6264 | if (this.state.inPipeline) {
|
6265 | node = this.startNode();
|
6266 |
|
6267 | if (this.getPluginOption("pipelineOperator", "proposal") !== "smart") {
|
6268 | this.raise(node.start, "Primary Topic Reference found but pipelineOperator not passed 'smart' for 'proposal' option.");
|
6269 | }
|
6270 |
|
6271 | this.next();
|
6272 |
|
6273 | if (this.primaryTopicReferenceIsAllowedInCurrentTopicContext()) {
|
6274 | this.registerTopicReference();
|
6275 | return this.finishNode(node, "PipelinePrimaryTopicReference");
|
6276 | } else {
|
6277 | throw this.raise(node.start, `Topic reference was used in a lexical context without topic binding`);
|
6278 | }
|
6279 | }
|
6280 | }
|
6281 |
|
6282 | default:
|
6283 | throw this.unexpected();
|
6284 | }
|
6285 | }
|
6286 |
|
6287 | parseBooleanLiteral() {
|
6288 | const node = this.startNode();
|
6289 | node.value = this.match(types._true);
|
6290 | this.next();
|
6291 | return this.finishNode(node, "BooleanLiteral");
|
6292 | }
|
6293 |
|
6294 | parseMaybePrivateName() {
|
6295 | const isPrivate = this.match(types.hash);
|
6296 |
|
6297 | if (isPrivate) {
|
6298 | this.expectOnePlugin(["classPrivateProperties", "classPrivateMethods"]);
|
6299 | const node = this.startNode();
|
6300 | const columnHashEnd = this.state.end;
|
6301 | this.next();
|
6302 | const columnIdentifierStart = this.state.start;
|
6303 | const spacesBetweenHashAndIdentifier = columnIdentifierStart - columnHashEnd;
|
6304 |
|
6305 | if (spacesBetweenHashAndIdentifier != 0) {
|
6306 | this.raise(columnIdentifierStart, "Unexpected space between # and identifier");
|
6307 | }
|
6308 |
|
6309 | node.id = this.parseIdentifier(true);
|
6310 | return this.finishNode(node, "PrivateName");
|
6311 | } else {
|
6312 | return this.parseIdentifier(true);
|
6313 | }
|
6314 | }
|
6315 |
|
6316 | parseFunctionExpression() {
|
6317 | const node = this.startNode();
|
6318 | let meta = this.startNode();
|
6319 | this.next();
|
6320 | meta = this.createIdentifier(meta, "function");
|
6321 |
|
6322 | if (this.state.inGenerator && this.eat(types.dot)) {
|
6323 | return this.parseMetaProperty(node, meta, "sent");
|
6324 | }
|
6325 |
|
6326 | return this.parseFunction(node, false);
|
6327 | }
|
6328 |
|
6329 | parseMetaProperty(node, meta, propertyName) {
|
6330 | node.meta = meta;
|
6331 |
|
6332 | if (meta.name === "function" && propertyName === "sent") {
|
6333 | if (this.isContextual(propertyName)) {
|
6334 | this.expectPlugin("functionSent");
|
6335 | } else if (!this.hasPlugin("functionSent")) {
|
6336 | this.unexpected();
|
6337 | }
|
6338 | }
|
6339 |
|
6340 | const containsEsc = this.state.containsEsc;
|
6341 | node.property = this.parseIdentifier(true);
|
6342 |
|
6343 | if (node.property.name !== propertyName || containsEsc) {
|
6344 | this.raise(node.property.start, `The only valid meta property for ${meta.name} is ${meta.name}.${propertyName}`);
|
6345 | }
|
6346 |
|
6347 | return this.finishNode(node, "MetaProperty");
|
6348 | }
|
6349 |
|
6350 | parseImportMetaProperty() {
|
6351 | const node = this.startNode();
|
6352 | const id = this.parseIdentifier(true);
|
6353 | this.expect(types.dot);
|
6354 |
|
6355 | if (id.name === "import") {
|
6356 | if (this.isContextual("meta")) {
|
6357 | this.expectPlugin("importMeta");
|
6358 | } else if (!this.hasPlugin("importMeta")) {
|
6359 | this.raise(id.start, `Dynamic imports require a parameter: import('a.js')`);
|
6360 | }
|
6361 | }
|
6362 |
|
6363 | if (!this.inModule) {
|
6364 | this.raise(id.start, `import.meta may appear only with 'sourceType: "module"'`, {
|
6365 | code: "BABEL_PARSER_SOURCETYPE_MODULE_REQUIRED"
|
6366 | });
|
6367 | }
|
6368 |
|
6369 | this.sawUnambiguousESM = true;
|
6370 | return this.parseMetaProperty(node, id, "meta");
|
6371 | }
|
6372 |
|
6373 | parseLiteral(value, type, startPos, startLoc) {
|
6374 | startPos = startPos || this.state.start;
|
6375 | startLoc = startLoc || this.state.startLoc;
|
6376 | const node = this.startNodeAt(startPos, startLoc);
|
6377 | this.addExtra(node, "rawValue", value);
|
6378 | this.addExtra(node, "raw", this.state.input.slice(startPos, this.state.end));
|
6379 | node.value = value;
|
6380 | this.next();
|
6381 | return this.finishNode(node, type);
|
6382 | }
|
6383 |
|
6384 | parseParenExpression() {
|
6385 | this.expect(types.parenL);
|
6386 | const val = this.parseExpression();
|
6387 | this.expect(types.parenR);
|
6388 | return val;
|
6389 | }
|
6390 |
|
6391 | parseParenAndDistinguishExpression(canBeArrow) {
|
6392 | const startPos = this.state.start;
|
6393 | const startLoc = this.state.startLoc;
|
6394 | let val;
|
6395 | this.expect(types.parenL);
|
6396 | const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
|
6397 | const oldYieldPos = this.state.yieldPos;
|
6398 | const oldAwaitPos = this.state.awaitPos;
|
6399 | this.state.maybeInArrowParameters = true;
|
6400 | this.state.yieldPos = 0;
|
6401 | this.state.awaitPos = 0;
|
6402 | const innerStartPos = this.state.start;
|
6403 | const innerStartLoc = this.state.startLoc;
|
6404 | const exprList = [];
|
6405 | const refShorthandDefaultPos = {
|
6406 | start: 0
|
6407 | };
|
6408 | const refNeedsArrowPos = {
|
6409 | start: 0
|
6410 | };
|
6411 | let first = true;
|
6412 | let spreadStart;
|
6413 | let optionalCommaStart;
|
6414 |
|
6415 | while (!this.match(types.parenR)) {
|
6416 | if (first) {
|
6417 | first = false;
|
6418 | } else {
|
6419 | this.expect(types.comma, refNeedsArrowPos.start || null);
|
6420 |
|
6421 | if (this.match(types.parenR)) {
|
6422 | optionalCommaStart = this.state.start;
|
6423 | break;
|
6424 | }
|
6425 | }
|
6426 |
|
6427 | if (this.match(types.ellipsis)) {
|
6428 | const spreadNodeStartPos = this.state.start;
|
6429 | const spreadNodeStartLoc = this.state.startLoc;
|
6430 | spreadStart = this.state.start;
|
6431 | exprList.push(this.parseParenItem(this.parseRest(), spreadNodeStartPos, spreadNodeStartLoc));
|
6432 | this.checkCommaAfterRest(types.parenR, "parameter");
|
6433 | break;
|
6434 | } else {
|
6435 | exprList.push(this.parseMaybeAssign(false, refShorthandDefaultPos, this.parseParenItem, refNeedsArrowPos));
|
6436 | }
|
6437 | }
|
6438 |
|
6439 | const innerEndPos = this.state.start;
|
6440 | const innerEndLoc = this.state.startLoc;
|
6441 | this.expect(types.parenR);
|
6442 | this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
|
6443 | let arrowNode = this.startNodeAt(startPos, startLoc);
|
6444 |
|
6445 | if (canBeArrow && this.shouldParseArrow() && (arrowNode = this.parseArrow(arrowNode))) {
|
6446 | this.checkYieldAwaitInDefaultParams();
|
6447 | this.state.yieldPos = oldYieldPos;
|
6448 | this.state.awaitPos = oldAwaitPos;
|
6449 |
|
6450 | for (let _i = 0; _i < exprList.length; _i++) {
|
6451 | const param = exprList[_i];
|
6452 |
|
6453 | if (param.extra && param.extra.parenthesized) {
|
6454 | this.unexpected(param.extra.parenStart);
|
6455 | }
|
6456 | }
|
6457 |
|
6458 | this.parseArrowExpression(arrowNode, exprList, false);
|
6459 | return arrowNode;
|
6460 | }
|
6461 |
|
6462 | this.state.yieldPos = oldYieldPos || this.state.yieldPos;
|
6463 | this.state.awaitPos = oldAwaitPos || this.state.awaitPos;
|
6464 |
|
6465 | if (!exprList.length) {
|
6466 | this.unexpected(this.state.lastTokStart);
|
6467 | }
|
6468 |
|
6469 | if (optionalCommaStart) this.unexpected(optionalCommaStart);
|
6470 | if (spreadStart) this.unexpected(spreadStart);
|
6471 |
|
6472 | if (refShorthandDefaultPos.start) {
|
6473 | this.unexpected(refShorthandDefaultPos.start);
|
6474 | }
|
6475 |
|
6476 | if (refNeedsArrowPos.start) this.unexpected(refNeedsArrowPos.start);
|
6477 | this.toReferencedListDeep(exprList, true);
|
6478 |
|
6479 | if (exprList.length > 1) {
|
6480 | val = this.startNodeAt(innerStartPos, innerStartLoc);
|
6481 | val.expressions = exprList;
|
6482 | this.finishNodeAt(val, "SequenceExpression", innerEndPos, innerEndLoc);
|
6483 | } else {
|
6484 | val = exprList[0];
|
6485 | }
|
6486 |
|
6487 | this.addExtra(val, "parenthesized", true);
|
6488 | this.addExtra(val, "parenStart", startPos);
|
6489 | return val;
|
6490 | }
|
6491 |
|
6492 | shouldParseArrow() {
|
6493 | return !this.canInsertSemicolon();
|
6494 | }
|
6495 |
|
6496 | parseArrow(node) {
|
6497 | if (this.eat(types.arrow)) {
|
6498 | return node;
|
6499 | }
|
6500 | }
|
6501 |
|
6502 | parseParenItem(node, startPos, startLoc) {
|
6503 | return node;
|
6504 | }
|
6505 |
|
6506 | parseNew() {
|
6507 | const node = this.startNode();
|
6508 | const meta = this.parseIdentifier(true);
|
6509 |
|
6510 | if (this.eat(types.dot)) {
|
6511 | const metaProp = this.parseMetaProperty(node, meta, "target");
|
6512 |
|
6513 | if (!this.state.inFunction && !this.state.inClassProperty) {
|
6514 | let error = "new.target can only be used in functions";
|
6515 |
|
6516 | if (this.hasPlugin("classProperties")) {
|
6517 | error += " or class properties";
|
6518 | }
|
6519 |
|
6520 | this.raise(metaProp.start, error);
|
6521 | }
|
6522 |
|
6523 | return metaProp;
|
6524 | }
|
6525 |
|
6526 | node.callee = this.parseNoCallExpr();
|
6527 |
|
6528 | if (node.callee.type === "Import") {
|
6529 | this.raise(node.callee.start, "Cannot use new with import(...)");
|
6530 | } else if (node.callee.type === "OptionalMemberExpression" || node.callee.type === "OptionalCallExpression") {
|
6531 | this.raise(this.state.lastTokEnd, "constructors in/after an Optional Chain are not allowed");
|
6532 | } else if (this.eat(types.questionDot)) {
|
6533 | this.raise(this.state.start, "constructors in/after an Optional Chain are not allowed");
|
6534 | }
|
6535 |
|
6536 | this.parseNewArguments(node);
|
6537 | return this.finishNode(node, "NewExpression");
|
6538 | }
|
6539 |
|
6540 | parseNewArguments(node) {
|
6541 | if (this.eat(types.parenL)) {
|
6542 | const args = this.parseExprList(types.parenR);
|
6543 | this.toReferencedList(args);
|
6544 | node.arguments = args;
|
6545 | } else {
|
6546 | node.arguments = [];
|
6547 | }
|
6548 | }
|
6549 |
|
6550 | parseTemplateElement(isTagged) {
|
6551 | const elem = this.startNode();
|
6552 |
|
6553 | if (this.state.value === null) {
|
6554 | if (!isTagged) {
|
6555 | this.raise(this.state.invalidTemplateEscapePosition || 0, "Invalid escape sequence in template");
|
6556 | } else {
|
6557 | this.state.invalidTemplateEscapePosition = null;
|
6558 | }
|
6559 | }
|
6560 |
|
6561 | elem.value = {
|
6562 | raw: this.state.input.slice(this.state.start, this.state.end).replace(/\r\n?/g, "\n"),
|
6563 | cooked: this.state.value
|
6564 | };
|
6565 | this.next();
|
6566 | elem.tail = this.match(types.backQuote);
|
6567 | return this.finishNode(elem, "TemplateElement");
|
6568 | }
|
6569 |
|
6570 | parseTemplate(isTagged) {
|
6571 | const node = this.startNode();
|
6572 | this.next();
|
6573 | node.expressions = [];
|
6574 | let curElt = this.parseTemplateElement(isTagged);
|
6575 | node.quasis = [curElt];
|
6576 |
|
6577 | while (!curElt.tail) {
|
6578 | this.expect(types.dollarBraceL);
|
6579 | node.expressions.push(this.parseExpression());
|
6580 | this.expect(types.braceR);
|
6581 | node.quasis.push(curElt = this.parseTemplateElement(isTagged));
|
6582 | }
|
6583 |
|
6584 | this.next();
|
6585 | return this.finishNode(node, "TemplateLiteral");
|
6586 | }
|
6587 |
|
6588 | parseObj(isPattern, refShorthandDefaultPos) {
|
6589 | let decorators = [];
|
6590 | const propHash = Object.create(null);
|
6591 | let first = true;
|
6592 | const node = this.startNode();
|
6593 | node.properties = [];
|
6594 | this.next();
|
6595 |
|
6596 | while (!this.eat(types.braceR)) {
|
6597 | if (first) {
|
6598 | first = false;
|
6599 | } else {
|
6600 | this.expect(types.comma);
|
6601 | if (this.eat(types.braceR)) break;
|
6602 | }
|
6603 |
|
6604 | if (this.match(types.at)) {
|
6605 | if (this.hasPlugin("decorators")) {
|
6606 | this.raise(this.state.start, "Stage 2 decorators disallow object literal property decorators");
|
6607 | } else {
|
6608 | while (this.match(types.at)) {
|
6609 | decorators.push(this.parseDecorator());
|
6610 | }
|
6611 | }
|
6612 | }
|
6613 |
|
6614 | let prop = this.startNode(),
|
6615 | isGenerator = false,
|
6616 | isAsync = false,
|
6617 | startPos,
|
6618 | startLoc;
|
6619 |
|
6620 | if (decorators.length) {
|
6621 | prop.decorators = decorators;
|
6622 | decorators = [];
|
6623 | }
|
6624 |
|
6625 | if (this.match(types.ellipsis)) {
|
6626 | prop = this.parseSpread(isPattern ? {
|
6627 | start: 0
|
6628 | } : undefined);
|
6629 | node.properties.push(prop);
|
6630 |
|
6631 | if (isPattern) {
|
6632 | this.toAssignable(prop, true, "object pattern");
|
6633 | this.checkCommaAfterRest(types.braceR, "property");
|
6634 | this.expect(types.braceR);
|
6635 | break;
|
6636 | }
|
6637 |
|
6638 | continue;
|
6639 | }
|
6640 |
|
6641 | prop.method = false;
|
6642 |
|
6643 | if (isPattern || refShorthandDefaultPos) {
|
6644 | startPos = this.state.start;
|
6645 | startLoc = this.state.startLoc;
|
6646 | }
|
6647 |
|
6648 | if (!isPattern) {
|
6649 | isGenerator = this.eat(types.star);
|
6650 | }
|
6651 |
|
6652 | const containsEsc = this.state.containsEsc;
|
6653 |
|
6654 | if (!isPattern && this.isContextual("async")) {
|
6655 | if (isGenerator) this.unexpected();
|
6656 | const asyncId = this.parseIdentifier();
|
6657 |
|
6658 | if (this.match(types.colon) || this.match(types.parenL) || this.match(types.braceR) || this.match(types.eq) || this.match(types.comma)) {
|
6659 | prop.key = asyncId;
|
6660 | prop.computed = false;
|
6661 | } else {
|
6662 | isAsync = true;
|
6663 | isGenerator = this.eat(types.star);
|
6664 | this.parsePropertyName(prop);
|
6665 | }
|
6666 | } else {
|
6667 | this.parsePropertyName(prop);
|
6668 | }
|
6669 |
|
6670 | this.parseObjPropValue(prop, startPos, startLoc, isGenerator, isAsync, isPattern, refShorthandDefaultPos, containsEsc);
|
6671 | this.checkPropClash(prop, propHash);
|
6672 |
|
6673 | if (prop.shorthand) {
|
6674 | this.addExtra(prop, "shorthand", true);
|
6675 | }
|
6676 |
|
6677 | node.properties.push(prop);
|
6678 | }
|
6679 |
|
6680 | if (decorators.length) {
|
6681 | this.raise(this.state.start, "You have trailing decorators with no property");
|
6682 | }
|
6683 |
|
6684 | return this.finishNode(node, isPattern ? "ObjectPattern" : "ObjectExpression");
|
6685 | }
|
6686 |
|
6687 | isGetterOrSetterMethod(prop, isPattern) {
|
6688 | return !isPattern && !prop.computed && prop.key.type === "Identifier" && (prop.key.name === "get" || prop.key.name === "set") && (this.match(types.string) || this.match(types.num) || this.match(types.bracketL) || this.match(types.name) || !!this.state.type.keyword);
|
6689 | }
|
6690 |
|
6691 | checkGetterSetterParams(method) {
|
6692 | const paramCount = method.kind === "get" ? 0 : 1;
|
6693 | const start = method.start;
|
6694 |
|
6695 | if (method.params.length !== paramCount) {
|
6696 | if (method.kind === "get") {
|
6697 | this.raise(start, "getter must not have any formal parameters");
|
6698 | } else {
|
6699 | this.raise(start, "setter must have exactly one formal parameter");
|
6700 | }
|
6701 | }
|
6702 |
|
6703 | if (method.kind === "set" && method.params[0].type === "RestElement") {
|
6704 | this.raise(start, "setter function argument must not be a rest parameter");
|
6705 | }
|
6706 | }
|
6707 |
|
6708 | parseObjectMethod(prop, isGenerator, isAsync, isPattern, containsEsc) {
|
6709 | if (isAsync || isGenerator || this.match(types.parenL)) {
|
6710 | if (isPattern) this.unexpected();
|
6711 | prop.kind = "method";
|
6712 | prop.method = true;
|
6713 | return this.parseMethod(prop, isGenerator, isAsync, false, "ObjectMethod");
|
6714 | }
|
6715 |
|
6716 | if (!containsEsc && this.isGetterOrSetterMethod(prop, isPattern)) {
|
6717 | if (isGenerator || isAsync) this.unexpected();
|
6718 | prop.kind = prop.key.name;
|
6719 | this.parsePropertyName(prop);
|
6720 | this.parseMethod(prop, false, false, false, "ObjectMethod");
|
6721 | this.checkGetterSetterParams(prop);
|
6722 | return prop;
|
6723 | }
|
6724 | }
|
6725 |
|
6726 | parseObjectProperty(prop, startPos, startLoc, isPattern, refShorthandDefaultPos) {
|
6727 | prop.shorthand = false;
|
6728 |
|
6729 | if (this.eat(types.colon)) {
|
6730 | prop.value = isPattern ? this.parseMaybeDefault(this.state.start, this.state.startLoc) : this.parseMaybeAssign(false, refShorthandDefaultPos);
|
6731 | return this.finishNode(prop, "ObjectProperty");
|
6732 | }
|
6733 |
|
6734 | if (!prop.computed && prop.key.type === "Identifier") {
|
6735 | this.checkReservedWord(prop.key.name, prop.key.start, true, true);
|
6736 |
|
6737 | if (isPattern) {
|
6738 | prop.value = this.parseMaybeDefault(startPos, startLoc, prop.key.__clone());
|
6739 | } else if (this.match(types.eq) && refShorthandDefaultPos) {
|
6740 | if (!refShorthandDefaultPos.start) {
|
6741 | refShorthandDefaultPos.start = this.state.start;
|
6742 | }
|
6743 |
|
6744 | prop.value = this.parseMaybeDefault(startPos, startLoc, prop.key.__clone());
|
6745 | } else {
|
6746 | prop.value = prop.key.__clone();
|
6747 | }
|
6748 |
|
6749 | prop.shorthand = true;
|
6750 | return this.finishNode(prop, "ObjectProperty");
|
6751 | }
|
6752 | }
|
6753 |
|
6754 | parseObjPropValue(prop, startPos, startLoc, isGenerator, isAsync, isPattern, refShorthandDefaultPos, containsEsc) {
|
6755 | const node = this.parseObjectMethod(prop, isGenerator, isAsync, isPattern, containsEsc) || this.parseObjectProperty(prop, startPos, startLoc, isPattern, refShorthandDefaultPos);
|
6756 | if (!node) this.unexpected();
|
6757 | return node;
|
6758 | }
|
6759 |
|
6760 | parsePropertyName(prop) {
|
6761 | if (this.eat(types.bracketL)) {
|
6762 | prop.computed = true;
|
6763 | prop.key = this.parseMaybeAssign();
|
6764 | this.expect(types.bracketR);
|
6765 | } else {
|
6766 | const oldInPropertyName = this.state.inPropertyName;
|
6767 | this.state.inPropertyName = true;
|
6768 | prop.key = this.match(types.num) || this.match(types.string) ? this.parseExprAtom() : this.parseMaybePrivateName();
|
6769 |
|
6770 | if (prop.key.type !== "PrivateName") {
|
6771 | prop.computed = false;
|
6772 | }
|
6773 |
|
6774 | this.state.inPropertyName = oldInPropertyName;
|
6775 | }
|
6776 |
|
6777 | return prop.key;
|
6778 | }
|
6779 |
|
6780 | initFunction(node, isAsync) {
|
6781 | node.id = null;
|
6782 | node.generator = false;
|
6783 | node.async = !!isAsync;
|
6784 | }
|
6785 |
|
6786 | parseMethod(node, isGenerator, isAsync, isConstructor, type) {
|
6787 | const oldInFunc = this.state.inFunction;
|
6788 | const oldInMethod = this.state.inMethod;
|
6789 | const oldInAsync = this.state.inAsync;
|
6790 | const oldInGenerator = this.state.inGenerator;
|
6791 | const oldYieldPos = this.state.yieldPos;
|
6792 | const oldAwaitPos = this.state.awaitPos;
|
6793 | this.state.inFunction = true;
|
6794 | this.state.inMethod = node.kind || true;
|
6795 | this.state.inAsync = isAsync;
|
6796 | this.state.inGenerator = isGenerator;
|
6797 | this.state.yieldPos = 0;
|
6798 | this.state.awaitPos = 0;
|
6799 | this.initFunction(node, isAsync);
|
6800 | node.generator = !!isGenerator;
|
6801 | const allowModifiers = isConstructor;
|
6802 | this.parseFunctionParams(node, allowModifiers);
|
6803 | this.checkYieldAwaitInDefaultParams();
|
6804 | this.parseFunctionBodyAndFinish(node, type);
|
6805 | this.state.inFunction = oldInFunc;
|
6806 | this.state.inMethod = oldInMethod;
|
6807 | this.state.inAsync = oldInAsync;
|
6808 | this.state.inGenerator = oldInGenerator;
|
6809 | this.state.yieldPos = oldYieldPos;
|
6810 | this.state.awaitPos = oldAwaitPos;
|
6811 | return node;
|
6812 | }
|
6813 |
|
6814 | parseArrowExpression(node, params, isAsync) {
|
6815 | this.initFunction(node, isAsync);
|
6816 | const oldInFunc = this.state.inFunction;
|
6817 | const oldInAsync = this.state.inAsync;
|
6818 | const oldInGenerator = this.state.inGenerator;
|
6819 | const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
|
6820 | const oldYieldPos = this.state.yieldPos;
|
6821 | const oldAwaitPos = this.state.awaitPos;
|
6822 | this.state.inFunction = true;
|
6823 | this.state.inAsync = isAsync;
|
6824 | this.state.inGenerator = false;
|
6825 | this.state.maybeInArrowParameters = false;
|
6826 | this.state.yieldPos = 0;
|
6827 | this.state.awaitPos = 0;
|
6828 | if (params) this.setArrowFunctionParameters(node, params);
|
6829 | this.parseFunctionBody(node, true);
|
6830 | this.state.inAsync = oldInAsync;
|
6831 | this.state.inGenerator = oldInGenerator;
|
6832 | this.state.inFunction = oldInFunc;
|
6833 | this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
|
6834 | this.state.yieldPos = oldYieldPos;
|
6835 | this.state.awaitPos = oldAwaitPos;
|
6836 | return this.finishNode(node, "ArrowFunctionExpression");
|
6837 | }
|
6838 |
|
6839 | setArrowFunctionParameters(node, params) {
|
6840 | node.params = this.toAssignableList(params, true, "arrow function parameters");
|
6841 | }
|
6842 |
|
6843 | isStrictBody(node) {
|
6844 | const isBlockStatement = node.body.type === "BlockStatement";
|
6845 |
|
6846 | if (isBlockStatement && node.body.directives.length) {
|
6847 | for (let _i2 = 0, _node$body$directives = node.body.directives; _i2 < _node$body$directives.length; _i2++) {
|
6848 | const directive = _node$body$directives[_i2];
|
6849 |
|
6850 | if (directive.value.value === "use strict") {
|
6851 | return true;
|
6852 | }
|
6853 | }
|
6854 | }
|
6855 |
|
6856 | return false;
|
6857 | }
|
6858 |
|
6859 | parseFunctionBodyAndFinish(node, type, allowExpressionBody) {
|
6860 | this.parseFunctionBody(node, allowExpressionBody);
|
6861 | this.finishNode(node, type);
|
6862 | }
|
6863 |
|
6864 | parseFunctionBody(node, allowExpression) {
|
6865 | const isExpression = allowExpression && !this.match(types.braceL);
|
6866 | const oldInParameters = this.state.inParameters;
|
6867 | this.state.inParameters = false;
|
6868 |
|
6869 | if (isExpression) {
|
6870 | node.body = this.parseMaybeAssign();
|
6871 | } else {
|
6872 | const oldInFunc = this.state.inFunction;
|
6873 | const oldLabels = this.state.labels;
|
6874 | this.state.inFunction = true;
|
6875 | this.state.labels = [];
|
6876 | node.body = this.parseBlock(true);
|
6877 | this.state.inFunction = oldInFunc;
|
6878 | this.state.labels = oldLabels;
|
6879 | }
|
6880 |
|
6881 | this.checkFunctionNameAndParams(node, allowExpression);
|
6882 | this.state.inParameters = oldInParameters;
|
6883 | }
|
6884 |
|
6885 | checkFunctionNameAndParams(node, isArrowFunction) {
|
6886 | const isStrict = this.isStrictBody(node);
|
6887 | const checkLVal = this.state.strict || isStrict || isArrowFunction;
|
6888 | const oldStrict = this.state.strict;
|
6889 | if (isStrict) this.state.strict = isStrict;
|
6890 |
|
6891 | if (checkLVal) {
|
6892 | const nameHash = Object.create(null);
|
6893 |
|
6894 | if (node.id) {
|
6895 | this.checkLVal(node.id, true, undefined, "function name");
|
6896 | }
|
6897 |
|
6898 | for (let _i3 = 0, _node$params = node.params; _i3 < _node$params.length; _i3++) {
|
6899 | const param = _node$params[_i3];
|
6900 |
|
6901 | if (isStrict && param.type !== "Identifier") {
|
6902 | this.raise(param.start, "Non-simple parameter in strict mode");
|
6903 | }
|
6904 |
|
6905 | this.checkLVal(param, true, nameHash, "function parameter list");
|
6906 | }
|
6907 | }
|
6908 |
|
6909 | this.state.strict = oldStrict;
|
6910 | }
|
6911 |
|
6912 | parseExprList(close, allowEmpty, refShorthandDefaultPos) {
|
6913 | const elts = [];
|
6914 | let first = true;
|
6915 |
|
6916 | while (!this.eat(close)) {
|
6917 | if (first) {
|
6918 | first = false;
|
6919 | } else {
|
6920 | this.expect(types.comma);
|
6921 | if (this.eat(close)) break;
|
6922 | }
|
6923 |
|
6924 | elts.push(this.parseExprListItem(allowEmpty, refShorthandDefaultPos));
|
6925 | }
|
6926 |
|
6927 | return elts;
|
6928 | }
|
6929 |
|
6930 | parseExprListItem(allowEmpty, refShorthandDefaultPos, refNeedsArrowPos) {
|
6931 | let elt;
|
6932 |
|
6933 | if (allowEmpty && this.match(types.comma)) {
|
6934 | elt = null;
|
6935 | } else if (this.match(types.ellipsis)) {
|
6936 | const spreadNodeStartPos = this.state.start;
|
6937 | const spreadNodeStartLoc = this.state.startLoc;
|
6938 | elt = this.parseParenItem(this.parseSpread(refShorthandDefaultPos, refNeedsArrowPos), spreadNodeStartPos, spreadNodeStartLoc);
|
6939 | } else {
|
6940 | elt = this.parseMaybeAssign(false, refShorthandDefaultPos, this.parseParenItem, refNeedsArrowPos);
|
6941 | }
|
6942 |
|
6943 | return elt;
|
6944 | }
|
6945 |
|
6946 | parseIdentifier(liberal) {
|
6947 | const node = this.startNode();
|
6948 | const name = this.parseIdentifierName(node.start, liberal);
|
6949 | return this.createIdentifier(node, name);
|
6950 | }
|
6951 |
|
6952 | createIdentifier(node, name) {
|
6953 | node.name = name;
|
6954 | node.loc.identifierName = name;
|
6955 | return this.finishNode(node, "Identifier");
|
6956 | }
|
6957 |
|
6958 | parseIdentifierName(pos, liberal) {
|
6959 | let name;
|
6960 |
|
6961 | if (this.match(types.name)) {
|
6962 | name = this.state.value;
|
6963 | } else if (this.state.type.keyword) {
|
6964 | name = this.state.type.keyword;
|
6965 |
|
6966 | if ((name === "class" || name === "function") && (this.state.lastTokEnd !== this.state.lastTokStart + 1 || this.state.input.charCodeAt(this.state.lastTokStart) !== 46)) {
|
6967 | this.state.context.pop();
|
6968 | }
|
6969 | } else {
|
6970 | throw this.unexpected();
|
6971 | }
|
6972 |
|
6973 | if (!liberal) {
|
6974 | this.checkReservedWord(name, this.state.start, !!this.state.type.keyword, false);
|
6975 | }
|
6976 |
|
6977 | this.next();
|
6978 | return name;
|
6979 | }
|
6980 |
|
6981 | checkReservedWord(word, startLoc, checkKeywords, isBinding) {
|
6982 | const state = this.state;
|
6983 |
|
6984 | if (state.inGenerator && word === "yield") {
|
6985 | this.raise(startLoc, "Can not use 'yield' as identifier inside a generator");
|
6986 | }
|
6987 |
|
6988 | if (state.inAsync && word === "await") {
|
6989 | this.raise(startLoc, "Can not use 'await' as identifier inside an async function");
|
6990 | }
|
6991 |
|
6992 | if (state.inClassProperty && word === "arguments") {
|
6993 | this.raise(startLoc, "'arguments' is not allowed in class field initializer");
|
6994 | }
|
6995 |
|
6996 | if (checkKeywords && isKeyword(word)) {
|
6997 | this.raise(startLoc, `Unexpected keyword '${word}'`);
|
6998 | }
|
6999 |
|
7000 | const reservedTest = !state.strict ? isReservedWord : isBinding ? isStrictBindReservedWord : isStrictReservedWord;
|
7001 |
|
7002 | if (reservedTest(word, this.inModule)) {
|
7003 | if (!state.inAsync && word === "await") {
|
7004 | this.raise(startLoc, "Can not use keyword 'await' outside an async function");
|
7005 | }
|
7006 |
|
7007 | this.raise(startLoc, `Unexpected reserved word '${word}'`);
|
7008 | }
|
7009 | }
|
7010 |
|
7011 | parseAwait() {
|
7012 | if (!this.state.awaitPos) {
|
7013 | this.state.awaitPos = this.state.start;
|
7014 | }
|
7015 |
|
7016 | const node = this.startNode();
|
7017 | this.next();
|
7018 |
|
7019 | if (this.state.inParameters) {
|
7020 | this.raise(node.start, "await is not allowed in async function parameters");
|
7021 | }
|
7022 |
|
7023 | if (this.match(types.star)) {
|
7024 | this.raise(node.start, "await* has been removed from the async functions proposal. Use Promise.all() instead.");
|
7025 | }
|
7026 |
|
7027 | node.argument = this.parseMaybeUnary();
|
7028 | return this.finishNode(node, "AwaitExpression");
|
7029 | }
|
7030 |
|
7031 | parseYield(noIn) {
|
7032 | if (!this.state.yieldPos) {
|
7033 | this.state.yieldPos = this.state.start;
|
7034 | }
|
7035 |
|
7036 | const node = this.startNode();
|
7037 |
|
7038 | if (this.state.inParameters) {
|
7039 | this.raise(node.start, "yield is not allowed in generator parameters");
|
7040 | }
|
7041 |
|
7042 | this.next();
|
7043 |
|
7044 | if (this.match(types.semi) || !this.match(types.star) && !this.state.type.startsExpr || this.canInsertSemicolon()) {
|
7045 | node.delegate = false;
|
7046 | node.argument = null;
|
7047 | } else {
|
7048 | node.delegate = this.eat(types.star);
|
7049 | node.argument = this.parseMaybeAssign(noIn);
|
7050 | }
|
7051 |
|
7052 | return this.finishNode(node, "YieldExpression");
|
7053 | }
|
7054 |
|
7055 | checkPipelineAtInfixOperator(left, leftStartPos) {
|
7056 | if (this.getPluginOption("pipelineOperator", "proposal") === "smart") {
|
7057 | if (left.type === "SequenceExpression") {
|
7058 | throw this.raise(leftStartPos, `Pipeline head should not be a comma-separated sequence expression`);
|
7059 | }
|
7060 | }
|
7061 | }
|
7062 |
|
7063 | parseSmartPipelineBody(childExpression, startPos, startLoc) {
|
7064 | const pipelineStyle = this.checkSmartPipelineBodyStyle(childExpression);
|
7065 | this.checkSmartPipelineBodyEarlyErrors(childExpression, pipelineStyle, startPos);
|
7066 | return this.parseSmartPipelineBodyInStyle(childExpression, pipelineStyle, startPos, startLoc);
|
7067 | }
|
7068 |
|
7069 | checkSmartPipelineBodyEarlyErrors(childExpression, pipelineStyle, startPos) {
|
7070 | if (this.match(types.arrow)) {
|
7071 | throw this.raise(this.state.start, `Unexpected arrow "=>" after pipeline body; arrow function in pipeline body must be parenthesized`);
|
7072 | } else if (pipelineStyle === "PipelineTopicExpression" && childExpression.type === "SequenceExpression") {
|
7073 | throw this.raise(startPos, `Pipeline body may not be a comma-separated sequence expression`);
|
7074 | }
|
7075 | }
|
7076 |
|
7077 | parseSmartPipelineBodyInStyle(childExpression, pipelineStyle, startPos, startLoc) {
|
7078 | const bodyNode = this.startNodeAt(startPos, startLoc);
|
7079 |
|
7080 | switch (pipelineStyle) {
|
7081 | case "PipelineBareFunction":
|
7082 | bodyNode.callee = childExpression;
|
7083 | break;
|
7084 |
|
7085 | case "PipelineBareConstructor":
|
7086 | bodyNode.callee = childExpression.callee;
|
7087 | break;
|
7088 |
|
7089 | case "PipelineBareAwaitedFunction":
|
7090 | bodyNode.callee = childExpression.argument;
|
7091 | break;
|
7092 |
|
7093 | case "PipelineTopicExpression":
|
7094 | if (!this.topicReferenceWasUsedInCurrentTopicContext()) {
|
7095 | throw this.raise(startPos, `Pipeline is in topic style but does not use topic reference`);
|
7096 | }
|
7097 |
|
7098 | bodyNode.expression = childExpression;
|
7099 | break;
|
7100 |
|
7101 | default:
|
7102 | throw this.raise(startPos, `Unknown pipeline style ${pipelineStyle}`);
|
7103 | }
|
7104 |
|
7105 | return this.finishNode(bodyNode, pipelineStyle);
|
7106 | }
|
7107 |
|
7108 | checkSmartPipelineBodyStyle(expression) {
|
7109 | switch (expression.type) {
|
7110 | default:
|
7111 | return this.isSimpleReference(expression) ? "PipelineBareFunction" : "PipelineTopicExpression";
|
7112 | }
|
7113 | }
|
7114 |
|
7115 | isSimpleReference(expression) {
|
7116 | switch (expression.type) {
|
7117 | case "MemberExpression":
|
7118 | return !expression.computed && this.isSimpleReference(expression.object);
|
7119 |
|
7120 | case "Identifier":
|
7121 | return true;
|
7122 |
|
7123 | default:
|
7124 | return false;
|
7125 | }
|
7126 | }
|
7127 |
|
7128 | withTopicPermittingContext(callback) {
|
7129 | const outerContextTopicState = this.state.topicContext;
|
7130 | this.state.topicContext = {
|
7131 | maxNumOfResolvableTopics: 1,
|
7132 | maxTopicIndex: null
|
7133 | };
|
7134 |
|
7135 | try {
|
7136 | return callback();
|
7137 | } finally {
|
7138 | this.state.topicContext = outerContextTopicState;
|
7139 | }
|
7140 | }
|
7141 |
|
7142 | withTopicForbiddingContext(callback) {
|
7143 | const outerContextTopicState = this.state.topicContext;
|
7144 | this.state.topicContext = {
|
7145 | maxNumOfResolvableTopics: 0,
|
7146 | maxTopicIndex: null
|
7147 | };
|
7148 |
|
7149 | try {
|
7150 | return callback();
|
7151 | } finally {
|
7152 | this.state.topicContext = outerContextTopicState;
|
7153 | }
|
7154 | }
|
7155 |
|
7156 | registerTopicReference() {
|
7157 | this.state.topicContext.maxTopicIndex = 0;
|
7158 | }
|
7159 |
|
7160 | primaryTopicReferenceIsAllowedInCurrentTopicContext() {
|
7161 | return this.state.topicContext.maxNumOfResolvableTopics >= 1;
|
7162 | }
|
7163 |
|
7164 | topicReferenceWasUsedInCurrentTopicContext() {
|
7165 | return this.state.topicContext.maxTopicIndex != null && this.state.topicContext.maxTopicIndex >= 0;
|
7166 | }
|
7167 |
|
7168 | }
|
7169 |
|
7170 | const empty = [];
|
7171 | const loopLabel = {
|
7172 | kind: "loop"
|
7173 | };
|
7174 | const switchLabel = {
|
7175 | kind: "switch"
|
7176 | };
|
7177 | class StatementParser extends ExpressionParser {
|
7178 | parseTopLevel(file, program) {
|
7179 | program.sourceType = this.options.sourceType;
|
7180 | program.interpreter = this.parseInterpreterDirective();
|
7181 | this.parseBlockBody(program, true, true, types.eof);
|
7182 | file.program = this.finishNode(program, "Program");
|
7183 | file.comments = this.state.comments;
|
7184 | if (this.options.tokens) file.tokens = this.state.tokens;
|
7185 | return this.finishNode(file, "File");
|
7186 | }
|
7187 |
|
7188 | stmtToDirective(stmt) {
|
7189 | const expr = stmt.expression;
|
7190 | const directiveLiteral = this.startNodeAt(expr.start, expr.loc.start);
|
7191 | const directive = this.startNodeAt(stmt.start, stmt.loc.start);
|
7192 | const raw = this.state.input.slice(expr.start, expr.end);
|
7193 | const val = directiveLiteral.value = raw.slice(1, -1);
|
7194 | this.addExtra(directiveLiteral, "raw", raw);
|
7195 | this.addExtra(directiveLiteral, "rawValue", val);
|
7196 | directive.value = this.finishNodeAt(directiveLiteral, "DirectiveLiteral", expr.end, expr.loc.end);
|
7197 | return this.finishNodeAt(directive, "Directive", stmt.end, stmt.loc.end);
|
7198 | }
|
7199 |
|
7200 | parseInterpreterDirective() {
|
7201 | if (!this.match(types.interpreterDirective)) {
|
7202 | return null;
|
7203 | }
|
7204 |
|
7205 | const node = this.startNode();
|
7206 | node.value = this.state.value;
|
7207 | this.next();
|
7208 | return this.finishNode(node, "InterpreterDirective");
|
7209 | }
|
7210 |
|
7211 | isLet(context) {
|
7212 | if (!this.isContextual("let")) {
|
7213 | return false;
|
7214 | }
|
7215 |
|
7216 | skipWhiteSpace.lastIndex = this.state.pos;
|
7217 | const skip = skipWhiteSpace.exec(this.state.input);
|
7218 | const next = this.state.pos + skip[0].length;
|
7219 | const nextCh = this.state.input.charCodeAt(next);
|
7220 | if (nextCh === 91) return true;
|
7221 | if (context) return false;
|
7222 | if (nextCh === 123) return true;
|
7223 |
|
7224 | if (isIdentifierStart(nextCh)) {
|
7225 | let pos = next + 1;
|
7226 |
|
7227 | while (isIdentifierChar(this.state.input.charCodeAt(pos))) {
|
7228 | ++pos;
|
7229 | }
|
7230 |
|
7231 | const ident = this.state.input.slice(next, pos);
|
7232 | if (!keywordRelationalOperator.test(ident)) return true;
|
7233 | }
|
7234 |
|
7235 | return false;
|
7236 | }
|
7237 |
|
7238 | parseStatement(context, topLevel) {
|
7239 | if (this.match(types.at)) {
|
7240 | this.parseDecorators(true);
|
7241 | }
|
7242 |
|
7243 | return this.parseStatementContent(context, topLevel);
|
7244 | }
|
7245 |
|
7246 | parseStatementContent(context, topLevel) {
|
7247 | let starttype = this.state.type;
|
7248 | const node = this.startNode();
|
7249 | let kind;
|
7250 |
|
7251 | if (this.isLet(context)) {
|
7252 | starttype = types._var;
|
7253 | kind = "let";
|
7254 | }
|
7255 |
|
7256 | switch (starttype) {
|
7257 | case types._break:
|
7258 | case types._continue:
|
7259 | return this.parseBreakContinueStatement(node, starttype.keyword);
|
7260 |
|
7261 | case types._debugger:
|
7262 | return this.parseDebuggerStatement(node);
|
7263 |
|
7264 | case types._do:
|
7265 | return this.parseDoStatement(node);
|
7266 |
|
7267 | case types._for:
|
7268 | return this.parseForStatement(node);
|
7269 |
|
7270 | case types._function:
|
7271 | {
|
7272 | if (this.lookahead().type === types.dot) break;
|
7273 |
|
7274 | if (context && (this.state.strict || context !== "if" && context !== "label")) {
|
7275 | this.raise(this.state.start, "Function declaration not allowed in this context");
|
7276 | }
|
7277 |
|
7278 | const result = this.parseFunctionStatement(node);
|
7279 |
|
7280 | if (context && result.generator) {
|
7281 | this.unexpected(node.start);
|
7282 | }
|
7283 |
|
7284 | return result;
|
7285 | }
|
7286 |
|
7287 | case types._class:
|
7288 | if (context) this.unexpected();
|
7289 | return this.parseClass(node, true);
|
7290 |
|
7291 | case types._if:
|
7292 | return this.parseIfStatement(node);
|
7293 |
|
7294 | case types._return:
|
7295 | return this.parseReturnStatement(node);
|
7296 |
|
7297 | case types._switch:
|
7298 | return this.parseSwitchStatement(node);
|
7299 |
|
7300 | case types._throw:
|
7301 | return this.parseThrowStatement(node);
|
7302 |
|
7303 | case types._try:
|
7304 | return this.parseTryStatement(node);
|
7305 |
|
7306 | case types._const:
|
7307 | case types._var:
|
7308 | kind = kind || this.state.value;
|
7309 | if (context && kind !== "var") this.unexpected();
|
7310 | return this.parseVarStatement(node, kind);
|
7311 |
|
7312 | case types._while:
|
7313 | return this.parseWhileStatement(node);
|
7314 |
|
7315 | case types._with:
|
7316 | return this.parseWithStatement(node);
|
7317 |
|
7318 | case types.braceL:
|
7319 | return this.parseBlock();
|
7320 |
|
7321 | case types.semi:
|
7322 | return this.parseEmptyStatement(node);
|
7323 |
|
7324 | case types._export:
|
7325 | case types._import:
|
7326 | {
|
7327 | const nextToken = this.lookahead();
|
7328 |
|
7329 | if (nextToken.type === types.parenL || nextToken.type === types.dot) {
|
7330 | break;
|
7331 | }
|
7332 |
|
7333 | if (!this.options.allowImportExportEverywhere && !topLevel) {
|
7334 | this.raise(this.state.start, "'import' and 'export' may only appear at the top level");
|
7335 | }
|
7336 |
|
7337 | this.next();
|
7338 | let result;
|
7339 |
|
7340 | if (starttype === types._import) {
|
7341 | result = this.parseImport(node);
|
7342 |
|
7343 | if (result.type === "ImportDeclaration" && (!result.importKind || result.importKind === "value")) {
|
7344 | this.sawUnambiguousESM = true;
|
7345 | }
|
7346 | } else {
|
7347 | result = this.parseExport(node);
|
7348 |
|
7349 | if (result.type === "ExportNamedDeclaration" && (!result.exportKind || result.exportKind === "value") || result.type === "ExportAllDeclaration" && (!result.exportKind || result.exportKind === "value") || result.type === "ExportDefaultDeclaration") {
|
7350 | this.sawUnambiguousESM = true;
|
7351 | }
|
7352 | }
|
7353 |
|
7354 | this.assertModuleNodeAllowed(node);
|
7355 | return result;
|
7356 | }
|
7357 |
|
7358 | case types.name:
|
7359 | if (this.isContextual("async")) {
|
7360 | const state = this.state.clone();
|
7361 | this.next();
|
7362 |
|
7363 | if (this.match(types._function) && !this.canInsertSemicolon()) {
|
7364 | if (context) {
|
7365 | this.raise(this.state.lastTokStart, "Function declaration not allowed in this context");
|
7366 | }
|
7367 |
|
7368 | this.next();
|
7369 | return this.parseFunction(node, true, false, true);
|
7370 | } else {
|
7371 | this.state = state;
|
7372 | }
|
7373 | }
|
7374 |
|
7375 | }
|
7376 |
|
7377 | const maybeName = this.state.value;
|
7378 | const expr = this.parseExpression();
|
7379 |
|
7380 | if (starttype === types.name && expr.type === "Identifier" && this.eat(types.colon)) {
|
7381 | return this.parseLabeledStatement(node, maybeName, expr, context);
|
7382 | } else {
|
7383 | return this.parseExpressionStatement(node, expr);
|
7384 | }
|
7385 | }
|
7386 |
|
7387 | assertModuleNodeAllowed(node) {
|
7388 | if (!this.options.allowImportExportEverywhere && !this.inModule) {
|
7389 | this.raise(node.start, `'import' and 'export' may appear only with 'sourceType: "module"'`, {
|
7390 | code: "BABEL_PARSER_SOURCETYPE_MODULE_REQUIRED"
|
7391 | });
|
7392 | }
|
7393 | }
|
7394 |
|
7395 | takeDecorators(node) {
|
7396 | const decorators = this.state.decoratorStack[this.state.decoratorStack.length - 1];
|
7397 |
|
7398 | if (decorators.length) {
|
7399 | node.decorators = decorators;
|
7400 | this.resetStartLocationFromNode(node, decorators[0]);
|
7401 | this.state.decoratorStack[this.state.decoratorStack.length - 1] = [];
|
7402 | }
|
7403 | }
|
7404 |
|
7405 | canHaveLeadingDecorator() {
|
7406 | return this.match(types._class);
|
7407 | }
|
7408 |
|
7409 | parseDecorators(allowExport) {
|
7410 | const currentContextDecorators = this.state.decoratorStack[this.state.decoratorStack.length - 1];
|
7411 |
|
7412 | while (this.match(types.at)) {
|
7413 | const decorator = this.parseDecorator();
|
7414 | currentContextDecorators.push(decorator);
|
7415 | }
|
7416 |
|
7417 | if (this.match(types._export)) {
|
7418 | if (!allowExport) {
|
7419 | this.unexpected();
|
7420 | }
|
7421 |
|
7422 | if (this.hasPlugin("decorators") && !this.getPluginOption("decorators", "decoratorsBeforeExport")) {
|
7423 | this.raise(this.state.start, "Using the export keyword between a decorator and a class is not allowed. " + "Please use `export @dec class` instead.");
|
7424 | }
|
7425 | } else if (!this.canHaveLeadingDecorator()) {
|
7426 | this.raise(this.state.start, "Leading decorators must be attached to a class declaration");
|
7427 | }
|
7428 | }
|
7429 |
|
7430 | parseDecorator() {
|
7431 | this.expectOnePlugin(["decorators-legacy", "decorators"]);
|
7432 | const node = this.startNode();
|
7433 | this.next();
|
7434 |
|
7435 | if (this.hasPlugin("decorators")) {
|
7436 | this.state.decoratorStack.push([]);
|
7437 | const startPos = this.state.start;
|
7438 | const startLoc = this.state.startLoc;
|
7439 | let expr;
|
7440 |
|
7441 | if (this.eat(types.parenL)) {
|
7442 | expr = this.parseExpression();
|
7443 | this.expect(types.parenR);
|
7444 | } else {
|
7445 | expr = this.parseIdentifier(false);
|
7446 |
|
7447 | while (this.eat(types.dot)) {
|
7448 | const node = this.startNodeAt(startPos, startLoc);
|
7449 | node.object = expr;
|
7450 | node.property = this.parseIdentifier(true);
|
7451 | node.computed = false;
|
7452 | expr = this.finishNode(node, "MemberExpression");
|
7453 | }
|
7454 | }
|
7455 |
|
7456 | node.expression = this.parseMaybeDecoratorArguments(expr);
|
7457 | this.state.decoratorStack.pop();
|
7458 | } else {
|
7459 | node.expression = this.parseMaybeAssign();
|
7460 | }
|
7461 |
|
7462 | return this.finishNode(node, "Decorator");
|
7463 | }
|
7464 |
|
7465 | parseMaybeDecoratorArguments(expr) {
|
7466 | if (this.eat(types.parenL)) {
|
7467 | const node = this.startNodeAtNode(expr);
|
7468 | node.callee = expr;
|
7469 | node.arguments = this.parseCallExpressionArguments(types.parenR, false);
|
7470 | this.toReferencedList(node.arguments);
|
7471 | return this.finishNode(node, "CallExpression");
|
7472 | }
|
7473 |
|
7474 | return expr;
|
7475 | }
|
7476 |
|
7477 | parseBreakContinueStatement(node, keyword) {
|
7478 | const isBreak = keyword === "break";
|
7479 | this.next();
|
7480 |
|
7481 | if (this.isLineTerminator()) {
|
7482 | node.label = null;
|
7483 | } else if (!this.match(types.name)) {
|
7484 | this.unexpected();
|
7485 | } else {
|
7486 | node.label = this.parseIdentifier();
|
7487 | this.semicolon();
|
7488 | }
|
7489 |
|
7490 | let i;
|
7491 |
|
7492 | for (i = 0; i < this.state.labels.length; ++i) {
|
7493 | const lab = this.state.labels[i];
|
7494 |
|
7495 | if (node.label == null || lab.name === node.label.name) {
|
7496 | if (lab.kind != null && (isBreak || lab.kind === "loop")) break;
|
7497 | if (node.label && isBreak) break;
|
7498 | }
|
7499 | }
|
7500 |
|
7501 | if (i === this.state.labels.length) {
|
7502 | this.raise(node.start, "Unsyntactic " + keyword);
|
7503 | }
|
7504 |
|
7505 | return this.finishNode(node, isBreak ? "BreakStatement" : "ContinueStatement");
|
7506 | }
|
7507 |
|
7508 | parseDebuggerStatement(node) {
|
7509 | this.next();
|
7510 | this.semicolon();
|
7511 | return this.finishNode(node, "DebuggerStatement");
|
7512 | }
|
7513 |
|
7514 | parseDoStatement(node) {
|
7515 | this.next();
|
7516 | this.state.labels.push(loopLabel);
|
7517 | node.body = this.withTopicForbiddingContext(() => this.parseStatement("do"));
|
7518 | this.state.labels.pop();
|
7519 | this.expect(types._while);
|
7520 | node.test = this.parseParenExpression();
|
7521 | this.eat(types.semi);
|
7522 | return this.finishNode(node, "DoWhileStatement");
|
7523 | }
|
7524 |
|
7525 | parseForStatement(node) {
|
7526 | this.next();
|
7527 | this.state.labels.push(loopLabel);
|
7528 | let awaitAt = -1;
|
7529 |
|
7530 | if ((this.state.inAsync || !this.state.inFunction && this.options.allowAwaitOutsideFunction) && this.eatContextual("await")) {
|
7531 | awaitAt = this.state.lastTokStart;
|
7532 | }
|
7533 |
|
7534 | this.expect(types.parenL);
|
7535 |
|
7536 | if (this.match(types.semi)) {
|
7537 | if (awaitAt > -1) {
|
7538 | this.unexpected(awaitAt);
|
7539 | }
|
7540 |
|
7541 | return this.parseFor(node, null);
|
7542 | }
|
7543 |
|
7544 | const isLet = this.isLet();
|
7545 |
|
7546 | if (this.match(types._var) || this.match(types._const) || isLet) {
|
7547 | const init = this.startNode();
|
7548 | const kind = isLet ? "let" : this.state.value;
|
7549 | this.next();
|
7550 | this.parseVar(init, true, kind);
|
7551 | this.finishNode(init, "VariableDeclaration");
|
7552 |
|
7553 | if (this.match(types._in) || this.isContextual("of")) {
|
7554 | if (init.declarations.length === 1) {
|
7555 | const declaration = init.declarations[0];
|
7556 | const isForInInitializer = kind === "var" && declaration.init && declaration.id.type != "ObjectPattern" && declaration.id.type != "ArrayPattern" && !this.isContextual("of");
|
7557 |
|
7558 | if (this.state.strict && isForInInitializer) {
|
7559 | this.raise(this.state.start, "for-in initializer in strict mode");
|
7560 | } else if (isForInInitializer || !declaration.init) {
|
7561 | return this.parseForIn(node, init, awaitAt);
|
7562 | }
|
7563 | }
|
7564 | }
|
7565 |
|
7566 | if (awaitAt > -1) {
|
7567 | this.unexpected(awaitAt);
|
7568 | }
|
7569 |
|
7570 | return this.parseFor(node, init);
|
7571 | }
|
7572 |
|
7573 | const refShorthandDefaultPos = {
|
7574 | start: 0
|
7575 | };
|
7576 | const init = this.parseExpression(true, refShorthandDefaultPos);
|
7577 |
|
7578 | if (this.match(types._in) || this.isContextual("of")) {
|
7579 | const description = this.isContextual("of") ? "for-of statement" : "for-in statement";
|
7580 | this.toAssignable(init, undefined, description);
|
7581 | this.checkLVal(init, undefined, undefined, description);
|
7582 | return this.parseForIn(node, init, awaitAt);
|
7583 | } else if (refShorthandDefaultPos.start) {
|
7584 | this.unexpected(refShorthandDefaultPos.start);
|
7585 | }
|
7586 |
|
7587 | if (awaitAt > -1) {
|
7588 | this.unexpected(awaitAt);
|
7589 | }
|
7590 |
|
7591 | return this.parseFor(node, init);
|
7592 | }
|
7593 |
|
7594 | parseFunctionStatement(node) {
|
7595 | this.next();
|
7596 | return this.parseFunction(node, true);
|
7597 | }
|
7598 |
|
7599 | parseIfStatement(node) {
|
7600 | this.next();
|
7601 | node.test = this.parseParenExpression();
|
7602 | node.consequent = this.parseStatement("if");
|
7603 | node.alternate = this.eat(types._else) ? this.parseStatement("if") : null;
|
7604 | return this.finishNode(node, "IfStatement");
|
7605 | }
|
7606 |
|
7607 | parseReturnStatement(node) {
|
7608 | if (!this.state.inFunction && !this.options.allowReturnOutsideFunction) {
|
7609 | this.raise(this.state.start, "'return' outside of function");
|
7610 | }
|
7611 |
|
7612 | this.next();
|
7613 |
|
7614 | if (this.isLineTerminator()) {
|
7615 | node.argument = null;
|
7616 | } else {
|
7617 | node.argument = this.parseExpression();
|
7618 | this.semicolon();
|
7619 | }
|
7620 |
|
7621 | return this.finishNode(node, "ReturnStatement");
|
7622 | }
|
7623 |
|
7624 | parseSwitchStatement(node) {
|
7625 | this.next();
|
7626 | node.discriminant = this.parseParenExpression();
|
7627 | const cases = node.cases = [];
|
7628 | this.expect(types.braceL);
|
7629 | this.state.labels.push(switchLabel);
|
7630 | let cur;
|
7631 |
|
7632 | for (let sawDefault; !this.match(types.braceR);) {
|
7633 | if (this.match(types._case) || this.match(types._default)) {
|
7634 | const isCase = this.match(types._case);
|
7635 | if (cur) this.finishNode(cur, "SwitchCase");
|
7636 | cases.push(cur = this.startNode());
|
7637 | cur.consequent = [];
|
7638 | this.next();
|
7639 |
|
7640 | if (isCase) {
|
7641 | cur.test = this.parseExpression();
|
7642 | } else {
|
7643 | if (sawDefault) {
|
7644 | this.raise(this.state.lastTokStart, "Multiple default clauses");
|
7645 | }
|
7646 |
|
7647 | sawDefault = true;
|
7648 | cur.test = null;
|
7649 | }
|
7650 |
|
7651 | this.expect(types.colon);
|
7652 | } else {
|
7653 | if (cur) {
|
7654 | cur.consequent.push(this.parseStatement(null));
|
7655 | } else {
|
7656 | this.unexpected();
|
7657 | }
|
7658 | }
|
7659 | }
|
7660 |
|
7661 | if (cur) this.finishNode(cur, "SwitchCase");
|
7662 | this.next();
|
7663 | this.state.labels.pop();
|
7664 | return this.finishNode(node, "SwitchStatement");
|
7665 | }
|
7666 |
|
7667 | parseThrowStatement(node) {
|
7668 | this.next();
|
7669 |
|
7670 | if (lineBreak.test(this.state.input.slice(this.state.lastTokEnd, this.state.start))) {
|
7671 | this.raise(this.state.lastTokEnd, "Illegal newline after throw");
|
7672 | }
|
7673 |
|
7674 | node.argument = this.parseExpression();
|
7675 | this.semicolon();
|
7676 | return this.finishNode(node, "ThrowStatement");
|
7677 | }
|
7678 |
|
7679 | parseTryStatement(node) {
|
7680 | this.next();
|
7681 | node.block = this.parseBlock();
|
7682 | node.handler = null;
|
7683 |
|
7684 | if (this.match(types._catch)) {
|
7685 | const clause = this.startNode();
|
7686 | this.next();
|
7687 |
|
7688 | if (this.match(types.parenL)) {
|
7689 | this.expect(types.parenL);
|
7690 | clause.param = this.parseBindingAtom();
|
7691 | const clashes = Object.create(null);
|
7692 | this.checkLVal(clause.param, true, clashes, "catch clause");
|
7693 | this.expect(types.parenR);
|
7694 | } else {
|
7695 | clause.param = null;
|
7696 | }
|
7697 |
|
7698 | clause.body = this.withTopicForbiddingContext(() => this.parseBlock(false));
|
7699 | node.handler = this.finishNode(clause, "CatchClause");
|
7700 | }
|
7701 |
|
7702 | node.guardedHandlers = empty;
|
7703 | node.finalizer = this.eat(types._finally) ? this.parseBlock() : null;
|
7704 |
|
7705 | if (!node.handler && !node.finalizer) {
|
7706 | this.raise(node.start, "Missing catch or finally clause");
|
7707 | }
|
7708 |
|
7709 | return this.finishNode(node, "TryStatement");
|
7710 | }
|
7711 |
|
7712 | parseVarStatement(node, kind) {
|
7713 | this.next();
|
7714 | this.parseVar(node, false, kind);
|
7715 | this.semicolon();
|
7716 | return this.finishNode(node, "VariableDeclaration");
|
7717 | }
|
7718 |
|
7719 | parseWhileStatement(node) {
|
7720 | this.next();
|
7721 | node.test = this.parseParenExpression();
|
7722 | this.state.labels.push(loopLabel);
|
7723 | node.body = this.withTopicForbiddingContext(() => this.parseStatement("while"));
|
7724 | this.state.labels.pop();
|
7725 | return this.finishNode(node, "WhileStatement");
|
7726 | }
|
7727 |
|
7728 | parseWithStatement(node) {
|
7729 | if (this.state.strict) {
|
7730 | this.raise(this.state.start, "'with' in strict mode");
|
7731 | }
|
7732 |
|
7733 | this.next();
|
7734 | node.object = this.parseParenExpression();
|
7735 | node.body = this.withTopicForbiddingContext(() => this.parseStatement("with"));
|
7736 | return this.finishNode(node, "WithStatement");
|
7737 | }
|
7738 |
|
7739 | parseEmptyStatement(node) {
|
7740 | this.next();
|
7741 | return this.finishNode(node, "EmptyStatement");
|
7742 | }
|
7743 |
|
7744 | parseLabeledStatement(node, maybeName, expr, context) {
|
7745 | for (let _i = 0, _this$state$labels = this.state.labels; _i < _this$state$labels.length; _i++) {
|
7746 | const label = _this$state$labels[_i];
|
7747 |
|
7748 | if (label.name === maybeName) {
|
7749 | this.raise(expr.start, `Label '${maybeName}' is already declared`);
|
7750 | }
|
7751 | }
|
7752 |
|
7753 | const kind = this.state.type.isLoop ? "loop" : this.match(types._switch) ? "switch" : null;
|
7754 |
|
7755 | for (let i = this.state.labels.length - 1; i >= 0; i--) {
|
7756 | const label = this.state.labels[i];
|
7757 |
|
7758 | if (label.statementStart === node.start) {
|
7759 | label.statementStart = this.state.start;
|
7760 | label.kind = kind;
|
7761 | } else {
|
7762 | break;
|
7763 | }
|
7764 | }
|
7765 |
|
7766 | this.state.labels.push({
|
7767 | name: maybeName,
|
7768 | kind: kind,
|
7769 | statementStart: this.state.start
|
7770 | });
|
7771 | node.body = this.parseStatement(context ? context.indexOf("label") === -1 ? context + "label" : context : "label");
|
7772 | this.state.labels.pop();
|
7773 | node.label = expr;
|
7774 | return this.finishNode(node, "LabeledStatement");
|
7775 | }
|
7776 |
|
7777 | parseExpressionStatement(node, expr) {
|
7778 | node.expression = expr;
|
7779 | this.semicolon();
|
7780 | return this.finishNode(node, "ExpressionStatement");
|
7781 | }
|
7782 |
|
7783 | parseBlock(allowDirectives) {
|
7784 | const node = this.startNode();
|
7785 | this.expect(types.braceL);
|
7786 | this.parseBlockBody(node, allowDirectives, false, types.braceR);
|
7787 | return this.finishNode(node, "BlockStatement");
|
7788 | }
|
7789 |
|
7790 | isValidDirective(stmt) {
|
7791 | return stmt.type === "ExpressionStatement" && stmt.expression.type === "StringLiteral" && !stmt.expression.extra.parenthesized;
|
7792 | }
|
7793 |
|
7794 | parseBlockBody(node, allowDirectives, topLevel, end) {
|
7795 | const body = node.body = [];
|
7796 | const directives = node.directives = [];
|
7797 | this.parseBlockOrModuleBlockBody(body, allowDirectives ? directives : undefined, topLevel, end);
|
7798 | }
|
7799 |
|
7800 | parseBlockOrModuleBlockBody(body, directives, topLevel, end) {
|
7801 | let parsedNonDirective = false;
|
7802 | let oldStrict;
|
7803 | let octalPosition;
|
7804 |
|
7805 | while (!this.eat(end)) {
|
7806 | if (!parsedNonDirective && this.state.containsOctal && !octalPosition) {
|
7807 | octalPosition = this.state.octalPosition;
|
7808 | }
|
7809 |
|
7810 | const stmt = this.parseStatement(null, topLevel);
|
7811 |
|
7812 | if (directives && !parsedNonDirective && this.isValidDirective(stmt)) {
|
7813 | const directive = this.stmtToDirective(stmt);
|
7814 | directives.push(directive);
|
7815 |
|
7816 | if (oldStrict === undefined && directive.value.value === "use strict") {
|
7817 | oldStrict = this.state.strict;
|
7818 | this.setStrict(true);
|
7819 |
|
7820 | if (octalPosition) {
|
7821 | this.raise(octalPosition, "Octal literal in strict mode");
|
7822 | }
|
7823 | }
|
7824 |
|
7825 | continue;
|
7826 | }
|
7827 |
|
7828 | parsedNonDirective = true;
|
7829 | body.push(stmt);
|
7830 | }
|
7831 |
|
7832 | if (oldStrict === false) {
|
7833 | this.setStrict(false);
|
7834 | }
|
7835 | }
|
7836 |
|
7837 | parseFor(node, init) {
|
7838 | node.init = init;
|
7839 | this.expect(types.semi);
|
7840 | node.test = this.match(types.semi) ? null : this.parseExpression();
|
7841 | this.expect(types.semi);
|
7842 | node.update = this.match(types.parenR) ? null : this.parseExpression();
|
7843 | this.expect(types.parenR);
|
7844 | node.body = this.withTopicForbiddingContext(() => this.parseStatement("for"));
|
7845 | this.state.labels.pop();
|
7846 | return this.finishNode(node, "ForStatement");
|
7847 | }
|
7848 |
|
7849 | parseForIn(node, init, awaitAt) {
|
7850 | const type = this.match(types._in) ? "ForInStatement" : "ForOfStatement";
|
7851 |
|
7852 | if (awaitAt > -1) {
|
7853 | this.eatContextual("of");
|
7854 | } else {
|
7855 | this.next();
|
7856 | }
|
7857 |
|
7858 | if (type === "ForOfStatement") {
|
7859 | node.await = awaitAt > -1;
|
7860 | }
|
7861 |
|
7862 | node.left = init;
|
7863 | node.right = this.parseExpression();
|
7864 | this.expect(types.parenR);
|
7865 | node.body = this.withTopicForbiddingContext(() => this.parseStatement("for"));
|
7866 | this.state.labels.pop();
|
7867 | return this.finishNode(node, type);
|
7868 | }
|
7869 |
|
7870 | parseVar(node, isFor, kind) {
|
7871 | const declarations = node.declarations = [];
|
7872 | const isTypescript = this.hasPlugin("typescript");
|
7873 | node.kind = kind;
|
7874 |
|
7875 | for (;;) {
|
7876 | const decl = this.startNode();
|
7877 | this.parseVarId(decl, kind);
|
7878 |
|
7879 | if (this.eat(types.eq)) {
|
7880 | decl.init = this.parseMaybeAssign(isFor);
|
7881 | } else {
|
7882 | if (kind === "const" && !(this.match(types._in) || this.isContextual("of"))) {
|
7883 | if (!isTypescript) {
|
7884 | this.unexpected();
|
7885 | }
|
7886 | } else if (decl.id.type !== "Identifier" && !(isFor && (this.match(types._in) || this.isContextual("of")))) {
|
7887 | this.raise(this.state.lastTokEnd, "Complex binding patterns require an initialization value");
|
7888 | }
|
7889 |
|
7890 | decl.init = null;
|
7891 | }
|
7892 |
|
7893 | declarations.push(this.finishNode(decl, "VariableDeclarator"));
|
7894 | if (!this.eat(types.comma)) break;
|
7895 | }
|
7896 |
|
7897 | return node;
|
7898 | }
|
7899 |
|
7900 | parseVarId(decl, kind) {
|
7901 | if ((kind === "const" || kind === "let") && this.isContextual("let")) {
|
7902 | this.unexpected(null, "let is disallowed as a lexically bound name");
|
7903 | }
|
7904 |
|
7905 | decl.id = this.parseBindingAtom();
|
7906 | this.checkLVal(decl.id, true, undefined, "variable declaration");
|
7907 | }
|
7908 |
|
7909 | parseFunction(node, isStatement, allowExpressionBody = false, isAsync = false, optionalId = false) {
|
7910 | const oldInFunc = this.state.inFunction;
|
7911 | const oldInMethod = this.state.inMethod;
|
7912 | const oldInAsync = this.state.inAsync;
|
7913 | const oldInGenerator = this.state.inGenerator;
|
7914 | const oldInClassProperty = this.state.inClassProperty;
|
7915 | const oldYieldPos = this.state.yieldPos;
|
7916 | const oldAwaitPos = this.state.awaitPos;
|
7917 | this.state.inFunction = true;
|
7918 | this.state.inMethod = false;
|
7919 | this.state.inClassProperty = false;
|
7920 | this.state.yieldPos = 0;
|
7921 | this.state.awaitPos = 0;
|
7922 | this.initFunction(node, isAsync);
|
7923 | node.generator = this.eat(types.star);
|
7924 |
|
7925 | if (isStatement && !optionalId && !this.match(types.name)) {
|
7926 | this.unexpected();
|
7927 | }
|
7928 |
|
7929 | if (!isStatement) {
|
7930 | this.state.inAsync = isAsync;
|
7931 | this.state.inGenerator = node.generator;
|
7932 | }
|
7933 |
|
7934 | if (this.match(types.name)) {
|
7935 | node.id = this.parseIdentifier();
|
7936 | }
|
7937 |
|
7938 | if (isStatement) {
|
7939 | this.state.inAsync = isAsync;
|
7940 | this.state.inGenerator = node.generator;
|
7941 | }
|
7942 |
|
7943 | this.parseFunctionParams(node);
|
7944 | this.withTopicForbiddingContext(() => {
|
7945 | this.parseFunctionBodyAndFinish(node, isStatement ? "FunctionDeclaration" : "FunctionExpression", allowExpressionBody);
|
7946 | });
|
7947 | this.state.inFunction = oldInFunc;
|
7948 | this.state.inMethod = oldInMethod;
|
7949 | this.state.inAsync = oldInAsync;
|
7950 | this.state.inGenerator = oldInGenerator;
|
7951 | this.state.inClassProperty = oldInClassProperty;
|
7952 | this.state.yieldPos = oldYieldPos;
|
7953 | this.state.awaitPos = oldAwaitPos;
|
7954 | return node;
|
7955 | }
|
7956 |
|
7957 | parseFunctionParams(node, allowModifiers) {
|
7958 | const oldInParameters = this.state.inParameters;
|
7959 | this.state.inParameters = true;
|
7960 | this.expect(types.parenL);
|
7961 | node.params = this.parseBindingList(types.parenR, false, allowModifiers);
|
7962 | this.state.inParameters = oldInParameters;
|
7963 | this.checkYieldAwaitInDefaultParams();
|
7964 | }
|
7965 |
|
7966 | parseClass(node, isStatement, optionalId) {
|
7967 | this.next();
|
7968 | this.takeDecorators(node);
|
7969 | const oldStrict = this.state.strict;
|
7970 | this.state.strict = true;
|
7971 | this.parseClassId(node, isStatement, optionalId);
|
7972 | this.parseClassSuper(node);
|
7973 | this.parseClassBody(node);
|
7974 | this.state.strict = oldStrict;
|
7975 | return this.finishNode(node, isStatement ? "ClassDeclaration" : "ClassExpression");
|
7976 | }
|
7977 |
|
7978 | isClassProperty() {
|
7979 | return this.match(types.eq) || this.match(types.semi) || this.match(types.braceR);
|
7980 | }
|
7981 |
|
7982 | isClassMethod() {
|
7983 | return this.match(types.parenL);
|
7984 | }
|
7985 |
|
7986 | isNonstaticConstructor(method) {
|
7987 | return !method.computed && !method.static && (method.key.name === "constructor" || method.key.value === "constructor");
|
7988 | }
|
7989 |
|
7990 | parseClassBody(node) {
|
7991 | this.state.classLevel++;
|
7992 | const state = {
|
7993 | hadConstructor: false
|
7994 | };
|
7995 | let decorators = [];
|
7996 | const classBody = this.startNode();
|
7997 | classBody.body = [];
|
7998 | this.expect(types.braceL);
|
7999 | this.withTopicForbiddingContext(() => {
|
8000 | while (!this.eat(types.braceR)) {
|
8001 | if (this.eat(types.semi)) {
|
8002 | if (decorators.length > 0) {
|
8003 | this.raise(this.state.lastTokEnd, "Decorators must not be followed by a semicolon");
|
8004 | }
|
8005 |
|
8006 | continue;
|
8007 | }
|
8008 |
|
8009 | if (this.match(types.at)) {
|
8010 | decorators.push(this.parseDecorator());
|
8011 | continue;
|
8012 | }
|
8013 |
|
8014 | const member = this.startNode();
|
8015 |
|
8016 | if (decorators.length) {
|
8017 | member.decorators = decorators;
|
8018 | this.resetStartLocationFromNode(member, decorators[0]);
|
8019 | decorators = [];
|
8020 | }
|
8021 |
|
8022 | this.parseClassMember(classBody, member, state);
|
8023 |
|
8024 | if (member.kind === "constructor" && member.decorators && member.decorators.length > 0) {
|
8025 | this.raise(member.start, "Decorators can't be used with a constructor. Did you mean '@dec class { ... }'?");
|
8026 | }
|
8027 | }
|
8028 | });
|
8029 |
|
8030 | if (decorators.length) {
|
8031 | this.raise(this.state.start, "You have trailing decorators with no method");
|
8032 | }
|
8033 |
|
8034 | node.body = this.finishNode(classBody, "ClassBody");
|
8035 | this.state.classLevel--;
|
8036 | }
|
8037 |
|
8038 | parseClassMember(classBody, member, state) {
|
8039 | let isStatic = false;
|
8040 | const containsEsc = this.state.containsEsc;
|
8041 |
|
8042 | if (this.match(types.name) && this.state.value === "static") {
|
8043 | const key = this.parseIdentifier(true);
|
8044 |
|
8045 | if (this.isClassMethod()) {
|
8046 | const method = member;
|
8047 | method.kind = "method";
|
8048 | method.computed = false;
|
8049 | method.key = key;
|
8050 | method.static = false;
|
8051 | this.pushClassMethod(classBody, method, false, false, false);
|
8052 | return;
|
8053 | } else if (this.isClassProperty()) {
|
8054 | const prop = member;
|
8055 | prop.computed = false;
|
8056 | prop.key = key;
|
8057 | prop.static = false;
|
8058 | classBody.body.push(this.parseClassProperty(prop));
|
8059 | return;
|
8060 | } else if (containsEsc) {
|
8061 | throw this.unexpected();
|
8062 | }
|
8063 |
|
8064 | isStatic = true;
|
8065 | }
|
8066 |
|
8067 | this.parseClassMemberWithIsStatic(classBody, member, state, isStatic);
|
8068 | }
|
8069 |
|
8070 | parseClassMemberWithIsStatic(classBody, member, state, isStatic) {
|
8071 | const publicMethod = member;
|
8072 | const privateMethod = member;
|
8073 | const publicProp = member;
|
8074 | const privateProp = member;
|
8075 | const method = publicMethod;
|
8076 | const publicMember = publicMethod;
|
8077 | member.static = isStatic;
|
8078 |
|
8079 | if (this.eat(types.star)) {
|
8080 | method.kind = "method";
|
8081 | this.parseClassPropertyName(method);
|
8082 |
|
8083 | if (method.key.type === "PrivateName") {
|
8084 | this.pushClassPrivateMethod(classBody, privateMethod, true, false);
|
8085 | return;
|
8086 | }
|
8087 |
|
8088 | if (this.isNonstaticConstructor(publicMethod)) {
|
8089 | this.raise(publicMethod.key.start, "Constructor can't be a generator");
|
8090 | }
|
8091 |
|
8092 | this.pushClassMethod(classBody, publicMethod, true, false, false);
|
8093 | return;
|
8094 | }
|
8095 |
|
8096 | const key = this.parseClassPropertyName(member);
|
8097 | const isPrivate = key.type === "PrivateName";
|
8098 | const isSimple = key.type === "Identifier";
|
8099 | this.parsePostMemberNameModifiers(publicMember);
|
8100 |
|
8101 | if (this.isClassMethod()) {
|
8102 | method.kind = "method";
|
8103 |
|
8104 | if (isPrivate) {
|
8105 | this.pushClassPrivateMethod(classBody, privateMethod, false, false);
|
8106 | return;
|
8107 | }
|
8108 |
|
8109 | const isConstructor = this.isNonstaticConstructor(publicMethod);
|
8110 |
|
8111 | if (isConstructor) {
|
8112 | publicMethod.kind = "constructor";
|
8113 |
|
8114 | if (publicMethod.decorators) {
|
8115 | this.raise(publicMethod.start, "You can't attach decorators to a class constructor");
|
8116 | }
|
8117 |
|
8118 | if (state.hadConstructor && !this.hasPlugin("typescript")) {
|
8119 | this.raise(key.start, "Duplicate constructor in the same class");
|
8120 | }
|
8121 |
|
8122 | state.hadConstructor = true;
|
8123 | }
|
8124 |
|
8125 | this.pushClassMethod(classBody, publicMethod, false, false, isConstructor);
|
8126 | } else if (this.isClassProperty()) {
|
8127 | if (isPrivate) {
|
8128 | this.pushClassPrivateProperty(classBody, privateProp);
|
8129 | } else {
|
8130 | this.pushClassProperty(classBody, publicProp);
|
8131 | }
|
8132 | } else if (isSimple && key.name === "async" && !this.isLineTerminator()) {
|
8133 | const isGenerator = this.eat(types.star);
|
8134 | method.kind = "method";
|
8135 | this.parseClassPropertyName(method);
|
8136 |
|
8137 | if (method.key.type === "PrivateName") {
|
8138 | this.pushClassPrivateMethod(classBody, privateMethod, isGenerator, true);
|
8139 | } else {
|
8140 | if (this.isNonstaticConstructor(publicMethod)) {
|
8141 | this.raise(publicMethod.key.start, "Constructor can't be an async function");
|
8142 | }
|
8143 |
|
8144 | this.pushClassMethod(classBody, publicMethod, isGenerator, true, false);
|
8145 | }
|
8146 | } else if (isSimple && (key.name === "get" || key.name === "set") && !(this.match(types.star) && this.isLineTerminator())) {
|
8147 | method.kind = key.name;
|
8148 | this.parseClassPropertyName(publicMethod);
|
8149 |
|
8150 | if (method.key.type === "PrivateName") {
|
8151 | this.pushClassPrivateMethod(classBody, privateMethod, false, false);
|
8152 | } else {
|
8153 | if (this.isNonstaticConstructor(publicMethod)) {
|
8154 | this.raise(publicMethod.key.start, "Constructor can't have get/set modifier");
|
8155 | }
|
8156 |
|
8157 | this.pushClassMethod(classBody, publicMethod, false, false, false);
|
8158 | }
|
8159 |
|
8160 | this.checkGetterSetterParams(publicMethod);
|
8161 | } else if (this.isLineTerminator()) {
|
8162 | if (isPrivate) {
|
8163 | this.pushClassPrivateProperty(classBody, privateProp);
|
8164 | } else {
|
8165 | this.pushClassProperty(classBody, publicProp);
|
8166 | }
|
8167 | } else {
|
8168 | this.unexpected();
|
8169 | }
|
8170 | }
|
8171 |
|
8172 | parseClassPropertyName(member) {
|
8173 | const key = this.parsePropertyName(member);
|
8174 |
|
8175 | if (!member.computed && member.static && (key.name === "prototype" || key.value === "prototype")) {
|
8176 | this.raise(key.start, "Classes may not have static property named prototype");
|
8177 | }
|
8178 |
|
8179 | if (key.type === "PrivateName" && key.id.name === "constructor") {
|
8180 | this.raise(key.start, "Classes may not have a private field named '#constructor'");
|
8181 | }
|
8182 |
|
8183 | return key;
|
8184 | }
|
8185 |
|
8186 | pushClassProperty(classBody, prop) {
|
8187 | if (this.isNonstaticConstructor(prop)) {
|
8188 | this.raise(prop.key.start, "Classes may not have a non-static field named 'constructor'");
|
8189 | }
|
8190 |
|
8191 | classBody.body.push(this.parseClassProperty(prop));
|
8192 | }
|
8193 |
|
8194 | pushClassPrivateProperty(classBody, prop) {
|
8195 | this.expectPlugin("classPrivateProperties", prop.key.start);
|
8196 | classBody.body.push(this.parseClassPrivateProperty(prop));
|
8197 | }
|
8198 |
|
8199 | pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor) {
|
8200 | classBody.body.push(this.parseMethod(method, isGenerator, isAsync, isConstructor, "ClassMethod"));
|
8201 | }
|
8202 |
|
8203 | pushClassPrivateMethod(classBody, method, isGenerator, isAsync) {
|
8204 | this.expectPlugin("classPrivateMethods", method.key.start);
|
8205 | classBody.body.push(this.parseMethod(method, isGenerator, isAsync, false, "ClassPrivateMethod"));
|
8206 | }
|
8207 |
|
8208 | parsePostMemberNameModifiers(methodOrProp) {}
|
8209 |
|
8210 | parseAccessModifier() {
|
8211 | return undefined;
|
8212 | }
|
8213 |
|
8214 | parseClassPrivateProperty(node) {
|
8215 | const oldInMethod = this.state.inMethod;
|
8216 | this.state.inMethod = false;
|
8217 | this.state.inClassProperty = true;
|
8218 | node.value = this.eat(types.eq) ? this.parseMaybeAssign() : null;
|
8219 | this.semicolon();
|
8220 | this.state.inClassProperty = false;
|
8221 | this.state.inMethod = oldInMethod;
|
8222 | return this.finishNode(node, "ClassPrivateProperty");
|
8223 | }
|
8224 |
|
8225 | parseClassProperty(node) {
|
8226 | if (!node.typeAnnotation) {
|
8227 | this.expectPlugin("classProperties");
|
8228 | }
|
8229 |
|
8230 | const oldInMethod = this.state.inMethod;
|
8231 | this.state.inMethod = false;
|
8232 | this.state.inClassProperty = true;
|
8233 |
|
8234 | if (this.match(types.eq)) {
|
8235 | this.expectPlugin("classProperties");
|
8236 | this.next();
|
8237 | node.value = this.parseMaybeAssign();
|
8238 | } else {
|
8239 | node.value = null;
|
8240 | }
|
8241 |
|
8242 | this.semicolon();
|
8243 | this.state.inClassProperty = false;
|
8244 | this.state.inMethod = oldInMethod;
|
8245 | return this.finishNode(node, "ClassProperty");
|
8246 | }
|
8247 |
|
8248 | parseClassId(node, isStatement, optionalId) {
|
8249 | if (this.match(types.name)) {
|
8250 | node.id = this.parseIdentifier();
|
8251 | } else {
|
8252 | if (optionalId || !isStatement) {
|
8253 | node.id = null;
|
8254 | } else {
|
8255 | this.unexpected(null, "A class name is required");
|
8256 | }
|
8257 | }
|
8258 | }
|
8259 |
|
8260 | parseClassSuper(node) {
|
8261 | node.superClass = this.eat(types._extends) ? this.parseExprSubscripts() : null;
|
8262 | }
|
8263 |
|
8264 | parseExport(node) {
|
8265 | const hasDefault = this.maybeParseExportDefaultSpecifier(node);
|
8266 | const parseAfterDefault = !hasDefault || this.eat(types.comma);
|
8267 | const hasStar = parseAfterDefault && this.eatExportStar(node);
|
8268 | const hasNamespace = hasStar && this.maybeParseExportNamespaceSpecifier(node);
|
8269 | const parseAfterNamespace = parseAfterDefault && (!hasNamespace || this.eat(types.comma));
|
8270 | const isFromRequired = hasDefault || hasStar;
|
8271 |
|
8272 | if (hasStar && !hasNamespace) {
|
8273 | if (hasDefault) this.unexpected();
|
8274 | this.parseExportFrom(node, true);
|
8275 | return this.finishNode(node, "ExportAllDeclaration");
|
8276 | }
|
8277 |
|
8278 | const hasSpecifiers = this.maybeParseExportNamedSpecifiers(node);
|
8279 |
|
8280 | if (hasDefault && parseAfterDefault && !hasStar && !hasSpecifiers || hasNamespace && parseAfterNamespace && !hasSpecifiers) {
|
8281 | throw this.unexpected(null, types.braceL);
|
8282 | }
|
8283 |
|
8284 | let hasDeclaration;
|
8285 |
|
8286 | if (isFromRequired || hasSpecifiers) {
|
8287 | hasDeclaration = false;
|
8288 | this.parseExportFrom(node, isFromRequired);
|
8289 | } else {
|
8290 | hasDeclaration = this.maybeParseExportDeclaration(node);
|
8291 | }
|
8292 |
|
8293 | if (isFromRequired || hasSpecifiers || hasDeclaration) {
|
8294 | this.checkExport(node, true);
|
8295 | return this.finishNode(node, "ExportNamedDeclaration");
|
8296 | }
|
8297 |
|
8298 | if (this.eat(types._default)) {
|
8299 | node.declaration = this.parseExportDefaultExpression();
|
8300 | this.checkExport(node, true, true);
|
8301 | return this.finishNode(node, "ExportDefaultDeclaration");
|
8302 | }
|
8303 |
|
8304 | throw this.unexpected(null, types.braceL);
|
8305 | }
|
8306 |
|
8307 | eatExportStar(node) {
|
8308 | return this.eat(types.star);
|
8309 | }
|
8310 |
|
8311 | maybeParseExportDefaultSpecifier(node) {
|
8312 | if (this.isExportDefaultSpecifier()) {
|
8313 | this.expectPlugin("exportDefaultFrom");
|
8314 | const specifier = this.startNode();
|
8315 | specifier.exported = this.parseIdentifier(true);
|
8316 | node.specifiers = [this.finishNode(specifier, "ExportDefaultSpecifier")];
|
8317 | return true;
|
8318 | }
|
8319 |
|
8320 | return false;
|
8321 | }
|
8322 |
|
8323 | maybeParseExportNamespaceSpecifier(node) {
|
8324 | if (this.isContextual("as")) {
|
8325 | if (!node.specifiers) node.specifiers = [];
|
8326 | this.expectPlugin("exportNamespaceFrom");
|
8327 | const specifier = this.startNodeAt(this.state.lastTokStart, this.state.lastTokStartLoc);
|
8328 | this.next();
|
8329 | specifier.exported = this.parseIdentifier(true);
|
8330 | node.specifiers.push(this.finishNode(specifier, "ExportNamespaceSpecifier"));
|
8331 | return true;
|
8332 | }
|
8333 |
|
8334 | return false;
|
8335 | }
|
8336 |
|
8337 | maybeParseExportNamedSpecifiers(node) {
|
8338 | if (this.match(types.braceL)) {
|
8339 | if (!node.specifiers) node.specifiers = [];
|
8340 | node.specifiers.push(...this.parseExportSpecifiers());
|
8341 | node.source = null;
|
8342 | node.declaration = null;
|
8343 | return true;
|
8344 | }
|
8345 |
|
8346 | return false;
|
8347 | }
|
8348 |
|
8349 | maybeParseExportDeclaration(node) {
|
8350 | if (this.shouldParseExportDeclaration()) {
|
8351 | if (this.isContextual("async")) {
|
8352 | const next = this.lookahead();
|
8353 |
|
8354 | if (next.type !== types._function) {
|
8355 | this.unexpected(next.start, `Unexpected token, expected "function"`);
|
8356 | }
|
8357 | }
|
8358 |
|
8359 | node.specifiers = [];
|
8360 | node.source = null;
|
8361 | node.declaration = this.parseExportDeclaration(node);
|
8362 | return true;
|
8363 | }
|
8364 |
|
8365 | return false;
|
8366 | }
|
8367 |
|
8368 | isAsyncFunction() {
|
8369 | if (!this.isContextual("async")) return false;
|
8370 | const {
|
8371 | input,
|
8372 | pos,
|
8373 | length
|
8374 | } = this.state;
|
8375 | skipWhiteSpace.lastIndex = pos;
|
8376 | const skip = skipWhiteSpace.exec(input);
|
8377 | if (!skip || !skip.length) return false;
|
8378 | const next = pos + skip[0].length;
|
8379 | return !lineBreak.test(input.slice(pos, next)) && input.slice(next, next + 8) === "function" && (next + 8 === length || !isIdentifierChar(input.charCodeAt(next + 8)));
|
8380 | }
|
8381 |
|
8382 | parseExportDefaultExpression() {
|
8383 | const expr = this.startNode();
|
8384 | const isAsync = this.isAsyncFunction();
|
8385 |
|
8386 | if (this.eat(types._function) || isAsync) {
|
8387 | if (isAsync) {
|
8388 | this.eatContextual("async");
|
8389 | this.expect(types._function);
|
8390 | }
|
8391 |
|
8392 | return this.parseFunction(expr, true, false, isAsync, true);
|
8393 | } else if (this.match(types._class)) {
|
8394 | return this.parseClass(expr, true, true);
|
8395 | } else if (this.match(types.at)) {
|
8396 | if (this.hasPlugin("decorators") && this.getPluginOption("decorators", "decoratorsBeforeExport")) {
|
8397 | this.unexpected(this.state.start, "Decorators must be placed *before* the 'export' keyword." + " You can set the 'decoratorsBeforeExport' option to false to use" + " the 'export @decorator class {}' syntax");
|
8398 | }
|
8399 |
|
8400 | this.parseDecorators(false);
|
8401 | return this.parseClass(expr, true, true);
|
8402 | } else if (this.match(types._const) || this.match(types._var) || this.isLet()) {
|
8403 | return this.raise(this.state.start, "Only expressions, functions or classes are allowed as the `default` export.");
|
8404 | } else {
|
8405 | const res = this.parseMaybeAssign();
|
8406 | this.semicolon();
|
8407 | return res;
|
8408 | }
|
8409 | }
|
8410 |
|
8411 | parseExportDeclaration(node) {
|
8412 | return this.parseStatement(null);
|
8413 | }
|
8414 |
|
8415 | isExportDefaultSpecifier() {
|
8416 | if (this.match(types.name)) {
|
8417 | return this.state.value !== "async" && this.state.value !== "let";
|
8418 | }
|
8419 |
|
8420 | if (!this.match(types._default)) {
|
8421 | return false;
|
8422 | }
|
8423 |
|
8424 | const lookahead = this.lookahead();
|
8425 | return lookahead.type === types.comma || lookahead.type === types.name && lookahead.value === "from";
|
8426 | }
|
8427 |
|
8428 | parseExportFrom(node, expect) {
|
8429 | if (this.eatContextual("from")) {
|
8430 | node.source = this.parseImportSource();
|
8431 | this.checkExport(node);
|
8432 | } else {
|
8433 | if (expect) {
|
8434 | this.unexpected();
|
8435 | } else {
|
8436 | node.source = null;
|
8437 | }
|
8438 | }
|
8439 |
|
8440 | this.semicolon();
|
8441 | }
|
8442 |
|
8443 | shouldParseExportDeclaration() {
|
8444 | if (this.match(types.at)) {
|
8445 | this.expectOnePlugin(["decorators", "decorators-legacy"]);
|
8446 |
|
8447 | if (this.hasPlugin("decorators")) {
|
8448 | if (this.getPluginOption("decorators", "decoratorsBeforeExport")) {
|
8449 | this.unexpected(this.state.start, "Decorators must be placed *before* the 'export' keyword." + " You can set the 'decoratorsBeforeExport' option to false to use" + " the 'export @decorator class {}' syntax");
|
8450 | } else {
|
8451 | return true;
|
8452 | }
|
8453 | }
|
8454 | }
|
8455 |
|
8456 | return this.state.type.keyword === "var" || this.state.type.keyword === "const" || this.state.type.keyword === "function" || this.state.type.keyword === "class" || this.isLet() || this.isAsyncFunction();
|
8457 | }
|
8458 |
|
8459 | checkExport(node, checkNames, isDefault) {
|
8460 | if (checkNames) {
|
8461 | if (isDefault) {
|
8462 | this.checkDuplicateExports(node, "default");
|
8463 | } else if (node.specifiers && node.specifiers.length) {
|
8464 | for (let _i2 = 0, _node$specifiers = node.specifiers; _i2 < _node$specifiers.length; _i2++) {
|
8465 | const specifier = _node$specifiers[_i2];
|
8466 | this.checkDuplicateExports(specifier, specifier.exported.name);
|
8467 | }
|
8468 | } else if (node.declaration) {
|
8469 | if (node.declaration.type === "FunctionDeclaration" || node.declaration.type === "ClassDeclaration") {
|
8470 | const id = node.declaration.id;
|
8471 | if (!id) throw new Error("Assertion failure");
|
8472 | this.checkDuplicateExports(node, id.name);
|
8473 | } else if (node.declaration.type === "VariableDeclaration") {
|
8474 | for (let _i3 = 0, _node$declaration$dec = node.declaration.declarations; _i3 < _node$declaration$dec.length; _i3++) {
|
8475 | const declaration = _node$declaration$dec[_i3];
|
8476 | this.checkDeclaration(declaration.id);
|
8477 | }
|
8478 | }
|
8479 | }
|
8480 | }
|
8481 |
|
8482 | const currentContextDecorators = this.state.decoratorStack[this.state.decoratorStack.length - 1];
|
8483 |
|
8484 | if (currentContextDecorators.length) {
|
8485 | const isClass = node.declaration && (node.declaration.type === "ClassDeclaration" || node.declaration.type === "ClassExpression");
|
8486 |
|
8487 | if (!node.declaration || !isClass) {
|
8488 | throw this.raise(node.start, "You can only use decorators on an export when exporting a class");
|
8489 | }
|
8490 |
|
8491 | this.takeDecorators(node.declaration);
|
8492 | }
|
8493 | }
|
8494 |
|
8495 | checkDeclaration(node) {
|
8496 | if (node.type === "ObjectPattern") {
|
8497 | for (let _i4 = 0, _node$properties = node.properties; _i4 < _node$properties.length; _i4++) {
|
8498 | const prop = _node$properties[_i4];
|
8499 | this.checkDeclaration(prop);
|
8500 | }
|
8501 | } else if (node.type === "ArrayPattern") {
|
8502 | for (let _i5 = 0, _node$elements = node.elements; _i5 < _node$elements.length; _i5++) {
|
8503 | const elem = _node$elements[_i5];
|
8504 |
|
8505 | if (elem) {
|
8506 | this.checkDeclaration(elem);
|
8507 | }
|
8508 | }
|
8509 | } else if (node.type === "ObjectProperty") {
|
8510 | this.checkDeclaration(node.value);
|
8511 | } else if (node.type === "RestElement") {
|
8512 | this.checkDeclaration(node.argument);
|
8513 | } else if (node.type === "Identifier") {
|
8514 | this.checkDuplicateExports(node, node.name);
|
8515 | }
|
8516 | }
|
8517 |
|
8518 | checkDuplicateExports(node, name) {
|
8519 | if (this.state.exportedIdentifiers.indexOf(name) > -1) {
|
8520 | throw this.raise(node.start, name === "default" ? "Only one default export allowed per module." : `\`${name}\` has already been exported. Exported identifiers must be unique.`);
|
8521 | }
|
8522 |
|
8523 | this.state.exportedIdentifiers.push(name);
|
8524 | }
|
8525 |
|
8526 | parseExportSpecifiers() {
|
8527 | const nodes = [];
|
8528 | let first = true;
|
8529 | let needsFrom;
|
8530 | this.expect(types.braceL);
|
8531 |
|
8532 | while (!this.eat(types.braceR)) {
|
8533 | if (first) {
|
8534 | first = false;
|
8535 | } else {
|
8536 | this.expect(types.comma);
|
8537 | if (this.eat(types.braceR)) break;
|
8538 | }
|
8539 |
|
8540 | const isDefault = this.match(types._default);
|
8541 | if (isDefault && !needsFrom) needsFrom = true;
|
8542 | const node = this.startNode();
|
8543 | node.local = this.parseIdentifier(isDefault);
|
8544 | node.exported = this.eatContextual("as") ? this.parseIdentifier(true) : node.local.__clone();
|
8545 | nodes.push(this.finishNode(node, "ExportSpecifier"));
|
8546 | }
|
8547 |
|
8548 | if (needsFrom && !this.isContextual("from")) {
|
8549 | this.unexpected();
|
8550 | }
|
8551 |
|
8552 | return nodes;
|
8553 | }
|
8554 |
|
8555 | parseImport(node) {
|
8556 | node.specifiers = [];
|
8557 |
|
8558 | if (!this.match(types.string)) {
|
8559 | const hasDefault = this.maybeParseDefaultImportSpecifier(node);
|
8560 | const parseNext = !hasDefault || this.eat(types.comma);
|
8561 | const hasStar = parseNext && this.maybeParseStarImportSpecifier(node);
|
8562 | if (parseNext && !hasStar) this.parseNamedImportSpecifiers(node);
|
8563 | this.expectContextual("from");
|
8564 | }
|
8565 |
|
8566 | node.source = this.parseImportSource();
|
8567 | this.semicolon();
|
8568 | return this.finishNode(node, "ImportDeclaration");
|
8569 | }
|
8570 |
|
8571 | parseImportSource() {
|
8572 | if (!this.match(types.string)) this.unexpected();
|
8573 | return this.parseExprAtom();
|
8574 | }
|
8575 |
|
8576 | shouldParseDefaultImport(node) {
|
8577 | return this.match(types.name);
|
8578 | }
|
8579 |
|
8580 | parseImportSpecifierLocal(node, specifier, type, contextDescription) {
|
8581 | specifier.local = this.parseIdentifier();
|
8582 | this.checkLVal(specifier.local, true, undefined, contextDescription);
|
8583 | node.specifiers.push(this.finishNode(specifier, type));
|
8584 | }
|
8585 |
|
8586 | maybeParseDefaultImportSpecifier(node) {
|
8587 | if (this.shouldParseDefaultImport(node)) {
|
8588 | this.parseImportSpecifierLocal(node, this.startNode(), "ImportDefaultSpecifier", "default import specifier");
|
8589 | return true;
|
8590 | }
|
8591 |
|
8592 | return false;
|
8593 | }
|
8594 |
|
8595 | maybeParseStarImportSpecifier(node) {
|
8596 | if (this.match(types.star)) {
|
8597 | const specifier = this.startNode();
|
8598 | this.next();
|
8599 | this.expectContextual("as");
|
8600 | this.parseImportSpecifierLocal(node, specifier, "ImportNamespaceSpecifier", "import namespace specifier");
|
8601 | return true;
|
8602 | }
|
8603 |
|
8604 | return false;
|
8605 | }
|
8606 |
|
8607 | parseNamedImportSpecifiers(node) {
|
8608 | let first = true;
|
8609 | this.expect(types.braceL);
|
8610 |
|
8611 | while (!this.eat(types.braceR)) {
|
8612 | if (first) {
|
8613 | first = false;
|
8614 | } else {
|
8615 | if (this.eat(types.colon)) {
|
8616 | this.unexpected(null, "ES2015 named imports do not destructure. " + "Use another statement for destructuring after the import.");
|
8617 | }
|
8618 |
|
8619 | this.expect(types.comma);
|
8620 | if (this.eat(types.braceR)) break;
|
8621 | }
|
8622 |
|
8623 | this.parseImportSpecifier(node);
|
8624 | }
|
8625 | }
|
8626 |
|
8627 | parseImportSpecifier(node) {
|
8628 | const specifier = this.startNode();
|
8629 | specifier.imported = this.parseIdentifier(true);
|
8630 |
|
8631 | if (this.eatContextual("as")) {
|
8632 | specifier.local = this.parseIdentifier();
|
8633 | } else {
|
8634 | this.checkReservedWord(specifier.imported.name, specifier.start, true, true);
|
8635 | specifier.local = specifier.imported.__clone();
|
8636 | }
|
8637 |
|
8638 | this.checkLVal(specifier.local, true, undefined, "import specifier");
|
8639 | node.specifiers.push(this.finishNode(specifier, "ImportSpecifier"));
|
8640 | }
|
8641 |
|
8642 | }
|
8643 |
|
8644 | class Parser extends StatementParser {
|
8645 | constructor(options, input) {
|
8646 | options = getOptions(options);
|
8647 | super(options, input);
|
8648 | this.options = options;
|
8649 | this.inModule = this.options.sourceType === "module";
|
8650 | this.plugins = pluginsMap(this.options.plugins);
|
8651 | this.filename = options.sourceFilename;
|
8652 | }
|
8653 |
|
8654 | parse() {
|
8655 | const file = this.startNode();
|
8656 | const program = this.startNode();
|
8657 | this.nextToken();
|
8658 | return this.parseTopLevel(file, program);
|
8659 | }
|
8660 |
|
8661 | }
|
8662 |
|
8663 | function pluginsMap(plugins) {
|
8664 | const pluginMap = new Map();
|
8665 |
|
8666 | for (let _i = 0; _i < plugins.length; _i++) {
|
8667 | const plugin = plugins[_i];
|
8668 | const [name, options] = Array.isArray(plugin) ? plugin : [plugin, {}];
|
8669 | if (!pluginMap.has(name)) pluginMap.set(name, options || {});
|
8670 | }
|
8671 |
|
8672 | return pluginMap;
|
8673 | }
|
8674 |
|
8675 | function nonNull(x) {
|
8676 | if (x == null) {
|
8677 | throw new Error(`Unexpected ${x} value.`);
|
8678 | }
|
8679 |
|
8680 | return x;
|
8681 | }
|
8682 |
|
8683 | function assert(x) {
|
8684 | if (!x) {
|
8685 | throw new Error("Assert fail");
|
8686 | }
|
8687 | }
|
8688 |
|
8689 | function keywordTypeFromName(value) {
|
8690 | switch (value) {
|
8691 | case "any":
|
8692 | return "TSAnyKeyword";
|
8693 |
|
8694 | case "boolean":
|
8695 | return "TSBooleanKeyword";
|
8696 |
|
8697 | case "bigint":
|
8698 | return "TSBigIntKeyword";
|
8699 |
|
8700 | case "never":
|
8701 | return "TSNeverKeyword";
|
8702 |
|
8703 | case "number":
|
8704 | return "TSNumberKeyword";
|
8705 |
|
8706 | case "object":
|
8707 | return "TSObjectKeyword";
|
8708 |
|
8709 | case "string":
|
8710 | return "TSStringKeyword";
|
8711 |
|
8712 | case "symbol":
|
8713 | return "TSSymbolKeyword";
|
8714 |
|
8715 | case "undefined":
|
8716 | return "TSUndefinedKeyword";
|
8717 |
|
8718 | case "unknown":
|
8719 | return "TSUnknownKeyword";
|
8720 |
|
8721 | default:
|
8722 | return undefined;
|
8723 | }
|
8724 | }
|
8725 |
|
8726 | var typescript = (superClass => class extends superClass {
|
8727 | tsIsIdentifier() {
|
8728 | return this.match(types.name);
|
8729 | }
|
8730 |
|
8731 | tsNextTokenCanFollowModifier() {
|
8732 | this.next();
|
8733 | return !this.hasPrecedingLineBreak() && !this.match(types.parenL) && !this.match(types.parenR) && !this.match(types.colon) && !this.match(types.eq) && !this.match(types.question);
|
8734 | }
|
8735 |
|
8736 | tsParseModifier(allowedModifiers) {
|
8737 | if (!this.match(types.name)) {
|
8738 | return undefined;
|
8739 | }
|
8740 |
|
8741 | const modifier = this.state.value;
|
8742 |
|
8743 | if (allowedModifiers.indexOf(modifier) !== -1 && this.tsTryParse(this.tsNextTokenCanFollowModifier.bind(this))) {
|
8744 | return modifier;
|
8745 | }
|
8746 |
|
8747 | return undefined;
|
8748 | }
|
8749 |
|
8750 | tsIsListTerminator(kind) {
|
8751 | switch (kind) {
|
8752 | case "EnumMembers":
|
8753 | case "TypeMembers":
|
8754 | return this.match(types.braceR);
|
8755 |
|
8756 | case "HeritageClauseElement":
|
8757 | return this.match(types.braceL);
|
8758 |
|
8759 | case "TupleElementTypes":
|
8760 | return this.match(types.bracketR);
|
8761 |
|
8762 | case "TypeParametersOrArguments":
|
8763 | return this.isRelational(">");
|
8764 | }
|
8765 |
|
8766 | throw new Error("Unreachable");
|
8767 | }
|
8768 |
|
8769 | tsParseList(kind, parseElement) {
|
8770 | const result = [];
|
8771 |
|
8772 | while (!this.tsIsListTerminator(kind)) {
|
8773 | result.push(parseElement());
|
8774 | }
|
8775 |
|
8776 | return result;
|
8777 | }
|
8778 |
|
8779 | tsParseDelimitedList(kind, parseElement) {
|
8780 | return nonNull(this.tsParseDelimitedListWorker(kind, parseElement, true));
|
8781 | }
|
8782 |
|
8783 | tsTryParseDelimitedList(kind, parseElement) {
|
8784 | return this.tsParseDelimitedListWorker(kind, parseElement, false);
|
8785 | }
|
8786 |
|
8787 | tsParseDelimitedListWorker(kind, parseElement, expectSuccess) {
|
8788 | const result = [];
|
8789 |
|
8790 | while (true) {
|
8791 | if (this.tsIsListTerminator(kind)) {
|
8792 | break;
|
8793 | }
|
8794 |
|
8795 | const element = parseElement();
|
8796 |
|
8797 | if (element == null) {
|
8798 | return undefined;
|
8799 | }
|
8800 |
|
8801 | result.push(element);
|
8802 |
|
8803 | if (this.eat(types.comma)) {
|
8804 | continue;
|
8805 | }
|
8806 |
|
8807 | if (this.tsIsListTerminator(kind)) {
|
8808 | break;
|
8809 | }
|
8810 |
|
8811 | if (expectSuccess) {
|
8812 | this.expect(types.comma);
|
8813 | }
|
8814 |
|
8815 | return undefined;
|
8816 | }
|
8817 |
|
8818 | return result;
|
8819 | }
|
8820 |
|
8821 | tsParseBracketedList(kind, parseElement, bracket, skipFirstToken) {
|
8822 | if (!skipFirstToken) {
|
8823 | if (bracket) {
|
8824 | this.expect(types.bracketL);
|
8825 | } else {
|
8826 | this.expectRelational("<");
|
8827 | }
|
8828 | }
|
8829 |
|
8830 | const result = this.tsParseDelimitedList(kind, parseElement);
|
8831 |
|
8832 | if (bracket) {
|
8833 | this.expect(types.bracketR);
|
8834 | } else {
|
8835 | this.expectRelational(">");
|
8836 | }
|
8837 |
|
8838 | return result;
|
8839 | }
|
8840 |
|
8841 | tsParseImportType() {
|
8842 | const node = this.startNode();
|
8843 | this.expect(types._import);
|
8844 | this.expect(types.parenL);
|
8845 |
|
8846 | if (!this.match(types.string)) {
|
8847 | throw this.unexpected(null, "Argument in a type import must be a string literal");
|
8848 | }
|
8849 |
|
8850 | node.argument = this.parseLiteral(this.state.value, "StringLiteral");
|
8851 | this.expect(types.parenR);
|
8852 |
|
8853 | if (this.eat(types.dot)) {
|
8854 | node.qualifier = this.tsParseEntityName(true);
|
8855 | }
|
8856 |
|
8857 | if (this.isRelational("<")) {
|
8858 | node.typeParameters = this.tsParseTypeArguments();
|
8859 | }
|
8860 |
|
8861 | return this.finishNode(node, "TSImportType");
|
8862 | }
|
8863 |
|
8864 | tsParseEntityName(allowReservedWords) {
|
8865 | let entity = this.parseIdentifier();
|
8866 |
|
8867 | while (this.eat(types.dot)) {
|
8868 | const node = this.startNodeAtNode(entity);
|
8869 | node.left = entity;
|
8870 | node.right = this.parseIdentifier(allowReservedWords);
|
8871 | entity = this.finishNode(node, "TSQualifiedName");
|
8872 | }
|
8873 |
|
8874 | return entity;
|
8875 | }
|
8876 |
|
8877 | tsParseTypeReference() {
|
8878 | const node = this.startNode();
|
8879 | node.typeName = this.tsParseEntityName(false);
|
8880 |
|
8881 | if (!this.hasPrecedingLineBreak() && this.isRelational("<")) {
|
8882 | node.typeParameters = this.tsParseTypeArguments();
|
8883 | }
|
8884 |
|
8885 | return this.finishNode(node, "TSTypeReference");
|
8886 | }
|
8887 |
|
8888 | tsParseThisTypePredicate(lhs) {
|
8889 | this.next();
|
8890 | const node = this.startNodeAtNode(lhs);
|
8891 | node.parameterName = lhs;
|
8892 | node.typeAnnotation = this.tsParseTypeAnnotation(false);
|
8893 | return this.finishNode(node, "TSTypePredicate");
|
8894 | }
|
8895 |
|
8896 | tsParseThisTypeNode() {
|
8897 | const node = this.startNode();
|
8898 | this.next();
|
8899 | return this.finishNode(node, "TSThisType");
|
8900 | }
|
8901 |
|
8902 | tsParseTypeQuery() {
|
8903 | const node = this.startNode();
|
8904 | this.expect(types._typeof);
|
8905 |
|
8906 | if (this.match(types._import)) {
|
8907 | node.exprName = this.tsParseImportType();
|
8908 | } else {
|
8909 | node.exprName = this.tsParseEntityName(true);
|
8910 | }
|
8911 |
|
8912 | return this.finishNode(node, "TSTypeQuery");
|
8913 | }
|
8914 |
|
8915 | tsParseTypeParameter() {
|
8916 | const node = this.startNode();
|
8917 | node.name = this.parseIdentifierName(node.start);
|
8918 | node.constraint = this.tsEatThenParseType(types._extends);
|
8919 | node.default = this.tsEatThenParseType(types.eq);
|
8920 | return this.finishNode(node, "TSTypeParameter");
|
8921 | }
|
8922 |
|
8923 | tsTryParseTypeParameters() {
|
8924 | if (this.isRelational("<")) {
|
8925 | return this.tsParseTypeParameters();
|
8926 | }
|
8927 | }
|
8928 |
|
8929 | tsParseTypeParameters() {
|
8930 | const node = this.startNode();
|
8931 |
|
8932 | if (this.isRelational("<") || this.match(types.jsxTagStart)) {
|
8933 | this.next();
|
8934 | } else {
|
8935 | this.unexpected();
|
8936 | }
|
8937 |
|
8938 | node.params = this.tsParseBracketedList("TypeParametersOrArguments", this.tsParseTypeParameter.bind(this), false, true);
|
8939 | return this.finishNode(node, "TSTypeParameterDeclaration");
|
8940 | }
|
8941 |
|
8942 | tsFillSignature(returnToken, signature) {
|
8943 | const returnTokenRequired = returnToken === types.arrow;
|
8944 | signature.typeParameters = this.tsTryParseTypeParameters();
|
8945 | this.expect(types.parenL);
|
8946 | signature.parameters = this.tsParseBindingListForSignature();
|
8947 |
|
8948 | if (returnTokenRequired) {
|
8949 | signature.typeAnnotation = this.tsParseTypeOrTypePredicateAnnotation(returnToken);
|
8950 | } else if (this.match(returnToken)) {
|
8951 | signature.typeAnnotation = this.tsParseTypeOrTypePredicateAnnotation(returnToken);
|
8952 | }
|
8953 | }
|
8954 |
|
8955 | tsParseBindingListForSignature() {
|
8956 | return this.parseBindingList(types.parenR).map(pattern => {
|
8957 | if (pattern.type !== "Identifier" && pattern.type !== "RestElement" && pattern.type !== "ObjectPattern" && pattern.type !== "ArrayPattern") {
|
8958 | throw this.unexpected(pattern.start, `Name in a signature must be an Identifier, ObjectPattern or ArrayPattern, instead got ${pattern.type}`);
|
8959 | }
|
8960 |
|
8961 | return pattern;
|
8962 | });
|
8963 | }
|
8964 |
|
8965 | tsParseTypeMemberSemicolon() {
|
8966 | if (!this.eat(types.comma)) {
|
8967 | this.semicolon();
|
8968 | }
|
8969 | }
|
8970 |
|
8971 | tsParseSignatureMember(kind) {
|
8972 | const node = this.startNode();
|
8973 |
|
8974 | if (kind === "TSConstructSignatureDeclaration") {
|
8975 | this.expect(types._new);
|
8976 | }
|
8977 |
|
8978 | this.tsFillSignature(types.colon, node);
|
8979 | this.tsParseTypeMemberSemicolon();
|
8980 | return this.finishNode(node, kind);
|
8981 | }
|
8982 |
|
8983 | tsIsUnambiguouslyIndexSignature() {
|
8984 | this.next();
|
8985 | return this.eat(types.name) && this.match(types.colon);
|
8986 | }
|
8987 |
|
8988 | tsTryParseIndexSignature(node) {
|
8989 | if (!(this.match(types.bracketL) && this.tsLookAhead(this.tsIsUnambiguouslyIndexSignature.bind(this)))) {
|
8990 | return undefined;
|
8991 | }
|
8992 |
|
8993 | this.expect(types.bracketL);
|
8994 | const id = this.parseIdentifier();
|
8995 | id.typeAnnotation = this.tsParseTypeAnnotation();
|
8996 | this.finishNode(id, "Identifier");
|
8997 | this.expect(types.bracketR);
|
8998 | node.parameters = [id];
|
8999 | const type = this.tsTryParseTypeAnnotation();
|
9000 | if (type) node.typeAnnotation = type;
|
9001 | this.tsParseTypeMemberSemicolon();
|
9002 | return this.finishNode(node, "TSIndexSignature");
|
9003 | }
|
9004 |
|
9005 | tsParsePropertyOrMethodSignature(node, readonly) {
|
9006 | this.parsePropertyName(node);
|
9007 | if (this.eat(types.question)) node.optional = true;
|
9008 | const nodeAny = node;
|
9009 |
|
9010 | if (!readonly && (this.match(types.parenL) || this.isRelational("<"))) {
|
9011 | const method = nodeAny;
|
9012 | this.tsFillSignature(types.colon, method);
|
9013 | this.tsParseTypeMemberSemicolon();
|
9014 | return this.finishNode(method, "TSMethodSignature");
|
9015 | } else {
|
9016 | const property = nodeAny;
|
9017 | if (readonly) property.readonly = true;
|
9018 | const type = this.tsTryParseTypeAnnotation();
|
9019 | if (type) property.typeAnnotation = type;
|
9020 | this.tsParseTypeMemberSemicolon();
|
9021 | return this.finishNode(property, "TSPropertySignature");
|
9022 | }
|
9023 | }
|
9024 |
|
9025 | tsParseTypeMember() {
|
9026 | if (this.match(types.parenL) || this.isRelational("<")) {
|
9027 | return this.tsParseSignatureMember("TSCallSignatureDeclaration");
|
9028 | }
|
9029 |
|
9030 | if (this.match(types._new) && this.tsLookAhead(this.tsIsStartOfConstructSignature.bind(this))) {
|
9031 | return this.tsParseSignatureMember("TSConstructSignatureDeclaration");
|
9032 | }
|
9033 |
|
9034 | const node = this.startNode();
|
9035 | const readonly = !!this.tsParseModifier(["readonly"]);
|
9036 | const idx = this.tsTryParseIndexSignature(node);
|
9037 |
|
9038 | if (idx) {
|
9039 | if (readonly) node.readonly = true;
|
9040 | return idx;
|
9041 | }
|
9042 |
|
9043 | return this.tsParsePropertyOrMethodSignature(node, readonly);
|
9044 | }
|
9045 |
|
9046 | tsIsStartOfConstructSignature() {
|
9047 | this.next();
|
9048 | return this.match(types.parenL) || this.isRelational("<");
|
9049 | }
|
9050 |
|
9051 | tsParseTypeLiteral() {
|
9052 | const node = this.startNode();
|
9053 | node.members = this.tsParseObjectTypeMembers();
|
9054 | return this.finishNode(node, "TSTypeLiteral");
|
9055 | }
|
9056 |
|
9057 | tsParseObjectTypeMembers() {
|
9058 | this.expect(types.braceL);
|
9059 | const members = this.tsParseList("TypeMembers", this.tsParseTypeMember.bind(this));
|
9060 | this.expect(types.braceR);
|
9061 | return members;
|
9062 | }
|
9063 |
|
9064 | tsIsStartOfMappedType() {
|
9065 | this.next();
|
9066 |
|
9067 | if (this.eat(types.plusMin)) {
|
9068 | return this.isContextual("readonly");
|
9069 | }
|
9070 |
|
9071 | if (this.isContextual("readonly")) {
|
9072 | this.next();
|
9073 | }
|
9074 |
|
9075 | if (!this.match(types.bracketL)) {
|
9076 | return false;
|
9077 | }
|
9078 |
|
9079 | this.next();
|
9080 |
|
9081 | if (!this.tsIsIdentifier()) {
|
9082 | return false;
|
9083 | }
|
9084 |
|
9085 | this.next();
|
9086 | return this.match(types._in);
|
9087 | }
|
9088 |
|
9089 | tsParseMappedTypeParameter() {
|
9090 | const node = this.startNode();
|
9091 | node.name = this.parseIdentifierName(node.start);
|
9092 | node.constraint = this.tsExpectThenParseType(types._in);
|
9093 | return this.finishNode(node, "TSTypeParameter");
|
9094 | }
|
9095 |
|
9096 | tsParseMappedType() {
|
9097 | const node = this.startNode();
|
9098 | this.expect(types.braceL);
|
9099 |
|
9100 | if (this.match(types.plusMin)) {
|
9101 | node.readonly = this.state.value;
|
9102 | this.next();
|
9103 | this.expectContextual("readonly");
|
9104 | } else if (this.eatContextual("readonly")) {
|
9105 | node.readonly = true;
|
9106 | }
|
9107 |
|
9108 | this.expect(types.bracketL);
|
9109 | node.typeParameter = this.tsParseMappedTypeParameter();
|
9110 | this.expect(types.bracketR);
|
9111 |
|
9112 | if (this.match(types.plusMin)) {
|
9113 | node.optional = this.state.value;
|
9114 | this.next();
|
9115 | this.expect(types.question);
|
9116 | } else if (this.eat(types.question)) {
|
9117 | node.optional = true;
|
9118 | }
|
9119 |
|
9120 | node.typeAnnotation = this.tsTryParseType();
|
9121 | this.semicolon();
|
9122 | this.expect(types.braceR);
|
9123 | return this.finishNode(node, "TSMappedType");
|
9124 | }
|
9125 |
|
9126 | tsParseTupleType() {
|
9127 | const node = this.startNode();
|
9128 | node.elementTypes = this.tsParseBracketedList("TupleElementTypes", this.tsParseTupleElementType.bind(this), true, false);
|
9129 | let seenOptionalElement = false;
|
9130 | node.elementTypes.forEach(elementNode => {
|
9131 | if (elementNode.type === "TSOptionalType") {
|
9132 | seenOptionalElement = true;
|
9133 | } else if (seenOptionalElement && elementNode.type !== "TSRestType") {
|
9134 | this.raise(elementNode.start, "A required element cannot follow an optional element.");
|
9135 | }
|
9136 | });
|
9137 | return this.finishNode(node, "TSTupleType");
|
9138 | }
|
9139 |
|
9140 | tsParseTupleElementType() {
|
9141 | if (this.match(types.ellipsis)) {
|
9142 | const restNode = this.startNode();
|
9143 | this.next();
|
9144 | restNode.typeAnnotation = this.tsParseType();
|
9145 | this.checkCommaAfterRest(types.bracketR, "type");
|
9146 | return this.finishNode(restNode, "TSRestType");
|
9147 | }
|
9148 |
|
9149 | const type = this.tsParseType();
|
9150 |
|
9151 | if (this.eat(types.question)) {
|
9152 | const optionalTypeNode = this.startNodeAtNode(type);
|
9153 | optionalTypeNode.typeAnnotation = type;
|
9154 | return this.finishNode(optionalTypeNode, "TSOptionalType");
|
9155 | }
|
9156 |
|
9157 | return type;
|
9158 | }
|
9159 |
|
9160 | tsParseParenthesizedType() {
|
9161 | const node = this.startNode();
|
9162 | this.expect(types.parenL);
|
9163 | node.typeAnnotation = this.tsParseType();
|
9164 | this.expect(types.parenR);
|
9165 | return this.finishNode(node, "TSParenthesizedType");
|
9166 | }
|
9167 |
|
9168 | tsParseFunctionOrConstructorType(type) {
|
9169 | const node = this.startNode();
|
9170 |
|
9171 | if (type === "TSConstructorType") {
|
9172 | this.expect(types._new);
|
9173 | }
|
9174 |
|
9175 | this.tsFillSignature(types.arrow, node);
|
9176 | return this.finishNode(node, type);
|
9177 | }
|
9178 |
|
9179 | tsParseLiteralTypeNode() {
|
9180 | const node = this.startNode();
|
9181 |
|
9182 | node.literal = (() => {
|
9183 | switch (this.state.type) {
|
9184 | case types.num:
|
9185 | return this.parseLiteral(this.state.value, "NumericLiteral");
|
9186 |
|
9187 | case types.string:
|
9188 | return this.parseLiteral(this.state.value, "StringLiteral");
|
9189 |
|
9190 | case types._true:
|
9191 | case types._false:
|
9192 | return this.parseBooleanLiteral();
|
9193 |
|
9194 | default:
|
9195 | throw this.unexpected();
|
9196 | }
|
9197 | })();
|
9198 |
|
9199 | return this.finishNode(node, "TSLiteralType");
|
9200 | }
|
9201 |
|
9202 | tsParseNonArrayType() {
|
9203 | switch (this.state.type) {
|
9204 | case types.name:
|
9205 | case types._void:
|
9206 | case types._null:
|
9207 | {
|
9208 | const type = this.match(types._void) ? "TSVoidKeyword" : this.match(types._null) ? "TSNullKeyword" : keywordTypeFromName(this.state.value);
|
9209 |
|
9210 | if (type !== undefined && this.lookahead().type !== types.dot) {
|
9211 | const node = this.startNode();
|
9212 | this.next();
|
9213 | return this.finishNode(node, type);
|
9214 | }
|
9215 |
|
9216 | return this.tsParseTypeReference();
|
9217 | }
|
9218 |
|
9219 | case types.string:
|
9220 | case types.num:
|
9221 | case types._true:
|
9222 | case types._false:
|
9223 | return this.tsParseLiteralTypeNode();
|
9224 |
|
9225 | case types.plusMin:
|
9226 | if (this.state.value === "-") {
|
9227 | const node = this.startNode();
|
9228 | this.next();
|
9229 |
|
9230 | if (!this.match(types.num)) {
|
9231 | throw this.unexpected();
|
9232 | }
|
9233 |
|
9234 | node.literal = this.parseLiteral(-this.state.value, "NumericLiteral", node.start, node.loc.start);
|
9235 | return this.finishNode(node, "TSLiteralType");
|
9236 | }
|
9237 |
|
9238 | break;
|
9239 |
|
9240 | case types._this:
|
9241 | {
|
9242 | const thisKeyword = this.tsParseThisTypeNode();
|
9243 |
|
9244 | if (this.isContextual("is") && !this.hasPrecedingLineBreak()) {
|
9245 | return this.tsParseThisTypePredicate(thisKeyword);
|
9246 | } else {
|
9247 | return thisKeyword;
|
9248 | }
|
9249 | }
|
9250 |
|
9251 | case types._typeof:
|
9252 | return this.tsParseTypeQuery();
|
9253 |
|
9254 | case types._import:
|
9255 | return this.tsParseImportType();
|
9256 |
|
9257 | case types.braceL:
|
9258 | return this.tsLookAhead(this.tsIsStartOfMappedType.bind(this)) ? this.tsParseMappedType() : this.tsParseTypeLiteral();
|
9259 |
|
9260 | case types.bracketL:
|
9261 | return this.tsParseTupleType();
|
9262 |
|
9263 | case types.parenL:
|
9264 | return this.tsParseParenthesizedType();
|
9265 | }
|
9266 |
|
9267 | throw this.unexpected();
|
9268 | }
|
9269 |
|
9270 | tsParseArrayTypeOrHigher() {
|
9271 | let type = this.tsParseNonArrayType();
|
9272 |
|
9273 | while (!this.hasPrecedingLineBreak() && this.eat(types.bracketL)) {
|
9274 | if (this.match(types.bracketR)) {
|
9275 | const node = this.startNodeAtNode(type);
|
9276 | node.elementType = type;
|
9277 | this.expect(types.bracketR);
|
9278 | type = this.finishNode(node, "TSArrayType");
|
9279 | } else {
|
9280 | const node = this.startNodeAtNode(type);
|
9281 | node.objectType = type;
|
9282 | node.indexType = this.tsParseType();
|
9283 | this.expect(types.bracketR);
|
9284 | type = this.finishNode(node, "TSIndexedAccessType");
|
9285 | }
|
9286 | }
|
9287 |
|
9288 | return type;
|
9289 | }
|
9290 |
|
9291 | tsParseTypeOperator(operator) {
|
9292 | const node = this.startNode();
|
9293 | this.expectContextual(operator);
|
9294 | node.operator = operator;
|
9295 | node.typeAnnotation = this.tsParseTypeOperatorOrHigher();
|
9296 | return this.finishNode(node, "TSTypeOperator");
|
9297 | }
|
9298 |
|
9299 | tsParseInferType() {
|
9300 | const node = this.startNode();
|
9301 | this.expectContextual("infer");
|
9302 | const typeParameter = this.startNode();
|
9303 | typeParameter.name = this.parseIdentifierName(typeParameter.start);
|
9304 | node.typeParameter = this.finishNode(typeParameter, "TSTypeParameter");
|
9305 | return this.finishNode(node, "TSInferType");
|
9306 | }
|
9307 |
|
9308 | tsParseTypeOperatorOrHigher() {
|
9309 | const operator = ["keyof", "unique"].find(kw => this.isContextual(kw));
|
9310 | return operator ? this.tsParseTypeOperator(operator) : this.isContextual("infer") ? this.tsParseInferType() : this.tsParseArrayTypeOrHigher();
|
9311 | }
|
9312 |
|
9313 | tsParseUnionOrIntersectionType(kind, parseConstituentType, operator) {
|
9314 | this.eat(operator);
|
9315 | let type = parseConstituentType();
|
9316 |
|
9317 | if (this.match(operator)) {
|
9318 | const types$$1 = [type];
|
9319 |
|
9320 | while (this.eat(operator)) {
|
9321 | types$$1.push(parseConstituentType());
|
9322 | }
|
9323 |
|
9324 | const node = this.startNodeAtNode(type);
|
9325 | node.types = types$$1;
|
9326 | type = this.finishNode(node, kind);
|
9327 | }
|
9328 |
|
9329 | return type;
|
9330 | }
|
9331 |
|
9332 | tsParseIntersectionTypeOrHigher() {
|
9333 | return this.tsParseUnionOrIntersectionType("TSIntersectionType", this.tsParseTypeOperatorOrHigher.bind(this), types.bitwiseAND);
|
9334 | }
|
9335 |
|
9336 | tsParseUnionTypeOrHigher() {
|
9337 | return this.tsParseUnionOrIntersectionType("TSUnionType", this.tsParseIntersectionTypeOrHigher.bind(this), types.bitwiseOR);
|
9338 | }
|
9339 |
|
9340 | tsIsStartOfFunctionType() {
|
9341 | if (this.isRelational("<")) {
|
9342 | return true;
|
9343 | }
|
9344 |
|
9345 | return this.match(types.parenL) && this.tsLookAhead(this.tsIsUnambiguouslyStartOfFunctionType.bind(this));
|
9346 | }
|
9347 |
|
9348 | tsSkipParameterStart() {
|
9349 | if (this.match(types.name) || this.match(types._this)) {
|
9350 | this.next();
|
9351 | return true;
|
9352 | }
|
9353 |
|
9354 | if (this.match(types.braceL)) {
|
9355 | let braceStackCounter = 1;
|
9356 | this.next();
|
9357 |
|
9358 | while (braceStackCounter > 0) {
|
9359 | if (this.match(types.braceL)) {
|
9360 | ++braceStackCounter;
|
9361 | } else if (this.match(types.braceR)) {
|
9362 | --braceStackCounter;
|
9363 | }
|
9364 |
|
9365 | this.next();
|
9366 | }
|
9367 |
|
9368 | return true;
|
9369 | }
|
9370 |
|
9371 | if (this.match(types.bracketL)) {
|
9372 | let braceStackCounter = 1;
|
9373 | this.next();
|
9374 |
|
9375 | while (braceStackCounter > 0) {
|
9376 | if (this.match(types.bracketL)) {
|
9377 | ++braceStackCounter;
|
9378 | } else if (this.match(types.bracketR)) {
|
9379 | --braceStackCounter;
|
9380 | }
|
9381 |
|
9382 | this.next();
|
9383 | }
|
9384 |
|
9385 | return true;
|
9386 | }
|
9387 |
|
9388 | return false;
|
9389 | }
|
9390 |
|
9391 | tsIsUnambiguouslyStartOfFunctionType() {
|
9392 | this.next();
|
9393 |
|
9394 | if (this.match(types.parenR) || this.match(types.ellipsis)) {
|
9395 | return true;
|
9396 | }
|
9397 |
|
9398 | if (this.tsSkipParameterStart()) {
|
9399 | if (this.match(types.colon) || this.match(types.comma) || this.match(types.question) || this.match(types.eq)) {
|
9400 | return true;
|
9401 | }
|
9402 |
|
9403 | if (this.match(types.parenR)) {
|
9404 | this.next();
|
9405 |
|
9406 | if (this.match(types.arrow)) {
|
9407 | return true;
|
9408 | }
|
9409 | }
|
9410 | }
|
9411 |
|
9412 | return false;
|
9413 | }
|
9414 |
|
9415 | tsParseTypeOrTypePredicateAnnotation(returnToken) {
|
9416 | return this.tsInType(() => {
|
9417 | const t = this.startNode();
|
9418 | this.expect(returnToken);
|
9419 | const typePredicateVariable = this.tsIsIdentifier() && this.tsTryParse(this.tsParseTypePredicatePrefix.bind(this));
|
9420 |
|
9421 | if (!typePredicateVariable) {
|
9422 | return this.tsParseTypeAnnotation(false, t);
|
9423 | }
|
9424 |
|
9425 | const type = this.tsParseTypeAnnotation(false);
|
9426 | const node = this.startNodeAtNode(typePredicateVariable);
|
9427 | node.parameterName = typePredicateVariable;
|
9428 | node.typeAnnotation = type;
|
9429 | t.typeAnnotation = this.finishNode(node, "TSTypePredicate");
|
9430 | return this.finishNode(t, "TSTypeAnnotation");
|
9431 | });
|
9432 | }
|
9433 |
|
9434 | tsTryParseTypeOrTypePredicateAnnotation() {
|
9435 | return this.match(types.colon) ? this.tsParseTypeOrTypePredicateAnnotation(types.colon) : undefined;
|
9436 | }
|
9437 |
|
9438 | tsTryParseTypeAnnotation() {
|
9439 | return this.match(types.colon) ? this.tsParseTypeAnnotation() : undefined;
|
9440 | }
|
9441 |
|
9442 | tsTryParseType() {
|
9443 | return this.tsEatThenParseType(types.colon);
|
9444 | }
|
9445 |
|
9446 | tsParseTypePredicatePrefix() {
|
9447 | const id = this.parseIdentifier();
|
9448 |
|
9449 | if (this.isContextual("is") && !this.hasPrecedingLineBreak()) {
|
9450 | this.next();
|
9451 | return id;
|
9452 | }
|
9453 | }
|
9454 |
|
9455 | tsParseTypeAnnotation(eatColon = true, t = this.startNode()) {
|
9456 | this.tsInType(() => {
|
9457 | if (eatColon) this.expect(types.colon);
|
9458 | t.typeAnnotation = this.tsParseType();
|
9459 | });
|
9460 | return this.finishNode(t, "TSTypeAnnotation");
|
9461 | }
|
9462 |
|
9463 | tsParseType() {
|
9464 | assert(this.state.inType);
|
9465 | const type = this.tsParseNonConditionalType();
|
9466 |
|
9467 | if (this.hasPrecedingLineBreak() || !this.eat(types._extends)) {
|
9468 | return type;
|
9469 | }
|
9470 |
|
9471 | const node = this.startNodeAtNode(type);
|
9472 | node.checkType = type;
|
9473 | node.extendsType = this.tsParseNonConditionalType();
|
9474 | this.expect(types.question);
|
9475 | node.trueType = this.tsParseType();
|
9476 | this.expect(types.colon);
|
9477 | node.falseType = this.tsParseType();
|
9478 | return this.finishNode(node, "TSConditionalType");
|
9479 | }
|
9480 |
|
9481 | tsParseNonConditionalType() {
|
9482 | if (this.tsIsStartOfFunctionType()) {
|
9483 | return this.tsParseFunctionOrConstructorType("TSFunctionType");
|
9484 | }
|
9485 |
|
9486 | if (this.match(types._new)) {
|
9487 | return this.tsParseFunctionOrConstructorType("TSConstructorType");
|
9488 | }
|
9489 |
|
9490 | return this.tsParseUnionTypeOrHigher();
|
9491 | }
|
9492 |
|
9493 | tsParseTypeAssertion() {
|
9494 | const node = this.startNode();
|
9495 | this.next();
|
9496 | node.typeAnnotation = this.tsInType(() => this.tsParseType());
|
9497 | this.expectRelational(">");
|
9498 | node.expression = this.parseMaybeUnary();
|
9499 | return this.finishNode(node, "TSTypeAssertion");
|
9500 | }
|
9501 |
|
9502 | tsParseHeritageClause(descriptor) {
|
9503 | const originalStart = this.state.start;
|
9504 | const delimitedList = this.tsParseDelimitedList("HeritageClauseElement", this.tsParseExpressionWithTypeArguments.bind(this));
|
9505 |
|
9506 | if (!delimitedList.length) {
|
9507 | this.raise(originalStart, `'${descriptor}' list cannot be empty.`);
|
9508 | }
|
9509 |
|
9510 | return delimitedList;
|
9511 | }
|
9512 |
|
9513 | tsParseExpressionWithTypeArguments() {
|
9514 | const node = this.startNode();
|
9515 | node.expression = this.tsParseEntityName(false);
|
9516 |
|
9517 | if (this.isRelational("<")) {
|
9518 | node.typeParameters = this.tsParseTypeArguments();
|
9519 | }
|
9520 |
|
9521 | return this.finishNode(node, "TSExpressionWithTypeArguments");
|
9522 | }
|
9523 |
|
9524 | tsParseInterfaceDeclaration(node) {
|
9525 | node.id = this.parseIdentifier();
|
9526 | node.typeParameters = this.tsTryParseTypeParameters();
|
9527 |
|
9528 | if (this.eat(types._extends)) {
|
9529 | node.extends = this.tsParseHeritageClause("extends");
|
9530 | }
|
9531 |
|
9532 | const body = this.startNode();
|
9533 | body.body = this.tsInType(this.tsParseObjectTypeMembers.bind(this));
|
9534 | node.body = this.finishNode(body, "TSInterfaceBody");
|
9535 | return this.finishNode(node, "TSInterfaceDeclaration");
|
9536 | }
|
9537 |
|
9538 | tsParseTypeAliasDeclaration(node) {
|
9539 | node.id = this.parseIdentifier();
|
9540 | node.typeParameters = this.tsTryParseTypeParameters();
|
9541 | node.typeAnnotation = this.tsExpectThenParseType(types.eq);
|
9542 | this.semicolon();
|
9543 | return this.finishNode(node, "TSTypeAliasDeclaration");
|
9544 | }
|
9545 |
|
9546 | tsInNoContext(cb) {
|
9547 | const oldContext = this.state.context;
|
9548 | this.state.context = [oldContext[0]];
|
9549 |
|
9550 | try {
|
9551 | return cb();
|
9552 | } finally {
|
9553 | this.state.context = oldContext;
|
9554 | }
|
9555 | }
|
9556 |
|
9557 | tsInType(cb) {
|
9558 | const oldInType = this.state.inType;
|
9559 | this.state.inType = true;
|
9560 |
|
9561 | try {
|
9562 | return cb();
|
9563 | } finally {
|
9564 | this.state.inType = oldInType;
|
9565 | }
|
9566 | }
|
9567 |
|
9568 | tsEatThenParseType(token) {
|
9569 | return !this.match(token) ? undefined : this.tsNextThenParseType();
|
9570 | }
|
9571 |
|
9572 | tsExpectThenParseType(token) {
|
9573 | return this.tsDoThenParseType(() => this.expect(token));
|
9574 | }
|
9575 |
|
9576 | tsNextThenParseType() {
|
9577 | return this.tsDoThenParseType(() => this.next());
|
9578 | }
|
9579 |
|
9580 | tsDoThenParseType(cb) {
|
9581 | return this.tsInType(() => {
|
9582 | cb();
|
9583 | return this.tsParseType();
|
9584 | });
|
9585 | }
|
9586 |
|
9587 | tsParseEnumMember() {
|
9588 | const node = this.startNode();
|
9589 | node.id = this.match(types.string) ? this.parseLiteral(this.state.value, "StringLiteral") : this.parseIdentifier(true);
|
9590 |
|
9591 | if (this.eat(types.eq)) {
|
9592 | node.initializer = this.parseMaybeAssign();
|
9593 | }
|
9594 |
|
9595 | return this.finishNode(node, "TSEnumMember");
|
9596 | }
|
9597 |
|
9598 | tsParseEnumDeclaration(node, isConst) {
|
9599 | if (isConst) node.const = true;
|
9600 | node.id = this.parseIdentifier();
|
9601 | this.expect(types.braceL);
|
9602 | node.members = this.tsParseDelimitedList("EnumMembers", this.tsParseEnumMember.bind(this));
|
9603 | this.expect(types.braceR);
|
9604 | return this.finishNode(node, "TSEnumDeclaration");
|
9605 | }
|
9606 |
|
9607 | tsParseModuleBlock() {
|
9608 | const node = this.startNode();
|
9609 | this.expect(types.braceL);
|
9610 | this.parseBlockOrModuleBlockBody(node.body = [], undefined, true, types.braceR);
|
9611 | return this.finishNode(node, "TSModuleBlock");
|
9612 | }
|
9613 |
|
9614 | tsParseModuleOrNamespaceDeclaration(node) {
|
9615 | node.id = this.parseIdentifier();
|
9616 |
|
9617 | if (this.eat(types.dot)) {
|
9618 | const inner = this.startNode();
|
9619 | this.tsParseModuleOrNamespaceDeclaration(inner);
|
9620 | node.body = inner;
|
9621 | } else {
|
9622 | node.body = this.tsParseModuleBlock();
|
9623 | }
|
9624 |
|
9625 | return this.finishNode(node, "TSModuleDeclaration");
|
9626 | }
|
9627 |
|
9628 | tsParseAmbientExternalModuleDeclaration(node) {
|
9629 | if (this.isContextual("global")) {
|
9630 | node.global = true;
|
9631 | node.id = this.parseIdentifier();
|
9632 | } else if (this.match(types.string)) {
|
9633 | node.id = this.parseExprAtom();
|
9634 | } else {
|
9635 | this.unexpected();
|
9636 | }
|
9637 |
|
9638 | if (this.match(types.braceL)) {
|
9639 | node.body = this.tsParseModuleBlock();
|
9640 | } else {
|
9641 | this.semicolon();
|
9642 | }
|
9643 |
|
9644 | return this.finishNode(node, "TSModuleDeclaration");
|
9645 | }
|
9646 |
|
9647 | tsParseImportEqualsDeclaration(node, isExport) {
|
9648 | node.isExport = isExport || false;
|
9649 | node.id = this.parseIdentifier();
|
9650 | this.expect(types.eq);
|
9651 | node.moduleReference = this.tsParseModuleReference();
|
9652 | this.semicolon();
|
9653 | return this.finishNode(node, "TSImportEqualsDeclaration");
|
9654 | }
|
9655 |
|
9656 | tsIsExternalModuleReference() {
|
9657 | return this.isContextual("require") && this.lookahead().type === types.parenL;
|
9658 | }
|
9659 |
|
9660 | tsParseModuleReference() {
|
9661 | return this.tsIsExternalModuleReference() ? this.tsParseExternalModuleReference() : this.tsParseEntityName(false);
|
9662 | }
|
9663 |
|
9664 | tsParseExternalModuleReference() {
|
9665 | const node = this.startNode();
|
9666 | this.expectContextual("require");
|
9667 | this.expect(types.parenL);
|
9668 |
|
9669 | if (!this.match(types.string)) {
|
9670 | throw this.unexpected();
|
9671 | }
|
9672 |
|
9673 | node.expression = this.parseLiteral(this.state.value, "StringLiteral");
|
9674 | this.expect(types.parenR);
|
9675 | return this.finishNode(node, "TSExternalModuleReference");
|
9676 | }
|
9677 |
|
9678 | tsLookAhead(f) {
|
9679 | const state = this.state.clone();
|
9680 | const res = f();
|
9681 | this.state = state;
|
9682 | return res;
|
9683 | }
|
9684 |
|
9685 | tsTryParseAndCatch(f) {
|
9686 | const state = this.state.clone();
|
9687 |
|
9688 | try {
|
9689 | return f();
|
9690 | } catch (e) {
|
9691 | if (e instanceof SyntaxError) {
|
9692 | this.state = state;
|
9693 | return undefined;
|
9694 | }
|
9695 |
|
9696 | throw e;
|
9697 | }
|
9698 | }
|
9699 |
|
9700 | tsTryParse(f) {
|
9701 | const state = this.state.clone();
|
9702 | const result = f();
|
9703 |
|
9704 | if (result !== undefined && result !== false) {
|
9705 | return result;
|
9706 | } else {
|
9707 | this.state = state;
|
9708 | return undefined;
|
9709 | }
|
9710 | }
|
9711 |
|
9712 | nodeWithSamePosition(original, type) {
|
9713 | const node = this.startNodeAtNode(original);
|
9714 | node.type = type;
|
9715 | node.end = original.end;
|
9716 | node.loc.end = original.loc.end;
|
9717 |
|
9718 | if (original.leadingComments) {
|
9719 | node.leadingComments = original.leadingComments;
|
9720 | }
|
9721 |
|
9722 | if (original.trailingComments) {
|
9723 | node.trailingComments = original.trailingComments;
|
9724 | }
|
9725 |
|
9726 | if (original.innerComments) node.innerComments = original.innerComments;
|
9727 | return node;
|
9728 | }
|
9729 |
|
9730 | tsTryParseDeclare(nany) {
|
9731 | if (this.isLineTerminator()) {
|
9732 | return;
|
9733 | }
|
9734 |
|
9735 | let starttype = this.state.type;
|
9736 | let kind;
|
9737 |
|
9738 | if (this.isContextual("let")) {
|
9739 | starttype = types._var;
|
9740 | kind = "let";
|
9741 | }
|
9742 |
|
9743 | switch (starttype) {
|
9744 | case types._function:
|
9745 | this.next();
|
9746 | return this.parseFunction(nany, true);
|
9747 |
|
9748 | case types._class:
|
9749 | return this.parseClass(nany, true, false);
|
9750 |
|
9751 | case types._const:
|
9752 | if (this.match(types._const) && this.isLookaheadContextual("enum")) {
|
9753 | this.expect(types._const);
|
9754 | this.expectContextual("enum");
|
9755 | return this.tsParseEnumDeclaration(nany, true);
|
9756 | }
|
9757 |
|
9758 | case types._var:
|
9759 | kind = kind || this.state.value;
|
9760 | return this.parseVarStatement(nany, kind);
|
9761 |
|
9762 | case types.name:
|
9763 | {
|
9764 | const value = this.state.value;
|
9765 |
|
9766 | if (value === "global") {
|
9767 | return this.tsParseAmbientExternalModuleDeclaration(nany);
|
9768 | } else {
|
9769 | return this.tsParseDeclaration(nany, value, true);
|
9770 | }
|
9771 | }
|
9772 | }
|
9773 | }
|
9774 |
|
9775 | tsTryParseExportDeclaration() {
|
9776 | return this.tsParseDeclaration(this.startNode(), this.state.value, true);
|
9777 | }
|
9778 |
|
9779 | tsParseExpressionStatement(node, expr) {
|
9780 | switch (expr.name) {
|
9781 | case "declare":
|
9782 | {
|
9783 | const declaration = this.tsTryParseDeclare(node);
|
9784 |
|
9785 | if (declaration) {
|
9786 | declaration.declare = true;
|
9787 | return declaration;
|
9788 | }
|
9789 |
|
9790 | break;
|
9791 | }
|
9792 |
|
9793 | case "global":
|
9794 | if (this.match(types.braceL)) {
|
9795 | const mod = node;
|
9796 | mod.global = true;
|
9797 | mod.id = expr;
|
9798 | mod.body = this.tsParseModuleBlock();
|
9799 | return this.finishNode(mod, "TSModuleDeclaration");
|
9800 | }
|
9801 |
|
9802 | break;
|
9803 |
|
9804 | default:
|
9805 | return this.tsParseDeclaration(node, expr.name, false);
|
9806 | }
|
9807 | }
|
9808 |
|
9809 | tsParseDeclaration(node, value, next) {
|
9810 | switch (value) {
|
9811 | case "abstract":
|
9812 | if (this.tsCheckLineTerminatorAndMatch(types._class, next)) {
|
9813 | const cls = node;
|
9814 | cls.abstract = true;
|
9815 |
|
9816 | if (next) {
|
9817 | this.next();
|
9818 |
|
9819 | if (!this.match(types._class)) {
|
9820 | this.unexpected(null, types._class);
|
9821 | }
|
9822 | }
|
9823 |
|
9824 | return this.parseClass(cls, true, false);
|
9825 | }
|
9826 |
|
9827 | break;
|
9828 |
|
9829 | case "enum":
|
9830 | if (next || this.match(types.name)) {
|
9831 | if (next) this.next();
|
9832 | return this.tsParseEnumDeclaration(node, false);
|
9833 | }
|
9834 |
|
9835 | break;
|
9836 |
|
9837 | case "interface":
|
9838 | if (this.tsCheckLineTerminatorAndMatch(types.name, next)) {
|
9839 | if (next) this.next();
|
9840 | return this.tsParseInterfaceDeclaration(node);
|
9841 | }
|
9842 |
|
9843 | break;
|
9844 |
|
9845 | case "module":
|
9846 | if (next) this.next();
|
9847 |
|
9848 | if (this.match(types.string)) {
|
9849 | return this.tsParseAmbientExternalModuleDeclaration(node);
|
9850 | } else if (this.tsCheckLineTerminatorAndMatch(types.name, next)) {
|
9851 | return this.tsParseModuleOrNamespaceDeclaration(node);
|
9852 | }
|
9853 |
|
9854 | break;
|
9855 |
|
9856 | case "namespace":
|
9857 | if (this.tsCheckLineTerminatorAndMatch(types.name, next)) {
|
9858 | if (next) this.next();
|
9859 | return this.tsParseModuleOrNamespaceDeclaration(node);
|
9860 | }
|
9861 |
|
9862 | break;
|
9863 |
|
9864 | case "type":
|
9865 | if (this.tsCheckLineTerminatorAndMatch(types.name, next)) {
|
9866 | if (next) this.next();
|
9867 | return this.tsParseTypeAliasDeclaration(node);
|
9868 | }
|
9869 |
|
9870 | break;
|
9871 | }
|
9872 | }
|
9873 |
|
9874 | tsCheckLineTerminatorAndMatch(tokenType, next) {
|
9875 | return (next || this.match(tokenType)) && !this.isLineTerminator();
|
9876 | }
|
9877 |
|
9878 | tsTryParseGenericAsyncArrowFunction(startPos, startLoc) {
|
9879 | const res = this.tsTryParseAndCatch(() => {
|
9880 | const node = this.startNodeAt(startPos, startLoc);
|
9881 | node.typeParameters = this.tsParseTypeParameters();
|
9882 | super.parseFunctionParams(node);
|
9883 | node.returnType = this.tsTryParseTypeOrTypePredicateAnnotation();
|
9884 | this.expect(types.arrow);
|
9885 | return node;
|
9886 | });
|
9887 |
|
9888 | if (!res) {
|
9889 | return undefined;
|
9890 | }
|
9891 |
|
9892 | const oldInAsync = this.state.inAsync;
|
9893 | const oldInGenerator = this.state.inGenerator;
|
9894 | this.state.inAsync = true;
|
9895 | this.state.inGenerator = false;
|
9896 | res.id = null;
|
9897 | res.generator = false;
|
9898 | res.expression = true;
|
9899 | res.async = true;
|
9900 | this.parseFunctionBody(res, true);
|
9901 | this.state.inAsync = oldInAsync;
|
9902 | this.state.inGenerator = oldInGenerator;
|
9903 | return this.finishNode(res, "ArrowFunctionExpression");
|
9904 | }
|
9905 |
|
9906 | tsParseTypeArguments() {
|
9907 | const node = this.startNode();
|
9908 | node.params = this.tsInType(() => this.tsInNoContext(() => {
|
9909 | this.expectRelational("<");
|
9910 | return this.tsParseDelimitedList("TypeParametersOrArguments", this.tsParseType.bind(this));
|
9911 | }));
|
9912 | this.state.exprAllowed = false;
|
9913 | this.expectRelational(">");
|
9914 | return this.finishNode(node, "TSTypeParameterInstantiation");
|
9915 | }
|
9916 |
|
9917 | tsIsDeclarationStart() {
|
9918 | if (this.match(types.name)) {
|
9919 | switch (this.state.value) {
|
9920 | case "abstract":
|
9921 | case "declare":
|
9922 | case "enum":
|
9923 | case "interface":
|
9924 | case "module":
|
9925 | case "namespace":
|
9926 | case "type":
|
9927 | return true;
|
9928 | }
|
9929 | }
|
9930 |
|
9931 | return false;
|
9932 | }
|
9933 |
|
9934 | isExportDefaultSpecifier() {
|
9935 | if (this.tsIsDeclarationStart()) return false;
|
9936 | return super.isExportDefaultSpecifier();
|
9937 | }
|
9938 |
|
9939 | parseAssignableListItem(allowModifiers, decorators) {
|
9940 | const startPos = this.state.start;
|
9941 | const startLoc = this.state.startLoc;
|
9942 | let accessibility;
|
9943 | let readonly = false;
|
9944 |
|
9945 | if (allowModifiers) {
|
9946 | accessibility = this.parseAccessModifier();
|
9947 | readonly = !!this.tsParseModifier(["readonly"]);
|
9948 | }
|
9949 |
|
9950 | const left = this.parseMaybeDefault();
|
9951 | this.parseAssignableListItemTypes(left);
|
9952 | const elt = this.parseMaybeDefault(left.start, left.loc.start, left);
|
9953 |
|
9954 | if (accessibility || readonly) {
|
9955 | const pp = this.startNodeAt(startPos, startLoc);
|
9956 |
|
9957 | if (decorators.length) {
|
9958 | pp.decorators = decorators;
|
9959 | }
|
9960 |
|
9961 | if (accessibility) pp.accessibility = accessibility;
|
9962 | if (readonly) pp.readonly = readonly;
|
9963 |
|
9964 | if (elt.type !== "Identifier" && elt.type !== "AssignmentPattern") {
|
9965 | throw this.raise(pp.start, "A parameter property may not be declared using a binding pattern.");
|
9966 | }
|
9967 |
|
9968 | pp.parameter = elt;
|
9969 | return this.finishNode(pp, "TSParameterProperty");
|
9970 | }
|
9971 |
|
9972 | if (decorators.length) {
|
9973 | left.decorators = decorators;
|
9974 | }
|
9975 |
|
9976 | return elt;
|
9977 | }
|
9978 |
|
9979 | parseFunctionBodyAndFinish(node, type, allowExpressionBody) {
|
9980 | if (!allowExpressionBody && this.match(types.colon)) {
|
9981 | node.returnType = this.tsParseTypeOrTypePredicateAnnotation(types.colon);
|
9982 | }
|
9983 |
|
9984 | const bodilessType = type === "FunctionDeclaration" ? "TSDeclareFunction" : type === "ClassMethod" ? "TSDeclareMethod" : undefined;
|
9985 |
|
9986 | if (bodilessType && !this.match(types.braceL) && this.isLineTerminator()) {
|
9987 | this.finishNode(node, bodilessType);
|
9988 | return;
|
9989 | }
|
9990 |
|
9991 | super.parseFunctionBodyAndFinish(node, type, allowExpressionBody);
|
9992 | }
|
9993 |
|
9994 | parseSubscript(base, startPos, startLoc, noCalls, state) {
|
9995 | if (!this.hasPrecedingLineBreak() && this.match(types.bang)) {
|
9996 | this.state.exprAllowed = false;
|
9997 | this.next();
|
9998 | const nonNullExpression = this.startNodeAt(startPos, startLoc);
|
9999 | nonNullExpression.expression = base;
|
10000 | return this.finishNode(nonNullExpression, "TSNonNullExpression");
|
10001 | }
|
10002 |
|
10003 | if (this.isRelational("<")) {
|
10004 | const result = this.tsTryParseAndCatch(() => {
|
10005 | if (!noCalls && this.atPossibleAsync(base)) {
|
10006 | const asyncArrowFn = this.tsTryParseGenericAsyncArrowFunction(startPos, startLoc);
|
10007 |
|
10008 | if (asyncArrowFn) {
|
10009 | return asyncArrowFn;
|
10010 | }
|
10011 | }
|
10012 |
|
10013 | const node = this.startNodeAt(startPos, startLoc);
|
10014 | node.callee = base;
|
10015 | const typeArguments = this.tsParseTypeArguments();
|
10016 |
|
10017 | if (typeArguments) {
|
10018 | if (!noCalls && this.eat(types.parenL)) {
|
10019 | node.arguments = this.parseCallExpressionArguments(types.parenR, false);
|
10020 | node.typeParameters = typeArguments;
|
10021 | return this.finishCallExpression(node);
|
10022 | } else if (this.match(types.backQuote)) {
|
10023 | return this.parseTaggedTemplateExpression(startPos, startLoc, base, state, typeArguments);
|
10024 | }
|
10025 | }
|
10026 |
|
10027 | this.unexpected();
|
10028 | });
|
10029 | if (result) return result;
|
10030 | }
|
10031 |
|
10032 | return super.parseSubscript(base, startPos, startLoc, noCalls, state);
|
10033 | }
|
10034 |
|
10035 | parseNewArguments(node) {
|
10036 | if (this.isRelational("<")) {
|
10037 | const typeParameters = this.tsTryParseAndCatch(() => {
|
10038 | const args = this.tsParseTypeArguments();
|
10039 | if (!this.match(types.parenL)) this.unexpected();
|
10040 | return args;
|
10041 | });
|
10042 |
|
10043 | if (typeParameters) {
|
10044 | node.typeParameters = typeParameters;
|
10045 | }
|
10046 | }
|
10047 |
|
10048 | super.parseNewArguments(node);
|
10049 | }
|
10050 |
|
10051 | parseExprOp(left, leftStartPos, leftStartLoc, minPrec, noIn) {
|
10052 | if (nonNull(types._in.binop) > minPrec && !this.hasPrecedingLineBreak() && this.isContextual("as")) {
|
10053 | const node = this.startNodeAt(leftStartPos, leftStartLoc);
|
10054 | node.expression = left;
|
10055 | node.typeAnnotation = this.tsNextThenParseType();
|
10056 | this.finishNode(node, "TSAsExpression");
|
10057 | return this.parseExprOp(node, leftStartPos, leftStartLoc, minPrec, noIn);
|
10058 | }
|
10059 |
|
10060 | return super.parseExprOp(left, leftStartPos, leftStartLoc, minPrec, noIn);
|
10061 | }
|
10062 |
|
10063 | checkReservedWord(word, startLoc, checkKeywords, isBinding) {}
|
10064 |
|
10065 | checkDuplicateExports() {}
|
10066 |
|
10067 | parseImport(node) {
|
10068 | if (this.match(types.name) && this.lookahead().type === types.eq) {
|
10069 | return this.tsParseImportEqualsDeclaration(node);
|
10070 | }
|
10071 |
|
10072 | return super.parseImport(node);
|
10073 | }
|
10074 |
|
10075 | parseExport(node) {
|
10076 | if (this.match(types._import)) {
|
10077 | this.expect(types._import);
|
10078 | return this.tsParseImportEqualsDeclaration(node, true);
|
10079 | } else if (this.eat(types.eq)) {
|
10080 | const assign = node;
|
10081 | assign.expression = this.parseExpression();
|
10082 | this.semicolon();
|
10083 | return this.finishNode(assign, "TSExportAssignment");
|
10084 | } else if (this.eatContextual("as")) {
|
10085 | const decl = node;
|
10086 | this.expectContextual("namespace");
|
10087 | decl.id = this.parseIdentifier();
|
10088 | this.semicolon();
|
10089 | return this.finishNode(decl, "TSNamespaceExportDeclaration");
|
10090 | } else {
|
10091 | return super.parseExport(node);
|
10092 | }
|
10093 | }
|
10094 |
|
10095 | isAbstractClass() {
|
10096 | return this.isContextual("abstract") && this.lookahead().type === types._class;
|
10097 | }
|
10098 |
|
10099 | parseExportDefaultExpression() {
|
10100 | if (this.isAbstractClass()) {
|
10101 | const cls = this.startNode();
|
10102 | this.next();
|
10103 | this.parseClass(cls, true, true);
|
10104 | cls.abstract = true;
|
10105 | return cls;
|
10106 | }
|
10107 |
|
10108 | if (this.state.value === "interface") {
|
10109 | const result = this.tsParseDeclaration(this.startNode(), this.state.value, true);
|
10110 | if (result) return result;
|
10111 | }
|
10112 |
|
10113 | return super.parseExportDefaultExpression();
|
10114 | }
|
10115 |
|
10116 | parseStatementContent(context, topLevel) {
|
10117 | if (this.state.type === types._const) {
|
10118 | const ahead = this.lookahead();
|
10119 |
|
10120 | if (ahead.type === types.name && ahead.value === "enum") {
|
10121 | const node = this.startNode();
|
10122 | this.expect(types._const);
|
10123 | this.expectContextual("enum");
|
10124 | return this.tsParseEnumDeclaration(node, true);
|
10125 | }
|
10126 | }
|
10127 |
|
10128 | return super.parseStatementContent(context, topLevel);
|
10129 | }
|
10130 |
|
10131 | parseAccessModifier() {
|
10132 | return this.tsParseModifier(["public", "protected", "private"]);
|
10133 | }
|
10134 |
|
10135 | parseClassMember(classBody, member, state) {
|
10136 | const accessibility = this.parseAccessModifier();
|
10137 | if (accessibility) member.accessibility = accessibility;
|
10138 | super.parseClassMember(classBody, member, state);
|
10139 | }
|
10140 |
|
10141 | parseClassMemberWithIsStatic(classBody, member, state, isStatic) {
|
10142 | const methodOrProp = member;
|
10143 | const prop = member;
|
10144 | const propOrIdx = member;
|
10145 | let abstract = false,
|
10146 | readonly = false;
|
10147 | const mod = this.tsParseModifier(["abstract", "readonly"]);
|
10148 |
|
10149 | switch (mod) {
|
10150 | case "readonly":
|
10151 | readonly = true;
|
10152 | abstract = !!this.tsParseModifier(["abstract"]);
|
10153 | break;
|
10154 |
|
10155 | case "abstract":
|
10156 | abstract = true;
|
10157 | readonly = !!this.tsParseModifier(["readonly"]);
|
10158 | break;
|
10159 | }
|
10160 |
|
10161 | if (abstract) methodOrProp.abstract = true;
|
10162 | if (readonly) propOrIdx.readonly = true;
|
10163 |
|
10164 | if (!abstract && !isStatic && !methodOrProp.accessibility) {
|
10165 | const idx = this.tsTryParseIndexSignature(member);
|
10166 |
|
10167 | if (idx) {
|
10168 | classBody.body.push(idx);
|
10169 | return;
|
10170 | }
|
10171 | }
|
10172 |
|
10173 | if (readonly) {
|
10174 | methodOrProp.static = isStatic;
|
10175 | this.parseClassPropertyName(prop);
|
10176 | this.parsePostMemberNameModifiers(methodOrProp);
|
10177 | this.pushClassProperty(classBody, prop);
|
10178 | return;
|
10179 | }
|
10180 |
|
10181 | super.parseClassMemberWithIsStatic(classBody, member, state, isStatic);
|
10182 | }
|
10183 |
|
10184 | parsePostMemberNameModifiers(methodOrProp) {
|
10185 | const optional = this.eat(types.question);
|
10186 | if (optional) methodOrProp.optional = true;
|
10187 | }
|
10188 |
|
10189 | parseExpressionStatement(node, expr) {
|
10190 | const decl = expr.type === "Identifier" ? this.tsParseExpressionStatement(node, expr) : undefined;
|
10191 | return decl || super.parseExpressionStatement(node, expr);
|
10192 | }
|
10193 |
|
10194 | shouldParseExportDeclaration() {
|
10195 | if (this.tsIsDeclarationStart()) return true;
|
10196 | return super.shouldParseExportDeclaration();
|
10197 | }
|
10198 |
|
10199 | parseConditional(expr, noIn, startPos, startLoc, refNeedsArrowPos) {
|
10200 | if (!refNeedsArrowPos || !this.match(types.question)) {
|
10201 | return super.parseConditional(expr, noIn, startPos, startLoc, refNeedsArrowPos);
|
10202 | }
|
10203 |
|
10204 | const state = this.state.clone();
|
10205 |
|
10206 | try {
|
10207 | return super.parseConditional(expr, noIn, startPos, startLoc);
|
10208 | } catch (err) {
|
10209 | if (!(err instanceof SyntaxError)) {
|
10210 | throw err;
|
10211 | }
|
10212 |
|
10213 | this.state = state;
|
10214 | refNeedsArrowPos.start = err.pos || this.state.start;
|
10215 | return expr;
|
10216 | }
|
10217 | }
|
10218 |
|
10219 | parseParenItem(node, startPos, startLoc) {
|
10220 | node = super.parseParenItem(node, startPos, startLoc);
|
10221 |
|
10222 | if (this.eat(types.question)) {
|
10223 | node.optional = true;
|
10224 | }
|
10225 |
|
10226 | if (this.match(types.colon)) {
|
10227 | const typeCastNode = this.startNodeAt(startPos, startLoc);
|
10228 | typeCastNode.expression = node;
|
10229 | typeCastNode.typeAnnotation = this.tsParseTypeAnnotation();
|
10230 | return this.finishNode(typeCastNode, "TSTypeCastExpression");
|
10231 | }
|
10232 |
|
10233 | return this.finishNode(node, node.type);
|
10234 | }
|
10235 |
|
10236 | parseExportDeclaration(node) {
|
10237 | const startPos = this.state.start;
|
10238 | const startLoc = this.state.startLoc;
|
10239 | const isDeclare = this.eatContextual("declare");
|
10240 | let declaration;
|
10241 |
|
10242 | if (this.match(types.name)) {
|
10243 | declaration = this.tsTryParseExportDeclaration();
|
10244 | }
|
10245 |
|
10246 | if (!declaration) {
|
10247 | declaration = super.parseExportDeclaration(node);
|
10248 | }
|
10249 |
|
10250 | if (declaration && isDeclare) {
|
10251 | this.resetStartLocation(declaration, startPos, startLoc);
|
10252 | declaration.declare = true;
|
10253 | }
|
10254 |
|
10255 | return declaration;
|
10256 | }
|
10257 |
|
10258 | parseClassId(node, isStatement, optionalId) {
|
10259 | if ((!isStatement || optionalId) && this.isContextual("implements")) {
|
10260 | return;
|
10261 | }
|
10262 |
|
10263 | super.parseClassId(...arguments);
|
10264 | const typeParameters = this.tsTryParseTypeParameters();
|
10265 | if (typeParameters) node.typeParameters = typeParameters;
|
10266 | }
|
10267 |
|
10268 | parseClassProperty(node) {
|
10269 | if (!node.optional && this.eat(types.bang)) {
|
10270 | node.definite = true;
|
10271 | }
|
10272 |
|
10273 | const type = this.tsTryParseTypeAnnotation();
|
10274 | if (type) node.typeAnnotation = type;
|
10275 | return super.parseClassProperty(node);
|
10276 | }
|
10277 |
|
10278 | pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor) {
|
10279 | const typeParameters = this.tsTryParseTypeParameters();
|
10280 | if (typeParameters) method.typeParameters = typeParameters;
|
10281 | super.pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor);
|
10282 | }
|
10283 |
|
10284 | pushClassPrivateMethod(classBody, method, isGenerator, isAsync) {
|
10285 | const typeParameters = this.tsTryParseTypeParameters();
|
10286 | if (typeParameters) method.typeParameters = typeParameters;
|
10287 | super.pushClassPrivateMethod(classBody, method, isGenerator, isAsync);
|
10288 | }
|
10289 |
|
10290 | parseClassSuper(node) {
|
10291 | super.parseClassSuper(node);
|
10292 |
|
10293 | if (node.superClass && this.isRelational("<")) {
|
10294 | node.superTypeParameters = this.tsParseTypeArguments();
|
10295 | }
|
10296 |
|
10297 | if (this.eatContextual("implements")) {
|
10298 | node.implements = this.tsParseHeritageClause("implements");
|
10299 | }
|
10300 | }
|
10301 |
|
10302 | parseObjPropValue(prop, ...args) {
|
10303 | const typeParameters = this.tsTryParseTypeParameters();
|
10304 | if (typeParameters) prop.typeParameters = typeParameters;
|
10305 | super.parseObjPropValue(prop, ...args);
|
10306 | }
|
10307 |
|
10308 | parseFunctionParams(node, allowModifiers) {
|
10309 | const typeParameters = this.tsTryParseTypeParameters();
|
10310 | if (typeParameters) node.typeParameters = typeParameters;
|
10311 | super.parseFunctionParams(node, allowModifiers);
|
10312 | }
|
10313 |
|
10314 | parseVarId(decl, kind) {
|
10315 | super.parseVarId(decl, kind);
|
10316 |
|
10317 | if (decl.id.type === "Identifier" && this.eat(types.bang)) {
|
10318 | decl.definite = true;
|
10319 | }
|
10320 |
|
10321 | const type = this.tsTryParseTypeAnnotation();
|
10322 |
|
10323 | if (type) {
|
10324 | decl.id.typeAnnotation = type;
|
10325 | this.finishNode(decl.id, decl.id.type);
|
10326 | }
|
10327 | }
|
10328 |
|
10329 | parseAsyncArrowFromCallExpression(node, call) {
|
10330 | if (this.match(types.colon)) {
|
10331 | node.returnType = this.tsParseTypeAnnotation();
|
10332 | }
|
10333 |
|
10334 | return super.parseAsyncArrowFromCallExpression(node, call);
|
10335 | }
|
10336 |
|
10337 | parseMaybeAssign(...args) {
|
10338 | let jsxError;
|
10339 |
|
10340 | if (this.match(types.jsxTagStart)) {
|
10341 | const context = this.curContext();
|
10342 | assert(context === types$1.j_oTag);
|
10343 | assert(this.state.context[this.state.context.length - 2] === types$1.j_expr);
|
10344 | const state = this.state.clone();
|
10345 |
|
10346 | try {
|
10347 | return super.parseMaybeAssign(...args);
|
10348 | } catch (err) {
|
10349 | if (!(err instanceof SyntaxError)) {
|
10350 | throw err;
|
10351 | }
|
10352 |
|
10353 | this.state = state;
|
10354 | assert(this.curContext() === types$1.j_oTag);
|
10355 | this.state.context.pop();
|
10356 | assert(this.curContext() === types$1.j_expr);
|
10357 | this.state.context.pop();
|
10358 | jsxError = err;
|
10359 | }
|
10360 | }
|
10361 |
|
10362 | if (jsxError === undefined && !this.isRelational("<")) {
|
10363 | return super.parseMaybeAssign(...args);
|
10364 | }
|
10365 |
|
10366 | let arrowExpression;
|
10367 | let typeParameters;
|
10368 | const state = this.state.clone();
|
10369 |
|
10370 | try {
|
10371 | typeParameters = this.tsParseTypeParameters();
|
10372 | arrowExpression = super.parseMaybeAssign(...args);
|
10373 |
|
10374 | if (arrowExpression.type !== "ArrowFunctionExpression") {
|
10375 | this.unexpected();
|
10376 | }
|
10377 | } catch (err) {
|
10378 | if (!(err instanceof SyntaxError)) {
|
10379 | throw err;
|
10380 | }
|
10381 |
|
10382 | if (jsxError) {
|
10383 | throw jsxError;
|
10384 | }
|
10385 |
|
10386 | assert(!this.hasPlugin("jsx"));
|
10387 | this.state = state;
|
10388 | return super.parseMaybeAssign(...args);
|
10389 | }
|
10390 |
|
10391 | if (typeParameters && typeParameters.params.length !== 0) {
|
10392 | this.resetStartLocationFromNode(arrowExpression, typeParameters);
|
10393 | }
|
10394 |
|
10395 | arrowExpression.typeParameters = typeParameters;
|
10396 | return arrowExpression;
|
10397 | }
|
10398 |
|
10399 | parseMaybeUnary(refShorthandDefaultPos) {
|
10400 | if (!this.hasPlugin("jsx") && this.isRelational("<")) {
|
10401 | return this.tsParseTypeAssertion();
|
10402 | } else {
|
10403 | return super.parseMaybeUnary(refShorthandDefaultPos);
|
10404 | }
|
10405 | }
|
10406 |
|
10407 | parseArrow(node) {
|
10408 | if (this.match(types.colon)) {
|
10409 | const state = this.state.clone();
|
10410 |
|
10411 | try {
|
10412 | const returnType = this.tsParseTypeOrTypePredicateAnnotation(types.colon);
|
10413 | if (this.canInsertSemicolon()) this.unexpected();
|
10414 | if (!this.match(types.arrow)) this.unexpected();
|
10415 | node.returnType = returnType;
|
10416 | } catch (err) {
|
10417 | if (err instanceof SyntaxError) {
|
10418 | this.state = state;
|
10419 | } else {
|
10420 | throw err;
|
10421 | }
|
10422 | }
|
10423 | }
|
10424 |
|
10425 | return super.parseArrow(node);
|
10426 | }
|
10427 |
|
10428 | parseAssignableListItemTypes(param) {
|
10429 | if (this.eat(types.question)) {
|
10430 | if (param.type !== "Identifier") {
|
10431 | throw this.raise(param.start, "A binding pattern parameter cannot be optional in an implementation signature.");
|
10432 | }
|
10433 |
|
10434 | param.optional = true;
|
10435 | }
|
10436 |
|
10437 | const type = this.tsTryParseTypeAnnotation();
|
10438 | if (type) param.typeAnnotation = type;
|
10439 | return this.finishNode(param, param.type);
|
10440 | }
|
10441 |
|
10442 | toAssignable(node, isBinding, contextDescription) {
|
10443 | switch (node.type) {
|
10444 | case "TSTypeCastExpression":
|
10445 | return super.toAssignable(this.typeCastToParameter(node), isBinding, contextDescription);
|
10446 |
|
10447 | case "TSParameterProperty":
|
10448 | return super.toAssignable(node, isBinding, contextDescription);
|
10449 |
|
10450 | case "TSAsExpression":
|
10451 | case "TSNonNullExpression":
|
10452 | case "TSTypeAssertion":
|
10453 | node.expression = this.toAssignable(node.expression, isBinding, contextDescription);
|
10454 | return node;
|
10455 |
|
10456 | default:
|
10457 | return super.toAssignable(node, isBinding, contextDescription);
|
10458 | }
|
10459 | }
|
10460 |
|
10461 | checkLVal(expr, isBinding, checkClashes, contextDescription) {
|
10462 | switch (expr.type) {
|
10463 | case "TSTypeCastExpression":
|
10464 | return;
|
10465 |
|
10466 | case "TSParameterProperty":
|
10467 | this.checkLVal(expr.parameter, isBinding, checkClashes, "parameter property");
|
10468 | return;
|
10469 |
|
10470 | case "TSAsExpression":
|
10471 | case "TSNonNullExpression":
|
10472 | case "TSTypeAssertion":
|
10473 | this.checkLVal(expr.expression, isBinding, checkClashes, contextDescription);
|
10474 | return;
|
10475 |
|
10476 | default:
|
10477 | super.checkLVal(expr, isBinding, checkClashes, contextDescription);
|
10478 | return;
|
10479 | }
|
10480 | }
|
10481 |
|
10482 | parseBindingAtom() {
|
10483 | switch (this.state.type) {
|
10484 | case types._this:
|
10485 | return this.parseIdentifier(true);
|
10486 |
|
10487 | default:
|
10488 | return super.parseBindingAtom();
|
10489 | }
|
10490 | }
|
10491 |
|
10492 | parseMaybeDecoratorArguments(expr) {
|
10493 | if (this.isRelational("<")) {
|
10494 | const typeArguments = this.tsParseTypeArguments();
|
10495 |
|
10496 | if (this.match(types.parenL)) {
|
10497 | const call = super.parseMaybeDecoratorArguments(expr);
|
10498 | call.typeParameters = typeArguments;
|
10499 | return call;
|
10500 | }
|
10501 |
|
10502 | this.unexpected(this.state.start, types.parenL);
|
10503 | }
|
10504 |
|
10505 | return super.parseMaybeDecoratorArguments(expr);
|
10506 | }
|
10507 |
|
10508 | isClassMethod() {
|
10509 | return this.isRelational("<") || super.isClassMethod();
|
10510 | }
|
10511 |
|
10512 | isClassProperty() {
|
10513 | return this.match(types.bang) || this.match(types.colon) || super.isClassProperty();
|
10514 | }
|
10515 |
|
10516 | parseMaybeDefault(...args) {
|
10517 | const node = super.parseMaybeDefault(...args);
|
10518 |
|
10519 | if (node.type === "AssignmentPattern" && node.typeAnnotation && node.right.start < node.typeAnnotation.start) {
|
10520 | this.raise(node.typeAnnotation.start, "Type annotations must come before default assignments, " + "e.g. instead of `age = 25: number` use `age: number = 25`");
|
10521 | }
|
10522 |
|
10523 | return node;
|
10524 | }
|
10525 |
|
10526 | getTokenFromCode(code) {
|
10527 | if (this.state.inType && (code === 62 || code === 60)) {
|
10528 | return this.finishOp(types.relational, 1);
|
10529 | } else {
|
10530 | return super.getTokenFromCode(code);
|
10531 | }
|
10532 | }
|
10533 |
|
10534 | toAssignableList(exprList, isBinding, contextDescription) {
|
10535 | for (let i = 0; i < exprList.length; i++) {
|
10536 | const expr = exprList[i];
|
10537 |
|
10538 | if (expr && expr.type === "TSTypeCastExpression") {
|
10539 | exprList[i] = this.typeCastToParameter(expr);
|
10540 | }
|
10541 | }
|
10542 |
|
10543 | return super.toAssignableList(exprList, isBinding, contextDescription);
|
10544 | }
|
10545 |
|
10546 | typeCastToParameter(node) {
|
10547 | node.expression.typeAnnotation = node.typeAnnotation;
|
10548 | return this.finishNodeAt(node.expression, node.expression.type, node.typeAnnotation.end, node.typeAnnotation.loc.end);
|
10549 | }
|
10550 |
|
10551 | toReferencedList(exprList, isInParens) {
|
10552 | for (let i = 0; i < exprList.length; i++) {
|
10553 | const expr = exprList[i];
|
10554 |
|
10555 | if (expr && expr._exprListItem && expr.type === "TsTypeCastExpression") {
|
10556 | this.raise(expr.start, "Did not expect a type annotation here.");
|
10557 | }
|
10558 | }
|
10559 |
|
10560 | return exprList;
|
10561 | }
|
10562 |
|
10563 | shouldParseArrow() {
|
10564 | return this.match(types.colon) || super.shouldParseArrow();
|
10565 | }
|
10566 |
|
10567 | shouldParseAsyncArrow() {
|
10568 | return this.match(types.colon) || super.shouldParseAsyncArrow();
|
10569 | }
|
10570 |
|
10571 | canHaveLeadingDecorator() {
|
10572 | return super.canHaveLeadingDecorator() || this.isAbstractClass();
|
10573 | }
|
10574 |
|
10575 | jsxParseOpeningElementAfterName(node) {
|
10576 | const typeArguments = this.tsTryParseAndCatch(() => this.tsParseTypeArguments());
|
10577 | if (typeArguments) node.typeParameters = typeArguments;
|
10578 | return super.jsxParseOpeningElementAfterName(node);
|
10579 | }
|
10580 |
|
10581 | });
|
10582 |
|
10583 | function hasPlugin(plugins, name) {
|
10584 | return plugins.some(plugin => {
|
10585 | if (Array.isArray(plugin)) {
|
10586 | return plugin[0] === name;
|
10587 | } else {
|
10588 | return plugin === name;
|
10589 | }
|
10590 | });
|
10591 | }
|
10592 | function getPluginOption(plugins, name, option) {
|
10593 | const plugin = plugins.find(plugin => {
|
10594 | if (Array.isArray(plugin)) {
|
10595 | return plugin[0] === name;
|
10596 | } else {
|
10597 | return plugin === name;
|
10598 | }
|
10599 | });
|
10600 |
|
10601 | if (plugin && Array.isArray(plugin)) {
|
10602 | return plugin[1][option];
|
10603 | }
|
10604 |
|
10605 | return null;
|
10606 | }
|
10607 | const PIPELINE_PROPOSALS = ["minimal", "smart"];
|
10608 | function validatePlugins(plugins) {
|
10609 | if (hasPlugin(plugins, "decorators")) {
|
10610 | if (hasPlugin(plugins, "decorators-legacy")) {
|
10611 | throw new Error("Cannot use the decorators and decorators-legacy plugin together");
|
10612 | }
|
10613 |
|
10614 | const decoratorsBeforeExport = getPluginOption(plugins, "decorators", "decoratorsBeforeExport");
|
10615 |
|
10616 | if (decoratorsBeforeExport == null) {
|
10617 | throw new Error("The 'decorators' plugin requires a 'decoratorsBeforeExport' option," + " whose value must be a boolean. If you are migrating from" + " Babylon/Babel 6 or want to use the old decorators proposal, you" + " should use the 'decorators-legacy' plugin instead of 'decorators'.");
|
10618 | } else if (typeof decoratorsBeforeExport !== "boolean") {
|
10619 | throw new Error("'decoratorsBeforeExport' must be a boolean.");
|
10620 | }
|
10621 | }
|
10622 |
|
10623 | if (hasPlugin(plugins, "flow") && hasPlugin(plugins, "typescript")) {
|
10624 | throw new Error("Cannot combine flow and typescript plugins.");
|
10625 | }
|
10626 |
|
10627 | if (hasPlugin(plugins, "pipelineOperator") && !PIPELINE_PROPOSALS.includes(getPluginOption(plugins, "pipelineOperator", "proposal"))) {
|
10628 | throw new Error("'pipelineOperator' requires 'proposal' option whose value should be one of: " + PIPELINE_PROPOSALS.map(p => `'${p}'`).join(", "));
|
10629 | }
|
10630 | }
|
10631 | const mixinPluginNames = ["estree", "jsx", "flow", "typescript"];
|
10632 | const mixinPlugins = {
|
10633 | estree,
|
10634 | jsx,
|
10635 | flow,
|
10636 | typescript
|
10637 | };
|
10638 |
|
10639 | function parse(input, options) {
|
10640 | if (options && options.sourceType === "unambiguous") {
|
10641 | options = Object.assign({}, options);
|
10642 |
|
10643 | try {
|
10644 | options.sourceType = "module";
|
10645 | const parser = getParser(options, input);
|
10646 | const ast = parser.parse();
|
10647 | if (!parser.sawUnambiguousESM) ast.program.sourceType = "script";
|
10648 | return ast;
|
10649 | } catch (moduleError) {
|
10650 | try {
|
10651 | options.sourceType = "script";
|
10652 | return getParser(options, input).parse();
|
10653 | } catch (scriptError) {}
|
10654 |
|
10655 | throw moduleError;
|
10656 | }
|
10657 | } else {
|
10658 | return getParser(options, input).parse();
|
10659 | }
|
10660 | }
|
10661 | function parseExpression(input, options) {
|
10662 | const parser = getParser(options, input);
|
10663 |
|
10664 | if (parser.options.strictMode) {
|
10665 | parser.state.strict = true;
|
10666 | }
|
10667 |
|
10668 | return parser.getExpression();
|
10669 | }
|
10670 | function getParser(options, input) {
|
10671 | let cls = Parser;
|
10672 |
|
10673 | if (options && options.plugins) {
|
10674 | validatePlugins(options.plugins);
|
10675 | cls = getParserClass(options.plugins);
|
10676 | }
|
10677 |
|
10678 | return new cls(options, input);
|
10679 | }
|
10680 |
|
10681 | const parserClassCache = {};
|
10682 |
|
10683 | function getParserClass(pluginsFromOptions) {
|
10684 | const pluginList = mixinPluginNames.filter(name => hasPlugin(pluginsFromOptions, name));
|
10685 | const key = pluginList.join("/");
|
10686 | let cls = parserClassCache[key];
|
10687 |
|
10688 | if (!cls) {
|
10689 | cls = Parser;
|
10690 |
|
10691 | for (let _i = 0; _i < pluginList.length; _i++) {
|
10692 | const plugin = pluginList[_i];
|
10693 | cls = mixinPlugins[plugin](cls);
|
10694 | }
|
10695 |
|
10696 | parserClassCache[key] = cls;
|
10697 | }
|
10698 |
|
10699 | return cls;
|
10700 | }
|
10701 |
|
10702 | exports.parse = parse;
|
10703 | exports.parseExpression = parseExpression;
|
10704 | exports.tokTypes = types;
|
10705 |
|
\ | No newline at end of file |