Нема описа

flow.js 38KB

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