UNPKG

47.3 kBJavaScriptView Raw
1"use strict";Object.defineProperty(exports, "__esModule", {value: true});
2
3
4
5
6
7
8
9var _index = require('../tokenizer/index');
10var _keywords = require('../tokenizer/keywords');
11var _types = require('../tokenizer/types');
12var _base = require('../traverser/base');
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28var _expression = require('../traverser/expression');
29var _lval = require('../traverser/lval');
30
31
32
33
34
35
36
37
38
39
40
41var _statement = require('../traverser/statement');
42
43
44
45
46
47
48
49
50
51
52
53var _util = require('../traverser/util');
54var _jsx = require('./jsx');
55
56function tsIsIdentifier() {
57 // TODO: actually a bit more complex in TypeScript, but shouldn't matter.
58 // See https://github.com/Microsoft/TypeScript/issues/15008
59 return _index.match.call(void 0, _types.TokenType.name);
60}
61
62function tsNextTokenCanFollowModifier() {
63 // Note: TypeScript's implementation is much more complicated because
64 // more things are considered modifiers there.
65 // This implementation only handles modifiers not handled by babylon itself. And "static".
66 // TODO: Would be nice to avoid lookahead. Want a hasLineBreakUpNext() method...
67 const snapshot = _base.state.snapshot();
68
69 _index.next.call(void 0, );
70 const canFollowModifier =
71 !_util.hasPrecedingLineBreak.call(void 0, ) &&
72 !_index.match.call(void 0, _types.TokenType.parenL) &&
73 !_index.match.call(void 0, _types.TokenType.parenR) &&
74 !_index.match.call(void 0, _types.TokenType.colon) &&
75 !_index.match.call(void 0, _types.TokenType.eq) &&
76 !_index.match.call(void 0, _types.TokenType.question);
77
78 if (canFollowModifier) {
79 return true;
80 } else {
81 _base.state.restoreFromSnapshot(snapshot);
82 return false;
83 }
84}
85
86/** Parses a modifier matching one the given modifier names. */
87 function tsParseModifier(
88 allowedModifiers,
89) {
90 if (!_index.match.call(void 0, _types.TokenType.name)) {
91 return null;
92 }
93
94 const modifier = _base.state.contextualKeyword;
95 if (allowedModifiers.indexOf(modifier) !== -1 && tsNextTokenCanFollowModifier()) {
96 switch (modifier) {
97 case _keywords.ContextualKeyword._readonly:
98 _base.state.tokens[_base.state.tokens.length - 1].type = _types.TokenType._readonly;
99 break;
100 case _keywords.ContextualKeyword._abstract:
101 _base.state.tokens[_base.state.tokens.length - 1].type = _types.TokenType._abstract;
102 break;
103 case _keywords.ContextualKeyword._static:
104 _base.state.tokens[_base.state.tokens.length - 1].type = _types.TokenType._static;
105 break;
106 case _keywords.ContextualKeyword._public:
107 _base.state.tokens[_base.state.tokens.length - 1].type = _types.TokenType._public;
108 break;
109 case _keywords.ContextualKeyword._private:
110 _base.state.tokens[_base.state.tokens.length - 1].type = _types.TokenType._private;
111 break;
112 case _keywords.ContextualKeyword._protected:
113 _base.state.tokens[_base.state.tokens.length - 1].type = _types.TokenType._protected;
114 break;
115 default:
116 break;
117 }
118 return modifier;
119 }
120 return null;
121} exports.tsParseModifier = tsParseModifier;
122
123function tsParseEntityName() {
124 _expression.parseIdentifier.call(void 0, );
125 while (_index.eat.call(void 0, _types.TokenType.dot)) {
126 _expression.parseIdentifier.call(void 0, );
127 }
128}
129
130function tsParseTypeReference() {
131 tsParseEntityName();
132 if (!_util.hasPrecedingLineBreak.call(void 0, ) && _index.match.call(void 0, _types.TokenType.lessThan)) {
133 tsParseTypeArguments();
134 }
135}
136
137function tsParseThisTypePredicate() {
138 _index.next.call(void 0, );
139 tsParseTypeAnnotation();
140}
141
142function tsParseThisTypeNode() {
143 _index.next.call(void 0, );
144}
145
146function tsParseTypeQuery() {
147 _util.expect.call(void 0, _types.TokenType._typeof);
148 if (_index.match.call(void 0, _types.TokenType._import)) {
149 tsParseImportType();
150 } else {
151 tsParseEntityName();
152 }
153}
154
155function tsParseImportType() {
156 _util.expect.call(void 0, _types.TokenType._import);
157 _util.expect.call(void 0, _types.TokenType.parenL);
158 _util.expect.call(void 0, _types.TokenType.string);
159 _util.expect.call(void 0, _types.TokenType.parenR);
160 if (_index.eat.call(void 0, _types.TokenType.dot)) {
161 tsParseEntityName();
162 }
163 if (_index.match.call(void 0, _types.TokenType.lessThan)) {
164 tsParseTypeArguments();
165 }
166}
167
168function tsParseTypeParameter() {
169 _expression.parseIdentifier.call(void 0, );
170 if (_index.eat.call(void 0, _types.TokenType._extends)) {
171 tsParseType();
172 }
173 if (_index.eat.call(void 0, _types.TokenType.eq)) {
174 tsParseType();
175 }
176}
177
178 function tsTryParseTypeParameters() {
179 if (_index.match.call(void 0, _types.TokenType.lessThan)) {
180 tsParseTypeParameters();
181 }
182} exports.tsTryParseTypeParameters = tsTryParseTypeParameters;
183
184function tsParseTypeParameters() {
185 const oldIsType = _index.pushTypeContext.call(void 0, 0);
186 if (_index.match.call(void 0, _types.TokenType.lessThan) || _index.match.call(void 0, _types.TokenType.typeParameterStart)) {
187 _index.next.call(void 0, );
188 } else {
189 _util.unexpected.call(void 0, );
190 }
191
192 while (!_index.eat.call(void 0, _types.TokenType.greaterThan) && !_base.state.error) {
193 tsParseTypeParameter();
194 _index.eat.call(void 0, _types.TokenType.comma);
195 }
196 _index.popTypeContext.call(void 0, oldIsType);
197}
198
199// Note: In TypeScript implementation we must provide `yieldContext` and `awaitContext`,
200// but here it's always false, because this is only used for types.
201function tsFillSignature(returnToken) {
202 // Arrow fns *must* have return token (`=>`). Normal functions can omit it.
203 const returnTokenRequired = returnToken === _types.TokenType.arrow;
204 tsTryParseTypeParameters();
205 _util.expect.call(void 0, _types.TokenType.parenL);
206 tsParseBindingListForSignature(false /* isBlockScope */);
207 if (returnTokenRequired) {
208 tsParseTypeOrTypePredicateAnnotation(returnToken);
209 } else if (_index.match.call(void 0, returnToken)) {
210 tsParseTypeOrTypePredicateAnnotation(returnToken);
211 }
212}
213
214function tsParseBindingListForSignature(isBlockScope) {
215 _lval.parseBindingList.call(void 0, _types.TokenType.parenR, isBlockScope);
216}
217
218function tsParseTypeMemberSemicolon() {
219 if (!_index.eat.call(void 0, _types.TokenType.comma)) {
220 _util.semicolon.call(void 0, );
221 }
222}
223
224var SignatureMemberKind; (function (SignatureMemberKind) {
225 const TSCallSignatureDeclaration = 0; SignatureMemberKind[SignatureMemberKind["TSCallSignatureDeclaration"] = TSCallSignatureDeclaration] = "TSCallSignatureDeclaration";
226 const TSConstructSignatureDeclaration = TSCallSignatureDeclaration + 1; SignatureMemberKind[SignatureMemberKind["TSConstructSignatureDeclaration"] = TSConstructSignatureDeclaration] = "TSConstructSignatureDeclaration";
227})(SignatureMemberKind || (SignatureMemberKind = {}));
228
229function tsParseSignatureMember(kind) {
230 if (kind === SignatureMemberKind.TSConstructSignatureDeclaration) {
231 _util.expect.call(void 0, _types.TokenType._new);
232 }
233 tsFillSignature(_types.TokenType.colon);
234 tsParseTypeMemberSemicolon();
235}
236
237function tsIsUnambiguouslyIndexSignature() {
238 const snapshot = _base.state.snapshot();
239 _index.next.call(void 0, ); // Skip '{'
240 const isIndexSignature = _index.eat.call(void 0, _types.TokenType.name) && _index.match.call(void 0, _types.TokenType.colon);
241 _base.state.restoreFromSnapshot(snapshot);
242 return isIndexSignature;
243}
244
245function tsTryParseIndexSignature() {
246 if (!(_index.match.call(void 0, _types.TokenType.bracketL) && tsIsUnambiguouslyIndexSignature())) {
247 return false;
248 }
249
250 const oldIsType = _index.pushTypeContext.call(void 0, 0);
251
252 _util.expect.call(void 0, _types.TokenType.bracketL);
253 _expression.parseIdentifier.call(void 0, );
254 tsParseTypeAnnotation();
255 _util.expect.call(void 0, _types.TokenType.bracketR);
256
257 tsTryParseTypeAnnotation();
258 tsParseTypeMemberSemicolon();
259
260 _index.popTypeContext.call(void 0, oldIsType);
261 return true;
262}
263
264function tsParsePropertyOrMethodSignature(isReadonly) {
265 _expression.parsePropertyName.call(void 0, -1 /* Types don't need context IDs. */);
266 _index.eat.call(void 0, _types.TokenType.question);
267
268 if (!isReadonly && (_index.match.call(void 0, _types.TokenType.parenL) || _index.match.call(void 0, _types.TokenType.lessThan))) {
269 tsFillSignature(_types.TokenType.colon);
270 tsParseTypeMemberSemicolon();
271 } else {
272 tsTryParseTypeAnnotation();
273 tsParseTypeMemberSemicolon();
274 }
275}
276
277function tsParseTypeMember() {
278 if (_index.match.call(void 0, _types.TokenType.parenL) || _index.match.call(void 0, _types.TokenType.lessThan)) {
279 tsParseSignatureMember(SignatureMemberKind.TSCallSignatureDeclaration);
280 return;
281 }
282 if (_index.match.call(void 0, _types.TokenType._new) && tsIsStartOfConstructSignature()) {
283 tsParseSignatureMember(SignatureMemberKind.TSConstructSignatureDeclaration);
284 return;
285 }
286 const readonly = !!tsParseModifier([_keywords.ContextualKeyword._readonly]);
287
288 const found = tsTryParseIndexSignature();
289 if (found) {
290 return;
291 }
292 tsParsePropertyOrMethodSignature(readonly);
293}
294
295function tsIsStartOfConstructSignature() {
296 const lookahead = _index.lookaheadType.call(void 0, );
297 return lookahead === _types.TokenType.parenL || lookahead === _types.TokenType.lessThan;
298}
299
300function tsParseTypeLiteral() {
301 tsParseObjectTypeMembers();
302}
303
304function tsParseObjectTypeMembers() {
305 _util.expect.call(void 0, _types.TokenType.braceL);
306 while (!_index.eat.call(void 0, _types.TokenType.braceR) && !_base.state.error) {
307 tsParseTypeMember();
308 }
309}
310
311function tsLookaheadIsStartOfMappedType() {
312 const snapshot = _base.state.snapshot();
313 const isStartOfMappedType = tsIsStartOfMappedType();
314 _base.state.restoreFromSnapshot(snapshot);
315 return isStartOfMappedType;
316}
317
318function tsIsStartOfMappedType() {
319 _index.next.call(void 0, );
320 if (_index.eat.call(void 0, _types.TokenType.plus) || _index.eat.call(void 0, _types.TokenType.minus)) {
321 return _util.isContextual.call(void 0, _keywords.ContextualKeyword._readonly);
322 }
323 if (_util.isContextual.call(void 0, _keywords.ContextualKeyword._readonly)) {
324 _index.next.call(void 0, );
325 }
326 if (!_index.match.call(void 0, _types.TokenType.bracketL)) {
327 return false;
328 }
329 _index.next.call(void 0, );
330 if (!tsIsIdentifier()) {
331 return false;
332 }
333 _index.next.call(void 0, );
334 return _index.match.call(void 0, _types.TokenType._in);
335}
336
337function tsParseMappedTypeParameter() {
338 _expression.parseIdentifier.call(void 0, );
339 _util.expect.call(void 0, _types.TokenType._in);
340 tsParseType();
341}
342
343function tsParseMappedType() {
344 _util.expect.call(void 0, _types.TokenType.braceL);
345 if (_index.match.call(void 0, _types.TokenType.plus) || _index.match.call(void 0, _types.TokenType.minus)) {
346 _index.next.call(void 0, );
347 _util.expectContextual.call(void 0, _keywords.ContextualKeyword._readonly);
348 } else {
349 _util.eatContextual.call(void 0, _keywords.ContextualKeyword._readonly);
350 }
351 _util.expect.call(void 0, _types.TokenType.bracketL);
352 tsParseMappedTypeParameter();
353 _util.expect.call(void 0, _types.TokenType.bracketR);
354 if (_index.match.call(void 0, _types.TokenType.plus) || _index.match.call(void 0, _types.TokenType.minus)) {
355 _index.next.call(void 0, );
356 _util.expect.call(void 0, _types.TokenType.question);
357 } else {
358 _index.eat.call(void 0, _types.TokenType.question);
359 }
360 tsTryParseType();
361 _util.semicolon.call(void 0, );
362 _util.expect.call(void 0, _types.TokenType.braceR);
363}
364
365function tsParseTupleType() {
366 _util.expect.call(void 0, _types.TokenType.bracketL);
367 while (!_index.eat.call(void 0, _types.TokenType.bracketR) && !_base.state.error) {
368 tsParseTupleElementType();
369 _index.eat.call(void 0, _types.TokenType.comma);
370 }
371}
372
373function tsParseTupleElementType() {
374 // parses `...TsType[]`
375 if (_index.eat.call(void 0, _types.TokenType.ellipsis)) {
376 tsParseType();
377 return;
378 }
379 // parses `TsType?`
380 tsParseType();
381 _index.eat.call(void 0, _types.TokenType.question);
382}
383
384function tsParseParenthesizedType() {
385 _util.expect.call(void 0, _types.TokenType.parenL);
386 tsParseType();
387 _util.expect.call(void 0, _types.TokenType.parenR);
388}
389
390var FunctionType; (function (FunctionType) {
391 const TSFunctionType = 0; FunctionType[FunctionType["TSFunctionType"] = TSFunctionType] = "TSFunctionType";
392 const TSConstructorType = TSFunctionType + 1; FunctionType[FunctionType["TSConstructorType"] = TSConstructorType] = "TSConstructorType";
393})(FunctionType || (FunctionType = {}));
394
395function tsParseFunctionOrConstructorType(type) {
396 if (type === FunctionType.TSConstructorType) {
397 _util.expect.call(void 0, _types.TokenType._new);
398 }
399 tsFillSignature(_types.TokenType.arrow);
400}
401
402function tsParseNonArrayType() {
403 switch (_base.state.type) {
404 case _types.TokenType.name:
405 tsParseTypeReference();
406 return;
407 case _types.TokenType._void:
408 case _types.TokenType._null:
409 _index.next.call(void 0, );
410 return;
411 case _types.TokenType.string:
412 case _types.TokenType.num:
413 case _types.TokenType._true:
414 case _types.TokenType._false:
415 _expression.parseLiteral.call(void 0, );
416 return;
417 case _types.TokenType.minus:
418 _index.next.call(void 0, );
419 _expression.parseLiteral.call(void 0, );
420 return;
421 case _types.TokenType._this: {
422 tsParseThisTypeNode();
423 if (_util.isContextual.call(void 0, _keywords.ContextualKeyword._is) && !_util.hasPrecedingLineBreak.call(void 0, )) {
424 tsParseThisTypePredicate();
425 }
426 return;
427 }
428 case _types.TokenType._typeof:
429 tsParseTypeQuery();
430 return;
431 case _types.TokenType._import:
432 tsParseImportType();
433 return;
434 case _types.TokenType.braceL:
435 if (tsLookaheadIsStartOfMappedType()) {
436 tsParseMappedType();
437 } else {
438 tsParseTypeLiteral();
439 }
440 return;
441 case _types.TokenType.bracketL:
442 tsParseTupleType();
443 return;
444 case _types.TokenType.parenL:
445 tsParseParenthesizedType();
446 return;
447 default:
448 break;
449 }
450
451 _util.unexpected.call(void 0, );
452}
453
454function tsParseArrayTypeOrHigher() {
455 tsParseNonArrayType();
456 while (!_util.hasPrecedingLineBreak.call(void 0, ) && _index.eat.call(void 0, _types.TokenType.bracketL)) {
457 if (!_index.eat.call(void 0, _types.TokenType.bracketR)) {
458 // If we hit ] immediately, this is an array type, otherwise it's an indexed access type.
459 tsParseType();
460 _util.expect.call(void 0, _types.TokenType.bracketR);
461 }
462 }
463}
464
465function tsParseInferType() {
466 _util.expectContextual.call(void 0, _keywords.ContextualKeyword._infer);
467 _expression.parseIdentifier.call(void 0, );
468}
469
470function tsParseTypeOperatorOrHigher() {
471 if (_util.isContextual.call(void 0, _keywords.ContextualKeyword._keyof) || _util.isContextual.call(void 0, _keywords.ContextualKeyword._unique)) {
472 _index.next.call(void 0, );
473 tsParseTypeOperatorOrHigher();
474 } else if (_util.isContextual.call(void 0, _keywords.ContextualKeyword._infer)) {
475 tsParseInferType();
476 } else {
477 tsParseArrayTypeOrHigher();
478 }
479}
480
481function tsParseIntersectionTypeOrHigher() {
482 _index.eat.call(void 0, _types.TokenType.bitwiseAND);
483 tsParseTypeOperatorOrHigher();
484 if (_index.match.call(void 0, _types.TokenType.bitwiseAND)) {
485 while (_index.eat.call(void 0, _types.TokenType.bitwiseAND)) {
486 tsParseTypeOperatorOrHigher();
487 }
488 }
489}
490
491function tsParseUnionTypeOrHigher() {
492 _index.eat.call(void 0, _types.TokenType.bitwiseOR);
493 tsParseIntersectionTypeOrHigher();
494 if (_index.match.call(void 0, _types.TokenType.bitwiseOR)) {
495 while (_index.eat.call(void 0, _types.TokenType.bitwiseOR)) {
496 tsParseIntersectionTypeOrHigher();
497 }
498 }
499}
500
501function tsIsStartOfFunctionType() {
502 if (_index.match.call(void 0, _types.TokenType.lessThan)) {
503 return true;
504 }
505 return _index.match.call(void 0, _types.TokenType.parenL) && tsLookaheadIsUnambiguouslyStartOfFunctionType();
506}
507
508function tsSkipParameterStart() {
509 if (_index.match.call(void 0, _types.TokenType.name) || _index.match.call(void 0, _types.TokenType._this)) {
510 _index.next.call(void 0, );
511 return true;
512 }
513 // If this is a possible array/object destructure, walk to the matching bracket/brace.
514 // The next token after will tell us definitively whether this is a function param.
515 if (_index.match.call(void 0, _types.TokenType.braceL) || _index.match.call(void 0, _types.TokenType.bracketL)) {
516 let depth = 1;
517 _index.next.call(void 0, );
518 while (depth > 0 && !_base.state.error) {
519 if (_index.match.call(void 0, _types.TokenType.braceL) || _index.match.call(void 0, _types.TokenType.bracketL)) {
520 depth++;
521 } else if (_index.match.call(void 0, _types.TokenType.braceR) || _index.match.call(void 0, _types.TokenType.bracketR)) {
522 depth--;
523 }
524 _index.next.call(void 0, );
525 }
526 return true;
527 }
528 return false;
529}
530
531function tsLookaheadIsUnambiguouslyStartOfFunctionType() {
532 const snapshot = _base.state.snapshot();
533 const isUnambiguouslyStartOfFunctionType = tsIsUnambiguouslyStartOfFunctionType();
534 _base.state.restoreFromSnapshot(snapshot);
535 return isUnambiguouslyStartOfFunctionType;
536}
537
538function tsIsUnambiguouslyStartOfFunctionType() {
539 _index.next.call(void 0, );
540 if (_index.match.call(void 0, _types.TokenType.parenR) || _index.match.call(void 0, _types.TokenType.ellipsis)) {
541 // ( )
542 // ( ...
543 return true;
544 }
545 if (tsSkipParameterStart()) {
546 if (_index.match.call(void 0, _types.TokenType.colon) || _index.match.call(void 0, _types.TokenType.comma) || _index.match.call(void 0, _types.TokenType.question) || _index.match.call(void 0, _types.TokenType.eq)) {
547 // ( xxx :
548 // ( xxx ,
549 // ( xxx ?
550 // ( xxx =
551 return true;
552 }
553 if (_index.match.call(void 0, _types.TokenType.parenR)) {
554 _index.next.call(void 0, );
555 if (_index.match.call(void 0, _types.TokenType.arrow)) {
556 // ( xxx ) =>
557 return true;
558 }
559 }
560 }
561 return false;
562}
563
564function tsParseTypeOrTypePredicateAnnotation(returnToken) {
565 const oldIsType = _index.pushTypeContext.call(void 0, 0);
566 _util.expect.call(void 0, returnToken);
567 tsParseTypePredicatePrefix();
568 // Regardless of whether we found an "is" token, there's now just a regular type in front of
569 // us.
570 tsParseType();
571 _index.popTypeContext.call(void 0, oldIsType);
572}
573
574function tsTryParseTypeOrTypePredicateAnnotation() {
575 if (_index.match.call(void 0, _types.TokenType.colon)) {
576 tsParseTypeOrTypePredicateAnnotation(_types.TokenType.colon);
577 }
578}
579
580 function tsTryParseTypeAnnotation() {
581 if (_index.match.call(void 0, _types.TokenType.colon)) {
582 tsParseTypeAnnotation();
583 }
584} exports.tsTryParseTypeAnnotation = tsTryParseTypeAnnotation;
585
586function tsTryParseType() {
587 if (_index.eat.call(void 0, _types.TokenType.colon)) {
588 tsParseType();
589 }
590}
591
592function tsParseTypePredicatePrefix() {
593 const snapshot = _base.state.snapshot();
594 _expression.parseIdentifier.call(void 0, );
595 if (_util.isContextual.call(void 0, _keywords.ContextualKeyword._is) && !_util.hasPrecedingLineBreak.call(void 0, )) {
596 _index.next.call(void 0, );
597 } else {
598 _base.state.restoreFromSnapshot(snapshot);
599 }
600}
601
602 function tsParseTypeAnnotation() {
603 const oldIsType = _index.pushTypeContext.call(void 0, 0);
604 _util.expect.call(void 0, _types.TokenType.colon);
605 tsParseType();
606 _index.popTypeContext.call(void 0, oldIsType);
607} exports.tsParseTypeAnnotation = tsParseTypeAnnotation;
608
609 function tsParseType() {
610 tsParseNonConditionalType();
611 if (_util.hasPrecedingLineBreak.call(void 0, ) || !_index.eat.call(void 0, _types.TokenType._extends)) {
612 return;
613 }
614 // extends type
615 tsParseNonConditionalType();
616 _util.expect.call(void 0, _types.TokenType.question);
617 // true type
618 tsParseType();
619 _util.expect.call(void 0, _types.TokenType.colon);
620 // false type
621 tsParseType();
622} exports.tsParseType = tsParseType;
623
624 function tsParseNonConditionalType() {
625 if (tsIsStartOfFunctionType()) {
626 tsParseFunctionOrConstructorType(FunctionType.TSFunctionType);
627 return;
628 }
629 if (_index.match.call(void 0, _types.TokenType._new)) {
630 // As in `new () => Date`
631 tsParseFunctionOrConstructorType(FunctionType.TSConstructorType);
632 return;
633 }
634 tsParseUnionTypeOrHigher();
635} exports.tsParseNonConditionalType = tsParseNonConditionalType;
636
637 function tsParseTypeAssertion() {
638 const oldIsType = _index.pushTypeContext.call(void 0, 1);
639 tsParseType();
640 _util.expect.call(void 0, _types.TokenType.greaterThan);
641 _index.popTypeContext.call(void 0, oldIsType);
642 _expression.parseMaybeUnary.call(void 0, );
643} exports.tsParseTypeAssertion = tsParseTypeAssertion;
644
645 function tsTryParseJSXTypeArgument() {
646 if (_index.eat.call(void 0, _types.TokenType.jsxTagStart)) {
647 _base.state.tokens[_base.state.tokens.length - 1].type = _types.TokenType.typeParameterStart;
648 const oldIsType = _index.pushTypeContext.call(void 0, 1);
649 while (!_index.match.call(void 0, _types.TokenType.greaterThan) && !_base.state.error) {
650 tsParseType();
651 _index.eat.call(void 0, _types.TokenType.comma);
652 }
653 // Process >, but the one after needs to be parsed JSX-style.
654 _jsx.nextJSXTagToken.call(void 0, );
655 _index.popTypeContext.call(void 0, oldIsType);
656 }
657} exports.tsTryParseJSXTypeArgument = tsTryParseJSXTypeArgument;
658
659function tsParseHeritageClause() {
660 while (!_index.match.call(void 0, _types.TokenType.braceL) && !_base.state.error) {
661 tsParseExpressionWithTypeArguments();
662 _index.eat.call(void 0, _types.TokenType.comma);
663 }
664}
665
666function tsParseExpressionWithTypeArguments() {
667 // Note: TS uses parseLeftHandSideExpressionOrHigher,
668 // then has grammar errors later if it's not an EntityName.
669 tsParseEntityName();
670 if (_index.match.call(void 0, _types.TokenType.lessThan)) {
671 tsParseTypeArguments();
672 }
673}
674
675function tsParseInterfaceDeclaration() {
676 _expression.parseIdentifier.call(void 0, );
677 tsTryParseTypeParameters();
678 if (_index.eat.call(void 0, _types.TokenType._extends)) {
679 tsParseHeritageClause();
680 }
681 tsParseObjectTypeMembers();
682}
683
684function tsParseTypeAliasDeclaration() {
685 _expression.parseIdentifier.call(void 0, );
686 tsTryParseTypeParameters();
687 _util.expect.call(void 0, _types.TokenType.eq);
688 tsParseType();
689 _util.semicolon.call(void 0, );
690}
691
692function tsParseEnumMember() {
693 // Computed property names are grammar errors in an enum, so accept just string literal or identifier.
694 if (_index.match.call(void 0, _types.TokenType.string)) {
695 _expression.parseLiteral.call(void 0, );
696 } else {
697 _expression.parseIdentifier.call(void 0, );
698 }
699 if (_index.eat.call(void 0, _types.TokenType.eq)) {
700 const eqIndex = _base.state.tokens.length - 1;
701 _expression.parseMaybeAssign.call(void 0, );
702 _base.state.tokens[eqIndex].rhsEndIndex = _base.state.tokens.length;
703 }
704}
705
706function tsParseEnumDeclaration() {
707 _expression.parseIdentifier.call(void 0, );
708 _util.expect.call(void 0, _types.TokenType.braceL);
709 while (!_index.eat.call(void 0, _types.TokenType.braceR) && !_base.state.error) {
710 tsParseEnumMember();
711 _index.eat.call(void 0, _types.TokenType.comma);
712 }
713}
714
715function tsParseModuleBlock() {
716 _util.expect.call(void 0, _types.TokenType.braceL);
717 _statement.parseBlockBody.call(void 0, /* end */ _types.TokenType.braceR);
718}
719
720function tsParseModuleOrNamespaceDeclaration() {
721 _expression.parseIdentifier.call(void 0, );
722 if (_index.eat.call(void 0, _types.TokenType.dot)) {
723 tsParseModuleOrNamespaceDeclaration();
724 } else {
725 tsParseModuleBlock();
726 }
727}
728
729function tsParseAmbientExternalModuleDeclaration() {
730 if (_util.isContextual.call(void 0, _keywords.ContextualKeyword._global)) {
731 _expression.parseIdentifier.call(void 0, );
732 } else if (_index.match.call(void 0, _types.TokenType.string)) {
733 _expression.parseExprAtom.call(void 0, );
734 } else {
735 _util.unexpected.call(void 0, );
736 }
737
738 if (_index.match.call(void 0, _types.TokenType.braceL)) {
739 tsParseModuleBlock();
740 } else {
741 _util.semicolon.call(void 0, );
742 }
743}
744
745 function tsParseImportEqualsDeclaration() {
746 _expression.parseIdentifier.call(void 0, );
747 _util.expect.call(void 0, _types.TokenType.eq);
748 tsParseModuleReference();
749 _util.semicolon.call(void 0, );
750} exports.tsParseImportEqualsDeclaration = tsParseImportEqualsDeclaration;
751
752function tsIsExternalModuleReference() {
753 return _util.isContextual.call(void 0, _keywords.ContextualKeyword._require) && _index.lookaheadType.call(void 0, ) === _types.TokenType.parenL;
754}
755
756function tsParseModuleReference() {
757 if (tsIsExternalModuleReference()) {
758 tsParseExternalModuleReference();
759 } else {
760 tsParseEntityName();
761 }
762}
763
764function tsParseExternalModuleReference() {
765 _util.expectContextual.call(void 0, _keywords.ContextualKeyword._require);
766 _util.expect.call(void 0, _types.TokenType.parenL);
767 if (!_index.match.call(void 0, _types.TokenType.string)) {
768 _util.unexpected.call(void 0, );
769 }
770 _expression.parseLiteral.call(void 0, );
771 _util.expect.call(void 0, _types.TokenType.parenR);
772}
773
774// Utilities
775
776// Returns true if a statement matched.
777function tsTryParseDeclare() {
778 switch (_base.state.type) {
779 case _types.TokenType._function: {
780 const oldIsType = _index.pushTypeContext.call(void 0, 1);
781 _index.next.call(void 0, );
782 // We don't need to precisely get the function start here, since it's only used to mark
783 // the function as a type if it's bodiless, and it's already a type here.
784 const functionStart = _base.state.start;
785 _statement.parseFunction.call(void 0, functionStart, /* isStatement */ true);
786 _index.popTypeContext.call(void 0, oldIsType);
787 return true;
788 }
789 case _types.TokenType._class: {
790 const oldIsType = _index.pushTypeContext.call(void 0, 1);
791 _statement.parseClass.call(void 0, /* isStatement */ true, /* optionalId */ false);
792 _index.popTypeContext.call(void 0, oldIsType);
793 return true;
794 }
795 case _types.TokenType._const: {
796 if (_index.match.call(void 0, _types.TokenType._const) && _util.isLookaheadContextual.call(void 0, _keywords.ContextualKeyword._enum)) {
797 const oldIsType = _index.pushTypeContext.call(void 0, 1);
798 // `const enum = 0;` not allowed because "enum" is a strict mode reserved word.
799 _util.expect.call(void 0, _types.TokenType._const);
800 _util.expectContextual.call(void 0, _keywords.ContextualKeyword._enum);
801 _base.state.tokens[_base.state.tokens.length - 1].type = _types.TokenType._enum;
802 tsParseEnumDeclaration();
803 _index.popTypeContext.call(void 0, oldIsType);
804 return true;
805 }
806 }
807 // falls through
808 case _types.TokenType._var:
809 case _types.TokenType._let: {
810 const oldIsType = _index.pushTypeContext.call(void 0, 1);
811 _statement.parseVarStatement.call(void 0, _base.state.type);
812 _index.popTypeContext.call(void 0, oldIsType);
813 return true;
814 }
815 case _types.TokenType.name: {
816 const oldIsType = _index.pushTypeContext.call(void 0, 1);
817 const contextualKeyword = _base.state.contextualKeyword;
818 let matched = false;
819 if (contextualKeyword === _keywords.ContextualKeyword._global) {
820 tsParseAmbientExternalModuleDeclaration();
821 matched = true;
822 } else {
823 matched = tsParseDeclaration(contextualKeyword, /* isBeforeToken */ true);
824 }
825 _index.popTypeContext.call(void 0, oldIsType);
826 return matched;
827 }
828 default:
829 return false;
830 }
831}
832
833// Note: this won't be called unless the keyword is allowed in `shouldParseExportDeclaration`.
834// Returns true if it matched a declaration.
835function tsTryParseExportDeclaration() {
836 return tsParseDeclaration(_base.state.contextualKeyword, /* isBeforeToken */ true);
837}
838
839// Returns true if it matched a statement.
840function tsParseExpressionStatement(contextualKeyword) {
841 switch (contextualKeyword) {
842 case _keywords.ContextualKeyword._declare: {
843 const declareTokenIndex = _base.state.tokens.length - 1;
844 const matched = tsTryParseDeclare();
845 if (matched) {
846 _base.state.tokens[declareTokenIndex].type = _types.TokenType._declare;
847 return true;
848 }
849 break;
850 }
851 case _keywords.ContextualKeyword._global:
852 // `global { }` (with no `declare`) may appear inside an ambient module declaration.
853 // Would like to use tsParseAmbientExternalModuleDeclaration here, but already ran past "global".
854 if (_index.match.call(void 0, _types.TokenType.braceL)) {
855 tsParseModuleBlock();
856 return true;
857 }
858 break;
859
860 default:
861 return tsParseDeclaration(contextualKeyword, /* isBeforeToken */ false);
862 }
863 return false;
864}
865
866// Common to tsTryParseDeclare, tsTryParseExportDeclaration, and tsParseExpressionStatement.
867// Returns true if it matched a declaration.
868function tsParseDeclaration(contextualKeyword, isBeforeToken) {
869 switch (contextualKeyword) {
870 case _keywords.ContextualKeyword._abstract:
871 if (isBeforeToken || _index.match.call(void 0, _types.TokenType._class)) {
872 if (isBeforeToken) _index.next.call(void 0, );
873 _base.state.tokens[_base.state.tokens.length - 1].type = _types.TokenType._abstract;
874 _statement.parseClass.call(void 0, /* isStatement */ true, /* optionalId */ false);
875 return true;
876 }
877 break;
878
879 case _keywords.ContextualKeyword._enum:
880 if (isBeforeToken || _index.match.call(void 0, _types.TokenType.name)) {
881 if (isBeforeToken) _index.next.call(void 0, );
882 _base.state.tokens[_base.state.tokens.length - 1].type = _types.TokenType._enum;
883 tsParseEnumDeclaration();
884 return true;
885 }
886 break;
887
888 case _keywords.ContextualKeyword._interface:
889 if (isBeforeToken || _index.match.call(void 0, _types.TokenType.name)) {
890 // `next` is true in "export" and "declare" contexts, so we want to remove that token
891 // as well.
892 const oldIsType = _index.pushTypeContext.call(void 0, 1);
893 if (isBeforeToken) _index.next.call(void 0, );
894 tsParseInterfaceDeclaration();
895 _index.popTypeContext.call(void 0, oldIsType);
896 return true;
897 }
898 break;
899
900 case _keywords.ContextualKeyword._module:
901 if (isBeforeToken) _index.next.call(void 0, );
902 if (_index.match.call(void 0, _types.TokenType.string)) {
903 const oldIsType = _index.pushTypeContext.call(void 0, isBeforeToken ? 2 : 1);
904 tsParseAmbientExternalModuleDeclaration();
905 _index.popTypeContext.call(void 0, oldIsType);
906 return true;
907 } else if (_index.match.call(void 0, _types.TokenType.name)) {
908 const oldIsType = _index.pushTypeContext.call(void 0, isBeforeToken ? 2 : 1);
909 tsParseModuleOrNamespaceDeclaration();
910 _index.popTypeContext.call(void 0, oldIsType);
911 return true;
912 }
913 break;
914
915 case _keywords.ContextualKeyword._namespace:
916 if (isBeforeToken || _index.match.call(void 0, _types.TokenType.name)) {
917 const oldIsType = _index.pushTypeContext.call(void 0, 1);
918 if (isBeforeToken) _index.next.call(void 0, );
919 tsParseModuleOrNamespaceDeclaration();
920 _index.popTypeContext.call(void 0, oldIsType);
921 return true;
922 }
923 break;
924
925 case _keywords.ContextualKeyword._type:
926 if (isBeforeToken || _index.match.call(void 0, _types.TokenType.name)) {
927 const oldIsType = _index.pushTypeContext.call(void 0, 1);
928 if (isBeforeToken) _index.next.call(void 0, );
929 tsParseTypeAliasDeclaration();
930 _index.popTypeContext.call(void 0, oldIsType);
931 return true;
932 }
933 break;
934
935 default:
936 break;
937 }
938 return false;
939}
940
941// Returns true if there was a generic async arrow function.
942function tsTryParseGenericAsyncArrowFunction() {
943 const snapshot = _base.state.snapshot();
944
945 tsParseTypeParameters();
946 _statement.parseFunctionParams.call(void 0, );
947 tsTryParseTypeOrTypePredicateAnnotation();
948 _util.expect.call(void 0, _types.TokenType.arrow);
949
950 if (_base.state.error) {
951 _base.state.restoreFromSnapshot(snapshot);
952 return false;
953 }
954
955 // We don't need to be precise about the function start since it's only used if this is a
956 // bodiless function, which isn't valid here.
957 const functionStart = _base.state.start;
958 _expression.parseFunctionBody.call(void 0, functionStart, false /* isGenerator */, true);
959 return true;
960}
961
962function tsParseTypeArguments() {
963 const oldIsType = _index.pushTypeContext.call(void 0, 0);
964 _util.expect.call(void 0, _types.TokenType.lessThan);
965 while (!_index.eat.call(void 0, _types.TokenType.greaterThan) && !_base.state.error) {
966 tsParseType();
967 _index.eat.call(void 0, _types.TokenType.comma);
968 }
969 _index.popTypeContext.call(void 0, oldIsType);
970}
971
972 function tsIsDeclarationStart() {
973 if (_index.match.call(void 0, _types.TokenType.name)) {
974 switch (_base.state.contextualKeyword) {
975 case _keywords.ContextualKeyword._abstract:
976 case _keywords.ContextualKeyword._declare:
977 case _keywords.ContextualKeyword._enum:
978 case _keywords.ContextualKeyword._interface:
979 case _keywords.ContextualKeyword._module:
980 case _keywords.ContextualKeyword._namespace:
981 case _keywords.ContextualKeyword._type:
982 return true;
983 default:
984 break;
985 }
986 }
987
988 return false;
989} exports.tsIsDeclarationStart = tsIsDeclarationStart;
990
991// ======================================================
992// OVERRIDES
993// ======================================================
994
995 function tsParseFunctionBodyAndFinish(
996 functionStart,
997 isGenerator,
998 allowExpressionBody = false,
999 funcContextId,
1000) {
1001 // For arrow functions, `parseArrow` handles the return type itself.
1002 if (!allowExpressionBody && _index.match.call(void 0, _types.TokenType.colon)) {
1003 tsParseTypeOrTypePredicateAnnotation(_types.TokenType.colon);
1004 }
1005
1006 // The original code checked the node type to make sure this function type allows a missing
1007 // body, but we skip that to avoid sending around the node type. We instead just use the
1008 // allowExpressionBody boolean to make sure it's not an arrow function.
1009 if (!allowExpressionBody && !_index.match.call(void 0, _types.TokenType.braceL) && _util.isLineTerminator.call(void 0, )) {
1010 // Retroactively mark the function declaration as a type.
1011 let i = _base.state.tokens.length - 1;
1012 while (
1013 i >= 0 &&
1014 (_base.state.tokens[i].start >= functionStart ||
1015 _base.state.tokens[i].type === _types.TokenType._default ||
1016 _base.state.tokens[i].type === _types.TokenType._export)
1017 ) {
1018 _base.state.tokens[i].isType = true;
1019 i--;
1020 }
1021 return;
1022 }
1023
1024 _expression.parseFunctionBody.call(void 0, functionStart, isGenerator, allowExpressionBody, funcContextId);
1025} exports.tsParseFunctionBodyAndFinish = tsParseFunctionBodyAndFinish;
1026
1027 function tsParseSubscript(startPos, noCalls, stopState) {
1028 if (!_util.hasPrecedingLineBreak.call(void 0, ) && _index.eat.call(void 0, _types.TokenType.bang)) {
1029 _base.state.tokens[_base.state.tokens.length - 1].type = _types.TokenType.nonNullAssertion;
1030 return;
1031 }
1032
1033 if (_index.match.call(void 0, _types.TokenType.lessThan)) {
1034 // There are number of things we are going to "maybe" parse, like type arguments on
1035 // tagged template expressions. If any of them fail, walk it back and continue.
1036 const snapshot = _base.state.snapshot();
1037
1038 if (!noCalls && _expression.atPossibleAsync.call(void 0, )) {
1039 // Almost certainly this is a generic async function `async <T>() => ...
1040 // But it might be a call with a type argument `async<T>();`
1041 const asyncArrowFn = tsTryParseGenericAsyncArrowFunction();
1042 if (asyncArrowFn) {
1043 return;
1044 }
1045 }
1046 tsParseTypeArguments();
1047 if (!noCalls && _index.eat.call(void 0, _types.TokenType.parenL)) {
1048 _expression.parseCallExpressionArguments.call(void 0, );
1049 } else if (_index.match.call(void 0, _types.TokenType.backQuote)) {
1050 // Tagged template with a type argument.
1051 _expression.parseTemplate.call(void 0, );
1052 }
1053
1054 if (_base.state.error) {
1055 _base.state.restoreFromSnapshot(snapshot);
1056 } else {
1057 return;
1058 }
1059 }
1060 _expression.baseParseSubscript.call(void 0, startPos, noCalls, stopState);
1061} exports.tsParseSubscript = tsParseSubscript;
1062
1063 function tsStartParseNewArguments() {
1064 if (_index.match.call(void 0, _types.TokenType.lessThan)) {
1065 // 99% certain this is `new C<T>();`. But may be `new C < T;`, which is also legal.
1066 const snapshot = _base.state.snapshot();
1067
1068 _base.state.type = _types.TokenType.typeParameterStart;
1069 tsParseTypeArguments();
1070 if (!_index.match.call(void 0, _types.TokenType.parenL)) {
1071 _util.unexpected.call(void 0, );
1072 }
1073
1074 if (_base.state.error) {
1075 _base.state.restoreFromSnapshot(snapshot);
1076 }
1077 }
1078} exports.tsStartParseNewArguments = tsStartParseNewArguments;
1079
1080 function tsTryParseExport() {
1081 if (_index.match.call(void 0, _types.TokenType._import)) {
1082 // `export import A = B;`
1083 _util.expect.call(void 0, _types.TokenType._import);
1084 tsParseImportEqualsDeclaration();
1085 return true;
1086 } else if (_index.eat.call(void 0, _types.TokenType.eq)) {
1087 // `export = x;`
1088 _expression.parseExpression.call(void 0, );
1089 _util.semicolon.call(void 0, );
1090 return true;
1091 } else if (_util.eatContextual.call(void 0, _keywords.ContextualKeyword._as)) {
1092 // `export as namespace A;`
1093 // See `parseNamespaceExportDeclaration` in TypeScript's own parser
1094 _util.expectContextual.call(void 0, _keywords.ContextualKeyword._namespace);
1095 _expression.parseIdentifier.call(void 0, );
1096 _util.semicolon.call(void 0, );
1097 return true;
1098 } else {
1099 return false;
1100 }
1101} exports.tsTryParseExport = tsTryParseExport;
1102
1103 function tsTryParseExportDefaultExpression() {
1104 if (_util.isContextual.call(void 0, _keywords.ContextualKeyword._abstract) && _index.lookaheadType.call(void 0, ) === _types.TokenType._class) {
1105 _base.state.type = _types.TokenType._abstract;
1106 _index.next.call(void 0, ); // Skip "abstract"
1107 _statement.parseClass.call(void 0, true, true);
1108 return true;
1109 }
1110 if (_util.isContextual.call(void 0, _keywords.ContextualKeyword._interface)) {
1111 // Make sure "export default" are considered type tokens so the whole thing is removed.
1112 const oldIsType = _index.pushTypeContext.call(void 0, 2);
1113 tsParseDeclaration(_keywords.ContextualKeyword._interface, true);
1114 _index.popTypeContext.call(void 0, oldIsType);
1115 return true;
1116 }
1117 return false;
1118} exports.tsTryParseExportDefaultExpression = tsTryParseExportDefaultExpression;
1119
1120 function tsTryParseStatementContent() {
1121 if (_base.state.type === _types.TokenType._const) {
1122 const ahead = _index.lookaheadTypeAndKeyword.call(void 0, );
1123 if (ahead.type === _types.TokenType.name && ahead.contextualKeyword === _keywords.ContextualKeyword._enum) {
1124 _util.expect.call(void 0, _types.TokenType._const);
1125 _util.expectContextual.call(void 0, _keywords.ContextualKeyword._enum);
1126 _base.state.tokens[_base.state.tokens.length - 1].type = _types.TokenType._enum;
1127 tsParseEnumDeclaration();
1128 return true;
1129 }
1130 }
1131 return false;
1132} exports.tsTryParseStatementContent = tsTryParseStatementContent;
1133
1134 function tsParseAccessModifier() {
1135 tsParseModifier([
1136 _keywords.ContextualKeyword._public,
1137 _keywords.ContextualKeyword._protected,
1138 _keywords.ContextualKeyword._private,
1139 ]);
1140} exports.tsParseAccessModifier = tsParseAccessModifier;
1141
1142 function tsTryParseClassMemberWithIsStatic(
1143 isStatic,
1144 classContextId,
1145) {
1146 let isAbstract = false;
1147 let isReadonly = false;
1148
1149 const mod = tsParseModifier([_keywords.ContextualKeyword._abstract, _keywords.ContextualKeyword._readonly]);
1150 switch (mod) {
1151 case _keywords.ContextualKeyword._readonly:
1152 isReadonly = true;
1153 isAbstract = !!tsParseModifier([_keywords.ContextualKeyword._abstract]);
1154 break;
1155 case _keywords.ContextualKeyword._abstract:
1156 isAbstract = true;
1157 isReadonly = !!tsParseModifier([_keywords.ContextualKeyword._readonly]);
1158 break;
1159 default:
1160 break;
1161 }
1162
1163 // We no longer check for public/private/etc, but tsTryParseIndexSignature should just return
1164 // false in that case for valid code.
1165 if (!isAbstract && !isStatic) {
1166 const found = tsTryParseIndexSignature();
1167 if (found) {
1168 return true;
1169 }
1170 }
1171
1172 if (isReadonly) {
1173 // Must be a property (if not an index signature).
1174 _statement.parseClassPropertyName.call(void 0, classContextId);
1175 _statement.parsePostMemberNameModifiers.call(void 0, );
1176 _statement.parseClassProperty.call(void 0, );
1177 return true;
1178 }
1179 return false;
1180} exports.tsTryParseClassMemberWithIsStatic = tsTryParseClassMemberWithIsStatic;
1181
1182// Note: The reason we do this in `parseIdentifierStatement` and not `parseStatement`
1183// is that e.g. `type()` is valid JS, so we must try parsing that first.
1184// If it's really a type, we will parse `type` as the statement, and can correct it here
1185// by parsing the rest.
1186 function tsParseIdentifierStatement(contextualKeyword) {
1187 const matched = tsParseExpressionStatement(contextualKeyword);
1188 if (!matched) {
1189 _util.semicolon.call(void 0, );
1190 }
1191} exports.tsParseIdentifierStatement = tsParseIdentifierStatement;
1192
1193 function tsParseExportDeclaration() {
1194 // "export declare" is equivalent to just "export".
1195 const isDeclare = _util.eatContextual.call(void 0, _keywords.ContextualKeyword._declare);
1196 if (isDeclare) {
1197 _base.state.tokens[_base.state.tokens.length - 1].type = _types.TokenType._declare;
1198 }
1199
1200 let matchedDeclaration = false;
1201 if (_index.match.call(void 0, _types.TokenType.name)) {
1202 if (isDeclare) {
1203 const oldIsType = _index.pushTypeContext.call(void 0, 2);
1204 matchedDeclaration = tsTryParseExportDeclaration();
1205 _index.popTypeContext.call(void 0, oldIsType);
1206 } else {
1207 matchedDeclaration = tsTryParseExportDeclaration();
1208 }
1209 }
1210 if (!matchedDeclaration) {
1211 if (isDeclare) {
1212 const oldIsType = _index.pushTypeContext.call(void 0, 2);
1213 _statement.parseStatement.call(void 0, true);
1214 _index.popTypeContext.call(void 0, oldIsType);
1215 } else {
1216 _statement.parseStatement.call(void 0, true);
1217 }
1218 }
1219} exports.tsParseExportDeclaration = tsParseExportDeclaration;
1220
1221 function tsAfterParseClassSuper(hasSuper) {
1222 if (hasSuper && _index.match.call(void 0, _types.TokenType.lessThan)) {
1223 tsParseTypeArguments();
1224 }
1225 if (_util.eatContextual.call(void 0, _keywords.ContextualKeyword._implements)) {
1226 _base.state.tokens[_base.state.tokens.length - 1].type = _types.TokenType._implements;
1227 const oldIsType = _index.pushTypeContext.call(void 0, 1);
1228 tsParseHeritageClause();
1229 _index.popTypeContext.call(void 0, oldIsType);
1230 }
1231} exports.tsAfterParseClassSuper = tsAfterParseClassSuper;
1232
1233 function tsStartParseObjPropValue() {
1234 tsTryParseTypeParameters();
1235} exports.tsStartParseObjPropValue = tsStartParseObjPropValue;
1236
1237 function tsStartParseFunctionParams() {
1238 tsTryParseTypeParameters();
1239} exports.tsStartParseFunctionParams = tsStartParseFunctionParams;
1240
1241// `let x: number;`
1242 function tsAfterParseVarHead() {
1243 const oldIsType = _index.pushTypeContext.call(void 0, 0);
1244 _index.eat.call(void 0, _types.TokenType.bang);
1245 tsTryParseTypeAnnotation();
1246 _index.popTypeContext.call(void 0, oldIsType);
1247} exports.tsAfterParseVarHead = tsAfterParseVarHead;
1248
1249// parse the return type of an async arrow function - let foo = (async (): number => {});
1250 function tsStartParseAsyncArrowFromCallExpression() {
1251 if (_index.match.call(void 0, _types.TokenType.colon)) {
1252 tsParseTypeAnnotation();
1253 }
1254} exports.tsStartParseAsyncArrowFromCallExpression = tsStartParseAsyncArrowFromCallExpression;
1255
1256// Returns true if the expression was an arrow function.
1257 function tsParseMaybeAssign(noIn, isWithinParens) {
1258 // Note: When the JSX plugin is on, type assertions (`<T> x`) aren't valid syntax.
1259 if (_base.isJSXEnabled) {
1260 return tsParseMaybeAssignWithJSX(noIn, isWithinParens);
1261 } else {
1262 return tsParseMaybeAssignWithoutJSX(noIn, isWithinParens);
1263 }
1264} exports.tsParseMaybeAssign = tsParseMaybeAssign;
1265
1266 function tsParseMaybeAssignWithJSX(noIn, isWithinParens) {
1267 if (!_index.match.call(void 0, _types.TokenType.lessThan)) {
1268 return _expression.baseParseMaybeAssign.call(void 0, noIn, isWithinParens);
1269 }
1270
1271 // Prefer to parse JSX if possible. But may be an arrow fn.
1272 const snapshot = _base.state.snapshot();
1273 let wasArrow = _expression.baseParseMaybeAssign.call(void 0, noIn, isWithinParens);
1274 if (_base.state.error) {
1275 _base.state.restoreFromSnapshot(snapshot);
1276 } else {
1277 return wasArrow;
1278 }
1279
1280 // Otherwise, try as type-parameterized arrow function.
1281 _base.state.type = _types.TokenType.typeParameterStart;
1282 // This is similar to TypeScript's `tryParseParenthesizedArrowFunctionExpression`.
1283 tsParseTypeParameters();
1284 wasArrow = _expression.baseParseMaybeAssign.call(void 0, noIn, isWithinParens);
1285 if (!wasArrow) {
1286 _util.unexpected.call(void 0, );
1287 }
1288
1289 return wasArrow;
1290} exports.tsParseMaybeAssignWithJSX = tsParseMaybeAssignWithJSX;
1291
1292 function tsParseMaybeAssignWithoutJSX(noIn, isWithinParens) {
1293 if (!_index.match.call(void 0, _types.TokenType.lessThan)) {
1294 return _expression.baseParseMaybeAssign.call(void 0, noIn, isWithinParens);
1295 }
1296
1297 const snapshot = _base.state.snapshot();
1298 // This is similar to TypeScript's `tryParseParenthesizedArrowFunctionExpression`.
1299 tsParseTypeParameters();
1300 const wasArrow = _expression.baseParseMaybeAssign.call(void 0, noIn, isWithinParens);
1301 if (!wasArrow) {
1302 _util.unexpected.call(void 0, );
1303 }
1304 if (_base.state.error) {
1305 _base.state.restoreFromSnapshot(snapshot);
1306 } else {
1307 return wasArrow;
1308 }
1309
1310 // Try parsing a type cast instead of an arrow function.
1311 // This will start with a type assertion (via parseMaybeUnary).
1312 // But don't directly call `tsParseTypeAssertion` because we want to handle any binary after it.
1313 return _expression.baseParseMaybeAssign.call(void 0, noIn, isWithinParens);
1314} exports.tsParseMaybeAssignWithoutJSX = tsParseMaybeAssignWithoutJSX;
1315
1316 function tsParseArrow() {
1317 if (_index.match.call(void 0, _types.TokenType.colon)) {
1318 // This is different from how the TS parser does it.
1319 // TS uses lookahead. Babylon parses it as a parenthesized expression and converts.
1320 const snapshot = _base.state.snapshot();
1321
1322 tsParseTypeOrTypePredicateAnnotation(_types.TokenType.colon);
1323 if (_util.canInsertSemicolon.call(void 0, )) _util.unexpected.call(void 0, );
1324 if (!_index.match.call(void 0, _types.TokenType.arrow)) _util.unexpected.call(void 0, );
1325
1326 if (_base.state.error) {
1327 _base.state.restoreFromSnapshot(snapshot);
1328 }
1329 }
1330 return _index.eat.call(void 0, _types.TokenType.arrow);
1331} exports.tsParseArrow = tsParseArrow;
1332
1333// Allow type annotations inside of a parameter list.
1334 function tsParseAssignableListItemTypes() {
1335 const oldIsType = _index.pushTypeContext.call(void 0, 0);
1336 _index.eat.call(void 0, _types.TokenType.question);
1337 tsTryParseTypeAnnotation();
1338 _index.popTypeContext.call(void 0, oldIsType);
1339} exports.tsParseAssignableListItemTypes = tsParseAssignableListItemTypes;
1340
1341 function tsParseMaybeDecoratorArguments() {
1342 if (_index.match.call(void 0, _types.TokenType.lessThan)) {
1343 tsParseTypeArguments();
1344 }
1345 _statement.baseParseMaybeDecoratorArguments.call(void 0, );
1346} exports.tsParseMaybeDecoratorArguments = tsParseMaybeDecoratorArguments;