No Description

flow.js 26KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106
  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. }