UNPKG

38 kBJavaScriptView Raw
1"use strict";Object.defineProperty(exports, "__esModule", {value: true});/* eslint max-len: 0 */
2
3var _index = require('../index');
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19var _flow = require('../plugins/flow');
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36var _typescript = require('../plugins/typescript');
37
38
39
40
41
42
43
44
45
46
47
48var _tokenizer = require('../tokenizer');
49var _keywords = require('../tokenizer/keywords');
50var _state = require('../tokenizer/state');
51var _types = require('../tokenizer/types');
52var _charcodes = require('../util/charcodes');
53var _base = require('./base');
54
55
56
57
58
59
60
61
62
63
64
65var _expression = require('./expression');
66
67
68
69
70
71var _lval = require('./lval');
72
73
74
75
76
77
78
79
80
81var _util = require('./util');
82
83 function parseTopLevel() {
84 parseBlockBody(_types.TokenType.eof);
85 _base.state.scopes.push(new (0, _state.Scope)(0, _base.state.tokens.length, true));
86 if (_base.state.scopeDepth !== 0) {
87 throw new Error(`Invalid scope depth at end of file: ${_base.state.scopeDepth}`);
88 }
89 return new (0, _index.File)(_base.state.tokens, _base.state.scopes);
90} exports.parseTopLevel = parseTopLevel;
91
92// Parse a single statement.
93//
94// If expecting a statement and finding a slash operator, parse a
95// regular expression literal. This is to handle cases like
96// `if (foo) /blah/.exec(foo)`, where looking at the previous token
97// does not help.
98
99 function parseStatement(declaration) {
100 if (_base.isFlowEnabled) {
101 if (_flow.flowTryParseStatement.call(void 0, )) {
102 return;
103 }
104 }
105 if (_tokenizer.match.call(void 0, _types.TokenType.at)) {
106 parseDecorators();
107 }
108 parseStatementContent(declaration);
109} exports.parseStatement = parseStatement;
110
111function parseStatementContent(declaration) {
112 if (_base.isTypeScriptEnabled) {
113 if (_typescript.tsTryParseStatementContent.call(void 0, )) {
114 return;
115 }
116 }
117
118 const starttype = _base.state.type;
119
120 // Most types of statements are recognized by the keyword they
121 // start with. Many are trivial to parse, some require a bit of
122 // complexity.
123
124 switch (starttype) {
125 case _types.TokenType._break:
126 case _types.TokenType._continue:
127 parseBreakContinueStatement();
128 return;
129 case _types.TokenType._debugger:
130 parseDebuggerStatement();
131 return;
132 case _types.TokenType._do:
133 parseDoStatement();
134 return;
135 case _types.TokenType._for:
136 parseForStatement();
137 return;
138 case _types.TokenType._function:
139 if (_tokenizer.lookaheadType.call(void 0, ) === _types.TokenType.dot) break;
140 if (!declaration) _util.unexpected.call(void 0, );
141 parseFunctionStatement();
142 return;
143
144 case _types.TokenType._class:
145 if (!declaration) _util.unexpected.call(void 0, );
146 parseClass(true);
147 return;
148
149 case _types.TokenType._if:
150 parseIfStatement();
151 return;
152 case _types.TokenType._return:
153 parseReturnStatement();
154 return;
155 case _types.TokenType._switch:
156 parseSwitchStatement();
157 return;
158 case _types.TokenType._throw:
159 parseThrowStatement();
160 return;
161 case _types.TokenType._try:
162 parseTryStatement();
163 return;
164
165 case _types.TokenType._let:
166 case _types.TokenType._const:
167 if (!declaration) _util.unexpected.call(void 0, ); // NOTE: falls through to _var
168
169 case _types.TokenType._var:
170 parseVarStatement(starttype);
171 return;
172
173 case _types.TokenType._while:
174 parseWhileStatement();
175 return;
176 case _types.TokenType.braceL:
177 parseBlock();
178 return;
179 case _types.TokenType.semi:
180 parseEmptyStatement();
181 return;
182 case _types.TokenType._export:
183 case _types.TokenType._import: {
184 const nextType = _tokenizer.lookaheadType.call(void 0, );
185 if (nextType === _types.TokenType.parenL || nextType === _types.TokenType.dot) {
186 break;
187 }
188 _tokenizer.next.call(void 0, );
189 if (starttype === _types.TokenType._import) {
190 parseImport();
191 } else {
192 parseExport();
193 }
194 return;
195 }
196 case _types.TokenType.name:
197 if (_base.state.contextualKeyword === _keywords.ContextualKeyword._async) {
198 const functionStart = _base.state.start;
199 // peek ahead and see if next token is a function
200 const snapshot = _base.state.snapshot();
201 _tokenizer.next.call(void 0, );
202 if (_tokenizer.match.call(void 0, _types.TokenType._function) && !_util.canInsertSemicolon.call(void 0, )) {
203 _util.expect.call(void 0, _types.TokenType._function);
204 parseFunction(functionStart, true);
205 return;
206 } else {
207 _base.state.restoreFromSnapshot(snapshot);
208 }
209 }
210 default:
211 // Do nothing.
212 break;
213 }
214
215 // If the statement does not start with a statement keyword or a
216 // brace, it's an ExpressionStatement or LabeledStatement. We
217 // simply start parsing an expression, and afterwards, if the
218 // next token is a colon and the expression was a simple
219 // Identifier node, we switch to interpreting it as a label.
220 const initialTokensLength = _base.state.tokens.length;
221 _expression.parseExpression.call(void 0, );
222 let simpleName = null;
223 if (_base.state.tokens.length === initialTokensLength + 1) {
224 const token = _base.state.tokens[_base.state.tokens.length - 1];
225 if (token.type === _types.TokenType.name) {
226 simpleName = token.contextualKeyword;
227 }
228 }
229 if (simpleName == null) {
230 _util.semicolon.call(void 0, );
231 return;
232 }
233 if (_tokenizer.eat.call(void 0, _types.TokenType.colon)) {
234 parseLabeledStatement();
235 } else {
236 // This was an identifier, so we might want to handle flow/typescript-specific cases.
237 parseIdentifierStatement(simpleName);
238 }
239}
240
241 function parseDecorators() {
242 while (_tokenizer.match.call(void 0, _types.TokenType.at)) {
243 parseDecorator();
244 }
245} exports.parseDecorators = parseDecorators;
246
247function parseDecorator() {
248 _tokenizer.next.call(void 0, );
249 if (_tokenizer.eat.call(void 0, _types.TokenType.parenL)) {
250 _expression.parseExpression.call(void 0, );
251 _util.expect.call(void 0, _types.TokenType.parenR);
252 } else {
253 _expression.parseIdentifier.call(void 0, );
254 while (_tokenizer.eat.call(void 0, _types.TokenType.dot)) {
255 _expression.parseIdentifier.call(void 0, );
256 }
257 }
258 parseMaybeDecoratorArguments();
259}
260
261function parseMaybeDecoratorArguments() {
262 if (_base.isTypeScriptEnabled) {
263 _typescript.tsParseMaybeDecoratorArguments.call(void 0, );
264 } else {
265 baseParseMaybeDecoratorArguments();
266 }
267}
268
269 function baseParseMaybeDecoratorArguments() {
270 if (_tokenizer.eat.call(void 0, _types.TokenType.parenL)) {
271 _expression.parseCallExpressionArguments.call(void 0, );
272 }
273} exports.baseParseMaybeDecoratorArguments = baseParseMaybeDecoratorArguments;
274
275function parseBreakContinueStatement() {
276 _tokenizer.next.call(void 0, );
277 if (!_util.isLineTerminator.call(void 0, )) {
278 _expression.parseIdentifier.call(void 0, );
279 _util.semicolon.call(void 0, );
280 }
281}
282
283function parseDebuggerStatement() {
284 _tokenizer.next.call(void 0, );
285 _util.semicolon.call(void 0, );
286}
287
288function parseDoStatement() {
289 _tokenizer.next.call(void 0, );
290 parseStatement(false);
291 _util.expect.call(void 0, _types.TokenType._while);
292 _expression.parseParenExpression.call(void 0, );
293 _tokenizer.eat.call(void 0, _types.TokenType.semi);
294}
295
296function parseForStatement() {
297 _base.state.scopeDepth++;
298 const startTokenIndex = _base.state.tokens.length;
299 parseAmbiguousForStatement();
300 const endTokenIndex = _base.state.tokens.length;
301 _base.state.scopes.push(new (0, _state.Scope)(startTokenIndex, endTokenIndex, false));
302 _base.state.scopeDepth--;
303}
304
305// Disambiguating between a `for` and a `for`/`in` or `for`/`of`
306// loop is non-trivial. Basically, we have to parse the init `var`
307// statement or expression, disallowing the `in` operator (see
308// the second parameter to `parseExpression`), and then check
309// whether the next token is `in` or `of`. When there is no init
310// part (semicolon immediately after the opening parenthesis), it
311// is a regular `for` loop.
312function parseAmbiguousForStatement() {
313 _tokenizer.next.call(void 0, );
314
315 let forAwait = false;
316 if (_util.isContextual.call(void 0, _keywords.ContextualKeyword._await)) {
317 forAwait = true;
318 _tokenizer.next.call(void 0, );
319 }
320 _util.expect.call(void 0, _types.TokenType.parenL);
321
322 if (_tokenizer.match.call(void 0, _types.TokenType.semi)) {
323 if (forAwait) {
324 _util.unexpected.call(void 0, );
325 }
326 parseFor();
327 return;
328 }
329
330 if (_tokenizer.match.call(void 0, _types.TokenType._var) || _tokenizer.match.call(void 0, _types.TokenType._let) || _tokenizer.match.call(void 0, _types.TokenType._const)) {
331 const varKind = _base.state.type;
332 _tokenizer.next.call(void 0, );
333 parseVar(true, varKind);
334 if (_tokenizer.match.call(void 0, _types.TokenType._in) || _util.isContextual.call(void 0, _keywords.ContextualKeyword._of)) {
335 parseForIn(forAwait);
336 return;
337 }
338 parseFor();
339 return;
340 }
341
342 _expression.parseExpression.call(void 0, true);
343 if (_tokenizer.match.call(void 0, _types.TokenType._in) || _util.isContextual.call(void 0, _keywords.ContextualKeyword._of)) {
344 parseForIn(forAwait);
345 return;
346 }
347 if (forAwait) {
348 _util.unexpected.call(void 0, );
349 }
350 parseFor();
351}
352
353function parseFunctionStatement() {
354 const functionStart = _base.state.start;
355 _tokenizer.next.call(void 0, );
356 parseFunction(functionStart, true);
357}
358
359function parseIfStatement() {
360 _tokenizer.next.call(void 0, );
361 _expression.parseParenExpression.call(void 0, );
362 parseStatement(false);
363 if (_tokenizer.eat.call(void 0, _types.TokenType._else)) {
364 parseStatement(false);
365 }
366}
367
368function parseReturnStatement() {
369 _tokenizer.next.call(void 0, );
370
371 // In `return` (and `break`/`continue`), the keywords with
372 // optional arguments, we eagerly look for a semicolon or the
373 // possibility to insert one.
374
375 if (!_util.isLineTerminator.call(void 0, )) {
376 _expression.parseExpression.call(void 0, );
377 _util.semicolon.call(void 0, );
378 }
379}
380
381function parseSwitchStatement() {
382 _tokenizer.next.call(void 0, );
383 _expression.parseParenExpression.call(void 0, );
384 _base.state.scopeDepth++;
385 const startTokenIndex = _base.state.tokens.length;
386 _util.expect.call(void 0, _types.TokenType.braceL);
387
388 // Don't bother validation; just go through any sequence of cases, defaults, and statements.
389 while (!_tokenizer.match.call(void 0, _types.TokenType.braceR) && !_base.state.error) {
390 if (_tokenizer.match.call(void 0, _types.TokenType._case) || _tokenizer.match.call(void 0, _types.TokenType._default)) {
391 const isCase = _tokenizer.match.call(void 0, _types.TokenType._case);
392 _tokenizer.next.call(void 0, );
393 if (isCase) {
394 _expression.parseExpression.call(void 0, );
395 }
396 _util.expect.call(void 0, _types.TokenType.colon);
397 } else {
398 parseStatement(true);
399 }
400 }
401 _tokenizer.next.call(void 0, ); // Closing brace
402 const endTokenIndex = _base.state.tokens.length;
403 _base.state.scopes.push(new (0, _state.Scope)(startTokenIndex, endTokenIndex, false));
404 _base.state.scopeDepth--;
405}
406
407function parseThrowStatement() {
408 _tokenizer.next.call(void 0, );
409 _expression.parseExpression.call(void 0, );
410 _util.semicolon.call(void 0, );
411}
412
413function parseCatchClauseParam() {
414 _lval.parseBindingAtom.call(void 0, true /* isBlockScope */);
415
416 if (_base.isTypeScriptEnabled) {
417 _typescript.tsTryParseTypeAnnotation.call(void 0, );
418 }
419}
420
421function parseTryStatement() {
422 _tokenizer.next.call(void 0, );
423
424 parseBlock();
425
426 if (_tokenizer.match.call(void 0, _types.TokenType._catch)) {
427 _tokenizer.next.call(void 0, );
428 let catchBindingStartTokenIndex = null;
429 if (_tokenizer.match.call(void 0, _types.TokenType.parenL)) {
430 _base.state.scopeDepth++;
431 catchBindingStartTokenIndex = _base.state.tokens.length;
432 _util.expect.call(void 0, _types.TokenType.parenL);
433 parseCatchClauseParam();
434 _util.expect.call(void 0, _types.TokenType.parenR);
435 }
436 parseBlock();
437 if (catchBindingStartTokenIndex != null) {
438 // We need a special scope for the catch binding which includes the binding itself and the
439 // catch block.
440 const endTokenIndex = _base.state.tokens.length;
441 _base.state.scopes.push(new (0, _state.Scope)(catchBindingStartTokenIndex, endTokenIndex, false));
442 _base.state.scopeDepth--;
443 }
444 }
445 if (_tokenizer.eat.call(void 0, _types.TokenType._finally)) {
446 parseBlock();
447 }
448}
449
450 function parseVarStatement(kind) {
451 _tokenizer.next.call(void 0, );
452 parseVar(false, kind);
453 _util.semicolon.call(void 0, );
454} exports.parseVarStatement = parseVarStatement;
455
456function parseWhileStatement() {
457 _tokenizer.next.call(void 0, );
458 _expression.parseParenExpression.call(void 0, );
459 parseStatement(false);
460}
461
462function parseEmptyStatement() {
463 _tokenizer.next.call(void 0, );
464}
465
466function parseLabeledStatement() {
467 parseStatement(true);
468}
469
470/**
471 * Parse a statement starting with an identifier of the given name. Subclasses match on the name
472 * to handle statements like "declare".
473 */
474function parseIdentifierStatement(contextualKeyword) {
475 if (_base.isTypeScriptEnabled) {
476 _typescript.tsParseIdentifierStatement.call(void 0, contextualKeyword);
477 } else if (_base.isFlowEnabled) {
478 _flow.flowParseIdentifierStatement.call(void 0, contextualKeyword);
479 } else {
480 _util.semicolon.call(void 0, );
481 }
482}
483
484// Parse a semicolon-enclosed block of statements.
485 function parseBlock(isFunctionScope = false, contextId = 0) {
486 const startTokenIndex = _base.state.tokens.length;
487 _base.state.scopeDepth++;
488 _util.expect.call(void 0, _types.TokenType.braceL);
489 if (contextId) {
490 _base.state.tokens[_base.state.tokens.length - 1].contextId = contextId;
491 }
492 parseBlockBody(_types.TokenType.braceR);
493 if (contextId) {
494 _base.state.tokens[_base.state.tokens.length - 1].contextId = contextId;
495 }
496 const endTokenIndex = _base.state.tokens.length;
497 _base.state.scopes.push(new (0, _state.Scope)(startTokenIndex, endTokenIndex, isFunctionScope));
498 _base.state.scopeDepth--;
499} exports.parseBlock = parseBlock;
500
501 function parseBlockBody(end) {
502 while (!_tokenizer.eat.call(void 0, end) && !_base.state.error) {
503 parseStatement(true);
504 }
505} exports.parseBlockBody = parseBlockBody;
506
507// Parse a regular `for` loop. The disambiguation code in
508// `parseStatement` will already have parsed the init statement or
509// expression.
510
511function parseFor() {
512 _util.expect.call(void 0, _types.TokenType.semi);
513 if (!_tokenizer.match.call(void 0, _types.TokenType.semi)) {
514 _expression.parseExpression.call(void 0, );
515 }
516 _util.expect.call(void 0, _types.TokenType.semi);
517 if (!_tokenizer.match.call(void 0, _types.TokenType.parenR)) {
518 _expression.parseExpression.call(void 0, );
519 }
520 _util.expect.call(void 0, _types.TokenType.parenR);
521 parseStatement(false);
522}
523
524// Parse a `for`/`in` and `for`/`of` loop, which are almost
525// same from parser's perspective.
526
527function parseForIn(forAwait) {
528 if (forAwait) {
529 _util.eatContextual.call(void 0, _keywords.ContextualKeyword._of);
530 } else {
531 _tokenizer.next.call(void 0, );
532 }
533 _expression.parseExpression.call(void 0, );
534 _util.expect.call(void 0, _types.TokenType.parenR);
535 parseStatement(false);
536}
537
538// Parse a list of variable declarations.
539
540function parseVar(isFor, kind) {
541 while (true) {
542 const isBlockScope = kind === _types.TokenType._const || kind === _types.TokenType._let;
543 parseVarHead(isBlockScope);
544 if (_tokenizer.eat.call(void 0, _types.TokenType.eq)) {
545 const eqIndex = _base.state.tokens.length - 1;
546 _expression.parseMaybeAssign.call(void 0, isFor);
547 _base.state.tokens[eqIndex].rhsEndIndex = _base.state.tokens.length;
548 }
549 if (!_tokenizer.eat.call(void 0, _types.TokenType.comma)) {
550 break;
551 }
552 }
553}
554
555function parseVarHead(isBlockScope) {
556 _lval.parseBindingAtom.call(void 0, isBlockScope);
557 if (_base.isTypeScriptEnabled) {
558 _typescript.tsAfterParseVarHead.call(void 0, );
559 } else if (_base.isFlowEnabled) {
560 _flow.flowAfterParseVarHead.call(void 0, );
561 }
562}
563
564// Parse a function declaration or literal (depending on the
565// `isStatement` parameter).
566
567 function parseFunction(
568 functionStart,
569 isStatement,
570 optionalId = false,
571) {
572 if (_tokenizer.match.call(void 0, _types.TokenType.star)) {
573 _tokenizer.next.call(void 0, );
574 }
575
576 if (isStatement && !optionalId && !_tokenizer.match.call(void 0, _types.TokenType.name) && !_tokenizer.match.call(void 0, _types.TokenType._yield)) {
577 _util.unexpected.call(void 0, );
578 }
579
580 let nameScopeStartTokenIndex = null;
581
582 if (_tokenizer.match.call(void 0, _types.TokenType.name)) {
583 // Expression-style functions should limit their name's scope to the function body, so we make
584 // a new function scope to enforce that.
585 if (!isStatement) {
586 nameScopeStartTokenIndex = _base.state.tokens.length;
587 _base.state.scopeDepth++;
588 }
589 _lval.parseBindingIdentifier.call(void 0, false);
590 }
591
592 const startTokenIndex = _base.state.tokens.length;
593 _base.state.scopeDepth++;
594 parseFunctionParams();
595 _expression.parseFunctionBodyAndFinish.call(void 0, functionStart);
596 const endTokenIndex = _base.state.tokens.length;
597 // In addition to the block scope of the function body, we need a separate function-style scope
598 // that includes the params.
599 _base.state.scopes.push(new (0, _state.Scope)(startTokenIndex, endTokenIndex, true));
600 _base.state.scopeDepth--;
601 if (nameScopeStartTokenIndex !== null) {
602 _base.state.scopes.push(new (0, _state.Scope)(nameScopeStartTokenIndex, endTokenIndex, true));
603 _base.state.scopeDepth--;
604 }
605} exports.parseFunction = parseFunction;
606
607 function parseFunctionParams(
608 allowModifiers = false,
609 funcContextId = 0,
610) {
611 if (_base.isTypeScriptEnabled) {
612 _typescript.tsStartParseFunctionParams.call(void 0, );
613 } else if (_base.isFlowEnabled) {
614 _flow.flowStartParseFunctionParams.call(void 0, );
615 }
616
617 _util.expect.call(void 0, _types.TokenType.parenL);
618 if (funcContextId) {
619 _base.state.tokens[_base.state.tokens.length - 1].contextId = funcContextId;
620 }
621 _lval.parseBindingList.call(void 0,
622 _types.TokenType.parenR,
623 false /* isBlockScope */,
624 false /* allowEmpty */,
625 allowModifiers,
626 funcContextId,
627 );
628 if (funcContextId) {
629 _base.state.tokens[_base.state.tokens.length - 1].contextId = funcContextId;
630 }
631} exports.parseFunctionParams = parseFunctionParams;
632
633// Parse a class declaration or literal (depending on the
634// `isStatement` parameter).
635
636 function parseClass(isStatement, optionalId = false) {
637 // Put a context ID on the class keyword, the open-brace, and the close-brace, so that later
638 // code can easily navigate to meaningful points on the class.
639 const contextId = _base.getNextContextId.call(void 0, );
640
641 _tokenizer.next.call(void 0, );
642 _base.state.tokens[_base.state.tokens.length - 1].contextId = contextId;
643 _base.state.tokens[_base.state.tokens.length - 1].isExpression = !isStatement;
644 // Like with functions, we declare a special "name scope" from the start of the name to the end
645 // of the class, but only with expression-style classes, to represent the fact that the name is
646 // available to the body of the class but not an outer declaration.
647 let nameScopeStartTokenIndex = null;
648 if (!isStatement) {
649 nameScopeStartTokenIndex = _base.state.tokens.length;
650 _base.state.scopeDepth++;
651 }
652 parseClassId(isStatement, optionalId);
653 parseClassSuper();
654 const openBraceIndex = _base.state.tokens.length;
655 parseClassBody(contextId);
656 if (_base.state.error) {
657 return;
658 }
659 _base.state.tokens[openBraceIndex].contextId = contextId;
660 _base.state.tokens[_base.state.tokens.length - 1].contextId = contextId;
661 if (nameScopeStartTokenIndex !== null) {
662 const endTokenIndex = _base.state.tokens.length;
663 _base.state.scopes.push(new (0, _state.Scope)(nameScopeStartTokenIndex, endTokenIndex, false));
664 _base.state.scopeDepth--;
665 }
666} exports.parseClass = parseClass;
667
668function isClassProperty() {
669 return _tokenizer.match.call(void 0, _types.TokenType.eq) || _tokenizer.match.call(void 0, _types.TokenType.semi) || _tokenizer.match.call(void 0, _types.TokenType.braceR) || _tokenizer.match.call(void 0, _types.TokenType.bang) || _tokenizer.match.call(void 0, _types.TokenType.colon);
670}
671
672function isClassMethod() {
673 return _tokenizer.match.call(void 0, _types.TokenType.parenL) || _tokenizer.match.call(void 0, _types.TokenType.lessThan);
674}
675
676function parseClassBody(classContextId) {
677 _util.expect.call(void 0, _types.TokenType.braceL);
678
679 while (!_tokenizer.eat.call(void 0, _types.TokenType.braceR) && !_base.state.error) {
680 if (_tokenizer.eat.call(void 0, _types.TokenType.semi)) {
681 continue;
682 }
683
684 if (_tokenizer.match.call(void 0, _types.TokenType.at)) {
685 parseDecorator();
686 continue;
687 }
688 const memberStart = _base.state.start;
689 parseClassMember(memberStart, classContextId);
690 }
691}
692
693function parseClassMember(memberStart, classContextId) {
694 if (_base.isTypeScriptEnabled) {
695 _typescript.tsParseModifiers.call(void 0, [
696 _keywords.ContextualKeyword._declare,
697 _keywords.ContextualKeyword._public,
698 _keywords.ContextualKeyword._protected,
699 _keywords.ContextualKeyword._private,
700 _keywords.ContextualKeyword._override,
701 ]);
702 }
703 let isStatic = false;
704 if (_tokenizer.match.call(void 0, _types.TokenType.name) && _base.state.contextualKeyword === _keywords.ContextualKeyword._static) {
705 _expression.parseIdentifier.call(void 0, ); // eats 'static'
706 if (isClassMethod()) {
707 parseClassMethod(memberStart, /* isConstructor */ false);
708 return;
709 } else if (isClassProperty()) {
710 parseClassProperty();
711 return;
712 }
713 // otherwise something static
714 _base.state.tokens[_base.state.tokens.length - 1].type = _types.TokenType._static;
715 isStatic = true;
716
717 if (_tokenizer.match.call(void 0, _types.TokenType.braceL)) {
718 // This is a static block. Mark the word "static" with the class context ID for class element
719 // detection and parse as a regular block.
720 _base.state.tokens[_base.state.tokens.length - 1].contextId = classContextId;
721 parseBlock();
722 return;
723 }
724 }
725
726 parseClassMemberWithIsStatic(memberStart, isStatic, classContextId);
727}
728
729function parseClassMemberWithIsStatic(
730 memberStart,
731 isStatic,
732 classContextId,
733) {
734 if (_base.isTypeScriptEnabled) {
735 if (_typescript.tsTryParseClassMemberWithIsStatic.call(void 0, isStatic)) {
736 return;
737 }
738 }
739 if (_tokenizer.eat.call(void 0, _types.TokenType.star)) {
740 // a generator
741 parseClassPropertyName(classContextId);
742 parseClassMethod(memberStart, /* isConstructor */ false);
743 return;
744 }
745
746 // Get the identifier name so we can tell if it's actually a keyword like "async", "get", or
747 // "set".
748 parseClassPropertyName(classContextId);
749 let isConstructor = false;
750 const token = _base.state.tokens[_base.state.tokens.length - 1];
751 // We allow "constructor" as either an identifier or a string.
752 if (token.contextualKeyword === _keywords.ContextualKeyword._constructor) {
753 isConstructor = true;
754 }
755 parsePostMemberNameModifiers();
756
757 if (isClassMethod()) {
758 parseClassMethod(memberStart, isConstructor);
759 } else if (isClassProperty()) {
760 parseClassProperty();
761 } else if (token.contextualKeyword === _keywords.ContextualKeyword._async && !_util.isLineTerminator.call(void 0, )) {
762 _base.state.tokens[_base.state.tokens.length - 1].type = _types.TokenType._async;
763 // an async method
764 const isGenerator = _tokenizer.match.call(void 0, _types.TokenType.star);
765 if (isGenerator) {
766 _tokenizer.next.call(void 0, );
767 }
768
769 // The so-called parsed name would have been "async": get the real name.
770 parseClassPropertyName(classContextId);
771 parsePostMemberNameModifiers();
772 parseClassMethod(memberStart, false /* isConstructor */);
773 } else if (
774 (token.contextualKeyword === _keywords.ContextualKeyword._get ||
775 token.contextualKeyword === _keywords.ContextualKeyword._set) &&
776 !(_util.isLineTerminator.call(void 0, ) && _tokenizer.match.call(void 0, _types.TokenType.star))
777 ) {
778 if (token.contextualKeyword === _keywords.ContextualKeyword._get) {
779 _base.state.tokens[_base.state.tokens.length - 1].type = _types.TokenType._get;
780 } else {
781 _base.state.tokens[_base.state.tokens.length - 1].type = _types.TokenType._set;
782 }
783 // `get\n*` is an uninitialized property named 'get' followed by a generator.
784 // a getter or setter
785 // The so-called parsed name would have been "get/set": get the real name.
786 parseClassPropertyName(classContextId);
787 parseClassMethod(memberStart, /* isConstructor */ false);
788 } else if (_util.isLineTerminator.call(void 0, )) {
789 // an uninitialized class property (due to ASI, since we don't otherwise recognize the next token)
790 parseClassProperty();
791 } else {
792 _util.unexpected.call(void 0, );
793 }
794}
795
796function parseClassMethod(functionStart, isConstructor) {
797 if (_base.isTypeScriptEnabled) {
798 _typescript.tsTryParseTypeParameters.call(void 0, );
799 } else if (_base.isFlowEnabled) {
800 if (_tokenizer.match.call(void 0, _types.TokenType.lessThan)) {
801 _flow.flowParseTypeParameterDeclaration.call(void 0, );
802 }
803 }
804 _expression.parseMethod.call(void 0, functionStart, isConstructor);
805}
806
807// Return the name of the class property, if it is a simple identifier.
808 function parseClassPropertyName(classContextId) {
809 _expression.parsePropertyName.call(void 0, classContextId);
810} exports.parseClassPropertyName = parseClassPropertyName;
811
812 function parsePostMemberNameModifiers() {
813 if (_base.isTypeScriptEnabled) {
814 const oldIsType = _tokenizer.pushTypeContext.call(void 0, 0);
815 _tokenizer.eat.call(void 0, _types.TokenType.question);
816 _tokenizer.popTypeContext.call(void 0, oldIsType);
817 }
818} exports.parsePostMemberNameModifiers = parsePostMemberNameModifiers;
819
820 function parseClassProperty() {
821 if (_base.isTypeScriptEnabled) {
822 _tokenizer.eat.call(void 0, _types.TokenType.bang);
823 _typescript.tsTryParseTypeAnnotation.call(void 0, );
824 } else if (_base.isFlowEnabled) {
825 if (_tokenizer.match.call(void 0, _types.TokenType.colon)) {
826 _flow.flowParseTypeAnnotation.call(void 0, );
827 }
828 }
829
830 if (_tokenizer.match.call(void 0, _types.TokenType.eq)) {
831 const equalsTokenIndex = _base.state.tokens.length;
832 _tokenizer.next.call(void 0, );
833 _expression.parseMaybeAssign.call(void 0, );
834 _base.state.tokens[equalsTokenIndex].rhsEndIndex = _base.state.tokens.length;
835 }
836 _util.semicolon.call(void 0, );
837} exports.parseClassProperty = parseClassProperty;
838
839function parseClassId(isStatement, optionalId = false) {
840 if (
841 _base.isTypeScriptEnabled &&
842 (!isStatement || optionalId) &&
843 _util.isContextual.call(void 0, _keywords.ContextualKeyword._implements)
844 ) {
845 return;
846 }
847
848 if (_tokenizer.match.call(void 0, _types.TokenType.name)) {
849 _lval.parseBindingIdentifier.call(void 0, true);
850 }
851
852 if (_base.isTypeScriptEnabled) {
853 _typescript.tsTryParseTypeParameters.call(void 0, );
854 } else if (_base.isFlowEnabled) {
855 if (_tokenizer.match.call(void 0, _types.TokenType.lessThan)) {
856 _flow.flowParseTypeParameterDeclaration.call(void 0, );
857 }
858 }
859}
860
861// Returns true if there was a superclass.
862function parseClassSuper() {
863 let hasSuper = false;
864 if (_tokenizer.eat.call(void 0, _types.TokenType._extends)) {
865 _expression.parseExprSubscripts.call(void 0, );
866 hasSuper = true;
867 } else {
868 hasSuper = false;
869 }
870 if (_base.isTypeScriptEnabled) {
871 _typescript.tsAfterParseClassSuper.call(void 0, hasSuper);
872 } else if (_base.isFlowEnabled) {
873 _flow.flowAfterParseClassSuper.call(void 0, hasSuper);
874 }
875}
876
877// Parses module export declaration.
878
879 function parseExport() {
880 const exportIndex = _base.state.tokens.length - 1;
881 if (_base.isTypeScriptEnabled) {
882 if (_typescript.tsTryParseExport.call(void 0, )) {
883 return;
884 }
885 }
886 // export * from '...'
887 if (shouldParseExportStar()) {
888 parseExportStar();
889 } else if (isExportDefaultSpecifier()) {
890 // export default from
891 _expression.parseIdentifier.call(void 0, );
892 if (_tokenizer.match.call(void 0, _types.TokenType.comma) && _tokenizer.lookaheadType.call(void 0, ) === _types.TokenType.star) {
893 _util.expect.call(void 0, _types.TokenType.comma);
894 _util.expect.call(void 0, _types.TokenType.star);
895 _util.expectContextual.call(void 0, _keywords.ContextualKeyword._as);
896 _expression.parseIdentifier.call(void 0, );
897 } else {
898 parseExportSpecifiersMaybe();
899 }
900 parseExportFrom();
901 } else if (_tokenizer.eat.call(void 0, _types.TokenType._default)) {
902 // export default ...
903 parseExportDefaultExpression();
904 } else if (shouldParseExportDeclaration()) {
905 parseExportDeclaration();
906 } else {
907 // export { x, y as z } [from '...']
908 parseExportSpecifiers();
909 parseExportFrom();
910 }
911 _base.state.tokens[exportIndex].rhsEndIndex = _base.state.tokens.length;
912} exports.parseExport = parseExport;
913
914function parseExportDefaultExpression() {
915 if (_base.isTypeScriptEnabled) {
916 if (_typescript.tsTryParseExportDefaultExpression.call(void 0, )) {
917 return;
918 }
919 }
920 const functionStart = _base.state.start;
921 if (_tokenizer.eat.call(void 0, _types.TokenType._function)) {
922 parseFunction(functionStart, true, true);
923 } else if (_util.isContextual.call(void 0, _keywords.ContextualKeyword._async) && _tokenizer.lookaheadType.call(void 0, ) === _types.TokenType._function) {
924 // async function declaration
925 _util.eatContextual.call(void 0, _keywords.ContextualKeyword._async);
926 _tokenizer.eat.call(void 0, _types.TokenType._function);
927 parseFunction(functionStart, true, true);
928 } else if (_tokenizer.match.call(void 0, _types.TokenType._class)) {
929 parseClass(true, true);
930 } else if (_tokenizer.match.call(void 0, _types.TokenType.at)) {
931 parseDecorators();
932 parseClass(true, true);
933 } else {
934 _expression.parseMaybeAssign.call(void 0, );
935 _util.semicolon.call(void 0, );
936 }
937}
938
939function parseExportDeclaration() {
940 if (_base.isTypeScriptEnabled) {
941 _typescript.tsParseExportDeclaration.call(void 0, );
942 } else if (_base.isFlowEnabled) {
943 _flow.flowParseExportDeclaration.call(void 0, );
944 } else {
945 parseStatement(true);
946 }
947}
948
949function isExportDefaultSpecifier() {
950 if (_base.isTypeScriptEnabled && _typescript.tsIsDeclarationStart.call(void 0, )) {
951 return false;
952 } else if (_base.isFlowEnabled && _flow.flowShouldDisallowExportDefaultSpecifier.call(void 0, )) {
953 return false;
954 }
955 if (_tokenizer.match.call(void 0, _types.TokenType.name)) {
956 return _base.state.contextualKeyword !== _keywords.ContextualKeyword._async;
957 }
958
959 if (!_tokenizer.match.call(void 0, _types.TokenType._default)) {
960 return false;
961 }
962
963 const _next = _tokenizer.nextTokenStart.call(void 0, );
964 const lookahead = _tokenizer.lookaheadTypeAndKeyword.call(void 0, );
965 const hasFrom =
966 lookahead.type === _types.TokenType.name && lookahead.contextualKeyword === _keywords.ContextualKeyword._from;
967 if (lookahead.type === _types.TokenType.comma) {
968 return true;
969 }
970 // lookahead again when `export default from` is seen
971 if (hasFrom) {
972 const nextAfterFrom = _base.input.charCodeAt(_tokenizer.nextTokenStartSince.call(void 0, _next + 4));
973 return nextAfterFrom === _charcodes.charCodes.quotationMark || nextAfterFrom === _charcodes.charCodes.apostrophe;
974 }
975 return false;
976}
977
978function parseExportSpecifiersMaybe() {
979 if (_tokenizer.eat.call(void 0, _types.TokenType.comma)) {
980 parseExportSpecifiers();
981 }
982}
983
984 function parseExportFrom() {
985 if (_util.eatContextual.call(void 0, _keywords.ContextualKeyword._from)) {
986 _expression.parseExprAtom.call(void 0, );
987 }
988 _util.semicolon.call(void 0, );
989} exports.parseExportFrom = parseExportFrom;
990
991function shouldParseExportStar() {
992 if (_base.isFlowEnabled) {
993 return _flow.flowShouldParseExportStar.call(void 0, );
994 } else {
995 return _tokenizer.match.call(void 0, _types.TokenType.star);
996 }
997}
998
999function parseExportStar() {
1000 if (_base.isFlowEnabled) {
1001 _flow.flowParseExportStar.call(void 0, );
1002 } else {
1003 baseParseExportStar();
1004 }
1005}
1006
1007 function baseParseExportStar() {
1008 _util.expect.call(void 0, _types.TokenType.star);
1009
1010 if (_util.isContextual.call(void 0, _keywords.ContextualKeyword._as)) {
1011 parseExportNamespace();
1012 } else {
1013 parseExportFrom();
1014 }
1015} exports.baseParseExportStar = baseParseExportStar;
1016
1017function parseExportNamespace() {
1018 _tokenizer.next.call(void 0, );
1019 _base.state.tokens[_base.state.tokens.length - 1].type = _types.TokenType._as;
1020 _expression.parseIdentifier.call(void 0, );
1021 parseExportSpecifiersMaybe();
1022 parseExportFrom();
1023}
1024
1025function shouldParseExportDeclaration() {
1026 return (
1027 (_base.isTypeScriptEnabled && _typescript.tsIsDeclarationStart.call(void 0, )) ||
1028 (_base.isFlowEnabled && _flow.flowShouldParseExportDeclaration.call(void 0, )) ||
1029 _base.state.type === _types.TokenType._var ||
1030 _base.state.type === _types.TokenType._const ||
1031 _base.state.type === _types.TokenType._let ||
1032 _base.state.type === _types.TokenType._function ||
1033 _base.state.type === _types.TokenType._class ||
1034 _util.isContextual.call(void 0, _keywords.ContextualKeyword._async) ||
1035 _tokenizer.match.call(void 0, _types.TokenType.at)
1036 );
1037}
1038
1039// Parses a comma-separated list of module exports.
1040 function parseExportSpecifiers() {
1041 let first = true;
1042
1043 // export { x, y as z } [from '...']
1044 _util.expect.call(void 0, _types.TokenType.braceL);
1045
1046 while (!_tokenizer.eat.call(void 0, _types.TokenType.braceR) && !_base.state.error) {
1047 if (first) {
1048 first = false;
1049 } else {
1050 _util.expect.call(void 0, _types.TokenType.comma);
1051 if (_tokenizer.eat.call(void 0, _types.TokenType.braceR)) {
1052 break;
1053 }
1054 }
1055
1056 _expression.parseIdentifier.call(void 0, );
1057 _base.state.tokens[_base.state.tokens.length - 1].identifierRole = _tokenizer.IdentifierRole.ExportAccess;
1058 if (_util.eatContextual.call(void 0, _keywords.ContextualKeyword._as)) {
1059 _expression.parseIdentifier.call(void 0, );
1060 }
1061 }
1062} exports.parseExportSpecifiers = parseExportSpecifiers;
1063
1064// Parses import declaration.
1065
1066 function parseImport() {
1067 if (_base.isTypeScriptEnabled && _tokenizer.match.call(void 0, _types.TokenType.name) && _tokenizer.lookaheadType.call(void 0, ) === _types.TokenType.eq) {
1068 _typescript.tsParseImportEqualsDeclaration.call(void 0, );
1069 return;
1070 }
1071 if (_base.isTypeScriptEnabled && _util.isContextual.call(void 0, _keywords.ContextualKeyword._type)) {
1072 const lookahead = _tokenizer.lookaheadType.call(void 0, );
1073 if (lookahead === _types.TokenType.name) {
1074 // One of these `import type` cases:
1075 // import type T = require('T');
1076 // import type A from 'A';
1077 _util.expectContextual.call(void 0, _keywords.ContextualKeyword._type);
1078 if (_tokenizer.lookaheadType.call(void 0, ) === _types.TokenType.eq) {
1079 _typescript.tsParseImportEqualsDeclaration.call(void 0, );
1080 return;
1081 }
1082 // If this is an `import type...from` statement, then we already ate the
1083 // type token, so proceed to the regular import parser.
1084 } else if (lookahead === _types.TokenType.star || lookahead === _types.TokenType.braceL) {
1085 // One of these `import type` cases, in which case we can eat the type token
1086 // and proceed as normal:
1087 // import type * as A from 'A';
1088 // import type {a} from 'A';
1089 _util.expectContextual.call(void 0, _keywords.ContextualKeyword._type);
1090 }
1091 // Otherwise, we are importing the name "type".
1092 }
1093
1094 // import '...'
1095 if (_tokenizer.match.call(void 0, _types.TokenType.string)) {
1096 _expression.parseExprAtom.call(void 0, );
1097 } else {
1098 parseImportSpecifiers();
1099 _util.expectContextual.call(void 0, _keywords.ContextualKeyword._from);
1100 _expression.parseExprAtom.call(void 0, );
1101 }
1102 _util.semicolon.call(void 0, );
1103} exports.parseImport = parseImport;
1104
1105// eslint-disable-next-line no-unused-vars
1106function shouldParseDefaultImport() {
1107 return _tokenizer.match.call(void 0, _types.TokenType.name);
1108}
1109
1110function parseImportSpecifierLocal() {
1111 _lval.parseImportedIdentifier.call(void 0, );
1112}
1113
1114// Parses a comma-separated list of module imports.
1115function parseImportSpecifiers() {
1116 if (_base.isFlowEnabled) {
1117 _flow.flowStartParseImportSpecifiers.call(void 0, );
1118 }
1119
1120 let first = true;
1121 if (shouldParseDefaultImport()) {
1122 // import defaultObj, { x, y as z } from '...'
1123 parseImportSpecifierLocal();
1124
1125 if (!_tokenizer.eat.call(void 0, _types.TokenType.comma)) return;
1126 }
1127
1128 if (_tokenizer.match.call(void 0, _types.TokenType.star)) {
1129 _tokenizer.next.call(void 0, );
1130 _util.expectContextual.call(void 0, _keywords.ContextualKeyword._as);
1131
1132 parseImportSpecifierLocal();
1133
1134 return;
1135 }
1136
1137 _util.expect.call(void 0, _types.TokenType.braceL);
1138 while (!_tokenizer.eat.call(void 0, _types.TokenType.braceR) && !_base.state.error) {
1139 if (first) {
1140 first = false;
1141 } else {
1142 // Detect an attempt to deep destructure
1143 if (_tokenizer.eat.call(void 0, _types.TokenType.colon)) {
1144 _util.unexpected.call(void 0,
1145 "ES2015 named imports do not destructure. Use another statement for destructuring after the import.",
1146 );
1147 }
1148
1149 _util.expect.call(void 0, _types.TokenType.comma);
1150 if (_tokenizer.eat.call(void 0, _types.TokenType.braceR)) {
1151 break;
1152 }
1153 }
1154
1155 parseImportSpecifier();
1156 }
1157}
1158
1159function parseImportSpecifier() {
1160 if (_base.isFlowEnabled) {
1161 _flow.flowParseImportSpecifier.call(void 0, );
1162 return;
1163 }
1164 _lval.parseImportedIdentifier.call(void 0, );
1165 if (_util.isContextual.call(void 0, _keywords.ContextualKeyword._as)) {
1166 _base.state.tokens[_base.state.tokens.length - 1].identifierRole = _tokenizer.IdentifierRole.ImportAccess;
1167 _tokenizer.next.call(void 0, );
1168 _lval.parseImportedIdentifier.call(void 0, );
1169 }
1170}