flow.js 38 KB

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