UNPKG

36.6 kBJavaScriptView Raw
1"use strict";Object.defineProperty(exports, "__esModule", {value: true});/* eslint max-len: 0 */
2
3
4
5
6
7
8
9
10
11
12var _index = require('../tokenizer/index');
13var _keywords = require('../tokenizer/keywords');
14var _types = require('../tokenizer/types');
15var _base = require('../traverser/base');
16
17
18
19
20
21
22
23
24
25
26
27
28
29var _expression = require('../traverser/expression');
30
31
32
33
34
35
36
37
38var _statement = require('../traverser/statement');
39
40
41
42
43
44
45
46
47
48var _util = require('../traverser/util');
49
50function isMaybeDefaultImport(lookahead) {
51 return (
52 (lookahead.type === _types.TokenType.name || !!(lookahead.type & _types.TokenType.IS_KEYWORD)) &&
53 lookahead.contextualKeyword !== _keywords.ContextualKeyword._from
54 );
55}
56
57function flowParseTypeInitialiser(tok) {
58 const oldIsType = _index.pushTypeContext.call(void 0, 0);
59 _util.expect.call(void 0, tok || _types.TokenType.colon);
60 flowParseType();
61 _index.popTypeContext.call(void 0, oldIsType);
62}
63
64function flowParsePredicate() {
65 _util.expect.call(void 0, _types.TokenType.modulo);
66 _util.expectContextual.call(void 0, _keywords.ContextualKeyword._checks);
67 if (_index.eat.call(void 0, _types.TokenType.parenL)) {
68 _expression.parseExpression.call(void 0, );
69 _util.expect.call(void 0, _types.TokenType.parenR);
70 }
71}
72
73function flowParseTypeAndPredicateInitialiser() {
74 const oldIsType = _index.pushTypeContext.call(void 0, 0);
75 _util.expect.call(void 0, _types.TokenType.colon);
76 if (_index.match.call(void 0, _types.TokenType.modulo)) {
77 flowParsePredicate();
78 } else {
79 flowParseType();
80 if (_index.match.call(void 0, _types.TokenType.modulo)) {
81 flowParsePredicate();
82 }
83 }
84 _index.popTypeContext.call(void 0, oldIsType);
85}
86
87function flowParseDeclareClass() {
88 _index.next.call(void 0, );
89 flowParseInterfaceish(/* isClass */ true);
90}
91
92function flowParseDeclareFunction() {
93 _index.next.call(void 0, );
94 _expression.parseIdentifier.call(void 0, );
95
96 if (_index.match.call(void 0, _types.TokenType.lessThan)) {
97 flowParseTypeParameterDeclaration();
98 }
99
100 _util.expect.call(void 0, _types.TokenType.parenL);
101 flowParseFunctionTypeParams();
102 _util.expect.call(void 0, _types.TokenType.parenR);
103
104 flowParseTypeAndPredicateInitialiser();
105
106 _util.semicolon.call(void 0, );
107}
108
109function flowParseDeclare() {
110 if (_index.match.call(void 0, _types.TokenType._class)) {
111 flowParseDeclareClass();
112 } else if (_index.match.call(void 0, _types.TokenType._function)) {
113 flowParseDeclareFunction();
114 } else if (_index.match.call(void 0, _types.TokenType._var)) {
115 flowParseDeclareVariable();
116 } else if (_util.isContextual.call(void 0, _keywords.ContextualKeyword._module)) {
117 if (_index.lookaheadType.call(void 0, ) === _types.TokenType.dot) {
118 flowParseDeclareModuleExports();
119 } else {
120 flowParseDeclareModule();
121 }
122 } else if (_util.isContextual.call(void 0, _keywords.ContextualKeyword._type)) {
123 flowParseDeclareTypeAlias();
124 } else if (_util.isContextual.call(void 0, _keywords.ContextualKeyword._opaque)) {
125 flowParseDeclareOpaqueType();
126 } else if (_util.isContextual.call(void 0, _keywords.ContextualKeyword._interface)) {
127 flowParseDeclareInterface();
128 } else if (_index.match.call(void 0, _types.TokenType._export)) {
129 flowParseDeclareExportDeclaration();
130 } else {
131 _util.unexpected.call(void 0, );
132 }
133}
134
135function flowParseDeclareVariable() {
136 _index.next.call(void 0, );
137 flowParseTypeAnnotatableIdentifier();
138 _util.semicolon.call(void 0, );
139}
140
141function flowParseDeclareModule() {
142 _index.next.call(void 0, );
143
144 if (_index.match.call(void 0, _types.TokenType.string)) {
145 _expression.parseExprAtom.call(void 0, );
146 } else {
147 _expression.parseIdentifier.call(void 0, );
148 }
149
150 _util.expect.call(void 0, _types.TokenType.braceL);
151 while (!_index.match.call(void 0, _types.TokenType.braceR) && !_base.state.error) {
152 if (_index.match.call(void 0, _types.TokenType._import)) {
153 _index.next.call(void 0, );
154 _statement.parseImport.call(void 0, );
155 } else {
156 _util.unexpected.call(void 0, );
157 }
158 }
159 _util.expect.call(void 0, _types.TokenType.braceR);
160}
161
162function flowParseDeclareExportDeclaration() {
163 _util.expect.call(void 0, _types.TokenType._export);
164
165 if (_index.eat.call(void 0, _types.TokenType._default)) {
166 if (_index.match.call(void 0, _types.TokenType._function) || _index.match.call(void 0, _types.TokenType._class)) {
167 // declare export default class ...
168 // declare export default function ...
169 flowParseDeclare();
170 } else {
171 // declare export default [type];
172 flowParseType();
173 _util.semicolon.call(void 0, );
174 }
175 } else if (
176 _index.match.call(void 0, _types.TokenType._var) || // declare export var ...
177 _index.match.call(void 0, _types.TokenType._function) || // declare export function ...
178 _index.match.call(void 0, _types.TokenType._class) || // declare export class ...
179 _util.isContextual.call(void 0, _keywords.ContextualKeyword._opaque) // declare export opaque ..
180 ) {
181 flowParseDeclare();
182 } else if (
183 _index.match.call(void 0, _types.TokenType.star) || // declare export * from ''
184 _index.match.call(void 0, _types.TokenType.braceL) || // declare export {} ...
185 _util.isContextual.call(void 0, _keywords.ContextualKeyword._interface) || // declare export interface ...
186 _util.isContextual.call(void 0, _keywords.ContextualKeyword._type) || // declare export type ...
187 _util.isContextual.call(void 0, _keywords.ContextualKeyword._opaque) // declare export opaque type ...
188 ) {
189 _statement.parseExport.call(void 0, );
190 } else {
191 _util.unexpected.call(void 0, );
192 }
193}
194
195function flowParseDeclareModuleExports() {
196 _util.expectContextual.call(void 0, _keywords.ContextualKeyword._module);
197 _util.expect.call(void 0, _types.TokenType.dot);
198 _util.expectContextual.call(void 0, _keywords.ContextualKeyword._exports);
199 flowParseTypeAnnotation();
200 _util.semicolon.call(void 0, );
201}
202
203function flowParseDeclareTypeAlias() {
204 _index.next.call(void 0, );
205 flowParseTypeAlias();
206}
207
208function flowParseDeclareOpaqueType() {
209 _index.next.call(void 0, );
210 flowParseOpaqueType(true);
211}
212
213function flowParseDeclareInterface() {
214 _index.next.call(void 0, );
215 flowParseInterfaceish();
216}
217
218// Interfaces
219
220function flowParseInterfaceish(isClass = false) {
221 flowParseRestrictedIdentifier();
222
223 if (_index.match.call(void 0, _types.TokenType.lessThan)) {
224 flowParseTypeParameterDeclaration();
225 }
226
227 if (_index.eat.call(void 0, _types.TokenType._extends)) {
228 do {
229 flowParseInterfaceExtends();
230 } while (!isClass && _index.eat.call(void 0, _types.TokenType.comma));
231 }
232
233 if (_util.isContextual.call(void 0, _keywords.ContextualKeyword._mixins)) {
234 _index.next.call(void 0, );
235 do {
236 flowParseInterfaceExtends();
237 } while (_index.eat.call(void 0, _types.TokenType.comma));
238 }
239
240 if (_util.isContextual.call(void 0, _keywords.ContextualKeyword._implements)) {
241 _index.next.call(void 0, );
242 do {
243 flowParseInterfaceExtends();
244 } while (_index.eat.call(void 0, _types.TokenType.comma));
245 }
246
247 flowParseObjectType(isClass, false, isClass);
248}
249
250function flowParseInterfaceExtends() {
251 flowParseQualifiedTypeIdentifier(false);
252 if (_index.match.call(void 0, _types.TokenType.lessThan)) {
253 flowParseTypeParameterInstantiation();
254 }
255}
256
257function flowParseInterface() {
258 flowParseInterfaceish();
259}
260
261function flowParseRestrictedIdentifier() {
262 _expression.parseIdentifier.call(void 0, );
263}
264
265function flowParseTypeAlias() {
266 flowParseRestrictedIdentifier();
267
268 if (_index.match.call(void 0, _types.TokenType.lessThan)) {
269 flowParseTypeParameterDeclaration();
270 }
271
272 flowParseTypeInitialiser(_types.TokenType.eq);
273 _util.semicolon.call(void 0, );
274}
275
276function flowParseOpaqueType(declare) {
277 _util.expectContextual.call(void 0, _keywords.ContextualKeyword._type);
278 flowParseRestrictedIdentifier();
279
280 if (_index.match.call(void 0, _types.TokenType.lessThan)) {
281 flowParseTypeParameterDeclaration();
282 }
283
284 // Parse the supertype
285 if (_index.match.call(void 0, _types.TokenType.colon)) {
286 flowParseTypeInitialiser(_types.TokenType.colon);
287 }
288
289 if (!declare) {
290 flowParseTypeInitialiser(_types.TokenType.eq);
291 }
292 _util.semicolon.call(void 0, );
293}
294
295function flowParseTypeParameter() {
296 flowParseVariance();
297 flowParseTypeAnnotatableIdentifier();
298
299 if (_index.eat.call(void 0, _types.TokenType.eq)) {
300 flowParseType();
301 }
302}
303
304 function flowParseTypeParameterDeclaration() {
305 const oldIsType = _index.pushTypeContext.call(void 0, 0);
306 // istanbul ignore else: this condition is already checked at all call sites
307 if (_index.match.call(void 0, _types.TokenType.lessThan) || _index.match.call(void 0, _types.TokenType.typeParameterStart)) {
308 _index.next.call(void 0, );
309 } else {
310 _util.unexpected.call(void 0, );
311 }
312
313 do {
314 flowParseTypeParameter();
315 if (!_index.match.call(void 0, _types.TokenType.greaterThan)) {
316 _util.expect.call(void 0, _types.TokenType.comma);
317 }
318 } while (!_index.match.call(void 0, _types.TokenType.greaterThan) && !_base.state.error);
319 _util.expect.call(void 0, _types.TokenType.greaterThan);
320 _index.popTypeContext.call(void 0, oldIsType);
321} exports.flowParseTypeParameterDeclaration = flowParseTypeParameterDeclaration;
322
323function flowParseTypeParameterInstantiation() {
324 const oldIsType = _index.pushTypeContext.call(void 0, 0);
325 _util.expect.call(void 0, _types.TokenType.lessThan);
326 while (!_index.match.call(void 0, _types.TokenType.greaterThan) && !_base.state.error) {
327 flowParseType();
328 if (!_index.match.call(void 0, _types.TokenType.greaterThan)) {
329 _util.expect.call(void 0, _types.TokenType.comma);
330 }
331 }
332 _util.expect.call(void 0, _types.TokenType.greaterThan);
333 _index.popTypeContext.call(void 0, oldIsType);
334}
335
336function flowParseInterfaceType() {
337 _util.expectContextual.call(void 0, _keywords.ContextualKeyword._interface);
338 if (_index.eat.call(void 0, _types.TokenType._extends)) {
339 do {
340 flowParseInterfaceExtends();
341 } while (_index.eat.call(void 0, _types.TokenType.comma));
342 }
343 flowParseObjectType(false, false, false);
344}
345
346function flowParseObjectPropertyKey() {
347 if (_index.match.call(void 0, _types.TokenType.num) || _index.match.call(void 0, _types.TokenType.string)) {
348 _expression.parseExprAtom.call(void 0, );
349 } else {
350 _expression.parseIdentifier.call(void 0, );
351 }
352}
353
354function flowParseObjectTypeIndexer() {
355 // Note: bracketL has already been consumed
356 if (_index.lookaheadType.call(void 0, ) === _types.TokenType.colon) {
357 flowParseObjectPropertyKey();
358 flowParseTypeInitialiser();
359 } else {
360 flowParseType();
361 }
362 _util.expect.call(void 0, _types.TokenType.bracketR);
363 flowParseTypeInitialiser();
364}
365
366function flowParseObjectTypeInternalSlot() {
367 // Note: both bracketL have already been consumed
368 flowParseObjectPropertyKey();
369 _util.expect.call(void 0, _types.TokenType.bracketR);
370 _util.expect.call(void 0, _types.TokenType.bracketR);
371 if (_index.match.call(void 0, _types.TokenType.lessThan) || _index.match.call(void 0, _types.TokenType.parenL)) {
372 flowParseObjectTypeMethodish();
373 } else {
374 _index.eat.call(void 0, _types.TokenType.question);
375 flowParseTypeInitialiser();
376 }
377}
378
379function flowParseObjectTypeMethodish() {
380 if (_index.match.call(void 0, _types.TokenType.lessThan)) {
381 flowParseTypeParameterDeclaration();
382 }
383
384 _util.expect.call(void 0, _types.TokenType.parenL);
385 while (!_index.match.call(void 0, _types.TokenType.parenR) && !_index.match.call(void 0, _types.TokenType.ellipsis) && !_base.state.error) {
386 flowParseFunctionTypeParam();
387 if (!_index.match.call(void 0, _types.TokenType.parenR)) {
388 _util.expect.call(void 0, _types.TokenType.comma);
389 }
390 }
391
392 if (_index.eat.call(void 0, _types.TokenType.ellipsis)) {
393 flowParseFunctionTypeParam();
394 }
395 _util.expect.call(void 0, _types.TokenType.parenR);
396 flowParseTypeInitialiser();
397}
398
399function flowParseObjectTypeCallProperty() {
400 flowParseObjectTypeMethodish();
401}
402
403function flowParseObjectType(allowStatic, allowExact, allowProto) {
404 let endDelim;
405 if (allowExact && _index.match.call(void 0, _types.TokenType.braceBarL)) {
406 _util.expect.call(void 0, _types.TokenType.braceBarL);
407 endDelim = _types.TokenType.braceBarR;
408 } else {
409 _util.expect.call(void 0, _types.TokenType.braceL);
410 endDelim = _types.TokenType.braceR;
411 }
412
413 while (!_index.match.call(void 0, endDelim) && !_base.state.error) {
414 if (allowProto && _util.isContextual.call(void 0, _keywords.ContextualKeyword._proto)) {
415 const lookahead = _index.lookaheadType.call(void 0, );
416 if (lookahead !== _types.TokenType.colon && lookahead !== _types.TokenType.question) {
417 _index.next.call(void 0, );
418 allowStatic = false;
419 }
420 }
421 if (allowStatic && _util.isContextual.call(void 0, _keywords.ContextualKeyword._static)) {
422 const lookahead = _index.lookaheadType.call(void 0, );
423 if (lookahead !== _types.TokenType.colon && lookahead !== _types.TokenType.question) {
424 _index.next.call(void 0, );
425 }
426 }
427
428 flowParseVariance();
429
430 if (_index.eat.call(void 0, _types.TokenType.bracketL)) {
431 if (_index.eat.call(void 0, _types.TokenType.bracketL)) {
432 flowParseObjectTypeInternalSlot();
433 } else {
434 flowParseObjectTypeIndexer();
435 }
436 } else if (_index.match.call(void 0, _types.TokenType.parenL) || _index.match.call(void 0, _types.TokenType.lessThan)) {
437 flowParseObjectTypeCallProperty();
438 } else {
439 if (_util.isContextual.call(void 0, _keywords.ContextualKeyword._get) || _util.isContextual.call(void 0, _keywords.ContextualKeyword._set)) {
440 const lookahead = _index.lookaheadType.call(void 0, );
441 if (lookahead === _types.TokenType.name || lookahead === _types.TokenType.string || lookahead === _types.TokenType.num) {
442 _index.next.call(void 0, );
443 }
444 }
445
446 flowParseObjectTypeProperty();
447 }
448
449 flowObjectTypeSemicolon();
450 }
451
452 _util.expect.call(void 0, endDelim);
453}
454
455function flowParseObjectTypeProperty() {
456 if (_index.match.call(void 0, _types.TokenType.ellipsis)) {
457 _util.expect.call(void 0, _types.TokenType.ellipsis);
458 if (!_index.eat.call(void 0, _types.TokenType.comma)) {
459 _index.eat.call(void 0, _types.TokenType.semi);
460 }
461 // Explicit inexact object syntax.
462 if (_index.match.call(void 0, _types.TokenType.braceR)) {
463 return;
464 }
465 flowParseType();
466 } else {
467 flowParseObjectPropertyKey();
468 if (_index.match.call(void 0, _types.TokenType.lessThan) || _index.match.call(void 0, _types.TokenType.parenL)) {
469 // This is a method property
470 flowParseObjectTypeMethodish();
471 } else {
472 _index.eat.call(void 0, _types.TokenType.question);
473 flowParseTypeInitialiser();
474 }
475 }
476}
477
478function flowObjectTypeSemicolon() {
479 if (!_index.eat.call(void 0, _types.TokenType.semi) && !_index.eat.call(void 0, _types.TokenType.comma) && !_index.match.call(void 0, _types.TokenType.braceR) && !_index.match.call(void 0, _types.TokenType.braceBarR)) {
480 _util.unexpected.call(void 0, );
481 }
482}
483
484function flowParseQualifiedTypeIdentifier(initialIdAlreadyParsed) {
485 if (!initialIdAlreadyParsed) {
486 _expression.parseIdentifier.call(void 0, );
487 }
488 while (_index.eat.call(void 0, _types.TokenType.dot)) {
489 _expression.parseIdentifier.call(void 0, );
490 }
491}
492
493function flowParseGenericType() {
494 flowParseQualifiedTypeIdentifier(true);
495 if (_index.match.call(void 0, _types.TokenType.lessThan)) {
496 flowParseTypeParameterInstantiation();
497 }
498}
499
500function flowParseTypeofType() {
501 _util.expect.call(void 0, _types.TokenType._typeof);
502 flowParsePrimaryType();
503}
504
505function flowParseTupleType() {
506 _util.expect.call(void 0, _types.TokenType.bracketL);
507 // We allow trailing commas
508 while (_base.state.pos < _base.input.length && !_index.match.call(void 0, _types.TokenType.bracketR)) {
509 flowParseType();
510 if (_index.match.call(void 0, _types.TokenType.bracketR)) {
511 break;
512 }
513 _util.expect.call(void 0, _types.TokenType.comma);
514 }
515 _util.expect.call(void 0, _types.TokenType.bracketR);
516}
517
518function flowParseFunctionTypeParam() {
519 const lookahead = _index.lookaheadType.call(void 0, );
520 if (lookahead === _types.TokenType.colon || lookahead === _types.TokenType.question) {
521 _expression.parseIdentifier.call(void 0, );
522 _index.eat.call(void 0, _types.TokenType.question);
523 flowParseTypeInitialiser();
524 } else {
525 flowParseType();
526 }
527}
528
529function flowParseFunctionTypeParams() {
530 while (!_index.match.call(void 0, _types.TokenType.parenR) && !_index.match.call(void 0, _types.TokenType.ellipsis) && !_base.state.error) {
531 flowParseFunctionTypeParam();
532 if (!_index.match.call(void 0, _types.TokenType.parenR)) {
533 _util.expect.call(void 0, _types.TokenType.comma);
534 }
535 }
536 if (_index.eat.call(void 0, _types.TokenType.ellipsis)) {
537 flowParseFunctionTypeParam();
538 }
539}
540
541// The parsing of types roughly parallels the parsing of expressions, and
542// primary types are kind of like primary expressions...they're the
543// primitives with which other types are constructed.
544function flowParsePrimaryType() {
545 let isGroupedType = false;
546 const oldNoAnonFunctionType = _base.state.noAnonFunctionType;
547
548 switch (_base.state.type) {
549 case _types.TokenType.name: {
550 if (_util.isContextual.call(void 0, _keywords.ContextualKeyword._interface)) {
551 flowParseInterfaceType();
552 return;
553 }
554 _expression.parseIdentifier.call(void 0, );
555 flowParseGenericType();
556 return;
557 }
558
559 case _types.TokenType.braceL:
560 flowParseObjectType(false, false, false);
561 return;
562
563 case _types.TokenType.braceBarL:
564 flowParseObjectType(false, true, false);
565 return;
566
567 case _types.TokenType.bracketL:
568 flowParseTupleType();
569 return;
570
571 case _types.TokenType.lessThan:
572 flowParseTypeParameterDeclaration();
573 _util.expect.call(void 0, _types.TokenType.parenL);
574 flowParseFunctionTypeParams();
575 _util.expect.call(void 0, _types.TokenType.parenR);
576 _util.expect.call(void 0, _types.TokenType.arrow);
577 flowParseType();
578 return;
579
580 case _types.TokenType.parenL:
581 _index.next.call(void 0, );
582
583 // Check to see if this is actually a grouped type
584 if (!_index.match.call(void 0, _types.TokenType.parenR) && !_index.match.call(void 0, _types.TokenType.ellipsis)) {
585 if (_index.match.call(void 0, _types.TokenType.name)) {
586 const token = _index.lookaheadType.call(void 0, );
587 isGroupedType = token !== _types.TokenType.question && token !== _types.TokenType.colon;
588 } else {
589 isGroupedType = true;
590 }
591 }
592
593 if (isGroupedType) {
594 _base.state.noAnonFunctionType = false;
595 flowParseType();
596 _base.state.noAnonFunctionType = oldNoAnonFunctionType;
597
598 // A `,` or a `) =>` means this is an anonymous function type
599 if (
600 _base.state.noAnonFunctionType ||
601 !(_index.match.call(void 0, _types.TokenType.comma) || (_index.match.call(void 0, _types.TokenType.parenR) && _index.lookaheadType.call(void 0, ) === _types.TokenType.arrow))
602 ) {
603 _util.expect.call(void 0, _types.TokenType.parenR);
604 return;
605 } else {
606 // Eat a comma if there is one
607 _index.eat.call(void 0, _types.TokenType.comma);
608 }
609 }
610
611 flowParseFunctionTypeParams();
612
613 _util.expect.call(void 0, _types.TokenType.parenR);
614 _util.expect.call(void 0, _types.TokenType.arrow);
615 flowParseType();
616 return;
617
618 case _types.TokenType.minus:
619 _index.next.call(void 0, );
620 _expression.parseLiteral.call(void 0, );
621 return;
622
623 case _types.TokenType.string:
624 case _types.TokenType.num:
625 case _types.TokenType._true:
626 case _types.TokenType._false:
627 case _types.TokenType._null:
628 case _types.TokenType._this:
629 case _types.TokenType._void:
630 case _types.TokenType.star:
631 _index.next.call(void 0, );
632 return;
633
634 default:
635 if (_base.state.type === _types.TokenType._typeof) {
636 flowParseTypeofType();
637 return;
638 }
639 }
640
641 _util.unexpected.call(void 0, );
642}
643
644function flowParsePostfixType() {
645 flowParsePrimaryType();
646 while (!_util.canInsertSemicolon.call(void 0, ) && _index.match.call(void 0, _types.TokenType.bracketL)) {
647 _util.expect.call(void 0, _types.TokenType.bracketL);
648 _util.expect.call(void 0, _types.TokenType.bracketR);
649 }
650}
651
652function flowParsePrefixType() {
653 if (_index.eat.call(void 0, _types.TokenType.question)) {
654 flowParsePrefixType();
655 } else {
656 flowParsePostfixType();
657 }
658}
659
660function flowParseAnonFunctionWithoutParens() {
661 flowParsePrefixType();
662 if (!_base.state.noAnonFunctionType && _index.eat.call(void 0, _types.TokenType.arrow)) {
663 flowParseType();
664 }
665}
666
667function flowParseIntersectionType() {
668 _index.eat.call(void 0, _types.TokenType.bitwiseAND);
669 flowParseAnonFunctionWithoutParens();
670 while (_index.eat.call(void 0, _types.TokenType.bitwiseAND)) {
671 flowParseAnonFunctionWithoutParens();
672 }
673}
674
675function flowParseUnionType() {
676 _index.eat.call(void 0, _types.TokenType.bitwiseOR);
677 flowParseIntersectionType();
678 while (_index.eat.call(void 0, _types.TokenType.bitwiseOR)) {
679 flowParseIntersectionType();
680 }
681}
682
683function flowParseType() {
684 flowParseUnionType();
685}
686
687 function flowParseTypeAnnotation() {
688 flowParseTypeInitialiser();
689} exports.flowParseTypeAnnotation = flowParseTypeAnnotation;
690
691function flowParseTypeAnnotatableIdentifier() {
692 _expression.parseIdentifier.call(void 0, );
693 if (_index.match.call(void 0, _types.TokenType.colon)) {
694 flowParseTypeAnnotation();
695 }
696}
697
698 function flowParseVariance() {
699 if (_index.match.call(void 0, _types.TokenType.plus) || _index.match.call(void 0, _types.TokenType.minus)) {
700 _index.next.call(void 0, );
701 }
702} exports.flowParseVariance = flowParseVariance;
703
704// ==================================
705// Overrides
706// ==================================
707
708 function flowParseFunctionBodyAndFinish(
709 functionStart,
710 isGenerator,
711 allowExpressionBody = false,
712 funcContextId,
713) {
714 // For arrow functions, `parseArrow` handles the return type itself.
715 if (!allowExpressionBody && _index.match.call(void 0, _types.TokenType.colon)) {
716 flowParseTypeAndPredicateInitialiser();
717 }
718
719 _expression.parseFunctionBody.call(void 0, functionStart, isGenerator, allowExpressionBody, funcContextId);
720} exports.flowParseFunctionBodyAndFinish = flowParseFunctionBodyAndFinish;
721
722 function flowParseSubscript(startPos, noCalls, stopState) {
723 if (_index.match.call(void 0, _types.TokenType.questionDot) && _index.lookaheadType.call(void 0, ) === _types.TokenType.lessThan) {
724 if (noCalls) {
725 stopState.stop = true;
726 return;
727 }
728 _index.next.call(void 0, );
729 flowParseTypeParameterInstantiation();
730 _util.expect.call(void 0, _types.TokenType.parenL);
731 _expression.parseCallExpressionArguments.call(void 0, );
732 return;
733 } else if (!noCalls && _index.match.call(void 0, _types.TokenType.lessThan)) {
734 const snapshot = _base.state.snapshot();
735 flowParseTypeParameterInstantiation();
736 _util.expect.call(void 0, _types.TokenType.parenL);
737 _expression.parseCallExpressionArguments.call(void 0, );
738 if (_base.state.error) {
739 _base.state.restoreFromSnapshot(snapshot);
740 } else {
741 return;
742 }
743 }
744 _expression.baseParseSubscript.call(void 0, startPos, noCalls, stopState);
745} exports.flowParseSubscript = flowParseSubscript;
746
747 function flowStartParseNewArguments() {
748 if (_index.match.call(void 0, _types.TokenType.lessThan)) {
749 const snapshot = _base.state.snapshot();
750 flowParseTypeParameterInstantiation();
751 if (_base.state.error) {
752 _base.state.restoreFromSnapshot(snapshot);
753 }
754 }
755} exports.flowStartParseNewArguments = flowStartParseNewArguments;
756
757// interfaces
758 function flowTryParseStatement() {
759 if (_index.match.call(void 0, _types.TokenType.name) && _base.state.contextualKeyword === _keywords.ContextualKeyword._interface) {
760 const oldIsType = _index.pushTypeContext.call(void 0, 0);
761 _index.next.call(void 0, );
762 flowParseInterface();
763 _index.popTypeContext.call(void 0, oldIsType);
764 return true;
765 } else {
766 return false;
767 }
768} exports.flowTryParseStatement = flowTryParseStatement;
769
770// declares, interfaces and type aliases
771 function flowParseIdentifierStatement(contextualKeyword) {
772 if (contextualKeyword === _keywords.ContextualKeyword._declare) {
773 if (
774 _index.match.call(void 0, _types.TokenType._class) ||
775 _index.match.call(void 0, _types.TokenType.name) ||
776 _index.match.call(void 0, _types.TokenType._function) ||
777 _index.match.call(void 0, _types.TokenType._var) ||
778 _index.match.call(void 0, _types.TokenType._export)
779 ) {
780 const oldIsType = _index.pushTypeContext.call(void 0, 1);
781 flowParseDeclare();
782 _index.popTypeContext.call(void 0, oldIsType);
783 }
784 } else if (_index.match.call(void 0, _types.TokenType.name)) {
785 if (contextualKeyword === _keywords.ContextualKeyword._interface) {
786 const oldIsType = _index.pushTypeContext.call(void 0, 1);
787 flowParseInterface();
788 _index.popTypeContext.call(void 0, oldIsType);
789 } else if (contextualKeyword === _keywords.ContextualKeyword._type) {
790 const oldIsType = _index.pushTypeContext.call(void 0, 1);
791 flowParseTypeAlias();
792 _index.popTypeContext.call(void 0, oldIsType);
793 } else if (contextualKeyword === _keywords.ContextualKeyword._opaque) {
794 const oldIsType = _index.pushTypeContext.call(void 0, 1);
795 flowParseOpaqueType(false);
796 _index.popTypeContext.call(void 0, oldIsType);
797 }
798 }
799 _util.semicolon.call(void 0, );
800} exports.flowParseIdentifierStatement = flowParseIdentifierStatement;
801
802// export type
803 function flowShouldParseExportDeclaration() {
804 return (
805 _util.isContextual.call(void 0, _keywords.ContextualKeyword._type) ||
806 _util.isContextual.call(void 0, _keywords.ContextualKeyword._interface) ||
807 _util.isContextual.call(void 0, _keywords.ContextualKeyword._opaque)
808 );
809} exports.flowShouldParseExportDeclaration = flowShouldParseExportDeclaration;
810
811 function flowShouldDisallowExportDefaultSpecifier() {
812 return (
813 _index.match.call(void 0, _types.TokenType.name) &&
814 (_base.state.contextualKeyword === _keywords.ContextualKeyword._type ||
815 _base.state.contextualKeyword === _keywords.ContextualKeyword._interface ||
816 _base.state.contextualKeyword === _keywords.ContextualKeyword._opaque)
817 );
818} exports.flowShouldDisallowExportDefaultSpecifier = flowShouldDisallowExportDefaultSpecifier;
819
820 function flowParseExportDeclaration() {
821 if (_util.isContextual.call(void 0, _keywords.ContextualKeyword._type)) {
822 const oldIsType = _index.pushTypeContext.call(void 0, 1);
823 _index.next.call(void 0, );
824
825 if (_index.match.call(void 0, _types.TokenType.braceL)) {
826 // export type { foo, bar };
827 _statement.parseExportSpecifiers.call(void 0, );
828 _statement.parseExportFrom.call(void 0, );
829 } else {
830 // export type Foo = Bar;
831 flowParseTypeAlias();
832 }
833 _index.popTypeContext.call(void 0, oldIsType);
834 } else if (_util.isContextual.call(void 0, _keywords.ContextualKeyword._opaque)) {
835 const oldIsType = _index.pushTypeContext.call(void 0, 1);
836 _index.next.call(void 0, );
837 // export opaque type Foo = Bar;
838 flowParseOpaqueType(false);
839 _index.popTypeContext.call(void 0, oldIsType);
840 } else if (_util.isContextual.call(void 0, _keywords.ContextualKeyword._interface)) {
841 const oldIsType = _index.pushTypeContext.call(void 0, 1);
842 _index.next.call(void 0, );
843 flowParseInterface();
844 _index.popTypeContext.call(void 0, oldIsType);
845 } else {
846 _statement.parseStatement.call(void 0, true);
847 }
848} exports.flowParseExportDeclaration = flowParseExportDeclaration;
849
850 function flowShouldParseExportStar() {
851 return _index.match.call(void 0, _types.TokenType.star) || (_util.isContextual.call(void 0, _keywords.ContextualKeyword._type) && _index.lookaheadType.call(void 0, ) === _types.TokenType.star);
852} exports.flowShouldParseExportStar = flowShouldParseExportStar;
853
854 function flowParseExportStar() {
855 if (_util.eatContextual.call(void 0, _keywords.ContextualKeyword._type)) {
856 const oldIsType = _index.pushTypeContext.call(void 0, 2);
857 _statement.baseParseExportStar.call(void 0, );
858 _index.popTypeContext.call(void 0, oldIsType);
859 } else {
860 _statement.baseParseExportStar.call(void 0, );
861 }
862} exports.flowParseExportStar = flowParseExportStar;
863
864// parse a the super class type parameters and implements
865 function flowAfterParseClassSuper(hasSuper) {
866 if (hasSuper && _index.match.call(void 0, _types.TokenType.lessThan)) {
867 flowParseTypeParameterInstantiation();
868 }
869 if (_util.isContextual.call(void 0, _keywords.ContextualKeyword._implements)) {
870 const oldIsType = _index.pushTypeContext.call(void 0, 0);
871 _index.next.call(void 0, );
872 _base.state.tokens[_base.state.tokens.length - 1].type = _types.TokenType._implements;
873 do {
874 flowParseRestrictedIdentifier();
875 if (_index.match.call(void 0, _types.TokenType.lessThan)) {
876 flowParseTypeParameterInstantiation();
877 }
878 } while (_index.eat.call(void 0, _types.TokenType.comma));
879 _index.popTypeContext.call(void 0, oldIsType);
880 }
881} exports.flowAfterParseClassSuper = flowAfterParseClassSuper;
882
883// parse type parameters for object method shorthand
884 function flowStartParseObjPropValue() {
885 // method shorthand
886 if (_index.match.call(void 0, _types.TokenType.lessThan)) {
887 flowParseTypeParameterDeclaration();
888 if (!_index.match.call(void 0, _types.TokenType.parenL)) _util.unexpected.call(void 0, );
889 }
890} exports.flowStartParseObjPropValue = flowStartParseObjPropValue;
891
892 function flowParseAssignableListItemTypes() {
893 const oldIsType = _index.pushTypeContext.call(void 0, 0);
894 _index.eat.call(void 0, _types.TokenType.question);
895 if (_index.match.call(void 0, _types.TokenType.colon)) {
896 flowParseTypeAnnotation();
897 }
898 _index.popTypeContext.call(void 0, oldIsType);
899} exports.flowParseAssignableListItemTypes = flowParseAssignableListItemTypes;
900
901// parse typeof and type imports
902 function flowStartParseImportSpecifiers() {
903 if (_index.match.call(void 0, _types.TokenType._typeof) || _util.isContextual.call(void 0, _keywords.ContextualKeyword._type)) {
904 const lh = _index.lookaheadTypeAndKeyword.call(void 0, );
905 if (isMaybeDefaultImport(lh) || lh.type === _types.TokenType.braceL || lh.type === _types.TokenType.star) {
906 _index.next.call(void 0, );
907 }
908 }
909} exports.flowStartParseImportSpecifiers = flowStartParseImportSpecifiers;
910
911// parse import-type/typeof shorthand
912 function flowParseImportSpecifier() {
913 const isTypeKeyword =
914 _base.state.contextualKeyword === _keywords.ContextualKeyword._type || _base.state.type === _types.TokenType._typeof;
915 if (isTypeKeyword) {
916 _index.next.call(void 0, );
917 } else {
918 _expression.parseIdentifier.call(void 0, );
919 }
920
921 if (_util.isContextual.call(void 0, _keywords.ContextualKeyword._as) && !_util.isLookaheadContextual.call(void 0, _keywords.ContextualKeyword._as)) {
922 _expression.parseIdentifier.call(void 0, );
923 if (isTypeKeyword && !_index.match.call(void 0, _types.TokenType.name) && !(_base.state.type & _types.TokenType.IS_KEYWORD)) {
924 // `import {type as ,` or `import {type as }`
925 } else {
926 // `import {type as foo`
927 _expression.parseIdentifier.call(void 0, );
928 }
929 } else if (isTypeKeyword && (_index.match.call(void 0, _types.TokenType.name) || !!(_base.state.type & _types.TokenType.IS_KEYWORD))) {
930 // `import {type foo`
931 _expression.parseIdentifier.call(void 0, );
932 if (_util.eatContextual.call(void 0, _keywords.ContextualKeyword._as)) {
933 _expression.parseIdentifier.call(void 0, );
934 }
935 }
936} exports.flowParseImportSpecifier = flowParseImportSpecifier;
937
938// parse function type parameters - function foo<T>() {}
939 function flowStartParseFunctionParams() {
940 // Originally this checked if the method is a getter/setter, but if it was, we'd crash soon
941 // anyway, so don't try to propagate that information.
942 if (_index.match.call(void 0, _types.TokenType.lessThan)) {
943 const oldIsType = _index.pushTypeContext.call(void 0, 0);
944 flowParseTypeParameterDeclaration();
945 _index.popTypeContext.call(void 0, oldIsType);
946 }
947} exports.flowStartParseFunctionParams = flowStartParseFunctionParams;
948
949// parse flow type annotations on variable declarator heads - let foo: string = bar
950 function flowAfterParseVarHead() {
951 if (_index.match.call(void 0, _types.TokenType.colon)) {
952 flowParseTypeAnnotation();
953 }
954} exports.flowAfterParseVarHead = flowAfterParseVarHead;
955
956// parse the return type of an async arrow function - let foo = (async (): number => {});
957 function flowStartParseAsyncArrowFromCallExpression() {
958 if (_index.match.call(void 0, _types.TokenType.colon)) {
959 const oldNoAnonFunctionType = _base.state.noAnonFunctionType;
960 _base.state.noAnonFunctionType = true;
961 flowParseTypeAnnotation();
962 _base.state.noAnonFunctionType = oldNoAnonFunctionType;
963 }
964} exports.flowStartParseAsyncArrowFromCallExpression = flowStartParseAsyncArrowFromCallExpression;
965
966// We need to support type parameter declarations for arrow functions. This
967// is tricky. There are three situations we need to handle
968//
969// 1. This is either JSX or an arrow function. We'll try JSX first. If that
970// fails, we'll try an arrow function. If that fails, we'll throw the JSX
971// error.
972// 2. This is an arrow function. We'll parse the type parameter declaration,
973// parse the rest, make sure the rest is an arrow function, and go from
974// there
975// 3. This is neither. Just call the super method
976 function flowParseMaybeAssign(noIn, isWithinParens) {
977 if (_index.match.call(void 0, _types.TokenType.lessThan)) {
978 const snapshot = _base.state.snapshot();
979 let wasArrow = _expression.baseParseMaybeAssign.call(void 0, noIn, isWithinParens);
980 if (_base.state.error) {
981 _base.state.restoreFromSnapshot(snapshot);
982 _base.state.type = _types.TokenType.typeParameterStart;
983 } else {
984 return wasArrow;
985 }
986
987 const oldIsType = _index.pushTypeContext.call(void 0, 0);
988 flowParseTypeParameterDeclaration();
989 _index.popTypeContext.call(void 0, oldIsType);
990 wasArrow = _expression.baseParseMaybeAssign.call(void 0, noIn, isWithinParens);
991 if (wasArrow) {
992 return true;
993 }
994 _util.unexpected.call(void 0, );
995 }
996
997 return _expression.baseParseMaybeAssign.call(void 0, noIn, isWithinParens);
998} exports.flowParseMaybeAssign = flowParseMaybeAssign;
999
1000// handle return types for arrow functions
1001 function flowParseArrow() {
1002 if (_index.match.call(void 0, _types.TokenType.colon)) {
1003 const oldIsType = _index.pushTypeContext.call(void 0, 0);
1004 const snapshot = _base.state.snapshot();
1005
1006 const oldNoAnonFunctionType = _base.state.noAnonFunctionType;
1007 _base.state.noAnonFunctionType = true;
1008 flowParseTypeAndPredicateInitialiser();
1009 _base.state.noAnonFunctionType = oldNoAnonFunctionType;
1010
1011 if (_util.canInsertSemicolon.call(void 0, )) _util.unexpected.call(void 0, );
1012 if (!_index.match.call(void 0, _types.TokenType.arrow)) _util.unexpected.call(void 0, );
1013
1014 if (_base.state.error) {
1015 _base.state.restoreFromSnapshot(snapshot);
1016 }
1017 _index.popTypeContext.call(void 0, oldIsType);
1018 }
1019 return _index.eat.call(void 0, _types.TokenType.arrow);
1020} exports.flowParseArrow = flowParseArrow;
1021
1022 function flowParseSubscripts(startPos, noCalls = false) {
1023 if (
1024 _base.state.tokens[_base.state.tokens.length - 1].contextualKeyword === _keywords.ContextualKeyword._async &&
1025 _index.match.call(void 0, _types.TokenType.lessThan)
1026 ) {
1027 const snapshot = _base.state.snapshot();
1028 const wasArrow = parseAsyncArrowWithTypeParameters(startPos);
1029 if (wasArrow && !_base.state.error) {
1030 return;
1031 }
1032 _base.state.restoreFromSnapshot(snapshot);
1033 }
1034
1035 _expression.baseParseSubscripts.call(void 0, startPos, noCalls);
1036} exports.flowParseSubscripts = flowParseSubscripts;
1037
1038// Returns true if there was an arrow function here.
1039function parseAsyncArrowWithTypeParameters(startPos) {
1040 _base.state.scopeDepth++;
1041 const startTokenIndex = _base.state.tokens.length;
1042 _statement.parseFunctionParams.call(void 0, );
1043 if (!_expression.parseArrow.call(void 0, )) {
1044 return false;
1045 }
1046 _expression.parseArrowExpression.call(void 0, startPos, startTokenIndex);
1047 return true;
1048}