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