flow.js 81 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _types = require("../tokenizer/types");
  7. var N = _interopRequireWildcard(require("../types"));
  8. var _context = require("../tokenizer/context");
  9. var _identifier = require("../util/identifier");
  10. var _scopeflags = require("../util/scopeflags");
  11. function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
  12. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
  13. const reservedTypes = new Set(["_", "any", "bool", "boolean", "empty", "extends", "false", "interface", "mixed", "null", "number", "static", "string", "true", "typeof", "void"]);
  14. function isEsModuleType(bodyElement) {
  15. return bodyElement.type === "DeclareExportAllDeclaration" || bodyElement.type === "DeclareExportDeclaration" && (!bodyElement.declaration || bodyElement.declaration.type !== "TypeAlias" && bodyElement.declaration.type !== "InterfaceDeclaration");
  16. }
  17. function hasTypeImportKind(node) {
  18. return node.importKind === "type" || node.importKind === "typeof";
  19. }
  20. function isMaybeDefaultImport(state) {
  21. return (state.type === _types.types.name || !!state.type.keyword) && state.value !== "from";
  22. }
  23. const exportSuggestions = {
  24. const: "declare export var",
  25. let: "declare export var",
  26. type: "export type",
  27. interface: "export interface"
  28. };
  29. function partition(list, test) {
  30. const list1 = [];
  31. const list2 = [];
  32. for (let i = 0; i < list.length; i++) {
  33. (test(list[i], i, list) ? list1 : list2).push(list[i]);
  34. }
  35. return [list1, list2];
  36. }
  37. const FLOW_PRAGMA_REGEX = /\*?\s*@((?:no)?flow)\b/;
  38. var _default = superClass => class extends superClass {
  39. constructor(options, input) {
  40. super(options, input);
  41. this.flowPragma = undefined;
  42. }
  43. shouldParseTypes() {
  44. return this.getPluginOption("flow", "all") || this.flowPragma === "flow";
  45. }
  46. shouldParseEnums() {
  47. return !!this.getPluginOption("flow", "enums");
  48. }
  49. finishToken(type, val) {
  50. if (type !== _types.types.string && type !== _types.types.semi && type !== _types.types.interpreterDirective) {
  51. if (this.flowPragma === undefined) {
  52. this.flowPragma = null;
  53. }
  54. }
  55. return super.finishToken(type, val);
  56. }
  57. addComment(comment) {
  58. if (this.flowPragma === undefined) {
  59. const matches = FLOW_PRAGMA_REGEX.exec(comment.value);
  60. if (!matches) {} else if (matches[1] === "flow") {
  61. this.flowPragma = "flow";
  62. } else if (matches[1] === "noflow") {
  63. this.flowPragma = "noflow";
  64. } else {
  65. throw new Error("Unexpected flow pragma");
  66. }
  67. }
  68. return super.addComment(comment);
  69. }
  70. flowParseTypeInitialiser(tok) {
  71. const oldInType = this.state.inType;
  72. this.state.inType = true;
  73. this.expect(tok || _types.types.colon);
  74. const type = this.flowParseType();
  75. this.state.inType = oldInType;
  76. return type;
  77. }
  78. flowParsePredicate() {
  79. const node = this.startNode();
  80. const moduloLoc = this.state.startLoc;
  81. const moduloPos = this.state.start;
  82. this.expect(_types.types.modulo);
  83. const checksLoc = this.state.startLoc;
  84. this.expectContextual("checks");
  85. if (moduloLoc.line !== checksLoc.line || moduloLoc.column !== checksLoc.column - 1) {
  86. this.raise(moduloPos, "Spaces between ´%´ and ´checks´ are not allowed here.");
  87. }
  88. if (this.eat(_types.types.parenL)) {
  89. node.value = this.parseExpression();
  90. this.expect(_types.types.parenR);
  91. return this.finishNode(node, "DeclaredPredicate");
  92. } else {
  93. return this.finishNode(node, "InferredPredicate");
  94. }
  95. }
  96. flowParseTypeAndPredicateInitialiser() {
  97. const oldInType = this.state.inType;
  98. this.state.inType = true;
  99. this.expect(_types.types.colon);
  100. let type = null;
  101. let predicate = null;
  102. if (this.match(_types.types.modulo)) {
  103. this.state.inType = oldInType;
  104. predicate = this.flowParsePredicate();
  105. } else {
  106. type = this.flowParseType();
  107. this.state.inType = oldInType;
  108. if (this.match(_types.types.modulo)) {
  109. predicate = this.flowParsePredicate();
  110. }
  111. }
  112. return [type, predicate];
  113. }
  114. flowParseDeclareClass(node) {
  115. this.next();
  116. this.flowParseInterfaceish(node, true);
  117. return this.finishNode(node, "DeclareClass");
  118. }
  119. flowParseDeclareFunction(node) {
  120. this.next();
  121. const id = node.id = this.parseIdentifier();
  122. const typeNode = this.startNode();
  123. const typeContainer = this.startNode();
  124. if (this.isRelational("<")) {
  125. typeNode.typeParameters = this.flowParseTypeParameterDeclaration();
  126. } else {
  127. typeNode.typeParameters = null;
  128. }
  129. this.expect(_types.types.parenL);
  130. const tmp = this.flowParseFunctionTypeParams();
  131. typeNode.params = tmp.params;
  132. typeNode.rest = tmp.rest;
  133. this.expect(_types.types.parenR);
  134. [typeNode.returnType, node.predicate] = this.flowParseTypeAndPredicateInitialiser();
  135. typeContainer.typeAnnotation = this.finishNode(typeNode, "FunctionTypeAnnotation");
  136. id.typeAnnotation = this.finishNode(typeContainer, "TypeAnnotation");
  137. this.resetEndLocation(id);
  138. this.semicolon();
  139. return this.finishNode(node, "DeclareFunction");
  140. }
  141. flowParseDeclare(node, insideModule) {
  142. if (this.match(_types.types._class)) {
  143. return this.flowParseDeclareClass(node);
  144. } else if (this.match(_types.types._function)) {
  145. return this.flowParseDeclareFunction(node);
  146. } else if (this.match(_types.types._var)) {
  147. return this.flowParseDeclareVariable(node);
  148. } else if (this.eatContextual("module")) {
  149. if (this.match(_types.types.dot)) {
  150. return this.flowParseDeclareModuleExports(node);
  151. } else {
  152. if (insideModule) {
  153. this.raise(this.state.lastTokStart, "`declare module` cannot be used inside another `declare module`");
  154. }
  155. return this.flowParseDeclareModule(node);
  156. }
  157. } else if (this.isContextual("type")) {
  158. return this.flowParseDeclareTypeAlias(node);
  159. } else if (this.isContextual("opaque")) {
  160. return this.flowParseDeclareOpaqueType(node);
  161. } else if (this.isContextual("interface")) {
  162. return this.flowParseDeclareInterface(node);
  163. } else if (this.match(_types.types._export)) {
  164. return this.flowParseDeclareExportDeclaration(node, insideModule);
  165. } else {
  166. throw this.unexpected();
  167. }
  168. }
  169. flowParseDeclareVariable(node) {
  170. this.next();
  171. node.id = this.flowParseTypeAnnotatableIdentifier(true);
  172. this.scope.declareName(node.id.name, _scopeflags.BIND_VAR, node.id.start);
  173. this.semicolon();
  174. return this.finishNode(node, "DeclareVariable");
  175. }
  176. flowParseDeclareModule(node) {
  177. this.scope.enter(_scopeflags.SCOPE_OTHER);
  178. if (this.match(_types.types.string)) {
  179. node.id = this.parseExprAtom();
  180. } else {
  181. node.id = this.parseIdentifier();
  182. }
  183. const bodyNode = node.body = this.startNode();
  184. const body = bodyNode.body = [];
  185. this.expect(_types.types.braceL);
  186. while (!this.match(_types.types.braceR)) {
  187. let bodyNode = this.startNode();
  188. if (this.match(_types.types._import)) {
  189. this.next();
  190. if (!this.isContextual("type") && !this.match(_types.types._typeof)) {
  191. this.raise(this.state.lastTokStart, "Imports within a `declare module` body must always be `import type` or `import typeof`");
  192. }
  193. this.parseImport(bodyNode);
  194. } else {
  195. this.expectContextual("declare", "Only declares and type imports are allowed inside declare module");
  196. bodyNode = this.flowParseDeclare(bodyNode, true);
  197. }
  198. body.push(bodyNode);
  199. }
  200. this.scope.exit();
  201. this.expect(_types.types.braceR);
  202. this.finishNode(bodyNode, "BlockStatement");
  203. let kind = null;
  204. let hasModuleExport = false;
  205. const errorMessage = "Found both `declare module.exports` and `declare export` in the same module. " + "Modules can only have 1 since they are either an ES module or they are a CommonJS module";
  206. body.forEach(bodyElement => {
  207. if (isEsModuleType(bodyElement)) {
  208. if (kind === "CommonJS") {
  209. this.raise(bodyElement.start, errorMessage);
  210. }
  211. kind = "ES";
  212. } else if (bodyElement.type === "DeclareModuleExports") {
  213. if (hasModuleExport) {
  214. this.raise(bodyElement.start, "Duplicate `declare module.exports` statement");
  215. }
  216. if (kind === "ES") this.raise(bodyElement.start, errorMessage);
  217. kind = "CommonJS";
  218. hasModuleExport = true;
  219. }
  220. });
  221. node.kind = kind || "CommonJS";
  222. return this.finishNode(node, "DeclareModule");
  223. }
  224. flowParseDeclareExportDeclaration(node, insideModule) {
  225. this.expect(_types.types._export);
  226. if (this.eat(_types.types._default)) {
  227. if (this.match(_types.types._function) || this.match(_types.types._class)) {
  228. node.declaration = this.flowParseDeclare(this.startNode());
  229. } else {
  230. node.declaration = this.flowParseType();
  231. this.semicolon();
  232. }
  233. node.default = true;
  234. return this.finishNode(node, "DeclareExportDeclaration");
  235. } else {
  236. if (this.match(_types.types._const) || this.isLet() || (this.isContextual("type") || this.isContextual("interface")) && !insideModule) {
  237. const label = this.state.value;
  238. const suggestion = exportSuggestions[label];
  239. this.unexpected(this.state.start, `\`declare export ${label}\` is not supported. Use \`${suggestion}\` instead`);
  240. }
  241. if (this.match(_types.types._var) || this.match(_types.types._function) || this.match(_types.types._class) || this.isContextual("opaque")) {
  242. node.declaration = this.flowParseDeclare(this.startNode());
  243. node.default = false;
  244. return this.finishNode(node, "DeclareExportDeclaration");
  245. } else if (this.match(_types.types.star) || this.match(_types.types.braceL) || this.isContextual("interface") || this.isContextual("type") || this.isContextual("opaque")) {
  246. node = this.parseExport(node);
  247. if (node.type === "ExportNamedDeclaration") {
  248. node.type = "ExportDeclaration";
  249. node.default = false;
  250. delete node.exportKind;
  251. }
  252. node.type = "Declare" + node.type;
  253. return node;
  254. }
  255. }
  256. throw this.unexpected();
  257. }
  258. flowParseDeclareModuleExports(node) {
  259. this.next();
  260. this.expectContextual("exports");
  261. node.typeAnnotation = this.flowParseTypeAnnotation();
  262. this.semicolon();
  263. return this.finishNode(node, "DeclareModuleExports");
  264. }
  265. flowParseDeclareTypeAlias(node) {
  266. this.next();
  267. this.flowParseTypeAlias(node);
  268. node.type = "DeclareTypeAlias";
  269. return node;
  270. }
  271. flowParseDeclareOpaqueType(node) {
  272. this.next();
  273. this.flowParseOpaqueType(node, true);
  274. node.type = "DeclareOpaqueType";
  275. return node;
  276. }
  277. flowParseDeclareInterface(node) {
  278. this.next();
  279. this.flowParseInterfaceish(node);
  280. return this.finishNode(node, "DeclareInterface");
  281. }
  282. flowParseInterfaceish(node, isClass = false) {
  283. node.id = this.flowParseRestrictedIdentifier(!isClass, true);
  284. this.scope.declareName(node.id.name, isClass ? _scopeflags.BIND_FUNCTION : _scopeflags.BIND_LEXICAL, node.id.start);
  285. if (this.isRelational("<")) {
  286. node.typeParameters = this.flowParseTypeParameterDeclaration();
  287. } else {
  288. node.typeParameters = null;
  289. }
  290. node.extends = [];
  291. node.implements = [];
  292. node.mixins = [];
  293. if (this.eat(_types.types._extends)) {
  294. do {
  295. node.extends.push(this.flowParseInterfaceExtends());
  296. } while (!isClass && this.eat(_types.types.comma));
  297. }
  298. if (this.isContextual("mixins")) {
  299. this.next();
  300. do {
  301. node.mixins.push(this.flowParseInterfaceExtends());
  302. } while (this.eat(_types.types.comma));
  303. }
  304. if (this.isContextual("implements")) {
  305. this.next();
  306. do {
  307. node.implements.push(this.flowParseInterfaceExtends());
  308. } while (this.eat(_types.types.comma));
  309. }
  310. node.body = this.flowParseObjectType({
  311. allowStatic: isClass,
  312. allowExact: false,
  313. allowSpread: false,
  314. allowProto: isClass,
  315. allowInexact: false
  316. });
  317. }
  318. flowParseInterfaceExtends() {
  319. const node = this.startNode();
  320. node.id = this.flowParseQualifiedTypeIdentifier();
  321. if (this.isRelational("<")) {
  322. node.typeParameters = this.flowParseTypeParameterInstantiation();
  323. } else {
  324. node.typeParameters = null;
  325. }
  326. return this.finishNode(node, "InterfaceExtends");
  327. }
  328. flowParseInterface(node) {
  329. this.flowParseInterfaceish(node);
  330. return this.finishNode(node, "InterfaceDeclaration");
  331. }
  332. checkNotUnderscore(word) {
  333. if (word === "_") {
  334. this.raise(this.state.start, "`_` is only allowed as a type argument to call or new");
  335. }
  336. }
  337. checkReservedType(word, startLoc, declaration) {
  338. if (!reservedTypes.has(word)) return;
  339. if (declaration) {
  340. this.raise(startLoc, `Cannot overwrite reserved type ${word}`);
  341. return;
  342. }
  343. this.raise(startLoc, `Unexpected reserved type ${word}`);
  344. }
  345. flowParseRestrictedIdentifier(liberal, declaration) {
  346. this.checkReservedType(this.state.value, this.state.start, declaration);
  347. return this.parseIdentifier(liberal);
  348. }
  349. flowParseTypeAlias(node) {
  350. node.id = this.flowParseRestrictedIdentifier(false, true);
  351. this.scope.declareName(node.id.name, _scopeflags.BIND_LEXICAL, node.id.start);
  352. if (this.isRelational("<")) {
  353. node.typeParameters = this.flowParseTypeParameterDeclaration();
  354. } else {
  355. node.typeParameters = null;
  356. }
  357. node.right = this.flowParseTypeInitialiser(_types.types.eq);
  358. this.semicolon();
  359. return this.finishNode(node, "TypeAlias");
  360. }
  361. flowParseOpaqueType(node, declare) {
  362. this.expectContextual("type");
  363. node.id = this.flowParseRestrictedIdentifier(true, true);
  364. this.scope.declareName(node.id.name, _scopeflags.BIND_LEXICAL, node.id.start);
  365. if (this.isRelational("<")) {
  366. node.typeParameters = this.flowParseTypeParameterDeclaration();
  367. } else {
  368. node.typeParameters = null;
  369. }
  370. node.supertype = null;
  371. if (this.match(_types.types.colon)) {
  372. node.supertype = this.flowParseTypeInitialiser(_types.types.colon);
  373. }
  374. node.impltype = null;
  375. if (!declare) {
  376. node.impltype = this.flowParseTypeInitialiser(_types.types.eq);
  377. }
  378. this.semicolon();
  379. return this.finishNode(node, "OpaqueType");
  380. }
  381. flowParseTypeParameter(requireDefault = false) {
  382. const nodeStart = this.state.start;
  383. const node = this.startNode();
  384. const variance = this.flowParseVariance();
  385. const ident = this.flowParseTypeAnnotatableIdentifier();
  386. node.name = ident.name;
  387. node.variance = variance;
  388. node.bound = ident.typeAnnotation;
  389. if (this.match(_types.types.eq)) {
  390. this.eat(_types.types.eq);
  391. node.default = this.flowParseType();
  392. } else {
  393. if (requireDefault) {
  394. this.raise(nodeStart, "Type parameter declaration needs a default, since a preceding type parameter declaration has a default.");
  395. }
  396. }
  397. return this.finishNode(node, "TypeParameter");
  398. }
  399. flowParseTypeParameterDeclaration() {
  400. const oldInType = this.state.inType;
  401. const node = this.startNode();
  402. node.params = [];
  403. this.state.inType = true;
  404. if (this.isRelational("<") || this.match(_types.types.jsxTagStart)) {
  405. this.next();
  406. } else {
  407. this.unexpected();
  408. }
  409. let defaultRequired = false;
  410. do {
  411. const typeParameter = this.flowParseTypeParameter(defaultRequired);
  412. node.params.push(typeParameter);
  413. if (typeParameter.default) {
  414. defaultRequired = true;
  415. }
  416. if (!this.isRelational(">")) {
  417. this.expect(_types.types.comma);
  418. }
  419. } while (!this.isRelational(">"));
  420. this.expectRelational(">");
  421. this.state.inType = oldInType;
  422. return this.finishNode(node, "TypeParameterDeclaration");
  423. }
  424. flowParseTypeParameterInstantiation() {
  425. const node = this.startNode();
  426. const oldInType = this.state.inType;
  427. node.params = [];
  428. this.state.inType = true;
  429. this.expectRelational("<");
  430. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  431. this.state.noAnonFunctionType = false;
  432. while (!this.isRelational(">")) {
  433. node.params.push(this.flowParseType());
  434. if (!this.isRelational(">")) {
  435. this.expect(_types.types.comma);
  436. }
  437. }
  438. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  439. this.expectRelational(">");
  440. this.state.inType = oldInType;
  441. return this.finishNode(node, "TypeParameterInstantiation");
  442. }
  443. flowParseTypeParameterInstantiationCallOrNew() {
  444. const node = this.startNode();
  445. const oldInType = this.state.inType;
  446. node.params = [];
  447. this.state.inType = true;
  448. this.expectRelational("<");
  449. while (!this.isRelational(">")) {
  450. node.params.push(this.flowParseTypeOrImplicitInstantiation());
  451. if (!this.isRelational(">")) {
  452. this.expect(_types.types.comma);
  453. }
  454. }
  455. this.expectRelational(">");
  456. this.state.inType = oldInType;
  457. return this.finishNode(node, "TypeParameterInstantiation");
  458. }
  459. flowParseInterfaceType() {
  460. const node = this.startNode();
  461. this.expectContextual("interface");
  462. node.extends = [];
  463. if (this.eat(_types.types._extends)) {
  464. do {
  465. node.extends.push(this.flowParseInterfaceExtends());
  466. } while (this.eat(_types.types.comma));
  467. }
  468. node.body = this.flowParseObjectType({
  469. allowStatic: false,
  470. allowExact: false,
  471. allowSpread: false,
  472. allowProto: false,
  473. allowInexact: false
  474. });
  475. return this.finishNode(node, "InterfaceTypeAnnotation");
  476. }
  477. flowParseObjectPropertyKey() {
  478. return this.match(_types.types.num) || this.match(_types.types.string) ? this.parseExprAtom() : this.parseIdentifier(true);
  479. }
  480. flowParseObjectTypeIndexer(node, isStatic, variance) {
  481. node.static = isStatic;
  482. if (this.lookahead().type === _types.types.colon) {
  483. node.id = this.flowParseObjectPropertyKey();
  484. node.key = this.flowParseTypeInitialiser();
  485. } else {
  486. node.id = null;
  487. node.key = this.flowParseType();
  488. }
  489. this.expect(_types.types.bracketR);
  490. node.value = this.flowParseTypeInitialiser();
  491. node.variance = variance;
  492. return this.finishNode(node, "ObjectTypeIndexer");
  493. }
  494. flowParseObjectTypeInternalSlot(node, isStatic) {
  495. node.static = isStatic;
  496. node.id = this.flowParseObjectPropertyKey();
  497. this.expect(_types.types.bracketR);
  498. this.expect(_types.types.bracketR);
  499. if (this.isRelational("<") || this.match(_types.types.parenL)) {
  500. node.method = true;
  501. node.optional = false;
  502. node.value = this.flowParseObjectTypeMethodish(this.startNodeAt(node.start, node.loc.start));
  503. } else {
  504. node.method = false;
  505. if (this.eat(_types.types.question)) {
  506. node.optional = true;
  507. }
  508. node.value = this.flowParseTypeInitialiser();
  509. }
  510. return this.finishNode(node, "ObjectTypeInternalSlot");
  511. }
  512. flowParseObjectTypeMethodish(node) {
  513. node.params = [];
  514. node.rest = null;
  515. node.typeParameters = null;
  516. if (this.isRelational("<")) {
  517. node.typeParameters = this.flowParseTypeParameterDeclaration();
  518. }
  519. this.expect(_types.types.parenL);
  520. while (!this.match(_types.types.parenR) && !this.match(_types.types.ellipsis)) {
  521. node.params.push(this.flowParseFunctionTypeParam());
  522. if (!this.match(_types.types.parenR)) {
  523. this.expect(_types.types.comma);
  524. }
  525. }
  526. if (this.eat(_types.types.ellipsis)) {
  527. node.rest = this.flowParseFunctionTypeParam();
  528. }
  529. this.expect(_types.types.parenR);
  530. node.returnType = this.flowParseTypeInitialiser();
  531. return this.finishNode(node, "FunctionTypeAnnotation");
  532. }
  533. flowParseObjectTypeCallProperty(node, isStatic) {
  534. const valueNode = this.startNode();
  535. node.static = isStatic;
  536. node.value = this.flowParseObjectTypeMethodish(valueNode);
  537. return this.finishNode(node, "ObjectTypeCallProperty");
  538. }
  539. flowParseObjectType({
  540. allowStatic,
  541. allowExact,
  542. allowSpread,
  543. allowProto,
  544. allowInexact
  545. }) {
  546. const oldInType = this.state.inType;
  547. this.state.inType = true;
  548. const nodeStart = this.startNode();
  549. nodeStart.callProperties = [];
  550. nodeStart.properties = [];
  551. nodeStart.indexers = [];
  552. nodeStart.internalSlots = [];
  553. let endDelim;
  554. let exact;
  555. let inexact = false;
  556. if (allowExact && this.match(_types.types.braceBarL)) {
  557. this.expect(_types.types.braceBarL);
  558. endDelim = _types.types.braceBarR;
  559. exact = true;
  560. } else {
  561. this.expect(_types.types.braceL);
  562. endDelim = _types.types.braceR;
  563. exact = false;
  564. }
  565. nodeStart.exact = exact;
  566. while (!this.match(endDelim)) {
  567. let isStatic = false;
  568. let protoStart = null;
  569. let inexactStart = null;
  570. const node = this.startNode();
  571. if (allowProto && this.isContextual("proto")) {
  572. const lookahead = this.lookahead();
  573. if (lookahead.type !== _types.types.colon && lookahead.type !== _types.types.question) {
  574. this.next();
  575. protoStart = this.state.start;
  576. allowStatic = false;
  577. }
  578. }
  579. if (allowStatic && this.isContextual("static")) {
  580. const lookahead = this.lookahead();
  581. if (lookahead.type !== _types.types.colon && lookahead.type !== _types.types.question) {
  582. this.next();
  583. isStatic = true;
  584. }
  585. }
  586. const variance = this.flowParseVariance();
  587. if (this.eat(_types.types.bracketL)) {
  588. if (protoStart != null) {
  589. this.unexpected(protoStart);
  590. }
  591. if (this.eat(_types.types.bracketL)) {
  592. if (variance) {
  593. this.unexpected(variance.start);
  594. }
  595. nodeStart.internalSlots.push(this.flowParseObjectTypeInternalSlot(node, isStatic));
  596. } else {
  597. nodeStart.indexers.push(this.flowParseObjectTypeIndexer(node, isStatic, variance));
  598. }
  599. } else if (this.match(_types.types.parenL) || this.isRelational("<")) {
  600. if (protoStart != null) {
  601. this.unexpected(protoStart);
  602. }
  603. if (variance) {
  604. this.unexpected(variance.start);
  605. }
  606. nodeStart.callProperties.push(this.flowParseObjectTypeCallProperty(node, isStatic));
  607. } else {
  608. let kind = "init";
  609. if (this.isContextual("get") || this.isContextual("set")) {
  610. const lookahead = this.lookahead();
  611. if (lookahead.type === _types.types.name || lookahead.type === _types.types.string || lookahead.type === _types.types.num) {
  612. kind = this.state.value;
  613. this.next();
  614. }
  615. }
  616. const propOrInexact = this.flowParseObjectTypeProperty(node, isStatic, protoStart, variance, kind, allowSpread, allowInexact != null ? allowInexact : !exact);
  617. if (propOrInexact === null) {
  618. inexact = true;
  619. inexactStart = this.state.lastTokStart;
  620. } else {
  621. nodeStart.properties.push(propOrInexact);
  622. }
  623. }
  624. this.flowObjectTypeSemicolon();
  625. if (inexactStart && !this.match(_types.types.braceR) && !this.match(_types.types.braceBarR)) {
  626. this.raise(inexactStart, "Explicit inexact syntax must appear at the end of an inexact object");
  627. }
  628. }
  629. this.expect(endDelim);
  630. if (allowSpread) {
  631. nodeStart.inexact = inexact;
  632. }
  633. const out = this.finishNode(nodeStart, "ObjectTypeAnnotation");
  634. this.state.inType = oldInType;
  635. return out;
  636. }
  637. flowParseObjectTypeProperty(node, isStatic, protoStart, variance, kind, allowSpread, allowInexact) {
  638. if (this.eat(_types.types.ellipsis)) {
  639. const isInexactToken = this.match(_types.types.comma) || this.match(_types.types.semi) || this.match(_types.types.braceR) || this.match(_types.types.braceBarR);
  640. if (isInexactToken) {
  641. if (!allowSpread) {
  642. this.raise(this.state.lastTokStart, "Explicit inexact syntax cannot appear in class or interface definitions");
  643. } else if (!allowInexact) {
  644. this.raise(this.state.lastTokStart, "Explicit inexact syntax cannot appear inside an explicit exact object type");
  645. }
  646. if (variance) {
  647. this.raise(variance.start, "Explicit inexact syntax cannot have variance");
  648. }
  649. return null;
  650. }
  651. if (!allowSpread) {
  652. this.raise(this.state.lastTokStart, "Spread operator cannot appear in class or interface definitions");
  653. }
  654. if (protoStart != null) {
  655. this.unexpected(protoStart);
  656. }
  657. if (variance) {
  658. this.raise(variance.start, "Spread properties cannot have variance");
  659. }
  660. node.argument = this.flowParseType();
  661. return this.finishNode(node, "ObjectTypeSpreadProperty");
  662. } else {
  663. node.key = this.flowParseObjectPropertyKey();
  664. node.static = isStatic;
  665. node.proto = protoStart != null;
  666. node.kind = kind;
  667. let optional = false;
  668. if (this.isRelational("<") || this.match(_types.types.parenL)) {
  669. node.method = true;
  670. if (protoStart != null) {
  671. this.unexpected(protoStart);
  672. }
  673. if (variance) {
  674. this.unexpected(variance.start);
  675. }
  676. node.value = this.flowParseObjectTypeMethodish(this.startNodeAt(node.start, node.loc.start));
  677. if (kind === "get" || kind === "set") {
  678. this.flowCheckGetterSetterParams(node);
  679. }
  680. } else {
  681. if (kind !== "init") this.unexpected();
  682. node.method = false;
  683. if (this.eat(_types.types.question)) {
  684. optional = true;
  685. }
  686. node.value = this.flowParseTypeInitialiser();
  687. node.variance = variance;
  688. }
  689. node.optional = optional;
  690. return this.finishNode(node, "ObjectTypeProperty");
  691. }
  692. }
  693. flowCheckGetterSetterParams(property) {
  694. const paramCount = property.kind === "get" ? 0 : 1;
  695. const start = property.start;
  696. const length = property.value.params.length + (property.value.rest ? 1 : 0);
  697. if (length !== paramCount) {
  698. if (property.kind === "get") {
  699. this.raise(start, "getter must not have any formal parameters");
  700. } else {
  701. this.raise(start, "setter must have exactly one formal parameter");
  702. }
  703. }
  704. if (property.kind === "set" && property.value.rest) {
  705. this.raise(start, "setter function argument must not be a rest parameter");
  706. }
  707. }
  708. flowObjectTypeSemicolon() {
  709. if (!this.eat(_types.types.semi) && !this.eat(_types.types.comma) && !this.match(_types.types.braceR) && !this.match(_types.types.braceBarR)) {
  710. this.unexpected();
  711. }
  712. }
  713. flowParseQualifiedTypeIdentifier(startPos, startLoc, id) {
  714. startPos = startPos || this.state.start;
  715. startLoc = startLoc || this.state.startLoc;
  716. let node = id || this.flowParseRestrictedIdentifier(true);
  717. while (this.eat(_types.types.dot)) {
  718. const node2 = this.startNodeAt(startPos, startLoc);
  719. node2.qualification = node;
  720. node2.id = this.flowParseRestrictedIdentifier(true);
  721. node = this.finishNode(node2, "QualifiedTypeIdentifier");
  722. }
  723. return node;
  724. }
  725. flowParseGenericType(startPos, startLoc, id) {
  726. const node = this.startNodeAt(startPos, startLoc);
  727. node.typeParameters = null;
  728. node.id = this.flowParseQualifiedTypeIdentifier(startPos, startLoc, id);
  729. if (this.isRelational("<")) {
  730. node.typeParameters = this.flowParseTypeParameterInstantiation();
  731. }
  732. return this.finishNode(node, "GenericTypeAnnotation");
  733. }
  734. flowParseTypeofType() {
  735. const node = this.startNode();
  736. this.expect(_types.types._typeof);
  737. node.argument = this.flowParsePrimaryType();
  738. return this.finishNode(node, "TypeofTypeAnnotation");
  739. }
  740. flowParseTupleType() {
  741. const node = this.startNode();
  742. node.types = [];
  743. this.expect(_types.types.bracketL);
  744. while (this.state.pos < this.length && !this.match(_types.types.bracketR)) {
  745. node.types.push(this.flowParseType());
  746. if (this.match(_types.types.bracketR)) break;
  747. this.expect(_types.types.comma);
  748. }
  749. this.expect(_types.types.bracketR);
  750. return this.finishNode(node, "TupleTypeAnnotation");
  751. }
  752. flowParseFunctionTypeParam() {
  753. let name = null;
  754. let optional = false;
  755. let typeAnnotation = null;
  756. const node = this.startNode();
  757. const lh = this.lookahead();
  758. if (lh.type === _types.types.colon || lh.type === _types.types.question) {
  759. name = this.parseIdentifier();
  760. if (this.eat(_types.types.question)) {
  761. optional = true;
  762. }
  763. typeAnnotation = this.flowParseTypeInitialiser();
  764. } else {
  765. typeAnnotation = this.flowParseType();
  766. }
  767. node.name = name;
  768. node.optional = optional;
  769. node.typeAnnotation = typeAnnotation;
  770. return this.finishNode(node, "FunctionTypeParam");
  771. }
  772. reinterpretTypeAsFunctionTypeParam(type) {
  773. const node = this.startNodeAt(type.start, type.loc.start);
  774. node.name = null;
  775. node.optional = false;
  776. node.typeAnnotation = type;
  777. return this.finishNode(node, "FunctionTypeParam");
  778. }
  779. flowParseFunctionTypeParams(params = []) {
  780. let rest = null;
  781. while (!this.match(_types.types.parenR) && !this.match(_types.types.ellipsis)) {
  782. params.push(this.flowParseFunctionTypeParam());
  783. if (!this.match(_types.types.parenR)) {
  784. this.expect(_types.types.comma);
  785. }
  786. }
  787. if (this.eat(_types.types.ellipsis)) {
  788. rest = this.flowParseFunctionTypeParam();
  789. }
  790. return {
  791. params,
  792. rest
  793. };
  794. }
  795. flowIdentToTypeAnnotation(startPos, startLoc, node, id) {
  796. switch (id.name) {
  797. case "any":
  798. return this.finishNode(node, "AnyTypeAnnotation");
  799. case "bool":
  800. case "boolean":
  801. return this.finishNode(node, "BooleanTypeAnnotation");
  802. case "mixed":
  803. return this.finishNode(node, "MixedTypeAnnotation");
  804. case "empty":
  805. return this.finishNode(node, "EmptyTypeAnnotation");
  806. case "number":
  807. return this.finishNode(node, "NumberTypeAnnotation");
  808. case "string":
  809. return this.finishNode(node, "StringTypeAnnotation");
  810. default:
  811. this.checkNotUnderscore(id.name);
  812. return this.flowParseGenericType(startPos, startLoc, id);
  813. }
  814. }
  815. flowParsePrimaryType() {
  816. const startPos = this.state.start;
  817. const startLoc = this.state.startLoc;
  818. const node = this.startNode();
  819. let tmp;
  820. let type;
  821. let isGroupedType = false;
  822. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  823. switch (this.state.type) {
  824. case _types.types.name:
  825. if (this.isContextual("interface")) {
  826. return this.flowParseInterfaceType();
  827. }
  828. return this.flowIdentToTypeAnnotation(startPos, startLoc, node, this.parseIdentifier());
  829. case _types.types.braceL:
  830. return this.flowParseObjectType({
  831. allowStatic: false,
  832. allowExact: false,
  833. allowSpread: true,
  834. allowProto: false,
  835. allowInexact: true
  836. });
  837. case _types.types.braceBarL:
  838. return this.flowParseObjectType({
  839. allowStatic: false,
  840. allowExact: true,
  841. allowSpread: true,
  842. allowProto: false,
  843. allowInexact: false
  844. });
  845. case _types.types.bracketL:
  846. this.state.noAnonFunctionType = false;
  847. type = this.flowParseTupleType();
  848. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  849. return type;
  850. case _types.types.relational:
  851. if (this.state.value === "<") {
  852. node.typeParameters = this.flowParseTypeParameterDeclaration();
  853. this.expect(_types.types.parenL);
  854. tmp = this.flowParseFunctionTypeParams();
  855. node.params = tmp.params;
  856. node.rest = tmp.rest;
  857. this.expect(_types.types.parenR);
  858. this.expect(_types.types.arrow);
  859. node.returnType = this.flowParseType();
  860. return this.finishNode(node, "FunctionTypeAnnotation");
  861. }
  862. break;
  863. case _types.types.parenL:
  864. this.next();
  865. if (!this.match(_types.types.parenR) && !this.match(_types.types.ellipsis)) {
  866. if (this.match(_types.types.name)) {
  867. const token = this.lookahead().type;
  868. isGroupedType = token !== _types.types.question && token !== _types.types.colon;
  869. } else {
  870. isGroupedType = true;
  871. }
  872. }
  873. if (isGroupedType) {
  874. this.state.noAnonFunctionType = false;
  875. type = this.flowParseType();
  876. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  877. if (this.state.noAnonFunctionType || !(this.match(_types.types.comma) || this.match(_types.types.parenR) && this.lookahead().type === _types.types.arrow)) {
  878. this.expect(_types.types.parenR);
  879. return type;
  880. } else {
  881. this.eat(_types.types.comma);
  882. }
  883. }
  884. if (type) {
  885. tmp = this.flowParseFunctionTypeParams([this.reinterpretTypeAsFunctionTypeParam(type)]);
  886. } else {
  887. tmp = this.flowParseFunctionTypeParams();
  888. }
  889. node.params = tmp.params;
  890. node.rest = tmp.rest;
  891. this.expect(_types.types.parenR);
  892. this.expect(_types.types.arrow);
  893. node.returnType = this.flowParseType();
  894. node.typeParameters = null;
  895. return this.finishNode(node, "FunctionTypeAnnotation");
  896. case _types.types.string:
  897. return this.parseLiteral(this.state.value, "StringLiteralTypeAnnotation");
  898. case _types.types._true:
  899. case _types.types._false:
  900. node.value = this.match(_types.types._true);
  901. this.next();
  902. return this.finishNode(node, "BooleanLiteralTypeAnnotation");
  903. case _types.types.plusMin:
  904. if (this.state.value === "-") {
  905. this.next();
  906. if (this.match(_types.types.num)) {
  907. return this.parseLiteral(-this.state.value, "NumberLiteralTypeAnnotation", node.start, node.loc.start);
  908. }
  909. if (this.match(_types.types.bigint)) {
  910. return this.parseLiteral(-this.state.value, "BigIntLiteralTypeAnnotation", node.start, node.loc.start);
  911. }
  912. throw this.raise(this.state.start, `Unexpected token, expected "number" or "bigint"`);
  913. }
  914. this.unexpected();
  915. case _types.types.num:
  916. return this.parseLiteral(this.state.value, "NumberLiteralTypeAnnotation");
  917. case _types.types.bigint:
  918. return this.parseLiteral(this.state.value, "BigIntLiteralTypeAnnotation");
  919. case _types.types._void:
  920. this.next();
  921. return this.finishNode(node, "VoidTypeAnnotation");
  922. case _types.types._null:
  923. this.next();
  924. return this.finishNode(node, "NullLiteralTypeAnnotation");
  925. case _types.types._this:
  926. this.next();
  927. return this.finishNode(node, "ThisTypeAnnotation");
  928. case _types.types.star:
  929. this.next();
  930. return this.finishNode(node, "ExistsTypeAnnotation");
  931. default:
  932. if (this.state.type.keyword === "typeof") {
  933. return this.flowParseTypeofType();
  934. } else if (this.state.type.keyword) {
  935. const label = this.state.type.label;
  936. this.next();
  937. return super.createIdentifier(node, label);
  938. }
  939. }
  940. throw this.unexpected();
  941. }
  942. flowParsePostfixType() {
  943. const startPos = this.state.start,
  944. startLoc = this.state.startLoc;
  945. let type = this.flowParsePrimaryType();
  946. while (this.match(_types.types.bracketL) && !this.canInsertSemicolon()) {
  947. const node = this.startNodeAt(startPos, startLoc);
  948. node.elementType = type;
  949. this.expect(_types.types.bracketL);
  950. this.expect(_types.types.bracketR);
  951. type = this.finishNode(node, "ArrayTypeAnnotation");
  952. }
  953. return type;
  954. }
  955. flowParsePrefixType() {
  956. const node = this.startNode();
  957. if (this.eat(_types.types.question)) {
  958. node.typeAnnotation = this.flowParsePrefixType();
  959. return this.finishNode(node, "NullableTypeAnnotation");
  960. } else {
  961. return this.flowParsePostfixType();
  962. }
  963. }
  964. flowParseAnonFunctionWithoutParens() {
  965. const param = this.flowParsePrefixType();
  966. if (!this.state.noAnonFunctionType && this.eat(_types.types.arrow)) {
  967. const node = this.startNodeAt(param.start, param.loc.start);
  968. node.params = [this.reinterpretTypeAsFunctionTypeParam(param)];
  969. node.rest = null;
  970. node.returnType = this.flowParseType();
  971. node.typeParameters = null;
  972. return this.finishNode(node, "FunctionTypeAnnotation");
  973. }
  974. return param;
  975. }
  976. flowParseIntersectionType() {
  977. const node = this.startNode();
  978. this.eat(_types.types.bitwiseAND);
  979. const type = this.flowParseAnonFunctionWithoutParens();
  980. node.types = [type];
  981. while (this.eat(_types.types.bitwiseAND)) {
  982. node.types.push(this.flowParseAnonFunctionWithoutParens());
  983. }
  984. return node.types.length === 1 ? type : this.finishNode(node, "IntersectionTypeAnnotation");
  985. }
  986. flowParseUnionType() {
  987. const node = this.startNode();
  988. this.eat(_types.types.bitwiseOR);
  989. const type = this.flowParseIntersectionType();
  990. node.types = [type];
  991. while (this.eat(_types.types.bitwiseOR)) {
  992. node.types.push(this.flowParseIntersectionType());
  993. }
  994. return node.types.length === 1 ? type : this.finishNode(node, "UnionTypeAnnotation");
  995. }
  996. flowParseType() {
  997. const oldInType = this.state.inType;
  998. this.state.inType = true;
  999. const type = this.flowParseUnionType();
  1000. this.state.inType = oldInType;
  1001. this.state.exprAllowed = this.state.exprAllowed || this.state.noAnonFunctionType;
  1002. return type;
  1003. }
  1004. flowParseTypeOrImplicitInstantiation() {
  1005. if (this.state.type === _types.types.name && this.state.value === "_") {
  1006. const startPos = this.state.start;
  1007. const startLoc = this.state.startLoc;
  1008. const node = this.parseIdentifier();
  1009. return this.flowParseGenericType(startPos, startLoc, node);
  1010. } else {
  1011. return this.flowParseType();
  1012. }
  1013. }
  1014. flowParseTypeAnnotation() {
  1015. const node = this.startNode();
  1016. node.typeAnnotation = this.flowParseTypeInitialiser();
  1017. return this.finishNode(node, "TypeAnnotation");
  1018. }
  1019. flowParseTypeAnnotatableIdentifier(allowPrimitiveOverride) {
  1020. const ident = allowPrimitiveOverride ? this.parseIdentifier() : this.flowParseRestrictedIdentifier();
  1021. if (this.match(_types.types.colon)) {
  1022. ident.typeAnnotation = this.flowParseTypeAnnotation();
  1023. this.resetEndLocation(ident);
  1024. }
  1025. return ident;
  1026. }
  1027. typeCastToParameter(node) {
  1028. node.expression.typeAnnotation = node.typeAnnotation;
  1029. this.resetEndLocation(node.expression, node.typeAnnotation.end, node.typeAnnotation.loc.end);
  1030. return node.expression;
  1031. }
  1032. flowParseVariance() {
  1033. let variance = null;
  1034. if (this.match(_types.types.plusMin)) {
  1035. variance = this.startNode();
  1036. if (this.state.value === "+") {
  1037. variance.kind = "plus";
  1038. } else {
  1039. variance.kind = "minus";
  1040. }
  1041. this.next();
  1042. this.finishNode(variance, "Variance");
  1043. }
  1044. return variance;
  1045. }
  1046. parseFunctionBody(node, allowExpressionBody, isMethod = false) {
  1047. if (allowExpressionBody) {
  1048. return this.forwardNoArrowParamsConversionAt(node, () => super.parseFunctionBody(node, true, isMethod));
  1049. }
  1050. return super.parseFunctionBody(node, false, isMethod);
  1051. }
  1052. parseFunctionBodyAndFinish(node, type, isMethod = false) {
  1053. if (this.match(_types.types.colon)) {
  1054. const typeNode = this.startNode();
  1055. [typeNode.typeAnnotation, node.predicate] = this.flowParseTypeAndPredicateInitialiser();
  1056. node.returnType = typeNode.typeAnnotation ? this.finishNode(typeNode, "TypeAnnotation") : null;
  1057. }
  1058. super.parseFunctionBodyAndFinish(node, type, isMethod);
  1059. }
  1060. parseStatement(context, topLevel) {
  1061. if (this.state.strict && this.match(_types.types.name) && this.state.value === "interface") {
  1062. const node = this.startNode();
  1063. this.next();
  1064. return this.flowParseInterface(node);
  1065. } else if (this.shouldParseEnums() && this.isContextual("enum")) {
  1066. const node = this.startNode();
  1067. this.next();
  1068. return this.flowParseEnumDeclaration(node);
  1069. } else {
  1070. const stmt = super.parseStatement(context, topLevel);
  1071. if (this.flowPragma === undefined && !this.isValidDirective(stmt)) {
  1072. this.flowPragma = null;
  1073. }
  1074. return stmt;
  1075. }
  1076. }
  1077. parseExpressionStatement(node, expr) {
  1078. if (expr.type === "Identifier") {
  1079. if (expr.name === "declare") {
  1080. if (this.match(_types.types._class) || this.match(_types.types.name) || this.match(_types.types._function) || this.match(_types.types._var) || this.match(_types.types._export)) {
  1081. return this.flowParseDeclare(node);
  1082. }
  1083. } else if (this.match(_types.types.name)) {
  1084. if (expr.name === "interface") {
  1085. return this.flowParseInterface(node);
  1086. } else if (expr.name === "type") {
  1087. return this.flowParseTypeAlias(node);
  1088. } else if (expr.name === "opaque") {
  1089. return this.flowParseOpaqueType(node, false);
  1090. }
  1091. }
  1092. }
  1093. return super.parseExpressionStatement(node, expr);
  1094. }
  1095. shouldParseExportDeclaration() {
  1096. return this.isContextual("type") || this.isContextual("interface") || this.isContextual("opaque") || this.shouldParseEnums() && this.isContextual("enum") || super.shouldParseExportDeclaration();
  1097. }
  1098. isExportDefaultSpecifier() {
  1099. if (this.match(_types.types.name) && (this.state.value === "type" || this.state.value === "interface" || this.state.value === "opaque" || this.shouldParseEnums() && this.state.value === "enum")) {
  1100. return false;
  1101. }
  1102. return super.isExportDefaultSpecifier();
  1103. }
  1104. parseExportDefaultExpression() {
  1105. if (this.shouldParseEnums() && this.isContextual("enum")) {
  1106. const node = this.startNode();
  1107. this.next();
  1108. return this.flowParseEnumDeclaration(node);
  1109. }
  1110. return super.parseExportDefaultExpression();
  1111. }
  1112. parseConditional(expr, noIn, startPos, startLoc, refNeedsArrowPos) {
  1113. if (!this.match(_types.types.question)) return expr;
  1114. if (refNeedsArrowPos) {
  1115. const result = this.tryParse(() => super.parseConditional(expr, noIn, startPos, startLoc));
  1116. if (!result.node) {
  1117. refNeedsArrowPos.start = result.error.pos || this.state.start;
  1118. return expr;
  1119. }
  1120. if (result.error) this.state = result.failState;
  1121. return result.node;
  1122. }
  1123. this.expect(_types.types.question);
  1124. const state = this.state.clone();
  1125. const originalNoArrowAt = this.state.noArrowAt;
  1126. const node = this.startNodeAt(startPos, startLoc);
  1127. let {
  1128. consequent,
  1129. failed
  1130. } = this.tryParseConditionalConsequent();
  1131. let [valid, invalid] = this.getArrowLikeExpressions(consequent);
  1132. if (failed || invalid.length > 0) {
  1133. const noArrowAt = [...originalNoArrowAt];
  1134. if (invalid.length > 0) {
  1135. this.state = state;
  1136. this.state.noArrowAt = noArrowAt;
  1137. for (let i = 0; i < invalid.length; i++) {
  1138. noArrowAt.push(invalid[i].start);
  1139. }
  1140. ({
  1141. consequent,
  1142. failed
  1143. } = this.tryParseConditionalConsequent());
  1144. [valid, invalid] = this.getArrowLikeExpressions(consequent);
  1145. }
  1146. if (failed && valid.length > 1) {
  1147. this.raise(state.start, "Ambiguous expression: wrap the arrow functions in parentheses to disambiguate.");
  1148. }
  1149. if (failed && valid.length === 1) {
  1150. this.state = state;
  1151. this.state.noArrowAt = noArrowAt.concat(valid[0].start);
  1152. ({
  1153. consequent,
  1154. failed
  1155. } = this.tryParseConditionalConsequent());
  1156. }
  1157. }
  1158. this.getArrowLikeExpressions(consequent, true);
  1159. this.state.noArrowAt = originalNoArrowAt;
  1160. this.expect(_types.types.colon);
  1161. node.test = expr;
  1162. node.consequent = consequent;
  1163. node.alternate = this.forwardNoArrowParamsConversionAt(node, () => this.parseMaybeAssign(noIn, undefined, undefined, undefined));
  1164. return this.finishNode(node, "ConditionalExpression");
  1165. }
  1166. tryParseConditionalConsequent() {
  1167. this.state.noArrowParamsConversionAt.push(this.state.start);
  1168. const consequent = this.parseMaybeAssign();
  1169. const failed = !this.match(_types.types.colon);
  1170. this.state.noArrowParamsConversionAt.pop();
  1171. return {
  1172. consequent,
  1173. failed
  1174. };
  1175. }
  1176. getArrowLikeExpressions(node, disallowInvalid) {
  1177. const stack = [node];
  1178. const arrows = [];
  1179. while (stack.length !== 0) {
  1180. const node = stack.pop();
  1181. if (node.type === "ArrowFunctionExpression") {
  1182. if (node.typeParameters || !node.returnType) {
  1183. this.finishArrowValidation(node);
  1184. } else {
  1185. arrows.push(node);
  1186. }
  1187. stack.push(node.body);
  1188. } else if (node.type === "ConditionalExpression") {
  1189. stack.push(node.consequent);
  1190. stack.push(node.alternate);
  1191. }
  1192. }
  1193. if (disallowInvalid) {
  1194. arrows.forEach(node => this.finishArrowValidation(node));
  1195. return [arrows, []];
  1196. }
  1197. return partition(arrows, node => node.params.every(param => this.isAssignable(param, true)));
  1198. }
  1199. finishArrowValidation(node) {
  1200. var _node$extra;
  1201. this.toAssignableList(node.params, (_node$extra = node.extra) == null ? void 0 : _node$extra.trailingComma);
  1202. this.scope.enter((0, _scopeflags.functionFlags)(false, false) | _scopeflags.SCOPE_ARROW);
  1203. super.checkParams(node, false, true);
  1204. this.scope.exit();
  1205. }
  1206. forwardNoArrowParamsConversionAt(node, parse) {
  1207. let result;
  1208. if (this.state.noArrowParamsConversionAt.indexOf(node.start) !== -1) {
  1209. this.state.noArrowParamsConversionAt.push(this.state.start);
  1210. result = parse();
  1211. this.state.noArrowParamsConversionAt.pop();
  1212. } else {
  1213. result = parse();
  1214. }
  1215. return result;
  1216. }
  1217. parseParenItem(node, startPos, startLoc) {
  1218. node = super.parseParenItem(node, startPos, startLoc);
  1219. if (this.eat(_types.types.question)) {
  1220. node.optional = true;
  1221. this.resetEndLocation(node);
  1222. }
  1223. if (this.match(_types.types.colon)) {
  1224. const typeCastNode = this.startNodeAt(startPos, startLoc);
  1225. typeCastNode.expression = node;
  1226. typeCastNode.typeAnnotation = this.flowParseTypeAnnotation();
  1227. return this.finishNode(typeCastNode, "TypeCastExpression");
  1228. }
  1229. return node;
  1230. }
  1231. assertModuleNodeAllowed(node) {
  1232. if (node.type === "ImportDeclaration" && (node.importKind === "type" || node.importKind === "typeof") || node.type === "ExportNamedDeclaration" && node.exportKind === "type" || node.type === "ExportAllDeclaration" && node.exportKind === "type") {
  1233. return;
  1234. }
  1235. super.assertModuleNodeAllowed(node);
  1236. }
  1237. parseExport(node) {
  1238. const decl = super.parseExport(node);
  1239. if (decl.type === "ExportNamedDeclaration" || decl.type === "ExportAllDeclaration") {
  1240. decl.exportKind = decl.exportKind || "value";
  1241. }
  1242. return decl;
  1243. }
  1244. parseExportDeclaration(node) {
  1245. if (this.isContextual("type")) {
  1246. node.exportKind = "type";
  1247. const declarationNode = this.startNode();
  1248. this.next();
  1249. if (this.match(_types.types.braceL)) {
  1250. node.specifiers = this.parseExportSpecifiers();
  1251. this.parseExportFrom(node);
  1252. return null;
  1253. } else {
  1254. return this.flowParseTypeAlias(declarationNode);
  1255. }
  1256. } else if (this.isContextual("opaque")) {
  1257. node.exportKind = "type";
  1258. const declarationNode = this.startNode();
  1259. this.next();
  1260. return this.flowParseOpaqueType(declarationNode, false);
  1261. } else if (this.isContextual("interface")) {
  1262. node.exportKind = "type";
  1263. const declarationNode = this.startNode();
  1264. this.next();
  1265. return this.flowParseInterface(declarationNode);
  1266. } else if (this.shouldParseEnums() && this.isContextual("enum")) {
  1267. node.exportKind = "value";
  1268. const declarationNode = this.startNode();
  1269. this.next();
  1270. return this.flowParseEnumDeclaration(declarationNode);
  1271. } else {
  1272. return super.parseExportDeclaration(node);
  1273. }
  1274. }
  1275. eatExportStar(node) {
  1276. if (super.eatExportStar(...arguments)) return true;
  1277. if (this.isContextual("type") && this.lookahead().type === _types.types.star) {
  1278. node.exportKind = "type";
  1279. this.next();
  1280. this.next();
  1281. return true;
  1282. }
  1283. return false;
  1284. }
  1285. maybeParseExportNamespaceSpecifier(node) {
  1286. const pos = this.state.start;
  1287. const hasNamespace = super.maybeParseExportNamespaceSpecifier(node);
  1288. if (hasNamespace && node.exportKind === "type") {
  1289. this.unexpected(pos);
  1290. }
  1291. return hasNamespace;
  1292. }
  1293. parseClassId(node, isStatement, optionalId) {
  1294. super.parseClassId(node, isStatement, optionalId);
  1295. if (this.isRelational("<")) {
  1296. node.typeParameters = this.flowParseTypeParameterDeclaration();
  1297. }
  1298. }
  1299. getTokenFromCode(code) {
  1300. const next = this.input.charCodeAt(this.state.pos + 1);
  1301. if (code === 123 && next === 124) {
  1302. return this.finishOp(_types.types.braceBarL, 2);
  1303. } else if (this.state.inType && (code === 62 || code === 60)) {
  1304. return this.finishOp(_types.types.relational, 1);
  1305. } else if ((0, _identifier.isIteratorStart)(code, next)) {
  1306. this.state.isIterator = true;
  1307. return super.readWord();
  1308. } else {
  1309. return super.getTokenFromCode(code);
  1310. }
  1311. }
  1312. isAssignable(node, isBinding) {
  1313. switch (node.type) {
  1314. case "Identifier":
  1315. case "ObjectPattern":
  1316. case "ArrayPattern":
  1317. case "AssignmentPattern":
  1318. return true;
  1319. case "ObjectExpression":
  1320. {
  1321. const last = node.properties.length - 1;
  1322. return node.properties.every((prop, i) => {
  1323. return prop.type !== "ObjectMethod" && (i === last || prop.type === "SpreadElement") && this.isAssignable(prop);
  1324. });
  1325. }
  1326. case "ObjectProperty":
  1327. return this.isAssignable(node.value);
  1328. case "SpreadElement":
  1329. return this.isAssignable(node.argument);
  1330. case "ArrayExpression":
  1331. return node.elements.every(element => this.isAssignable(element));
  1332. case "AssignmentExpression":
  1333. return node.operator === "=";
  1334. case "ParenthesizedExpression":
  1335. case "TypeCastExpression":
  1336. return this.isAssignable(node.expression);
  1337. case "MemberExpression":
  1338. case "OptionalMemberExpression":
  1339. return !isBinding;
  1340. default:
  1341. return false;
  1342. }
  1343. }
  1344. toAssignable(node) {
  1345. if (node.type === "TypeCastExpression") {
  1346. return super.toAssignable(this.typeCastToParameter(node));
  1347. } else {
  1348. return super.toAssignable(node);
  1349. }
  1350. }
  1351. toAssignableList(exprList, trailingCommaPos) {
  1352. for (let i = 0; i < exprList.length; i++) {
  1353. const expr = exprList[i];
  1354. if (expr && expr.type === "TypeCastExpression") {
  1355. exprList[i] = this.typeCastToParameter(expr);
  1356. }
  1357. }
  1358. return super.toAssignableList(exprList, trailingCommaPos);
  1359. }
  1360. toReferencedList(exprList, isParenthesizedExpr) {
  1361. for (let i = 0; i < exprList.length; i++) {
  1362. const expr = exprList[i];
  1363. if (expr && expr.type === "TypeCastExpression" && (!expr.extra || !expr.extra.parenthesized) && (exprList.length > 1 || !isParenthesizedExpr)) {
  1364. this.raise(expr.typeAnnotation.start, "The type cast expression is expected to be wrapped with parenthesis");
  1365. }
  1366. }
  1367. return exprList;
  1368. }
  1369. checkLVal(expr, bindingType = _scopeflags.BIND_NONE, checkClashes, contextDescription) {
  1370. if (expr.type !== "TypeCastExpression") {
  1371. return super.checkLVal(expr, bindingType, checkClashes, contextDescription);
  1372. }
  1373. }
  1374. parseClassProperty(node) {
  1375. if (this.match(_types.types.colon)) {
  1376. node.typeAnnotation = this.flowParseTypeAnnotation();
  1377. }
  1378. return super.parseClassProperty(node);
  1379. }
  1380. parseClassPrivateProperty(node) {
  1381. if (this.match(_types.types.colon)) {
  1382. node.typeAnnotation = this.flowParseTypeAnnotation();
  1383. }
  1384. return super.parseClassPrivateProperty(node);
  1385. }
  1386. isClassMethod() {
  1387. return this.isRelational("<") || super.isClassMethod();
  1388. }
  1389. isClassProperty() {
  1390. return this.match(_types.types.colon) || super.isClassProperty();
  1391. }
  1392. isNonstaticConstructor(method) {
  1393. return !this.match(_types.types.colon) && super.isNonstaticConstructor(method);
  1394. }
  1395. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  1396. if (method.variance) {
  1397. this.unexpected(method.variance.start);
  1398. }
  1399. delete method.variance;
  1400. if (this.isRelational("<")) {
  1401. method.typeParameters = this.flowParseTypeParameterDeclaration();
  1402. }
  1403. super.pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper);
  1404. }
  1405. pushClassPrivateMethod(classBody, method, isGenerator, isAsync) {
  1406. if (method.variance) {
  1407. this.unexpected(method.variance.start);
  1408. }
  1409. delete method.variance;
  1410. if (this.isRelational("<")) {
  1411. method.typeParameters = this.flowParseTypeParameterDeclaration();
  1412. }
  1413. super.pushClassPrivateMethod(classBody, method, isGenerator, isAsync);
  1414. }
  1415. parseClassSuper(node) {
  1416. super.parseClassSuper(node);
  1417. if (node.superClass && this.isRelational("<")) {
  1418. node.superTypeParameters = this.flowParseTypeParameterInstantiation();
  1419. }
  1420. if (this.isContextual("implements")) {
  1421. this.next();
  1422. const implemented = node.implements = [];
  1423. do {
  1424. const node = this.startNode();
  1425. node.id = this.flowParseRestrictedIdentifier(true);
  1426. if (this.isRelational("<")) {
  1427. node.typeParameters = this.flowParseTypeParameterInstantiation();
  1428. } else {
  1429. node.typeParameters = null;
  1430. }
  1431. implemented.push(this.finishNode(node, "ClassImplements"));
  1432. } while (this.eat(_types.types.comma));
  1433. }
  1434. }
  1435. parsePropertyName(node, isPrivateNameAllowed) {
  1436. const variance = this.flowParseVariance();
  1437. const key = super.parsePropertyName(node, isPrivateNameAllowed);
  1438. node.variance = variance;
  1439. return key;
  1440. }
  1441. parseObjPropValue(prop, startPos, startLoc, isGenerator, isAsync, isPattern, refExpressionErrors, containsEsc) {
  1442. if (prop.variance) {
  1443. this.unexpected(prop.variance.start);
  1444. }
  1445. delete prop.variance;
  1446. let typeParameters;
  1447. if (this.isRelational("<")) {
  1448. typeParameters = this.flowParseTypeParameterDeclaration();
  1449. if (!this.match(_types.types.parenL)) this.unexpected();
  1450. }
  1451. super.parseObjPropValue(prop, startPos, startLoc, isGenerator, isAsync, isPattern, refExpressionErrors, containsEsc);
  1452. if (typeParameters) {
  1453. (prop.value || prop).typeParameters = typeParameters;
  1454. }
  1455. }
  1456. parseAssignableListItemTypes(param) {
  1457. if (this.eat(_types.types.question)) {
  1458. if (param.type !== "Identifier") {
  1459. this.raise(param.start, "A binding pattern parameter cannot be optional in an implementation signature.");
  1460. }
  1461. param.optional = true;
  1462. }
  1463. if (this.match(_types.types.colon)) {
  1464. param.typeAnnotation = this.flowParseTypeAnnotation();
  1465. }
  1466. this.resetEndLocation(param);
  1467. return param;
  1468. }
  1469. parseMaybeDefault(startPos, startLoc, left) {
  1470. const node = super.parseMaybeDefault(startPos, startLoc, left);
  1471. if (node.type === "AssignmentPattern" && node.typeAnnotation && node.right.start < node.typeAnnotation.start) {
  1472. this.raise(node.typeAnnotation.start, "Type annotations must come before default assignments, " + "e.g. instead of `age = 25: number` use `age: number = 25`");
  1473. }
  1474. return node;
  1475. }
  1476. shouldParseDefaultImport(node) {
  1477. if (!hasTypeImportKind(node)) {
  1478. return super.shouldParseDefaultImport(node);
  1479. }
  1480. return isMaybeDefaultImport(this.state);
  1481. }
  1482. parseImportSpecifierLocal(node, specifier, type, contextDescription) {
  1483. specifier.local = hasTypeImportKind(node) ? this.flowParseRestrictedIdentifier(true, true) : this.parseIdentifier();
  1484. this.checkLVal(specifier.local, _scopeflags.BIND_LEXICAL, undefined, contextDescription);
  1485. node.specifiers.push(this.finishNode(specifier, type));
  1486. }
  1487. maybeParseDefaultImportSpecifier(node) {
  1488. node.importKind = "value";
  1489. let kind = null;
  1490. if (this.match(_types.types._typeof)) {
  1491. kind = "typeof";
  1492. } else if (this.isContextual("type")) {
  1493. kind = "type";
  1494. }
  1495. if (kind) {
  1496. const lh = this.lookahead();
  1497. if (kind === "type" && lh.type === _types.types.star) {
  1498. this.unexpected(lh.start);
  1499. }
  1500. if (isMaybeDefaultImport(lh) || lh.type === _types.types.braceL || lh.type === _types.types.star) {
  1501. this.next();
  1502. node.importKind = kind;
  1503. }
  1504. }
  1505. return super.maybeParseDefaultImportSpecifier(node);
  1506. }
  1507. parseImportSpecifier(node) {
  1508. const specifier = this.startNode();
  1509. const firstIdentLoc = this.state.start;
  1510. const firstIdent = this.parseIdentifier(true);
  1511. let specifierTypeKind = null;
  1512. if (firstIdent.name === "type") {
  1513. specifierTypeKind = "type";
  1514. } else if (firstIdent.name === "typeof") {
  1515. specifierTypeKind = "typeof";
  1516. }
  1517. let isBinding = false;
  1518. if (this.isContextual("as") && !this.isLookaheadContextual("as")) {
  1519. const as_ident = this.parseIdentifier(true);
  1520. if (specifierTypeKind !== null && !this.match(_types.types.name) && !this.state.type.keyword) {
  1521. specifier.imported = as_ident;
  1522. specifier.importKind = specifierTypeKind;
  1523. specifier.local = as_ident.__clone();
  1524. } else {
  1525. specifier.imported = firstIdent;
  1526. specifier.importKind = null;
  1527. specifier.local = this.parseIdentifier();
  1528. }
  1529. } else if (specifierTypeKind !== null && (this.match(_types.types.name) || this.state.type.keyword)) {
  1530. specifier.imported = this.parseIdentifier(true);
  1531. specifier.importKind = specifierTypeKind;
  1532. if (this.eatContextual("as")) {
  1533. specifier.local = this.parseIdentifier();
  1534. } else {
  1535. isBinding = true;
  1536. specifier.local = specifier.imported.__clone();
  1537. }
  1538. } else {
  1539. isBinding = true;
  1540. specifier.imported = firstIdent;
  1541. specifier.importKind = null;
  1542. specifier.local = specifier.imported.__clone();
  1543. }
  1544. const nodeIsTypeImport = hasTypeImportKind(node);
  1545. const specifierIsTypeImport = hasTypeImportKind(specifier);
  1546. if (nodeIsTypeImport && specifierIsTypeImport) {
  1547. this.raise(firstIdentLoc, "The `type` and `typeof` keywords on named imports can only be used on regular " + "`import` statements. It cannot be used with `import type` or `import typeof` statements");
  1548. }
  1549. if (nodeIsTypeImport || specifierIsTypeImport) {
  1550. this.checkReservedType(specifier.local.name, specifier.local.start, true);
  1551. }
  1552. if (isBinding && !nodeIsTypeImport && !specifierIsTypeImport) {
  1553. this.checkReservedWord(specifier.local.name, specifier.start, true, true);
  1554. }
  1555. this.checkLVal(specifier.local, _scopeflags.BIND_LEXICAL, undefined, "import specifier");
  1556. node.specifiers.push(this.finishNode(specifier, "ImportSpecifier"));
  1557. }
  1558. parseFunctionParams(node, allowModifiers) {
  1559. const kind = node.kind;
  1560. if (kind !== "get" && kind !== "set" && this.isRelational("<")) {
  1561. node.typeParameters = this.flowParseTypeParameterDeclaration();
  1562. }
  1563. super.parseFunctionParams(node, allowModifiers);
  1564. }
  1565. parseVarId(decl, kind) {
  1566. super.parseVarId(decl, kind);
  1567. if (this.match(_types.types.colon)) {
  1568. decl.id.typeAnnotation = this.flowParseTypeAnnotation();
  1569. this.resetEndLocation(decl.id);
  1570. }
  1571. }
  1572. parseAsyncArrowFromCallExpression(node, call) {
  1573. if (this.match(_types.types.colon)) {
  1574. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  1575. this.state.noAnonFunctionType = true;
  1576. node.returnType = this.flowParseTypeAnnotation();
  1577. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  1578. }
  1579. return super.parseAsyncArrowFromCallExpression(node, call);
  1580. }
  1581. shouldParseAsyncArrow() {
  1582. return this.match(_types.types.colon) || super.shouldParseAsyncArrow();
  1583. }
  1584. parseMaybeAssign(noIn, refExpressionErrors, afterLeftParse, refNeedsArrowPos) {
  1585. let state = null;
  1586. let jsx;
  1587. if (this.hasPlugin("jsx") && (this.match(_types.types.jsxTagStart) || this.isRelational("<"))) {
  1588. state = this.state.clone();
  1589. jsx = this.tryParse(() => super.parseMaybeAssign(noIn, refExpressionErrors, afterLeftParse, refNeedsArrowPos), state);
  1590. if (!jsx.error) return jsx.node;
  1591. const {
  1592. context
  1593. } = this.state;
  1594. if (context[context.length - 1] === _context.types.j_oTag) {
  1595. context.length -= 2;
  1596. } else if (context[context.length - 1] === _context.types.j_expr) {
  1597. context.length -= 1;
  1598. }
  1599. }
  1600. if (jsx && jsx.error || this.isRelational("<")) {
  1601. state = state || this.state.clone();
  1602. let typeParameters;
  1603. const arrow = this.tryParse(() => {
  1604. typeParameters = this.flowParseTypeParameterDeclaration();
  1605. const arrowExpression = this.forwardNoArrowParamsConversionAt(typeParameters, () => super.parseMaybeAssign(noIn, refExpressionErrors, afterLeftParse, refNeedsArrowPos));
  1606. arrowExpression.typeParameters = typeParameters;
  1607. this.resetStartLocationFromNode(arrowExpression, typeParameters);
  1608. return arrowExpression;
  1609. }, state);
  1610. const arrowExpression = arrow.node && arrow.node.type === "ArrowFunctionExpression" ? arrow.node : null;
  1611. if (!arrow.error && arrowExpression) return arrowExpression;
  1612. if (jsx && jsx.node) {
  1613. this.state = jsx.failState;
  1614. return jsx.node;
  1615. }
  1616. if (arrowExpression) {
  1617. this.state = arrow.failState;
  1618. return arrowExpression;
  1619. }
  1620. if (jsx && jsx.thrown) throw jsx.error;
  1621. if (arrow.thrown) throw arrow.error;
  1622. throw this.raise(typeParameters.start, "Expected an arrow function after this type parameter declaration");
  1623. }
  1624. return super.parseMaybeAssign(noIn, refExpressionErrors, afterLeftParse, refNeedsArrowPos);
  1625. }
  1626. parseArrow(node) {
  1627. if (this.match(_types.types.colon)) {
  1628. const result = this.tryParse(() => {
  1629. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  1630. this.state.noAnonFunctionType = true;
  1631. const typeNode = this.startNode();
  1632. [typeNode.typeAnnotation, node.predicate] = this.flowParseTypeAndPredicateInitialiser();
  1633. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  1634. if (this.canInsertSemicolon()) this.unexpected();
  1635. if (!this.match(_types.types.arrow)) this.unexpected();
  1636. return typeNode;
  1637. });
  1638. if (result.thrown) return null;
  1639. if (result.error) this.state = result.failState;
  1640. node.returnType = result.node.typeAnnotation ? this.finishNode(result.node, "TypeAnnotation") : null;
  1641. }
  1642. return super.parseArrow(node);
  1643. }
  1644. shouldParseArrow() {
  1645. return this.match(_types.types.colon) || super.shouldParseArrow();
  1646. }
  1647. setArrowFunctionParameters(node, params) {
  1648. if (this.state.noArrowParamsConversionAt.indexOf(node.start) !== -1) {
  1649. node.params = params;
  1650. } else {
  1651. super.setArrowFunctionParameters(node, params);
  1652. }
  1653. }
  1654. checkParams(node, allowDuplicates, isArrowFunction) {
  1655. if (isArrowFunction && this.state.noArrowParamsConversionAt.indexOf(node.start) !== -1) {
  1656. return;
  1657. }
  1658. return super.checkParams(...arguments);
  1659. }
  1660. parseParenAndDistinguishExpression(canBeArrow) {
  1661. return super.parseParenAndDistinguishExpression(canBeArrow && this.state.noArrowAt.indexOf(this.state.start) === -1);
  1662. }
  1663. parseSubscripts(base, startPos, startLoc, noCalls) {
  1664. if (base.type === "Identifier" && base.name === "async" && this.state.noArrowAt.indexOf(startPos) !== -1) {
  1665. this.next();
  1666. const node = this.startNodeAt(startPos, startLoc);
  1667. node.callee = base;
  1668. node.arguments = this.parseCallExpressionArguments(_types.types.parenR, false);
  1669. base = this.finishNode(node, "CallExpression");
  1670. } else if (base.type === "Identifier" && base.name === "async" && this.isRelational("<")) {
  1671. const state = this.state.clone();
  1672. const arrow = this.tryParse(abort => this.parseAsyncArrowWithTypeParameters(startPos, startLoc) || abort(), state);
  1673. if (!arrow.error && !arrow.aborted) return arrow.node;
  1674. const result = this.tryParse(() => super.parseSubscripts(base, startPos, startLoc, noCalls), state);
  1675. if (result.node && !result.error) return result.node;
  1676. if (arrow.node) {
  1677. this.state = arrow.failState;
  1678. return arrow.node;
  1679. }
  1680. if (result.node) {
  1681. this.state = result.failState;
  1682. return result.node;
  1683. }
  1684. throw arrow.error || result.error;
  1685. }
  1686. return super.parseSubscripts(base, startPos, startLoc, noCalls);
  1687. }
  1688. parseSubscript(base, startPos, startLoc, noCalls, subscriptState) {
  1689. if (this.match(_types.types.questionDot) && this.isLookaheadRelational("<")) {
  1690. subscriptState.optionalChainMember = true;
  1691. if (noCalls) {
  1692. subscriptState.stop = true;
  1693. return base;
  1694. }
  1695. this.next();
  1696. const node = this.startNodeAt(startPos, startLoc);
  1697. node.callee = base;
  1698. node.typeArguments = this.flowParseTypeParameterInstantiation();
  1699. this.expect(_types.types.parenL);
  1700. node.arguments = this.parseCallExpressionArguments(_types.types.parenR, false);
  1701. node.optional = true;
  1702. return this.finishCallExpression(node, true);
  1703. } else if (!noCalls && this.shouldParseTypes() && this.isRelational("<")) {
  1704. const node = this.startNodeAt(startPos, startLoc);
  1705. node.callee = base;
  1706. const result = this.tryParse(() => {
  1707. node.typeArguments = this.flowParseTypeParameterInstantiationCallOrNew();
  1708. this.expect(_types.types.parenL);
  1709. node.arguments = this.parseCallExpressionArguments(_types.types.parenR, false);
  1710. if (subscriptState.optionalChainMember) node.optional = false;
  1711. return this.finishCallExpression(node, subscriptState.optionalChainMember);
  1712. });
  1713. if (result.node) {
  1714. if (result.error) this.state = result.failState;
  1715. return result.node;
  1716. }
  1717. }
  1718. return super.parseSubscript(base, startPos, startLoc, noCalls, subscriptState);
  1719. }
  1720. parseNewArguments(node) {
  1721. let targs = null;
  1722. if (this.shouldParseTypes() && this.isRelational("<")) {
  1723. targs = this.tryParse(() => this.flowParseTypeParameterInstantiationCallOrNew()).node;
  1724. }
  1725. node.typeArguments = targs;
  1726. super.parseNewArguments(node);
  1727. }
  1728. parseAsyncArrowWithTypeParameters(startPos, startLoc) {
  1729. const node = this.startNodeAt(startPos, startLoc);
  1730. this.parseFunctionParams(node);
  1731. if (!this.parseArrow(node)) return;
  1732. return this.parseArrowExpression(node, undefined, true);
  1733. }
  1734. readToken_mult_modulo(code) {
  1735. const next = this.input.charCodeAt(this.state.pos + 1);
  1736. if (code === 42 && next === 47 && this.state.hasFlowComment) {
  1737. this.state.hasFlowComment = false;
  1738. this.state.pos += 2;
  1739. this.nextToken();
  1740. return;
  1741. }
  1742. super.readToken_mult_modulo(code);
  1743. }
  1744. readToken_pipe_amp(code) {
  1745. const next = this.input.charCodeAt(this.state.pos + 1);
  1746. if (code === 124 && next === 125) {
  1747. this.finishOp(_types.types.braceBarR, 2);
  1748. return;
  1749. }
  1750. super.readToken_pipe_amp(code);
  1751. }
  1752. parseTopLevel(file, program) {
  1753. const fileNode = super.parseTopLevel(file, program);
  1754. if (this.state.hasFlowComment) {
  1755. this.raise(this.state.pos, "Unterminated flow-comment");
  1756. }
  1757. return fileNode;
  1758. }
  1759. skipBlockComment() {
  1760. if (this.hasPlugin("flowComments") && this.skipFlowComment()) {
  1761. if (this.state.hasFlowComment) {
  1762. this.unexpected(null, "Cannot have a flow comment inside another flow comment");
  1763. }
  1764. this.hasFlowCommentCompletion();
  1765. this.state.pos += this.skipFlowComment();
  1766. this.state.hasFlowComment = true;
  1767. return;
  1768. }
  1769. if (this.state.hasFlowComment) {
  1770. const end = this.input.indexOf("*-/", this.state.pos += 2);
  1771. if (end === -1) {
  1772. throw this.raise(this.state.pos - 2, "Unterminated comment");
  1773. }
  1774. this.state.pos = end + 3;
  1775. return;
  1776. }
  1777. super.skipBlockComment();
  1778. }
  1779. skipFlowComment() {
  1780. const {
  1781. pos
  1782. } = this.state;
  1783. let shiftToFirstNonWhiteSpace = 2;
  1784. while ([32, 9].includes(this.input.charCodeAt(pos + shiftToFirstNonWhiteSpace))) {
  1785. shiftToFirstNonWhiteSpace++;
  1786. }
  1787. const ch2 = this.input.charCodeAt(shiftToFirstNonWhiteSpace + pos);
  1788. const ch3 = this.input.charCodeAt(shiftToFirstNonWhiteSpace + pos + 1);
  1789. if (ch2 === 58 && ch3 === 58) {
  1790. return shiftToFirstNonWhiteSpace + 2;
  1791. }
  1792. if (this.input.slice(shiftToFirstNonWhiteSpace + pos, shiftToFirstNonWhiteSpace + pos + 12) === "flow-include") {
  1793. return shiftToFirstNonWhiteSpace + 12;
  1794. }
  1795. if (ch2 === 58 && ch3 !== 58) {
  1796. return shiftToFirstNonWhiteSpace;
  1797. }
  1798. return false;
  1799. }
  1800. hasFlowCommentCompletion() {
  1801. const end = this.input.indexOf("*/", this.state.pos);
  1802. if (end === -1) {
  1803. throw this.raise(this.state.pos, "Unterminated comment");
  1804. }
  1805. }
  1806. flowEnumErrorBooleanMemberNotInitialized(pos, {
  1807. enumName,
  1808. memberName
  1809. }) {
  1810. this.raise(pos, `Boolean enum members need to be initialized. Use either \`${memberName} = true,\` ` + `or \`${memberName} = false,\` in enum \`${enumName}\`.`);
  1811. }
  1812. flowEnumErrorInvalidMemberName(pos, {
  1813. enumName,
  1814. memberName
  1815. }) {
  1816. const suggestion = memberName[0].toUpperCase() + memberName.slice(1);
  1817. this.raise(pos, `Enum member names cannot start with lowercase 'a' through 'z'. Instead of using ` + `\`${memberName}\`, consider using \`${suggestion}\`, in enum \`${enumName}\`.`);
  1818. }
  1819. flowEnumErrorDuplicateMemberName(pos, {
  1820. enumName,
  1821. memberName
  1822. }) {
  1823. this.raise(pos, `Enum member names need to be unique, but the name \`${memberName}\` has already been used ` + `before in enum \`${enumName}\`.`);
  1824. }
  1825. flowEnumErrorInconsistentMemberValues(pos, {
  1826. enumName
  1827. }) {
  1828. this.raise(pos, `Enum \`${enumName}\` has inconsistent member initializers. Either use no initializers, or ` + `consistently use literals (either booleans, numbers, or strings) for all member initializers.`);
  1829. }
  1830. flowEnumErrorInvalidExplicitType(pos, {
  1831. enumName,
  1832. suppliedType
  1833. }) {
  1834. const suggestion = `Use one of \`boolean\`, \`number\`, \`string\`, or \`symbol\` in ` + `enum \`${enumName}\`.`;
  1835. const message = suppliedType === null ? `Supplied enum type is not valid. ${suggestion}` : `Enum type \`${suppliedType}\` is not valid. ${suggestion}`;
  1836. return this.raise(pos, message);
  1837. }
  1838. flowEnumErrorInvalidMemberInitializer(pos, {
  1839. enumName,
  1840. explicitType,
  1841. memberName
  1842. }) {
  1843. let message = null;
  1844. switch (explicitType) {
  1845. case "boolean":
  1846. case "number":
  1847. case "string":
  1848. message = `Enum \`${enumName}\` has type \`${explicitType}\`, so the initializer of ` + `\`${memberName}\` needs to be a ${explicitType} literal.`;
  1849. break;
  1850. case "symbol":
  1851. message = `Symbol enum members cannot be initialized. Use \`${memberName},\` in ` + `enum \`${enumName}\`.`;
  1852. break;
  1853. default:
  1854. message = `The enum member initializer for \`${memberName}\` needs to be a literal (either ` + `a boolean, number, or string) in enum \`${enumName}\`.`;
  1855. }
  1856. return this.raise(pos, message);
  1857. }
  1858. flowEnumErrorNumberMemberNotInitialized(pos, {
  1859. enumName,
  1860. memberName
  1861. }) {
  1862. this.raise(pos, `Number enum members need to be initialized, e.g. \`${memberName} = 1\` in enum \`${enumName}\`.`);
  1863. }
  1864. flowEnumErrorStringMemberInconsistentlyInitailized(pos, {
  1865. enumName
  1866. }) {
  1867. this.raise(pos, `String enum members need to consistently either all use initializers, or use no initializers, ` + `in enum \`${enumName}\`.`);
  1868. }
  1869. flowEnumMemberInit() {
  1870. const startPos = this.state.start;
  1871. const endOfInit = () => this.match(_types.types.comma) || this.match(_types.types.braceR);
  1872. switch (this.state.type) {
  1873. case _types.types.num:
  1874. {
  1875. const literal = this.parseLiteral(this.state.value, "NumericLiteral");
  1876. if (endOfInit()) {
  1877. return {
  1878. type: "number",
  1879. pos: literal.start,
  1880. value: literal
  1881. };
  1882. }
  1883. return {
  1884. type: "invalid",
  1885. pos: startPos
  1886. };
  1887. }
  1888. case _types.types.string:
  1889. {
  1890. const literal = this.parseLiteral(this.state.value, "StringLiteral");
  1891. if (endOfInit()) {
  1892. return {
  1893. type: "string",
  1894. pos: literal.start,
  1895. value: literal
  1896. };
  1897. }
  1898. return {
  1899. type: "invalid",
  1900. pos: startPos
  1901. };
  1902. }
  1903. case _types.types._true:
  1904. case _types.types._false:
  1905. {
  1906. const literal = this.parseBooleanLiteral();
  1907. if (endOfInit()) {
  1908. return {
  1909. type: "boolean",
  1910. pos: literal.start,
  1911. value: literal
  1912. };
  1913. }
  1914. return {
  1915. type: "invalid",
  1916. pos: startPos
  1917. };
  1918. }
  1919. default:
  1920. return {
  1921. type: "invalid",
  1922. pos: startPos
  1923. };
  1924. }
  1925. }
  1926. flowEnumMemberRaw() {
  1927. const pos = this.state.start;
  1928. const id = this.parseIdentifier(true);
  1929. const init = this.eat(_types.types.eq) ? this.flowEnumMemberInit() : {
  1930. type: "none",
  1931. pos
  1932. };
  1933. return {
  1934. id,
  1935. init
  1936. };
  1937. }
  1938. flowEnumCheckExplicitTypeMismatch(pos, context, expectedType) {
  1939. const {
  1940. explicitType
  1941. } = context;
  1942. if (explicitType === null) {
  1943. return;
  1944. }
  1945. if (explicitType !== expectedType) {
  1946. this.flowEnumErrorInvalidMemberInitializer(pos, context);
  1947. }
  1948. }
  1949. flowEnumMembers({
  1950. enumName,
  1951. explicitType
  1952. }) {
  1953. const seenNames = new Set();
  1954. const members = {
  1955. booleanMembers: [],
  1956. numberMembers: [],
  1957. stringMembers: [],
  1958. defaultedMembers: []
  1959. };
  1960. while (!this.match(_types.types.braceR)) {
  1961. const memberNode = this.startNode();
  1962. const {
  1963. id,
  1964. init
  1965. } = this.flowEnumMemberRaw();
  1966. const memberName = id.name;
  1967. if (memberName === "") {
  1968. continue;
  1969. }
  1970. if (/^[a-z]/.test(memberName)) {
  1971. this.flowEnumErrorInvalidMemberName(id.start, {
  1972. enumName,
  1973. memberName
  1974. });
  1975. }
  1976. if (seenNames.has(memberName)) {
  1977. this.flowEnumErrorDuplicateMemberName(id.start, {
  1978. enumName,
  1979. memberName
  1980. });
  1981. }
  1982. seenNames.add(memberName);
  1983. const context = {
  1984. enumName,
  1985. explicitType,
  1986. memberName
  1987. };
  1988. memberNode.id = id;
  1989. switch (init.type) {
  1990. case "boolean":
  1991. {
  1992. this.flowEnumCheckExplicitTypeMismatch(init.pos, context, "boolean");
  1993. memberNode.init = init.value;
  1994. members.booleanMembers.push(this.finishNode(memberNode, "EnumBooleanMember"));
  1995. break;
  1996. }
  1997. case "number":
  1998. {
  1999. this.flowEnumCheckExplicitTypeMismatch(init.pos, context, "number");
  2000. memberNode.init = init.value;
  2001. members.numberMembers.push(this.finishNode(memberNode, "EnumNumberMember"));
  2002. break;
  2003. }
  2004. case "string":
  2005. {
  2006. this.flowEnumCheckExplicitTypeMismatch(init.pos, context, "string");
  2007. memberNode.init = init.value;
  2008. members.stringMembers.push(this.finishNode(memberNode, "EnumStringMember"));
  2009. break;
  2010. }
  2011. case "invalid":
  2012. {
  2013. throw this.flowEnumErrorInvalidMemberInitializer(init.pos, context);
  2014. }
  2015. case "none":
  2016. {
  2017. switch (explicitType) {
  2018. case "boolean":
  2019. this.flowEnumErrorBooleanMemberNotInitialized(init.pos, context);
  2020. break;
  2021. case "number":
  2022. this.flowEnumErrorNumberMemberNotInitialized(init.pos, context);
  2023. break;
  2024. default:
  2025. members.defaultedMembers.push(this.finishNode(memberNode, "EnumDefaultedMember"));
  2026. }
  2027. }
  2028. }
  2029. if (!this.match(_types.types.braceR)) {
  2030. this.expect(_types.types.comma);
  2031. }
  2032. }
  2033. return members;
  2034. }
  2035. flowEnumStringMembers(initializedMembers, defaultedMembers, {
  2036. enumName
  2037. }) {
  2038. if (initializedMembers.length === 0) {
  2039. return defaultedMembers;
  2040. } else if (defaultedMembers.length === 0) {
  2041. return initializedMembers;
  2042. } else if (defaultedMembers.length > initializedMembers.length) {
  2043. for (let _i = 0; _i < initializedMembers.length; _i++) {
  2044. const member = initializedMembers[_i];
  2045. this.flowEnumErrorStringMemberInconsistentlyInitailized(member.start, {
  2046. enumName
  2047. });
  2048. }
  2049. return defaultedMembers;
  2050. } else {
  2051. for (let _i2 = 0; _i2 < defaultedMembers.length; _i2++) {
  2052. const member = defaultedMembers[_i2];
  2053. this.flowEnumErrorStringMemberInconsistentlyInitailized(member.start, {
  2054. enumName
  2055. });
  2056. }
  2057. return initializedMembers;
  2058. }
  2059. }
  2060. flowEnumParseExplicitType({
  2061. enumName
  2062. }) {
  2063. if (this.eatContextual("of")) {
  2064. if (!this.match(_types.types.name)) {
  2065. throw this.flowEnumErrorInvalidExplicitType(this.state.start, {
  2066. enumName,
  2067. suppliedType: null
  2068. });
  2069. }
  2070. const {
  2071. value
  2072. } = this.state;
  2073. this.next();
  2074. if (value !== "boolean" && value !== "number" && value !== "string" && value !== "symbol") {
  2075. this.flowEnumErrorInvalidExplicitType(this.state.start, {
  2076. enumName,
  2077. suppliedType: value
  2078. });
  2079. }
  2080. return value;
  2081. }
  2082. return null;
  2083. }
  2084. flowEnumBody(node, {
  2085. enumName,
  2086. nameLoc
  2087. }) {
  2088. const explicitType = this.flowEnumParseExplicitType({
  2089. enumName
  2090. });
  2091. this.expect(_types.types.braceL);
  2092. const members = this.flowEnumMembers({
  2093. enumName,
  2094. explicitType
  2095. });
  2096. switch (explicitType) {
  2097. case "boolean":
  2098. node.explicitType = true;
  2099. node.members = members.booleanMembers;
  2100. this.expect(_types.types.braceR);
  2101. return this.finishNode(node, "EnumBooleanBody");
  2102. case "number":
  2103. node.explicitType = true;
  2104. node.members = members.numberMembers;
  2105. this.expect(_types.types.braceR);
  2106. return this.finishNode(node, "EnumNumberBody");
  2107. case "string":
  2108. node.explicitType = true;
  2109. node.members = this.flowEnumStringMembers(members.stringMembers, members.defaultedMembers, {
  2110. enumName
  2111. });
  2112. this.expect(_types.types.braceR);
  2113. return this.finishNode(node, "EnumStringBody");
  2114. case "symbol":
  2115. node.members = members.defaultedMembers;
  2116. this.expect(_types.types.braceR);
  2117. return this.finishNode(node, "EnumSymbolBody");
  2118. default:
  2119. {
  2120. const empty = () => {
  2121. node.members = [];
  2122. this.expect(_types.types.braceR);
  2123. return this.finishNode(node, "EnumStringBody");
  2124. };
  2125. node.explicitType = false;
  2126. const boolsLen = members.booleanMembers.length;
  2127. const numsLen = members.numberMembers.length;
  2128. const strsLen = members.stringMembers.length;
  2129. const defaultedLen = members.defaultedMembers.length;
  2130. if (!boolsLen && !numsLen && !strsLen && !defaultedLen) {
  2131. return empty();
  2132. } else if (!boolsLen && !numsLen) {
  2133. node.members = this.flowEnumStringMembers(members.stringMembers, members.defaultedMembers, {
  2134. enumName
  2135. });
  2136. this.expect(_types.types.braceR);
  2137. return this.finishNode(node, "EnumStringBody");
  2138. } else if (!numsLen && !strsLen && boolsLen >= defaultedLen) {
  2139. for (let _i3 = 0, _members$defaultedMem = members.defaultedMembers; _i3 < _members$defaultedMem.length; _i3++) {
  2140. const member = _members$defaultedMem[_i3];
  2141. this.flowEnumErrorBooleanMemberNotInitialized(member.start, {
  2142. enumName,
  2143. memberName: member.id.name
  2144. });
  2145. }
  2146. node.members = members.booleanMembers;
  2147. this.expect(_types.types.braceR);
  2148. return this.finishNode(node, "EnumBooleanBody");
  2149. } else if (!boolsLen && !strsLen && numsLen >= defaultedLen) {
  2150. for (let _i4 = 0, _members$defaultedMem2 = members.defaultedMembers; _i4 < _members$defaultedMem2.length; _i4++) {
  2151. const member = _members$defaultedMem2[_i4];
  2152. this.flowEnumErrorNumberMemberNotInitialized(member.start, {
  2153. enumName,
  2154. memberName: member.id.name
  2155. });
  2156. }
  2157. node.members = members.numberMembers;
  2158. this.expect(_types.types.braceR);
  2159. return this.finishNode(node, "EnumNumberBody");
  2160. } else {
  2161. this.flowEnumErrorInconsistentMemberValues(nameLoc, {
  2162. enumName
  2163. });
  2164. return empty();
  2165. }
  2166. }
  2167. }
  2168. }
  2169. flowParseEnumDeclaration(node) {
  2170. const id = this.parseIdentifier();
  2171. node.id = id;
  2172. node.body = this.flowEnumBody(this.startNode(), {
  2173. enumName: id.name,
  2174. nameLoc: id.start
  2175. });
  2176. return this.finishNode(node, "EnumDeclaration");
  2177. }
  2178. };
  2179. exports.default = _default;