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