UNPKG

1.45 kBJavaScriptView Raw
1import {eat, lookaheadType, match} from "../tokenizer/index";
2import {TokenType as tt} from "../tokenizer/types";
3import {isFlowEnabled, isTypeScriptEnabled, state} from "../traverser/base";
4import {baseParseConditional} from "../traverser/expression";
5import {flowParseTypeAnnotation} from "./flow";
6import {tsParseTypeAnnotation} from "./typescript";
7
8/**
9 * Common parser code for TypeScript and Flow.
10 */
11
12// An apparent conditional expression could actually be an optional parameter in an arrow function.
13export function typedParseConditional(noIn) {
14 // If we see ?:, this can't possibly be a valid conditional. typedParseParenItem will be called
15 // later to finish off the arrow parameter. We also need to handle bare ? tokens for optional
16 // parameters without type annotations, i.e. ?, and ?) .
17 if (match(tt.question)) {
18 const nextType = lookaheadType();
19 if (nextType === tt.colon || nextType === tt.comma || nextType === tt.parenR) {
20 return;
21 }
22 }
23 baseParseConditional(noIn);
24}
25
26// Note: These "type casts" are *not* valid TS expressions.
27// But we parse them here and change them when completing the arrow function.
28export function typedParseParenItem() {
29 if (eat(tt.question)) {
30 state.tokens[state.tokens.length - 1].isType = true;
31 }
32 if (match(tt.colon)) {
33 if (isTypeScriptEnabled) {
34 tsParseTypeAnnotation();
35 } else if (isFlowEnabled) {
36 flowParseTypeAnnotation();
37 }
38 }
39}