index.js 474 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. function _objectWithoutPropertiesLoose(source, excluded) {
  6. if (source == null) return {};
  7. var target = {};
  8. var sourceKeys = Object.keys(source);
  9. var key, i;
  10. for (i = 0; i < sourceKeys.length; i++) {
  11. key = sourceKeys[i];
  12. if (excluded.indexOf(key) >= 0) continue;
  13. target[key] = source[key];
  14. }
  15. return target;
  16. }
  17. class Position {
  18. constructor(line, col, index) {
  19. this.line = void 0;
  20. this.column = void 0;
  21. this.index = void 0;
  22. this.line = line;
  23. this.column = col;
  24. this.index = index;
  25. }
  26. }
  27. class SourceLocation {
  28. constructor(start, end) {
  29. this.start = void 0;
  30. this.end = void 0;
  31. this.filename = void 0;
  32. this.identifierName = void 0;
  33. this.start = start;
  34. this.end = end;
  35. }
  36. }
  37. function createPositionWithColumnOffset(position, columnOffset) {
  38. const {
  39. line,
  40. column,
  41. index
  42. } = position;
  43. return new Position(line, column + columnOffset, index + columnOffset);
  44. }
  45. const code = "BABEL_PARSER_SOURCETYPE_MODULE_REQUIRED";
  46. var ModuleErrors = {
  47. ImportMetaOutsideModule: {
  48. message: `import.meta may appear only with 'sourceType: "module"'`,
  49. code
  50. },
  51. ImportOutsideModule: {
  52. message: `'import' and 'export' may appear only with 'sourceType: "module"'`,
  53. code
  54. }
  55. };
  56. const NodeDescriptions = {
  57. ArrayPattern: "array destructuring pattern",
  58. AssignmentExpression: "assignment expression",
  59. AssignmentPattern: "assignment expression",
  60. ArrowFunctionExpression: "arrow function expression",
  61. ConditionalExpression: "conditional expression",
  62. CatchClause: "catch clause",
  63. ForOfStatement: "for-of statement",
  64. ForInStatement: "for-in statement",
  65. ForStatement: "for-loop",
  66. FormalParameters: "function parameter list",
  67. Identifier: "identifier",
  68. ImportSpecifier: "import specifier",
  69. ImportDefaultSpecifier: "import default specifier",
  70. ImportNamespaceSpecifier: "import namespace specifier",
  71. ObjectPattern: "object destructuring pattern",
  72. ParenthesizedExpression: "parenthesized expression",
  73. RestElement: "rest element",
  74. UpdateExpression: {
  75. true: "prefix operation",
  76. false: "postfix operation"
  77. },
  78. VariableDeclarator: "variable declaration",
  79. YieldExpression: "yield expression"
  80. };
  81. const toNodeDescription = node => node.type === "UpdateExpression" ? NodeDescriptions.UpdateExpression[`${node.prefix}`] : NodeDescriptions[node.type];
  82. var StandardErrors = {
  83. AccessorIsGenerator: ({
  84. kind
  85. }) => `A ${kind}ter cannot be a generator.`,
  86. ArgumentsInClass: "'arguments' is only allowed in functions and class methods.",
  87. AsyncFunctionInSingleStatementContext: "Async functions can only be declared at the top level or inside a block.",
  88. AwaitBindingIdentifier: "Can not use 'await' as identifier inside an async function.",
  89. AwaitBindingIdentifierInStaticBlock: "Can not use 'await' as identifier inside a static block.",
  90. AwaitExpressionFormalParameter: "'await' is not allowed in async function parameters.",
  91. AwaitUsingNotInAsyncContext: "'await using' is only allowed within async functions and at the top levels of modules.",
  92. AwaitNotInAsyncContext: "'await' is only allowed within async functions and at the top levels of modules.",
  93. AwaitNotInAsyncFunction: "'await' is only allowed within async functions.",
  94. BadGetterArity: "A 'get' accessor must not have any formal parameters.",
  95. BadSetterArity: "A 'set' accessor must have exactly one formal parameter.",
  96. BadSetterRestParameter: "A 'set' accessor function argument must not be a rest parameter.",
  97. ConstructorClassField: "Classes may not have a field named 'constructor'.",
  98. ConstructorClassPrivateField: "Classes may not have a private field named '#constructor'.",
  99. ConstructorIsAccessor: "Class constructor may not be an accessor.",
  100. ConstructorIsAsync: "Constructor can't be an async function.",
  101. ConstructorIsGenerator: "Constructor can't be a generator.",
  102. DeclarationMissingInitializer: ({
  103. kind
  104. }) => `Missing initializer in ${kind} declaration.`,
  105. DecoratorArgumentsOutsideParentheses: "Decorator arguments must be moved inside parentheses: use '@(decorator(args))' instead of '@(decorator)(args)'.",
  106. DecoratorBeforeExport: "Decorators must be placed *before* the 'export' keyword. Remove the 'decoratorsBeforeExport: true' option to use the 'export @decorator class {}' syntax.",
  107. DecoratorsBeforeAfterExport: "Decorators can be placed *either* before or after the 'export' keyword, but not in both locations at the same time.",
  108. DecoratorConstructor: "Decorators can't be used with a constructor. Did you mean '@dec class { ... }'?",
  109. DecoratorExportClass: "Decorators must be placed *after* the 'export' keyword. Remove the 'decoratorsBeforeExport: false' option to use the '@decorator export class {}' syntax.",
  110. DecoratorSemicolon: "Decorators must not be followed by a semicolon.",
  111. DecoratorStaticBlock: "Decorators can't be used with a static block.",
  112. DeferImportRequiresNamespace: 'Only `import defer * as x from "./module"` is valid.',
  113. DeletePrivateField: "Deleting a private field is not allowed.",
  114. DestructureNamedImport: "ES2015 named imports do not destructure. Use another statement for destructuring after the import.",
  115. DuplicateConstructor: "Duplicate constructor in the same class.",
  116. DuplicateDefaultExport: "Only one default export allowed per module.",
  117. DuplicateExport: ({
  118. exportName
  119. }) => `\`${exportName}\` has already been exported. Exported identifiers must be unique.`,
  120. DuplicateProto: "Redefinition of __proto__ property.",
  121. DuplicateRegExpFlags: "Duplicate regular expression flag.",
  122. DynamicImportPhaseRequiresImportExpressions: ({
  123. phase
  124. }) => `'import.${phase}(...)' can only be parsed when using the 'createImportExpressions' option.`,
  125. ElementAfterRest: "Rest element must be last element.",
  126. EscapedCharNotAnIdentifier: "Invalid Unicode escape.",
  127. ExportBindingIsString: ({
  128. localName,
  129. exportName
  130. }) => `A string literal cannot be used as an exported binding without \`from\`.\n- Did you mean \`export { '${localName}' as '${exportName}' } from 'some-module'\`?`,
  131. ExportDefaultFromAsIdentifier: "'from' is not allowed as an identifier after 'export default'.",
  132. ForInOfLoopInitializer: ({
  133. type
  134. }) => `'${type === "ForInStatement" ? "for-in" : "for-of"}' loop variable declaration may not have an initializer.`,
  135. ForInUsing: "For-in loop may not start with 'using' declaration.",
  136. ForOfAsync: "The left-hand side of a for-of loop may not be 'async'.",
  137. ForOfLet: "The left-hand side of a for-of loop may not start with 'let'.",
  138. GeneratorInSingleStatementContext: "Generators can only be declared at the top level or inside a block.",
  139. IllegalBreakContinue: ({
  140. type
  141. }) => `Unsyntactic ${type === "BreakStatement" ? "break" : "continue"}.`,
  142. IllegalLanguageModeDirective: "Illegal 'use strict' directive in function with non-simple parameter list.",
  143. IllegalReturn: "'return' outside of function.",
  144. ImportAttributesUseAssert: "The `assert` keyword in import attributes is deprecated and it has been replaced by the `with` keyword. You can enable the `deprecatedAssertSyntax: true` option in the import attributes plugin to suppress this error.",
  145. ImportBindingIsString: ({
  146. importName
  147. }) => `A string literal cannot be used as an imported binding.\n- Did you mean \`import { "${importName}" as foo }\`?`,
  148. ImportCallArgumentTrailingComma: "Trailing comma is disallowed inside import(...) arguments.",
  149. ImportCallArity: ({
  150. maxArgumentCount
  151. }) => `\`import()\` requires exactly ${maxArgumentCount === 1 ? "one argument" : "one or two arguments"}.`,
  152. ImportCallNotNewExpression: "Cannot use new with import(...).",
  153. ImportCallSpreadArgument: "`...` is not allowed in `import()`.",
  154. ImportJSONBindingNotDefault: "A JSON module can only be imported with `default`.",
  155. ImportReflectionHasAssertion: "`import module x` cannot have assertions.",
  156. ImportReflectionNotBinding: 'Only `import module x from "./module"` is valid.',
  157. IncompatibleRegExpUVFlags: "The 'u' and 'v' regular expression flags cannot be enabled at the same time.",
  158. InvalidBigIntLiteral: "Invalid BigIntLiteral.",
  159. InvalidCodePoint: "Code point out of bounds.",
  160. InvalidCoverInitializedName: "Invalid shorthand property initializer.",
  161. InvalidDecimal: "Invalid decimal.",
  162. InvalidDigit: ({
  163. radix
  164. }) => `Expected number in radix ${radix}.`,
  165. InvalidEscapeSequence: "Bad character escape sequence.",
  166. InvalidEscapeSequenceTemplate: "Invalid escape sequence in template.",
  167. InvalidEscapedReservedWord: ({
  168. reservedWord
  169. }) => `Escape sequence in keyword ${reservedWord}.`,
  170. InvalidIdentifier: ({
  171. identifierName
  172. }) => `Invalid identifier ${identifierName}.`,
  173. InvalidLhs: ({
  174. ancestor
  175. }) => `Invalid left-hand side in ${toNodeDescription(ancestor)}.`,
  176. InvalidLhsBinding: ({
  177. ancestor
  178. }) => `Binding invalid left-hand side in ${toNodeDescription(ancestor)}.`,
  179. InvalidLhsOptionalChaining: ({
  180. ancestor
  181. }) => `Invalid optional chaining in the left-hand side of ${toNodeDescription(ancestor)}.`,
  182. InvalidNumber: "Invalid number.",
  183. InvalidOrMissingExponent: "Floating-point numbers require a valid exponent after the 'e'.",
  184. InvalidOrUnexpectedToken: ({
  185. unexpected
  186. }) => `Unexpected character '${unexpected}'.`,
  187. InvalidParenthesizedAssignment: "Invalid parenthesized assignment pattern.",
  188. InvalidPrivateFieldResolution: ({
  189. identifierName
  190. }) => `Private name #${identifierName} is not defined.`,
  191. InvalidPropertyBindingPattern: "Binding member expression.",
  192. InvalidRecordProperty: "Only properties and spread elements are allowed in record definitions.",
  193. InvalidRestAssignmentPattern: "Invalid rest operator's argument.",
  194. LabelRedeclaration: ({
  195. labelName
  196. }) => `Label '${labelName}' is already declared.`,
  197. LetInLexicalBinding: "'let' is disallowed as a lexically bound name.",
  198. LineTerminatorBeforeArrow: "No line break is allowed before '=>'.",
  199. MalformedRegExpFlags: "Invalid regular expression flag.",
  200. MissingClassName: "A class name is required.",
  201. MissingEqInAssignment: "Only '=' operator can be used for specifying default value.",
  202. MissingSemicolon: "Missing semicolon.",
  203. MissingPlugin: ({
  204. missingPlugin
  205. }) => `This experimental syntax requires enabling the parser plugin: ${missingPlugin.map(name => JSON.stringify(name)).join(", ")}.`,
  206. MissingOneOfPlugins: ({
  207. missingPlugin
  208. }) => `This experimental syntax requires enabling one of the following parser plugin(s): ${missingPlugin.map(name => JSON.stringify(name)).join(", ")}.`,
  209. MissingUnicodeEscape: "Expecting Unicode escape sequence \\uXXXX.",
  210. MixingCoalesceWithLogical: "Nullish coalescing operator(??) requires parens when mixing with logical operators.",
  211. ModuleAttributeDifferentFromType: "The only accepted module attribute is `type`.",
  212. ModuleAttributeInvalidValue: "Only string literals are allowed as module attribute values.",
  213. ModuleAttributesWithDuplicateKeys: ({
  214. key
  215. }) => `Duplicate key "${key}" is not allowed in module attributes.`,
  216. ModuleExportNameHasLoneSurrogate: ({
  217. surrogateCharCode
  218. }) => `An export name cannot include a lone surrogate, found '\\u${surrogateCharCode.toString(16)}'.`,
  219. ModuleExportUndefined: ({
  220. localName
  221. }) => `Export '${localName}' is not defined.`,
  222. MultipleDefaultsInSwitch: "Multiple default clauses.",
  223. NewlineAfterThrow: "Illegal newline after throw.",
  224. NoCatchOrFinally: "Missing catch or finally clause.",
  225. NumberIdentifier: "Identifier directly after number.",
  226. NumericSeparatorInEscapeSequence: "Numeric separators are not allowed inside unicode escape sequences or hex escape sequences.",
  227. ObsoleteAwaitStar: "'await*' has been removed from the async functions proposal. Use Promise.all() instead.",
  228. OptionalChainingNoNew: "Constructors in/after an Optional Chain are not allowed.",
  229. OptionalChainingNoTemplate: "Tagged Template Literals are not allowed in optionalChain.",
  230. OverrideOnConstructor: "'override' modifier cannot appear on a constructor declaration.",
  231. ParamDupe: "Argument name clash.",
  232. PatternHasAccessor: "Object pattern can't contain getter or setter.",
  233. PatternHasMethod: "Object pattern can't contain methods.",
  234. PrivateInExpectedIn: ({
  235. identifierName
  236. }) => `Private names are only allowed in property accesses (\`obj.#${identifierName}\`) or in \`in\` expressions (\`#${identifierName} in obj\`).`,
  237. PrivateNameRedeclaration: ({
  238. identifierName
  239. }) => `Duplicate private name #${identifierName}.`,
  240. RecordExpressionBarIncorrectEndSyntaxType: "Record expressions ending with '|}' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'bar'.",
  241. RecordExpressionBarIncorrectStartSyntaxType: "Record expressions starting with '{|' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'bar'.",
  242. RecordExpressionHashIncorrectStartSyntaxType: "Record expressions starting with '#{' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'hash'.",
  243. RecordNoProto: "'__proto__' is not allowed in Record expressions.",
  244. RestTrailingComma: "Unexpected trailing comma after rest element.",
  245. SloppyFunction: "In non-strict mode code, functions can only be declared at top level or inside a block.",
  246. SloppyFunctionAnnexB: "In non-strict mode code, functions can only be declared at top level, inside a block, or as the body of an if statement.",
  247. SourcePhaseImportRequiresDefault: 'Only `import source x from "./module"` is valid.',
  248. StaticPrototype: "Classes may not have static property named prototype.",
  249. SuperNotAllowed: "`super()` is only valid inside a class constructor of a subclass. Maybe a typo in the method name ('constructor') or not extending another class?",
  250. SuperPrivateField: "Private fields can't be accessed on super.",
  251. TrailingDecorator: "Decorators must be attached to a class element.",
  252. TupleExpressionBarIncorrectEndSyntaxType: "Tuple expressions ending with '|]' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'bar'.",
  253. TupleExpressionBarIncorrectStartSyntaxType: "Tuple expressions starting with '[|' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'bar'.",
  254. TupleExpressionHashIncorrectStartSyntaxType: "Tuple expressions starting with '#[' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'hash'.",
  255. UnexpectedArgumentPlaceholder: "Unexpected argument placeholder.",
  256. UnexpectedAwaitAfterPipelineBody: 'Unexpected "await" after pipeline body; await must have parentheses in minimal proposal.',
  257. UnexpectedDigitAfterHash: "Unexpected digit after hash token.",
  258. UnexpectedImportExport: "'import' and 'export' may only appear at the top level.",
  259. UnexpectedKeyword: ({
  260. keyword
  261. }) => `Unexpected keyword '${keyword}'.`,
  262. UnexpectedLeadingDecorator: "Leading decorators must be attached to a class declaration.",
  263. UnexpectedLexicalDeclaration: "Lexical declaration cannot appear in a single-statement context.",
  264. UnexpectedNewTarget: "`new.target` can only be used in functions or class properties.",
  265. UnexpectedNumericSeparator: "A numeric separator is only allowed between two digits.",
  266. UnexpectedPrivateField: "Unexpected private name.",
  267. UnexpectedReservedWord: ({
  268. reservedWord
  269. }) => `Unexpected reserved word '${reservedWord}'.`,
  270. UnexpectedSuper: "'super' is only allowed in object methods and classes.",
  271. UnexpectedToken: ({
  272. expected,
  273. unexpected
  274. }) => `Unexpected token${unexpected ? ` '${unexpected}'.` : ""}${expected ? `, expected "${expected}"` : ""}`,
  275. UnexpectedTokenUnaryExponentiation: "Illegal expression. Wrap left hand side or entire exponentiation in parentheses.",
  276. UnexpectedUsingDeclaration: "Using declaration cannot appear in the top level when source type is `script`.",
  277. UnsupportedBind: "Binding should be performed on object property.",
  278. UnsupportedDecoratorExport: "A decorated export must export a class declaration.",
  279. UnsupportedDefaultExport: "Only expressions, functions or classes are allowed as the `default` export.",
  280. UnsupportedImport: "`import` can only be used in `import()` or `import.meta`.",
  281. UnsupportedMetaProperty: ({
  282. target,
  283. onlyValidPropertyName
  284. }) => `The only valid meta property for ${target} is ${target}.${onlyValidPropertyName}.`,
  285. UnsupportedParameterDecorator: "Decorators cannot be used to decorate parameters.",
  286. UnsupportedPropertyDecorator: "Decorators cannot be used to decorate object literal properties.",
  287. UnsupportedSuper: "'super' can only be used with function calls (i.e. super()) or in property accesses (i.e. super.prop or super[prop]).",
  288. UnterminatedComment: "Unterminated comment.",
  289. UnterminatedRegExp: "Unterminated regular expression.",
  290. UnterminatedString: "Unterminated string constant.",
  291. UnterminatedTemplate: "Unterminated template.",
  292. UsingDeclarationExport: "Using declaration cannot be exported.",
  293. UsingDeclarationHasBindingPattern: "Using declaration cannot have destructuring patterns.",
  294. VarRedeclaration: ({
  295. identifierName
  296. }) => `Identifier '${identifierName}' has already been declared.`,
  297. YieldBindingIdentifier: "Can not use 'yield' as identifier inside a generator.",
  298. YieldInParameter: "Yield expression is not allowed in formal parameters.",
  299. ZeroDigitNumericSeparator: "Numeric separator can not be used after leading 0."
  300. };
  301. var StrictModeErrors = {
  302. StrictDelete: "Deleting local variable in strict mode.",
  303. StrictEvalArguments: ({
  304. referenceName
  305. }) => `Assigning to '${referenceName}' in strict mode.`,
  306. StrictEvalArgumentsBinding: ({
  307. bindingName
  308. }) => `Binding '${bindingName}' in strict mode.`,
  309. StrictFunction: "In strict mode code, functions can only be declared at top level or inside a block.",
  310. StrictNumericEscape: "The only valid numeric escape in strict mode is '\\0'.",
  311. StrictOctalLiteral: "Legacy octal literals are not allowed in strict mode.",
  312. StrictWith: "'with' in strict mode."
  313. };
  314. const UnparenthesizedPipeBodyDescriptions = new Set(["ArrowFunctionExpression", "AssignmentExpression", "ConditionalExpression", "YieldExpression"]);
  315. var PipelineOperatorErrors = {
  316. PipeBodyIsTighter: "Unexpected yield after pipeline body; any yield expression acting as Hack-style pipe body must be parenthesized due to its loose operator precedence.",
  317. PipeTopicRequiresHackPipes: 'Topic reference is used, but the pipelineOperator plugin was not passed a "proposal": "hack" or "smart" option.',
  318. PipeTopicUnbound: "Topic reference is unbound; it must be inside a pipe body.",
  319. PipeTopicUnconfiguredToken: ({
  320. token
  321. }) => `Invalid topic token ${token}. In order to use ${token} as a topic reference, the pipelineOperator plugin must be configured with { "proposal": "hack", "topicToken": "${token}" }.`,
  322. PipeTopicUnused: "Hack-style pipe body does not contain a topic reference; Hack-style pipes must use topic at least once.",
  323. PipeUnparenthesizedBody: ({
  324. type
  325. }) => `Hack-style pipe body cannot be an unparenthesized ${toNodeDescription({
  326. type
  327. })}; please wrap it in parentheses.`,
  328. PipelineBodyNoArrow: 'Unexpected arrow "=>" after pipeline body; arrow function in pipeline body must be parenthesized.',
  329. PipelineBodySequenceExpression: "Pipeline body may not be a comma-separated sequence expression.",
  330. PipelineHeadSequenceExpression: "Pipeline head should not be a comma-separated sequence expression.",
  331. PipelineTopicUnused: "Pipeline is in topic style but does not use topic reference.",
  332. PrimaryTopicNotAllowed: "Topic reference was used in a lexical context without topic binding.",
  333. PrimaryTopicRequiresSmartPipeline: 'Topic reference is used, but the pipelineOperator plugin was not passed a "proposal": "hack" or "smart" option.'
  334. };
  335. const _excluded = ["toMessage"],
  336. _excluded2 = ["message"];
  337. function defineHidden(obj, key, value) {
  338. Object.defineProperty(obj, key, {
  339. enumerable: false,
  340. configurable: true,
  341. value
  342. });
  343. }
  344. function toParseErrorConstructor(_ref) {
  345. let {
  346. toMessage
  347. } = _ref,
  348. properties = _objectWithoutPropertiesLoose(_ref, _excluded);
  349. return function constructor(loc, details) {
  350. const error = new SyntaxError();
  351. Object.assign(error, properties, {
  352. loc,
  353. pos: loc.index
  354. });
  355. if ("missingPlugin" in details) {
  356. Object.assign(error, {
  357. missingPlugin: details.missingPlugin
  358. });
  359. }
  360. defineHidden(error, "clone", function clone(overrides = {}) {
  361. var _overrides$loc;
  362. const {
  363. line,
  364. column,
  365. index
  366. } = (_overrides$loc = overrides.loc) != null ? _overrides$loc : loc;
  367. return constructor(new Position(line, column, index), Object.assign({}, details, overrides.details));
  368. });
  369. defineHidden(error, "details", details);
  370. Object.defineProperty(error, "message", {
  371. configurable: true,
  372. get() {
  373. const message = `${toMessage(details)} (${loc.line}:${loc.column})`;
  374. this.message = message;
  375. return message;
  376. },
  377. set(value) {
  378. Object.defineProperty(this, "message", {
  379. value,
  380. writable: true
  381. });
  382. }
  383. });
  384. return error;
  385. };
  386. }
  387. function ParseErrorEnum(argument, syntaxPlugin) {
  388. if (Array.isArray(argument)) {
  389. return parseErrorTemplates => ParseErrorEnum(parseErrorTemplates, argument[0]);
  390. }
  391. const ParseErrorConstructors = {};
  392. for (const reasonCode of Object.keys(argument)) {
  393. const template = argument[reasonCode];
  394. const _ref2 = typeof template === "string" ? {
  395. message: () => template
  396. } : typeof template === "function" ? {
  397. message: template
  398. } : template,
  399. {
  400. message
  401. } = _ref2,
  402. rest = _objectWithoutPropertiesLoose(_ref2, _excluded2);
  403. const toMessage = typeof message === "string" ? () => message : message;
  404. ParseErrorConstructors[reasonCode] = toParseErrorConstructor(Object.assign({
  405. code: "BABEL_PARSER_SYNTAX_ERROR",
  406. reasonCode,
  407. toMessage
  408. }, syntaxPlugin ? {
  409. syntaxPlugin
  410. } : {}, rest));
  411. }
  412. return ParseErrorConstructors;
  413. }
  414. const Errors = Object.assign({}, ParseErrorEnum(ModuleErrors), ParseErrorEnum(StandardErrors), ParseErrorEnum(StrictModeErrors), ParseErrorEnum`pipelineOperator`(PipelineOperatorErrors));
  415. const {
  416. defineProperty
  417. } = Object;
  418. const toUnenumerable = (object, key) => defineProperty(object, key, {
  419. enumerable: false,
  420. value: object[key]
  421. });
  422. function toESTreeLocation(node) {
  423. node.loc.start && toUnenumerable(node.loc.start, "index");
  424. node.loc.end && toUnenumerable(node.loc.end, "index");
  425. return node;
  426. }
  427. var estree = superClass => class ESTreeParserMixin extends superClass {
  428. parse() {
  429. const file = toESTreeLocation(super.parse());
  430. if (this.options.tokens) {
  431. file.tokens = file.tokens.map(toESTreeLocation);
  432. }
  433. return file;
  434. }
  435. parseRegExpLiteral({
  436. pattern,
  437. flags
  438. }) {
  439. let regex = null;
  440. try {
  441. regex = new RegExp(pattern, flags);
  442. } catch (e) {}
  443. const node = this.estreeParseLiteral(regex);
  444. node.regex = {
  445. pattern,
  446. flags
  447. };
  448. return node;
  449. }
  450. parseBigIntLiteral(value) {
  451. let bigInt;
  452. try {
  453. bigInt = BigInt(value);
  454. } catch (_unused) {
  455. bigInt = null;
  456. }
  457. const node = this.estreeParseLiteral(bigInt);
  458. node.bigint = String(node.value || value);
  459. return node;
  460. }
  461. parseDecimalLiteral(value) {
  462. const decimal = null;
  463. const node = this.estreeParseLiteral(decimal);
  464. node.decimal = String(node.value || value);
  465. return node;
  466. }
  467. estreeParseLiteral(value) {
  468. return this.parseLiteral(value, "Literal");
  469. }
  470. parseStringLiteral(value) {
  471. return this.estreeParseLiteral(value);
  472. }
  473. parseNumericLiteral(value) {
  474. return this.estreeParseLiteral(value);
  475. }
  476. parseNullLiteral() {
  477. return this.estreeParseLiteral(null);
  478. }
  479. parseBooleanLiteral(value) {
  480. return this.estreeParseLiteral(value);
  481. }
  482. directiveToStmt(directive) {
  483. const expression = directive.value;
  484. delete directive.value;
  485. expression.type = "Literal";
  486. expression.raw = expression.extra.raw;
  487. expression.value = expression.extra.expressionValue;
  488. const stmt = directive;
  489. stmt.type = "ExpressionStatement";
  490. stmt.expression = expression;
  491. stmt.directive = expression.extra.rawValue;
  492. delete expression.extra;
  493. return stmt;
  494. }
  495. initFunction(node, isAsync) {
  496. super.initFunction(node, isAsync);
  497. node.expression = false;
  498. }
  499. checkDeclaration(node) {
  500. if (node != null && this.isObjectProperty(node)) {
  501. this.checkDeclaration(node.value);
  502. } else {
  503. super.checkDeclaration(node);
  504. }
  505. }
  506. getObjectOrClassMethodParams(method) {
  507. return method.value.params;
  508. }
  509. isValidDirective(stmt) {
  510. var _stmt$expression$extr;
  511. return stmt.type === "ExpressionStatement" && stmt.expression.type === "Literal" && typeof stmt.expression.value === "string" && !((_stmt$expression$extr = stmt.expression.extra) != null && _stmt$expression$extr.parenthesized);
  512. }
  513. parseBlockBody(node, allowDirectives, topLevel, end, afterBlockParse) {
  514. super.parseBlockBody(node, allowDirectives, topLevel, end, afterBlockParse);
  515. const directiveStatements = node.directives.map(d => this.directiveToStmt(d));
  516. node.body = directiveStatements.concat(node.body);
  517. delete node.directives;
  518. }
  519. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  520. this.parseMethod(method, isGenerator, isAsync, isConstructor, allowsDirectSuper, "ClassMethod", true);
  521. if (method.typeParameters) {
  522. method.value.typeParameters = method.typeParameters;
  523. delete method.typeParameters;
  524. }
  525. classBody.body.push(method);
  526. }
  527. parsePrivateName() {
  528. const node = super.parsePrivateName();
  529. {
  530. if (!this.getPluginOption("estree", "classFeatures")) {
  531. return node;
  532. }
  533. }
  534. return this.convertPrivateNameToPrivateIdentifier(node);
  535. }
  536. convertPrivateNameToPrivateIdentifier(node) {
  537. const name = super.getPrivateNameSV(node);
  538. node = node;
  539. delete node.id;
  540. node.name = name;
  541. node.type = "PrivateIdentifier";
  542. return node;
  543. }
  544. isPrivateName(node) {
  545. {
  546. if (!this.getPluginOption("estree", "classFeatures")) {
  547. return super.isPrivateName(node);
  548. }
  549. }
  550. return node.type === "PrivateIdentifier";
  551. }
  552. getPrivateNameSV(node) {
  553. {
  554. if (!this.getPluginOption("estree", "classFeatures")) {
  555. return super.getPrivateNameSV(node);
  556. }
  557. }
  558. return node.name;
  559. }
  560. parseLiteral(value, type) {
  561. const node = super.parseLiteral(value, type);
  562. node.raw = node.extra.raw;
  563. delete node.extra;
  564. return node;
  565. }
  566. parseFunctionBody(node, allowExpression, isMethod = false) {
  567. super.parseFunctionBody(node, allowExpression, isMethod);
  568. node.expression = node.body.type !== "BlockStatement";
  569. }
  570. parseMethod(node, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope = false) {
  571. let funcNode = this.startNode();
  572. funcNode.kind = node.kind;
  573. funcNode = super.parseMethod(funcNode, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope);
  574. funcNode.type = "FunctionExpression";
  575. delete funcNode.kind;
  576. node.value = funcNode;
  577. if (type === "ClassPrivateMethod") {
  578. node.computed = false;
  579. }
  580. return this.finishNode(node, "MethodDefinition");
  581. }
  582. nameIsConstructor(key) {
  583. if (key.type === "Literal") return key.value === "constructor";
  584. return super.nameIsConstructor(key);
  585. }
  586. parseClassProperty(...args) {
  587. const propertyNode = super.parseClassProperty(...args);
  588. {
  589. if (!this.getPluginOption("estree", "classFeatures")) {
  590. return propertyNode;
  591. }
  592. }
  593. propertyNode.type = "PropertyDefinition";
  594. return propertyNode;
  595. }
  596. parseClassPrivateProperty(...args) {
  597. const propertyNode = super.parseClassPrivateProperty(...args);
  598. {
  599. if (!this.getPluginOption("estree", "classFeatures")) {
  600. return propertyNode;
  601. }
  602. }
  603. propertyNode.type = "PropertyDefinition";
  604. propertyNode.computed = false;
  605. return propertyNode;
  606. }
  607. parseObjectMethod(prop, isGenerator, isAsync, isPattern, isAccessor) {
  608. const node = super.parseObjectMethod(prop, isGenerator, isAsync, isPattern, isAccessor);
  609. if (node) {
  610. node.type = "Property";
  611. if (node.kind === "method") {
  612. node.kind = "init";
  613. }
  614. node.shorthand = false;
  615. }
  616. return node;
  617. }
  618. parseObjectProperty(prop, startLoc, isPattern, refExpressionErrors) {
  619. const node = super.parseObjectProperty(prop, startLoc, isPattern, refExpressionErrors);
  620. if (node) {
  621. node.kind = "init";
  622. node.type = "Property";
  623. }
  624. return node;
  625. }
  626. isValidLVal(type, isUnparenthesizedInAssign, binding) {
  627. return type === "Property" ? "value" : super.isValidLVal(type, isUnparenthesizedInAssign, binding);
  628. }
  629. isAssignable(node, isBinding) {
  630. if (node != null && this.isObjectProperty(node)) {
  631. return this.isAssignable(node.value, isBinding);
  632. }
  633. return super.isAssignable(node, isBinding);
  634. }
  635. toAssignable(node, isLHS = false) {
  636. if (node != null && this.isObjectProperty(node)) {
  637. const {
  638. key,
  639. value
  640. } = node;
  641. if (this.isPrivateName(key)) {
  642. this.classScope.usePrivateName(this.getPrivateNameSV(key), key.loc.start);
  643. }
  644. this.toAssignable(value, isLHS);
  645. } else {
  646. super.toAssignable(node, isLHS);
  647. }
  648. }
  649. toAssignableObjectExpressionProp(prop, isLast, isLHS) {
  650. if (prop.type === "Property" && (prop.kind === "get" || prop.kind === "set")) {
  651. this.raise(Errors.PatternHasAccessor, prop.key);
  652. } else if (prop.type === "Property" && prop.method) {
  653. this.raise(Errors.PatternHasMethod, prop.key);
  654. } else {
  655. super.toAssignableObjectExpressionProp(prop, isLast, isLHS);
  656. }
  657. }
  658. finishCallExpression(unfinished, optional) {
  659. const node = super.finishCallExpression(unfinished, optional);
  660. if (node.callee.type === "Import") {
  661. node.type = "ImportExpression";
  662. node.source = node.arguments[0];
  663. if (this.hasPlugin("importAttributes") || this.hasPlugin("importAssertions")) {
  664. var _ref, _ref2;
  665. node.options = (_ref = node.arguments[1]) != null ? _ref : null;
  666. node.attributes = (_ref2 = node.arguments[1]) != null ? _ref2 : null;
  667. }
  668. delete node.arguments;
  669. delete node.callee;
  670. }
  671. return node;
  672. }
  673. toReferencedArguments(node) {
  674. if (node.type === "ImportExpression") {
  675. return;
  676. }
  677. super.toReferencedArguments(node);
  678. }
  679. parseExport(unfinished, decorators) {
  680. const exportStartLoc = this.state.lastTokStartLoc;
  681. const node = super.parseExport(unfinished, decorators);
  682. switch (node.type) {
  683. case "ExportAllDeclaration":
  684. node.exported = null;
  685. break;
  686. case "ExportNamedDeclaration":
  687. if (node.specifiers.length === 1 && node.specifiers[0].type === "ExportNamespaceSpecifier") {
  688. node.type = "ExportAllDeclaration";
  689. node.exported = node.specifiers[0].exported;
  690. delete node.specifiers;
  691. }
  692. case "ExportDefaultDeclaration":
  693. {
  694. var _declaration$decorato;
  695. const {
  696. declaration
  697. } = node;
  698. if ((declaration == null ? void 0 : declaration.type) === "ClassDeclaration" && ((_declaration$decorato = declaration.decorators) == null ? void 0 : _declaration$decorato.length) > 0 && declaration.start === node.start) {
  699. this.resetStartLocation(node, exportStartLoc);
  700. }
  701. }
  702. break;
  703. }
  704. return node;
  705. }
  706. parseSubscript(base, startLoc, noCalls, state) {
  707. const node = super.parseSubscript(base, startLoc, noCalls, state);
  708. if (state.optionalChainMember) {
  709. if (node.type === "OptionalMemberExpression" || node.type === "OptionalCallExpression") {
  710. node.type = node.type.substring(8);
  711. }
  712. if (state.stop) {
  713. const chain = this.startNodeAtNode(node);
  714. chain.expression = node;
  715. return this.finishNode(chain, "ChainExpression");
  716. }
  717. } else if (node.type === "MemberExpression" || node.type === "CallExpression") {
  718. node.optional = false;
  719. }
  720. return node;
  721. }
  722. isOptionalMemberExpression(node) {
  723. if (node.type === "ChainExpression") {
  724. return node.expression.type === "MemberExpression";
  725. }
  726. return super.isOptionalMemberExpression(node);
  727. }
  728. hasPropertyAsPrivateName(node) {
  729. if (node.type === "ChainExpression") {
  730. node = node.expression;
  731. }
  732. return super.hasPropertyAsPrivateName(node);
  733. }
  734. isObjectProperty(node) {
  735. return node.type === "Property" && node.kind === "init" && !node.method;
  736. }
  737. isObjectMethod(node) {
  738. return node.type === "Property" && (node.method || node.kind === "get" || node.kind === "set");
  739. }
  740. finishNodeAt(node, type, endLoc) {
  741. return toESTreeLocation(super.finishNodeAt(node, type, endLoc));
  742. }
  743. resetStartLocation(node, startLoc) {
  744. super.resetStartLocation(node, startLoc);
  745. toESTreeLocation(node);
  746. }
  747. resetEndLocation(node, endLoc = this.state.lastTokEndLoc) {
  748. super.resetEndLocation(node, endLoc);
  749. toESTreeLocation(node);
  750. }
  751. };
  752. class TokContext {
  753. constructor(token, preserveSpace) {
  754. this.token = void 0;
  755. this.preserveSpace = void 0;
  756. this.token = token;
  757. this.preserveSpace = !!preserveSpace;
  758. }
  759. }
  760. const types = {
  761. brace: new TokContext("{"),
  762. j_oTag: new TokContext("<tag"),
  763. j_cTag: new TokContext("</tag"),
  764. j_expr: new TokContext("<tag>...</tag>", true)
  765. };
  766. {
  767. types.template = new TokContext("`", true);
  768. }
  769. const beforeExpr = true;
  770. const startsExpr = true;
  771. const isLoop = true;
  772. const isAssign = true;
  773. const prefix = true;
  774. const postfix = true;
  775. class ExportedTokenType {
  776. constructor(label, conf = {}) {
  777. this.label = void 0;
  778. this.keyword = void 0;
  779. this.beforeExpr = void 0;
  780. this.startsExpr = void 0;
  781. this.rightAssociative = void 0;
  782. this.isLoop = void 0;
  783. this.isAssign = void 0;
  784. this.prefix = void 0;
  785. this.postfix = void 0;
  786. this.binop = void 0;
  787. this.label = label;
  788. this.keyword = conf.keyword;
  789. this.beforeExpr = !!conf.beforeExpr;
  790. this.startsExpr = !!conf.startsExpr;
  791. this.rightAssociative = !!conf.rightAssociative;
  792. this.isLoop = !!conf.isLoop;
  793. this.isAssign = !!conf.isAssign;
  794. this.prefix = !!conf.prefix;
  795. this.postfix = !!conf.postfix;
  796. this.binop = conf.binop != null ? conf.binop : null;
  797. {
  798. this.updateContext = null;
  799. }
  800. }
  801. }
  802. const keywords$1 = new Map();
  803. function createKeyword(name, options = {}) {
  804. options.keyword = name;
  805. const token = createToken(name, options);
  806. keywords$1.set(name, token);
  807. return token;
  808. }
  809. function createBinop(name, binop) {
  810. return createToken(name, {
  811. beforeExpr,
  812. binop
  813. });
  814. }
  815. let tokenTypeCounter = -1;
  816. const tokenTypes = [];
  817. const tokenLabels = [];
  818. const tokenBinops = [];
  819. const tokenBeforeExprs = [];
  820. const tokenStartsExprs = [];
  821. const tokenPrefixes = [];
  822. function createToken(name, options = {}) {
  823. var _options$binop, _options$beforeExpr, _options$startsExpr, _options$prefix;
  824. ++tokenTypeCounter;
  825. tokenLabels.push(name);
  826. tokenBinops.push((_options$binop = options.binop) != null ? _options$binop : -1);
  827. tokenBeforeExprs.push((_options$beforeExpr = options.beforeExpr) != null ? _options$beforeExpr : false);
  828. tokenStartsExprs.push((_options$startsExpr = options.startsExpr) != null ? _options$startsExpr : false);
  829. tokenPrefixes.push((_options$prefix = options.prefix) != null ? _options$prefix : false);
  830. tokenTypes.push(new ExportedTokenType(name, options));
  831. return tokenTypeCounter;
  832. }
  833. function createKeywordLike(name, options = {}) {
  834. var _options$binop2, _options$beforeExpr2, _options$startsExpr2, _options$prefix2;
  835. ++tokenTypeCounter;
  836. keywords$1.set(name, tokenTypeCounter);
  837. tokenLabels.push(name);
  838. tokenBinops.push((_options$binop2 = options.binop) != null ? _options$binop2 : -1);
  839. tokenBeforeExprs.push((_options$beforeExpr2 = options.beforeExpr) != null ? _options$beforeExpr2 : false);
  840. tokenStartsExprs.push((_options$startsExpr2 = options.startsExpr) != null ? _options$startsExpr2 : false);
  841. tokenPrefixes.push((_options$prefix2 = options.prefix) != null ? _options$prefix2 : false);
  842. tokenTypes.push(new ExportedTokenType("name", options));
  843. return tokenTypeCounter;
  844. }
  845. const tt = {
  846. bracketL: createToken("[", {
  847. beforeExpr,
  848. startsExpr
  849. }),
  850. bracketHashL: createToken("#[", {
  851. beforeExpr,
  852. startsExpr
  853. }),
  854. bracketBarL: createToken("[|", {
  855. beforeExpr,
  856. startsExpr
  857. }),
  858. bracketR: createToken("]"),
  859. bracketBarR: createToken("|]"),
  860. braceL: createToken("{", {
  861. beforeExpr,
  862. startsExpr
  863. }),
  864. braceBarL: createToken("{|", {
  865. beforeExpr,
  866. startsExpr
  867. }),
  868. braceHashL: createToken("#{", {
  869. beforeExpr,
  870. startsExpr
  871. }),
  872. braceR: createToken("}"),
  873. braceBarR: createToken("|}"),
  874. parenL: createToken("(", {
  875. beforeExpr,
  876. startsExpr
  877. }),
  878. parenR: createToken(")"),
  879. comma: createToken(",", {
  880. beforeExpr
  881. }),
  882. semi: createToken(";", {
  883. beforeExpr
  884. }),
  885. colon: createToken(":", {
  886. beforeExpr
  887. }),
  888. doubleColon: createToken("::", {
  889. beforeExpr
  890. }),
  891. dot: createToken("."),
  892. question: createToken("?", {
  893. beforeExpr
  894. }),
  895. questionDot: createToken("?."),
  896. arrow: createToken("=>", {
  897. beforeExpr
  898. }),
  899. template: createToken("template"),
  900. ellipsis: createToken("...", {
  901. beforeExpr
  902. }),
  903. backQuote: createToken("`", {
  904. startsExpr
  905. }),
  906. dollarBraceL: createToken("${", {
  907. beforeExpr,
  908. startsExpr
  909. }),
  910. templateTail: createToken("...`", {
  911. startsExpr
  912. }),
  913. templateNonTail: createToken("...${", {
  914. beforeExpr,
  915. startsExpr
  916. }),
  917. at: createToken("@"),
  918. hash: createToken("#", {
  919. startsExpr
  920. }),
  921. interpreterDirective: createToken("#!..."),
  922. eq: createToken("=", {
  923. beforeExpr,
  924. isAssign
  925. }),
  926. assign: createToken("_=", {
  927. beforeExpr,
  928. isAssign
  929. }),
  930. slashAssign: createToken("_=", {
  931. beforeExpr,
  932. isAssign
  933. }),
  934. xorAssign: createToken("_=", {
  935. beforeExpr,
  936. isAssign
  937. }),
  938. moduloAssign: createToken("_=", {
  939. beforeExpr,
  940. isAssign
  941. }),
  942. incDec: createToken("++/--", {
  943. prefix,
  944. postfix,
  945. startsExpr
  946. }),
  947. bang: createToken("!", {
  948. beforeExpr,
  949. prefix,
  950. startsExpr
  951. }),
  952. tilde: createToken("~", {
  953. beforeExpr,
  954. prefix,
  955. startsExpr
  956. }),
  957. doubleCaret: createToken("^^", {
  958. startsExpr
  959. }),
  960. doubleAt: createToken("@@", {
  961. startsExpr
  962. }),
  963. pipeline: createBinop("|>", 0),
  964. nullishCoalescing: createBinop("??", 1),
  965. logicalOR: createBinop("||", 1),
  966. logicalAND: createBinop("&&", 2),
  967. bitwiseOR: createBinop("|", 3),
  968. bitwiseXOR: createBinop("^", 4),
  969. bitwiseAND: createBinop("&", 5),
  970. equality: createBinop("==/!=/===/!==", 6),
  971. lt: createBinop("</>/<=/>=", 7),
  972. gt: createBinop("</>/<=/>=", 7),
  973. relational: createBinop("</>/<=/>=", 7),
  974. bitShift: createBinop("<</>>/>>>", 8),
  975. bitShiftL: createBinop("<</>>/>>>", 8),
  976. bitShiftR: createBinop("<</>>/>>>", 8),
  977. plusMin: createToken("+/-", {
  978. beforeExpr,
  979. binop: 9,
  980. prefix,
  981. startsExpr
  982. }),
  983. modulo: createToken("%", {
  984. binop: 10,
  985. startsExpr
  986. }),
  987. star: createToken("*", {
  988. binop: 10
  989. }),
  990. slash: createBinop("/", 10),
  991. exponent: createToken("**", {
  992. beforeExpr,
  993. binop: 11,
  994. rightAssociative: true
  995. }),
  996. _in: createKeyword("in", {
  997. beforeExpr,
  998. binop: 7
  999. }),
  1000. _instanceof: createKeyword("instanceof", {
  1001. beforeExpr,
  1002. binop: 7
  1003. }),
  1004. _break: createKeyword("break"),
  1005. _case: createKeyword("case", {
  1006. beforeExpr
  1007. }),
  1008. _catch: createKeyword("catch"),
  1009. _continue: createKeyword("continue"),
  1010. _debugger: createKeyword("debugger"),
  1011. _default: createKeyword("default", {
  1012. beforeExpr
  1013. }),
  1014. _else: createKeyword("else", {
  1015. beforeExpr
  1016. }),
  1017. _finally: createKeyword("finally"),
  1018. _function: createKeyword("function", {
  1019. startsExpr
  1020. }),
  1021. _if: createKeyword("if"),
  1022. _return: createKeyword("return", {
  1023. beforeExpr
  1024. }),
  1025. _switch: createKeyword("switch"),
  1026. _throw: createKeyword("throw", {
  1027. beforeExpr,
  1028. prefix,
  1029. startsExpr
  1030. }),
  1031. _try: createKeyword("try"),
  1032. _var: createKeyword("var"),
  1033. _const: createKeyword("const"),
  1034. _with: createKeyword("with"),
  1035. _new: createKeyword("new", {
  1036. beforeExpr,
  1037. startsExpr
  1038. }),
  1039. _this: createKeyword("this", {
  1040. startsExpr
  1041. }),
  1042. _super: createKeyword("super", {
  1043. startsExpr
  1044. }),
  1045. _class: createKeyword("class", {
  1046. startsExpr
  1047. }),
  1048. _extends: createKeyword("extends", {
  1049. beforeExpr
  1050. }),
  1051. _export: createKeyword("export"),
  1052. _import: createKeyword("import", {
  1053. startsExpr
  1054. }),
  1055. _null: createKeyword("null", {
  1056. startsExpr
  1057. }),
  1058. _true: createKeyword("true", {
  1059. startsExpr
  1060. }),
  1061. _false: createKeyword("false", {
  1062. startsExpr
  1063. }),
  1064. _typeof: createKeyword("typeof", {
  1065. beforeExpr,
  1066. prefix,
  1067. startsExpr
  1068. }),
  1069. _void: createKeyword("void", {
  1070. beforeExpr,
  1071. prefix,
  1072. startsExpr
  1073. }),
  1074. _delete: createKeyword("delete", {
  1075. beforeExpr,
  1076. prefix,
  1077. startsExpr
  1078. }),
  1079. _do: createKeyword("do", {
  1080. isLoop,
  1081. beforeExpr
  1082. }),
  1083. _for: createKeyword("for", {
  1084. isLoop
  1085. }),
  1086. _while: createKeyword("while", {
  1087. isLoop
  1088. }),
  1089. _as: createKeywordLike("as", {
  1090. startsExpr
  1091. }),
  1092. _assert: createKeywordLike("assert", {
  1093. startsExpr
  1094. }),
  1095. _async: createKeywordLike("async", {
  1096. startsExpr
  1097. }),
  1098. _await: createKeywordLike("await", {
  1099. startsExpr
  1100. }),
  1101. _defer: createKeywordLike("defer", {
  1102. startsExpr
  1103. }),
  1104. _from: createKeywordLike("from", {
  1105. startsExpr
  1106. }),
  1107. _get: createKeywordLike("get", {
  1108. startsExpr
  1109. }),
  1110. _let: createKeywordLike("let", {
  1111. startsExpr
  1112. }),
  1113. _meta: createKeywordLike("meta", {
  1114. startsExpr
  1115. }),
  1116. _of: createKeywordLike("of", {
  1117. startsExpr
  1118. }),
  1119. _sent: createKeywordLike("sent", {
  1120. startsExpr
  1121. }),
  1122. _set: createKeywordLike("set", {
  1123. startsExpr
  1124. }),
  1125. _source: createKeywordLike("source", {
  1126. startsExpr
  1127. }),
  1128. _static: createKeywordLike("static", {
  1129. startsExpr
  1130. }),
  1131. _using: createKeywordLike("using", {
  1132. startsExpr
  1133. }),
  1134. _yield: createKeywordLike("yield", {
  1135. startsExpr
  1136. }),
  1137. _asserts: createKeywordLike("asserts", {
  1138. startsExpr
  1139. }),
  1140. _checks: createKeywordLike("checks", {
  1141. startsExpr
  1142. }),
  1143. _exports: createKeywordLike("exports", {
  1144. startsExpr
  1145. }),
  1146. _global: createKeywordLike("global", {
  1147. startsExpr
  1148. }),
  1149. _implements: createKeywordLike("implements", {
  1150. startsExpr
  1151. }),
  1152. _intrinsic: createKeywordLike("intrinsic", {
  1153. startsExpr
  1154. }),
  1155. _infer: createKeywordLike("infer", {
  1156. startsExpr
  1157. }),
  1158. _is: createKeywordLike("is", {
  1159. startsExpr
  1160. }),
  1161. _mixins: createKeywordLike("mixins", {
  1162. startsExpr
  1163. }),
  1164. _proto: createKeywordLike("proto", {
  1165. startsExpr
  1166. }),
  1167. _require: createKeywordLike("require", {
  1168. startsExpr
  1169. }),
  1170. _satisfies: createKeywordLike("satisfies", {
  1171. startsExpr
  1172. }),
  1173. _keyof: createKeywordLike("keyof", {
  1174. startsExpr
  1175. }),
  1176. _readonly: createKeywordLike("readonly", {
  1177. startsExpr
  1178. }),
  1179. _unique: createKeywordLike("unique", {
  1180. startsExpr
  1181. }),
  1182. _abstract: createKeywordLike("abstract", {
  1183. startsExpr
  1184. }),
  1185. _declare: createKeywordLike("declare", {
  1186. startsExpr
  1187. }),
  1188. _enum: createKeywordLike("enum", {
  1189. startsExpr
  1190. }),
  1191. _module: createKeywordLike("module", {
  1192. startsExpr
  1193. }),
  1194. _namespace: createKeywordLike("namespace", {
  1195. startsExpr
  1196. }),
  1197. _interface: createKeywordLike("interface", {
  1198. startsExpr
  1199. }),
  1200. _type: createKeywordLike("type", {
  1201. startsExpr
  1202. }),
  1203. _opaque: createKeywordLike("opaque", {
  1204. startsExpr
  1205. }),
  1206. name: createToken("name", {
  1207. startsExpr
  1208. }),
  1209. string: createToken("string", {
  1210. startsExpr
  1211. }),
  1212. num: createToken("num", {
  1213. startsExpr
  1214. }),
  1215. bigint: createToken("bigint", {
  1216. startsExpr
  1217. }),
  1218. decimal: createToken("decimal", {
  1219. startsExpr
  1220. }),
  1221. regexp: createToken("regexp", {
  1222. startsExpr
  1223. }),
  1224. privateName: createToken("#name", {
  1225. startsExpr
  1226. }),
  1227. eof: createToken("eof"),
  1228. jsxName: createToken("jsxName"),
  1229. jsxText: createToken("jsxText", {
  1230. beforeExpr: true
  1231. }),
  1232. jsxTagStart: createToken("jsxTagStart", {
  1233. startsExpr: true
  1234. }),
  1235. jsxTagEnd: createToken("jsxTagEnd"),
  1236. placeholder: createToken("%%", {
  1237. startsExpr: true
  1238. })
  1239. };
  1240. function tokenIsIdentifier(token) {
  1241. return token >= 93 && token <= 132;
  1242. }
  1243. function tokenKeywordOrIdentifierIsKeyword(token) {
  1244. return token <= 92;
  1245. }
  1246. function tokenIsKeywordOrIdentifier(token) {
  1247. return token >= 58 && token <= 132;
  1248. }
  1249. function tokenIsLiteralPropertyName(token) {
  1250. return token >= 58 && token <= 136;
  1251. }
  1252. function tokenComesBeforeExpression(token) {
  1253. return tokenBeforeExprs[token];
  1254. }
  1255. function tokenCanStartExpression(token) {
  1256. return tokenStartsExprs[token];
  1257. }
  1258. function tokenIsAssignment(token) {
  1259. return token >= 29 && token <= 33;
  1260. }
  1261. function tokenIsFlowInterfaceOrTypeOrOpaque(token) {
  1262. return token >= 129 && token <= 131;
  1263. }
  1264. function tokenIsLoop(token) {
  1265. return token >= 90 && token <= 92;
  1266. }
  1267. function tokenIsKeyword(token) {
  1268. return token >= 58 && token <= 92;
  1269. }
  1270. function tokenIsOperator(token) {
  1271. return token >= 39 && token <= 59;
  1272. }
  1273. function tokenIsPostfix(token) {
  1274. return token === 34;
  1275. }
  1276. function tokenIsPrefix(token) {
  1277. return tokenPrefixes[token];
  1278. }
  1279. function tokenIsTSTypeOperator(token) {
  1280. return token >= 121 && token <= 123;
  1281. }
  1282. function tokenIsTSDeclarationStart(token) {
  1283. return token >= 124 && token <= 130;
  1284. }
  1285. function tokenLabelName(token) {
  1286. return tokenLabels[token];
  1287. }
  1288. function tokenOperatorPrecedence(token) {
  1289. return tokenBinops[token];
  1290. }
  1291. function tokenIsRightAssociative(token) {
  1292. return token === 57;
  1293. }
  1294. function tokenIsTemplate(token) {
  1295. return token >= 24 && token <= 25;
  1296. }
  1297. function getExportedToken(token) {
  1298. return tokenTypes[token];
  1299. }
  1300. {
  1301. tokenTypes[8].updateContext = context => {
  1302. context.pop();
  1303. };
  1304. tokenTypes[5].updateContext = tokenTypes[7].updateContext = tokenTypes[23].updateContext = context => {
  1305. context.push(types.brace);
  1306. };
  1307. tokenTypes[22].updateContext = context => {
  1308. if (context[context.length - 1] === types.template) {
  1309. context.pop();
  1310. } else {
  1311. context.push(types.template);
  1312. }
  1313. };
  1314. tokenTypes[142].updateContext = context => {
  1315. context.push(types.j_expr, types.j_oTag);
  1316. };
  1317. }
  1318. let nonASCIIidentifierStartChars = "\xaa\xb5\xba\xc0-\xd6\xd8-\xf6\xf8-\u02c1\u02c6-\u02d1\u02e0-\u02e4\u02ec\u02ee\u0370-\u0374\u0376\u0377\u037a-\u037d\u037f\u0386\u0388-\u038a\u038c\u038e-\u03a1\u03a3-\u03f5\u03f7-\u0481\u048a-\u052f\u0531-\u0556\u0559\u0560-\u0588\u05d0-\u05ea\u05ef-\u05f2\u0620-\u064a\u066e\u066f\u0671-\u06d3\u06d5\u06e5\u06e6\u06ee\u06ef\u06fa-\u06fc\u06ff\u0710\u0712-\u072f\u074d-\u07a5\u07b1\u07ca-\u07ea\u07f4\u07f5\u07fa\u0800-\u0815\u081a\u0824\u0828\u0840-\u0858\u0860-\u086a\u0870-\u0887\u0889-\u088e\u08a0-\u08c9\u0904-\u0939\u093d\u0950\u0958-\u0961\u0971-\u0980\u0985-\u098c\u098f\u0990\u0993-\u09a8\u09aa-\u09b0\u09b2\u09b6-\u09b9\u09bd\u09ce\u09dc\u09dd\u09df-\u09e1\u09f0\u09f1\u09fc\u0a05-\u0a0a\u0a0f\u0a10\u0a13-\u0a28\u0a2a-\u0a30\u0a32\u0a33\u0a35\u0a36\u0a38\u0a39\u0a59-\u0a5c\u0a5e\u0a72-\u0a74\u0a85-\u0a8d\u0a8f-\u0a91\u0a93-\u0aa8\u0aaa-\u0ab0\u0ab2\u0ab3\u0ab5-\u0ab9\u0abd\u0ad0\u0ae0\u0ae1\u0af9\u0b05-\u0b0c\u0b0f\u0b10\u0b13-\u0b28\u0b2a-\u0b30\u0b32\u0b33\u0b35-\u0b39\u0b3d\u0b5c\u0b5d\u0b5f-\u0b61\u0b71\u0b83\u0b85-\u0b8a\u0b8e-\u0b90\u0b92-\u0b95\u0b99\u0b9a\u0b9c\u0b9e\u0b9f\u0ba3\u0ba4\u0ba8-\u0baa\u0bae-\u0bb9\u0bd0\u0c05-\u0c0c\u0c0e-\u0c10\u0c12-\u0c28\u0c2a-\u0c39\u0c3d\u0c58-\u0c5a\u0c5d\u0c60\u0c61\u0c80\u0c85-\u0c8c\u0c8e-\u0c90\u0c92-\u0ca8\u0caa-\u0cb3\u0cb5-\u0cb9\u0cbd\u0cdd\u0cde\u0ce0\u0ce1\u0cf1\u0cf2\u0d04-\u0d0c\u0d0e-\u0d10\u0d12-\u0d3a\u0d3d\u0d4e\u0d54-\u0d56\u0d5f-\u0d61\u0d7a-\u0d7f\u0d85-\u0d96\u0d9a-\u0db1\u0db3-\u0dbb\u0dbd\u0dc0-\u0dc6\u0e01-\u0e30\u0e32\u0e33\u0e40-\u0e46\u0e81\u0e82\u0e84\u0e86-\u0e8a\u0e8c-\u0ea3\u0ea5\u0ea7-\u0eb0\u0eb2\u0eb3\u0ebd\u0ec0-\u0ec4\u0ec6\u0edc-\u0edf\u0f00\u0f40-\u0f47\u0f49-\u0f6c\u0f88-\u0f8c\u1000-\u102a\u103f\u1050-\u1055\u105a-\u105d\u1061\u1065\u1066\u106e-\u1070\u1075-\u1081\u108e\u10a0-\u10c5\u10c7\u10cd\u10d0-\u10fa\u10fc-\u1248\u124a-\u124d\u1250-\u1256\u1258\u125a-\u125d\u1260-\u1288\u128a-\u128d\u1290-\u12b0\u12b2-\u12b5\u12b8-\u12be\u12c0\u12c2-\u12c5\u12c8-\u12d6\u12d8-\u1310\u1312-\u1315\u1318-\u135a\u1380-\u138f\u13a0-\u13f5\u13f8-\u13fd\u1401-\u166c\u166f-\u167f\u1681-\u169a\u16a0-\u16ea\u16ee-\u16f8\u1700-\u1711\u171f-\u1731\u1740-\u1751\u1760-\u176c\u176e-\u1770\u1780-\u17b3\u17d7\u17dc\u1820-\u1878\u1880-\u18a8\u18aa\u18b0-\u18f5\u1900-\u191e\u1950-\u196d\u1970-\u1974\u1980-\u19ab\u19b0-\u19c9\u1a00-\u1a16\u1a20-\u1a54\u1aa7\u1b05-\u1b33\u1b45-\u1b4c\u1b83-\u1ba0\u1bae\u1baf\u1bba-\u1be5\u1c00-\u1c23\u1c4d-\u1c4f\u1c5a-\u1c7d\u1c80-\u1c88\u1c90-\u1cba\u1cbd-\u1cbf\u1ce9-\u1cec\u1cee-\u1cf3\u1cf5\u1cf6\u1cfa\u1d00-\u1dbf\u1e00-\u1f15\u1f18-\u1f1d\u1f20-\u1f45\u1f48-\u1f4d\u1f50-\u1f57\u1f59\u1f5b\u1f5d\u1f5f-\u1f7d\u1f80-\u1fb4\u1fb6-\u1fbc\u1fbe\u1fc2-\u1fc4\u1fc6-\u1fcc\u1fd0-\u1fd3\u1fd6-\u1fdb\u1fe0-\u1fec\u1ff2-\u1ff4\u1ff6-\u1ffc\u2071\u207f\u2090-\u209c\u2102\u2107\u210a-\u2113\u2115\u2118-\u211d\u2124\u2126\u2128\u212a-\u2139\u213c-\u213f\u2145-\u2149\u214e\u2160-\u2188\u2c00-\u2ce4\u2ceb-\u2cee\u2cf2\u2cf3\u2d00-\u2d25\u2d27\u2d2d\u2d30-\u2d67\u2d6f\u2d80-\u2d96\u2da0-\u2da6\u2da8-\u2dae\u2db0-\u2db6\u2db8-\u2dbe\u2dc0-\u2dc6\u2dc8-\u2dce\u2dd0-\u2dd6\u2dd8-\u2dde\u3005-\u3007\u3021-\u3029\u3031-\u3035\u3038-\u303c\u3041-\u3096\u309b-\u309f\u30a1-\u30fa\u30fc-\u30ff\u3105-\u312f\u3131-\u318e\u31a0-\u31bf\u31f0-\u31ff\u3400-\u4dbf\u4e00-\ua48c\ua4d0-\ua4fd\ua500-\ua60c\ua610-\ua61f\ua62a\ua62b\ua640-\ua66e\ua67f-\ua69d\ua6a0-\ua6ef\ua717-\ua71f\ua722-\ua788\ua78b-\ua7ca\ua7d0\ua7d1\ua7d3\ua7d5-\ua7d9\ua7f2-\ua801\ua803-\ua805\ua807-\ua80a\ua80c-\ua822\ua840-\ua873\ua882-\ua8b3\ua8f2-\ua8f7\ua8fb\ua8fd\ua8fe\ua90a-\ua925\ua930-\ua946\ua960-\ua97c\ua984-\ua9b2\ua9cf\ua9e0-\ua9e4\ua9e6-\ua9ef\ua9fa-\ua9fe\uaa00-\uaa28\uaa40-\uaa42\uaa44-\uaa4b\uaa60-\uaa76\uaa7a\uaa7e-\uaaaf\uaab1\uaab5\uaab6\uaab9-\uaabd\uaac0\uaac2\uaadb-\uaadd\uaae0-\uaaea\uaaf2-\uaaf4\uab01-\uab06\uab09-\uab0e\uab11-\uab16\uab20-\uab26\uab28-\uab2e\uab30-\uab5a\uab5c-\uab69\uab70-\uabe2\uac00-\ud7a3\ud7b0-\ud7c6\ud7cb-\ud7fb\uf900-\ufa6d\ufa70-\ufad9\ufb00-\ufb06\ufb13-\ufb17\ufb1d\ufb1f-\ufb28\ufb2a-\ufb36\ufb38-\ufb3c\ufb3e\ufb40\ufb41\ufb43\ufb44\ufb46-\ufbb1\ufbd3-\ufd3d\ufd50-\ufd8f\ufd92-\ufdc7\ufdf0-\ufdfb\ufe70-\ufe74\ufe76-\ufefc\uff21-\uff3a\uff41-\uff5a\uff66-\uffbe\uffc2-\uffc7\uffca-\uffcf\uffd2-\uffd7\uffda-\uffdc";
  1319. let nonASCIIidentifierChars = "\u200c\u200d\xb7\u0300-\u036f\u0387\u0483-\u0487\u0591-\u05bd\u05bf\u05c1\u05c2\u05c4\u05c5\u05c7\u0610-\u061a\u064b-\u0669\u0670\u06d6-\u06dc\u06df-\u06e4\u06e7\u06e8\u06ea-\u06ed\u06f0-\u06f9\u0711\u0730-\u074a\u07a6-\u07b0\u07c0-\u07c9\u07eb-\u07f3\u07fd\u0816-\u0819\u081b-\u0823\u0825-\u0827\u0829-\u082d\u0859-\u085b\u0898-\u089f\u08ca-\u08e1\u08e3-\u0903\u093a-\u093c\u093e-\u094f\u0951-\u0957\u0962\u0963\u0966-\u096f\u0981-\u0983\u09bc\u09be-\u09c4\u09c7\u09c8\u09cb-\u09cd\u09d7\u09e2\u09e3\u09e6-\u09ef\u09fe\u0a01-\u0a03\u0a3c\u0a3e-\u0a42\u0a47\u0a48\u0a4b-\u0a4d\u0a51\u0a66-\u0a71\u0a75\u0a81-\u0a83\u0abc\u0abe-\u0ac5\u0ac7-\u0ac9\u0acb-\u0acd\u0ae2\u0ae3\u0ae6-\u0aef\u0afa-\u0aff\u0b01-\u0b03\u0b3c\u0b3e-\u0b44\u0b47\u0b48\u0b4b-\u0b4d\u0b55-\u0b57\u0b62\u0b63\u0b66-\u0b6f\u0b82\u0bbe-\u0bc2\u0bc6-\u0bc8\u0bca-\u0bcd\u0bd7\u0be6-\u0bef\u0c00-\u0c04\u0c3c\u0c3e-\u0c44\u0c46-\u0c48\u0c4a-\u0c4d\u0c55\u0c56\u0c62\u0c63\u0c66-\u0c6f\u0c81-\u0c83\u0cbc\u0cbe-\u0cc4\u0cc6-\u0cc8\u0cca-\u0ccd\u0cd5\u0cd6\u0ce2\u0ce3\u0ce6-\u0cef\u0cf3\u0d00-\u0d03\u0d3b\u0d3c\u0d3e-\u0d44\u0d46-\u0d48\u0d4a-\u0d4d\u0d57\u0d62\u0d63\u0d66-\u0d6f\u0d81-\u0d83\u0dca\u0dcf-\u0dd4\u0dd6\u0dd8-\u0ddf\u0de6-\u0def\u0df2\u0df3\u0e31\u0e34-\u0e3a\u0e47-\u0e4e\u0e50-\u0e59\u0eb1\u0eb4-\u0ebc\u0ec8-\u0ece\u0ed0-\u0ed9\u0f18\u0f19\u0f20-\u0f29\u0f35\u0f37\u0f39\u0f3e\u0f3f\u0f71-\u0f84\u0f86\u0f87\u0f8d-\u0f97\u0f99-\u0fbc\u0fc6\u102b-\u103e\u1040-\u1049\u1056-\u1059\u105e-\u1060\u1062-\u1064\u1067-\u106d\u1071-\u1074\u1082-\u108d\u108f-\u109d\u135d-\u135f\u1369-\u1371\u1712-\u1715\u1732-\u1734\u1752\u1753\u1772\u1773\u17b4-\u17d3\u17dd\u17e0-\u17e9\u180b-\u180d\u180f-\u1819\u18a9\u1920-\u192b\u1930-\u193b\u1946-\u194f\u19d0-\u19da\u1a17-\u1a1b\u1a55-\u1a5e\u1a60-\u1a7c\u1a7f-\u1a89\u1a90-\u1a99\u1ab0-\u1abd\u1abf-\u1ace\u1b00-\u1b04\u1b34-\u1b44\u1b50-\u1b59\u1b6b-\u1b73\u1b80-\u1b82\u1ba1-\u1bad\u1bb0-\u1bb9\u1be6-\u1bf3\u1c24-\u1c37\u1c40-\u1c49\u1c50-\u1c59\u1cd0-\u1cd2\u1cd4-\u1ce8\u1ced\u1cf4\u1cf7-\u1cf9\u1dc0-\u1dff\u200c\u200d\u203f\u2040\u2054\u20d0-\u20dc\u20e1\u20e5-\u20f0\u2cef-\u2cf1\u2d7f\u2de0-\u2dff\u302a-\u302f\u3099\u309a\u30fb\ua620-\ua629\ua66f\ua674-\ua67d\ua69e\ua69f\ua6f0\ua6f1\ua802\ua806\ua80b\ua823-\ua827\ua82c\ua880\ua881\ua8b4-\ua8c5\ua8d0-\ua8d9\ua8e0-\ua8f1\ua8ff-\ua909\ua926-\ua92d\ua947-\ua953\ua980-\ua983\ua9b3-\ua9c0\ua9d0-\ua9d9\ua9e5\ua9f0-\ua9f9\uaa29-\uaa36\uaa43\uaa4c\uaa4d\uaa50-\uaa59\uaa7b-\uaa7d\uaab0\uaab2-\uaab4\uaab7\uaab8\uaabe\uaabf\uaac1\uaaeb-\uaaef\uaaf5\uaaf6\uabe3-\uabea\uabec\uabed\uabf0-\uabf9\ufb1e\ufe00-\ufe0f\ufe20-\ufe2f\ufe33\ufe34\ufe4d-\ufe4f\uff10-\uff19\uff3f\uff65";
  1320. const nonASCIIidentifierStart = new RegExp("[" + nonASCIIidentifierStartChars + "]");
  1321. const nonASCIIidentifier = new RegExp("[" + nonASCIIidentifierStartChars + nonASCIIidentifierChars + "]");
  1322. nonASCIIidentifierStartChars = nonASCIIidentifierChars = null;
  1323. const astralIdentifierStartCodes = [0, 11, 2, 25, 2, 18, 2, 1, 2, 14, 3, 13, 35, 122, 70, 52, 268, 28, 4, 48, 48, 31, 14, 29, 6, 37, 11, 29, 3, 35, 5, 7, 2, 4, 43, 157, 19, 35, 5, 35, 5, 39, 9, 51, 13, 10, 2, 14, 2, 6, 2, 1, 2, 10, 2, 14, 2, 6, 2, 1, 68, 310, 10, 21, 11, 7, 25, 5, 2, 41, 2, 8, 70, 5, 3, 0, 2, 43, 2, 1, 4, 0, 3, 22, 11, 22, 10, 30, 66, 18, 2, 1, 11, 21, 11, 25, 71, 55, 7, 1, 65, 0, 16, 3, 2, 2, 2, 28, 43, 28, 4, 28, 36, 7, 2, 27, 28, 53, 11, 21, 11, 18, 14, 17, 111, 72, 56, 50, 14, 50, 14, 35, 349, 41, 7, 1, 79, 28, 11, 0, 9, 21, 43, 17, 47, 20, 28, 22, 13, 52, 58, 1, 3, 0, 14, 44, 33, 24, 27, 35, 30, 0, 3, 0, 9, 34, 4, 0, 13, 47, 15, 3, 22, 0, 2, 0, 36, 17, 2, 24, 20, 1, 64, 6, 2, 0, 2, 3, 2, 14, 2, 9, 8, 46, 39, 7, 3, 1, 3, 21, 2, 6, 2, 1, 2, 4, 4, 0, 19, 0, 13, 4, 159, 52, 19, 3, 21, 2, 31, 47, 21, 1, 2, 0, 185, 46, 42, 3, 37, 47, 21, 0, 60, 42, 14, 0, 72, 26, 38, 6, 186, 43, 117, 63, 32, 7, 3, 0, 3, 7, 2, 1, 2, 23, 16, 0, 2, 0, 95, 7, 3, 38, 17, 0, 2, 0, 29, 0, 11, 39, 8, 0, 22, 0, 12, 45, 20, 0, 19, 72, 264, 8, 2, 36, 18, 0, 50, 29, 113, 6, 2, 1, 2, 37, 22, 0, 26, 5, 2, 1, 2, 31, 15, 0, 328, 18, 16, 0, 2, 12, 2, 33, 125, 0, 80, 921, 103, 110, 18, 195, 2637, 96, 16, 1071, 18, 5, 4026, 582, 8634, 568, 8, 30, 18, 78, 18, 29, 19, 47, 17, 3, 32, 20, 6, 18, 689, 63, 129, 74, 6, 0, 67, 12, 65, 1, 2, 0, 29, 6135, 9, 1237, 43, 8, 8936, 3, 2, 6, 2, 1, 2, 290, 16, 0, 30, 2, 3, 0, 15, 3, 9, 395, 2309, 106, 6, 12, 4, 8, 8, 9, 5991, 84, 2, 70, 2, 1, 3, 0, 3, 1, 3, 3, 2, 11, 2, 0, 2, 6, 2, 64, 2, 3, 3, 7, 2, 6, 2, 27, 2, 3, 2, 4, 2, 0, 4, 6, 2, 339, 3, 24, 2, 24, 2, 30, 2, 24, 2, 30, 2, 24, 2, 30, 2, 24, 2, 30, 2, 24, 2, 7, 1845, 30, 7, 5, 262, 61, 147, 44, 11, 6, 17, 0, 322, 29, 19, 43, 485, 27, 757, 6, 2, 3, 2, 1, 2, 14, 2, 196, 60, 67, 8, 0, 1205, 3, 2, 26, 2, 1, 2, 0, 3, 0, 2, 9, 2, 3, 2, 0, 2, 0, 7, 0, 5, 0, 2, 0, 2, 0, 2, 2, 2, 1, 2, 0, 3, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 1, 2, 0, 3, 3, 2, 6, 2, 3, 2, 3, 2, 0, 2, 9, 2, 16, 6, 2, 2, 4, 2, 16, 4421, 42719, 33, 4153, 7, 221, 3, 5761, 15, 7472, 16, 621, 2467, 541, 1507, 4938, 6, 4191];
  1324. const astralIdentifierCodes = [509, 0, 227, 0, 150, 4, 294, 9, 1368, 2, 2, 1, 6, 3, 41, 2, 5, 0, 166, 1, 574, 3, 9, 9, 370, 1, 81, 2, 71, 10, 50, 3, 123, 2, 54, 14, 32, 10, 3, 1, 11, 3, 46, 10, 8, 0, 46, 9, 7, 2, 37, 13, 2, 9, 6, 1, 45, 0, 13, 2, 49, 13, 9, 3, 2, 11, 83, 11, 7, 0, 3, 0, 158, 11, 6, 9, 7, 3, 56, 1, 2, 6, 3, 1, 3, 2, 10, 0, 11, 1, 3, 6, 4, 4, 193, 17, 10, 9, 5, 0, 82, 19, 13, 9, 214, 6, 3, 8, 28, 1, 83, 16, 16, 9, 82, 12, 9, 9, 84, 14, 5, 9, 243, 14, 166, 9, 71, 5, 2, 1, 3, 3, 2, 0, 2, 1, 13, 9, 120, 6, 3, 6, 4, 0, 29, 9, 41, 6, 2, 3, 9, 0, 10, 10, 47, 15, 406, 7, 2, 7, 17, 9, 57, 21, 2, 13, 123, 5, 4, 0, 2, 1, 2, 6, 2, 0, 9, 9, 49, 4, 2, 1, 2, 4, 9, 9, 330, 3, 10, 1, 2, 0, 49, 6, 4, 4, 14, 9, 5351, 0, 7, 14, 13835, 9, 87, 9, 39, 4, 60, 6, 26, 9, 1014, 0, 2, 54, 8, 3, 82, 0, 12, 1, 19628, 1, 4706, 45, 3, 22, 543, 4, 4, 5, 9, 7, 3, 6, 31, 3, 149, 2, 1418, 49, 513, 54, 5, 49, 9, 0, 15, 0, 23, 4, 2, 14, 1361, 6, 2, 16, 3, 6, 2, 1, 2, 4, 101, 0, 161, 6, 10, 9, 357, 0, 62, 13, 499, 13, 983, 6, 110, 6, 6, 9, 4759, 9, 787719, 239];
  1325. function isInAstralSet(code, set) {
  1326. let pos = 0x10000;
  1327. for (let i = 0, length = set.length; i < length; i += 2) {
  1328. pos += set[i];
  1329. if (pos > code) return false;
  1330. pos += set[i + 1];
  1331. if (pos >= code) return true;
  1332. }
  1333. return false;
  1334. }
  1335. function isIdentifierStart(code) {
  1336. if (code < 65) return code === 36;
  1337. if (code <= 90) return true;
  1338. if (code < 97) return code === 95;
  1339. if (code <= 122) return true;
  1340. if (code <= 0xffff) {
  1341. return code >= 0xaa && nonASCIIidentifierStart.test(String.fromCharCode(code));
  1342. }
  1343. return isInAstralSet(code, astralIdentifierStartCodes);
  1344. }
  1345. function isIdentifierChar(code) {
  1346. if (code < 48) return code === 36;
  1347. if (code < 58) return true;
  1348. if (code < 65) return false;
  1349. if (code <= 90) return true;
  1350. if (code < 97) return code === 95;
  1351. if (code <= 122) return true;
  1352. if (code <= 0xffff) {
  1353. return code >= 0xaa && nonASCIIidentifier.test(String.fromCharCode(code));
  1354. }
  1355. return isInAstralSet(code, astralIdentifierStartCodes) || isInAstralSet(code, astralIdentifierCodes);
  1356. }
  1357. const reservedWords = {
  1358. keyword: ["break", "case", "catch", "continue", "debugger", "default", "do", "else", "finally", "for", "function", "if", "return", "switch", "throw", "try", "var", "const", "while", "with", "new", "this", "super", "class", "extends", "export", "import", "null", "true", "false", "in", "instanceof", "typeof", "void", "delete"],
  1359. strict: ["implements", "interface", "let", "package", "private", "protected", "public", "static", "yield"],
  1360. strictBind: ["eval", "arguments"]
  1361. };
  1362. const keywords = new Set(reservedWords.keyword);
  1363. const reservedWordsStrictSet = new Set(reservedWords.strict);
  1364. const reservedWordsStrictBindSet = new Set(reservedWords.strictBind);
  1365. function isReservedWord(word, inModule) {
  1366. return inModule && word === "await" || word === "enum";
  1367. }
  1368. function isStrictReservedWord(word, inModule) {
  1369. return isReservedWord(word, inModule) || reservedWordsStrictSet.has(word);
  1370. }
  1371. function isStrictBindOnlyReservedWord(word) {
  1372. return reservedWordsStrictBindSet.has(word);
  1373. }
  1374. function isStrictBindReservedWord(word, inModule) {
  1375. return isStrictReservedWord(word, inModule) || isStrictBindOnlyReservedWord(word);
  1376. }
  1377. function isKeyword(word) {
  1378. return keywords.has(word);
  1379. }
  1380. function isIteratorStart(current, next, next2) {
  1381. return current === 64 && next === 64 && isIdentifierStart(next2);
  1382. }
  1383. const reservedWordLikeSet = new Set(["break", "case", "catch", "continue", "debugger", "default", "do", "else", "finally", "for", "function", "if", "return", "switch", "throw", "try", "var", "const", "while", "with", "new", "this", "super", "class", "extends", "export", "import", "null", "true", "false", "in", "instanceof", "typeof", "void", "delete", "implements", "interface", "let", "package", "private", "protected", "public", "static", "yield", "eval", "arguments", "enum", "await"]);
  1384. function canBeReservedWord(word) {
  1385. return reservedWordLikeSet.has(word);
  1386. }
  1387. class Scope {
  1388. constructor(flags) {
  1389. this.flags = 0;
  1390. this.names = new Map();
  1391. this.firstLexicalName = "";
  1392. this.flags = flags;
  1393. }
  1394. }
  1395. class ScopeHandler {
  1396. constructor(parser, inModule) {
  1397. this.parser = void 0;
  1398. this.scopeStack = [];
  1399. this.inModule = void 0;
  1400. this.undefinedExports = new Map();
  1401. this.parser = parser;
  1402. this.inModule = inModule;
  1403. }
  1404. get inTopLevel() {
  1405. return (this.currentScope().flags & 1) > 0;
  1406. }
  1407. get inFunction() {
  1408. return (this.currentVarScopeFlags() & 2) > 0;
  1409. }
  1410. get allowSuper() {
  1411. return (this.currentThisScopeFlags() & 16) > 0;
  1412. }
  1413. get allowDirectSuper() {
  1414. return (this.currentThisScopeFlags() & 32) > 0;
  1415. }
  1416. get inClass() {
  1417. return (this.currentThisScopeFlags() & 64) > 0;
  1418. }
  1419. get inClassAndNotInNonArrowFunction() {
  1420. const flags = this.currentThisScopeFlags();
  1421. return (flags & 64) > 0 && (flags & 2) === 0;
  1422. }
  1423. get inStaticBlock() {
  1424. for (let i = this.scopeStack.length - 1;; i--) {
  1425. const {
  1426. flags
  1427. } = this.scopeStack[i];
  1428. if (flags & 128) {
  1429. return true;
  1430. }
  1431. if (flags & (387 | 64)) {
  1432. return false;
  1433. }
  1434. }
  1435. }
  1436. get inNonArrowFunction() {
  1437. return (this.currentThisScopeFlags() & 2) > 0;
  1438. }
  1439. get treatFunctionsAsVar() {
  1440. return this.treatFunctionsAsVarInScope(this.currentScope());
  1441. }
  1442. createScope(flags) {
  1443. return new Scope(flags);
  1444. }
  1445. enter(flags) {
  1446. this.scopeStack.push(this.createScope(flags));
  1447. }
  1448. exit() {
  1449. const scope = this.scopeStack.pop();
  1450. return scope.flags;
  1451. }
  1452. treatFunctionsAsVarInScope(scope) {
  1453. return !!(scope.flags & (2 | 128) || !this.parser.inModule && scope.flags & 1);
  1454. }
  1455. declareName(name, bindingType, loc) {
  1456. let scope = this.currentScope();
  1457. if (bindingType & 8 || bindingType & 16) {
  1458. this.checkRedeclarationInScope(scope, name, bindingType, loc);
  1459. let type = scope.names.get(name) || 0;
  1460. if (bindingType & 16) {
  1461. type = type | 4;
  1462. } else {
  1463. if (!scope.firstLexicalName) {
  1464. scope.firstLexicalName = name;
  1465. }
  1466. type = type | 2;
  1467. }
  1468. scope.names.set(name, type);
  1469. if (bindingType & 8) {
  1470. this.maybeExportDefined(scope, name);
  1471. }
  1472. } else if (bindingType & 4) {
  1473. for (let i = this.scopeStack.length - 1; i >= 0; --i) {
  1474. scope = this.scopeStack[i];
  1475. this.checkRedeclarationInScope(scope, name, bindingType, loc);
  1476. scope.names.set(name, (scope.names.get(name) || 0) | 1);
  1477. this.maybeExportDefined(scope, name);
  1478. if (scope.flags & 387) break;
  1479. }
  1480. }
  1481. if (this.parser.inModule && scope.flags & 1) {
  1482. this.undefinedExports.delete(name);
  1483. }
  1484. }
  1485. maybeExportDefined(scope, name) {
  1486. if (this.parser.inModule && scope.flags & 1) {
  1487. this.undefinedExports.delete(name);
  1488. }
  1489. }
  1490. checkRedeclarationInScope(scope, name, bindingType, loc) {
  1491. if (this.isRedeclaredInScope(scope, name, bindingType)) {
  1492. this.parser.raise(Errors.VarRedeclaration, loc, {
  1493. identifierName: name
  1494. });
  1495. }
  1496. }
  1497. isRedeclaredInScope(scope, name, bindingType) {
  1498. if (!(bindingType & 1)) return false;
  1499. if (bindingType & 8) {
  1500. return scope.names.has(name);
  1501. }
  1502. const type = scope.names.get(name);
  1503. if (bindingType & 16) {
  1504. return (type & 2) > 0 || !this.treatFunctionsAsVarInScope(scope) && (type & 1) > 0;
  1505. }
  1506. return (type & 2) > 0 && !(scope.flags & 8 && scope.firstLexicalName === name) || !this.treatFunctionsAsVarInScope(scope) && (type & 4) > 0;
  1507. }
  1508. checkLocalExport(id) {
  1509. const {
  1510. name
  1511. } = id;
  1512. const topLevelScope = this.scopeStack[0];
  1513. if (!topLevelScope.names.has(name)) {
  1514. this.undefinedExports.set(name, id.loc.start);
  1515. }
  1516. }
  1517. currentScope() {
  1518. return this.scopeStack[this.scopeStack.length - 1];
  1519. }
  1520. currentVarScopeFlags() {
  1521. for (let i = this.scopeStack.length - 1;; i--) {
  1522. const {
  1523. flags
  1524. } = this.scopeStack[i];
  1525. if (flags & 387) {
  1526. return flags;
  1527. }
  1528. }
  1529. }
  1530. currentThisScopeFlags() {
  1531. for (let i = this.scopeStack.length - 1;; i--) {
  1532. const {
  1533. flags
  1534. } = this.scopeStack[i];
  1535. if (flags & (387 | 64) && !(flags & 4)) {
  1536. return flags;
  1537. }
  1538. }
  1539. }
  1540. }
  1541. class FlowScope extends Scope {
  1542. constructor(...args) {
  1543. super(...args);
  1544. this.declareFunctions = new Set();
  1545. }
  1546. }
  1547. class FlowScopeHandler extends ScopeHandler {
  1548. createScope(flags) {
  1549. return new FlowScope(flags);
  1550. }
  1551. declareName(name, bindingType, loc) {
  1552. const scope = this.currentScope();
  1553. if (bindingType & 2048) {
  1554. this.checkRedeclarationInScope(scope, name, bindingType, loc);
  1555. this.maybeExportDefined(scope, name);
  1556. scope.declareFunctions.add(name);
  1557. return;
  1558. }
  1559. super.declareName(name, bindingType, loc);
  1560. }
  1561. isRedeclaredInScope(scope, name, bindingType) {
  1562. if (super.isRedeclaredInScope(scope, name, bindingType)) return true;
  1563. if (bindingType & 2048 && !scope.declareFunctions.has(name)) {
  1564. const type = scope.names.get(name);
  1565. return (type & 4) > 0 || (type & 2) > 0;
  1566. }
  1567. return false;
  1568. }
  1569. checkLocalExport(id) {
  1570. if (!this.scopeStack[0].declareFunctions.has(id.name)) {
  1571. super.checkLocalExport(id);
  1572. }
  1573. }
  1574. }
  1575. class BaseParser {
  1576. constructor() {
  1577. this.sawUnambiguousESM = false;
  1578. this.ambiguousScriptDifferentAst = false;
  1579. }
  1580. hasPlugin(pluginConfig) {
  1581. if (typeof pluginConfig === "string") {
  1582. return this.plugins.has(pluginConfig);
  1583. } else {
  1584. const [pluginName, pluginOptions] = pluginConfig;
  1585. if (!this.hasPlugin(pluginName)) {
  1586. return false;
  1587. }
  1588. const actualOptions = this.plugins.get(pluginName);
  1589. for (const key of Object.keys(pluginOptions)) {
  1590. if ((actualOptions == null ? void 0 : actualOptions[key]) !== pluginOptions[key]) {
  1591. return false;
  1592. }
  1593. }
  1594. return true;
  1595. }
  1596. }
  1597. getPluginOption(plugin, name) {
  1598. var _this$plugins$get;
  1599. return (_this$plugins$get = this.plugins.get(plugin)) == null ? void 0 : _this$plugins$get[name];
  1600. }
  1601. }
  1602. function setTrailingComments(node, comments) {
  1603. if (node.trailingComments === undefined) {
  1604. node.trailingComments = comments;
  1605. } else {
  1606. node.trailingComments.unshift(...comments);
  1607. }
  1608. }
  1609. function setLeadingComments(node, comments) {
  1610. if (node.leadingComments === undefined) {
  1611. node.leadingComments = comments;
  1612. } else {
  1613. node.leadingComments.unshift(...comments);
  1614. }
  1615. }
  1616. function setInnerComments(node, comments) {
  1617. if (node.innerComments === undefined) {
  1618. node.innerComments = comments;
  1619. } else {
  1620. node.innerComments.unshift(...comments);
  1621. }
  1622. }
  1623. function adjustInnerComments(node, elements, commentWS) {
  1624. let lastElement = null;
  1625. let i = elements.length;
  1626. while (lastElement === null && i > 0) {
  1627. lastElement = elements[--i];
  1628. }
  1629. if (lastElement === null || lastElement.start > commentWS.start) {
  1630. setInnerComments(node, commentWS.comments);
  1631. } else {
  1632. setTrailingComments(lastElement, commentWS.comments);
  1633. }
  1634. }
  1635. class CommentsParser extends BaseParser {
  1636. addComment(comment) {
  1637. if (this.filename) comment.loc.filename = this.filename;
  1638. const {
  1639. commentsLen
  1640. } = this.state;
  1641. if (this.comments.length !== commentsLen) {
  1642. this.comments.length = commentsLen;
  1643. }
  1644. this.comments.push(comment);
  1645. this.state.commentsLen++;
  1646. }
  1647. processComment(node) {
  1648. const {
  1649. commentStack
  1650. } = this.state;
  1651. const commentStackLength = commentStack.length;
  1652. if (commentStackLength === 0) return;
  1653. let i = commentStackLength - 1;
  1654. const lastCommentWS = commentStack[i];
  1655. if (lastCommentWS.start === node.end) {
  1656. lastCommentWS.leadingNode = node;
  1657. i--;
  1658. }
  1659. const {
  1660. start: nodeStart
  1661. } = node;
  1662. for (; i >= 0; i--) {
  1663. const commentWS = commentStack[i];
  1664. const commentEnd = commentWS.end;
  1665. if (commentEnd > nodeStart) {
  1666. commentWS.containingNode = node;
  1667. this.finalizeComment(commentWS);
  1668. commentStack.splice(i, 1);
  1669. } else {
  1670. if (commentEnd === nodeStart) {
  1671. commentWS.trailingNode = node;
  1672. }
  1673. break;
  1674. }
  1675. }
  1676. }
  1677. finalizeComment(commentWS) {
  1678. const {
  1679. comments
  1680. } = commentWS;
  1681. if (commentWS.leadingNode !== null || commentWS.trailingNode !== null) {
  1682. if (commentWS.leadingNode !== null) {
  1683. setTrailingComments(commentWS.leadingNode, comments);
  1684. }
  1685. if (commentWS.trailingNode !== null) {
  1686. setLeadingComments(commentWS.trailingNode, comments);
  1687. }
  1688. } else {
  1689. const {
  1690. containingNode: node,
  1691. start: commentStart
  1692. } = commentWS;
  1693. if (this.input.charCodeAt(commentStart - 1) === 44) {
  1694. switch (node.type) {
  1695. case "ObjectExpression":
  1696. case "ObjectPattern":
  1697. case "RecordExpression":
  1698. adjustInnerComments(node, node.properties, commentWS);
  1699. break;
  1700. case "CallExpression":
  1701. case "OptionalCallExpression":
  1702. adjustInnerComments(node, node.arguments, commentWS);
  1703. break;
  1704. case "FunctionDeclaration":
  1705. case "FunctionExpression":
  1706. case "ArrowFunctionExpression":
  1707. case "ObjectMethod":
  1708. case "ClassMethod":
  1709. case "ClassPrivateMethod":
  1710. adjustInnerComments(node, node.params, commentWS);
  1711. break;
  1712. case "ArrayExpression":
  1713. case "ArrayPattern":
  1714. case "TupleExpression":
  1715. adjustInnerComments(node, node.elements, commentWS);
  1716. break;
  1717. case "ExportNamedDeclaration":
  1718. case "ImportDeclaration":
  1719. adjustInnerComments(node, node.specifiers, commentWS);
  1720. break;
  1721. default:
  1722. {
  1723. setInnerComments(node, comments);
  1724. }
  1725. }
  1726. } else {
  1727. setInnerComments(node, comments);
  1728. }
  1729. }
  1730. }
  1731. finalizeRemainingComments() {
  1732. const {
  1733. commentStack
  1734. } = this.state;
  1735. for (let i = commentStack.length - 1; i >= 0; i--) {
  1736. this.finalizeComment(commentStack[i]);
  1737. }
  1738. this.state.commentStack = [];
  1739. }
  1740. resetPreviousNodeTrailingComments(node) {
  1741. const {
  1742. commentStack
  1743. } = this.state;
  1744. const {
  1745. length
  1746. } = commentStack;
  1747. if (length === 0) return;
  1748. const commentWS = commentStack[length - 1];
  1749. if (commentWS.leadingNode === node) {
  1750. commentWS.leadingNode = null;
  1751. }
  1752. }
  1753. resetPreviousIdentifierLeadingComments(node) {
  1754. const {
  1755. commentStack
  1756. } = this.state;
  1757. const {
  1758. length
  1759. } = commentStack;
  1760. if (length === 0) return;
  1761. if (commentStack[length - 1].trailingNode === node) {
  1762. commentStack[length - 1].trailingNode = null;
  1763. } else if (length >= 2 && commentStack[length - 2].trailingNode === node) {
  1764. commentStack[length - 2].trailingNode = null;
  1765. }
  1766. }
  1767. takeSurroundingComments(node, start, end) {
  1768. const {
  1769. commentStack
  1770. } = this.state;
  1771. const commentStackLength = commentStack.length;
  1772. if (commentStackLength === 0) return;
  1773. let i = commentStackLength - 1;
  1774. for (; i >= 0; i--) {
  1775. const commentWS = commentStack[i];
  1776. const commentEnd = commentWS.end;
  1777. const commentStart = commentWS.start;
  1778. if (commentStart === end) {
  1779. commentWS.leadingNode = node;
  1780. } else if (commentEnd === start) {
  1781. commentWS.trailingNode = node;
  1782. } else if (commentEnd < start) {
  1783. break;
  1784. }
  1785. }
  1786. }
  1787. }
  1788. const lineBreak = /\r\n?|[\n\u2028\u2029]/;
  1789. const lineBreakG = new RegExp(lineBreak.source, "g");
  1790. function isNewLine(code) {
  1791. switch (code) {
  1792. case 10:
  1793. case 13:
  1794. case 8232:
  1795. case 8233:
  1796. return true;
  1797. default:
  1798. return false;
  1799. }
  1800. }
  1801. const skipWhiteSpace = /(?:\s|\/\/.*|\/\*[^]*?\*\/)*/g;
  1802. const skipWhiteSpaceInLine = /(?:[^\S\n\r\u2028\u2029]|\/\/.*|\/\*.*?\*\/)*/g;
  1803. const skipWhiteSpaceToLineBreak = new RegExp("(?=(" + skipWhiteSpaceInLine.source + "))\\1" + /(?=[\n\r\u2028\u2029]|\/\*(?!.*?\*\/)|$)/.source, "y");
  1804. function isWhitespace(code) {
  1805. switch (code) {
  1806. case 0x0009:
  1807. case 0x000b:
  1808. case 0x000c:
  1809. case 32:
  1810. case 160:
  1811. case 5760:
  1812. case 0x2000:
  1813. case 0x2001:
  1814. case 0x2002:
  1815. case 0x2003:
  1816. case 0x2004:
  1817. case 0x2005:
  1818. case 0x2006:
  1819. case 0x2007:
  1820. case 0x2008:
  1821. case 0x2009:
  1822. case 0x200a:
  1823. case 0x202f:
  1824. case 0x205f:
  1825. case 0x3000:
  1826. case 0xfeff:
  1827. return true;
  1828. default:
  1829. return false;
  1830. }
  1831. }
  1832. class State {
  1833. constructor() {
  1834. this.flags = 1024;
  1835. this.curLine = void 0;
  1836. this.lineStart = void 0;
  1837. this.startLoc = void 0;
  1838. this.endLoc = void 0;
  1839. this.errors = [];
  1840. this.potentialArrowAt = -1;
  1841. this.noArrowAt = [];
  1842. this.noArrowParamsConversionAt = [];
  1843. this.topicContext = {
  1844. maxNumOfResolvableTopics: 0,
  1845. maxTopicIndex: null
  1846. };
  1847. this.labels = [];
  1848. this.commentsLen = 0;
  1849. this.commentStack = [];
  1850. this.pos = 0;
  1851. this.type = 139;
  1852. this.value = null;
  1853. this.start = 0;
  1854. this.end = 0;
  1855. this.lastTokEndLoc = null;
  1856. this.lastTokStartLoc = null;
  1857. this.context = [types.brace];
  1858. this.firstInvalidTemplateEscapePos = null;
  1859. this.strictErrors = new Map();
  1860. this.tokensLength = 0;
  1861. }
  1862. get strict() {
  1863. return (this.flags & 1) > 0;
  1864. }
  1865. set strict(v) {
  1866. if (v) this.flags |= 1;else this.flags &= -2;
  1867. }
  1868. init({
  1869. strictMode,
  1870. sourceType,
  1871. startLine,
  1872. startColumn
  1873. }) {
  1874. this.strict = strictMode === false ? false : strictMode === true ? true : sourceType === "module";
  1875. this.curLine = startLine;
  1876. this.lineStart = -startColumn;
  1877. this.startLoc = this.endLoc = new Position(startLine, startColumn, 0);
  1878. }
  1879. get maybeInArrowParameters() {
  1880. return (this.flags & 2) > 0;
  1881. }
  1882. set maybeInArrowParameters(v) {
  1883. if (v) this.flags |= 2;else this.flags &= -3;
  1884. }
  1885. get inType() {
  1886. return (this.flags & 4) > 0;
  1887. }
  1888. set inType(v) {
  1889. if (v) this.flags |= 4;else this.flags &= -5;
  1890. }
  1891. get noAnonFunctionType() {
  1892. return (this.flags & 8) > 0;
  1893. }
  1894. set noAnonFunctionType(v) {
  1895. if (v) this.flags |= 8;else this.flags &= -9;
  1896. }
  1897. get hasFlowComment() {
  1898. return (this.flags & 16) > 0;
  1899. }
  1900. set hasFlowComment(v) {
  1901. if (v) this.flags |= 16;else this.flags &= -17;
  1902. }
  1903. get isAmbientContext() {
  1904. return (this.flags & 32) > 0;
  1905. }
  1906. set isAmbientContext(v) {
  1907. if (v) this.flags |= 32;else this.flags &= -33;
  1908. }
  1909. get inAbstractClass() {
  1910. return (this.flags & 64) > 0;
  1911. }
  1912. set inAbstractClass(v) {
  1913. if (v) this.flags |= 64;else this.flags &= -65;
  1914. }
  1915. get inDisallowConditionalTypesContext() {
  1916. return (this.flags & 128) > 0;
  1917. }
  1918. set inDisallowConditionalTypesContext(v) {
  1919. if (v) this.flags |= 128;else this.flags &= -129;
  1920. }
  1921. get soloAwait() {
  1922. return (this.flags & 256) > 0;
  1923. }
  1924. set soloAwait(v) {
  1925. if (v) this.flags |= 256;else this.flags &= -257;
  1926. }
  1927. get inFSharpPipelineDirectBody() {
  1928. return (this.flags & 512) > 0;
  1929. }
  1930. set inFSharpPipelineDirectBody(v) {
  1931. if (v) this.flags |= 512;else this.flags &= -513;
  1932. }
  1933. get canStartJSXElement() {
  1934. return (this.flags & 1024) > 0;
  1935. }
  1936. set canStartJSXElement(v) {
  1937. if (v) this.flags |= 1024;else this.flags &= -1025;
  1938. }
  1939. get containsEsc() {
  1940. return (this.flags & 2048) > 0;
  1941. }
  1942. set containsEsc(v) {
  1943. if (v) this.flags |= 2048;else this.flags &= -2049;
  1944. }
  1945. curPosition() {
  1946. return new Position(this.curLine, this.pos - this.lineStart, this.pos);
  1947. }
  1948. clone() {
  1949. const state = new State();
  1950. state.flags = this.flags;
  1951. state.curLine = this.curLine;
  1952. state.lineStart = this.lineStart;
  1953. state.startLoc = this.startLoc;
  1954. state.endLoc = this.endLoc;
  1955. state.errors = this.errors.slice();
  1956. state.potentialArrowAt = this.potentialArrowAt;
  1957. state.noArrowAt = this.noArrowAt.slice();
  1958. state.noArrowParamsConversionAt = this.noArrowParamsConversionAt.slice();
  1959. state.topicContext = this.topicContext;
  1960. state.labels = this.labels.slice();
  1961. state.commentsLen = this.commentsLen;
  1962. state.commentStack = this.commentStack.slice();
  1963. state.pos = this.pos;
  1964. state.type = this.type;
  1965. state.value = this.value;
  1966. state.start = this.start;
  1967. state.end = this.end;
  1968. state.lastTokEndLoc = this.lastTokEndLoc;
  1969. state.lastTokStartLoc = this.lastTokStartLoc;
  1970. state.context = this.context.slice();
  1971. state.firstInvalidTemplateEscapePos = this.firstInvalidTemplateEscapePos;
  1972. state.strictErrors = this.strictErrors;
  1973. state.tokensLength = this.tokensLength;
  1974. return state;
  1975. }
  1976. }
  1977. var _isDigit = function isDigit(code) {
  1978. return code >= 48 && code <= 57;
  1979. };
  1980. const forbiddenNumericSeparatorSiblings = {
  1981. decBinOct: new Set([46, 66, 69, 79, 95, 98, 101, 111]),
  1982. hex: new Set([46, 88, 95, 120])
  1983. };
  1984. const isAllowedNumericSeparatorSibling = {
  1985. bin: ch => ch === 48 || ch === 49,
  1986. oct: ch => ch >= 48 && ch <= 55,
  1987. dec: ch => ch >= 48 && ch <= 57,
  1988. hex: ch => ch >= 48 && ch <= 57 || ch >= 65 && ch <= 70 || ch >= 97 && ch <= 102
  1989. };
  1990. function readStringContents(type, input, pos, lineStart, curLine, errors) {
  1991. const initialPos = pos;
  1992. const initialLineStart = lineStart;
  1993. const initialCurLine = curLine;
  1994. let out = "";
  1995. let firstInvalidLoc = null;
  1996. let chunkStart = pos;
  1997. const {
  1998. length
  1999. } = input;
  2000. for (;;) {
  2001. if (pos >= length) {
  2002. errors.unterminated(initialPos, initialLineStart, initialCurLine);
  2003. out += input.slice(chunkStart, pos);
  2004. break;
  2005. }
  2006. const ch = input.charCodeAt(pos);
  2007. if (isStringEnd(type, ch, input, pos)) {
  2008. out += input.slice(chunkStart, pos);
  2009. break;
  2010. }
  2011. if (ch === 92) {
  2012. out += input.slice(chunkStart, pos);
  2013. const res = readEscapedChar(input, pos, lineStart, curLine, type === "template", errors);
  2014. if (res.ch === null && !firstInvalidLoc) {
  2015. firstInvalidLoc = {
  2016. pos,
  2017. lineStart,
  2018. curLine
  2019. };
  2020. } else {
  2021. out += res.ch;
  2022. }
  2023. ({
  2024. pos,
  2025. lineStart,
  2026. curLine
  2027. } = res);
  2028. chunkStart = pos;
  2029. } else if (ch === 8232 || ch === 8233) {
  2030. ++pos;
  2031. ++curLine;
  2032. lineStart = pos;
  2033. } else if (ch === 10 || ch === 13) {
  2034. if (type === "template") {
  2035. out += input.slice(chunkStart, pos) + "\n";
  2036. ++pos;
  2037. if (ch === 13 && input.charCodeAt(pos) === 10) {
  2038. ++pos;
  2039. }
  2040. ++curLine;
  2041. chunkStart = lineStart = pos;
  2042. } else {
  2043. errors.unterminated(initialPos, initialLineStart, initialCurLine);
  2044. }
  2045. } else {
  2046. ++pos;
  2047. }
  2048. }
  2049. return {
  2050. pos,
  2051. str: out,
  2052. firstInvalidLoc,
  2053. lineStart,
  2054. curLine,
  2055. containsInvalid: !!firstInvalidLoc
  2056. };
  2057. }
  2058. function isStringEnd(type, ch, input, pos) {
  2059. if (type === "template") {
  2060. return ch === 96 || ch === 36 && input.charCodeAt(pos + 1) === 123;
  2061. }
  2062. return ch === (type === "double" ? 34 : 39);
  2063. }
  2064. function readEscapedChar(input, pos, lineStart, curLine, inTemplate, errors) {
  2065. const throwOnInvalid = !inTemplate;
  2066. pos++;
  2067. const res = ch => ({
  2068. pos,
  2069. ch,
  2070. lineStart,
  2071. curLine
  2072. });
  2073. const ch = input.charCodeAt(pos++);
  2074. switch (ch) {
  2075. case 110:
  2076. return res("\n");
  2077. case 114:
  2078. return res("\r");
  2079. case 120:
  2080. {
  2081. let code;
  2082. ({
  2083. code,
  2084. pos
  2085. } = readHexChar(input, pos, lineStart, curLine, 2, false, throwOnInvalid, errors));
  2086. return res(code === null ? null : String.fromCharCode(code));
  2087. }
  2088. case 117:
  2089. {
  2090. let code;
  2091. ({
  2092. code,
  2093. pos
  2094. } = readCodePoint(input, pos, lineStart, curLine, throwOnInvalid, errors));
  2095. return res(code === null ? null : String.fromCodePoint(code));
  2096. }
  2097. case 116:
  2098. return res("\t");
  2099. case 98:
  2100. return res("\b");
  2101. case 118:
  2102. return res("\u000b");
  2103. case 102:
  2104. return res("\f");
  2105. case 13:
  2106. if (input.charCodeAt(pos) === 10) {
  2107. ++pos;
  2108. }
  2109. case 10:
  2110. lineStart = pos;
  2111. ++curLine;
  2112. case 8232:
  2113. case 8233:
  2114. return res("");
  2115. case 56:
  2116. case 57:
  2117. if (inTemplate) {
  2118. return res(null);
  2119. } else {
  2120. errors.strictNumericEscape(pos - 1, lineStart, curLine);
  2121. }
  2122. default:
  2123. if (ch >= 48 && ch <= 55) {
  2124. const startPos = pos - 1;
  2125. const match = input.slice(startPos, pos + 2).match(/^[0-7]+/);
  2126. let octalStr = match[0];
  2127. let octal = parseInt(octalStr, 8);
  2128. if (octal > 255) {
  2129. octalStr = octalStr.slice(0, -1);
  2130. octal = parseInt(octalStr, 8);
  2131. }
  2132. pos += octalStr.length - 1;
  2133. const next = input.charCodeAt(pos);
  2134. if (octalStr !== "0" || next === 56 || next === 57) {
  2135. if (inTemplate) {
  2136. return res(null);
  2137. } else {
  2138. errors.strictNumericEscape(startPos, lineStart, curLine);
  2139. }
  2140. }
  2141. return res(String.fromCharCode(octal));
  2142. }
  2143. return res(String.fromCharCode(ch));
  2144. }
  2145. }
  2146. function readHexChar(input, pos, lineStart, curLine, len, forceLen, throwOnInvalid, errors) {
  2147. const initialPos = pos;
  2148. let n;
  2149. ({
  2150. n,
  2151. pos
  2152. } = readInt(input, pos, lineStart, curLine, 16, len, forceLen, false, errors, !throwOnInvalid));
  2153. if (n === null) {
  2154. if (throwOnInvalid) {
  2155. errors.invalidEscapeSequence(initialPos, lineStart, curLine);
  2156. } else {
  2157. pos = initialPos - 1;
  2158. }
  2159. }
  2160. return {
  2161. code: n,
  2162. pos
  2163. };
  2164. }
  2165. function readInt(input, pos, lineStart, curLine, radix, len, forceLen, allowNumSeparator, errors, bailOnError) {
  2166. const start = pos;
  2167. const forbiddenSiblings = radix === 16 ? forbiddenNumericSeparatorSiblings.hex : forbiddenNumericSeparatorSiblings.decBinOct;
  2168. const isAllowedSibling = radix === 16 ? isAllowedNumericSeparatorSibling.hex : radix === 10 ? isAllowedNumericSeparatorSibling.dec : radix === 8 ? isAllowedNumericSeparatorSibling.oct : isAllowedNumericSeparatorSibling.bin;
  2169. let invalid = false;
  2170. let total = 0;
  2171. for (let i = 0, e = len == null ? Infinity : len; i < e; ++i) {
  2172. const code = input.charCodeAt(pos);
  2173. let val;
  2174. if (code === 95 && allowNumSeparator !== "bail") {
  2175. const prev = input.charCodeAt(pos - 1);
  2176. const next = input.charCodeAt(pos + 1);
  2177. if (!allowNumSeparator) {
  2178. if (bailOnError) return {
  2179. n: null,
  2180. pos
  2181. };
  2182. errors.numericSeparatorInEscapeSequence(pos, lineStart, curLine);
  2183. } else if (Number.isNaN(next) || !isAllowedSibling(next) || forbiddenSiblings.has(prev) || forbiddenSiblings.has(next)) {
  2184. if (bailOnError) return {
  2185. n: null,
  2186. pos
  2187. };
  2188. errors.unexpectedNumericSeparator(pos, lineStart, curLine);
  2189. }
  2190. ++pos;
  2191. continue;
  2192. }
  2193. if (code >= 97) {
  2194. val = code - 97 + 10;
  2195. } else if (code >= 65) {
  2196. val = code - 65 + 10;
  2197. } else if (_isDigit(code)) {
  2198. val = code - 48;
  2199. } else {
  2200. val = Infinity;
  2201. }
  2202. if (val >= radix) {
  2203. if (val <= 9 && bailOnError) {
  2204. return {
  2205. n: null,
  2206. pos
  2207. };
  2208. } else if (val <= 9 && errors.invalidDigit(pos, lineStart, curLine, radix)) {
  2209. val = 0;
  2210. } else if (forceLen) {
  2211. val = 0;
  2212. invalid = true;
  2213. } else {
  2214. break;
  2215. }
  2216. }
  2217. ++pos;
  2218. total = total * radix + val;
  2219. }
  2220. if (pos === start || len != null && pos - start !== len || invalid) {
  2221. return {
  2222. n: null,
  2223. pos
  2224. };
  2225. }
  2226. return {
  2227. n: total,
  2228. pos
  2229. };
  2230. }
  2231. function readCodePoint(input, pos, lineStart, curLine, throwOnInvalid, errors) {
  2232. const ch = input.charCodeAt(pos);
  2233. let code;
  2234. if (ch === 123) {
  2235. ++pos;
  2236. ({
  2237. code,
  2238. pos
  2239. } = readHexChar(input, pos, lineStart, curLine, input.indexOf("}", pos) - pos, true, throwOnInvalid, errors));
  2240. ++pos;
  2241. if (code !== null && code > 0x10ffff) {
  2242. if (throwOnInvalid) {
  2243. errors.invalidCodePoint(pos, lineStart, curLine);
  2244. } else {
  2245. return {
  2246. code: null,
  2247. pos
  2248. };
  2249. }
  2250. }
  2251. } else {
  2252. ({
  2253. code,
  2254. pos
  2255. } = readHexChar(input, pos, lineStart, curLine, 4, false, throwOnInvalid, errors));
  2256. }
  2257. return {
  2258. code,
  2259. pos
  2260. };
  2261. }
  2262. function buildPosition(pos, lineStart, curLine) {
  2263. return new Position(curLine, pos - lineStart, pos);
  2264. }
  2265. const VALID_REGEX_FLAGS = new Set([103, 109, 115, 105, 121, 117, 100, 118]);
  2266. class Token {
  2267. constructor(state) {
  2268. this.type = state.type;
  2269. this.value = state.value;
  2270. this.start = state.start;
  2271. this.end = state.end;
  2272. this.loc = new SourceLocation(state.startLoc, state.endLoc);
  2273. }
  2274. }
  2275. class Tokenizer extends CommentsParser {
  2276. constructor(options, input) {
  2277. super();
  2278. this.isLookahead = void 0;
  2279. this.tokens = [];
  2280. this.errorHandlers_readInt = {
  2281. invalidDigit: (pos, lineStart, curLine, radix) => {
  2282. if (!this.options.errorRecovery) return false;
  2283. this.raise(Errors.InvalidDigit, buildPosition(pos, lineStart, curLine), {
  2284. radix
  2285. });
  2286. return true;
  2287. },
  2288. numericSeparatorInEscapeSequence: this.errorBuilder(Errors.NumericSeparatorInEscapeSequence),
  2289. unexpectedNumericSeparator: this.errorBuilder(Errors.UnexpectedNumericSeparator)
  2290. };
  2291. this.errorHandlers_readCodePoint = Object.assign({}, this.errorHandlers_readInt, {
  2292. invalidEscapeSequence: this.errorBuilder(Errors.InvalidEscapeSequence),
  2293. invalidCodePoint: this.errorBuilder(Errors.InvalidCodePoint)
  2294. });
  2295. this.errorHandlers_readStringContents_string = Object.assign({}, this.errorHandlers_readCodePoint, {
  2296. strictNumericEscape: (pos, lineStart, curLine) => {
  2297. this.recordStrictModeErrors(Errors.StrictNumericEscape, buildPosition(pos, lineStart, curLine));
  2298. },
  2299. unterminated: (pos, lineStart, curLine) => {
  2300. throw this.raise(Errors.UnterminatedString, buildPosition(pos - 1, lineStart, curLine));
  2301. }
  2302. });
  2303. this.errorHandlers_readStringContents_template = Object.assign({}, this.errorHandlers_readCodePoint, {
  2304. strictNumericEscape: this.errorBuilder(Errors.StrictNumericEscape),
  2305. unterminated: (pos, lineStart, curLine) => {
  2306. throw this.raise(Errors.UnterminatedTemplate, buildPosition(pos, lineStart, curLine));
  2307. }
  2308. });
  2309. this.state = new State();
  2310. this.state.init(options);
  2311. this.input = input;
  2312. this.length = input.length;
  2313. this.comments = [];
  2314. this.isLookahead = false;
  2315. }
  2316. pushToken(token) {
  2317. this.tokens.length = this.state.tokensLength;
  2318. this.tokens.push(token);
  2319. ++this.state.tokensLength;
  2320. }
  2321. next() {
  2322. this.checkKeywordEscapes();
  2323. if (this.options.tokens) {
  2324. this.pushToken(new Token(this.state));
  2325. }
  2326. this.state.lastTokEndLoc = this.state.endLoc;
  2327. this.state.lastTokStartLoc = this.state.startLoc;
  2328. this.nextToken();
  2329. }
  2330. eat(type) {
  2331. if (this.match(type)) {
  2332. this.next();
  2333. return true;
  2334. } else {
  2335. return false;
  2336. }
  2337. }
  2338. match(type) {
  2339. return this.state.type === type;
  2340. }
  2341. createLookaheadState(state) {
  2342. return {
  2343. pos: state.pos,
  2344. value: null,
  2345. type: state.type,
  2346. start: state.start,
  2347. end: state.end,
  2348. context: [this.curContext()],
  2349. inType: state.inType,
  2350. startLoc: state.startLoc,
  2351. lastTokEndLoc: state.lastTokEndLoc,
  2352. curLine: state.curLine,
  2353. lineStart: state.lineStart,
  2354. curPosition: state.curPosition
  2355. };
  2356. }
  2357. lookahead() {
  2358. const old = this.state;
  2359. this.state = this.createLookaheadState(old);
  2360. this.isLookahead = true;
  2361. this.nextToken();
  2362. this.isLookahead = false;
  2363. const curr = this.state;
  2364. this.state = old;
  2365. return curr;
  2366. }
  2367. nextTokenStart() {
  2368. return this.nextTokenStartSince(this.state.pos);
  2369. }
  2370. nextTokenStartSince(pos) {
  2371. skipWhiteSpace.lastIndex = pos;
  2372. return skipWhiteSpace.test(this.input) ? skipWhiteSpace.lastIndex : pos;
  2373. }
  2374. lookaheadCharCode() {
  2375. return this.input.charCodeAt(this.nextTokenStart());
  2376. }
  2377. nextTokenInLineStart() {
  2378. return this.nextTokenInLineStartSince(this.state.pos);
  2379. }
  2380. nextTokenInLineStartSince(pos) {
  2381. skipWhiteSpaceInLine.lastIndex = pos;
  2382. return skipWhiteSpaceInLine.test(this.input) ? skipWhiteSpaceInLine.lastIndex : pos;
  2383. }
  2384. lookaheadInLineCharCode() {
  2385. return this.input.charCodeAt(this.nextTokenInLineStart());
  2386. }
  2387. codePointAtPos(pos) {
  2388. let cp = this.input.charCodeAt(pos);
  2389. if ((cp & 0xfc00) === 0xd800 && ++pos < this.input.length) {
  2390. const trail = this.input.charCodeAt(pos);
  2391. if ((trail & 0xfc00) === 0xdc00) {
  2392. cp = 0x10000 + ((cp & 0x3ff) << 10) + (trail & 0x3ff);
  2393. }
  2394. }
  2395. return cp;
  2396. }
  2397. setStrict(strict) {
  2398. this.state.strict = strict;
  2399. if (strict) {
  2400. this.state.strictErrors.forEach(([toParseError, at]) => this.raise(toParseError, at));
  2401. this.state.strictErrors.clear();
  2402. }
  2403. }
  2404. curContext() {
  2405. return this.state.context[this.state.context.length - 1];
  2406. }
  2407. nextToken() {
  2408. this.skipSpace();
  2409. this.state.start = this.state.pos;
  2410. if (!this.isLookahead) this.state.startLoc = this.state.curPosition();
  2411. if (this.state.pos >= this.length) {
  2412. this.finishToken(139);
  2413. return;
  2414. }
  2415. this.getTokenFromCode(this.codePointAtPos(this.state.pos));
  2416. }
  2417. skipBlockComment(commentEnd) {
  2418. let startLoc;
  2419. if (!this.isLookahead) startLoc = this.state.curPosition();
  2420. const start = this.state.pos;
  2421. const end = this.input.indexOf(commentEnd, start + 2);
  2422. if (end === -1) {
  2423. throw this.raise(Errors.UnterminatedComment, this.state.curPosition());
  2424. }
  2425. this.state.pos = end + commentEnd.length;
  2426. lineBreakG.lastIndex = start + 2;
  2427. while (lineBreakG.test(this.input) && lineBreakG.lastIndex <= end) {
  2428. ++this.state.curLine;
  2429. this.state.lineStart = lineBreakG.lastIndex;
  2430. }
  2431. if (this.isLookahead) return;
  2432. const comment = {
  2433. type: "CommentBlock",
  2434. value: this.input.slice(start + 2, end),
  2435. start,
  2436. end: end + commentEnd.length,
  2437. loc: new SourceLocation(startLoc, this.state.curPosition())
  2438. };
  2439. if (this.options.tokens) this.pushToken(comment);
  2440. return comment;
  2441. }
  2442. skipLineComment(startSkip) {
  2443. const start = this.state.pos;
  2444. let startLoc;
  2445. if (!this.isLookahead) startLoc = this.state.curPosition();
  2446. let ch = this.input.charCodeAt(this.state.pos += startSkip);
  2447. if (this.state.pos < this.length) {
  2448. while (!isNewLine(ch) && ++this.state.pos < this.length) {
  2449. ch = this.input.charCodeAt(this.state.pos);
  2450. }
  2451. }
  2452. if (this.isLookahead) return;
  2453. const end = this.state.pos;
  2454. const value = this.input.slice(start + startSkip, end);
  2455. const comment = {
  2456. type: "CommentLine",
  2457. value,
  2458. start,
  2459. end,
  2460. loc: new SourceLocation(startLoc, this.state.curPosition())
  2461. };
  2462. if (this.options.tokens) this.pushToken(comment);
  2463. return comment;
  2464. }
  2465. skipSpace() {
  2466. const spaceStart = this.state.pos;
  2467. const comments = [];
  2468. loop: while (this.state.pos < this.length) {
  2469. const ch = this.input.charCodeAt(this.state.pos);
  2470. switch (ch) {
  2471. case 32:
  2472. case 160:
  2473. case 9:
  2474. ++this.state.pos;
  2475. break;
  2476. case 13:
  2477. if (this.input.charCodeAt(this.state.pos + 1) === 10) {
  2478. ++this.state.pos;
  2479. }
  2480. case 10:
  2481. case 8232:
  2482. case 8233:
  2483. ++this.state.pos;
  2484. ++this.state.curLine;
  2485. this.state.lineStart = this.state.pos;
  2486. break;
  2487. case 47:
  2488. switch (this.input.charCodeAt(this.state.pos + 1)) {
  2489. case 42:
  2490. {
  2491. const comment = this.skipBlockComment("*/");
  2492. if (comment !== undefined) {
  2493. this.addComment(comment);
  2494. if (this.options.attachComment) comments.push(comment);
  2495. }
  2496. break;
  2497. }
  2498. case 47:
  2499. {
  2500. const comment = this.skipLineComment(2);
  2501. if (comment !== undefined) {
  2502. this.addComment(comment);
  2503. if (this.options.attachComment) comments.push(comment);
  2504. }
  2505. break;
  2506. }
  2507. default:
  2508. break loop;
  2509. }
  2510. break;
  2511. default:
  2512. if (isWhitespace(ch)) {
  2513. ++this.state.pos;
  2514. } else if (ch === 45 && !this.inModule && this.options.annexB) {
  2515. const pos = this.state.pos;
  2516. if (this.input.charCodeAt(pos + 1) === 45 && this.input.charCodeAt(pos + 2) === 62 && (spaceStart === 0 || this.state.lineStart > spaceStart)) {
  2517. const comment = this.skipLineComment(3);
  2518. if (comment !== undefined) {
  2519. this.addComment(comment);
  2520. if (this.options.attachComment) comments.push(comment);
  2521. }
  2522. } else {
  2523. break loop;
  2524. }
  2525. } else if (ch === 60 && !this.inModule && this.options.annexB) {
  2526. const pos = this.state.pos;
  2527. if (this.input.charCodeAt(pos + 1) === 33 && this.input.charCodeAt(pos + 2) === 45 && this.input.charCodeAt(pos + 3) === 45) {
  2528. const comment = this.skipLineComment(4);
  2529. if (comment !== undefined) {
  2530. this.addComment(comment);
  2531. if (this.options.attachComment) comments.push(comment);
  2532. }
  2533. } else {
  2534. break loop;
  2535. }
  2536. } else {
  2537. break loop;
  2538. }
  2539. }
  2540. }
  2541. if (comments.length > 0) {
  2542. const end = this.state.pos;
  2543. const commentWhitespace = {
  2544. start: spaceStart,
  2545. end,
  2546. comments,
  2547. leadingNode: null,
  2548. trailingNode: null,
  2549. containingNode: null
  2550. };
  2551. this.state.commentStack.push(commentWhitespace);
  2552. }
  2553. }
  2554. finishToken(type, val) {
  2555. this.state.end = this.state.pos;
  2556. this.state.endLoc = this.state.curPosition();
  2557. const prevType = this.state.type;
  2558. this.state.type = type;
  2559. this.state.value = val;
  2560. if (!this.isLookahead) {
  2561. this.updateContext(prevType);
  2562. }
  2563. }
  2564. replaceToken(type) {
  2565. this.state.type = type;
  2566. this.updateContext();
  2567. }
  2568. readToken_numberSign() {
  2569. if (this.state.pos === 0 && this.readToken_interpreter()) {
  2570. return;
  2571. }
  2572. const nextPos = this.state.pos + 1;
  2573. const next = this.codePointAtPos(nextPos);
  2574. if (next >= 48 && next <= 57) {
  2575. throw this.raise(Errors.UnexpectedDigitAfterHash, this.state.curPosition());
  2576. }
  2577. if (next === 123 || next === 91 && this.hasPlugin("recordAndTuple")) {
  2578. this.expectPlugin("recordAndTuple");
  2579. if (this.getPluginOption("recordAndTuple", "syntaxType") === "bar") {
  2580. throw this.raise(next === 123 ? Errors.RecordExpressionHashIncorrectStartSyntaxType : Errors.TupleExpressionHashIncorrectStartSyntaxType, this.state.curPosition());
  2581. }
  2582. this.state.pos += 2;
  2583. if (next === 123) {
  2584. this.finishToken(7);
  2585. } else {
  2586. this.finishToken(1);
  2587. }
  2588. } else if (isIdentifierStart(next)) {
  2589. ++this.state.pos;
  2590. this.finishToken(138, this.readWord1(next));
  2591. } else if (next === 92) {
  2592. ++this.state.pos;
  2593. this.finishToken(138, this.readWord1());
  2594. } else {
  2595. this.finishOp(27, 1);
  2596. }
  2597. }
  2598. readToken_dot() {
  2599. const next = this.input.charCodeAt(this.state.pos + 1);
  2600. if (next >= 48 && next <= 57) {
  2601. this.readNumber(true);
  2602. return;
  2603. }
  2604. if (next === 46 && this.input.charCodeAt(this.state.pos + 2) === 46) {
  2605. this.state.pos += 3;
  2606. this.finishToken(21);
  2607. } else {
  2608. ++this.state.pos;
  2609. this.finishToken(16);
  2610. }
  2611. }
  2612. readToken_slash() {
  2613. const next = this.input.charCodeAt(this.state.pos + 1);
  2614. if (next === 61) {
  2615. this.finishOp(31, 2);
  2616. } else {
  2617. this.finishOp(56, 1);
  2618. }
  2619. }
  2620. readToken_interpreter() {
  2621. if (this.state.pos !== 0 || this.length < 2) return false;
  2622. let ch = this.input.charCodeAt(this.state.pos + 1);
  2623. if (ch !== 33) return false;
  2624. const start = this.state.pos;
  2625. this.state.pos += 1;
  2626. while (!isNewLine(ch) && ++this.state.pos < this.length) {
  2627. ch = this.input.charCodeAt(this.state.pos);
  2628. }
  2629. const value = this.input.slice(start + 2, this.state.pos);
  2630. this.finishToken(28, value);
  2631. return true;
  2632. }
  2633. readToken_mult_modulo(code) {
  2634. let type = code === 42 ? 55 : 54;
  2635. let width = 1;
  2636. let next = this.input.charCodeAt(this.state.pos + 1);
  2637. if (code === 42 && next === 42) {
  2638. width++;
  2639. next = this.input.charCodeAt(this.state.pos + 2);
  2640. type = 57;
  2641. }
  2642. if (next === 61 && !this.state.inType) {
  2643. width++;
  2644. type = code === 37 ? 33 : 30;
  2645. }
  2646. this.finishOp(type, width);
  2647. }
  2648. readToken_pipe_amp(code) {
  2649. const next = this.input.charCodeAt(this.state.pos + 1);
  2650. if (next === code) {
  2651. if (this.input.charCodeAt(this.state.pos + 2) === 61) {
  2652. this.finishOp(30, 3);
  2653. } else {
  2654. this.finishOp(code === 124 ? 41 : 42, 2);
  2655. }
  2656. return;
  2657. }
  2658. if (code === 124) {
  2659. if (next === 62) {
  2660. this.finishOp(39, 2);
  2661. return;
  2662. }
  2663. if (this.hasPlugin("recordAndTuple") && next === 125) {
  2664. if (this.getPluginOption("recordAndTuple", "syntaxType") !== "bar") {
  2665. throw this.raise(Errors.RecordExpressionBarIncorrectEndSyntaxType, this.state.curPosition());
  2666. }
  2667. this.state.pos += 2;
  2668. this.finishToken(9);
  2669. return;
  2670. }
  2671. if (this.hasPlugin("recordAndTuple") && next === 93) {
  2672. if (this.getPluginOption("recordAndTuple", "syntaxType") !== "bar") {
  2673. throw this.raise(Errors.TupleExpressionBarIncorrectEndSyntaxType, this.state.curPosition());
  2674. }
  2675. this.state.pos += 2;
  2676. this.finishToken(4);
  2677. return;
  2678. }
  2679. }
  2680. if (next === 61) {
  2681. this.finishOp(30, 2);
  2682. return;
  2683. }
  2684. this.finishOp(code === 124 ? 43 : 45, 1);
  2685. }
  2686. readToken_caret() {
  2687. const next = this.input.charCodeAt(this.state.pos + 1);
  2688. if (next === 61 && !this.state.inType) {
  2689. this.finishOp(32, 2);
  2690. } else if (next === 94 && this.hasPlugin(["pipelineOperator", {
  2691. proposal: "hack",
  2692. topicToken: "^^"
  2693. }])) {
  2694. this.finishOp(37, 2);
  2695. const lookaheadCh = this.input.codePointAt(this.state.pos);
  2696. if (lookaheadCh === 94) {
  2697. this.unexpected();
  2698. }
  2699. } else {
  2700. this.finishOp(44, 1);
  2701. }
  2702. }
  2703. readToken_atSign() {
  2704. const next = this.input.charCodeAt(this.state.pos + 1);
  2705. if (next === 64 && this.hasPlugin(["pipelineOperator", {
  2706. proposal: "hack",
  2707. topicToken: "@@"
  2708. }])) {
  2709. this.finishOp(38, 2);
  2710. } else {
  2711. this.finishOp(26, 1);
  2712. }
  2713. }
  2714. readToken_plus_min(code) {
  2715. const next = this.input.charCodeAt(this.state.pos + 1);
  2716. if (next === code) {
  2717. this.finishOp(34, 2);
  2718. return;
  2719. }
  2720. if (next === 61) {
  2721. this.finishOp(30, 2);
  2722. } else {
  2723. this.finishOp(53, 1);
  2724. }
  2725. }
  2726. readToken_lt() {
  2727. const {
  2728. pos
  2729. } = this.state;
  2730. const next = this.input.charCodeAt(pos + 1);
  2731. if (next === 60) {
  2732. if (this.input.charCodeAt(pos + 2) === 61) {
  2733. this.finishOp(30, 3);
  2734. return;
  2735. }
  2736. this.finishOp(51, 2);
  2737. return;
  2738. }
  2739. if (next === 61) {
  2740. this.finishOp(49, 2);
  2741. return;
  2742. }
  2743. this.finishOp(47, 1);
  2744. }
  2745. readToken_gt() {
  2746. const {
  2747. pos
  2748. } = this.state;
  2749. const next = this.input.charCodeAt(pos + 1);
  2750. if (next === 62) {
  2751. const size = this.input.charCodeAt(pos + 2) === 62 ? 3 : 2;
  2752. if (this.input.charCodeAt(pos + size) === 61) {
  2753. this.finishOp(30, size + 1);
  2754. return;
  2755. }
  2756. this.finishOp(52, size);
  2757. return;
  2758. }
  2759. if (next === 61) {
  2760. this.finishOp(49, 2);
  2761. return;
  2762. }
  2763. this.finishOp(48, 1);
  2764. }
  2765. readToken_eq_excl(code) {
  2766. const next = this.input.charCodeAt(this.state.pos + 1);
  2767. if (next === 61) {
  2768. this.finishOp(46, this.input.charCodeAt(this.state.pos + 2) === 61 ? 3 : 2);
  2769. return;
  2770. }
  2771. if (code === 61 && next === 62) {
  2772. this.state.pos += 2;
  2773. this.finishToken(19);
  2774. return;
  2775. }
  2776. this.finishOp(code === 61 ? 29 : 35, 1);
  2777. }
  2778. readToken_question() {
  2779. const next = this.input.charCodeAt(this.state.pos + 1);
  2780. const next2 = this.input.charCodeAt(this.state.pos + 2);
  2781. if (next === 63) {
  2782. if (next2 === 61) {
  2783. this.finishOp(30, 3);
  2784. } else {
  2785. this.finishOp(40, 2);
  2786. }
  2787. } else if (next === 46 && !(next2 >= 48 && next2 <= 57)) {
  2788. this.state.pos += 2;
  2789. this.finishToken(18);
  2790. } else {
  2791. ++this.state.pos;
  2792. this.finishToken(17);
  2793. }
  2794. }
  2795. getTokenFromCode(code) {
  2796. switch (code) {
  2797. case 46:
  2798. this.readToken_dot();
  2799. return;
  2800. case 40:
  2801. ++this.state.pos;
  2802. this.finishToken(10);
  2803. return;
  2804. case 41:
  2805. ++this.state.pos;
  2806. this.finishToken(11);
  2807. return;
  2808. case 59:
  2809. ++this.state.pos;
  2810. this.finishToken(13);
  2811. return;
  2812. case 44:
  2813. ++this.state.pos;
  2814. this.finishToken(12);
  2815. return;
  2816. case 91:
  2817. if (this.hasPlugin("recordAndTuple") && this.input.charCodeAt(this.state.pos + 1) === 124) {
  2818. if (this.getPluginOption("recordAndTuple", "syntaxType") !== "bar") {
  2819. throw this.raise(Errors.TupleExpressionBarIncorrectStartSyntaxType, this.state.curPosition());
  2820. }
  2821. this.state.pos += 2;
  2822. this.finishToken(2);
  2823. } else {
  2824. ++this.state.pos;
  2825. this.finishToken(0);
  2826. }
  2827. return;
  2828. case 93:
  2829. ++this.state.pos;
  2830. this.finishToken(3);
  2831. return;
  2832. case 123:
  2833. if (this.hasPlugin("recordAndTuple") && this.input.charCodeAt(this.state.pos + 1) === 124) {
  2834. if (this.getPluginOption("recordAndTuple", "syntaxType") !== "bar") {
  2835. throw this.raise(Errors.RecordExpressionBarIncorrectStartSyntaxType, this.state.curPosition());
  2836. }
  2837. this.state.pos += 2;
  2838. this.finishToken(6);
  2839. } else {
  2840. ++this.state.pos;
  2841. this.finishToken(5);
  2842. }
  2843. return;
  2844. case 125:
  2845. ++this.state.pos;
  2846. this.finishToken(8);
  2847. return;
  2848. case 58:
  2849. if (this.hasPlugin("functionBind") && this.input.charCodeAt(this.state.pos + 1) === 58) {
  2850. this.finishOp(15, 2);
  2851. } else {
  2852. ++this.state.pos;
  2853. this.finishToken(14);
  2854. }
  2855. return;
  2856. case 63:
  2857. this.readToken_question();
  2858. return;
  2859. case 96:
  2860. this.readTemplateToken();
  2861. return;
  2862. case 48:
  2863. {
  2864. const next = this.input.charCodeAt(this.state.pos + 1);
  2865. if (next === 120 || next === 88) {
  2866. this.readRadixNumber(16);
  2867. return;
  2868. }
  2869. if (next === 111 || next === 79) {
  2870. this.readRadixNumber(8);
  2871. return;
  2872. }
  2873. if (next === 98 || next === 66) {
  2874. this.readRadixNumber(2);
  2875. return;
  2876. }
  2877. }
  2878. case 49:
  2879. case 50:
  2880. case 51:
  2881. case 52:
  2882. case 53:
  2883. case 54:
  2884. case 55:
  2885. case 56:
  2886. case 57:
  2887. this.readNumber(false);
  2888. return;
  2889. case 34:
  2890. case 39:
  2891. this.readString(code);
  2892. return;
  2893. case 47:
  2894. this.readToken_slash();
  2895. return;
  2896. case 37:
  2897. case 42:
  2898. this.readToken_mult_modulo(code);
  2899. return;
  2900. case 124:
  2901. case 38:
  2902. this.readToken_pipe_amp(code);
  2903. return;
  2904. case 94:
  2905. this.readToken_caret();
  2906. return;
  2907. case 43:
  2908. case 45:
  2909. this.readToken_plus_min(code);
  2910. return;
  2911. case 60:
  2912. this.readToken_lt();
  2913. return;
  2914. case 62:
  2915. this.readToken_gt();
  2916. return;
  2917. case 61:
  2918. case 33:
  2919. this.readToken_eq_excl(code);
  2920. return;
  2921. case 126:
  2922. this.finishOp(36, 1);
  2923. return;
  2924. case 64:
  2925. this.readToken_atSign();
  2926. return;
  2927. case 35:
  2928. this.readToken_numberSign();
  2929. return;
  2930. case 92:
  2931. this.readWord();
  2932. return;
  2933. default:
  2934. if (isIdentifierStart(code)) {
  2935. this.readWord(code);
  2936. return;
  2937. }
  2938. }
  2939. throw this.raise(Errors.InvalidOrUnexpectedToken, this.state.curPosition(), {
  2940. unexpected: String.fromCodePoint(code)
  2941. });
  2942. }
  2943. finishOp(type, size) {
  2944. const str = this.input.slice(this.state.pos, this.state.pos + size);
  2945. this.state.pos += size;
  2946. this.finishToken(type, str);
  2947. }
  2948. readRegexp() {
  2949. const startLoc = this.state.startLoc;
  2950. const start = this.state.start + 1;
  2951. let escaped, inClass;
  2952. let {
  2953. pos
  2954. } = this.state;
  2955. for (;; ++pos) {
  2956. if (pos >= this.length) {
  2957. throw this.raise(Errors.UnterminatedRegExp, createPositionWithColumnOffset(startLoc, 1));
  2958. }
  2959. const ch = this.input.charCodeAt(pos);
  2960. if (isNewLine(ch)) {
  2961. throw this.raise(Errors.UnterminatedRegExp, createPositionWithColumnOffset(startLoc, 1));
  2962. }
  2963. if (escaped) {
  2964. escaped = false;
  2965. } else {
  2966. if (ch === 91) {
  2967. inClass = true;
  2968. } else if (ch === 93 && inClass) {
  2969. inClass = false;
  2970. } else if (ch === 47 && !inClass) {
  2971. break;
  2972. }
  2973. escaped = ch === 92;
  2974. }
  2975. }
  2976. const content = this.input.slice(start, pos);
  2977. ++pos;
  2978. let mods = "";
  2979. const nextPos = () => createPositionWithColumnOffset(startLoc, pos + 2 - start);
  2980. while (pos < this.length) {
  2981. const cp = this.codePointAtPos(pos);
  2982. const char = String.fromCharCode(cp);
  2983. if (VALID_REGEX_FLAGS.has(cp)) {
  2984. if (cp === 118) {
  2985. if (mods.includes("u")) {
  2986. this.raise(Errors.IncompatibleRegExpUVFlags, nextPos());
  2987. }
  2988. } else if (cp === 117) {
  2989. if (mods.includes("v")) {
  2990. this.raise(Errors.IncompatibleRegExpUVFlags, nextPos());
  2991. }
  2992. }
  2993. if (mods.includes(char)) {
  2994. this.raise(Errors.DuplicateRegExpFlags, nextPos());
  2995. }
  2996. } else if (isIdentifierChar(cp) || cp === 92) {
  2997. this.raise(Errors.MalformedRegExpFlags, nextPos());
  2998. } else {
  2999. break;
  3000. }
  3001. ++pos;
  3002. mods += char;
  3003. }
  3004. this.state.pos = pos;
  3005. this.finishToken(137, {
  3006. pattern: content,
  3007. flags: mods
  3008. });
  3009. }
  3010. readInt(radix, len, forceLen = false, allowNumSeparator = true) {
  3011. const {
  3012. n,
  3013. pos
  3014. } = readInt(this.input, this.state.pos, this.state.lineStart, this.state.curLine, radix, len, forceLen, allowNumSeparator, this.errorHandlers_readInt, false);
  3015. this.state.pos = pos;
  3016. return n;
  3017. }
  3018. readRadixNumber(radix) {
  3019. const startLoc = this.state.curPosition();
  3020. let isBigInt = false;
  3021. this.state.pos += 2;
  3022. const val = this.readInt(radix);
  3023. if (val == null) {
  3024. this.raise(Errors.InvalidDigit, createPositionWithColumnOffset(startLoc, 2), {
  3025. radix
  3026. });
  3027. }
  3028. const next = this.input.charCodeAt(this.state.pos);
  3029. if (next === 110) {
  3030. ++this.state.pos;
  3031. isBigInt = true;
  3032. } else if (next === 109) {
  3033. throw this.raise(Errors.InvalidDecimal, startLoc);
  3034. }
  3035. if (isIdentifierStart(this.codePointAtPos(this.state.pos))) {
  3036. throw this.raise(Errors.NumberIdentifier, this.state.curPosition());
  3037. }
  3038. if (isBigInt) {
  3039. const str = this.input.slice(startLoc.index, this.state.pos).replace(/[_n]/g, "");
  3040. this.finishToken(135, str);
  3041. return;
  3042. }
  3043. this.finishToken(134, val);
  3044. }
  3045. readNumber(startsWithDot) {
  3046. const start = this.state.pos;
  3047. const startLoc = this.state.curPosition();
  3048. let isFloat = false;
  3049. let isBigInt = false;
  3050. let isDecimal = false;
  3051. let hasExponent = false;
  3052. let isOctal = false;
  3053. if (!startsWithDot && this.readInt(10) === null) {
  3054. this.raise(Errors.InvalidNumber, this.state.curPosition());
  3055. }
  3056. const hasLeadingZero = this.state.pos - start >= 2 && this.input.charCodeAt(start) === 48;
  3057. if (hasLeadingZero) {
  3058. const integer = this.input.slice(start, this.state.pos);
  3059. this.recordStrictModeErrors(Errors.StrictOctalLiteral, startLoc);
  3060. if (!this.state.strict) {
  3061. const underscorePos = integer.indexOf("_");
  3062. if (underscorePos > 0) {
  3063. this.raise(Errors.ZeroDigitNumericSeparator, createPositionWithColumnOffset(startLoc, underscorePos));
  3064. }
  3065. }
  3066. isOctal = hasLeadingZero && !/[89]/.test(integer);
  3067. }
  3068. let next = this.input.charCodeAt(this.state.pos);
  3069. if (next === 46 && !isOctal) {
  3070. ++this.state.pos;
  3071. this.readInt(10);
  3072. isFloat = true;
  3073. next = this.input.charCodeAt(this.state.pos);
  3074. }
  3075. if ((next === 69 || next === 101) && !isOctal) {
  3076. next = this.input.charCodeAt(++this.state.pos);
  3077. if (next === 43 || next === 45) {
  3078. ++this.state.pos;
  3079. }
  3080. if (this.readInt(10) === null) {
  3081. this.raise(Errors.InvalidOrMissingExponent, startLoc);
  3082. }
  3083. isFloat = true;
  3084. hasExponent = true;
  3085. next = this.input.charCodeAt(this.state.pos);
  3086. }
  3087. if (next === 110) {
  3088. if (isFloat || hasLeadingZero) {
  3089. this.raise(Errors.InvalidBigIntLiteral, startLoc);
  3090. }
  3091. ++this.state.pos;
  3092. isBigInt = true;
  3093. }
  3094. if (next === 109) {
  3095. this.expectPlugin("decimal", this.state.curPosition());
  3096. if (hasExponent || hasLeadingZero) {
  3097. this.raise(Errors.InvalidDecimal, startLoc);
  3098. }
  3099. ++this.state.pos;
  3100. isDecimal = true;
  3101. }
  3102. if (isIdentifierStart(this.codePointAtPos(this.state.pos))) {
  3103. throw this.raise(Errors.NumberIdentifier, this.state.curPosition());
  3104. }
  3105. const str = this.input.slice(start, this.state.pos).replace(/[_mn]/g, "");
  3106. if (isBigInt) {
  3107. this.finishToken(135, str);
  3108. return;
  3109. }
  3110. if (isDecimal) {
  3111. this.finishToken(136, str);
  3112. return;
  3113. }
  3114. const val = isOctal ? parseInt(str, 8) : parseFloat(str);
  3115. this.finishToken(134, val);
  3116. }
  3117. readCodePoint(throwOnInvalid) {
  3118. const {
  3119. code,
  3120. pos
  3121. } = readCodePoint(this.input, this.state.pos, this.state.lineStart, this.state.curLine, throwOnInvalid, this.errorHandlers_readCodePoint);
  3122. this.state.pos = pos;
  3123. return code;
  3124. }
  3125. readString(quote) {
  3126. const {
  3127. str,
  3128. pos,
  3129. curLine,
  3130. lineStart
  3131. } = readStringContents(quote === 34 ? "double" : "single", this.input, this.state.pos + 1, this.state.lineStart, this.state.curLine, this.errorHandlers_readStringContents_string);
  3132. this.state.pos = pos + 1;
  3133. this.state.lineStart = lineStart;
  3134. this.state.curLine = curLine;
  3135. this.finishToken(133, str);
  3136. }
  3137. readTemplateContinuation() {
  3138. if (!this.match(8)) {
  3139. this.unexpected(null, 8);
  3140. }
  3141. this.state.pos--;
  3142. this.readTemplateToken();
  3143. }
  3144. readTemplateToken() {
  3145. const opening = this.input[this.state.pos];
  3146. const {
  3147. str,
  3148. firstInvalidLoc,
  3149. pos,
  3150. curLine,
  3151. lineStart
  3152. } = readStringContents("template", this.input, this.state.pos + 1, this.state.lineStart, this.state.curLine, this.errorHandlers_readStringContents_template);
  3153. this.state.pos = pos + 1;
  3154. this.state.lineStart = lineStart;
  3155. this.state.curLine = curLine;
  3156. if (firstInvalidLoc) {
  3157. this.state.firstInvalidTemplateEscapePos = new Position(firstInvalidLoc.curLine, firstInvalidLoc.pos - firstInvalidLoc.lineStart, firstInvalidLoc.pos);
  3158. }
  3159. if (this.input.codePointAt(pos) === 96) {
  3160. this.finishToken(24, firstInvalidLoc ? null : opening + str + "`");
  3161. } else {
  3162. this.state.pos++;
  3163. this.finishToken(25, firstInvalidLoc ? null : opening + str + "${");
  3164. }
  3165. }
  3166. recordStrictModeErrors(toParseError, at) {
  3167. const index = at.index;
  3168. if (this.state.strict && !this.state.strictErrors.has(index)) {
  3169. this.raise(toParseError, at);
  3170. } else {
  3171. this.state.strictErrors.set(index, [toParseError, at]);
  3172. }
  3173. }
  3174. readWord1(firstCode) {
  3175. this.state.containsEsc = false;
  3176. let word = "";
  3177. const start = this.state.pos;
  3178. let chunkStart = this.state.pos;
  3179. if (firstCode !== undefined) {
  3180. this.state.pos += firstCode <= 0xffff ? 1 : 2;
  3181. }
  3182. while (this.state.pos < this.length) {
  3183. const ch = this.codePointAtPos(this.state.pos);
  3184. if (isIdentifierChar(ch)) {
  3185. this.state.pos += ch <= 0xffff ? 1 : 2;
  3186. } else if (ch === 92) {
  3187. this.state.containsEsc = true;
  3188. word += this.input.slice(chunkStart, this.state.pos);
  3189. const escStart = this.state.curPosition();
  3190. const identifierCheck = this.state.pos === start ? isIdentifierStart : isIdentifierChar;
  3191. if (this.input.charCodeAt(++this.state.pos) !== 117) {
  3192. this.raise(Errors.MissingUnicodeEscape, this.state.curPosition());
  3193. chunkStart = this.state.pos - 1;
  3194. continue;
  3195. }
  3196. ++this.state.pos;
  3197. const esc = this.readCodePoint(true);
  3198. if (esc !== null) {
  3199. if (!identifierCheck(esc)) {
  3200. this.raise(Errors.EscapedCharNotAnIdentifier, escStart);
  3201. }
  3202. word += String.fromCodePoint(esc);
  3203. }
  3204. chunkStart = this.state.pos;
  3205. } else {
  3206. break;
  3207. }
  3208. }
  3209. return word + this.input.slice(chunkStart, this.state.pos);
  3210. }
  3211. readWord(firstCode) {
  3212. const word = this.readWord1(firstCode);
  3213. const type = keywords$1.get(word);
  3214. if (type !== undefined) {
  3215. this.finishToken(type, tokenLabelName(type));
  3216. } else {
  3217. this.finishToken(132, word);
  3218. }
  3219. }
  3220. checkKeywordEscapes() {
  3221. const {
  3222. type
  3223. } = this.state;
  3224. if (tokenIsKeyword(type) && this.state.containsEsc) {
  3225. this.raise(Errors.InvalidEscapedReservedWord, this.state.startLoc, {
  3226. reservedWord: tokenLabelName(type)
  3227. });
  3228. }
  3229. }
  3230. raise(toParseError, at, details = {}) {
  3231. const loc = at instanceof Position ? at : at.loc.start;
  3232. const error = toParseError(loc, details);
  3233. if (!this.options.errorRecovery) throw error;
  3234. if (!this.isLookahead) this.state.errors.push(error);
  3235. return error;
  3236. }
  3237. raiseOverwrite(toParseError, at, details = {}) {
  3238. const loc = at instanceof Position ? at : at.loc.start;
  3239. const pos = loc.index;
  3240. const errors = this.state.errors;
  3241. for (let i = errors.length - 1; i >= 0; i--) {
  3242. const error = errors[i];
  3243. if (error.loc.index === pos) {
  3244. return errors[i] = toParseError(loc, details);
  3245. }
  3246. if (error.loc.index < pos) break;
  3247. }
  3248. return this.raise(toParseError, at, details);
  3249. }
  3250. updateContext(prevType) {}
  3251. unexpected(loc, type) {
  3252. throw this.raise(Errors.UnexpectedToken, loc != null ? loc : this.state.startLoc, {
  3253. expected: type ? tokenLabelName(type) : null
  3254. });
  3255. }
  3256. expectPlugin(pluginName, loc) {
  3257. if (this.hasPlugin(pluginName)) {
  3258. return true;
  3259. }
  3260. throw this.raise(Errors.MissingPlugin, loc != null ? loc : this.state.startLoc, {
  3261. missingPlugin: [pluginName]
  3262. });
  3263. }
  3264. expectOnePlugin(pluginNames) {
  3265. if (!pluginNames.some(name => this.hasPlugin(name))) {
  3266. throw this.raise(Errors.MissingOneOfPlugins, this.state.startLoc, {
  3267. missingPlugin: pluginNames
  3268. });
  3269. }
  3270. }
  3271. errorBuilder(error) {
  3272. return (pos, lineStart, curLine) => {
  3273. this.raise(error, buildPosition(pos, lineStart, curLine));
  3274. };
  3275. }
  3276. }
  3277. class ClassScope {
  3278. constructor() {
  3279. this.privateNames = new Set();
  3280. this.loneAccessors = new Map();
  3281. this.undefinedPrivateNames = new Map();
  3282. }
  3283. }
  3284. class ClassScopeHandler {
  3285. constructor(parser) {
  3286. this.parser = void 0;
  3287. this.stack = [];
  3288. this.undefinedPrivateNames = new Map();
  3289. this.parser = parser;
  3290. }
  3291. current() {
  3292. return this.stack[this.stack.length - 1];
  3293. }
  3294. enter() {
  3295. this.stack.push(new ClassScope());
  3296. }
  3297. exit() {
  3298. const oldClassScope = this.stack.pop();
  3299. const current = this.current();
  3300. for (const [name, loc] of Array.from(oldClassScope.undefinedPrivateNames)) {
  3301. if (current) {
  3302. if (!current.undefinedPrivateNames.has(name)) {
  3303. current.undefinedPrivateNames.set(name, loc);
  3304. }
  3305. } else {
  3306. this.parser.raise(Errors.InvalidPrivateFieldResolution, loc, {
  3307. identifierName: name
  3308. });
  3309. }
  3310. }
  3311. }
  3312. declarePrivateName(name, elementType, loc) {
  3313. const {
  3314. privateNames,
  3315. loneAccessors,
  3316. undefinedPrivateNames
  3317. } = this.current();
  3318. let redefined = privateNames.has(name);
  3319. if (elementType & 3) {
  3320. const accessor = redefined && loneAccessors.get(name);
  3321. if (accessor) {
  3322. const oldStatic = accessor & 4;
  3323. const newStatic = elementType & 4;
  3324. const oldKind = accessor & 3;
  3325. const newKind = elementType & 3;
  3326. redefined = oldKind === newKind || oldStatic !== newStatic;
  3327. if (!redefined) loneAccessors.delete(name);
  3328. } else if (!redefined) {
  3329. loneAccessors.set(name, elementType);
  3330. }
  3331. }
  3332. if (redefined) {
  3333. this.parser.raise(Errors.PrivateNameRedeclaration, loc, {
  3334. identifierName: name
  3335. });
  3336. }
  3337. privateNames.add(name);
  3338. undefinedPrivateNames.delete(name);
  3339. }
  3340. usePrivateName(name, loc) {
  3341. let classScope;
  3342. for (classScope of this.stack) {
  3343. if (classScope.privateNames.has(name)) return;
  3344. }
  3345. if (classScope) {
  3346. classScope.undefinedPrivateNames.set(name, loc);
  3347. } else {
  3348. this.parser.raise(Errors.InvalidPrivateFieldResolution, loc, {
  3349. identifierName: name
  3350. });
  3351. }
  3352. }
  3353. }
  3354. class ExpressionScope {
  3355. constructor(type = 0) {
  3356. this.type = type;
  3357. }
  3358. canBeArrowParameterDeclaration() {
  3359. return this.type === 2 || this.type === 1;
  3360. }
  3361. isCertainlyParameterDeclaration() {
  3362. return this.type === 3;
  3363. }
  3364. }
  3365. class ArrowHeadParsingScope extends ExpressionScope {
  3366. constructor(type) {
  3367. super(type);
  3368. this.declarationErrors = new Map();
  3369. }
  3370. recordDeclarationError(ParsingErrorClass, at) {
  3371. const index = at.index;
  3372. this.declarationErrors.set(index, [ParsingErrorClass, at]);
  3373. }
  3374. clearDeclarationError(index) {
  3375. this.declarationErrors.delete(index);
  3376. }
  3377. iterateErrors(iterator) {
  3378. this.declarationErrors.forEach(iterator);
  3379. }
  3380. }
  3381. class ExpressionScopeHandler {
  3382. constructor(parser) {
  3383. this.parser = void 0;
  3384. this.stack = [new ExpressionScope()];
  3385. this.parser = parser;
  3386. }
  3387. enter(scope) {
  3388. this.stack.push(scope);
  3389. }
  3390. exit() {
  3391. this.stack.pop();
  3392. }
  3393. recordParameterInitializerError(toParseError, node) {
  3394. const origin = node.loc.start;
  3395. const {
  3396. stack
  3397. } = this;
  3398. let i = stack.length - 1;
  3399. let scope = stack[i];
  3400. while (!scope.isCertainlyParameterDeclaration()) {
  3401. if (scope.canBeArrowParameterDeclaration()) {
  3402. scope.recordDeclarationError(toParseError, origin);
  3403. } else {
  3404. return;
  3405. }
  3406. scope = stack[--i];
  3407. }
  3408. this.parser.raise(toParseError, origin);
  3409. }
  3410. recordArrowParameterBindingError(error, node) {
  3411. const {
  3412. stack
  3413. } = this;
  3414. const scope = stack[stack.length - 1];
  3415. const origin = node.loc.start;
  3416. if (scope.isCertainlyParameterDeclaration()) {
  3417. this.parser.raise(error, origin);
  3418. } else if (scope.canBeArrowParameterDeclaration()) {
  3419. scope.recordDeclarationError(error, origin);
  3420. } else {
  3421. return;
  3422. }
  3423. }
  3424. recordAsyncArrowParametersError(at) {
  3425. const {
  3426. stack
  3427. } = this;
  3428. let i = stack.length - 1;
  3429. let scope = stack[i];
  3430. while (scope.canBeArrowParameterDeclaration()) {
  3431. if (scope.type === 2) {
  3432. scope.recordDeclarationError(Errors.AwaitBindingIdentifier, at);
  3433. }
  3434. scope = stack[--i];
  3435. }
  3436. }
  3437. validateAsPattern() {
  3438. const {
  3439. stack
  3440. } = this;
  3441. const currentScope = stack[stack.length - 1];
  3442. if (!currentScope.canBeArrowParameterDeclaration()) return;
  3443. currentScope.iterateErrors(([toParseError, loc]) => {
  3444. this.parser.raise(toParseError, loc);
  3445. let i = stack.length - 2;
  3446. let scope = stack[i];
  3447. while (scope.canBeArrowParameterDeclaration()) {
  3448. scope.clearDeclarationError(loc.index);
  3449. scope = stack[--i];
  3450. }
  3451. });
  3452. }
  3453. }
  3454. function newParameterDeclarationScope() {
  3455. return new ExpressionScope(3);
  3456. }
  3457. function newArrowHeadScope() {
  3458. return new ArrowHeadParsingScope(1);
  3459. }
  3460. function newAsyncArrowScope() {
  3461. return new ArrowHeadParsingScope(2);
  3462. }
  3463. function newExpressionScope() {
  3464. return new ExpressionScope();
  3465. }
  3466. class ProductionParameterHandler {
  3467. constructor() {
  3468. this.stacks = [];
  3469. }
  3470. enter(flags) {
  3471. this.stacks.push(flags);
  3472. }
  3473. exit() {
  3474. this.stacks.pop();
  3475. }
  3476. currentFlags() {
  3477. return this.stacks[this.stacks.length - 1];
  3478. }
  3479. get hasAwait() {
  3480. return (this.currentFlags() & 2) > 0;
  3481. }
  3482. get hasYield() {
  3483. return (this.currentFlags() & 1) > 0;
  3484. }
  3485. get hasReturn() {
  3486. return (this.currentFlags() & 4) > 0;
  3487. }
  3488. get hasIn() {
  3489. return (this.currentFlags() & 8) > 0;
  3490. }
  3491. }
  3492. function functionFlags(isAsync, isGenerator) {
  3493. return (isAsync ? 2 : 0) | (isGenerator ? 1 : 0);
  3494. }
  3495. class UtilParser extends Tokenizer {
  3496. addExtra(node, key, value, enumerable = true) {
  3497. if (!node) return;
  3498. const extra = node.extra = node.extra || {};
  3499. if (enumerable) {
  3500. extra[key] = value;
  3501. } else {
  3502. Object.defineProperty(extra, key, {
  3503. enumerable,
  3504. value
  3505. });
  3506. }
  3507. }
  3508. isContextual(token) {
  3509. return this.state.type === token && !this.state.containsEsc;
  3510. }
  3511. isUnparsedContextual(nameStart, name) {
  3512. const nameEnd = nameStart + name.length;
  3513. if (this.input.slice(nameStart, nameEnd) === name) {
  3514. const nextCh = this.input.charCodeAt(nameEnd);
  3515. return !(isIdentifierChar(nextCh) || (nextCh & 0xfc00) === 0xd800);
  3516. }
  3517. return false;
  3518. }
  3519. isLookaheadContextual(name) {
  3520. const next = this.nextTokenStart();
  3521. return this.isUnparsedContextual(next, name);
  3522. }
  3523. eatContextual(token) {
  3524. if (this.isContextual(token)) {
  3525. this.next();
  3526. return true;
  3527. }
  3528. return false;
  3529. }
  3530. expectContextual(token, toParseError) {
  3531. if (!this.eatContextual(token)) {
  3532. if (toParseError != null) {
  3533. throw this.raise(toParseError, this.state.startLoc);
  3534. }
  3535. this.unexpected(null, token);
  3536. }
  3537. }
  3538. canInsertSemicolon() {
  3539. return this.match(139) || this.match(8) || this.hasPrecedingLineBreak();
  3540. }
  3541. hasPrecedingLineBreak() {
  3542. return lineBreak.test(this.input.slice(this.state.lastTokEndLoc.index, this.state.start));
  3543. }
  3544. hasFollowingLineBreak() {
  3545. skipWhiteSpaceToLineBreak.lastIndex = this.state.end;
  3546. return skipWhiteSpaceToLineBreak.test(this.input);
  3547. }
  3548. isLineTerminator() {
  3549. return this.eat(13) || this.canInsertSemicolon();
  3550. }
  3551. semicolon(allowAsi = true) {
  3552. if (allowAsi ? this.isLineTerminator() : this.eat(13)) return;
  3553. this.raise(Errors.MissingSemicolon, this.state.lastTokEndLoc);
  3554. }
  3555. expect(type, loc) {
  3556. this.eat(type) || this.unexpected(loc, type);
  3557. }
  3558. tryParse(fn, oldState = this.state.clone()) {
  3559. const abortSignal = {
  3560. node: null
  3561. };
  3562. try {
  3563. const node = fn((node = null) => {
  3564. abortSignal.node = node;
  3565. throw abortSignal;
  3566. });
  3567. if (this.state.errors.length > oldState.errors.length) {
  3568. const failState = this.state;
  3569. this.state = oldState;
  3570. this.state.tokensLength = failState.tokensLength;
  3571. return {
  3572. node,
  3573. error: failState.errors[oldState.errors.length],
  3574. thrown: false,
  3575. aborted: false,
  3576. failState
  3577. };
  3578. }
  3579. return {
  3580. node,
  3581. error: null,
  3582. thrown: false,
  3583. aborted: false,
  3584. failState: null
  3585. };
  3586. } catch (error) {
  3587. const failState = this.state;
  3588. this.state = oldState;
  3589. if (error instanceof SyntaxError) {
  3590. return {
  3591. node: null,
  3592. error,
  3593. thrown: true,
  3594. aborted: false,
  3595. failState
  3596. };
  3597. }
  3598. if (error === abortSignal) {
  3599. return {
  3600. node: abortSignal.node,
  3601. error: null,
  3602. thrown: false,
  3603. aborted: true,
  3604. failState
  3605. };
  3606. }
  3607. throw error;
  3608. }
  3609. }
  3610. checkExpressionErrors(refExpressionErrors, andThrow) {
  3611. if (!refExpressionErrors) return false;
  3612. const {
  3613. shorthandAssignLoc,
  3614. doubleProtoLoc,
  3615. privateKeyLoc,
  3616. optionalParametersLoc
  3617. } = refExpressionErrors;
  3618. const hasErrors = !!shorthandAssignLoc || !!doubleProtoLoc || !!optionalParametersLoc || !!privateKeyLoc;
  3619. if (!andThrow) {
  3620. return hasErrors;
  3621. }
  3622. if (shorthandAssignLoc != null) {
  3623. this.raise(Errors.InvalidCoverInitializedName, shorthandAssignLoc);
  3624. }
  3625. if (doubleProtoLoc != null) {
  3626. this.raise(Errors.DuplicateProto, doubleProtoLoc);
  3627. }
  3628. if (privateKeyLoc != null) {
  3629. this.raise(Errors.UnexpectedPrivateField, privateKeyLoc);
  3630. }
  3631. if (optionalParametersLoc != null) {
  3632. this.unexpected(optionalParametersLoc);
  3633. }
  3634. }
  3635. isLiteralPropertyName() {
  3636. return tokenIsLiteralPropertyName(this.state.type);
  3637. }
  3638. isPrivateName(node) {
  3639. return node.type === "PrivateName";
  3640. }
  3641. getPrivateNameSV(node) {
  3642. return node.id.name;
  3643. }
  3644. hasPropertyAsPrivateName(node) {
  3645. return (node.type === "MemberExpression" || node.type === "OptionalMemberExpression") && this.isPrivateName(node.property);
  3646. }
  3647. isObjectProperty(node) {
  3648. return node.type === "ObjectProperty";
  3649. }
  3650. isObjectMethod(node) {
  3651. return node.type === "ObjectMethod";
  3652. }
  3653. initializeScopes(inModule = this.options.sourceType === "module") {
  3654. const oldLabels = this.state.labels;
  3655. this.state.labels = [];
  3656. const oldExportedIdentifiers = this.exportedIdentifiers;
  3657. this.exportedIdentifiers = new Set();
  3658. const oldInModule = this.inModule;
  3659. this.inModule = inModule;
  3660. const oldScope = this.scope;
  3661. const ScopeHandler = this.getScopeHandler();
  3662. this.scope = new ScopeHandler(this, inModule);
  3663. const oldProdParam = this.prodParam;
  3664. this.prodParam = new ProductionParameterHandler();
  3665. const oldClassScope = this.classScope;
  3666. this.classScope = new ClassScopeHandler(this);
  3667. const oldExpressionScope = this.expressionScope;
  3668. this.expressionScope = new ExpressionScopeHandler(this);
  3669. return () => {
  3670. this.state.labels = oldLabels;
  3671. this.exportedIdentifiers = oldExportedIdentifiers;
  3672. this.inModule = oldInModule;
  3673. this.scope = oldScope;
  3674. this.prodParam = oldProdParam;
  3675. this.classScope = oldClassScope;
  3676. this.expressionScope = oldExpressionScope;
  3677. };
  3678. }
  3679. enterInitialScopes() {
  3680. let paramFlags = 0;
  3681. if (this.inModule) {
  3682. paramFlags |= 2;
  3683. }
  3684. this.scope.enter(1);
  3685. this.prodParam.enter(paramFlags);
  3686. }
  3687. checkDestructuringPrivate(refExpressionErrors) {
  3688. const {
  3689. privateKeyLoc
  3690. } = refExpressionErrors;
  3691. if (privateKeyLoc !== null) {
  3692. this.expectPlugin("destructuringPrivate", privateKeyLoc);
  3693. }
  3694. }
  3695. }
  3696. class ExpressionErrors {
  3697. constructor() {
  3698. this.shorthandAssignLoc = null;
  3699. this.doubleProtoLoc = null;
  3700. this.privateKeyLoc = null;
  3701. this.optionalParametersLoc = null;
  3702. }
  3703. }
  3704. class Node {
  3705. constructor(parser, pos, loc) {
  3706. this.type = "";
  3707. this.start = pos;
  3708. this.end = 0;
  3709. this.loc = new SourceLocation(loc);
  3710. if (parser != null && parser.options.ranges) this.range = [pos, 0];
  3711. if (parser != null && parser.filename) this.loc.filename = parser.filename;
  3712. }
  3713. }
  3714. const NodePrototype = Node.prototype;
  3715. {
  3716. NodePrototype.__clone = function () {
  3717. const newNode = new Node(undefined, this.start, this.loc.start);
  3718. const keys = Object.keys(this);
  3719. for (let i = 0, length = keys.length; i < length; i++) {
  3720. const key = keys[i];
  3721. if (key !== "leadingComments" && key !== "trailingComments" && key !== "innerComments") {
  3722. newNode[key] = this[key];
  3723. }
  3724. }
  3725. return newNode;
  3726. };
  3727. }
  3728. function clonePlaceholder(node) {
  3729. return cloneIdentifier(node);
  3730. }
  3731. function cloneIdentifier(node) {
  3732. const {
  3733. type,
  3734. start,
  3735. end,
  3736. loc,
  3737. range,
  3738. extra,
  3739. name
  3740. } = node;
  3741. const cloned = Object.create(NodePrototype);
  3742. cloned.type = type;
  3743. cloned.start = start;
  3744. cloned.end = end;
  3745. cloned.loc = loc;
  3746. cloned.range = range;
  3747. cloned.extra = extra;
  3748. cloned.name = name;
  3749. if (type === "Placeholder") {
  3750. cloned.expectedNode = node.expectedNode;
  3751. }
  3752. return cloned;
  3753. }
  3754. function cloneStringLiteral(node) {
  3755. const {
  3756. type,
  3757. start,
  3758. end,
  3759. loc,
  3760. range,
  3761. extra
  3762. } = node;
  3763. if (type === "Placeholder") {
  3764. return clonePlaceholder(node);
  3765. }
  3766. const cloned = Object.create(NodePrototype);
  3767. cloned.type = type;
  3768. cloned.start = start;
  3769. cloned.end = end;
  3770. cloned.loc = loc;
  3771. cloned.range = range;
  3772. if (node.raw !== undefined) {
  3773. cloned.raw = node.raw;
  3774. } else {
  3775. cloned.extra = extra;
  3776. }
  3777. cloned.value = node.value;
  3778. return cloned;
  3779. }
  3780. class NodeUtils extends UtilParser {
  3781. startNode() {
  3782. const loc = this.state.startLoc;
  3783. return new Node(this, loc.index, loc);
  3784. }
  3785. startNodeAt(loc) {
  3786. return new Node(this, loc.index, loc);
  3787. }
  3788. startNodeAtNode(type) {
  3789. return this.startNodeAt(type.loc.start);
  3790. }
  3791. finishNode(node, type) {
  3792. return this.finishNodeAt(node, type, this.state.lastTokEndLoc);
  3793. }
  3794. finishNodeAt(node, type, endLoc) {
  3795. node.type = type;
  3796. node.end = endLoc.index;
  3797. node.loc.end = endLoc;
  3798. if (this.options.ranges) node.range[1] = endLoc.index;
  3799. if (this.options.attachComment) this.processComment(node);
  3800. return node;
  3801. }
  3802. resetStartLocation(node, startLoc) {
  3803. node.start = startLoc.index;
  3804. node.loc.start = startLoc;
  3805. if (this.options.ranges) node.range[0] = startLoc.index;
  3806. }
  3807. resetEndLocation(node, endLoc = this.state.lastTokEndLoc) {
  3808. node.end = endLoc.index;
  3809. node.loc.end = endLoc;
  3810. if (this.options.ranges) node.range[1] = endLoc.index;
  3811. }
  3812. resetStartLocationFromNode(node, locationNode) {
  3813. this.resetStartLocation(node, locationNode.loc.start);
  3814. }
  3815. }
  3816. const reservedTypes = new Set(["_", "any", "bool", "boolean", "empty", "extends", "false", "interface", "mixed", "null", "number", "static", "string", "true", "typeof", "void"]);
  3817. const FlowErrors = ParseErrorEnum`flow`({
  3818. AmbiguousConditionalArrow: "Ambiguous expression: wrap the arrow functions in parentheses to disambiguate.",
  3819. AmbiguousDeclareModuleKind: "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.",
  3820. AssignReservedType: ({
  3821. reservedType
  3822. }) => `Cannot overwrite reserved type ${reservedType}.`,
  3823. DeclareClassElement: "The `declare` modifier can only appear on class fields.",
  3824. DeclareClassFieldInitializer: "Initializers are not allowed in fields with the `declare` modifier.",
  3825. DuplicateDeclareModuleExports: "Duplicate `declare module.exports` statement.",
  3826. EnumBooleanMemberNotInitialized: ({
  3827. memberName,
  3828. enumName
  3829. }) => `Boolean enum members need to be initialized. Use either \`${memberName} = true,\` or \`${memberName} = false,\` in enum \`${enumName}\`.`,
  3830. EnumDuplicateMemberName: ({
  3831. memberName,
  3832. enumName
  3833. }) => `Enum member names need to be unique, but the name \`${memberName}\` has already been used before in enum \`${enumName}\`.`,
  3834. EnumInconsistentMemberValues: ({
  3835. enumName
  3836. }) => `Enum \`${enumName}\` has inconsistent member initializers. Either use no initializers, or consistently use literals (either booleans, numbers, or strings) for all member initializers.`,
  3837. EnumInvalidExplicitType: ({
  3838. invalidEnumType,
  3839. enumName
  3840. }) => `Enum type \`${invalidEnumType}\` is not valid. Use one of \`boolean\`, \`number\`, \`string\`, or \`symbol\` in enum \`${enumName}\`.`,
  3841. EnumInvalidExplicitTypeUnknownSupplied: ({
  3842. enumName
  3843. }) => `Supplied enum type is not valid. Use one of \`boolean\`, \`number\`, \`string\`, or \`symbol\` in enum \`${enumName}\`.`,
  3844. EnumInvalidMemberInitializerPrimaryType: ({
  3845. enumName,
  3846. memberName,
  3847. explicitType
  3848. }) => `Enum \`${enumName}\` has type \`${explicitType}\`, so the initializer of \`${memberName}\` needs to be a ${explicitType} literal.`,
  3849. EnumInvalidMemberInitializerSymbolType: ({
  3850. enumName,
  3851. memberName
  3852. }) => `Symbol enum members cannot be initialized. Use \`${memberName},\` in enum \`${enumName}\`.`,
  3853. EnumInvalidMemberInitializerUnknownType: ({
  3854. enumName,
  3855. memberName
  3856. }) => `The enum member initializer for \`${memberName}\` needs to be a literal (either a boolean, number, or string) in enum \`${enumName}\`.`,
  3857. EnumInvalidMemberName: ({
  3858. enumName,
  3859. memberName,
  3860. suggestion
  3861. }) => `Enum member names cannot start with lowercase 'a' through 'z'. Instead of using \`${memberName}\`, consider using \`${suggestion}\`, in enum \`${enumName}\`.`,
  3862. EnumNumberMemberNotInitialized: ({
  3863. enumName,
  3864. memberName
  3865. }) => `Number enum members need to be initialized, e.g. \`${memberName} = 1\` in enum \`${enumName}\`.`,
  3866. EnumStringMemberInconsistentlyInitialized: ({
  3867. enumName
  3868. }) => `String enum members need to consistently either all use initializers, or use no initializers, in enum \`${enumName}\`.`,
  3869. GetterMayNotHaveThisParam: "A getter cannot have a `this` parameter.",
  3870. ImportReflectionHasImportType: "An `import module` declaration can not use `type` or `typeof` keyword.",
  3871. ImportTypeShorthandOnlyInPureImport: "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.",
  3872. InexactInsideExact: "Explicit inexact syntax cannot appear inside an explicit exact object type.",
  3873. InexactInsideNonObject: "Explicit inexact syntax cannot appear in class or interface definitions.",
  3874. InexactVariance: "Explicit inexact syntax cannot have variance.",
  3875. InvalidNonTypeImportInDeclareModule: "Imports within a `declare module` body must always be `import type` or `import typeof`.",
  3876. MissingTypeParamDefault: "Type parameter declaration needs a default, since a preceding type parameter declaration has a default.",
  3877. NestedDeclareModule: "`declare module` cannot be used inside another `declare module`.",
  3878. NestedFlowComment: "Cannot have a flow comment inside another flow comment.",
  3879. PatternIsOptional: Object.assign({
  3880. message: "A binding pattern parameter cannot be optional in an implementation signature."
  3881. }, {
  3882. reasonCode: "OptionalBindingPattern"
  3883. }),
  3884. SetterMayNotHaveThisParam: "A setter cannot have a `this` parameter.",
  3885. SpreadVariance: "Spread properties cannot have variance.",
  3886. ThisParamAnnotationRequired: "A type annotation is required for the `this` parameter.",
  3887. ThisParamBannedInConstructor: "Constructors cannot have a `this` parameter; constructors don't bind `this` like other functions.",
  3888. ThisParamMayNotBeOptional: "The `this` parameter cannot be optional.",
  3889. ThisParamMustBeFirst: "The `this` parameter must be the first function parameter.",
  3890. ThisParamNoDefault: "The `this` parameter may not have a default value.",
  3891. TypeBeforeInitializer: "Type annotations must come before default assignments, e.g. instead of `age = 25: number` use `age: number = 25`.",
  3892. TypeCastInPattern: "The type cast expression is expected to be wrapped with parenthesis.",
  3893. UnexpectedExplicitInexactInObject: "Explicit inexact syntax must appear at the end of an inexact object.",
  3894. UnexpectedReservedType: ({
  3895. reservedType
  3896. }) => `Unexpected reserved type ${reservedType}.`,
  3897. UnexpectedReservedUnderscore: "`_` is only allowed as a type argument to call or new.",
  3898. UnexpectedSpaceBetweenModuloChecks: "Spaces between `%` and `checks` are not allowed here.",
  3899. UnexpectedSpreadType: "Spread operator cannot appear in class or interface definitions.",
  3900. UnexpectedSubtractionOperand: 'Unexpected token, expected "number" or "bigint".',
  3901. UnexpectedTokenAfterTypeParameter: "Expected an arrow function after this type parameter declaration.",
  3902. UnexpectedTypeParameterBeforeAsyncArrowFunction: "Type parameters must come after the async keyword, e.g. instead of `<T> async () => {}`, use `async <T>() => {}`.",
  3903. UnsupportedDeclareExportKind: ({
  3904. unsupportedExportKind,
  3905. suggestion
  3906. }) => `\`declare export ${unsupportedExportKind}\` is not supported. Use \`${suggestion}\` instead.`,
  3907. UnsupportedStatementInDeclareModule: "Only declares and type imports are allowed inside declare module.",
  3908. UnterminatedFlowComment: "Unterminated flow-comment."
  3909. });
  3910. function isEsModuleType(bodyElement) {
  3911. return bodyElement.type === "DeclareExportAllDeclaration" || bodyElement.type === "DeclareExportDeclaration" && (!bodyElement.declaration || bodyElement.declaration.type !== "TypeAlias" && bodyElement.declaration.type !== "InterfaceDeclaration");
  3912. }
  3913. function hasTypeImportKind(node) {
  3914. return node.importKind === "type" || node.importKind === "typeof";
  3915. }
  3916. const exportSuggestions = {
  3917. const: "declare export var",
  3918. let: "declare export var",
  3919. type: "export type",
  3920. interface: "export interface"
  3921. };
  3922. function partition(list, test) {
  3923. const list1 = [];
  3924. const list2 = [];
  3925. for (let i = 0; i < list.length; i++) {
  3926. (test(list[i], i, list) ? list1 : list2).push(list[i]);
  3927. }
  3928. return [list1, list2];
  3929. }
  3930. const FLOW_PRAGMA_REGEX = /\*?\s*@((?:no)?flow)\b/;
  3931. var flow = superClass => class FlowParserMixin extends superClass {
  3932. constructor(...args) {
  3933. super(...args);
  3934. this.flowPragma = undefined;
  3935. }
  3936. getScopeHandler() {
  3937. return FlowScopeHandler;
  3938. }
  3939. shouldParseTypes() {
  3940. return this.getPluginOption("flow", "all") || this.flowPragma === "flow";
  3941. }
  3942. shouldParseEnums() {
  3943. return !!this.getPluginOption("flow", "enums");
  3944. }
  3945. finishToken(type, val) {
  3946. if (type !== 133 && type !== 13 && type !== 28) {
  3947. if (this.flowPragma === undefined) {
  3948. this.flowPragma = null;
  3949. }
  3950. }
  3951. super.finishToken(type, val);
  3952. }
  3953. addComment(comment) {
  3954. if (this.flowPragma === undefined) {
  3955. const matches = FLOW_PRAGMA_REGEX.exec(comment.value);
  3956. if (!matches) ;else if (matches[1] === "flow") {
  3957. this.flowPragma = "flow";
  3958. } else if (matches[1] === "noflow") {
  3959. this.flowPragma = "noflow";
  3960. } else {
  3961. throw new Error("Unexpected flow pragma");
  3962. }
  3963. }
  3964. super.addComment(comment);
  3965. }
  3966. flowParseTypeInitialiser(tok) {
  3967. const oldInType = this.state.inType;
  3968. this.state.inType = true;
  3969. this.expect(tok || 14);
  3970. const type = this.flowParseType();
  3971. this.state.inType = oldInType;
  3972. return type;
  3973. }
  3974. flowParsePredicate() {
  3975. const node = this.startNode();
  3976. const moduloLoc = this.state.startLoc;
  3977. this.next();
  3978. this.expectContextual(110);
  3979. if (this.state.lastTokStartLoc.index > moduloLoc.index + 1) {
  3980. this.raise(FlowErrors.UnexpectedSpaceBetweenModuloChecks, moduloLoc);
  3981. }
  3982. if (this.eat(10)) {
  3983. node.value = super.parseExpression();
  3984. this.expect(11);
  3985. return this.finishNode(node, "DeclaredPredicate");
  3986. } else {
  3987. return this.finishNode(node, "InferredPredicate");
  3988. }
  3989. }
  3990. flowParseTypeAndPredicateInitialiser() {
  3991. const oldInType = this.state.inType;
  3992. this.state.inType = true;
  3993. this.expect(14);
  3994. let type = null;
  3995. let predicate = null;
  3996. if (this.match(54)) {
  3997. this.state.inType = oldInType;
  3998. predicate = this.flowParsePredicate();
  3999. } else {
  4000. type = this.flowParseType();
  4001. this.state.inType = oldInType;
  4002. if (this.match(54)) {
  4003. predicate = this.flowParsePredicate();
  4004. }
  4005. }
  4006. return [type, predicate];
  4007. }
  4008. flowParseDeclareClass(node) {
  4009. this.next();
  4010. this.flowParseInterfaceish(node, true);
  4011. return this.finishNode(node, "DeclareClass");
  4012. }
  4013. flowParseDeclareFunction(node) {
  4014. this.next();
  4015. const id = node.id = this.parseIdentifier();
  4016. const typeNode = this.startNode();
  4017. const typeContainer = this.startNode();
  4018. if (this.match(47)) {
  4019. typeNode.typeParameters = this.flowParseTypeParameterDeclaration();
  4020. } else {
  4021. typeNode.typeParameters = null;
  4022. }
  4023. this.expect(10);
  4024. const tmp = this.flowParseFunctionTypeParams();
  4025. typeNode.params = tmp.params;
  4026. typeNode.rest = tmp.rest;
  4027. typeNode.this = tmp._this;
  4028. this.expect(11);
  4029. [typeNode.returnType, node.predicate] = this.flowParseTypeAndPredicateInitialiser();
  4030. typeContainer.typeAnnotation = this.finishNode(typeNode, "FunctionTypeAnnotation");
  4031. id.typeAnnotation = this.finishNode(typeContainer, "TypeAnnotation");
  4032. this.resetEndLocation(id);
  4033. this.semicolon();
  4034. this.scope.declareName(node.id.name, 2048, node.id.loc.start);
  4035. return this.finishNode(node, "DeclareFunction");
  4036. }
  4037. flowParseDeclare(node, insideModule) {
  4038. if (this.match(80)) {
  4039. return this.flowParseDeclareClass(node);
  4040. } else if (this.match(68)) {
  4041. return this.flowParseDeclareFunction(node);
  4042. } else if (this.match(74)) {
  4043. return this.flowParseDeclareVariable(node);
  4044. } else if (this.eatContextual(127)) {
  4045. if (this.match(16)) {
  4046. return this.flowParseDeclareModuleExports(node);
  4047. } else {
  4048. if (insideModule) {
  4049. this.raise(FlowErrors.NestedDeclareModule, this.state.lastTokStartLoc);
  4050. }
  4051. return this.flowParseDeclareModule(node);
  4052. }
  4053. } else if (this.isContextual(130)) {
  4054. return this.flowParseDeclareTypeAlias(node);
  4055. } else if (this.isContextual(131)) {
  4056. return this.flowParseDeclareOpaqueType(node);
  4057. } else if (this.isContextual(129)) {
  4058. return this.flowParseDeclareInterface(node);
  4059. } else if (this.match(82)) {
  4060. return this.flowParseDeclareExportDeclaration(node, insideModule);
  4061. } else {
  4062. this.unexpected();
  4063. }
  4064. }
  4065. flowParseDeclareVariable(node) {
  4066. this.next();
  4067. node.id = this.flowParseTypeAnnotatableIdentifier(true);
  4068. this.scope.declareName(node.id.name, 5, node.id.loc.start);
  4069. this.semicolon();
  4070. return this.finishNode(node, "DeclareVariable");
  4071. }
  4072. flowParseDeclareModule(node) {
  4073. this.scope.enter(0);
  4074. if (this.match(133)) {
  4075. node.id = super.parseExprAtom();
  4076. } else {
  4077. node.id = this.parseIdentifier();
  4078. }
  4079. const bodyNode = node.body = this.startNode();
  4080. const body = bodyNode.body = [];
  4081. this.expect(5);
  4082. while (!this.match(8)) {
  4083. let bodyNode = this.startNode();
  4084. if (this.match(83)) {
  4085. this.next();
  4086. if (!this.isContextual(130) && !this.match(87)) {
  4087. this.raise(FlowErrors.InvalidNonTypeImportInDeclareModule, this.state.lastTokStartLoc);
  4088. }
  4089. super.parseImport(bodyNode);
  4090. } else {
  4091. this.expectContextual(125, FlowErrors.UnsupportedStatementInDeclareModule);
  4092. bodyNode = this.flowParseDeclare(bodyNode, true);
  4093. }
  4094. body.push(bodyNode);
  4095. }
  4096. this.scope.exit();
  4097. this.expect(8);
  4098. this.finishNode(bodyNode, "BlockStatement");
  4099. let kind = null;
  4100. let hasModuleExport = false;
  4101. body.forEach(bodyElement => {
  4102. if (isEsModuleType(bodyElement)) {
  4103. if (kind === "CommonJS") {
  4104. this.raise(FlowErrors.AmbiguousDeclareModuleKind, bodyElement);
  4105. }
  4106. kind = "ES";
  4107. } else if (bodyElement.type === "DeclareModuleExports") {
  4108. if (hasModuleExport) {
  4109. this.raise(FlowErrors.DuplicateDeclareModuleExports, bodyElement);
  4110. }
  4111. if (kind === "ES") {
  4112. this.raise(FlowErrors.AmbiguousDeclareModuleKind, bodyElement);
  4113. }
  4114. kind = "CommonJS";
  4115. hasModuleExport = true;
  4116. }
  4117. });
  4118. node.kind = kind || "CommonJS";
  4119. return this.finishNode(node, "DeclareModule");
  4120. }
  4121. flowParseDeclareExportDeclaration(node, insideModule) {
  4122. this.expect(82);
  4123. if (this.eat(65)) {
  4124. if (this.match(68) || this.match(80)) {
  4125. node.declaration = this.flowParseDeclare(this.startNode());
  4126. } else {
  4127. node.declaration = this.flowParseType();
  4128. this.semicolon();
  4129. }
  4130. node.default = true;
  4131. return this.finishNode(node, "DeclareExportDeclaration");
  4132. } else {
  4133. if (this.match(75) || this.isLet() || (this.isContextual(130) || this.isContextual(129)) && !insideModule) {
  4134. const label = this.state.value;
  4135. throw this.raise(FlowErrors.UnsupportedDeclareExportKind, this.state.startLoc, {
  4136. unsupportedExportKind: label,
  4137. suggestion: exportSuggestions[label]
  4138. });
  4139. }
  4140. if (this.match(74) || this.match(68) || this.match(80) || this.isContextual(131)) {
  4141. node.declaration = this.flowParseDeclare(this.startNode());
  4142. node.default = false;
  4143. return this.finishNode(node, "DeclareExportDeclaration");
  4144. } else if (this.match(55) || this.match(5) || this.isContextual(129) || this.isContextual(130) || this.isContextual(131)) {
  4145. node = this.parseExport(node, null);
  4146. if (node.type === "ExportNamedDeclaration") {
  4147. node.type = "ExportDeclaration";
  4148. node.default = false;
  4149. delete node.exportKind;
  4150. }
  4151. node.type = "Declare" + node.type;
  4152. return node;
  4153. }
  4154. }
  4155. this.unexpected();
  4156. }
  4157. flowParseDeclareModuleExports(node) {
  4158. this.next();
  4159. this.expectContextual(111);
  4160. node.typeAnnotation = this.flowParseTypeAnnotation();
  4161. this.semicolon();
  4162. return this.finishNode(node, "DeclareModuleExports");
  4163. }
  4164. flowParseDeclareTypeAlias(node) {
  4165. this.next();
  4166. const finished = this.flowParseTypeAlias(node);
  4167. finished.type = "DeclareTypeAlias";
  4168. return finished;
  4169. }
  4170. flowParseDeclareOpaqueType(node) {
  4171. this.next();
  4172. const finished = this.flowParseOpaqueType(node, true);
  4173. finished.type = "DeclareOpaqueType";
  4174. return finished;
  4175. }
  4176. flowParseDeclareInterface(node) {
  4177. this.next();
  4178. this.flowParseInterfaceish(node, false);
  4179. return this.finishNode(node, "DeclareInterface");
  4180. }
  4181. flowParseInterfaceish(node, isClass) {
  4182. node.id = this.flowParseRestrictedIdentifier(!isClass, true);
  4183. this.scope.declareName(node.id.name, isClass ? 17 : 8201, node.id.loc.start);
  4184. if (this.match(47)) {
  4185. node.typeParameters = this.flowParseTypeParameterDeclaration();
  4186. } else {
  4187. node.typeParameters = null;
  4188. }
  4189. node.extends = [];
  4190. if (this.eat(81)) {
  4191. do {
  4192. node.extends.push(this.flowParseInterfaceExtends());
  4193. } while (!isClass && this.eat(12));
  4194. }
  4195. if (isClass) {
  4196. node.implements = [];
  4197. node.mixins = [];
  4198. if (this.eatContextual(117)) {
  4199. do {
  4200. node.mixins.push(this.flowParseInterfaceExtends());
  4201. } while (this.eat(12));
  4202. }
  4203. if (this.eatContextual(113)) {
  4204. do {
  4205. node.implements.push(this.flowParseInterfaceExtends());
  4206. } while (this.eat(12));
  4207. }
  4208. }
  4209. node.body = this.flowParseObjectType({
  4210. allowStatic: isClass,
  4211. allowExact: false,
  4212. allowSpread: false,
  4213. allowProto: isClass,
  4214. allowInexact: false
  4215. });
  4216. }
  4217. flowParseInterfaceExtends() {
  4218. const node = this.startNode();
  4219. node.id = this.flowParseQualifiedTypeIdentifier();
  4220. if (this.match(47)) {
  4221. node.typeParameters = this.flowParseTypeParameterInstantiation();
  4222. } else {
  4223. node.typeParameters = null;
  4224. }
  4225. return this.finishNode(node, "InterfaceExtends");
  4226. }
  4227. flowParseInterface(node) {
  4228. this.flowParseInterfaceish(node, false);
  4229. return this.finishNode(node, "InterfaceDeclaration");
  4230. }
  4231. checkNotUnderscore(word) {
  4232. if (word === "_") {
  4233. this.raise(FlowErrors.UnexpectedReservedUnderscore, this.state.startLoc);
  4234. }
  4235. }
  4236. checkReservedType(word, startLoc, declaration) {
  4237. if (!reservedTypes.has(word)) return;
  4238. this.raise(declaration ? FlowErrors.AssignReservedType : FlowErrors.UnexpectedReservedType, startLoc, {
  4239. reservedType: word
  4240. });
  4241. }
  4242. flowParseRestrictedIdentifier(liberal, declaration) {
  4243. this.checkReservedType(this.state.value, this.state.startLoc, declaration);
  4244. return this.parseIdentifier(liberal);
  4245. }
  4246. flowParseTypeAlias(node) {
  4247. node.id = this.flowParseRestrictedIdentifier(false, true);
  4248. this.scope.declareName(node.id.name, 8201, node.id.loc.start);
  4249. if (this.match(47)) {
  4250. node.typeParameters = this.flowParseTypeParameterDeclaration();
  4251. } else {
  4252. node.typeParameters = null;
  4253. }
  4254. node.right = this.flowParseTypeInitialiser(29);
  4255. this.semicolon();
  4256. return this.finishNode(node, "TypeAlias");
  4257. }
  4258. flowParseOpaqueType(node, declare) {
  4259. this.expectContextual(130);
  4260. node.id = this.flowParseRestrictedIdentifier(true, true);
  4261. this.scope.declareName(node.id.name, 8201, node.id.loc.start);
  4262. if (this.match(47)) {
  4263. node.typeParameters = this.flowParseTypeParameterDeclaration();
  4264. } else {
  4265. node.typeParameters = null;
  4266. }
  4267. node.supertype = null;
  4268. if (this.match(14)) {
  4269. node.supertype = this.flowParseTypeInitialiser(14);
  4270. }
  4271. node.impltype = null;
  4272. if (!declare) {
  4273. node.impltype = this.flowParseTypeInitialiser(29);
  4274. }
  4275. this.semicolon();
  4276. return this.finishNode(node, "OpaqueType");
  4277. }
  4278. flowParseTypeParameter(requireDefault = false) {
  4279. const nodeStartLoc = this.state.startLoc;
  4280. const node = this.startNode();
  4281. const variance = this.flowParseVariance();
  4282. const ident = this.flowParseTypeAnnotatableIdentifier();
  4283. node.name = ident.name;
  4284. node.variance = variance;
  4285. node.bound = ident.typeAnnotation;
  4286. if (this.match(29)) {
  4287. this.eat(29);
  4288. node.default = this.flowParseType();
  4289. } else {
  4290. if (requireDefault) {
  4291. this.raise(FlowErrors.MissingTypeParamDefault, nodeStartLoc);
  4292. }
  4293. }
  4294. return this.finishNode(node, "TypeParameter");
  4295. }
  4296. flowParseTypeParameterDeclaration() {
  4297. const oldInType = this.state.inType;
  4298. const node = this.startNode();
  4299. node.params = [];
  4300. this.state.inType = true;
  4301. if (this.match(47) || this.match(142)) {
  4302. this.next();
  4303. } else {
  4304. this.unexpected();
  4305. }
  4306. let defaultRequired = false;
  4307. do {
  4308. const typeParameter = this.flowParseTypeParameter(defaultRequired);
  4309. node.params.push(typeParameter);
  4310. if (typeParameter.default) {
  4311. defaultRequired = true;
  4312. }
  4313. if (!this.match(48)) {
  4314. this.expect(12);
  4315. }
  4316. } while (!this.match(48));
  4317. this.expect(48);
  4318. this.state.inType = oldInType;
  4319. return this.finishNode(node, "TypeParameterDeclaration");
  4320. }
  4321. flowParseTypeParameterInstantiation() {
  4322. const node = this.startNode();
  4323. const oldInType = this.state.inType;
  4324. node.params = [];
  4325. this.state.inType = true;
  4326. this.expect(47);
  4327. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  4328. this.state.noAnonFunctionType = false;
  4329. while (!this.match(48)) {
  4330. node.params.push(this.flowParseType());
  4331. if (!this.match(48)) {
  4332. this.expect(12);
  4333. }
  4334. }
  4335. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  4336. this.expect(48);
  4337. this.state.inType = oldInType;
  4338. return this.finishNode(node, "TypeParameterInstantiation");
  4339. }
  4340. flowParseTypeParameterInstantiationCallOrNew() {
  4341. const node = this.startNode();
  4342. const oldInType = this.state.inType;
  4343. node.params = [];
  4344. this.state.inType = true;
  4345. this.expect(47);
  4346. while (!this.match(48)) {
  4347. node.params.push(this.flowParseTypeOrImplicitInstantiation());
  4348. if (!this.match(48)) {
  4349. this.expect(12);
  4350. }
  4351. }
  4352. this.expect(48);
  4353. this.state.inType = oldInType;
  4354. return this.finishNode(node, "TypeParameterInstantiation");
  4355. }
  4356. flowParseInterfaceType() {
  4357. const node = this.startNode();
  4358. this.expectContextual(129);
  4359. node.extends = [];
  4360. if (this.eat(81)) {
  4361. do {
  4362. node.extends.push(this.flowParseInterfaceExtends());
  4363. } while (this.eat(12));
  4364. }
  4365. node.body = this.flowParseObjectType({
  4366. allowStatic: false,
  4367. allowExact: false,
  4368. allowSpread: false,
  4369. allowProto: false,
  4370. allowInexact: false
  4371. });
  4372. return this.finishNode(node, "InterfaceTypeAnnotation");
  4373. }
  4374. flowParseObjectPropertyKey() {
  4375. return this.match(134) || this.match(133) ? super.parseExprAtom() : this.parseIdentifier(true);
  4376. }
  4377. flowParseObjectTypeIndexer(node, isStatic, variance) {
  4378. node.static = isStatic;
  4379. if (this.lookahead().type === 14) {
  4380. node.id = this.flowParseObjectPropertyKey();
  4381. node.key = this.flowParseTypeInitialiser();
  4382. } else {
  4383. node.id = null;
  4384. node.key = this.flowParseType();
  4385. }
  4386. this.expect(3);
  4387. node.value = this.flowParseTypeInitialiser();
  4388. node.variance = variance;
  4389. return this.finishNode(node, "ObjectTypeIndexer");
  4390. }
  4391. flowParseObjectTypeInternalSlot(node, isStatic) {
  4392. node.static = isStatic;
  4393. node.id = this.flowParseObjectPropertyKey();
  4394. this.expect(3);
  4395. this.expect(3);
  4396. if (this.match(47) || this.match(10)) {
  4397. node.method = true;
  4398. node.optional = false;
  4399. node.value = this.flowParseObjectTypeMethodish(this.startNodeAt(node.loc.start));
  4400. } else {
  4401. node.method = false;
  4402. if (this.eat(17)) {
  4403. node.optional = true;
  4404. }
  4405. node.value = this.flowParseTypeInitialiser();
  4406. }
  4407. return this.finishNode(node, "ObjectTypeInternalSlot");
  4408. }
  4409. flowParseObjectTypeMethodish(node) {
  4410. node.params = [];
  4411. node.rest = null;
  4412. node.typeParameters = null;
  4413. node.this = null;
  4414. if (this.match(47)) {
  4415. node.typeParameters = this.flowParseTypeParameterDeclaration();
  4416. }
  4417. this.expect(10);
  4418. if (this.match(78)) {
  4419. node.this = this.flowParseFunctionTypeParam(true);
  4420. node.this.name = null;
  4421. if (!this.match(11)) {
  4422. this.expect(12);
  4423. }
  4424. }
  4425. while (!this.match(11) && !this.match(21)) {
  4426. node.params.push(this.flowParseFunctionTypeParam(false));
  4427. if (!this.match(11)) {
  4428. this.expect(12);
  4429. }
  4430. }
  4431. if (this.eat(21)) {
  4432. node.rest = this.flowParseFunctionTypeParam(false);
  4433. }
  4434. this.expect(11);
  4435. node.returnType = this.flowParseTypeInitialiser();
  4436. return this.finishNode(node, "FunctionTypeAnnotation");
  4437. }
  4438. flowParseObjectTypeCallProperty(node, isStatic) {
  4439. const valueNode = this.startNode();
  4440. node.static = isStatic;
  4441. node.value = this.flowParseObjectTypeMethodish(valueNode);
  4442. return this.finishNode(node, "ObjectTypeCallProperty");
  4443. }
  4444. flowParseObjectType({
  4445. allowStatic,
  4446. allowExact,
  4447. allowSpread,
  4448. allowProto,
  4449. allowInexact
  4450. }) {
  4451. const oldInType = this.state.inType;
  4452. this.state.inType = true;
  4453. const nodeStart = this.startNode();
  4454. nodeStart.callProperties = [];
  4455. nodeStart.properties = [];
  4456. nodeStart.indexers = [];
  4457. nodeStart.internalSlots = [];
  4458. let endDelim;
  4459. let exact;
  4460. let inexact = false;
  4461. if (allowExact && this.match(6)) {
  4462. this.expect(6);
  4463. endDelim = 9;
  4464. exact = true;
  4465. } else {
  4466. this.expect(5);
  4467. endDelim = 8;
  4468. exact = false;
  4469. }
  4470. nodeStart.exact = exact;
  4471. while (!this.match(endDelim)) {
  4472. let isStatic = false;
  4473. let protoStartLoc = null;
  4474. let inexactStartLoc = null;
  4475. const node = this.startNode();
  4476. if (allowProto && this.isContextual(118)) {
  4477. const lookahead = this.lookahead();
  4478. if (lookahead.type !== 14 && lookahead.type !== 17) {
  4479. this.next();
  4480. protoStartLoc = this.state.startLoc;
  4481. allowStatic = false;
  4482. }
  4483. }
  4484. if (allowStatic && this.isContextual(106)) {
  4485. const lookahead = this.lookahead();
  4486. if (lookahead.type !== 14 && lookahead.type !== 17) {
  4487. this.next();
  4488. isStatic = true;
  4489. }
  4490. }
  4491. const variance = this.flowParseVariance();
  4492. if (this.eat(0)) {
  4493. if (protoStartLoc != null) {
  4494. this.unexpected(protoStartLoc);
  4495. }
  4496. if (this.eat(0)) {
  4497. if (variance) {
  4498. this.unexpected(variance.loc.start);
  4499. }
  4500. nodeStart.internalSlots.push(this.flowParseObjectTypeInternalSlot(node, isStatic));
  4501. } else {
  4502. nodeStart.indexers.push(this.flowParseObjectTypeIndexer(node, isStatic, variance));
  4503. }
  4504. } else if (this.match(10) || this.match(47)) {
  4505. if (protoStartLoc != null) {
  4506. this.unexpected(protoStartLoc);
  4507. }
  4508. if (variance) {
  4509. this.unexpected(variance.loc.start);
  4510. }
  4511. nodeStart.callProperties.push(this.flowParseObjectTypeCallProperty(node, isStatic));
  4512. } else {
  4513. let kind = "init";
  4514. if (this.isContextual(99) || this.isContextual(104)) {
  4515. const lookahead = this.lookahead();
  4516. if (tokenIsLiteralPropertyName(lookahead.type)) {
  4517. kind = this.state.value;
  4518. this.next();
  4519. }
  4520. }
  4521. const propOrInexact = this.flowParseObjectTypeProperty(node, isStatic, protoStartLoc, variance, kind, allowSpread, allowInexact != null ? allowInexact : !exact);
  4522. if (propOrInexact === null) {
  4523. inexact = true;
  4524. inexactStartLoc = this.state.lastTokStartLoc;
  4525. } else {
  4526. nodeStart.properties.push(propOrInexact);
  4527. }
  4528. }
  4529. this.flowObjectTypeSemicolon();
  4530. if (inexactStartLoc && !this.match(8) && !this.match(9)) {
  4531. this.raise(FlowErrors.UnexpectedExplicitInexactInObject, inexactStartLoc);
  4532. }
  4533. }
  4534. this.expect(endDelim);
  4535. if (allowSpread) {
  4536. nodeStart.inexact = inexact;
  4537. }
  4538. const out = this.finishNode(nodeStart, "ObjectTypeAnnotation");
  4539. this.state.inType = oldInType;
  4540. return out;
  4541. }
  4542. flowParseObjectTypeProperty(node, isStatic, protoStartLoc, variance, kind, allowSpread, allowInexact) {
  4543. if (this.eat(21)) {
  4544. const isInexactToken = this.match(12) || this.match(13) || this.match(8) || this.match(9);
  4545. if (isInexactToken) {
  4546. if (!allowSpread) {
  4547. this.raise(FlowErrors.InexactInsideNonObject, this.state.lastTokStartLoc);
  4548. } else if (!allowInexact) {
  4549. this.raise(FlowErrors.InexactInsideExact, this.state.lastTokStartLoc);
  4550. }
  4551. if (variance) {
  4552. this.raise(FlowErrors.InexactVariance, variance);
  4553. }
  4554. return null;
  4555. }
  4556. if (!allowSpread) {
  4557. this.raise(FlowErrors.UnexpectedSpreadType, this.state.lastTokStartLoc);
  4558. }
  4559. if (protoStartLoc != null) {
  4560. this.unexpected(protoStartLoc);
  4561. }
  4562. if (variance) {
  4563. this.raise(FlowErrors.SpreadVariance, variance);
  4564. }
  4565. node.argument = this.flowParseType();
  4566. return this.finishNode(node, "ObjectTypeSpreadProperty");
  4567. } else {
  4568. node.key = this.flowParseObjectPropertyKey();
  4569. node.static = isStatic;
  4570. node.proto = protoStartLoc != null;
  4571. node.kind = kind;
  4572. let optional = false;
  4573. if (this.match(47) || this.match(10)) {
  4574. node.method = true;
  4575. if (protoStartLoc != null) {
  4576. this.unexpected(protoStartLoc);
  4577. }
  4578. if (variance) {
  4579. this.unexpected(variance.loc.start);
  4580. }
  4581. node.value = this.flowParseObjectTypeMethodish(this.startNodeAt(node.loc.start));
  4582. if (kind === "get" || kind === "set") {
  4583. this.flowCheckGetterSetterParams(node);
  4584. }
  4585. if (!allowSpread && node.key.name === "constructor" && node.value.this) {
  4586. this.raise(FlowErrors.ThisParamBannedInConstructor, node.value.this);
  4587. }
  4588. } else {
  4589. if (kind !== "init") this.unexpected();
  4590. node.method = false;
  4591. if (this.eat(17)) {
  4592. optional = true;
  4593. }
  4594. node.value = this.flowParseTypeInitialiser();
  4595. node.variance = variance;
  4596. }
  4597. node.optional = optional;
  4598. return this.finishNode(node, "ObjectTypeProperty");
  4599. }
  4600. }
  4601. flowCheckGetterSetterParams(property) {
  4602. const paramCount = property.kind === "get" ? 0 : 1;
  4603. const length = property.value.params.length + (property.value.rest ? 1 : 0);
  4604. if (property.value.this) {
  4605. this.raise(property.kind === "get" ? FlowErrors.GetterMayNotHaveThisParam : FlowErrors.SetterMayNotHaveThisParam, property.value.this);
  4606. }
  4607. if (length !== paramCount) {
  4608. this.raise(property.kind === "get" ? Errors.BadGetterArity : Errors.BadSetterArity, property);
  4609. }
  4610. if (property.kind === "set" && property.value.rest) {
  4611. this.raise(Errors.BadSetterRestParameter, property);
  4612. }
  4613. }
  4614. flowObjectTypeSemicolon() {
  4615. if (!this.eat(13) && !this.eat(12) && !this.match(8) && !this.match(9)) {
  4616. this.unexpected();
  4617. }
  4618. }
  4619. flowParseQualifiedTypeIdentifier(startLoc, id) {
  4620. var _startLoc;
  4621. (_startLoc = startLoc) != null ? _startLoc : startLoc = this.state.startLoc;
  4622. let node = id || this.flowParseRestrictedIdentifier(true);
  4623. while (this.eat(16)) {
  4624. const node2 = this.startNodeAt(startLoc);
  4625. node2.qualification = node;
  4626. node2.id = this.flowParseRestrictedIdentifier(true);
  4627. node = this.finishNode(node2, "QualifiedTypeIdentifier");
  4628. }
  4629. return node;
  4630. }
  4631. flowParseGenericType(startLoc, id) {
  4632. const node = this.startNodeAt(startLoc);
  4633. node.typeParameters = null;
  4634. node.id = this.flowParseQualifiedTypeIdentifier(startLoc, id);
  4635. if (this.match(47)) {
  4636. node.typeParameters = this.flowParseTypeParameterInstantiation();
  4637. }
  4638. return this.finishNode(node, "GenericTypeAnnotation");
  4639. }
  4640. flowParseTypeofType() {
  4641. const node = this.startNode();
  4642. this.expect(87);
  4643. node.argument = this.flowParsePrimaryType();
  4644. return this.finishNode(node, "TypeofTypeAnnotation");
  4645. }
  4646. flowParseTupleType() {
  4647. const node = this.startNode();
  4648. node.types = [];
  4649. this.expect(0);
  4650. while (this.state.pos < this.length && !this.match(3)) {
  4651. node.types.push(this.flowParseType());
  4652. if (this.match(3)) break;
  4653. this.expect(12);
  4654. }
  4655. this.expect(3);
  4656. return this.finishNode(node, "TupleTypeAnnotation");
  4657. }
  4658. flowParseFunctionTypeParam(first) {
  4659. let name = null;
  4660. let optional = false;
  4661. let typeAnnotation = null;
  4662. const node = this.startNode();
  4663. const lh = this.lookahead();
  4664. const isThis = this.state.type === 78;
  4665. if (lh.type === 14 || lh.type === 17) {
  4666. if (isThis && !first) {
  4667. this.raise(FlowErrors.ThisParamMustBeFirst, node);
  4668. }
  4669. name = this.parseIdentifier(isThis);
  4670. if (this.eat(17)) {
  4671. optional = true;
  4672. if (isThis) {
  4673. this.raise(FlowErrors.ThisParamMayNotBeOptional, node);
  4674. }
  4675. }
  4676. typeAnnotation = this.flowParseTypeInitialiser();
  4677. } else {
  4678. typeAnnotation = this.flowParseType();
  4679. }
  4680. node.name = name;
  4681. node.optional = optional;
  4682. node.typeAnnotation = typeAnnotation;
  4683. return this.finishNode(node, "FunctionTypeParam");
  4684. }
  4685. reinterpretTypeAsFunctionTypeParam(type) {
  4686. const node = this.startNodeAt(type.loc.start);
  4687. node.name = null;
  4688. node.optional = false;
  4689. node.typeAnnotation = type;
  4690. return this.finishNode(node, "FunctionTypeParam");
  4691. }
  4692. flowParseFunctionTypeParams(params = []) {
  4693. let rest = null;
  4694. let _this = null;
  4695. if (this.match(78)) {
  4696. _this = this.flowParseFunctionTypeParam(true);
  4697. _this.name = null;
  4698. if (!this.match(11)) {
  4699. this.expect(12);
  4700. }
  4701. }
  4702. while (!this.match(11) && !this.match(21)) {
  4703. params.push(this.flowParseFunctionTypeParam(false));
  4704. if (!this.match(11)) {
  4705. this.expect(12);
  4706. }
  4707. }
  4708. if (this.eat(21)) {
  4709. rest = this.flowParseFunctionTypeParam(false);
  4710. }
  4711. return {
  4712. params,
  4713. rest,
  4714. _this
  4715. };
  4716. }
  4717. flowIdentToTypeAnnotation(startLoc, node, id) {
  4718. switch (id.name) {
  4719. case "any":
  4720. return this.finishNode(node, "AnyTypeAnnotation");
  4721. case "bool":
  4722. case "boolean":
  4723. return this.finishNode(node, "BooleanTypeAnnotation");
  4724. case "mixed":
  4725. return this.finishNode(node, "MixedTypeAnnotation");
  4726. case "empty":
  4727. return this.finishNode(node, "EmptyTypeAnnotation");
  4728. case "number":
  4729. return this.finishNode(node, "NumberTypeAnnotation");
  4730. case "string":
  4731. return this.finishNode(node, "StringTypeAnnotation");
  4732. case "symbol":
  4733. return this.finishNode(node, "SymbolTypeAnnotation");
  4734. default:
  4735. this.checkNotUnderscore(id.name);
  4736. return this.flowParseGenericType(startLoc, id);
  4737. }
  4738. }
  4739. flowParsePrimaryType() {
  4740. const startLoc = this.state.startLoc;
  4741. const node = this.startNode();
  4742. let tmp;
  4743. let type;
  4744. let isGroupedType = false;
  4745. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  4746. switch (this.state.type) {
  4747. case 5:
  4748. return this.flowParseObjectType({
  4749. allowStatic: false,
  4750. allowExact: false,
  4751. allowSpread: true,
  4752. allowProto: false,
  4753. allowInexact: true
  4754. });
  4755. case 6:
  4756. return this.flowParseObjectType({
  4757. allowStatic: false,
  4758. allowExact: true,
  4759. allowSpread: true,
  4760. allowProto: false,
  4761. allowInexact: false
  4762. });
  4763. case 0:
  4764. this.state.noAnonFunctionType = false;
  4765. type = this.flowParseTupleType();
  4766. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  4767. return type;
  4768. case 47:
  4769. {
  4770. const node = this.startNode();
  4771. node.typeParameters = this.flowParseTypeParameterDeclaration();
  4772. this.expect(10);
  4773. tmp = this.flowParseFunctionTypeParams();
  4774. node.params = tmp.params;
  4775. node.rest = tmp.rest;
  4776. node.this = tmp._this;
  4777. this.expect(11);
  4778. this.expect(19);
  4779. node.returnType = this.flowParseType();
  4780. return this.finishNode(node, "FunctionTypeAnnotation");
  4781. }
  4782. case 10:
  4783. {
  4784. const node = this.startNode();
  4785. this.next();
  4786. if (!this.match(11) && !this.match(21)) {
  4787. if (tokenIsIdentifier(this.state.type) || this.match(78)) {
  4788. const token = this.lookahead().type;
  4789. isGroupedType = token !== 17 && token !== 14;
  4790. } else {
  4791. isGroupedType = true;
  4792. }
  4793. }
  4794. if (isGroupedType) {
  4795. this.state.noAnonFunctionType = false;
  4796. type = this.flowParseType();
  4797. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  4798. if (this.state.noAnonFunctionType || !(this.match(12) || this.match(11) && this.lookahead().type === 19)) {
  4799. this.expect(11);
  4800. return type;
  4801. } else {
  4802. this.eat(12);
  4803. }
  4804. }
  4805. if (type) {
  4806. tmp = this.flowParseFunctionTypeParams([this.reinterpretTypeAsFunctionTypeParam(type)]);
  4807. } else {
  4808. tmp = this.flowParseFunctionTypeParams();
  4809. }
  4810. node.params = tmp.params;
  4811. node.rest = tmp.rest;
  4812. node.this = tmp._this;
  4813. this.expect(11);
  4814. this.expect(19);
  4815. node.returnType = this.flowParseType();
  4816. node.typeParameters = null;
  4817. return this.finishNode(node, "FunctionTypeAnnotation");
  4818. }
  4819. case 133:
  4820. return this.parseLiteral(this.state.value, "StringLiteralTypeAnnotation");
  4821. case 85:
  4822. case 86:
  4823. node.value = this.match(85);
  4824. this.next();
  4825. return this.finishNode(node, "BooleanLiteralTypeAnnotation");
  4826. case 53:
  4827. if (this.state.value === "-") {
  4828. this.next();
  4829. if (this.match(134)) {
  4830. return this.parseLiteralAtNode(-this.state.value, "NumberLiteralTypeAnnotation", node);
  4831. }
  4832. if (this.match(135)) {
  4833. return this.parseLiteralAtNode(-this.state.value, "BigIntLiteralTypeAnnotation", node);
  4834. }
  4835. throw this.raise(FlowErrors.UnexpectedSubtractionOperand, this.state.startLoc);
  4836. }
  4837. this.unexpected();
  4838. return;
  4839. case 134:
  4840. return this.parseLiteral(this.state.value, "NumberLiteralTypeAnnotation");
  4841. case 135:
  4842. return this.parseLiteral(this.state.value, "BigIntLiteralTypeAnnotation");
  4843. case 88:
  4844. this.next();
  4845. return this.finishNode(node, "VoidTypeAnnotation");
  4846. case 84:
  4847. this.next();
  4848. return this.finishNode(node, "NullLiteralTypeAnnotation");
  4849. case 78:
  4850. this.next();
  4851. return this.finishNode(node, "ThisTypeAnnotation");
  4852. case 55:
  4853. this.next();
  4854. return this.finishNode(node, "ExistsTypeAnnotation");
  4855. case 87:
  4856. return this.flowParseTypeofType();
  4857. default:
  4858. if (tokenIsKeyword(this.state.type)) {
  4859. const label = tokenLabelName(this.state.type);
  4860. this.next();
  4861. return super.createIdentifier(node, label);
  4862. } else if (tokenIsIdentifier(this.state.type)) {
  4863. if (this.isContextual(129)) {
  4864. return this.flowParseInterfaceType();
  4865. }
  4866. return this.flowIdentToTypeAnnotation(startLoc, node, this.parseIdentifier());
  4867. }
  4868. }
  4869. this.unexpected();
  4870. }
  4871. flowParsePostfixType() {
  4872. const startLoc = this.state.startLoc;
  4873. let type = this.flowParsePrimaryType();
  4874. let seenOptionalIndexedAccess = false;
  4875. while ((this.match(0) || this.match(18)) && !this.canInsertSemicolon()) {
  4876. const node = this.startNodeAt(startLoc);
  4877. const optional = this.eat(18);
  4878. seenOptionalIndexedAccess = seenOptionalIndexedAccess || optional;
  4879. this.expect(0);
  4880. if (!optional && this.match(3)) {
  4881. node.elementType = type;
  4882. this.next();
  4883. type = this.finishNode(node, "ArrayTypeAnnotation");
  4884. } else {
  4885. node.objectType = type;
  4886. node.indexType = this.flowParseType();
  4887. this.expect(3);
  4888. if (seenOptionalIndexedAccess) {
  4889. node.optional = optional;
  4890. type = this.finishNode(node, "OptionalIndexedAccessType");
  4891. } else {
  4892. type = this.finishNode(node, "IndexedAccessType");
  4893. }
  4894. }
  4895. }
  4896. return type;
  4897. }
  4898. flowParsePrefixType() {
  4899. const node = this.startNode();
  4900. if (this.eat(17)) {
  4901. node.typeAnnotation = this.flowParsePrefixType();
  4902. return this.finishNode(node, "NullableTypeAnnotation");
  4903. } else {
  4904. return this.flowParsePostfixType();
  4905. }
  4906. }
  4907. flowParseAnonFunctionWithoutParens() {
  4908. const param = this.flowParsePrefixType();
  4909. if (!this.state.noAnonFunctionType && this.eat(19)) {
  4910. const node = this.startNodeAt(param.loc.start);
  4911. node.params = [this.reinterpretTypeAsFunctionTypeParam(param)];
  4912. node.rest = null;
  4913. node.this = null;
  4914. node.returnType = this.flowParseType();
  4915. node.typeParameters = null;
  4916. return this.finishNode(node, "FunctionTypeAnnotation");
  4917. }
  4918. return param;
  4919. }
  4920. flowParseIntersectionType() {
  4921. const node = this.startNode();
  4922. this.eat(45);
  4923. const type = this.flowParseAnonFunctionWithoutParens();
  4924. node.types = [type];
  4925. while (this.eat(45)) {
  4926. node.types.push(this.flowParseAnonFunctionWithoutParens());
  4927. }
  4928. return node.types.length === 1 ? type : this.finishNode(node, "IntersectionTypeAnnotation");
  4929. }
  4930. flowParseUnionType() {
  4931. const node = this.startNode();
  4932. this.eat(43);
  4933. const type = this.flowParseIntersectionType();
  4934. node.types = [type];
  4935. while (this.eat(43)) {
  4936. node.types.push(this.flowParseIntersectionType());
  4937. }
  4938. return node.types.length === 1 ? type : this.finishNode(node, "UnionTypeAnnotation");
  4939. }
  4940. flowParseType() {
  4941. const oldInType = this.state.inType;
  4942. this.state.inType = true;
  4943. const type = this.flowParseUnionType();
  4944. this.state.inType = oldInType;
  4945. return type;
  4946. }
  4947. flowParseTypeOrImplicitInstantiation() {
  4948. if (this.state.type === 132 && this.state.value === "_") {
  4949. const startLoc = this.state.startLoc;
  4950. const node = this.parseIdentifier();
  4951. return this.flowParseGenericType(startLoc, node);
  4952. } else {
  4953. return this.flowParseType();
  4954. }
  4955. }
  4956. flowParseTypeAnnotation() {
  4957. const node = this.startNode();
  4958. node.typeAnnotation = this.flowParseTypeInitialiser();
  4959. return this.finishNode(node, "TypeAnnotation");
  4960. }
  4961. flowParseTypeAnnotatableIdentifier(allowPrimitiveOverride) {
  4962. const ident = allowPrimitiveOverride ? this.parseIdentifier() : this.flowParseRestrictedIdentifier();
  4963. if (this.match(14)) {
  4964. ident.typeAnnotation = this.flowParseTypeAnnotation();
  4965. this.resetEndLocation(ident);
  4966. }
  4967. return ident;
  4968. }
  4969. typeCastToParameter(node) {
  4970. node.expression.typeAnnotation = node.typeAnnotation;
  4971. this.resetEndLocation(node.expression, node.typeAnnotation.loc.end);
  4972. return node.expression;
  4973. }
  4974. flowParseVariance() {
  4975. let variance = null;
  4976. if (this.match(53)) {
  4977. variance = this.startNode();
  4978. if (this.state.value === "+") {
  4979. variance.kind = "plus";
  4980. } else {
  4981. variance.kind = "minus";
  4982. }
  4983. this.next();
  4984. return this.finishNode(variance, "Variance");
  4985. }
  4986. return variance;
  4987. }
  4988. parseFunctionBody(node, allowExpressionBody, isMethod = false) {
  4989. if (allowExpressionBody) {
  4990. this.forwardNoArrowParamsConversionAt(node, () => super.parseFunctionBody(node, true, isMethod));
  4991. return;
  4992. }
  4993. super.parseFunctionBody(node, false, isMethod);
  4994. }
  4995. parseFunctionBodyAndFinish(node, type, isMethod = false) {
  4996. if (this.match(14)) {
  4997. const typeNode = this.startNode();
  4998. [typeNode.typeAnnotation, node.predicate] = this.flowParseTypeAndPredicateInitialiser();
  4999. node.returnType = typeNode.typeAnnotation ? this.finishNode(typeNode, "TypeAnnotation") : null;
  5000. }
  5001. return super.parseFunctionBodyAndFinish(node, type, isMethod);
  5002. }
  5003. parseStatementLike(flags) {
  5004. if (this.state.strict && this.isContextual(129)) {
  5005. const lookahead = this.lookahead();
  5006. if (tokenIsKeywordOrIdentifier(lookahead.type)) {
  5007. const node = this.startNode();
  5008. this.next();
  5009. return this.flowParseInterface(node);
  5010. }
  5011. } else if (this.shouldParseEnums() && this.isContextual(126)) {
  5012. const node = this.startNode();
  5013. this.next();
  5014. return this.flowParseEnumDeclaration(node);
  5015. }
  5016. const stmt = super.parseStatementLike(flags);
  5017. if (this.flowPragma === undefined && !this.isValidDirective(stmt)) {
  5018. this.flowPragma = null;
  5019. }
  5020. return stmt;
  5021. }
  5022. parseExpressionStatement(node, expr, decorators) {
  5023. if (expr.type === "Identifier") {
  5024. if (expr.name === "declare") {
  5025. if (this.match(80) || tokenIsIdentifier(this.state.type) || this.match(68) || this.match(74) || this.match(82)) {
  5026. return this.flowParseDeclare(node);
  5027. }
  5028. } else if (tokenIsIdentifier(this.state.type)) {
  5029. if (expr.name === "interface") {
  5030. return this.flowParseInterface(node);
  5031. } else if (expr.name === "type") {
  5032. return this.flowParseTypeAlias(node);
  5033. } else if (expr.name === "opaque") {
  5034. return this.flowParseOpaqueType(node, false);
  5035. }
  5036. }
  5037. }
  5038. return super.parseExpressionStatement(node, expr, decorators);
  5039. }
  5040. shouldParseExportDeclaration() {
  5041. const {
  5042. type
  5043. } = this.state;
  5044. if (tokenIsFlowInterfaceOrTypeOrOpaque(type) || this.shouldParseEnums() && type === 126) {
  5045. return !this.state.containsEsc;
  5046. }
  5047. return super.shouldParseExportDeclaration();
  5048. }
  5049. isExportDefaultSpecifier() {
  5050. const {
  5051. type
  5052. } = this.state;
  5053. if (tokenIsFlowInterfaceOrTypeOrOpaque(type) || this.shouldParseEnums() && type === 126) {
  5054. return this.state.containsEsc;
  5055. }
  5056. return super.isExportDefaultSpecifier();
  5057. }
  5058. parseExportDefaultExpression() {
  5059. if (this.shouldParseEnums() && this.isContextual(126)) {
  5060. const node = this.startNode();
  5061. this.next();
  5062. return this.flowParseEnumDeclaration(node);
  5063. }
  5064. return super.parseExportDefaultExpression();
  5065. }
  5066. parseConditional(expr, startLoc, refExpressionErrors) {
  5067. if (!this.match(17)) return expr;
  5068. if (this.state.maybeInArrowParameters) {
  5069. const nextCh = this.lookaheadCharCode();
  5070. if (nextCh === 44 || nextCh === 61 || nextCh === 58 || nextCh === 41) {
  5071. this.setOptionalParametersError(refExpressionErrors);
  5072. return expr;
  5073. }
  5074. }
  5075. this.expect(17);
  5076. const state = this.state.clone();
  5077. const originalNoArrowAt = this.state.noArrowAt;
  5078. const node = this.startNodeAt(startLoc);
  5079. let {
  5080. consequent,
  5081. failed
  5082. } = this.tryParseConditionalConsequent();
  5083. let [valid, invalid] = this.getArrowLikeExpressions(consequent);
  5084. if (failed || invalid.length > 0) {
  5085. const noArrowAt = [...originalNoArrowAt];
  5086. if (invalid.length > 0) {
  5087. this.state = state;
  5088. this.state.noArrowAt = noArrowAt;
  5089. for (let i = 0; i < invalid.length; i++) {
  5090. noArrowAt.push(invalid[i].start);
  5091. }
  5092. ({
  5093. consequent,
  5094. failed
  5095. } = this.tryParseConditionalConsequent());
  5096. [valid, invalid] = this.getArrowLikeExpressions(consequent);
  5097. }
  5098. if (failed && valid.length > 1) {
  5099. this.raise(FlowErrors.AmbiguousConditionalArrow, state.startLoc);
  5100. }
  5101. if (failed && valid.length === 1) {
  5102. this.state = state;
  5103. noArrowAt.push(valid[0].start);
  5104. this.state.noArrowAt = noArrowAt;
  5105. ({
  5106. consequent,
  5107. failed
  5108. } = this.tryParseConditionalConsequent());
  5109. }
  5110. }
  5111. this.getArrowLikeExpressions(consequent, true);
  5112. this.state.noArrowAt = originalNoArrowAt;
  5113. this.expect(14);
  5114. node.test = expr;
  5115. node.consequent = consequent;
  5116. node.alternate = this.forwardNoArrowParamsConversionAt(node, () => this.parseMaybeAssign(undefined, undefined));
  5117. return this.finishNode(node, "ConditionalExpression");
  5118. }
  5119. tryParseConditionalConsequent() {
  5120. this.state.noArrowParamsConversionAt.push(this.state.start);
  5121. const consequent = this.parseMaybeAssignAllowIn();
  5122. const failed = !this.match(14);
  5123. this.state.noArrowParamsConversionAt.pop();
  5124. return {
  5125. consequent,
  5126. failed
  5127. };
  5128. }
  5129. getArrowLikeExpressions(node, disallowInvalid) {
  5130. const stack = [node];
  5131. const arrows = [];
  5132. while (stack.length !== 0) {
  5133. const node = stack.pop();
  5134. if (node.type === "ArrowFunctionExpression" && node.body.type !== "BlockStatement") {
  5135. if (node.typeParameters || !node.returnType) {
  5136. this.finishArrowValidation(node);
  5137. } else {
  5138. arrows.push(node);
  5139. }
  5140. stack.push(node.body);
  5141. } else if (node.type === "ConditionalExpression") {
  5142. stack.push(node.consequent);
  5143. stack.push(node.alternate);
  5144. }
  5145. }
  5146. if (disallowInvalid) {
  5147. arrows.forEach(node => this.finishArrowValidation(node));
  5148. return [arrows, []];
  5149. }
  5150. return partition(arrows, node => node.params.every(param => this.isAssignable(param, true)));
  5151. }
  5152. finishArrowValidation(node) {
  5153. var _node$extra;
  5154. this.toAssignableList(node.params, (_node$extra = node.extra) == null ? void 0 : _node$extra.trailingCommaLoc, false);
  5155. this.scope.enter(2 | 4);
  5156. super.checkParams(node, false, true);
  5157. this.scope.exit();
  5158. }
  5159. forwardNoArrowParamsConversionAt(node, parse) {
  5160. let result;
  5161. if (this.state.noArrowParamsConversionAt.indexOf(node.start) !== -1) {
  5162. this.state.noArrowParamsConversionAt.push(this.state.start);
  5163. result = parse();
  5164. this.state.noArrowParamsConversionAt.pop();
  5165. } else {
  5166. result = parse();
  5167. }
  5168. return result;
  5169. }
  5170. parseParenItem(node, startLoc) {
  5171. const newNode = super.parseParenItem(node, startLoc);
  5172. if (this.eat(17)) {
  5173. newNode.optional = true;
  5174. this.resetEndLocation(node);
  5175. }
  5176. if (this.match(14)) {
  5177. const typeCastNode = this.startNodeAt(startLoc);
  5178. typeCastNode.expression = newNode;
  5179. typeCastNode.typeAnnotation = this.flowParseTypeAnnotation();
  5180. return this.finishNode(typeCastNode, "TypeCastExpression");
  5181. }
  5182. return newNode;
  5183. }
  5184. assertModuleNodeAllowed(node) {
  5185. if (node.type === "ImportDeclaration" && (node.importKind === "type" || node.importKind === "typeof") || node.type === "ExportNamedDeclaration" && node.exportKind === "type" || node.type === "ExportAllDeclaration" && node.exportKind === "type") {
  5186. return;
  5187. }
  5188. super.assertModuleNodeAllowed(node);
  5189. }
  5190. parseExportDeclaration(node) {
  5191. if (this.isContextual(130)) {
  5192. node.exportKind = "type";
  5193. const declarationNode = this.startNode();
  5194. this.next();
  5195. if (this.match(5)) {
  5196. node.specifiers = this.parseExportSpecifiers(true);
  5197. super.parseExportFrom(node);
  5198. return null;
  5199. } else {
  5200. return this.flowParseTypeAlias(declarationNode);
  5201. }
  5202. } else if (this.isContextual(131)) {
  5203. node.exportKind = "type";
  5204. const declarationNode = this.startNode();
  5205. this.next();
  5206. return this.flowParseOpaqueType(declarationNode, false);
  5207. } else if (this.isContextual(129)) {
  5208. node.exportKind = "type";
  5209. const declarationNode = this.startNode();
  5210. this.next();
  5211. return this.flowParseInterface(declarationNode);
  5212. } else if (this.shouldParseEnums() && this.isContextual(126)) {
  5213. node.exportKind = "value";
  5214. const declarationNode = this.startNode();
  5215. this.next();
  5216. return this.flowParseEnumDeclaration(declarationNode);
  5217. } else {
  5218. return super.parseExportDeclaration(node);
  5219. }
  5220. }
  5221. eatExportStar(node) {
  5222. if (super.eatExportStar(node)) return true;
  5223. if (this.isContextual(130) && this.lookahead().type === 55) {
  5224. node.exportKind = "type";
  5225. this.next();
  5226. this.next();
  5227. return true;
  5228. }
  5229. return false;
  5230. }
  5231. maybeParseExportNamespaceSpecifier(node) {
  5232. const {
  5233. startLoc
  5234. } = this.state;
  5235. const hasNamespace = super.maybeParseExportNamespaceSpecifier(node);
  5236. if (hasNamespace && node.exportKind === "type") {
  5237. this.unexpected(startLoc);
  5238. }
  5239. return hasNamespace;
  5240. }
  5241. parseClassId(node, isStatement, optionalId) {
  5242. super.parseClassId(node, isStatement, optionalId);
  5243. if (this.match(47)) {
  5244. node.typeParameters = this.flowParseTypeParameterDeclaration();
  5245. }
  5246. }
  5247. parseClassMember(classBody, member, state) {
  5248. const {
  5249. startLoc
  5250. } = this.state;
  5251. if (this.isContextual(125)) {
  5252. if (super.parseClassMemberFromModifier(classBody, member)) {
  5253. return;
  5254. }
  5255. member.declare = true;
  5256. }
  5257. super.parseClassMember(classBody, member, state);
  5258. if (member.declare) {
  5259. if (member.type !== "ClassProperty" && member.type !== "ClassPrivateProperty" && member.type !== "PropertyDefinition") {
  5260. this.raise(FlowErrors.DeclareClassElement, startLoc);
  5261. } else if (member.value) {
  5262. this.raise(FlowErrors.DeclareClassFieldInitializer, member.value);
  5263. }
  5264. }
  5265. }
  5266. isIterator(word) {
  5267. return word === "iterator" || word === "asyncIterator";
  5268. }
  5269. readIterator() {
  5270. const word = super.readWord1();
  5271. const fullWord = "@@" + word;
  5272. if (!this.isIterator(word) || !this.state.inType) {
  5273. this.raise(Errors.InvalidIdentifier, this.state.curPosition(), {
  5274. identifierName: fullWord
  5275. });
  5276. }
  5277. this.finishToken(132, fullWord);
  5278. }
  5279. getTokenFromCode(code) {
  5280. const next = this.input.charCodeAt(this.state.pos + 1);
  5281. if (code === 123 && next === 124) {
  5282. this.finishOp(6, 2);
  5283. } else if (this.state.inType && (code === 62 || code === 60)) {
  5284. this.finishOp(code === 62 ? 48 : 47, 1);
  5285. } else if (this.state.inType && code === 63) {
  5286. if (next === 46) {
  5287. this.finishOp(18, 2);
  5288. } else {
  5289. this.finishOp(17, 1);
  5290. }
  5291. } else if (isIteratorStart(code, next, this.input.charCodeAt(this.state.pos + 2))) {
  5292. this.state.pos += 2;
  5293. this.readIterator();
  5294. } else {
  5295. super.getTokenFromCode(code);
  5296. }
  5297. }
  5298. isAssignable(node, isBinding) {
  5299. if (node.type === "TypeCastExpression") {
  5300. return this.isAssignable(node.expression, isBinding);
  5301. } else {
  5302. return super.isAssignable(node, isBinding);
  5303. }
  5304. }
  5305. toAssignable(node, isLHS = false) {
  5306. if (!isLHS && node.type === "AssignmentExpression" && node.left.type === "TypeCastExpression") {
  5307. node.left = this.typeCastToParameter(node.left);
  5308. }
  5309. super.toAssignable(node, isLHS);
  5310. }
  5311. toAssignableList(exprList, trailingCommaLoc, isLHS) {
  5312. for (let i = 0; i < exprList.length; i++) {
  5313. const expr = exprList[i];
  5314. if ((expr == null ? void 0 : expr.type) === "TypeCastExpression") {
  5315. exprList[i] = this.typeCastToParameter(expr);
  5316. }
  5317. }
  5318. super.toAssignableList(exprList, trailingCommaLoc, isLHS);
  5319. }
  5320. toReferencedList(exprList, isParenthesizedExpr) {
  5321. for (let i = 0; i < exprList.length; i++) {
  5322. var _expr$extra;
  5323. const expr = exprList[i];
  5324. if (expr && expr.type === "TypeCastExpression" && !((_expr$extra = expr.extra) != null && _expr$extra.parenthesized) && (exprList.length > 1 || !isParenthesizedExpr)) {
  5325. this.raise(FlowErrors.TypeCastInPattern, expr.typeAnnotation);
  5326. }
  5327. }
  5328. return exprList;
  5329. }
  5330. parseArrayLike(close, canBePattern, isTuple, refExpressionErrors) {
  5331. const node = super.parseArrayLike(close, canBePattern, isTuple, refExpressionErrors);
  5332. if (canBePattern && !this.state.maybeInArrowParameters) {
  5333. this.toReferencedList(node.elements);
  5334. }
  5335. return node;
  5336. }
  5337. isValidLVal(type, isParenthesized, binding) {
  5338. return type === "TypeCastExpression" || super.isValidLVal(type, isParenthesized, binding);
  5339. }
  5340. parseClassProperty(node) {
  5341. if (this.match(14)) {
  5342. node.typeAnnotation = this.flowParseTypeAnnotation();
  5343. }
  5344. return super.parseClassProperty(node);
  5345. }
  5346. parseClassPrivateProperty(node) {
  5347. if (this.match(14)) {
  5348. node.typeAnnotation = this.flowParseTypeAnnotation();
  5349. }
  5350. return super.parseClassPrivateProperty(node);
  5351. }
  5352. isClassMethod() {
  5353. return this.match(47) || super.isClassMethod();
  5354. }
  5355. isClassProperty() {
  5356. return this.match(14) || super.isClassProperty();
  5357. }
  5358. isNonstaticConstructor(method) {
  5359. return !this.match(14) && super.isNonstaticConstructor(method);
  5360. }
  5361. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  5362. if (method.variance) {
  5363. this.unexpected(method.variance.loc.start);
  5364. }
  5365. delete method.variance;
  5366. if (this.match(47)) {
  5367. method.typeParameters = this.flowParseTypeParameterDeclaration();
  5368. }
  5369. super.pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper);
  5370. if (method.params && isConstructor) {
  5371. const params = method.params;
  5372. if (params.length > 0 && this.isThisParam(params[0])) {
  5373. this.raise(FlowErrors.ThisParamBannedInConstructor, method);
  5374. }
  5375. } else if (method.type === "MethodDefinition" && isConstructor && method.value.params) {
  5376. const params = method.value.params;
  5377. if (params.length > 0 && this.isThisParam(params[0])) {
  5378. this.raise(FlowErrors.ThisParamBannedInConstructor, method);
  5379. }
  5380. }
  5381. }
  5382. pushClassPrivateMethod(classBody, method, isGenerator, isAsync) {
  5383. if (method.variance) {
  5384. this.unexpected(method.variance.loc.start);
  5385. }
  5386. delete method.variance;
  5387. if (this.match(47)) {
  5388. method.typeParameters = this.flowParseTypeParameterDeclaration();
  5389. }
  5390. super.pushClassPrivateMethod(classBody, method, isGenerator, isAsync);
  5391. }
  5392. parseClassSuper(node) {
  5393. super.parseClassSuper(node);
  5394. if (node.superClass && this.match(47)) {
  5395. node.superTypeParameters = this.flowParseTypeParameterInstantiation();
  5396. }
  5397. if (this.isContextual(113)) {
  5398. this.next();
  5399. const implemented = node.implements = [];
  5400. do {
  5401. const node = this.startNode();
  5402. node.id = this.flowParseRestrictedIdentifier(true);
  5403. if (this.match(47)) {
  5404. node.typeParameters = this.flowParseTypeParameterInstantiation();
  5405. } else {
  5406. node.typeParameters = null;
  5407. }
  5408. implemented.push(this.finishNode(node, "ClassImplements"));
  5409. } while (this.eat(12));
  5410. }
  5411. }
  5412. checkGetterSetterParams(method) {
  5413. super.checkGetterSetterParams(method);
  5414. const params = this.getObjectOrClassMethodParams(method);
  5415. if (params.length > 0) {
  5416. const param = params[0];
  5417. if (this.isThisParam(param) && method.kind === "get") {
  5418. this.raise(FlowErrors.GetterMayNotHaveThisParam, param);
  5419. } else if (this.isThisParam(param)) {
  5420. this.raise(FlowErrors.SetterMayNotHaveThisParam, param);
  5421. }
  5422. }
  5423. }
  5424. parsePropertyNamePrefixOperator(node) {
  5425. node.variance = this.flowParseVariance();
  5426. }
  5427. parseObjPropValue(prop, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors) {
  5428. if (prop.variance) {
  5429. this.unexpected(prop.variance.loc.start);
  5430. }
  5431. delete prop.variance;
  5432. let typeParameters;
  5433. if (this.match(47) && !isAccessor) {
  5434. typeParameters = this.flowParseTypeParameterDeclaration();
  5435. if (!this.match(10)) this.unexpected();
  5436. }
  5437. const result = super.parseObjPropValue(prop, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors);
  5438. if (typeParameters) {
  5439. (result.value || result).typeParameters = typeParameters;
  5440. }
  5441. return result;
  5442. }
  5443. parseAssignableListItemTypes(param) {
  5444. if (this.eat(17)) {
  5445. if (param.type !== "Identifier") {
  5446. this.raise(FlowErrors.PatternIsOptional, param);
  5447. }
  5448. if (this.isThisParam(param)) {
  5449. this.raise(FlowErrors.ThisParamMayNotBeOptional, param);
  5450. }
  5451. param.optional = true;
  5452. }
  5453. if (this.match(14)) {
  5454. param.typeAnnotation = this.flowParseTypeAnnotation();
  5455. } else if (this.isThisParam(param)) {
  5456. this.raise(FlowErrors.ThisParamAnnotationRequired, param);
  5457. }
  5458. if (this.match(29) && this.isThisParam(param)) {
  5459. this.raise(FlowErrors.ThisParamNoDefault, param);
  5460. }
  5461. this.resetEndLocation(param);
  5462. return param;
  5463. }
  5464. parseMaybeDefault(startLoc, left) {
  5465. const node = super.parseMaybeDefault(startLoc, left);
  5466. if (node.type === "AssignmentPattern" && node.typeAnnotation && node.right.start < node.typeAnnotation.start) {
  5467. this.raise(FlowErrors.TypeBeforeInitializer, node.typeAnnotation);
  5468. }
  5469. return node;
  5470. }
  5471. checkImportReflection(node) {
  5472. super.checkImportReflection(node);
  5473. if (node.module && node.importKind !== "value") {
  5474. this.raise(FlowErrors.ImportReflectionHasImportType, node.specifiers[0].loc.start);
  5475. }
  5476. }
  5477. parseImportSpecifierLocal(node, specifier, type) {
  5478. specifier.local = hasTypeImportKind(node) ? this.flowParseRestrictedIdentifier(true, true) : this.parseIdentifier();
  5479. node.specifiers.push(this.finishImportSpecifier(specifier, type));
  5480. }
  5481. isPotentialImportPhase(isExport) {
  5482. if (super.isPotentialImportPhase(isExport)) return true;
  5483. if (this.isContextual(130)) {
  5484. if (!isExport) return true;
  5485. const ch = this.lookaheadCharCode();
  5486. return ch === 123 || ch === 42;
  5487. }
  5488. return !isExport && this.isContextual(87);
  5489. }
  5490. applyImportPhase(node, isExport, phase, loc) {
  5491. super.applyImportPhase(node, isExport, phase, loc);
  5492. if (isExport) {
  5493. if (!phase && this.match(65)) {
  5494. return;
  5495. }
  5496. node.exportKind = phase === "type" ? phase : "value";
  5497. } else {
  5498. if (phase === "type" && this.match(55)) this.unexpected();
  5499. node.importKind = phase === "type" || phase === "typeof" ? phase : "value";
  5500. }
  5501. }
  5502. parseImportSpecifier(specifier, importedIsString, isInTypeOnlyImport, isMaybeTypeOnly, bindingType) {
  5503. const firstIdent = specifier.imported;
  5504. let specifierTypeKind = null;
  5505. if (firstIdent.type === "Identifier") {
  5506. if (firstIdent.name === "type") {
  5507. specifierTypeKind = "type";
  5508. } else if (firstIdent.name === "typeof") {
  5509. specifierTypeKind = "typeof";
  5510. }
  5511. }
  5512. let isBinding = false;
  5513. if (this.isContextual(93) && !this.isLookaheadContextual("as")) {
  5514. const as_ident = this.parseIdentifier(true);
  5515. if (specifierTypeKind !== null && !tokenIsKeywordOrIdentifier(this.state.type)) {
  5516. specifier.imported = as_ident;
  5517. specifier.importKind = specifierTypeKind;
  5518. specifier.local = cloneIdentifier(as_ident);
  5519. } else {
  5520. specifier.imported = firstIdent;
  5521. specifier.importKind = null;
  5522. specifier.local = this.parseIdentifier();
  5523. }
  5524. } else {
  5525. if (specifierTypeKind !== null && tokenIsKeywordOrIdentifier(this.state.type)) {
  5526. specifier.imported = this.parseIdentifier(true);
  5527. specifier.importKind = specifierTypeKind;
  5528. } else {
  5529. if (importedIsString) {
  5530. throw this.raise(Errors.ImportBindingIsString, specifier, {
  5531. importName: firstIdent.value
  5532. });
  5533. }
  5534. specifier.imported = firstIdent;
  5535. specifier.importKind = null;
  5536. }
  5537. if (this.eatContextual(93)) {
  5538. specifier.local = this.parseIdentifier();
  5539. } else {
  5540. isBinding = true;
  5541. specifier.local = cloneIdentifier(specifier.imported);
  5542. }
  5543. }
  5544. const specifierIsTypeImport = hasTypeImportKind(specifier);
  5545. if (isInTypeOnlyImport && specifierIsTypeImport) {
  5546. this.raise(FlowErrors.ImportTypeShorthandOnlyInPureImport, specifier);
  5547. }
  5548. if (isInTypeOnlyImport || specifierIsTypeImport) {
  5549. this.checkReservedType(specifier.local.name, specifier.local.loc.start, true);
  5550. }
  5551. if (isBinding && !isInTypeOnlyImport && !specifierIsTypeImport) {
  5552. this.checkReservedWord(specifier.local.name, specifier.loc.start, true, true);
  5553. }
  5554. return this.finishImportSpecifier(specifier, "ImportSpecifier");
  5555. }
  5556. parseBindingAtom() {
  5557. switch (this.state.type) {
  5558. case 78:
  5559. return this.parseIdentifier(true);
  5560. default:
  5561. return super.parseBindingAtom();
  5562. }
  5563. }
  5564. parseFunctionParams(node, isConstructor) {
  5565. const kind = node.kind;
  5566. if (kind !== "get" && kind !== "set" && this.match(47)) {
  5567. node.typeParameters = this.flowParseTypeParameterDeclaration();
  5568. }
  5569. super.parseFunctionParams(node, isConstructor);
  5570. }
  5571. parseVarId(decl, kind) {
  5572. super.parseVarId(decl, kind);
  5573. if (this.match(14)) {
  5574. decl.id.typeAnnotation = this.flowParseTypeAnnotation();
  5575. this.resetEndLocation(decl.id);
  5576. }
  5577. }
  5578. parseAsyncArrowFromCallExpression(node, call) {
  5579. if (this.match(14)) {
  5580. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  5581. this.state.noAnonFunctionType = true;
  5582. node.returnType = this.flowParseTypeAnnotation();
  5583. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  5584. }
  5585. return super.parseAsyncArrowFromCallExpression(node, call);
  5586. }
  5587. shouldParseAsyncArrow() {
  5588. return this.match(14) || super.shouldParseAsyncArrow();
  5589. }
  5590. parseMaybeAssign(refExpressionErrors, afterLeftParse) {
  5591. var _jsx;
  5592. let state = null;
  5593. let jsx;
  5594. if (this.hasPlugin("jsx") && (this.match(142) || this.match(47))) {
  5595. state = this.state.clone();
  5596. jsx = this.tryParse(() => super.parseMaybeAssign(refExpressionErrors, afterLeftParse), state);
  5597. if (!jsx.error) return jsx.node;
  5598. const {
  5599. context
  5600. } = this.state;
  5601. const currentContext = context[context.length - 1];
  5602. if (currentContext === types.j_oTag || currentContext === types.j_expr) {
  5603. context.pop();
  5604. }
  5605. }
  5606. if ((_jsx = jsx) != null && _jsx.error || this.match(47)) {
  5607. var _jsx2, _jsx3;
  5608. state = state || this.state.clone();
  5609. let typeParameters;
  5610. const arrow = this.tryParse(abort => {
  5611. var _arrowExpression$extr;
  5612. typeParameters = this.flowParseTypeParameterDeclaration();
  5613. const arrowExpression = this.forwardNoArrowParamsConversionAt(typeParameters, () => {
  5614. const result = super.parseMaybeAssign(refExpressionErrors, afterLeftParse);
  5615. this.resetStartLocationFromNode(result, typeParameters);
  5616. return result;
  5617. });
  5618. if ((_arrowExpression$extr = arrowExpression.extra) != null && _arrowExpression$extr.parenthesized) abort();
  5619. const expr = this.maybeUnwrapTypeCastExpression(arrowExpression);
  5620. if (expr.type !== "ArrowFunctionExpression") abort();
  5621. expr.typeParameters = typeParameters;
  5622. this.resetStartLocationFromNode(expr, typeParameters);
  5623. return arrowExpression;
  5624. }, state);
  5625. let arrowExpression = null;
  5626. if (arrow.node && this.maybeUnwrapTypeCastExpression(arrow.node).type === "ArrowFunctionExpression") {
  5627. if (!arrow.error && !arrow.aborted) {
  5628. if (arrow.node.async) {
  5629. this.raise(FlowErrors.UnexpectedTypeParameterBeforeAsyncArrowFunction, typeParameters);
  5630. }
  5631. return arrow.node;
  5632. }
  5633. arrowExpression = arrow.node;
  5634. }
  5635. if ((_jsx2 = jsx) != null && _jsx2.node) {
  5636. this.state = jsx.failState;
  5637. return jsx.node;
  5638. }
  5639. if (arrowExpression) {
  5640. this.state = arrow.failState;
  5641. return arrowExpression;
  5642. }
  5643. if ((_jsx3 = jsx) != null && _jsx3.thrown) throw jsx.error;
  5644. if (arrow.thrown) throw arrow.error;
  5645. throw this.raise(FlowErrors.UnexpectedTokenAfterTypeParameter, typeParameters);
  5646. }
  5647. return super.parseMaybeAssign(refExpressionErrors, afterLeftParse);
  5648. }
  5649. parseArrow(node) {
  5650. if (this.match(14)) {
  5651. const result = this.tryParse(() => {
  5652. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  5653. this.state.noAnonFunctionType = true;
  5654. const typeNode = this.startNode();
  5655. [typeNode.typeAnnotation, node.predicate] = this.flowParseTypeAndPredicateInitialiser();
  5656. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  5657. if (this.canInsertSemicolon()) this.unexpected();
  5658. if (!this.match(19)) this.unexpected();
  5659. return typeNode;
  5660. });
  5661. if (result.thrown) return null;
  5662. if (result.error) this.state = result.failState;
  5663. node.returnType = result.node.typeAnnotation ? this.finishNode(result.node, "TypeAnnotation") : null;
  5664. }
  5665. return super.parseArrow(node);
  5666. }
  5667. shouldParseArrow(params) {
  5668. return this.match(14) || super.shouldParseArrow(params);
  5669. }
  5670. setArrowFunctionParameters(node, params) {
  5671. if (this.state.noArrowParamsConversionAt.indexOf(node.start) !== -1) {
  5672. node.params = params;
  5673. } else {
  5674. super.setArrowFunctionParameters(node, params);
  5675. }
  5676. }
  5677. checkParams(node, allowDuplicates, isArrowFunction, strictModeChanged = true) {
  5678. if (isArrowFunction && this.state.noArrowParamsConversionAt.indexOf(node.start) !== -1) {
  5679. return;
  5680. }
  5681. for (let i = 0; i < node.params.length; i++) {
  5682. if (this.isThisParam(node.params[i]) && i > 0) {
  5683. this.raise(FlowErrors.ThisParamMustBeFirst, node.params[i]);
  5684. }
  5685. }
  5686. super.checkParams(node, allowDuplicates, isArrowFunction, strictModeChanged);
  5687. }
  5688. parseParenAndDistinguishExpression(canBeArrow) {
  5689. return super.parseParenAndDistinguishExpression(canBeArrow && this.state.noArrowAt.indexOf(this.state.start) === -1);
  5690. }
  5691. parseSubscripts(base, startLoc, noCalls) {
  5692. if (base.type === "Identifier" && base.name === "async" && this.state.noArrowAt.indexOf(startLoc.index) !== -1) {
  5693. this.next();
  5694. const node = this.startNodeAt(startLoc);
  5695. node.callee = base;
  5696. node.arguments = super.parseCallExpressionArguments(11, false);
  5697. base = this.finishNode(node, "CallExpression");
  5698. } else if (base.type === "Identifier" && base.name === "async" && this.match(47)) {
  5699. const state = this.state.clone();
  5700. const arrow = this.tryParse(abort => this.parseAsyncArrowWithTypeParameters(startLoc) || abort(), state);
  5701. if (!arrow.error && !arrow.aborted) return arrow.node;
  5702. const result = this.tryParse(() => super.parseSubscripts(base, startLoc, noCalls), state);
  5703. if (result.node && !result.error) return result.node;
  5704. if (arrow.node) {
  5705. this.state = arrow.failState;
  5706. return arrow.node;
  5707. }
  5708. if (result.node) {
  5709. this.state = result.failState;
  5710. return result.node;
  5711. }
  5712. throw arrow.error || result.error;
  5713. }
  5714. return super.parseSubscripts(base, startLoc, noCalls);
  5715. }
  5716. parseSubscript(base, startLoc, noCalls, subscriptState) {
  5717. if (this.match(18) && this.isLookaheadToken_lt()) {
  5718. subscriptState.optionalChainMember = true;
  5719. if (noCalls) {
  5720. subscriptState.stop = true;
  5721. return base;
  5722. }
  5723. this.next();
  5724. const node = this.startNodeAt(startLoc);
  5725. node.callee = base;
  5726. node.typeArguments = this.flowParseTypeParameterInstantiation();
  5727. this.expect(10);
  5728. node.arguments = this.parseCallExpressionArguments(11, false);
  5729. node.optional = true;
  5730. return this.finishCallExpression(node, true);
  5731. } else if (!noCalls && this.shouldParseTypes() && this.match(47)) {
  5732. const node = this.startNodeAt(startLoc);
  5733. node.callee = base;
  5734. const result = this.tryParse(() => {
  5735. node.typeArguments = this.flowParseTypeParameterInstantiationCallOrNew();
  5736. this.expect(10);
  5737. node.arguments = super.parseCallExpressionArguments(11, false);
  5738. if (subscriptState.optionalChainMember) {
  5739. node.optional = false;
  5740. }
  5741. return this.finishCallExpression(node, subscriptState.optionalChainMember);
  5742. });
  5743. if (result.node) {
  5744. if (result.error) this.state = result.failState;
  5745. return result.node;
  5746. }
  5747. }
  5748. return super.parseSubscript(base, startLoc, noCalls, subscriptState);
  5749. }
  5750. parseNewCallee(node) {
  5751. super.parseNewCallee(node);
  5752. let targs = null;
  5753. if (this.shouldParseTypes() && this.match(47)) {
  5754. targs = this.tryParse(() => this.flowParseTypeParameterInstantiationCallOrNew()).node;
  5755. }
  5756. node.typeArguments = targs;
  5757. }
  5758. parseAsyncArrowWithTypeParameters(startLoc) {
  5759. const node = this.startNodeAt(startLoc);
  5760. this.parseFunctionParams(node, false);
  5761. if (!this.parseArrow(node)) return;
  5762. return super.parseArrowExpression(node, undefined, true);
  5763. }
  5764. readToken_mult_modulo(code) {
  5765. const next = this.input.charCodeAt(this.state.pos + 1);
  5766. if (code === 42 && next === 47 && this.state.hasFlowComment) {
  5767. this.state.hasFlowComment = false;
  5768. this.state.pos += 2;
  5769. this.nextToken();
  5770. return;
  5771. }
  5772. super.readToken_mult_modulo(code);
  5773. }
  5774. readToken_pipe_amp(code) {
  5775. const next = this.input.charCodeAt(this.state.pos + 1);
  5776. if (code === 124 && next === 125) {
  5777. this.finishOp(9, 2);
  5778. return;
  5779. }
  5780. super.readToken_pipe_amp(code);
  5781. }
  5782. parseTopLevel(file, program) {
  5783. const fileNode = super.parseTopLevel(file, program);
  5784. if (this.state.hasFlowComment) {
  5785. this.raise(FlowErrors.UnterminatedFlowComment, this.state.curPosition());
  5786. }
  5787. return fileNode;
  5788. }
  5789. skipBlockComment() {
  5790. if (this.hasPlugin("flowComments") && this.skipFlowComment()) {
  5791. if (this.state.hasFlowComment) {
  5792. throw this.raise(FlowErrors.NestedFlowComment, this.state.startLoc);
  5793. }
  5794. this.hasFlowCommentCompletion();
  5795. const commentSkip = this.skipFlowComment();
  5796. if (commentSkip) {
  5797. this.state.pos += commentSkip;
  5798. this.state.hasFlowComment = true;
  5799. }
  5800. return;
  5801. }
  5802. return super.skipBlockComment(this.state.hasFlowComment ? "*-/" : "*/");
  5803. }
  5804. skipFlowComment() {
  5805. const {
  5806. pos
  5807. } = this.state;
  5808. let shiftToFirstNonWhiteSpace = 2;
  5809. while ([32, 9].includes(this.input.charCodeAt(pos + shiftToFirstNonWhiteSpace))) {
  5810. shiftToFirstNonWhiteSpace++;
  5811. }
  5812. const ch2 = this.input.charCodeAt(shiftToFirstNonWhiteSpace + pos);
  5813. const ch3 = this.input.charCodeAt(shiftToFirstNonWhiteSpace + pos + 1);
  5814. if (ch2 === 58 && ch3 === 58) {
  5815. return shiftToFirstNonWhiteSpace + 2;
  5816. }
  5817. if (this.input.slice(shiftToFirstNonWhiteSpace + pos, shiftToFirstNonWhiteSpace + pos + 12) === "flow-include") {
  5818. return shiftToFirstNonWhiteSpace + 12;
  5819. }
  5820. if (ch2 === 58 && ch3 !== 58) {
  5821. return shiftToFirstNonWhiteSpace;
  5822. }
  5823. return false;
  5824. }
  5825. hasFlowCommentCompletion() {
  5826. const end = this.input.indexOf("*/", this.state.pos);
  5827. if (end === -1) {
  5828. throw this.raise(Errors.UnterminatedComment, this.state.curPosition());
  5829. }
  5830. }
  5831. flowEnumErrorBooleanMemberNotInitialized(loc, {
  5832. enumName,
  5833. memberName
  5834. }) {
  5835. this.raise(FlowErrors.EnumBooleanMemberNotInitialized, loc, {
  5836. memberName,
  5837. enumName
  5838. });
  5839. }
  5840. flowEnumErrorInvalidMemberInitializer(loc, enumContext) {
  5841. return this.raise(!enumContext.explicitType ? FlowErrors.EnumInvalidMemberInitializerUnknownType : enumContext.explicitType === "symbol" ? FlowErrors.EnumInvalidMemberInitializerSymbolType : FlowErrors.EnumInvalidMemberInitializerPrimaryType, loc, enumContext);
  5842. }
  5843. flowEnumErrorNumberMemberNotInitialized(loc, details) {
  5844. this.raise(FlowErrors.EnumNumberMemberNotInitialized, loc, details);
  5845. }
  5846. flowEnumErrorStringMemberInconsistentlyInitialized(node, details) {
  5847. this.raise(FlowErrors.EnumStringMemberInconsistentlyInitialized, node, details);
  5848. }
  5849. flowEnumMemberInit() {
  5850. const startLoc = this.state.startLoc;
  5851. const endOfInit = () => this.match(12) || this.match(8);
  5852. switch (this.state.type) {
  5853. case 134:
  5854. {
  5855. const literal = this.parseNumericLiteral(this.state.value);
  5856. if (endOfInit()) {
  5857. return {
  5858. type: "number",
  5859. loc: literal.loc.start,
  5860. value: literal
  5861. };
  5862. }
  5863. return {
  5864. type: "invalid",
  5865. loc: startLoc
  5866. };
  5867. }
  5868. case 133:
  5869. {
  5870. const literal = this.parseStringLiteral(this.state.value);
  5871. if (endOfInit()) {
  5872. return {
  5873. type: "string",
  5874. loc: literal.loc.start,
  5875. value: literal
  5876. };
  5877. }
  5878. return {
  5879. type: "invalid",
  5880. loc: startLoc
  5881. };
  5882. }
  5883. case 85:
  5884. case 86:
  5885. {
  5886. const literal = this.parseBooleanLiteral(this.match(85));
  5887. if (endOfInit()) {
  5888. return {
  5889. type: "boolean",
  5890. loc: literal.loc.start,
  5891. value: literal
  5892. };
  5893. }
  5894. return {
  5895. type: "invalid",
  5896. loc: startLoc
  5897. };
  5898. }
  5899. default:
  5900. return {
  5901. type: "invalid",
  5902. loc: startLoc
  5903. };
  5904. }
  5905. }
  5906. flowEnumMemberRaw() {
  5907. const loc = this.state.startLoc;
  5908. const id = this.parseIdentifier(true);
  5909. const init = this.eat(29) ? this.flowEnumMemberInit() : {
  5910. type: "none",
  5911. loc
  5912. };
  5913. return {
  5914. id,
  5915. init
  5916. };
  5917. }
  5918. flowEnumCheckExplicitTypeMismatch(loc, context, expectedType) {
  5919. const {
  5920. explicitType
  5921. } = context;
  5922. if (explicitType === null) {
  5923. return;
  5924. }
  5925. if (explicitType !== expectedType) {
  5926. this.flowEnumErrorInvalidMemberInitializer(loc, context);
  5927. }
  5928. }
  5929. flowEnumMembers({
  5930. enumName,
  5931. explicitType
  5932. }) {
  5933. const seenNames = new Set();
  5934. const members = {
  5935. booleanMembers: [],
  5936. numberMembers: [],
  5937. stringMembers: [],
  5938. defaultedMembers: []
  5939. };
  5940. let hasUnknownMembers = false;
  5941. while (!this.match(8)) {
  5942. if (this.eat(21)) {
  5943. hasUnknownMembers = true;
  5944. break;
  5945. }
  5946. const memberNode = this.startNode();
  5947. const {
  5948. id,
  5949. init
  5950. } = this.flowEnumMemberRaw();
  5951. const memberName = id.name;
  5952. if (memberName === "") {
  5953. continue;
  5954. }
  5955. if (/^[a-z]/.test(memberName)) {
  5956. this.raise(FlowErrors.EnumInvalidMemberName, id, {
  5957. memberName,
  5958. suggestion: memberName[0].toUpperCase() + memberName.slice(1),
  5959. enumName
  5960. });
  5961. }
  5962. if (seenNames.has(memberName)) {
  5963. this.raise(FlowErrors.EnumDuplicateMemberName, id, {
  5964. memberName,
  5965. enumName
  5966. });
  5967. }
  5968. seenNames.add(memberName);
  5969. const context = {
  5970. enumName,
  5971. explicitType,
  5972. memberName
  5973. };
  5974. memberNode.id = id;
  5975. switch (init.type) {
  5976. case "boolean":
  5977. {
  5978. this.flowEnumCheckExplicitTypeMismatch(init.loc, context, "boolean");
  5979. memberNode.init = init.value;
  5980. members.booleanMembers.push(this.finishNode(memberNode, "EnumBooleanMember"));
  5981. break;
  5982. }
  5983. case "number":
  5984. {
  5985. this.flowEnumCheckExplicitTypeMismatch(init.loc, context, "number");
  5986. memberNode.init = init.value;
  5987. members.numberMembers.push(this.finishNode(memberNode, "EnumNumberMember"));
  5988. break;
  5989. }
  5990. case "string":
  5991. {
  5992. this.flowEnumCheckExplicitTypeMismatch(init.loc, context, "string");
  5993. memberNode.init = init.value;
  5994. members.stringMembers.push(this.finishNode(memberNode, "EnumStringMember"));
  5995. break;
  5996. }
  5997. case "invalid":
  5998. {
  5999. throw this.flowEnumErrorInvalidMemberInitializer(init.loc, context);
  6000. }
  6001. case "none":
  6002. {
  6003. switch (explicitType) {
  6004. case "boolean":
  6005. this.flowEnumErrorBooleanMemberNotInitialized(init.loc, context);
  6006. break;
  6007. case "number":
  6008. this.flowEnumErrorNumberMemberNotInitialized(init.loc, context);
  6009. break;
  6010. default:
  6011. members.defaultedMembers.push(this.finishNode(memberNode, "EnumDefaultedMember"));
  6012. }
  6013. }
  6014. }
  6015. if (!this.match(8)) {
  6016. this.expect(12);
  6017. }
  6018. }
  6019. return {
  6020. members,
  6021. hasUnknownMembers
  6022. };
  6023. }
  6024. flowEnumStringMembers(initializedMembers, defaultedMembers, {
  6025. enumName
  6026. }) {
  6027. if (initializedMembers.length === 0) {
  6028. return defaultedMembers;
  6029. } else if (defaultedMembers.length === 0) {
  6030. return initializedMembers;
  6031. } else if (defaultedMembers.length > initializedMembers.length) {
  6032. for (const member of initializedMembers) {
  6033. this.flowEnumErrorStringMemberInconsistentlyInitialized(member, {
  6034. enumName
  6035. });
  6036. }
  6037. return defaultedMembers;
  6038. } else {
  6039. for (const member of defaultedMembers) {
  6040. this.flowEnumErrorStringMemberInconsistentlyInitialized(member, {
  6041. enumName
  6042. });
  6043. }
  6044. return initializedMembers;
  6045. }
  6046. }
  6047. flowEnumParseExplicitType({
  6048. enumName
  6049. }) {
  6050. if (!this.eatContextual(102)) return null;
  6051. if (!tokenIsIdentifier(this.state.type)) {
  6052. throw this.raise(FlowErrors.EnumInvalidExplicitTypeUnknownSupplied, this.state.startLoc, {
  6053. enumName
  6054. });
  6055. }
  6056. const {
  6057. value
  6058. } = this.state;
  6059. this.next();
  6060. if (value !== "boolean" && value !== "number" && value !== "string" && value !== "symbol") {
  6061. this.raise(FlowErrors.EnumInvalidExplicitType, this.state.startLoc, {
  6062. enumName,
  6063. invalidEnumType: value
  6064. });
  6065. }
  6066. return value;
  6067. }
  6068. flowEnumBody(node, id) {
  6069. const enumName = id.name;
  6070. const nameLoc = id.loc.start;
  6071. const explicitType = this.flowEnumParseExplicitType({
  6072. enumName
  6073. });
  6074. this.expect(5);
  6075. const {
  6076. members,
  6077. hasUnknownMembers
  6078. } = this.flowEnumMembers({
  6079. enumName,
  6080. explicitType
  6081. });
  6082. node.hasUnknownMembers = hasUnknownMembers;
  6083. switch (explicitType) {
  6084. case "boolean":
  6085. node.explicitType = true;
  6086. node.members = members.booleanMembers;
  6087. this.expect(8);
  6088. return this.finishNode(node, "EnumBooleanBody");
  6089. case "number":
  6090. node.explicitType = true;
  6091. node.members = members.numberMembers;
  6092. this.expect(8);
  6093. return this.finishNode(node, "EnumNumberBody");
  6094. case "string":
  6095. node.explicitType = true;
  6096. node.members = this.flowEnumStringMembers(members.stringMembers, members.defaultedMembers, {
  6097. enumName
  6098. });
  6099. this.expect(8);
  6100. return this.finishNode(node, "EnumStringBody");
  6101. case "symbol":
  6102. node.members = members.defaultedMembers;
  6103. this.expect(8);
  6104. return this.finishNode(node, "EnumSymbolBody");
  6105. default:
  6106. {
  6107. const empty = () => {
  6108. node.members = [];
  6109. this.expect(8);
  6110. return this.finishNode(node, "EnumStringBody");
  6111. };
  6112. node.explicitType = false;
  6113. const boolsLen = members.booleanMembers.length;
  6114. const numsLen = members.numberMembers.length;
  6115. const strsLen = members.stringMembers.length;
  6116. const defaultedLen = members.defaultedMembers.length;
  6117. if (!boolsLen && !numsLen && !strsLen && !defaultedLen) {
  6118. return empty();
  6119. } else if (!boolsLen && !numsLen) {
  6120. node.members = this.flowEnumStringMembers(members.stringMembers, members.defaultedMembers, {
  6121. enumName
  6122. });
  6123. this.expect(8);
  6124. return this.finishNode(node, "EnumStringBody");
  6125. } else if (!numsLen && !strsLen && boolsLen >= defaultedLen) {
  6126. for (const member of members.defaultedMembers) {
  6127. this.flowEnumErrorBooleanMemberNotInitialized(member.loc.start, {
  6128. enumName,
  6129. memberName: member.id.name
  6130. });
  6131. }
  6132. node.members = members.booleanMembers;
  6133. this.expect(8);
  6134. return this.finishNode(node, "EnumBooleanBody");
  6135. } else if (!boolsLen && !strsLen && numsLen >= defaultedLen) {
  6136. for (const member of members.defaultedMembers) {
  6137. this.flowEnumErrorNumberMemberNotInitialized(member.loc.start, {
  6138. enumName,
  6139. memberName: member.id.name
  6140. });
  6141. }
  6142. node.members = members.numberMembers;
  6143. this.expect(8);
  6144. return this.finishNode(node, "EnumNumberBody");
  6145. } else {
  6146. this.raise(FlowErrors.EnumInconsistentMemberValues, nameLoc, {
  6147. enumName
  6148. });
  6149. return empty();
  6150. }
  6151. }
  6152. }
  6153. }
  6154. flowParseEnumDeclaration(node) {
  6155. const id = this.parseIdentifier();
  6156. node.id = id;
  6157. node.body = this.flowEnumBody(this.startNode(), id);
  6158. return this.finishNode(node, "EnumDeclaration");
  6159. }
  6160. isLookaheadToken_lt() {
  6161. const next = this.nextTokenStart();
  6162. if (this.input.charCodeAt(next) === 60) {
  6163. const afterNext = this.input.charCodeAt(next + 1);
  6164. return afterNext !== 60 && afterNext !== 61;
  6165. }
  6166. return false;
  6167. }
  6168. maybeUnwrapTypeCastExpression(node) {
  6169. return node.type === "TypeCastExpression" ? node.expression : node;
  6170. }
  6171. };
  6172. const entities = {
  6173. __proto__: null,
  6174. quot: "\u0022",
  6175. amp: "&",
  6176. apos: "\u0027",
  6177. lt: "<",
  6178. gt: ">",
  6179. nbsp: "\u00A0",
  6180. iexcl: "\u00A1",
  6181. cent: "\u00A2",
  6182. pound: "\u00A3",
  6183. curren: "\u00A4",
  6184. yen: "\u00A5",
  6185. brvbar: "\u00A6",
  6186. sect: "\u00A7",
  6187. uml: "\u00A8",
  6188. copy: "\u00A9",
  6189. ordf: "\u00AA",
  6190. laquo: "\u00AB",
  6191. not: "\u00AC",
  6192. shy: "\u00AD",
  6193. reg: "\u00AE",
  6194. macr: "\u00AF",
  6195. deg: "\u00B0",
  6196. plusmn: "\u00B1",
  6197. sup2: "\u00B2",
  6198. sup3: "\u00B3",
  6199. acute: "\u00B4",
  6200. micro: "\u00B5",
  6201. para: "\u00B6",
  6202. middot: "\u00B7",
  6203. cedil: "\u00B8",
  6204. sup1: "\u00B9",
  6205. ordm: "\u00BA",
  6206. raquo: "\u00BB",
  6207. frac14: "\u00BC",
  6208. frac12: "\u00BD",
  6209. frac34: "\u00BE",
  6210. iquest: "\u00BF",
  6211. Agrave: "\u00C0",
  6212. Aacute: "\u00C1",
  6213. Acirc: "\u00C2",
  6214. Atilde: "\u00C3",
  6215. Auml: "\u00C4",
  6216. Aring: "\u00C5",
  6217. AElig: "\u00C6",
  6218. Ccedil: "\u00C7",
  6219. Egrave: "\u00C8",
  6220. Eacute: "\u00C9",
  6221. Ecirc: "\u00CA",
  6222. Euml: "\u00CB",
  6223. Igrave: "\u00CC",
  6224. Iacute: "\u00CD",
  6225. Icirc: "\u00CE",
  6226. Iuml: "\u00CF",
  6227. ETH: "\u00D0",
  6228. Ntilde: "\u00D1",
  6229. Ograve: "\u00D2",
  6230. Oacute: "\u00D3",
  6231. Ocirc: "\u00D4",
  6232. Otilde: "\u00D5",
  6233. Ouml: "\u00D6",
  6234. times: "\u00D7",
  6235. Oslash: "\u00D8",
  6236. Ugrave: "\u00D9",
  6237. Uacute: "\u00DA",
  6238. Ucirc: "\u00DB",
  6239. Uuml: "\u00DC",
  6240. Yacute: "\u00DD",
  6241. THORN: "\u00DE",
  6242. szlig: "\u00DF",
  6243. agrave: "\u00E0",
  6244. aacute: "\u00E1",
  6245. acirc: "\u00E2",
  6246. atilde: "\u00E3",
  6247. auml: "\u00E4",
  6248. aring: "\u00E5",
  6249. aelig: "\u00E6",
  6250. ccedil: "\u00E7",
  6251. egrave: "\u00E8",
  6252. eacute: "\u00E9",
  6253. ecirc: "\u00EA",
  6254. euml: "\u00EB",
  6255. igrave: "\u00EC",
  6256. iacute: "\u00ED",
  6257. icirc: "\u00EE",
  6258. iuml: "\u00EF",
  6259. eth: "\u00F0",
  6260. ntilde: "\u00F1",
  6261. ograve: "\u00F2",
  6262. oacute: "\u00F3",
  6263. ocirc: "\u00F4",
  6264. otilde: "\u00F5",
  6265. ouml: "\u00F6",
  6266. divide: "\u00F7",
  6267. oslash: "\u00F8",
  6268. ugrave: "\u00F9",
  6269. uacute: "\u00FA",
  6270. ucirc: "\u00FB",
  6271. uuml: "\u00FC",
  6272. yacute: "\u00FD",
  6273. thorn: "\u00FE",
  6274. yuml: "\u00FF",
  6275. OElig: "\u0152",
  6276. oelig: "\u0153",
  6277. Scaron: "\u0160",
  6278. scaron: "\u0161",
  6279. Yuml: "\u0178",
  6280. fnof: "\u0192",
  6281. circ: "\u02C6",
  6282. tilde: "\u02DC",
  6283. Alpha: "\u0391",
  6284. Beta: "\u0392",
  6285. Gamma: "\u0393",
  6286. Delta: "\u0394",
  6287. Epsilon: "\u0395",
  6288. Zeta: "\u0396",
  6289. Eta: "\u0397",
  6290. Theta: "\u0398",
  6291. Iota: "\u0399",
  6292. Kappa: "\u039A",
  6293. Lambda: "\u039B",
  6294. Mu: "\u039C",
  6295. Nu: "\u039D",
  6296. Xi: "\u039E",
  6297. Omicron: "\u039F",
  6298. Pi: "\u03A0",
  6299. Rho: "\u03A1",
  6300. Sigma: "\u03A3",
  6301. Tau: "\u03A4",
  6302. Upsilon: "\u03A5",
  6303. Phi: "\u03A6",
  6304. Chi: "\u03A7",
  6305. Psi: "\u03A8",
  6306. Omega: "\u03A9",
  6307. alpha: "\u03B1",
  6308. beta: "\u03B2",
  6309. gamma: "\u03B3",
  6310. delta: "\u03B4",
  6311. epsilon: "\u03B5",
  6312. zeta: "\u03B6",
  6313. eta: "\u03B7",
  6314. theta: "\u03B8",
  6315. iota: "\u03B9",
  6316. kappa: "\u03BA",
  6317. lambda: "\u03BB",
  6318. mu: "\u03BC",
  6319. nu: "\u03BD",
  6320. xi: "\u03BE",
  6321. omicron: "\u03BF",
  6322. pi: "\u03C0",
  6323. rho: "\u03C1",
  6324. sigmaf: "\u03C2",
  6325. sigma: "\u03C3",
  6326. tau: "\u03C4",
  6327. upsilon: "\u03C5",
  6328. phi: "\u03C6",
  6329. chi: "\u03C7",
  6330. psi: "\u03C8",
  6331. omega: "\u03C9",
  6332. thetasym: "\u03D1",
  6333. upsih: "\u03D2",
  6334. piv: "\u03D6",
  6335. ensp: "\u2002",
  6336. emsp: "\u2003",
  6337. thinsp: "\u2009",
  6338. zwnj: "\u200C",
  6339. zwj: "\u200D",
  6340. lrm: "\u200E",
  6341. rlm: "\u200F",
  6342. ndash: "\u2013",
  6343. mdash: "\u2014",
  6344. lsquo: "\u2018",
  6345. rsquo: "\u2019",
  6346. sbquo: "\u201A",
  6347. ldquo: "\u201C",
  6348. rdquo: "\u201D",
  6349. bdquo: "\u201E",
  6350. dagger: "\u2020",
  6351. Dagger: "\u2021",
  6352. bull: "\u2022",
  6353. hellip: "\u2026",
  6354. permil: "\u2030",
  6355. prime: "\u2032",
  6356. Prime: "\u2033",
  6357. lsaquo: "\u2039",
  6358. rsaquo: "\u203A",
  6359. oline: "\u203E",
  6360. frasl: "\u2044",
  6361. euro: "\u20AC",
  6362. image: "\u2111",
  6363. weierp: "\u2118",
  6364. real: "\u211C",
  6365. trade: "\u2122",
  6366. alefsym: "\u2135",
  6367. larr: "\u2190",
  6368. uarr: "\u2191",
  6369. rarr: "\u2192",
  6370. darr: "\u2193",
  6371. harr: "\u2194",
  6372. crarr: "\u21B5",
  6373. lArr: "\u21D0",
  6374. uArr: "\u21D1",
  6375. rArr: "\u21D2",
  6376. dArr: "\u21D3",
  6377. hArr: "\u21D4",
  6378. forall: "\u2200",
  6379. part: "\u2202",
  6380. exist: "\u2203",
  6381. empty: "\u2205",
  6382. nabla: "\u2207",
  6383. isin: "\u2208",
  6384. notin: "\u2209",
  6385. ni: "\u220B",
  6386. prod: "\u220F",
  6387. sum: "\u2211",
  6388. minus: "\u2212",
  6389. lowast: "\u2217",
  6390. radic: "\u221A",
  6391. prop: "\u221D",
  6392. infin: "\u221E",
  6393. ang: "\u2220",
  6394. and: "\u2227",
  6395. or: "\u2228",
  6396. cap: "\u2229",
  6397. cup: "\u222A",
  6398. int: "\u222B",
  6399. there4: "\u2234",
  6400. sim: "\u223C",
  6401. cong: "\u2245",
  6402. asymp: "\u2248",
  6403. ne: "\u2260",
  6404. equiv: "\u2261",
  6405. le: "\u2264",
  6406. ge: "\u2265",
  6407. sub: "\u2282",
  6408. sup: "\u2283",
  6409. nsub: "\u2284",
  6410. sube: "\u2286",
  6411. supe: "\u2287",
  6412. oplus: "\u2295",
  6413. otimes: "\u2297",
  6414. perp: "\u22A5",
  6415. sdot: "\u22C5",
  6416. lceil: "\u2308",
  6417. rceil: "\u2309",
  6418. lfloor: "\u230A",
  6419. rfloor: "\u230B",
  6420. lang: "\u2329",
  6421. rang: "\u232A",
  6422. loz: "\u25CA",
  6423. spades: "\u2660",
  6424. clubs: "\u2663",
  6425. hearts: "\u2665",
  6426. diams: "\u2666"
  6427. };
  6428. const JsxErrors = ParseErrorEnum`jsx`({
  6429. AttributeIsEmpty: "JSX attributes must only be assigned a non-empty expression.",
  6430. MissingClosingTagElement: ({
  6431. openingTagName
  6432. }) => `Expected corresponding JSX closing tag for <${openingTagName}>.`,
  6433. MissingClosingTagFragment: "Expected corresponding JSX closing tag for <>.",
  6434. UnexpectedSequenceExpression: "Sequence expressions cannot be directly nested inside JSX. Did you mean to wrap it in parentheses (...)?",
  6435. UnexpectedToken: ({
  6436. unexpected,
  6437. HTMLEntity
  6438. }) => `Unexpected token \`${unexpected}\`. Did you mean \`${HTMLEntity}\` or \`{'${unexpected}'}\`?`,
  6439. UnsupportedJsxValue: "JSX value should be either an expression or a quoted JSX text.",
  6440. UnterminatedJsxContent: "Unterminated JSX contents.",
  6441. UnwrappedAdjacentJSXElements: "Adjacent JSX elements must be wrapped in an enclosing tag. Did you want a JSX fragment <>...</>?"
  6442. });
  6443. function isFragment(object) {
  6444. return object ? object.type === "JSXOpeningFragment" || object.type === "JSXClosingFragment" : false;
  6445. }
  6446. function getQualifiedJSXName(object) {
  6447. if (object.type === "JSXIdentifier") {
  6448. return object.name;
  6449. }
  6450. if (object.type === "JSXNamespacedName") {
  6451. return object.namespace.name + ":" + object.name.name;
  6452. }
  6453. if (object.type === "JSXMemberExpression") {
  6454. return getQualifiedJSXName(object.object) + "." + getQualifiedJSXName(object.property);
  6455. }
  6456. throw new Error("Node had unexpected type: " + object.type);
  6457. }
  6458. var jsx = superClass => class JSXParserMixin extends superClass {
  6459. jsxReadToken() {
  6460. let out = "";
  6461. let chunkStart = this.state.pos;
  6462. for (;;) {
  6463. if (this.state.pos >= this.length) {
  6464. throw this.raise(JsxErrors.UnterminatedJsxContent, this.state.startLoc);
  6465. }
  6466. const ch = this.input.charCodeAt(this.state.pos);
  6467. switch (ch) {
  6468. case 60:
  6469. case 123:
  6470. if (this.state.pos === this.state.start) {
  6471. if (ch === 60 && this.state.canStartJSXElement) {
  6472. ++this.state.pos;
  6473. this.finishToken(142);
  6474. } else {
  6475. super.getTokenFromCode(ch);
  6476. }
  6477. return;
  6478. }
  6479. out += this.input.slice(chunkStart, this.state.pos);
  6480. this.finishToken(141, out);
  6481. return;
  6482. case 38:
  6483. out += this.input.slice(chunkStart, this.state.pos);
  6484. out += this.jsxReadEntity();
  6485. chunkStart = this.state.pos;
  6486. break;
  6487. case 62:
  6488. case 125:
  6489. default:
  6490. if (isNewLine(ch)) {
  6491. out += this.input.slice(chunkStart, this.state.pos);
  6492. out += this.jsxReadNewLine(true);
  6493. chunkStart = this.state.pos;
  6494. } else {
  6495. ++this.state.pos;
  6496. }
  6497. }
  6498. }
  6499. }
  6500. jsxReadNewLine(normalizeCRLF) {
  6501. const ch = this.input.charCodeAt(this.state.pos);
  6502. let out;
  6503. ++this.state.pos;
  6504. if (ch === 13 && this.input.charCodeAt(this.state.pos) === 10) {
  6505. ++this.state.pos;
  6506. out = normalizeCRLF ? "\n" : "\r\n";
  6507. } else {
  6508. out = String.fromCharCode(ch);
  6509. }
  6510. ++this.state.curLine;
  6511. this.state.lineStart = this.state.pos;
  6512. return out;
  6513. }
  6514. jsxReadString(quote) {
  6515. let out = "";
  6516. let chunkStart = ++this.state.pos;
  6517. for (;;) {
  6518. if (this.state.pos >= this.length) {
  6519. throw this.raise(Errors.UnterminatedString, this.state.startLoc);
  6520. }
  6521. const ch = this.input.charCodeAt(this.state.pos);
  6522. if (ch === quote) break;
  6523. if (ch === 38) {
  6524. out += this.input.slice(chunkStart, this.state.pos);
  6525. out += this.jsxReadEntity();
  6526. chunkStart = this.state.pos;
  6527. } else if (isNewLine(ch)) {
  6528. out += this.input.slice(chunkStart, this.state.pos);
  6529. out += this.jsxReadNewLine(false);
  6530. chunkStart = this.state.pos;
  6531. } else {
  6532. ++this.state.pos;
  6533. }
  6534. }
  6535. out += this.input.slice(chunkStart, this.state.pos++);
  6536. this.finishToken(133, out);
  6537. }
  6538. jsxReadEntity() {
  6539. const startPos = ++this.state.pos;
  6540. if (this.codePointAtPos(this.state.pos) === 35) {
  6541. ++this.state.pos;
  6542. let radix = 10;
  6543. if (this.codePointAtPos(this.state.pos) === 120) {
  6544. radix = 16;
  6545. ++this.state.pos;
  6546. }
  6547. const codePoint = this.readInt(radix, undefined, false, "bail");
  6548. if (codePoint !== null && this.codePointAtPos(this.state.pos) === 59) {
  6549. ++this.state.pos;
  6550. return String.fromCodePoint(codePoint);
  6551. }
  6552. } else {
  6553. let count = 0;
  6554. let semi = false;
  6555. while (count++ < 10 && this.state.pos < this.length && !(semi = this.codePointAtPos(this.state.pos) === 59)) {
  6556. ++this.state.pos;
  6557. }
  6558. if (semi) {
  6559. const desc = this.input.slice(startPos, this.state.pos);
  6560. const entity = entities[desc];
  6561. ++this.state.pos;
  6562. if (entity) {
  6563. return entity;
  6564. }
  6565. }
  6566. }
  6567. this.state.pos = startPos;
  6568. return "&";
  6569. }
  6570. jsxReadWord() {
  6571. let ch;
  6572. const start = this.state.pos;
  6573. do {
  6574. ch = this.input.charCodeAt(++this.state.pos);
  6575. } while (isIdentifierChar(ch) || ch === 45);
  6576. this.finishToken(140, this.input.slice(start, this.state.pos));
  6577. }
  6578. jsxParseIdentifier() {
  6579. const node = this.startNode();
  6580. if (this.match(140)) {
  6581. node.name = this.state.value;
  6582. } else if (tokenIsKeyword(this.state.type)) {
  6583. node.name = tokenLabelName(this.state.type);
  6584. } else {
  6585. this.unexpected();
  6586. }
  6587. this.next();
  6588. return this.finishNode(node, "JSXIdentifier");
  6589. }
  6590. jsxParseNamespacedName() {
  6591. const startLoc = this.state.startLoc;
  6592. const name = this.jsxParseIdentifier();
  6593. if (!this.eat(14)) return name;
  6594. const node = this.startNodeAt(startLoc);
  6595. node.namespace = name;
  6596. node.name = this.jsxParseIdentifier();
  6597. return this.finishNode(node, "JSXNamespacedName");
  6598. }
  6599. jsxParseElementName() {
  6600. const startLoc = this.state.startLoc;
  6601. let node = this.jsxParseNamespacedName();
  6602. if (node.type === "JSXNamespacedName") {
  6603. return node;
  6604. }
  6605. while (this.eat(16)) {
  6606. const newNode = this.startNodeAt(startLoc);
  6607. newNode.object = node;
  6608. newNode.property = this.jsxParseIdentifier();
  6609. node = this.finishNode(newNode, "JSXMemberExpression");
  6610. }
  6611. return node;
  6612. }
  6613. jsxParseAttributeValue() {
  6614. let node;
  6615. switch (this.state.type) {
  6616. case 5:
  6617. node = this.startNode();
  6618. this.setContext(types.brace);
  6619. this.next();
  6620. node = this.jsxParseExpressionContainer(node, types.j_oTag);
  6621. if (node.expression.type === "JSXEmptyExpression") {
  6622. this.raise(JsxErrors.AttributeIsEmpty, node);
  6623. }
  6624. return node;
  6625. case 142:
  6626. case 133:
  6627. return this.parseExprAtom();
  6628. default:
  6629. throw this.raise(JsxErrors.UnsupportedJsxValue, this.state.startLoc);
  6630. }
  6631. }
  6632. jsxParseEmptyExpression() {
  6633. const node = this.startNodeAt(this.state.lastTokEndLoc);
  6634. return this.finishNodeAt(node, "JSXEmptyExpression", this.state.startLoc);
  6635. }
  6636. jsxParseSpreadChild(node) {
  6637. this.next();
  6638. node.expression = this.parseExpression();
  6639. this.setContext(types.j_expr);
  6640. this.state.canStartJSXElement = true;
  6641. this.expect(8);
  6642. return this.finishNode(node, "JSXSpreadChild");
  6643. }
  6644. jsxParseExpressionContainer(node, previousContext) {
  6645. if (this.match(8)) {
  6646. node.expression = this.jsxParseEmptyExpression();
  6647. } else {
  6648. const expression = this.parseExpression();
  6649. node.expression = expression;
  6650. }
  6651. this.setContext(previousContext);
  6652. this.state.canStartJSXElement = true;
  6653. this.expect(8);
  6654. return this.finishNode(node, "JSXExpressionContainer");
  6655. }
  6656. jsxParseAttribute() {
  6657. const node = this.startNode();
  6658. if (this.match(5)) {
  6659. this.setContext(types.brace);
  6660. this.next();
  6661. this.expect(21);
  6662. node.argument = this.parseMaybeAssignAllowIn();
  6663. this.setContext(types.j_oTag);
  6664. this.state.canStartJSXElement = true;
  6665. this.expect(8);
  6666. return this.finishNode(node, "JSXSpreadAttribute");
  6667. }
  6668. node.name = this.jsxParseNamespacedName();
  6669. node.value = this.eat(29) ? this.jsxParseAttributeValue() : null;
  6670. return this.finishNode(node, "JSXAttribute");
  6671. }
  6672. jsxParseOpeningElementAt(startLoc) {
  6673. const node = this.startNodeAt(startLoc);
  6674. if (this.eat(143)) {
  6675. return this.finishNode(node, "JSXOpeningFragment");
  6676. }
  6677. node.name = this.jsxParseElementName();
  6678. return this.jsxParseOpeningElementAfterName(node);
  6679. }
  6680. jsxParseOpeningElementAfterName(node) {
  6681. const attributes = [];
  6682. while (!this.match(56) && !this.match(143)) {
  6683. attributes.push(this.jsxParseAttribute());
  6684. }
  6685. node.attributes = attributes;
  6686. node.selfClosing = this.eat(56);
  6687. this.expect(143);
  6688. return this.finishNode(node, "JSXOpeningElement");
  6689. }
  6690. jsxParseClosingElementAt(startLoc) {
  6691. const node = this.startNodeAt(startLoc);
  6692. if (this.eat(143)) {
  6693. return this.finishNode(node, "JSXClosingFragment");
  6694. }
  6695. node.name = this.jsxParseElementName();
  6696. this.expect(143);
  6697. return this.finishNode(node, "JSXClosingElement");
  6698. }
  6699. jsxParseElementAt(startLoc) {
  6700. const node = this.startNodeAt(startLoc);
  6701. const children = [];
  6702. const openingElement = this.jsxParseOpeningElementAt(startLoc);
  6703. let closingElement = null;
  6704. if (!openingElement.selfClosing) {
  6705. contents: for (;;) {
  6706. switch (this.state.type) {
  6707. case 142:
  6708. startLoc = this.state.startLoc;
  6709. this.next();
  6710. if (this.eat(56)) {
  6711. closingElement = this.jsxParseClosingElementAt(startLoc);
  6712. break contents;
  6713. }
  6714. children.push(this.jsxParseElementAt(startLoc));
  6715. break;
  6716. case 141:
  6717. children.push(this.parseLiteral(this.state.value, "JSXText"));
  6718. break;
  6719. case 5:
  6720. {
  6721. const node = this.startNode();
  6722. this.setContext(types.brace);
  6723. this.next();
  6724. if (this.match(21)) {
  6725. children.push(this.jsxParseSpreadChild(node));
  6726. } else {
  6727. children.push(this.jsxParseExpressionContainer(node, types.j_expr));
  6728. }
  6729. break;
  6730. }
  6731. default:
  6732. this.unexpected();
  6733. }
  6734. }
  6735. if (isFragment(openingElement) && !isFragment(closingElement) && closingElement !== null) {
  6736. this.raise(JsxErrors.MissingClosingTagFragment, closingElement);
  6737. } else if (!isFragment(openingElement) && isFragment(closingElement)) {
  6738. this.raise(JsxErrors.MissingClosingTagElement, closingElement, {
  6739. openingTagName: getQualifiedJSXName(openingElement.name)
  6740. });
  6741. } else if (!isFragment(openingElement) && !isFragment(closingElement)) {
  6742. if (getQualifiedJSXName(closingElement.name) !== getQualifiedJSXName(openingElement.name)) {
  6743. this.raise(JsxErrors.MissingClosingTagElement, closingElement, {
  6744. openingTagName: getQualifiedJSXName(openingElement.name)
  6745. });
  6746. }
  6747. }
  6748. }
  6749. if (isFragment(openingElement)) {
  6750. node.openingFragment = openingElement;
  6751. node.closingFragment = closingElement;
  6752. } else {
  6753. node.openingElement = openingElement;
  6754. node.closingElement = closingElement;
  6755. }
  6756. node.children = children;
  6757. if (this.match(47)) {
  6758. throw this.raise(JsxErrors.UnwrappedAdjacentJSXElements, this.state.startLoc);
  6759. }
  6760. return isFragment(openingElement) ? this.finishNode(node, "JSXFragment") : this.finishNode(node, "JSXElement");
  6761. }
  6762. jsxParseElement() {
  6763. const startLoc = this.state.startLoc;
  6764. this.next();
  6765. return this.jsxParseElementAt(startLoc);
  6766. }
  6767. setContext(newContext) {
  6768. const {
  6769. context
  6770. } = this.state;
  6771. context[context.length - 1] = newContext;
  6772. }
  6773. parseExprAtom(refExpressionErrors) {
  6774. if (this.match(142)) {
  6775. return this.jsxParseElement();
  6776. } else if (this.match(47) && this.input.charCodeAt(this.state.pos) !== 33) {
  6777. this.replaceToken(142);
  6778. return this.jsxParseElement();
  6779. } else {
  6780. return super.parseExprAtom(refExpressionErrors);
  6781. }
  6782. }
  6783. skipSpace() {
  6784. const curContext = this.curContext();
  6785. if (!curContext.preserveSpace) super.skipSpace();
  6786. }
  6787. getTokenFromCode(code) {
  6788. const context = this.curContext();
  6789. if (context === types.j_expr) {
  6790. this.jsxReadToken();
  6791. return;
  6792. }
  6793. if (context === types.j_oTag || context === types.j_cTag) {
  6794. if (isIdentifierStart(code)) {
  6795. this.jsxReadWord();
  6796. return;
  6797. }
  6798. if (code === 62) {
  6799. ++this.state.pos;
  6800. this.finishToken(143);
  6801. return;
  6802. }
  6803. if ((code === 34 || code === 39) && context === types.j_oTag) {
  6804. this.jsxReadString(code);
  6805. return;
  6806. }
  6807. }
  6808. if (code === 60 && this.state.canStartJSXElement && this.input.charCodeAt(this.state.pos + 1) !== 33) {
  6809. ++this.state.pos;
  6810. this.finishToken(142);
  6811. return;
  6812. }
  6813. super.getTokenFromCode(code);
  6814. }
  6815. updateContext(prevType) {
  6816. const {
  6817. context,
  6818. type
  6819. } = this.state;
  6820. if (type === 56 && prevType === 142) {
  6821. context.splice(-2, 2, types.j_cTag);
  6822. this.state.canStartJSXElement = false;
  6823. } else if (type === 142) {
  6824. context.push(types.j_oTag);
  6825. } else if (type === 143) {
  6826. const out = context[context.length - 1];
  6827. if (out === types.j_oTag && prevType === 56 || out === types.j_cTag) {
  6828. context.pop();
  6829. this.state.canStartJSXElement = context[context.length - 1] === types.j_expr;
  6830. } else {
  6831. this.setContext(types.j_expr);
  6832. this.state.canStartJSXElement = true;
  6833. }
  6834. } else {
  6835. this.state.canStartJSXElement = tokenComesBeforeExpression(type);
  6836. }
  6837. }
  6838. };
  6839. class TypeScriptScope extends Scope {
  6840. constructor(...args) {
  6841. super(...args);
  6842. this.tsNames = new Map();
  6843. }
  6844. }
  6845. class TypeScriptScopeHandler extends ScopeHandler {
  6846. constructor(...args) {
  6847. super(...args);
  6848. this.importsStack = [];
  6849. }
  6850. createScope(flags) {
  6851. this.importsStack.push(new Set());
  6852. return new TypeScriptScope(flags);
  6853. }
  6854. enter(flags) {
  6855. if (flags === 256) {
  6856. this.importsStack.push(new Set());
  6857. }
  6858. super.enter(flags);
  6859. }
  6860. exit() {
  6861. const flags = super.exit();
  6862. if (flags === 256) {
  6863. this.importsStack.pop();
  6864. }
  6865. return flags;
  6866. }
  6867. hasImport(name, allowShadow) {
  6868. const len = this.importsStack.length;
  6869. if (this.importsStack[len - 1].has(name)) {
  6870. return true;
  6871. }
  6872. if (!allowShadow && len > 1) {
  6873. for (let i = 0; i < len - 1; i++) {
  6874. if (this.importsStack[i].has(name)) return true;
  6875. }
  6876. }
  6877. return false;
  6878. }
  6879. declareName(name, bindingType, loc) {
  6880. if (bindingType & 4096) {
  6881. if (this.hasImport(name, true)) {
  6882. this.parser.raise(Errors.VarRedeclaration, loc, {
  6883. identifierName: name
  6884. });
  6885. }
  6886. this.importsStack[this.importsStack.length - 1].add(name);
  6887. return;
  6888. }
  6889. const scope = this.currentScope();
  6890. let type = scope.tsNames.get(name) || 0;
  6891. if (bindingType & 1024) {
  6892. this.maybeExportDefined(scope, name);
  6893. scope.tsNames.set(name, type | 16);
  6894. return;
  6895. }
  6896. super.declareName(name, bindingType, loc);
  6897. if (bindingType & 2) {
  6898. if (!(bindingType & 1)) {
  6899. this.checkRedeclarationInScope(scope, name, bindingType, loc);
  6900. this.maybeExportDefined(scope, name);
  6901. }
  6902. type = type | 1;
  6903. }
  6904. if (bindingType & 256) {
  6905. type = type | 2;
  6906. }
  6907. if (bindingType & 512) {
  6908. type = type | 4;
  6909. }
  6910. if (bindingType & 128) {
  6911. type = type | 8;
  6912. }
  6913. if (type) scope.tsNames.set(name, type);
  6914. }
  6915. isRedeclaredInScope(scope, name, bindingType) {
  6916. const type = scope.tsNames.get(name);
  6917. if ((type & 2) > 0) {
  6918. if (bindingType & 256) {
  6919. const isConst = !!(bindingType & 512);
  6920. const wasConst = (type & 4) > 0;
  6921. return isConst !== wasConst;
  6922. }
  6923. return true;
  6924. }
  6925. if (bindingType & 128 && (type & 8) > 0) {
  6926. if (scope.names.get(name) & 2) {
  6927. return !!(bindingType & 1);
  6928. } else {
  6929. return false;
  6930. }
  6931. }
  6932. if (bindingType & 2 && (type & 1) > 0) {
  6933. return true;
  6934. }
  6935. return super.isRedeclaredInScope(scope, name, bindingType);
  6936. }
  6937. checkLocalExport(id) {
  6938. const {
  6939. name
  6940. } = id;
  6941. if (this.hasImport(name)) return;
  6942. const len = this.scopeStack.length;
  6943. for (let i = len - 1; i >= 0; i--) {
  6944. const scope = this.scopeStack[i];
  6945. const type = scope.tsNames.get(name);
  6946. if ((type & 1) > 0 || (type & 16) > 0) {
  6947. return;
  6948. }
  6949. }
  6950. super.checkLocalExport(id);
  6951. }
  6952. }
  6953. const getOwn$1 = (object, key) => hasOwnProperty.call(object, key) && object[key];
  6954. const unwrapParenthesizedExpression = node => {
  6955. return node.type === "ParenthesizedExpression" ? unwrapParenthesizedExpression(node.expression) : node;
  6956. };
  6957. class LValParser extends NodeUtils {
  6958. toAssignable(node, isLHS = false) {
  6959. var _node$extra, _node$extra3;
  6960. let parenthesized = undefined;
  6961. if (node.type === "ParenthesizedExpression" || (_node$extra = node.extra) != null && _node$extra.parenthesized) {
  6962. parenthesized = unwrapParenthesizedExpression(node);
  6963. if (isLHS) {
  6964. if (parenthesized.type === "Identifier") {
  6965. this.expressionScope.recordArrowParameterBindingError(Errors.InvalidParenthesizedAssignment, node);
  6966. } else if (parenthesized.type !== "MemberExpression" && !this.isOptionalMemberExpression(parenthesized)) {
  6967. this.raise(Errors.InvalidParenthesizedAssignment, node);
  6968. }
  6969. } else {
  6970. this.raise(Errors.InvalidParenthesizedAssignment, node);
  6971. }
  6972. }
  6973. switch (node.type) {
  6974. case "Identifier":
  6975. case "ObjectPattern":
  6976. case "ArrayPattern":
  6977. case "AssignmentPattern":
  6978. case "RestElement":
  6979. break;
  6980. case "ObjectExpression":
  6981. node.type = "ObjectPattern";
  6982. for (let i = 0, length = node.properties.length, last = length - 1; i < length; i++) {
  6983. var _node$extra2;
  6984. const prop = node.properties[i];
  6985. const isLast = i === last;
  6986. this.toAssignableObjectExpressionProp(prop, isLast, isLHS);
  6987. if (isLast && prop.type === "RestElement" && (_node$extra2 = node.extra) != null && _node$extra2.trailingCommaLoc) {
  6988. this.raise(Errors.RestTrailingComma, node.extra.trailingCommaLoc);
  6989. }
  6990. }
  6991. break;
  6992. case "ObjectProperty":
  6993. {
  6994. const {
  6995. key,
  6996. value
  6997. } = node;
  6998. if (this.isPrivateName(key)) {
  6999. this.classScope.usePrivateName(this.getPrivateNameSV(key), key.loc.start);
  7000. }
  7001. this.toAssignable(value, isLHS);
  7002. break;
  7003. }
  7004. case "SpreadElement":
  7005. {
  7006. throw new Error("Internal @babel/parser error (this is a bug, please report it)." + " SpreadElement should be converted by .toAssignable's caller.");
  7007. }
  7008. case "ArrayExpression":
  7009. node.type = "ArrayPattern";
  7010. this.toAssignableList(node.elements, (_node$extra3 = node.extra) == null ? void 0 : _node$extra3.trailingCommaLoc, isLHS);
  7011. break;
  7012. case "AssignmentExpression":
  7013. if (node.operator !== "=") {
  7014. this.raise(Errors.MissingEqInAssignment, node.left.loc.end);
  7015. }
  7016. node.type = "AssignmentPattern";
  7017. delete node.operator;
  7018. this.toAssignable(node.left, isLHS);
  7019. break;
  7020. case "ParenthesizedExpression":
  7021. this.toAssignable(parenthesized, isLHS);
  7022. break;
  7023. }
  7024. }
  7025. toAssignableObjectExpressionProp(prop, isLast, isLHS) {
  7026. if (prop.type === "ObjectMethod") {
  7027. this.raise(prop.kind === "get" || prop.kind === "set" ? Errors.PatternHasAccessor : Errors.PatternHasMethod, prop.key);
  7028. } else if (prop.type === "SpreadElement") {
  7029. prop.type = "RestElement";
  7030. const arg = prop.argument;
  7031. this.checkToRestConversion(arg, false);
  7032. this.toAssignable(arg, isLHS);
  7033. if (!isLast) {
  7034. this.raise(Errors.RestTrailingComma, prop);
  7035. }
  7036. } else {
  7037. this.toAssignable(prop, isLHS);
  7038. }
  7039. }
  7040. toAssignableList(exprList, trailingCommaLoc, isLHS) {
  7041. const end = exprList.length - 1;
  7042. for (let i = 0; i <= end; i++) {
  7043. const elt = exprList[i];
  7044. if (!elt) continue;
  7045. if (elt.type === "SpreadElement") {
  7046. elt.type = "RestElement";
  7047. const arg = elt.argument;
  7048. this.checkToRestConversion(arg, true);
  7049. this.toAssignable(arg, isLHS);
  7050. } else {
  7051. this.toAssignable(elt, isLHS);
  7052. }
  7053. if (elt.type === "RestElement") {
  7054. if (i < end) {
  7055. this.raise(Errors.RestTrailingComma, elt);
  7056. } else if (trailingCommaLoc) {
  7057. this.raise(Errors.RestTrailingComma, trailingCommaLoc);
  7058. }
  7059. }
  7060. }
  7061. }
  7062. isAssignable(node, isBinding) {
  7063. switch (node.type) {
  7064. case "Identifier":
  7065. case "ObjectPattern":
  7066. case "ArrayPattern":
  7067. case "AssignmentPattern":
  7068. case "RestElement":
  7069. return true;
  7070. case "ObjectExpression":
  7071. {
  7072. const last = node.properties.length - 1;
  7073. return node.properties.every((prop, i) => {
  7074. return prop.type !== "ObjectMethod" && (i === last || prop.type !== "SpreadElement") && this.isAssignable(prop);
  7075. });
  7076. }
  7077. case "ObjectProperty":
  7078. return this.isAssignable(node.value);
  7079. case "SpreadElement":
  7080. return this.isAssignable(node.argument);
  7081. case "ArrayExpression":
  7082. return node.elements.every(element => element === null || this.isAssignable(element));
  7083. case "AssignmentExpression":
  7084. return node.operator === "=";
  7085. case "ParenthesizedExpression":
  7086. return this.isAssignable(node.expression);
  7087. case "MemberExpression":
  7088. case "OptionalMemberExpression":
  7089. return !isBinding;
  7090. default:
  7091. return false;
  7092. }
  7093. }
  7094. toReferencedList(exprList, isParenthesizedExpr) {
  7095. return exprList;
  7096. }
  7097. toReferencedListDeep(exprList, isParenthesizedExpr) {
  7098. this.toReferencedList(exprList, isParenthesizedExpr);
  7099. for (const expr of exprList) {
  7100. if ((expr == null ? void 0 : expr.type) === "ArrayExpression") {
  7101. this.toReferencedListDeep(expr.elements);
  7102. }
  7103. }
  7104. }
  7105. parseSpread(refExpressionErrors) {
  7106. const node = this.startNode();
  7107. this.next();
  7108. node.argument = this.parseMaybeAssignAllowIn(refExpressionErrors, undefined);
  7109. return this.finishNode(node, "SpreadElement");
  7110. }
  7111. parseRestBinding() {
  7112. const node = this.startNode();
  7113. this.next();
  7114. node.argument = this.parseBindingAtom();
  7115. return this.finishNode(node, "RestElement");
  7116. }
  7117. parseBindingAtom() {
  7118. switch (this.state.type) {
  7119. case 0:
  7120. {
  7121. const node = this.startNode();
  7122. this.next();
  7123. node.elements = this.parseBindingList(3, 93, 1);
  7124. return this.finishNode(node, "ArrayPattern");
  7125. }
  7126. case 5:
  7127. return this.parseObjectLike(8, true);
  7128. }
  7129. return this.parseIdentifier();
  7130. }
  7131. parseBindingList(close, closeCharCode, flags) {
  7132. const allowEmpty = flags & 1;
  7133. const elts = [];
  7134. let first = true;
  7135. while (!this.eat(close)) {
  7136. if (first) {
  7137. first = false;
  7138. } else {
  7139. this.expect(12);
  7140. }
  7141. if (allowEmpty && this.match(12)) {
  7142. elts.push(null);
  7143. } else if (this.eat(close)) {
  7144. break;
  7145. } else if (this.match(21)) {
  7146. elts.push(this.parseAssignableListItemTypes(this.parseRestBinding(), flags));
  7147. if (!this.checkCommaAfterRest(closeCharCode)) {
  7148. this.expect(close);
  7149. break;
  7150. }
  7151. } else {
  7152. const decorators = [];
  7153. if (this.match(26) && this.hasPlugin("decorators")) {
  7154. this.raise(Errors.UnsupportedParameterDecorator, this.state.startLoc);
  7155. }
  7156. while (this.match(26)) {
  7157. decorators.push(this.parseDecorator());
  7158. }
  7159. elts.push(this.parseAssignableListItem(flags, decorators));
  7160. }
  7161. }
  7162. return elts;
  7163. }
  7164. parseBindingRestProperty(prop) {
  7165. this.next();
  7166. prop.argument = this.parseIdentifier();
  7167. this.checkCommaAfterRest(125);
  7168. return this.finishNode(prop, "RestElement");
  7169. }
  7170. parseBindingProperty() {
  7171. const {
  7172. type,
  7173. startLoc
  7174. } = this.state;
  7175. if (type === 21) {
  7176. return this.parseBindingRestProperty(this.startNode());
  7177. }
  7178. const prop = this.startNode();
  7179. if (type === 138) {
  7180. this.expectPlugin("destructuringPrivate", startLoc);
  7181. this.classScope.usePrivateName(this.state.value, startLoc);
  7182. prop.key = this.parsePrivateName();
  7183. } else {
  7184. this.parsePropertyName(prop);
  7185. }
  7186. prop.method = false;
  7187. return this.parseObjPropValue(prop, startLoc, false, false, true, false);
  7188. }
  7189. parseAssignableListItem(flags, decorators) {
  7190. const left = this.parseMaybeDefault();
  7191. this.parseAssignableListItemTypes(left, flags);
  7192. const elt = this.parseMaybeDefault(left.loc.start, left);
  7193. if (decorators.length) {
  7194. left.decorators = decorators;
  7195. }
  7196. return elt;
  7197. }
  7198. parseAssignableListItemTypes(param, flags) {
  7199. return param;
  7200. }
  7201. parseMaybeDefault(startLoc, left) {
  7202. var _startLoc, _left;
  7203. (_startLoc = startLoc) != null ? _startLoc : startLoc = this.state.startLoc;
  7204. left = (_left = left) != null ? _left : this.parseBindingAtom();
  7205. if (!this.eat(29)) return left;
  7206. const node = this.startNodeAt(startLoc);
  7207. node.left = left;
  7208. node.right = this.parseMaybeAssignAllowIn();
  7209. return this.finishNode(node, "AssignmentPattern");
  7210. }
  7211. isValidLVal(type, isUnparenthesizedInAssign, binding) {
  7212. return getOwn$1({
  7213. AssignmentPattern: "left",
  7214. RestElement: "argument",
  7215. ObjectProperty: "value",
  7216. ParenthesizedExpression: "expression",
  7217. ArrayPattern: "elements",
  7218. ObjectPattern: "properties"
  7219. }, type);
  7220. }
  7221. isOptionalMemberExpression(expression) {
  7222. return expression.type === "OptionalMemberExpression";
  7223. }
  7224. checkLVal(expression, {
  7225. in: ancestor,
  7226. binding = 64,
  7227. checkClashes = false,
  7228. strictModeChanged = false,
  7229. hasParenthesizedAncestor = false
  7230. }) {
  7231. var _expression$extra;
  7232. const type = expression.type;
  7233. if (this.isObjectMethod(expression)) return;
  7234. const isOptionalMemberExpression = this.isOptionalMemberExpression(expression);
  7235. if (isOptionalMemberExpression || type === "MemberExpression") {
  7236. if (isOptionalMemberExpression) {
  7237. this.expectPlugin("optionalChainingAssign", expression.loc.start);
  7238. if (ancestor.type !== "AssignmentExpression") {
  7239. this.raise(Errors.InvalidLhsOptionalChaining, expression, {
  7240. ancestor
  7241. });
  7242. }
  7243. }
  7244. if (binding !== 64) {
  7245. this.raise(Errors.InvalidPropertyBindingPattern, expression);
  7246. }
  7247. return;
  7248. }
  7249. if (type === "Identifier") {
  7250. this.checkIdentifier(expression, binding, strictModeChanged);
  7251. const {
  7252. name
  7253. } = expression;
  7254. if (checkClashes) {
  7255. if (checkClashes.has(name)) {
  7256. this.raise(Errors.ParamDupe, expression);
  7257. } else {
  7258. checkClashes.add(name);
  7259. }
  7260. }
  7261. return;
  7262. }
  7263. const validity = this.isValidLVal(type, !(hasParenthesizedAncestor || (_expression$extra = expression.extra) != null && _expression$extra.parenthesized) && ancestor.type === "AssignmentExpression", binding);
  7264. if (validity === true) return;
  7265. if (validity === false) {
  7266. const ParseErrorClass = binding === 64 ? Errors.InvalidLhs : Errors.InvalidLhsBinding;
  7267. this.raise(ParseErrorClass, expression, {
  7268. ancestor
  7269. });
  7270. return;
  7271. }
  7272. const [key, isParenthesizedExpression] = Array.isArray(validity) ? validity : [validity, type === "ParenthesizedExpression"];
  7273. const nextAncestor = type === "ArrayPattern" || type === "ObjectPattern" ? {
  7274. type
  7275. } : ancestor;
  7276. for (const child of [].concat(expression[key])) {
  7277. if (child) {
  7278. this.checkLVal(child, {
  7279. in: nextAncestor,
  7280. binding,
  7281. checkClashes,
  7282. strictModeChanged,
  7283. hasParenthesizedAncestor: isParenthesizedExpression
  7284. });
  7285. }
  7286. }
  7287. }
  7288. checkIdentifier(at, bindingType, strictModeChanged = false) {
  7289. if (this.state.strict && (strictModeChanged ? isStrictBindReservedWord(at.name, this.inModule) : isStrictBindOnlyReservedWord(at.name))) {
  7290. if (bindingType === 64) {
  7291. this.raise(Errors.StrictEvalArguments, at, {
  7292. referenceName: at.name
  7293. });
  7294. } else {
  7295. this.raise(Errors.StrictEvalArgumentsBinding, at, {
  7296. bindingName: at.name
  7297. });
  7298. }
  7299. }
  7300. if (bindingType & 8192 && at.name === "let") {
  7301. this.raise(Errors.LetInLexicalBinding, at);
  7302. }
  7303. if (!(bindingType & 64)) {
  7304. this.declareNameFromIdentifier(at, bindingType);
  7305. }
  7306. }
  7307. declareNameFromIdentifier(identifier, binding) {
  7308. this.scope.declareName(identifier.name, binding, identifier.loc.start);
  7309. }
  7310. checkToRestConversion(node, allowPattern) {
  7311. switch (node.type) {
  7312. case "ParenthesizedExpression":
  7313. this.checkToRestConversion(node.expression, allowPattern);
  7314. break;
  7315. case "Identifier":
  7316. case "MemberExpression":
  7317. break;
  7318. case "ArrayExpression":
  7319. case "ObjectExpression":
  7320. if (allowPattern) break;
  7321. default:
  7322. this.raise(Errors.InvalidRestAssignmentPattern, node);
  7323. }
  7324. }
  7325. checkCommaAfterRest(close) {
  7326. if (!this.match(12)) {
  7327. return false;
  7328. }
  7329. this.raise(this.lookaheadCharCode() === close ? Errors.RestTrailingComma : Errors.ElementAfterRest, this.state.startLoc);
  7330. return true;
  7331. }
  7332. }
  7333. const getOwn = (object, key) => hasOwnProperty.call(object, key) && object[key];
  7334. function nonNull(x) {
  7335. if (x == null) {
  7336. throw new Error(`Unexpected ${x} value.`);
  7337. }
  7338. return x;
  7339. }
  7340. function assert(x) {
  7341. if (!x) {
  7342. throw new Error("Assert fail");
  7343. }
  7344. }
  7345. const TSErrors = ParseErrorEnum`typescript`({
  7346. AbstractMethodHasImplementation: ({
  7347. methodName
  7348. }) => `Method '${methodName}' cannot have an implementation because it is marked abstract.`,
  7349. AbstractPropertyHasInitializer: ({
  7350. propertyName
  7351. }) => `Property '${propertyName}' cannot have an initializer because it is marked abstract.`,
  7352. AccesorCannotDeclareThisParameter: "'get' and 'set' accessors cannot declare 'this' parameters.",
  7353. AccesorCannotHaveTypeParameters: "An accessor cannot have type parameters.",
  7354. AccessorCannotBeOptional: "An 'accessor' property cannot be declared optional.",
  7355. ClassMethodHasDeclare: "Class methods cannot have the 'declare' modifier.",
  7356. ClassMethodHasReadonly: "Class methods cannot have the 'readonly' modifier.",
  7357. ConstInitiailizerMustBeStringOrNumericLiteralOrLiteralEnumReference: "A 'const' initializer in an ambient context must be a string or numeric literal or literal enum reference.",
  7358. ConstructorHasTypeParameters: "Type parameters cannot appear on a constructor declaration.",
  7359. DeclareAccessor: ({
  7360. kind
  7361. }) => `'declare' is not allowed in ${kind}ters.`,
  7362. DeclareClassFieldHasInitializer: "Initializers are not allowed in ambient contexts.",
  7363. DeclareFunctionHasImplementation: "An implementation cannot be declared in ambient contexts.",
  7364. DuplicateAccessibilityModifier: ({
  7365. modifier
  7366. }) => `Accessibility modifier already seen.`,
  7367. DuplicateModifier: ({
  7368. modifier
  7369. }) => `Duplicate modifier: '${modifier}'.`,
  7370. EmptyHeritageClauseType: ({
  7371. token
  7372. }) => `'${token}' list cannot be empty.`,
  7373. EmptyTypeArguments: "Type argument list cannot be empty.",
  7374. EmptyTypeParameters: "Type parameter list cannot be empty.",
  7375. ExpectedAmbientAfterExportDeclare: "'export declare' must be followed by an ambient declaration.",
  7376. ImportAliasHasImportType: "An import alias can not use 'import type'.",
  7377. ImportReflectionHasImportType: "An `import module` declaration can not use `type` modifier",
  7378. IncompatibleModifiers: ({
  7379. modifiers
  7380. }) => `'${modifiers[0]}' modifier cannot be used with '${modifiers[1]}' modifier.`,
  7381. IndexSignatureHasAbstract: "Index signatures cannot have the 'abstract' modifier.",
  7382. IndexSignatureHasAccessibility: ({
  7383. modifier
  7384. }) => `Index signatures cannot have an accessibility modifier ('${modifier}').`,
  7385. IndexSignatureHasDeclare: "Index signatures cannot have the 'declare' modifier.",
  7386. IndexSignatureHasOverride: "'override' modifier cannot appear on an index signature.",
  7387. IndexSignatureHasStatic: "Index signatures cannot have the 'static' modifier.",
  7388. InitializerNotAllowedInAmbientContext: "Initializers are not allowed in ambient contexts.",
  7389. InvalidModifierOnTypeMember: ({
  7390. modifier
  7391. }) => `'${modifier}' modifier cannot appear on a type member.`,
  7392. InvalidModifierOnTypeParameter: ({
  7393. modifier
  7394. }) => `'${modifier}' modifier cannot appear on a type parameter.`,
  7395. InvalidModifierOnTypeParameterPositions: ({
  7396. modifier
  7397. }) => `'${modifier}' modifier can only appear on a type parameter of a class, interface or type alias.`,
  7398. InvalidModifiersOrder: ({
  7399. orderedModifiers
  7400. }) => `'${orderedModifiers[0]}' modifier must precede '${orderedModifiers[1]}' modifier.`,
  7401. InvalidPropertyAccessAfterInstantiationExpression: "Invalid property access after an instantiation expression. " + "You can either wrap the instantiation expression in parentheses, or delete the type arguments.",
  7402. InvalidTupleMemberLabel: "Tuple members must be labeled with a simple identifier.",
  7403. MissingInterfaceName: "'interface' declarations must be followed by an identifier.",
  7404. NonAbstractClassHasAbstractMethod: "Abstract methods can only appear within an abstract class.",
  7405. NonClassMethodPropertyHasAbstractModifer: "'abstract' modifier can only appear on a class, method, or property declaration.",
  7406. OptionalTypeBeforeRequired: "A required element cannot follow an optional element.",
  7407. OverrideNotInSubClass: "This member cannot have an 'override' modifier because its containing class does not extend another class.",
  7408. PatternIsOptional: "A binding pattern parameter cannot be optional in an implementation signature.",
  7409. PrivateElementHasAbstract: "Private elements cannot have the 'abstract' modifier.",
  7410. PrivateElementHasAccessibility: ({
  7411. modifier
  7412. }) => `Private elements cannot have an accessibility modifier ('${modifier}').`,
  7413. ReadonlyForMethodSignature: "'readonly' modifier can only appear on a property declaration or index signature.",
  7414. ReservedArrowTypeParam: "This syntax is reserved in files with the .mts or .cts extension. Add a trailing comma, as in `<T,>() => ...`.",
  7415. ReservedTypeAssertion: "This syntax is reserved in files with the .mts or .cts extension. Use an `as` expression instead.",
  7416. SetAccesorCannotHaveOptionalParameter: "A 'set' accessor cannot have an optional parameter.",
  7417. SetAccesorCannotHaveRestParameter: "A 'set' accessor cannot have rest parameter.",
  7418. SetAccesorCannotHaveReturnType: "A 'set' accessor cannot have a return type annotation.",
  7419. SingleTypeParameterWithoutTrailingComma: ({
  7420. typeParameterName
  7421. }) => `Single type parameter ${typeParameterName} should have a trailing comma. Example usage: <${typeParameterName},>.`,
  7422. StaticBlockCannotHaveModifier: "Static class blocks cannot have any modifier.",
  7423. TupleOptionalAfterType: "A labeled tuple optional element must be declared using a question mark after the name and before the colon (`name?: type`), rather than after the type (`name: type?`).",
  7424. TypeAnnotationAfterAssign: "Type annotations must come before default assignments, e.g. instead of `age = 25: number` use `age: number = 25`.",
  7425. TypeImportCannotSpecifyDefaultAndNamed: "A type-only import can specify a default import or named bindings, but not both.",
  7426. TypeModifierIsUsedInTypeExports: "The 'type' modifier cannot be used on a named export when 'export type' is used on its export statement.",
  7427. TypeModifierIsUsedInTypeImports: "The 'type' modifier cannot be used on a named import when 'import type' is used on its import statement.",
  7428. UnexpectedParameterModifier: "A parameter property is only allowed in a constructor implementation.",
  7429. UnexpectedReadonly: "'readonly' type modifier is only permitted on array and tuple literal types.",
  7430. UnexpectedTypeAnnotation: "Did not expect a type annotation here.",
  7431. UnexpectedTypeCastInParameter: "Unexpected type cast in parameter position.",
  7432. UnsupportedImportTypeArgument: "Argument in a type import must be a string literal.",
  7433. UnsupportedParameterPropertyKind: "A parameter property may not be declared using a binding pattern.",
  7434. UnsupportedSignatureParameterKind: ({
  7435. type
  7436. }) => `Name in a signature must be an Identifier, ObjectPattern or ArrayPattern, instead got ${type}.`
  7437. });
  7438. function keywordTypeFromName(value) {
  7439. switch (value) {
  7440. case "any":
  7441. return "TSAnyKeyword";
  7442. case "boolean":
  7443. return "TSBooleanKeyword";
  7444. case "bigint":
  7445. return "TSBigIntKeyword";
  7446. case "never":
  7447. return "TSNeverKeyword";
  7448. case "number":
  7449. return "TSNumberKeyword";
  7450. case "object":
  7451. return "TSObjectKeyword";
  7452. case "string":
  7453. return "TSStringKeyword";
  7454. case "symbol":
  7455. return "TSSymbolKeyword";
  7456. case "undefined":
  7457. return "TSUndefinedKeyword";
  7458. case "unknown":
  7459. return "TSUnknownKeyword";
  7460. default:
  7461. return undefined;
  7462. }
  7463. }
  7464. function tsIsAccessModifier(modifier) {
  7465. return modifier === "private" || modifier === "public" || modifier === "protected";
  7466. }
  7467. function tsIsVarianceAnnotations(modifier) {
  7468. return modifier === "in" || modifier === "out";
  7469. }
  7470. var typescript = superClass => class TypeScriptParserMixin extends superClass {
  7471. constructor(...args) {
  7472. super(...args);
  7473. this.tsParseInOutModifiers = this.tsParseModifiers.bind(this, {
  7474. allowedModifiers: ["in", "out"],
  7475. disallowedModifiers: ["const", "public", "private", "protected", "readonly", "declare", "abstract", "override"],
  7476. errorTemplate: TSErrors.InvalidModifierOnTypeParameter
  7477. });
  7478. this.tsParseConstModifier = this.tsParseModifiers.bind(this, {
  7479. allowedModifiers: ["const"],
  7480. disallowedModifiers: ["in", "out"],
  7481. errorTemplate: TSErrors.InvalidModifierOnTypeParameterPositions
  7482. });
  7483. this.tsParseInOutConstModifiers = this.tsParseModifiers.bind(this, {
  7484. allowedModifiers: ["in", "out", "const"],
  7485. disallowedModifiers: ["public", "private", "protected", "readonly", "declare", "abstract", "override"],
  7486. errorTemplate: TSErrors.InvalidModifierOnTypeParameter
  7487. });
  7488. }
  7489. getScopeHandler() {
  7490. return TypeScriptScopeHandler;
  7491. }
  7492. tsIsIdentifier() {
  7493. return tokenIsIdentifier(this.state.type);
  7494. }
  7495. tsTokenCanFollowModifier() {
  7496. return (this.match(0) || this.match(5) || this.match(55) || this.match(21) || this.match(138) || this.isLiteralPropertyName()) && !this.hasPrecedingLineBreak();
  7497. }
  7498. tsNextTokenCanFollowModifier() {
  7499. this.next();
  7500. return this.tsTokenCanFollowModifier();
  7501. }
  7502. tsParseModifier(allowedModifiers, stopOnStartOfClassStaticBlock) {
  7503. if (!tokenIsIdentifier(this.state.type) && this.state.type !== 58 && this.state.type !== 75) {
  7504. return undefined;
  7505. }
  7506. const modifier = this.state.value;
  7507. if (allowedModifiers.indexOf(modifier) !== -1) {
  7508. if (stopOnStartOfClassStaticBlock && this.tsIsStartOfStaticBlocks()) {
  7509. return undefined;
  7510. }
  7511. if (this.tsTryParse(this.tsNextTokenCanFollowModifier.bind(this))) {
  7512. return modifier;
  7513. }
  7514. }
  7515. return undefined;
  7516. }
  7517. tsParseModifiers({
  7518. allowedModifiers,
  7519. disallowedModifiers,
  7520. stopOnStartOfClassStaticBlock,
  7521. errorTemplate = TSErrors.InvalidModifierOnTypeMember
  7522. }, modified) {
  7523. const enforceOrder = (loc, modifier, before, after) => {
  7524. if (modifier === before && modified[after]) {
  7525. this.raise(TSErrors.InvalidModifiersOrder, loc, {
  7526. orderedModifiers: [before, after]
  7527. });
  7528. }
  7529. };
  7530. const incompatible = (loc, modifier, mod1, mod2) => {
  7531. if (modified[mod1] && modifier === mod2 || modified[mod2] && modifier === mod1) {
  7532. this.raise(TSErrors.IncompatibleModifiers, loc, {
  7533. modifiers: [mod1, mod2]
  7534. });
  7535. }
  7536. };
  7537. for (;;) {
  7538. const {
  7539. startLoc
  7540. } = this.state;
  7541. const modifier = this.tsParseModifier(allowedModifiers.concat(disallowedModifiers != null ? disallowedModifiers : []), stopOnStartOfClassStaticBlock);
  7542. if (!modifier) break;
  7543. if (tsIsAccessModifier(modifier)) {
  7544. if (modified.accessibility) {
  7545. this.raise(TSErrors.DuplicateAccessibilityModifier, startLoc, {
  7546. modifier
  7547. });
  7548. } else {
  7549. enforceOrder(startLoc, modifier, modifier, "override");
  7550. enforceOrder(startLoc, modifier, modifier, "static");
  7551. enforceOrder(startLoc, modifier, modifier, "readonly");
  7552. modified.accessibility = modifier;
  7553. }
  7554. } else if (tsIsVarianceAnnotations(modifier)) {
  7555. if (modified[modifier]) {
  7556. this.raise(TSErrors.DuplicateModifier, startLoc, {
  7557. modifier
  7558. });
  7559. }
  7560. modified[modifier] = true;
  7561. enforceOrder(startLoc, modifier, "in", "out");
  7562. } else {
  7563. if (hasOwnProperty.call(modified, modifier)) {
  7564. this.raise(TSErrors.DuplicateModifier, startLoc, {
  7565. modifier
  7566. });
  7567. } else {
  7568. enforceOrder(startLoc, modifier, "static", "readonly");
  7569. enforceOrder(startLoc, modifier, "static", "override");
  7570. enforceOrder(startLoc, modifier, "override", "readonly");
  7571. enforceOrder(startLoc, modifier, "abstract", "override");
  7572. incompatible(startLoc, modifier, "declare", "override");
  7573. incompatible(startLoc, modifier, "static", "abstract");
  7574. }
  7575. modified[modifier] = true;
  7576. }
  7577. if (disallowedModifiers != null && disallowedModifiers.includes(modifier)) {
  7578. this.raise(errorTemplate, startLoc, {
  7579. modifier
  7580. });
  7581. }
  7582. }
  7583. }
  7584. tsIsListTerminator(kind) {
  7585. switch (kind) {
  7586. case "EnumMembers":
  7587. case "TypeMembers":
  7588. return this.match(8);
  7589. case "HeritageClauseElement":
  7590. return this.match(5);
  7591. case "TupleElementTypes":
  7592. return this.match(3);
  7593. case "TypeParametersOrArguments":
  7594. return this.match(48);
  7595. }
  7596. }
  7597. tsParseList(kind, parseElement) {
  7598. const result = [];
  7599. while (!this.tsIsListTerminator(kind)) {
  7600. result.push(parseElement());
  7601. }
  7602. return result;
  7603. }
  7604. tsParseDelimitedList(kind, parseElement, refTrailingCommaPos) {
  7605. return nonNull(this.tsParseDelimitedListWorker(kind, parseElement, true, refTrailingCommaPos));
  7606. }
  7607. tsParseDelimitedListWorker(kind, parseElement, expectSuccess, refTrailingCommaPos) {
  7608. const result = [];
  7609. let trailingCommaPos = -1;
  7610. for (;;) {
  7611. if (this.tsIsListTerminator(kind)) {
  7612. break;
  7613. }
  7614. trailingCommaPos = -1;
  7615. const element = parseElement();
  7616. if (element == null) {
  7617. return undefined;
  7618. }
  7619. result.push(element);
  7620. if (this.eat(12)) {
  7621. trailingCommaPos = this.state.lastTokStartLoc.index;
  7622. continue;
  7623. }
  7624. if (this.tsIsListTerminator(kind)) {
  7625. break;
  7626. }
  7627. if (expectSuccess) {
  7628. this.expect(12);
  7629. }
  7630. return undefined;
  7631. }
  7632. if (refTrailingCommaPos) {
  7633. refTrailingCommaPos.value = trailingCommaPos;
  7634. }
  7635. return result;
  7636. }
  7637. tsParseBracketedList(kind, parseElement, bracket, skipFirstToken, refTrailingCommaPos) {
  7638. if (!skipFirstToken) {
  7639. if (bracket) {
  7640. this.expect(0);
  7641. } else {
  7642. this.expect(47);
  7643. }
  7644. }
  7645. const result = this.tsParseDelimitedList(kind, parseElement, refTrailingCommaPos);
  7646. if (bracket) {
  7647. this.expect(3);
  7648. } else {
  7649. this.expect(48);
  7650. }
  7651. return result;
  7652. }
  7653. tsParseImportType() {
  7654. const node = this.startNode();
  7655. this.expect(83);
  7656. this.expect(10);
  7657. if (!this.match(133)) {
  7658. this.raise(TSErrors.UnsupportedImportTypeArgument, this.state.startLoc);
  7659. }
  7660. node.argument = super.parseExprAtom();
  7661. if (this.hasPlugin("importAttributes") || this.hasPlugin("importAssertions")) {
  7662. node.options = null;
  7663. }
  7664. if (this.eat(12)) {
  7665. this.expectImportAttributesPlugin();
  7666. if (!this.match(11)) {
  7667. node.options = super.parseMaybeAssignAllowIn();
  7668. this.eat(12);
  7669. }
  7670. }
  7671. this.expect(11);
  7672. if (this.eat(16)) {
  7673. node.qualifier = this.tsParseEntityName();
  7674. }
  7675. if (this.match(47)) {
  7676. node.typeParameters = this.tsParseTypeArguments();
  7677. }
  7678. return this.finishNode(node, "TSImportType");
  7679. }
  7680. tsParseEntityName(allowReservedWords = true) {
  7681. let entity = this.parseIdentifier(allowReservedWords);
  7682. while (this.eat(16)) {
  7683. const node = this.startNodeAtNode(entity);
  7684. node.left = entity;
  7685. node.right = this.parseIdentifier(allowReservedWords);
  7686. entity = this.finishNode(node, "TSQualifiedName");
  7687. }
  7688. return entity;
  7689. }
  7690. tsParseTypeReference() {
  7691. const node = this.startNode();
  7692. node.typeName = this.tsParseEntityName();
  7693. if (!this.hasPrecedingLineBreak() && this.match(47)) {
  7694. node.typeParameters = this.tsParseTypeArguments();
  7695. }
  7696. return this.finishNode(node, "TSTypeReference");
  7697. }
  7698. tsParseThisTypePredicate(lhs) {
  7699. this.next();
  7700. const node = this.startNodeAtNode(lhs);
  7701. node.parameterName = lhs;
  7702. node.typeAnnotation = this.tsParseTypeAnnotation(false);
  7703. node.asserts = false;
  7704. return this.finishNode(node, "TSTypePredicate");
  7705. }
  7706. tsParseThisTypeNode() {
  7707. const node = this.startNode();
  7708. this.next();
  7709. return this.finishNode(node, "TSThisType");
  7710. }
  7711. tsParseTypeQuery() {
  7712. const node = this.startNode();
  7713. this.expect(87);
  7714. if (this.match(83)) {
  7715. node.exprName = this.tsParseImportType();
  7716. } else {
  7717. node.exprName = this.tsParseEntityName();
  7718. }
  7719. if (!this.hasPrecedingLineBreak() && this.match(47)) {
  7720. node.typeParameters = this.tsParseTypeArguments();
  7721. }
  7722. return this.finishNode(node, "TSTypeQuery");
  7723. }
  7724. tsParseTypeParameter(parseModifiers) {
  7725. const node = this.startNode();
  7726. parseModifiers(node);
  7727. node.name = this.tsParseTypeParameterName();
  7728. node.constraint = this.tsEatThenParseType(81);
  7729. node.default = this.tsEatThenParseType(29);
  7730. return this.finishNode(node, "TSTypeParameter");
  7731. }
  7732. tsTryParseTypeParameters(parseModifiers) {
  7733. if (this.match(47)) {
  7734. return this.tsParseTypeParameters(parseModifiers);
  7735. }
  7736. }
  7737. tsParseTypeParameters(parseModifiers) {
  7738. const node = this.startNode();
  7739. if (this.match(47) || this.match(142)) {
  7740. this.next();
  7741. } else {
  7742. this.unexpected();
  7743. }
  7744. const refTrailingCommaPos = {
  7745. value: -1
  7746. };
  7747. node.params = this.tsParseBracketedList("TypeParametersOrArguments", this.tsParseTypeParameter.bind(this, parseModifiers), false, true, refTrailingCommaPos);
  7748. if (node.params.length === 0) {
  7749. this.raise(TSErrors.EmptyTypeParameters, node);
  7750. }
  7751. if (refTrailingCommaPos.value !== -1) {
  7752. this.addExtra(node, "trailingComma", refTrailingCommaPos.value);
  7753. }
  7754. return this.finishNode(node, "TSTypeParameterDeclaration");
  7755. }
  7756. tsFillSignature(returnToken, signature) {
  7757. const returnTokenRequired = returnToken === 19;
  7758. const paramsKey = "parameters";
  7759. const returnTypeKey = "typeAnnotation";
  7760. signature.typeParameters = this.tsTryParseTypeParameters(this.tsParseConstModifier);
  7761. this.expect(10);
  7762. signature[paramsKey] = this.tsParseBindingListForSignature();
  7763. if (returnTokenRequired) {
  7764. signature[returnTypeKey] = this.tsParseTypeOrTypePredicateAnnotation(returnToken);
  7765. } else if (this.match(returnToken)) {
  7766. signature[returnTypeKey] = this.tsParseTypeOrTypePredicateAnnotation(returnToken);
  7767. }
  7768. }
  7769. tsParseBindingListForSignature() {
  7770. const list = super.parseBindingList(11, 41, 2);
  7771. for (const pattern of list) {
  7772. const {
  7773. type
  7774. } = pattern;
  7775. if (type === "AssignmentPattern" || type === "TSParameterProperty") {
  7776. this.raise(TSErrors.UnsupportedSignatureParameterKind, pattern, {
  7777. type
  7778. });
  7779. }
  7780. }
  7781. return list;
  7782. }
  7783. tsParseTypeMemberSemicolon() {
  7784. if (!this.eat(12) && !this.isLineTerminator()) {
  7785. this.expect(13);
  7786. }
  7787. }
  7788. tsParseSignatureMember(kind, node) {
  7789. this.tsFillSignature(14, node);
  7790. this.tsParseTypeMemberSemicolon();
  7791. return this.finishNode(node, kind);
  7792. }
  7793. tsIsUnambiguouslyIndexSignature() {
  7794. this.next();
  7795. if (tokenIsIdentifier(this.state.type)) {
  7796. this.next();
  7797. return this.match(14);
  7798. }
  7799. return false;
  7800. }
  7801. tsTryParseIndexSignature(node) {
  7802. if (!(this.match(0) && this.tsLookAhead(this.tsIsUnambiguouslyIndexSignature.bind(this)))) {
  7803. return;
  7804. }
  7805. this.expect(0);
  7806. const id = this.parseIdentifier();
  7807. id.typeAnnotation = this.tsParseTypeAnnotation();
  7808. this.resetEndLocation(id);
  7809. this.expect(3);
  7810. node.parameters = [id];
  7811. const type = this.tsTryParseTypeAnnotation();
  7812. if (type) node.typeAnnotation = type;
  7813. this.tsParseTypeMemberSemicolon();
  7814. return this.finishNode(node, "TSIndexSignature");
  7815. }
  7816. tsParsePropertyOrMethodSignature(node, readonly) {
  7817. if (this.eat(17)) node.optional = true;
  7818. const nodeAny = node;
  7819. if (this.match(10) || this.match(47)) {
  7820. if (readonly) {
  7821. this.raise(TSErrors.ReadonlyForMethodSignature, node);
  7822. }
  7823. const method = nodeAny;
  7824. if (method.kind && this.match(47)) {
  7825. this.raise(TSErrors.AccesorCannotHaveTypeParameters, this.state.curPosition());
  7826. }
  7827. this.tsFillSignature(14, method);
  7828. this.tsParseTypeMemberSemicolon();
  7829. const paramsKey = "parameters";
  7830. const returnTypeKey = "typeAnnotation";
  7831. if (method.kind === "get") {
  7832. if (method[paramsKey].length > 0) {
  7833. this.raise(Errors.BadGetterArity, this.state.curPosition());
  7834. if (this.isThisParam(method[paramsKey][0])) {
  7835. this.raise(TSErrors.AccesorCannotDeclareThisParameter, this.state.curPosition());
  7836. }
  7837. }
  7838. } else if (method.kind === "set") {
  7839. if (method[paramsKey].length !== 1) {
  7840. this.raise(Errors.BadSetterArity, this.state.curPosition());
  7841. } else {
  7842. const firstParameter = method[paramsKey][0];
  7843. if (this.isThisParam(firstParameter)) {
  7844. this.raise(TSErrors.AccesorCannotDeclareThisParameter, this.state.curPosition());
  7845. }
  7846. if (firstParameter.type === "Identifier" && firstParameter.optional) {
  7847. this.raise(TSErrors.SetAccesorCannotHaveOptionalParameter, this.state.curPosition());
  7848. }
  7849. if (firstParameter.type === "RestElement") {
  7850. this.raise(TSErrors.SetAccesorCannotHaveRestParameter, this.state.curPosition());
  7851. }
  7852. }
  7853. if (method[returnTypeKey]) {
  7854. this.raise(TSErrors.SetAccesorCannotHaveReturnType, method[returnTypeKey]);
  7855. }
  7856. } else {
  7857. method.kind = "method";
  7858. }
  7859. return this.finishNode(method, "TSMethodSignature");
  7860. } else {
  7861. const property = nodeAny;
  7862. if (readonly) property.readonly = true;
  7863. const type = this.tsTryParseTypeAnnotation();
  7864. if (type) property.typeAnnotation = type;
  7865. this.tsParseTypeMemberSemicolon();
  7866. return this.finishNode(property, "TSPropertySignature");
  7867. }
  7868. }
  7869. tsParseTypeMember() {
  7870. const node = this.startNode();
  7871. if (this.match(10) || this.match(47)) {
  7872. return this.tsParseSignatureMember("TSCallSignatureDeclaration", node);
  7873. }
  7874. if (this.match(77)) {
  7875. const id = this.startNode();
  7876. this.next();
  7877. if (this.match(10) || this.match(47)) {
  7878. return this.tsParseSignatureMember("TSConstructSignatureDeclaration", node);
  7879. } else {
  7880. node.key = this.createIdentifier(id, "new");
  7881. return this.tsParsePropertyOrMethodSignature(node, false);
  7882. }
  7883. }
  7884. this.tsParseModifiers({
  7885. allowedModifiers: ["readonly"],
  7886. disallowedModifiers: ["declare", "abstract", "private", "protected", "public", "static", "override"]
  7887. }, node);
  7888. const idx = this.tsTryParseIndexSignature(node);
  7889. if (idx) {
  7890. return idx;
  7891. }
  7892. super.parsePropertyName(node);
  7893. if (!node.computed && node.key.type === "Identifier" && (node.key.name === "get" || node.key.name === "set") && this.tsTokenCanFollowModifier()) {
  7894. node.kind = node.key.name;
  7895. super.parsePropertyName(node);
  7896. }
  7897. return this.tsParsePropertyOrMethodSignature(node, !!node.readonly);
  7898. }
  7899. tsParseTypeLiteral() {
  7900. const node = this.startNode();
  7901. node.members = this.tsParseObjectTypeMembers();
  7902. return this.finishNode(node, "TSTypeLiteral");
  7903. }
  7904. tsParseObjectTypeMembers() {
  7905. this.expect(5);
  7906. const members = this.tsParseList("TypeMembers", this.tsParseTypeMember.bind(this));
  7907. this.expect(8);
  7908. return members;
  7909. }
  7910. tsIsStartOfMappedType() {
  7911. this.next();
  7912. if (this.eat(53)) {
  7913. return this.isContextual(122);
  7914. }
  7915. if (this.isContextual(122)) {
  7916. this.next();
  7917. }
  7918. if (!this.match(0)) {
  7919. return false;
  7920. }
  7921. this.next();
  7922. if (!this.tsIsIdentifier()) {
  7923. return false;
  7924. }
  7925. this.next();
  7926. return this.match(58);
  7927. }
  7928. tsParseMappedTypeParameter() {
  7929. const node = this.startNode();
  7930. node.name = this.tsParseTypeParameterName();
  7931. node.constraint = this.tsExpectThenParseType(58);
  7932. return this.finishNode(node, "TSTypeParameter");
  7933. }
  7934. tsParseMappedType() {
  7935. const node = this.startNode();
  7936. this.expect(5);
  7937. if (this.match(53)) {
  7938. node.readonly = this.state.value;
  7939. this.next();
  7940. this.expectContextual(122);
  7941. } else if (this.eatContextual(122)) {
  7942. node.readonly = true;
  7943. }
  7944. this.expect(0);
  7945. node.typeParameter = this.tsParseMappedTypeParameter();
  7946. node.nameType = this.eatContextual(93) ? this.tsParseType() : null;
  7947. this.expect(3);
  7948. if (this.match(53)) {
  7949. node.optional = this.state.value;
  7950. this.next();
  7951. this.expect(17);
  7952. } else if (this.eat(17)) {
  7953. node.optional = true;
  7954. }
  7955. node.typeAnnotation = this.tsTryParseType();
  7956. this.semicolon();
  7957. this.expect(8);
  7958. return this.finishNode(node, "TSMappedType");
  7959. }
  7960. tsParseTupleType() {
  7961. const node = this.startNode();
  7962. node.elementTypes = this.tsParseBracketedList("TupleElementTypes", this.tsParseTupleElementType.bind(this), true, false);
  7963. let seenOptionalElement = false;
  7964. node.elementTypes.forEach(elementNode => {
  7965. const {
  7966. type
  7967. } = elementNode;
  7968. if (seenOptionalElement && type !== "TSRestType" && type !== "TSOptionalType" && !(type === "TSNamedTupleMember" && elementNode.optional)) {
  7969. this.raise(TSErrors.OptionalTypeBeforeRequired, elementNode);
  7970. }
  7971. seenOptionalElement || (seenOptionalElement = type === "TSNamedTupleMember" && elementNode.optional || type === "TSOptionalType");
  7972. });
  7973. return this.finishNode(node, "TSTupleType");
  7974. }
  7975. tsParseTupleElementType() {
  7976. const {
  7977. startLoc
  7978. } = this.state;
  7979. const rest = this.eat(21);
  7980. let labeled;
  7981. let label;
  7982. let optional;
  7983. let type;
  7984. const isWord = tokenIsKeywordOrIdentifier(this.state.type);
  7985. const chAfterWord = isWord ? this.lookaheadCharCode() : null;
  7986. if (chAfterWord === 58) {
  7987. labeled = true;
  7988. optional = false;
  7989. label = this.parseIdentifier(true);
  7990. this.expect(14);
  7991. type = this.tsParseType();
  7992. } else if (chAfterWord === 63) {
  7993. optional = true;
  7994. const startLoc = this.state.startLoc;
  7995. const wordName = this.state.value;
  7996. const typeOrLabel = this.tsParseNonArrayType();
  7997. if (this.lookaheadCharCode() === 58) {
  7998. labeled = true;
  7999. label = this.createIdentifier(this.startNodeAt(startLoc), wordName);
  8000. this.expect(17);
  8001. this.expect(14);
  8002. type = this.tsParseType();
  8003. } else {
  8004. labeled = false;
  8005. type = typeOrLabel;
  8006. this.expect(17);
  8007. }
  8008. } else {
  8009. type = this.tsParseType();
  8010. optional = this.eat(17);
  8011. labeled = this.eat(14);
  8012. }
  8013. if (labeled) {
  8014. let labeledNode;
  8015. if (label) {
  8016. labeledNode = this.startNodeAtNode(label);
  8017. labeledNode.optional = optional;
  8018. labeledNode.label = label;
  8019. labeledNode.elementType = type;
  8020. if (this.eat(17)) {
  8021. labeledNode.optional = true;
  8022. this.raise(TSErrors.TupleOptionalAfterType, this.state.lastTokStartLoc);
  8023. }
  8024. } else {
  8025. labeledNode = this.startNodeAtNode(type);
  8026. labeledNode.optional = optional;
  8027. this.raise(TSErrors.InvalidTupleMemberLabel, type);
  8028. labeledNode.label = type;
  8029. labeledNode.elementType = this.tsParseType();
  8030. }
  8031. type = this.finishNode(labeledNode, "TSNamedTupleMember");
  8032. } else if (optional) {
  8033. const optionalTypeNode = this.startNodeAtNode(type);
  8034. optionalTypeNode.typeAnnotation = type;
  8035. type = this.finishNode(optionalTypeNode, "TSOptionalType");
  8036. }
  8037. if (rest) {
  8038. const restNode = this.startNodeAt(startLoc);
  8039. restNode.typeAnnotation = type;
  8040. type = this.finishNode(restNode, "TSRestType");
  8041. }
  8042. return type;
  8043. }
  8044. tsParseParenthesizedType() {
  8045. const node = this.startNode();
  8046. this.expect(10);
  8047. node.typeAnnotation = this.tsParseType();
  8048. this.expect(11);
  8049. return this.finishNode(node, "TSParenthesizedType");
  8050. }
  8051. tsParseFunctionOrConstructorType(type, abstract) {
  8052. const node = this.startNode();
  8053. if (type === "TSConstructorType") {
  8054. node.abstract = !!abstract;
  8055. if (abstract) this.next();
  8056. this.next();
  8057. }
  8058. this.tsInAllowConditionalTypesContext(() => this.tsFillSignature(19, node));
  8059. return this.finishNode(node, type);
  8060. }
  8061. tsParseLiteralTypeNode() {
  8062. const node = this.startNode();
  8063. switch (this.state.type) {
  8064. case 134:
  8065. case 135:
  8066. case 133:
  8067. case 85:
  8068. case 86:
  8069. node.literal = super.parseExprAtom();
  8070. break;
  8071. default:
  8072. this.unexpected();
  8073. }
  8074. return this.finishNode(node, "TSLiteralType");
  8075. }
  8076. tsParseTemplateLiteralType() {
  8077. const node = this.startNode();
  8078. node.literal = super.parseTemplate(false);
  8079. return this.finishNode(node, "TSLiteralType");
  8080. }
  8081. parseTemplateSubstitution() {
  8082. if (this.state.inType) return this.tsParseType();
  8083. return super.parseTemplateSubstitution();
  8084. }
  8085. tsParseThisTypeOrThisTypePredicate() {
  8086. const thisKeyword = this.tsParseThisTypeNode();
  8087. if (this.isContextual(116) && !this.hasPrecedingLineBreak()) {
  8088. return this.tsParseThisTypePredicate(thisKeyword);
  8089. } else {
  8090. return thisKeyword;
  8091. }
  8092. }
  8093. tsParseNonArrayType() {
  8094. switch (this.state.type) {
  8095. case 133:
  8096. case 134:
  8097. case 135:
  8098. case 85:
  8099. case 86:
  8100. return this.tsParseLiteralTypeNode();
  8101. case 53:
  8102. if (this.state.value === "-") {
  8103. const node = this.startNode();
  8104. const nextToken = this.lookahead();
  8105. if (nextToken.type !== 134 && nextToken.type !== 135) {
  8106. this.unexpected();
  8107. }
  8108. node.literal = this.parseMaybeUnary();
  8109. return this.finishNode(node, "TSLiteralType");
  8110. }
  8111. break;
  8112. case 78:
  8113. return this.tsParseThisTypeOrThisTypePredicate();
  8114. case 87:
  8115. return this.tsParseTypeQuery();
  8116. case 83:
  8117. return this.tsParseImportType();
  8118. case 5:
  8119. return this.tsLookAhead(this.tsIsStartOfMappedType.bind(this)) ? this.tsParseMappedType() : this.tsParseTypeLiteral();
  8120. case 0:
  8121. return this.tsParseTupleType();
  8122. case 10:
  8123. return this.tsParseParenthesizedType();
  8124. case 25:
  8125. case 24:
  8126. return this.tsParseTemplateLiteralType();
  8127. default:
  8128. {
  8129. const {
  8130. type
  8131. } = this.state;
  8132. if (tokenIsIdentifier(type) || type === 88 || type === 84) {
  8133. const nodeType = type === 88 ? "TSVoidKeyword" : type === 84 ? "TSNullKeyword" : keywordTypeFromName(this.state.value);
  8134. if (nodeType !== undefined && this.lookaheadCharCode() !== 46) {
  8135. const node = this.startNode();
  8136. this.next();
  8137. return this.finishNode(node, nodeType);
  8138. }
  8139. return this.tsParseTypeReference();
  8140. }
  8141. }
  8142. }
  8143. this.unexpected();
  8144. }
  8145. tsParseArrayTypeOrHigher() {
  8146. let type = this.tsParseNonArrayType();
  8147. while (!this.hasPrecedingLineBreak() && this.eat(0)) {
  8148. if (this.match(3)) {
  8149. const node = this.startNodeAtNode(type);
  8150. node.elementType = type;
  8151. this.expect(3);
  8152. type = this.finishNode(node, "TSArrayType");
  8153. } else {
  8154. const node = this.startNodeAtNode(type);
  8155. node.objectType = type;
  8156. node.indexType = this.tsParseType();
  8157. this.expect(3);
  8158. type = this.finishNode(node, "TSIndexedAccessType");
  8159. }
  8160. }
  8161. return type;
  8162. }
  8163. tsParseTypeOperator() {
  8164. const node = this.startNode();
  8165. const operator = this.state.value;
  8166. this.next();
  8167. node.operator = operator;
  8168. node.typeAnnotation = this.tsParseTypeOperatorOrHigher();
  8169. if (operator === "readonly") {
  8170. this.tsCheckTypeAnnotationForReadOnly(node);
  8171. }
  8172. return this.finishNode(node, "TSTypeOperator");
  8173. }
  8174. tsCheckTypeAnnotationForReadOnly(node) {
  8175. switch (node.typeAnnotation.type) {
  8176. case "TSTupleType":
  8177. case "TSArrayType":
  8178. return;
  8179. default:
  8180. this.raise(TSErrors.UnexpectedReadonly, node);
  8181. }
  8182. }
  8183. tsParseInferType() {
  8184. const node = this.startNode();
  8185. this.expectContextual(115);
  8186. const typeParameter = this.startNode();
  8187. typeParameter.name = this.tsParseTypeParameterName();
  8188. typeParameter.constraint = this.tsTryParse(() => this.tsParseConstraintForInferType());
  8189. node.typeParameter = this.finishNode(typeParameter, "TSTypeParameter");
  8190. return this.finishNode(node, "TSInferType");
  8191. }
  8192. tsParseConstraintForInferType() {
  8193. if (this.eat(81)) {
  8194. const constraint = this.tsInDisallowConditionalTypesContext(() => this.tsParseType());
  8195. if (this.state.inDisallowConditionalTypesContext || !this.match(17)) {
  8196. return constraint;
  8197. }
  8198. }
  8199. }
  8200. tsParseTypeOperatorOrHigher() {
  8201. const isTypeOperator = tokenIsTSTypeOperator(this.state.type) && !this.state.containsEsc;
  8202. return isTypeOperator ? this.tsParseTypeOperator() : this.isContextual(115) ? this.tsParseInferType() : this.tsInAllowConditionalTypesContext(() => this.tsParseArrayTypeOrHigher());
  8203. }
  8204. tsParseUnionOrIntersectionType(kind, parseConstituentType, operator) {
  8205. const node = this.startNode();
  8206. const hasLeadingOperator = this.eat(operator);
  8207. const types = [];
  8208. do {
  8209. types.push(parseConstituentType());
  8210. } while (this.eat(operator));
  8211. if (types.length === 1 && !hasLeadingOperator) {
  8212. return types[0];
  8213. }
  8214. node.types = types;
  8215. return this.finishNode(node, kind);
  8216. }
  8217. tsParseIntersectionTypeOrHigher() {
  8218. return this.tsParseUnionOrIntersectionType("TSIntersectionType", this.tsParseTypeOperatorOrHigher.bind(this), 45);
  8219. }
  8220. tsParseUnionTypeOrHigher() {
  8221. return this.tsParseUnionOrIntersectionType("TSUnionType", this.tsParseIntersectionTypeOrHigher.bind(this), 43);
  8222. }
  8223. tsIsStartOfFunctionType() {
  8224. if (this.match(47)) {
  8225. return true;
  8226. }
  8227. return this.match(10) && this.tsLookAhead(this.tsIsUnambiguouslyStartOfFunctionType.bind(this));
  8228. }
  8229. tsSkipParameterStart() {
  8230. if (tokenIsIdentifier(this.state.type) || this.match(78)) {
  8231. this.next();
  8232. return true;
  8233. }
  8234. if (this.match(5)) {
  8235. const {
  8236. errors
  8237. } = this.state;
  8238. const previousErrorCount = errors.length;
  8239. try {
  8240. this.parseObjectLike(8, true);
  8241. return errors.length === previousErrorCount;
  8242. } catch (_unused) {
  8243. return false;
  8244. }
  8245. }
  8246. if (this.match(0)) {
  8247. this.next();
  8248. const {
  8249. errors
  8250. } = this.state;
  8251. const previousErrorCount = errors.length;
  8252. try {
  8253. super.parseBindingList(3, 93, 1);
  8254. return errors.length === previousErrorCount;
  8255. } catch (_unused2) {
  8256. return false;
  8257. }
  8258. }
  8259. return false;
  8260. }
  8261. tsIsUnambiguouslyStartOfFunctionType() {
  8262. this.next();
  8263. if (this.match(11) || this.match(21)) {
  8264. return true;
  8265. }
  8266. if (this.tsSkipParameterStart()) {
  8267. if (this.match(14) || this.match(12) || this.match(17) || this.match(29)) {
  8268. return true;
  8269. }
  8270. if (this.match(11)) {
  8271. this.next();
  8272. if (this.match(19)) {
  8273. return true;
  8274. }
  8275. }
  8276. }
  8277. return false;
  8278. }
  8279. tsParseTypeOrTypePredicateAnnotation(returnToken) {
  8280. return this.tsInType(() => {
  8281. const t = this.startNode();
  8282. this.expect(returnToken);
  8283. const node = this.startNode();
  8284. const asserts = !!this.tsTryParse(this.tsParseTypePredicateAsserts.bind(this));
  8285. if (asserts && this.match(78)) {
  8286. let thisTypePredicate = this.tsParseThisTypeOrThisTypePredicate();
  8287. if (thisTypePredicate.type === "TSThisType") {
  8288. node.parameterName = thisTypePredicate;
  8289. node.asserts = true;
  8290. node.typeAnnotation = null;
  8291. thisTypePredicate = this.finishNode(node, "TSTypePredicate");
  8292. } else {
  8293. this.resetStartLocationFromNode(thisTypePredicate, node);
  8294. thisTypePredicate.asserts = true;
  8295. }
  8296. t.typeAnnotation = thisTypePredicate;
  8297. return this.finishNode(t, "TSTypeAnnotation");
  8298. }
  8299. const typePredicateVariable = this.tsIsIdentifier() && this.tsTryParse(this.tsParseTypePredicatePrefix.bind(this));
  8300. if (!typePredicateVariable) {
  8301. if (!asserts) {
  8302. return this.tsParseTypeAnnotation(false, t);
  8303. }
  8304. node.parameterName = this.parseIdentifier();
  8305. node.asserts = asserts;
  8306. node.typeAnnotation = null;
  8307. t.typeAnnotation = this.finishNode(node, "TSTypePredicate");
  8308. return this.finishNode(t, "TSTypeAnnotation");
  8309. }
  8310. const type = this.tsParseTypeAnnotation(false);
  8311. node.parameterName = typePredicateVariable;
  8312. node.typeAnnotation = type;
  8313. node.asserts = asserts;
  8314. t.typeAnnotation = this.finishNode(node, "TSTypePredicate");
  8315. return this.finishNode(t, "TSTypeAnnotation");
  8316. });
  8317. }
  8318. tsTryParseTypeOrTypePredicateAnnotation() {
  8319. if (this.match(14)) {
  8320. return this.tsParseTypeOrTypePredicateAnnotation(14);
  8321. }
  8322. }
  8323. tsTryParseTypeAnnotation() {
  8324. if (this.match(14)) {
  8325. return this.tsParseTypeAnnotation();
  8326. }
  8327. }
  8328. tsTryParseType() {
  8329. return this.tsEatThenParseType(14);
  8330. }
  8331. tsParseTypePredicatePrefix() {
  8332. const id = this.parseIdentifier();
  8333. if (this.isContextual(116) && !this.hasPrecedingLineBreak()) {
  8334. this.next();
  8335. return id;
  8336. }
  8337. }
  8338. tsParseTypePredicateAsserts() {
  8339. if (this.state.type !== 109) {
  8340. return false;
  8341. }
  8342. const containsEsc = this.state.containsEsc;
  8343. this.next();
  8344. if (!tokenIsIdentifier(this.state.type) && !this.match(78)) {
  8345. return false;
  8346. }
  8347. if (containsEsc) {
  8348. this.raise(Errors.InvalidEscapedReservedWord, this.state.lastTokStartLoc, {
  8349. reservedWord: "asserts"
  8350. });
  8351. }
  8352. return true;
  8353. }
  8354. tsParseTypeAnnotation(eatColon = true, t = this.startNode()) {
  8355. this.tsInType(() => {
  8356. if (eatColon) this.expect(14);
  8357. t.typeAnnotation = this.tsParseType();
  8358. });
  8359. return this.finishNode(t, "TSTypeAnnotation");
  8360. }
  8361. tsParseType() {
  8362. assert(this.state.inType);
  8363. const type = this.tsParseNonConditionalType();
  8364. if (this.state.inDisallowConditionalTypesContext || this.hasPrecedingLineBreak() || !this.eat(81)) {
  8365. return type;
  8366. }
  8367. const node = this.startNodeAtNode(type);
  8368. node.checkType = type;
  8369. node.extendsType = this.tsInDisallowConditionalTypesContext(() => this.tsParseNonConditionalType());
  8370. this.expect(17);
  8371. node.trueType = this.tsInAllowConditionalTypesContext(() => this.tsParseType());
  8372. this.expect(14);
  8373. node.falseType = this.tsInAllowConditionalTypesContext(() => this.tsParseType());
  8374. return this.finishNode(node, "TSConditionalType");
  8375. }
  8376. isAbstractConstructorSignature() {
  8377. return this.isContextual(124) && this.lookahead().type === 77;
  8378. }
  8379. tsParseNonConditionalType() {
  8380. if (this.tsIsStartOfFunctionType()) {
  8381. return this.tsParseFunctionOrConstructorType("TSFunctionType");
  8382. }
  8383. if (this.match(77)) {
  8384. return this.tsParseFunctionOrConstructorType("TSConstructorType");
  8385. } else if (this.isAbstractConstructorSignature()) {
  8386. return this.tsParseFunctionOrConstructorType("TSConstructorType", true);
  8387. }
  8388. return this.tsParseUnionTypeOrHigher();
  8389. }
  8390. tsParseTypeAssertion() {
  8391. if (this.getPluginOption("typescript", "disallowAmbiguousJSXLike")) {
  8392. this.raise(TSErrors.ReservedTypeAssertion, this.state.startLoc);
  8393. }
  8394. const node = this.startNode();
  8395. node.typeAnnotation = this.tsInType(() => {
  8396. this.next();
  8397. return this.match(75) ? this.tsParseTypeReference() : this.tsParseType();
  8398. });
  8399. this.expect(48);
  8400. node.expression = this.parseMaybeUnary();
  8401. return this.finishNode(node, "TSTypeAssertion");
  8402. }
  8403. tsParseHeritageClause(token) {
  8404. const originalStartLoc = this.state.startLoc;
  8405. const delimitedList = this.tsParseDelimitedList("HeritageClauseElement", () => {
  8406. const node = this.startNode();
  8407. node.expression = this.tsParseEntityName();
  8408. if (this.match(47)) {
  8409. node.typeParameters = this.tsParseTypeArguments();
  8410. }
  8411. return this.finishNode(node, "TSExpressionWithTypeArguments");
  8412. });
  8413. if (!delimitedList.length) {
  8414. this.raise(TSErrors.EmptyHeritageClauseType, originalStartLoc, {
  8415. token
  8416. });
  8417. }
  8418. return delimitedList;
  8419. }
  8420. tsParseInterfaceDeclaration(node, properties = {}) {
  8421. if (this.hasFollowingLineBreak()) return null;
  8422. this.expectContextual(129);
  8423. if (properties.declare) node.declare = true;
  8424. if (tokenIsIdentifier(this.state.type)) {
  8425. node.id = this.parseIdentifier();
  8426. this.checkIdentifier(node.id, 130);
  8427. } else {
  8428. node.id = null;
  8429. this.raise(TSErrors.MissingInterfaceName, this.state.startLoc);
  8430. }
  8431. node.typeParameters = this.tsTryParseTypeParameters(this.tsParseInOutConstModifiers);
  8432. if (this.eat(81)) {
  8433. node.extends = this.tsParseHeritageClause("extends");
  8434. }
  8435. const body = this.startNode();
  8436. body.body = this.tsInType(this.tsParseObjectTypeMembers.bind(this));
  8437. node.body = this.finishNode(body, "TSInterfaceBody");
  8438. return this.finishNode(node, "TSInterfaceDeclaration");
  8439. }
  8440. tsParseTypeAliasDeclaration(node) {
  8441. node.id = this.parseIdentifier();
  8442. this.checkIdentifier(node.id, 2);
  8443. node.typeAnnotation = this.tsInType(() => {
  8444. node.typeParameters = this.tsTryParseTypeParameters(this.tsParseInOutModifiers);
  8445. this.expect(29);
  8446. if (this.isContextual(114) && this.lookahead().type !== 16) {
  8447. const node = this.startNode();
  8448. this.next();
  8449. return this.finishNode(node, "TSIntrinsicKeyword");
  8450. }
  8451. return this.tsParseType();
  8452. });
  8453. this.semicolon();
  8454. return this.finishNode(node, "TSTypeAliasDeclaration");
  8455. }
  8456. tsInNoContext(cb) {
  8457. const oldContext = this.state.context;
  8458. this.state.context = [oldContext[0]];
  8459. try {
  8460. return cb();
  8461. } finally {
  8462. this.state.context = oldContext;
  8463. }
  8464. }
  8465. tsInType(cb) {
  8466. const oldInType = this.state.inType;
  8467. this.state.inType = true;
  8468. try {
  8469. return cb();
  8470. } finally {
  8471. this.state.inType = oldInType;
  8472. }
  8473. }
  8474. tsInDisallowConditionalTypesContext(cb) {
  8475. const oldInDisallowConditionalTypesContext = this.state.inDisallowConditionalTypesContext;
  8476. this.state.inDisallowConditionalTypesContext = true;
  8477. try {
  8478. return cb();
  8479. } finally {
  8480. this.state.inDisallowConditionalTypesContext = oldInDisallowConditionalTypesContext;
  8481. }
  8482. }
  8483. tsInAllowConditionalTypesContext(cb) {
  8484. const oldInDisallowConditionalTypesContext = this.state.inDisallowConditionalTypesContext;
  8485. this.state.inDisallowConditionalTypesContext = false;
  8486. try {
  8487. return cb();
  8488. } finally {
  8489. this.state.inDisallowConditionalTypesContext = oldInDisallowConditionalTypesContext;
  8490. }
  8491. }
  8492. tsEatThenParseType(token) {
  8493. if (this.match(token)) {
  8494. return this.tsNextThenParseType();
  8495. }
  8496. }
  8497. tsExpectThenParseType(token) {
  8498. return this.tsInType(() => {
  8499. this.expect(token);
  8500. return this.tsParseType();
  8501. });
  8502. }
  8503. tsNextThenParseType() {
  8504. return this.tsInType(() => {
  8505. this.next();
  8506. return this.tsParseType();
  8507. });
  8508. }
  8509. tsParseEnumMember() {
  8510. const node = this.startNode();
  8511. node.id = this.match(133) ? super.parseStringLiteral(this.state.value) : this.parseIdentifier(true);
  8512. if (this.eat(29)) {
  8513. node.initializer = super.parseMaybeAssignAllowIn();
  8514. }
  8515. return this.finishNode(node, "TSEnumMember");
  8516. }
  8517. tsParseEnumDeclaration(node, properties = {}) {
  8518. if (properties.const) node.const = true;
  8519. if (properties.declare) node.declare = true;
  8520. this.expectContextual(126);
  8521. node.id = this.parseIdentifier();
  8522. this.checkIdentifier(node.id, node.const ? 8971 : 8459);
  8523. this.expect(5);
  8524. node.members = this.tsParseDelimitedList("EnumMembers", this.tsParseEnumMember.bind(this));
  8525. this.expect(8);
  8526. return this.finishNode(node, "TSEnumDeclaration");
  8527. }
  8528. tsParseModuleBlock() {
  8529. const node = this.startNode();
  8530. this.scope.enter(0);
  8531. this.expect(5);
  8532. super.parseBlockOrModuleBlockBody(node.body = [], undefined, true, 8);
  8533. this.scope.exit();
  8534. return this.finishNode(node, "TSModuleBlock");
  8535. }
  8536. tsParseModuleOrNamespaceDeclaration(node, nested = false) {
  8537. node.id = this.parseIdentifier();
  8538. if (!nested) {
  8539. this.checkIdentifier(node.id, 1024);
  8540. }
  8541. if (this.eat(16)) {
  8542. const inner = this.startNode();
  8543. this.tsParseModuleOrNamespaceDeclaration(inner, true);
  8544. node.body = inner;
  8545. } else {
  8546. this.scope.enter(256);
  8547. this.prodParam.enter(0);
  8548. node.body = this.tsParseModuleBlock();
  8549. this.prodParam.exit();
  8550. this.scope.exit();
  8551. }
  8552. return this.finishNode(node, "TSModuleDeclaration");
  8553. }
  8554. tsParseAmbientExternalModuleDeclaration(node) {
  8555. if (this.isContextual(112)) {
  8556. node.global = true;
  8557. node.id = this.parseIdentifier();
  8558. } else if (this.match(133)) {
  8559. node.id = super.parseStringLiteral(this.state.value);
  8560. } else {
  8561. this.unexpected();
  8562. }
  8563. if (this.match(5)) {
  8564. this.scope.enter(256);
  8565. this.prodParam.enter(0);
  8566. node.body = this.tsParseModuleBlock();
  8567. this.prodParam.exit();
  8568. this.scope.exit();
  8569. } else {
  8570. this.semicolon();
  8571. }
  8572. return this.finishNode(node, "TSModuleDeclaration");
  8573. }
  8574. tsParseImportEqualsDeclaration(node, maybeDefaultIdentifier, isExport) {
  8575. node.isExport = isExport || false;
  8576. node.id = maybeDefaultIdentifier || this.parseIdentifier();
  8577. this.checkIdentifier(node.id, 4096);
  8578. this.expect(29);
  8579. const moduleReference = this.tsParseModuleReference();
  8580. if (node.importKind === "type" && moduleReference.type !== "TSExternalModuleReference") {
  8581. this.raise(TSErrors.ImportAliasHasImportType, moduleReference);
  8582. }
  8583. node.moduleReference = moduleReference;
  8584. this.semicolon();
  8585. return this.finishNode(node, "TSImportEqualsDeclaration");
  8586. }
  8587. tsIsExternalModuleReference() {
  8588. return this.isContextual(119) && this.lookaheadCharCode() === 40;
  8589. }
  8590. tsParseModuleReference() {
  8591. return this.tsIsExternalModuleReference() ? this.tsParseExternalModuleReference() : this.tsParseEntityName(false);
  8592. }
  8593. tsParseExternalModuleReference() {
  8594. const node = this.startNode();
  8595. this.expectContextual(119);
  8596. this.expect(10);
  8597. if (!this.match(133)) {
  8598. this.unexpected();
  8599. }
  8600. node.expression = super.parseExprAtom();
  8601. this.expect(11);
  8602. this.sawUnambiguousESM = true;
  8603. return this.finishNode(node, "TSExternalModuleReference");
  8604. }
  8605. tsLookAhead(f) {
  8606. const state = this.state.clone();
  8607. const res = f();
  8608. this.state = state;
  8609. return res;
  8610. }
  8611. tsTryParseAndCatch(f) {
  8612. const result = this.tryParse(abort => f() || abort());
  8613. if (result.aborted || !result.node) return;
  8614. if (result.error) this.state = result.failState;
  8615. return result.node;
  8616. }
  8617. tsTryParse(f) {
  8618. const state = this.state.clone();
  8619. const result = f();
  8620. if (result !== undefined && result !== false) {
  8621. return result;
  8622. }
  8623. this.state = state;
  8624. }
  8625. tsTryParseDeclare(nany) {
  8626. if (this.isLineTerminator()) {
  8627. return;
  8628. }
  8629. let startType = this.state.type;
  8630. let kind;
  8631. if (this.isContextual(100)) {
  8632. startType = 74;
  8633. kind = "let";
  8634. }
  8635. return this.tsInAmbientContext(() => {
  8636. switch (startType) {
  8637. case 68:
  8638. nany.declare = true;
  8639. return super.parseFunctionStatement(nany, false, false);
  8640. case 80:
  8641. nany.declare = true;
  8642. return this.parseClass(nany, true, false);
  8643. case 126:
  8644. return this.tsParseEnumDeclaration(nany, {
  8645. declare: true
  8646. });
  8647. case 112:
  8648. return this.tsParseAmbientExternalModuleDeclaration(nany);
  8649. case 75:
  8650. case 74:
  8651. if (!this.match(75) || !this.isLookaheadContextual("enum")) {
  8652. nany.declare = true;
  8653. return this.parseVarStatement(nany, kind || this.state.value, true);
  8654. }
  8655. this.expect(75);
  8656. return this.tsParseEnumDeclaration(nany, {
  8657. const: true,
  8658. declare: true
  8659. });
  8660. case 129:
  8661. {
  8662. const result = this.tsParseInterfaceDeclaration(nany, {
  8663. declare: true
  8664. });
  8665. if (result) return result;
  8666. }
  8667. default:
  8668. if (tokenIsIdentifier(startType)) {
  8669. return this.tsParseDeclaration(nany, this.state.value, true, null);
  8670. }
  8671. }
  8672. });
  8673. }
  8674. tsTryParseExportDeclaration() {
  8675. return this.tsParseDeclaration(this.startNode(), this.state.value, true, null);
  8676. }
  8677. tsParseExpressionStatement(node, expr, decorators) {
  8678. switch (expr.name) {
  8679. case "declare":
  8680. {
  8681. const declaration = this.tsTryParseDeclare(node);
  8682. if (declaration) {
  8683. declaration.declare = true;
  8684. }
  8685. return declaration;
  8686. }
  8687. case "global":
  8688. if (this.match(5)) {
  8689. this.scope.enter(256);
  8690. this.prodParam.enter(0);
  8691. const mod = node;
  8692. mod.global = true;
  8693. mod.id = expr;
  8694. mod.body = this.tsParseModuleBlock();
  8695. this.scope.exit();
  8696. this.prodParam.exit();
  8697. return this.finishNode(mod, "TSModuleDeclaration");
  8698. }
  8699. break;
  8700. default:
  8701. return this.tsParseDeclaration(node, expr.name, false, decorators);
  8702. }
  8703. }
  8704. tsParseDeclaration(node, value, next, decorators) {
  8705. switch (value) {
  8706. case "abstract":
  8707. if (this.tsCheckLineTerminator(next) && (this.match(80) || tokenIsIdentifier(this.state.type))) {
  8708. return this.tsParseAbstractDeclaration(node, decorators);
  8709. }
  8710. break;
  8711. case "module":
  8712. if (this.tsCheckLineTerminator(next)) {
  8713. if (this.match(133)) {
  8714. return this.tsParseAmbientExternalModuleDeclaration(node);
  8715. } else if (tokenIsIdentifier(this.state.type)) {
  8716. return this.tsParseModuleOrNamespaceDeclaration(node);
  8717. }
  8718. }
  8719. break;
  8720. case "namespace":
  8721. if (this.tsCheckLineTerminator(next) && tokenIsIdentifier(this.state.type)) {
  8722. return this.tsParseModuleOrNamespaceDeclaration(node);
  8723. }
  8724. break;
  8725. case "type":
  8726. if (this.tsCheckLineTerminator(next) && tokenIsIdentifier(this.state.type)) {
  8727. return this.tsParseTypeAliasDeclaration(node);
  8728. }
  8729. break;
  8730. }
  8731. }
  8732. tsCheckLineTerminator(next) {
  8733. if (next) {
  8734. if (this.hasFollowingLineBreak()) return false;
  8735. this.next();
  8736. return true;
  8737. }
  8738. return !this.isLineTerminator();
  8739. }
  8740. tsTryParseGenericAsyncArrowFunction(startLoc) {
  8741. if (!this.match(47)) return;
  8742. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  8743. this.state.maybeInArrowParameters = true;
  8744. const res = this.tsTryParseAndCatch(() => {
  8745. const node = this.startNodeAt(startLoc);
  8746. node.typeParameters = this.tsParseTypeParameters(this.tsParseConstModifier);
  8747. super.parseFunctionParams(node);
  8748. node.returnType = this.tsTryParseTypeOrTypePredicateAnnotation();
  8749. this.expect(19);
  8750. return node;
  8751. });
  8752. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  8753. if (!res) return;
  8754. return super.parseArrowExpression(res, null, true);
  8755. }
  8756. tsParseTypeArgumentsInExpression() {
  8757. if (this.reScan_lt() !== 47) return;
  8758. return this.tsParseTypeArguments();
  8759. }
  8760. tsParseTypeArguments() {
  8761. const node = this.startNode();
  8762. node.params = this.tsInType(() => this.tsInNoContext(() => {
  8763. this.expect(47);
  8764. return this.tsParseDelimitedList("TypeParametersOrArguments", this.tsParseType.bind(this));
  8765. }));
  8766. if (node.params.length === 0) {
  8767. this.raise(TSErrors.EmptyTypeArguments, node);
  8768. } else if (!this.state.inType && this.curContext() === types.brace) {
  8769. this.reScan_lt_gt();
  8770. }
  8771. this.expect(48);
  8772. return this.finishNode(node, "TSTypeParameterInstantiation");
  8773. }
  8774. tsIsDeclarationStart() {
  8775. return tokenIsTSDeclarationStart(this.state.type);
  8776. }
  8777. isExportDefaultSpecifier() {
  8778. if (this.tsIsDeclarationStart()) return false;
  8779. return super.isExportDefaultSpecifier();
  8780. }
  8781. parseAssignableListItem(flags, decorators) {
  8782. const startLoc = this.state.startLoc;
  8783. const modified = {};
  8784. this.tsParseModifiers({
  8785. allowedModifiers: ["public", "private", "protected", "override", "readonly"]
  8786. }, modified);
  8787. const accessibility = modified.accessibility;
  8788. const override = modified.override;
  8789. const readonly = modified.readonly;
  8790. if (!(flags & 4) && (accessibility || readonly || override)) {
  8791. this.raise(TSErrors.UnexpectedParameterModifier, startLoc);
  8792. }
  8793. const left = this.parseMaybeDefault();
  8794. this.parseAssignableListItemTypes(left, flags);
  8795. const elt = this.parseMaybeDefault(left.loc.start, left);
  8796. if (accessibility || readonly || override) {
  8797. const pp = this.startNodeAt(startLoc);
  8798. if (decorators.length) {
  8799. pp.decorators = decorators;
  8800. }
  8801. if (accessibility) pp.accessibility = accessibility;
  8802. if (readonly) pp.readonly = readonly;
  8803. if (override) pp.override = override;
  8804. if (elt.type !== "Identifier" && elt.type !== "AssignmentPattern") {
  8805. this.raise(TSErrors.UnsupportedParameterPropertyKind, pp);
  8806. }
  8807. pp.parameter = elt;
  8808. return this.finishNode(pp, "TSParameterProperty");
  8809. }
  8810. if (decorators.length) {
  8811. left.decorators = decorators;
  8812. }
  8813. return elt;
  8814. }
  8815. isSimpleParameter(node) {
  8816. return node.type === "TSParameterProperty" && super.isSimpleParameter(node.parameter) || super.isSimpleParameter(node);
  8817. }
  8818. tsDisallowOptionalPattern(node) {
  8819. for (const param of node.params) {
  8820. if (param.type !== "Identifier" && param.optional && !this.state.isAmbientContext) {
  8821. this.raise(TSErrors.PatternIsOptional, param);
  8822. }
  8823. }
  8824. }
  8825. setArrowFunctionParameters(node, params, trailingCommaLoc) {
  8826. super.setArrowFunctionParameters(node, params, trailingCommaLoc);
  8827. this.tsDisallowOptionalPattern(node);
  8828. }
  8829. parseFunctionBodyAndFinish(node, type, isMethod = false) {
  8830. if (this.match(14)) {
  8831. node.returnType = this.tsParseTypeOrTypePredicateAnnotation(14);
  8832. }
  8833. const bodilessType = type === "FunctionDeclaration" ? "TSDeclareFunction" : type === "ClassMethod" || type === "ClassPrivateMethod" ? "TSDeclareMethod" : undefined;
  8834. if (bodilessType && !this.match(5) && this.isLineTerminator()) {
  8835. return this.finishNode(node, bodilessType);
  8836. }
  8837. if (bodilessType === "TSDeclareFunction" && this.state.isAmbientContext) {
  8838. this.raise(TSErrors.DeclareFunctionHasImplementation, node);
  8839. if (node.declare) {
  8840. return super.parseFunctionBodyAndFinish(node, bodilessType, isMethod);
  8841. }
  8842. }
  8843. this.tsDisallowOptionalPattern(node);
  8844. return super.parseFunctionBodyAndFinish(node, type, isMethod);
  8845. }
  8846. registerFunctionStatementId(node) {
  8847. if (!node.body && node.id) {
  8848. this.checkIdentifier(node.id, 1024);
  8849. } else {
  8850. super.registerFunctionStatementId(node);
  8851. }
  8852. }
  8853. tsCheckForInvalidTypeCasts(items) {
  8854. items.forEach(node => {
  8855. if ((node == null ? void 0 : node.type) === "TSTypeCastExpression") {
  8856. this.raise(TSErrors.UnexpectedTypeAnnotation, node.typeAnnotation);
  8857. }
  8858. });
  8859. }
  8860. toReferencedList(exprList, isInParens) {
  8861. this.tsCheckForInvalidTypeCasts(exprList);
  8862. return exprList;
  8863. }
  8864. parseArrayLike(close, canBePattern, isTuple, refExpressionErrors) {
  8865. const node = super.parseArrayLike(close, canBePattern, isTuple, refExpressionErrors);
  8866. if (node.type === "ArrayExpression") {
  8867. this.tsCheckForInvalidTypeCasts(node.elements);
  8868. }
  8869. return node;
  8870. }
  8871. parseSubscript(base, startLoc, noCalls, state) {
  8872. if (!this.hasPrecedingLineBreak() && this.match(35)) {
  8873. this.state.canStartJSXElement = false;
  8874. this.next();
  8875. const nonNullExpression = this.startNodeAt(startLoc);
  8876. nonNullExpression.expression = base;
  8877. return this.finishNode(nonNullExpression, "TSNonNullExpression");
  8878. }
  8879. let isOptionalCall = false;
  8880. if (this.match(18) && this.lookaheadCharCode() === 60) {
  8881. if (noCalls) {
  8882. state.stop = true;
  8883. return base;
  8884. }
  8885. state.optionalChainMember = isOptionalCall = true;
  8886. this.next();
  8887. }
  8888. if (this.match(47) || this.match(51)) {
  8889. let missingParenErrorLoc;
  8890. const result = this.tsTryParseAndCatch(() => {
  8891. if (!noCalls && this.atPossibleAsyncArrow(base)) {
  8892. const asyncArrowFn = this.tsTryParseGenericAsyncArrowFunction(startLoc);
  8893. if (asyncArrowFn) {
  8894. return asyncArrowFn;
  8895. }
  8896. }
  8897. const typeArguments = this.tsParseTypeArgumentsInExpression();
  8898. if (!typeArguments) return;
  8899. if (isOptionalCall && !this.match(10)) {
  8900. missingParenErrorLoc = this.state.curPosition();
  8901. return;
  8902. }
  8903. if (tokenIsTemplate(this.state.type)) {
  8904. const result = super.parseTaggedTemplateExpression(base, startLoc, state);
  8905. result.typeParameters = typeArguments;
  8906. return result;
  8907. }
  8908. if (!noCalls && this.eat(10)) {
  8909. const node = this.startNodeAt(startLoc);
  8910. node.callee = base;
  8911. node.arguments = this.parseCallExpressionArguments(11, false);
  8912. this.tsCheckForInvalidTypeCasts(node.arguments);
  8913. node.typeParameters = typeArguments;
  8914. if (state.optionalChainMember) {
  8915. node.optional = isOptionalCall;
  8916. }
  8917. return this.finishCallExpression(node, state.optionalChainMember);
  8918. }
  8919. const tokenType = this.state.type;
  8920. if (tokenType === 48 || tokenType === 52 || tokenType !== 10 && tokenCanStartExpression(tokenType) && !this.hasPrecedingLineBreak()) {
  8921. return;
  8922. }
  8923. const node = this.startNodeAt(startLoc);
  8924. node.expression = base;
  8925. node.typeParameters = typeArguments;
  8926. return this.finishNode(node, "TSInstantiationExpression");
  8927. });
  8928. if (missingParenErrorLoc) {
  8929. this.unexpected(missingParenErrorLoc, 10);
  8930. }
  8931. if (result) {
  8932. if (result.type === "TSInstantiationExpression" && (this.match(16) || this.match(18) && this.lookaheadCharCode() !== 40)) {
  8933. this.raise(TSErrors.InvalidPropertyAccessAfterInstantiationExpression, this.state.startLoc);
  8934. }
  8935. return result;
  8936. }
  8937. }
  8938. return super.parseSubscript(base, startLoc, noCalls, state);
  8939. }
  8940. parseNewCallee(node) {
  8941. var _callee$extra;
  8942. super.parseNewCallee(node);
  8943. const {
  8944. callee
  8945. } = node;
  8946. if (callee.type === "TSInstantiationExpression" && !((_callee$extra = callee.extra) != null && _callee$extra.parenthesized)) {
  8947. node.typeParameters = callee.typeParameters;
  8948. node.callee = callee.expression;
  8949. }
  8950. }
  8951. parseExprOp(left, leftStartLoc, minPrec) {
  8952. let isSatisfies;
  8953. if (tokenOperatorPrecedence(58) > minPrec && !this.hasPrecedingLineBreak() && (this.isContextual(93) || (isSatisfies = this.isContextual(120)))) {
  8954. const node = this.startNodeAt(leftStartLoc);
  8955. node.expression = left;
  8956. node.typeAnnotation = this.tsInType(() => {
  8957. this.next();
  8958. if (this.match(75)) {
  8959. if (isSatisfies) {
  8960. this.raise(Errors.UnexpectedKeyword, this.state.startLoc, {
  8961. keyword: "const"
  8962. });
  8963. }
  8964. return this.tsParseTypeReference();
  8965. }
  8966. return this.tsParseType();
  8967. });
  8968. this.finishNode(node, isSatisfies ? "TSSatisfiesExpression" : "TSAsExpression");
  8969. this.reScan_lt_gt();
  8970. return this.parseExprOp(node, leftStartLoc, minPrec);
  8971. }
  8972. return super.parseExprOp(left, leftStartLoc, minPrec);
  8973. }
  8974. checkReservedWord(word, startLoc, checkKeywords, isBinding) {
  8975. if (!this.state.isAmbientContext) {
  8976. super.checkReservedWord(word, startLoc, checkKeywords, isBinding);
  8977. }
  8978. }
  8979. checkImportReflection(node) {
  8980. super.checkImportReflection(node);
  8981. if (node.module && node.importKind !== "value") {
  8982. this.raise(TSErrors.ImportReflectionHasImportType, node.specifiers[0].loc.start);
  8983. }
  8984. }
  8985. checkDuplicateExports() {}
  8986. isPotentialImportPhase(isExport) {
  8987. if (super.isPotentialImportPhase(isExport)) return true;
  8988. if (this.isContextual(130)) {
  8989. const ch = this.lookaheadCharCode();
  8990. return isExport ? ch === 123 || ch === 42 : ch !== 61;
  8991. }
  8992. return !isExport && this.isContextual(87);
  8993. }
  8994. applyImportPhase(node, isExport, phase, loc) {
  8995. super.applyImportPhase(node, isExport, phase, loc);
  8996. if (isExport) {
  8997. node.exportKind = phase === "type" ? "type" : "value";
  8998. } else {
  8999. node.importKind = phase === "type" || phase === "typeof" ? phase : "value";
  9000. }
  9001. }
  9002. parseImport(node) {
  9003. if (this.match(133)) {
  9004. node.importKind = "value";
  9005. return super.parseImport(node);
  9006. }
  9007. let importNode;
  9008. if (tokenIsIdentifier(this.state.type) && this.lookaheadCharCode() === 61) {
  9009. node.importKind = "value";
  9010. return this.tsParseImportEqualsDeclaration(node);
  9011. } else if (this.isContextual(130)) {
  9012. const maybeDefaultIdentifier = this.parseMaybeImportPhase(node, false);
  9013. if (this.lookaheadCharCode() === 61) {
  9014. return this.tsParseImportEqualsDeclaration(node, maybeDefaultIdentifier);
  9015. } else {
  9016. importNode = super.parseImportSpecifiersAndAfter(node, maybeDefaultIdentifier);
  9017. }
  9018. } else {
  9019. importNode = super.parseImport(node);
  9020. }
  9021. if (importNode.importKind === "type" && importNode.specifiers.length > 1 && importNode.specifiers[0].type === "ImportDefaultSpecifier") {
  9022. this.raise(TSErrors.TypeImportCannotSpecifyDefaultAndNamed, importNode);
  9023. }
  9024. return importNode;
  9025. }
  9026. parseExport(node, decorators) {
  9027. if (this.match(83)) {
  9028. this.next();
  9029. const nodeImportEquals = node;
  9030. let maybeDefaultIdentifier = null;
  9031. if (this.isContextual(130) && this.isPotentialImportPhase(false)) {
  9032. maybeDefaultIdentifier = this.parseMaybeImportPhase(nodeImportEquals, false);
  9033. } else {
  9034. nodeImportEquals.importKind = "value";
  9035. }
  9036. return this.tsParseImportEqualsDeclaration(nodeImportEquals, maybeDefaultIdentifier, true);
  9037. } else if (this.eat(29)) {
  9038. const assign = node;
  9039. assign.expression = super.parseExpression();
  9040. this.semicolon();
  9041. this.sawUnambiguousESM = true;
  9042. return this.finishNode(assign, "TSExportAssignment");
  9043. } else if (this.eatContextual(93)) {
  9044. const decl = node;
  9045. this.expectContextual(128);
  9046. decl.id = this.parseIdentifier();
  9047. this.semicolon();
  9048. return this.finishNode(decl, "TSNamespaceExportDeclaration");
  9049. } else {
  9050. return super.parseExport(node, decorators);
  9051. }
  9052. }
  9053. isAbstractClass() {
  9054. return this.isContextual(124) && this.lookahead().type === 80;
  9055. }
  9056. parseExportDefaultExpression() {
  9057. if (this.isAbstractClass()) {
  9058. const cls = this.startNode();
  9059. this.next();
  9060. cls.abstract = true;
  9061. return this.parseClass(cls, true, true);
  9062. }
  9063. if (this.match(129)) {
  9064. const result = this.tsParseInterfaceDeclaration(this.startNode());
  9065. if (result) return result;
  9066. }
  9067. return super.parseExportDefaultExpression();
  9068. }
  9069. parseVarStatement(node, kind, allowMissingInitializer = false) {
  9070. const {
  9071. isAmbientContext
  9072. } = this.state;
  9073. const declaration = super.parseVarStatement(node, kind, allowMissingInitializer || isAmbientContext);
  9074. if (!isAmbientContext) return declaration;
  9075. for (const {
  9076. id,
  9077. init
  9078. } of declaration.declarations) {
  9079. if (!init) continue;
  9080. if (kind !== "const" || !!id.typeAnnotation) {
  9081. this.raise(TSErrors.InitializerNotAllowedInAmbientContext, init);
  9082. } else if (!isValidAmbientConstInitializer(init, this.hasPlugin("estree"))) {
  9083. this.raise(TSErrors.ConstInitiailizerMustBeStringOrNumericLiteralOrLiteralEnumReference, init);
  9084. }
  9085. }
  9086. return declaration;
  9087. }
  9088. parseStatementContent(flags, decorators) {
  9089. if (this.match(75) && this.isLookaheadContextual("enum")) {
  9090. const node = this.startNode();
  9091. this.expect(75);
  9092. return this.tsParseEnumDeclaration(node, {
  9093. const: true
  9094. });
  9095. }
  9096. if (this.isContextual(126)) {
  9097. return this.tsParseEnumDeclaration(this.startNode());
  9098. }
  9099. if (this.isContextual(129)) {
  9100. const result = this.tsParseInterfaceDeclaration(this.startNode());
  9101. if (result) return result;
  9102. }
  9103. return super.parseStatementContent(flags, decorators);
  9104. }
  9105. parseAccessModifier() {
  9106. return this.tsParseModifier(["public", "protected", "private"]);
  9107. }
  9108. tsHasSomeModifiers(member, modifiers) {
  9109. return modifiers.some(modifier => {
  9110. if (tsIsAccessModifier(modifier)) {
  9111. return member.accessibility === modifier;
  9112. }
  9113. return !!member[modifier];
  9114. });
  9115. }
  9116. tsIsStartOfStaticBlocks() {
  9117. return this.isContextual(106) && this.lookaheadCharCode() === 123;
  9118. }
  9119. parseClassMember(classBody, member, state) {
  9120. const modifiers = ["declare", "private", "public", "protected", "override", "abstract", "readonly", "static"];
  9121. this.tsParseModifiers({
  9122. allowedModifiers: modifiers,
  9123. disallowedModifiers: ["in", "out"],
  9124. stopOnStartOfClassStaticBlock: true,
  9125. errorTemplate: TSErrors.InvalidModifierOnTypeParameterPositions
  9126. }, member);
  9127. const callParseClassMemberWithIsStatic = () => {
  9128. if (this.tsIsStartOfStaticBlocks()) {
  9129. this.next();
  9130. this.next();
  9131. if (this.tsHasSomeModifiers(member, modifiers)) {
  9132. this.raise(TSErrors.StaticBlockCannotHaveModifier, this.state.curPosition());
  9133. }
  9134. super.parseClassStaticBlock(classBody, member);
  9135. } else {
  9136. this.parseClassMemberWithIsStatic(classBody, member, state, !!member.static);
  9137. }
  9138. };
  9139. if (member.declare) {
  9140. this.tsInAmbientContext(callParseClassMemberWithIsStatic);
  9141. } else {
  9142. callParseClassMemberWithIsStatic();
  9143. }
  9144. }
  9145. parseClassMemberWithIsStatic(classBody, member, state, isStatic) {
  9146. const idx = this.tsTryParseIndexSignature(member);
  9147. if (idx) {
  9148. classBody.body.push(idx);
  9149. if (member.abstract) {
  9150. this.raise(TSErrors.IndexSignatureHasAbstract, member);
  9151. }
  9152. if (member.accessibility) {
  9153. this.raise(TSErrors.IndexSignatureHasAccessibility, member, {
  9154. modifier: member.accessibility
  9155. });
  9156. }
  9157. if (member.declare) {
  9158. this.raise(TSErrors.IndexSignatureHasDeclare, member);
  9159. }
  9160. if (member.override) {
  9161. this.raise(TSErrors.IndexSignatureHasOverride, member);
  9162. }
  9163. return;
  9164. }
  9165. if (!this.state.inAbstractClass && member.abstract) {
  9166. this.raise(TSErrors.NonAbstractClassHasAbstractMethod, member);
  9167. }
  9168. if (member.override) {
  9169. if (!state.hadSuperClass) {
  9170. this.raise(TSErrors.OverrideNotInSubClass, member);
  9171. }
  9172. }
  9173. super.parseClassMemberWithIsStatic(classBody, member, state, isStatic);
  9174. }
  9175. parsePostMemberNameModifiers(methodOrProp) {
  9176. const optional = this.eat(17);
  9177. if (optional) methodOrProp.optional = true;
  9178. if (methodOrProp.readonly && this.match(10)) {
  9179. this.raise(TSErrors.ClassMethodHasReadonly, methodOrProp);
  9180. }
  9181. if (methodOrProp.declare && this.match(10)) {
  9182. this.raise(TSErrors.ClassMethodHasDeclare, methodOrProp);
  9183. }
  9184. }
  9185. parseExpressionStatement(node, expr, decorators) {
  9186. const decl = expr.type === "Identifier" ? this.tsParseExpressionStatement(node, expr, decorators) : undefined;
  9187. return decl || super.parseExpressionStatement(node, expr, decorators);
  9188. }
  9189. shouldParseExportDeclaration() {
  9190. if (this.tsIsDeclarationStart()) return true;
  9191. return super.shouldParseExportDeclaration();
  9192. }
  9193. parseConditional(expr, startLoc, refExpressionErrors) {
  9194. if (!this.state.maybeInArrowParameters || !this.match(17)) {
  9195. return super.parseConditional(expr, startLoc, refExpressionErrors);
  9196. }
  9197. const result = this.tryParse(() => super.parseConditional(expr, startLoc));
  9198. if (!result.node) {
  9199. if (result.error) {
  9200. super.setOptionalParametersError(refExpressionErrors, result.error);
  9201. }
  9202. return expr;
  9203. }
  9204. if (result.error) this.state = result.failState;
  9205. return result.node;
  9206. }
  9207. parseParenItem(node, startLoc) {
  9208. const newNode = super.parseParenItem(node, startLoc);
  9209. if (this.eat(17)) {
  9210. newNode.optional = true;
  9211. this.resetEndLocation(node);
  9212. }
  9213. if (this.match(14)) {
  9214. const typeCastNode = this.startNodeAt(startLoc);
  9215. typeCastNode.expression = node;
  9216. typeCastNode.typeAnnotation = this.tsParseTypeAnnotation();
  9217. return this.finishNode(typeCastNode, "TSTypeCastExpression");
  9218. }
  9219. return node;
  9220. }
  9221. parseExportDeclaration(node) {
  9222. if (!this.state.isAmbientContext && this.isContextual(125)) {
  9223. return this.tsInAmbientContext(() => this.parseExportDeclaration(node));
  9224. }
  9225. const startLoc = this.state.startLoc;
  9226. const isDeclare = this.eatContextual(125);
  9227. if (isDeclare && (this.isContextual(125) || !this.shouldParseExportDeclaration())) {
  9228. throw this.raise(TSErrors.ExpectedAmbientAfterExportDeclare, this.state.startLoc);
  9229. }
  9230. const isIdentifier = tokenIsIdentifier(this.state.type);
  9231. const declaration = isIdentifier && this.tsTryParseExportDeclaration() || super.parseExportDeclaration(node);
  9232. if (!declaration) return null;
  9233. if (declaration.type === "TSInterfaceDeclaration" || declaration.type === "TSTypeAliasDeclaration" || isDeclare) {
  9234. node.exportKind = "type";
  9235. }
  9236. if (isDeclare) {
  9237. this.resetStartLocation(declaration, startLoc);
  9238. declaration.declare = true;
  9239. }
  9240. return declaration;
  9241. }
  9242. parseClassId(node, isStatement, optionalId, bindingType) {
  9243. if ((!isStatement || optionalId) && this.isContextual(113)) {
  9244. return;
  9245. }
  9246. super.parseClassId(node, isStatement, optionalId, node.declare ? 1024 : 8331);
  9247. const typeParameters = this.tsTryParseTypeParameters(this.tsParseInOutConstModifiers);
  9248. if (typeParameters) node.typeParameters = typeParameters;
  9249. }
  9250. parseClassPropertyAnnotation(node) {
  9251. if (!node.optional) {
  9252. if (this.eat(35)) {
  9253. node.definite = true;
  9254. } else if (this.eat(17)) {
  9255. node.optional = true;
  9256. }
  9257. }
  9258. const type = this.tsTryParseTypeAnnotation();
  9259. if (type) node.typeAnnotation = type;
  9260. }
  9261. parseClassProperty(node) {
  9262. this.parseClassPropertyAnnotation(node);
  9263. if (this.state.isAmbientContext && !(node.readonly && !node.typeAnnotation) && this.match(29)) {
  9264. this.raise(TSErrors.DeclareClassFieldHasInitializer, this.state.startLoc);
  9265. }
  9266. if (node.abstract && this.match(29)) {
  9267. const {
  9268. key
  9269. } = node;
  9270. this.raise(TSErrors.AbstractPropertyHasInitializer, this.state.startLoc, {
  9271. propertyName: key.type === "Identifier" && !node.computed ? key.name : `[${this.input.slice(key.start, key.end)}]`
  9272. });
  9273. }
  9274. return super.parseClassProperty(node);
  9275. }
  9276. parseClassPrivateProperty(node) {
  9277. if (node.abstract) {
  9278. this.raise(TSErrors.PrivateElementHasAbstract, node);
  9279. }
  9280. if (node.accessibility) {
  9281. this.raise(TSErrors.PrivateElementHasAccessibility, node, {
  9282. modifier: node.accessibility
  9283. });
  9284. }
  9285. this.parseClassPropertyAnnotation(node);
  9286. return super.parseClassPrivateProperty(node);
  9287. }
  9288. parseClassAccessorProperty(node) {
  9289. this.parseClassPropertyAnnotation(node);
  9290. if (node.optional) {
  9291. this.raise(TSErrors.AccessorCannotBeOptional, node);
  9292. }
  9293. return super.parseClassAccessorProperty(node);
  9294. }
  9295. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  9296. const typeParameters = this.tsTryParseTypeParameters(this.tsParseConstModifier);
  9297. if (typeParameters && isConstructor) {
  9298. this.raise(TSErrors.ConstructorHasTypeParameters, typeParameters);
  9299. }
  9300. const {
  9301. declare = false,
  9302. kind
  9303. } = method;
  9304. if (declare && (kind === "get" || kind === "set")) {
  9305. this.raise(TSErrors.DeclareAccessor, method, {
  9306. kind
  9307. });
  9308. }
  9309. if (typeParameters) method.typeParameters = typeParameters;
  9310. super.pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper);
  9311. }
  9312. pushClassPrivateMethod(classBody, method, isGenerator, isAsync) {
  9313. const typeParameters = this.tsTryParseTypeParameters(this.tsParseConstModifier);
  9314. if (typeParameters) method.typeParameters = typeParameters;
  9315. super.pushClassPrivateMethod(classBody, method, isGenerator, isAsync);
  9316. }
  9317. declareClassPrivateMethodInScope(node, kind) {
  9318. if (node.type === "TSDeclareMethod") return;
  9319. if (node.type === "MethodDefinition" && !hasOwnProperty.call(node.value, "body")) {
  9320. return;
  9321. }
  9322. super.declareClassPrivateMethodInScope(node, kind);
  9323. }
  9324. parseClassSuper(node) {
  9325. super.parseClassSuper(node);
  9326. if (node.superClass && (this.match(47) || this.match(51))) {
  9327. node.superTypeParameters = this.tsParseTypeArgumentsInExpression();
  9328. }
  9329. if (this.eatContextual(113)) {
  9330. node.implements = this.tsParseHeritageClause("implements");
  9331. }
  9332. }
  9333. parseObjPropValue(prop, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors) {
  9334. const typeParameters = this.tsTryParseTypeParameters(this.tsParseConstModifier);
  9335. if (typeParameters) prop.typeParameters = typeParameters;
  9336. return super.parseObjPropValue(prop, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors);
  9337. }
  9338. parseFunctionParams(node, isConstructor) {
  9339. const typeParameters = this.tsTryParseTypeParameters(this.tsParseConstModifier);
  9340. if (typeParameters) node.typeParameters = typeParameters;
  9341. super.parseFunctionParams(node, isConstructor);
  9342. }
  9343. parseVarId(decl, kind) {
  9344. super.parseVarId(decl, kind);
  9345. if (decl.id.type === "Identifier" && !this.hasPrecedingLineBreak() && this.eat(35)) {
  9346. decl.definite = true;
  9347. }
  9348. const type = this.tsTryParseTypeAnnotation();
  9349. if (type) {
  9350. decl.id.typeAnnotation = type;
  9351. this.resetEndLocation(decl.id);
  9352. }
  9353. }
  9354. parseAsyncArrowFromCallExpression(node, call) {
  9355. if (this.match(14)) {
  9356. node.returnType = this.tsParseTypeAnnotation();
  9357. }
  9358. return super.parseAsyncArrowFromCallExpression(node, call);
  9359. }
  9360. parseMaybeAssign(refExpressionErrors, afterLeftParse) {
  9361. var _jsx, _jsx2, _typeCast, _jsx3, _typeCast2;
  9362. let state;
  9363. let jsx;
  9364. let typeCast;
  9365. if (this.hasPlugin("jsx") && (this.match(142) || this.match(47))) {
  9366. state = this.state.clone();
  9367. jsx = this.tryParse(() => super.parseMaybeAssign(refExpressionErrors, afterLeftParse), state);
  9368. if (!jsx.error) return jsx.node;
  9369. const {
  9370. context
  9371. } = this.state;
  9372. const currentContext = context[context.length - 1];
  9373. if (currentContext === types.j_oTag || currentContext === types.j_expr) {
  9374. context.pop();
  9375. }
  9376. }
  9377. if (!((_jsx = jsx) != null && _jsx.error) && !this.match(47)) {
  9378. return super.parseMaybeAssign(refExpressionErrors, afterLeftParse);
  9379. }
  9380. if (!state || state === this.state) state = this.state.clone();
  9381. let typeParameters;
  9382. const arrow = this.tryParse(abort => {
  9383. var _expr$extra, _typeParameters;
  9384. typeParameters = this.tsParseTypeParameters(this.tsParseConstModifier);
  9385. const expr = super.parseMaybeAssign(refExpressionErrors, afterLeftParse);
  9386. if (expr.type !== "ArrowFunctionExpression" || (_expr$extra = expr.extra) != null && _expr$extra.parenthesized) {
  9387. abort();
  9388. }
  9389. if (((_typeParameters = typeParameters) == null ? void 0 : _typeParameters.params.length) !== 0) {
  9390. this.resetStartLocationFromNode(expr, typeParameters);
  9391. }
  9392. expr.typeParameters = typeParameters;
  9393. return expr;
  9394. }, state);
  9395. if (!arrow.error && !arrow.aborted) {
  9396. if (typeParameters) this.reportReservedArrowTypeParam(typeParameters);
  9397. return arrow.node;
  9398. }
  9399. if (!jsx) {
  9400. assert(!this.hasPlugin("jsx"));
  9401. typeCast = this.tryParse(() => super.parseMaybeAssign(refExpressionErrors, afterLeftParse), state);
  9402. if (!typeCast.error) return typeCast.node;
  9403. }
  9404. if ((_jsx2 = jsx) != null && _jsx2.node) {
  9405. this.state = jsx.failState;
  9406. return jsx.node;
  9407. }
  9408. if (arrow.node) {
  9409. this.state = arrow.failState;
  9410. if (typeParameters) this.reportReservedArrowTypeParam(typeParameters);
  9411. return arrow.node;
  9412. }
  9413. if ((_typeCast = typeCast) != null && _typeCast.node) {
  9414. this.state = typeCast.failState;
  9415. return typeCast.node;
  9416. }
  9417. throw ((_jsx3 = jsx) == null ? void 0 : _jsx3.error) || arrow.error || ((_typeCast2 = typeCast) == null ? void 0 : _typeCast2.error);
  9418. }
  9419. reportReservedArrowTypeParam(node) {
  9420. var _node$extra;
  9421. if (node.params.length === 1 && !node.params[0].constraint && !((_node$extra = node.extra) != null && _node$extra.trailingComma) && this.getPluginOption("typescript", "disallowAmbiguousJSXLike")) {
  9422. this.raise(TSErrors.ReservedArrowTypeParam, node);
  9423. }
  9424. }
  9425. parseMaybeUnary(refExpressionErrors, sawUnary) {
  9426. if (!this.hasPlugin("jsx") && this.match(47)) {
  9427. return this.tsParseTypeAssertion();
  9428. }
  9429. return super.parseMaybeUnary(refExpressionErrors, sawUnary);
  9430. }
  9431. parseArrow(node) {
  9432. if (this.match(14)) {
  9433. const result = this.tryParse(abort => {
  9434. const returnType = this.tsParseTypeOrTypePredicateAnnotation(14);
  9435. if (this.canInsertSemicolon() || !this.match(19)) abort();
  9436. return returnType;
  9437. });
  9438. if (result.aborted) return;
  9439. if (!result.thrown) {
  9440. if (result.error) this.state = result.failState;
  9441. node.returnType = result.node;
  9442. }
  9443. }
  9444. return super.parseArrow(node);
  9445. }
  9446. parseAssignableListItemTypes(param, flags) {
  9447. if (!(flags & 2)) return param;
  9448. if (this.eat(17)) {
  9449. param.optional = true;
  9450. }
  9451. const type = this.tsTryParseTypeAnnotation();
  9452. if (type) param.typeAnnotation = type;
  9453. this.resetEndLocation(param);
  9454. return param;
  9455. }
  9456. isAssignable(node, isBinding) {
  9457. switch (node.type) {
  9458. case "TSTypeCastExpression":
  9459. return this.isAssignable(node.expression, isBinding);
  9460. case "TSParameterProperty":
  9461. return true;
  9462. default:
  9463. return super.isAssignable(node, isBinding);
  9464. }
  9465. }
  9466. toAssignable(node, isLHS = false) {
  9467. switch (node.type) {
  9468. case "ParenthesizedExpression":
  9469. this.toAssignableParenthesizedExpression(node, isLHS);
  9470. break;
  9471. case "TSAsExpression":
  9472. case "TSSatisfiesExpression":
  9473. case "TSNonNullExpression":
  9474. case "TSTypeAssertion":
  9475. if (isLHS) {
  9476. this.expressionScope.recordArrowParameterBindingError(TSErrors.UnexpectedTypeCastInParameter, node);
  9477. } else {
  9478. this.raise(TSErrors.UnexpectedTypeCastInParameter, node);
  9479. }
  9480. this.toAssignable(node.expression, isLHS);
  9481. break;
  9482. case "AssignmentExpression":
  9483. if (!isLHS && node.left.type === "TSTypeCastExpression") {
  9484. node.left = this.typeCastToParameter(node.left);
  9485. }
  9486. default:
  9487. super.toAssignable(node, isLHS);
  9488. }
  9489. }
  9490. toAssignableParenthesizedExpression(node, isLHS) {
  9491. switch (node.expression.type) {
  9492. case "TSAsExpression":
  9493. case "TSSatisfiesExpression":
  9494. case "TSNonNullExpression":
  9495. case "TSTypeAssertion":
  9496. case "ParenthesizedExpression":
  9497. this.toAssignable(node.expression, isLHS);
  9498. break;
  9499. default:
  9500. super.toAssignable(node, isLHS);
  9501. }
  9502. }
  9503. checkToRestConversion(node, allowPattern) {
  9504. switch (node.type) {
  9505. case "TSAsExpression":
  9506. case "TSSatisfiesExpression":
  9507. case "TSTypeAssertion":
  9508. case "TSNonNullExpression":
  9509. this.checkToRestConversion(node.expression, false);
  9510. break;
  9511. default:
  9512. super.checkToRestConversion(node, allowPattern);
  9513. }
  9514. }
  9515. isValidLVal(type, isUnparenthesizedInAssign, binding) {
  9516. return getOwn({
  9517. TSTypeCastExpression: true,
  9518. TSParameterProperty: "parameter",
  9519. TSNonNullExpression: "expression",
  9520. TSInstantiationExpression: "expression",
  9521. TSAsExpression: (binding !== 64 || !isUnparenthesizedInAssign) && ["expression", true],
  9522. TSSatisfiesExpression: (binding !== 64 || !isUnparenthesizedInAssign) && ["expression", true],
  9523. TSTypeAssertion: (binding !== 64 || !isUnparenthesizedInAssign) && ["expression", true]
  9524. }, type) || super.isValidLVal(type, isUnparenthesizedInAssign, binding);
  9525. }
  9526. parseBindingAtom() {
  9527. if (this.state.type === 78) {
  9528. return this.parseIdentifier(true);
  9529. }
  9530. return super.parseBindingAtom();
  9531. }
  9532. parseMaybeDecoratorArguments(expr) {
  9533. if (this.match(47) || this.match(51)) {
  9534. const typeArguments = this.tsParseTypeArgumentsInExpression();
  9535. if (this.match(10)) {
  9536. const call = super.parseMaybeDecoratorArguments(expr);
  9537. call.typeParameters = typeArguments;
  9538. return call;
  9539. }
  9540. this.unexpected(null, 10);
  9541. }
  9542. return super.parseMaybeDecoratorArguments(expr);
  9543. }
  9544. checkCommaAfterRest(close) {
  9545. if (this.state.isAmbientContext && this.match(12) && this.lookaheadCharCode() === close) {
  9546. this.next();
  9547. return false;
  9548. }
  9549. return super.checkCommaAfterRest(close);
  9550. }
  9551. isClassMethod() {
  9552. return this.match(47) || super.isClassMethod();
  9553. }
  9554. isClassProperty() {
  9555. return this.match(35) || this.match(14) || super.isClassProperty();
  9556. }
  9557. parseMaybeDefault(startLoc, left) {
  9558. const node = super.parseMaybeDefault(startLoc, left);
  9559. if (node.type === "AssignmentPattern" && node.typeAnnotation && node.right.start < node.typeAnnotation.start) {
  9560. this.raise(TSErrors.TypeAnnotationAfterAssign, node.typeAnnotation);
  9561. }
  9562. return node;
  9563. }
  9564. getTokenFromCode(code) {
  9565. if (this.state.inType) {
  9566. if (code === 62) {
  9567. this.finishOp(48, 1);
  9568. return;
  9569. }
  9570. if (code === 60) {
  9571. this.finishOp(47, 1);
  9572. return;
  9573. }
  9574. }
  9575. super.getTokenFromCode(code);
  9576. }
  9577. reScan_lt_gt() {
  9578. const {
  9579. type
  9580. } = this.state;
  9581. if (type === 47) {
  9582. this.state.pos -= 1;
  9583. this.readToken_lt();
  9584. } else if (type === 48) {
  9585. this.state.pos -= 1;
  9586. this.readToken_gt();
  9587. }
  9588. }
  9589. reScan_lt() {
  9590. const {
  9591. type
  9592. } = this.state;
  9593. if (type === 51) {
  9594. this.state.pos -= 2;
  9595. this.finishOp(47, 1);
  9596. return 47;
  9597. }
  9598. return type;
  9599. }
  9600. toAssignableList(exprList, trailingCommaLoc, isLHS) {
  9601. for (let i = 0; i < exprList.length; i++) {
  9602. const expr = exprList[i];
  9603. if ((expr == null ? void 0 : expr.type) === "TSTypeCastExpression") {
  9604. exprList[i] = this.typeCastToParameter(expr);
  9605. }
  9606. }
  9607. super.toAssignableList(exprList, trailingCommaLoc, isLHS);
  9608. }
  9609. typeCastToParameter(node) {
  9610. node.expression.typeAnnotation = node.typeAnnotation;
  9611. this.resetEndLocation(node.expression, node.typeAnnotation.loc.end);
  9612. return node.expression;
  9613. }
  9614. shouldParseArrow(params) {
  9615. if (this.match(14)) {
  9616. return params.every(expr => this.isAssignable(expr, true));
  9617. }
  9618. return super.shouldParseArrow(params);
  9619. }
  9620. shouldParseAsyncArrow() {
  9621. return this.match(14) || super.shouldParseAsyncArrow();
  9622. }
  9623. canHaveLeadingDecorator() {
  9624. return super.canHaveLeadingDecorator() || this.isAbstractClass();
  9625. }
  9626. jsxParseOpeningElementAfterName(node) {
  9627. if (this.match(47) || this.match(51)) {
  9628. const typeArguments = this.tsTryParseAndCatch(() => this.tsParseTypeArgumentsInExpression());
  9629. if (typeArguments) node.typeParameters = typeArguments;
  9630. }
  9631. return super.jsxParseOpeningElementAfterName(node);
  9632. }
  9633. getGetterSetterExpectedParamCount(method) {
  9634. const baseCount = super.getGetterSetterExpectedParamCount(method);
  9635. const params = this.getObjectOrClassMethodParams(method);
  9636. const firstParam = params[0];
  9637. const hasContextParam = firstParam && this.isThisParam(firstParam);
  9638. return hasContextParam ? baseCount + 1 : baseCount;
  9639. }
  9640. parseCatchClauseParam() {
  9641. const param = super.parseCatchClauseParam();
  9642. const type = this.tsTryParseTypeAnnotation();
  9643. if (type) {
  9644. param.typeAnnotation = type;
  9645. this.resetEndLocation(param);
  9646. }
  9647. return param;
  9648. }
  9649. tsInAmbientContext(cb) {
  9650. const oldIsAmbientContext = this.state.isAmbientContext;
  9651. this.state.isAmbientContext = true;
  9652. try {
  9653. return cb();
  9654. } finally {
  9655. this.state.isAmbientContext = oldIsAmbientContext;
  9656. }
  9657. }
  9658. parseClass(node, isStatement, optionalId) {
  9659. const oldInAbstractClass = this.state.inAbstractClass;
  9660. this.state.inAbstractClass = !!node.abstract;
  9661. try {
  9662. return super.parseClass(node, isStatement, optionalId);
  9663. } finally {
  9664. this.state.inAbstractClass = oldInAbstractClass;
  9665. }
  9666. }
  9667. tsParseAbstractDeclaration(node, decorators) {
  9668. if (this.match(80)) {
  9669. node.abstract = true;
  9670. return this.maybeTakeDecorators(decorators, this.parseClass(node, true, false));
  9671. } else if (this.isContextual(129)) {
  9672. if (!this.hasFollowingLineBreak()) {
  9673. node.abstract = true;
  9674. this.raise(TSErrors.NonClassMethodPropertyHasAbstractModifer, node);
  9675. return this.tsParseInterfaceDeclaration(node);
  9676. }
  9677. } else {
  9678. this.unexpected(null, 80);
  9679. }
  9680. }
  9681. parseMethod(node, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope) {
  9682. const method = super.parseMethod(node, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope);
  9683. if (method.abstract) {
  9684. const hasBody = this.hasPlugin("estree") ? !!method.value.body : !!method.body;
  9685. if (hasBody) {
  9686. const {
  9687. key
  9688. } = method;
  9689. this.raise(TSErrors.AbstractMethodHasImplementation, method, {
  9690. methodName: key.type === "Identifier" && !method.computed ? key.name : `[${this.input.slice(key.start, key.end)}]`
  9691. });
  9692. }
  9693. }
  9694. return method;
  9695. }
  9696. tsParseTypeParameterName() {
  9697. const typeName = this.parseIdentifier();
  9698. return typeName.name;
  9699. }
  9700. shouldParseAsAmbientContext() {
  9701. return !!this.getPluginOption("typescript", "dts");
  9702. }
  9703. parse() {
  9704. if (this.shouldParseAsAmbientContext()) {
  9705. this.state.isAmbientContext = true;
  9706. }
  9707. return super.parse();
  9708. }
  9709. getExpression() {
  9710. if (this.shouldParseAsAmbientContext()) {
  9711. this.state.isAmbientContext = true;
  9712. }
  9713. return super.getExpression();
  9714. }
  9715. parseExportSpecifier(node, isString, isInTypeExport, isMaybeTypeOnly) {
  9716. if (!isString && isMaybeTypeOnly) {
  9717. this.parseTypeOnlyImportExportSpecifier(node, false, isInTypeExport);
  9718. return this.finishNode(node, "ExportSpecifier");
  9719. }
  9720. node.exportKind = "value";
  9721. return super.parseExportSpecifier(node, isString, isInTypeExport, isMaybeTypeOnly);
  9722. }
  9723. parseImportSpecifier(specifier, importedIsString, isInTypeOnlyImport, isMaybeTypeOnly, bindingType) {
  9724. if (!importedIsString && isMaybeTypeOnly) {
  9725. this.parseTypeOnlyImportExportSpecifier(specifier, true, isInTypeOnlyImport);
  9726. return this.finishNode(specifier, "ImportSpecifier");
  9727. }
  9728. specifier.importKind = "value";
  9729. return super.parseImportSpecifier(specifier, importedIsString, isInTypeOnlyImport, isMaybeTypeOnly, isInTypeOnlyImport ? 4098 : 4096);
  9730. }
  9731. parseTypeOnlyImportExportSpecifier(node, isImport, isInTypeOnlyImportExport) {
  9732. const leftOfAsKey = isImport ? "imported" : "local";
  9733. const rightOfAsKey = isImport ? "local" : "exported";
  9734. let leftOfAs = node[leftOfAsKey];
  9735. let rightOfAs;
  9736. let hasTypeSpecifier = false;
  9737. let canParseAsKeyword = true;
  9738. const loc = leftOfAs.loc.start;
  9739. if (this.isContextual(93)) {
  9740. const firstAs = this.parseIdentifier();
  9741. if (this.isContextual(93)) {
  9742. const secondAs = this.parseIdentifier();
  9743. if (tokenIsKeywordOrIdentifier(this.state.type)) {
  9744. hasTypeSpecifier = true;
  9745. leftOfAs = firstAs;
  9746. rightOfAs = isImport ? this.parseIdentifier() : this.parseModuleExportName();
  9747. canParseAsKeyword = false;
  9748. } else {
  9749. rightOfAs = secondAs;
  9750. canParseAsKeyword = false;
  9751. }
  9752. } else if (tokenIsKeywordOrIdentifier(this.state.type)) {
  9753. canParseAsKeyword = false;
  9754. rightOfAs = isImport ? this.parseIdentifier() : this.parseModuleExportName();
  9755. } else {
  9756. hasTypeSpecifier = true;
  9757. leftOfAs = firstAs;
  9758. }
  9759. } else if (tokenIsKeywordOrIdentifier(this.state.type)) {
  9760. hasTypeSpecifier = true;
  9761. if (isImport) {
  9762. leftOfAs = this.parseIdentifier(true);
  9763. if (!this.isContextual(93)) {
  9764. this.checkReservedWord(leftOfAs.name, leftOfAs.loc.start, true, true);
  9765. }
  9766. } else {
  9767. leftOfAs = this.parseModuleExportName();
  9768. }
  9769. }
  9770. if (hasTypeSpecifier && isInTypeOnlyImportExport) {
  9771. this.raise(isImport ? TSErrors.TypeModifierIsUsedInTypeImports : TSErrors.TypeModifierIsUsedInTypeExports, loc);
  9772. }
  9773. node[leftOfAsKey] = leftOfAs;
  9774. node[rightOfAsKey] = rightOfAs;
  9775. const kindKey = isImport ? "importKind" : "exportKind";
  9776. node[kindKey] = hasTypeSpecifier ? "type" : "value";
  9777. if (canParseAsKeyword && this.eatContextual(93)) {
  9778. node[rightOfAsKey] = isImport ? this.parseIdentifier() : this.parseModuleExportName();
  9779. }
  9780. if (!node[rightOfAsKey]) {
  9781. node[rightOfAsKey] = cloneIdentifier(node[leftOfAsKey]);
  9782. }
  9783. if (isImport) {
  9784. this.checkIdentifier(node[rightOfAsKey], hasTypeSpecifier ? 4098 : 4096);
  9785. }
  9786. }
  9787. };
  9788. function isPossiblyLiteralEnum(expression) {
  9789. if (expression.type !== "MemberExpression") return false;
  9790. const {
  9791. computed,
  9792. property
  9793. } = expression;
  9794. if (computed && property.type !== "StringLiteral" && (property.type !== "TemplateLiteral" || property.expressions.length > 0)) {
  9795. return false;
  9796. }
  9797. return isUncomputedMemberExpressionChain(expression.object);
  9798. }
  9799. function isValidAmbientConstInitializer(expression, estree) {
  9800. var _expression$extra;
  9801. const {
  9802. type
  9803. } = expression;
  9804. if ((_expression$extra = expression.extra) != null && _expression$extra.parenthesized) {
  9805. return false;
  9806. }
  9807. if (estree) {
  9808. if (type === "Literal") {
  9809. const {
  9810. value
  9811. } = expression;
  9812. if (typeof value === "string" || typeof value === "boolean") {
  9813. return true;
  9814. }
  9815. }
  9816. } else {
  9817. if (type === "StringLiteral" || type === "BooleanLiteral") {
  9818. return true;
  9819. }
  9820. }
  9821. if (isNumber(expression, estree) || isNegativeNumber(expression, estree)) {
  9822. return true;
  9823. }
  9824. if (type === "TemplateLiteral" && expression.expressions.length === 0) {
  9825. return true;
  9826. }
  9827. if (isPossiblyLiteralEnum(expression)) {
  9828. return true;
  9829. }
  9830. return false;
  9831. }
  9832. function isNumber(expression, estree) {
  9833. if (estree) {
  9834. return expression.type === "Literal" && (typeof expression.value === "number" || "bigint" in expression);
  9835. }
  9836. return expression.type === "NumericLiteral" || expression.type === "BigIntLiteral";
  9837. }
  9838. function isNegativeNumber(expression, estree) {
  9839. if (expression.type === "UnaryExpression") {
  9840. const {
  9841. operator,
  9842. argument
  9843. } = expression;
  9844. if (operator === "-" && isNumber(argument, estree)) {
  9845. return true;
  9846. }
  9847. }
  9848. return false;
  9849. }
  9850. function isUncomputedMemberExpressionChain(expression) {
  9851. if (expression.type === "Identifier") return true;
  9852. if (expression.type !== "MemberExpression" || expression.computed) {
  9853. return false;
  9854. }
  9855. return isUncomputedMemberExpressionChain(expression.object);
  9856. }
  9857. const PlaceholderErrors = ParseErrorEnum`placeholders`({
  9858. ClassNameIsRequired: "A class name is required.",
  9859. UnexpectedSpace: "Unexpected space in placeholder."
  9860. });
  9861. var placeholders = superClass => class PlaceholdersParserMixin extends superClass {
  9862. parsePlaceholder(expectedNode) {
  9863. if (this.match(144)) {
  9864. const node = this.startNode();
  9865. this.next();
  9866. this.assertNoSpace();
  9867. node.name = super.parseIdentifier(true);
  9868. this.assertNoSpace();
  9869. this.expect(144);
  9870. return this.finishPlaceholder(node, expectedNode);
  9871. }
  9872. }
  9873. finishPlaceholder(node, expectedNode) {
  9874. let placeholder = node;
  9875. if (!placeholder.expectedNode || !placeholder.type) {
  9876. placeholder = this.finishNode(placeholder, "Placeholder");
  9877. }
  9878. placeholder.expectedNode = expectedNode;
  9879. return placeholder;
  9880. }
  9881. getTokenFromCode(code) {
  9882. if (code === 37 && this.input.charCodeAt(this.state.pos + 1) === 37) {
  9883. this.finishOp(144, 2);
  9884. } else {
  9885. super.getTokenFromCode(code);
  9886. }
  9887. }
  9888. parseExprAtom(refExpressionErrors) {
  9889. return this.parsePlaceholder("Expression") || super.parseExprAtom(refExpressionErrors);
  9890. }
  9891. parseIdentifier(liberal) {
  9892. return this.parsePlaceholder("Identifier") || super.parseIdentifier(liberal);
  9893. }
  9894. checkReservedWord(word, startLoc, checkKeywords, isBinding) {
  9895. if (word !== undefined) {
  9896. super.checkReservedWord(word, startLoc, checkKeywords, isBinding);
  9897. }
  9898. }
  9899. parseBindingAtom() {
  9900. return this.parsePlaceholder("Pattern") || super.parseBindingAtom();
  9901. }
  9902. isValidLVal(type, isParenthesized, binding) {
  9903. return type === "Placeholder" || super.isValidLVal(type, isParenthesized, binding);
  9904. }
  9905. toAssignable(node, isLHS) {
  9906. if (node && node.type === "Placeholder" && node.expectedNode === "Expression") {
  9907. node.expectedNode = "Pattern";
  9908. } else {
  9909. super.toAssignable(node, isLHS);
  9910. }
  9911. }
  9912. chStartsBindingIdentifier(ch, pos) {
  9913. if (super.chStartsBindingIdentifier(ch, pos)) {
  9914. return true;
  9915. }
  9916. const nextToken = this.lookahead();
  9917. if (nextToken.type === 144) {
  9918. return true;
  9919. }
  9920. return false;
  9921. }
  9922. verifyBreakContinue(node, isBreak) {
  9923. if (node.label && node.label.type === "Placeholder") return;
  9924. super.verifyBreakContinue(node, isBreak);
  9925. }
  9926. parseExpressionStatement(node, expr) {
  9927. var _expr$extra;
  9928. if (expr.type !== "Placeholder" || (_expr$extra = expr.extra) != null && _expr$extra.parenthesized) {
  9929. return super.parseExpressionStatement(node, expr);
  9930. }
  9931. if (this.match(14)) {
  9932. const stmt = node;
  9933. stmt.label = this.finishPlaceholder(expr, "Identifier");
  9934. this.next();
  9935. stmt.body = super.parseStatementOrSloppyAnnexBFunctionDeclaration();
  9936. return this.finishNode(stmt, "LabeledStatement");
  9937. }
  9938. this.semicolon();
  9939. const stmtPlaceholder = node;
  9940. stmtPlaceholder.name = expr.name;
  9941. return this.finishPlaceholder(stmtPlaceholder, "Statement");
  9942. }
  9943. parseBlock(allowDirectives, createNewLexicalScope, afterBlockParse) {
  9944. return this.parsePlaceholder("BlockStatement") || super.parseBlock(allowDirectives, createNewLexicalScope, afterBlockParse);
  9945. }
  9946. parseFunctionId(requireId) {
  9947. return this.parsePlaceholder("Identifier") || super.parseFunctionId(requireId);
  9948. }
  9949. parseClass(node, isStatement, optionalId) {
  9950. const type = isStatement ? "ClassDeclaration" : "ClassExpression";
  9951. this.next();
  9952. const oldStrict = this.state.strict;
  9953. const placeholder = this.parsePlaceholder("Identifier");
  9954. if (placeholder) {
  9955. if (this.match(81) || this.match(144) || this.match(5)) {
  9956. node.id = placeholder;
  9957. } else if (optionalId || !isStatement) {
  9958. node.id = null;
  9959. node.body = this.finishPlaceholder(placeholder, "ClassBody");
  9960. return this.finishNode(node, type);
  9961. } else {
  9962. throw this.raise(PlaceholderErrors.ClassNameIsRequired, this.state.startLoc);
  9963. }
  9964. } else {
  9965. this.parseClassId(node, isStatement, optionalId);
  9966. }
  9967. super.parseClassSuper(node);
  9968. node.body = this.parsePlaceholder("ClassBody") || super.parseClassBody(!!node.superClass, oldStrict);
  9969. return this.finishNode(node, type);
  9970. }
  9971. parseExport(node, decorators) {
  9972. const placeholder = this.parsePlaceholder("Identifier");
  9973. if (!placeholder) return super.parseExport(node, decorators);
  9974. const node2 = node;
  9975. if (!this.isContextual(98) && !this.match(12)) {
  9976. node2.specifiers = [];
  9977. node2.source = null;
  9978. node2.declaration = this.finishPlaceholder(placeholder, "Declaration");
  9979. return this.finishNode(node2, "ExportNamedDeclaration");
  9980. }
  9981. this.expectPlugin("exportDefaultFrom");
  9982. const specifier = this.startNode();
  9983. specifier.exported = placeholder;
  9984. node2.specifiers = [this.finishNode(specifier, "ExportDefaultSpecifier")];
  9985. return super.parseExport(node2, decorators);
  9986. }
  9987. isExportDefaultSpecifier() {
  9988. if (this.match(65)) {
  9989. const next = this.nextTokenStart();
  9990. if (this.isUnparsedContextual(next, "from")) {
  9991. if (this.input.startsWith(tokenLabelName(144), this.nextTokenStartSince(next + 4))) {
  9992. return true;
  9993. }
  9994. }
  9995. }
  9996. return super.isExportDefaultSpecifier();
  9997. }
  9998. maybeParseExportDefaultSpecifier(node, maybeDefaultIdentifier) {
  9999. var _specifiers;
  10000. if ((_specifiers = node.specifiers) != null && _specifiers.length) {
  10001. return true;
  10002. }
  10003. return super.maybeParseExportDefaultSpecifier(node, maybeDefaultIdentifier);
  10004. }
  10005. checkExport(node) {
  10006. const {
  10007. specifiers
  10008. } = node;
  10009. if (specifiers != null && specifiers.length) {
  10010. node.specifiers = specifiers.filter(node => node.exported.type === "Placeholder");
  10011. }
  10012. super.checkExport(node);
  10013. node.specifiers = specifiers;
  10014. }
  10015. parseImport(node) {
  10016. const placeholder = this.parsePlaceholder("Identifier");
  10017. if (!placeholder) return super.parseImport(node);
  10018. node.specifiers = [];
  10019. if (!this.isContextual(98) && !this.match(12)) {
  10020. node.source = this.finishPlaceholder(placeholder, "StringLiteral");
  10021. this.semicolon();
  10022. return this.finishNode(node, "ImportDeclaration");
  10023. }
  10024. const specifier = this.startNodeAtNode(placeholder);
  10025. specifier.local = placeholder;
  10026. node.specifiers.push(this.finishNode(specifier, "ImportDefaultSpecifier"));
  10027. if (this.eat(12)) {
  10028. const hasStarImport = this.maybeParseStarImportSpecifier(node);
  10029. if (!hasStarImport) this.parseNamedImportSpecifiers(node);
  10030. }
  10031. this.expectContextual(98);
  10032. node.source = this.parseImportSource();
  10033. this.semicolon();
  10034. return this.finishNode(node, "ImportDeclaration");
  10035. }
  10036. parseImportSource() {
  10037. return this.parsePlaceholder("StringLiteral") || super.parseImportSource();
  10038. }
  10039. assertNoSpace() {
  10040. if (this.state.start > this.state.lastTokEndLoc.index) {
  10041. this.raise(PlaceholderErrors.UnexpectedSpace, this.state.lastTokEndLoc);
  10042. }
  10043. }
  10044. };
  10045. var v8intrinsic = superClass => class V8IntrinsicMixin extends superClass {
  10046. parseV8Intrinsic() {
  10047. if (this.match(54)) {
  10048. const v8IntrinsicStartLoc = this.state.startLoc;
  10049. const node = this.startNode();
  10050. this.next();
  10051. if (tokenIsIdentifier(this.state.type)) {
  10052. const name = this.parseIdentifierName();
  10053. const identifier = this.createIdentifier(node, name);
  10054. identifier.type = "V8IntrinsicIdentifier";
  10055. if (this.match(10)) {
  10056. return identifier;
  10057. }
  10058. }
  10059. this.unexpected(v8IntrinsicStartLoc);
  10060. }
  10061. }
  10062. parseExprAtom(refExpressionErrors) {
  10063. return this.parseV8Intrinsic() || super.parseExprAtom(refExpressionErrors);
  10064. }
  10065. };
  10066. function hasPlugin(plugins, expectedConfig) {
  10067. const [expectedName, expectedOptions] = typeof expectedConfig === "string" ? [expectedConfig, {}] : expectedConfig;
  10068. const expectedKeys = Object.keys(expectedOptions);
  10069. const expectedOptionsIsEmpty = expectedKeys.length === 0;
  10070. return plugins.some(p => {
  10071. if (typeof p === "string") {
  10072. return expectedOptionsIsEmpty && p === expectedName;
  10073. } else {
  10074. const [pluginName, pluginOptions] = p;
  10075. if (pluginName !== expectedName) {
  10076. return false;
  10077. }
  10078. for (const key of expectedKeys) {
  10079. if (pluginOptions[key] !== expectedOptions[key]) {
  10080. return false;
  10081. }
  10082. }
  10083. return true;
  10084. }
  10085. });
  10086. }
  10087. function getPluginOption(plugins, name, option) {
  10088. const plugin = plugins.find(plugin => {
  10089. if (Array.isArray(plugin)) {
  10090. return plugin[0] === name;
  10091. } else {
  10092. return plugin === name;
  10093. }
  10094. });
  10095. if (plugin && Array.isArray(plugin) && plugin.length > 1) {
  10096. return plugin[1][option];
  10097. }
  10098. return null;
  10099. }
  10100. const PIPELINE_PROPOSALS = ["minimal", "fsharp", "hack", "smart"];
  10101. const TOPIC_TOKENS = ["^^", "@@", "^", "%", "#"];
  10102. function validatePlugins(plugins) {
  10103. if (hasPlugin(plugins, "decorators")) {
  10104. if (hasPlugin(plugins, "decorators-legacy")) {
  10105. throw new Error("Cannot use the decorators and decorators-legacy plugin together");
  10106. }
  10107. const decoratorsBeforeExport = getPluginOption(plugins, "decorators", "decoratorsBeforeExport");
  10108. if (decoratorsBeforeExport != null && typeof decoratorsBeforeExport !== "boolean") {
  10109. throw new Error("'decoratorsBeforeExport' must be a boolean, if specified.");
  10110. }
  10111. const allowCallParenthesized = getPluginOption(plugins, "decorators", "allowCallParenthesized");
  10112. if (allowCallParenthesized != null && typeof allowCallParenthesized !== "boolean") {
  10113. throw new Error("'allowCallParenthesized' must be a boolean.");
  10114. }
  10115. }
  10116. if (hasPlugin(plugins, "flow") && hasPlugin(plugins, "typescript")) {
  10117. throw new Error("Cannot combine flow and typescript plugins.");
  10118. }
  10119. if (hasPlugin(plugins, "placeholders") && hasPlugin(plugins, "v8intrinsic")) {
  10120. throw new Error("Cannot combine placeholders and v8intrinsic plugins.");
  10121. }
  10122. if (hasPlugin(plugins, "pipelineOperator")) {
  10123. const proposal = getPluginOption(plugins, "pipelineOperator", "proposal");
  10124. if (!PIPELINE_PROPOSALS.includes(proposal)) {
  10125. const proposalList = PIPELINE_PROPOSALS.map(p => `"${p}"`).join(", ");
  10126. throw new Error(`"pipelineOperator" requires "proposal" option whose value must be one of: ${proposalList}.`);
  10127. }
  10128. const recordAndTupleConfigItem = ["recordAndTuple", {
  10129. syntaxType: "hash"
  10130. }];
  10131. const tupleSyntaxIsHash = hasPlugin(plugins, recordAndTupleConfigItem);
  10132. if (proposal === "hack") {
  10133. if (hasPlugin(plugins, "placeholders")) {
  10134. throw new Error("Cannot combine placeholders plugin and Hack-style pipes.");
  10135. }
  10136. if (hasPlugin(plugins, "v8intrinsic")) {
  10137. throw new Error("Cannot combine v8intrinsic plugin and Hack-style pipes.");
  10138. }
  10139. const topicToken = getPluginOption(plugins, "pipelineOperator", "topicToken");
  10140. if (!TOPIC_TOKENS.includes(topicToken)) {
  10141. const tokenList = TOPIC_TOKENS.map(t => `"${t}"`).join(", ");
  10142. throw new Error(`"pipelineOperator" in "proposal": "hack" mode also requires a "topicToken" option whose value must be one of: ${tokenList}.`);
  10143. }
  10144. if (topicToken === "#" && tupleSyntaxIsHash) {
  10145. throw new Error(`Plugin conflict between \`["pipelineOperator", { proposal: "hack", topicToken: "#" }]\` and \`${JSON.stringify(recordAndTupleConfigItem)}\`.`);
  10146. }
  10147. } else if (proposal === "smart" && tupleSyntaxIsHash) {
  10148. throw new Error(`Plugin conflict between \`["pipelineOperator", { proposal: "smart" }]\` and \`${JSON.stringify(recordAndTupleConfigItem)}\`.`);
  10149. }
  10150. }
  10151. if (hasPlugin(plugins, "moduleAttributes")) {
  10152. {
  10153. if (hasPlugin(plugins, "importAssertions") || hasPlugin(plugins, "importAttributes")) {
  10154. throw new Error("Cannot combine importAssertions, importAttributes and moduleAttributes plugins.");
  10155. }
  10156. const moduleAttributesVersionPluginOption = getPluginOption(plugins, "moduleAttributes", "version");
  10157. if (moduleAttributesVersionPluginOption !== "may-2020") {
  10158. throw new Error("The 'moduleAttributes' plugin requires a 'version' option," + " representing the last proposal update. Currently, the" + " only supported value is 'may-2020'.");
  10159. }
  10160. }
  10161. }
  10162. if (hasPlugin(plugins, "importAssertions") && hasPlugin(plugins, "importAttributes")) {
  10163. throw new Error("Cannot combine importAssertions and importAttributes plugins.");
  10164. }
  10165. if (hasPlugin(plugins, "recordAndTuple")) {
  10166. const syntaxType = getPluginOption(plugins, "recordAndTuple", "syntaxType");
  10167. if (syntaxType != null) {
  10168. {
  10169. const RECORD_AND_TUPLE_SYNTAX_TYPES = ["hash", "bar"];
  10170. if (!RECORD_AND_TUPLE_SYNTAX_TYPES.includes(syntaxType)) {
  10171. throw new Error("The 'syntaxType' option of the 'recordAndTuple' plugin must be one of: " + RECORD_AND_TUPLE_SYNTAX_TYPES.map(p => `'${p}'`).join(", "));
  10172. }
  10173. }
  10174. }
  10175. }
  10176. if (hasPlugin(plugins, "asyncDoExpressions") && !hasPlugin(plugins, "doExpressions")) {
  10177. const error = new Error("'asyncDoExpressions' requires 'doExpressions', please add 'doExpressions' to parser plugins.");
  10178. error.missingPlugins = "doExpressions";
  10179. throw error;
  10180. }
  10181. if (hasPlugin(plugins, "optionalChainingAssign") && getPluginOption(plugins, "optionalChainingAssign", "version") !== "2023-07") {
  10182. throw new Error("The 'optionalChainingAssign' plugin requires a 'version' option," + " representing the last proposal update. Currently, the" + " only supported value is '2023-07'.");
  10183. }
  10184. }
  10185. const mixinPlugins = {
  10186. estree,
  10187. jsx,
  10188. flow,
  10189. typescript,
  10190. v8intrinsic,
  10191. placeholders
  10192. };
  10193. const mixinPluginNames = Object.keys(mixinPlugins);
  10194. const defaultOptions = {
  10195. sourceType: "script",
  10196. sourceFilename: undefined,
  10197. startColumn: 0,
  10198. startLine: 1,
  10199. allowAwaitOutsideFunction: false,
  10200. allowReturnOutsideFunction: false,
  10201. allowNewTargetOutsideFunction: false,
  10202. allowImportExportEverywhere: false,
  10203. allowSuperOutsideMethod: false,
  10204. allowUndeclaredExports: false,
  10205. plugins: [],
  10206. strictMode: null,
  10207. ranges: false,
  10208. tokens: false,
  10209. createImportExpressions: false,
  10210. createParenthesizedExpressions: false,
  10211. errorRecovery: false,
  10212. attachComment: true,
  10213. annexB: true
  10214. };
  10215. function getOptions(opts) {
  10216. if (opts == null) {
  10217. return Object.assign({}, defaultOptions);
  10218. }
  10219. if (opts.annexB != null && opts.annexB !== false) {
  10220. throw new Error("The `annexB` option can only be set to `false`.");
  10221. }
  10222. const options = {};
  10223. for (const key of Object.keys(defaultOptions)) {
  10224. var _opts$key;
  10225. options[key] = (_opts$key = opts[key]) != null ? _opts$key : defaultOptions[key];
  10226. }
  10227. return options;
  10228. }
  10229. class ExpressionParser extends LValParser {
  10230. checkProto(prop, isRecord, protoRef, refExpressionErrors) {
  10231. if (prop.type === "SpreadElement" || this.isObjectMethod(prop) || prop.computed || prop.shorthand) {
  10232. return;
  10233. }
  10234. const key = prop.key;
  10235. const name = key.type === "Identifier" ? key.name : key.value;
  10236. if (name === "__proto__") {
  10237. if (isRecord) {
  10238. this.raise(Errors.RecordNoProto, key);
  10239. return;
  10240. }
  10241. if (protoRef.used) {
  10242. if (refExpressionErrors) {
  10243. if (refExpressionErrors.doubleProtoLoc === null) {
  10244. refExpressionErrors.doubleProtoLoc = key.loc.start;
  10245. }
  10246. } else {
  10247. this.raise(Errors.DuplicateProto, key);
  10248. }
  10249. }
  10250. protoRef.used = true;
  10251. }
  10252. }
  10253. shouldExitDescending(expr, potentialArrowAt) {
  10254. return expr.type === "ArrowFunctionExpression" && expr.start === potentialArrowAt;
  10255. }
  10256. getExpression() {
  10257. this.enterInitialScopes();
  10258. this.nextToken();
  10259. const expr = this.parseExpression();
  10260. if (!this.match(139)) {
  10261. this.unexpected();
  10262. }
  10263. this.finalizeRemainingComments();
  10264. expr.comments = this.comments;
  10265. expr.errors = this.state.errors;
  10266. if (this.options.tokens) {
  10267. expr.tokens = this.tokens;
  10268. }
  10269. return expr;
  10270. }
  10271. parseExpression(disallowIn, refExpressionErrors) {
  10272. if (disallowIn) {
  10273. return this.disallowInAnd(() => this.parseExpressionBase(refExpressionErrors));
  10274. }
  10275. return this.allowInAnd(() => this.parseExpressionBase(refExpressionErrors));
  10276. }
  10277. parseExpressionBase(refExpressionErrors) {
  10278. const startLoc = this.state.startLoc;
  10279. const expr = this.parseMaybeAssign(refExpressionErrors);
  10280. if (this.match(12)) {
  10281. const node = this.startNodeAt(startLoc);
  10282. node.expressions = [expr];
  10283. while (this.eat(12)) {
  10284. node.expressions.push(this.parseMaybeAssign(refExpressionErrors));
  10285. }
  10286. this.toReferencedList(node.expressions);
  10287. return this.finishNode(node, "SequenceExpression");
  10288. }
  10289. return expr;
  10290. }
  10291. parseMaybeAssignDisallowIn(refExpressionErrors, afterLeftParse) {
  10292. return this.disallowInAnd(() => this.parseMaybeAssign(refExpressionErrors, afterLeftParse));
  10293. }
  10294. parseMaybeAssignAllowIn(refExpressionErrors, afterLeftParse) {
  10295. return this.allowInAnd(() => this.parseMaybeAssign(refExpressionErrors, afterLeftParse));
  10296. }
  10297. setOptionalParametersError(refExpressionErrors, resultError) {
  10298. var _resultError$loc;
  10299. refExpressionErrors.optionalParametersLoc = (_resultError$loc = resultError == null ? void 0 : resultError.loc) != null ? _resultError$loc : this.state.startLoc;
  10300. }
  10301. parseMaybeAssign(refExpressionErrors, afterLeftParse) {
  10302. const startLoc = this.state.startLoc;
  10303. if (this.isContextual(108)) {
  10304. if (this.prodParam.hasYield) {
  10305. let left = this.parseYield();
  10306. if (afterLeftParse) {
  10307. left = afterLeftParse.call(this, left, startLoc);
  10308. }
  10309. return left;
  10310. }
  10311. }
  10312. let ownExpressionErrors;
  10313. if (refExpressionErrors) {
  10314. ownExpressionErrors = false;
  10315. } else {
  10316. refExpressionErrors = new ExpressionErrors();
  10317. ownExpressionErrors = true;
  10318. }
  10319. const {
  10320. type
  10321. } = this.state;
  10322. if (type === 10 || tokenIsIdentifier(type)) {
  10323. this.state.potentialArrowAt = this.state.start;
  10324. }
  10325. let left = this.parseMaybeConditional(refExpressionErrors);
  10326. if (afterLeftParse) {
  10327. left = afterLeftParse.call(this, left, startLoc);
  10328. }
  10329. if (tokenIsAssignment(this.state.type)) {
  10330. const node = this.startNodeAt(startLoc);
  10331. const operator = this.state.value;
  10332. node.operator = operator;
  10333. if (this.match(29)) {
  10334. this.toAssignable(left, true);
  10335. node.left = left;
  10336. const startIndex = startLoc.index;
  10337. if (refExpressionErrors.doubleProtoLoc != null && refExpressionErrors.doubleProtoLoc.index >= startIndex) {
  10338. refExpressionErrors.doubleProtoLoc = null;
  10339. }
  10340. if (refExpressionErrors.shorthandAssignLoc != null && refExpressionErrors.shorthandAssignLoc.index >= startIndex) {
  10341. refExpressionErrors.shorthandAssignLoc = null;
  10342. }
  10343. if (refExpressionErrors.privateKeyLoc != null && refExpressionErrors.privateKeyLoc.index >= startIndex) {
  10344. this.checkDestructuringPrivate(refExpressionErrors);
  10345. refExpressionErrors.privateKeyLoc = null;
  10346. }
  10347. } else {
  10348. node.left = left;
  10349. }
  10350. this.next();
  10351. node.right = this.parseMaybeAssign();
  10352. this.checkLVal(left, {
  10353. in: this.finishNode(node, "AssignmentExpression")
  10354. });
  10355. return node;
  10356. } else if (ownExpressionErrors) {
  10357. this.checkExpressionErrors(refExpressionErrors, true);
  10358. }
  10359. return left;
  10360. }
  10361. parseMaybeConditional(refExpressionErrors) {
  10362. const startLoc = this.state.startLoc;
  10363. const potentialArrowAt = this.state.potentialArrowAt;
  10364. const expr = this.parseExprOps(refExpressionErrors);
  10365. if (this.shouldExitDescending(expr, potentialArrowAt)) {
  10366. return expr;
  10367. }
  10368. return this.parseConditional(expr, startLoc, refExpressionErrors);
  10369. }
  10370. parseConditional(expr, startLoc, refExpressionErrors) {
  10371. if (this.eat(17)) {
  10372. const node = this.startNodeAt(startLoc);
  10373. node.test = expr;
  10374. node.consequent = this.parseMaybeAssignAllowIn();
  10375. this.expect(14);
  10376. node.alternate = this.parseMaybeAssign();
  10377. return this.finishNode(node, "ConditionalExpression");
  10378. }
  10379. return expr;
  10380. }
  10381. parseMaybeUnaryOrPrivate(refExpressionErrors) {
  10382. return this.match(138) ? this.parsePrivateName() : this.parseMaybeUnary(refExpressionErrors);
  10383. }
  10384. parseExprOps(refExpressionErrors) {
  10385. const startLoc = this.state.startLoc;
  10386. const potentialArrowAt = this.state.potentialArrowAt;
  10387. const expr = this.parseMaybeUnaryOrPrivate(refExpressionErrors);
  10388. if (this.shouldExitDescending(expr, potentialArrowAt)) {
  10389. return expr;
  10390. }
  10391. return this.parseExprOp(expr, startLoc, -1);
  10392. }
  10393. parseExprOp(left, leftStartLoc, minPrec) {
  10394. if (this.isPrivateName(left)) {
  10395. const value = this.getPrivateNameSV(left);
  10396. if (minPrec >= tokenOperatorPrecedence(58) || !this.prodParam.hasIn || !this.match(58)) {
  10397. this.raise(Errors.PrivateInExpectedIn, left, {
  10398. identifierName: value
  10399. });
  10400. }
  10401. this.classScope.usePrivateName(value, left.loc.start);
  10402. }
  10403. const op = this.state.type;
  10404. if (tokenIsOperator(op) && (this.prodParam.hasIn || !this.match(58))) {
  10405. let prec = tokenOperatorPrecedence(op);
  10406. if (prec > minPrec) {
  10407. if (op === 39) {
  10408. this.expectPlugin("pipelineOperator");
  10409. if (this.state.inFSharpPipelineDirectBody) {
  10410. return left;
  10411. }
  10412. this.checkPipelineAtInfixOperator(left, leftStartLoc);
  10413. }
  10414. const node = this.startNodeAt(leftStartLoc);
  10415. node.left = left;
  10416. node.operator = this.state.value;
  10417. const logical = op === 41 || op === 42;
  10418. const coalesce = op === 40;
  10419. if (coalesce) {
  10420. prec = tokenOperatorPrecedence(42);
  10421. }
  10422. this.next();
  10423. if (op === 39 && this.hasPlugin(["pipelineOperator", {
  10424. proposal: "minimal"
  10425. }])) {
  10426. if (this.state.type === 96 && this.prodParam.hasAwait) {
  10427. throw this.raise(Errors.UnexpectedAwaitAfterPipelineBody, this.state.startLoc);
  10428. }
  10429. }
  10430. node.right = this.parseExprOpRightExpr(op, prec);
  10431. const finishedNode = this.finishNode(node, logical || coalesce ? "LogicalExpression" : "BinaryExpression");
  10432. const nextOp = this.state.type;
  10433. if (coalesce && (nextOp === 41 || nextOp === 42) || logical && nextOp === 40) {
  10434. throw this.raise(Errors.MixingCoalesceWithLogical, this.state.startLoc);
  10435. }
  10436. return this.parseExprOp(finishedNode, leftStartLoc, minPrec);
  10437. }
  10438. }
  10439. return left;
  10440. }
  10441. parseExprOpRightExpr(op, prec) {
  10442. const startLoc = this.state.startLoc;
  10443. switch (op) {
  10444. case 39:
  10445. switch (this.getPluginOption("pipelineOperator", "proposal")) {
  10446. case "hack":
  10447. return this.withTopicBindingContext(() => {
  10448. return this.parseHackPipeBody();
  10449. });
  10450. case "smart":
  10451. return this.withTopicBindingContext(() => {
  10452. if (this.prodParam.hasYield && this.isContextual(108)) {
  10453. throw this.raise(Errors.PipeBodyIsTighter, this.state.startLoc);
  10454. }
  10455. return this.parseSmartPipelineBodyInStyle(this.parseExprOpBaseRightExpr(op, prec), startLoc);
  10456. });
  10457. case "fsharp":
  10458. return this.withSoloAwaitPermittingContext(() => {
  10459. return this.parseFSharpPipelineBody(prec);
  10460. });
  10461. }
  10462. default:
  10463. return this.parseExprOpBaseRightExpr(op, prec);
  10464. }
  10465. }
  10466. parseExprOpBaseRightExpr(op, prec) {
  10467. const startLoc = this.state.startLoc;
  10468. return this.parseExprOp(this.parseMaybeUnaryOrPrivate(), startLoc, tokenIsRightAssociative(op) ? prec - 1 : prec);
  10469. }
  10470. parseHackPipeBody() {
  10471. var _body$extra;
  10472. const {
  10473. startLoc
  10474. } = this.state;
  10475. const body = this.parseMaybeAssign();
  10476. const requiredParentheses = UnparenthesizedPipeBodyDescriptions.has(body.type);
  10477. if (requiredParentheses && !((_body$extra = body.extra) != null && _body$extra.parenthesized)) {
  10478. this.raise(Errors.PipeUnparenthesizedBody, startLoc, {
  10479. type: body.type
  10480. });
  10481. }
  10482. if (!this.topicReferenceWasUsedInCurrentContext()) {
  10483. this.raise(Errors.PipeTopicUnused, startLoc);
  10484. }
  10485. return body;
  10486. }
  10487. checkExponentialAfterUnary(node) {
  10488. if (this.match(57)) {
  10489. this.raise(Errors.UnexpectedTokenUnaryExponentiation, node.argument);
  10490. }
  10491. }
  10492. parseMaybeUnary(refExpressionErrors, sawUnary) {
  10493. const startLoc = this.state.startLoc;
  10494. const isAwait = this.isContextual(96);
  10495. if (isAwait && this.isAwaitAllowed()) {
  10496. this.next();
  10497. const expr = this.parseAwait(startLoc);
  10498. if (!sawUnary) this.checkExponentialAfterUnary(expr);
  10499. return expr;
  10500. }
  10501. const update = this.match(34);
  10502. const node = this.startNode();
  10503. if (tokenIsPrefix(this.state.type)) {
  10504. node.operator = this.state.value;
  10505. node.prefix = true;
  10506. if (this.match(72)) {
  10507. this.expectPlugin("throwExpressions");
  10508. }
  10509. const isDelete = this.match(89);
  10510. this.next();
  10511. node.argument = this.parseMaybeUnary(null, true);
  10512. this.checkExpressionErrors(refExpressionErrors, true);
  10513. if (this.state.strict && isDelete) {
  10514. const arg = node.argument;
  10515. if (arg.type === "Identifier") {
  10516. this.raise(Errors.StrictDelete, node);
  10517. } else if (this.hasPropertyAsPrivateName(arg)) {
  10518. this.raise(Errors.DeletePrivateField, node);
  10519. }
  10520. }
  10521. if (!update) {
  10522. if (!sawUnary) {
  10523. this.checkExponentialAfterUnary(node);
  10524. }
  10525. return this.finishNode(node, "UnaryExpression");
  10526. }
  10527. }
  10528. const expr = this.parseUpdate(node, update, refExpressionErrors);
  10529. if (isAwait) {
  10530. const {
  10531. type
  10532. } = this.state;
  10533. const startsExpr = this.hasPlugin("v8intrinsic") ? tokenCanStartExpression(type) : tokenCanStartExpression(type) && !this.match(54);
  10534. if (startsExpr && !this.isAmbiguousAwait()) {
  10535. this.raiseOverwrite(Errors.AwaitNotInAsyncContext, startLoc);
  10536. return this.parseAwait(startLoc);
  10537. }
  10538. }
  10539. return expr;
  10540. }
  10541. parseUpdate(node, update, refExpressionErrors) {
  10542. if (update) {
  10543. const updateExpressionNode = node;
  10544. this.checkLVal(updateExpressionNode.argument, {
  10545. in: this.finishNode(updateExpressionNode, "UpdateExpression")
  10546. });
  10547. return node;
  10548. }
  10549. const startLoc = this.state.startLoc;
  10550. let expr = this.parseExprSubscripts(refExpressionErrors);
  10551. if (this.checkExpressionErrors(refExpressionErrors, false)) return expr;
  10552. while (tokenIsPostfix(this.state.type) && !this.canInsertSemicolon()) {
  10553. const node = this.startNodeAt(startLoc);
  10554. node.operator = this.state.value;
  10555. node.prefix = false;
  10556. node.argument = expr;
  10557. this.next();
  10558. this.checkLVal(expr, {
  10559. in: expr = this.finishNode(node, "UpdateExpression")
  10560. });
  10561. }
  10562. return expr;
  10563. }
  10564. parseExprSubscripts(refExpressionErrors) {
  10565. const startLoc = this.state.startLoc;
  10566. const potentialArrowAt = this.state.potentialArrowAt;
  10567. const expr = this.parseExprAtom(refExpressionErrors);
  10568. if (this.shouldExitDescending(expr, potentialArrowAt)) {
  10569. return expr;
  10570. }
  10571. return this.parseSubscripts(expr, startLoc);
  10572. }
  10573. parseSubscripts(base, startLoc, noCalls) {
  10574. const state = {
  10575. optionalChainMember: false,
  10576. maybeAsyncArrow: this.atPossibleAsyncArrow(base),
  10577. stop: false
  10578. };
  10579. do {
  10580. base = this.parseSubscript(base, startLoc, noCalls, state);
  10581. state.maybeAsyncArrow = false;
  10582. } while (!state.stop);
  10583. return base;
  10584. }
  10585. parseSubscript(base, startLoc, noCalls, state) {
  10586. const {
  10587. type
  10588. } = this.state;
  10589. if (!noCalls && type === 15) {
  10590. return this.parseBind(base, startLoc, noCalls, state);
  10591. } else if (tokenIsTemplate(type)) {
  10592. return this.parseTaggedTemplateExpression(base, startLoc, state);
  10593. }
  10594. let optional = false;
  10595. if (type === 18) {
  10596. if (noCalls) {
  10597. this.raise(Errors.OptionalChainingNoNew, this.state.startLoc);
  10598. if (this.lookaheadCharCode() === 40) {
  10599. state.stop = true;
  10600. return base;
  10601. }
  10602. }
  10603. state.optionalChainMember = optional = true;
  10604. this.next();
  10605. }
  10606. if (!noCalls && this.match(10)) {
  10607. return this.parseCoverCallAndAsyncArrowHead(base, startLoc, state, optional);
  10608. } else {
  10609. const computed = this.eat(0);
  10610. if (computed || optional || this.eat(16)) {
  10611. return this.parseMember(base, startLoc, state, computed, optional);
  10612. } else {
  10613. state.stop = true;
  10614. return base;
  10615. }
  10616. }
  10617. }
  10618. parseMember(base, startLoc, state, computed, optional) {
  10619. const node = this.startNodeAt(startLoc);
  10620. node.object = base;
  10621. node.computed = computed;
  10622. if (computed) {
  10623. node.property = this.parseExpression();
  10624. this.expect(3);
  10625. } else if (this.match(138)) {
  10626. if (base.type === "Super") {
  10627. this.raise(Errors.SuperPrivateField, startLoc);
  10628. }
  10629. this.classScope.usePrivateName(this.state.value, this.state.startLoc);
  10630. node.property = this.parsePrivateName();
  10631. } else {
  10632. node.property = this.parseIdentifier(true);
  10633. }
  10634. if (state.optionalChainMember) {
  10635. node.optional = optional;
  10636. return this.finishNode(node, "OptionalMemberExpression");
  10637. } else {
  10638. return this.finishNode(node, "MemberExpression");
  10639. }
  10640. }
  10641. parseBind(base, startLoc, noCalls, state) {
  10642. const node = this.startNodeAt(startLoc);
  10643. node.object = base;
  10644. this.next();
  10645. node.callee = this.parseNoCallExpr();
  10646. state.stop = true;
  10647. return this.parseSubscripts(this.finishNode(node, "BindExpression"), startLoc, noCalls);
  10648. }
  10649. parseCoverCallAndAsyncArrowHead(base, startLoc, state, optional) {
  10650. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  10651. let refExpressionErrors = null;
  10652. this.state.maybeInArrowParameters = true;
  10653. this.next();
  10654. const node = this.startNodeAt(startLoc);
  10655. node.callee = base;
  10656. const {
  10657. maybeAsyncArrow,
  10658. optionalChainMember
  10659. } = state;
  10660. if (maybeAsyncArrow) {
  10661. this.expressionScope.enter(newAsyncArrowScope());
  10662. refExpressionErrors = new ExpressionErrors();
  10663. }
  10664. if (optionalChainMember) {
  10665. node.optional = optional;
  10666. }
  10667. if (optional) {
  10668. node.arguments = this.parseCallExpressionArguments(11);
  10669. } else {
  10670. node.arguments = this.parseCallExpressionArguments(11, base.type === "Import", base.type !== "Super", node, refExpressionErrors);
  10671. }
  10672. let finishedNode = this.finishCallExpression(node, optionalChainMember);
  10673. if (maybeAsyncArrow && this.shouldParseAsyncArrow() && !optional) {
  10674. state.stop = true;
  10675. this.checkDestructuringPrivate(refExpressionErrors);
  10676. this.expressionScope.validateAsPattern();
  10677. this.expressionScope.exit();
  10678. finishedNode = this.parseAsyncArrowFromCallExpression(this.startNodeAt(startLoc), finishedNode);
  10679. } else {
  10680. if (maybeAsyncArrow) {
  10681. this.checkExpressionErrors(refExpressionErrors, true);
  10682. this.expressionScope.exit();
  10683. }
  10684. this.toReferencedArguments(finishedNode);
  10685. }
  10686. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  10687. return finishedNode;
  10688. }
  10689. toReferencedArguments(node, isParenthesizedExpr) {
  10690. this.toReferencedListDeep(node.arguments, isParenthesizedExpr);
  10691. }
  10692. parseTaggedTemplateExpression(base, startLoc, state) {
  10693. const node = this.startNodeAt(startLoc);
  10694. node.tag = base;
  10695. node.quasi = this.parseTemplate(true);
  10696. if (state.optionalChainMember) {
  10697. this.raise(Errors.OptionalChainingNoTemplate, startLoc);
  10698. }
  10699. return this.finishNode(node, "TaggedTemplateExpression");
  10700. }
  10701. atPossibleAsyncArrow(base) {
  10702. return base.type === "Identifier" && base.name === "async" && this.state.lastTokEndLoc.index === base.end && !this.canInsertSemicolon() && base.end - base.start === 5 && base.start === this.state.potentialArrowAt;
  10703. }
  10704. expectImportAttributesPlugin() {
  10705. if (!this.hasPlugin("importAssertions")) {
  10706. this.expectPlugin("importAttributes");
  10707. }
  10708. }
  10709. finishCallExpression(node, optional) {
  10710. if (node.callee.type === "Import") {
  10711. if (node.arguments.length === 2) {
  10712. {
  10713. if (!this.hasPlugin("moduleAttributes")) {
  10714. this.expectImportAttributesPlugin();
  10715. }
  10716. }
  10717. }
  10718. if (node.arguments.length === 0 || node.arguments.length > 2) {
  10719. this.raise(Errors.ImportCallArity, node, {
  10720. maxArgumentCount: this.hasPlugin("importAttributes") || this.hasPlugin("importAssertions") || this.hasPlugin("moduleAttributes") ? 2 : 1
  10721. });
  10722. } else {
  10723. for (const arg of node.arguments) {
  10724. if (arg.type === "SpreadElement") {
  10725. this.raise(Errors.ImportCallSpreadArgument, arg);
  10726. }
  10727. }
  10728. }
  10729. }
  10730. return this.finishNode(node, optional ? "OptionalCallExpression" : "CallExpression");
  10731. }
  10732. parseCallExpressionArguments(close, dynamicImport, allowPlaceholder, nodeForExtra, refExpressionErrors) {
  10733. const elts = [];
  10734. let first = true;
  10735. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  10736. this.state.inFSharpPipelineDirectBody = false;
  10737. while (!this.eat(close)) {
  10738. if (first) {
  10739. first = false;
  10740. } else {
  10741. this.expect(12);
  10742. if (this.match(close)) {
  10743. if (dynamicImport && !this.hasPlugin("importAttributes") && !this.hasPlugin("importAssertions") && !this.hasPlugin("moduleAttributes")) {
  10744. this.raise(Errors.ImportCallArgumentTrailingComma, this.state.lastTokStartLoc);
  10745. }
  10746. if (nodeForExtra) {
  10747. this.addTrailingCommaExtraToNode(nodeForExtra);
  10748. }
  10749. this.next();
  10750. break;
  10751. }
  10752. }
  10753. elts.push(this.parseExprListItem(false, refExpressionErrors, allowPlaceholder));
  10754. }
  10755. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  10756. return elts;
  10757. }
  10758. shouldParseAsyncArrow() {
  10759. return this.match(19) && !this.canInsertSemicolon();
  10760. }
  10761. parseAsyncArrowFromCallExpression(node, call) {
  10762. var _call$extra;
  10763. this.resetPreviousNodeTrailingComments(call);
  10764. this.expect(19);
  10765. this.parseArrowExpression(node, call.arguments, true, (_call$extra = call.extra) == null ? void 0 : _call$extra.trailingCommaLoc);
  10766. if (call.innerComments) {
  10767. setInnerComments(node, call.innerComments);
  10768. }
  10769. if (call.callee.trailingComments) {
  10770. setInnerComments(node, call.callee.trailingComments);
  10771. }
  10772. return node;
  10773. }
  10774. parseNoCallExpr() {
  10775. const startLoc = this.state.startLoc;
  10776. return this.parseSubscripts(this.parseExprAtom(), startLoc, true);
  10777. }
  10778. parseExprAtom(refExpressionErrors) {
  10779. let node;
  10780. let decorators = null;
  10781. const {
  10782. type
  10783. } = this.state;
  10784. switch (type) {
  10785. case 79:
  10786. return this.parseSuper();
  10787. case 83:
  10788. node = this.startNode();
  10789. this.next();
  10790. if (this.match(16)) {
  10791. return this.parseImportMetaProperty(node);
  10792. }
  10793. if (this.match(10)) {
  10794. if (this.options.createImportExpressions) {
  10795. return this.parseImportCall(node);
  10796. } else {
  10797. return this.finishNode(node, "Import");
  10798. }
  10799. } else {
  10800. this.raise(Errors.UnsupportedImport, this.state.lastTokStartLoc);
  10801. return this.finishNode(node, "Import");
  10802. }
  10803. case 78:
  10804. node = this.startNode();
  10805. this.next();
  10806. return this.finishNode(node, "ThisExpression");
  10807. case 90:
  10808. {
  10809. return this.parseDo(this.startNode(), false);
  10810. }
  10811. case 56:
  10812. case 31:
  10813. {
  10814. this.readRegexp();
  10815. return this.parseRegExpLiteral(this.state.value);
  10816. }
  10817. case 134:
  10818. return this.parseNumericLiteral(this.state.value);
  10819. case 135:
  10820. return this.parseBigIntLiteral(this.state.value);
  10821. case 136:
  10822. return this.parseDecimalLiteral(this.state.value);
  10823. case 133:
  10824. return this.parseStringLiteral(this.state.value);
  10825. case 84:
  10826. return this.parseNullLiteral();
  10827. case 85:
  10828. return this.parseBooleanLiteral(true);
  10829. case 86:
  10830. return this.parseBooleanLiteral(false);
  10831. case 10:
  10832. {
  10833. const canBeArrow = this.state.potentialArrowAt === this.state.start;
  10834. return this.parseParenAndDistinguishExpression(canBeArrow);
  10835. }
  10836. case 2:
  10837. case 1:
  10838. {
  10839. return this.parseArrayLike(this.state.type === 2 ? 4 : 3, false, true);
  10840. }
  10841. case 0:
  10842. {
  10843. return this.parseArrayLike(3, true, false, refExpressionErrors);
  10844. }
  10845. case 6:
  10846. case 7:
  10847. {
  10848. return this.parseObjectLike(this.state.type === 6 ? 9 : 8, false, true);
  10849. }
  10850. case 5:
  10851. {
  10852. return this.parseObjectLike(8, false, false, refExpressionErrors);
  10853. }
  10854. case 68:
  10855. return this.parseFunctionOrFunctionSent();
  10856. case 26:
  10857. decorators = this.parseDecorators();
  10858. case 80:
  10859. return this.parseClass(this.maybeTakeDecorators(decorators, this.startNode()), false);
  10860. case 77:
  10861. return this.parseNewOrNewTarget();
  10862. case 25:
  10863. case 24:
  10864. return this.parseTemplate(false);
  10865. case 15:
  10866. {
  10867. node = this.startNode();
  10868. this.next();
  10869. node.object = null;
  10870. const callee = node.callee = this.parseNoCallExpr();
  10871. if (callee.type === "MemberExpression") {
  10872. return this.finishNode(node, "BindExpression");
  10873. } else {
  10874. throw this.raise(Errors.UnsupportedBind, callee);
  10875. }
  10876. }
  10877. case 138:
  10878. {
  10879. this.raise(Errors.PrivateInExpectedIn, this.state.startLoc, {
  10880. identifierName: this.state.value
  10881. });
  10882. return this.parsePrivateName();
  10883. }
  10884. case 33:
  10885. {
  10886. return this.parseTopicReferenceThenEqualsSign(54, "%");
  10887. }
  10888. case 32:
  10889. {
  10890. return this.parseTopicReferenceThenEqualsSign(44, "^");
  10891. }
  10892. case 37:
  10893. case 38:
  10894. {
  10895. return this.parseTopicReference("hack");
  10896. }
  10897. case 44:
  10898. case 54:
  10899. case 27:
  10900. {
  10901. const pipeProposal = this.getPluginOption("pipelineOperator", "proposal");
  10902. if (pipeProposal) {
  10903. return this.parseTopicReference(pipeProposal);
  10904. }
  10905. this.unexpected();
  10906. break;
  10907. }
  10908. case 47:
  10909. {
  10910. const lookaheadCh = this.input.codePointAt(this.nextTokenStart());
  10911. if (isIdentifierStart(lookaheadCh) || lookaheadCh === 62) {
  10912. this.expectOnePlugin(["jsx", "flow", "typescript"]);
  10913. } else {
  10914. this.unexpected();
  10915. }
  10916. break;
  10917. }
  10918. default:
  10919. if (tokenIsIdentifier(type)) {
  10920. if (this.isContextual(127) && this.lookaheadInLineCharCode() === 123) {
  10921. return this.parseModuleExpression();
  10922. }
  10923. const canBeArrow = this.state.potentialArrowAt === this.state.start;
  10924. const containsEsc = this.state.containsEsc;
  10925. const id = this.parseIdentifier();
  10926. if (!containsEsc && id.name === "async" && !this.canInsertSemicolon()) {
  10927. const {
  10928. type
  10929. } = this.state;
  10930. if (type === 68) {
  10931. this.resetPreviousNodeTrailingComments(id);
  10932. this.next();
  10933. return this.parseAsyncFunctionExpression(this.startNodeAtNode(id));
  10934. } else if (tokenIsIdentifier(type)) {
  10935. if (this.lookaheadCharCode() === 61) {
  10936. return this.parseAsyncArrowUnaryFunction(this.startNodeAtNode(id));
  10937. } else {
  10938. return id;
  10939. }
  10940. } else if (type === 90) {
  10941. this.resetPreviousNodeTrailingComments(id);
  10942. return this.parseDo(this.startNodeAtNode(id), true);
  10943. }
  10944. }
  10945. if (canBeArrow && this.match(19) && !this.canInsertSemicolon()) {
  10946. this.next();
  10947. return this.parseArrowExpression(this.startNodeAtNode(id), [id], false);
  10948. }
  10949. return id;
  10950. } else {
  10951. this.unexpected();
  10952. }
  10953. }
  10954. }
  10955. parseTopicReferenceThenEqualsSign(topicTokenType, topicTokenValue) {
  10956. const pipeProposal = this.getPluginOption("pipelineOperator", "proposal");
  10957. if (pipeProposal) {
  10958. this.state.type = topicTokenType;
  10959. this.state.value = topicTokenValue;
  10960. this.state.pos--;
  10961. this.state.end--;
  10962. this.state.endLoc = createPositionWithColumnOffset(this.state.endLoc, -1);
  10963. return this.parseTopicReference(pipeProposal);
  10964. } else {
  10965. this.unexpected();
  10966. }
  10967. }
  10968. parseTopicReference(pipeProposal) {
  10969. const node = this.startNode();
  10970. const startLoc = this.state.startLoc;
  10971. const tokenType = this.state.type;
  10972. this.next();
  10973. return this.finishTopicReference(node, startLoc, pipeProposal, tokenType);
  10974. }
  10975. finishTopicReference(node, startLoc, pipeProposal, tokenType) {
  10976. if (this.testTopicReferenceConfiguration(pipeProposal, startLoc, tokenType)) {
  10977. const nodeType = pipeProposal === "smart" ? "PipelinePrimaryTopicReference" : "TopicReference";
  10978. if (!this.topicReferenceIsAllowedInCurrentContext()) {
  10979. this.raise(pipeProposal === "smart" ? Errors.PrimaryTopicNotAllowed : Errors.PipeTopicUnbound, startLoc);
  10980. }
  10981. this.registerTopicReference();
  10982. return this.finishNode(node, nodeType);
  10983. } else {
  10984. throw this.raise(Errors.PipeTopicUnconfiguredToken, startLoc, {
  10985. token: tokenLabelName(tokenType)
  10986. });
  10987. }
  10988. }
  10989. testTopicReferenceConfiguration(pipeProposal, startLoc, tokenType) {
  10990. switch (pipeProposal) {
  10991. case "hack":
  10992. {
  10993. return this.hasPlugin(["pipelineOperator", {
  10994. topicToken: tokenLabelName(tokenType)
  10995. }]);
  10996. }
  10997. case "smart":
  10998. return tokenType === 27;
  10999. default:
  11000. throw this.raise(Errors.PipeTopicRequiresHackPipes, startLoc);
  11001. }
  11002. }
  11003. parseAsyncArrowUnaryFunction(node) {
  11004. this.prodParam.enter(functionFlags(true, this.prodParam.hasYield));
  11005. const params = [this.parseIdentifier()];
  11006. this.prodParam.exit();
  11007. if (this.hasPrecedingLineBreak()) {
  11008. this.raise(Errors.LineTerminatorBeforeArrow, this.state.curPosition());
  11009. }
  11010. this.expect(19);
  11011. return this.parseArrowExpression(node, params, true);
  11012. }
  11013. parseDo(node, isAsync) {
  11014. this.expectPlugin("doExpressions");
  11015. if (isAsync) {
  11016. this.expectPlugin("asyncDoExpressions");
  11017. }
  11018. node.async = isAsync;
  11019. this.next();
  11020. const oldLabels = this.state.labels;
  11021. this.state.labels = [];
  11022. if (isAsync) {
  11023. this.prodParam.enter(2);
  11024. node.body = this.parseBlock();
  11025. this.prodParam.exit();
  11026. } else {
  11027. node.body = this.parseBlock();
  11028. }
  11029. this.state.labels = oldLabels;
  11030. return this.finishNode(node, "DoExpression");
  11031. }
  11032. parseSuper() {
  11033. const node = this.startNode();
  11034. this.next();
  11035. if (this.match(10) && !this.scope.allowDirectSuper && !this.options.allowSuperOutsideMethod) {
  11036. this.raise(Errors.SuperNotAllowed, node);
  11037. } else if (!this.scope.allowSuper && !this.options.allowSuperOutsideMethod) {
  11038. this.raise(Errors.UnexpectedSuper, node);
  11039. }
  11040. if (!this.match(10) && !this.match(0) && !this.match(16)) {
  11041. this.raise(Errors.UnsupportedSuper, node);
  11042. }
  11043. return this.finishNode(node, "Super");
  11044. }
  11045. parsePrivateName() {
  11046. const node = this.startNode();
  11047. const id = this.startNodeAt(createPositionWithColumnOffset(this.state.startLoc, 1));
  11048. const name = this.state.value;
  11049. this.next();
  11050. node.id = this.createIdentifier(id, name);
  11051. return this.finishNode(node, "PrivateName");
  11052. }
  11053. parseFunctionOrFunctionSent() {
  11054. const node = this.startNode();
  11055. this.next();
  11056. if (this.prodParam.hasYield && this.match(16)) {
  11057. const meta = this.createIdentifier(this.startNodeAtNode(node), "function");
  11058. this.next();
  11059. if (this.match(103)) {
  11060. this.expectPlugin("functionSent");
  11061. } else if (!this.hasPlugin("functionSent")) {
  11062. this.unexpected();
  11063. }
  11064. return this.parseMetaProperty(node, meta, "sent");
  11065. }
  11066. return this.parseFunction(node);
  11067. }
  11068. parseMetaProperty(node, meta, propertyName) {
  11069. node.meta = meta;
  11070. const containsEsc = this.state.containsEsc;
  11071. node.property = this.parseIdentifier(true);
  11072. if (node.property.name !== propertyName || containsEsc) {
  11073. this.raise(Errors.UnsupportedMetaProperty, node.property, {
  11074. target: meta.name,
  11075. onlyValidPropertyName: propertyName
  11076. });
  11077. }
  11078. return this.finishNode(node, "MetaProperty");
  11079. }
  11080. parseImportMetaProperty(node) {
  11081. const id = this.createIdentifier(this.startNodeAtNode(node), "import");
  11082. this.next();
  11083. if (this.isContextual(101)) {
  11084. if (!this.inModule) {
  11085. this.raise(Errors.ImportMetaOutsideModule, id);
  11086. }
  11087. this.sawUnambiguousESM = true;
  11088. } else if (this.isContextual(105) || this.isContextual(97)) {
  11089. const isSource = this.isContextual(105);
  11090. if (!isSource) this.unexpected();
  11091. this.expectPlugin(isSource ? "sourcePhaseImports" : "deferredImportEvaluation");
  11092. if (!this.options.createImportExpressions) {
  11093. throw this.raise(Errors.DynamicImportPhaseRequiresImportExpressions, this.state.startLoc, {
  11094. phase: this.state.value
  11095. });
  11096. }
  11097. this.next();
  11098. node.phase = isSource ? "source" : "defer";
  11099. return this.parseImportCall(node);
  11100. }
  11101. return this.parseMetaProperty(node, id, "meta");
  11102. }
  11103. parseLiteralAtNode(value, type, node) {
  11104. this.addExtra(node, "rawValue", value);
  11105. this.addExtra(node, "raw", this.input.slice(node.start, this.state.end));
  11106. node.value = value;
  11107. this.next();
  11108. return this.finishNode(node, type);
  11109. }
  11110. parseLiteral(value, type) {
  11111. const node = this.startNode();
  11112. return this.parseLiteralAtNode(value, type, node);
  11113. }
  11114. parseStringLiteral(value) {
  11115. return this.parseLiteral(value, "StringLiteral");
  11116. }
  11117. parseNumericLiteral(value) {
  11118. return this.parseLiteral(value, "NumericLiteral");
  11119. }
  11120. parseBigIntLiteral(value) {
  11121. return this.parseLiteral(value, "BigIntLiteral");
  11122. }
  11123. parseDecimalLiteral(value) {
  11124. return this.parseLiteral(value, "DecimalLiteral");
  11125. }
  11126. parseRegExpLiteral(value) {
  11127. const node = this.parseLiteral(value.value, "RegExpLiteral");
  11128. node.pattern = value.pattern;
  11129. node.flags = value.flags;
  11130. return node;
  11131. }
  11132. parseBooleanLiteral(value) {
  11133. const node = this.startNode();
  11134. node.value = value;
  11135. this.next();
  11136. return this.finishNode(node, "BooleanLiteral");
  11137. }
  11138. parseNullLiteral() {
  11139. const node = this.startNode();
  11140. this.next();
  11141. return this.finishNode(node, "NullLiteral");
  11142. }
  11143. parseParenAndDistinguishExpression(canBeArrow) {
  11144. const startLoc = this.state.startLoc;
  11145. let val;
  11146. this.next();
  11147. this.expressionScope.enter(newArrowHeadScope());
  11148. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  11149. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  11150. this.state.maybeInArrowParameters = true;
  11151. this.state.inFSharpPipelineDirectBody = false;
  11152. const innerStartLoc = this.state.startLoc;
  11153. const exprList = [];
  11154. const refExpressionErrors = new ExpressionErrors();
  11155. let first = true;
  11156. let spreadStartLoc;
  11157. let optionalCommaStartLoc;
  11158. while (!this.match(11)) {
  11159. if (first) {
  11160. first = false;
  11161. } else {
  11162. this.expect(12, refExpressionErrors.optionalParametersLoc === null ? null : refExpressionErrors.optionalParametersLoc);
  11163. if (this.match(11)) {
  11164. optionalCommaStartLoc = this.state.startLoc;
  11165. break;
  11166. }
  11167. }
  11168. if (this.match(21)) {
  11169. const spreadNodeStartLoc = this.state.startLoc;
  11170. spreadStartLoc = this.state.startLoc;
  11171. exprList.push(this.parseParenItem(this.parseRestBinding(), spreadNodeStartLoc));
  11172. if (!this.checkCommaAfterRest(41)) {
  11173. break;
  11174. }
  11175. } else {
  11176. exprList.push(this.parseMaybeAssignAllowIn(refExpressionErrors, this.parseParenItem));
  11177. }
  11178. }
  11179. const innerEndLoc = this.state.lastTokEndLoc;
  11180. this.expect(11);
  11181. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  11182. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  11183. let arrowNode = this.startNodeAt(startLoc);
  11184. if (canBeArrow && this.shouldParseArrow(exprList) && (arrowNode = this.parseArrow(arrowNode))) {
  11185. this.checkDestructuringPrivate(refExpressionErrors);
  11186. this.expressionScope.validateAsPattern();
  11187. this.expressionScope.exit();
  11188. this.parseArrowExpression(arrowNode, exprList, false);
  11189. return arrowNode;
  11190. }
  11191. this.expressionScope.exit();
  11192. if (!exprList.length) {
  11193. this.unexpected(this.state.lastTokStartLoc);
  11194. }
  11195. if (optionalCommaStartLoc) this.unexpected(optionalCommaStartLoc);
  11196. if (spreadStartLoc) this.unexpected(spreadStartLoc);
  11197. this.checkExpressionErrors(refExpressionErrors, true);
  11198. this.toReferencedListDeep(exprList, true);
  11199. if (exprList.length > 1) {
  11200. val = this.startNodeAt(innerStartLoc);
  11201. val.expressions = exprList;
  11202. this.finishNode(val, "SequenceExpression");
  11203. this.resetEndLocation(val, innerEndLoc);
  11204. } else {
  11205. val = exprList[0];
  11206. }
  11207. return this.wrapParenthesis(startLoc, val);
  11208. }
  11209. wrapParenthesis(startLoc, expression) {
  11210. if (!this.options.createParenthesizedExpressions) {
  11211. this.addExtra(expression, "parenthesized", true);
  11212. this.addExtra(expression, "parenStart", startLoc.index);
  11213. this.takeSurroundingComments(expression, startLoc.index, this.state.lastTokEndLoc.index);
  11214. return expression;
  11215. }
  11216. const parenExpression = this.startNodeAt(startLoc);
  11217. parenExpression.expression = expression;
  11218. return this.finishNode(parenExpression, "ParenthesizedExpression");
  11219. }
  11220. shouldParseArrow(params) {
  11221. return !this.canInsertSemicolon();
  11222. }
  11223. parseArrow(node) {
  11224. if (this.eat(19)) {
  11225. return node;
  11226. }
  11227. }
  11228. parseParenItem(node, startLoc) {
  11229. return node;
  11230. }
  11231. parseNewOrNewTarget() {
  11232. const node = this.startNode();
  11233. this.next();
  11234. if (this.match(16)) {
  11235. const meta = this.createIdentifier(this.startNodeAtNode(node), "new");
  11236. this.next();
  11237. const metaProp = this.parseMetaProperty(node, meta, "target");
  11238. if (!this.scope.inNonArrowFunction && !this.scope.inClass && !this.options.allowNewTargetOutsideFunction) {
  11239. this.raise(Errors.UnexpectedNewTarget, metaProp);
  11240. }
  11241. return metaProp;
  11242. }
  11243. return this.parseNew(node);
  11244. }
  11245. parseNew(node) {
  11246. this.parseNewCallee(node);
  11247. if (this.eat(10)) {
  11248. const args = this.parseExprList(11);
  11249. this.toReferencedList(args);
  11250. node.arguments = args;
  11251. } else {
  11252. node.arguments = [];
  11253. }
  11254. return this.finishNode(node, "NewExpression");
  11255. }
  11256. parseNewCallee(node) {
  11257. const isImport = this.match(83);
  11258. const callee = this.parseNoCallExpr();
  11259. node.callee = callee;
  11260. if (isImport && (callee.type === "Import" || callee.type === "ImportExpression")) {
  11261. this.raise(Errors.ImportCallNotNewExpression, callee);
  11262. }
  11263. }
  11264. parseTemplateElement(isTagged) {
  11265. const {
  11266. start,
  11267. startLoc,
  11268. end,
  11269. value
  11270. } = this.state;
  11271. const elemStart = start + 1;
  11272. const elem = this.startNodeAt(createPositionWithColumnOffset(startLoc, 1));
  11273. if (value === null) {
  11274. if (!isTagged) {
  11275. this.raise(Errors.InvalidEscapeSequenceTemplate, createPositionWithColumnOffset(this.state.firstInvalidTemplateEscapePos, 1));
  11276. }
  11277. }
  11278. const isTail = this.match(24);
  11279. const endOffset = isTail ? -1 : -2;
  11280. const elemEnd = end + endOffset;
  11281. elem.value = {
  11282. raw: this.input.slice(elemStart, elemEnd).replace(/\r\n?/g, "\n"),
  11283. cooked: value === null ? null : value.slice(1, endOffset)
  11284. };
  11285. elem.tail = isTail;
  11286. this.next();
  11287. const finishedNode = this.finishNode(elem, "TemplateElement");
  11288. this.resetEndLocation(finishedNode, createPositionWithColumnOffset(this.state.lastTokEndLoc, endOffset));
  11289. return finishedNode;
  11290. }
  11291. parseTemplate(isTagged) {
  11292. const node = this.startNode();
  11293. let curElt = this.parseTemplateElement(isTagged);
  11294. const quasis = [curElt];
  11295. const substitutions = [];
  11296. while (!curElt.tail) {
  11297. substitutions.push(this.parseTemplateSubstitution());
  11298. this.readTemplateContinuation();
  11299. quasis.push(curElt = this.parseTemplateElement(isTagged));
  11300. }
  11301. node.expressions = substitutions;
  11302. node.quasis = quasis;
  11303. return this.finishNode(node, "TemplateLiteral");
  11304. }
  11305. parseTemplateSubstitution() {
  11306. return this.parseExpression();
  11307. }
  11308. parseObjectLike(close, isPattern, isRecord, refExpressionErrors) {
  11309. if (isRecord) {
  11310. this.expectPlugin("recordAndTuple");
  11311. }
  11312. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  11313. this.state.inFSharpPipelineDirectBody = false;
  11314. const propHash = Object.create(null);
  11315. let first = true;
  11316. const node = this.startNode();
  11317. node.properties = [];
  11318. this.next();
  11319. while (!this.match(close)) {
  11320. if (first) {
  11321. first = false;
  11322. } else {
  11323. this.expect(12);
  11324. if (this.match(close)) {
  11325. this.addTrailingCommaExtraToNode(node);
  11326. break;
  11327. }
  11328. }
  11329. let prop;
  11330. if (isPattern) {
  11331. prop = this.parseBindingProperty();
  11332. } else {
  11333. prop = this.parsePropertyDefinition(refExpressionErrors);
  11334. this.checkProto(prop, isRecord, propHash, refExpressionErrors);
  11335. }
  11336. if (isRecord && !this.isObjectProperty(prop) && prop.type !== "SpreadElement") {
  11337. this.raise(Errors.InvalidRecordProperty, prop);
  11338. }
  11339. {
  11340. if (prop.shorthand) {
  11341. this.addExtra(prop, "shorthand", true);
  11342. }
  11343. }
  11344. node.properties.push(prop);
  11345. }
  11346. this.next();
  11347. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  11348. let type = "ObjectExpression";
  11349. if (isPattern) {
  11350. type = "ObjectPattern";
  11351. } else if (isRecord) {
  11352. type = "RecordExpression";
  11353. }
  11354. return this.finishNode(node, type);
  11355. }
  11356. addTrailingCommaExtraToNode(node) {
  11357. this.addExtra(node, "trailingComma", this.state.lastTokStartLoc.index);
  11358. this.addExtra(node, "trailingCommaLoc", this.state.lastTokStartLoc, false);
  11359. }
  11360. maybeAsyncOrAccessorProp(prop) {
  11361. return !prop.computed && prop.key.type === "Identifier" && (this.isLiteralPropertyName() || this.match(0) || this.match(55));
  11362. }
  11363. parsePropertyDefinition(refExpressionErrors) {
  11364. let decorators = [];
  11365. if (this.match(26)) {
  11366. if (this.hasPlugin("decorators")) {
  11367. this.raise(Errors.UnsupportedPropertyDecorator, this.state.startLoc);
  11368. }
  11369. while (this.match(26)) {
  11370. decorators.push(this.parseDecorator());
  11371. }
  11372. }
  11373. const prop = this.startNode();
  11374. let isAsync = false;
  11375. let isAccessor = false;
  11376. let startLoc;
  11377. if (this.match(21)) {
  11378. if (decorators.length) this.unexpected();
  11379. return this.parseSpread();
  11380. }
  11381. if (decorators.length) {
  11382. prop.decorators = decorators;
  11383. decorators = [];
  11384. }
  11385. prop.method = false;
  11386. if (refExpressionErrors) {
  11387. startLoc = this.state.startLoc;
  11388. }
  11389. let isGenerator = this.eat(55);
  11390. this.parsePropertyNamePrefixOperator(prop);
  11391. const containsEsc = this.state.containsEsc;
  11392. this.parsePropertyName(prop, refExpressionErrors);
  11393. if (!isGenerator && !containsEsc && this.maybeAsyncOrAccessorProp(prop)) {
  11394. const {
  11395. key
  11396. } = prop;
  11397. const keyName = key.name;
  11398. if (keyName === "async" && !this.hasPrecedingLineBreak()) {
  11399. isAsync = true;
  11400. this.resetPreviousNodeTrailingComments(key);
  11401. isGenerator = this.eat(55);
  11402. this.parsePropertyName(prop);
  11403. }
  11404. if (keyName === "get" || keyName === "set") {
  11405. isAccessor = true;
  11406. this.resetPreviousNodeTrailingComments(key);
  11407. prop.kind = keyName;
  11408. if (this.match(55)) {
  11409. isGenerator = true;
  11410. this.raise(Errors.AccessorIsGenerator, this.state.curPosition(), {
  11411. kind: keyName
  11412. });
  11413. this.next();
  11414. }
  11415. this.parsePropertyName(prop);
  11416. }
  11417. }
  11418. return this.parseObjPropValue(prop, startLoc, isGenerator, isAsync, false, isAccessor, refExpressionErrors);
  11419. }
  11420. getGetterSetterExpectedParamCount(method) {
  11421. return method.kind === "get" ? 0 : 1;
  11422. }
  11423. getObjectOrClassMethodParams(method) {
  11424. return method.params;
  11425. }
  11426. checkGetterSetterParams(method) {
  11427. var _params;
  11428. const paramCount = this.getGetterSetterExpectedParamCount(method);
  11429. const params = this.getObjectOrClassMethodParams(method);
  11430. if (params.length !== paramCount) {
  11431. this.raise(method.kind === "get" ? Errors.BadGetterArity : Errors.BadSetterArity, method);
  11432. }
  11433. if (method.kind === "set" && ((_params = params[params.length - 1]) == null ? void 0 : _params.type) === "RestElement") {
  11434. this.raise(Errors.BadSetterRestParameter, method);
  11435. }
  11436. }
  11437. parseObjectMethod(prop, isGenerator, isAsync, isPattern, isAccessor) {
  11438. if (isAccessor) {
  11439. const finishedProp = this.parseMethod(prop, isGenerator, false, false, false, "ObjectMethod");
  11440. this.checkGetterSetterParams(finishedProp);
  11441. return finishedProp;
  11442. }
  11443. if (isAsync || isGenerator || this.match(10)) {
  11444. if (isPattern) this.unexpected();
  11445. prop.kind = "method";
  11446. prop.method = true;
  11447. return this.parseMethod(prop, isGenerator, isAsync, false, false, "ObjectMethod");
  11448. }
  11449. }
  11450. parseObjectProperty(prop, startLoc, isPattern, refExpressionErrors) {
  11451. prop.shorthand = false;
  11452. if (this.eat(14)) {
  11453. prop.value = isPattern ? this.parseMaybeDefault(this.state.startLoc) : this.parseMaybeAssignAllowIn(refExpressionErrors);
  11454. return this.finishNode(prop, "ObjectProperty");
  11455. }
  11456. if (!prop.computed && prop.key.type === "Identifier") {
  11457. this.checkReservedWord(prop.key.name, prop.key.loc.start, true, false);
  11458. if (isPattern) {
  11459. prop.value = this.parseMaybeDefault(startLoc, cloneIdentifier(prop.key));
  11460. } else if (this.match(29)) {
  11461. const shorthandAssignLoc = this.state.startLoc;
  11462. if (refExpressionErrors != null) {
  11463. if (refExpressionErrors.shorthandAssignLoc === null) {
  11464. refExpressionErrors.shorthandAssignLoc = shorthandAssignLoc;
  11465. }
  11466. } else {
  11467. this.raise(Errors.InvalidCoverInitializedName, shorthandAssignLoc);
  11468. }
  11469. prop.value = this.parseMaybeDefault(startLoc, cloneIdentifier(prop.key));
  11470. } else {
  11471. prop.value = cloneIdentifier(prop.key);
  11472. }
  11473. prop.shorthand = true;
  11474. return this.finishNode(prop, "ObjectProperty");
  11475. }
  11476. }
  11477. parseObjPropValue(prop, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors) {
  11478. const node = this.parseObjectMethod(prop, isGenerator, isAsync, isPattern, isAccessor) || this.parseObjectProperty(prop, startLoc, isPattern, refExpressionErrors);
  11479. if (!node) this.unexpected();
  11480. return node;
  11481. }
  11482. parsePropertyName(prop, refExpressionErrors) {
  11483. if (this.eat(0)) {
  11484. prop.computed = true;
  11485. prop.key = this.parseMaybeAssignAllowIn();
  11486. this.expect(3);
  11487. } else {
  11488. const {
  11489. type,
  11490. value
  11491. } = this.state;
  11492. let key;
  11493. if (tokenIsKeywordOrIdentifier(type)) {
  11494. key = this.parseIdentifier(true);
  11495. } else {
  11496. switch (type) {
  11497. case 134:
  11498. key = this.parseNumericLiteral(value);
  11499. break;
  11500. case 133:
  11501. key = this.parseStringLiteral(value);
  11502. break;
  11503. case 135:
  11504. key = this.parseBigIntLiteral(value);
  11505. break;
  11506. case 136:
  11507. key = this.parseDecimalLiteral(value);
  11508. break;
  11509. case 138:
  11510. {
  11511. const privateKeyLoc = this.state.startLoc;
  11512. if (refExpressionErrors != null) {
  11513. if (refExpressionErrors.privateKeyLoc === null) {
  11514. refExpressionErrors.privateKeyLoc = privateKeyLoc;
  11515. }
  11516. } else {
  11517. this.raise(Errors.UnexpectedPrivateField, privateKeyLoc);
  11518. }
  11519. key = this.parsePrivateName();
  11520. break;
  11521. }
  11522. default:
  11523. this.unexpected();
  11524. }
  11525. }
  11526. prop.key = key;
  11527. if (type !== 138) {
  11528. prop.computed = false;
  11529. }
  11530. }
  11531. }
  11532. initFunction(node, isAsync) {
  11533. node.id = null;
  11534. node.generator = false;
  11535. node.async = isAsync;
  11536. }
  11537. parseMethod(node, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope = false) {
  11538. this.initFunction(node, isAsync);
  11539. node.generator = isGenerator;
  11540. this.scope.enter(2 | 16 | (inClassScope ? 64 : 0) | (allowDirectSuper ? 32 : 0));
  11541. this.prodParam.enter(functionFlags(isAsync, node.generator));
  11542. this.parseFunctionParams(node, isConstructor);
  11543. const finishedNode = this.parseFunctionBodyAndFinish(node, type, true);
  11544. this.prodParam.exit();
  11545. this.scope.exit();
  11546. return finishedNode;
  11547. }
  11548. parseArrayLike(close, canBePattern, isTuple, refExpressionErrors) {
  11549. if (isTuple) {
  11550. this.expectPlugin("recordAndTuple");
  11551. }
  11552. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  11553. this.state.inFSharpPipelineDirectBody = false;
  11554. const node = this.startNode();
  11555. this.next();
  11556. node.elements = this.parseExprList(close, !isTuple, refExpressionErrors, node);
  11557. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  11558. return this.finishNode(node, isTuple ? "TupleExpression" : "ArrayExpression");
  11559. }
  11560. parseArrowExpression(node, params, isAsync, trailingCommaLoc) {
  11561. this.scope.enter(2 | 4);
  11562. let flags = functionFlags(isAsync, false);
  11563. if (!this.match(5) && this.prodParam.hasIn) {
  11564. flags |= 8;
  11565. }
  11566. this.prodParam.enter(flags);
  11567. this.initFunction(node, isAsync);
  11568. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  11569. if (params) {
  11570. this.state.maybeInArrowParameters = true;
  11571. this.setArrowFunctionParameters(node, params, trailingCommaLoc);
  11572. }
  11573. this.state.maybeInArrowParameters = false;
  11574. this.parseFunctionBody(node, true);
  11575. this.prodParam.exit();
  11576. this.scope.exit();
  11577. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  11578. return this.finishNode(node, "ArrowFunctionExpression");
  11579. }
  11580. setArrowFunctionParameters(node, params, trailingCommaLoc) {
  11581. this.toAssignableList(params, trailingCommaLoc, false);
  11582. node.params = params;
  11583. }
  11584. parseFunctionBodyAndFinish(node, type, isMethod = false) {
  11585. this.parseFunctionBody(node, false, isMethod);
  11586. return this.finishNode(node, type);
  11587. }
  11588. parseFunctionBody(node, allowExpression, isMethod = false) {
  11589. const isExpression = allowExpression && !this.match(5);
  11590. this.expressionScope.enter(newExpressionScope());
  11591. if (isExpression) {
  11592. node.body = this.parseMaybeAssign();
  11593. this.checkParams(node, false, allowExpression, false);
  11594. } else {
  11595. const oldStrict = this.state.strict;
  11596. const oldLabels = this.state.labels;
  11597. this.state.labels = [];
  11598. this.prodParam.enter(this.prodParam.currentFlags() | 4);
  11599. node.body = this.parseBlock(true, false, hasStrictModeDirective => {
  11600. const nonSimple = !this.isSimpleParamList(node.params);
  11601. if (hasStrictModeDirective && nonSimple) {
  11602. this.raise(Errors.IllegalLanguageModeDirective, (node.kind === "method" || node.kind === "constructor") && !!node.key ? node.key.loc.end : node);
  11603. }
  11604. const strictModeChanged = !oldStrict && this.state.strict;
  11605. this.checkParams(node, !this.state.strict && !allowExpression && !isMethod && !nonSimple, allowExpression, strictModeChanged);
  11606. if (this.state.strict && node.id) {
  11607. this.checkIdentifier(node.id, 65, strictModeChanged);
  11608. }
  11609. });
  11610. this.prodParam.exit();
  11611. this.state.labels = oldLabels;
  11612. }
  11613. this.expressionScope.exit();
  11614. }
  11615. isSimpleParameter(node) {
  11616. return node.type === "Identifier";
  11617. }
  11618. isSimpleParamList(params) {
  11619. for (let i = 0, len = params.length; i < len; i++) {
  11620. if (!this.isSimpleParameter(params[i])) return false;
  11621. }
  11622. return true;
  11623. }
  11624. checkParams(node, allowDuplicates, isArrowFunction, strictModeChanged = true) {
  11625. const checkClashes = !allowDuplicates && new Set();
  11626. const formalParameters = {
  11627. type: "FormalParameters"
  11628. };
  11629. for (const param of node.params) {
  11630. this.checkLVal(param, {
  11631. in: formalParameters,
  11632. binding: 5,
  11633. checkClashes,
  11634. strictModeChanged
  11635. });
  11636. }
  11637. }
  11638. parseExprList(close, allowEmpty, refExpressionErrors, nodeForExtra) {
  11639. const elts = [];
  11640. let first = true;
  11641. while (!this.eat(close)) {
  11642. if (first) {
  11643. first = false;
  11644. } else {
  11645. this.expect(12);
  11646. if (this.match(close)) {
  11647. if (nodeForExtra) {
  11648. this.addTrailingCommaExtraToNode(nodeForExtra);
  11649. }
  11650. this.next();
  11651. break;
  11652. }
  11653. }
  11654. elts.push(this.parseExprListItem(allowEmpty, refExpressionErrors));
  11655. }
  11656. return elts;
  11657. }
  11658. parseExprListItem(allowEmpty, refExpressionErrors, allowPlaceholder) {
  11659. let elt;
  11660. if (this.match(12)) {
  11661. if (!allowEmpty) {
  11662. this.raise(Errors.UnexpectedToken, this.state.curPosition(), {
  11663. unexpected: ","
  11664. });
  11665. }
  11666. elt = null;
  11667. } else if (this.match(21)) {
  11668. const spreadNodeStartLoc = this.state.startLoc;
  11669. elt = this.parseParenItem(this.parseSpread(refExpressionErrors), spreadNodeStartLoc);
  11670. } else if (this.match(17)) {
  11671. this.expectPlugin("partialApplication");
  11672. if (!allowPlaceholder) {
  11673. this.raise(Errors.UnexpectedArgumentPlaceholder, this.state.startLoc);
  11674. }
  11675. const node = this.startNode();
  11676. this.next();
  11677. elt = this.finishNode(node, "ArgumentPlaceholder");
  11678. } else {
  11679. elt = this.parseMaybeAssignAllowIn(refExpressionErrors, this.parseParenItem);
  11680. }
  11681. return elt;
  11682. }
  11683. parseIdentifier(liberal) {
  11684. const node = this.startNode();
  11685. const name = this.parseIdentifierName(liberal);
  11686. return this.createIdentifier(node, name);
  11687. }
  11688. createIdentifier(node, name) {
  11689. node.name = name;
  11690. node.loc.identifierName = name;
  11691. return this.finishNode(node, "Identifier");
  11692. }
  11693. parseIdentifierName(liberal) {
  11694. let name;
  11695. const {
  11696. startLoc,
  11697. type
  11698. } = this.state;
  11699. if (tokenIsKeywordOrIdentifier(type)) {
  11700. name = this.state.value;
  11701. } else {
  11702. this.unexpected();
  11703. }
  11704. const tokenIsKeyword = tokenKeywordOrIdentifierIsKeyword(type);
  11705. if (liberal) {
  11706. if (tokenIsKeyword) {
  11707. this.replaceToken(132);
  11708. }
  11709. } else {
  11710. this.checkReservedWord(name, startLoc, tokenIsKeyword, false);
  11711. }
  11712. this.next();
  11713. return name;
  11714. }
  11715. checkReservedWord(word, startLoc, checkKeywords, isBinding) {
  11716. if (word.length > 10) {
  11717. return;
  11718. }
  11719. if (!canBeReservedWord(word)) {
  11720. return;
  11721. }
  11722. if (checkKeywords && isKeyword(word)) {
  11723. this.raise(Errors.UnexpectedKeyword, startLoc, {
  11724. keyword: word
  11725. });
  11726. return;
  11727. }
  11728. const reservedTest = !this.state.strict ? isReservedWord : isBinding ? isStrictBindReservedWord : isStrictReservedWord;
  11729. if (reservedTest(word, this.inModule)) {
  11730. this.raise(Errors.UnexpectedReservedWord, startLoc, {
  11731. reservedWord: word
  11732. });
  11733. return;
  11734. } else if (word === "yield") {
  11735. if (this.prodParam.hasYield) {
  11736. this.raise(Errors.YieldBindingIdentifier, startLoc);
  11737. return;
  11738. }
  11739. } else if (word === "await") {
  11740. if (this.prodParam.hasAwait) {
  11741. this.raise(Errors.AwaitBindingIdentifier, startLoc);
  11742. return;
  11743. }
  11744. if (this.scope.inStaticBlock) {
  11745. this.raise(Errors.AwaitBindingIdentifierInStaticBlock, startLoc);
  11746. return;
  11747. }
  11748. this.expressionScope.recordAsyncArrowParametersError(startLoc);
  11749. } else if (word === "arguments") {
  11750. if (this.scope.inClassAndNotInNonArrowFunction) {
  11751. this.raise(Errors.ArgumentsInClass, startLoc);
  11752. return;
  11753. }
  11754. }
  11755. }
  11756. isAwaitAllowed() {
  11757. if (this.prodParam.hasAwait) return true;
  11758. if (this.options.allowAwaitOutsideFunction && !this.scope.inFunction) {
  11759. return true;
  11760. }
  11761. return false;
  11762. }
  11763. parseAwait(startLoc) {
  11764. const node = this.startNodeAt(startLoc);
  11765. this.expressionScope.recordParameterInitializerError(Errors.AwaitExpressionFormalParameter, node);
  11766. if (this.eat(55)) {
  11767. this.raise(Errors.ObsoleteAwaitStar, node);
  11768. }
  11769. if (!this.scope.inFunction && !this.options.allowAwaitOutsideFunction) {
  11770. if (this.isAmbiguousAwait()) {
  11771. this.ambiguousScriptDifferentAst = true;
  11772. } else {
  11773. this.sawUnambiguousESM = true;
  11774. }
  11775. }
  11776. if (!this.state.soloAwait) {
  11777. node.argument = this.parseMaybeUnary(null, true);
  11778. }
  11779. return this.finishNode(node, "AwaitExpression");
  11780. }
  11781. isAmbiguousAwait() {
  11782. if (this.hasPrecedingLineBreak()) return true;
  11783. const {
  11784. type
  11785. } = this.state;
  11786. return type === 53 || type === 10 || type === 0 || tokenIsTemplate(type) || type === 102 && !this.state.containsEsc || type === 137 || type === 56 || this.hasPlugin("v8intrinsic") && type === 54;
  11787. }
  11788. parseYield() {
  11789. const node = this.startNode();
  11790. this.expressionScope.recordParameterInitializerError(Errors.YieldInParameter, node);
  11791. this.next();
  11792. let delegating = false;
  11793. let argument = null;
  11794. if (!this.hasPrecedingLineBreak()) {
  11795. delegating = this.eat(55);
  11796. switch (this.state.type) {
  11797. case 13:
  11798. case 139:
  11799. case 8:
  11800. case 11:
  11801. case 3:
  11802. case 9:
  11803. case 14:
  11804. case 12:
  11805. if (!delegating) break;
  11806. default:
  11807. argument = this.parseMaybeAssign();
  11808. }
  11809. }
  11810. node.delegate = delegating;
  11811. node.argument = argument;
  11812. return this.finishNode(node, "YieldExpression");
  11813. }
  11814. parseImportCall(node) {
  11815. this.next();
  11816. node.source = this.parseMaybeAssignAllowIn();
  11817. if (this.hasPlugin("importAttributes") || this.hasPlugin("importAssertions")) {
  11818. node.options = null;
  11819. }
  11820. if (this.eat(12)) {
  11821. this.expectImportAttributesPlugin();
  11822. if (!this.match(11)) {
  11823. node.options = this.parseMaybeAssignAllowIn();
  11824. this.eat(12);
  11825. }
  11826. }
  11827. this.expect(11);
  11828. return this.finishNode(node, "ImportExpression");
  11829. }
  11830. checkPipelineAtInfixOperator(left, leftStartLoc) {
  11831. if (this.hasPlugin(["pipelineOperator", {
  11832. proposal: "smart"
  11833. }])) {
  11834. if (left.type === "SequenceExpression") {
  11835. this.raise(Errors.PipelineHeadSequenceExpression, leftStartLoc);
  11836. }
  11837. }
  11838. }
  11839. parseSmartPipelineBodyInStyle(childExpr, startLoc) {
  11840. if (this.isSimpleReference(childExpr)) {
  11841. const bodyNode = this.startNodeAt(startLoc);
  11842. bodyNode.callee = childExpr;
  11843. return this.finishNode(bodyNode, "PipelineBareFunction");
  11844. } else {
  11845. const bodyNode = this.startNodeAt(startLoc);
  11846. this.checkSmartPipeTopicBodyEarlyErrors(startLoc);
  11847. bodyNode.expression = childExpr;
  11848. return this.finishNode(bodyNode, "PipelineTopicExpression");
  11849. }
  11850. }
  11851. isSimpleReference(expression) {
  11852. switch (expression.type) {
  11853. case "MemberExpression":
  11854. return !expression.computed && this.isSimpleReference(expression.object);
  11855. case "Identifier":
  11856. return true;
  11857. default:
  11858. return false;
  11859. }
  11860. }
  11861. checkSmartPipeTopicBodyEarlyErrors(startLoc) {
  11862. if (this.match(19)) {
  11863. throw this.raise(Errors.PipelineBodyNoArrow, this.state.startLoc);
  11864. }
  11865. if (!this.topicReferenceWasUsedInCurrentContext()) {
  11866. this.raise(Errors.PipelineTopicUnused, startLoc);
  11867. }
  11868. }
  11869. withTopicBindingContext(callback) {
  11870. const outerContextTopicState = this.state.topicContext;
  11871. this.state.topicContext = {
  11872. maxNumOfResolvableTopics: 1,
  11873. maxTopicIndex: null
  11874. };
  11875. try {
  11876. return callback();
  11877. } finally {
  11878. this.state.topicContext = outerContextTopicState;
  11879. }
  11880. }
  11881. withSmartMixTopicForbiddingContext(callback) {
  11882. if (this.hasPlugin(["pipelineOperator", {
  11883. proposal: "smart"
  11884. }])) {
  11885. const outerContextTopicState = this.state.topicContext;
  11886. this.state.topicContext = {
  11887. maxNumOfResolvableTopics: 0,
  11888. maxTopicIndex: null
  11889. };
  11890. try {
  11891. return callback();
  11892. } finally {
  11893. this.state.topicContext = outerContextTopicState;
  11894. }
  11895. } else {
  11896. return callback();
  11897. }
  11898. }
  11899. withSoloAwaitPermittingContext(callback) {
  11900. const outerContextSoloAwaitState = this.state.soloAwait;
  11901. this.state.soloAwait = true;
  11902. try {
  11903. return callback();
  11904. } finally {
  11905. this.state.soloAwait = outerContextSoloAwaitState;
  11906. }
  11907. }
  11908. allowInAnd(callback) {
  11909. const flags = this.prodParam.currentFlags();
  11910. const prodParamToSet = 8 & ~flags;
  11911. if (prodParamToSet) {
  11912. this.prodParam.enter(flags | 8);
  11913. try {
  11914. return callback();
  11915. } finally {
  11916. this.prodParam.exit();
  11917. }
  11918. }
  11919. return callback();
  11920. }
  11921. disallowInAnd(callback) {
  11922. const flags = this.prodParam.currentFlags();
  11923. const prodParamToClear = 8 & flags;
  11924. if (prodParamToClear) {
  11925. this.prodParam.enter(flags & ~8);
  11926. try {
  11927. return callback();
  11928. } finally {
  11929. this.prodParam.exit();
  11930. }
  11931. }
  11932. return callback();
  11933. }
  11934. registerTopicReference() {
  11935. this.state.topicContext.maxTopicIndex = 0;
  11936. }
  11937. topicReferenceIsAllowedInCurrentContext() {
  11938. return this.state.topicContext.maxNumOfResolvableTopics >= 1;
  11939. }
  11940. topicReferenceWasUsedInCurrentContext() {
  11941. return this.state.topicContext.maxTopicIndex != null && this.state.topicContext.maxTopicIndex >= 0;
  11942. }
  11943. parseFSharpPipelineBody(prec) {
  11944. const startLoc = this.state.startLoc;
  11945. this.state.potentialArrowAt = this.state.start;
  11946. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  11947. this.state.inFSharpPipelineDirectBody = true;
  11948. const ret = this.parseExprOp(this.parseMaybeUnaryOrPrivate(), startLoc, prec);
  11949. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  11950. return ret;
  11951. }
  11952. parseModuleExpression() {
  11953. this.expectPlugin("moduleBlocks");
  11954. const node = this.startNode();
  11955. this.next();
  11956. if (!this.match(5)) {
  11957. this.unexpected(null, 5);
  11958. }
  11959. const program = this.startNodeAt(this.state.endLoc);
  11960. this.next();
  11961. const revertScopes = this.initializeScopes(true);
  11962. this.enterInitialScopes();
  11963. try {
  11964. node.body = this.parseProgram(program, 8, "module");
  11965. } finally {
  11966. revertScopes();
  11967. }
  11968. return this.finishNode(node, "ModuleExpression");
  11969. }
  11970. parsePropertyNamePrefixOperator(prop) {}
  11971. }
  11972. const loopLabel = {
  11973. kind: 1
  11974. },
  11975. switchLabel = {
  11976. kind: 2
  11977. };
  11978. const loneSurrogate = /[\uD800-\uDFFF]/u;
  11979. const keywordRelationalOperator = /in(?:stanceof)?/y;
  11980. function babel7CompatTokens(tokens, input) {
  11981. for (let i = 0; i < tokens.length; i++) {
  11982. const token = tokens[i];
  11983. const {
  11984. type
  11985. } = token;
  11986. if (typeof type === "number") {
  11987. {
  11988. if (type === 138) {
  11989. const {
  11990. loc,
  11991. start,
  11992. value,
  11993. end
  11994. } = token;
  11995. const hashEndPos = start + 1;
  11996. const hashEndLoc = createPositionWithColumnOffset(loc.start, 1);
  11997. tokens.splice(i, 1, new Token({
  11998. type: getExportedToken(27),
  11999. value: "#",
  12000. start: start,
  12001. end: hashEndPos,
  12002. startLoc: loc.start,
  12003. endLoc: hashEndLoc
  12004. }), new Token({
  12005. type: getExportedToken(132),
  12006. value: value,
  12007. start: hashEndPos,
  12008. end: end,
  12009. startLoc: hashEndLoc,
  12010. endLoc: loc.end
  12011. }));
  12012. i++;
  12013. continue;
  12014. }
  12015. if (tokenIsTemplate(type)) {
  12016. const {
  12017. loc,
  12018. start,
  12019. value,
  12020. end
  12021. } = token;
  12022. const backquoteEnd = start + 1;
  12023. const backquoteEndLoc = createPositionWithColumnOffset(loc.start, 1);
  12024. let startToken;
  12025. if (input.charCodeAt(start) === 96) {
  12026. startToken = new Token({
  12027. type: getExportedToken(22),
  12028. value: "`",
  12029. start: start,
  12030. end: backquoteEnd,
  12031. startLoc: loc.start,
  12032. endLoc: backquoteEndLoc
  12033. });
  12034. } else {
  12035. startToken = new Token({
  12036. type: getExportedToken(8),
  12037. value: "}",
  12038. start: start,
  12039. end: backquoteEnd,
  12040. startLoc: loc.start,
  12041. endLoc: backquoteEndLoc
  12042. });
  12043. }
  12044. let templateValue, templateElementEnd, templateElementEndLoc, endToken;
  12045. if (type === 24) {
  12046. templateElementEnd = end - 1;
  12047. templateElementEndLoc = createPositionWithColumnOffset(loc.end, -1);
  12048. templateValue = value === null ? null : value.slice(1, -1);
  12049. endToken = new Token({
  12050. type: getExportedToken(22),
  12051. value: "`",
  12052. start: templateElementEnd,
  12053. end: end,
  12054. startLoc: templateElementEndLoc,
  12055. endLoc: loc.end
  12056. });
  12057. } else {
  12058. templateElementEnd = end - 2;
  12059. templateElementEndLoc = createPositionWithColumnOffset(loc.end, -2);
  12060. templateValue = value === null ? null : value.slice(1, -2);
  12061. endToken = new Token({
  12062. type: getExportedToken(23),
  12063. value: "${",
  12064. start: templateElementEnd,
  12065. end: end,
  12066. startLoc: templateElementEndLoc,
  12067. endLoc: loc.end
  12068. });
  12069. }
  12070. tokens.splice(i, 1, startToken, new Token({
  12071. type: getExportedToken(20),
  12072. value: templateValue,
  12073. start: backquoteEnd,
  12074. end: templateElementEnd,
  12075. startLoc: backquoteEndLoc,
  12076. endLoc: templateElementEndLoc
  12077. }), endToken);
  12078. i += 2;
  12079. continue;
  12080. }
  12081. }
  12082. token.type = getExportedToken(type);
  12083. }
  12084. }
  12085. return tokens;
  12086. }
  12087. class StatementParser extends ExpressionParser {
  12088. parseTopLevel(file, program) {
  12089. file.program = this.parseProgram(program);
  12090. file.comments = this.comments;
  12091. if (this.options.tokens) {
  12092. file.tokens = babel7CompatTokens(this.tokens, this.input);
  12093. }
  12094. return this.finishNode(file, "File");
  12095. }
  12096. parseProgram(program, end = 139, sourceType = this.options.sourceType) {
  12097. program.sourceType = sourceType;
  12098. program.interpreter = this.parseInterpreterDirective();
  12099. this.parseBlockBody(program, true, true, end);
  12100. if (this.inModule && !this.options.allowUndeclaredExports && this.scope.undefinedExports.size > 0) {
  12101. for (const [localName, at] of Array.from(this.scope.undefinedExports)) {
  12102. this.raise(Errors.ModuleExportUndefined, at, {
  12103. localName
  12104. });
  12105. }
  12106. }
  12107. let finishedProgram;
  12108. if (end === 139) {
  12109. finishedProgram = this.finishNode(program, "Program");
  12110. } else {
  12111. finishedProgram = this.finishNodeAt(program, "Program", createPositionWithColumnOffset(this.state.startLoc, -1));
  12112. }
  12113. return finishedProgram;
  12114. }
  12115. stmtToDirective(stmt) {
  12116. const directive = stmt;
  12117. directive.type = "Directive";
  12118. directive.value = directive.expression;
  12119. delete directive.expression;
  12120. const directiveLiteral = directive.value;
  12121. const expressionValue = directiveLiteral.value;
  12122. const raw = this.input.slice(directiveLiteral.start, directiveLiteral.end);
  12123. const val = directiveLiteral.value = raw.slice(1, -1);
  12124. this.addExtra(directiveLiteral, "raw", raw);
  12125. this.addExtra(directiveLiteral, "rawValue", val);
  12126. this.addExtra(directiveLiteral, "expressionValue", expressionValue);
  12127. directiveLiteral.type = "DirectiveLiteral";
  12128. return directive;
  12129. }
  12130. parseInterpreterDirective() {
  12131. if (!this.match(28)) {
  12132. return null;
  12133. }
  12134. const node = this.startNode();
  12135. node.value = this.state.value;
  12136. this.next();
  12137. return this.finishNode(node, "InterpreterDirective");
  12138. }
  12139. isLet() {
  12140. if (!this.isContextual(100)) {
  12141. return false;
  12142. }
  12143. return this.hasFollowingBindingAtom();
  12144. }
  12145. chStartsBindingIdentifier(ch, pos) {
  12146. if (isIdentifierStart(ch)) {
  12147. keywordRelationalOperator.lastIndex = pos;
  12148. if (keywordRelationalOperator.test(this.input)) {
  12149. const endCh = this.codePointAtPos(keywordRelationalOperator.lastIndex);
  12150. if (!isIdentifierChar(endCh) && endCh !== 92) {
  12151. return false;
  12152. }
  12153. }
  12154. return true;
  12155. } else if (ch === 92) {
  12156. return true;
  12157. } else {
  12158. return false;
  12159. }
  12160. }
  12161. chStartsBindingPattern(ch) {
  12162. return ch === 91 || ch === 123;
  12163. }
  12164. hasFollowingBindingAtom() {
  12165. const next = this.nextTokenStart();
  12166. const nextCh = this.codePointAtPos(next);
  12167. return this.chStartsBindingPattern(nextCh) || this.chStartsBindingIdentifier(nextCh, next);
  12168. }
  12169. hasInLineFollowingBindingIdentifier() {
  12170. const next = this.nextTokenInLineStart();
  12171. const nextCh = this.codePointAtPos(next);
  12172. return this.chStartsBindingIdentifier(nextCh, next);
  12173. }
  12174. startsUsingForOf() {
  12175. const {
  12176. type,
  12177. containsEsc
  12178. } = this.lookahead();
  12179. if (type === 102 && !containsEsc) {
  12180. return false;
  12181. } else if (tokenIsIdentifier(type) && !this.hasFollowingLineBreak()) {
  12182. this.expectPlugin("explicitResourceManagement");
  12183. return true;
  12184. }
  12185. }
  12186. startsAwaitUsing() {
  12187. let next = this.nextTokenInLineStart();
  12188. if (this.isUnparsedContextual(next, "using")) {
  12189. next = this.nextTokenInLineStartSince(next + 5);
  12190. const nextCh = this.codePointAtPos(next);
  12191. if (this.chStartsBindingIdentifier(nextCh, next)) {
  12192. this.expectPlugin("explicitResourceManagement");
  12193. return true;
  12194. }
  12195. }
  12196. return false;
  12197. }
  12198. parseModuleItem() {
  12199. return this.parseStatementLike(1 | 2 | 4 | 8);
  12200. }
  12201. parseStatementListItem() {
  12202. return this.parseStatementLike(2 | 4 | (!this.options.annexB || this.state.strict ? 0 : 8));
  12203. }
  12204. parseStatementOrSloppyAnnexBFunctionDeclaration(allowLabeledFunction = false) {
  12205. let flags = 0;
  12206. if (this.options.annexB && !this.state.strict) {
  12207. flags |= 4;
  12208. if (allowLabeledFunction) {
  12209. flags |= 8;
  12210. }
  12211. }
  12212. return this.parseStatementLike(flags);
  12213. }
  12214. parseStatement() {
  12215. return this.parseStatementLike(0);
  12216. }
  12217. parseStatementLike(flags) {
  12218. let decorators = null;
  12219. if (this.match(26)) {
  12220. decorators = this.parseDecorators(true);
  12221. }
  12222. return this.parseStatementContent(flags, decorators);
  12223. }
  12224. parseStatementContent(flags, decorators) {
  12225. const starttype = this.state.type;
  12226. const node = this.startNode();
  12227. const allowDeclaration = !!(flags & 2);
  12228. const allowFunctionDeclaration = !!(flags & 4);
  12229. const topLevel = flags & 1;
  12230. switch (starttype) {
  12231. case 60:
  12232. return this.parseBreakContinueStatement(node, true);
  12233. case 63:
  12234. return this.parseBreakContinueStatement(node, false);
  12235. case 64:
  12236. return this.parseDebuggerStatement(node);
  12237. case 90:
  12238. return this.parseDoWhileStatement(node);
  12239. case 91:
  12240. return this.parseForStatement(node);
  12241. case 68:
  12242. if (this.lookaheadCharCode() === 46) break;
  12243. if (!allowFunctionDeclaration) {
  12244. this.raise(this.state.strict ? Errors.StrictFunction : this.options.annexB ? Errors.SloppyFunctionAnnexB : Errors.SloppyFunction, this.state.startLoc);
  12245. }
  12246. return this.parseFunctionStatement(node, false, !allowDeclaration && allowFunctionDeclaration);
  12247. case 80:
  12248. if (!allowDeclaration) this.unexpected();
  12249. return this.parseClass(this.maybeTakeDecorators(decorators, node), true);
  12250. case 69:
  12251. return this.parseIfStatement(node);
  12252. case 70:
  12253. return this.parseReturnStatement(node);
  12254. case 71:
  12255. return this.parseSwitchStatement(node);
  12256. case 72:
  12257. return this.parseThrowStatement(node);
  12258. case 73:
  12259. return this.parseTryStatement(node);
  12260. case 96:
  12261. if (!this.state.containsEsc && this.startsAwaitUsing()) {
  12262. if (!this.isAwaitAllowed()) {
  12263. this.raise(Errors.AwaitUsingNotInAsyncContext, node);
  12264. } else if (!allowDeclaration) {
  12265. this.raise(Errors.UnexpectedLexicalDeclaration, node);
  12266. }
  12267. this.next();
  12268. return this.parseVarStatement(node, "await using");
  12269. }
  12270. break;
  12271. case 107:
  12272. if (this.state.containsEsc || !this.hasInLineFollowingBindingIdentifier()) {
  12273. break;
  12274. }
  12275. this.expectPlugin("explicitResourceManagement");
  12276. if (!this.scope.inModule && this.scope.inTopLevel) {
  12277. this.raise(Errors.UnexpectedUsingDeclaration, this.state.startLoc);
  12278. } else if (!allowDeclaration) {
  12279. this.raise(Errors.UnexpectedLexicalDeclaration, this.state.startLoc);
  12280. }
  12281. return this.parseVarStatement(node, "using");
  12282. case 100:
  12283. {
  12284. if (this.state.containsEsc) {
  12285. break;
  12286. }
  12287. const next = this.nextTokenStart();
  12288. const nextCh = this.codePointAtPos(next);
  12289. if (nextCh !== 91) {
  12290. if (!allowDeclaration && this.hasFollowingLineBreak()) break;
  12291. if (!this.chStartsBindingIdentifier(nextCh, next) && nextCh !== 123) {
  12292. break;
  12293. }
  12294. }
  12295. }
  12296. case 75:
  12297. {
  12298. if (!allowDeclaration) {
  12299. this.raise(Errors.UnexpectedLexicalDeclaration, this.state.startLoc);
  12300. }
  12301. }
  12302. case 74:
  12303. {
  12304. const kind = this.state.value;
  12305. return this.parseVarStatement(node, kind);
  12306. }
  12307. case 92:
  12308. return this.parseWhileStatement(node);
  12309. case 76:
  12310. return this.parseWithStatement(node);
  12311. case 5:
  12312. return this.parseBlock();
  12313. case 13:
  12314. return this.parseEmptyStatement(node);
  12315. case 83:
  12316. {
  12317. const nextTokenCharCode = this.lookaheadCharCode();
  12318. if (nextTokenCharCode === 40 || nextTokenCharCode === 46) {
  12319. break;
  12320. }
  12321. }
  12322. case 82:
  12323. {
  12324. if (!this.options.allowImportExportEverywhere && !topLevel) {
  12325. this.raise(Errors.UnexpectedImportExport, this.state.startLoc);
  12326. }
  12327. this.next();
  12328. let result;
  12329. if (starttype === 83) {
  12330. result = this.parseImport(node);
  12331. if (result.type === "ImportDeclaration" && (!result.importKind || result.importKind === "value")) {
  12332. this.sawUnambiguousESM = true;
  12333. }
  12334. } else {
  12335. result = this.parseExport(node, decorators);
  12336. if (result.type === "ExportNamedDeclaration" && (!result.exportKind || result.exportKind === "value") || result.type === "ExportAllDeclaration" && (!result.exportKind || result.exportKind === "value") || result.type === "ExportDefaultDeclaration") {
  12337. this.sawUnambiguousESM = true;
  12338. }
  12339. }
  12340. this.assertModuleNodeAllowed(result);
  12341. return result;
  12342. }
  12343. default:
  12344. {
  12345. if (this.isAsyncFunction()) {
  12346. if (!allowDeclaration) {
  12347. this.raise(Errors.AsyncFunctionInSingleStatementContext, this.state.startLoc);
  12348. }
  12349. this.next();
  12350. return this.parseFunctionStatement(node, true, !allowDeclaration && allowFunctionDeclaration);
  12351. }
  12352. }
  12353. }
  12354. const maybeName = this.state.value;
  12355. const expr = this.parseExpression();
  12356. if (tokenIsIdentifier(starttype) && expr.type === "Identifier" && this.eat(14)) {
  12357. return this.parseLabeledStatement(node, maybeName, expr, flags);
  12358. } else {
  12359. return this.parseExpressionStatement(node, expr, decorators);
  12360. }
  12361. }
  12362. assertModuleNodeAllowed(node) {
  12363. if (!this.options.allowImportExportEverywhere && !this.inModule) {
  12364. this.raise(Errors.ImportOutsideModule, node);
  12365. }
  12366. }
  12367. decoratorsEnabledBeforeExport() {
  12368. if (this.hasPlugin("decorators-legacy")) return true;
  12369. return this.hasPlugin("decorators") && this.getPluginOption("decorators", "decoratorsBeforeExport") !== false;
  12370. }
  12371. maybeTakeDecorators(maybeDecorators, classNode, exportNode) {
  12372. if (maybeDecorators) {
  12373. if (classNode.decorators && classNode.decorators.length > 0) {
  12374. if (typeof this.getPluginOption("decorators", "decoratorsBeforeExport") !== "boolean") {
  12375. this.raise(Errors.DecoratorsBeforeAfterExport, classNode.decorators[0]);
  12376. }
  12377. classNode.decorators.unshift(...maybeDecorators);
  12378. } else {
  12379. classNode.decorators = maybeDecorators;
  12380. }
  12381. this.resetStartLocationFromNode(classNode, maybeDecorators[0]);
  12382. if (exportNode) this.resetStartLocationFromNode(exportNode, classNode);
  12383. }
  12384. return classNode;
  12385. }
  12386. canHaveLeadingDecorator() {
  12387. return this.match(80);
  12388. }
  12389. parseDecorators(allowExport) {
  12390. const decorators = [];
  12391. do {
  12392. decorators.push(this.parseDecorator());
  12393. } while (this.match(26));
  12394. if (this.match(82)) {
  12395. if (!allowExport) {
  12396. this.unexpected();
  12397. }
  12398. if (!this.decoratorsEnabledBeforeExport()) {
  12399. this.raise(Errors.DecoratorExportClass, this.state.startLoc);
  12400. }
  12401. } else if (!this.canHaveLeadingDecorator()) {
  12402. throw this.raise(Errors.UnexpectedLeadingDecorator, this.state.startLoc);
  12403. }
  12404. return decorators;
  12405. }
  12406. parseDecorator() {
  12407. this.expectOnePlugin(["decorators", "decorators-legacy"]);
  12408. const node = this.startNode();
  12409. this.next();
  12410. if (this.hasPlugin("decorators")) {
  12411. const startLoc = this.state.startLoc;
  12412. let expr;
  12413. if (this.match(10)) {
  12414. const startLoc = this.state.startLoc;
  12415. this.next();
  12416. expr = this.parseExpression();
  12417. this.expect(11);
  12418. expr = this.wrapParenthesis(startLoc, expr);
  12419. const paramsStartLoc = this.state.startLoc;
  12420. node.expression = this.parseMaybeDecoratorArguments(expr);
  12421. if (this.getPluginOption("decorators", "allowCallParenthesized") === false && node.expression !== expr) {
  12422. this.raise(Errors.DecoratorArgumentsOutsideParentheses, paramsStartLoc);
  12423. }
  12424. } else {
  12425. expr = this.parseIdentifier(false);
  12426. while (this.eat(16)) {
  12427. const node = this.startNodeAt(startLoc);
  12428. node.object = expr;
  12429. if (this.match(138)) {
  12430. this.classScope.usePrivateName(this.state.value, this.state.startLoc);
  12431. node.property = this.parsePrivateName();
  12432. } else {
  12433. node.property = this.parseIdentifier(true);
  12434. }
  12435. node.computed = false;
  12436. expr = this.finishNode(node, "MemberExpression");
  12437. }
  12438. node.expression = this.parseMaybeDecoratorArguments(expr);
  12439. }
  12440. } else {
  12441. node.expression = this.parseExprSubscripts();
  12442. }
  12443. return this.finishNode(node, "Decorator");
  12444. }
  12445. parseMaybeDecoratorArguments(expr) {
  12446. if (this.eat(10)) {
  12447. const node = this.startNodeAtNode(expr);
  12448. node.callee = expr;
  12449. node.arguments = this.parseCallExpressionArguments(11, false);
  12450. this.toReferencedList(node.arguments);
  12451. return this.finishNode(node, "CallExpression");
  12452. }
  12453. return expr;
  12454. }
  12455. parseBreakContinueStatement(node, isBreak) {
  12456. this.next();
  12457. if (this.isLineTerminator()) {
  12458. node.label = null;
  12459. } else {
  12460. node.label = this.parseIdentifier();
  12461. this.semicolon();
  12462. }
  12463. this.verifyBreakContinue(node, isBreak);
  12464. return this.finishNode(node, isBreak ? "BreakStatement" : "ContinueStatement");
  12465. }
  12466. verifyBreakContinue(node, isBreak) {
  12467. let i;
  12468. for (i = 0; i < this.state.labels.length; ++i) {
  12469. const lab = this.state.labels[i];
  12470. if (node.label == null || lab.name === node.label.name) {
  12471. if (lab.kind != null && (isBreak || lab.kind === 1)) {
  12472. break;
  12473. }
  12474. if (node.label && isBreak) break;
  12475. }
  12476. }
  12477. if (i === this.state.labels.length) {
  12478. const type = isBreak ? "BreakStatement" : "ContinueStatement";
  12479. this.raise(Errors.IllegalBreakContinue, node, {
  12480. type
  12481. });
  12482. }
  12483. }
  12484. parseDebuggerStatement(node) {
  12485. this.next();
  12486. this.semicolon();
  12487. return this.finishNode(node, "DebuggerStatement");
  12488. }
  12489. parseHeaderExpression() {
  12490. this.expect(10);
  12491. const val = this.parseExpression();
  12492. this.expect(11);
  12493. return val;
  12494. }
  12495. parseDoWhileStatement(node) {
  12496. this.next();
  12497. this.state.labels.push(loopLabel);
  12498. node.body = this.withSmartMixTopicForbiddingContext(() => this.parseStatement());
  12499. this.state.labels.pop();
  12500. this.expect(92);
  12501. node.test = this.parseHeaderExpression();
  12502. this.eat(13);
  12503. return this.finishNode(node, "DoWhileStatement");
  12504. }
  12505. parseForStatement(node) {
  12506. this.next();
  12507. this.state.labels.push(loopLabel);
  12508. let awaitAt = null;
  12509. if (this.isAwaitAllowed() && this.eatContextual(96)) {
  12510. awaitAt = this.state.lastTokStartLoc;
  12511. }
  12512. this.scope.enter(0);
  12513. this.expect(10);
  12514. if (this.match(13)) {
  12515. if (awaitAt !== null) {
  12516. this.unexpected(awaitAt);
  12517. }
  12518. return this.parseFor(node, null);
  12519. }
  12520. const startsWithLet = this.isContextual(100);
  12521. {
  12522. const startsWithAwaitUsing = this.isContextual(96) && this.startsAwaitUsing();
  12523. const starsWithUsingDeclaration = startsWithAwaitUsing || this.isContextual(107) && this.startsUsingForOf();
  12524. const isLetOrUsing = startsWithLet && this.hasFollowingBindingAtom() || starsWithUsingDeclaration;
  12525. if (this.match(74) || this.match(75) || isLetOrUsing) {
  12526. const initNode = this.startNode();
  12527. let kind;
  12528. if (startsWithAwaitUsing) {
  12529. kind = "await using";
  12530. if (!this.isAwaitAllowed()) {
  12531. this.raise(Errors.AwaitUsingNotInAsyncContext, this.state.startLoc);
  12532. }
  12533. this.next();
  12534. } else {
  12535. kind = this.state.value;
  12536. }
  12537. this.next();
  12538. this.parseVar(initNode, true, kind);
  12539. const init = this.finishNode(initNode, "VariableDeclaration");
  12540. const isForIn = this.match(58);
  12541. if (isForIn && starsWithUsingDeclaration) {
  12542. this.raise(Errors.ForInUsing, init);
  12543. }
  12544. if ((isForIn || this.isContextual(102)) && init.declarations.length === 1) {
  12545. return this.parseForIn(node, init, awaitAt);
  12546. }
  12547. if (awaitAt !== null) {
  12548. this.unexpected(awaitAt);
  12549. }
  12550. return this.parseFor(node, init);
  12551. }
  12552. }
  12553. const startsWithAsync = this.isContextual(95);
  12554. const refExpressionErrors = new ExpressionErrors();
  12555. const init = this.parseExpression(true, refExpressionErrors);
  12556. const isForOf = this.isContextual(102);
  12557. if (isForOf) {
  12558. if (startsWithLet) {
  12559. this.raise(Errors.ForOfLet, init);
  12560. }
  12561. if (awaitAt === null && startsWithAsync && init.type === "Identifier") {
  12562. this.raise(Errors.ForOfAsync, init);
  12563. }
  12564. }
  12565. if (isForOf || this.match(58)) {
  12566. this.checkDestructuringPrivate(refExpressionErrors);
  12567. this.toAssignable(init, true);
  12568. const type = isForOf ? "ForOfStatement" : "ForInStatement";
  12569. this.checkLVal(init, {
  12570. in: {
  12571. type
  12572. }
  12573. });
  12574. return this.parseForIn(node, init, awaitAt);
  12575. } else {
  12576. this.checkExpressionErrors(refExpressionErrors, true);
  12577. }
  12578. if (awaitAt !== null) {
  12579. this.unexpected(awaitAt);
  12580. }
  12581. return this.parseFor(node, init);
  12582. }
  12583. parseFunctionStatement(node, isAsync, isHangingDeclaration) {
  12584. this.next();
  12585. return this.parseFunction(node, 1 | (isHangingDeclaration ? 2 : 0) | (isAsync ? 8 : 0));
  12586. }
  12587. parseIfStatement(node) {
  12588. this.next();
  12589. node.test = this.parseHeaderExpression();
  12590. node.consequent = this.parseStatementOrSloppyAnnexBFunctionDeclaration();
  12591. node.alternate = this.eat(66) ? this.parseStatementOrSloppyAnnexBFunctionDeclaration() : null;
  12592. return this.finishNode(node, "IfStatement");
  12593. }
  12594. parseReturnStatement(node) {
  12595. if (!this.prodParam.hasReturn && !this.options.allowReturnOutsideFunction) {
  12596. this.raise(Errors.IllegalReturn, this.state.startLoc);
  12597. }
  12598. this.next();
  12599. if (this.isLineTerminator()) {
  12600. node.argument = null;
  12601. } else {
  12602. node.argument = this.parseExpression();
  12603. this.semicolon();
  12604. }
  12605. return this.finishNode(node, "ReturnStatement");
  12606. }
  12607. parseSwitchStatement(node) {
  12608. this.next();
  12609. node.discriminant = this.parseHeaderExpression();
  12610. const cases = node.cases = [];
  12611. this.expect(5);
  12612. this.state.labels.push(switchLabel);
  12613. this.scope.enter(0);
  12614. let cur;
  12615. for (let sawDefault; !this.match(8);) {
  12616. if (this.match(61) || this.match(65)) {
  12617. const isCase = this.match(61);
  12618. if (cur) this.finishNode(cur, "SwitchCase");
  12619. cases.push(cur = this.startNode());
  12620. cur.consequent = [];
  12621. this.next();
  12622. if (isCase) {
  12623. cur.test = this.parseExpression();
  12624. } else {
  12625. if (sawDefault) {
  12626. this.raise(Errors.MultipleDefaultsInSwitch, this.state.lastTokStartLoc);
  12627. }
  12628. sawDefault = true;
  12629. cur.test = null;
  12630. }
  12631. this.expect(14);
  12632. } else {
  12633. if (cur) {
  12634. cur.consequent.push(this.parseStatementListItem());
  12635. } else {
  12636. this.unexpected();
  12637. }
  12638. }
  12639. }
  12640. this.scope.exit();
  12641. if (cur) this.finishNode(cur, "SwitchCase");
  12642. this.next();
  12643. this.state.labels.pop();
  12644. return this.finishNode(node, "SwitchStatement");
  12645. }
  12646. parseThrowStatement(node) {
  12647. this.next();
  12648. if (this.hasPrecedingLineBreak()) {
  12649. this.raise(Errors.NewlineAfterThrow, this.state.lastTokEndLoc);
  12650. }
  12651. node.argument = this.parseExpression();
  12652. this.semicolon();
  12653. return this.finishNode(node, "ThrowStatement");
  12654. }
  12655. parseCatchClauseParam() {
  12656. const param = this.parseBindingAtom();
  12657. this.scope.enter(this.options.annexB && param.type === "Identifier" ? 8 : 0);
  12658. this.checkLVal(param, {
  12659. in: {
  12660. type: "CatchClause"
  12661. },
  12662. binding: 9
  12663. });
  12664. return param;
  12665. }
  12666. parseTryStatement(node) {
  12667. this.next();
  12668. node.block = this.parseBlock();
  12669. node.handler = null;
  12670. if (this.match(62)) {
  12671. const clause = this.startNode();
  12672. this.next();
  12673. if (this.match(10)) {
  12674. this.expect(10);
  12675. clause.param = this.parseCatchClauseParam();
  12676. this.expect(11);
  12677. } else {
  12678. clause.param = null;
  12679. this.scope.enter(0);
  12680. }
  12681. clause.body = this.withSmartMixTopicForbiddingContext(() => this.parseBlock(false, false));
  12682. this.scope.exit();
  12683. node.handler = this.finishNode(clause, "CatchClause");
  12684. }
  12685. node.finalizer = this.eat(67) ? this.parseBlock() : null;
  12686. if (!node.handler && !node.finalizer) {
  12687. this.raise(Errors.NoCatchOrFinally, node);
  12688. }
  12689. return this.finishNode(node, "TryStatement");
  12690. }
  12691. parseVarStatement(node, kind, allowMissingInitializer = false) {
  12692. this.next();
  12693. this.parseVar(node, false, kind, allowMissingInitializer);
  12694. this.semicolon();
  12695. return this.finishNode(node, "VariableDeclaration");
  12696. }
  12697. parseWhileStatement(node) {
  12698. this.next();
  12699. node.test = this.parseHeaderExpression();
  12700. this.state.labels.push(loopLabel);
  12701. node.body = this.withSmartMixTopicForbiddingContext(() => this.parseStatement());
  12702. this.state.labels.pop();
  12703. return this.finishNode(node, "WhileStatement");
  12704. }
  12705. parseWithStatement(node) {
  12706. if (this.state.strict) {
  12707. this.raise(Errors.StrictWith, this.state.startLoc);
  12708. }
  12709. this.next();
  12710. node.object = this.parseHeaderExpression();
  12711. node.body = this.withSmartMixTopicForbiddingContext(() => this.parseStatement());
  12712. return this.finishNode(node, "WithStatement");
  12713. }
  12714. parseEmptyStatement(node) {
  12715. this.next();
  12716. return this.finishNode(node, "EmptyStatement");
  12717. }
  12718. parseLabeledStatement(node, maybeName, expr, flags) {
  12719. for (const label of this.state.labels) {
  12720. if (label.name === maybeName) {
  12721. this.raise(Errors.LabelRedeclaration, expr, {
  12722. labelName: maybeName
  12723. });
  12724. }
  12725. }
  12726. const kind = tokenIsLoop(this.state.type) ? 1 : this.match(71) ? 2 : null;
  12727. for (let i = this.state.labels.length - 1; i >= 0; i--) {
  12728. const label = this.state.labels[i];
  12729. if (label.statementStart === node.start) {
  12730. label.statementStart = this.state.start;
  12731. label.kind = kind;
  12732. } else {
  12733. break;
  12734. }
  12735. }
  12736. this.state.labels.push({
  12737. name: maybeName,
  12738. kind: kind,
  12739. statementStart: this.state.start
  12740. });
  12741. node.body = flags & 8 ? this.parseStatementOrSloppyAnnexBFunctionDeclaration(true) : this.parseStatement();
  12742. this.state.labels.pop();
  12743. node.label = expr;
  12744. return this.finishNode(node, "LabeledStatement");
  12745. }
  12746. parseExpressionStatement(node, expr, decorators) {
  12747. node.expression = expr;
  12748. this.semicolon();
  12749. return this.finishNode(node, "ExpressionStatement");
  12750. }
  12751. parseBlock(allowDirectives = false, createNewLexicalScope = true, afterBlockParse) {
  12752. const node = this.startNode();
  12753. if (allowDirectives) {
  12754. this.state.strictErrors.clear();
  12755. }
  12756. this.expect(5);
  12757. if (createNewLexicalScope) {
  12758. this.scope.enter(0);
  12759. }
  12760. this.parseBlockBody(node, allowDirectives, false, 8, afterBlockParse);
  12761. if (createNewLexicalScope) {
  12762. this.scope.exit();
  12763. }
  12764. return this.finishNode(node, "BlockStatement");
  12765. }
  12766. isValidDirective(stmt) {
  12767. return stmt.type === "ExpressionStatement" && stmt.expression.type === "StringLiteral" && !stmt.expression.extra.parenthesized;
  12768. }
  12769. parseBlockBody(node, allowDirectives, topLevel, end, afterBlockParse) {
  12770. const body = node.body = [];
  12771. const directives = node.directives = [];
  12772. this.parseBlockOrModuleBlockBody(body, allowDirectives ? directives : undefined, topLevel, end, afterBlockParse);
  12773. }
  12774. parseBlockOrModuleBlockBody(body, directives, topLevel, end, afterBlockParse) {
  12775. const oldStrict = this.state.strict;
  12776. let hasStrictModeDirective = false;
  12777. let parsedNonDirective = false;
  12778. while (!this.match(end)) {
  12779. const stmt = topLevel ? this.parseModuleItem() : this.parseStatementListItem();
  12780. if (directives && !parsedNonDirective) {
  12781. if (this.isValidDirective(stmt)) {
  12782. const directive = this.stmtToDirective(stmt);
  12783. directives.push(directive);
  12784. if (!hasStrictModeDirective && directive.value.value === "use strict") {
  12785. hasStrictModeDirective = true;
  12786. this.setStrict(true);
  12787. }
  12788. continue;
  12789. }
  12790. parsedNonDirective = true;
  12791. this.state.strictErrors.clear();
  12792. }
  12793. body.push(stmt);
  12794. }
  12795. afterBlockParse == null || afterBlockParse.call(this, hasStrictModeDirective);
  12796. if (!oldStrict) {
  12797. this.setStrict(false);
  12798. }
  12799. this.next();
  12800. }
  12801. parseFor(node, init) {
  12802. node.init = init;
  12803. this.semicolon(false);
  12804. node.test = this.match(13) ? null : this.parseExpression();
  12805. this.semicolon(false);
  12806. node.update = this.match(11) ? null : this.parseExpression();
  12807. this.expect(11);
  12808. node.body = this.withSmartMixTopicForbiddingContext(() => this.parseStatement());
  12809. this.scope.exit();
  12810. this.state.labels.pop();
  12811. return this.finishNode(node, "ForStatement");
  12812. }
  12813. parseForIn(node, init, awaitAt) {
  12814. const isForIn = this.match(58);
  12815. this.next();
  12816. if (isForIn) {
  12817. if (awaitAt !== null) this.unexpected(awaitAt);
  12818. } else {
  12819. node.await = awaitAt !== null;
  12820. }
  12821. if (init.type === "VariableDeclaration" && init.declarations[0].init != null && (!isForIn || !this.options.annexB || this.state.strict || init.kind !== "var" || init.declarations[0].id.type !== "Identifier")) {
  12822. this.raise(Errors.ForInOfLoopInitializer, init, {
  12823. type: isForIn ? "ForInStatement" : "ForOfStatement"
  12824. });
  12825. }
  12826. if (init.type === "AssignmentPattern") {
  12827. this.raise(Errors.InvalidLhs, init, {
  12828. ancestor: {
  12829. type: "ForStatement"
  12830. }
  12831. });
  12832. }
  12833. node.left = init;
  12834. node.right = isForIn ? this.parseExpression() : this.parseMaybeAssignAllowIn();
  12835. this.expect(11);
  12836. node.body = this.withSmartMixTopicForbiddingContext(() => this.parseStatement());
  12837. this.scope.exit();
  12838. this.state.labels.pop();
  12839. return this.finishNode(node, isForIn ? "ForInStatement" : "ForOfStatement");
  12840. }
  12841. parseVar(node, isFor, kind, allowMissingInitializer = false) {
  12842. const declarations = node.declarations = [];
  12843. node.kind = kind;
  12844. for (;;) {
  12845. const decl = this.startNode();
  12846. this.parseVarId(decl, kind);
  12847. decl.init = !this.eat(29) ? null : isFor ? this.parseMaybeAssignDisallowIn() : this.parseMaybeAssignAllowIn();
  12848. if (decl.init === null && !allowMissingInitializer) {
  12849. if (decl.id.type !== "Identifier" && !(isFor && (this.match(58) || this.isContextual(102)))) {
  12850. this.raise(Errors.DeclarationMissingInitializer, this.state.lastTokEndLoc, {
  12851. kind: "destructuring"
  12852. });
  12853. } else if ((kind === "const" || kind === "using" || kind === "await using") && !(this.match(58) || this.isContextual(102))) {
  12854. this.raise(Errors.DeclarationMissingInitializer, this.state.lastTokEndLoc, {
  12855. kind
  12856. });
  12857. }
  12858. }
  12859. declarations.push(this.finishNode(decl, "VariableDeclarator"));
  12860. if (!this.eat(12)) break;
  12861. }
  12862. return node;
  12863. }
  12864. parseVarId(decl, kind) {
  12865. const id = this.parseBindingAtom();
  12866. this.checkLVal(id, {
  12867. in: {
  12868. type: "VariableDeclarator"
  12869. },
  12870. binding: kind === "var" ? 5 : 8201
  12871. });
  12872. decl.id = id;
  12873. }
  12874. parseAsyncFunctionExpression(node) {
  12875. return this.parseFunction(node, 8);
  12876. }
  12877. parseFunction(node, flags = 0) {
  12878. const hangingDeclaration = flags & 2;
  12879. const isDeclaration = !!(flags & 1);
  12880. const requireId = isDeclaration && !(flags & 4);
  12881. const isAsync = !!(flags & 8);
  12882. this.initFunction(node, isAsync);
  12883. if (this.match(55)) {
  12884. if (hangingDeclaration) {
  12885. this.raise(Errors.GeneratorInSingleStatementContext, this.state.startLoc);
  12886. }
  12887. this.next();
  12888. node.generator = true;
  12889. }
  12890. if (isDeclaration) {
  12891. node.id = this.parseFunctionId(requireId);
  12892. }
  12893. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  12894. this.state.maybeInArrowParameters = false;
  12895. this.scope.enter(2);
  12896. this.prodParam.enter(functionFlags(isAsync, node.generator));
  12897. if (!isDeclaration) {
  12898. node.id = this.parseFunctionId();
  12899. }
  12900. this.parseFunctionParams(node, false);
  12901. this.withSmartMixTopicForbiddingContext(() => {
  12902. this.parseFunctionBodyAndFinish(node, isDeclaration ? "FunctionDeclaration" : "FunctionExpression");
  12903. });
  12904. this.prodParam.exit();
  12905. this.scope.exit();
  12906. if (isDeclaration && !hangingDeclaration) {
  12907. this.registerFunctionStatementId(node);
  12908. }
  12909. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  12910. return node;
  12911. }
  12912. parseFunctionId(requireId) {
  12913. return requireId || tokenIsIdentifier(this.state.type) ? this.parseIdentifier() : null;
  12914. }
  12915. parseFunctionParams(node, isConstructor) {
  12916. this.expect(10);
  12917. this.expressionScope.enter(newParameterDeclarationScope());
  12918. node.params = this.parseBindingList(11, 41, 2 | (isConstructor ? 4 : 0));
  12919. this.expressionScope.exit();
  12920. }
  12921. registerFunctionStatementId(node) {
  12922. if (!node.id) return;
  12923. this.scope.declareName(node.id.name, !this.options.annexB || this.state.strict || node.generator || node.async ? this.scope.treatFunctionsAsVar ? 5 : 8201 : 17, node.id.loc.start);
  12924. }
  12925. parseClass(node, isStatement, optionalId) {
  12926. this.next();
  12927. const oldStrict = this.state.strict;
  12928. this.state.strict = true;
  12929. this.parseClassId(node, isStatement, optionalId);
  12930. this.parseClassSuper(node);
  12931. node.body = this.parseClassBody(!!node.superClass, oldStrict);
  12932. return this.finishNode(node, isStatement ? "ClassDeclaration" : "ClassExpression");
  12933. }
  12934. isClassProperty() {
  12935. return this.match(29) || this.match(13) || this.match(8);
  12936. }
  12937. isClassMethod() {
  12938. return this.match(10);
  12939. }
  12940. nameIsConstructor(key) {
  12941. return key.type === "Identifier" && key.name === "constructor" || key.type === "StringLiteral" && key.value === "constructor";
  12942. }
  12943. isNonstaticConstructor(method) {
  12944. return !method.computed && !method.static && this.nameIsConstructor(method.key);
  12945. }
  12946. parseClassBody(hadSuperClass, oldStrict) {
  12947. this.classScope.enter();
  12948. const state = {
  12949. hadConstructor: false,
  12950. hadSuperClass
  12951. };
  12952. let decorators = [];
  12953. const classBody = this.startNode();
  12954. classBody.body = [];
  12955. this.expect(5);
  12956. this.withSmartMixTopicForbiddingContext(() => {
  12957. while (!this.match(8)) {
  12958. if (this.eat(13)) {
  12959. if (decorators.length > 0) {
  12960. throw this.raise(Errors.DecoratorSemicolon, this.state.lastTokEndLoc);
  12961. }
  12962. continue;
  12963. }
  12964. if (this.match(26)) {
  12965. decorators.push(this.parseDecorator());
  12966. continue;
  12967. }
  12968. const member = this.startNode();
  12969. if (decorators.length) {
  12970. member.decorators = decorators;
  12971. this.resetStartLocationFromNode(member, decorators[0]);
  12972. decorators = [];
  12973. }
  12974. this.parseClassMember(classBody, member, state);
  12975. if (member.kind === "constructor" && member.decorators && member.decorators.length > 0) {
  12976. this.raise(Errors.DecoratorConstructor, member);
  12977. }
  12978. }
  12979. });
  12980. this.state.strict = oldStrict;
  12981. this.next();
  12982. if (decorators.length) {
  12983. throw this.raise(Errors.TrailingDecorator, this.state.startLoc);
  12984. }
  12985. this.classScope.exit();
  12986. return this.finishNode(classBody, "ClassBody");
  12987. }
  12988. parseClassMemberFromModifier(classBody, member) {
  12989. const key = this.parseIdentifier(true);
  12990. if (this.isClassMethod()) {
  12991. const method = member;
  12992. method.kind = "method";
  12993. method.computed = false;
  12994. method.key = key;
  12995. method.static = false;
  12996. this.pushClassMethod(classBody, method, false, false, false, false);
  12997. return true;
  12998. } else if (this.isClassProperty()) {
  12999. const prop = member;
  13000. prop.computed = false;
  13001. prop.key = key;
  13002. prop.static = false;
  13003. classBody.body.push(this.parseClassProperty(prop));
  13004. return true;
  13005. }
  13006. this.resetPreviousNodeTrailingComments(key);
  13007. return false;
  13008. }
  13009. parseClassMember(classBody, member, state) {
  13010. const isStatic = this.isContextual(106);
  13011. if (isStatic) {
  13012. if (this.parseClassMemberFromModifier(classBody, member)) {
  13013. return;
  13014. }
  13015. if (this.eat(5)) {
  13016. this.parseClassStaticBlock(classBody, member);
  13017. return;
  13018. }
  13019. }
  13020. this.parseClassMemberWithIsStatic(classBody, member, state, isStatic);
  13021. }
  13022. parseClassMemberWithIsStatic(classBody, member, state, isStatic) {
  13023. const publicMethod = member;
  13024. const privateMethod = member;
  13025. const publicProp = member;
  13026. const privateProp = member;
  13027. const accessorProp = member;
  13028. const method = publicMethod;
  13029. const publicMember = publicMethod;
  13030. member.static = isStatic;
  13031. this.parsePropertyNamePrefixOperator(member);
  13032. if (this.eat(55)) {
  13033. method.kind = "method";
  13034. const isPrivateName = this.match(138);
  13035. this.parseClassElementName(method);
  13036. if (isPrivateName) {
  13037. this.pushClassPrivateMethod(classBody, privateMethod, true, false);
  13038. return;
  13039. }
  13040. if (this.isNonstaticConstructor(publicMethod)) {
  13041. this.raise(Errors.ConstructorIsGenerator, publicMethod.key);
  13042. }
  13043. this.pushClassMethod(classBody, publicMethod, true, false, false, false);
  13044. return;
  13045. }
  13046. const isContextual = !this.state.containsEsc && tokenIsIdentifier(this.state.type);
  13047. const key = this.parseClassElementName(member);
  13048. const maybeContextualKw = isContextual ? key.name : null;
  13049. const isPrivate = this.isPrivateName(key);
  13050. const maybeQuestionTokenStartLoc = this.state.startLoc;
  13051. this.parsePostMemberNameModifiers(publicMember);
  13052. if (this.isClassMethod()) {
  13053. method.kind = "method";
  13054. if (isPrivate) {
  13055. this.pushClassPrivateMethod(classBody, privateMethod, false, false);
  13056. return;
  13057. }
  13058. const isConstructor = this.isNonstaticConstructor(publicMethod);
  13059. let allowsDirectSuper = false;
  13060. if (isConstructor) {
  13061. publicMethod.kind = "constructor";
  13062. if (state.hadConstructor && !this.hasPlugin("typescript")) {
  13063. this.raise(Errors.DuplicateConstructor, key);
  13064. }
  13065. if (isConstructor && this.hasPlugin("typescript") && member.override) {
  13066. this.raise(Errors.OverrideOnConstructor, key);
  13067. }
  13068. state.hadConstructor = true;
  13069. allowsDirectSuper = state.hadSuperClass;
  13070. }
  13071. this.pushClassMethod(classBody, publicMethod, false, false, isConstructor, allowsDirectSuper);
  13072. } else if (this.isClassProperty()) {
  13073. if (isPrivate) {
  13074. this.pushClassPrivateProperty(classBody, privateProp);
  13075. } else {
  13076. this.pushClassProperty(classBody, publicProp);
  13077. }
  13078. } else if (maybeContextualKw === "async" && !this.isLineTerminator()) {
  13079. this.resetPreviousNodeTrailingComments(key);
  13080. const isGenerator = this.eat(55);
  13081. if (publicMember.optional) {
  13082. this.unexpected(maybeQuestionTokenStartLoc);
  13083. }
  13084. method.kind = "method";
  13085. const isPrivate = this.match(138);
  13086. this.parseClassElementName(method);
  13087. this.parsePostMemberNameModifiers(publicMember);
  13088. if (isPrivate) {
  13089. this.pushClassPrivateMethod(classBody, privateMethod, isGenerator, true);
  13090. } else {
  13091. if (this.isNonstaticConstructor(publicMethod)) {
  13092. this.raise(Errors.ConstructorIsAsync, publicMethod.key);
  13093. }
  13094. this.pushClassMethod(classBody, publicMethod, isGenerator, true, false, false);
  13095. }
  13096. } else if ((maybeContextualKw === "get" || maybeContextualKw === "set") && !(this.match(55) && this.isLineTerminator())) {
  13097. this.resetPreviousNodeTrailingComments(key);
  13098. method.kind = maybeContextualKw;
  13099. const isPrivate = this.match(138);
  13100. this.parseClassElementName(publicMethod);
  13101. if (isPrivate) {
  13102. this.pushClassPrivateMethod(classBody, privateMethod, false, false);
  13103. } else {
  13104. if (this.isNonstaticConstructor(publicMethod)) {
  13105. this.raise(Errors.ConstructorIsAccessor, publicMethod.key);
  13106. }
  13107. this.pushClassMethod(classBody, publicMethod, false, false, false, false);
  13108. }
  13109. this.checkGetterSetterParams(publicMethod);
  13110. } else if (maybeContextualKw === "accessor" && !this.isLineTerminator()) {
  13111. this.expectPlugin("decoratorAutoAccessors");
  13112. this.resetPreviousNodeTrailingComments(key);
  13113. const isPrivate = this.match(138);
  13114. this.parseClassElementName(publicProp);
  13115. this.pushClassAccessorProperty(classBody, accessorProp, isPrivate);
  13116. } else if (this.isLineTerminator()) {
  13117. if (isPrivate) {
  13118. this.pushClassPrivateProperty(classBody, privateProp);
  13119. } else {
  13120. this.pushClassProperty(classBody, publicProp);
  13121. }
  13122. } else {
  13123. this.unexpected();
  13124. }
  13125. }
  13126. parseClassElementName(member) {
  13127. const {
  13128. type,
  13129. value
  13130. } = this.state;
  13131. if ((type === 132 || type === 133) && member.static && value === "prototype") {
  13132. this.raise(Errors.StaticPrototype, this.state.startLoc);
  13133. }
  13134. if (type === 138) {
  13135. if (value === "constructor") {
  13136. this.raise(Errors.ConstructorClassPrivateField, this.state.startLoc);
  13137. }
  13138. const key = this.parsePrivateName();
  13139. member.key = key;
  13140. return key;
  13141. }
  13142. this.parsePropertyName(member);
  13143. return member.key;
  13144. }
  13145. parseClassStaticBlock(classBody, member) {
  13146. var _member$decorators;
  13147. this.scope.enter(64 | 128 | 16);
  13148. const oldLabels = this.state.labels;
  13149. this.state.labels = [];
  13150. this.prodParam.enter(0);
  13151. const body = member.body = [];
  13152. this.parseBlockOrModuleBlockBody(body, undefined, false, 8);
  13153. this.prodParam.exit();
  13154. this.scope.exit();
  13155. this.state.labels = oldLabels;
  13156. classBody.body.push(this.finishNode(member, "StaticBlock"));
  13157. if ((_member$decorators = member.decorators) != null && _member$decorators.length) {
  13158. this.raise(Errors.DecoratorStaticBlock, member);
  13159. }
  13160. }
  13161. pushClassProperty(classBody, prop) {
  13162. if (!prop.computed && this.nameIsConstructor(prop.key)) {
  13163. this.raise(Errors.ConstructorClassField, prop.key);
  13164. }
  13165. classBody.body.push(this.parseClassProperty(prop));
  13166. }
  13167. pushClassPrivateProperty(classBody, prop) {
  13168. const node = this.parseClassPrivateProperty(prop);
  13169. classBody.body.push(node);
  13170. this.classScope.declarePrivateName(this.getPrivateNameSV(node.key), 0, node.key.loc.start);
  13171. }
  13172. pushClassAccessorProperty(classBody, prop, isPrivate) {
  13173. if (!isPrivate && !prop.computed && this.nameIsConstructor(prop.key)) {
  13174. this.raise(Errors.ConstructorClassField, prop.key);
  13175. }
  13176. const node = this.parseClassAccessorProperty(prop);
  13177. classBody.body.push(node);
  13178. if (isPrivate) {
  13179. this.classScope.declarePrivateName(this.getPrivateNameSV(node.key), 0, node.key.loc.start);
  13180. }
  13181. }
  13182. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  13183. classBody.body.push(this.parseMethod(method, isGenerator, isAsync, isConstructor, allowsDirectSuper, "ClassMethod", true));
  13184. }
  13185. pushClassPrivateMethod(classBody, method, isGenerator, isAsync) {
  13186. const node = this.parseMethod(method, isGenerator, isAsync, false, false, "ClassPrivateMethod", true);
  13187. classBody.body.push(node);
  13188. const kind = node.kind === "get" ? node.static ? 6 : 2 : node.kind === "set" ? node.static ? 5 : 1 : 0;
  13189. this.declareClassPrivateMethodInScope(node, kind);
  13190. }
  13191. declareClassPrivateMethodInScope(node, kind) {
  13192. this.classScope.declarePrivateName(this.getPrivateNameSV(node.key), kind, node.key.loc.start);
  13193. }
  13194. parsePostMemberNameModifiers(methodOrProp) {}
  13195. parseClassPrivateProperty(node) {
  13196. this.parseInitializer(node);
  13197. this.semicolon();
  13198. return this.finishNode(node, "ClassPrivateProperty");
  13199. }
  13200. parseClassProperty(node) {
  13201. this.parseInitializer(node);
  13202. this.semicolon();
  13203. return this.finishNode(node, "ClassProperty");
  13204. }
  13205. parseClassAccessorProperty(node) {
  13206. this.parseInitializer(node);
  13207. this.semicolon();
  13208. return this.finishNode(node, "ClassAccessorProperty");
  13209. }
  13210. parseInitializer(node) {
  13211. this.scope.enter(64 | 16);
  13212. this.expressionScope.enter(newExpressionScope());
  13213. this.prodParam.enter(0);
  13214. node.value = this.eat(29) ? this.parseMaybeAssignAllowIn() : null;
  13215. this.expressionScope.exit();
  13216. this.prodParam.exit();
  13217. this.scope.exit();
  13218. }
  13219. parseClassId(node, isStatement, optionalId, bindingType = 8331) {
  13220. if (tokenIsIdentifier(this.state.type)) {
  13221. node.id = this.parseIdentifier();
  13222. if (isStatement) {
  13223. this.declareNameFromIdentifier(node.id, bindingType);
  13224. }
  13225. } else {
  13226. if (optionalId || !isStatement) {
  13227. node.id = null;
  13228. } else {
  13229. throw this.raise(Errors.MissingClassName, this.state.startLoc);
  13230. }
  13231. }
  13232. }
  13233. parseClassSuper(node) {
  13234. node.superClass = this.eat(81) ? this.parseExprSubscripts() : null;
  13235. }
  13236. parseExport(node, decorators) {
  13237. const maybeDefaultIdentifier = this.parseMaybeImportPhase(node, true);
  13238. const hasDefault = this.maybeParseExportDefaultSpecifier(node, maybeDefaultIdentifier);
  13239. const parseAfterDefault = !hasDefault || this.eat(12);
  13240. const hasStar = parseAfterDefault && this.eatExportStar(node);
  13241. const hasNamespace = hasStar && this.maybeParseExportNamespaceSpecifier(node);
  13242. const parseAfterNamespace = parseAfterDefault && (!hasNamespace || this.eat(12));
  13243. const isFromRequired = hasDefault || hasStar;
  13244. if (hasStar && !hasNamespace) {
  13245. if (hasDefault) this.unexpected();
  13246. if (decorators) {
  13247. throw this.raise(Errors.UnsupportedDecoratorExport, node);
  13248. }
  13249. this.parseExportFrom(node, true);
  13250. return this.finishNode(node, "ExportAllDeclaration");
  13251. }
  13252. const hasSpecifiers = this.maybeParseExportNamedSpecifiers(node);
  13253. if (hasDefault && parseAfterDefault && !hasStar && !hasSpecifiers) {
  13254. this.unexpected(null, 5);
  13255. }
  13256. if (hasNamespace && parseAfterNamespace) {
  13257. this.unexpected(null, 98);
  13258. }
  13259. let hasDeclaration;
  13260. if (isFromRequired || hasSpecifiers) {
  13261. hasDeclaration = false;
  13262. if (decorators) {
  13263. throw this.raise(Errors.UnsupportedDecoratorExport, node);
  13264. }
  13265. this.parseExportFrom(node, isFromRequired);
  13266. } else {
  13267. hasDeclaration = this.maybeParseExportDeclaration(node);
  13268. }
  13269. if (isFromRequired || hasSpecifiers || hasDeclaration) {
  13270. var _node2$declaration;
  13271. const node2 = node;
  13272. this.checkExport(node2, true, false, !!node2.source);
  13273. if (((_node2$declaration = node2.declaration) == null ? void 0 : _node2$declaration.type) === "ClassDeclaration") {
  13274. this.maybeTakeDecorators(decorators, node2.declaration, node2);
  13275. } else if (decorators) {
  13276. throw this.raise(Errors.UnsupportedDecoratorExport, node);
  13277. }
  13278. return this.finishNode(node2, "ExportNamedDeclaration");
  13279. }
  13280. if (this.eat(65)) {
  13281. const node2 = node;
  13282. const decl = this.parseExportDefaultExpression();
  13283. node2.declaration = decl;
  13284. if (decl.type === "ClassDeclaration") {
  13285. this.maybeTakeDecorators(decorators, decl, node2);
  13286. } else if (decorators) {
  13287. throw this.raise(Errors.UnsupportedDecoratorExport, node);
  13288. }
  13289. this.checkExport(node2, true, true);
  13290. return this.finishNode(node2, "ExportDefaultDeclaration");
  13291. }
  13292. this.unexpected(null, 5);
  13293. }
  13294. eatExportStar(node) {
  13295. return this.eat(55);
  13296. }
  13297. maybeParseExportDefaultSpecifier(node, maybeDefaultIdentifier) {
  13298. if (maybeDefaultIdentifier || this.isExportDefaultSpecifier()) {
  13299. this.expectPlugin("exportDefaultFrom", maybeDefaultIdentifier == null ? void 0 : maybeDefaultIdentifier.loc.start);
  13300. const id = maybeDefaultIdentifier || this.parseIdentifier(true);
  13301. const specifier = this.startNodeAtNode(id);
  13302. specifier.exported = id;
  13303. node.specifiers = [this.finishNode(specifier, "ExportDefaultSpecifier")];
  13304. return true;
  13305. }
  13306. return false;
  13307. }
  13308. maybeParseExportNamespaceSpecifier(node) {
  13309. if (this.isContextual(93)) {
  13310. var _ref, _ref$specifiers;
  13311. (_ref$specifiers = (_ref = node).specifiers) != null ? _ref$specifiers : _ref.specifiers = [];
  13312. const specifier = this.startNodeAt(this.state.lastTokStartLoc);
  13313. this.next();
  13314. specifier.exported = this.parseModuleExportName();
  13315. node.specifiers.push(this.finishNode(specifier, "ExportNamespaceSpecifier"));
  13316. return true;
  13317. }
  13318. return false;
  13319. }
  13320. maybeParseExportNamedSpecifiers(node) {
  13321. if (this.match(5)) {
  13322. const node2 = node;
  13323. if (!node2.specifiers) node2.specifiers = [];
  13324. const isTypeExport = node2.exportKind === "type";
  13325. node2.specifiers.push(...this.parseExportSpecifiers(isTypeExport));
  13326. node2.source = null;
  13327. node2.declaration = null;
  13328. if (this.hasPlugin("importAssertions")) {
  13329. node2.assertions = [];
  13330. }
  13331. return true;
  13332. }
  13333. return false;
  13334. }
  13335. maybeParseExportDeclaration(node) {
  13336. if (this.shouldParseExportDeclaration()) {
  13337. node.specifiers = [];
  13338. node.source = null;
  13339. if (this.hasPlugin("importAssertions")) {
  13340. node.assertions = [];
  13341. }
  13342. node.declaration = this.parseExportDeclaration(node);
  13343. return true;
  13344. }
  13345. return false;
  13346. }
  13347. isAsyncFunction() {
  13348. if (!this.isContextual(95)) return false;
  13349. const next = this.nextTokenInLineStart();
  13350. return this.isUnparsedContextual(next, "function");
  13351. }
  13352. parseExportDefaultExpression() {
  13353. const expr = this.startNode();
  13354. if (this.match(68)) {
  13355. this.next();
  13356. return this.parseFunction(expr, 1 | 4);
  13357. } else if (this.isAsyncFunction()) {
  13358. this.next();
  13359. this.next();
  13360. return this.parseFunction(expr, 1 | 4 | 8);
  13361. }
  13362. if (this.match(80)) {
  13363. return this.parseClass(expr, true, true);
  13364. }
  13365. if (this.match(26)) {
  13366. if (this.hasPlugin("decorators") && this.getPluginOption("decorators", "decoratorsBeforeExport") === true) {
  13367. this.raise(Errors.DecoratorBeforeExport, this.state.startLoc);
  13368. }
  13369. return this.parseClass(this.maybeTakeDecorators(this.parseDecorators(false), this.startNode()), true, true);
  13370. }
  13371. if (this.match(75) || this.match(74) || this.isLet()) {
  13372. throw this.raise(Errors.UnsupportedDefaultExport, this.state.startLoc);
  13373. }
  13374. const res = this.parseMaybeAssignAllowIn();
  13375. this.semicolon();
  13376. return res;
  13377. }
  13378. parseExportDeclaration(node) {
  13379. if (this.match(80)) {
  13380. const node = this.parseClass(this.startNode(), true, false);
  13381. return node;
  13382. }
  13383. return this.parseStatementListItem();
  13384. }
  13385. isExportDefaultSpecifier() {
  13386. const {
  13387. type
  13388. } = this.state;
  13389. if (tokenIsIdentifier(type)) {
  13390. if (type === 95 && !this.state.containsEsc || type === 100) {
  13391. return false;
  13392. }
  13393. if ((type === 130 || type === 129) && !this.state.containsEsc) {
  13394. const {
  13395. type: nextType
  13396. } = this.lookahead();
  13397. if (tokenIsIdentifier(nextType) && nextType !== 98 || nextType === 5) {
  13398. this.expectOnePlugin(["flow", "typescript"]);
  13399. return false;
  13400. }
  13401. }
  13402. } else if (!this.match(65)) {
  13403. return false;
  13404. }
  13405. const next = this.nextTokenStart();
  13406. const hasFrom = this.isUnparsedContextual(next, "from");
  13407. if (this.input.charCodeAt(next) === 44 || tokenIsIdentifier(this.state.type) && hasFrom) {
  13408. return true;
  13409. }
  13410. if (this.match(65) && hasFrom) {
  13411. const nextAfterFrom = this.input.charCodeAt(this.nextTokenStartSince(next + 4));
  13412. return nextAfterFrom === 34 || nextAfterFrom === 39;
  13413. }
  13414. return false;
  13415. }
  13416. parseExportFrom(node, expect) {
  13417. if (this.eatContextual(98)) {
  13418. node.source = this.parseImportSource();
  13419. this.checkExport(node);
  13420. this.maybeParseImportAttributes(node);
  13421. this.checkJSONModuleImport(node);
  13422. } else if (expect) {
  13423. this.unexpected();
  13424. }
  13425. this.semicolon();
  13426. }
  13427. shouldParseExportDeclaration() {
  13428. const {
  13429. type
  13430. } = this.state;
  13431. if (type === 26) {
  13432. this.expectOnePlugin(["decorators", "decorators-legacy"]);
  13433. if (this.hasPlugin("decorators")) {
  13434. if (this.getPluginOption("decorators", "decoratorsBeforeExport") === true) {
  13435. this.raise(Errors.DecoratorBeforeExport, this.state.startLoc);
  13436. }
  13437. return true;
  13438. }
  13439. }
  13440. if (this.isContextual(107)) {
  13441. this.raise(Errors.UsingDeclarationExport, this.state.startLoc);
  13442. return true;
  13443. }
  13444. if (this.isContextual(96) && this.startsAwaitUsing()) {
  13445. this.raise(Errors.UsingDeclarationExport, this.state.startLoc);
  13446. return true;
  13447. }
  13448. return type === 74 || type === 75 || type === 68 || type === 80 || this.isLet() || this.isAsyncFunction();
  13449. }
  13450. checkExport(node, checkNames, isDefault, isFrom) {
  13451. if (checkNames) {
  13452. var _node$specifiers;
  13453. if (isDefault) {
  13454. this.checkDuplicateExports(node, "default");
  13455. if (this.hasPlugin("exportDefaultFrom")) {
  13456. var _declaration$extra;
  13457. const declaration = node.declaration;
  13458. if (declaration.type === "Identifier" && declaration.name === "from" && declaration.end - declaration.start === 4 && !((_declaration$extra = declaration.extra) != null && _declaration$extra.parenthesized)) {
  13459. this.raise(Errors.ExportDefaultFromAsIdentifier, declaration);
  13460. }
  13461. }
  13462. } else if ((_node$specifiers = node.specifiers) != null && _node$specifiers.length) {
  13463. for (const specifier of node.specifiers) {
  13464. const {
  13465. exported
  13466. } = specifier;
  13467. const exportName = exported.type === "Identifier" ? exported.name : exported.value;
  13468. this.checkDuplicateExports(specifier, exportName);
  13469. if (!isFrom && specifier.local) {
  13470. const {
  13471. local
  13472. } = specifier;
  13473. if (local.type !== "Identifier") {
  13474. this.raise(Errors.ExportBindingIsString, specifier, {
  13475. localName: local.value,
  13476. exportName
  13477. });
  13478. } else {
  13479. this.checkReservedWord(local.name, local.loc.start, true, false);
  13480. this.scope.checkLocalExport(local);
  13481. }
  13482. }
  13483. }
  13484. } else if (node.declaration) {
  13485. const decl = node.declaration;
  13486. if (decl.type === "FunctionDeclaration" || decl.type === "ClassDeclaration") {
  13487. const {
  13488. id
  13489. } = decl;
  13490. if (!id) throw new Error("Assertion failure");
  13491. this.checkDuplicateExports(node, id.name);
  13492. } else if (decl.type === "VariableDeclaration") {
  13493. for (const declaration of decl.declarations) {
  13494. this.checkDeclaration(declaration.id);
  13495. }
  13496. }
  13497. }
  13498. }
  13499. }
  13500. checkDeclaration(node) {
  13501. if (node.type === "Identifier") {
  13502. this.checkDuplicateExports(node, node.name);
  13503. } else if (node.type === "ObjectPattern") {
  13504. for (const prop of node.properties) {
  13505. this.checkDeclaration(prop);
  13506. }
  13507. } else if (node.type === "ArrayPattern") {
  13508. for (const elem of node.elements) {
  13509. if (elem) {
  13510. this.checkDeclaration(elem);
  13511. }
  13512. }
  13513. } else if (node.type === "ObjectProperty") {
  13514. this.checkDeclaration(node.value);
  13515. } else if (node.type === "RestElement") {
  13516. this.checkDeclaration(node.argument);
  13517. } else if (node.type === "AssignmentPattern") {
  13518. this.checkDeclaration(node.left);
  13519. }
  13520. }
  13521. checkDuplicateExports(node, exportName) {
  13522. if (this.exportedIdentifiers.has(exportName)) {
  13523. if (exportName === "default") {
  13524. this.raise(Errors.DuplicateDefaultExport, node);
  13525. } else {
  13526. this.raise(Errors.DuplicateExport, node, {
  13527. exportName
  13528. });
  13529. }
  13530. }
  13531. this.exportedIdentifiers.add(exportName);
  13532. }
  13533. parseExportSpecifiers(isInTypeExport) {
  13534. const nodes = [];
  13535. let first = true;
  13536. this.expect(5);
  13537. while (!this.eat(8)) {
  13538. if (first) {
  13539. first = false;
  13540. } else {
  13541. this.expect(12);
  13542. if (this.eat(8)) break;
  13543. }
  13544. const isMaybeTypeOnly = this.isContextual(130);
  13545. const isString = this.match(133);
  13546. const node = this.startNode();
  13547. node.local = this.parseModuleExportName();
  13548. nodes.push(this.parseExportSpecifier(node, isString, isInTypeExport, isMaybeTypeOnly));
  13549. }
  13550. return nodes;
  13551. }
  13552. parseExportSpecifier(node, isString, isInTypeExport, isMaybeTypeOnly) {
  13553. if (this.eatContextual(93)) {
  13554. node.exported = this.parseModuleExportName();
  13555. } else if (isString) {
  13556. node.exported = cloneStringLiteral(node.local);
  13557. } else if (!node.exported) {
  13558. node.exported = cloneIdentifier(node.local);
  13559. }
  13560. return this.finishNode(node, "ExportSpecifier");
  13561. }
  13562. parseModuleExportName() {
  13563. if (this.match(133)) {
  13564. const result = this.parseStringLiteral(this.state.value);
  13565. const surrogate = result.value.match(loneSurrogate);
  13566. if (surrogate) {
  13567. this.raise(Errors.ModuleExportNameHasLoneSurrogate, result, {
  13568. surrogateCharCode: surrogate[0].charCodeAt(0)
  13569. });
  13570. }
  13571. return result;
  13572. }
  13573. return this.parseIdentifier(true);
  13574. }
  13575. isJSONModuleImport(node) {
  13576. if (node.assertions != null) {
  13577. return node.assertions.some(({
  13578. key,
  13579. value
  13580. }) => {
  13581. return value.value === "json" && (key.type === "Identifier" ? key.name === "type" : key.value === "type");
  13582. });
  13583. }
  13584. return false;
  13585. }
  13586. checkImportReflection(node) {
  13587. const {
  13588. specifiers
  13589. } = node;
  13590. const singleBindingType = specifiers.length === 1 ? specifiers[0].type : null;
  13591. if (node.phase === "source") {
  13592. if (singleBindingType !== "ImportDefaultSpecifier") {
  13593. this.raise(Errors.SourcePhaseImportRequiresDefault, specifiers[0].loc.start);
  13594. }
  13595. } else if (node.phase === "defer") {
  13596. if (singleBindingType !== "ImportNamespaceSpecifier") {
  13597. this.raise(Errors.DeferImportRequiresNamespace, specifiers[0].loc.start);
  13598. }
  13599. } else if (node.module) {
  13600. var _node$assertions;
  13601. if (singleBindingType !== "ImportDefaultSpecifier") {
  13602. this.raise(Errors.ImportReflectionNotBinding, specifiers[0].loc.start);
  13603. }
  13604. if (((_node$assertions = node.assertions) == null ? void 0 : _node$assertions.length) > 0) {
  13605. this.raise(Errors.ImportReflectionHasAssertion, specifiers[0].loc.start);
  13606. }
  13607. }
  13608. }
  13609. checkJSONModuleImport(node) {
  13610. if (this.isJSONModuleImport(node) && node.type !== "ExportAllDeclaration") {
  13611. const {
  13612. specifiers
  13613. } = node;
  13614. if (specifiers != null) {
  13615. const nonDefaultNamedSpecifier = specifiers.find(specifier => {
  13616. let imported;
  13617. if (specifier.type === "ExportSpecifier") {
  13618. imported = specifier.local;
  13619. } else if (specifier.type === "ImportSpecifier") {
  13620. imported = specifier.imported;
  13621. }
  13622. if (imported !== undefined) {
  13623. return imported.type === "Identifier" ? imported.name !== "default" : imported.value !== "default";
  13624. }
  13625. });
  13626. if (nonDefaultNamedSpecifier !== undefined) {
  13627. this.raise(Errors.ImportJSONBindingNotDefault, nonDefaultNamedSpecifier.loc.start);
  13628. }
  13629. }
  13630. }
  13631. }
  13632. isPotentialImportPhase(isExport) {
  13633. if (isExport) return false;
  13634. return this.isContextual(105) || this.isContextual(97) || this.isContextual(127);
  13635. }
  13636. applyImportPhase(node, isExport, phase, loc) {
  13637. if (isExport) {
  13638. return;
  13639. }
  13640. if (phase === "module") {
  13641. this.expectPlugin("importReflection", loc);
  13642. node.module = true;
  13643. } else if (this.hasPlugin("importReflection")) {
  13644. node.module = false;
  13645. }
  13646. if (phase === "source") {
  13647. this.expectPlugin("sourcePhaseImports", loc);
  13648. node.phase = "source";
  13649. } else if (phase === "defer") {
  13650. this.expectPlugin("deferredImportEvaluation", loc);
  13651. node.phase = "defer";
  13652. } else if (this.hasPlugin("sourcePhaseImports")) {
  13653. node.phase = null;
  13654. }
  13655. }
  13656. parseMaybeImportPhase(node, isExport) {
  13657. if (!this.isPotentialImportPhase(isExport)) {
  13658. this.applyImportPhase(node, isExport, null);
  13659. return null;
  13660. }
  13661. const phaseIdentifier = this.parseIdentifier(true);
  13662. const {
  13663. type
  13664. } = this.state;
  13665. const isImportPhase = tokenIsKeywordOrIdentifier(type) ? type !== 98 || this.lookaheadCharCode() === 102 : type !== 12;
  13666. if (isImportPhase) {
  13667. this.resetPreviousIdentifierLeadingComments(phaseIdentifier);
  13668. this.applyImportPhase(node, isExport, phaseIdentifier.name, phaseIdentifier.loc.start);
  13669. return null;
  13670. } else {
  13671. this.applyImportPhase(node, isExport, null);
  13672. return phaseIdentifier;
  13673. }
  13674. }
  13675. isPrecedingIdImportPhase(phase) {
  13676. const {
  13677. type
  13678. } = this.state;
  13679. return tokenIsIdentifier(type) ? type !== 98 || this.lookaheadCharCode() === 102 : type !== 12;
  13680. }
  13681. parseImport(node) {
  13682. if (this.match(133)) {
  13683. return this.parseImportSourceAndAttributes(node);
  13684. }
  13685. return this.parseImportSpecifiersAndAfter(node, this.parseMaybeImportPhase(node, false));
  13686. }
  13687. parseImportSpecifiersAndAfter(node, maybeDefaultIdentifier) {
  13688. node.specifiers = [];
  13689. const hasDefault = this.maybeParseDefaultImportSpecifier(node, maybeDefaultIdentifier);
  13690. const parseNext = !hasDefault || this.eat(12);
  13691. const hasStar = parseNext && this.maybeParseStarImportSpecifier(node);
  13692. if (parseNext && !hasStar) this.parseNamedImportSpecifiers(node);
  13693. this.expectContextual(98);
  13694. return this.parseImportSourceAndAttributes(node);
  13695. }
  13696. parseImportSourceAndAttributes(node) {
  13697. var _node$specifiers2;
  13698. (_node$specifiers2 = node.specifiers) != null ? _node$specifiers2 : node.specifiers = [];
  13699. node.source = this.parseImportSource();
  13700. this.maybeParseImportAttributes(node);
  13701. this.checkImportReflection(node);
  13702. this.checkJSONModuleImport(node);
  13703. this.semicolon();
  13704. return this.finishNode(node, "ImportDeclaration");
  13705. }
  13706. parseImportSource() {
  13707. if (!this.match(133)) this.unexpected();
  13708. return this.parseExprAtom();
  13709. }
  13710. parseImportSpecifierLocal(node, specifier, type) {
  13711. specifier.local = this.parseIdentifier();
  13712. node.specifiers.push(this.finishImportSpecifier(specifier, type));
  13713. }
  13714. finishImportSpecifier(specifier, type, bindingType = 8201) {
  13715. this.checkLVal(specifier.local, {
  13716. in: {
  13717. type
  13718. },
  13719. binding: bindingType
  13720. });
  13721. return this.finishNode(specifier, type);
  13722. }
  13723. parseImportAttributes() {
  13724. this.expect(5);
  13725. const attrs = [];
  13726. const attrNames = new Set();
  13727. do {
  13728. if (this.match(8)) {
  13729. break;
  13730. }
  13731. const node = this.startNode();
  13732. const keyName = this.state.value;
  13733. if (attrNames.has(keyName)) {
  13734. this.raise(Errors.ModuleAttributesWithDuplicateKeys, this.state.startLoc, {
  13735. key: keyName
  13736. });
  13737. }
  13738. attrNames.add(keyName);
  13739. if (this.match(133)) {
  13740. node.key = this.parseStringLiteral(keyName);
  13741. } else {
  13742. node.key = this.parseIdentifier(true);
  13743. }
  13744. this.expect(14);
  13745. if (!this.match(133)) {
  13746. throw this.raise(Errors.ModuleAttributeInvalidValue, this.state.startLoc);
  13747. }
  13748. node.value = this.parseStringLiteral(this.state.value);
  13749. attrs.push(this.finishNode(node, "ImportAttribute"));
  13750. } while (this.eat(12));
  13751. this.expect(8);
  13752. return attrs;
  13753. }
  13754. parseModuleAttributes() {
  13755. const attrs = [];
  13756. const attributes = new Set();
  13757. do {
  13758. const node = this.startNode();
  13759. node.key = this.parseIdentifier(true);
  13760. if (node.key.name !== "type") {
  13761. this.raise(Errors.ModuleAttributeDifferentFromType, node.key);
  13762. }
  13763. if (attributes.has(node.key.name)) {
  13764. this.raise(Errors.ModuleAttributesWithDuplicateKeys, node.key, {
  13765. key: node.key.name
  13766. });
  13767. }
  13768. attributes.add(node.key.name);
  13769. this.expect(14);
  13770. if (!this.match(133)) {
  13771. throw this.raise(Errors.ModuleAttributeInvalidValue, this.state.startLoc);
  13772. }
  13773. node.value = this.parseStringLiteral(this.state.value);
  13774. attrs.push(this.finishNode(node, "ImportAttribute"));
  13775. } while (this.eat(12));
  13776. return attrs;
  13777. }
  13778. maybeParseImportAttributes(node) {
  13779. let attributes;
  13780. let useWith = false;
  13781. if (this.match(76)) {
  13782. if (this.hasPrecedingLineBreak() && this.lookaheadCharCode() === 40) {
  13783. return;
  13784. }
  13785. this.next();
  13786. {
  13787. if (this.hasPlugin("moduleAttributes")) {
  13788. attributes = this.parseModuleAttributes();
  13789. } else {
  13790. this.expectImportAttributesPlugin();
  13791. attributes = this.parseImportAttributes();
  13792. }
  13793. }
  13794. useWith = true;
  13795. } else if (this.isContextual(94) && !this.hasPrecedingLineBreak()) {
  13796. if (this.hasPlugin("importAttributes")) {
  13797. if (this.getPluginOption("importAttributes", "deprecatedAssertSyntax") !== true) {
  13798. this.raise(Errors.ImportAttributesUseAssert, this.state.startLoc);
  13799. }
  13800. this.addExtra(node, "deprecatedAssertSyntax", true);
  13801. } else {
  13802. this.expectOnePlugin(["importAttributes", "importAssertions"]);
  13803. }
  13804. this.next();
  13805. attributes = this.parseImportAttributes();
  13806. } else if (this.hasPlugin("importAttributes") || this.hasPlugin("importAssertions")) {
  13807. attributes = [];
  13808. } else {
  13809. if (this.hasPlugin("moduleAttributes")) {
  13810. attributes = [];
  13811. } else return;
  13812. }
  13813. if (!useWith && this.hasPlugin("importAssertions")) {
  13814. node.assertions = attributes;
  13815. } else {
  13816. node.attributes = attributes;
  13817. }
  13818. }
  13819. maybeParseDefaultImportSpecifier(node, maybeDefaultIdentifier) {
  13820. if (maybeDefaultIdentifier) {
  13821. const specifier = this.startNodeAtNode(maybeDefaultIdentifier);
  13822. specifier.local = maybeDefaultIdentifier;
  13823. node.specifiers.push(this.finishImportSpecifier(specifier, "ImportDefaultSpecifier"));
  13824. return true;
  13825. } else if (tokenIsKeywordOrIdentifier(this.state.type)) {
  13826. this.parseImportSpecifierLocal(node, this.startNode(), "ImportDefaultSpecifier");
  13827. return true;
  13828. }
  13829. return false;
  13830. }
  13831. maybeParseStarImportSpecifier(node) {
  13832. if (this.match(55)) {
  13833. const specifier = this.startNode();
  13834. this.next();
  13835. this.expectContextual(93);
  13836. this.parseImportSpecifierLocal(node, specifier, "ImportNamespaceSpecifier");
  13837. return true;
  13838. }
  13839. return false;
  13840. }
  13841. parseNamedImportSpecifiers(node) {
  13842. let first = true;
  13843. this.expect(5);
  13844. while (!this.eat(8)) {
  13845. if (first) {
  13846. first = false;
  13847. } else {
  13848. if (this.eat(14)) {
  13849. throw this.raise(Errors.DestructureNamedImport, this.state.startLoc);
  13850. }
  13851. this.expect(12);
  13852. if (this.eat(8)) break;
  13853. }
  13854. const specifier = this.startNode();
  13855. const importedIsString = this.match(133);
  13856. const isMaybeTypeOnly = this.isContextual(130);
  13857. specifier.imported = this.parseModuleExportName();
  13858. const importSpecifier = this.parseImportSpecifier(specifier, importedIsString, node.importKind === "type" || node.importKind === "typeof", isMaybeTypeOnly, undefined);
  13859. node.specifiers.push(importSpecifier);
  13860. }
  13861. }
  13862. parseImportSpecifier(specifier, importedIsString, isInTypeOnlyImport, isMaybeTypeOnly, bindingType) {
  13863. if (this.eatContextual(93)) {
  13864. specifier.local = this.parseIdentifier();
  13865. } else {
  13866. const {
  13867. imported
  13868. } = specifier;
  13869. if (importedIsString) {
  13870. throw this.raise(Errors.ImportBindingIsString, specifier, {
  13871. importName: imported.value
  13872. });
  13873. }
  13874. this.checkReservedWord(imported.name, specifier.loc.start, true, true);
  13875. if (!specifier.local) {
  13876. specifier.local = cloneIdentifier(imported);
  13877. }
  13878. }
  13879. return this.finishImportSpecifier(specifier, "ImportSpecifier", bindingType);
  13880. }
  13881. isThisParam(param) {
  13882. return param.type === "Identifier" && param.name === "this";
  13883. }
  13884. }
  13885. class Parser extends StatementParser {
  13886. constructor(options, input) {
  13887. options = getOptions(options);
  13888. super(options, input);
  13889. this.options = options;
  13890. this.initializeScopes();
  13891. this.plugins = pluginsMap(this.options.plugins);
  13892. this.filename = options.sourceFilename;
  13893. }
  13894. getScopeHandler() {
  13895. return ScopeHandler;
  13896. }
  13897. parse() {
  13898. this.enterInitialScopes();
  13899. const file = this.startNode();
  13900. const program = this.startNode();
  13901. this.nextToken();
  13902. file.errors = null;
  13903. this.parseTopLevel(file, program);
  13904. file.errors = this.state.errors;
  13905. file.comments.length = this.state.commentsLen;
  13906. return file;
  13907. }
  13908. }
  13909. function pluginsMap(plugins) {
  13910. const pluginMap = new Map();
  13911. for (const plugin of plugins) {
  13912. const [name, options] = Array.isArray(plugin) ? plugin : [plugin, {}];
  13913. if (!pluginMap.has(name)) pluginMap.set(name, options || {});
  13914. }
  13915. return pluginMap;
  13916. }
  13917. function parse(input, options) {
  13918. var _options;
  13919. if (((_options = options) == null ? void 0 : _options.sourceType) === "unambiguous") {
  13920. options = Object.assign({}, options);
  13921. try {
  13922. options.sourceType = "module";
  13923. const parser = getParser(options, input);
  13924. const ast = parser.parse();
  13925. if (parser.sawUnambiguousESM) {
  13926. return ast;
  13927. }
  13928. if (parser.ambiguousScriptDifferentAst) {
  13929. try {
  13930. options.sourceType = "script";
  13931. return getParser(options, input).parse();
  13932. } catch (_unused) {}
  13933. } else {
  13934. ast.program.sourceType = "script";
  13935. }
  13936. return ast;
  13937. } catch (moduleError) {
  13938. try {
  13939. options.sourceType = "script";
  13940. return getParser(options, input).parse();
  13941. } catch (_unused2) {}
  13942. throw moduleError;
  13943. }
  13944. } else {
  13945. return getParser(options, input).parse();
  13946. }
  13947. }
  13948. function parseExpression(input, options) {
  13949. const parser = getParser(options, input);
  13950. if (parser.options.strictMode) {
  13951. parser.state.strict = true;
  13952. }
  13953. return parser.getExpression();
  13954. }
  13955. function generateExportedTokenTypes(internalTokenTypes) {
  13956. const tokenTypes = {};
  13957. for (const typeName of Object.keys(internalTokenTypes)) {
  13958. tokenTypes[typeName] = getExportedToken(internalTokenTypes[typeName]);
  13959. }
  13960. return tokenTypes;
  13961. }
  13962. const tokTypes = generateExportedTokenTypes(tt);
  13963. function getParser(options, input) {
  13964. let cls = Parser;
  13965. if (options != null && options.plugins) {
  13966. validatePlugins(options.plugins);
  13967. cls = getParserClass(options.plugins);
  13968. }
  13969. return new cls(options, input);
  13970. }
  13971. const parserClassCache = {};
  13972. function getParserClass(pluginsFromOptions) {
  13973. const pluginList = mixinPluginNames.filter(name => hasPlugin(pluginsFromOptions, name));
  13974. const key = pluginList.join("/");
  13975. let cls = parserClassCache[key];
  13976. if (!cls) {
  13977. cls = Parser;
  13978. for (const plugin of pluginList) {
  13979. cls = mixinPlugins[plugin](cls);
  13980. }
  13981. parserClassCache[key] = cls;
  13982. }
  13983. return cls;
  13984. }
  13985. exports.parse = parse;
  13986. exports.parseExpression = parseExpression;
  13987. exports.tokTypes = tokTypes;
  13988. //# sourceMappingURL=index.js.map