source: frontend/node_modules/sucrase/dist/parser/plugins/flow.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 2 weeks ago

Fix frontend appearance

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