flow.js 26 KB

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