Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 420KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. const beforeExpr = true;
  4. const startsExpr = true;
  5. const isLoop = true;
  6. const isAssign = true;
  7. const prefix = true;
  8. const postfix = true;
  9. class TokenType {
  10. constructor(label, conf = {}) {
  11. this.label = void 0;
  12. this.keyword = void 0;
  13. this.beforeExpr = void 0;
  14. this.startsExpr = void 0;
  15. this.rightAssociative = void 0;
  16. this.isLoop = void 0;
  17. this.isAssign = void 0;
  18. this.prefix = void 0;
  19. this.postfix = void 0;
  20. this.binop = void 0;
  21. this.updateContext = void 0;
  22. this.label = label;
  23. this.keyword = conf.keyword;
  24. this.beforeExpr = !!conf.beforeExpr;
  25. this.startsExpr = !!conf.startsExpr;
  26. this.rightAssociative = !!conf.rightAssociative;
  27. this.isLoop = !!conf.isLoop;
  28. this.isAssign = !!conf.isAssign;
  29. this.prefix = !!conf.prefix;
  30. this.postfix = !!conf.postfix;
  31. this.binop = conf.binop != null ? conf.binop : null;
  32. this.updateContext = null;
  33. }
  34. }
  35. const keywords$1 = new Map();
  36. function createKeyword(name, options = {}) {
  37. options.keyword = name;
  38. const token = new TokenType(name, options);
  39. keywords$1.set(name, token);
  40. return token;
  41. }
  42. function createBinop(name, binop) {
  43. return new TokenType(name, {
  44. beforeExpr,
  45. binop
  46. });
  47. }
  48. const types$1 = {
  49. num: new TokenType("num", {
  50. startsExpr
  51. }),
  52. bigint: new TokenType("bigint", {
  53. startsExpr
  54. }),
  55. decimal: new TokenType("decimal", {
  56. startsExpr
  57. }),
  58. regexp: new TokenType("regexp", {
  59. startsExpr
  60. }),
  61. string: new TokenType("string", {
  62. startsExpr
  63. }),
  64. name: new TokenType("name", {
  65. startsExpr
  66. }),
  67. privateName: new TokenType("#name", {
  68. startsExpr
  69. }),
  70. eof: new TokenType("eof"),
  71. bracketL: new TokenType("[", {
  72. beforeExpr,
  73. startsExpr
  74. }),
  75. bracketHashL: new TokenType("#[", {
  76. beforeExpr,
  77. startsExpr
  78. }),
  79. bracketBarL: new TokenType("[|", {
  80. beforeExpr,
  81. startsExpr
  82. }),
  83. bracketR: new TokenType("]"),
  84. bracketBarR: new TokenType("|]"),
  85. braceL: new TokenType("{", {
  86. beforeExpr,
  87. startsExpr
  88. }),
  89. braceBarL: new TokenType("{|", {
  90. beforeExpr,
  91. startsExpr
  92. }),
  93. braceHashL: new TokenType("#{", {
  94. beforeExpr,
  95. startsExpr
  96. }),
  97. braceR: new TokenType("}", {
  98. beforeExpr
  99. }),
  100. braceBarR: new TokenType("|}"),
  101. parenL: new TokenType("(", {
  102. beforeExpr,
  103. startsExpr
  104. }),
  105. parenR: new TokenType(")"),
  106. comma: new TokenType(",", {
  107. beforeExpr
  108. }),
  109. semi: new TokenType(";", {
  110. beforeExpr
  111. }),
  112. colon: new TokenType(":", {
  113. beforeExpr
  114. }),
  115. doubleColon: new TokenType("::", {
  116. beforeExpr
  117. }),
  118. dot: new TokenType("."),
  119. question: new TokenType("?", {
  120. beforeExpr
  121. }),
  122. questionDot: new TokenType("?."),
  123. arrow: new TokenType("=>", {
  124. beforeExpr
  125. }),
  126. template: new TokenType("template"),
  127. ellipsis: new TokenType("...", {
  128. beforeExpr
  129. }),
  130. backQuote: new TokenType("`", {
  131. startsExpr
  132. }),
  133. dollarBraceL: new TokenType("${", {
  134. beforeExpr,
  135. startsExpr
  136. }),
  137. at: new TokenType("@"),
  138. hash: new TokenType("#", {
  139. startsExpr
  140. }),
  141. interpreterDirective: new TokenType("#!..."),
  142. eq: new TokenType("=", {
  143. beforeExpr,
  144. isAssign
  145. }),
  146. assign: new TokenType("_=", {
  147. beforeExpr,
  148. isAssign
  149. }),
  150. slashAssign: new TokenType("_=", {
  151. beforeExpr,
  152. isAssign
  153. }),
  154. incDec: new TokenType("++/--", {
  155. prefix,
  156. postfix,
  157. startsExpr
  158. }),
  159. bang: new TokenType("!", {
  160. beforeExpr,
  161. prefix,
  162. startsExpr
  163. }),
  164. tilde: new TokenType("~", {
  165. beforeExpr,
  166. prefix,
  167. startsExpr
  168. }),
  169. pipeline: createBinop("|>", 0),
  170. nullishCoalescing: createBinop("??", 1),
  171. logicalOR: createBinop("||", 1),
  172. logicalAND: createBinop("&&", 2),
  173. bitwiseOR: createBinop("|", 3),
  174. bitwiseXOR: createBinop("^", 4),
  175. bitwiseAND: createBinop("&", 5),
  176. equality: createBinop("==/!=/===/!==", 6),
  177. relational: createBinop("</>/<=/>=", 7),
  178. bitShift: createBinop("<</>>/>>>", 8),
  179. plusMin: new TokenType("+/-", {
  180. beforeExpr,
  181. binop: 9,
  182. prefix,
  183. startsExpr
  184. }),
  185. modulo: new TokenType("%", {
  186. beforeExpr,
  187. binop: 10,
  188. startsExpr
  189. }),
  190. star: new TokenType("*", {
  191. binop: 10
  192. }),
  193. slash: createBinop("/", 10),
  194. exponent: new TokenType("**", {
  195. beforeExpr,
  196. binop: 11,
  197. rightAssociative: true
  198. }),
  199. _break: createKeyword("break"),
  200. _case: createKeyword("case", {
  201. beforeExpr
  202. }),
  203. _catch: createKeyword("catch"),
  204. _continue: createKeyword("continue"),
  205. _debugger: createKeyword("debugger"),
  206. _default: createKeyword("default", {
  207. beforeExpr
  208. }),
  209. _do: createKeyword("do", {
  210. isLoop,
  211. beforeExpr
  212. }),
  213. _else: createKeyword("else", {
  214. beforeExpr
  215. }),
  216. _finally: createKeyword("finally"),
  217. _for: createKeyword("for", {
  218. isLoop
  219. }),
  220. _function: createKeyword("function", {
  221. startsExpr
  222. }),
  223. _if: createKeyword("if"),
  224. _return: createKeyword("return", {
  225. beforeExpr
  226. }),
  227. _switch: createKeyword("switch"),
  228. _throw: createKeyword("throw", {
  229. beforeExpr,
  230. prefix,
  231. startsExpr
  232. }),
  233. _try: createKeyword("try"),
  234. _var: createKeyword("var"),
  235. _const: createKeyword("const"),
  236. _while: createKeyword("while", {
  237. isLoop
  238. }),
  239. _with: createKeyword("with"),
  240. _new: createKeyword("new", {
  241. beforeExpr,
  242. startsExpr
  243. }),
  244. _this: createKeyword("this", {
  245. startsExpr
  246. }),
  247. _super: createKeyword("super", {
  248. startsExpr
  249. }),
  250. _class: createKeyword("class", {
  251. startsExpr
  252. }),
  253. _extends: createKeyword("extends", {
  254. beforeExpr
  255. }),
  256. _export: createKeyword("export"),
  257. _import: createKeyword("import", {
  258. startsExpr
  259. }),
  260. _null: createKeyword("null", {
  261. startsExpr
  262. }),
  263. _true: createKeyword("true", {
  264. startsExpr
  265. }),
  266. _false: createKeyword("false", {
  267. startsExpr
  268. }),
  269. _in: createKeyword("in", {
  270. beforeExpr,
  271. binop: 7
  272. }),
  273. _instanceof: createKeyword("instanceof", {
  274. beforeExpr,
  275. binop: 7
  276. }),
  277. _typeof: createKeyword("typeof", {
  278. beforeExpr,
  279. prefix,
  280. startsExpr
  281. }),
  282. _void: createKeyword("void", {
  283. beforeExpr,
  284. prefix,
  285. startsExpr
  286. }),
  287. _delete: createKeyword("delete", {
  288. beforeExpr,
  289. prefix,
  290. startsExpr
  291. })
  292. };
  293. const lineBreak = /\r\n?|[\n\u2028\u2029]/;
  294. const lineBreakG = new RegExp(lineBreak.source, "g");
  295. function isNewLine(code) {
  296. switch (code) {
  297. case 10:
  298. case 13:
  299. case 8232:
  300. case 8233:
  301. return true;
  302. default:
  303. return false;
  304. }
  305. }
  306. const skipWhiteSpace = /(?:\s|\/\/.*|\/\*[^]*?\*\/)*/g;
  307. function isWhitespace(code) {
  308. switch (code) {
  309. case 0x0009:
  310. case 0x000b:
  311. case 0x000c:
  312. case 32:
  313. case 160:
  314. case 5760:
  315. case 0x2000:
  316. case 0x2001:
  317. case 0x2002:
  318. case 0x2003:
  319. case 0x2004:
  320. case 0x2005:
  321. case 0x2006:
  322. case 0x2007:
  323. case 0x2008:
  324. case 0x2009:
  325. case 0x200a:
  326. case 0x202f:
  327. case 0x205f:
  328. case 0x3000:
  329. case 0xfeff:
  330. return true;
  331. default:
  332. return false;
  333. }
  334. }
  335. class Position {
  336. constructor(line, col) {
  337. this.line = void 0;
  338. this.column = void 0;
  339. this.line = line;
  340. this.column = col;
  341. }
  342. }
  343. class SourceLocation {
  344. constructor(start, end) {
  345. this.start = void 0;
  346. this.end = void 0;
  347. this.filename = void 0;
  348. this.identifierName = void 0;
  349. this.start = start;
  350. this.end = end;
  351. }
  352. }
  353. function getLineInfo(input, offset) {
  354. let line = 1;
  355. let lineStart = 0;
  356. let match;
  357. lineBreakG.lastIndex = 0;
  358. while ((match = lineBreakG.exec(input)) && match.index < offset) {
  359. line++;
  360. lineStart = lineBreakG.lastIndex;
  361. }
  362. return new Position(line, offset - lineStart);
  363. }
  364. class BaseParser {
  365. constructor() {
  366. this.sawUnambiguousESM = false;
  367. this.ambiguousScriptDifferentAst = false;
  368. }
  369. hasPlugin(name) {
  370. return this.plugins.has(name);
  371. }
  372. getPluginOption(plugin, name) {
  373. if (this.hasPlugin(plugin)) return this.plugins.get(plugin)[name];
  374. }
  375. }
  376. function last(stack) {
  377. return stack[stack.length - 1];
  378. }
  379. class CommentsParser extends BaseParser {
  380. addComment(comment) {
  381. if (this.filename) comment.loc.filename = this.filename;
  382. this.state.trailingComments.push(comment);
  383. this.state.leadingComments.push(comment);
  384. }
  385. adjustCommentsAfterTrailingComma(node, elements, takeAllComments) {
  386. if (this.state.leadingComments.length === 0) {
  387. return;
  388. }
  389. let lastElement = null;
  390. let i = elements.length;
  391. while (lastElement === null && i > 0) {
  392. lastElement = elements[--i];
  393. }
  394. if (lastElement === null) {
  395. return;
  396. }
  397. for (let j = 0; j < this.state.leadingComments.length; j++) {
  398. if (this.state.leadingComments[j].end < this.state.commentPreviousNode.end) {
  399. this.state.leadingComments.splice(j, 1);
  400. j--;
  401. }
  402. }
  403. const newTrailingComments = [];
  404. for (let i = 0; i < this.state.leadingComments.length; i++) {
  405. const leadingComment = this.state.leadingComments[i];
  406. if (leadingComment.end < node.end) {
  407. newTrailingComments.push(leadingComment);
  408. if (!takeAllComments) {
  409. this.state.leadingComments.splice(i, 1);
  410. i--;
  411. }
  412. } else {
  413. if (node.trailingComments === undefined) {
  414. node.trailingComments = [];
  415. }
  416. node.trailingComments.push(leadingComment);
  417. }
  418. }
  419. if (takeAllComments) this.state.leadingComments = [];
  420. if (newTrailingComments.length > 0) {
  421. lastElement.trailingComments = newTrailingComments;
  422. } else if (lastElement.trailingComments !== undefined) {
  423. lastElement.trailingComments = [];
  424. }
  425. }
  426. processComment(node) {
  427. if (node.type === "Program" && node.body.length > 0) return;
  428. const stack = this.state.commentStack;
  429. let firstChild, lastChild, trailingComments, i, j;
  430. if (this.state.trailingComments.length > 0) {
  431. if (this.state.trailingComments[0].start >= node.end) {
  432. trailingComments = this.state.trailingComments;
  433. this.state.trailingComments = [];
  434. } else {
  435. this.state.trailingComments.length = 0;
  436. }
  437. } else if (stack.length > 0) {
  438. const lastInStack = last(stack);
  439. if (lastInStack.trailingComments && lastInStack.trailingComments[0].start >= node.end) {
  440. trailingComments = lastInStack.trailingComments;
  441. delete lastInStack.trailingComments;
  442. }
  443. }
  444. if (stack.length > 0 && last(stack).start >= node.start) {
  445. firstChild = stack.pop();
  446. }
  447. while (stack.length > 0 && last(stack).start >= node.start) {
  448. lastChild = stack.pop();
  449. }
  450. if (!lastChild && firstChild) lastChild = firstChild;
  451. if (firstChild) {
  452. switch (node.type) {
  453. case "ObjectExpression":
  454. this.adjustCommentsAfterTrailingComma(node, node.properties);
  455. break;
  456. case "ObjectPattern":
  457. this.adjustCommentsAfterTrailingComma(node, node.properties, true);
  458. break;
  459. case "CallExpression":
  460. this.adjustCommentsAfterTrailingComma(node, node.arguments);
  461. break;
  462. case "ArrayExpression":
  463. this.adjustCommentsAfterTrailingComma(node, node.elements);
  464. break;
  465. case "ArrayPattern":
  466. this.adjustCommentsAfterTrailingComma(node, node.elements, true);
  467. break;
  468. }
  469. } else if (this.state.commentPreviousNode && (this.state.commentPreviousNode.type === "ImportSpecifier" && node.type !== "ImportSpecifier" || this.state.commentPreviousNode.type === "ExportSpecifier" && node.type !== "ExportSpecifier")) {
  470. this.adjustCommentsAfterTrailingComma(node, [this.state.commentPreviousNode]);
  471. }
  472. if (lastChild) {
  473. if (lastChild.leadingComments) {
  474. if (lastChild !== node && lastChild.leadingComments.length > 0 && last(lastChild.leadingComments).end <= node.start) {
  475. node.leadingComments = lastChild.leadingComments;
  476. delete lastChild.leadingComments;
  477. } else {
  478. for (i = lastChild.leadingComments.length - 2; i >= 0; --i) {
  479. if (lastChild.leadingComments[i].end <= node.start) {
  480. node.leadingComments = lastChild.leadingComments.splice(0, i + 1);
  481. break;
  482. }
  483. }
  484. }
  485. }
  486. } else if (this.state.leadingComments.length > 0) {
  487. if (last(this.state.leadingComments).end <= node.start) {
  488. if (this.state.commentPreviousNode) {
  489. for (j = 0; j < this.state.leadingComments.length; j++) {
  490. if (this.state.leadingComments[j].end < this.state.commentPreviousNode.end) {
  491. this.state.leadingComments.splice(j, 1);
  492. j--;
  493. }
  494. }
  495. }
  496. if (this.state.leadingComments.length > 0) {
  497. node.leadingComments = this.state.leadingComments;
  498. this.state.leadingComments = [];
  499. }
  500. } else {
  501. for (i = 0; i < this.state.leadingComments.length; i++) {
  502. if (this.state.leadingComments[i].end > node.start) {
  503. break;
  504. }
  505. }
  506. const leadingComments = this.state.leadingComments.slice(0, i);
  507. if (leadingComments.length) {
  508. node.leadingComments = leadingComments;
  509. }
  510. trailingComments = this.state.leadingComments.slice(i);
  511. if (trailingComments.length === 0) {
  512. trailingComments = null;
  513. }
  514. }
  515. }
  516. this.state.commentPreviousNode = node;
  517. if (trailingComments) {
  518. if (trailingComments.length && trailingComments[0].start >= node.start && last(trailingComments).end <= node.end) {
  519. node.innerComments = trailingComments;
  520. } else {
  521. const firstTrailingCommentIndex = trailingComments.findIndex(comment => comment.end >= node.end);
  522. if (firstTrailingCommentIndex > 0) {
  523. node.innerComments = trailingComments.slice(0, firstTrailingCommentIndex);
  524. node.trailingComments = trailingComments.slice(firstTrailingCommentIndex);
  525. } else {
  526. node.trailingComments = trailingComments;
  527. }
  528. }
  529. }
  530. stack.push(node);
  531. }
  532. }
  533. const ErrorCodes = Object.freeze({
  534. SyntaxError: "BABEL_PARSER_SYNTAX_ERROR",
  535. SourceTypeModuleError: "BABEL_PARSER_SOURCETYPE_MODULE_REQUIRED"
  536. });
  537. const ErrorMessages = makeErrorTemplates({
  538. AccessorIsGenerator: "A %0ter cannot be a generator.",
  539. ArgumentsInClass: "'arguments' is only allowed in functions and class methods.",
  540. AsyncFunctionInSingleStatementContext: "Async functions can only be declared at the top level or inside a block.",
  541. AwaitBindingIdentifier: "Can not use 'await' as identifier inside an async function.",
  542. AwaitBindingIdentifierInStaticBlock: "Can not use 'await' as identifier inside a static block.",
  543. AwaitExpressionFormalParameter: "'await' is not allowed in async function parameters.",
  544. AwaitNotInAsyncContext: "'await' is only allowed within async functions and at the top levels of modules.",
  545. AwaitNotInAsyncFunction: "'await' is only allowed within async functions.",
  546. BadGetterArity: "A 'get' accesor must not have any formal parameters.",
  547. BadSetterArity: "A 'set' accesor must have exactly one formal parameter.",
  548. BadSetterRestParameter: "A 'set' accesor function argument must not be a rest parameter.",
  549. ConstructorClassField: "Classes may not have a field named 'constructor'.",
  550. ConstructorClassPrivateField: "Classes may not have a private field named '#constructor'.",
  551. ConstructorIsAccessor: "Class constructor may not be an accessor.",
  552. ConstructorIsAsync: "Constructor can't be an async function.",
  553. ConstructorIsGenerator: "Constructor can't be a generator.",
  554. DeclarationMissingInitializer: "'%0' require an initialization value.",
  555. DecoratorBeforeExport: "Decorators must be placed *before* the 'export' keyword. You can set the 'decoratorsBeforeExport' option to false to use the 'export @decorator class {}' syntax.",
  556. DecoratorConstructor: "Decorators can't be used with a constructor. Did you mean '@dec class { ... }'?",
  557. DecoratorExportClass: "Using the export keyword between a decorator and a class is not allowed. Please use `export @dec class` instead.",
  558. DecoratorSemicolon: "Decorators must not be followed by a semicolon.",
  559. DecoratorStaticBlock: "Decorators can't be used with a static block.",
  560. DeletePrivateField: "Deleting a private field is not allowed.",
  561. DestructureNamedImport: "ES2015 named imports do not destructure. Use another statement for destructuring after the import.",
  562. DuplicateConstructor: "Duplicate constructor in the same class.",
  563. DuplicateDefaultExport: "Only one default export allowed per module.",
  564. DuplicateExport: "`%0` has already been exported. Exported identifiers must be unique.",
  565. DuplicateProto: "Redefinition of __proto__ property.",
  566. DuplicateRegExpFlags: "Duplicate regular expression flag.",
  567. ElementAfterRest: "Rest element must be last element.",
  568. EscapedCharNotAnIdentifier: "Invalid Unicode escape.",
  569. ExportBindingIsString: "A string literal cannot be used as an exported binding without `from`.\n- Did you mean `export { '%0' as '%1' } from 'some-module'`?",
  570. ExportDefaultFromAsIdentifier: "'from' is not allowed as an identifier after 'export default'.",
  571. ForInOfLoopInitializer: "'%0' loop variable declaration may not have an initializer.",
  572. ForOfAsync: "The left-hand side of a for-of loop may not be 'async'.",
  573. ForOfLet: "The left-hand side of a for-of loop may not start with 'let'.",
  574. GeneratorInSingleStatementContext: "Generators can only be declared at the top level or inside a block.",
  575. IllegalBreakContinue: "Unsyntactic %0.",
  576. IllegalLanguageModeDirective: "Illegal 'use strict' directive in function with non-simple parameter list.",
  577. IllegalReturn: "'return' outside of function.",
  578. ImportBindingIsString: 'A string literal cannot be used as an imported binding.\n- Did you mean `import { "%0" as foo }`?',
  579. ImportCallArgumentTrailingComma: "Trailing comma is disallowed inside import(...) arguments.",
  580. ImportCallArity: "`import()` requires exactly %0.",
  581. ImportCallNotNewExpression: "Cannot use new with import(...).",
  582. ImportCallSpreadArgument: "`...` is not allowed in `import()`.",
  583. InvalidBigIntLiteral: "Invalid BigIntLiteral.",
  584. InvalidCodePoint: "Code point out of bounds.",
  585. InvalidDecimal: "Invalid decimal.",
  586. InvalidDigit: "Expected number in radix %0.",
  587. InvalidEscapeSequence: "Bad character escape sequence.",
  588. InvalidEscapeSequenceTemplate: "Invalid escape sequence in template.",
  589. InvalidEscapedReservedWord: "Escape sequence in keyword %0.",
  590. InvalidIdentifier: "Invalid identifier %0.",
  591. InvalidLhs: "Invalid left-hand side in %0.",
  592. InvalidLhsBinding: "Binding invalid left-hand side in %0.",
  593. InvalidNumber: "Invalid number.",
  594. InvalidOrMissingExponent: "Floating-point numbers require a valid exponent after the 'e'.",
  595. InvalidOrUnexpectedToken: "Unexpected character '%0'.",
  596. InvalidParenthesizedAssignment: "Invalid parenthesized assignment pattern.",
  597. InvalidPrivateFieldResolution: "Private name #%0 is not defined.",
  598. InvalidPropertyBindingPattern: "Binding member expression.",
  599. InvalidRecordProperty: "Only properties and spread elements are allowed in record definitions.",
  600. InvalidRestAssignmentPattern: "Invalid rest operator's argument.",
  601. LabelRedeclaration: "Label '%0' is already declared.",
  602. LetInLexicalBinding: "'let' is not allowed to be used as a name in 'let' or 'const' declarations.",
  603. LineTerminatorBeforeArrow: "No line break is allowed before '=>'.",
  604. MalformedRegExpFlags: "Invalid regular expression flag.",
  605. MissingClassName: "A class name is required.",
  606. MissingEqInAssignment: "Only '=' operator can be used for specifying default value.",
  607. MissingSemicolon: "Missing semicolon.",
  608. MissingUnicodeEscape: "Expecting Unicode escape sequence \\uXXXX.",
  609. MixingCoalesceWithLogical: "Nullish coalescing operator(??) requires parens when mixing with logical operators.",
  610. ModuleAttributeDifferentFromType: "The only accepted module attribute is `type`.",
  611. ModuleAttributeInvalidValue: "Only string literals are allowed as module attribute values.",
  612. ModuleAttributesWithDuplicateKeys: 'Duplicate key "%0" is not allowed in module attributes.',
  613. ModuleExportNameHasLoneSurrogate: "An export name cannot include a lone surrogate, found '\\u%0'.",
  614. ModuleExportUndefined: "Export '%0' is not defined.",
  615. MultipleDefaultsInSwitch: "Multiple default clauses.",
  616. NewlineAfterThrow: "Illegal newline after throw.",
  617. NoCatchOrFinally: "Missing catch or finally clause.",
  618. NumberIdentifier: "Identifier directly after number.",
  619. NumericSeparatorInEscapeSequence: "Numeric separators are not allowed inside unicode escape sequences or hex escape sequences.",
  620. ObsoleteAwaitStar: "'await*' has been removed from the async functions proposal. Use Promise.all() instead.",
  621. OptionalChainingNoNew: "Constructors in/after an Optional Chain are not allowed.",
  622. OptionalChainingNoTemplate: "Tagged Template Literals are not allowed in optionalChain.",
  623. OverrideOnConstructor: "'override' modifier cannot appear on a constructor declaration.",
  624. ParamDupe: "Argument name clash.",
  625. PatternHasAccessor: "Object pattern can't contain getter or setter.",
  626. PatternHasMethod: "Object pattern can't contain methods.",
  627. PipelineBodyNoArrow: 'Unexpected arrow "=>" after pipeline body; arrow function in pipeline body must be parenthesized.',
  628. PipelineBodySequenceExpression: "Pipeline body may not be a comma-separated sequence expression.",
  629. PipelineHeadSequenceExpression: "Pipeline head should not be a comma-separated sequence expression.",
  630. PipelineTopicUnused: "Pipeline is in topic style but does not use topic reference.",
  631. PrimaryTopicNotAllowed: "Topic reference was used in a lexical context without topic binding.",
  632. PrimaryTopicRequiresSmartPipeline: "Primary Topic Reference found but pipelineOperator not passed 'smart' for 'proposal' option.",
  633. PrivateInExpectedIn: "Private names are only allowed in property accesses (`obj.#%0`) or in `in` expressions (`#%0 in obj`).",
  634. PrivateNameRedeclaration: "Duplicate private name #%0.",
  635. RecordExpressionBarIncorrectEndSyntaxType: "Record expressions ending with '|}' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'bar'.",
  636. RecordExpressionBarIncorrectStartSyntaxType: "Record expressions starting with '{|' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'bar'.",
  637. RecordExpressionHashIncorrectStartSyntaxType: "Record expressions starting with '#{' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'hash'.",
  638. RecordNoProto: "'__proto__' is not allowed in Record expressions.",
  639. RestTrailingComma: "Unexpected trailing comma after rest element.",
  640. SloppyFunction: "In non-strict mode code, functions can only be declared at top level, inside a block, or as the body of an if statement.",
  641. StaticPrototype: "Classes may not have static property named prototype.",
  642. StrictDelete: "Deleting local variable in strict mode.",
  643. StrictEvalArguments: "Assigning to '%0' in strict mode.",
  644. StrictEvalArgumentsBinding: "Binding '%0' in strict mode.",
  645. StrictFunction: "In strict mode code, functions can only be declared at top level or inside a block.",
  646. StrictNumericEscape: "The only valid numeric escape in strict mode is '\\0'.",
  647. StrictOctalLiteral: "Legacy octal literals are not allowed in strict mode.",
  648. StrictWith: "'with' in strict mode.",
  649. 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?",
  650. SuperPrivateField: "Private fields can't be accessed on super.",
  651. TrailingDecorator: "Decorators must be attached to a class element.",
  652. TupleExpressionBarIncorrectEndSyntaxType: "Tuple expressions ending with '|]' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'bar'.",
  653. TupleExpressionBarIncorrectStartSyntaxType: "Tuple expressions starting with '[|' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'bar'.",
  654. TupleExpressionHashIncorrectStartSyntaxType: "Tuple expressions starting with '#[' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'hash'.",
  655. UnexpectedArgumentPlaceholder: "Unexpected argument placeholder.",
  656. UnexpectedAwaitAfterPipelineBody: 'Unexpected "await" after pipeline body; await must have parentheses in minimal proposal.',
  657. UnexpectedDigitAfterHash: "Unexpected digit after hash token.",
  658. UnexpectedImportExport: "'import' and 'export' may only appear at the top level.",
  659. UnexpectedKeyword: "Unexpected keyword '%0'.",
  660. UnexpectedLeadingDecorator: "Leading decorators must be attached to a class declaration.",
  661. UnexpectedLexicalDeclaration: "Lexical declaration cannot appear in a single-statement context.",
  662. UnexpectedNewTarget: "`new.target` can only be used in functions or class properties.",
  663. UnexpectedNumericSeparator: "A numeric separator is only allowed between two digits.",
  664. UnexpectedPrivateField: "Private names can only be used as the name of a class element (i.e. class C { #p = 42; #m() {} } )\n or a property of member expression (i.e. this.#p).",
  665. UnexpectedReservedWord: "Unexpected reserved word '%0'.",
  666. UnexpectedSuper: "'super' is only allowed in object methods and classes.",
  667. UnexpectedToken: "Unexpected token '%0'.",
  668. UnexpectedTokenUnaryExponentiation: "Illegal expression. Wrap left hand side or entire exponentiation in parentheses.",
  669. UnsupportedBind: "Binding should be performed on object property.",
  670. UnsupportedDecoratorExport: "A decorated export must export a class declaration.",
  671. UnsupportedDefaultExport: "Only expressions, functions or classes are allowed as the `default` export.",
  672. UnsupportedImport: "`import` can only be used in `import()` or `import.meta`.",
  673. UnsupportedMetaProperty: "The only valid meta property for %0 is %0.%1.",
  674. UnsupportedParameterDecorator: "Decorators cannot be used to decorate parameters.",
  675. UnsupportedPropertyDecorator: "Decorators cannot be used to decorate object literal properties.",
  676. UnsupportedSuper: "'super' can only be used with function calls (i.e. super()) or in property accesses (i.e. super.prop or super[prop]).",
  677. UnterminatedComment: "Unterminated comment.",
  678. UnterminatedRegExp: "Unterminated regular expression.",
  679. UnterminatedString: "Unterminated string constant.",
  680. UnterminatedTemplate: "Unterminated template.",
  681. VarRedeclaration: "Identifier '%0' has already been declared.",
  682. YieldBindingIdentifier: "Can not use 'yield' as identifier inside a generator.",
  683. YieldInParameter: "Yield expression is not allowed in formal parameters.",
  684. ZeroDigitNumericSeparator: "Numeric separator can not be used after leading 0."
  685. }, ErrorCodes.SyntaxError);
  686. const SourceTypeModuleErrorMessages = makeErrorTemplates({
  687. ImportMetaOutsideModule: `import.meta may appear only with 'sourceType: "module"'`,
  688. ImportOutsideModule: `'import' and 'export' may appear only with 'sourceType: "module"'`
  689. }, ErrorCodes.SourceTypeModuleError);
  690. function makeErrorTemplates(messages, code) {
  691. const templates = {};
  692. Object.keys(messages).forEach(reasonCode => {
  693. templates[reasonCode] = Object.freeze({
  694. code,
  695. reasonCode,
  696. template: messages[reasonCode]
  697. });
  698. });
  699. return Object.freeze(templates);
  700. }
  701. class ParserError extends CommentsParser {
  702. getLocationForPosition(pos) {
  703. let loc;
  704. if (pos === this.state.start) loc = this.state.startLoc;else if (pos === this.state.lastTokStart) loc = this.state.lastTokStartLoc;else if (pos === this.state.end) loc = this.state.endLoc;else if (pos === this.state.lastTokEnd) loc = this.state.lastTokEndLoc;else loc = getLineInfo(this.input, pos);
  705. return loc;
  706. }
  707. raise(pos, {
  708. code,
  709. reasonCode,
  710. template
  711. }, ...params) {
  712. return this.raiseWithData(pos, {
  713. code,
  714. reasonCode
  715. }, template, ...params);
  716. }
  717. raiseOverwrite(pos, {
  718. code,
  719. template
  720. }, ...params) {
  721. const loc = this.getLocationForPosition(pos);
  722. const message = template.replace(/%(\d+)/g, (_, i) => params[i]) + ` (${loc.line}:${loc.column})`;
  723. if (this.options.errorRecovery) {
  724. const errors = this.state.errors;
  725. for (let i = errors.length - 1; i >= 0; i--) {
  726. const error = errors[i];
  727. if (error.pos === pos) {
  728. return Object.assign(error, {
  729. message
  730. });
  731. } else if (error.pos < pos) {
  732. break;
  733. }
  734. }
  735. }
  736. return this._raise({
  737. code,
  738. loc,
  739. pos
  740. }, message);
  741. }
  742. raiseWithData(pos, data, errorTemplate, ...params) {
  743. const loc = this.getLocationForPosition(pos);
  744. const message = errorTemplate.replace(/%(\d+)/g, (_, i) => params[i]) + ` (${loc.line}:${loc.column})`;
  745. return this._raise(Object.assign({
  746. loc,
  747. pos
  748. }, data), message);
  749. }
  750. _raise(errorContext, message) {
  751. const err = new SyntaxError(message);
  752. Object.assign(err, errorContext);
  753. if (this.options.errorRecovery) {
  754. if (!this.isLookahead) this.state.errors.push(err);
  755. return err;
  756. } else {
  757. throw err;
  758. }
  759. }
  760. }
  761. var estree = (superClass => class extends superClass {
  762. parseRegExpLiteral({
  763. pattern,
  764. flags
  765. }) {
  766. let regex = null;
  767. try {
  768. regex = new RegExp(pattern, flags);
  769. } catch (e) {}
  770. const node = this.estreeParseLiteral(regex);
  771. node.regex = {
  772. pattern,
  773. flags
  774. };
  775. return node;
  776. }
  777. parseBigIntLiteral(value) {
  778. let bigInt;
  779. try {
  780. bigInt = BigInt(value);
  781. } catch (_unused) {
  782. bigInt = null;
  783. }
  784. const node = this.estreeParseLiteral(bigInt);
  785. node.bigint = String(node.value || value);
  786. return node;
  787. }
  788. parseDecimalLiteral(value) {
  789. const decimal = null;
  790. const node = this.estreeParseLiteral(decimal);
  791. node.decimal = String(node.value || value);
  792. return node;
  793. }
  794. estreeParseLiteral(value) {
  795. return this.parseLiteral(value, "Literal");
  796. }
  797. parseStringLiteral(value) {
  798. return this.estreeParseLiteral(value);
  799. }
  800. parseNumericLiteral(value) {
  801. return this.estreeParseLiteral(value);
  802. }
  803. parseNullLiteral() {
  804. return this.estreeParseLiteral(null);
  805. }
  806. parseBooleanLiteral(value) {
  807. return this.estreeParseLiteral(value);
  808. }
  809. directiveToStmt(directive) {
  810. const directiveLiteral = directive.value;
  811. const stmt = this.startNodeAt(directive.start, directive.loc.start);
  812. const expression = this.startNodeAt(directiveLiteral.start, directiveLiteral.loc.start);
  813. expression.value = directiveLiteral.extra.expressionValue;
  814. expression.raw = directiveLiteral.extra.raw;
  815. stmt.expression = this.finishNodeAt(expression, "Literal", directiveLiteral.end, directiveLiteral.loc.end);
  816. stmt.directive = directiveLiteral.extra.raw.slice(1, -1);
  817. return this.finishNodeAt(stmt, "ExpressionStatement", directive.end, directive.loc.end);
  818. }
  819. initFunction(node, isAsync) {
  820. super.initFunction(node, isAsync);
  821. node.expression = false;
  822. }
  823. checkDeclaration(node) {
  824. if (node != null && this.isObjectProperty(node)) {
  825. this.checkDeclaration(node.value);
  826. } else {
  827. super.checkDeclaration(node);
  828. }
  829. }
  830. getObjectOrClassMethodParams(method) {
  831. return method.value.params;
  832. }
  833. isValidDirective(stmt) {
  834. var _stmt$expression$extr;
  835. return stmt.type === "ExpressionStatement" && stmt.expression.type === "Literal" && typeof stmt.expression.value === "string" && !((_stmt$expression$extr = stmt.expression.extra) != null && _stmt$expression$extr.parenthesized);
  836. }
  837. stmtToDirective(stmt) {
  838. const directive = super.stmtToDirective(stmt);
  839. const value = stmt.expression.value;
  840. this.addExtra(directive.value, "expressionValue", value);
  841. return directive;
  842. }
  843. parseBlockBody(node, ...args) {
  844. super.parseBlockBody(node, ...args);
  845. const directiveStatements = node.directives.map(d => this.directiveToStmt(d));
  846. node.body = directiveStatements.concat(node.body);
  847. delete node.directives;
  848. }
  849. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  850. this.parseMethod(method, isGenerator, isAsync, isConstructor, allowsDirectSuper, "ClassMethod", true);
  851. if (method.typeParameters) {
  852. method.value.typeParameters = method.typeParameters;
  853. delete method.typeParameters;
  854. }
  855. classBody.body.push(method);
  856. }
  857. parseMaybePrivateName(...args) {
  858. const node = super.parseMaybePrivateName(...args);
  859. if (node.type === "PrivateName" && this.getPluginOption("estree", "classFeatures")) {
  860. return this.convertPrivateNameToPrivateIdentifier(node);
  861. }
  862. return node;
  863. }
  864. convertPrivateNameToPrivateIdentifier(node) {
  865. const name = super.getPrivateNameSV(node);
  866. node = node;
  867. delete node.id;
  868. node.name = name;
  869. node.type = "PrivateIdentifier";
  870. return node;
  871. }
  872. isPrivateName(node) {
  873. if (!this.getPluginOption("estree", "classFeatures")) {
  874. return super.isPrivateName(node);
  875. }
  876. return node.type === "PrivateIdentifier";
  877. }
  878. getPrivateNameSV(node) {
  879. if (!this.getPluginOption("estree", "classFeatures")) {
  880. return super.getPrivateNameSV(node);
  881. }
  882. return node.name;
  883. }
  884. parseLiteral(value, type) {
  885. const node = super.parseLiteral(value, type);
  886. node.raw = node.extra.raw;
  887. delete node.extra;
  888. return node;
  889. }
  890. parseFunctionBody(node, allowExpression, isMethod = false) {
  891. super.parseFunctionBody(node, allowExpression, isMethod);
  892. node.expression = node.body.type !== "BlockStatement";
  893. }
  894. parseMethod(node, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope = false) {
  895. let funcNode = this.startNode();
  896. funcNode.kind = node.kind;
  897. funcNode = super.parseMethod(funcNode, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope);
  898. funcNode.type = "FunctionExpression";
  899. delete funcNode.kind;
  900. node.value = funcNode;
  901. if (type === "ClassPrivateMethod") {
  902. node.computed = false;
  903. }
  904. type = "MethodDefinition";
  905. return this.finishNode(node, type);
  906. }
  907. parseClassProperty(...args) {
  908. const propertyNode = super.parseClassProperty(...args);
  909. if (this.getPluginOption("estree", "classFeatures")) {
  910. propertyNode.type = "PropertyDefinition";
  911. }
  912. return propertyNode;
  913. }
  914. parseClassPrivateProperty(...args) {
  915. const propertyNode = super.parseClassPrivateProperty(...args);
  916. if (this.getPluginOption("estree", "classFeatures")) {
  917. propertyNode.type = "PropertyDefinition";
  918. propertyNode.computed = false;
  919. }
  920. return propertyNode;
  921. }
  922. parseObjectMethod(prop, isGenerator, isAsync, isPattern, isAccessor) {
  923. const node = super.parseObjectMethod(prop, isGenerator, isAsync, isPattern, isAccessor);
  924. if (node) {
  925. node.type = "Property";
  926. if (node.kind === "method") node.kind = "init";
  927. node.shorthand = false;
  928. }
  929. return node;
  930. }
  931. parseObjectProperty(prop, startPos, startLoc, isPattern, refExpressionErrors) {
  932. const node = super.parseObjectProperty(prop, startPos, startLoc, isPattern, refExpressionErrors);
  933. if (node) {
  934. node.kind = "init";
  935. node.type = "Property";
  936. }
  937. return node;
  938. }
  939. toAssignable(node, isLHS = false) {
  940. if (node != null && this.isObjectProperty(node)) {
  941. this.toAssignable(node.value, isLHS);
  942. return node;
  943. }
  944. return super.toAssignable(node, isLHS);
  945. }
  946. toAssignableObjectExpressionProp(prop, ...args) {
  947. if (prop.kind === "get" || prop.kind === "set") {
  948. this.raise(prop.key.start, ErrorMessages.PatternHasAccessor);
  949. } else if (prop.method) {
  950. this.raise(prop.key.start, ErrorMessages.PatternHasMethod);
  951. } else {
  952. super.toAssignableObjectExpressionProp(prop, ...args);
  953. }
  954. }
  955. finishCallExpression(node, optional) {
  956. super.finishCallExpression(node, optional);
  957. if (node.callee.type === "Import") {
  958. node.type = "ImportExpression";
  959. node.source = node.arguments[0];
  960. if (this.hasPlugin("importAssertions")) {
  961. var _node$arguments$;
  962. node.attributes = (_node$arguments$ = node.arguments[1]) != null ? _node$arguments$ : null;
  963. }
  964. delete node.arguments;
  965. delete node.callee;
  966. }
  967. return node;
  968. }
  969. toReferencedArguments(node) {
  970. if (node.type === "ImportExpression") {
  971. return;
  972. }
  973. super.toReferencedArguments(node);
  974. }
  975. parseExport(node) {
  976. super.parseExport(node);
  977. switch (node.type) {
  978. case "ExportAllDeclaration":
  979. node.exported = null;
  980. break;
  981. case "ExportNamedDeclaration":
  982. if (node.specifiers.length === 1 && node.specifiers[0].type === "ExportNamespaceSpecifier") {
  983. node.type = "ExportAllDeclaration";
  984. node.exported = node.specifiers[0].exported;
  985. delete node.specifiers;
  986. }
  987. break;
  988. }
  989. return node;
  990. }
  991. parseSubscript(base, startPos, startLoc, noCalls, state) {
  992. const node = super.parseSubscript(base, startPos, startLoc, noCalls, state);
  993. if (state.optionalChainMember) {
  994. if (node.type === "OptionalMemberExpression" || node.type === "OptionalCallExpression") {
  995. node.type = node.type.substring(8);
  996. }
  997. if (state.stop) {
  998. const chain = this.startNodeAtNode(node);
  999. chain.expression = node;
  1000. return this.finishNode(chain, "ChainExpression");
  1001. }
  1002. } else if (node.type === "MemberExpression" || node.type === "CallExpression") {
  1003. node.optional = false;
  1004. }
  1005. return node;
  1006. }
  1007. hasPropertyAsPrivateName(node) {
  1008. if (node.type === "ChainExpression") {
  1009. node = node.expression;
  1010. }
  1011. return super.hasPropertyAsPrivateName(node);
  1012. }
  1013. isOptionalChain(node) {
  1014. return node.type === "ChainExpression";
  1015. }
  1016. isObjectProperty(node) {
  1017. return node.type === "Property" && node.kind === "init" && !node.method;
  1018. }
  1019. isObjectMethod(node) {
  1020. return node.method || node.kind === "get" || node.kind === "set";
  1021. }
  1022. });
  1023. class TokContext {
  1024. constructor(token, preserveSpace) {
  1025. this.token = void 0;
  1026. this.preserveSpace = void 0;
  1027. this.token = token;
  1028. this.preserveSpace = !!preserveSpace;
  1029. }
  1030. }
  1031. const types = {
  1032. brace: new TokContext("{"),
  1033. template: new TokContext("`", true)
  1034. };
  1035. types$1.braceR.updateContext = context => {
  1036. context.pop();
  1037. };
  1038. types$1.braceL.updateContext = types$1.braceHashL.updateContext = types$1.dollarBraceL.updateContext = context => {
  1039. context.push(types.brace);
  1040. };
  1041. types$1.backQuote.updateContext = context => {
  1042. if (context[context.length - 1] === types.template) {
  1043. context.pop();
  1044. } else {
  1045. context.push(types.template);
  1046. }
  1047. };
  1048. 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\u08a0-\u08b4\u08b6-\u08c7\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\u0c60\u0c61\u0c80\u0c85-\u0c8c\u0c8e-\u0c90\u0c92-\u0ca8\u0caa-\u0cb3\u0cb5-\u0cb9\u0cbd\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-\u170c\u170e-\u1711\u1720-\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-\u1b4b\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-\u2c2e\u2c30-\u2c5e\u2c60-\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-\u9ffc\ua000-\ua48c\ua4d0-\ua4fd\ua500-\ua60c\ua610-\ua61f\ua62a\ua62b\ua640-\ua66e\ua67f-\ua69d\ua6a0-\ua6ef\ua717-\ua71f\ua722-\ua788\ua78b-\ua7bf\ua7c2-\ua7ca\ua7f5-\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";
  1049. 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\u08d3-\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\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\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-\u0ecd\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-\u1714\u1732-\u1734\u1752\u1753\u1772\u1773\u17b4-\u17d3\u17dd\u17e0-\u17e9\u180b-\u180d\u1810-\u1819\u18a9\u1920-\u192b\u1930-\u193b\u1946-\u194f\u19d0-\u19da\u1a17-\u1a1b\u1a55-\u1a5e\u1a60-\u1a7c\u1a7f-\u1a89\u1a90-\u1a99\u1ab0-\u1abd\u1abf\u1ac0\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-\u1df9\u1dfb-\u1dff\u203f\u2040\u2054\u20d0-\u20dc\u20e1\u20e5-\u20f0\u2cef-\u2cf1\u2d7f\u2de0-\u2dff\u302a-\u302f\u3099\u309a\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";
  1050. const nonASCIIidentifierStart = new RegExp("[" + nonASCIIidentifierStartChars + "]");
  1051. const nonASCIIidentifier = new RegExp("[" + nonASCIIidentifierStartChars + nonASCIIidentifierChars + "]");
  1052. nonASCIIidentifierStartChars = nonASCIIidentifierChars = null;
  1053. 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, 157, 310, 10, 21, 11, 7, 153, 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, 107, 20, 28, 22, 13, 52, 76, 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, 85, 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, 230, 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, 35, 56, 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, 190, 0, 80, 921, 103, 110, 18, 195, 2749, 1070, 4050, 582, 8634, 568, 8, 30, 114, 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, 8952, 286, 50, 2, 18, 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, 2357, 44, 11, 6, 17, 0, 370, 43, 1301, 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, 42717, 35, 4148, 12, 221, 3, 5761, 15, 7472, 3104, 541, 1507, 4938];
  1054. 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, 154, 10, 176, 2, 54, 14, 32, 9, 16, 3, 46, 10, 54, 9, 7, 2, 37, 13, 2, 9, 6, 1, 45, 0, 13, 2, 49, 13, 9, 3, 2, 11, 83, 11, 7, 0, 161, 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, 19306, 9, 135, 4, 60, 6, 26, 9, 1014, 0, 2, 54, 8, 3, 82, 0, 12, 1, 19628, 1, 5319, 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, 262, 6, 10, 9, 419, 13, 1495, 6, 110, 6, 6, 9, 4759, 9, 787719, 239];
  1055. function isInAstralSet(code, set) {
  1056. let pos = 0x10000;
  1057. for (let i = 0, length = set.length; i < length; i += 2) {
  1058. pos += set[i];
  1059. if (pos > code) return false;
  1060. pos += set[i + 1];
  1061. if (pos >= code) return true;
  1062. }
  1063. return false;
  1064. }
  1065. function isIdentifierStart(code) {
  1066. if (code < 65) return code === 36;
  1067. if (code <= 90) return true;
  1068. if (code < 97) return code === 95;
  1069. if (code <= 122) return true;
  1070. if (code <= 0xffff) {
  1071. return code >= 0xaa && nonASCIIidentifierStart.test(String.fromCharCode(code));
  1072. }
  1073. return isInAstralSet(code, astralIdentifierStartCodes);
  1074. }
  1075. function isIdentifierChar(code) {
  1076. if (code < 48) return code === 36;
  1077. if (code < 58) return true;
  1078. if (code < 65) return false;
  1079. if (code <= 90) return true;
  1080. if (code < 97) return code === 95;
  1081. if (code <= 122) return true;
  1082. if (code <= 0xffff) {
  1083. return code >= 0xaa && nonASCIIidentifier.test(String.fromCharCode(code));
  1084. }
  1085. return isInAstralSet(code, astralIdentifierStartCodes) || isInAstralSet(code, astralIdentifierCodes);
  1086. }
  1087. const reservedWords = {
  1088. 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"],
  1089. strict: ["implements", "interface", "let", "package", "private", "protected", "public", "static", "yield"],
  1090. strictBind: ["eval", "arguments"]
  1091. };
  1092. const keywords = new Set(reservedWords.keyword);
  1093. const reservedWordsStrictSet = new Set(reservedWords.strict);
  1094. const reservedWordsStrictBindSet = new Set(reservedWords.strictBind);
  1095. function isReservedWord(word, inModule) {
  1096. return inModule && word === "await" || word === "enum";
  1097. }
  1098. function isStrictReservedWord(word, inModule) {
  1099. return isReservedWord(word, inModule) || reservedWordsStrictSet.has(word);
  1100. }
  1101. function isStrictBindOnlyReservedWord(word) {
  1102. return reservedWordsStrictBindSet.has(word);
  1103. }
  1104. function isStrictBindReservedWord(word, inModule) {
  1105. return isStrictReservedWord(word, inModule) || isStrictBindOnlyReservedWord(word);
  1106. }
  1107. function isKeyword(word) {
  1108. return keywords.has(word);
  1109. }
  1110. function isIteratorStart(current, next) {
  1111. return current === 64 && next === 64;
  1112. }
  1113. 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"]);
  1114. function canBeReservedWord(word) {
  1115. return reservedWordLikeSet.has(word);
  1116. }
  1117. const SCOPE_OTHER = 0b000000000,
  1118. SCOPE_PROGRAM = 0b000000001,
  1119. SCOPE_FUNCTION = 0b000000010,
  1120. SCOPE_ARROW = 0b000000100,
  1121. SCOPE_SIMPLE_CATCH = 0b000001000,
  1122. SCOPE_SUPER = 0b000010000,
  1123. SCOPE_DIRECT_SUPER = 0b000100000,
  1124. SCOPE_CLASS = 0b001000000,
  1125. SCOPE_STATIC_BLOCK = 0b010000000,
  1126. SCOPE_TS_MODULE = 0b100000000,
  1127. SCOPE_VAR = SCOPE_PROGRAM | SCOPE_FUNCTION | SCOPE_TS_MODULE;
  1128. const BIND_KIND_VALUE = 0b000000000001,
  1129. BIND_KIND_TYPE = 0b000000000010,
  1130. BIND_SCOPE_VAR = 0b000000000100,
  1131. BIND_SCOPE_LEXICAL = 0b000000001000,
  1132. BIND_SCOPE_FUNCTION = 0b000000010000,
  1133. BIND_FLAGS_NONE = 0b000001000000,
  1134. BIND_FLAGS_CLASS = 0b000010000000,
  1135. BIND_FLAGS_TS_ENUM = 0b000100000000,
  1136. BIND_FLAGS_TS_CONST_ENUM = 0b001000000000,
  1137. BIND_FLAGS_TS_EXPORT_ONLY = 0b010000000000,
  1138. BIND_FLAGS_FLOW_DECLARE_FN = 0b100000000000;
  1139. const BIND_CLASS = BIND_KIND_VALUE | BIND_KIND_TYPE | BIND_SCOPE_LEXICAL | BIND_FLAGS_CLASS,
  1140. BIND_LEXICAL = BIND_KIND_VALUE | 0 | BIND_SCOPE_LEXICAL | 0,
  1141. BIND_VAR = BIND_KIND_VALUE | 0 | BIND_SCOPE_VAR | 0,
  1142. BIND_FUNCTION = BIND_KIND_VALUE | 0 | BIND_SCOPE_FUNCTION | 0,
  1143. BIND_TS_INTERFACE = 0 | BIND_KIND_TYPE | 0 | BIND_FLAGS_CLASS,
  1144. BIND_TS_TYPE = 0 | BIND_KIND_TYPE | 0 | 0,
  1145. BIND_TS_ENUM = BIND_KIND_VALUE | BIND_KIND_TYPE | BIND_SCOPE_LEXICAL | BIND_FLAGS_TS_ENUM,
  1146. BIND_TS_AMBIENT = 0 | 0 | 0 | BIND_FLAGS_TS_EXPORT_ONLY,
  1147. BIND_NONE = 0 | 0 | 0 | BIND_FLAGS_NONE,
  1148. BIND_OUTSIDE = BIND_KIND_VALUE | 0 | 0 | BIND_FLAGS_NONE,
  1149. BIND_TS_CONST_ENUM = BIND_TS_ENUM | BIND_FLAGS_TS_CONST_ENUM,
  1150. BIND_TS_NAMESPACE = 0 | 0 | 0 | BIND_FLAGS_TS_EXPORT_ONLY,
  1151. BIND_FLOW_DECLARE_FN = BIND_FLAGS_FLOW_DECLARE_FN;
  1152. const CLASS_ELEMENT_FLAG_STATIC = 0b100,
  1153. CLASS_ELEMENT_KIND_GETTER = 0b010,
  1154. CLASS_ELEMENT_KIND_SETTER = 0b001,
  1155. CLASS_ELEMENT_KIND_ACCESSOR = CLASS_ELEMENT_KIND_GETTER | CLASS_ELEMENT_KIND_SETTER;
  1156. const CLASS_ELEMENT_STATIC_GETTER = CLASS_ELEMENT_KIND_GETTER | CLASS_ELEMENT_FLAG_STATIC,
  1157. CLASS_ELEMENT_STATIC_SETTER = CLASS_ELEMENT_KIND_SETTER | CLASS_ELEMENT_FLAG_STATIC,
  1158. CLASS_ELEMENT_INSTANCE_GETTER = CLASS_ELEMENT_KIND_GETTER,
  1159. CLASS_ELEMENT_INSTANCE_SETTER = CLASS_ELEMENT_KIND_SETTER,
  1160. CLASS_ELEMENT_OTHER = 0;
  1161. class Scope {
  1162. constructor(flags) {
  1163. this.var = new Set();
  1164. this.lexical = new Set();
  1165. this.functions = new Set();
  1166. this.flags = flags;
  1167. }
  1168. }
  1169. class ScopeHandler {
  1170. constructor(raise, inModule) {
  1171. this.scopeStack = [];
  1172. this.undefinedExports = new Map();
  1173. this.undefinedPrivateNames = new Map();
  1174. this.raise = raise;
  1175. this.inModule = inModule;
  1176. }
  1177. get inFunction() {
  1178. return (this.currentVarScopeFlags() & SCOPE_FUNCTION) > 0;
  1179. }
  1180. get allowSuper() {
  1181. return (this.currentThisScopeFlags() & SCOPE_SUPER) > 0;
  1182. }
  1183. get allowDirectSuper() {
  1184. return (this.currentThisScopeFlags() & SCOPE_DIRECT_SUPER) > 0;
  1185. }
  1186. get inClass() {
  1187. return (this.currentThisScopeFlags() & SCOPE_CLASS) > 0;
  1188. }
  1189. get inClassAndNotInNonArrowFunction() {
  1190. const flags = this.currentThisScopeFlags();
  1191. return (flags & SCOPE_CLASS) > 0 && (flags & SCOPE_FUNCTION) === 0;
  1192. }
  1193. get inStaticBlock() {
  1194. return (this.currentThisScopeFlags() & SCOPE_STATIC_BLOCK) > 0;
  1195. }
  1196. get inNonArrowFunction() {
  1197. return (this.currentThisScopeFlags() & SCOPE_FUNCTION) > 0;
  1198. }
  1199. get treatFunctionsAsVar() {
  1200. return this.treatFunctionsAsVarInScope(this.currentScope());
  1201. }
  1202. createScope(flags) {
  1203. return new Scope(flags);
  1204. }
  1205. enter(flags) {
  1206. this.scopeStack.push(this.createScope(flags));
  1207. }
  1208. exit() {
  1209. this.scopeStack.pop();
  1210. }
  1211. treatFunctionsAsVarInScope(scope) {
  1212. return !!(scope.flags & SCOPE_FUNCTION || !this.inModule && scope.flags & SCOPE_PROGRAM);
  1213. }
  1214. declareName(name, bindingType, pos) {
  1215. let scope = this.currentScope();
  1216. if (bindingType & BIND_SCOPE_LEXICAL || bindingType & BIND_SCOPE_FUNCTION) {
  1217. this.checkRedeclarationInScope(scope, name, bindingType, pos);
  1218. if (bindingType & BIND_SCOPE_FUNCTION) {
  1219. scope.functions.add(name);
  1220. } else {
  1221. scope.lexical.add(name);
  1222. }
  1223. if (bindingType & BIND_SCOPE_LEXICAL) {
  1224. this.maybeExportDefined(scope, name);
  1225. }
  1226. } else if (bindingType & BIND_SCOPE_VAR) {
  1227. for (let i = this.scopeStack.length - 1; i >= 0; --i) {
  1228. scope = this.scopeStack[i];
  1229. this.checkRedeclarationInScope(scope, name, bindingType, pos);
  1230. scope.var.add(name);
  1231. this.maybeExportDefined(scope, name);
  1232. if (scope.flags & SCOPE_VAR) break;
  1233. }
  1234. }
  1235. if (this.inModule && scope.flags & SCOPE_PROGRAM) {
  1236. this.undefinedExports.delete(name);
  1237. }
  1238. }
  1239. maybeExportDefined(scope, name) {
  1240. if (this.inModule && scope.flags & SCOPE_PROGRAM) {
  1241. this.undefinedExports.delete(name);
  1242. }
  1243. }
  1244. checkRedeclarationInScope(scope, name, bindingType, pos) {
  1245. if (this.isRedeclaredInScope(scope, name, bindingType)) {
  1246. this.raise(pos, ErrorMessages.VarRedeclaration, name);
  1247. }
  1248. }
  1249. isRedeclaredInScope(scope, name, bindingType) {
  1250. if (!(bindingType & BIND_KIND_VALUE)) return false;
  1251. if (bindingType & BIND_SCOPE_LEXICAL) {
  1252. return scope.lexical.has(name) || scope.functions.has(name) || scope.var.has(name);
  1253. }
  1254. if (bindingType & BIND_SCOPE_FUNCTION) {
  1255. return scope.lexical.has(name) || !this.treatFunctionsAsVarInScope(scope) && scope.var.has(name);
  1256. }
  1257. return scope.lexical.has(name) && !(scope.flags & SCOPE_SIMPLE_CATCH && scope.lexical.values().next().value === name) || !this.treatFunctionsAsVarInScope(scope) && scope.functions.has(name);
  1258. }
  1259. checkLocalExport(id) {
  1260. const {
  1261. name
  1262. } = id;
  1263. const topLevelScope = this.scopeStack[0];
  1264. if (!topLevelScope.lexical.has(name) && !topLevelScope.var.has(name) && !topLevelScope.functions.has(name)) {
  1265. this.undefinedExports.set(name, id.start);
  1266. }
  1267. }
  1268. currentScope() {
  1269. return this.scopeStack[this.scopeStack.length - 1];
  1270. }
  1271. currentVarScopeFlags() {
  1272. for (let i = this.scopeStack.length - 1;; i--) {
  1273. const {
  1274. flags
  1275. } = this.scopeStack[i];
  1276. if (flags & SCOPE_VAR) {
  1277. return flags;
  1278. }
  1279. }
  1280. }
  1281. currentThisScopeFlags() {
  1282. for (let i = this.scopeStack.length - 1;; i--) {
  1283. const {
  1284. flags
  1285. } = this.scopeStack[i];
  1286. if (flags & (SCOPE_VAR | SCOPE_CLASS) && !(flags & SCOPE_ARROW)) {
  1287. return flags;
  1288. }
  1289. }
  1290. }
  1291. }
  1292. class FlowScope extends Scope {
  1293. constructor(...args) {
  1294. super(...args);
  1295. this.declareFunctions = new Set();
  1296. }
  1297. }
  1298. class FlowScopeHandler extends ScopeHandler {
  1299. createScope(flags) {
  1300. return new FlowScope(flags);
  1301. }
  1302. declareName(name, bindingType, pos) {
  1303. const scope = this.currentScope();
  1304. if (bindingType & BIND_FLAGS_FLOW_DECLARE_FN) {
  1305. this.checkRedeclarationInScope(scope, name, bindingType, pos);
  1306. this.maybeExportDefined(scope, name);
  1307. scope.declareFunctions.add(name);
  1308. return;
  1309. }
  1310. super.declareName(...arguments);
  1311. }
  1312. isRedeclaredInScope(scope, name, bindingType) {
  1313. if (super.isRedeclaredInScope(...arguments)) return true;
  1314. if (bindingType & BIND_FLAGS_FLOW_DECLARE_FN) {
  1315. return !scope.declareFunctions.has(name) && (scope.lexical.has(name) || scope.functions.has(name));
  1316. }
  1317. return false;
  1318. }
  1319. checkLocalExport(id) {
  1320. if (!this.scopeStack[0].declareFunctions.has(id.name)) {
  1321. super.checkLocalExport(id);
  1322. }
  1323. }
  1324. }
  1325. const reservedTypes = new Set(["_", "any", "bool", "boolean", "empty", "extends", "false", "interface", "mixed", "null", "number", "static", "string", "true", "typeof", "void"]);
  1326. const FlowErrors = makeErrorTemplates({
  1327. AmbiguousConditionalArrow: "Ambiguous expression: wrap the arrow functions in parentheses to disambiguate.",
  1328. 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.",
  1329. AssignReservedType: "Cannot overwrite reserved type %0.",
  1330. DeclareClassElement: "The `declare` modifier can only appear on class fields.",
  1331. DeclareClassFieldInitializer: "Initializers are not allowed in fields with the `declare` modifier.",
  1332. DuplicateDeclareModuleExports: "Duplicate `declare module.exports` statement.",
  1333. EnumBooleanMemberNotInitialized: "Boolean enum members need to be initialized. Use either `%0 = true,` or `%0 = false,` in enum `%1`.",
  1334. EnumDuplicateMemberName: "Enum member names need to be unique, but the name `%0` has already been used before in enum `%1`.",
  1335. EnumInconsistentMemberValues: "Enum `%0` has inconsistent member initializers. Either use no initializers, or consistently use literals (either booleans, numbers, or strings) for all member initializers.",
  1336. EnumInvalidExplicitType: "Enum type `%1` is not valid. Use one of `boolean`, `number`, `string`, or `symbol` in enum `%0`.",
  1337. EnumInvalidExplicitTypeUnknownSupplied: "Supplied enum type is not valid. Use one of `boolean`, `number`, `string`, or `symbol` in enum `%0`.",
  1338. EnumInvalidMemberInitializerPrimaryType: "Enum `%0` has type `%2`, so the initializer of `%1` needs to be a %2 literal.",
  1339. EnumInvalidMemberInitializerSymbolType: "Symbol enum members cannot be initialized. Use `%1,` in enum `%0`.",
  1340. EnumInvalidMemberInitializerUnknownType: "The enum member initializer for `%1` needs to be a literal (either a boolean, number, or string) in enum `%0`.",
  1341. EnumInvalidMemberName: "Enum member names cannot start with lowercase 'a' through 'z'. Instead of using `%0`, consider using `%1`, in enum `%2`.",
  1342. EnumNumberMemberNotInitialized: "Number enum members need to be initialized, e.g. `%1 = 1` in enum `%0`.",
  1343. EnumStringMemberInconsistentlyInitailized: "String enum members need to consistently either all use initializers, or use no initializers, in enum `%0`.",
  1344. GetterMayNotHaveThisParam: "A getter cannot have a `this` parameter.",
  1345. 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.",
  1346. InexactInsideExact: "Explicit inexact syntax cannot appear inside an explicit exact object type.",
  1347. InexactInsideNonObject: "Explicit inexact syntax cannot appear in class or interface definitions.",
  1348. InexactVariance: "Explicit inexact syntax cannot have variance.",
  1349. InvalidNonTypeImportInDeclareModule: "Imports within a `declare module` body must always be `import type` or `import typeof`.",
  1350. MissingTypeParamDefault: "Type parameter declaration needs a default, since a preceding type parameter declaration has a default.",
  1351. NestedDeclareModule: "`declare module` cannot be used inside another `declare module`.",
  1352. NestedFlowComment: "Cannot have a flow comment inside another flow comment.",
  1353. OptionalBindingPattern: "A binding pattern parameter cannot be optional in an implementation signature.",
  1354. SetterMayNotHaveThisParam: "A setter cannot have a `this` parameter.",
  1355. SpreadVariance: "Spread properties cannot have variance.",
  1356. ThisParamAnnotationRequired: "A type annotation is required for the `this` parameter.",
  1357. ThisParamBannedInConstructor: "Constructors cannot have a `this` parameter; constructors don't bind `this` like other functions.",
  1358. ThisParamMayNotBeOptional: "The `this` parameter cannot be optional.",
  1359. ThisParamMustBeFirst: "The `this` parameter must be the first function parameter.",
  1360. ThisParamNoDefault: "The `this` parameter may not have a default value.",
  1361. TypeBeforeInitializer: "Type annotations must come before default assignments, e.g. instead of `age = 25: number` use `age: number = 25`.",
  1362. TypeCastInPattern: "The type cast expression is expected to be wrapped with parenthesis.",
  1363. UnexpectedExplicitInexactInObject: "Explicit inexact syntax must appear at the end of an inexact object.",
  1364. UnexpectedReservedType: "Unexpected reserved type %0.",
  1365. UnexpectedReservedUnderscore: "`_` is only allowed as a type argument to call or new.",
  1366. UnexpectedSpaceBetweenModuloChecks: "Spaces between `%` and `checks` are not allowed here.",
  1367. UnexpectedSpreadType: "Spread operator cannot appear in class or interface definitions.",
  1368. UnexpectedSubtractionOperand: 'Unexpected token, expected "number" or "bigint".',
  1369. UnexpectedTokenAfterTypeParameter: "Expected an arrow function after this type parameter declaration.",
  1370. UnexpectedTypeParameterBeforeAsyncArrowFunction: "Type parameters must come after the async keyword, e.g. instead of `<T> async () => {}`, use `async <T>() => {}`.",
  1371. UnsupportedDeclareExportKind: "`declare export %0` is not supported. Use `%1` instead.",
  1372. UnsupportedStatementInDeclareModule: "Only declares and type imports are allowed inside declare module.",
  1373. UnterminatedFlowComment: "Unterminated flow-comment."
  1374. }, ErrorCodes.SyntaxError);
  1375. function isEsModuleType(bodyElement) {
  1376. return bodyElement.type === "DeclareExportAllDeclaration" || bodyElement.type === "DeclareExportDeclaration" && (!bodyElement.declaration || bodyElement.declaration.type !== "TypeAlias" && bodyElement.declaration.type !== "InterfaceDeclaration");
  1377. }
  1378. function hasTypeImportKind(node) {
  1379. return node.importKind === "type" || node.importKind === "typeof";
  1380. }
  1381. function isMaybeDefaultImport(state) {
  1382. return (state.type === types$1.name || !!state.type.keyword) && state.value !== "from";
  1383. }
  1384. const exportSuggestions = {
  1385. const: "declare export var",
  1386. let: "declare export var",
  1387. type: "export type",
  1388. interface: "export interface"
  1389. };
  1390. function partition(list, test) {
  1391. const list1 = [];
  1392. const list2 = [];
  1393. for (let i = 0; i < list.length; i++) {
  1394. (test(list[i], i, list) ? list1 : list2).push(list[i]);
  1395. }
  1396. return [list1, list2];
  1397. }
  1398. const FLOW_PRAGMA_REGEX = /\*?\s*@((?:no)?flow)\b/;
  1399. var flow = (superClass => class extends superClass {
  1400. constructor(...args) {
  1401. super(...args);
  1402. this.flowPragma = undefined;
  1403. }
  1404. getScopeHandler() {
  1405. return FlowScopeHandler;
  1406. }
  1407. shouldParseTypes() {
  1408. return this.getPluginOption("flow", "all") || this.flowPragma === "flow";
  1409. }
  1410. shouldParseEnums() {
  1411. return !!this.getPluginOption("flow", "enums");
  1412. }
  1413. finishToken(type, val) {
  1414. if (type !== types$1.string && type !== types$1.semi && type !== types$1.interpreterDirective) {
  1415. if (this.flowPragma === undefined) {
  1416. this.flowPragma = null;
  1417. }
  1418. }
  1419. return super.finishToken(type, val);
  1420. }
  1421. addComment(comment) {
  1422. if (this.flowPragma === undefined) {
  1423. const matches = FLOW_PRAGMA_REGEX.exec(comment.value);
  1424. if (!matches) ; else if (matches[1] === "flow") {
  1425. this.flowPragma = "flow";
  1426. } else if (matches[1] === "noflow") {
  1427. this.flowPragma = "noflow";
  1428. } else {
  1429. throw new Error("Unexpected flow pragma");
  1430. }
  1431. }
  1432. return super.addComment(comment);
  1433. }
  1434. flowParseTypeInitialiser(tok) {
  1435. const oldInType = this.state.inType;
  1436. this.state.inType = true;
  1437. this.expect(tok || types$1.colon);
  1438. const type = this.flowParseType();
  1439. this.state.inType = oldInType;
  1440. return type;
  1441. }
  1442. flowParsePredicate() {
  1443. const node = this.startNode();
  1444. const moduloPos = this.state.start;
  1445. this.next();
  1446. this.expectContextual("checks");
  1447. if (this.state.lastTokStart > moduloPos + 1) {
  1448. this.raise(moduloPos, FlowErrors.UnexpectedSpaceBetweenModuloChecks);
  1449. }
  1450. if (this.eat(types$1.parenL)) {
  1451. node.value = this.parseExpression();
  1452. this.expect(types$1.parenR);
  1453. return this.finishNode(node, "DeclaredPredicate");
  1454. } else {
  1455. return this.finishNode(node, "InferredPredicate");
  1456. }
  1457. }
  1458. flowParseTypeAndPredicateInitialiser() {
  1459. const oldInType = this.state.inType;
  1460. this.state.inType = true;
  1461. this.expect(types$1.colon);
  1462. let type = null;
  1463. let predicate = null;
  1464. if (this.match(types$1.modulo)) {
  1465. this.state.inType = oldInType;
  1466. predicate = this.flowParsePredicate();
  1467. } else {
  1468. type = this.flowParseType();
  1469. this.state.inType = oldInType;
  1470. if (this.match(types$1.modulo)) {
  1471. predicate = this.flowParsePredicate();
  1472. }
  1473. }
  1474. return [type, predicate];
  1475. }
  1476. flowParseDeclareClass(node) {
  1477. this.next();
  1478. this.flowParseInterfaceish(node, true);
  1479. return this.finishNode(node, "DeclareClass");
  1480. }
  1481. flowParseDeclareFunction(node) {
  1482. this.next();
  1483. const id = node.id = this.parseIdentifier();
  1484. const typeNode = this.startNode();
  1485. const typeContainer = this.startNode();
  1486. if (this.isRelational("<")) {
  1487. typeNode.typeParameters = this.flowParseTypeParameterDeclaration();
  1488. } else {
  1489. typeNode.typeParameters = null;
  1490. }
  1491. this.expect(types$1.parenL);
  1492. const tmp = this.flowParseFunctionTypeParams();
  1493. typeNode.params = tmp.params;
  1494. typeNode.rest = tmp.rest;
  1495. typeNode.this = tmp._this;
  1496. this.expect(types$1.parenR);
  1497. [typeNode.returnType, node.predicate] = this.flowParseTypeAndPredicateInitialiser();
  1498. typeContainer.typeAnnotation = this.finishNode(typeNode, "FunctionTypeAnnotation");
  1499. id.typeAnnotation = this.finishNode(typeContainer, "TypeAnnotation");
  1500. this.resetEndLocation(id);
  1501. this.semicolon();
  1502. this.scope.declareName(node.id.name, BIND_FLOW_DECLARE_FN, node.id.start);
  1503. return this.finishNode(node, "DeclareFunction");
  1504. }
  1505. flowParseDeclare(node, insideModule) {
  1506. if (this.match(types$1._class)) {
  1507. return this.flowParseDeclareClass(node);
  1508. } else if (this.match(types$1._function)) {
  1509. return this.flowParseDeclareFunction(node);
  1510. } else if (this.match(types$1._var)) {
  1511. return this.flowParseDeclareVariable(node);
  1512. } else if (this.eatContextual("module")) {
  1513. if (this.match(types$1.dot)) {
  1514. return this.flowParseDeclareModuleExports(node);
  1515. } else {
  1516. if (insideModule) {
  1517. this.raise(this.state.lastTokStart, FlowErrors.NestedDeclareModule);
  1518. }
  1519. return this.flowParseDeclareModule(node);
  1520. }
  1521. } else if (this.isContextual("type")) {
  1522. return this.flowParseDeclareTypeAlias(node);
  1523. } else if (this.isContextual("opaque")) {
  1524. return this.flowParseDeclareOpaqueType(node);
  1525. } else if (this.isContextual("interface")) {
  1526. return this.flowParseDeclareInterface(node);
  1527. } else if (this.match(types$1._export)) {
  1528. return this.flowParseDeclareExportDeclaration(node, insideModule);
  1529. } else {
  1530. throw this.unexpected();
  1531. }
  1532. }
  1533. flowParseDeclareVariable(node) {
  1534. this.next();
  1535. node.id = this.flowParseTypeAnnotatableIdentifier(true);
  1536. this.scope.declareName(node.id.name, BIND_VAR, node.id.start);
  1537. this.semicolon();
  1538. return this.finishNode(node, "DeclareVariable");
  1539. }
  1540. flowParseDeclareModule(node) {
  1541. this.scope.enter(SCOPE_OTHER);
  1542. if (this.match(types$1.string)) {
  1543. node.id = this.parseExprAtom();
  1544. } else {
  1545. node.id = this.parseIdentifier();
  1546. }
  1547. const bodyNode = node.body = this.startNode();
  1548. const body = bodyNode.body = [];
  1549. this.expect(types$1.braceL);
  1550. while (!this.match(types$1.braceR)) {
  1551. let bodyNode = this.startNode();
  1552. if (this.match(types$1._import)) {
  1553. this.next();
  1554. if (!this.isContextual("type") && !this.match(types$1._typeof)) {
  1555. this.raise(this.state.lastTokStart, FlowErrors.InvalidNonTypeImportInDeclareModule);
  1556. }
  1557. this.parseImport(bodyNode);
  1558. } else {
  1559. this.expectContextual("declare", FlowErrors.UnsupportedStatementInDeclareModule);
  1560. bodyNode = this.flowParseDeclare(bodyNode, true);
  1561. }
  1562. body.push(bodyNode);
  1563. }
  1564. this.scope.exit();
  1565. this.expect(types$1.braceR);
  1566. this.finishNode(bodyNode, "BlockStatement");
  1567. let kind = null;
  1568. let hasModuleExport = false;
  1569. body.forEach(bodyElement => {
  1570. if (isEsModuleType(bodyElement)) {
  1571. if (kind === "CommonJS") {
  1572. this.raise(bodyElement.start, FlowErrors.AmbiguousDeclareModuleKind);
  1573. }
  1574. kind = "ES";
  1575. } else if (bodyElement.type === "DeclareModuleExports") {
  1576. if (hasModuleExport) {
  1577. this.raise(bodyElement.start, FlowErrors.DuplicateDeclareModuleExports);
  1578. }
  1579. if (kind === "ES") {
  1580. this.raise(bodyElement.start, FlowErrors.AmbiguousDeclareModuleKind);
  1581. }
  1582. kind = "CommonJS";
  1583. hasModuleExport = true;
  1584. }
  1585. });
  1586. node.kind = kind || "CommonJS";
  1587. return this.finishNode(node, "DeclareModule");
  1588. }
  1589. flowParseDeclareExportDeclaration(node, insideModule) {
  1590. this.expect(types$1._export);
  1591. if (this.eat(types$1._default)) {
  1592. if (this.match(types$1._function) || this.match(types$1._class)) {
  1593. node.declaration = this.flowParseDeclare(this.startNode());
  1594. } else {
  1595. node.declaration = this.flowParseType();
  1596. this.semicolon();
  1597. }
  1598. node.default = true;
  1599. return this.finishNode(node, "DeclareExportDeclaration");
  1600. } else {
  1601. if (this.match(types$1._const) || this.isLet() || (this.isContextual("type") || this.isContextual("interface")) && !insideModule) {
  1602. const label = this.state.value;
  1603. const suggestion = exportSuggestions[label];
  1604. throw this.raise(this.state.start, FlowErrors.UnsupportedDeclareExportKind, label, suggestion);
  1605. }
  1606. if (this.match(types$1._var) || this.match(types$1._function) || this.match(types$1._class) || this.isContextual("opaque")) {
  1607. node.declaration = this.flowParseDeclare(this.startNode());
  1608. node.default = false;
  1609. return this.finishNode(node, "DeclareExportDeclaration");
  1610. } else if (this.match(types$1.star) || this.match(types$1.braceL) || this.isContextual("interface") || this.isContextual("type") || this.isContextual("opaque")) {
  1611. node = this.parseExport(node);
  1612. if (node.type === "ExportNamedDeclaration") {
  1613. node.type = "ExportDeclaration";
  1614. node.default = false;
  1615. delete node.exportKind;
  1616. }
  1617. node.type = "Declare" + node.type;
  1618. return node;
  1619. }
  1620. }
  1621. throw this.unexpected();
  1622. }
  1623. flowParseDeclareModuleExports(node) {
  1624. this.next();
  1625. this.expectContextual("exports");
  1626. node.typeAnnotation = this.flowParseTypeAnnotation();
  1627. this.semicolon();
  1628. return this.finishNode(node, "DeclareModuleExports");
  1629. }
  1630. flowParseDeclareTypeAlias(node) {
  1631. this.next();
  1632. this.flowParseTypeAlias(node);
  1633. node.type = "DeclareTypeAlias";
  1634. return node;
  1635. }
  1636. flowParseDeclareOpaqueType(node) {
  1637. this.next();
  1638. this.flowParseOpaqueType(node, true);
  1639. node.type = "DeclareOpaqueType";
  1640. return node;
  1641. }
  1642. flowParseDeclareInterface(node) {
  1643. this.next();
  1644. this.flowParseInterfaceish(node);
  1645. return this.finishNode(node, "DeclareInterface");
  1646. }
  1647. flowParseInterfaceish(node, isClass = false) {
  1648. node.id = this.flowParseRestrictedIdentifier(!isClass, true);
  1649. this.scope.declareName(node.id.name, isClass ? BIND_FUNCTION : BIND_LEXICAL, node.id.start);
  1650. if (this.isRelational("<")) {
  1651. node.typeParameters = this.flowParseTypeParameterDeclaration();
  1652. } else {
  1653. node.typeParameters = null;
  1654. }
  1655. node.extends = [];
  1656. node.implements = [];
  1657. node.mixins = [];
  1658. if (this.eat(types$1._extends)) {
  1659. do {
  1660. node.extends.push(this.flowParseInterfaceExtends());
  1661. } while (!isClass && this.eat(types$1.comma));
  1662. }
  1663. if (this.isContextual("mixins")) {
  1664. this.next();
  1665. do {
  1666. node.mixins.push(this.flowParseInterfaceExtends());
  1667. } while (this.eat(types$1.comma));
  1668. }
  1669. if (this.isContextual("implements")) {
  1670. this.next();
  1671. do {
  1672. node.implements.push(this.flowParseInterfaceExtends());
  1673. } while (this.eat(types$1.comma));
  1674. }
  1675. node.body = this.flowParseObjectType({
  1676. allowStatic: isClass,
  1677. allowExact: false,
  1678. allowSpread: false,
  1679. allowProto: isClass,
  1680. allowInexact: false
  1681. });
  1682. }
  1683. flowParseInterfaceExtends() {
  1684. const node = this.startNode();
  1685. node.id = this.flowParseQualifiedTypeIdentifier();
  1686. if (this.isRelational("<")) {
  1687. node.typeParameters = this.flowParseTypeParameterInstantiation();
  1688. } else {
  1689. node.typeParameters = null;
  1690. }
  1691. return this.finishNode(node, "InterfaceExtends");
  1692. }
  1693. flowParseInterface(node) {
  1694. this.flowParseInterfaceish(node);
  1695. return this.finishNode(node, "InterfaceDeclaration");
  1696. }
  1697. checkNotUnderscore(word) {
  1698. if (word === "_") {
  1699. this.raise(this.state.start, FlowErrors.UnexpectedReservedUnderscore);
  1700. }
  1701. }
  1702. checkReservedType(word, startLoc, declaration) {
  1703. if (!reservedTypes.has(word)) return;
  1704. this.raise(startLoc, declaration ? FlowErrors.AssignReservedType : FlowErrors.UnexpectedReservedType, word);
  1705. }
  1706. flowParseRestrictedIdentifier(liberal, declaration) {
  1707. this.checkReservedType(this.state.value, this.state.start, declaration);
  1708. return this.parseIdentifier(liberal);
  1709. }
  1710. flowParseTypeAlias(node) {
  1711. node.id = this.flowParseRestrictedIdentifier(false, true);
  1712. this.scope.declareName(node.id.name, BIND_LEXICAL, node.id.start);
  1713. if (this.isRelational("<")) {
  1714. node.typeParameters = this.flowParseTypeParameterDeclaration();
  1715. } else {
  1716. node.typeParameters = null;
  1717. }
  1718. node.right = this.flowParseTypeInitialiser(types$1.eq);
  1719. this.semicolon();
  1720. return this.finishNode(node, "TypeAlias");
  1721. }
  1722. flowParseOpaqueType(node, declare) {
  1723. this.expectContextual("type");
  1724. node.id = this.flowParseRestrictedIdentifier(true, true);
  1725. this.scope.declareName(node.id.name, BIND_LEXICAL, node.id.start);
  1726. if (this.isRelational("<")) {
  1727. node.typeParameters = this.flowParseTypeParameterDeclaration();
  1728. } else {
  1729. node.typeParameters = null;
  1730. }
  1731. node.supertype = null;
  1732. if (this.match(types$1.colon)) {
  1733. node.supertype = this.flowParseTypeInitialiser(types$1.colon);
  1734. }
  1735. node.impltype = null;
  1736. if (!declare) {
  1737. node.impltype = this.flowParseTypeInitialiser(types$1.eq);
  1738. }
  1739. this.semicolon();
  1740. return this.finishNode(node, "OpaqueType");
  1741. }
  1742. flowParseTypeParameter(requireDefault = false) {
  1743. const nodeStart = this.state.start;
  1744. const node = this.startNode();
  1745. const variance = this.flowParseVariance();
  1746. const ident = this.flowParseTypeAnnotatableIdentifier();
  1747. node.name = ident.name;
  1748. node.variance = variance;
  1749. node.bound = ident.typeAnnotation;
  1750. if (this.match(types$1.eq)) {
  1751. this.eat(types$1.eq);
  1752. node.default = this.flowParseType();
  1753. } else {
  1754. if (requireDefault) {
  1755. this.raise(nodeStart, FlowErrors.MissingTypeParamDefault);
  1756. }
  1757. }
  1758. return this.finishNode(node, "TypeParameter");
  1759. }
  1760. flowParseTypeParameterDeclaration() {
  1761. const oldInType = this.state.inType;
  1762. const node = this.startNode();
  1763. node.params = [];
  1764. this.state.inType = true;
  1765. if (this.isRelational("<") || this.match(types$1.jsxTagStart)) {
  1766. this.next();
  1767. } else {
  1768. this.unexpected();
  1769. }
  1770. let defaultRequired = false;
  1771. do {
  1772. const typeParameter = this.flowParseTypeParameter(defaultRequired);
  1773. node.params.push(typeParameter);
  1774. if (typeParameter.default) {
  1775. defaultRequired = true;
  1776. }
  1777. if (!this.isRelational(">")) {
  1778. this.expect(types$1.comma);
  1779. }
  1780. } while (!this.isRelational(">"));
  1781. this.expectRelational(">");
  1782. this.state.inType = oldInType;
  1783. return this.finishNode(node, "TypeParameterDeclaration");
  1784. }
  1785. flowParseTypeParameterInstantiation() {
  1786. const node = this.startNode();
  1787. const oldInType = this.state.inType;
  1788. node.params = [];
  1789. this.state.inType = true;
  1790. this.expectRelational("<");
  1791. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  1792. this.state.noAnonFunctionType = false;
  1793. while (!this.isRelational(">")) {
  1794. node.params.push(this.flowParseType());
  1795. if (!this.isRelational(">")) {
  1796. this.expect(types$1.comma);
  1797. }
  1798. }
  1799. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  1800. this.expectRelational(">");
  1801. this.state.inType = oldInType;
  1802. return this.finishNode(node, "TypeParameterInstantiation");
  1803. }
  1804. flowParseTypeParameterInstantiationCallOrNew() {
  1805. const node = this.startNode();
  1806. const oldInType = this.state.inType;
  1807. node.params = [];
  1808. this.state.inType = true;
  1809. this.expectRelational("<");
  1810. while (!this.isRelational(">")) {
  1811. node.params.push(this.flowParseTypeOrImplicitInstantiation());
  1812. if (!this.isRelational(">")) {
  1813. this.expect(types$1.comma);
  1814. }
  1815. }
  1816. this.expectRelational(">");
  1817. this.state.inType = oldInType;
  1818. return this.finishNode(node, "TypeParameterInstantiation");
  1819. }
  1820. flowParseInterfaceType() {
  1821. const node = this.startNode();
  1822. this.expectContextual("interface");
  1823. node.extends = [];
  1824. if (this.eat(types$1._extends)) {
  1825. do {
  1826. node.extends.push(this.flowParseInterfaceExtends());
  1827. } while (this.eat(types$1.comma));
  1828. }
  1829. node.body = this.flowParseObjectType({
  1830. allowStatic: false,
  1831. allowExact: false,
  1832. allowSpread: false,
  1833. allowProto: false,
  1834. allowInexact: false
  1835. });
  1836. return this.finishNode(node, "InterfaceTypeAnnotation");
  1837. }
  1838. flowParseObjectPropertyKey() {
  1839. return this.match(types$1.num) || this.match(types$1.string) ? this.parseExprAtom() : this.parseIdentifier(true);
  1840. }
  1841. flowParseObjectTypeIndexer(node, isStatic, variance) {
  1842. node.static = isStatic;
  1843. if (this.lookahead().type === types$1.colon) {
  1844. node.id = this.flowParseObjectPropertyKey();
  1845. node.key = this.flowParseTypeInitialiser();
  1846. } else {
  1847. node.id = null;
  1848. node.key = this.flowParseType();
  1849. }
  1850. this.expect(types$1.bracketR);
  1851. node.value = this.flowParseTypeInitialiser();
  1852. node.variance = variance;
  1853. return this.finishNode(node, "ObjectTypeIndexer");
  1854. }
  1855. flowParseObjectTypeInternalSlot(node, isStatic) {
  1856. node.static = isStatic;
  1857. node.id = this.flowParseObjectPropertyKey();
  1858. this.expect(types$1.bracketR);
  1859. this.expect(types$1.bracketR);
  1860. if (this.isRelational("<") || this.match(types$1.parenL)) {
  1861. node.method = true;
  1862. node.optional = false;
  1863. node.value = this.flowParseObjectTypeMethodish(this.startNodeAt(node.start, node.loc.start));
  1864. } else {
  1865. node.method = false;
  1866. if (this.eat(types$1.question)) {
  1867. node.optional = true;
  1868. }
  1869. node.value = this.flowParseTypeInitialiser();
  1870. }
  1871. return this.finishNode(node, "ObjectTypeInternalSlot");
  1872. }
  1873. flowParseObjectTypeMethodish(node) {
  1874. node.params = [];
  1875. node.rest = null;
  1876. node.typeParameters = null;
  1877. node.this = null;
  1878. if (this.isRelational("<")) {
  1879. node.typeParameters = this.flowParseTypeParameterDeclaration();
  1880. }
  1881. this.expect(types$1.parenL);
  1882. if (this.match(types$1._this)) {
  1883. node.this = this.flowParseFunctionTypeParam(true);
  1884. node.this.name = null;
  1885. if (!this.match(types$1.parenR)) {
  1886. this.expect(types$1.comma);
  1887. }
  1888. }
  1889. while (!this.match(types$1.parenR) && !this.match(types$1.ellipsis)) {
  1890. node.params.push(this.flowParseFunctionTypeParam(false));
  1891. if (!this.match(types$1.parenR)) {
  1892. this.expect(types$1.comma);
  1893. }
  1894. }
  1895. if (this.eat(types$1.ellipsis)) {
  1896. node.rest = this.flowParseFunctionTypeParam(false);
  1897. }
  1898. this.expect(types$1.parenR);
  1899. node.returnType = this.flowParseTypeInitialiser();
  1900. return this.finishNode(node, "FunctionTypeAnnotation");
  1901. }
  1902. flowParseObjectTypeCallProperty(node, isStatic) {
  1903. const valueNode = this.startNode();
  1904. node.static = isStatic;
  1905. node.value = this.flowParseObjectTypeMethodish(valueNode);
  1906. return this.finishNode(node, "ObjectTypeCallProperty");
  1907. }
  1908. flowParseObjectType({
  1909. allowStatic,
  1910. allowExact,
  1911. allowSpread,
  1912. allowProto,
  1913. allowInexact
  1914. }) {
  1915. const oldInType = this.state.inType;
  1916. this.state.inType = true;
  1917. const nodeStart = this.startNode();
  1918. nodeStart.callProperties = [];
  1919. nodeStart.properties = [];
  1920. nodeStart.indexers = [];
  1921. nodeStart.internalSlots = [];
  1922. let endDelim;
  1923. let exact;
  1924. let inexact = false;
  1925. if (allowExact && this.match(types$1.braceBarL)) {
  1926. this.expect(types$1.braceBarL);
  1927. endDelim = types$1.braceBarR;
  1928. exact = true;
  1929. } else {
  1930. this.expect(types$1.braceL);
  1931. endDelim = types$1.braceR;
  1932. exact = false;
  1933. }
  1934. nodeStart.exact = exact;
  1935. while (!this.match(endDelim)) {
  1936. let isStatic = false;
  1937. let protoStart = null;
  1938. let inexactStart = null;
  1939. const node = this.startNode();
  1940. if (allowProto && this.isContextual("proto")) {
  1941. const lookahead = this.lookahead();
  1942. if (lookahead.type !== types$1.colon && lookahead.type !== types$1.question) {
  1943. this.next();
  1944. protoStart = this.state.start;
  1945. allowStatic = false;
  1946. }
  1947. }
  1948. if (allowStatic && this.isContextual("static")) {
  1949. const lookahead = this.lookahead();
  1950. if (lookahead.type !== types$1.colon && lookahead.type !== types$1.question) {
  1951. this.next();
  1952. isStatic = true;
  1953. }
  1954. }
  1955. const variance = this.flowParseVariance();
  1956. if (this.eat(types$1.bracketL)) {
  1957. if (protoStart != null) {
  1958. this.unexpected(protoStart);
  1959. }
  1960. if (this.eat(types$1.bracketL)) {
  1961. if (variance) {
  1962. this.unexpected(variance.start);
  1963. }
  1964. nodeStart.internalSlots.push(this.flowParseObjectTypeInternalSlot(node, isStatic));
  1965. } else {
  1966. nodeStart.indexers.push(this.flowParseObjectTypeIndexer(node, isStatic, variance));
  1967. }
  1968. } else if (this.match(types$1.parenL) || this.isRelational("<")) {
  1969. if (protoStart != null) {
  1970. this.unexpected(protoStart);
  1971. }
  1972. if (variance) {
  1973. this.unexpected(variance.start);
  1974. }
  1975. nodeStart.callProperties.push(this.flowParseObjectTypeCallProperty(node, isStatic));
  1976. } else {
  1977. let kind = "init";
  1978. if (this.isContextual("get") || this.isContextual("set")) {
  1979. const lookahead = this.lookahead();
  1980. if (lookahead.type === types$1.name || lookahead.type === types$1.string || lookahead.type === types$1.num) {
  1981. kind = this.state.value;
  1982. this.next();
  1983. }
  1984. }
  1985. const propOrInexact = this.flowParseObjectTypeProperty(node, isStatic, protoStart, variance, kind, allowSpread, allowInexact != null ? allowInexact : !exact);
  1986. if (propOrInexact === null) {
  1987. inexact = true;
  1988. inexactStart = this.state.lastTokStart;
  1989. } else {
  1990. nodeStart.properties.push(propOrInexact);
  1991. }
  1992. }
  1993. this.flowObjectTypeSemicolon();
  1994. if (inexactStart && !this.match(types$1.braceR) && !this.match(types$1.braceBarR)) {
  1995. this.raise(inexactStart, FlowErrors.UnexpectedExplicitInexactInObject);
  1996. }
  1997. }
  1998. this.expect(endDelim);
  1999. if (allowSpread) {
  2000. nodeStart.inexact = inexact;
  2001. }
  2002. const out = this.finishNode(nodeStart, "ObjectTypeAnnotation");
  2003. this.state.inType = oldInType;
  2004. return out;
  2005. }
  2006. flowParseObjectTypeProperty(node, isStatic, protoStart, variance, kind, allowSpread, allowInexact) {
  2007. if (this.eat(types$1.ellipsis)) {
  2008. const isInexactToken = this.match(types$1.comma) || this.match(types$1.semi) || this.match(types$1.braceR) || this.match(types$1.braceBarR);
  2009. if (isInexactToken) {
  2010. if (!allowSpread) {
  2011. this.raise(this.state.lastTokStart, FlowErrors.InexactInsideNonObject);
  2012. } else if (!allowInexact) {
  2013. this.raise(this.state.lastTokStart, FlowErrors.InexactInsideExact);
  2014. }
  2015. if (variance) {
  2016. this.raise(variance.start, FlowErrors.InexactVariance);
  2017. }
  2018. return null;
  2019. }
  2020. if (!allowSpread) {
  2021. this.raise(this.state.lastTokStart, FlowErrors.UnexpectedSpreadType);
  2022. }
  2023. if (protoStart != null) {
  2024. this.unexpected(protoStart);
  2025. }
  2026. if (variance) {
  2027. this.raise(variance.start, FlowErrors.SpreadVariance);
  2028. }
  2029. node.argument = this.flowParseType();
  2030. return this.finishNode(node, "ObjectTypeSpreadProperty");
  2031. } else {
  2032. node.key = this.flowParseObjectPropertyKey();
  2033. node.static = isStatic;
  2034. node.proto = protoStart != null;
  2035. node.kind = kind;
  2036. let optional = false;
  2037. if (this.isRelational("<") || this.match(types$1.parenL)) {
  2038. node.method = true;
  2039. if (protoStart != null) {
  2040. this.unexpected(protoStart);
  2041. }
  2042. if (variance) {
  2043. this.unexpected(variance.start);
  2044. }
  2045. node.value = this.flowParseObjectTypeMethodish(this.startNodeAt(node.start, node.loc.start));
  2046. if (kind === "get" || kind === "set") {
  2047. this.flowCheckGetterSetterParams(node);
  2048. }
  2049. if (!allowSpread && node.key.name === "constructor" && node.value.this) {
  2050. this.raise(node.value.this.start, FlowErrors.ThisParamBannedInConstructor);
  2051. }
  2052. } else {
  2053. if (kind !== "init") this.unexpected();
  2054. node.method = false;
  2055. if (this.eat(types$1.question)) {
  2056. optional = true;
  2057. }
  2058. node.value = this.flowParseTypeInitialiser();
  2059. node.variance = variance;
  2060. }
  2061. node.optional = optional;
  2062. return this.finishNode(node, "ObjectTypeProperty");
  2063. }
  2064. }
  2065. flowCheckGetterSetterParams(property) {
  2066. const paramCount = property.kind === "get" ? 0 : 1;
  2067. const start = property.start;
  2068. const length = property.value.params.length + (property.value.rest ? 1 : 0);
  2069. if (property.value.this) {
  2070. this.raise(property.value.this.start, property.kind === "get" ? FlowErrors.GetterMayNotHaveThisParam : FlowErrors.SetterMayNotHaveThisParam);
  2071. }
  2072. if (length !== paramCount) {
  2073. if (property.kind === "get") {
  2074. this.raise(start, ErrorMessages.BadGetterArity);
  2075. } else {
  2076. this.raise(start, ErrorMessages.BadSetterArity);
  2077. }
  2078. }
  2079. if (property.kind === "set" && property.value.rest) {
  2080. this.raise(start, ErrorMessages.BadSetterRestParameter);
  2081. }
  2082. }
  2083. flowObjectTypeSemicolon() {
  2084. if (!this.eat(types$1.semi) && !this.eat(types$1.comma) && !this.match(types$1.braceR) && !this.match(types$1.braceBarR)) {
  2085. this.unexpected();
  2086. }
  2087. }
  2088. flowParseQualifiedTypeIdentifier(startPos, startLoc, id) {
  2089. startPos = startPos || this.state.start;
  2090. startLoc = startLoc || this.state.startLoc;
  2091. let node = id || this.flowParseRestrictedIdentifier(true);
  2092. while (this.eat(types$1.dot)) {
  2093. const node2 = this.startNodeAt(startPos, startLoc);
  2094. node2.qualification = node;
  2095. node2.id = this.flowParseRestrictedIdentifier(true);
  2096. node = this.finishNode(node2, "QualifiedTypeIdentifier");
  2097. }
  2098. return node;
  2099. }
  2100. flowParseGenericType(startPos, startLoc, id) {
  2101. const node = this.startNodeAt(startPos, startLoc);
  2102. node.typeParameters = null;
  2103. node.id = this.flowParseQualifiedTypeIdentifier(startPos, startLoc, id);
  2104. if (this.isRelational("<")) {
  2105. node.typeParameters = this.flowParseTypeParameterInstantiation();
  2106. }
  2107. return this.finishNode(node, "GenericTypeAnnotation");
  2108. }
  2109. flowParseTypeofType() {
  2110. const node = this.startNode();
  2111. this.expect(types$1._typeof);
  2112. node.argument = this.flowParsePrimaryType();
  2113. return this.finishNode(node, "TypeofTypeAnnotation");
  2114. }
  2115. flowParseTupleType() {
  2116. const node = this.startNode();
  2117. node.types = [];
  2118. this.expect(types$1.bracketL);
  2119. while (this.state.pos < this.length && !this.match(types$1.bracketR)) {
  2120. node.types.push(this.flowParseType());
  2121. if (this.match(types$1.bracketR)) break;
  2122. this.expect(types$1.comma);
  2123. }
  2124. this.expect(types$1.bracketR);
  2125. return this.finishNode(node, "TupleTypeAnnotation");
  2126. }
  2127. flowParseFunctionTypeParam(first) {
  2128. let name = null;
  2129. let optional = false;
  2130. let typeAnnotation = null;
  2131. const node = this.startNode();
  2132. const lh = this.lookahead();
  2133. const isThis = this.state.type === types$1._this;
  2134. if (lh.type === types$1.colon || lh.type === types$1.question) {
  2135. if (isThis && !first) {
  2136. this.raise(node.start, FlowErrors.ThisParamMustBeFirst);
  2137. }
  2138. name = this.parseIdentifier(isThis);
  2139. if (this.eat(types$1.question)) {
  2140. optional = true;
  2141. if (isThis) {
  2142. this.raise(node.start, FlowErrors.ThisParamMayNotBeOptional);
  2143. }
  2144. }
  2145. typeAnnotation = this.flowParseTypeInitialiser();
  2146. } else {
  2147. typeAnnotation = this.flowParseType();
  2148. }
  2149. node.name = name;
  2150. node.optional = optional;
  2151. node.typeAnnotation = typeAnnotation;
  2152. return this.finishNode(node, "FunctionTypeParam");
  2153. }
  2154. reinterpretTypeAsFunctionTypeParam(type) {
  2155. const node = this.startNodeAt(type.start, type.loc.start);
  2156. node.name = null;
  2157. node.optional = false;
  2158. node.typeAnnotation = type;
  2159. return this.finishNode(node, "FunctionTypeParam");
  2160. }
  2161. flowParseFunctionTypeParams(params = []) {
  2162. let rest = null;
  2163. let _this = null;
  2164. if (this.match(types$1._this)) {
  2165. _this = this.flowParseFunctionTypeParam(true);
  2166. _this.name = null;
  2167. if (!this.match(types$1.parenR)) {
  2168. this.expect(types$1.comma);
  2169. }
  2170. }
  2171. while (!this.match(types$1.parenR) && !this.match(types$1.ellipsis)) {
  2172. params.push(this.flowParseFunctionTypeParam(false));
  2173. if (!this.match(types$1.parenR)) {
  2174. this.expect(types$1.comma);
  2175. }
  2176. }
  2177. if (this.eat(types$1.ellipsis)) {
  2178. rest = this.flowParseFunctionTypeParam(false);
  2179. }
  2180. return {
  2181. params,
  2182. rest,
  2183. _this
  2184. };
  2185. }
  2186. flowIdentToTypeAnnotation(startPos, startLoc, node, id) {
  2187. switch (id.name) {
  2188. case "any":
  2189. return this.finishNode(node, "AnyTypeAnnotation");
  2190. case "bool":
  2191. case "boolean":
  2192. return this.finishNode(node, "BooleanTypeAnnotation");
  2193. case "mixed":
  2194. return this.finishNode(node, "MixedTypeAnnotation");
  2195. case "empty":
  2196. return this.finishNode(node, "EmptyTypeAnnotation");
  2197. case "number":
  2198. return this.finishNode(node, "NumberTypeAnnotation");
  2199. case "string":
  2200. return this.finishNode(node, "StringTypeAnnotation");
  2201. case "symbol":
  2202. return this.finishNode(node, "SymbolTypeAnnotation");
  2203. default:
  2204. this.checkNotUnderscore(id.name);
  2205. return this.flowParseGenericType(startPos, startLoc, id);
  2206. }
  2207. }
  2208. flowParsePrimaryType() {
  2209. const startPos = this.state.start;
  2210. const startLoc = this.state.startLoc;
  2211. const node = this.startNode();
  2212. let tmp;
  2213. let type;
  2214. let isGroupedType = false;
  2215. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  2216. switch (this.state.type) {
  2217. case types$1.name:
  2218. if (this.isContextual("interface")) {
  2219. return this.flowParseInterfaceType();
  2220. }
  2221. return this.flowIdentToTypeAnnotation(startPos, startLoc, node, this.parseIdentifier());
  2222. case types$1.braceL:
  2223. return this.flowParseObjectType({
  2224. allowStatic: false,
  2225. allowExact: false,
  2226. allowSpread: true,
  2227. allowProto: false,
  2228. allowInexact: true
  2229. });
  2230. case types$1.braceBarL:
  2231. return this.flowParseObjectType({
  2232. allowStatic: false,
  2233. allowExact: true,
  2234. allowSpread: true,
  2235. allowProto: false,
  2236. allowInexact: false
  2237. });
  2238. case types$1.bracketL:
  2239. this.state.noAnonFunctionType = false;
  2240. type = this.flowParseTupleType();
  2241. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  2242. return type;
  2243. case types$1.relational:
  2244. if (this.state.value === "<") {
  2245. node.typeParameters = this.flowParseTypeParameterDeclaration();
  2246. this.expect(types$1.parenL);
  2247. tmp = this.flowParseFunctionTypeParams();
  2248. node.params = tmp.params;
  2249. node.rest = tmp.rest;
  2250. node.this = tmp._this;
  2251. this.expect(types$1.parenR);
  2252. this.expect(types$1.arrow);
  2253. node.returnType = this.flowParseType();
  2254. return this.finishNode(node, "FunctionTypeAnnotation");
  2255. }
  2256. break;
  2257. case types$1.parenL:
  2258. this.next();
  2259. if (!this.match(types$1.parenR) && !this.match(types$1.ellipsis)) {
  2260. if (this.match(types$1.name) || this.match(types$1._this)) {
  2261. const token = this.lookahead().type;
  2262. isGroupedType = token !== types$1.question && token !== types$1.colon;
  2263. } else {
  2264. isGroupedType = true;
  2265. }
  2266. }
  2267. if (isGroupedType) {
  2268. this.state.noAnonFunctionType = false;
  2269. type = this.flowParseType();
  2270. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  2271. if (this.state.noAnonFunctionType || !(this.match(types$1.comma) || this.match(types$1.parenR) && this.lookahead().type === types$1.arrow)) {
  2272. this.expect(types$1.parenR);
  2273. return type;
  2274. } else {
  2275. this.eat(types$1.comma);
  2276. }
  2277. }
  2278. if (type) {
  2279. tmp = this.flowParseFunctionTypeParams([this.reinterpretTypeAsFunctionTypeParam(type)]);
  2280. } else {
  2281. tmp = this.flowParseFunctionTypeParams();
  2282. }
  2283. node.params = tmp.params;
  2284. node.rest = tmp.rest;
  2285. node.this = tmp._this;
  2286. this.expect(types$1.parenR);
  2287. this.expect(types$1.arrow);
  2288. node.returnType = this.flowParseType();
  2289. node.typeParameters = null;
  2290. return this.finishNode(node, "FunctionTypeAnnotation");
  2291. case types$1.string:
  2292. return this.parseLiteral(this.state.value, "StringLiteralTypeAnnotation");
  2293. case types$1._true:
  2294. case types$1._false:
  2295. node.value = this.match(types$1._true);
  2296. this.next();
  2297. return this.finishNode(node, "BooleanLiteralTypeAnnotation");
  2298. case types$1.plusMin:
  2299. if (this.state.value === "-") {
  2300. this.next();
  2301. if (this.match(types$1.num)) {
  2302. return this.parseLiteralAtNode(-this.state.value, "NumberLiteralTypeAnnotation", node);
  2303. }
  2304. if (this.match(types$1.bigint)) {
  2305. return this.parseLiteralAtNode(-this.state.value, "BigIntLiteralTypeAnnotation", node);
  2306. }
  2307. throw this.raise(this.state.start, FlowErrors.UnexpectedSubtractionOperand);
  2308. }
  2309. throw this.unexpected();
  2310. case types$1.num:
  2311. return this.parseLiteral(this.state.value, "NumberLiteralTypeAnnotation");
  2312. case types$1.bigint:
  2313. return this.parseLiteral(this.state.value, "BigIntLiteralTypeAnnotation");
  2314. case types$1._void:
  2315. this.next();
  2316. return this.finishNode(node, "VoidTypeAnnotation");
  2317. case types$1._null:
  2318. this.next();
  2319. return this.finishNode(node, "NullLiteralTypeAnnotation");
  2320. case types$1._this:
  2321. this.next();
  2322. return this.finishNode(node, "ThisTypeAnnotation");
  2323. case types$1.star:
  2324. this.next();
  2325. return this.finishNode(node, "ExistsTypeAnnotation");
  2326. default:
  2327. if (this.state.type.keyword === "typeof") {
  2328. return this.flowParseTypeofType();
  2329. } else if (this.state.type.keyword) {
  2330. const label = this.state.type.label;
  2331. this.next();
  2332. return super.createIdentifier(node, label);
  2333. }
  2334. }
  2335. throw this.unexpected();
  2336. }
  2337. flowParsePostfixType() {
  2338. const startPos = this.state.start;
  2339. const startLoc = this.state.startLoc;
  2340. let type = this.flowParsePrimaryType();
  2341. let seenOptionalIndexedAccess = false;
  2342. while ((this.match(types$1.bracketL) || this.match(types$1.questionDot)) && !this.canInsertSemicolon()) {
  2343. const node = this.startNodeAt(startPos, startLoc);
  2344. const optional = this.eat(types$1.questionDot);
  2345. seenOptionalIndexedAccess = seenOptionalIndexedAccess || optional;
  2346. this.expect(types$1.bracketL);
  2347. if (!optional && this.match(types$1.bracketR)) {
  2348. node.elementType = type;
  2349. this.next();
  2350. type = this.finishNode(node, "ArrayTypeAnnotation");
  2351. } else {
  2352. node.objectType = type;
  2353. node.indexType = this.flowParseType();
  2354. this.expect(types$1.bracketR);
  2355. if (seenOptionalIndexedAccess) {
  2356. node.optional = optional;
  2357. type = this.finishNode(node, "OptionalIndexedAccessType");
  2358. } else {
  2359. type = this.finishNode(node, "IndexedAccessType");
  2360. }
  2361. }
  2362. }
  2363. return type;
  2364. }
  2365. flowParsePrefixType() {
  2366. const node = this.startNode();
  2367. if (this.eat(types$1.question)) {
  2368. node.typeAnnotation = this.flowParsePrefixType();
  2369. return this.finishNode(node, "NullableTypeAnnotation");
  2370. } else {
  2371. return this.flowParsePostfixType();
  2372. }
  2373. }
  2374. flowParseAnonFunctionWithoutParens() {
  2375. const param = this.flowParsePrefixType();
  2376. if (!this.state.noAnonFunctionType && this.eat(types$1.arrow)) {
  2377. const node = this.startNodeAt(param.start, param.loc.start);
  2378. node.params = [this.reinterpretTypeAsFunctionTypeParam(param)];
  2379. node.rest = null;
  2380. node.this = null;
  2381. node.returnType = this.flowParseType();
  2382. node.typeParameters = null;
  2383. return this.finishNode(node, "FunctionTypeAnnotation");
  2384. }
  2385. return param;
  2386. }
  2387. flowParseIntersectionType() {
  2388. const node = this.startNode();
  2389. this.eat(types$1.bitwiseAND);
  2390. const type = this.flowParseAnonFunctionWithoutParens();
  2391. node.types = [type];
  2392. while (this.eat(types$1.bitwiseAND)) {
  2393. node.types.push(this.flowParseAnonFunctionWithoutParens());
  2394. }
  2395. return node.types.length === 1 ? type : this.finishNode(node, "IntersectionTypeAnnotation");
  2396. }
  2397. flowParseUnionType() {
  2398. const node = this.startNode();
  2399. this.eat(types$1.bitwiseOR);
  2400. const type = this.flowParseIntersectionType();
  2401. node.types = [type];
  2402. while (this.eat(types$1.bitwiseOR)) {
  2403. node.types.push(this.flowParseIntersectionType());
  2404. }
  2405. return node.types.length === 1 ? type : this.finishNode(node, "UnionTypeAnnotation");
  2406. }
  2407. flowParseType() {
  2408. const oldInType = this.state.inType;
  2409. this.state.inType = true;
  2410. const type = this.flowParseUnionType();
  2411. this.state.inType = oldInType;
  2412. return type;
  2413. }
  2414. flowParseTypeOrImplicitInstantiation() {
  2415. if (this.state.type === types$1.name && this.state.value === "_") {
  2416. const startPos = this.state.start;
  2417. const startLoc = this.state.startLoc;
  2418. const node = this.parseIdentifier();
  2419. return this.flowParseGenericType(startPos, startLoc, node);
  2420. } else {
  2421. return this.flowParseType();
  2422. }
  2423. }
  2424. flowParseTypeAnnotation() {
  2425. const node = this.startNode();
  2426. node.typeAnnotation = this.flowParseTypeInitialiser();
  2427. return this.finishNode(node, "TypeAnnotation");
  2428. }
  2429. flowParseTypeAnnotatableIdentifier(allowPrimitiveOverride) {
  2430. const ident = allowPrimitiveOverride ? this.parseIdentifier() : this.flowParseRestrictedIdentifier();
  2431. if (this.match(types$1.colon)) {
  2432. ident.typeAnnotation = this.flowParseTypeAnnotation();
  2433. this.resetEndLocation(ident);
  2434. }
  2435. return ident;
  2436. }
  2437. typeCastToParameter(node) {
  2438. node.expression.typeAnnotation = node.typeAnnotation;
  2439. this.resetEndLocation(node.expression, node.typeAnnotation.end, node.typeAnnotation.loc.end);
  2440. return node.expression;
  2441. }
  2442. flowParseVariance() {
  2443. let variance = null;
  2444. if (this.match(types$1.plusMin)) {
  2445. variance = this.startNode();
  2446. if (this.state.value === "+") {
  2447. variance.kind = "plus";
  2448. } else {
  2449. variance.kind = "minus";
  2450. }
  2451. this.next();
  2452. this.finishNode(variance, "Variance");
  2453. }
  2454. return variance;
  2455. }
  2456. parseFunctionBody(node, allowExpressionBody, isMethod = false) {
  2457. if (allowExpressionBody) {
  2458. return this.forwardNoArrowParamsConversionAt(node, () => super.parseFunctionBody(node, true, isMethod));
  2459. }
  2460. return super.parseFunctionBody(node, false, isMethod);
  2461. }
  2462. parseFunctionBodyAndFinish(node, type, isMethod = false) {
  2463. if (this.match(types$1.colon)) {
  2464. const typeNode = this.startNode();
  2465. [typeNode.typeAnnotation, node.predicate] = this.flowParseTypeAndPredicateInitialiser();
  2466. node.returnType = typeNode.typeAnnotation ? this.finishNode(typeNode, "TypeAnnotation") : null;
  2467. }
  2468. super.parseFunctionBodyAndFinish(node, type, isMethod);
  2469. }
  2470. parseStatement(context, topLevel) {
  2471. if (this.state.strict && this.match(types$1.name) && this.state.value === "interface") {
  2472. const lookahead = this.lookahead();
  2473. if (lookahead.type === types$1.name || isKeyword(lookahead.value)) {
  2474. const node = this.startNode();
  2475. this.next();
  2476. return this.flowParseInterface(node);
  2477. }
  2478. } else if (this.shouldParseEnums() && this.isContextual("enum")) {
  2479. const node = this.startNode();
  2480. this.next();
  2481. return this.flowParseEnumDeclaration(node);
  2482. }
  2483. const stmt = super.parseStatement(context, topLevel);
  2484. if (this.flowPragma === undefined && !this.isValidDirective(stmt)) {
  2485. this.flowPragma = null;
  2486. }
  2487. return stmt;
  2488. }
  2489. parseExpressionStatement(node, expr) {
  2490. if (expr.type === "Identifier") {
  2491. if (expr.name === "declare") {
  2492. if (this.match(types$1._class) || this.match(types$1.name) || this.match(types$1._function) || this.match(types$1._var) || this.match(types$1._export)) {
  2493. return this.flowParseDeclare(node);
  2494. }
  2495. } else if (this.match(types$1.name)) {
  2496. if (expr.name === "interface") {
  2497. return this.flowParseInterface(node);
  2498. } else if (expr.name === "type") {
  2499. return this.flowParseTypeAlias(node);
  2500. } else if (expr.name === "opaque") {
  2501. return this.flowParseOpaqueType(node, false);
  2502. }
  2503. }
  2504. }
  2505. return super.parseExpressionStatement(node, expr);
  2506. }
  2507. shouldParseExportDeclaration() {
  2508. return this.isContextual("type") || this.isContextual("interface") || this.isContextual("opaque") || this.shouldParseEnums() && this.isContextual("enum") || super.shouldParseExportDeclaration();
  2509. }
  2510. isExportDefaultSpecifier() {
  2511. if (this.match(types$1.name) && (this.state.value === "type" || this.state.value === "interface" || this.state.value === "opaque" || this.shouldParseEnums() && this.state.value === "enum")) {
  2512. return false;
  2513. }
  2514. return super.isExportDefaultSpecifier();
  2515. }
  2516. parseExportDefaultExpression() {
  2517. if (this.shouldParseEnums() && this.isContextual("enum")) {
  2518. const node = this.startNode();
  2519. this.next();
  2520. return this.flowParseEnumDeclaration(node);
  2521. }
  2522. return super.parseExportDefaultExpression();
  2523. }
  2524. parseConditional(expr, startPos, startLoc, refExpressionErrors) {
  2525. if (!this.match(types$1.question)) return expr;
  2526. if (this.state.maybeInArrowParameters) {
  2527. const result = this.tryParse(() => super.parseConditional(expr, startPos, startLoc));
  2528. if (!result.node) {
  2529. if (result.error) {
  2530. super.setOptionalParametersError(refExpressionErrors, result.error);
  2531. }
  2532. return expr;
  2533. }
  2534. if (result.error) this.state = result.failState;
  2535. return result.node;
  2536. }
  2537. this.expect(types$1.question);
  2538. const state = this.state.clone();
  2539. const originalNoArrowAt = this.state.noArrowAt;
  2540. const node = this.startNodeAt(startPos, startLoc);
  2541. let {
  2542. consequent,
  2543. failed
  2544. } = this.tryParseConditionalConsequent();
  2545. let [valid, invalid] = this.getArrowLikeExpressions(consequent);
  2546. if (failed || invalid.length > 0) {
  2547. const noArrowAt = [...originalNoArrowAt];
  2548. if (invalid.length > 0) {
  2549. this.state = state;
  2550. this.state.noArrowAt = noArrowAt;
  2551. for (let i = 0; i < invalid.length; i++) {
  2552. noArrowAt.push(invalid[i].start);
  2553. }
  2554. ({
  2555. consequent,
  2556. failed
  2557. } = this.tryParseConditionalConsequent());
  2558. [valid, invalid] = this.getArrowLikeExpressions(consequent);
  2559. }
  2560. if (failed && valid.length > 1) {
  2561. this.raise(state.start, FlowErrors.AmbiguousConditionalArrow);
  2562. }
  2563. if (failed && valid.length === 1) {
  2564. this.state = state;
  2565. this.state.noArrowAt = noArrowAt.concat(valid[0].start);
  2566. ({
  2567. consequent,
  2568. failed
  2569. } = this.tryParseConditionalConsequent());
  2570. }
  2571. }
  2572. this.getArrowLikeExpressions(consequent, true);
  2573. this.state.noArrowAt = originalNoArrowAt;
  2574. this.expect(types$1.colon);
  2575. node.test = expr;
  2576. node.consequent = consequent;
  2577. node.alternate = this.forwardNoArrowParamsConversionAt(node, () => this.parseMaybeAssign(undefined, undefined));
  2578. return this.finishNode(node, "ConditionalExpression");
  2579. }
  2580. tryParseConditionalConsequent() {
  2581. this.state.noArrowParamsConversionAt.push(this.state.start);
  2582. const consequent = this.parseMaybeAssignAllowIn();
  2583. const failed = !this.match(types$1.colon);
  2584. this.state.noArrowParamsConversionAt.pop();
  2585. return {
  2586. consequent,
  2587. failed
  2588. };
  2589. }
  2590. getArrowLikeExpressions(node, disallowInvalid) {
  2591. const stack = [node];
  2592. const arrows = [];
  2593. while (stack.length !== 0) {
  2594. const node = stack.pop();
  2595. if (node.type === "ArrowFunctionExpression") {
  2596. if (node.typeParameters || !node.returnType) {
  2597. this.finishArrowValidation(node);
  2598. } else {
  2599. arrows.push(node);
  2600. }
  2601. stack.push(node.body);
  2602. } else if (node.type === "ConditionalExpression") {
  2603. stack.push(node.consequent);
  2604. stack.push(node.alternate);
  2605. }
  2606. }
  2607. if (disallowInvalid) {
  2608. arrows.forEach(node => this.finishArrowValidation(node));
  2609. return [arrows, []];
  2610. }
  2611. return partition(arrows, node => node.params.every(param => this.isAssignable(param, true)));
  2612. }
  2613. finishArrowValidation(node) {
  2614. var _node$extra;
  2615. this.toAssignableList(node.params, (_node$extra = node.extra) == null ? void 0 : _node$extra.trailingComma, false);
  2616. this.scope.enter(SCOPE_FUNCTION | SCOPE_ARROW);
  2617. super.checkParams(node, false, true);
  2618. this.scope.exit();
  2619. }
  2620. forwardNoArrowParamsConversionAt(node, parse) {
  2621. let result;
  2622. if (this.state.noArrowParamsConversionAt.indexOf(node.start) !== -1) {
  2623. this.state.noArrowParamsConversionAt.push(this.state.start);
  2624. result = parse();
  2625. this.state.noArrowParamsConversionAt.pop();
  2626. } else {
  2627. result = parse();
  2628. }
  2629. return result;
  2630. }
  2631. parseParenItem(node, startPos, startLoc) {
  2632. node = super.parseParenItem(node, startPos, startLoc);
  2633. if (this.eat(types$1.question)) {
  2634. node.optional = true;
  2635. this.resetEndLocation(node);
  2636. }
  2637. if (this.match(types$1.colon)) {
  2638. const typeCastNode = this.startNodeAt(startPos, startLoc);
  2639. typeCastNode.expression = node;
  2640. typeCastNode.typeAnnotation = this.flowParseTypeAnnotation();
  2641. return this.finishNode(typeCastNode, "TypeCastExpression");
  2642. }
  2643. return node;
  2644. }
  2645. assertModuleNodeAllowed(node) {
  2646. if (node.type === "ImportDeclaration" && (node.importKind === "type" || node.importKind === "typeof") || node.type === "ExportNamedDeclaration" && node.exportKind === "type" || node.type === "ExportAllDeclaration" && node.exportKind === "type") {
  2647. return;
  2648. }
  2649. super.assertModuleNodeAllowed(node);
  2650. }
  2651. parseExport(node) {
  2652. const decl = super.parseExport(node);
  2653. if (decl.type === "ExportNamedDeclaration" || decl.type === "ExportAllDeclaration") {
  2654. decl.exportKind = decl.exportKind || "value";
  2655. }
  2656. return decl;
  2657. }
  2658. parseExportDeclaration(node) {
  2659. if (this.isContextual("type")) {
  2660. node.exportKind = "type";
  2661. const declarationNode = this.startNode();
  2662. this.next();
  2663. if (this.match(types$1.braceL)) {
  2664. node.specifiers = this.parseExportSpecifiers();
  2665. this.parseExportFrom(node);
  2666. return null;
  2667. } else {
  2668. return this.flowParseTypeAlias(declarationNode);
  2669. }
  2670. } else if (this.isContextual("opaque")) {
  2671. node.exportKind = "type";
  2672. const declarationNode = this.startNode();
  2673. this.next();
  2674. return this.flowParseOpaqueType(declarationNode, false);
  2675. } else if (this.isContextual("interface")) {
  2676. node.exportKind = "type";
  2677. const declarationNode = this.startNode();
  2678. this.next();
  2679. return this.flowParseInterface(declarationNode);
  2680. } else if (this.shouldParseEnums() && this.isContextual("enum")) {
  2681. node.exportKind = "value";
  2682. const declarationNode = this.startNode();
  2683. this.next();
  2684. return this.flowParseEnumDeclaration(declarationNode);
  2685. } else {
  2686. return super.parseExportDeclaration(node);
  2687. }
  2688. }
  2689. eatExportStar(node) {
  2690. if (super.eatExportStar(...arguments)) return true;
  2691. if (this.isContextual("type") && this.lookahead().type === types$1.star) {
  2692. node.exportKind = "type";
  2693. this.next();
  2694. this.next();
  2695. return true;
  2696. }
  2697. return false;
  2698. }
  2699. maybeParseExportNamespaceSpecifier(node) {
  2700. const pos = this.state.start;
  2701. const hasNamespace = super.maybeParseExportNamespaceSpecifier(node);
  2702. if (hasNamespace && node.exportKind === "type") {
  2703. this.unexpected(pos);
  2704. }
  2705. return hasNamespace;
  2706. }
  2707. parseClassId(node, isStatement, optionalId) {
  2708. super.parseClassId(node, isStatement, optionalId);
  2709. if (this.isRelational("<")) {
  2710. node.typeParameters = this.flowParseTypeParameterDeclaration();
  2711. }
  2712. }
  2713. parseClassMember(classBody, member, state) {
  2714. const pos = this.state.start;
  2715. if (this.isContextual("declare")) {
  2716. if (this.parseClassMemberFromModifier(classBody, member)) {
  2717. return;
  2718. }
  2719. member.declare = true;
  2720. }
  2721. super.parseClassMember(classBody, member, state);
  2722. if (member.declare) {
  2723. if (member.type !== "ClassProperty" && member.type !== "ClassPrivateProperty" && member.type !== "PropertyDefinition") {
  2724. this.raise(pos, FlowErrors.DeclareClassElement);
  2725. } else if (member.value) {
  2726. this.raise(member.value.start, FlowErrors.DeclareClassFieldInitializer);
  2727. }
  2728. }
  2729. }
  2730. isIterator(word) {
  2731. return word === "iterator" || word === "asyncIterator";
  2732. }
  2733. readIterator() {
  2734. const word = super.readWord1();
  2735. const fullWord = "@@" + word;
  2736. if (!this.isIterator(word) || !this.state.inType) {
  2737. this.raise(this.state.pos, ErrorMessages.InvalidIdentifier, fullWord);
  2738. }
  2739. this.finishToken(types$1.name, fullWord);
  2740. }
  2741. getTokenFromCode(code) {
  2742. const next = this.input.charCodeAt(this.state.pos + 1);
  2743. if (code === 123 && next === 124) {
  2744. return this.finishOp(types$1.braceBarL, 2);
  2745. } else if (this.state.inType && (code === 62 || code === 60)) {
  2746. return this.finishOp(types$1.relational, 1);
  2747. } else if (this.state.inType && code === 63) {
  2748. if (next === 46) {
  2749. return this.finishOp(types$1.questionDot, 2);
  2750. }
  2751. return this.finishOp(types$1.question, 1);
  2752. } else if (isIteratorStart(code, next)) {
  2753. this.state.pos += 2;
  2754. return this.readIterator();
  2755. } else {
  2756. return super.getTokenFromCode(code);
  2757. }
  2758. }
  2759. isAssignable(node, isBinding) {
  2760. switch (node.type) {
  2761. case "Identifier":
  2762. case "ObjectPattern":
  2763. case "ArrayPattern":
  2764. case "AssignmentPattern":
  2765. return true;
  2766. case "ObjectExpression":
  2767. {
  2768. const last = node.properties.length - 1;
  2769. return node.properties.every((prop, i) => {
  2770. return prop.type !== "ObjectMethod" && (i === last || prop.type === "SpreadElement") && this.isAssignable(prop);
  2771. });
  2772. }
  2773. case "ObjectProperty":
  2774. return this.isAssignable(node.value);
  2775. case "SpreadElement":
  2776. return this.isAssignable(node.argument);
  2777. case "ArrayExpression":
  2778. return node.elements.every(element => this.isAssignable(element));
  2779. case "AssignmentExpression":
  2780. return node.operator === "=";
  2781. case "ParenthesizedExpression":
  2782. case "TypeCastExpression":
  2783. return this.isAssignable(node.expression);
  2784. case "MemberExpression":
  2785. case "OptionalMemberExpression":
  2786. return !isBinding;
  2787. default:
  2788. return false;
  2789. }
  2790. }
  2791. toAssignable(node, isLHS = false) {
  2792. if (node.type === "TypeCastExpression") {
  2793. return super.toAssignable(this.typeCastToParameter(node), isLHS);
  2794. } else {
  2795. return super.toAssignable(node, isLHS);
  2796. }
  2797. }
  2798. toAssignableList(exprList, trailingCommaPos, isLHS) {
  2799. for (let i = 0; i < exprList.length; i++) {
  2800. const expr = exprList[i];
  2801. if ((expr == null ? void 0 : expr.type) === "TypeCastExpression") {
  2802. exprList[i] = this.typeCastToParameter(expr);
  2803. }
  2804. }
  2805. return super.toAssignableList(exprList, trailingCommaPos, isLHS);
  2806. }
  2807. toReferencedList(exprList, isParenthesizedExpr) {
  2808. for (let i = 0; i < exprList.length; i++) {
  2809. var _expr$extra;
  2810. const expr = exprList[i];
  2811. if (expr && expr.type === "TypeCastExpression" && !((_expr$extra = expr.extra) != null && _expr$extra.parenthesized) && (exprList.length > 1 || !isParenthesizedExpr)) {
  2812. this.raise(expr.typeAnnotation.start, FlowErrors.TypeCastInPattern);
  2813. }
  2814. }
  2815. return exprList;
  2816. }
  2817. parseArrayLike(close, canBePattern, isTuple, refExpressionErrors) {
  2818. const node = super.parseArrayLike(close, canBePattern, isTuple, refExpressionErrors);
  2819. if (canBePattern && !this.state.maybeInArrowParameters) {
  2820. this.toReferencedList(node.elements);
  2821. }
  2822. return node;
  2823. }
  2824. checkLVal(expr, ...args) {
  2825. if (expr.type !== "TypeCastExpression") {
  2826. return super.checkLVal(expr, ...args);
  2827. }
  2828. }
  2829. parseClassProperty(node) {
  2830. if (this.match(types$1.colon)) {
  2831. node.typeAnnotation = this.flowParseTypeAnnotation();
  2832. }
  2833. return super.parseClassProperty(node);
  2834. }
  2835. parseClassPrivateProperty(node) {
  2836. if (this.match(types$1.colon)) {
  2837. node.typeAnnotation = this.flowParseTypeAnnotation();
  2838. }
  2839. return super.parseClassPrivateProperty(node);
  2840. }
  2841. isClassMethod() {
  2842. return this.isRelational("<") || super.isClassMethod();
  2843. }
  2844. isClassProperty() {
  2845. return this.match(types$1.colon) || super.isClassProperty();
  2846. }
  2847. isNonstaticConstructor(method) {
  2848. return !this.match(types$1.colon) && super.isNonstaticConstructor(method);
  2849. }
  2850. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  2851. if (method.variance) {
  2852. this.unexpected(method.variance.start);
  2853. }
  2854. delete method.variance;
  2855. if (this.isRelational("<")) {
  2856. method.typeParameters = this.flowParseTypeParameterDeclaration();
  2857. }
  2858. super.pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper);
  2859. if (method.params && isConstructor) {
  2860. const params = method.params;
  2861. if (params.length > 0 && this.isThisParam(params[0])) {
  2862. this.raise(method.start, FlowErrors.ThisParamBannedInConstructor);
  2863. }
  2864. } else if (method.type === "MethodDefinition" && isConstructor && method.value.params) {
  2865. const params = method.value.params;
  2866. if (params.length > 0 && this.isThisParam(params[0])) {
  2867. this.raise(method.start, FlowErrors.ThisParamBannedInConstructor);
  2868. }
  2869. }
  2870. }
  2871. pushClassPrivateMethod(classBody, method, isGenerator, isAsync) {
  2872. if (method.variance) {
  2873. this.unexpected(method.variance.start);
  2874. }
  2875. delete method.variance;
  2876. if (this.isRelational("<")) {
  2877. method.typeParameters = this.flowParseTypeParameterDeclaration();
  2878. }
  2879. super.pushClassPrivateMethod(classBody, method, isGenerator, isAsync);
  2880. }
  2881. parseClassSuper(node) {
  2882. super.parseClassSuper(node);
  2883. if (node.superClass && this.isRelational("<")) {
  2884. node.superTypeParameters = this.flowParseTypeParameterInstantiation();
  2885. }
  2886. if (this.isContextual("implements")) {
  2887. this.next();
  2888. const implemented = node.implements = [];
  2889. do {
  2890. const node = this.startNode();
  2891. node.id = this.flowParseRestrictedIdentifier(true);
  2892. if (this.isRelational("<")) {
  2893. node.typeParameters = this.flowParseTypeParameterInstantiation();
  2894. } else {
  2895. node.typeParameters = null;
  2896. }
  2897. implemented.push(this.finishNode(node, "ClassImplements"));
  2898. } while (this.eat(types$1.comma));
  2899. }
  2900. }
  2901. checkGetterSetterParams(method) {
  2902. super.checkGetterSetterParams(method);
  2903. const params = this.getObjectOrClassMethodParams(method);
  2904. if (params.length > 0) {
  2905. const param = params[0];
  2906. if (this.isThisParam(param) && method.kind === "get") {
  2907. this.raise(param.start, FlowErrors.GetterMayNotHaveThisParam);
  2908. } else if (this.isThisParam(param)) {
  2909. this.raise(param.start, FlowErrors.SetterMayNotHaveThisParam);
  2910. }
  2911. }
  2912. }
  2913. parsePropertyName(node, isPrivateNameAllowed) {
  2914. const variance = this.flowParseVariance();
  2915. const key = super.parsePropertyName(node, isPrivateNameAllowed);
  2916. node.variance = variance;
  2917. return key;
  2918. }
  2919. parseObjPropValue(prop, startPos, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors) {
  2920. if (prop.variance) {
  2921. this.unexpected(prop.variance.start);
  2922. }
  2923. delete prop.variance;
  2924. let typeParameters;
  2925. if (this.isRelational("<") && !isAccessor) {
  2926. typeParameters = this.flowParseTypeParameterDeclaration();
  2927. if (!this.match(types$1.parenL)) this.unexpected();
  2928. }
  2929. super.parseObjPropValue(prop, startPos, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors);
  2930. if (typeParameters) {
  2931. (prop.value || prop).typeParameters = typeParameters;
  2932. }
  2933. }
  2934. parseAssignableListItemTypes(param) {
  2935. if (this.eat(types$1.question)) {
  2936. if (param.type !== "Identifier") {
  2937. this.raise(param.start, FlowErrors.OptionalBindingPattern);
  2938. }
  2939. if (this.isThisParam(param)) {
  2940. this.raise(param.start, FlowErrors.ThisParamMayNotBeOptional);
  2941. }
  2942. param.optional = true;
  2943. }
  2944. if (this.match(types$1.colon)) {
  2945. param.typeAnnotation = this.flowParseTypeAnnotation();
  2946. } else if (this.isThisParam(param)) {
  2947. this.raise(param.start, FlowErrors.ThisParamAnnotationRequired);
  2948. }
  2949. if (this.match(types$1.eq) && this.isThisParam(param)) {
  2950. this.raise(param.start, FlowErrors.ThisParamNoDefault);
  2951. }
  2952. this.resetEndLocation(param);
  2953. return param;
  2954. }
  2955. parseMaybeDefault(startPos, startLoc, left) {
  2956. const node = super.parseMaybeDefault(startPos, startLoc, left);
  2957. if (node.type === "AssignmentPattern" && node.typeAnnotation && node.right.start < node.typeAnnotation.start) {
  2958. this.raise(node.typeAnnotation.start, FlowErrors.TypeBeforeInitializer);
  2959. }
  2960. return node;
  2961. }
  2962. shouldParseDefaultImport(node) {
  2963. if (!hasTypeImportKind(node)) {
  2964. return super.shouldParseDefaultImport(node);
  2965. }
  2966. return isMaybeDefaultImport(this.state);
  2967. }
  2968. parseImportSpecifierLocal(node, specifier, type, contextDescription) {
  2969. specifier.local = hasTypeImportKind(node) ? this.flowParseRestrictedIdentifier(true, true) : this.parseIdentifier();
  2970. this.checkLVal(specifier.local, contextDescription, BIND_LEXICAL);
  2971. node.specifiers.push(this.finishNode(specifier, type));
  2972. }
  2973. maybeParseDefaultImportSpecifier(node) {
  2974. node.importKind = "value";
  2975. let kind = null;
  2976. if (this.match(types$1._typeof)) {
  2977. kind = "typeof";
  2978. } else if (this.isContextual("type")) {
  2979. kind = "type";
  2980. }
  2981. if (kind) {
  2982. const lh = this.lookahead();
  2983. if (kind === "type" && lh.type === types$1.star) {
  2984. this.unexpected(lh.start);
  2985. }
  2986. if (isMaybeDefaultImport(lh) || lh.type === types$1.braceL || lh.type === types$1.star) {
  2987. this.next();
  2988. node.importKind = kind;
  2989. }
  2990. }
  2991. return super.maybeParseDefaultImportSpecifier(node);
  2992. }
  2993. parseImportSpecifier(node) {
  2994. const specifier = this.startNode();
  2995. const firstIdentIsString = this.match(types$1.string);
  2996. const firstIdent = this.parseModuleExportName();
  2997. let specifierTypeKind = null;
  2998. if (firstIdent.type === "Identifier") {
  2999. if (firstIdent.name === "type") {
  3000. specifierTypeKind = "type";
  3001. } else if (firstIdent.name === "typeof") {
  3002. specifierTypeKind = "typeof";
  3003. }
  3004. }
  3005. let isBinding = false;
  3006. if (this.isContextual("as") && !this.isLookaheadContextual("as")) {
  3007. const as_ident = this.parseIdentifier(true);
  3008. if (specifierTypeKind !== null && !this.match(types$1.name) && !this.state.type.keyword) {
  3009. specifier.imported = as_ident;
  3010. specifier.importKind = specifierTypeKind;
  3011. specifier.local = as_ident.__clone();
  3012. } else {
  3013. specifier.imported = firstIdent;
  3014. specifier.importKind = null;
  3015. specifier.local = this.parseIdentifier();
  3016. }
  3017. } else if (specifierTypeKind !== null && (this.match(types$1.name) || this.state.type.keyword)) {
  3018. specifier.imported = this.parseIdentifier(true);
  3019. specifier.importKind = specifierTypeKind;
  3020. if (this.eatContextual("as")) {
  3021. specifier.local = this.parseIdentifier();
  3022. } else {
  3023. isBinding = true;
  3024. specifier.local = specifier.imported.__clone();
  3025. }
  3026. } else {
  3027. if (firstIdentIsString) {
  3028. throw this.raise(specifier.start, ErrorMessages.ImportBindingIsString, firstIdent.value);
  3029. }
  3030. isBinding = true;
  3031. specifier.imported = firstIdent;
  3032. specifier.importKind = null;
  3033. specifier.local = specifier.imported.__clone();
  3034. }
  3035. const nodeIsTypeImport = hasTypeImportKind(node);
  3036. const specifierIsTypeImport = hasTypeImportKind(specifier);
  3037. if (nodeIsTypeImport && specifierIsTypeImport) {
  3038. this.raise(specifier.start, FlowErrors.ImportTypeShorthandOnlyInPureImport);
  3039. }
  3040. if (nodeIsTypeImport || specifierIsTypeImport) {
  3041. this.checkReservedType(specifier.local.name, specifier.local.start, true);
  3042. }
  3043. if (isBinding && !nodeIsTypeImport && !specifierIsTypeImport) {
  3044. this.checkReservedWord(specifier.local.name, specifier.start, true, true);
  3045. }
  3046. this.checkLVal(specifier.local, "import specifier", BIND_LEXICAL);
  3047. node.specifiers.push(this.finishNode(specifier, "ImportSpecifier"));
  3048. }
  3049. parseBindingAtom() {
  3050. switch (this.state.type) {
  3051. case types$1._this:
  3052. return this.parseIdentifier(true);
  3053. default:
  3054. return super.parseBindingAtom();
  3055. }
  3056. }
  3057. parseFunctionParams(node, allowModifiers) {
  3058. const kind = node.kind;
  3059. if (kind !== "get" && kind !== "set" && this.isRelational("<")) {
  3060. node.typeParameters = this.flowParseTypeParameterDeclaration();
  3061. }
  3062. super.parseFunctionParams(node, allowModifiers);
  3063. }
  3064. parseVarId(decl, kind) {
  3065. super.parseVarId(decl, kind);
  3066. if (this.match(types$1.colon)) {
  3067. decl.id.typeAnnotation = this.flowParseTypeAnnotation();
  3068. this.resetEndLocation(decl.id);
  3069. }
  3070. }
  3071. parseAsyncArrowFromCallExpression(node, call) {
  3072. if (this.match(types$1.colon)) {
  3073. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  3074. this.state.noAnonFunctionType = true;
  3075. node.returnType = this.flowParseTypeAnnotation();
  3076. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  3077. }
  3078. return super.parseAsyncArrowFromCallExpression(node, call);
  3079. }
  3080. shouldParseAsyncArrow() {
  3081. return this.match(types$1.colon) || super.shouldParseAsyncArrow();
  3082. }
  3083. parseMaybeAssign(refExpressionErrors, afterLeftParse) {
  3084. var _jsx;
  3085. let state = null;
  3086. let jsx;
  3087. if (this.hasPlugin("jsx") && (this.match(types$1.jsxTagStart) || this.isRelational("<"))) {
  3088. state = this.state.clone();
  3089. jsx = this.tryParse(() => super.parseMaybeAssign(refExpressionErrors, afterLeftParse), state);
  3090. if (!jsx.error) return jsx.node;
  3091. const {
  3092. context
  3093. } = this.state;
  3094. const curContext = context[context.length - 1];
  3095. if (curContext === types.j_oTag) {
  3096. context.length -= 2;
  3097. } else if (curContext === types.j_expr) {
  3098. context.length -= 1;
  3099. }
  3100. }
  3101. if ((_jsx = jsx) != null && _jsx.error || this.isRelational("<")) {
  3102. var _jsx2, _jsx3;
  3103. state = state || this.state.clone();
  3104. let typeParameters;
  3105. const arrow = this.tryParse(abort => {
  3106. var _arrowExpression$extr;
  3107. typeParameters = this.flowParseTypeParameterDeclaration();
  3108. const arrowExpression = this.forwardNoArrowParamsConversionAt(typeParameters, () => {
  3109. const result = super.parseMaybeAssign(refExpressionErrors, afterLeftParse);
  3110. this.resetStartLocationFromNode(result, typeParameters);
  3111. return result;
  3112. });
  3113. if (arrowExpression.type !== "ArrowFunctionExpression" && (_arrowExpression$extr = arrowExpression.extra) != null && _arrowExpression$extr.parenthesized) {
  3114. abort();
  3115. }
  3116. const expr = this.maybeUnwrapTypeCastExpression(arrowExpression);
  3117. expr.typeParameters = typeParameters;
  3118. this.resetStartLocationFromNode(expr, typeParameters);
  3119. return arrowExpression;
  3120. }, state);
  3121. let arrowExpression = null;
  3122. if (arrow.node && this.maybeUnwrapTypeCastExpression(arrow.node).type === "ArrowFunctionExpression") {
  3123. if (!arrow.error && !arrow.aborted) {
  3124. if (arrow.node.async) {
  3125. this.raise(typeParameters.start, FlowErrors.UnexpectedTypeParameterBeforeAsyncArrowFunction);
  3126. }
  3127. return arrow.node;
  3128. }
  3129. arrowExpression = arrow.node;
  3130. }
  3131. if ((_jsx2 = jsx) != null && _jsx2.node) {
  3132. this.state = jsx.failState;
  3133. return jsx.node;
  3134. }
  3135. if (arrowExpression) {
  3136. this.state = arrow.failState;
  3137. return arrowExpression;
  3138. }
  3139. if ((_jsx3 = jsx) != null && _jsx3.thrown) throw jsx.error;
  3140. if (arrow.thrown) throw arrow.error;
  3141. throw this.raise(typeParameters.start, FlowErrors.UnexpectedTokenAfterTypeParameter);
  3142. }
  3143. return super.parseMaybeAssign(refExpressionErrors, afterLeftParse);
  3144. }
  3145. parseArrow(node) {
  3146. if (this.match(types$1.colon)) {
  3147. const result = this.tryParse(() => {
  3148. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  3149. this.state.noAnonFunctionType = true;
  3150. const typeNode = this.startNode();
  3151. [typeNode.typeAnnotation, node.predicate] = this.flowParseTypeAndPredicateInitialiser();
  3152. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  3153. if (this.canInsertSemicolon()) this.unexpected();
  3154. if (!this.match(types$1.arrow)) this.unexpected();
  3155. return typeNode;
  3156. });
  3157. if (result.thrown) return null;
  3158. if (result.error) this.state = result.failState;
  3159. node.returnType = result.node.typeAnnotation ? this.finishNode(result.node, "TypeAnnotation") : null;
  3160. }
  3161. return super.parseArrow(node);
  3162. }
  3163. shouldParseArrow() {
  3164. return this.match(types$1.colon) || super.shouldParseArrow();
  3165. }
  3166. setArrowFunctionParameters(node, params) {
  3167. if (this.state.noArrowParamsConversionAt.indexOf(node.start) !== -1) {
  3168. node.params = params;
  3169. } else {
  3170. super.setArrowFunctionParameters(node, params);
  3171. }
  3172. }
  3173. checkParams(node, allowDuplicates, isArrowFunction) {
  3174. if (isArrowFunction && this.state.noArrowParamsConversionAt.indexOf(node.start) !== -1) {
  3175. return;
  3176. }
  3177. for (let i = 0; i < node.params.length; i++) {
  3178. if (this.isThisParam(node.params[i]) && i > 0) {
  3179. this.raise(node.params[i].start, FlowErrors.ThisParamMustBeFirst);
  3180. }
  3181. }
  3182. return super.checkParams(...arguments);
  3183. }
  3184. parseParenAndDistinguishExpression(canBeArrow) {
  3185. return super.parseParenAndDistinguishExpression(canBeArrow && this.state.noArrowAt.indexOf(this.state.start) === -1);
  3186. }
  3187. parseSubscripts(base, startPos, startLoc, noCalls) {
  3188. if (base.type === "Identifier" && base.name === "async" && this.state.noArrowAt.indexOf(startPos) !== -1) {
  3189. this.next();
  3190. const node = this.startNodeAt(startPos, startLoc);
  3191. node.callee = base;
  3192. node.arguments = this.parseCallExpressionArguments(types$1.parenR, false);
  3193. base = this.finishNode(node, "CallExpression");
  3194. } else if (base.type === "Identifier" && base.name === "async" && this.isRelational("<")) {
  3195. const state = this.state.clone();
  3196. const arrow = this.tryParse(abort => this.parseAsyncArrowWithTypeParameters(startPos, startLoc) || abort(), state);
  3197. if (!arrow.error && !arrow.aborted) return arrow.node;
  3198. const result = this.tryParse(() => super.parseSubscripts(base, startPos, startLoc, noCalls), state);
  3199. if (result.node && !result.error) return result.node;
  3200. if (arrow.node) {
  3201. this.state = arrow.failState;
  3202. return arrow.node;
  3203. }
  3204. if (result.node) {
  3205. this.state = result.failState;
  3206. return result.node;
  3207. }
  3208. throw arrow.error || result.error;
  3209. }
  3210. return super.parseSubscripts(base, startPos, startLoc, noCalls);
  3211. }
  3212. parseSubscript(base, startPos, startLoc, noCalls, subscriptState) {
  3213. if (this.match(types$1.questionDot) && this.isLookaheadToken_lt()) {
  3214. subscriptState.optionalChainMember = true;
  3215. if (noCalls) {
  3216. subscriptState.stop = true;
  3217. return base;
  3218. }
  3219. this.next();
  3220. const node = this.startNodeAt(startPos, startLoc);
  3221. node.callee = base;
  3222. node.typeArguments = this.flowParseTypeParameterInstantiation();
  3223. this.expect(types$1.parenL);
  3224. node.arguments = this.parseCallExpressionArguments(types$1.parenR, false);
  3225. node.optional = true;
  3226. return this.finishCallExpression(node, true);
  3227. } else if (!noCalls && this.shouldParseTypes() && this.isRelational("<")) {
  3228. const node = this.startNodeAt(startPos, startLoc);
  3229. node.callee = base;
  3230. const result = this.tryParse(() => {
  3231. node.typeArguments = this.flowParseTypeParameterInstantiationCallOrNew();
  3232. this.expect(types$1.parenL);
  3233. node.arguments = this.parseCallExpressionArguments(types$1.parenR, false);
  3234. if (subscriptState.optionalChainMember) node.optional = false;
  3235. return this.finishCallExpression(node, subscriptState.optionalChainMember);
  3236. });
  3237. if (result.node) {
  3238. if (result.error) this.state = result.failState;
  3239. return result.node;
  3240. }
  3241. }
  3242. return super.parseSubscript(base, startPos, startLoc, noCalls, subscriptState);
  3243. }
  3244. parseNewArguments(node) {
  3245. let targs = null;
  3246. if (this.shouldParseTypes() && this.isRelational("<")) {
  3247. targs = this.tryParse(() => this.flowParseTypeParameterInstantiationCallOrNew()).node;
  3248. }
  3249. node.typeArguments = targs;
  3250. super.parseNewArguments(node);
  3251. }
  3252. parseAsyncArrowWithTypeParameters(startPos, startLoc) {
  3253. const node = this.startNodeAt(startPos, startLoc);
  3254. this.parseFunctionParams(node);
  3255. if (!this.parseArrow(node)) return;
  3256. return this.parseArrowExpression(node, undefined, true);
  3257. }
  3258. readToken_mult_modulo(code) {
  3259. const next = this.input.charCodeAt(this.state.pos + 1);
  3260. if (code === 42 && next === 47 && this.state.hasFlowComment) {
  3261. this.state.hasFlowComment = false;
  3262. this.state.pos += 2;
  3263. this.nextToken();
  3264. return;
  3265. }
  3266. super.readToken_mult_modulo(code);
  3267. }
  3268. readToken_pipe_amp(code) {
  3269. const next = this.input.charCodeAt(this.state.pos + 1);
  3270. if (code === 124 && next === 125) {
  3271. this.finishOp(types$1.braceBarR, 2);
  3272. return;
  3273. }
  3274. super.readToken_pipe_amp(code);
  3275. }
  3276. parseTopLevel(file, program) {
  3277. const fileNode = super.parseTopLevel(file, program);
  3278. if (this.state.hasFlowComment) {
  3279. this.raise(this.state.pos, FlowErrors.UnterminatedFlowComment);
  3280. }
  3281. return fileNode;
  3282. }
  3283. skipBlockComment() {
  3284. if (this.hasPlugin("flowComments") && this.skipFlowComment()) {
  3285. if (this.state.hasFlowComment) {
  3286. this.unexpected(null, FlowErrors.NestedFlowComment);
  3287. }
  3288. this.hasFlowCommentCompletion();
  3289. this.state.pos += this.skipFlowComment();
  3290. this.state.hasFlowComment = true;
  3291. return;
  3292. }
  3293. if (this.state.hasFlowComment) {
  3294. const end = this.input.indexOf("*-/", this.state.pos += 2);
  3295. if (end === -1) {
  3296. throw this.raise(this.state.pos - 2, ErrorMessages.UnterminatedComment);
  3297. }
  3298. this.state.pos = end + 3;
  3299. return;
  3300. }
  3301. super.skipBlockComment();
  3302. }
  3303. skipFlowComment() {
  3304. const {
  3305. pos
  3306. } = this.state;
  3307. let shiftToFirstNonWhiteSpace = 2;
  3308. while ([32, 9].includes(this.input.charCodeAt(pos + shiftToFirstNonWhiteSpace))) {
  3309. shiftToFirstNonWhiteSpace++;
  3310. }
  3311. const ch2 = this.input.charCodeAt(shiftToFirstNonWhiteSpace + pos);
  3312. const ch3 = this.input.charCodeAt(shiftToFirstNonWhiteSpace + pos + 1);
  3313. if (ch2 === 58 && ch3 === 58) {
  3314. return shiftToFirstNonWhiteSpace + 2;
  3315. }
  3316. if (this.input.slice(shiftToFirstNonWhiteSpace + pos, shiftToFirstNonWhiteSpace + pos + 12) === "flow-include") {
  3317. return shiftToFirstNonWhiteSpace + 12;
  3318. }
  3319. if (ch2 === 58 && ch3 !== 58) {
  3320. return shiftToFirstNonWhiteSpace;
  3321. }
  3322. return false;
  3323. }
  3324. hasFlowCommentCompletion() {
  3325. const end = this.input.indexOf("*/", this.state.pos);
  3326. if (end === -1) {
  3327. throw this.raise(this.state.pos, ErrorMessages.UnterminatedComment);
  3328. }
  3329. }
  3330. flowEnumErrorBooleanMemberNotInitialized(pos, {
  3331. enumName,
  3332. memberName
  3333. }) {
  3334. this.raise(pos, FlowErrors.EnumBooleanMemberNotInitialized, memberName, enumName);
  3335. }
  3336. flowEnumErrorInvalidMemberName(pos, {
  3337. enumName,
  3338. memberName
  3339. }) {
  3340. const suggestion = memberName[0].toUpperCase() + memberName.slice(1);
  3341. this.raise(pos, FlowErrors.EnumInvalidMemberName, memberName, suggestion, enumName);
  3342. }
  3343. flowEnumErrorDuplicateMemberName(pos, {
  3344. enumName,
  3345. memberName
  3346. }) {
  3347. this.raise(pos, FlowErrors.EnumDuplicateMemberName, memberName, enumName);
  3348. }
  3349. flowEnumErrorInconsistentMemberValues(pos, {
  3350. enumName
  3351. }) {
  3352. this.raise(pos, FlowErrors.EnumInconsistentMemberValues, enumName);
  3353. }
  3354. flowEnumErrorInvalidExplicitType(pos, {
  3355. enumName,
  3356. suppliedType
  3357. }) {
  3358. return this.raise(pos, suppliedType === null ? FlowErrors.EnumInvalidExplicitTypeUnknownSupplied : FlowErrors.EnumInvalidExplicitType, enumName, suppliedType);
  3359. }
  3360. flowEnumErrorInvalidMemberInitializer(pos, {
  3361. enumName,
  3362. explicitType,
  3363. memberName
  3364. }) {
  3365. let message = null;
  3366. switch (explicitType) {
  3367. case "boolean":
  3368. case "number":
  3369. case "string":
  3370. message = FlowErrors.EnumInvalidMemberInitializerPrimaryType;
  3371. break;
  3372. case "symbol":
  3373. message = FlowErrors.EnumInvalidMemberInitializerSymbolType;
  3374. break;
  3375. default:
  3376. message = FlowErrors.EnumInvalidMemberInitializerUnknownType;
  3377. }
  3378. return this.raise(pos, message, enumName, memberName, explicitType);
  3379. }
  3380. flowEnumErrorNumberMemberNotInitialized(pos, {
  3381. enumName,
  3382. memberName
  3383. }) {
  3384. this.raise(pos, FlowErrors.EnumNumberMemberNotInitialized, enumName, memberName);
  3385. }
  3386. flowEnumErrorStringMemberInconsistentlyInitailized(pos, {
  3387. enumName
  3388. }) {
  3389. this.raise(pos, FlowErrors.EnumStringMemberInconsistentlyInitailized, enumName);
  3390. }
  3391. flowEnumMemberInit() {
  3392. const startPos = this.state.start;
  3393. const endOfInit = () => this.match(types$1.comma) || this.match(types$1.braceR);
  3394. switch (this.state.type) {
  3395. case types$1.num:
  3396. {
  3397. const literal = this.parseNumericLiteral(this.state.value);
  3398. if (endOfInit()) {
  3399. return {
  3400. type: "number",
  3401. pos: literal.start,
  3402. value: literal
  3403. };
  3404. }
  3405. return {
  3406. type: "invalid",
  3407. pos: startPos
  3408. };
  3409. }
  3410. case types$1.string:
  3411. {
  3412. const literal = this.parseStringLiteral(this.state.value);
  3413. if (endOfInit()) {
  3414. return {
  3415. type: "string",
  3416. pos: literal.start,
  3417. value: literal
  3418. };
  3419. }
  3420. return {
  3421. type: "invalid",
  3422. pos: startPos
  3423. };
  3424. }
  3425. case types$1._true:
  3426. case types$1._false:
  3427. {
  3428. const literal = this.parseBooleanLiteral(this.match(types$1._true));
  3429. if (endOfInit()) {
  3430. return {
  3431. type: "boolean",
  3432. pos: literal.start,
  3433. value: literal
  3434. };
  3435. }
  3436. return {
  3437. type: "invalid",
  3438. pos: startPos
  3439. };
  3440. }
  3441. default:
  3442. return {
  3443. type: "invalid",
  3444. pos: startPos
  3445. };
  3446. }
  3447. }
  3448. flowEnumMemberRaw() {
  3449. const pos = this.state.start;
  3450. const id = this.parseIdentifier(true);
  3451. const init = this.eat(types$1.eq) ? this.flowEnumMemberInit() : {
  3452. type: "none",
  3453. pos
  3454. };
  3455. return {
  3456. id,
  3457. init
  3458. };
  3459. }
  3460. flowEnumCheckExplicitTypeMismatch(pos, context, expectedType) {
  3461. const {
  3462. explicitType
  3463. } = context;
  3464. if (explicitType === null) {
  3465. return;
  3466. }
  3467. if (explicitType !== expectedType) {
  3468. this.flowEnumErrorInvalidMemberInitializer(pos, context);
  3469. }
  3470. }
  3471. flowEnumMembers({
  3472. enumName,
  3473. explicitType
  3474. }) {
  3475. const seenNames = new Set();
  3476. const members = {
  3477. booleanMembers: [],
  3478. numberMembers: [],
  3479. stringMembers: [],
  3480. defaultedMembers: []
  3481. };
  3482. let hasUnknownMembers = false;
  3483. while (!this.match(types$1.braceR)) {
  3484. if (this.eat(types$1.ellipsis)) {
  3485. hasUnknownMembers = true;
  3486. break;
  3487. }
  3488. const memberNode = this.startNode();
  3489. const {
  3490. id,
  3491. init
  3492. } = this.flowEnumMemberRaw();
  3493. const memberName = id.name;
  3494. if (memberName === "") {
  3495. continue;
  3496. }
  3497. if (/^[a-z]/.test(memberName)) {
  3498. this.flowEnumErrorInvalidMemberName(id.start, {
  3499. enumName,
  3500. memberName
  3501. });
  3502. }
  3503. if (seenNames.has(memberName)) {
  3504. this.flowEnumErrorDuplicateMemberName(id.start, {
  3505. enumName,
  3506. memberName
  3507. });
  3508. }
  3509. seenNames.add(memberName);
  3510. const context = {
  3511. enumName,
  3512. explicitType,
  3513. memberName
  3514. };
  3515. memberNode.id = id;
  3516. switch (init.type) {
  3517. case "boolean":
  3518. {
  3519. this.flowEnumCheckExplicitTypeMismatch(init.pos, context, "boolean");
  3520. memberNode.init = init.value;
  3521. members.booleanMembers.push(this.finishNode(memberNode, "EnumBooleanMember"));
  3522. break;
  3523. }
  3524. case "number":
  3525. {
  3526. this.flowEnumCheckExplicitTypeMismatch(init.pos, context, "number");
  3527. memberNode.init = init.value;
  3528. members.numberMembers.push(this.finishNode(memberNode, "EnumNumberMember"));
  3529. break;
  3530. }
  3531. case "string":
  3532. {
  3533. this.flowEnumCheckExplicitTypeMismatch(init.pos, context, "string");
  3534. memberNode.init = init.value;
  3535. members.stringMembers.push(this.finishNode(memberNode, "EnumStringMember"));
  3536. break;
  3537. }
  3538. case "invalid":
  3539. {
  3540. throw this.flowEnumErrorInvalidMemberInitializer(init.pos, context);
  3541. }
  3542. case "none":
  3543. {
  3544. switch (explicitType) {
  3545. case "boolean":
  3546. this.flowEnumErrorBooleanMemberNotInitialized(init.pos, context);
  3547. break;
  3548. case "number":
  3549. this.flowEnumErrorNumberMemberNotInitialized(init.pos, context);
  3550. break;
  3551. default:
  3552. members.defaultedMembers.push(this.finishNode(memberNode, "EnumDefaultedMember"));
  3553. }
  3554. }
  3555. }
  3556. if (!this.match(types$1.braceR)) {
  3557. this.expect(types$1.comma);
  3558. }
  3559. }
  3560. return {
  3561. members,
  3562. hasUnknownMembers
  3563. };
  3564. }
  3565. flowEnumStringMembers(initializedMembers, defaultedMembers, {
  3566. enumName
  3567. }) {
  3568. if (initializedMembers.length === 0) {
  3569. return defaultedMembers;
  3570. } else if (defaultedMembers.length === 0) {
  3571. return initializedMembers;
  3572. } else if (defaultedMembers.length > initializedMembers.length) {
  3573. for (const member of initializedMembers) {
  3574. this.flowEnumErrorStringMemberInconsistentlyInitailized(member.start, {
  3575. enumName
  3576. });
  3577. }
  3578. return defaultedMembers;
  3579. } else {
  3580. for (const member of defaultedMembers) {
  3581. this.flowEnumErrorStringMemberInconsistentlyInitailized(member.start, {
  3582. enumName
  3583. });
  3584. }
  3585. return initializedMembers;
  3586. }
  3587. }
  3588. flowEnumParseExplicitType({
  3589. enumName
  3590. }) {
  3591. if (this.eatContextual("of")) {
  3592. if (!this.match(types$1.name)) {
  3593. throw this.flowEnumErrorInvalidExplicitType(this.state.start, {
  3594. enumName,
  3595. suppliedType: null
  3596. });
  3597. }
  3598. const {
  3599. value
  3600. } = this.state;
  3601. this.next();
  3602. if (value !== "boolean" && value !== "number" && value !== "string" && value !== "symbol") {
  3603. this.flowEnumErrorInvalidExplicitType(this.state.start, {
  3604. enumName,
  3605. suppliedType: value
  3606. });
  3607. }
  3608. return value;
  3609. }
  3610. return null;
  3611. }
  3612. flowEnumBody(node, {
  3613. enumName,
  3614. nameLoc
  3615. }) {
  3616. const explicitType = this.flowEnumParseExplicitType({
  3617. enumName
  3618. });
  3619. this.expect(types$1.braceL);
  3620. const {
  3621. members,
  3622. hasUnknownMembers
  3623. } = this.flowEnumMembers({
  3624. enumName,
  3625. explicitType
  3626. });
  3627. node.hasUnknownMembers = hasUnknownMembers;
  3628. switch (explicitType) {
  3629. case "boolean":
  3630. node.explicitType = true;
  3631. node.members = members.booleanMembers;
  3632. this.expect(types$1.braceR);
  3633. return this.finishNode(node, "EnumBooleanBody");
  3634. case "number":
  3635. node.explicitType = true;
  3636. node.members = members.numberMembers;
  3637. this.expect(types$1.braceR);
  3638. return this.finishNode(node, "EnumNumberBody");
  3639. case "string":
  3640. node.explicitType = true;
  3641. node.members = this.flowEnumStringMembers(members.stringMembers, members.defaultedMembers, {
  3642. enumName
  3643. });
  3644. this.expect(types$1.braceR);
  3645. return this.finishNode(node, "EnumStringBody");
  3646. case "symbol":
  3647. node.members = members.defaultedMembers;
  3648. this.expect(types$1.braceR);
  3649. return this.finishNode(node, "EnumSymbolBody");
  3650. default:
  3651. {
  3652. const empty = () => {
  3653. node.members = [];
  3654. this.expect(types$1.braceR);
  3655. return this.finishNode(node, "EnumStringBody");
  3656. };
  3657. node.explicitType = false;
  3658. const boolsLen = members.booleanMembers.length;
  3659. const numsLen = members.numberMembers.length;
  3660. const strsLen = members.stringMembers.length;
  3661. const defaultedLen = members.defaultedMembers.length;
  3662. if (!boolsLen && !numsLen && !strsLen && !defaultedLen) {
  3663. return empty();
  3664. } else if (!boolsLen && !numsLen) {
  3665. node.members = this.flowEnumStringMembers(members.stringMembers, members.defaultedMembers, {
  3666. enumName
  3667. });
  3668. this.expect(types$1.braceR);
  3669. return this.finishNode(node, "EnumStringBody");
  3670. } else if (!numsLen && !strsLen && boolsLen >= defaultedLen) {
  3671. for (const member of members.defaultedMembers) {
  3672. this.flowEnumErrorBooleanMemberNotInitialized(member.start, {
  3673. enumName,
  3674. memberName: member.id.name
  3675. });
  3676. }
  3677. node.members = members.booleanMembers;
  3678. this.expect(types$1.braceR);
  3679. return this.finishNode(node, "EnumBooleanBody");
  3680. } else if (!boolsLen && !strsLen && numsLen >= defaultedLen) {
  3681. for (const member of members.defaultedMembers) {
  3682. this.flowEnumErrorNumberMemberNotInitialized(member.start, {
  3683. enumName,
  3684. memberName: member.id.name
  3685. });
  3686. }
  3687. node.members = members.numberMembers;
  3688. this.expect(types$1.braceR);
  3689. return this.finishNode(node, "EnumNumberBody");
  3690. } else {
  3691. this.flowEnumErrorInconsistentMemberValues(nameLoc, {
  3692. enumName
  3693. });
  3694. return empty();
  3695. }
  3696. }
  3697. }
  3698. }
  3699. flowParseEnumDeclaration(node) {
  3700. const id = this.parseIdentifier();
  3701. node.id = id;
  3702. node.body = this.flowEnumBody(this.startNode(), {
  3703. enumName: id.name,
  3704. nameLoc: id.start
  3705. });
  3706. return this.finishNode(node, "EnumDeclaration");
  3707. }
  3708. isLookaheadToken_lt() {
  3709. const next = this.nextTokenStart();
  3710. if (this.input.charCodeAt(next) === 60) {
  3711. const afterNext = this.input.charCodeAt(next + 1);
  3712. return afterNext !== 60 && afterNext !== 61;
  3713. }
  3714. return false;
  3715. }
  3716. maybeUnwrapTypeCastExpression(node) {
  3717. return node.type === "TypeCastExpression" ? node.expression : node;
  3718. }
  3719. });
  3720. const entities = {
  3721. quot: "\u0022",
  3722. amp: "&",
  3723. apos: "\u0027",
  3724. lt: "<",
  3725. gt: ">",
  3726. nbsp: "\u00A0",
  3727. iexcl: "\u00A1",
  3728. cent: "\u00A2",
  3729. pound: "\u00A3",
  3730. curren: "\u00A4",
  3731. yen: "\u00A5",
  3732. brvbar: "\u00A6",
  3733. sect: "\u00A7",
  3734. uml: "\u00A8",
  3735. copy: "\u00A9",
  3736. ordf: "\u00AA",
  3737. laquo: "\u00AB",
  3738. not: "\u00AC",
  3739. shy: "\u00AD",
  3740. reg: "\u00AE",
  3741. macr: "\u00AF",
  3742. deg: "\u00B0",
  3743. plusmn: "\u00B1",
  3744. sup2: "\u00B2",
  3745. sup3: "\u00B3",
  3746. acute: "\u00B4",
  3747. micro: "\u00B5",
  3748. para: "\u00B6",
  3749. middot: "\u00B7",
  3750. cedil: "\u00B8",
  3751. sup1: "\u00B9",
  3752. ordm: "\u00BA",
  3753. raquo: "\u00BB",
  3754. frac14: "\u00BC",
  3755. frac12: "\u00BD",
  3756. frac34: "\u00BE",
  3757. iquest: "\u00BF",
  3758. Agrave: "\u00C0",
  3759. Aacute: "\u00C1",
  3760. Acirc: "\u00C2",
  3761. Atilde: "\u00C3",
  3762. Auml: "\u00C4",
  3763. Aring: "\u00C5",
  3764. AElig: "\u00C6",
  3765. Ccedil: "\u00C7",
  3766. Egrave: "\u00C8",
  3767. Eacute: "\u00C9",
  3768. Ecirc: "\u00CA",
  3769. Euml: "\u00CB",
  3770. Igrave: "\u00CC",
  3771. Iacute: "\u00CD",
  3772. Icirc: "\u00CE",
  3773. Iuml: "\u00CF",
  3774. ETH: "\u00D0",
  3775. Ntilde: "\u00D1",
  3776. Ograve: "\u00D2",
  3777. Oacute: "\u00D3",
  3778. Ocirc: "\u00D4",
  3779. Otilde: "\u00D5",
  3780. Ouml: "\u00D6",
  3781. times: "\u00D7",
  3782. Oslash: "\u00D8",
  3783. Ugrave: "\u00D9",
  3784. Uacute: "\u00DA",
  3785. Ucirc: "\u00DB",
  3786. Uuml: "\u00DC",
  3787. Yacute: "\u00DD",
  3788. THORN: "\u00DE",
  3789. szlig: "\u00DF",
  3790. agrave: "\u00E0",
  3791. aacute: "\u00E1",
  3792. acirc: "\u00E2",
  3793. atilde: "\u00E3",
  3794. auml: "\u00E4",
  3795. aring: "\u00E5",
  3796. aelig: "\u00E6",
  3797. ccedil: "\u00E7",
  3798. egrave: "\u00E8",
  3799. eacute: "\u00E9",
  3800. ecirc: "\u00EA",
  3801. euml: "\u00EB",
  3802. igrave: "\u00EC",
  3803. iacute: "\u00ED",
  3804. icirc: "\u00EE",
  3805. iuml: "\u00EF",
  3806. eth: "\u00F0",
  3807. ntilde: "\u00F1",
  3808. ograve: "\u00F2",
  3809. oacute: "\u00F3",
  3810. ocirc: "\u00F4",
  3811. otilde: "\u00F5",
  3812. ouml: "\u00F6",
  3813. divide: "\u00F7",
  3814. oslash: "\u00F8",
  3815. ugrave: "\u00F9",
  3816. uacute: "\u00FA",
  3817. ucirc: "\u00FB",
  3818. uuml: "\u00FC",
  3819. yacute: "\u00FD",
  3820. thorn: "\u00FE",
  3821. yuml: "\u00FF",
  3822. OElig: "\u0152",
  3823. oelig: "\u0153",
  3824. Scaron: "\u0160",
  3825. scaron: "\u0161",
  3826. Yuml: "\u0178",
  3827. fnof: "\u0192",
  3828. circ: "\u02C6",
  3829. tilde: "\u02DC",
  3830. Alpha: "\u0391",
  3831. Beta: "\u0392",
  3832. Gamma: "\u0393",
  3833. Delta: "\u0394",
  3834. Epsilon: "\u0395",
  3835. Zeta: "\u0396",
  3836. Eta: "\u0397",
  3837. Theta: "\u0398",
  3838. Iota: "\u0399",
  3839. Kappa: "\u039A",
  3840. Lambda: "\u039B",
  3841. Mu: "\u039C",
  3842. Nu: "\u039D",
  3843. Xi: "\u039E",
  3844. Omicron: "\u039F",
  3845. Pi: "\u03A0",
  3846. Rho: "\u03A1",
  3847. Sigma: "\u03A3",
  3848. Tau: "\u03A4",
  3849. Upsilon: "\u03A5",
  3850. Phi: "\u03A6",
  3851. Chi: "\u03A7",
  3852. Psi: "\u03A8",
  3853. Omega: "\u03A9",
  3854. alpha: "\u03B1",
  3855. beta: "\u03B2",
  3856. gamma: "\u03B3",
  3857. delta: "\u03B4",
  3858. epsilon: "\u03B5",
  3859. zeta: "\u03B6",
  3860. eta: "\u03B7",
  3861. theta: "\u03B8",
  3862. iota: "\u03B9",
  3863. kappa: "\u03BA",
  3864. lambda: "\u03BB",
  3865. mu: "\u03BC",
  3866. nu: "\u03BD",
  3867. xi: "\u03BE",
  3868. omicron: "\u03BF",
  3869. pi: "\u03C0",
  3870. rho: "\u03C1",
  3871. sigmaf: "\u03C2",
  3872. sigma: "\u03C3",
  3873. tau: "\u03C4",
  3874. upsilon: "\u03C5",
  3875. phi: "\u03C6",
  3876. chi: "\u03C7",
  3877. psi: "\u03C8",
  3878. omega: "\u03C9",
  3879. thetasym: "\u03D1",
  3880. upsih: "\u03D2",
  3881. piv: "\u03D6",
  3882. ensp: "\u2002",
  3883. emsp: "\u2003",
  3884. thinsp: "\u2009",
  3885. zwnj: "\u200C",
  3886. zwj: "\u200D",
  3887. lrm: "\u200E",
  3888. rlm: "\u200F",
  3889. ndash: "\u2013",
  3890. mdash: "\u2014",
  3891. lsquo: "\u2018",
  3892. rsquo: "\u2019",
  3893. sbquo: "\u201A",
  3894. ldquo: "\u201C",
  3895. rdquo: "\u201D",
  3896. bdquo: "\u201E",
  3897. dagger: "\u2020",
  3898. Dagger: "\u2021",
  3899. bull: "\u2022",
  3900. hellip: "\u2026",
  3901. permil: "\u2030",
  3902. prime: "\u2032",
  3903. Prime: "\u2033",
  3904. lsaquo: "\u2039",
  3905. rsaquo: "\u203A",
  3906. oline: "\u203E",
  3907. frasl: "\u2044",
  3908. euro: "\u20AC",
  3909. image: "\u2111",
  3910. weierp: "\u2118",
  3911. real: "\u211C",
  3912. trade: "\u2122",
  3913. alefsym: "\u2135",
  3914. larr: "\u2190",
  3915. uarr: "\u2191",
  3916. rarr: "\u2192",
  3917. darr: "\u2193",
  3918. harr: "\u2194",
  3919. crarr: "\u21B5",
  3920. lArr: "\u21D0",
  3921. uArr: "\u21D1",
  3922. rArr: "\u21D2",
  3923. dArr: "\u21D3",
  3924. hArr: "\u21D4",
  3925. forall: "\u2200",
  3926. part: "\u2202",
  3927. exist: "\u2203",
  3928. empty: "\u2205",
  3929. nabla: "\u2207",
  3930. isin: "\u2208",
  3931. notin: "\u2209",
  3932. ni: "\u220B",
  3933. prod: "\u220F",
  3934. sum: "\u2211",
  3935. minus: "\u2212",
  3936. lowast: "\u2217",
  3937. radic: "\u221A",
  3938. prop: "\u221D",
  3939. infin: "\u221E",
  3940. ang: "\u2220",
  3941. and: "\u2227",
  3942. or: "\u2228",
  3943. cap: "\u2229",
  3944. cup: "\u222A",
  3945. int: "\u222B",
  3946. there4: "\u2234",
  3947. sim: "\u223C",
  3948. cong: "\u2245",
  3949. asymp: "\u2248",
  3950. ne: "\u2260",
  3951. equiv: "\u2261",
  3952. le: "\u2264",
  3953. ge: "\u2265",
  3954. sub: "\u2282",
  3955. sup: "\u2283",
  3956. nsub: "\u2284",
  3957. sube: "\u2286",
  3958. supe: "\u2287",
  3959. oplus: "\u2295",
  3960. otimes: "\u2297",
  3961. perp: "\u22A5",
  3962. sdot: "\u22C5",
  3963. lceil: "\u2308",
  3964. rceil: "\u2309",
  3965. lfloor: "\u230A",
  3966. rfloor: "\u230B",
  3967. lang: "\u2329",
  3968. rang: "\u232A",
  3969. loz: "\u25CA",
  3970. spades: "\u2660",
  3971. clubs: "\u2663",
  3972. hearts: "\u2665",
  3973. diams: "\u2666"
  3974. };
  3975. class State {
  3976. constructor() {
  3977. this.strict = void 0;
  3978. this.curLine = void 0;
  3979. this.startLoc = void 0;
  3980. this.endLoc = void 0;
  3981. this.errors = [];
  3982. this.potentialArrowAt = -1;
  3983. this.noArrowAt = [];
  3984. this.noArrowParamsConversionAt = [];
  3985. this.maybeInArrowParameters = false;
  3986. this.inPipeline = false;
  3987. this.inType = false;
  3988. this.noAnonFunctionType = false;
  3989. this.inPropertyName = false;
  3990. this.hasFlowComment = false;
  3991. this.isAmbientContext = false;
  3992. this.inAbstractClass = false;
  3993. this.topicContext = {
  3994. maxNumOfResolvableTopics: 0,
  3995. maxTopicIndex: null
  3996. };
  3997. this.soloAwait = false;
  3998. this.inFSharpPipelineDirectBody = false;
  3999. this.labels = [];
  4000. this.decoratorStack = [[]];
  4001. this.comments = [];
  4002. this.trailingComments = [];
  4003. this.leadingComments = [];
  4004. this.commentStack = [];
  4005. this.commentPreviousNode = null;
  4006. this.pos = 0;
  4007. this.lineStart = 0;
  4008. this.type = types$1.eof;
  4009. this.value = null;
  4010. this.start = 0;
  4011. this.end = 0;
  4012. this.lastTokEndLoc = null;
  4013. this.lastTokStartLoc = null;
  4014. this.lastTokStart = 0;
  4015. this.lastTokEnd = 0;
  4016. this.context = [types.brace];
  4017. this.exprAllowed = true;
  4018. this.containsEsc = false;
  4019. this.strictErrors = new Map();
  4020. this.tokensLength = 0;
  4021. }
  4022. init(options) {
  4023. this.strict = options.strictMode === false ? false : options.sourceType === "module";
  4024. this.curLine = options.startLine;
  4025. this.startLoc = this.endLoc = this.curPosition();
  4026. }
  4027. curPosition() {
  4028. return new Position(this.curLine, this.pos - this.lineStart);
  4029. }
  4030. clone(skipArrays) {
  4031. const state = new State();
  4032. const keys = Object.keys(this);
  4033. for (let i = 0, length = keys.length; i < length; i++) {
  4034. const key = keys[i];
  4035. let val = this[key];
  4036. if (!skipArrays && Array.isArray(val)) {
  4037. val = val.slice();
  4038. }
  4039. state[key] = val;
  4040. }
  4041. return state;
  4042. }
  4043. }
  4044. const HEX_NUMBER = /^[\da-fA-F]+$/;
  4045. const DECIMAL_NUMBER = /^\d+$/;
  4046. const JsxErrors = makeErrorTemplates({
  4047. AttributeIsEmpty: "JSX attributes must only be assigned a non-empty expression.",
  4048. MissingClosingTagElement: "Expected corresponding JSX closing tag for <%0>.",
  4049. MissingClosingTagFragment: "Expected corresponding JSX closing tag for <>.",
  4050. UnexpectedSequenceExpression: "Sequence expressions cannot be directly nested inside JSX. Did you mean to wrap it in parentheses (...)?",
  4051. UnsupportedJsxValue: "JSX value should be either an expression or a quoted JSX text.",
  4052. UnterminatedJsxContent: "Unterminated JSX contents.",
  4053. UnwrappedAdjacentJSXElements: "Adjacent JSX elements must be wrapped in an enclosing tag. Did you want a JSX fragment <>...</>?"
  4054. }, ErrorCodes.SyntaxError);
  4055. types.j_oTag = new TokContext("<tag");
  4056. types.j_cTag = new TokContext("</tag");
  4057. types.j_expr = new TokContext("<tag>...</tag>", true);
  4058. types$1.jsxName = new TokenType("jsxName");
  4059. types$1.jsxText = new TokenType("jsxText", {
  4060. beforeExpr: true
  4061. });
  4062. types$1.jsxTagStart = new TokenType("jsxTagStart", {
  4063. startsExpr: true
  4064. });
  4065. types$1.jsxTagEnd = new TokenType("jsxTagEnd");
  4066. types$1.jsxTagStart.updateContext = context => {
  4067. context.push(types.j_expr, types.j_oTag);
  4068. };
  4069. function isFragment(object) {
  4070. return object ? object.type === "JSXOpeningFragment" || object.type === "JSXClosingFragment" : false;
  4071. }
  4072. function getQualifiedJSXName(object) {
  4073. if (object.type === "JSXIdentifier") {
  4074. return object.name;
  4075. }
  4076. if (object.type === "JSXNamespacedName") {
  4077. return object.namespace.name + ":" + object.name.name;
  4078. }
  4079. if (object.type === "JSXMemberExpression") {
  4080. return getQualifiedJSXName(object.object) + "." + getQualifiedJSXName(object.property);
  4081. }
  4082. throw new Error("Node had unexpected type: " + object.type);
  4083. }
  4084. var jsx = (superClass => class extends superClass {
  4085. jsxReadToken() {
  4086. let out = "";
  4087. let chunkStart = this.state.pos;
  4088. for (;;) {
  4089. if (this.state.pos >= this.length) {
  4090. throw this.raise(this.state.start, JsxErrors.UnterminatedJsxContent);
  4091. }
  4092. const ch = this.input.charCodeAt(this.state.pos);
  4093. switch (ch) {
  4094. case 60:
  4095. case 123:
  4096. if (this.state.pos === this.state.start) {
  4097. if (ch === 60 && this.state.exprAllowed) {
  4098. ++this.state.pos;
  4099. return this.finishToken(types$1.jsxTagStart);
  4100. }
  4101. return super.getTokenFromCode(ch);
  4102. }
  4103. out += this.input.slice(chunkStart, this.state.pos);
  4104. return this.finishToken(types$1.jsxText, out);
  4105. case 38:
  4106. out += this.input.slice(chunkStart, this.state.pos);
  4107. out += this.jsxReadEntity();
  4108. chunkStart = this.state.pos;
  4109. break;
  4110. case 62:
  4111. case 125:
  4112. default:
  4113. if (isNewLine(ch)) {
  4114. out += this.input.slice(chunkStart, this.state.pos);
  4115. out += this.jsxReadNewLine(true);
  4116. chunkStart = this.state.pos;
  4117. } else {
  4118. ++this.state.pos;
  4119. }
  4120. }
  4121. }
  4122. }
  4123. jsxReadNewLine(normalizeCRLF) {
  4124. const ch = this.input.charCodeAt(this.state.pos);
  4125. let out;
  4126. ++this.state.pos;
  4127. if (ch === 13 && this.input.charCodeAt(this.state.pos) === 10) {
  4128. ++this.state.pos;
  4129. out = normalizeCRLF ? "\n" : "\r\n";
  4130. } else {
  4131. out = String.fromCharCode(ch);
  4132. }
  4133. ++this.state.curLine;
  4134. this.state.lineStart = this.state.pos;
  4135. return out;
  4136. }
  4137. jsxReadString(quote) {
  4138. let out = "";
  4139. let chunkStart = ++this.state.pos;
  4140. for (;;) {
  4141. if (this.state.pos >= this.length) {
  4142. throw this.raise(this.state.start, ErrorMessages.UnterminatedString);
  4143. }
  4144. const ch = this.input.charCodeAt(this.state.pos);
  4145. if (ch === quote) break;
  4146. if (ch === 38) {
  4147. out += this.input.slice(chunkStart, this.state.pos);
  4148. out += this.jsxReadEntity();
  4149. chunkStart = this.state.pos;
  4150. } else if (isNewLine(ch)) {
  4151. out += this.input.slice(chunkStart, this.state.pos);
  4152. out += this.jsxReadNewLine(false);
  4153. chunkStart = this.state.pos;
  4154. } else {
  4155. ++this.state.pos;
  4156. }
  4157. }
  4158. out += this.input.slice(chunkStart, this.state.pos++);
  4159. return this.finishToken(types$1.string, out);
  4160. }
  4161. jsxReadEntity() {
  4162. let str = "";
  4163. let count = 0;
  4164. let entity;
  4165. let ch = this.input[this.state.pos];
  4166. const startPos = ++this.state.pos;
  4167. while (this.state.pos < this.length && count++ < 10) {
  4168. ch = this.input[this.state.pos++];
  4169. if (ch === ";") {
  4170. if (str[0] === "#") {
  4171. if (str[1] === "x") {
  4172. str = str.substr(2);
  4173. if (HEX_NUMBER.test(str)) {
  4174. entity = String.fromCodePoint(parseInt(str, 16));
  4175. }
  4176. } else {
  4177. str = str.substr(1);
  4178. if (DECIMAL_NUMBER.test(str)) {
  4179. entity = String.fromCodePoint(parseInt(str, 10));
  4180. }
  4181. }
  4182. } else {
  4183. entity = entities[str];
  4184. }
  4185. break;
  4186. }
  4187. str += ch;
  4188. }
  4189. if (!entity) {
  4190. this.state.pos = startPos;
  4191. return "&";
  4192. }
  4193. return entity;
  4194. }
  4195. jsxReadWord() {
  4196. let ch;
  4197. const start = this.state.pos;
  4198. do {
  4199. ch = this.input.charCodeAt(++this.state.pos);
  4200. } while (isIdentifierChar(ch) || ch === 45);
  4201. return this.finishToken(types$1.jsxName, this.input.slice(start, this.state.pos));
  4202. }
  4203. jsxParseIdentifier() {
  4204. const node = this.startNode();
  4205. if (this.match(types$1.jsxName)) {
  4206. node.name = this.state.value;
  4207. } else if (this.state.type.keyword) {
  4208. node.name = this.state.type.keyword;
  4209. } else {
  4210. this.unexpected();
  4211. }
  4212. this.next();
  4213. return this.finishNode(node, "JSXIdentifier");
  4214. }
  4215. jsxParseNamespacedName() {
  4216. const startPos = this.state.start;
  4217. const startLoc = this.state.startLoc;
  4218. const name = this.jsxParseIdentifier();
  4219. if (!this.eat(types$1.colon)) return name;
  4220. const node = this.startNodeAt(startPos, startLoc);
  4221. node.namespace = name;
  4222. node.name = this.jsxParseIdentifier();
  4223. return this.finishNode(node, "JSXNamespacedName");
  4224. }
  4225. jsxParseElementName() {
  4226. const startPos = this.state.start;
  4227. const startLoc = this.state.startLoc;
  4228. let node = this.jsxParseNamespacedName();
  4229. if (node.type === "JSXNamespacedName") {
  4230. return node;
  4231. }
  4232. while (this.eat(types$1.dot)) {
  4233. const newNode = this.startNodeAt(startPos, startLoc);
  4234. newNode.object = node;
  4235. newNode.property = this.jsxParseIdentifier();
  4236. node = this.finishNode(newNode, "JSXMemberExpression");
  4237. }
  4238. return node;
  4239. }
  4240. jsxParseAttributeValue() {
  4241. let node;
  4242. switch (this.state.type) {
  4243. case types$1.braceL:
  4244. node = this.startNode();
  4245. this.next();
  4246. node = this.jsxParseExpressionContainer(node);
  4247. if (node.expression.type === "JSXEmptyExpression") {
  4248. this.raise(node.start, JsxErrors.AttributeIsEmpty);
  4249. }
  4250. return node;
  4251. case types$1.jsxTagStart:
  4252. case types$1.string:
  4253. return this.parseExprAtom();
  4254. default:
  4255. throw this.raise(this.state.start, JsxErrors.UnsupportedJsxValue);
  4256. }
  4257. }
  4258. jsxParseEmptyExpression() {
  4259. const node = this.startNodeAt(this.state.lastTokEnd, this.state.lastTokEndLoc);
  4260. return this.finishNodeAt(node, "JSXEmptyExpression", this.state.start, this.state.startLoc);
  4261. }
  4262. jsxParseSpreadChild(node) {
  4263. this.next();
  4264. node.expression = this.parseExpression();
  4265. this.expect(types$1.braceR);
  4266. return this.finishNode(node, "JSXSpreadChild");
  4267. }
  4268. jsxParseExpressionContainer(node) {
  4269. if (this.match(types$1.braceR)) {
  4270. node.expression = this.jsxParseEmptyExpression();
  4271. } else {
  4272. const expression = this.parseExpression();
  4273. node.expression = expression;
  4274. }
  4275. this.expect(types$1.braceR);
  4276. return this.finishNode(node, "JSXExpressionContainer");
  4277. }
  4278. jsxParseAttribute() {
  4279. const node = this.startNode();
  4280. if (this.eat(types$1.braceL)) {
  4281. this.expect(types$1.ellipsis);
  4282. node.argument = this.parseMaybeAssignAllowIn();
  4283. this.expect(types$1.braceR);
  4284. return this.finishNode(node, "JSXSpreadAttribute");
  4285. }
  4286. node.name = this.jsxParseNamespacedName();
  4287. node.value = this.eat(types$1.eq) ? this.jsxParseAttributeValue() : null;
  4288. return this.finishNode(node, "JSXAttribute");
  4289. }
  4290. jsxParseOpeningElementAt(startPos, startLoc) {
  4291. const node = this.startNodeAt(startPos, startLoc);
  4292. if (this.match(types$1.jsxTagEnd)) {
  4293. this.expect(types$1.jsxTagEnd);
  4294. return this.finishNode(node, "JSXOpeningFragment");
  4295. }
  4296. node.name = this.jsxParseElementName();
  4297. return this.jsxParseOpeningElementAfterName(node);
  4298. }
  4299. jsxParseOpeningElementAfterName(node) {
  4300. const attributes = [];
  4301. while (!this.match(types$1.slash) && !this.match(types$1.jsxTagEnd)) {
  4302. attributes.push(this.jsxParseAttribute());
  4303. }
  4304. node.attributes = attributes;
  4305. node.selfClosing = this.eat(types$1.slash);
  4306. this.expect(types$1.jsxTagEnd);
  4307. return this.finishNode(node, "JSXOpeningElement");
  4308. }
  4309. jsxParseClosingElementAt(startPos, startLoc) {
  4310. const node = this.startNodeAt(startPos, startLoc);
  4311. if (this.match(types$1.jsxTagEnd)) {
  4312. this.expect(types$1.jsxTagEnd);
  4313. return this.finishNode(node, "JSXClosingFragment");
  4314. }
  4315. node.name = this.jsxParseElementName();
  4316. this.expect(types$1.jsxTagEnd);
  4317. return this.finishNode(node, "JSXClosingElement");
  4318. }
  4319. jsxParseElementAt(startPos, startLoc) {
  4320. const node = this.startNodeAt(startPos, startLoc);
  4321. const children = [];
  4322. const openingElement = this.jsxParseOpeningElementAt(startPos, startLoc);
  4323. let closingElement = null;
  4324. if (!openingElement.selfClosing) {
  4325. contents: for (;;) {
  4326. switch (this.state.type) {
  4327. case types$1.jsxTagStart:
  4328. startPos = this.state.start;
  4329. startLoc = this.state.startLoc;
  4330. this.next();
  4331. if (this.eat(types$1.slash)) {
  4332. closingElement = this.jsxParseClosingElementAt(startPos, startLoc);
  4333. break contents;
  4334. }
  4335. children.push(this.jsxParseElementAt(startPos, startLoc));
  4336. break;
  4337. case types$1.jsxText:
  4338. children.push(this.parseExprAtom());
  4339. break;
  4340. case types$1.braceL:
  4341. {
  4342. const node = this.startNode();
  4343. this.next();
  4344. if (this.match(types$1.ellipsis)) {
  4345. children.push(this.jsxParseSpreadChild(node));
  4346. } else {
  4347. children.push(this.jsxParseExpressionContainer(node));
  4348. }
  4349. break;
  4350. }
  4351. default:
  4352. throw this.unexpected();
  4353. }
  4354. }
  4355. if (isFragment(openingElement) && !isFragment(closingElement)) {
  4356. this.raise(closingElement.start, JsxErrors.MissingClosingTagFragment);
  4357. } else if (!isFragment(openingElement) && isFragment(closingElement)) {
  4358. this.raise(closingElement.start, JsxErrors.MissingClosingTagElement, getQualifiedJSXName(openingElement.name));
  4359. } else if (!isFragment(openingElement) && !isFragment(closingElement)) {
  4360. if (getQualifiedJSXName(closingElement.name) !== getQualifiedJSXName(openingElement.name)) {
  4361. this.raise(closingElement.start, JsxErrors.MissingClosingTagElement, getQualifiedJSXName(openingElement.name));
  4362. }
  4363. }
  4364. }
  4365. if (isFragment(openingElement)) {
  4366. node.openingFragment = openingElement;
  4367. node.closingFragment = closingElement;
  4368. } else {
  4369. node.openingElement = openingElement;
  4370. node.closingElement = closingElement;
  4371. }
  4372. node.children = children;
  4373. if (this.isRelational("<")) {
  4374. throw this.raise(this.state.start, JsxErrors.UnwrappedAdjacentJSXElements);
  4375. }
  4376. return isFragment(openingElement) ? this.finishNode(node, "JSXFragment") : this.finishNode(node, "JSXElement");
  4377. }
  4378. jsxParseElement() {
  4379. const startPos = this.state.start;
  4380. const startLoc = this.state.startLoc;
  4381. this.next();
  4382. return this.jsxParseElementAt(startPos, startLoc);
  4383. }
  4384. parseExprAtom(refExpressionErrors) {
  4385. if (this.match(types$1.jsxText)) {
  4386. return this.parseLiteral(this.state.value, "JSXText");
  4387. } else if (this.match(types$1.jsxTagStart)) {
  4388. return this.jsxParseElement();
  4389. } else if (this.isRelational("<") && this.input.charCodeAt(this.state.pos) !== 33) {
  4390. this.finishToken(types$1.jsxTagStart);
  4391. return this.jsxParseElement();
  4392. } else {
  4393. return super.parseExprAtom(refExpressionErrors);
  4394. }
  4395. }
  4396. createLookaheadState(state) {
  4397. const lookaheadState = super.createLookaheadState(state);
  4398. lookaheadState.inPropertyName = state.inPropertyName;
  4399. return lookaheadState;
  4400. }
  4401. getTokenFromCode(code) {
  4402. if (this.state.inPropertyName) return super.getTokenFromCode(code);
  4403. const context = this.curContext();
  4404. if (context === types.j_expr) {
  4405. return this.jsxReadToken();
  4406. }
  4407. if (context === types.j_oTag || context === types.j_cTag) {
  4408. if (isIdentifierStart(code)) {
  4409. return this.jsxReadWord();
  4410. }
  4411. if (code === 62) {
  4412. ++this.state.pos;
  4413. return this.finishToken(types$1.jsxTagEnd);
  4414. }
  4415. if ((code === 34 || code === 39) && context === types.j_oTag) {
  4416. return this.jsxReadString(code);
  4417. }
  4418. }
  4419. if (code === 60 && this.state.exprAllowed && this.input.charCodeAt(this.state.pos + 1) !== 33) {
  4420. ++this.state.pos;
  4421. return this.finishToken(types$1.jsxTagStart);
  4422. }
  4423. return super.getTokenFromCode(code);
  4424. }
  4425. updateContext(prevType) {
  4426. super.updateContext(prevType);
  4427. const {
  4428. context,
  4429. type
  4430. } = this.state;
  4431. if (type === types$1.slash && prevType === types$1.jsxTagStart) {
  4432. context.splice(-2, 2, types.j_cTag);
  4433. this.state.exprAllowed = false;
  4434. } else if (type === types$1.jsxTagEnd) {
  4435. const out = context.pop();
  4436. if (out === types.j_oTag && prevType === types$1.slash || out === types.j_cTag) {
  4437. context.pop();
  4438. this.state.exprAllowed = context[context.length - 1] === types.j_expr;
  4439. } else {
  4440. this.state.exprAllowed = true;
  4441. }
  4442. } else if (type.keyword && (prevType === types$1.dot || prevType === types$1.questionDot)) {
  4443. this.state.exprAllowed = false;
  4444. } else {
  4445. this.state.exprAllowed = type.beforeExpr;
  4446. }
  4447. }
  4448. });
  4449. class TypeScriptScope extends Scope {
  4450. constructor(...args) {
  4451. super(...args);
  4452. this.types = new Set();
  4453. this.enums = new Set();
  4454. this.constEnums = new Set();
  4455. this.classes = new Set();
  4456. this.exportOnlyBindings = new Set();
  4457. }
  4458. }
  4459. class TypeScriptScopeHandler extends ScopeHandler {
  4460. createScope(flags) {
  4461. return new TypeScriptScope(flags);
  4462. }
  4463. declareName(name, bindingType, pos) {
  4464. const scope = this.currentScope();
  4465. if (bindingType & BIND_FLAGS_TS_EXPORT_ONLY) {
  4466. this.maybeExportDefined(scope, name);
  4467. scope.exportOnlyBindings.add(name);
  4468. return;
  4469. }
  4470. super.declareName(...arguments);
  4471. if (bindingType & BIND_KIND_TYPE) {
  4472. if (!(bindingType & BIND_KIND_VALUE)) {
  4473. this.checkRedeclarationInScope(scope, name, bindingType, pos);
  4474. this.maybeExportDefined(scope, name);
  4475. }
  4476. scope.types.add(name);
  4477. }
  4478. if (bindingType & BIND_FLAGS_TS_ENUM) scope.enums.add(name);
  4479. if (bindingType & BIND_FLAGS_TS_CONST_ENUM) scope.constEnums.add(name);
  4480. if (bindingType & BIND_FLAGS_CLASS) scope.classes.add(name);
  4481. }
  4482. isRedeclaredInScope(scope, name, bindingType) {
  4483. if (scope.enums.has(name)) {
  4484. if (bindingType & BIND_FLAGS_TS_ENUM) {
  4485. const isConst = !!(bindingType & BIND_FLAGS_TS_CONST_ENUM);
  4486. const wasConst = scope.constEnums.has(name);
  4487. return isConst !== wasConst;
  4488. }
  4489. return true;
  4490. }
  4491. if (bindingType & BIND_FLAGS_CLASS && scope.classes.has(name)) {
  4492. if (scope.lexical.has(name)) {
  4493. return !!(bindingType & BIND_KIND_VALUE);
  4494. } else {
  4495. return false;
  4496. }
  4497. }
  4498. if (bindingType & BIND_KIND_TYPE && scope.types.has(name)) {
  4499. return true;
  4500. }
  4501. return super.isRedeclaredInScope(...arguments);
  4502. }
  4503. checkLocalExport(id) {
  4504. const topLevelScope = this.scopeStack[0];
  4505. const {
  4506. name
  4507. } = id;
  4508. if (!topLevelScope.types.has(name) && !topLevelScope.exportOnlyBindings.has(name)) {
  4509. super.checkLocalExport(id);
  4510. }
  4511. }
  4512. }
  4513. const PARAM = 0b0000,
  4514. PARAM_YIELD = 0b0001,
  4515. PARAM_AWAIT = 0b0010,
  4516. PARAM_RETURN = 0b0100,
  4517. PARAM_IN = 0b1000;
  4518. class ProductionParameterHandler {
  4519. constructor() {
  4520. this.stacks = [];
  4521. }
  4522. enter(flags) {
  4523. this.stacks.push(flags);
  4524. }
  4525. exit() {
  4526. this.stacks.pop();
  4527. }
  4528. currentFlags() {
  4529. return this.stacks[this.stacks.length - 1];
  4530. }
  4531. get hasAwait() {
  4532. return (this.currentFlags() & PARAM_AWAIT) > 0;
  4533. }
  4534. get hasYield() {
  4535. return (this.currentFlags() & PARAM_YIELD) > 0;
  4536. }
  4537. get hasReturn() {
  4538. return (this.currentFlags() & PARAM_RETURN) > 0;
  4539. }
  4540. get hasIn() {
  4541. return (this.currentFlags() & PARAM_IN) > 0;
  4542. }
  4543. }
  4544. function functionFlags(isAsync, isGenerator) {
  4545. return (isAsync ? PARAM_AWAIT : 0) | (isGenerator ? PARAM_YIELD : 0);
  4546. }
  4547. function nonNull(x) {
  4548. if (x == null) {
  4549. throw new Error(`Unexpected ${x} value.`);
  4550. }
  4551. return x;
  4552. }
  4553. function assert(x) {
  4554. if (!x) {
  4555. throw new Error("Assert fail");
  4556. }
  4557. }
  4558. const TSErrors = makeErrorTemplates({
  4559. AbstractMethodHasImplementation: "Method '%0' cannot have an implementation because it is marked abstract.",
  4560. AccesorCannotDeclareThisParameter: "'get' and 'set' accessors cannot declare 'this' parameters.",
  4561. AccesorCannotHaveTypeParameters: "An accessor cannot have type parameters.",
  4562. ClassMethodHasDeclare: "Class methods cannot have the 'declare' modifier.",
  4563. ClassMethodHasReadonly: "Class methods cannot have the 'readonly' modifier.",
  4564. ConstructorHasTypeParameters: "Type parameters cannot appear on a constructor declaration.",
  4565. DeclareAccessor: "'declare' is not allowed in %0ters.",
  4566. DeclareClassFieldHasInitializer: "Initializers are not allowed in ambient contexts.",
  4567. DeclareFunctionHasImplementation: "An implementation cannot be declared in ambient contexts.",
  4568. DuplicateAccessibilityModifier: "Accessibility modifier already seen.",
  4569. DuplicateModifier: "Duplicate modifier: '%0'.",
  4570. EmptyHeritageClauseType: "'%0' list cannot be empty.",
  4571. EmptyTypeArguments: "Type argument list cannot be empty.",
  4572. EmptyTypeParameters: "Type parameter list cannot be empty.",
  4573. ExpectedAmbientAfterExportDeclare: "'export declare' must be followed by an ambient declaration.",
  4574. ImportAliasHasImportType: "An import alias can not use 'import type'.",
  4575. IncompatibleModifiers: "'%0' modifier cannot be used with '%1' modifier.",
  4576. IndexSignatureHasAbstract: "Index signatures cannot have the 'abstract' modifier.",
  4577. IndexSignatureHasAccessibility: "Index signatures cannot have an accessibility modifier ('%0').",
  4578. IndexSignatureHasDeclare: "Index signatures cannot have the 'declare' modifier.",
  4579. IndexSignatureHasOverride: "'override' modifier cannot appear on an index signature.",
  4580. IndexSignatureHasStatic: "Index signatures cannot have the 'static' modifier.",
  4581. InvalidModifierOnTypeMember: "'%0' modifier cannot appear on a type member.",
  4582. InvalidModifiersOrder: "'%0' modifier must precede '%1' modifier.",
  4583. InvalidTupleMemberLabel: "Tuple members must be labeled with a simple identifier.",
  4584. MixedLabeledAndUnlabeledElements: "Tuple members must all have names or all not have names.",
  4585. NonAbstractClassHasAbstractMethod: "Abstract methods can only appear within an abstract class.",
  4586. NonClassMethodPropertyHasAbstractModifer: "'abstract' modifier can only appear on a class, method, or property declaration.",
  4587. OptionalTypeBeforeRequired: "A required element cannot follow an optional element.",
  4588. OverrideNotInSubClass: "This member cannot have an 'override' modifier because its containing class does not extend another class.",
  4589. PatternIsOptional: "A binding pattern parameter cannot be optional in an implementation signature.",
  4590. PrivateElementHasAbstract: "Private elements cannot have the 'abstract' modifier.",
  4591. PrivateElementHasAccessibility: "Private elements cannot have an accessibility modifier ('%0').",
  4592. ReadonlyForMethodSignature: "'readonly' modifier can only appear on a property declaration or index signature.",
  4593. SetAccesorCannotHaveOptionalParameter: "A 'set' accessor cannot have an optional parameter.",
  4594. SetAccesorCannotHaveRestParameter: "A 'set' accessor cannot have rest parameter.",
  4595. SetAccesorCannotHaveReturnType: "A 'set' accessor cannot have a return type annotation.",
  4596. StaticBlockCannotHaveModifier: "Static class blocks cannot have any modifier.",
  4597. TypeAnnotationAfterAssign: "Type annotations must come before default assignments, e.g. instead of `age = 25: number` use `age: number = 25`.",
  4598. TypeImportCannotSpecifyDefaultAndNamed: "A type-only import can specify a default import or named bindings, but not both.",
  4599. UnexpectedParameterModifier: "A parameter property is only allowed in a constructor implementation.",
  4600. UnexpectedReadonly: "'readonly' type modifier is only permitted on array and tuple literal types.",
  4601. UnexpectedTypeAnnotation: "Did not expect a type annotation here.",
  4602. UnexpectedTypeCastInParameter: "Unexpected type cast in parameter position.",
  4603. UnsupportedImportTypeArgument: "Argument in a type import must be a string literal.",
  4604. UnsupportedParameterPropertyKind: "A parameter property may not be declared using a binding pattern.",
  4605. UnsupportedSignatureParameterKind: "Name in a signature must be an Identifier, ObjectPattern or ArrayPattern, instead got %0."
  4606. }, ErrorCodes.SyntaxError);
  4607. function keywordTypeFromName(value) {
  4608. switch (value) {
  4609. case "any":
  4610. return "TSAnyKeyword";
  4611. case "boolean":
  4612. return "TSBooleanKeyword";
  4613. case "bigint":
  4614. return "TSBigIntKeyword";
  4615. case "never":
  4616. return "TSNeverKeyword";
  4617. case "number":
  4618. return "TSNumberKeyword";
  4619. case "object":
  4620. return "TSObjectKeyword";
  4621. case "string":
  4622. return "TSStringKeyword";
  4623. case "symbol":
  4624. return "TSSymbolKeyword";
  4625. case "undefined":
  4626. return "TSUndefinedKeyword";
  4627. case "unknown":
  4628. return "TSUnknownKeyword";
  4629. default:
  4630. return undefined;
  4631. }
  4632. }
  4633. function tsIsAccessModifier(modifier) {
  4634. return modifier === "private" || modifier === "public" || modifier === "protected";
  4635. }
  4636. var typescript = (superClass => class extends superClass {
  4637. getScopeHandler() {
  4638. return TypeScriptScopeHandler;
  4639. }
  4640. tsIsIdentifier() {
  4641. return this.match(types$1.name);
  4642. }
  4643. tsTokenCanFollowModifier() {
  4644. return (this.match(types$1.bracketL) || this.match(types$1.braceL) || this.match(types$1.star) || this.match(types$1.ellipsis) || this.match(types$1.privateName) || this.isLiteralPropertyName()) && !this.hasPrecedingLineBreak();
  4645. }
  4646. tsNextTokenCanFollowModifier() {
  4647. this.next();
  4648. return this.tsTokenCanFollowModifier();
  4649. }
  4650. tsParseModifier(allowedModifiers) {
  4651. if (!this.match(types$1.name)) {
  4652. return undefined;
  4653. }
  4654. const modifier = this.state.value;
  4655. if (allowedModifiers.indexOf(modifier) !== -1 && this.tsTryParse(this.tsNextTokenCanFollowModifier.bind(this))) {
  4656. return modifier;
  4657. }
  4658. return undefined;
  4659. }
  4660. tsParseModifiers(modified, allowedModifiers, disallowedModifiers, errorTemplate) {
  4661. const enforceOrder = (pos, modifier, before, after) => {
  4662. if (modifier === before && modified[after]) {
  4663. this.raise(pos, TSErrors.InvalidModifiersOrder, before, after);
  4664. }
  4665. };
  4666. const incompatible = (pos, modifier, mod1, mod2) => {
  4667. if (modified[mod1] && modifier === mod2 || modified[mod2] && modifier === mod1) {
  4668. this.raise(pos, TSErrors.IncompatibleModifiers, mod1, mod2);
  4669. }
  4670. };
  4671. for (;;) {
  4672. const startPos = this.state.start;
  4673. const modifier = this.tsParseModifier(allowedModifiers.concat(disallowedModifiers != null ? disallowedModifiers : []));
  4674. if (!modifier) break;
  4675. if (tsIsAccessModifier(modifier)) {
  4676. if (modified.accessibility) {
  4677. this.raise(startPos, TSErrors.DuplicateAccessibilityModifier);
  4678. } else {
  4679. enforceOrder(startPos, modifier, modifier, "override");
  4680. enforceOrder(startPos, modifier, modifier, "static");
  4681. enforceOrder(startPos, modifier, modifier, "readonly");
  4682. modified.accessibility = modifier;
  4683. }
  4684. } else {
  4685. if (Object.hasOwnProperty.call(modified, modifier)) {
  4686. this.raise(startPos, TSErrors.DuplicateModifier, modifier);
  4687. } else {
  4688. enforceOrder(startPos, modifier, "static", "readonly");
  4689. enforceOrder(startPos, modifier, "static", "override");
  4690. enforceOrder(startPos, modifier, "override", "readonly");
  4691. enforceOrder(startPos, modifier, "abstract", "override");
  4692. incompatible(startPos, modifier, "declare", "override");
  4693. incompatible(startPos, modifier, "static", "abstract");
  4694. }
  4695. modified[modifier] = true;
  4696. }
  4697. if (disallowedModifiers != null && disallowedModifiers.includes(modifier)) {
  4698. this.raise(startPos, errorTemplate, modifier);
  4699. }
  4700. }
  4701. }
  4702. tsIsListTerminator(kind) {
  4703. switch (kind) {
  4704. case "EnumMembers":
  4705. case "TypeMembers":
  4706. return this.match(types$1.braceR);
  4707. case "HeritageClauseElement":
  4708. return this.match(types$1.braceL);
  4709. case "TupleElementTypes":
  4710. return this.match(types$1.bracketR);
  4711. case "TypeParametersOrArguments":
  4712. return this.isRelational(">");
  4713. }
  4714. throw new Error("Unreachable");
  4715. }
  4716. tsParseList(kind, parseElement) {
  4717. const result = [];
  4718. while (!this.tsIsListTerminator(kind)) {
  4719. result.push(parseElement());
  4720. }
  4721. return result;
  4722. }
  4723. tsParseDelimitedList(kind, parseElement) {
  4724. return nonNull(this.tsParseDelimitedListWorker(kind, parseElement, true));
  4725. }
  4726. tsParseDelimitedListWorker(kind, parseElement, expectSuccess) {
  4727. const result = [];
  4728. for (;;) {
  4729. if (this.tsIsListTerminator(kind)) {
  4730. break;
  4731. }
  4732. const element = parseElement();
  4733. if (element == null) {
  4734. return undefined;
  4735. }
  4736. result.push(element);
  4737. if (this.eat(types$1.comma)) {
  4738. continue;
  4739. }
  4740. if (this.tsIsListTerminator(kind)) {
  4741. break;
  4742. }
  4743. if (expectSuccess) {
  4744. this.expect(types$1.comma);
  4745. }
  4746. return undefined;
  4747. }
  4748. return result;
  4749. }
  4750. tsParseBracketedList(kind, parseElement, bracket, skipFirstToken) {
  4751. if (!skipFirstToken) {
  4752. if (bracket) {
  4753. this.expect(types$1.bracketL);
  4754. } else {
  4755. this.expectRelational("<");
  4756. }
  4757. }
  4758. const result = this.tsParseDelimitedList(kind, parseElement);
  4759. if (bracket) {
  4760. this.expect(types$1.bracketR);
  4761. } else {
  4762. this.expectRelational(">");
  4763. }
  4764. return result;
  4765. }
  4766. tsParseImportType() {
  4767. const node = this.startNode();
  4768. this.expect(types$1._import);
  4769. this.expect(types$1.parenL);
  4770. if (!this.match(types$1.string)) {
  4771. this.raise(this.state.start, TSErrors.UnsupportedImportTypeArgument);
  4772. }
  4773. node.argument = this.parseExprAtom();
  4774. this.expect(types$1.parenR);
  4775. if (this.eat(types$1.dot)) {
  4776. node.qualifier = this.tsParseEntityName(true);
  4777. }
  4778. if (this.isRelational("<")) {
  4779. node.typeParameters = this.tsParseTypeArguments();
  4780. }
  4781. return this.finishNode(node, "TSImportType");
  4782. }
  4783. tsParseEntityName(allowReservedWords) {
  4784. let entity = this.parseIdentifier();
  4785. while (this.eat(types$1.dot)) {
  4786. const node = this.startNodeAtNode(entity);
  4787. node.left = entity;
  4788. node.right = this.parseIdentifier(allowReservedWords);
  4789. entity = this.finishNode(node, "TSQualifiedName");
  4790. }
  4791. return entity;
  4792. }
  4793. tsParseTypeReference() {
  4794. const node = this.startNode();
  4795. node.typeName = this.tsParseEntityName(false);
  4796. if (!this.hasPrecedingLineBreak() && this.isRelational("<")) {
  4797. node.typeParameters = this.tsParseTypeArguments();
  4798. }
  4799. return this.finishNode(node, "TSTypeReference");
  4800. }
  4801. tsParseThisTypePredicate(lhs) {
  4802. this.next();
  4803. const node = this.startNodeAtNode(lhs);
  4804. node.parameterName = lhs;
  4805. node.typeAnnotation = this.tsParseTypeAnnotation(false);
  4806. node.asserts = false;
  4807. return this.finishNode(node, "TSTypePredicate");
  4808. }
  4809. tsParseThisTypeNode() {
  4810. const node = this.startNode();
  4811. this.next();
  4812. return this.finishNode(node, "TSThisType");
  4813. }
  4814. tsParseTypeQuery() {
  4815. const node = this.startNode();
  4816. this.expect(types$1._typeof);
  4817. if (this.match(types$1._import)) {
  4818. node.exprName = this.tsParseImportType();
  4819. } else {
  4820. node.exprName = this.tsParseEntityName(true);
  4821. }
  4822. return this.finishNode(node, "TSTypeQuery");
  4823. }
  4824. tsParseTypeParameter() {
  4825. const node = this.startNode();
  4826. node.name = this.parseIdentifierName(node.start);
  4827. node.constraint = this.tsEatThenParseType(types$1._extends);
  4828. node.default = this.tsEatThenParseType(types$1.eq);
  4829. return this.finishNode(node, "TSTypeParameter");
  4830. }
  4831. tsTryParseTypeParameters() {
  4832. if (this.isRelational("<")) {
  4833. return this.tsParseTypeParameters();
  4834. }
  4835. }
  4836. tsParseTypeParameters() {
  4837. const node = this.startNode();
  4838. if (this.isRelational("<") || this.match(types$1.jsxTagStart)) {
  4839. this.next();
  4840. } else {
  4841. this.unexpected();
  4842. }
  4843. node.params = this.tsParseBracketedList("TypeParametersOrArguments", this.tsParseTypeParameter.bind(this), false, true);
  4844. if (node.params.length === 0) {
  4845. this.raise(node.start, TSErrors.EmptyTypeParameters);
  4846. }
  4847. return this.finishNode(node, "TSTypeParameterDeclaration");
  4848. }
  4849. tsTryNextParseConstantContext() {
  4850. if (this.lookahead().type === types$1._const) {
  4851. this.next();
  4852. return this.tsParseTypeReference();
  4853. }
  4854. return null;
  4855. }
  4856. tsFillSignature(returnToken, signature) {
  4857. const returnTokenRequired = returnToken === types$1.arrow;
  4858. signature.typeParameters = this.tsTryParseTypeParameters();
  4859. this.expect(types$1.parenL);
  4860. signature.parameters = this.tsParseBindingListForSignature();
  4861. if (returnTokenRequired) {
  4862. signature.typeAnnotation = this.tsParseTypeOrTypePredicateAnnotation(returnToken);
  4863. } else if (this.match(returnToken)) {
  4864. signature.typeAnnotation = this.tsParseTypeOrTypePredicateAnnotation(returnToken);
  4865. }
  4866. }
  4867. tsParseBindingListForSignature() {
  4868. return this.parseBindingList(types$1.parenR, 41).map(pattern => {
  4869. if (pattern.type !== "Identifier" && pattern.type !== "RestElement" && pattern.type !== "ObjectPattern" && pattern.type !== "ArrayPattern") {
  4870. this.raise(pattern.start, TSErrors.UnsupportedSignatureParameterKind, pattern.type);
  4871. }
  4872. return pattern;
  4873. });
  4874. }
  4875. tsParseTypeMemberSemicolon() {
  4876. if (!this.eat(types$1.comma) && !this.isLineTerminator()) {
  4877. this.expect(types$1.semi);
  4878. }
  4879. }
  4880. tsParseSignatureMember(kind, node) {
  4881. this.tsFillSignature(types$1.colon, node);
  4882. this.tsParseTypeMemberSemicolon();
  4883. return this.finishNode(node, kind);
  4884. }
  4885. tsIsUnambiguouslyIndexSignature() {
  4886. this.next();
  4887. return this.eat(types$1.name) && this.match(types$1.colon);
  4888. }
  4889. tsTryParseIndexSignature(node) {
  4890. if (!(this.match(types$1.bracketL) && this.tsLookAhead(this.tsIsUnambiguouslyIndexSignature.bind(this)))) {
  4891. return undefined;
  4892. }
  4893. this.expect(types$1.bracketL);
  4894. const id = this.parseIdentifier();
  4895. id.typeAnnotation = this.tsParseTypeAnnotation();
  4896. this.resetEndLocation(id);
  4897. this.expect(types$1.bracketR);
  4898. node.parameters = [id];
  4899. const type = this.tsTryParseTypeAnnotation();
  4900. if (type) node.typeAnnotation = type;
  4901. this.tsParseTypeMemberSemicolon();
  4902. return this.finishNode(node, "TSIndexSignature");
  4903. }
  4904. tsParsePropertyOrMethodSignature(node, readonly) {
  4905. if (this.eat(types$1.question)) node.optional = true;
  4906. const nodeAny = node;
  4907. if (this.match(types$1.parenL) || this.isRelational("<")) {
  4908. if (readonly) {
  4909. this.raise(node.start, TSErrors.ReadonlyForMethodSignature);
  4910. }
  4911. const method = nodeAny;
  4912. if (method.kind && this.isRelational("<")) {
  4913. this.raise(this.state.pos, TSErrors.AccesorCannotHaveTypeParameters);
  4914. }
  4915. this.tsFillSignature(types$1.colon, method);
  4916. this.tsParseTypeMemberSemicolon();
  4917. if (method.kind === "get") {
  4918. if (method.parameters.length > 0) {
  4919. this.raise(this.state.pos, ErrorMessages.BadGetterArity);
  4920. if (this.isThisParam(method.parameters[0])) {
  4921. this.raise(this.state.pos, TSErrors.AccesorCannotDeclareThisParameter);
  4922. }
  4923. }
  4924. } else if (method.kind === "set") {
  4925. if (method.parameters.length !== 1) {
  4926. this.raise(this.state.pos, ErrorMessages.BadSetterArity);
  4927. } else {
  4928. const firstParameter = method.parameters[0];
  4929. if (this.isThisParam(firstParameter)) {
  4930. this.raise(this.state.pos, TSErrors.AccesorCannotDeclareThisParameter);
  4931. }
  4932. if (firstParameter.type === "Identifier" && firstParameter.optional) {
  4933. this.raise(this.state.pos, TSErrors.SetAccesorCannotHaveOptionalParameter);
  4934. }
  4935. if (firstParameter.type === "RestElement") {
  4936. this.raise(this.state.pos, TSErrors.SetAccesorCannotHaveRestParameter);
  4937. }
  4938. }
  4939. if (method.typeAnnotation) {
  4940. this.raise(method.typeAnnotation.start, TSErrors.SetAccesorCannotHaveReturnType);
  4941. }
  4942. } else {
  4943. method.kind = "method";
  4944. }
  4945. return this.finishNode(method, "TSMethodSignature");
  4946. } else {
  4947. const property = nodeAny;
  4948. if (readonly) property.readonly = true;
  4949. const type = this.tsTryParseTypeAnnotation();
  4950. if (type) property.typeAnnotation = type;
  4951. this.tsParseTypeMemberSemicolon();
  4952. return this.finishNode(property, "TSPropertySignature");
  4953. }
  4954. }
  4955. tsParseTypeMember() {
  4956. const node = this.startNode();
  4957. if (this.match(types$1.parenL) || this.isRelational("<")) {
  4958. return this.tsParseSignatureMember("TSCallSignatureDeclaration", node);
  4959. }
  4960. if (this.match(types$1._new)) {
  4961. const id = this.startNode();
  4962. this.next();
  4963. if (this.match(types$1.parenL) || this.isRelational("<")) {
  4964. return this.tsParseSignatureMember("TSConstructSignatureDeclaration", node);
  4965. } else {
  4966. node.key = this.createIdentifier(id, "new");
  4967. return this.tsParsePropertyOrMethodSignature(node, false);
  4968. }
  4969. }
  4970. this.tsParseModifiers(node, ["readonly"], ["declare", "abstract", "private", "protected", "public", "static", "override"], TSErrors.InvalidModifierOnTypeMember);
  4971. const idx = this.tsTryParseIndexSignature(node);
  4972. if (idx) {
  4973. return idx;
  4974. }
  4975. this.parsePropertyName(node, false);
  4976. if (!node.computed && node.key.type === "Identifier" && (node.key.name === "get" || node.key.name === "set") && this.tsTokenCanFollowModifier()) {
  4977. node.kind = node.key.name;
  4978. this.parsePropertyName(node, false);
  4979. }
  4980. return this.tsParsePropertyOrMethodSignature(node, !!node.readonly);
  4981. }
  4982. tsParseTypeLiteral() {
  4983. const node = this.startNode();
  4984. node.members = this.tsParseObjectTypeMembers();
  4985. return this.finishNode(node, "TSTypeLiteral");
  4986. }
  4987. tsParseObjectTypeMembers() {
  4988. this.expect(types$1.braceL);
  4989. const members = this.tsParseList("TypeMembers", this.tsParseTypeMember.bind(this));
  4990. this.expect(types$1.braceR);
  4991. return members;
  4992. }
  4993. tsIsStartOfMappedType() {
  4994. this.next();
  4995. if (this.eat(types$1.plusMin)) {
  4996. return this.isContextual("readonly");
  4997. }
  4998. if (this.isContextual("readonly")) {
  4999. this.next();
  5000. }
  5001. if (!this.match(types$1.bracketL)) {
  5002. return false;
  5003. }
  5004. this.next();
  5005. if (!this.tsIsIdentifier()) {
  5006. return false;
  5007. }
  5008. this.next();
  5009. return this.match(types$1._in);
  5010. }
  5011. tsParseMappedTypeParameter() {
  5012. const node = this.startNode();
  5013. node.name = this.parseIdentifierName(node.start);
  5014. node.constraint = this.tsExpectThenParseType(types$1._in);
  5015. return this.finishNode(node, "TSTypeParameter");
  5016. }
  5017. tsParseMappedType() {
  5018. const node = this.startNode();
  5019. this.expect(types$1.braceL);
  5020. if (this.match(types$1.plusMin)) {
  5021. node.readonly = this.state.value;
  5022. this.next();
  5023. this.expectContextual("readonly");
  5024. } else if (this.eatContextual("readonly")) {
  5025. node.readonly = true;
  5026. }
  5027. this.expect(types$1.bracketL);
  5028. node.typeParameter = this.tsParseMappedTypeParameter();
  5029. node.nameType = this.eatContextual("as") ? this.tsParseType() : null;
  5030. this.expect(types$1.bracketR);
  5031. if (this.match(types$1.plusMin)) {
  5032. node.optional = this.state.value;
  5033. this.next();
  5034. this.expect(types$1.question);
  5035. } else if (this.eat(types$1.question)) {
  5036. node.optional = true;
  5037. }
  5038. node.typeAnnotation = this.tsTryParseType();
  5039. this.semicolon();
  5040. this.expect(types$1.braceR);
  5041. return this.finishNode(node, "TSMappedType");
  5042. }
  5043. tsParseTupleType() {
  5044. const node = this.startNode();
  5045. node.elementTypes = this.tsParseBracketedList("TupleElementTypes", this.tsParseTupleElementType.bind(this), true, false);
  5046. let seenOptionalElement = false;
  5047. let labeledElements = null;
  5048. node.elementTypes.forEach(elementNode => {
  5049. var _labeledElements;
  5050. let {
  5051. type
  5052. } = elementNode;
  5053. if (seenOptionalElement && type !== "TSRestType" && type !== "TSOptionalType" && !(type === "TSNamedTupleMember" && elementNode.optional)) {
  5054. this.raise(elementNode.start, TSErrors.OptionalTypeBeforeRequired);
  5055. }
  5056. seenOptionalElement = seenOptionalElement || type === "TSNamedTupleMember" && elementNode.optional || type === "TSOptionalType";
  5057. if (type === "TSRestType") {
  5058. elementNode = elementNode.typeAnnotation;
  5059. type = elementNode.type;
  5060. }
  5061. const isLabeled = type === "TSNamedTupleMember";
  5062. labeledElements = (_labeledElements = labeledElements) != null ? _labeledElements : isLabeled;
  5063. if (labeledElements !== isLabeled) {
  5064. this.raise(elementNode.start, TSErrors.MixedLabeledAndUnlabeledElements);
  5065. }
  5066. });
  5067. return this.finishNode(node, "TSTupleType");
  5068. }
  5069. tsParseTupleElementType() {
  5070. const {
  5071. start: startPos,
  5072. startLoc
  5073. } = this.state;
  5074. const rest = this.eat(types$1.ellipsis);
  5075. let type = this.tsParseType();
  5076. const optional = this.eat(types$1.question);
  5077. const labeled = this.eat(types$1.colon);
  5078. if (labeled) {
  5079. const labeledNode = this.startNodeAtNode(type);
  5080. labeledNode.optional = optional;
  5081. if (type.type === "TSTypeReference" && !type.typeParameters && type.typeName.type === "Identifier") {
  5082. labeledNode.label = type.typeName;
  5083. } else {
  5084. this.raise(type.start, TSErrors.InvalidTupleMemberLabel);
  5085. labeledNode.label = type;
  5086. }
  5087. labeledNode.elementType = this.tsParseType();
  5088. type = this.finishNode(labeledNode, "TSNamedTupleMember");
  5089. } else if (optional) {
  5090. const optionalTypeNode = this.startNodeAtNode(type);
  5091. optionalTypeNode.typeAnnotation = type;
  5092. type = this.finishNode(optionalTypeNode, "TSOptionalType");
  5093. }
  5094. if (rest) {
  5095. const restNode = this.startNodeAt(startPos, startLoc);
  5096. restNode.typeAnnotation = type;
  5097. type = this.finishNode(restNode, "TSRestType");
  5098. }
  5099. return type;
  5100. }
  5101. tsParseParenthesizedType() {
  5102. const node = this.startNode();
  5103. this.expect(types$1.parenL);
  5104. node.typeAnnotation = this.tsParseType();
  5105. this.expect(types$1.parenR);
  5106. return this.finishNode(node, "TSParenthesizedType");
  5107. }
  5108. tsParseFunctionOrConstructorType(type, abstract) {
  5109. const node = this.startNode();
  5110. if (type === "TSConstructorType") {
  5111. node.abstract = !!abstract;
  5112. if (abstract) this.next();
  5113. this.next();
  5114. }
  5115. this.tsFillSignature(types$1.arrow, node);
  5116. return this.finishNode(node, type);
  5117. }
  5118. tsParseLiteralTypeNode() {
  5119. const node = this.startNode();
  5120. node.literal = (() => {
  5121. switch (this.state.type) {
  5122. case types$1.num:
  5123. case types$1.bigint:
  5124. case types$1.string:
  5125. case types$1._true:
  5126. case types$1._false:
  5127. return this.parseExprAtom();
  5128. default:
  5129. throw this.unexpected();
  5130. }
  5131. })();
  5132. return this.finishNode(node, "TSLiteralType");
  5133. }
  5134. tsParseTemplateLiteralType() {
  5135. const node = this.startNode();
  5136. node.literal = this.parseTemplate(false);
  5137. return this.finishNode(node, "TSLiteralType");
  5138. }
  5139. parseTemplateSubstitution() {
  5140. if (this.state.inType) return this.tsParseType();
  5141. return super.parseTemplateSubstitution();
  5142. }
  5143. tsParseThisTypeOrThisTypePredicate() {
  5144. const thisKeyword = this.tsParseThisTypeNode();
  5145. if (this.isContextual("is") && !this.hasPrecedingLineBreak()) {
  5146. return this.tsParseThisTypePredicate(thisKeyword);
  5147. } else {
  5148. return thisKeyword;
  5149. }
  5150. }
  5151. tsParseNonArrayType() {
  5152. switch (this.state.type) {
  5153. case types$1.name:
  5154. case types$1._void:
  5155. case types$1._null:
  5156. {
  5157. const type = this.match(types$1._void) ? "TSVoidKeyword" : this.match(types$1._null) ? "TSNullKeyword" : keywordTypeFromName(this.state.value);
  5158. if (type !== undefined && this.lookaheadCharCode() !== 46) {
  5159. const node = this.startNode();
  5160. this.next();
  5161. return this.finishNode(node, type);
  5162. }
  5163. return this.tsParseTypeReference();
  5164. }
  5165. case types$1.string:
  5166. case types$1.num:
  5167. case types$1.bigint:
  5168. case types$1._true:
  5169. case types$1._false:
  5170. return this.tsParseLiteralTypeNode();
  5171. case types$1.plusMin:
  5172. if (this.state.value === "-") {
  5173. const node = this.startNode();
  5174. const nextToken = this.lookahead();
  5175. if (nextToken.type !== types$1.num && nextToken.type !== types$1.bigint) {
  5176. throw this.unexpected();
  5177. }
  5178. node.literal = this.parseMaybeUnary();
  5179. return this.finishNode(node, "TSLiteralType");
  5180. }
  5181. break;
  5182. case types$1._this:
  5183. return this.tsParseThisTypeOrThisTypePredicate();
  5184. case types$1._typeof:
  5185. return this.tsParseTypeQuery();
  5186. case types$1._import:
  5187. return this.tsParseImportType();
  5188. case types$1.braceL:
  5189. return this.tsLookAhead(this.tsIsStartOfMappedType.bind(this)) ? this.tsParseMappedType() : this.tsParseTypeLiteral();
  5190. case types$1.bracketL:
  5191. return this.tsParseTupleType();
  5192. case types$1.parenL:
  5193. return this.tsParseParenthesizedType();
  5194. case types$1.backQuote:
  5195. return this.tsParseTemplateLiteralType();
  5196. }
  5197. throw this.unexpected();
  5198. }
  5199. tsParseArrayTypeOrHigher() {
  5200. let type = this.tsParseNonArrayType();
  5201. while (!this.hasPrecedingLineBreak() && this.eat(types$1.bracketL)) {
  5202. if (this.match(types$1.bracketR)) {
  5203. const node = this.startNodeAtNode(type);
  5204. node.elementType = type;
  5205. this.expect(types$1.bracketR);
  5206. type = this.finishNode(node, "TSArrayType");
  5207. } else {
  5208. const node = this.startNodeAtNode(type);
  5209. node.objectType = type;
  5210. node.indexType = this.tsParseType();
  5211. this.expect(types$1.bracketR);
  5212. type = this.finishNode(node, "TSIndexedAccessType");
  5213. }
  5214. }
  5215. return type;
  5216. }
  5217. tsParseTypeOperator(operator) {
  5218. const node = this.startNode();
  5219. this.expectContextual(operator);
  5220. node.operator = operator;
  5221. node.typeAnnotation = this.tsParseTypeOperatorOrHigher();
  5222. if (operator === "readonly") {
  5223. this.tsCheckTypeAnnotationForReadOnly(node);
  5224. }
  5225. return this.finishNode(node, "TSTypeOperator");
  5226. }
  5227. tsCheckTypeAnnotationForReadOnly(node) {
  5228. switch (node.typeAnnotation.type) {
  5229. case "TSTupleType":
  5230. case "TSArrayType":
  5231. return;
  5232. default:
  5233. this.raise(node.start, TSErrors.UnexpectedReadonly);
  5234. }
  5235. }
  5236. tsParseInferType() {
  5237. const node = this.startNode();
  5238. this.expectContextual("infer");
  5239. const typeParameter = this.startNode();
  5240. typeParameter.name = this.parseIdentifierName(typeParameter.start);
  5241. node.typeParameter = this.finishNode(typeParameter, "TSTypeParameter");
  5242. return this.finishNode(node, "TSInferType");
  5243. }
  5244. tsParseTypeOperatorOrHigher() {
  5245. const operator = ["keyof", "unique", "readonly"].find(kw => this.isContextual(kw));
  5246. return operator ? this.tsParseTypeOperator(operator) : this.isContextual("infer") ? this.tsParseInferType() : this.tsParseArrayTypeOrHigher();
  5247. }
  5248. tsParseUnionOrIntersectionType(kind, parseConstituentType, operator) {
  5249. const node = this.startNode();
  5250. const hasLeadingOperator = this.eat(operator);
  5251. const types = [];
  5252. do {
  5253. types.push(parseConstituentType());
  5254. } while (this.eat(operator));
  5255. if (types.length === 1 && !hasLeadingOperator) {
  5256. return types[0];
  5257. }
  5258. node.types = types;
  5259. return this.finishNode(node, kind);
  5260. }
  5261. tsParseIntersectionTypeOrHigher() {
  5262. return this.tsParseUnionOrIntersectionType("TSIntersectionType", this.tsParseTypeOperatorOrHigher.bind(this), types$1.bitwiseAND);
  5263. }
  5264. tsParseUnionTypeOrHigher() {
  5265. return this.tsParseUnionOrIntersectionType("TSUnionType", this.tsParseIntersectionTypeOrHigher.bind(this), types$1.bitwiseOR);
  5266. }
  5267. tsIsStartOfFunctionType() {
  5268. if (this.isRelational("<")) {
  5269. return true;
  5270. }
  5271. return this.match(types$1.parenL) && this.tsLookAhead(this.tsIsUnambiguouslyStartOfFunctionType.bind(this));
  5272. }
  5273. tsSkipParameterStart() {
  5274. if (this.match(types$1.name) || this.match(types$1._this)) {
  5275. this.next();
  5276. return true;
  5277. }
  5278. if (this.match(types$1.braceL)) {
  5279. let braceStackCounter = 1;
  5280. this.next();
  5281. while (braceStackCounter > 0) {
  5282. if (this.match(types$1.braceL)) {
  5283. ++braceStackCounter;
  5284. } else if (this.match(types$1.braceR)) {
  5285. --braceStackCounter;
  5286. }
  5287. this.next();
  5288. }
  5289. return true;
  5290. }
  5291. if (this.match(types$1.bracketL)) {
  5292. let braceStackCounter = 1;
  5293. this.next();
  5294. while (braceStackCounter > 0) {
  5295. if (this.match(types$1.bracketL)) {
  5296. ++braceStackCounter;
  5297. } else if (this.match(types$1.bracketR)) {
  5298. --braceStackCounter;
  5299. }
  5300. this.next();
  5301. }
  5302. return true;
  5303. }
  5304. return false;
  5305. }
  5306. tsIsUnambiguouslyStartOfFunctionType() {
  5307. this.next();
  5308. if (this.match(types$1.parenR) || this.match(types$1.ellipsis)) {
  5309. return true;
  5310. }
  5311. if (this.tsSkipParameterStart()) {
  5312. if (this.match(types$1.colon) || this.match(types$1.comma) || this.match(types$1.question) || this.match(types$1.eq)) {
  5313. return true;
  5314. }
  5315. if (this.match(types$1.parenR)) {
  5316. this.next();
  5317. if (this.match(types$1.arrow)) {
  5318. return true;
  5319. }
  5320. }
  5321. }
  5322. return false;
  5323. }
  5324. tsParseTypeOrTypePredicateAnnotation(returnToken) {
  5325. return this.tsInType(() => {
  5326. const t = this.startNode();
  5327. this.expect(returnToken);
  5328. const node = this.startNode();
  5329. const asserts = !!this.tsTryParse(this.tsParseTypePredicateAsserts.bind(this));
  5330. if (asserts && this.match(types$1._this)) {
  5331. let thisTypePredicate = this.tsParseThisTypeOrThisTypePredicate();
  5332. if (thisTypePredicate.type === "TSThisType") {
  5333. node.parameterName = thisTypePredicate;
  5334. node.asserts = true;
  5335. node.typeAnnotation = null;
  5336. thisTypePredicate = this.finishNode(node, "TSTypePredicate");
  5337. } else {
  5338. this.resetStartLocationFromNode(thisTypePredicate, node);
  5339. thisTypePredicate.asserts = true;
  5340. }
  5341. t.typeAnnotation = thisTypePredicate;
  5342. return this.finishNode(t, "TSTypeAnnotation");
  5343. }
  5344. const typePredicateVariable = this.tsIsIdentifier() && this.tsTryParse(this.tsParseTypePredicatePrefix.bind(this));
  5345. if (!typePredicateVariable) {
  5346. if (!asserts) {
  5347. return this.tsParseTypeAnnotation(false, t);
  5348. }
  5349. node.parameterName = this.parseIdentifier();
  5350. node.asserts = asserts;
  5351. node.typeAnnotation = null;
  5352. t.typeAnnotation = this.finishNode(node, "TSTypePredicate");
  5353. return this.finishNode(t, "TSTypeAnnotation");
  5354. }
  5355. const type = this.tsParseTypeAnnotation(false);
  5356. node.parameterName = typePredicateVariable;
  5357. node.typeAnnotation = type;
  5358. node.asserts = asserts;
  5359. t.typeAnnotation = this.finishNode(node, "TSTypePredicate");
  5360. return this.finishNode(t, "TSTypeAnnotation");
  5361. });
  5362. }
  5363. tsTryParseTypeOrTypePredicateAnnotation() {
  5364. return this.match(types$1.colon) ? this.tsParseTypeOrTypePredicateAnnotation(types$1.colon) : undefined;
  5365. }
  5366. tsTryParseTypeAnnotation() {
  5367. return this.match(types$1.colon) ? this.tsParseTypeAnnotation() : undefined;
  5368. }
  5369. tsTryParseType() {
  5370. return this.tsEatThenParseType(types$1.colon);
  5371. }
  5372. tsParseTypePredicatePrefix() {
  5373. const id = this.parseIdentifier();
  5374. if (this.isContextual("is") && !this.hasPrecedingLineBreak()) {
  5375. this.next();
  5376. return id;
  5377. }
  5378. }
  5379. tsParseTypePredicateAsserts() {
  5380. if (!this.match(types$1.name) || this.state.value !== "asserts" || this.hasPrecedingLineBreak()) {
  5381. return false;
  5382. }
  5383. const containsEsc = this.state.containsEsc;
  5384. this.next();
  5385. if (!this.match(types$1.name) && !this.match(types$1._this)) {
  5386. return false;
  5387. }
  5388. if (containsEsc) {
  5389. this.raise(this.state.lastTokStart, ErrorMessages.InvalidEscapedReservedWord, "asserts");
  5390. }
  5391. return true;
  5392. }
  5393. tsParseTypeAnnotation(eatColon = true, t = this.startNode()) {
  5394. this.tsInType(() => {
  5395. if (eatColon) this.expect(types$1.colon);
  5396. t.typeAnnotation = this.tsParseType();
  5397. });
  5398. return this.finishNode(t, "TSTypeAnnotation");
  5399. }
  5400. tsParseType() {
  5401. assert(this.state.inType);
  5402. const type = this.tsParseNonConditionalType();
  5403. if (this.hasPrecedingLineBreak() || !this.eat(types$1._extends)) {
  5404. return type;
  5405. }
  5406. const node = this.startNodeAtNode(type);
  5407. node.checkType = type;
  5408. node.extendsType = this.tsParseNonConditionalType();
  5409. this.expect(types$1.question);
  5410. node.trueType = this.tsParseType();
  5411. this.expect(types$1.colon);
  5412. node.falseType = this.tsParseType();
  5413. return this.finishNode(node, "TSConditionalType");
  5414. }
  5415. isAbstractConstructorSignature() {
  5416. return this.isContextual("abstract") && this.lookahead().type === types$1._new;
  5417. }
  5418. tsParseNonConditionalType() {
  5419. if (this.tsIsStartOfFunctionType()) {
  5420. return this.tsParseFunctionOrConstructorType("TSFunctionType");
  5421. }
  5422. if (this.match(types$1._new)) {
  5423. return this.tsParseFunctionOrConstructorType("TSConstructorType");
  5424. } else if (this.isAbstractConstructorSignature()) {
  5425. return this.tsParseFunctionOrConstructorType("TSConstructorType", true);
  5426. }
  5427. return this.tsParseUnionTypeOrHigher();
  5428. }
  5429. tsParseTypeAssertion() {
  5430. const node = this.startNode();
  5431. const _const = this.tsTryNextParseConstantContext();
  5432. node.typeAnnotation = _const || this.tsNextThenParseType();
  5433. this.expectRelational(">");
  5434. node.expression = this.parseMaybeUnary();
  5435. return this.finishNode(node, "TSTypeAssertion");
  5436. }
  5437. tsParseHeritageClause(descriptor) {
  5438. const originalStart = this.state.start;
  5439. const delimitedList = this.tsParseDelimitedList("HeritageClauseElement", this.tsParseExpressionWithTypeArguments.bind(this));
  5440. if (!delimitedList.length) {
  5441. this.raise(originalStart, TSErrors.EmptyHeritageClauseType, descriptor);
  5442. }
  5443. return delimitedList;
  5444. }
  5445. tsParseExpressionWithTypeArguments() {
  5446. const node = this.startNode();
  5447. node.expression = this.tsParseEntityName(false);
  5448. if (this.isRelational("<")) {
  5449. node.typeParameters = this.tsParseTypeArguments();
  5450. }
  5451. return this.finishNode(node, "TSExpressionWithTypeArguments");
  5452. }
  5453. tsParseInterfaceDeclaration(node) {
  5454. node.id = this.parseIdentifier();
  5455. this.checkLVal(node.id, "typescript interface declaration", BIND_TS_INTERFACE);
  5456. node.typeParameters = this.tsTryParseTypeParameters();
  5457. if (this.eat(types$1._extends)) {
  5458. node.extends = this.tsParseHeritageClause("extends");
  5459. }
  5460. const body = this.startNode();
  5461. body.body = this.tsInType(this.tsParseObjectTypeMembers.bind(this));
  5462. node.body = this.finishNode(body, "TSInterfaceBody");
  5463. return this.finishNode(node, "TSInterfaceDeclaration");
  5464. }
  5465. tsParseTypeAliasDeclaration(node) {
  5466. node.id = this.parseIdentifier();
  5467. this.checkLVal(node.id, "typescript type alias", BIND_TS_TYPE);
  5468. node.typeParameters = this.tsTryParseTypeParameters();
  5469. node.typeAnnotation = this.tsInType(() => {
  5470. this.expect(types$1.eq);
  5471. if (this.isContextual("intrinsic") && this.lookahead().type !== types$1.dot) {
  5472. const node = this.startNode();
  5473. this.next();
  5474. return this.finishNode(node, "TSIntrinsicKeyword");
  5475. }
  5476. return this.tsParseType();
  5477. });
  5478. this.semicolon();
  5479. return this.finishNode(node, "TSTypeAliasDeclaration");
  5480. }
  5481. tsInNoContext(cb) {
  5482. const oldContext = this.state.context;
  5483. this.state.context = [oldContext[0]];
  5484. try {
  5485. return cb();
  5486. } finally {
  5487. this.state.context = oldContext;
  5488. }
  5489. }
  5490. tsInType(cb) {
  5491. const oldInType = this.state.inType;
  5492. this.state.inType = true;
  5493. try {
  5494. return cb();
  5495. } finally {
  5496. this.state.inType = oldInType;
  5497. }
  5498. }
  5499. tsEatThenParseType(token) {
  5500. return !this.match(token) ? undefined : this.tsNextThenParseType();
  5501. }
  5502. tsExpectThenParseType(token) {
  5503. return this.tsDoThenParseType(() => this.expect(token));
  5504. }
  5505. tsNextThenParseType() {
  5506. return this.tsDoThenParseType(() => this.next());
  5507. }
  5508. tsDoThenParseType(cb) {
  5509. return this.tsInType(() => {
  5510. cb();
  5511. return this.tsParseType();
  5512. });
  5513. }
  5514. tsParseEnumMember() {
  5515. const node = this.startNode();
  5516. node.id = this.match(types$1.string) ? this.parseExprAtom() : this.parseIdentifier(true);
  5517. if (this.eat(types$1.eq)) {
  5518. node.initializer = this.parseMaybeAssignAllowIn();
  5519. }
  5520. return this.finishNode(node, "TSEnumMember");
  5521. }
  5522. tsParseEnumDeclaration(node, isConst) {
  5523. if (isConst) node.const = true;
  5524. node.id = this.parseIdentifier();
  5525. this.checkLVal(node.id, "typescript enum declaration", isConst ? BIND_TS_CONST_ENUM : BIND_TS_ENUM);
  5526. this.expect(types$1.braceL);
  5527. node.members = this.tsParseDelimitedList("EnumMembers", this.tsParseEnumMember.bind(this));
  5528. this.expect(types$1.braceR);
  5529. return this.finishNode(node, "TSEnumDeclaration");
  5530. }
  5531. tsParseModuleBlock() {
  5532. const node = this.startNode();
  5533. this.scope.enter(SCOPE_OTHER);
  5534. this.expect(types$1.braceL);
  5535. this.parseBlockOrModuleBlockBody(node.body = [], undefined, true, types$1.braceR);
  5536. this.scope.exit();
  5537. return this.finishNode(node, "TSModuleBlock");
  5538. }
  5539. tsParseModuleOrNamespaceDeclaration(node, nested = false) {
  5540. node.id = this.parseIdentifier();
  5541. if (!nested) {
  5542. this.checkLVal(node.id, "module or namespace declaration", BIND_TS_NAMESPACE);
  5543. }
  5544. if (this.eat(types$1.dot)) {
  5545. const inner = this.startNode();
  5546. this.tsParseModuleOrNamespaceDeclaration(inner, true);
  5547. node.body = inner;
  5548. } else {
  5549. this.scope.enter(SCOPE_TS_MODULE);
  5550. this.prodParam.enter(PARAM);
  5551. node.body = this.tsParseModuleBlock();
  5552. this.prodParam.exit();
  5553. this.scope.exit();
  5554. }
  5555. return this.finishNode(node, "TSModuleDeclaration");
  5556. }
  5557. tsParseAmbientExternalModuleDeclaration(node) {
  5558. if (this.isContextual("global")) {
  5559. node.global = true;
  5560. node.id = this.parseIdentifier();
  5561. } else if (this.match(types$1.string)) {
  5562. node.id = this.parseExprAtom();
  5563. } else {
  5564. this.unexpected();
  5565. }
  5566. if (this.match(types$1.braceL)) {
  5567. this.scope.enter(SCOPE_TS_MODULE);
  5568. this.prodParam.enter(PARAM);
  5569. node.body = this.tsParseModuleBlock();
  5570. this.prodParam.exit();
  5571. this.scope.exit();
  5572. } else {
  5573. this.semicolon();
  5574. }
  5575. return this.finishNode(node, "TSModuleDeclaration");
  5576. }
  5577. tsParseImportEqualsDeclaration(node, isExport) {
  5578. node.isExport = isExport || false;
  5579. node.id = this.parseIdentifier();
  5580. this.checkLVal(node.id, "import equals declaration", BIND_LEXICAL);
  5581. this.expect(types$1.eq);
  5582. const moduleReference = this.tsParseModuleReference();
  5583. if (node.importKind === "type" && moduleReference.type !== "TSExternalModuleReference") {
  5584. this.raise(moduleReference.start, TSErrors.ImportAliasHasImportType);
  5585. }
  5586. node.moduleReference = moduleReference;
  5587. this.semicolon();
  5588. return this.finishNode(node, "TSImportEqualsDeclaration");
  5589. }
  5590. tsIsExternalModuleReference() {
  5591. return this.isContextual("require") && this.lookaheadCharCode() === 40;
  5592. }
  5593. tsParseModuleReference() {
  5594. return this.tsIsExternalModuleReference() ? this.tsParseExternalModuleReference() : this.tsParseEntityName(false);
  5595. }
  5596. tsParseExternalModuleReference() {
  5597. const node = this.startNode();
  5598. this.expectContextual("require");
  5599. this.expect(types$1.parenL);
  5600. if (!this.match(types$1.string)) {
  5601. throw this.unexpected();
  5602. }
  5603. node.expression = this.parseExprAtom();
  5604. this.expect(types$1.parenR);
  5605. return this.finishNode(node, "TSExternalModuleReference");
  5606. }
  5607. tsLookAhead(f) {
  5608. const state = this.state.clone();
  5609. const res = f();
  5610. this.state = state;
  5611. return res;
  5612. }
  5613. tsTryParseAndCatch(f) {
  5614. const result = this.tryParse(abort => f() || abort());
  5615. if (result.aborted || !result.node) return undefined;
  5616. if (result.error) this.state = result.failState;
  5617. return result.node;
  5618. }
  5619. tsTryParse(f) {
  5620. const state = this.state.clone();
  5621. const result = f();
  5622. if (result !== undefined && result !== false) {
  5623. return result;
  5624. } else {
  5625. this.state = state;
  5626. return undefined;
  5627. }
  5628. }
  5629. tsTryParseDeclare(nany) {
  5630. if (this.isLineTerminator()) {
  5631. return;
  5632. }
  5633. let starttype = this.state.type;
  5634. let kind;
  5635. if (this.isContextual("let")) {
  5636. starttype = types$1._var;
  5637. kind = "let";
  5638. }
  5639. return this.tsInAmbientContext(() => {
  5640. switch (starttype) {
  5641. case types$1._function:
  5642. nany.declare = true;
  5643. return this.parseFunctionStatement(nany, false, true);
  5644. case types$1._class:
  5645. nany.declare = true;
  5646. return this.parseClass(nany, true, false);
  5647. case types$1._const:
  5648. if (this.match(types$1._const) && this.isLookaheadContextual("enum")) {
  5649. this.expect(types$1._const);
  5650. this.expectContextual("enum");
  5651. return this.tsParseEnumDeclaration(nany, true);
  5652. }
  5653. case types$1._var:
  5654. kind = kind || this.state.value;
  5655. return this.parseVarStatement(nany, kind);
  5656. case types$1.name:
  5657. {
  5658. const value = this.state.value;
  5659. if (value === "global") {
  5660. return this.tsParseAmbientExternalModuleDeclaration(nany);
  5661. } else {
  5662. return this.tsParseDeclaration(nany, value, true);
  5663. }
  5664. }
  5665. }
  5666. });
  5667. }
  5668. tsTryParseExportDeclaration() {
  5669. return this.tsParseDeclaration(this.startNode(), this.state.value, true);
  5670. }
  5671. tsParseExpressionStatement(node, expr) {
  5672. switch (expr.name) {
  5673. case "declare":
  5674. {
  5675. const declaration = this.tsTryParseDeclare(node);
  5676. if (declaration) {
  5677. declaration.declare = true;
  5678. return declaration;
  5679. }
  5680. break;
  5681. }
  5682. case "global":
  5683. if (this.match(types$1.braceL)) {
  5684. this.scope.enter(SCOPE_TS_MODULE);
  5685. this.prodParam.enter(PARAM);
  5686. const mod = node;
  5687. mod.global = true;
  5688. mod.id = expr;
  5689. mod.body = this.tsParseModuleBlock();
  5690. this.scope.exit();
  5691. this.prodParam.exit();
  5692. return this.finishNode(mod, "TSModuleDeclaration");
  5693. }
  5694. break;
  5695. default:
  5696. return this.tsParseDeclaration(node, expr.name, false);
  5697. }
  5698. }
  5699. tsParseDeclaration(node, value, next) {
  5700. switch (value) {
  5701. case "abstract":
  5702. if (this.tsCheckLineTerminator(next) && (this.match(types$1._class) || this.match(types$1.name))) {
  5703. return this.tsParseAbstractDeclaration(node);
  5704. }
  5705. break;
  5706. case "enum":
  5707. if (next || this.match(types$1.name)) {
  5708. if (next) this.next();
  5709. return this.tsParseEnumDeclaration(node, false);
  5710. }
  5711. break;
  5712. case "interface":
  5713. if (this.tsCheckLineTerminator(next) && this.match(types$1.name)) {
  5714. return this.tsParseInterfaceDeclaration(node);
  5715. }
  5716. break;
  5717. case "module":
  5718. if (this.tsCheckLineTerminator(next)) {
  5719. if (this.match(types$1.string)) {
  5720. return this.tsParseAmbientExternalModuleDeclaration(node);
  5721. } else if (this.match(types$1.name)) {
  5722. return this.tsParseModuleOrNamespaceDeclaration(node);
  5723. }
  5724. }
  5725. break;
  5726. case "namespace":
  5727. if (this.tsCheckLineTerminator(next) && this.match(types$1.name)) {
  5728. return this.tsParseModuleOrNamespaceDeclaration(node);
  5729. }
  5730. break;
  5731. case "type":
  5732. if (this.tsCheckLineTerminator(next) && this.match(types$1.name)) {
  5733. return this.tsParseTypeAliasDeclaration(node);
  5734. }
  5735. break;
  5736. }
  5737. }
  5738. tsCheckLineTerminator(next) {
  5739. if (next) {
  5740. if (this.hasFollowingLineBreak()) return false;
  5741. this.next();
  5742. return true;
  5743. }
  5744. return !this.isLineTerminator();
  5745. }
  5746. tsTryParseGenericAsyncArrowFunction(startPos, startLoc) {
  5747. if (!this.isRelational("<")) {
  5748. return undefined;
  5749. }
  5750. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  5751. this.state.maybeInArrowParameters = true;
  5752. const res = this.tsTryParseAndCatch(() => {
  5753. const node = this.startNodeAt(startPos, startLoc);
  5754. node.typeParameters = this.tsParseTypeParameters();
  5755. super.parseFunctionParams(node);
  5756. node.returnType = this.tsTryParseTypeOrTypePredicateAnnotation();
  5757. this.expect(types$1.arrow);
  5758. return node;
  5759. });
  5760. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  5761. if (!res) {
  5762. return undefined;
  5763. }
  5764. return this.parseArrowExpression(res, null, true);
  5765. }
  5766. tsParseTypeArguments() {
  5767. const node = this.startNode();
  5768. node.params = this.tsInType(() => this.tsInNoContext(() => {
  5769. this.expectRelational("<");
  5770. return this.tsParseDelimitedList("TypeParametersOrArguments", this.tsParseType.bind(this));
  5771. }));
  5772. if (node.params.length === 0) {
  5773. this.raise(node.start, TSErrors.EmptyTypeArguments);
  5774. }
  5775. this.expectRelational(">");
  5776. return this.finishNode(node, "TSTypeParameterInstantiation");
  5777. }
  5778. tsIsDeclarationStart() {
  5779. if (this.match(types$1.name)) {
  5780. switch (this.state.value) {
  5781. case "abstract":
  5782. case "declare":
  5783. case "enum":
  5784. case "interface":
  5785. case "module":
  5786. case "namespace":
  5787. case "type":
  5788. return true;
  5789. }
  5790. }
  5791. return false;
  5792. }
  5793. isExportDefaultSpecifier() {
  5794. if (this.tsIsDeclarationStart()) return false;
  5795. return super.isExportDefaultSpecifier();
  5796. }
  5797. parseAssignableListItem(allowModifiers, decorators) {
  5798. const startPos = this.state.start;
  5799. const startLoc = this.state.startLoc;
  5800. let accessibility;
  5801. let readonly = false;
  5802. let override = false;
  5803. if (allowModifiers !== undefined) {
  5804. const modified = {};
  5805. this.tsParseModifiers(modified, ["public", "private", "protected", "override", "readonly"]);
  5806. accessibility = modified.accessibility;
  5807. override = modified.override;
  5808. readonly = modified.readonly;
  5809. if (allowModifiers === false && (accessibility || readonly || override)) {
  5810. this.raise(startPos, TSErrors.UnexpectedParameterModifier);
  5811. }
  5812. }
  5813. const left = this.parseMaybeDefault();
  5814. this.parseAssignableListItemTypes(left);
  5815. const elt = this.parseMaybeDefault(left.start, left.loc.start, left);
  5816. if (accessibility || readonly || override) {
  5817. const pp = this.startNodeAt(startPos, startLoc);
  5818. if (decorators.length) {
  5819. pp.decorators = decorators;
  5820. }
  5821. if (accessibility) pp.accessibility = accessibility;
  5822. if (readonly) pp.readonly = readonly;
  5823. if (override) pp.override = override;
  5824. if (elt.type !== "Identifier" && elt.type !== "AssignmentPattern") {
  5825. this.raise(pp.start, TSErrors.UnsupportedParameterPropertyKind);
  5826. }
  5827. pp.parameter = elt;
  5828. return this.finishNode(pp, "TSParameterProperty");
  5829. }
  5830. if (decorators.length) {
  5831. left.decorators = decorators;
  5832. }
  5833. return elt;
  5834. }
  5835. parseFunctionBodyAndFinish(node, type, isMethod = false) {
  5836. if (this.match(types$1.colon)) {
  5837. node.returnType = this.tsParseTypeOrTypePredicateAnnotation(types$1.colon);
  5838. }
  5839. const bodilessType = type === "FunctionDeclaration" ? "TSDeclareFunction" : type === "ClassMethod" ? "TSDeclareMethod" : undefined;
  5840. if (bodilessType && !this.match(types$1.braceL) && this.isLineTerminator()) {
  5841. this.finishNode(node, bodilessType);
  5842. return;
  5843. }
  5844. if (bodilessType === "TSDeclareFunction" && this.state.isAmbientContext) {
  5845. this.raise(node.start, TSErrors.DeclareFunctionHasImplementation);
  5846. if (node.declare) {
  5847. super.parseFunctionBodyAndFinish(node, bodilessType, isMethod);
  5848. return;
  5849. }
  5850. }
  5851. super.parseFunctionBodyAndFinish(node, type, isMethod);
  5852. }
  5853. registerFunctionStatementId(node) {
  5854. if (!node.body && node.id) {
  5855. this.checkLVal(node.id, "function name", BIND_TS_AMBIENT);
  5856. } else {
  5857. super.registerFunctionStatementId(...arguments);
  5858. }
  5859. }
  5860. tsCheckForInvalidTypeCasts(items) {
  5861. items.forEach(node => {
  5862. if ((node == null ? void 0 : node.type) === "TSTypeCastExpression") {
  5863. this.raise(node.typeAnnotation.start, TSErrors.UnexpectedTypeAnnotation);
  5864. }
  5865. });
  5866. }
  5867. toReferencedList(exprList, isInParens) {
  5868. this.tsCheckForInvalidTypeCasts(exprList);
  5869. return exprList;
  5870. }
  5871. parseArrayLike(...args) {
  5872. const node = super.parseArrayLike(...args);
  5873. if (node.type === "ArrayExpression") {
  5874. this.tsCheckForInvalidTypeCasts(node.elements);
  5875. }
  5876. return node;
  5877. }
  5878. parseSubscript(base, startPos, startLoc, noCalls, state) {
  5879. if (!this.hasPrecedingLineBreak() && this.match(types$1.bang)) {
  5880. this.state.exprAllowed = false;
  5881. this.next();
  5882. const nonNullExpression = this.startNodeAt(startPos, startLoc);
  5883. nonNullExpression.expression = base;
  5884. return this.finishNode(nonNullExpression, "TSNonNullExpression");
  5885. }
  5886. if (this.isRelational("<")) {
  5887. const result = this.tsTryParseAndCatch(() => {
  5888. if (!noCalls && this.atPossibleAsyncArrow(base)) {
  5889. const asyncArrowFn = this.tsTryParseGenericAsyncArrowFunction(startPos, startLoc);
  5890. if (asyncArrowFn) {
  5891. return asyncArrowFn;
  5892. }
  5893. }
  5894. const node = this.startNodeAt(startPos, startLoc);
  5895. node.callee = base;
  5896. const typeArguments = this.tsParseTypeArguments();
  5897. if (typeArguments) {
  5898. if (!noCalls && this.eat(types$1.parenL)) {
  5899. node.arguments = this.parseCallExpressionArguments(types$1.parenR, false);
  5900. this.tsCheckForInvalidTypeCasts(node.arguments);
  5901. node.typeParameters = typeArguments;
  5902. if (state.optionalChainMember) {
  5903. node.optional = false;
  5904. }
  5905. return this.finishCallExpression(node, state.optionalChainMember);
  5906. } else if (this.match(types$1.backQuote)) {
  5907. const result = this.parseTaggedTemplateExpression(base, startPos, startLoc, state);
  5908. result.typeParameters = typeArguments;
  5909. return result;
  5910. }
  5911. }
  5912. this.unexpected();
  5913. });
  5914. if (result) return result;
  5915. }
  5916. return super.parseSubscript(base, startPos, startLoc, noCalls, state);
  5917. }
  5918. parseNewArguments(node) {
  5919. if (this.isRelational("<")) {
  5920. const typeParameters = this.tsTryParseAndCatch(() => {
  5921. const args = this.tsParseTypeArguments();
  5922. if (!this.match(types$1.parenL)) this.unexpected();
  5923. return args;
  5924. });
  5925. if (typeParameters) {
  5926. node.typeParameters = typeParameters;
  5927. }
  5928. }
  5929. super.parseNewArguments(node);
  5930. }
  5931. parseExprOp(left, leftStartPos, leftStartLoc, minPrec) {
  5932. if (nonNull(types$1._in.binop) > minPrec && !this.hasPrecedingLineBreak() && this.isContextual("as")) {
  5933. const node = this.startNodeAt(leftStartPos, leftStartLoc);
  5934. node.expression = left;
  5935. const _const = this.tsTryNextParseConstantContext();
  5936. if (_const) {
  5937. node.typeAnnotation = _const;
  5938. } else {
  5939. node.typeAnnotation = this.tsNextThenParseType();
  5940. }
  5941. this.finishNode(node, "TSAsExpression");
  5942. this.reScan_lt_gt();
  5943. return this.parseExprOp(node, leftStartPos, leftStartLoc, minPrec);
  5944. }
  5945. return super.parseExprOp(left, leftStartPos, leftStartLoc, minPrec);
  5946. }
  5947. checkReservedWord(word, startLoc, checkKeywords, isBinding) {}
  5948. checkDuplicateExports() {}
  5949. parseImport(node) {
  5950. node.importKind = "value";
  5951. if (this.match(types$1.name) || this.match(types$1.star) || this.match(types$1.braceL)) {
  5952. let ahead = this.lookahead();
  5953. if (this.isContextual("type") && ahead.type !== types$1.comma && !(ahead.type === types$1.name && ahead.value === "from") && ahead.type !== types$1.eq) {
  5954. node.importKind = "type";
  5955. this.next();
  5956. ahead = this.lookahead();
  5957. }
  5958. if (this.match(types$1.name) && ahead.type === types$1.eq) {
  5959. return this.tsParseImportEqualsDeclaration(node);
  5960. }
  5961. }
  5962. const importNode = super.parseImport(node);
  5963. if (importNode.importKind === "type" && importNode.specifiers.length > 1 && importNode.specifiers[0].type === "ImportDefaultSpecifier") {
  5964. this.raise(importNode.start, TSErrors.TypeImportCannotSpecifyDefaultAndNamed);
  5965. }
  5966. return importNode;
  5967. }
  5968. parseExport(node) {
  5969. if (this.match(types$1._import)) {
  5970. this.next();
  5971. if (this.isContextual("type") && this.lookaheadCharCode() !== 61) {
  5972. node.importKind = "type";
  5973. this.next();
  5974. } else {
  5975. node.importKind = "value";
  5976. }
  5977. return this.tsParseImportEqualsDeclaration(node, true);
  5978. } else if (this.eat(types$1.eq)) {
  5979. const assign = node;
  5980. assign.expression = this.parseExpression();
  5981. this.semicolon();
  5982. return this.finishNode(assign, "TSExportAssignment");
  5983. } else if (this.eatContextual("as")) {
  5984. const decl = node;
  5985. this.expectContextual("namespace");
  5986. decl.id = this.parseIdentifier();
  5987. this.semicolon();
  5988. return this.finishNode(decl, "TSNamespaceExportDeclaration");
  5989. } else {
  5990. if (this.isContextual("type") && this.lookahead().type === types$1.braceL) {
  5991. this.next();
  5992. node.exportKind = "type";
  5993. } else {
  5994. node.exportKind = "value";
  5995. }
  5996. return super.parseExport(node);
  5997. }
  5998. }
  5999. isAbstractClass() {
  6000. return this.isContextual("abstract") && this.lookahead().type === types$1._class;
  6001. }
  6002. parseExportDefaultExpression() {
  6003. if (this.isAbstractClass()) {
  6004. const cls = this.startNode();
  6005. this.next();
  6006. cls.abstract = true;
  6007. this.parseClass(cls, true, true);
  6008. return cls;
  6009. }
  6010. if (this.state.value === "interface") {
  6011. const result = this.tsParseDeclaration(this.startNode(), this.state.value, true);
  6012. if (result) return result;
  6013. }
  6014. return super.parseExportDefaultExpression();
  6015. }
  6016. parseStatementContent(context, topLevel) {
  6017. if (this.state.type === types$1._const) {
  6018. const ahead = this.lookahead();
  6019. if (ahead.type === types$1.name && ahead.value === "enum") {
  6020. const node = this.startNode();
  6021. this.expect(types$1._const);
  6022. this.expectContextual("enum");
  6023. return this.tsParseEnumDeclaration(node, true);
  6024. }
  6025. }
  6026. return super.parseStatementContent(context, topLevel);
  6027. }
  6028. parseAccessModifier() {
  6029. return this.tsParseModifier(["public", "protected", "private"]);
  6030. }
  6031. tsHasSomeModifiers(member, modifiers) {
  6032. return modifiers.some(modifier => {
  6033. if (tsIsAccessModifier(modifier)) {
  6034. return member.accessibility === modifier;
  6035. }
  6036. return !!member[modifier];
  6037. });
  6038. }
  6039. parseClassMember(classBody, member, state) {
  6040. const invalidModifersForStaticBlocks = ["declare", "private", "public", "protected", "override", "abstract", "readonly"];
  6041. this.tsParseModifiers(member, invalidModifersForStaticBlocks.concat(["static"]));
  6042. const callParseClassMemberWithIsStatic = () => {
  6043. const isStatic = !!member.static;
  6044. if (isStatic && this.eat(types$1.braceL)) {
  6045. if (this.tsHasSomeModifiers(member, invalidModifersForStaticBlocks)) {
  6046. this.raise(this.state.pos, TSErrors.StaticBlockCannotHaveModifier);
  6047. }
  6048. this.parseClassStaticBlock(classBody, member);
  6049. } else {
  6050. this.parseClassMemberWithIsStatic(classBody, member, state, isStatic);
  6051. }
  6052. };
  6053. if (member.declare) {
  6054. this.tsInAmbientContext(callParseClassMemberWithIsStatic);
  6055. } else {
  6056. callParseClassMemberWithIsStatic();
  6057. }
  6058. }
  6059. parseClassMemberWithIsStatic(classBody, member, state, isStatic) {
  6060. const idx = this.tsTryParseIndexSignature(member);
  6061. if (idx) {
  6062. classBody.body.push(idx);
  6063. if (member.abstract) {
  6064. this.raise(member.start, TSErrors.IndexSignatureHasAbstract);
  6065. }
  6066. if (member.accessibility) {
  6067. this.raise(member.start, TSErrors.IndexSignatureHasAccessibility, member.accessibility);
  6068. }
  6069. if (member.declare) {
  6070. this.raise(member.start, TSErrors.IndexSignatureHasDeclare);
  6071. }
  6072. if (member.override) {
  6073. this.raise(member.start, TSErrors.IndexSignatureHasOverride);
  6074. }
  6075. return;
  6076. }
  6077. if (!this.state.inAbstractClass && member.abstract) {
  6078. this.raise(member.start, TSErrors.NonAbstractClassHasAbstractMethod);
  6079. }
  6080. if (member.override) {
  6081. if (!state.hadSuperClass) {
  6082. this.raise(member.start, TSErrors.OverrideNotInSubClass);
  6083. }
  6084. }
  6085. super.parseClassMemberWithIsStatic(classBody, member, state, isStatic);
  6086. }
  6087. parsePostMemberNameModifiers(methodOrProp) {
  6088. const optional = this.eat(types$1.question);
  6089. if (optional) methodOrProp.optional = true;
  6090. if (methodOrProp.readonly && this.match(types$1.parenL)) {
  6091. this.raise(methodOrProp.start, TSErrors.ClassMethodHasReadonly);
  6092. }
  6093. if (methodOrProp.declare && this.match(types$1.parenL)) {
  6094. this.raise(methodOrProp.start, TSErrors.ClassMethodHasDeclare);
  6095. }
  6096. }
  6097. parseExpressionStatement(node, expr) {
  6098. const decl = expr.type === "Identifier" ? this.tsParseExpressionStatement(node, expr) : undefined;
  6099. return decl || super.parseExpressionStatement(node, expr);
  6100. }
  6101. shouldParseExportDeclaration() {
  6102. if (this.tsIsDeclarationStart()) return true;
  6103. return super.shouldParseExportDeclaration();
  6104. }
  6105. parseConditional(expr, startPos, startLoc, refExpressionErrors) {
  6106. if (!this.state.maybeInArrowParameters || !this.match(types$1.question)) {
  6107. return super.parseConditional(expr, startPos, startLoc, refExpressionErrors);
  6108. }
  6109. const result = this.tryParse(() => super.parseConditional(expr, startPos, startLoc));
  6110. if (!result.node) {
  6111. if (result.error) {
  6112. super.setOptionalParametersError(refExpressionErrors, result.error);
  6113. }
  6114. return expr;
  6115. }
  6116. if (result.error) this.state = result.failState;
  6117. return result.node;
  6118. }
  6119. parseParenItem(node, startPos, startLoc) {
  6120. node = super.parseParenItem(node, startPos, startLoc);
  6121. if (this.eat(types$1.question)) {
  6122. node.optional = true;
  6123. this.resetEndLocation(node);
  6124. }
  6125. if (this.match(types$1.colon)) {
  6126. const typeCastNode = this.startNodeAt(startPos, startLoc);
  6127. typeCastNode.expression = node;
  6128. typeCastNode.typeAnnotation = this.tsParseTypeAnnotation();
  6129. return this.finishNode(typeCastNode, "TSTypeCastExpression");
  6130. }
  6131. return node;
  6132. }
  6133. parseExportDeclaration(node) {
  6134. const startPos = this.state.start;
  6135. const startLoc = this.state.startLoc;
  6136. const isDeclare = this.eatContextual("declare");
  6137. if (isDeclare && (this.isContextual("declare") || !this.shouldParseExportDeclaration())) {
  6138. throw this.raise(this.state.start, TSErrors.ExpectedAmbientAfterExportDeclare);
  6139. }
  6140. let declaration;
  6141. if (this.match(types$1.name)) {
  6142. declaration = this.tsTryParseExportDeclaration();
  6143. }
  6144. if (!declaration) {
  6145. declaration = super.parseExportDeclaration(node);
  6146. }
  6147. if (declaration && (declaration.type === "TSInterfaceDeclaration" || declaration.type === "TSTypeAliasDeclaration" || isDeclare)) {
  6148. node.exportKind = "type";
  6149. }
  6150. if (declaration && isDeclare) {
  6151. this.resetStartLocation(declaration, startPos, startLoc);
  6152. declaration.declare = true;
  6153. }
  6154. return declaration;
  6155. }
  6156. parseClassId(node, isStatement, optionalId) {
  6157. if ((!isStatement || optionalId) && this.isContextual("implements")) {
  6158. return;
  6159. }
  6160. super.parseClassId(node, isStatement, optionalId, node.declare ? BIND_TS_AMBIENT : BIND_CLASS);
  6161. const typeParameters = this.tsTryParseTypeParameters();
  6162. if (typeParameters) node.typeParameters = typeParameters;
  6163. }
  6164. parseClassPropertyAnnotation(node) {
  6165. if (!node.optional && this.eat(types$1.bang)) {
  6166. node.definite = true;
  6167. }
  6168. const type = this.tsTryParseTypeAnnotation();
  6169. if (type) node.typeAnnotation = type;
  6170. }
  6171. parseClassProperty(node) {
  6172. this.parseClassPropertyAnnotation(node);
  6173. if (this.state.isAmbientContext && this.match(types$1.eq)) {
  6174. this.raise(this.state.start, TSErrors.DeclareClassFieldHasInitializer);
  6175. }
  6176. return super.parseClassProperty(node);
  6177. }
  6178. parseClassPrivateProperty(node) {
  6179. if (node.abstract) {
  6180. this.raise(node.start, TSErrors.PrivateElementHasAbstract);
  6181. }
  6182. if (node.accessibility) {
  6183. this.raise(node.start, TSErrors.PrivateElementHasAccessibility, node.accessibility);
  6184. }
  6185. this.parseClassPropertyAnnotation(node);
  6186. return super.parseClassPrivateProperty(node);
  6187. }
  6188. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  6189. const typeParameters = this.tsTryParseTypeParameters();
  6190. if (typeParameters && isConstructor) {
  6191. this.raise(typeParameters.start, TSErrors.ConstructorHasTypeParameters);
  6192. }
  6193. if (method.declare && (method.kind === "get" || method.kind === "set")) {
  6194. this.raise(method.start, TSErrors.DeclareAccessor, method.kind);
  6195. }
  6196. if (typeParameters) method.typeParameters = typeParameters;
  6197. super.pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper);
  6198. }
  6199. pushClassPrivateMethod(classBody, method, isGenerator, isAsync) {
  6200. const typeParameters = this.tsTryParseTypeParameters();
  6201. if (typeParameters) method.typeParameters = typeParameters;
  6202. super.pushClassPrivateMethod(classBody, method, isGenerator, isAsync);
  6203. }
  6204. parseClassSuper(node) {
  6205. super.parseClassSuper(node);
  6206. if (node.superClass && this.isRelational("<")) {
  6207. node.superTypeParameters = this.tsParseTypeArguments();
  6208. }
  6209. if (this.eatContextual("implements")) {
  6210. node.implements = this.tsParseHeritageClause("implements");
  6211. }
  6212. }
  6213. parseObjPropValue(prop, ...args) {
  6214. const typeParameters = this.tsTryParseTypeParameters();
  6215. if (typeParameters) prop.typeParameters = typeParameters;
  6216. super.parseObjPropValue(prop, ...args);
  6217. }
  6218. parseFunctionParams(node, allowModifiers) {
  6219. const typeParameters = this.tsTryParseTypeParameters();
  6220. if (typeParameters) node.typeParameters = typeParameters;
  6221. super.parseFunctionParams(node, allowModifiers);
  6222. }
  6223. parseVarId(decl, kind) {
  6224. super.parseVarId(decl, kind);
  6225. if (decl.id.type === "Identifier" && this.eat(types$1.bang)) {
  6226. decl.definite = true;
  6227. }
  6228. const type = this.tsTryParseTypeAnnotation();
  6229. if (type) {
  6230. decl.id.typeAnnotation = type;
  6231. this.resetEndLocation(decl.id);
  6232. }
  6233. }
  6234. parseAsyncArrowFromCallExpression(node, call) {
  6235. if (this.match(types$1.colon)) {
  6236. node.returnType = this.tsParseTypeAnnotation();
  6237. }
  6238. return super.parseAsyncArrowFromCallExpression(node, call);
  6239. }
  6240. parseMaybeAssign(...args) {
  6241. var _jsx, _jsx2, _typeCast, _jsx3, _typeCast2, _jsx4, _typeCast3;
  6242. let state;
  6243. let jsx;
  6244. let typeCast;
  6245. if (this.hasPlugin("jsx") && (this.match(types$1.jsxTagStart) || this.isRelational("<"))) {
  6246. state = this.state.clone();
  6247. jsx = this.tryParse(() => super.parseMaybeAssign(...args), state);
  6248. if (!jsx.error) return jsx.node;
  6249. const {
  6250. context
  6251. } = this.state;
  6252. if (context[context.length - 1] === types.j_oTag) {
  6253. context.length -= 2;
  6254. } else if (context[context.length - 1] === types.j_expr) {
  6255. context.length -= 1;
  6256. }
  6257. }
  6258. if (!((_jsx = jsx) != null && _jsx.error) && !this.isRelational("<")) {
  6259. return super.parseMaybeAssign(...args);
  6260. }
  6261. let typeParameters;
  6262. state = state || this.state.clone();
  6263. const arrow = this.tryParse(abort => {
  6264. var _expr$extra, _typeParameters;
  6265. typeParameters = this.tsParseTypeParameters();
  6266. const expr = super.parseMaybeAssign(...args);
  6267. if (expr.type !== "ArrowFunctionExpression" || (_expr$extra = expr.extra) != null && _expr$extra.parenthesized) {
  6268. abort();
  6269. }
  6270. if (((_typeParameters = typeParameters) == null ? void 0 : _typeParameters.params.length) !== 0) {
  6271. this.resetStartLocationFromNode(expr, typeParameters);
  6272. }
  6273. expr.typeParameters = typeParameters;
  6274. return expr;
  6275. }, state);
  6276. if (!arrow.error && !arrow.aborted) return arrow.node;
  6277. if (!jsx) {
  6278. assert(!this.hasPlugin("jsx"));
  6279. typeCast = this.tryParse(() => super.parseMaybeAssign(...args), state);
  6280. if (!typeCast.error) return typeCast.node;
  6281. }
  6282. if ((_jsx2 = jsx) != null && _jsx2.node) {
  6283. this.state = jsx.failState;
  6284. return jsx.node;
  6285. }
  6286. if (arrow.node) {
  6287. this.state = arrow.failState;
  6288. return arrow.node;
  6289. }
  6290. if ((_typeCast = typeCast) != null && _typeCast.node) {
  6291. this.state = typeCast.failState;
  6292. return typeCast.node;
  6293. }
  6294. if ((_jsx3 = jsx) != null && _jsx3.thrown) throw jsx.error;
  6295. if (arrow.thrown) throw arrow.error;
  6296. if ((_typeCast2 = typeCast) != null && _typeCast2.thrown) throw typeCast.error;
  6297. throw ((_jsx4 = jsx) == null ? void 0 : _jsx4.error) || arrow.error || ((_typeCast3 = typeCast) == null ? void 0 : _typeCast3.error);
  6298. }
  6299. parseMaybeUnary(refExpressionErrors) {
  6300. if (!this.hasPlugin("jsx") && this.isRelational("<")) {
  6301. return this.tsParseTypeAssertion();
  6302. } else {
  6303. return super.parseMaybeUnary(refExpressionErrors);
  6304. }
  6305. }
  6306. parseArrow(node) {
  6307. if (this.match(types$1.colon)) {
  6308. const result = this.tryParse(abort => {
  6309. const returnType = this.tsParseTypeOrTypePredicateAnnotation(types$1.colon);
  6310. if (this.canInsertSemicolon() || !this.match(types$1.arrow)) abort();
  6311. return returnType;
  6312. });
  6313. if (result.aborted) return;
  6314. if (!result.thrown) {
  6315. if (result.error) this.state = result.failState;
  6316. node.returnType = result.node;
  6317. }
  6318. }
  6319. return super.parseArrow(node);
  6320. }
  6321. parseAssignableListItemTypes(param) {
  6322. if (this.eat(types$1.question)) {
  6323. if (param.type !== "Identifier" && !this.state.isAmbientContext && !this.state.inType) {
  6324. this.raise(param.start, TSErrors.PatternIsOptional);
  6325. }
  6326. param.optional = true;
  6327. }
  6328. const type = this.tsTryParseTypeAnnotation();
  6329. if (type) param.typeAnnotation = type;
  6330. this.resetEndLocation(param);
  6331. return param;
  6332. }
  6333. toAssignable(node, isLHS = false) {
  6334. switch (node.type) {
  6335. case "TSTypeCastExpression":
  6336. return super.toAssignable(this.typeCastToParameter(node), isLHS);
  6337. case "TSParameterProperty":
  6338. return super.toAssignable(node, isLHS);
  6339. case "ParenthesizedExpression":
  6340. return this.toAssignableParenthesizedExpression(node, isLHS);
  6341. case "TSAsExpression":
  6342. case "TSNonNullExpression":
  6343. case "TSTypeAssertion":
  6344. node.expression = this.toAssignable(node.expression, isLHS);
  6345. return node;
  6346. default:
  6347. return super.toAssignable(node, isLHS);
  6348. }
  6349. }
  6350. toAssignableParenthesizedExpression(node, isLHS) {
  6351. switch (node.expression.type) {
  6352. case "TSAsExpression":
  6353. case "TSNonNullExpression":
  6354. case "TSTypeAssertion":
  6355. case "ParenthesizedExpression":
  6356. node.expression = this.toAssignable(node.expression, isLHS);
  6357. return node;
  6358. default:
  6359. return super.toAssignable(node, isLHS);
  6360. }
  6361. }
  6362. checkLVal(expr, contextDescription, ...args) {
  6363. var _expr$extra2;
  6364. switch (expr.type) {
  6365. case "TSTypeCastExpression":
  6366. return;
  6367. case "TSParameterProperty":
  6368. this.checkLVal(expr.parameter, "parameter property", ...args);
  6369. return;
  6370. case "TSAsExpression":
  6371. case "TSTypeAssertion":
  6372. if (!args[0] && contextDescription !== "parenthesized expression" && !((_expr$extra2 = expr.extra) != null && _expr$extra2.parenthesized)) {
  6373. this.raise(expr.start, ErrorMessages.InvalidLhs, contextDescription);
  6374. break;
  6375. }
  6376. this.checkLVal(expr.expression, "parenthesized expression", ...args);
  6377. return;
  6378. case "TSNonNullExpression":
  6379. this.checkLVal(expr.expression, contextDescription, ...args);
  6380. return;
  6381. default:
  6382. super.checkLVal(expr, contextDescription, ...args);
  6383. return;
  6384. }
  6385. }
  6386. parseBindingAtom() {
  6387. switch (this.state.type) {
  6388. case types$1._this:
  6389. return this.parseIdentifier(true);
  6390. default:
  6391. return super.parseBindingAtom();
  6392. }
  6393. }
  6394. parseMaybeDecoratorArguments(expr) {
  6395. if (this.isRelational("<")) {
  6396. const typeArguments = this.tsParseTypeArguments();
  6397. if (this.match(types$1.parenL)) {
  6398. const call = super.parseMaybeDecoratorArguments(expr);
  6399. call.typeParameters = typeArguments;
  6400. return call;
  6401. }
  6402. this.unexpected(this.state.start, types$1.parenL);
  6403. }
  6404. return super.parseMaybeDecoratorArguments(expr);
  6405. }
  6406. checkCommaAfterRest(close) {
  6407. if (this.state.isAmbientContext && this.match(types$1.comma) && this.lookaheadCharCode() === close) {
  6408. this.next();
  6409. } else {
  6410. super.checkCommaAfterRest(close);
  6411. }
  6412. }
  6413. isClassMethod() {
  6414. return this.isRelational("<") || super.isClassMethod();
  6415. }
  6416. isClassProperty() {
  6417. return this.match(types$1.bang) || this.match(types$1.colon) || super.isClassProperty();
  6418. }
  6419. parseMaybeDefault(...args) {
  6420. const node = super.parseMaybeDefault(...args);
  6421. if (node.type === "AssignmentPattern" && node.typeAnnotation && node.right.start < node.typeAnnotation.start) {
  6422. this.raise(node.typeAnnotation.start, TSErrors.TypeAnnotationAfterAssign);
  6423. }
  6424. return node;
  6425. }
  6426. getTokenFromCode(code) {
  6427. if (this.state.inType && (code === 62 || code === 60)) {
  6428. return this.finishOp(types$1.relational, 1);
  6429. } else {
  6430. return super.getTokenFromCode(code);
  6431. }
  6432. }
  6433. reScan_lt_gt() {
  6434. if (this.match(types$1.relational)) {
  6435. const code = this.input.charCodeAt(this.state.start);
  6436. if (code === 60 || code === 62) {
  6437. this.state.pos -= 1;
  6438. this.readToken_lt_gt(code);
  6439. }
  6440. }
  6441. }
  6442. toAssignableList(exprList) {
  6443. for (let i = 0; i < exprList.length; i++) {
  6444. const expr = exprList[i];
  6445. if (!expr) continue;
  6446. switch (expr.type) {
  6447. case "TSTypeCastExpression":
  6448. exprList[i] = this.typeCastToParameter(expr);
  6449. break;
  6450. case "TSAsExpression":
  6451. case "TSTypeAssertion":
  6452. if (!this.state.maybeInArrowParameters) {
  6453. exprList[i] = this.typeCastToParameter(expr);
  6454. } else {
  6455. this.raise(expr.start, TSErrors.UnexpectedTypeCastInParameter);
  6456. }
  6457. break;
  6458. }
  6459. }
  6460. return super.toAssignableList(...arguments);
  6461. }
  6462. typeCastToParameter(node) {
  6463. node.expression.typeAnnotation = node.typeAnnotation;
  6464. this.resetEndLocation(node.expression, node.typeAnnotation.end, node.typeAnnotation.loc.end);
  6465. return node.expression;
  6466. }
  6467. shouldParseArrow() {
  6468. return this.match(types$1.colon) || super.shouldParseArrow();
  6469. }
  6470. shouldParseAsyncArrow() {
  6471. return this.match(types$1.colon) || super.shouldParseAsyncArrow();
  6472. }
  6473. canHaveLeadingDecorator() {
  6474. return super.canHaveLeadingDecorator() || this.isAbstractClass();
  6475. }
  6476. jsxParseOpeningElementAfterName(node) {
  6477. if (this.isRelational("<")) {
  6478. const typeArguments = this.tsTryParseAndCatch(() => this.tsParseTypeArguments());
  6479. if (typeArguments) node.typeParameters = typeArguments;
  6480. }
  6481. return super.jsxParseOpeningElementAfterName(node);
  6482. }
  6483. getGetterSetterExpectedParamCount(method) {
  6484. const baseCount = super.getGetterSetterExpectedParamCount(method);
  6485. const params = this.getObjectOrClassMethodParams(method);
  6486. const firstParam = params[0];
  6487. const hasContextParam = firstParam && this.isThisParam(firstParam);
  6488. return hasContextParam ? baseCount + 1 : baseCount;
  6489. }
  6490. parseCatchClauseParam() {
  6491. const param = super.parseCatchClauseParam();
  6492. const type = this.tsTryParseTypeAnnotation();
  6493. if (type) {
  6494. param.typeAnnotation = type;
  6495. this.resetEndLocation(param);
  6496. }
  6497. return param;
  6498. }
  6499. tsInAmbientContext(cb) {
  6500. const oldIsAmbientContext = this.state.isAmbientContext;
  6501. this.state.isAmbientContext = true;
  6502. try {
  6503. return cb();
  6504. } finally {
  6505. this.state.isAmbientContext = oldIsAmbientContext;
  6506. }
  6507. }
  6508. parseClass(node, ...args) {
  6509. const oldInAbstractClass = this.state.inAbstractClass;
  6510. this.state.inAbstractClass = !!node.abstract;
  6511. try {
  6512. return super.parseClass(node, ...args);
  6513. } finally {
  6514. this.state.inAbstractClass = oldInAbstractClass;
  6515. }
  6516. }
  6517. tsParseAbstractDeclaration(node) {
  6518. if (this.match(types$1._class)) {
  6519. node.abstract = true;
  6520. return this.parseClass(node, true, false);
  6521. } else if (this.isContextual("interface")) {
  6522. if (!this.hasFollowingLineBreak()) {
  6523. node.abstract = true;
  6524. this.raise(node.start, TSErrors.NonClassMethodPropertyHasAbstractModifer);
  6525. this.next();
  6526. return this.tsParseInterfaceDeclaration(node);
  6527. }
  6528. } else {
  6529. this.unexpected(null, types$1._class);
  6530. }
  6531. }
  6532. parseMethod(...args) {
  6533. const method = super.parseMethod(...args);
  6534. if (method.abstract) {
  6535. const hasBody = this.hasPlugin("estree") ? !!method.value.body : !!method.body;
  6536. if (hasBody) {
  6537. const {
  6538. key
  6539. } = method;
  6540. this.raise(method.start, TSErrors.AbstractMethodHasImplementation, key.type === "Identifier" ? key.name : `[${this.input.slice(key.start, key.end)}]`);
  6541. }
  6542. }
  6543. return method;
  6544. }
  6545. shouldParseAsAmbientContext() {
  6546. return !!this.getPluginOption("typescript", "dts");
  6547. }
  6548. parse() {
  6549. if (this.shouldParseAsAmbientContext()) {
  6550. this.state.isAmbientContext = true;
  6551. }
  6552. return super.parse();
  6553. }
  6554. getExpression() {
  6555. if (this.shouldParseAsAmbientContext()) {
  6556. this.state.isAmbientContext = true;
  6557. }
  6558. return super.getExpression();
  6559. }
  6560. });
  6561. types$1.placeholder = new TokenType("%%", {
  6562. startsExpr: true
  6563. });
  6564. const PlaceHolderErrors = makeErrorTemplates({
  6565. ClassNameIsRequired: "A class name is required."
  6566. }, ErrorCodes.SyntaxError);
  6567. var placeholders = (superClass => class extends superClass {
  6568. parsePlaceholder(expectedNode) {
  6569. if (this.match(types$1.placeholder)) {
  6570. const node = this.startNode();
  6571. this.next();
  6572. this.assertNoSpace("Unexpected space in placeholder.");
  6573. node.name = super.parseIdentifier(true);
  6574. this.assertNoSpace("Unexpected space in placeholder.");
  6575. this.expect(types$1.placeholder);
  6576. return this.finishPlaceholder(node, expectedNode);
  6577. }
  6578. }
  6579. finishPlaceholder(node, expectedNode) {
  6580. const isFinished = !!(node.expectedNode && node.type === "Placeholder");
  6581. node.expectedNode = expectedNode;
  6582. return isFinished ? node : this.finishNode(node, "Placeholder");
  6583. }
  6584. getTokenFromCode(code) {
  6585. if (code === 37 && this.input.charCodeAt(this.state.pos + 1) === 37) {
  6586. return this.finishOp(types$1.placeholder, 2);
  6587. }
  6588. return super.getTokenFromCode(...arguments);
  6589. }
  6590. parseExprAtom() {
  6591. return this.parsePlaceholder("Expression") || super.parseExprAtom(...arguments);
  6592. }
  6593. parseIdentifier() {
  6594. return this.parsePlaceholder("Identifier") || super.parseIdentifier(...arguments);
  6595. }
  6596. checkReservedWord(word) {
  6597. if (word !== undefined) super.checkReservedWord(...arguments);
  6598. }
  6599. parseBindingAtom() {
  6600. return this.parsePlaceholder("Pattern") || super.parseBindingAtom(...arguments);
  6601. }
  6602. checkLVal(expr) {
  6603. if (expr.type !== "Placeholder") super.checkLVal(...arguments);
  6604. }
  6605. toAssignable(node) {
  6606. if (node && node.type === "Placeholder" && node.expectedNode === "Expression") {
  6607. node.expectedNode = "Pattern";
  6608. return node;
  6609. }
  6610. return super.toAssignable(...arguments);
  6611. }
  6612. isLet(context) {
  6613. if (super.isLet(context)) {
  6614. return true;
  6615. }
  6616. if (!this.isContextual("let")) {
  6617. return false;
  6618. }
  6619. if (context) return false;
  6620. const nextToken = this.lookahead();
  6621. if (nextToken.type === types$1.placeholder) {
  6622. return true;
  6623. }
  6624. return false;
  6625. }
  6626. verifyBreakContinue(node) {
  6627. if (node.label && node.label.type === "Placeholder") return;
  6628. super.verifyBreakContinue(...arguments);
  6629. }
  6630. parseExpressionStatement(node, expr) {
  6631. if (expr.type !== "Placeholder" || expr.extra && expr.extra.parenthesized) {
  6632. return super.parseExpressionStatement(...arguments);
  6633. }
  6634. if (this.match(types$1.colon)) {
  6635. const stmt = node;
  6636. stmt.label = this.finishPlaceholder(expr, "Identifier");
  6637. this.next();
  6638. stmt.body = this.parseStatement("label");
  6639. return this.finishNode(stmt, "LabeledStatement");
  6640. }
  6641. this.semicolon();
  6642. node.name = expr.name;
  6643. return this.finishPlaceholder(node, "Statement");
  6644. }
  6645. parseBlock() {
  6646. return this.parsePlaceholder("BlockStatement") || super.parseBlock(...arguments);
  6647. }
  6648. parseFunctionId() {
  6649. return this.parsePlaceholder("Identifier") || super.parseFunctionId(...arguments);
  6650. }
  6651. parseClass(node, isStatement, optionalId) {
  6652. const type = isStatement ? "ClassDeclaration" : "ClassExpression";
  6653. this.next();
  6654. this.takeDecorators(node);
  6655. const oldStrict = this.state.strict;
  6656. const placeholder = this.parsePlaceholder("Identifier");
  6657. if (placeholder) {
  6658. if (this.match(types$1._extends) || this.match(types$1.placeholder) || this.match(types$1.braceL)) {
  6659. node.id = placeholder;
  6660. } else if (optionalId || !isStatement) {
  6661. node.id = null;
  6662. node.body = this.finishPlaceholder(placeholder, "ClassBody");
  6663. return this.finishNode(node, type);
  6664. } else {
  6665. this.unexpected(null, PlaceHolderErrors.ClassNameIsRequired);
  6666. }
  6667. } else {
  6668. this.parseClassId(node, isStatement, optionalId);
  6669. }
  6670. this.parseClassSuper(node);
  6671. node.body = this.parsePlaceholder("ClassBody") || this.parseClassBody(!!node.superClass, oldStrict);
  6672. return this.finishNode(node, type);
  6673. }
  6674. parseExport(node) {
  6675. const placeholder = this.parsePlaceholder("Identifier");
  6676. if (!placeholder) return super.parseExport(...arguments);
  6677. if (!this.isContextual("from") && !this.match(types$1.comma)) {
  6678. node.specifiers = [];
  6679. node.source = null;
  6680. node.declaration = this.finishPlaceholder(placeholder, "Declaration");
  6681. return this.finishNode(node, "ExportNamedDeclaration");
  6682. }
  6683. this.expectPlugin("exportDefaultFrom");
  6684. const specifier = this.startNode();
  6685. specifier.exported = placeholder;
  6686. node.specifiers = [this.finishNode(specifier, "ExportDefaultSpecifier")];
  6687. return super.parseExport(node);
  6688. }
  6689. isExportDefaultSpecifier() {
  6690. if (this.match(types$1._default)) {
  6691. const next = this.nextTokenStart();
  6692. if (this.isUnparsedContextual(next, "from")) {
  6693. if (this.input.startsWith(types$1.placeholder.label, this.nextTokenStartSince(next + 4))) {
  6694. return true;
  6695. }
  6696. }
  6697. }
  6698. return super.isExportDefaultSpecifier();
  6699. }
  6700. maybeParseExportDefaultSpecifier(node) {
  6701. if (node.specifiers && node.specifiers.length > 0) {
  6702. return true;
  6703. }
  6704. return super.maybeParseExportDefaultSpecifier(...arguments);
  6705. }
  6706. checkExport(node) {
  6707. const {
  6708. specifiers
  6709. } = node;
  6710. if (specifiers != null && specifiers.length) {
  6711. node.specifiers = specifiers.filter(node => node.exported.type === "Placeholder");
  6712. }
  6713. super.checkExport(node);
  6714. node.specifiers = specifiers;
  6715. }
  6716. parseImport(node) {
  6717. const placeholder = this.parsePlaceholder("Identifier");
  6718. if (!placeholder) return super.parseImport(...arguments);
  6719. node.specifiers = [];
  6720. if (!this.isContextual("from") && !this.match(types$1.comma)) {
  6721. node.source = this.finishPlaceholder(placeholder, "StringLiteral");
  6722. this.semicolon();
  6723. return this.finishNode(node, "ImportDeclaration");
  6724. }
  6725. const specifier = this.startNodeAtNode(placeholder);
  6726. specifier.local = placeholder;
  6727. this.finishNode(specifier, "ImportDefaultSpecifier");
  6728. node.specifiers.push(specifier);
  6729. if (this.eat(types$1.comma)) {
  6730. const hasStarImport = this.maybeParseStarImportSpecifier(node);
  6731. if (!hasStarImport) this.parseNamedImportSpecifiers(node);
  6732. }
  6733. this.expectContextual("from");
  6734. node.source = this.parseImportSource();
  6735. this.semicolon();
  6736. return this.finishNode(node, "ImportDeclaration");
  6737. }
  6738. parseImportSource() {
  6739. return this.parsePlaceholder("StringLiteral") || super.parseImportSource(...arguments);
  6740. }
  6741. });
  6742. var v8intrinsic = (superClass => class extends superClass {
  6743. parseV8Intrinsic() {
  6744. if (this.match(types$1.modulo)) {
  6745. const v8IntrinsicStart = this.state.start;
  6746. const node = this.startNode();
  6747. this.eat(types$1.modulo);
  6748. if (this.match(types$1.name)) {
  6749. const name = this.parseIdentifierName(this.state.start);
  6750. const identifier = this.createIdentifier(node, name);
  6751. identifier.type = "V8IntrinsicIdentifier";
  6752. if (this.match(types$1.parenL)) {
  6753. return identifier;
  6754. }
  6755. }
  6756. this.unexpected(v8IntrinsicStart);
  6757. }
  6758. }
  6759. parseExprAtom() {
  6760. return this.parseV8Intrinsic() || super.parseExprAtom(...arguments);
  6761. }
  6762. });
  6763. function hasPlugin(plugins, name) {
  6764. return plugins.some(plugin => {
  6765. if (Array.isArray(plugin)) {
  6766. return plugin[0] === name;
  6767. } else {
  6768. return plugin === name;
  6769. }
  6770. });
  6771. }
  6772. function getPluginOption(plugins, name, option) {
  6773. const plugin = plugins.find(plugin => {
  6774. if (Array.isArray(plugin)) {
  6775. return plugin[0] === name;
  6776. } else {
  6777. return plugin === name;
  6778. }
  6779. });
  6780. if (plugin && Array.isArray(plugin)) {
  6781. return plugin[1][option];
  6782. }
  6783. return null;
  6784. }
  6785. const PIPELINE_PROPOSALS = ["minimal", "smart", "fsharp"];
  6786. const RECORD_AND_TUPLE_SYNTAX_TYPES = ["hash", "bar"];
  6787. function validatePlugins(plugins) {
  6788. if (hasPlugin(plugins, "decorators")) {
  6789. if (hasPlugin(plugins, "decorators-legacy")) {
  6790. throw new Error("Cannot use the decorators and decorators-legacy plugin together");
  6791. }
  6792. const decoratorsBeforeExport = getPluginOption(plugins, "decorators", "decoratorsBeforeExport");
  6793. if (decoratorsBeforeExport == null) {
  6794. throw new Error("The 'decorators' plugin requires a 'decoratorsBeforeExport' option," + " whose value must be a boolean. If you are migrating from" + " Babylon/Babel 6 or want to use the old decorators proposal, you" + " should use the 'decorators-legacy' plugin instead of 'decorators'.");
  6795. } else if (typeof decoratorsBeforeExport !== "boolean") {
  6796. throw new Error("'decoratorsBeforeExport' must be a boolean.");
  6797. }
  6798. }
  6799. if (hasPlugin(plugins, "flow") && hasPlugin(plugins, "typescript")) {
  6800. throw new Error("Cannot combine flow and typescript plugins.");
  6801. }
  6802. if (hasPlugin(plugins, "placeholders") && hasPlugin(plugins, "v8intrinsic")) {
  6803. throw new Error("Cannot combine placeholders and v8intrinsic plugins.");
  6804. }
  6805. if (hasPlugin(plugins, "pipelineOperator") && !PIPELINE_PROPOSALS.includes(getPluginOption(plugins, "pipelineOperator", "proposal"))) {
  6806. throw new Error("'pipelineOperator' requires 'proposal' option whose value should be one of: " + PIPELINE_PROPOSALS.map(p => `'${p}'`).join(", "));
  6807. }
  6808. if (hasPlugin(plugins, "moduleAttributes")) {
  6809. {
  6810. if (hasPlugin(plugins, "importAssertions")) {
  6811. throw new Error("Cannot combine importAssertions and moduleAttributes plugins.");
  6812. }
  6813. const moduleAttributesVerionPluginOption = getPluginOption(plugins, "moduleAttributes", "version");
  6814. if (moduleAttributesVerionPluginOption !== "may-2020") {
  6815. throw new Error("The 'moduleAttributes' plugin requires a 'version' option," + " representing the last proposal update. Currently, the" + " only supported value is 'may-2020'.");
  6816. }
  6817. }
  6818. }
  6819. if (hasPlugin(plugins, "recordAndTuple") && !RECORD_AND_TUPLE_SYNTAX_TYPES.includes(getPluginOption(plugins, "recordAndTuple", "syntaxType"))) {
  6820. throw new Error("'recordAndTuple' requires 'syntaxType' option whose value should be one of: " + RECORD_AND_TUPLE_SYNTAX_TYPES.map(p => `'${p}'`).join(", "));
  6821. }
  6822. if (hasPlugin(plugins, "asyncDoExpressions") && !hasPlugin(plugins, "doExpressions")) {
  6823. const error = new Error("'asyncDoExpressions' requires 'doExpressions', please add 'doExpressions' to parser plugins.");
  6824. error.missingPlugins = "doExpressions";
  6825. throw error;
  6826. }
  6827. }
  6828. const mixinPlugins = {
  6829. estree,
  6830. jsx,
  6831. flow,
  6832. typescript,
  6833. v8intrinsic,
  6834. placeholders
  6835. };
  6836. const mixinPluginNames = Object.keys(mixinPlugins);
  6837. const defaultOptions = {
  6838. sourceType: "script",
  6839. sourceFilename: undefined,
  6840. startLine: 1,
  6841. allowAwaitOutsideFunction: false,
  6842. allowReturnOutsideFunction: false,
  6843. allowImportExportEverywhere: false,
  6844. allowSuperOutsideMethod: false,
  6845. allowUndeclaredExports: false,
  6846. plugins: [],
  6847. strictMode: null,
  6848. ranges: false,
  6849. tokens: false,
  6850. createParenthesizedExpressions: false,
  6851. errorRecovery: false
  6852. };
  6853. function getOptions(opts) {
  6854. const options = {};
  6855. for (const key of Object.keys(defaultOptions)) {
  6856. options[key] = opts && opts[key] != null ? opts[key] : defaultOptions[key];
  6857. }
  6858. return options;
  6859. }
  6860. var _isDigit = function isDigit(code) {
  6861. return code >= 48 && code <= 57;
  6862. };
  6863. const VALID_REGEX_FLAGS = new Set([103, 109, 115, 105, 121, 117, 100]);
  6864. const forbiddenNumericSeparatorSiblings = {
  6865. decBinOct: [46, 66, 69, 79, 95, 98, 101, 111],
  6866. hex: [46, 88, 95, 120]
  6867. };
  6868. const allowedNumericSeparatorSiblings = {};
  6869. allowedNumericSeparatorSiblings.bin = [48, 49];
  6870. allowedNumericSeparatorSiblings.oct = [...allowedNumericSeparatorSiblings.bin, 50, 51, 52, 53, 54, 55];
  6871. allowedNumericSeparatorSiblings.dec = [...allowedNumericSeparatorSiblings.oct, 56, 57];
  6872. allowedNumericSeparatorSiblings.hex = [...allowedNumericSeparatorSiblings.dec, 65, 66, 67, 68, 69, 70, 97, 98, 99, 100, 101, 102];
  6873. class Token {
  6874. constructor(state) {
  6875. this.type = state.type;
  6876. this.value = state.value;
  6877. this.start = state.start;
  6878. this.end = state.end;
  6879. this.loc = new SourceLocation(state.startLoc, state.endLoc);
  6880. }
  6881. }
  6882. class Tokenizer extends ParserError {
  6883. constructor(options, input) {
  6884. super();
  6885. this.isLookahead = void 0;
  6886. this.tokens = [];
  6887. this.state = new State();
  6888. this.state.init(options);
  6889. this.input = input;
  6890. this.length = input.length;
  6891. this.isLookahead = false;
  6892. }
  6893. pushToken(token) {
  6894. this.tokens.length = this.state.tokensLength;
  6895. this.tokens.push(token);
  6896. ++this.state.tokensLength;
  6897. }
  6898. next() {
  6899. this.checkKeywordEscapes();
  6900. if (this.options.tokens) {
  6901. this.pushToken(new Token(this.state));
  6902. }
  6903. this.state.lastTokEnd = this.state.end;
  6904. this.state.lastTokStart = this.state.start;
  6905. this.state.lastTokEndLoc = this.state.endLoc;
  6906. this.state.lastTokStartLoc = this.state.startLoc;
  6907. this.nextToken();
  6908. }
  6909. eat(type) {
  6910. if (this.match(type)) {
  6911. this.next();
  6912. return true;
  6913. } else {
  6914. return false;
  6915. }
  6916. }
  6917. match(type) {
  6918. return this.state.type === type;
  6919. }
  6920. createLookaheadState(state) {
  6921. return {
  6922. pos: state.pos,
  6923. value: null,
  6924. type: state.type,
  6925. start: state.start,
  6926. end: state.end,
  6927. lastTokEnd: state.end,
  6928. context: [this.curContext()],
  6929. inType: state.inType
  6930. };
  6931. }
  6932. lookahead() {
  6933. const old = this.state;
  6934. this.state = this.createLookaheadState(old);
  6935. this.isLookahead = true;
  6936. this.nextToken();
  6937. this.isLookahead = false;
  6938. const curr = this.state;
  6939. this.state = old;
  6940. return curr;
  6941. }
  6942. nextTokenStart() {
  6943. return this.nextTokenStartSince(this.state.pos);
  6944. }
  6945. nextTokenStartSince(pos) {
  6946. skipWhiteSpace.lastIndex = pos;
  6947. const skip = skipWhiteSpace.exec(this.input);
  6948. return pos + skip[0].length;
  6949. }
  6950. lookaheadCharCode() {
  6951. return this.input.charCodeAt(this.nextTokenStart());
  6952. }
  6953. codePointAtPos(pos) {
  6954. let cp = this.input.charCodeAt(pos);
  6955. if ((cp & 0xfc00) === 0xd800 && ++pos < this.input.length) {
  6956. const trail = this.input.charCodeAt(pos);
  6957. if ((trail & 0xfc00) === 0xdc00) {
  6958. cp = 0x10000 + ((cp & 0x3ff) << 10) + (trail & 0x3ff);
  6959. }
  6960. }
  6961. return cp;
  6962. }
  6963. setStrict(strict) {
  6964. this.state.strict = strict;
  6965. if (strict) {
  6966. this.state.strictErrors.forEach((message, pos) => this.raise(pos, message));
  6967. this.state.strictErrors.clear();
  6968. }
  6969. }
  6970. curContext() {
  6971. return this.state.context[this.state.context.length - 1];
  6972. }
  6973. nextToken() {
  6974. const curContext = this.curContext();
  6975. if (!curContext.preserveSpace) this.skipSpace();
  6976. this.state.start = this.state.pos;
  6977. if (!this.isLookahead) this.state.startLoc = this.state.curPosition();
  6978. if (this.state.pos >= this.length) {
  6979. this.finishToken(types$1.eof);
  6980. return;
  6981. }
  6982. if (curContext === types.template) {
  6983. this.readTmplToken();
  6984. } else {
  6985. this.getTokenFromCode(this.codePointAtPos(this.state.pos));
  6986. }
  6987. }
  6988. pushComment(block, text, start, end, startLoc, endLoc) {
  6989. const comment = {
  6990. type: block ? "CommentBlock" : "CommentLine",
  6991. value: text,
  6992. start: start,
  6993. end: end,
  6994. loc: new SourceLocation(startLoc, endLoc)
  6995. };
  6996. if (this.options.tokens) this.pushToken(comment);
  6997. this.state.comments.push(comment);
  6998. this.addComment(comment);
  6999. }
  7000. skipBlockComment() {
  7001. let startLoc;
  7002. if (!this.isLookahead) startLoc = this.state.curPosition();
  7003. const start = this.state.pos;
  7004. const end = this.input.indexOf("*/", this.state.pos + 2);
  7005. if (end === -1) throw this.raise(start, ErrorMessages.UnterminatedComment);
  7006. this.state.pos = end + 2;
  7007. lineBreakG.lastIndex = start;
  7008. let match;
  7009. while ((match = lineBreakG.exec(this.input)) && match.index < this.state.pos) {
  7010. ++this.state.curLine;
  7011. this.state.lineStart = match.index + match[0].length;
  7012. }
  7013. if (this.isLookahead) return;
  7014. this.pushComment(true, this.input.slice(start + 2, end), start, this.state.pos, startLoc, this.state.curPosition());
  7015. }
  7016. skipLineComment(startSkip) {
  7017. const start = this.state.pos;
  7018. let startLoc;
  7019. if (!this.isLookahead) startLoc = this.state.curPosition();
  7020. let ch = this.input.charCodeAt(this.state.pos += startSkip);
  7021. if (this.state.pos < this.length) {
  7022. while (!isNewLine(ch) && ++this.state.pos < this.length) {
  7023. ch = this.input.charCodeAt(this.state.pos);
  7024. }
  7025. }
  7026. if (this.isLookahead) return;
  7027. this.pushComment(false, this.input.slice(start + startSkip, this.state.pos), start, this.state.pos, startLoc, this.state.curPosition());
  7028. }
  7029. skipSpace() {
  7030. loop: while (this.state.pos < this.length) {
  7031. const ch = this.input.charCodeAt(this.state.pos);
  7032. switch (ch) {
  7033. case 32:
  7034. case 160:
  7035. case 9:
  7036. ++this.state.pos;
  7037. break;
  7038. case 13:
  7039. if (this.input.charCodeAt(this.state.pos + 1) === 10) {
  7040. ++this.state.pos;
  7041. }
  7042. case 10:
  7043. case 8232:
  7044. case 8233:
  7045. ++this.state.pos;
  7046. ++this.state.curLine;
  7047. this.state.lineStart = this.state.pos;
  7048. break;
  7049. case 47:
  7050. switch (this.input.charCodeAt(this.state.pos + 1)) {
  7051. case 42:
  7052. this.skipBlockComment();
  7053. break;
  7054. case 47:
  7055. this.skipLineComment(2);
  7056. break;
  7057. default:
  7058. break loop;
  7059. }
  7060. break;
  7061. default:
  7062. if (isWhitespace(ch)) {
  7063. ++this.state.pos;
  7064. } else {
  7065. break loop;
  7066. }
  7067. }
  7068. }
  7069. }
  7070. finishToken(type, val) {
  7071. this.state.end = this.state.pos;
  7072. const prevType = this.state.type;
  7073. this.state.type = type;
  7074. this.state.value = val;
  7075. if (!this.isLookahead) {
  7076. this.state.endLoc = this.state.curPosition();
  7077. this.updateContext(prevType);
  7078. }
  7079. }
  7080. readToken_numberSign() {
  7081. if (this.state.pos === 0 && this.readToken_interpreter()) {
  7082. return;
  7083. }
  7084. const nextPos = this.state.pos + 1;
  7085. const next = this.codePointAtPos(nextPos);
  7086. if (next >= 48 && next <= 57) {
  7087. throw this.raise(this.state.pos, ErrorMessages.UnexpectedDigitAfterHash);
  7088. }
  7089. if (next === 123 || next === 91 && this.hasPlugin("recordAndTuple")) {
  7090. this.expectPlugin("recordAndTuple");
  7091. if (this.getPluginOption("recordAndTuple", "syntaxType") !== "hash") {
  7092. throw this.raise(this.state.pos, next === 123 ? ErrorMessages.RecordExpressionHashIncorrectStartSyntaxType : ErrorMessages.TupleExpressionHashIncorrectStartSyntaxType);
  7093. }
  7094. this.state.pos += 2;
  7095. if (next === 123) {
  7096. this.finishToken(types$1.braceHashL);
  7097. } else {
  7098. this.finishToken(types$1.bracketHashL);
  7099. }
  7100. } else if (isIdentifierStart(next)) {
  7101. ++this.state.pos;
  7102. this.finishToken(types$1.privateName, this.readWord1(next));
  7103. } else if (next === 92) {
  7104. ++this.state.pos;
  7105. this.finishToken(types$1.privateName, this.readWord1());
  7106. } else {
  7107. this.finishOp(types$1.hash, 1);
  7108. }
  7109. }
  7110. readToken_dot() {
  7111. const next = this.input.charCodeAt(this.state.pos + 1);
  7112. if (next >= 48 && next <= 57) {
  7113. this.readNumber(true);
  7114. return;
  7115. }
  7116. if (next === 46 && this.input.charCodeAt(this.state.pos + 2) === 46) {
  7117. this.state.pos += 3;
  7118. this.finishToken(types$1.ellipsis);
  7119. } else {
  7120. ++this.state.pos;
  7121. this.finishToken(types$1.dot);
  7122. }
  7123. }
  7124. readToken_slash() {
  7125. const next = this.input.charCodeAt(this.state.pos + 1);
  7126. if (next === 61) {
  7127. this.finishOp(types$1.slashAssign, 2);
  7128. } else {
  7129. this.finishOp(types$1.slash, 1);
  7130. }
  7131. }
  7132. readToken_interpreter() {
  7133. if (this.state.pos !== 0 || this.length < 2) return false;
  7134. let ch = this.input.charCodeAt(this.state.pos + 1);
  7135. if (ch !== 33) return false;
  7136. const start = this.state.pos;
  7137. this.state.pos += 1;
  7138. while (!isNewLine(ch) && ++this.state.pos < this.length) {
  7139. ch = this.input.charCodeAt(this.state.pos);
  7140. }
  7141. const value = this.input.slice(start + 2, this.state.pos);
  7142. this.finishToken(types$1.interpreterDirective, value);
  7143. return true;
  7144. }
  7145. readToken_mult_modulo(code) {
  7146. let type = code === 42 ? types$1.star : types$1.modulo;
  7147. let width = 1;
  7148. let next = this.input.charCodeAt(this.state.pos + 1);
  7149. if (code === 42 && next === 42) {
  7150. width++;
  7151. next = this.input.charCodeAt(this.state.pos + 2);
  7152. type = types$1.exponent;
  7153. }
  7154. if (next === 61 && !this.state.inType) {
  7155. width++;
  7156. type = types$1.assign;
  7157. }
  7158. this.finishOp(type, width);
  7159. }
  7160. readToken_pipe_amp(code) {
  7161. const next = this.input.charCodeAt(this.state.pos + 1);
  7162. if (next === code) {
  7163. if (this.input.charCodeAt(this.state.pos + 2) === 61) {
  7164. this.finishOp(types$1.assign, 3);
  7165. } else {
  7166. this.finishOp(code === 124 ? types$1.logicalOR : types$1.logicalAND, 2);
  7167. }
  7168. return;
  7169. }
  7170. if (code === 124) {
  7171. if (next === 62) {
  7172. this.finishOp(types$1.pipeline, 2);
  7173. return;
  7174. }
  7175. if (this.hasPlugin("recordAndTuple") && next === 125) {
  7176. if (this.getPluginOption("recordAndTuple", "syntaxType") !== "bar") {
  7177. throw this.raise(this.state.pos, ErrorMessages.RecordExpressionBarIncorrectEndSyntaxType);
  7178. }
  7179. this.state.pos += 2;
  7180. this.finishToken(types$1.braceBarR);
  7181. return;
  7182. }
  7183. if (this.hasPlugin("recordAndTuple") && next === 93) {
  7184. if (this.getPluginOption("recordAndTuple", "syntaxType") !== "bar") {
  7185. throw this.raise(this.state.pos, ErrorMessages.TupleExpressionBarIncorrectEndSyntaxType);
  7186. }
  7187. this.state.pos += 2;
  7188. this.finishToken(types$1.bracketBarR);
  7189. return;
  7190. }
  7191. }
  7192. if (next === 61) {
  7193. this.finishOp(types$1.assign, 2);
  7194. return;
  7195. }
  7196. this.finishOp(code === 124 ? types$1.bitwiseOR : types$1.bitwiseAND, 1);
  7197. }
  7198. readToken_caret() {
  7199. const next = this.input.charCodeAt(this.state.pos + 1);
  7200. if (next === 61) {
  7201. this.finishOp(types$1.assign, 2);
  7202. } else {
  7203. this.finishOp(types$1.bitwiseXOR, 1);
  7204. }
  7205. }
  7206. readToken_plus_min(code) {
  7207. const next = this.input.charCodeAt(this.state.pos + 1);
  7208. if (next === code) {
  7209. if (next === 45 && !this.inModule && this.input.charCodeAt(this.state.pos + 2) === 62 && (this.state.lastTokEnd === 0 || this.hasPrecedingLineBreak())) {
  7210. this.skipLineComment(3);
  7211. this.skipSpace();
  7212. this.nextToken();
  7213. return;
  7214. }
  7215. this.finishOp(types$1.incDec, 2);
  7216. return;
  7217. }
  7218. if (next === 61) {
  7219. this.finishOp(types$1.assign, 2);
  7220. } else {
  7221. this.finishOp(types$1.plusMin, 1);
  7222. }
  7223. }
  7224. readToken_lt_gt(code) {
  7225. const next = this.input.charCodeAt(this.state.pos + 1);
  7226. let size = 1;
  7227. if (next === code) {
  7228. size = code === 62 && this.input.charCodeAt(this.state.pos + 2) === 62 ? 3 : 2;
  7229. if (this.input.charCodeAt(this.state.pos + size) === 61) {
  7230. this.finishOp(types$1.assign, size + 1);
  7231. return;
  7232. }
  7233. this.finishOp(types$1.bitShift, size);
  7234. return;
  7235. }
  7236. if (next === 33 && code === 60 && !this.inModule && this.input.charCodeAt(this.state.pos + 2) === 45 && this.input.charCodeAt(this.state.pos + 3) === 45) {
  7237. this.skipLineComment(4);
  7238. this.skipSpace();
  7239. this.nextToken();
  7240. return;
  7241. }
  7242. if (next === 61) {
  7243. size = 2;
  7244. }
  7245. this.finishOp(types$1.relational, size);
  7246. }
  7247. readToken_eq_excl(code) {
  7248. const next = this.input.charCodeAt(this.state.pos + 1);
  7249. if (next === 61) {
  7250. this.finishOp(types$1.equality, this.input.charCodeAt(this.state.pos + 2) === 61 ? 3 : 2);
  7251. return;
  7252. }
  7253. if (code === 61 && next === 62) {
  7254. this.state.pos += 2;
  7255. this.finishToken(types$1.arrow);
  7256. return;
  7257. }
  7258. this.finishOp(code === 61 ? types$1.eq : types$1.bang, 1);
  7259. }
  7260. readToken_question() {
  7261. const next = this.input.charCodeAt(this.state.pos + 1);
  7262. const next2 = this.input.charCodeAt(this.state.pos + 2);
  7263. if (next === 63) {
  7264. if (next2 === 61) {
  7265. this.finishOp(types$1.assign, 3);
  7266. } else {
  7267. this.finishOp(types$1.nullishCoalescing, 2);
  7268. }
  7269. } else if (next === 46 && !(next2 >= 48 && next2 <= 57)) {
  7270. this.state.pos += 2;
  7271. this.finishToken(types$1.questionDot);
  7272. } else {
  7273. ++this.state.pos;
  7274. this.finishToken(types$1.question);
  7275. }
  7276. }
  7277. getTokenFromCode(code) {
  7278. switch (code) {
  7279. case 46:
  7280. this.readToken_dot();
  7281. return;
  7282. case 40:
  7283. ++this.state.pos;
  7284. this.finishToken(types$1.parenL);
  7285. return;
  7286. case 41:
  7287. ++this.state.pos;
  7288. this.finishToken(types$1.parenR);
  7289. return;
  7290. case 59:
  7291. ++this.state.pos;
  7292. this.finishToken(types$1.semi);
  7293. return;
  7294. case 44:
  7295. ++this.state.pos;
  7296. this.finishToken(types$1.comma);
  7297. return;
  7298. case 91:
  7299. if (this.hasPlugin("recordAndTuple") && this.input.charCodeAt(this.state.pos + 1) === 124) {
  7300. if (this.getPluginOption("recordAndTuple", "syntaxType") !== "bar") {
  7301. throw this.raise(this.state.pos, ErrorMessages.TupleExpressionBarIncorrectStartSyntaxType);
  7302. }
  7303. this.state.pos += 2;
  7304. this.finishToken(types$1.bracketBarL);
  7305. } else {
  7306. ++this.state.pos;
  7307. this.finishToken(types$1.bracketL);
  7308. }
  7309. return;
  7310. case 93:
  7311. ++this.state.pos;
  7312. this.finishToken(types$1.bracketR);
  7313. return;
  7314. case 123:
  7315. if (this.hasPlugin("recordAndTuple") && this.input.charCodeAt(this.state.pos + 1) === 124) {
  7316. if (this.getPluginOption("recordAndTuple", "syntaxType") !== "bar") {
  7317. throw this.raise(this.state.pos, ErrorMessages.RecordExpressionBarIncorrectStartSyntaxType);
  7318. }
  7319. this.state.pos += 2;
  7320. this.finishToken(types$1.braceBarL);
  7321. } else {
  7322. ++this.state.pos;
  7323. this.finishToken(types$1.braceL);
  7324. }
  7325. return;
  7326. case 125:
  7327. ++this.state.pos;
  7328. this.finishToken(types$1.braceR);
  7329. return;
  7330. case 58:
  7331. if (this.hasPlugin("functionBind") && this.input.charCodeAt(this.state.pos + 1) === 58) {
  7332. this.finishOp(types$1.doubleColon, 2);
  7333. } else {
  7334. ++this.state.pos;
  7335. this.finishToken(types$1.colon);
  7336. }
  7337. return;
  7338. case 63:
  7339. this.readToken_question();
  7340. return;
  7341. case 96:
  7342. ++this.state.pos;
  7343. this.finishToken(types$1.backQuote);
  7344. return;
  7345. case 48:
  7346. {
  7347. const next = this.input.charCodeAt(this.state.pos + 1);
  7348. if (next === 120 || next === 88) {
  7349. this.readRadixNumber(16);
  7350. return;
  7351. }
  7352. if (next === 111 || next === 79) {
  7353. this.readRadixNumber(8);
  7354. return;
  7355. }
  7356. if (next === 98 || next === 66) {
  7357. this.readRadixNumber(2);
  7358. return;
  7359. }
  7360. }
  7361. case 49:
  7362. case 50:
  7363. case 51:
  7364. case 52:
  7365. case 53:
  7366. case 54:
  7367. case 55:
  7368. case 56:
  7369. case 57:
  7370. this.readNumber(false);
  7371. return;
  7372. case 34:
  7373. case 39:
  7374. this.readString(code);
  7375. return;
  7376. case 47:
  7377. this.readToken_slash();
  7378. return;
  7379. case 37:
  7380. case 42:
  7381. this.readToken_mult_modulo(code);
  7382. return;
  7383. case 124:
  7384. case 38:
  7385. this.readToken_pipe_amp(code);
  7386. return;
  7387. case 94:
  7388. this.readToken_caret();
  7389. return;
  7390. case 43:
  7391. case 45:
  7392. this.readToken_plus_min(code);
  7393. return;
  7394. case 60:
  7395. case 62:
  7396. this.readToken_lt_gt(code);
  7397. return;
  7398. case 61:
  7399. case 33:
  7400. this.readToken_eq_excl(code);
  7401. return;
  7402. case 126:
  7403. this.finishOp(types$1.tilde, 1);
  7404. return;
  7405. case 64:
  7406. ++this.state.pos;
  7407. this.finishToken(types$1.at);
  7408. return;
  7409. case 35:
  7410. this.readToken_numberSign();
  7411. return;
  7412. case 92:
  7413. this.readWord();
  7414. return;
  7415. default:
  7416. if (isIdentifierStart(code)) {
  7417. this.readWord(code);
  7418. return;
  7419. }
  7420. }
  7421. throw this.raise(this.state.pos, ErrorMessages.InvalidOrUnexpectedToken, String.fromCodePoint(code));
  7422. }
  7423. finishOp(type, size) {
  7424. const str = this.input.slice(this.state.pos, this.state.pos + size);
  7425. this.state.pos += size;
  7426. this.finishToken(type, str);
  7427. }
  7428. readRegexp() {
  7429. const start = this.state.start + 1;
  7430. let escaped, inClass;
  7431. let {
  7432. pos
  7433. } = this.state;
  7434. for (;; ++pos) {
  7435. if (pos >= this.length) {
  7436. throw this.raise(start, ErrorMessages.UnterminatedRegExp);
  7437. }
  7438. const ch = this.input.charCodeAt(pos);
  7439. if (isNewLine(ch)) {
  7440. throw this.raise(start, ErrorMessages.UnterminatedRegExp);
  7441. }
  7442. if (escaped) {
  7443. escaped = false;
  7444. } else {
  7445. if (ch === 91) {
  7446. inClass = true;
  7447. } else if (ch === 93 && inClass) {
  7448. inClass = false;
  7449. } else if (ch === 47 && !inClass) {
  7450. break;
  7451. }
  7452. escaped = ch === 92;
  7453. }
  7454. }
  7455. const content = this.input.slice(start, pos);
  7456. ++pos;
  7457. let mods = "";
  7458. while (pos < this.length) {
  7459. const cp = this.codePointAtPos(pos);
  7460. const char = String.fromCharCode(cp);
  7461. if (VALID_REGEX_FLAGS.has(cp)) {
  7462. if (mods.includes(char)) {
  7463. this.raise(pos + 1, ErrorMessages.DuplicateRegExpFlags);
  7464. }
  7465. } else if (isIdentifierChar(cp) || cp === 92) {
  7466. this.raise(pos + 1, ErrorMessages.MalformedRegExpFlags);
  7467. } else {
  7468. break;
  7469. }
  7470. ++pos;
  7471. mods += char;
  7472. }
  7473. this.state.pos = pos;
  7474. this.finishToken(types$1.regexp, {
  7475. pattern: content,
  7476. flags: mods
  7477. });
  7478. }
  7479. readInt(radix, len, forceLen, allowNumSeparator = true) {
  7480. const start = this.state.pos;
  7481. const forbiddenSiblings = radix === 16 ? forbiddenNumericSeparatorSiblings.hex : forbiddenNumericSeparatorSiblings.decBinOct;
  7482. const allowedSiblings = radix === 16 ? allowedNumericSeparatorSiblings.hex : radix === 10 ? allowedNumericSeparatorSiblings.dec : radix === 8 ? allowedNumericSeparatorSiblings.oct : allowedNumericSeparatorSiblings.bin;
  7483. let invalid = false;
  7484. let total = 0;
  7485. for (let i = 0, e = len == null ? Infinity : len; i < e; ++i) {
  7486. const code = this.input.charCodeAt(this.state.pos);
  7487. let val;
  7488. if (code === 95) {
  7489. const prev = this.input.charCodeAt(this.state.pos - 1);
  7490. const next = this.input.charCodeAt(this.state.pos + 1);
  7491. if (allowedSiblings.indexOf(next) === -1) {
  7492. this.raise(this.state.pos, ErrorMessages.UnexpectedNumericSeparator);
  7493. } else if (forbiddenSiblings.indexOf(prev) > -1 || forbiddenSiblings.indexOf(next) > -1 || Number.isNaN(next)) {
  7494. this.raise(this.state.pos, ErrorMessages.UnexpectedNumericSeparator);
  7495. }
  7496. if (!allowNumSeparator) {
  7497. this.raise(this.state.pos, ErrorMessages.NumericSeparatorInEscapeSequence);
  7498. }
  7499. ++this.state.pos;
  7500. continue;
  7501. }
  7502. if (code >= 97) {
  7503. val = code - 97 + 10;
  7504. } else if (code >= 65) {
  7505. val = code - 65 + 10;
  7506. } else if (_isDigit(code)) {
  7507. val = code - 48;
  7508. } else {
  7509. val = Infinity;
  7510. }
  7511. if (val >= radix) {
  7512. if (this.options.errorRecovery && val <= 9) {
  7513. val = 0;
  7514. this.raise(this.state.start + i + 2, ErrorMessages.InvalidDigit, radix);
  7515. } else if (forceLen) {
  7516. val = 0;
  7517. invalid = true;
  7518. } else {
  7519. break;
  7520. }
  7521. }
  7522. ++this.state.pos;
  7523. total = total * radix + val;
  7524. }
  7525. if (this.state.pos === start || len != null && this.state.pos - start !== len || invalid) {
  7526. return null;
  7527. }
  7528. return total;
  7529. }
  7530. readRadixNumber(radix) {
  7531. const start = this.state.pos;
  7532. let isBigInt = false;
  7533. this.state.pos += 2;
  7534. const val = this.readInt(radix);
  7535. if (val == null) {
  7536. this.raise(this.state.start + 2, ErrorMessages.InvalidDigit, radix);
  7537. }
  7538. const next = this.input.charCodeAt(this.state.pos);
  7539. if (next === 110) {
  7540. ++this.state.pos;
  7541. isBigInt = true;
  7542. } else if (next === 109) {
  7543. throw this.raise(start, ErrorMessages.InvalidDecimal);
  7544. }
  7545. if (isIdentifierStart(this.codePointAtPos(this.state.pos))) {
  7546. throw this.raise(this.state.pos, ErrorMessages.NumberIdentifier);
  7547. }
  7548. if (isBigInt) {
  7549. const str = this.input.slice(start, this.state.pos).replace(/[_n]/g, "");
  7550. this.finishToken(types$1.bigint, str);
  7551. return;
  7552. }
  7553. this.finishToken(types$1.num, val);
  7554. }
  7555. readNumber(startsWithDot) {
  7556. const start = this.state.pos;
  7557. let isFloat = false;
  7558. let isBigInt = false;
  7559. let isDecimal = false;
  7560. let hasExponent = false;
  7561. let isOctal = false;
  7562. if (!startsWithDot && this.readInt(10) === null) {
  7563. this.raise(start, ErrorMessages.InvalidNumber);
  7564. }
  7565. const hasLeadingZero = this.state.pos - start >= 2 && this.input.charCodeAt(start) === 48;
  7566. if (hasLeadingZero) {
  7567. const integer = this.input.slice(start, this.state.pos);
  7568. this.recordStrictModeErrors(start, ErrorMessages.StrictOctalLiteral);
  7569. if (!this.state.strict) {
  7570. const underscorePos = integer.indexOf("_");
  7571. if (underscorePos > 0) {
  7572. this.raise(underscorePos + start, ErrorMessages.ZeroDigitNumericSeparator);
  7573. }
  7574. }
  7575. isOctal = hasLeadingZero && !/[89]/.test(integer);
  7576. }
  7577. let next = this.input.charCodeAt(this.state.pos);
  7578. if (next === 46 && !isOctal) {
  7579. ++this.state.pos;
  7580. this.readInt(10);
  7581. isFloat = true;
  7582. next = this.input.charCodeAt(this.state.pos);
  7583. }
  7584. if ((next === 69 || next === 101) && !isOctal) {
  7585. next = this.input.charCodeAt(++this.state.pos);
  7586. if (next === 43 || next === 45) {
  7587. ++this.state.pos;
  7588. }
  7589. if (this.readInt(10) === null) {
  7590. this.raise(start, ErrorMessages.InvalidOrMissingExponent);
  7591. }
  7592. isFloat = true;
  7593. hasExponent = true;
  7594. next = this.input.charCodeAt(this.state.pos);
  7595. }
  7596. if (next === 110) {
  7597. if (isFloat || hasLeadingZero) {
  7598. this.raise(start, ErrorMessages.InvalidBigIntLiteral);
  7599. }
  7600. ++this.state.pos;
  7601. isBigInt = true;
  7602. }
  7603. if (next === 109) {
  7604. this.expectPlugin("decimal", this.state.pos);
  7605. if (hasExponent || hasLeadingZero) {
  7606. this.raise(start, ErrorMessages.InvalidDecimal);
  7607. }
  7608. ++this.state.pos;
  7609. isDecimal = true;
  7610. }
  7611. if (isIdentifierStart(this.codePointAtPos(this.state.pos))) {
  7612. throw this.raise(this.state.pos, ErrorMessages.NumberIdentifier);
  7613. }
  7614. const str = this.input.slice(start, this.state.pos).replace(/[_mn]/g, "");
  7615. if (isBigInt) {
  7616. this.finishToken(types$1.bigint, str);
  7617. return;
  7618. }
  7619. if (isDecimal) {
  7620. this.finishToken(types$1.decimal, str);
  7621. return;
  7622. }
  7623. const val = isOctal ? parseInt(str, 8) : parseFloat(str);
  7624. this.finishToken(types$1.num, val);
  7625. }
  7626. readCodePoint(throwOnInvalid) {
  7627. const ch = this.input.charCodeAt(this.state.pos);
  7628. let code;
  7629. if (ch === 123) {
  7630. const codePos = ++this.state.pos;
  7631. code = this.readHexChar(this.input.indexOf("}", this.state.pos) - this.state.pos, true, throwOnInvalid);
  7632. ++this.state.pos;
  7633. if (code !== null && code > 0x10ffff) {
  7634. if (throwOnInvalid) {
  7635. this.raise(codePos, ErrorMessages.InvalidCodePoint);
  7636. } else {
  7637. return null;
  7638. }
  7639. }
  7640. } else {
  7641. code = this.readHexChar(4, false, throwOnInvalid);
  7642. }
  7643. return code;
  7644. }
  7645. readString(quote) {
  7646. let out = "",
  7647. chunkStart = ++this.state.pos;
  7648. for (;;) {
  7649. if (this.state.pos >= this.length) {
  7650. throw this.raise(this.state.start, ErrorMessages.UnterminatedString);
  7651. }
  7652. const ch = this.input.charCodeAt(this.state.pos);
  7653. if (ch === quote) break;
  7654. if (ch === 92) {
  7655. out += this.input.slice(chunkStart, this.state.pos);
  7656. out += this.readEscapedChar(false);
  7657. chunkStart = this.state.pos;
  7658. } else if (ch === 8232 || ch === 8233) {
  7659. ++this.state.pos;
  7660. ++this.state.curLine;
  7661. this.state.lineStart = this.state.pos;
  7662. } else if (isNewLine(ch)) {
  7663. throw this.raise(this.state.start, ErrorMessages.UnterminatedString);
  7664. } else {
  7665. ++this.state.pos;
  7666. }
  7667. }
  7668. out += this.input.slice(chunkStart, this.state.pos++);
  7669. this.finishToken(types$1.string, out);
  7670. }
  7671. readTmplToken() {
  7672. let out = "",
  7673. chunkStart = this.state.pos,
  7674. containsInvalid = false;
  7675. for (;;) {
  7676. if (this.state.pos >= this.length) {
  7677. throw this.raise(this.state.start, ErrorMessages.UnterminatedTemplate);
  7678. }
  7679. const ch = this.input.charCodeAt(this.state.pos);
  7680. if (ch === 96 || ch === 36 && this.input.charCodeAt(this.state.pos + 1) === 123) {
  7681. if (this.state.pos === this.state.start && this.match(types$1.template)) {
  7682. if (ch === 36) {
  7683. this.state.pos += 2;
  7684. this.finishToken(types$1.dollarBraceL);
  7685. return;
  7686. } else {
  7687. ++this.state.pos;
  7688. this.finishToken(types$1.backQuote);
  7689. return;
  7690. }
  7691. }
  7692. out += this.input.slice(chunkStart, this.state.pos);
  7693. this.finishToken(types$1.template, containsInvalid ? null : out);
  7694. return;
  7695. }
  7696. if (ch === 92) {
  7697. out += this.input.slice(chunkStart, this.state.pos);
  7698. const escaped = this.readEscapedChar(true);
  7699. if (escaped === null) {
  7700. containsInvalid = true;
  7701. } else {
  7702. out += escaped;
  7703. }
  7704. chunkStart = this.state.pos;
  7705. } else if (isNewLine(ch)) {
  7706. out += this.input.slice(chunkStart, this.state.pos);
  7707. ++this.state.pos;
  7708. switch (ch) {
  7709. case 13:
  7710. if (this.input.charCodeAt(this.state.pos) === 10) {
  7711. ++this.state.pos;
  7712. }
  7713. case 10:
  7714. out += "\n";
  7715. break;
  7716. default:
  7717. out += String.fromCharCode(ch);
  7718. break;
  7719. }
  7720. ++this.state.curLine;
  7721. this.state.lineStart = this.state.pos;
  7722. chunkStart = this.state.pos;
  7723. } else {
  7724. ++this.state.pos;
  7725. }
  7726. }
  7727. }
  7728. recordStrictModeErrors(pos, message) {
  7729. if (this.state.strict && !this.state.strictErrors.has(pos)) {
  7730. this.raise(pos, message);
  7731. } else {
  7732. this.state.strictErrors.set(pos, message);
  7733. }
  7734. }
  7735. readEscapedChar(inTemplate) {
  7736. const throwOnInvalid = !inTemplate;
  7737. const ch = this.input.charCodeAt(++this.state.pos);
  7738. ++this.state.pos;
  7739. switch (ch) {
  7740. case 110:
  7741. return "\n";
  7742. case 114:
  7743. return "\r";
  7744. case 120:
  7745. {
  7746. const code = this.readHexChar(2, false, throwOnInvalid);
  7747. return code === null ? null : String.fromCharCode(code);
  7748. }
  7749. case 117:
  7750. {
  7751. const code = this.readCodePoint(throwOnInvalid);
  7752. return code === null ? null : String.fromCodePoint(code);
  7753. }
  7754. case 116:
  7755. return "\t";
  7756. case 98:
  7757. return "\b";
  7758. case 118:
  7759. return "\u000b";
  7760. case 102:
  7761. return "\f";
  7762. case 13:
  7763. if (this.input.charCodeAt(this.state.pos) === 10) {
  7764. ++this.state.pos;
  7765. }
  7766. case 10:
  7767. this.state.lineStart = this.state.pos;
  7768. ++this.state.curLine;
  7769. case 8232:
  7770. case 8233:
  7771. return "";
  7772. case 56:
  7773. case 57:
  7774. if (inTemplate) {
  7775. return null;
  7776. } else {
  7777. this.recordStrictModeErrors(this.state.pos - 1, ErrorMessages.StrictNumericEscape);
  7778. }
  7779. default:
  7780. if (ch >= 48 && ch <= 55) {
  7781. const codePos = this.state.pos - 1;
  7782. const match = this.input.substr(this.state.pos - 1, 3).match(/^[0-7]+/);
  7783. let octalStr = match[0];
  7784. let octal = parseInt(octalStr, 8);
  7785. if (octal > 255) {
  7786. octalStr = octalStr.slice(0, -1);
  7787. octal = parseInt(octalStr, 8);
  7788. }
  7789. this.state.pos += octalStr.length - 1;
  7790. const next = this.input.charCodeAt(this.state.pos);
  7791. if (octalStr !== "0" || next === 56 || next === 57) {
  7792. if (inTemplate) {
  7793. return null;
  7794. } else {
  7795. this.recordStrictModeErrors(codePos, ErrorMessages.StrictNumericEscape);
  7796. }
  7797. }
  7798. return String.fromCharCode(octal);
  7799. }
  7800. return String.fromCharCode(ch);
  7801. }
  7802. }
  7803. readHexChar(len, forceLen, throwOnInvalid) {
  7804. const codePos = this.state.pos;
  7805. const n = this.readInt(16, len, forceLen, false);
  7806. if (n === null) {
  7807. if (throwOnInvalid) {
  7808. this.raise(codePos, ErrorMessages.InvalidEscapeSequence);
  7809. } else {
  7810. this.state.pos = codePos - 1;
  7811. }
  7812. }
  7813. return n;
  7814. }
  7815. readWord1(firstCode) {
  7816. this.state.containsEsc = false;
  7817. let word = "";
  7818. const start = this.state.pos;
  7819. let chunkStart = this.state.pos;
  7820. if (firstCode !== undefined) {
  7821. this.state.pos += firstCode <= 0xffff ? 1 : 2;
  7822. }
  7823. while (this.state.pos < this.length) {
  7824. const ch = this.codePointAtPos(this.state.pos);
  7825. if (isIdentifierChar(ch)) {
  7826. this.state.pos += ch <= 0xffff ? 1 : 2;
  7827. } else if (ch === 92) {
  7828. this.state.containsEsc = true;
  7829. word += this.input.slice(chunkStart, this.state.pos);
  7830. const escStart = this.state.pos;
  7831. const identifierCheck = this.state.pos === start ? isIdentifierStart : isIdentifierChar;
  7832. if (this.input.charCodeAt(++this.state.pos) !== 117) {
  7833. this.raise(this.state.pos, ErrorMessages.MissingUnicodeEscape);
  7834. chunkStart = this.state.pos - 1;
  7835. continue;
  7836. }
  7837. ++this.state.pos;
  7838. const esc = this.readCodePoint(true);
  7839. if (esc !== null) {
  7840. if (!identifierCheck(esc)) {
  7841. this.raise(escStart, ErrorMessages.EscapedCharNotAnIdentifier);
  7842. }
  7843. word += String.fromCodePoint(esc);
  7844. }
  7845. chunkStart = this.state.pos;
  7846. } else {
  7847. break;
  7848. }
  7849. }
  7850. return word + this.input.slice(chunkStart, this.state.pos);
  7851. }
  7852. readWord(firstCode) {
  7853. const word = this.readWord1(firstCode);
  7854. const type = keywords$1.get(word) || types$1.name;
  7855. this.finishToken(type, word);
  7856. }
  7857. checkKeywordEscapes() {
  7858. const kw = this.state.type.keyword;
  7859. if (kw && this.state.containsEsc) {
  7860. this.raise(this.state.start, ErrorMessages.InvalidEscapedReservedWord, kw);
  7861. }
  7862. }
  7863. updateContext(prevType) {
  7864. var _this$state$type$upda, _this$state$type;
  7865. (_this$state$type$upda = (_this$state$type = this.state.type).updateContext) == null ? void 0 : _this$state$type$upda.call(_this$state$type, this.state.context);
  7866. }
  7867. }
  7868. class ClassScope {
  7869. constructor() {
  7870. this.privateNames = new Set();
  7871. this.loneAccessors = new Map();
  7872. this.undefinedPrivateNames = new Map();
  7873. }
  7874. }
  7875. class ClassScopeHandler {
  7876. constructor(raise) {
  7877. this.stack = [];
  7878. this.undefinedPrivateNames = new Map();
  7879. this.raise = raise;
  7880. }
  7881. current() {
  7882. return this.stack[this.stack.length - 1];
  7883. }
  7884. enter() {
  7885. this.stack.push(new ClassScope());
  7886. }
  7887. exit() {
  7888. const oldClassScope = this.stack.pop();
  7889. const current = this.current();
  7890. for (const [name, pos] of Array.from(oldClassScope.undefinedPrivateNames)) {
  7891. if (current) {
  7892. if (!current.undefinedPrivateNames.has(name)) {
  7893. current.undefinedPrivateNames.set(name, pos);
  7894. }
  7895. } else {
  7896. this.raise(pos, ErrorMessages.InvalidPrivateFieldResolution, name);
  7897. }
  7898. }
  7899. }
  7900. declarePrivateName(name, elementType, pos) {
  7901. const classScope = this.current();
  7902. let redefined = classScope.privateNames.has(name);
  7903. if (elementType & CLASS_ELEMENT_KIND_ACCESSOR) {
  7904. const accessor = redefined && classScope.loneAccessors.get(name);
  7905. if (accessor) {
  7906. const oldStatic = accessor & CLASS_ELEMENT_FLAG_STATIC;
  7907. const newStatic = elementType & CLASS_ELEMENT_FLAG_STATIC;
  7908. const oldKind = accessor & CLASS_ELEMENT_KIND_ACCESSOR;
  7909. const newKind = elementType & CLASS_ELEMENT_KIND_ACCESSOR;
  7910. redefined = oldKind === newKind || oldStatic !== newStatic;
  7911. if (!redefined) classScope.loneAccessors.delete(name);
  7912. } else if (!redefined) {
  7913. classScope.loneAccessors.set(name, elementType);
  7914. }
  7915. }
  7916. if (redefined) {
  7917. this.raise(pos, ErrorMessages.PrivateNameRedeclaration, name);
  7918. }
  7919. classScope.privateNames.add(name);
  7920. classScope.undefinedPrivateNames.delete(name);
  7921. }
  7922. usePrivateName(name, pos) {
  7923. let classScope;
  7924. for (classScope of this.stack) {
  7925. if (classScope.privateNames.has(name)) return;
  7926. }
  7927. if (classScope) {
  7928. classScope.undefinedPrivateNames.set(name, pos);
  7929. } else {
  7930. this.raise(pos, ErrorMessages.InvalidPrivateFieldResolution, name);
  7931. }
  7932. }
  7933. }
  7934. const kExpression = 0,
  7935. kMaybeArrowParameterDeclaration = 1,
  7936. kMaybeAsyncArrowParameterDeclaration = 2,
  7937. kParameterDeclaration = 3;
  7938. class ExpressionScope {
  7939. constructor(type = kExpression) {
  7940. this.type = void 0;
  7941. this.type = type;
  7942. }
  7943. canBeArrowParameterDeclaration() {
  7944. return this.type === kMaybeAsyncArrowParameterDeclaration || this.type === kMaybeArrowParameterDeclaration;
  7945. }
  7946. isCertainlyParameterDeclaration() {
  7947. return this.type === kParameterDeclaration;
  7948. }
  7949. }
  7950. class ArrowHeadParsingScope extends ExpressionScope {
  7951. constructor(type) {
  7952. super(type);
  7953. this.errors = new Map();
  7954. }
  7955. recordDeclarationError(pos, template) {
  7956. this.errors.set(pos, template);
  7957. }
  7958. clearDeclarationError(pos) {
  7959. this.errors.delete(pos);
  7960. }
  7961. iterateErrors(iterator) {
  7962. this.errors.forEach(iterator);
  7963. }
  7964. }
  7965. class ExpressionScopeHandler {
  7966. constructor(raise) {
  7967. this.stack = [new ExpressionScope()];
  7968. this.raise = raise;
  7969. }
  7970. enter(scope) {
  7971. this.stack.push(scope);
  7972. }
  7973. exit() {
  7974. this.stack.pop();
  7975. }
  7976. recordParameterInitializerError(pos, template) {
  7977. const {
  7978. stack
  7979. } = this;
  7980. let i = stack.length - 1;
  7981. let scope = stack[i];
  7982. while (!scope.isCertainlyParameterDeclaration()) {
  7983. if (scope.canBeArrowParameterDeclaration()) {
  7984. scope.recordDeclarationError(pos, template);
  7985. } else {
  7986. return;
  7987. }
  7988. scope = stack[--i];
  7989. }
  7990. this.raise(pos, template);
  7991. }
  7992. recordParenthesizedIdentifierError(pos, template) {
  7993. const {
  7994. stack
  7995. } = this;
  7996. const scope = stack[stack.length - 1];
  7997. if (scope.isCertainlyParameterDeclaration()) {
  7998. this.raise(pos, template);
  7999. } else if (scope.canBeArrowParameterDeclaration()) {
  8000. scope.recordDeclarationError(pos, template);
  8001. } else {
  8002. return;
  8003. }
  8004. }
  8005. recordAsyncArrowParametersError(pos, template) {
  8006. const {
  8007. stack
  8008. } = this;
  8009. let i = stack.length - 1;
  8010. let scope = stack[i];
  8011. while (scope.canBeArrowParameterDeclaration()) {
  8012. if (scope.type === kMaybeAsyncArrowParameterDeclaration) {
  8013. scope.recordDeclarationError(pos, template);
  8014. }
  8015. scope = stack[--i];
  8016. }
  8017. }
  8018. validateAsPattern() {
  8019. const {
  8020. stack
  8021. } = this;
  8022. const currentScope = stack[stack.length - 1];
  8023. if (!currentScope.canBeArrowParameterDeclaration()) return;
  8024. currentScope.iterateErrors((template, pos) => {
  8025. this.raise(pos, template);
  8026. let i = stack.length - 2;
  8027. let scope = stack[i];
  8028. while (scope.canBeArrowParameterDeclaration()) {
  8029. scope.clearDeclarationError(pos);
  8030. scope = stack[--i];
  8031. }
  8032. });
  8033. }
  8034. }
  8035. function newParameterDeclarationScope() {
  8036. return new ExpressionScope(kParameterDeclaration);
  8037. }
  8038. function newArrowHeadScope() {
  8039. return new ArrowHeadParsingScope(kMaybeArrowParameterDeclaration);
  8040. }
  8041. function newAsyncArrowScope() {
  8042. return new ArrowHeadParsingScope(kMaybeAsyncArrowParameterDeclaration);
  8043. }
  8044. function newExpressionScope() {
  8045. return new ExpressionScope();
  8046. }
  8047. class UtilParser extends Tokenizer {
  8048. addExtra(node, key, val) {
  8049. if (!node) return;
  8050. const extra = node.extra = node.extra || {};
  8051. extra[key] = val;
  8052. }
  8053. isRelational(op) {
  8054. return this.match(types$1.relational) && this.state.value === op;
  8055. }
  8056. expectRelational(op) {
  8057. if (this.isRelational(op)) {
  8058. this.next();
  8059. } else {
  8060. this.unexpected(null, types$1.relational);
  8061. }
  8062. }
  8063. isContextual(name) {
  8064. return this.match(types$1.name) && this.state.value === name && !this.state.containsEsc;
  8065. }
  8066. isUnparsedContextual(nameStart, name) {
  8067. const nameEnd = nameStart + name.length;
  8068. if (this.input.slice(nameStart, nameEnd) === name) {
  8069. const nextCh = this.input.charCodeAt(nameEnd);
  8070. return !(isIdentifierChar(nextCh) || (nextCh & 0xfc00) === 0xd800);
  8071. }
  8072. return false;
  8073. }
  8074. isLookaheadContextual(name) {
  8075. const next = this.nextTokenStart();
  8076. return this.isUnparsedContextual(next, name);
  8077. }
  8078. eatContextual(name) {
  8079. return this.isContextual(name) && this.eat(types$1.name);
  8080. }
  8081. expectContextual(name, template) {
  8082. if (!this.eatContextual(name)) this.unexpected(null, template);
  8083. }
  8084. canInsertSemicolon() {
  8085. return this.match(types$1.eof) || this.match(types$1.braceR) || this.hasPrecedingLineBreak();
  8086. }
  8087. hasPrecedingLineBreak() {
  8088. return lineBreak.test(this.input.slice(this.state.lastTokEnd, this.state.start));
  8089. }
  8090. hasFollowingLineBreak() {
  8091. return lineBreak.test(this.input.slice(this.state.end, this.nextTokenStart()));
  8092. }
  8093. isLineTerminator() {
  8094. return this.eat(types$1.semi) || this.canInsertSemicolon();
  8095. }
  8096. semicolon(allowAsi = true) {
  8097. if (allowAsi ? this.isLineTerminator() : this.eat(types$1.semi)) return;
  8098. this.raise(this.state.lastTokEnd, ErrorMessages.MissingSemicolon);
  8099. }
  8100. expect(type, pos) {
  8101. this.eat(type) || this.unexpected(pos, type);
  8102. }
  8103. assertNoSpace(message = "Unexpected space.") {
  8104. if (this.state.start > this.state.lastTokEnd) {
  8105. this.raise(this.state.lastTokEnd, {
  8106. code: ErrorCodes.SyntaxError,
  8107. reasonCode: "UnexpectedSpace",
  8108. template: message
  8109. });
  8110. }
  8111. }
  8112. unexpected(pos, messageOrType = {
  8113. code: ErrorCodes.SyntaxError,
  8114. reasonCode: "UnexpectedToken",
  8115. template: "Unexpected token"
  8116. }) {
  8117. if (messageOrType instanceof TokenType) {
  8118. messageOrType = {
  8119. code: ErrorCodes.SyntaxError,
  8120. reasonCode: "UnexpectedToken",
  8121. template: `Unexpected token, expected "${messageOrType.label}"`
  8122. };
  8123. }
  8124. throw this.raise(pos != null ? pos : this.state.start, messageOrType);
  8125. }
  8126. expectPlugin(name, pos) {
  8127. if (!this.hasPlugin(name)) {
  8128. throw this.raiseWithData(pos != null ? pos : this.state.start, {
  8129. missingPlugin: [name]
  8130. }, `This experimental syntax requires enabling the parser plugin: '${name}'`);
  8131. }
  8132. return true;
  8133. }
  8134. expectOnePlugin(names, pos) {
  8135. if (!names.some(n => this.hasPlugin(n))) {
  8136. throw this.raiseWithData(pos != null ? pos : this.state.start, {
  8137. missingPlugin: names
  8138. }, `This experimental syntax requires enabling one of the following parser plugin(s): '${names.join(", ")}'`);
  8139. }
  8140. }
  8141. tryParse(fn, oldState = this.state.clone()) {
  8142. const abortSignal = {
  8143. node: null
  8144. };
  8145. try {
  8146. const node = fn((node = null) => {
  8147. abortSignal.node = node;
  8148. throw abortSignal;
  8149. });
  8150. if (this.state.errors.length > oldState.errors.length) {
  8151. const failState = this.state;
  8152. this.state = oldState;
  8153. this.state.tokensLength = failState.tokensLength;
  8154. return {
  8155. node,
  8156. error: failState.errors[oldState.errors.length],
  8157. thrown: false,
  8158. aborted: false,
  8159. failState
  8160. };
  8161. }
  8162. return {
  8163. node,
  8164. error: null,
  8165. thrown: false,
  8166. aborted: false,
  8167. failState: null
  8168. };
  8169. } catch (error) {
  8170. const failState = this.state;
  8171. this.state = oldState;
  8172. if (error instanceof SyntaxError) {
  8173. return {
  8174. node: null,
  8175. error,
  8176. thrown: true,
  8177. aborted: false,
  8178. failState
  8179. };
  8180. }
  8181. if (error === abortSignal) {
  8182. return {
  8183. node: abortSignal.node,
  8184. error: null,
  8185. thrown: false,
  8186. aborted: true,
  8187. failState
  8188. };
  8189. }
  8190. throw error;
  8191. }
  8192. }
  8193. checkExpressionErrors(refExpressionErrors, andThrow) {
  8194. if (!refExpressionErrors) return false;
  8195. const {
  8196. shorthandAssign,
  8197. doubleProto,
  8198. optionalParameters
  8199. } = refExpressionErrors;
  8200. if (!andThrow) {
  8201. return shorthandAssign >= 0 || doubleProto >= 0 || optionalParameters >= 0;
  8202. }
  8203. if (shorthandAssign >= 0) {
  8204. this.unexpected(shorthandAssign);
  8205. }
  8206. if (doubleProto >= 0) {
  8207. this.raise(doubleProto, ErrorMessages.DuplicateProto);
  8208. }
  8209. if (optionalParameters >= 0) {
  8210. this.unexpected(optionalParameters);
  8211. }
  8212. }
  8213. isLiteralPropertyName() {
  8214. return this.match(types$1.name) || !!this.state.type.keyword || this.match(types$1.string) || this.match(types$1.num) || this.match(types$1.bigint) || this.match(types$1.decimal);
  8215. }
  8216. isPrivateName(node) {
  8217. return node.type === "PrivateName";
  8218. }
  8219. getPrivateNameSV(node) {
  8220. return node.id.name;
  8221. }
  8222. hasPropertyAsPrivateName(node) {
  8223. return (node.type === "MemberExpression" || node.type === "OptionalMemberExpression") && this.isPrivateName(node.property);
  8224. }
  8225. isOptionalChain(node) {
  8226. return node.type === "OptionalMemberExpression" || node.type === "OptionalCallExpression";
  8227. }
  8228. isObjectProperty(node) {
  8229. return node.type === "ObjectProperty";
  8230. }
  8231. isObjectMethod(node) {
  8232. return node.type === "ObjectMethod";
  8233. }
  8234. initializeScopes(inModule = this.options.sourceType === "module") {
  8235. const oldLabels = this.state.labels;
  8236. this.state.labels = [];
  8237. const oldExportedIdentifiers = this.exportedIdentifiers;
  8238. this.exportedIdentifiers = new Set();
  8239. const oldInModule = this.inModule;
  8240. this.inModule = inModule;
  8241. const oldScope = this.scope;
  8242. const ScopeHandler = this.getScopeHandler();
  8243. this.scope = new ScopeHandler(this.raise.bind(this), this.inModule);
  8244. const oldProdParam = this.prodParam;
  8245. this.prodParam = new ProductionParameterHandler();
  8246. const oldClassScope = this.classScope;
  8247. this.classScope = new ClassScopeHandler(this.raise.bind(this));
  8248. const oldExpressionScope = this.expressionScope;
  8249. this.expressionScope = new ExpressionScopeHandler(this.raise.bind(this));
  8250. return () => {
  8251. this.state.labels = oldLabels;
  8252. this.exportedIdentifiers = oldExportedIdentifiers;
  8253. this.inModule = oldInModule;
  8254. this.scope = oldScope;
  8255. this.prodParam = oldProdParam;
  8256. this.classScope = oldClassScope;
  8257. this.expressionScope = oldExpressionScope;
  8258. };
  8259. }
  8260. enterInitialScopes() {
  8261. let paramFlags = PARAM;
  8262. if (this.hasPlugin("topLevelAwait") && this.inModule) {
  8263. paramFlags |= PARAM_AWAIT;
  8264. }
  8265. this.scope.enter(SCOPE_PROGRAM);
  8266. this.prodParam.enter(paramFlags);
  8267. }
  8268. }
  8269. class ExpressionErrors {
  8270. constructor() {
  8271. this.shorthandAssign = -1;
  8272. this.doubleProto = -1;
  8273. this.optionalParameters = -1;
  8274. }
  8275. }
  8276. class Node {
  8277. constructor(parser, pos, loc) {
  8278. this.type = void 0;
  8279. this.start = void 0;
  8280. this.end = void 0;
  8281. this.loc = void 0;
  8282. this.range = void 0;
  8283. this.leadingComments = void 0;
  8284. this.trailingComments = void 0;
  8285. this.innerComments = void 0;
  8286. this.extra = void 0;
  8287. this.type = "";
  8288. this.start = pos;
  8289. this.end = 0;
  8290. this.loc = new SourceLocation(loc);
  8291. if (parser != null && parser.options.ranges) this.range = [pos, 0];
  8292. if (parser != null && parser.filename) this.loc.filename = parser.filename;
  8293. }
  8294. __clone() {
  8295. const newNode = new Node();
  8296. const keys = Object.keys(this);
  8297. for (let i = 0, length = keys.length; i < length; i++) {
  8298. const key = keys[i];
  8299. if (key !== "leadingComments" && key !== "trailingComments" && key !== "innerComments") {
  8300. newNode[key] = this[key];
  8301. }
  8302. }
  8303. return newNode;
  8304. }
  8305. }
  8306. class NodeUtils extends UtilParser {
  8307. startNode() {
  8308. return new Node(this, this.state.start, this.state.startLoc);
  8309. }
  8310. startNodeAt(pos, loc) {
  8311. return new Node(this, pos, loc);
  8312. }
  8313. startNodeAtNode(type) {
  8314. return this.startNodeAt(type.start, type.loc.start);
  8315. }
  8316. finishNode(node, type) {
  8317. return this.finishNodeAt(node, type, this.state.lastTokEnd, this.state.lastTokEndLoc);
  8318. }
  8319. finishNodeAt(node, type, pos, loc) {
  8320. node.type = type;
  8321. node.end = pos;
  8322. node.loc.end = loc;
  8323. if (this.options.ranges) node.range[1] = pos;
  8324. this.processComment(node);
  8325. return node;
  8326. }
  8327. resetStartLocation(node, start, startLoc) {
  8328. node.start = start;
  8329. node.loc.start = startLoc;
  8330. if (this.options.ranges) node.range[0] = start;
  8331. }
  8332. resetEndLocation(node, end = this.state.lastTokEnd, endLoc = this.state.lastTokEndLoc) {
  8333. node.end = end;
  8334. node.loc.end = endLoc;
  8335. if (this.options.ranges) node.range[1] = end;
  8336. }
  8337. resetStartLocationFromNode(node, locationNode) {
  8338. this.resetStartLocation(node, locationNode.start, locationNode.loc.start);
  8339. }
  8340. }
  8341. const unwrapParenthesizedExpression = node => {
  8342. return node.type === "ParenthesizedExpression" ? unwrapParenthesizedExpression(node.expression) : node;
  8343. };
  8344. class LValParser extends NodeUtils {
  8345. toAssignable(node, isLHS = false) {
  8346. var _node$extra, _node$extra3;
  8347. let parenthesized = undefined;
  8348. if (node.type === "ParenthesizedExpression" || (_node$extra = node.extra) != null && _node$extra.parenthesized) {
  8349. parenthesized = unwrapParenthesizedExpression(node);
  8350. if (isLHS) {
  8351. if (parenthesized.type === "Identifier") {
  8352. this.expressionScope.recordParenthesizedIdentifierError(node.start, ErrorMessages.InvalidParenthesizedAssignment);
  8353. } else if (parenthesized.type !== "MemberExpression") {
  8354. this.raise(node.start, ErrorMessages.InvalidParenthesizedAssignment);
  8355. }
  8356. } else {
  8357. this.raise(node.start, ErrorMessages.InvalidParenthesizedAssignment);
  8358. }
  8359. }
  8360. switch (node.type) {
  8361. case "Identifier":
  8362. case "ObjectPattern":
  8363. case "ArrayPattern":
  8364. case "AssignmentPattern":
  8365. break;
  8366. case "ObjectExpression":
  8367. node.type = "ObjectPattern";
  8368. for (let i = 0, length = node.properties.length, last = length - 1; i < length; i++) {
  8369. var _node$extra2;
  8370. const prop = node.properties[i];
  8371. const isLast = i === last;
  8372. this.toAssignableObjectExpressionProp(prop, isLast, isLHS);
  8373. if (isLast && prop.type === "RestElement" && (_node$extra2 = node.extra) != null && _node$extra2.trailingComma) {
  8374. this.raiseRestNotLast(node.extra.trailingComma);
  8375. }
  8376. }
  8377. break;
  8378. case "ObjectProperty":
  8379. this.toAssignable(node.value, isLHS);
  8380. break;
  8381. case "SpreadElement":
  8382. {
  8383. this.checkToRestConversion(node);
  8384. node.type = "RestElement";
  8385. const arg = node.argument;
  8386. this.toAssignable(arg, isLHS);
  8387. break;
  8388. }
  8389. case "ArrayExpression":
  8390. node.type = "ArrayPattern";
  8391. this.toAssignableList(node.elements, (_node$extra3 = node.extra) == null ? void 0 : _node$extra3.trailingComma, isLHS);
  8392. break;
  8393. case "AssignmentExpression":
  8394. if (node.operator !== "=") {
  8395. this.raise(node.left.end, ErrorMessages.MissingEqInAssignment);
  8396. }
  8397. node.type = "AssignmentPattern";
  8398. delete node.operator;
  8399. this.toAssignable(node.left, isLHS);
  8400. break;
  8401. case "ParenthesizedExpression":
  8402. this.toAssignable(parenthesized, isLHS);
  8403. break;
  8404. }
  8405. return node;
  8406. }
  8407. toAssignableObjectExpressionProp(prop, isLast, isLHS) {
  8408. if (prop.type === "ObjectMethod") {
  8409. const error = prop.kind === "get" || prop.kind === "set" ? ErrorMessages.PatternHasAccessor : ErrorMessages.PatternHasMethod;
  8410. this.raise(prop.key.start, error);
  8411. } else if (prop.type === "SpreadElement" && !isLast) {
  8412. this.raiseRestNotLast(prop.start);
  8413. } else {
  8414. this.toAssignable(prop, isLHS);
  8415. }
  8416. }
  8417. toAssignableList(exprList, trailingCommaPos, isLHS) {
  8418. let end = exprList.length;
  8419. if (end) {
  8420. const last = exprList[end - 1];
  8421. if ((last == null ? void 0 : last.type) === "RestElement") {
  8422. --end;
  8423. } else if ((last == null ? void 0 : last.type) === "SpreadElement") {
  8424. last.type = "RestElement";
  8425. let arg = last.argument;
  8426. this.toAssignable(arg, isLHS);
  8427. arg = unwrapParenthesizedExpression(arg);
  8428. if (arg.type !== "Identifier" && arg.type !== "MemberExpression" && arg.type !== "ArrayPattern" && arg.type !== "ObjectPattern") {
  8429. this.unexpected(arg.start);
  8430. }
  8431. if (trailingCommaPos) {
  8432. this.raiseTrailingCommaAfterRest(trailingCommaPos);
  8433. }
  8434. --end;
  8435. }
  8436. }
  8437. for (let i = 0; i < end; i++) {
  8438. const elt = exprList[i];
  8439. if (elt) {
  8440. this.toAssignable(elt, isLHS);
  8441. if (elt.type === "RestElement") {
  8442. this.raiseRestNotLast(elt.start);
  8443. }
  8444. }
  8445. }
  8446. return exprList;
  8447. }
  8448. toReferencedList(exprList, isParenthesizedExpr) {
  8449. return exprList;
  8450. }
  8451. toReferencedListDeep(exprList, isParenthesizedExpr) {
  8452. this.toReferencedList(exprList, isParenthesizedExpr);
  8453. for (const expr of exprList) {
  8454. if ((expr == null ? void 0 : expr.type) === "ArrayExpression") {
  8455. this.toReferencedListDeep(expr.elements);
  8456. }
  8457. }
  8458. }
  8459. parseSpread(refExpressionErrors, refNeedsArrowPos) {
  8460. const node = this.startNode();
  8461. this.next();
  8462. node.argument = this.parseMaybeAssignAllowIn(refExpressionErrors, undefined, refNeedsArrowPos);
  8463. return this.finishNode(node, "SpreadElement");
  8464. }
  8465. parseRestBinding() {
  8466. const node = this.startNode();
  8467. this.next();
  8468. node.argument = this.parseBindingAtom();
  8469. return this.finishNode(node, "RestElement");
  8470. }
  8471. parseBindingAtom() {
  8472. switch (this.state.type) {
  8473. case types$1.bracketL:
  8474. {
  8475. const node = this.startNode();
  8476. this.next();
  8477. node.elements = this.parseBindingList(types$1.bracketR, 93, true);
  8478. return this.finishNode(node, "ArrayPattern");
  8479. }
  8480. case types$1.braceL:
  8481. return this.parseObjectLike(types$1.braceR, true);
  8482. }
  8483. return this.parseIdentifier();
  8484. }
  8485. parseBindingList(close, closeCharCode, allowEmpty, allowModifiers) {
  8486. const elts = [];
  8487. let first = true;
  8488. while (!this.eat(close)) {
  8489. if (first) {
  8490. first = false;
  8491. } else {
  8492. this.expect(types$1.comma);
  8493. }
  8494. if (allowEmpty && this.match(types$1.comma)) {
  8495. elts.push(null);
  8496. } else if (this.eat(close)) {
  8497. break;
  8498. } else if (this.match(types$1.ellipsis)) {
  8499. elts.push(this.parseAssignableListItemTypes(this.parseRestBinding()));
  8500. this.checkCommaAfterRest(closeCharCode);
  8501. this.expect(close);
  8502. break;
  8503. } else {
  8504. const decorators = [];
  8505. if (this.match(types$1.at) && this.hasPlugin("decorators")) {
  8506. this.raise(this.state.start, ErrorMessages.UnsupportedParameterDecorator);
  8507. }
  8508. while (this.match(types$1.at)) {
  8509. decorators.push(this.parseDecorator());
  8510. }
  8511. elts.push(this.parseAssignableListItem(allowModifiers, decorators));
  8512. }
  8513. }
  8514. return elts;
  8515. }
  8516. parseAssignableListItem(allowModifiers, decorators) {
  8517. const left = this.parseMaybeDefault();
  8518. this.parseAssignableListItemTypes(left);
  8519. const elt = this.parseMaybeDefault(left.start, left.loc.start, left);
  8520. if (decorators.length) {
  8521. left.decorators = decorators;
  8522. }
  8523. return elt;
  8524. }
  8525. parseAssignableListItemTypes(param) {
  8526. return param;
  8527. }
  8528. parseMaybeDefault(startPos, startLoc, left) {
  8529. var _startLoc, _startPos, _left;
  8530. startLoc = (_startLoc = startLoc) != null ? _startLoc : this.state.startLoc;
  8531. startPos = (_startPos = startPos) != null ? _startPos : this.state.start;
  8532. left = (_left = left) != null ? _left : this.parseBindingAtom();
  8533. if (!this.eat(types$1.eq)) return left;
  8534. const node = this.startNodeAt(startPos, startLoc);
  8535. node.left = left;
  8536. node.right = this.parseMaybeAssignAllowIn();
  8537. return this.finishNode(node, "AssignmentPattern");
  8538. }
  8539. checkLVal(expr, contextDescription, bindingType = BIND_NONE, checkClashes, disallowLetBinding, strictModeChanged = false) {
  8540. switch (expr.type) {
  8541. case "Identifier":
  8542. {
  8543. const {
  8544. name
  8545. } = expr;
  8546. if (this.state.strict && (strictModeChanged ? isStrictBindReservedWord(name, this.inModule) : isStrictBindOnlyReservedWord(name))) {
  8547. this.raise(expr.start, bindingType === BIND_NONE ? ErrorMessages.StrictEvalArguments : ErrorMessages.StrictEvalArgumentsBinding, name);
  8548. }
  8549. if (checkClashes) {
  8550. if (checkClashes.has(name)) {
  8551. this.raise(expr.start, ErrorMessages.ParamDupe);
  8552. } else {
  8553. checkClashes.add(name);
  8554. }
  8555. }
  8556. if (disallowLetBinding && name === "let") {
  8557. this.raise(expr.start, ErrorMessages.LetInLexicalBinding);
  8558. }
  8559. if (!(bindingType & BIND_NONE)) {
  8560. this.scope.declareName(name, bindingType, expr.start);
  8561. }
  8562. break;
  8563. }
  8564. case "MemberExpression":
  8565. if (bindingType !== BIND_NONE) {
  8566. this.raise(expr.start, ErrorMessages.InvalidPropertyBindingPattern);
  8567. }
  8568. break;
  8569. case "ObjectPattern":
  8570. for (let prop of expr.properties) {
  8571. if (this.isObjectProperty(prop)) prop = prop.value;else if (this.isObjectMethod(prop)) continue;
  8572. this.checkLVal(prop, "object destructuring pattern", bindingType, checkClashes, disallowLetBinding);
  8573. }
  8574. break;
  8575. case "ArrayPattern":
  8576. for (const elem of expr.elements) {
  8577. if (elem) {
  8578. this.checkLVal(elem, "array destructuring pattern", bindingType, checkClashes, disallowLetBinding);
  8579. }
  8580. }
  8581. break;
  8582. case "AssignmentPattern":
  8583. this.checkLVal(expr.left, "assignment pattern", bindingType, checkClashes);
  8584. break;
  8585. case "RestElement":
  8586. this.checkLVal(expr.argument, "rest element", bindingType, checkClashes);
  8587. break;
  8588. case "ParenthesizedExpression":
  8589. this.checkLVal(expr.expression, "parenthesized expression", bindingType, checkClashes);
  8590. break;
  8591. default:
  8592. {
  8593. this.raise(expr.start, bindingType === BIND_NONE ? ErrorMessages.InvalidLhs : ErrorMessages.InvalidLhsBinding, contextDescription);
  8594. }
  8595. }
  8596. }
  8597. checkToRestConversion(node) {
  8598. if (node.argument.type !== "Identifier" && node.argument.type !== "MemberExpression") {
  8599. this.raise(node.argument.start, ErrorMessages.InvalidRestAssignmentPattern);
  8600. }
  8601. }
  8602. checkCommaAfterRest(close) {
  8603. if (this.match(types$1.comma)) {
  8604. if (this.lookaheadCharCode() === close) {
  8605. this.raiseTrailingCommaAfterRest(this.state.start);
  8606. } else {
  8607. this.raiseRestNotLast(this.state.start);
  8608. }
  8609. }
  8610. }
  8611. raiseRestNotLast(pos) {
  8612. throw this.raise(pos, ErrorMessages.ElementAfterRest);
  8613. }
  8614. raiseTrailingCommaAfterRest(pos) {
  8615. this.raise(pos, ErrorMessages.RestTrailingComma);
  8616. }
  8617. }
  8618. class ExpressionParser extends LValParser {
  8619. checkProto(prop, isRecord, protoRef, refExpressionErrors) {
  8620. if (prop.type === "SpreadElement" || this.isObjectMethod(prop) || prop.computed || prop.shorthand) {
  8621. return;
  8622. }
  8623. const key = prop.key;
  8624. const name = key.type === "Identifier" ? key.name : key.value;
  8625. if (name === "__proto__") {
  8626. if (isRecord) {
  8627. this.raise(key.start, ErrorMessages.RecordNoProto);
  8628. return;
  8629. }
  8630. if (protoRef.used) {
  8631. if (refExpressionErrors) {
  8632. if (refExpressionErrors.doubleProto === -1) {
  8633. refExpressionErrors.doubleProto = key.start;
  8634. }
  8635. } else {
  8636. this.raise(key.start, ErrorMessages.DuplicateProto);
  8637. }
  8638. }
  8639. protoRef.used = true;
  8640. }
  8641. }
  8642. shouldExitDescending(expr, potentialArrowAt) {
  8643. return expr.type === "ArrowFunctionExpression" && expr.start === potentialArrowAt;
  8644. }
  8645. getExpression() {
  8646. let paramFlags = PARAM;
  8647. if (this.hasPlugin("topLevelAwait") && this.inModule) {
  8648. paramFlags |= PARAM_AWAIT;
  8649. }
  8650. this.scope.enter(SCOPE_PROGRAM);
  8651. this.prodParam.enter(paramFlags);
  8652. this.nextToken();
  8653. const expr = this.parseExpression();
  8654. if (!this.match(types$1.eof)) {
  8655. this.unexpected();
  8656. }
  8657. expr.comments = this.state.comments;
  8658. expr.errors = this.state.errors;
  8659. if (this.options.tokens) {
  8660. expr.tokens = this.tokens;
  8661. }
  8662. return expr;
  8663. }
  8664. parseExpression(disallowIn, refExpressionErrors) {
  8665. if (disallowIn) {
  8666. return this.disallowInAnd(() => this.parseExpressionBase(refExpressionErrors));
  8667. }
  8668. return this.allowInAnd(() => this.parseExpressionBase(refExpressionErrors));
  8669. }
  8670. parseExpressionBase(refExpressionErrors) {
  8671. const startPos = this.state.start;
  8672. const startLoc = this.state.startLoc;
  8673. const expr = this.parseMaybeAssign(refExpressionErrors);
  8674. if (this.match(types$1.comma)) {
  8675. const node = this.startNodeAt(startPos, startLoc);
  8676. node.expressions = [expr];
  8677. while (this.eat(types$1.comma)) {
  8678. node.expressions.push(this.parseMaybeAssign(refExpressionErrors));
  8679. }
  8680. this.toReferencedList(node.expressions);
  8681. return this.finishNode(node, "SequenceExpression");
  8682. }
  8683. return expr;
  8684. }
  8685. parseMaybeAssignDisallowIn(refExpressionErrors, afterLeftParse) {
  8686. return this.disallowInAnd(() => this.parseMaybeAssign(refExpressionErrors, afterLeftParse));
  8687. }
  8688. parseMaybeAssignAllowIn(refExpressionErrors, afterLeftParse) {
  8689. return this.allowInAnd(() => this.parseMaybeAssign(refExpressionErrors, afterLeftParse));
  8690. }
  8691. setOptionalParametersError(refExpressionErrors, resultError) {
  8692. var _resultError$pos;
  8693. refExpressionErrors.optionalParameters = (_resultError$pos = resultError == null ? void 0 : resultError.pos) != null ? _resultError$pos : this.state.start;
  8694. }
  8695. parseMaybeAssign(refExpressionErrors, afterLeftParse) {
  8696. const startPos = this.state.start;
  8697. const startLoc = this.state.startLoc;
  8698. if (this.isContextual("yield")) {
  8699. if (this.prodParam.hasYield) {
  8700. let left = this.parseYield();
  8701. if (afterLeftParse) {
  8702. left = afterLeftParse.call(this, left, startPos, startLoc);
  8703. }
  8704. return left;
  8705. }
  8706. }
  8707. let ownExpressionErrors;
  8708. if (refExpressionErrors) {
  8709. ownExpressionErrors = false;
  8710. } else {
  8711. refExpressionErrors = new ExpressionErrors();
  8712. ownExpressionErrors = true;
  8713. }
  8714. if (this.match(types$1.parenL) || this.match(types$1.name)) {
  8715. this.state.potentialArrowAt = this.state.start;
  8716. }
  8717. let left = this.parseMaybeConditional(refExpressionErrors);
  8718. if (afterLeftParse) {
  8719. left = afterLeftParse.call(this, left, startPos, startLoc);
  8720. }
  8721. if (this.state.type.isAssign) {
  8722. const node = this.startNodeAt(startPos, startLoc);
  8723. const operator = this.state.value;
  8724. node.operator = operator;
  8725. if (this.match(types$1.eq)) {
  8726. node.left = this.toAssignable(left, true);
  8727. refExpressionErrors.doubleProto = -1;
  8728. } else {
  8729. node.left = left;
  8730. }
  8731. if (refExpressionErrors.shorthandAssign >= node.left.start) {
  8732. refExpressionErrors.shorthandAssign = -1;
  8733. }
  8734. this.checkLVal(left, "assignment expression");
  8735. this.next();
  8736. node.right = this.parseMaybeAssign();
  8737. return this.finishNode(node, "AssignmentExpression");
  8738. } else if (ownExpressionErrors) {
  8739. this.checkExpressionErrors(refExpressionErrors, true);
  8740. }
  8741. return left;
  8742. }
  8743. parseMaybeConditional(refExpressionErrors) {
  8744. const startPos = this.state.start;
  8745. const startLoc = this.state.startLoc;
  8746. const potentialArrowAt = this.state.potentialArrowAt;
  8747. const expr = this.parseExprOps(refExpressionErrors);
  8748. if (this.shouldExitDescending(expr, potentialArrowAt)) {
  8749. return expr;
  8750. }
  8751. return this.parseConditional(expr, startPos, startLoc, refExpressionErrors);
  8752. }
  8753. parseConditional(expr, startPos, startLoc, refExpressionErrors) {
  8754. if (this.eat(types$1.question)) {
  8755. const node = this.startNodeAt(startPos, startLoc);
  8756. node.test = expr;
  8757. node.consequent = this.parseMaybeAssignAllowIn();
  8758. this.expect(types$1.colon);
  8759. node.alternate = this.parseMaybeAssign();
  8760. return this.finishNode(node, "ConditionalExpression");
  8761. }
  8762. return expr;
  8763. }
  8764. parseExprOps(refExpressionErrors) {
  8765. const startPos = this.state.start;
  8766. const startLoc = this.state.startLoc;
  8767. const potentialArrowAt = this.state.potentialArrowAt;
  8768. const expr = this.parseMaybeUnary(refExpressionErrors);
  8769. if (this.shouldExitDescending(expr, potentialArrowAt)) {
  8770. return expr;
  8771. }
  8772. return this.parseExprOp(expr, startPos, startLoc, -1);
  8773. }
  8774. parseExprOp(left, leftStartPos, leftStartLoc, minPrec) {
  8775. let prec = this.state.type.binop;
  8776. if (prec != null && (this.prodParam.hasIn || !this.match(types$1._in))) {
  8777. if (prec > minPrec) {
  8778. const op = this.state.type;
  8779. if (op === types$1.pipeline) {
  8780. this.expectPlugin("pipelineOperator");
  8781. if (this.state.inFSharpPipelineDirectBody) {
  8782. return left;
  8783. }
  8784. this.state.inPipeline = true;
  8785. this.checkPipelineAtInfixOperator(left, leftStartPos);
  8786. }
  8787. const node = this.startNodeAt(leftStartPos, leftStartLoc);
  8788. node.left = left;
  8789. node.operator = this.state.value;
  8790. const logical = op === types$1.logicalOR || op === types$1.logicalAND;
  8791. const coalesce = op === types$1.nullishCoalescing;
  8792. if (coalesce) {
  8793. prec = types$1.logicalAND.binop;
  8794. }
  8795. this.next();
  8796. if (op === types$1.pipeline && this.getPluginOption("pipelineOperator", "proposal") === "minimal") {
  8797. if (this.match(types$1.name) && this.state.value === "await" && this.prodParam.hasAwait) {
  8798. throw this.raise(this.state.start, ErrorMessages.UnexpectedAwaitAfterPipelineBody);
  8799. }
  8800. }
  8801. node.right = this.parseExprOpRightExpr(op, prec);
  8802. this.finishNode(node, logical || coalesce ? "LogicalExpression" : "BinaryExpression");
  8803. const nextOp = this.state.type;
  8804. if (coalesce && (nextOp === types$1.logicalOR || nextOp === types$1.logicalAND) || logical && nextOp === types$1.nullishCoalescing) {
  8805. throw this.raise(this.state.start, ErrorMessages.MixingCoalesceWithLogical);
  8806. }
  8807. return this.parseExprOp(node, leftStartPos, leftStartLoc, minPrec);
  8808. }
  8809. }
  8810. return left;
  8811. }
  8812. parseExprOpRightExpr(op, prec) {
  8813. const startPos = this.state.start;
  8814. const startLoc = this.state.startLoc;
  8815. switch (op) {
  8816. case types$1.pipeline:
  8817. switch (this.getPluginOption("pipelineOperator", "proposal")) {
  8818. case "smart":
  8819. return this.withTopicPermittingContext(() => {
  8820. return this.parseSmartPipelineBody(this.parseExprOpBaseRightExpr(op, prec), startPos, startLoc);
  8821. });
  8822. case "fsharp":
  8823. return this.withSoloAwaitPermittingContext(() => {
  8824. return this.parseFSharpPipelineBody(prec);
  8825. });
  8826. }
  8827. default:
  8828. return this.parseExprOpBaseRightExpr(op, prec);
  8829. }
  8830. }
  8831. parseExprOpBaseRightExpr(op, prec) {
  8832. const startPos = this.state.start;
  8833. const startLoc = this.state.startLoc;
  8834. return this.parseExprOp(this.parseMaybeUnary(), startPos, startLoc, op.rightAssociative ? prec - 1 : prec);
  8835. }
  8836. checkExponentialAfterUnary(node) {
  8837. if (this.match(types$1.exponent)) {
  8838. this.raise(node.argument.start, ErrorMessages.UnexpectedTokenUnaryExponentiation);
  8839. }
  8840. }
  8841. parseMaybeUnary(refExpressionErrors, sawUnary) {
  8842. const startPos = this.state.start;
  8843. const startLoc = this.state.startLoc;
  8844. const isAwait = this.isContextual("await");
  8845. if (isAwait && this.isAwaitAllowed()) {
  8846. this.next();
  8847. const expr = this.parseAwait(startPos, startLoc);
  8848. if (!sawUnary) this.checkExponentialAfterUnary(expr);
  8849. return expr;
  8850. }
  8851. if (this.isContextual("module") && this.lookaheadCharCode() === 123 && !this.hasFollowingLineBreak()) {
  8852. return this.parseModuleExpression();
  8853. }
  8854. const update = this.match(types$1.incDec);
  8855. const node = this.startNode();
  8856. if (this.state.type.prefix) {
  8857. node.operator = this.state.value;
  8858. node.prefix = true;
  8859. if (this.match(types$1._throw)) {
  8860. this.expectPlugin("throwExpressions");
  8861. }
  8862. const isDelete = this.match(types$1._delete);
  8863. this.next();
  8864. node.argument = this.parseMaybeUnary(null, true);
  8865. this.checkExpressionErrors(refExpressionErrors, true);
  8866. if (this.state.strict && isDelete) {
  8867. const arg = node.argument;
  8868. if (arg.type === "Identifier") {
  8869. this.raise(node.start, ErrorMessages.StrictDelete);
  8870. } else if (this.hasPropertyAsPrivateName(arg)) {
  8871. this.raise(node.start, ErrorMessages.DeletePrivateField);
  8872. }
  8873. }
  8874. if (!update) {
  8875. if (!sawUnary) this.checkExponentialAfterUnary(node);
  8876. return this.finishNode(node, "UnaryExpression");
  8877. }
  8878. }
  8879. const expr = this.parseUpdate(node, update, refExpressionErrors);
  8880. if (isAwait) {
  8881. const startsExpr = this.hasPlugin("v8intrinsic") ? this.state.type.startsExpr : this.state.type.startsExpr && !this.match(types$1.modulo);
  8882. if (startsExpr && !this.isAmbiguousAwait()) {
  8883. this.raiseOverwrite(startPos, this.hasPlugin("topLevelAwait") ? ErrorMessages.AwaitNotInAsyncContext : ErrorMessages.AwaitNotInAsyncFunction);
  8884. return this.parseAwait(startPos, startLoc);
  8885. }
  8886. }
  8887. return expr;
  8888. }
  8889. parseUpdate(node, update, refExpressionErrors) {
  8890. if (update) {
  8891. this.checkLVal(node.argument, "prefix operation");
  8892. return this.finishNode(node, "UpdateExpression");
  8893. }
  8894. const startPos = this.state.start;
  8895. const startLoc = this.state.startLoc;
  8896. let expr = this.parseExprSubscripts(refExpressionErrors);
  8897. if (this.checkExpressionErrors(refExpressionErrors, false)) return expr;
  8898. while (this.state.type.postfix && !this.canInsertSemicolon()) {
  8899. const node = this.startNodeAt(startPos, startLoc);
  8900. node.operator = this.state.value;
  8901. node.prefix = false;
  8902. node.argument = expr;
  8903. this.checkLVal(expr, "postfix operation");
  8904. this.next();
  8905. expr = this.finishNode(node, "UpdateExpression");
  8906. }
  8907. return expr;
  8908. }
  8909. parseExprSubscripts(refExpressionErrors) {
  8910. const startPos = this.state.start;
  8911. const startLoc = this.state.startLoc;
  8912. const potentialArrowAt = this.state.potentialArrowAt;
  8913. const expr = this.parseExprAtom(refExpressionErrors);
  8914. if (this.shouldExitDescending(expr, potentialArrowAt)) {
  8915. return expr;
  8916. }
  8917. return this.parseSubscripts(expr, startPos, startLoc);
  8918. }
  8919. parseSubscripts(base, startPos, startLoc, noCalls) {
  8920. const state = {
  8921. optionalChainMember: false,
  8922. maybeAsyncArrow: this.atPossibleAsyncArrow(base),
  8923. stop: false
  8924. };
  8925. do {
  8926. base = this.parseSubscript(base, startPos, startLoc, noCalls, state);
  8927. state.maybeAsyncArrow = false;
  8928. } while (!state.stop);
  8929. return base;
  8930. }
  8931. parseSubscript(base, startPos, startLoc, noCalls, state) {
  8932. if (!noCalls && this.eat(types$1.doubleColon)) {
  8933. return this.parseBind(base, startPos, startLoc, noCalls, state);
  8934. } else if (this.match(types$1.backQuote)) {
  8935. return this.parseTaggedTemplateExpression(base, startPos, startLoc, state);
  8936. }
  8937. let optional = false;
  8938. if (this.match(types$1.questionDot)) {
  8939. if (noCalls && this.lookaheadCharCode() === 40) {
  8940. state.stop = true;
  8941. return base;
  8942. }
  8943. state.optionalChainMember = optional = true;
  8944. this.next();
  8945. }
  8946. if (!noCalls && this.match(types$1.parenL)) {
  8947. return this.parseCoverCallAndAsyncArrowHead(base, startPos, startLoc, state, optional);
  8948. } else if (optional || this.match(types$1.bracketL) || this.eat(types$1.dot)) {
  8949. return this.parseMember(base, startPos, startLoc, state, optional);
  8950. } else {
  8951. state.stop = true;
  8952. return base;
  8953. }
  8954. }
  8955. parseMember(base, startPos, startLoc, state, optional) {
  8956. const node = this.startNodeAt(startPos, startLoc);
  8957. const computed = this.eat(types$1.bracketL);
  8958. node.object = base;
  8959. node.computed = computed;
  8960. const privateName = !computed && this.match(types$1.privateName) && this.state.value;
  8961. const property = computed ? this.parseExpression() : privateName ? this.parsePrivateName() : this.parseIdentifier(true);
  8962. if (privateName !== false) {
  8963. if (node.object.type === "Super") {
  8964. this.raise(startPos, ErrorMessages.SuperPrivateField);
  8965. }
  8966. this.classScope.usePrivateName(privateName, property.start);
  8967. }
  8968. node.property = property;
  8969. if (computed) {
  8970. this.expect(types$1.bracketR);
  8971. }
  8972. if (state.optionalChainMember) {
  8973. node.optional = optional;
  8974. return this.finishNode(node, "OptionalMemberExpression");
  8975. } else {
  8976. return this.finishNode(node, "MemberExpression");
  8977. }
  8978. }
  8979. parseBind(base, startPos, startLoc, noCalls, state) {
  8980. const node = this.startNodeAt(startPos, startLoc);
  8981. node.object = base;
  8982. node.callee = this.parseNoCallExpr();
  8983. state.stop = true;
  8984. return this.parseSubscripts(this.finishNode(node, "BindExpression"), startPos, startLoc, noCalls);
  8985. }
  8986. parseCoverCallAndAsyncArrowHead(base, startPos, startLoc, state, optional) {
  8987. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  8988. let refExpressionErrors = null;
  8989. this.state.maybeInArrowParameters = true;
  8990. this.next();
  8991. let node = this.startNodeAt(startPos, startLoc);
  8992. node.callee = base;
  8993. if (state.maybeAsyncArrow) {
  8994. this.expressionScope.enter(newAsyncArrowScope());
  8995. refExpressionErrors = new ExpressionErrors();
  8996. }
  8997. if (state.optionalChainMember) {
  8998. node.optional = optional;
  8999. }
  9000. if (optional) {
  9001. node.arguments = this.parseCallExpressionArguments(types$1.parenR);
  9002. } else {
  9003. node.arguments = this.parseCallExpressionArguments(types$1.parenR, base.type === "Import", base.type !== "Super", node, refExpressionErrors);
  9004. }
  9005. this.finishCallExpression(node, state.optionalChainMember);
  9006. if (state.maybeAsyncArrow && this.shouldParseAsyncArrow() && !optional) {
  9007. state.stop = true;
  9008. this.expressionScope.validateAsPattern();
  9009. this.expressionScope.exit();
  9010. node = this.parseAsyncArrowFromCallExpression(this.startNodeAt(startPos, startLoc), node);
  9011. } else {
  9012. if (state.maybeAsyncArrow) {
  9013. this.checkExpressionErrors(refExpressionErrors, true);
  9014. this.expressionScope.exit();
  9015. }
  9016. this.toReferencedArguments(node);
  9017. }
  9018. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  9019. return node;
  9020. }
  9021. toReferencedArguments(node, isParenthesizedExpr) {
  9022. this.toReferencedListDeep(node.arguments, isParenthesizedExpr);
  9023. }
  9024. parseTaggedTemplateExpression(base, startPos, startLoc, state) {
  9025. const node = this.startNodeAt(startPos, startLoc);
  9026. node.tag = base;
  9027. node.quasi = this.parseTemplate(true);
  9028. if (state.optionalChainMember) {
  9029. this.raise(startPos, ErrorMessages.OptionalChainingNoTemplate);
  9030. }
  9031. return this.finishNode(node, "TaggedTemplateExpression");
  9032. }
  9033. atPossibleAsyncArrow(base) {
  9034. return base.type === "Identifier" && base.name === "async" && this.state.lastTokEnd === base.end && !this.canInsertSemicolon() && base.end - base.start === 5 && base.start === this.state.potentialArrowAt;
  9035. }
  9036. finishCallExpression(node, optional) {
  9037. if (node.callee.type === "Import") {
  9038. if (node.arguments.length === 2) {
  9039. {
  9040. if (!this.hasPlugin("moduleAttributes")) {
  9041. this.expectPlugin("importAssertions");
  9042. }
  9043. }
  9044. }
  9045. if (node.arguments.length === 0 || node.arguments.length > 2) {
  9046. this.raise(node.start, ErrorMessages.ImportCallArity, this.hasPlugin("importAssertions") || this.hasPlugin("moduleAttributes") ? "one or two arguments" : "one argument");
  9047. } else {
  9048. for (const arg of node.arguments) {
  9049. if (arg.type === "SpreadElement") {
  9050. this.raise(arg.start, ErrorMessages.ImportCallSpreadArgument);
  9051. }
  9052. }
  9053. }
  9054. }
  9055. return this.finishNode(node, optional ? "OptionalCallExpression" : "CallExpression");
  9056. }
  9057. parseCallExpressionArguments(close, dynamicImport, allowPlaceholder, nodeForExtra, refExpressionErrors) {
  9058. const elts = [];
  9059. let first = true;
  9060. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  9061. this.state.inFSharpPipelineDirectBody = false;
  9062. while (!this.eat(close)) {
  9063. if (first) {
  9064. first = false;
  9065. } else {
  9066. this.expect(types$1.comma);
  9067. if (this.match(close)) {
  9068. if (dynamicImport && !this.hasPlugin("importAssertions") && !this.hasPlugin("moduleAttributes")) {
  9069. this.raise(this.state.lastTokStart, ErrorMessages.ImportCallArgumentTrailingComma);
  9070. }
  9071. if (nodeForExtra) {
  9072. this.addExtra(nodeForExtra, "trailingComma", this.state.lastTokStart);
  9073. }
  9074. this.next();
  9075. break;
  9076. }
  9077. }
  9078. elts.push(this.parseExprListItem(false, refExpressionErrors, allowPlaceholder));
  9079. }
  9080. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  9081. return elts;
  9082. }
  9083. shouldParseAsyncArrow() {
  9084. return this.match(types$1.arrow) && !this.canInsertSemicolon();
  9085. }
  9086. parseAsyncArrowFromCallExpression(node, call) {
  9087. var _call$extra;
  9088. this.expect(types$1.arrow);
  9089. this.parseArrowExpression(node, call.arguments, true, (_call$extra = call.extra) == null ? void 0 : _call$extra.trailingComma);
  9090. return node;
  9091. }
  9092. parseNoCallExpr() {
  9093. const startPos = this.state.start;
  9094. const startLoc = this.state.startLoc;
  9095. return this.parseSubscripts(this.parseExprAtom(), startPos, startLoc, true);
  9096. }
  9097. parseExprAtom(refExpressionErrors) {
  9098. let node;
  9099. switch (this.state.type) {
  9100. case types$1._super:
  9101. return this.parseSuper();
  9102. case types$1._import:
  9103. node = this.startNode();
  9104. this.next();
  9105. if (this.match(types$1.dot)) {
  9106. return this.parseImportMetaProperty(node);
  9107. }
  9108. if (!this.match(types$1.parenL)) {
  9109. this.raise(this.state.lastTokStart, ErrorMessages.UnsupportedImport);
  9110. }
  9111. return this.finishNode(node, "Import");
  9112. case types$1._this:
  9113. node = this.startNode();
  9114. this.next();
  9115. return this.finishNode(node, "ThisExpression");
  9116. case types$1.name:
  9117. {
  9118. const canBeArrow = this.state.potentialArrowAt === this.state.start;
  9119. const containsEsc = this.state.containsEsc;
  9120. const id = this.parseIdentifier();
  9121. if (!containsEsc && id.name === "async" && !this.canInsertSemicolon()) {
  9122. if (this.match(types$1._function)) {
  9123. this.next();
  9124. return this.parseFunction(this.startNodeAtNode(id), undefined, true);
  9125. } else if (this.match(types$1.name)) {
  9126. if (this.lookaheadCharCode() === 61) {
  9127. return this.parseAsyncArrowUnaryFunction(id);
  9128. } else {
  9129. return id;
  9130. }
  9131. } else if (this.match(types$1._do)) {
  9132. return this.parseDo(true);
  9133. }
  9134. }
  9135. if (canBeArrow && this.match(types$1.arrow) && !this.canInsertSemicolon()) {
  9136. this.next();
  9137. return this.parseArrowExpression(this.startNodeAtNode(id), [id], false);
  9138. }
  9139. return id;
  9140. }
  9141. case types$1._do:
  9142. {
  9143. return this.parseDo(false);
  9144. }
  9145. case types$1.slash:
  9146. case types$1.slashAssign:
  9147. {
  9148. this.readRegexp();
  9149. return this.parseRegExpLiteral(this.state.value);
  9150. }
  9151. case types$1.num:
  9152. return this.parseNumericLiteral(this.state.value);
  9153. case types$1.bigint:
  9154. return this.parseBigIntLiteral(this.state.value);
  9155. case types$1.decimal:
  9156. return this.parseDecimalLiteral(this.state.value);
  9157. case types$1.string:
  9158. return this.parseStringLiteral(this.state.value);
  9159. case types$1._null:
  9160. return this.parseNullLiteral();
  9161. case types$1._true:
  9162. return this.parseBooleanLiteral(true);
  9163. case types$1._false:
  9164. return this.parseBooleanLiteral(false);
  9165. case types$1.parenL:
  9166. {
  9167. const canBeArrow = this.state.potentialArrowAt === this.state.start;
  9168. return this.parseParenAndDistinguishExpression(canBeArrow);
  9169. }
  9170. case types$1.bracketBarL:
  9171. case types$1.bracketHashL:
  9172. {
  9173. return this.parseArrayLike(this.state.type === types$1.bracketBarL ? types$1.bracketBarR : types$1.bracketR, false, true, refExpressionErrors);
  9174. }
  9175. case types$1.bracketL:
  9176. {
  9177. return this.parseArrayLike(types$1.bracketR, true, false, refExpressionErrors);
  9178. }
  9179. case types$1.braceBarL:
  9180. case types$1.braceHashL:
  9181. {
  9182. return this.parseObjectLike(this.state.type === types$1.braceBarL ? types$1.braceBarR : types$1.braceR, false, true, refExpressionErrors);
  9183. }
  9184. case types$1.braceL:
  9185. {
  9186. return this.parseObjectLike(types$1.braceR, false, false, refExpressionErrors);
  9187. }
  9188. case types$1._function:
  9189. return this.parseFunctionOrFunctionSent();
  9190. case types$1.at:
  9191. this.parseDecorators();
  9192. case types$1._class:
  9193. node = this.startNode();
  9194. this.takeDecorators(node);
  9195. return this.parseClass(node, false);
  9196. case types$1._new:
  9197. return this.parseNewOrNewTarget();
  9198. case types$1.backQuote:
  9199. return this.parseTemplate(false);
  9200. case types$1.doubleColon:
  9201. {
  9202. node = this.startNode();
  9203. this.next();
  9204. node.object = null;
  9205. const callee = node.callee = this.parseNoCallExpr();
  9206. if (callee.type === "MemberExpression") {
  9207. return this.finishNode(node, "BindExpression");
  9208. } else {
  9209. throw this.raise(callee.start, ErrorMessages.UnsupportedBind);
  9210. }
  9211. }
  9212. case types$1.privateName:
  9213. {
  9214. const start = this.state.start;
  9215. const value = this.state.value;
  9216. node = this.parsePrivateName();
  9217. if (this.match(types$1._in)) {
  9218. this.expectPlugin("privateIn");
  9219. this.classScope.usePrivateName(value, node.start);
  9220. } else if (this.hasPlugin("privateIn")) {
  9221. this.raise(this.state.start, ErrorMessages.PrivateInExpectedIn, value);
  9222. } else {
  9223. throw this.unexpected(start);
  9224. }
  9225. return node;
  9226. }
  9227. case types$1.hash:
  9228. {
  9229. if (this.state.inPipeline) {
  9230. node = this.startNode();
  9231. if (this.getPluginOption("pipelineOperator", "proposal") !== "smart") {
  9232. this.raise(node.start, ErrorMessages.PrimaryTopicRequiresSmartPipeline);
  9233. }
  9234. this.next();
  9235. if (!this.primaryTopicReferenceIsAllowedInCurrentTopicContext()) {
  9236. this.raise(node.start, ErrorMessages.PrimaryTopicNotAllowed);
  9237. }
  9238. this.registerTopicReference();
  9239. return this.finishNode(node, "PipelinePrimaryTopicReference");
  9240. }
  9241. }
  9242. case types$1.relational:
  9243. {
  9244. if (this.state.value === "<") {
  9245. const lookaheadCh = this.input.codePointAt(this.nextTokenStart());
  9246. if (isIdentifierStart(lookaheadCh) || lookaheadCh === 62) {
  9247. this.expectOnePlugin(["jsx", "flow", "typescript"]);
  9248. }
  9249. }
  9250. }
  9251. default:
  9252. throw this.unexpected();
  9253. }
  9254. }
  9255. parseAsyncArrowUnaryFunction(id) {
  9256. const node = this.startNodeAtNode(id);
  9257. this.prodParam.enter(functionFlags(true, this.prodParam.hasYield));
  9258. const params = [this.parseIdentifier()];
  9259. this.prodParam.exit();
  9260. if (this.hasPrecedingLineBreak()) {
  9261. this.raise(this.state.pos, ErrorMessages.LineTerminatorBeforeArrow);
  9262. }
  9263. this.expect(types$1.arrow);
  9264. this.parseArrowExpression(node, params, true);
  9265. return node;
  9266. }
  9267. parseDo(isAsync) {
  9268. this.expectPlugin("doExpressions");
  9269. if (isAsync) {
  9270. this.expectPlugin("asyncDoExpressions");
  9271. }
  9272. const node = this.startNode();
  9273. node.async = isAsync;
  9274. this.next();
  9275. const oldLabels = this.state.labels;
  9276. this.state.labels = [];
  9277. if (isAsync) {
  9278. this.prodParam.enter(PARAM_AWAIT);
  9279. node.body = this.parseBlock();
  9280. this.prodParam.exit();
  9281. } else {
  9282. node.body = this.parseBlock();
  9283. }
  9284. this.state.labels = oldLabels;
  9285. return this.finishNode(node, "DoExpression");
  9286. }
  9287. parseSuper() {
  9288. const node = this.startNode();
  9289. this.next();
  9290. if (this.match(types$1.parenL) && !this.scope.allowDirectSuper && !this.options.allowSuperOutsideMethod) {
  9291. this.raise(node.start, ErrorMessages.SuperNotAllowed);
  9292. } else if (!this.scope.allowSuper && !this.options.allowSuperOutsideMethod) {
  9293. this.raise(node.start, ErrorMessages.UnexpectedSuper);
  9294. }
  9295. if (!this.match(types$1.parenL) && !this.match(types$1.bracketL) && !this.match(types$1.dot)) {
  9296. this.raise(node.start, ErrorMessages.UnsupportedSuper);
  9297. }
  9298. return this.finishNode(node, "Super");
  9299. }
  9300. parseMaybePrivateName(isPrivateNameAllowed) {
  9301. const isPrivate = this.match(types$1.privateName);
  9302. if (isPrivate) {
  9303. if (!isPrivateNameAllowed) {
  9304. this.raise(this.state.start + 1, ErrorMessages.UnexpectedPrivateField);
  9305. }
  9306. return this.parsePrivateName();
  9307. } else {
  9308. return this.parseIdentifier(true);
  9309. }
  9310. }
  9311. parsePrivateName() {
  9312. const node = this.startNode();
  9313. const id = this.startNodeAt(this.state.start + 1, new Position(this.state.curLine, this.state.start + 1 - this.state.lineStart));
  9314. const name = this.state.value;
  9315. this.next();
  9316. node.id = this.createIdentifier(id, name);
  9317. return this.finishNode(node, "PrivateName");
  9318. }
  9319. parseFunctionOrFunctionSent() {
  9320. const node = this.startNode();
  9321. this.next();
  9322. if (this.prodParam.hasYield && this.match(types$1.dot)) {
  9323. const meta = this.createIdentifier(this.startNodeAtNode(node), "function");
  9324. this.next();
  9325. return this.parseMetaProperty(node, meta, "sent");
  9326. }
  9327. return this.parseFunction(node);
  9328. }
  9329. parseMetaProperty(node, meta, propertyName) {
  9330. node.meta = meta;
  9331. if (meta.name === "function" && propertyName === "sent") {
  9332. if (this.isContextual(propertyName)) {
  9333. this.expectPlugin("functionSent");
  9334. } else if (!this.hasPlugin("functionSent")) {
  9335. this.unexpected();
  9336. }
  9337. }
  9338. const containsEsc = this.state.containsEsc;
  9339. node.property = this.parseIdentifier(true);
  9340. if (node.property.name !== propertyName || containsEsc) {
  9341. this.raise(node.property.start, ErrorMessages.UnsupportedMetaProperty, meta.name, propertyName);
  9342. }
  9343. return this.finishNode(node, "MetaProperty");
  9344. }
  9345. parseImportMetaProperty(node) {
  9346. const id = this.createIdentifier(this.startNodeAtNode(node), "import");
  9347. this.next();
  9348. if (this.isContextual("meta")) {
  9349. if (!this.inModule) {
  9350. this.raise(id.start, SourceTypeModuleErrorMessages.ImportMetaOutsideModule);
  9351. }
  9352. this.sawUnambiguousESM = true;
  9353. }
  9354. return this.parseMetaProperty(node, id, "meta");
  9355. }
  9356. parseLiteralAtNode(value, type, node) {
  9357. this.addExtra(node, "rawValue", value);
  9358. this.addExtra(node, "raw", this.input.slice(node.start, this.state.end));
  9359. node.value = value;
  9360. this.next();
  9361. return this.finishNode(node, type);
  9362. }
  9363. parseLiteral(value, type) {
  9364. const node = this.startNode();
  9365. return this.parseLiteralAtNode(value, type, node);
  9366. }
  9367. parseStringLiteral(value) {
  9368. return this.parseLiteral(value, "StringLiteral");
  9369. }
  9370. parseNumericLiteral(value) {
  9371. return this.parseLiteral(value, "NumericLiteral");
  9372. }
  9373. parseBigIntLiteral(value) {
  9374. return this.parseLiteral(value, "BigIntLiteral");
  9375. }
  9376. parseDecimalLiteral(value) {
  9377. return this.parseLiteral(value, "DecimalLiteral");
  9378. }
  9379. parseRegExpLiteral(value) {
  9380. const node = this.parseLiteral(value.value, "RegExpLiteral");
  9381. node.pattern = value.pattern;
  9382. node.flags = value.flags;
  9383. return node;
  9384. }
  9385. parseBooleanLiteral(value) {
  9386. const node = this.startNode();
  9387. node.value = value;
  9388. this.next();
  9389. return this.finishNode(node, "BooleanLiteral");
  9390. }
  9391. parseNullLiteral() {
  9392. const node = this.startNode();
  9393. this.next();
  9394. return this.finishNode(node, "NullLiteral");
  9395. }
  9396. parseParenAndDistinguishExpression(canBeArrow) {
  9397. const startPos = this.state.start;
  9398. const startLoc = this.state.startLoc;
  9399. let val;
  9400. this.next();
  9401. this.expressionScope.enter(newArrowHeadScope());
  9402. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  9403. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  9404. this.state.maybeInArrowParameters = true;
  9405. this.state.inFSharpPipelineDirectBody = false;
  9406. const innerStartPos = this.state.start;
  9407. const innerStartLoc = this.state.startLoc;
  9408. const exprList = [];
  9409. const refExpressionErrors = new ExpressionErrors();
  9410. let first = true;
  9411. let spreadStart;
  9412. let optionalCommaStart;
  9413. while (!this.match(types$1.parenR)) {
  9414. if (first) {
  9415. first = false;
  9416. } else {
  9417. this.expect(types$1.comma, refExpressionErrors.optionalParameters === -1 ? null : refExpressionErrors.optionalParameters);
  9418. if (this.match(types$1.parenR)) {
  9419. optionalCommaStart = this.state.start;
  9420. break;
  9421. }
  9422. }
  9423. if (this.match(types$1.ellipsis)) {
  9424. const spreadNodeStartPos = this.state.start;
  9425. const spreadNodeStartLoc = this.state.startLoc;
  9426. spreadStart = this.state.start;
  9427. exprList.push(this.parseParenItem(this.parseRestBinding(), spreadNodeStartPos, spreadNodeStartLoc));
  9428. this.checkCommaAfterRest(41);
  9429. break;
  9430. } else {
  9431. exprList.push(this.parseMaybeAssignAllowIn(refExpressionErrors, this.parseParenItem));
  9432. }
  9433. }
  9434. const innerEndPos = this.state.lastTokEnd;
  9435. const innerEndLoc = this.state.lastTokEndLoc;
  9436. this.expect(types$1.parenR);
  9437. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  9438. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  9439. let arrowNode = this.startNodeAt(startPos, startLoc);
  9440. if (canBeArrow && this.shouldParseArrow() && (arrowNode = this.parseArrow(arrowNode))) {
  9441. this.expressionScope.validateAsPattern();
  9442. this.expressionScope.exit();
  9443. this.parseArrowExpression(arrowNode, exprList, false);
  9444. return arrowNode;
  9445. }
  9446. this.expressionScope.exit();
  9447. if (!exprList.length) {
  9448. this.unexpected(this.state.lastTokStart);
  9449. }
  9450. if (optionalCommaStart) this.unexpected(optionalCommaStart);
  9451. if (spreadStart) this.unexpected(spreadStart);
  9452. this.checkExpressionErrors(refExpressionErrors, true);
  9453. this.toReferencedListDeep(exprList, true);
  9454. if (exprList.length > 1) {
  9455. val = this.startNodeAt(innerStartPos, innerStartLoc);
  9456. val.expressions = exprList;
  9457. this.finishNodeAt(val, "SequenceExpression", innerEndPos, innerEndLoc);
  9458. } else {
  9459. val = exprList[0];
  9460. }
  9461. if (!this.options.createParenthesizedExpressions) {
  9462. this.addExtra(val, "parenthesized", true);
  9463. this.addExtra(val, "parenStart", startPos);
  9464. return val;
  9465. }
  9466. const parenExpression = this.startNodeAt(startPos, startLoc);
  9467. parenExpression.expression = val;
  9468. this.finishNode(parenExpression, "ParenthesizedExpression");
  9469. return parenExpression;
  9470. }
  9471. shouldParseArrow() {
  9472. return !this.canInsertSemicolon();
  9473. }
  9474. parseArrow(node) {
  9475. if (this.eat(types$1.arrow)) {
  9476. return node;
  9477. }
  9478. }
  9479. parseParenItem(node, startPos, startLoc) {
  9480. return node;
  9481. }
  9482. parseNewOrNewTarget() {
  9483. const node = this.startNode();
  9484. this.next();
  9485. if (this.match(types$1.dot)) {
  9486. const meta = this.createIdentifier(this.startNodeAtNode(node), "new");
  9487. this.next();
  9488. const metaProp = this.parseMetaProperty(node, meta, "target");
  9489. if (!this.scope.inNonArrowFunction && !this.scope.inClass) {
  9490. this.raise(metaProp.start, ErrorMessages.UnexpectedNewTarget);
  9491. }
  9492. return metaProp;
  9493. }
  9494. return this.parseNew(node);
  9495. }
  9496. parseNew(node) {
  9497. node.callee = this.parseNoCallExpr();
  9498. if (node.callee.type === "Import") {
  9499. this.raise(node.callee.start, ErrorMessages.ImportCallNotNewExpression);
  9500. } else if (this.isOptionalChain(node.callee)) {
  9501. this.raise(this.state.lastTokEnd, ErrorMessages.OptionalChainingNoNew);
  9502. } else if (this.eat(types$1.questionDot)) {
  9503. this.raise(this.state.start, ErrorMessages.OptionalChainingNoNew);
  9504. }
  9505. this.parseNewArguments(node);
  9506. return this.finishNode(node, "NewExpression");
  9507. }
  9508. parseNewArguments(node) {
  9509. if (this.eat(types$1.parenL)) {
  9510. const args = this.parseExprList(types$1.parenR);
  9511. this.toReferencedList(args);
  9512. node.arguments = args;
  9513. } else {
  9514. node.arguments = [];
  9515. }
  9516. }
  9517. parseTemplateElement(isTagged) {
  9518. const elem = this.startNode();
  9519. if (this.state.value === null) {
  9520. if (!isTagged) {
  9521. this.raise(this.state.start + 1, ErrorMessages.InvalidEscapeSequenceTemplate);
  9522. }
  9523. }
  9524. elem.value = {
  9525. raw: this.input.slice(this.state.start, this.state.end).replace(/\r\n?/g, "\n"),
  9526. cooked: this.state.value
  9527. };
  9528. this.next();
  9529. elem.tail = this.match(types$1.backQuote);
  9530. return this.finishNode(elem, "TemplateElement");
  9531. }
  9532. parseTemplate(isTagged) {
  9533. const node = this.startNode();
  9534. this.next();
  9535. node.expressions = [];
  9536. let curElt = this.parseTemplateElement(isTagged);
  9537. node.quasis = [curElt];
  9538. while (!curElt.tail) {
  9539. this.expect(types$1.dollarBraceL);
  9540. node.expressions.push(this.parseTemplateSubstitution());
  9541. this.expect(types$1.braceR);
  9542. node.quasis.push(curElt = this.parseTemplateElement(isTagged));
  9543. }
  9544. this.next();
  9545. return this.finishNode(node, "TemplateLiteral");
  9546. }
  9547. parseTemplateSubstitution() {
  9548. return this.parseExpression();
  9549. }
  9550. parseObjectLike(close, isPattern, isRecord, refExpressionErrors) {
  9551. if (isRecord) {
  9552. this.expectPlugin("recordAndTuple");
  9553. }
  9554. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  9555. this.state.inFSharpPipelineDirectBody = false;
  9556. const propHash = Object.create(null);
  9557. let first = true;
  9558. const node = this.startNode();
  9559. node.properties = [];
  9560. this.next();
  9561. while (!this.match(close)) {
  9562. if (first) {
  9563. first = false;
  9564. } else {
  9565. this.expect(types$1.comma);
  9566. if (this.match(close)) {
  9567. this.addExtra(node, "trailingComma", this.state.lastTokStart);
  9568. break;
  9569. }
  9570. }
  9571. const prop = this.parsePropertyDefinition(isPattern, refExpressionErrors);
  9572. if (!isPattern) {
  9573. this.checkProto(prop, isRecord, propHash, refExpressionErrors);
  9574. }
  9575. if (isRecord && !this.isObjectProperty(prop) && prop.type !== "SpreadElement") {
  9576. this.raise(prop.start, ErrorMessages.InvalidRecordProperty);
  9577. }
  9578. if (prop.shorthand) {
  9579. this.addExtra(prop, "shorthand", true);
  9580. }
  9581. node.properties.push(prop);
  9582. }
  9583. this.next();
  9584. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  9585. let type = "ObjectExpression";
  9586. if (isPattern) {
  9587. type = "ObjectPattern";
  9588. } else if (isRecord) {
  9589. type = "RecordExpression";
  9590. }
  9591. return this.finishNode(node, type);
  9592. }
  9593. maybeAsyncOrAccessorProp(prop) {
  9594. return !prop.computed && prop.key.type === "Identifier" && (this.isLiteralPropertyName() || this.match(types$1.bracketL) || this.match(types$1.star));
  9595. }
  9596. parsePropertyDefinition(isPattern, refExpressionErrors) {
  9597. let decorators = [];
  9598. if (this.match(types$1.at)) {
  9599. if (this.hasPlugin("decorators")) {
  9600. this.raise(this.state.start, ErrorMessages.UnsupportedPropertyDecorator);
  9601. }
  9602. while (this.match(types$1.at)) {
  9603. decorators.push(this.parseDecorator());
  9604. }
  9605. }
  9606. const prop = this.startNode();
  9607. let isGenerator = false;
  9608. let isAsync = false;
  9609. let isAccessor = false;
  9610. let startPos;
  9611. let startLoc;
  9612. if (this.match(types$1.ellipsis)) {
  9613. if (decorators.length) this.unexpected();
  9614. if (isPattern) {
  9615. this.next();
  9616. prop.argument = this.parseIdentifier();
  9617. this.checkCommaAfterRest(125);
  9618. return this.finishNode(prop, "RestElement");
  9619. }
  9620. return this.parseSpread();
  9621. }
  9622. if (decorators.length) {
  9623. prop.decorators = decorators;
  9624. decorators = [];
  9625. }
  9626. prop.method = false;
  9627. if (isPattern || refExpressionErrors) {
  9628. startPos = this.state.start;
  9629. startLoc = this.state.startLoc;
  9630. }
  9631. if (!isPattern) {
  9632. isGenerator = this.eat(types$1.star);
  9633. }
  9634. const containsEsc = this.state.containsEsc;
  9635. const key = this.parsePropertyName(prop, false);
  9636. if (!isPattern && !isGenerator && !containsEsc && this.maybeAsyncOrAccessorProp(prop)) {
  9637. const keyName = key.name;
  9638. if (keyName === "async" && !this.hasPrecedingLineBreak()) {
  9639. isAsync = true;
  9640. isGenerator = this.eat(types$1.star);
  9641. this.parsePropertyName(prop, false);
  9642. }
  9643. if (keyName === "get" || keyName === "set") {
  9644. isAccessor = true;
  9645. prop.kind = keyName;
  9646. if (this.match(types$1.star)) {
  9647. isGenerator = true;
  9648. this.raise(this.state.pos, ErrorMessages.AccessorIsGenerator, keyName);
  9649. this.next();
  9650. }
  9651. this.parsePropertyName(prop, false);
  9652. }
  9653. }
  9654. this.parseObjPropValue(prop, startPos, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors);
  9655. return prop;
  9656. }
  9657. getGetterSetterExpectedParamCount(method) {
  9658. return method.kind === "get" ? 0 : 1;
  9659. }
  9660. getObjectOrClassMethodParams(method) {
  9661. return method.params;
  9662. }
  9663. checkGetterSetterParams(method) {
  9664. var _params;
  9665. const paramCount = this.getGetterSetterExpectedParamCount(method);
  9666. const params = this.getObjectOrClassMethodParams(method);
  9667. const start = method.start;
  9668. if (params.length !== paramCount) {
  9669. if (method.kind === "get") {
  9670. this.raise(start, ErrorMessages.BadGetterArity);
  9671. } else {
  9672. this.raise(start, ErrorMessages.BadSetterArity);
  9673. }
  9674. }
  9675. if (method.kind === "set" && ((_params = params[params.length - 1]) == null ? void 0 : _params.type) === "RestElement") {
  9676. this.raise(start, ErrorMessages.BadSetterRestParameter);
  9677. }
  9678. }
  9679. parseObjectMethod(prop, isGenerator, isAsync, isPattern, isAccessor) {
  9680. if (isAccessor) {
  9681. this.parseMethod(prop, isGenerator, false, false, false, "ObjectMethod");
  9682. this.checkGetterSetterParams(prop);
  9683. return prop;
  9684. }
  9685. if (isAsync || isGenerator || this.match(types$1.parenL)) {
  9686. if (isPattern) this.unexpected();
  9687. prop.kind = "method";
  9688. prop.method = true;
  9689. return this.parseMethod(prop, isGenerator, isAsync, false, false, "ObjectMethod");
  9690. }
  9691. }
  9692. parseObjectProperty(prop, startPos, startLoc, isPattern, refExpressionErrors) {
  9693. prop.shorthand = false;
  9694. if (this.eat(types$1.colon)) {
  9695. prop.value = isPattern ? this.parseMaybeDefault(this.state.start, this.state.startLoc) : this.parseMaybeAssignAllowIn(refExpressionErrors);
  9696. return this.finishNode(prop, "ObjectProperty");
  9697. }
  9698. if (!prop.computed && prop.key.type === "Identifier") {
  9699. this.checkReservedWord(prop.key.name, prop.key.start, true, false);
  9700. if (isPattern) {
  9701. prop.value = this.parseMaybeDefault(startPos, startLoc, prop.key.__clone());
  9702. } else if (this.match(types$1.eq) && refExpressionErrors) {
  9703. if (refExpressionErrors.shorthandAssign === -1) {
  9704. refExpressionErrors.shorthandAssign = this.state.start;
  9705. }
  9706. prop.value = this.parseMaybeDefault(startPos, startLoc, prop.key.__clone());
  9707. } else {
  9708. prop.value = prop.key.__clone();
  9709. }
  9710. prop.shorthand = true;
  9711. return this.finishNode(prop, "ObjectProperty");
  9712. }
  9713. }
  9714. parseObjPropValue(prop, startPos, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors) {
  9715. const node = this.parseObjectMethod(prop, isGenerator, isAsync, isPattern, isAccessor) || this.parseObjectProperty(prop, startPos, startLoc, isPattern, refExpressionErrors);
  9716. if (!node) this.unexpected();
  9717. return node;
  9718. }
  9719. parsePropertyName(prop, isPrivateNameAllowed) {
  9720. if (this.eat(types$1.bracketL)) {
  9721. prop.computed = true;
  9722. prop.key = this.parseMaybeAssignAllowIn();
  9723. this.expect(types$1.bracketR);
  9724. } else {
  9725. const oldInPropertyName = this.state.inPropertyName;
  9726. this.state.inPropertyName = true;
  9727. const type = this.state.type;
  9728. prop.key = type === types$1.num || type === types$1.string || type === types$1.bigint || type === types$1.decimal ? this.parseExprAtom() : this.parseMaybePrivateName(isPrivateNameAllowed);
  9729. if (type !== types$1.privateName) {
  9730. prop.computed = false;
  9731. }
  9732. this.state.inPropertyName = oldInPropertyName;
  9733. }
  9734. return prop.key;
  9735. }
  9736. initFunction(node, isAsync) {
  9737. node.id = null;
  9738. node.generator = false;
  9739. node.async = !!isAsync;
  9740. }
  9741. parseMethod(node, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope = false) {
  9742. this.initFunction(node, isAsync);
  9743. node.generator = !!isGenerator;
  9744. const allowModifiers = isConstructor;
  9745. this.scope.enter(SCOPE_FUNCTION | SCOPE_SUPER | (inClassScope ? SCOPE_CLASS : 0) | (allowDirectSuper ? SCOPE_DIRECT_SUPER : 0));
  9746. this.prodParam.enter(functionFlags(isAsync, node.generator));
  9747. this.parseFunctionParams(node, allowModifiers);
  9748. this.parseFunctionBodyAndFinish(node, type, true);
  9749. this.prodParam.exit();
  9750. this.scope.exit();
  9751. return node;
  9752. }
  9753. parseArrayLike(close, canBePattern, isTuple, refExpressionErrors) {
  9754. if (isTuple) {
  9755. this.expectPlugin("recordAndTuple");
  9756. }
  9757. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  9758. this.state.inFSharpPipelineDirectBody = false;
  9759. const node = this.startNode();
  9760. this.next();
  9761. node.elements = this.parseExprList(close, !isTuple, refExpressionErrors, node);
  9762. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  9763. return this.finishNode(node, isTuple ? "TupleExpression" : "ArrayExpression");
  9764. }
  9765. parseArrowExpression(node, params, isAsync, trailingCommaPos) {
  9766. this.scope.enter(SCOPE_FUNCTION | SCOPE_ARROW);
  9767. let flags = functionFlags(isAsync, false);
  9768. if (!this.match(types$1.bracketL) && this.prodParam.hasIn) {
  9769. flags |= PARAM_IN;
  9770. }
  9771. this.prodParam.enter(flags);
  9772. this.initFunction(node, isAsync);
  9773. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  9774. if (params) {
  9775. this.state.maybeInArrowParameters = true;
  9776. this.setArrowFunctionParameters(node, params, trailingCommaPos);
  9777. }
  9778. this.state.maybeInArrowParameters = false;
  9779. this.parseFunctionBody(node, true);
  9780. this.prodParam.exit();
  9781. this.scope.exit();
  9782. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  9783. return this.finishNode(node, "ArrowFunctionExpression");
  9784. }
  9785. setArrowFunctionParameters(node, params, trailingCommaPos) {
  9786. node.params = this.toAssignableList(params, trailingCommaPos, false);
  9787. }
  9788. parseFunctionBodyAndFinish(node, type, isMethod = false) {
  9789. this.parseFunctionBody(node, false, isMethod);
  9790. this.finishNode(node, type);
  9791. }
  9792. parseFunctionBody(node, allowExpression, isMethod = false) {
  9793. const isExpression = allowExpression && !this.match(types$1.braceL);
  9794. this.expressionScope.enter(newExpressionScope());
  9795. if (isExpression) {
  9796. node.body = this.parseMaybeAssign();
  9797. this.checkParams(node, false, allowExpression, false);
  9798. } else {
  9799. const oldStrict = this.state.strict;
  9800. const oldLabels = this.state.labels;
  9801. this.state.labels = [];
  9802. this.prodParam.enter(this.prodParam.currentFlags() | PARAM_RETURN);
  9803. node.body = this.parseBlock(true, false, hasStrictModeDirective => {
  9804. const nonSimple = !this.isSimpleParamList(node.params);
  9805. if (hasStrictModeDirective && nonSimple) {
  9806. const errorPos = (node.kind === "method" || node.kind === "constructor") && !!node.key ? node.key.end : node.start;
  9807. this.raise(errorPos, ErrorMessages.IllegalLanguageModeDirective);
  9808. }
  9809. const strictModeChanged = !oldStrict && this.state.strict;
  9810. this.checkParams(node, !this.state.strict && !allowExpression && !isMethod && !nonSimple, allowExpression, strictModeChanged);
  9811. if (this.state.strict && node.id) {
  9812. this.checkLVal(node.id, "function name", BIND_OUTSIDE, undefined, undefined, strictModeChanged);
  9813. }
  9814. });
  9815. this.prodParam.exit();
  9816. this.expressionScope.exit();
  9817. this.state.labels = oldLabels;
  9818. }
  9819. }
  9820. isSimpleParamList(params) {
  9821. for (let i = 0, len = params.length; i < len; i++) {
  9822. if (params[i].type !== "Identifier") return false;
  9823. }
  9824. return true;
  9825. }
  9826. checkParams(node, allowDuplicates, isArrowFunction, strictModeChanged = true) {
  9827. const checkClashes = new Set();
  9828. for (const param of node.params) {
  9829. this.checkLVal(param, "function parameter list", BIND_VAR, allowDuplicates ? null : checkClashes, undefined, strictModeChanged);
  9830. }
  9831. }
  9832. parseExprList(close, allowEmpty, refExpressionErrors, nodeForExtra) {
  9833. const elts = [];
  9834. let first = true;
  9835. while (!this.eat(close)) {
  9836. if (first) {
  9837. first = false;
  9838. } else {
  9839. this.expect(types$1.comma);
  9840. if (this.match(close)) {
  9841. if (nodeForExtra) {
  9842. this.addExtra(nodeForExtra, "trailingComma", this.state.lastTokStart);
  9843. }
  9844. this.next();
  9845. break;
  9846. }
  9847. }
  9848. elts.push(this.parseExprListItem(allowEmpty, refExpressionErrors));
  9849. }
  9850. return elts;
  9851. }
  9852. parseExprListItem(allowEmpty, refExpressionErrors, allowPlaceholder) {
  9853. let elt;
  9854. if (this.match(types$1.comma)) {
  9855. if (!allowEmpty) {
  9856. this.raise(this.state.pos, ErrorMessages.UnexpectedToken, ",");
  9857. }
  9858. elt = null;
  9859. } else if (this.match(types$1.ellipsis)) {
  9860. const spreadNodeStartPos = this.state.start;
  9861. const spreadNodeStartLoc = this.state.startLoc;
  9862. elt = this.parseParenItem(this.parseSpread(refExpressionErrors), spreadNodeStartPos, spreadNodeStartLoc);
  9863. } else if (this.match(types$1.question)) {
  9864. this.expectPlugin("partialApplication");
  9865. if (!allowPlaceholder) {
  9866. this.raise(this.state.start, ErrorMessages.UnexpectedArgumentPlaceholder);
  9867. }
  9868. const node = this.startNode();
  9869. this.next();
  9870. elt = this.finishNode(node, "ArgumentPlaceholder");
  9871. } else {
  9872. elt = this.parseMaybeAssignAllowIn(refExpressionErrors, this.parseParenItem);
  9873. }
  9874. return elt;
  9875. }
  9876. parseIdentifier(liberal) {
  9877. const node = this.startNode();
  9878. const name = this.parseIdentifierName(node.start, liberal);
  9879. return this.createIdentifier(node, name);
  9880. }
  9881. createIdentifier(node, name) {
  9882. node.name = name;
  9883. node.loc.identifierName = name;
  9884. return this.finishNode(node, "Identifier");
  9885. }
  9886. parseIdentifierName(pos, liberal) {
  9887. let name;
  9888. const {
  9889. start,
  9890. type
  9891. } = this.state;
  9892. if (type === types$1.name) {
  9893. name = this.state.value;
  9894. } else if (type.keyword) {
  9895. name = type.keyword;
  9896. } else {
  9897. throw this.unexpected();
  9898. }
  9899. if (liberal) {
  9900. this.state.type = types$1.name;
  9901. } else {
  9902. this.checkReservedWord(name, start, !!type.keyword, false);
  9903. }
  9904. this.next();
  9905. return name;
  9906. }
  9907. checkReservedWord(word, startLoc, checkKeywords, isBinding) {
  9908. if (word.length > 10) {
  9909. return;
  9910. }
  9911. if (!canBeReservedWord(word)) {
  9912. return;
  9913. }
  9914. if (word === "yield") {
  9915. if (this.prodParam.hasYield) {
  9916. this.raise(startLoc, ErrorMessages.YieldBindingIdentifier);
  9917. return;
  9918. }
  9919. } else if (word === "await") {
  9920. if (this.prodParam.hasAwait) {
  9921. this.raise(startLoc, ErrorMessages.AwaitBindingIdentifier);
  9922. return;
  9923. } else if (this.scope.inStaticBlock && !this.scope.inNonArrowFunction) {
  9924. this.raise(startLoc, ErrorMessages.AwaitBindingIdentifierInStaticBlock);
  9925. return;
  9926. } else {
  9927. this.expressionScope.recordAsyncArrowParametersError(startLoc, ErrorMessages.AwaitBindingIdentifier);
  9928. }
  9929. } else if (word === "arguments") {
  9930. if (this.scope.inClassAndNotInNonArrowFunction) {
  9931. this.raise(startLoc, ErrorMessages.ArgumentsInClass);
  9932. return;
  9933. }
  9934. }
  9935. if (checkKeywords && isKeyword(word)) {
  9936. this.raise(startLoc, ErrorMessages.UnexpectedKeyword, word);
  9937. return;
  9938. }
  9939. const reservedTest = !this.state.strict ? isReservedWord : isBinding ? isStrictBindReservedWord : isStrictReservedWord;
  9940. if (reservedTest(word, this.inModule)) {
  9941. this.raise(startLoc, ErrorMessages.UnexpectedReservedWord, word);
  9942. }
  9943. }
  9944. isAwaitAllowed() {
  9945. if (this.prodParam.hasAwait) return true;
  9946. if (this.options.allowAwaitOutsideFunction && !this.scope.inFunction) {
  9947. return true;
  9948. }
  9949. return false;
  9950. }
  9951. parseAwait(startPos, startLoc) {
  9952. const node = this.startNodeAt(startPos, startLoc);
  9953. this.expressionScope.recordParameterInitializerError(node.start, ErrorMessages.AwaitExpressionFormalParameter);
  9954. if (this.eat(types$1.star)) {
  9955. this.raise(node.start, ErrorMessages.ObsoleteAwaitStar);
  9956. }
  9957. if (!this.scope.inFunction && !this.options.allowAwaitOutsideFunction) {
  9958. if (this.isAmbiguousAwait()) {
  9959. this.ambiguousScriptDifferentAst = true;
  9960. } else {
  9961. this.sawUnambiguousESM = true;
  9962. }
  9963. }
  9964. if (!this.state.soloAwait) {
  9965. node.argument = this.parseMaybeUnary(null, true);
  9966. }
  9967. return this.finishNode(node, "AwaitExpression");
  9968. }
  9969. isAmbiguousAwait() {
  9970. return this.hasPrecedingLineBreak() || this.match(types$1.plusMin) || this.match(types$1.parenL) || this.match(types$1.bracketL) || this.match(types$1.backQuote) || this.match(types$1.regexp) || this.match(types$1.slash) || this.hasPlugin("v8intrinsic") && this.match(types$1.modulo);
  9971. }
  9972. parseYield() {
  9973. const node = this.startNode();
  9974. this.expressionScope.recordParameterInitializerError(node.start, ErrorMessages.YieldInParameter);
  9975. this.next();
  9976. let delegating = false;
  9977. let argument = null;
  9978. if (!this.hasPrecedingLineBreak()) {
  9979. delegating = this.eat(types$1.star);
  9980. switch (this.state.type) {
  9981. case types$1.semi:
  9982. case types$1.eof:
  9983. case types$1.braceR:
  9984. case types$1.parenR:
  9985. case types$1.bracketR:
  9986. case types$1.braceBarR:
  9987. case types$1.colon:
  9988. case types$1.comma:
  9989. if (!delegating) break;
  9990. default:
  9991. argument = this.parseMaybeAssign();
  9992. }
  9993. }
  9994. node.delegate = delegating;
  9995. node.argument = argument;
  9996. return this.finishNode(node, "YieldExpression");
  9997. }
  9998. checkPipelineAtInfixOperator(left, leftStartPos) {
  9999. if (this.getPluginOption("pipelineOperator", "proposal") === "smart") {
  10000. if (left.type === "SequenceExpression") {
  10001. this.raise(leftStartPos, ErrorMessages.PipelineHeadSequenceExpression);
  10002. }
  10003. }
  10004. }
  10005. parseSmartPipelineBody(childExpression, startPos, startLoc) {
  10006. this.checkSmartPipelineBodyEarlyErrors(childExpression, startPos);
  10007. return this.parseSmartPipelineBodyInStyle(childExpression, startPos, startLoc);
  10008. }
  10009. checkSmartPipelineBodyEarlyErrors(childExpression, startPos) {
  10010. if (this.match(types$1.arrow)) {
  10011. throw this.raise(this.state.start, ErrorMessages.PipelineBodyNoArrow);
  10012. } else if (childExpression.type === "SequenceExpression") {
  10013. this.raise(startPos, ErrorMessages.PipelineBodySequenceExpression);
  10014. }
  10015. }
  10016. parseSmartPipelineBodyInStyle(childExpression, startPos, startLoc) {
  10017. const bodyNode = this.startNodeAt(startPos, startLoc);
  10018. const isSimpleReference = this.isSimpleReference(childExpression);
  10019. if (isSimpleReference) {
  10020. bodyNode.callee = childExpression;
  10021. } else {
  10022. if (!this.topicReferenceWasUsedInCurrentTopicContext()) {
  10023. this.raise(startPos, ErrorMessages.PipelineTopicUnused);
  10024. }
  10025. bodyNode.expression = childExpression;
  10026. }
  10027. return this.finishNode(bodyNode, isSimpleReference ? "PipelineBareFunction" : "PipelineTopicExpression");
  10028. }
  10029. isSimpleReference(expression) {
  10030. switch (expression.type) {
  10031. case "MemberExpression":
  10032. return !expression.computed && this.isSimpleReference(expression.object);
  10033. case "Identifier":
  10034. return true;
  10035. default:
  10036. return false;
  10037. }
  10038. }
  10039. withTopicPermittingContext(callback) {
  10040. const outerContextTopicState = this.state.topicContext;
  10041. this.state.topicContext = {
  10042. maxNumOfResolvableTopics: 1,
  10043. maxTopicIndex: null
  10044. };
  10045. try {
  10046. return callback();
  10047. } finally {
  10048. this.state.topicContext = outerContextTopicState;
  10049. }
  10050. }
  10051. withTopicForbiddingContext(callback) {
  10052. const outerContextTopicState = this.state.topicContext;
  10053. this.state.topicContext = {
  10054. maxNumOfResolvableTopics: 0,
  10055. maxTopicIndex: null
  10056. };
  10057. try {
  10058. return callback();
  10059. } finally {
  10060. this.state.topicContext = outerContextTopicState;
  10061. }
  10062. }
  10063. withSoloAwaitPermittingContext(callback) {
  10064. const outerContextSoloAwaitState = this.state.soloAwait;
  10065. this.state.soloAwait = true;
  10066. try {
  10067. return callback();
  10068. } finally {
  10069. this.state.soloAwait = outerContextSoloAwaitState;
  10070. }
  10071. }
  10072. allowInAnd(callback) {
  10073. const flags = this.prodParam.currentFlags();
  10074. const prodParamToSet = PARAM_IN & ~flags;
  10075. if (prodParamToSet) {
  10076. this.prodParam.enter(flags | PARAM_IN);
  10077. try {
  10078. return callback();
  10079. } finally {
  10080. this.prodParam.exit();
  10081. }
  10082. }
  10083. return callback();
  10084. }
  10085. disallowInAnd(callback) {
  10086. const flags = this.prodParam.currentFlags();
  10087. const prodParamToClear = PARAM_IN & flags;
  10088. if (prodParamToClear) {
  10089. this.prodParam.enter(flags & ~PARAM_IN);
  10090. try {
  10091. return callback();
  10092. } finally {
  10093. this.prodParam.exit();
  10094. }
  10095. }
  10096. return callback();
  10097. }
  10098. registerTopicReference() {
  10099. this.state.topicContext.maxTopicIndex = 0;
  10100. }
  10101. primaryTopicReferenceIsAllowedInCurrentTopicContext() {
  10102. return this.state.topicContext.maxNumOfResolvableTopics >= 1;
  10103. }
  10104. topicReferenceWasUsedInCurrentTopicContext() {
  10105. return this.state.topicContext.maxTopicIndex != null && this.state.topicContext.maxTopicIndex >= 0;
  10106. }
  10107. parseFSharpPipelineBody(prec) {
  10108. const startPos = this.state.start;
  10109. const startLoc = this.state.startLoc;
  10110. this.state.potentialArrowAt = this.state.start;
  10111. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  10112. this.state.inFSharpPipelineDirectBody = true;
  10113. const ret = this.parseExprOp(this.parseMaybeUnary(), startPos, startLoc, prec);
  10114. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  10115. return ret;
  10116. }
  10117. parseModuleExpression() {
  10118. this.expectPlugin("moduleBlocks");
  10119. const node = this.startNode();
  10120. this.next();
  10121. this.eat(types$1.braceL);
  10122. const revertScopes = this.initializeScopes(true);
  10123. this.enterInitialScopes();
  10124. const program = this.startNode();
  10125. try {
  10126. node.body = this.parseProgram(program, types$1.braceR, "module");
  10127. } finally {
  10128. revertScopes();
  10129. }
  10130. this.eat(types$1.braceR);
  10131. return this.finishNode(node, "ModuleExpression");
  10132. }
  10133. }
  10134. const loopLabel = {
  10135. kind: "loop"
  10136. },
  10137. switchLabel = {
  10138. kind: "switch"
  10139. };
  10140. const FUNC_NO_FLAGS = 0b000,
  10141. FUNC_STATEMENT = 0b001,
  10142. FUNC_HANGING_STATEMENT = 0b010,
  10143. FUNC_NULLABLE_ID = 0b100;
  10144. const loneSurrogate = /[\uD800-\uDFFF]/u;
  10145. const keywordRelationalOperator = /in(?:stanceof)?/y;
  10146. function babel7CompatTokens(tokens) {
  10147. {
  10148. for (let i = 0; i < tokens.length; i++) {
  10149. const token = tokens[i];
  10150. if (token.type === types$1.privateName) {
  10151. const {
  10152. loc,
  10153. start,
  10154. value,
  10155. end
  10156. } = token;
  10157. const hashEndPos = start + 1;
  10158. const hashEndLoc = new Position(loc.start.line, loc.start.column + 1);
  10159. tokens.splice(i, 1, new Token({
  10160. type: types$1.hash,
  10161. value: "#",
  10162. start: start,
  10163. end: hashEndPos,
  10164. startLoc: loc.start,
  10165. endLoc: hashEndLoc
  10166. }), new Token({
  10167. type: types$1.name,
  10168. value: value,
  10169. start: hashEndPos,
  10170. end: end,
  10171. startLoc: hashEndLoc,
  10172. endLoc: loc.end
  10173. }));
  10174. }
  10175. }
  10176. }
  10177. return tokens;
  10178. }
  10179. class StatementParser extends ExpressionParser {
  10180. parseTopLevel(file, program) {
  10181. file.program = this.parseProgram(program);
  10182. file.comments = this.state.comments;
  10183. if (this.options.tokens) file.tokens = babel7CompatTokens(this.tokens);
  10184. return this.finishNode(file, "File");
  10185. }
  10186. parseProgram(program, end = types$1.eof, sourceType = this.options.sourceType) {
  10187. program.sourceType = sourceType;
  10188. program.interpreter = this.parseInterpreterDirective();
  10189. this.parseBlockBody(program, true, true, end);
  10190. if (this.inModule && !this.options.allowUndeclaredExports && this.scope.undefinedExports.size > 0) {
  10191. for (const [name] of Array.from(this.scope.undefinedExports)) {
  10192. const pos = this.scope.undefinedExports.get(name);
  10193. this.raise(pos, ErrorMessages.ModuleExportUndefined, name);
  10194. }
  10195. }
  10196. return this.finishNode(program, "Program");
  10197. }
  10198. stmtToDirective(stmt) {
  10199. const expr = stmt.expression;
  10200. const directiveLiteral = this.startNodeAt(expr.start, expr.loc.start);
  10201. const directive = this.startNodeAt(stmt.start, stmt.loc.start);
  10202. const raw = this.input.slice(expr.start, expr.end);
  10203. const val = directiveLiteral.value = raw.slice(1, -1);
  10204. this.addExtra(directiveLiteral, "raw", raw);
  10205. this.addExtra(directiveLiteral, "rawValue", val);
  10206. directive.value = this.finishNodeAt(directiveLiteral, "DirectiveLiteral", expr.end, expr.loc.end);
  10207. return this.finishNodeAt(directive, "Directive", stmt.end, stmt.loc.end);
  10208. }
  10209. parseInterpreterDirective() {
  10210. if (!this.match(types$1.interpreterDirective)) {
  10211. return null;
  10212. }
  10213. const node = this.startNode();
  10214. node.value = this.state.value;
  10215. this.next();
  10216. return this.finishNode(node, "InterpreterDirective");
  10217. }
  10218. isLet(context) {
  10219. if (!this.isContextual("let")) {
  10220. return false;
  10221. }
  10222. return this.isLetKeyword(context);
  10223. }
  10224. isLetKeyword(context) {
  10225. const next = this.nextTokenStart();
  10226. const nextCh = this.codePointAtPos(next);
  10227. if (nextCh === 92 || nextCh === 91) {
  10228. return true;
  10229. }
  10230. if (context) return false;
  10231. if (nextCh === 123) return true;
  10232. if (isIdentifierStart(nextCh)) {
  10233. keywordRelationalOperator.lastIndex = next;
  10234. const matched = keywordRelationalOperator.exec(this.input);
  10235. if (matched !== null) {
  10236. const endCh = this.codePointAtPos(next + matched[0].length);
  10237. if (!isIdentifierChar(endCh) && endCh !== 92) {
  10238. return false;
  10239. }
  10240. }
  10241. return true;
  10242. }
  10243. return false;
  10244. }
  10245. parseStatement(context, topLevel) {
  10246. if (this.match(types$1.at)) {
  10247. this.parseDecorators(true);
  10248. }
  10249. return this.parseStatementContent(context, topLevel);
  10250. }
  10251. parseStatementContent(context, topLevel) {
  10252. let starttype = this.state.type;
  10253. const node = this.startNode();
  10254. let kind;
  10255. if (this.isLet(context)) {
  10256. starttype = types$1._var;
  10257. kind = "let";
  10258. }
  10259. switch (starttype) {
  10260. case types$1._break:
  10261. case types$1._continue:
  10262. return this.parseBreakContinueStatement(node, starttype.keyword);
  10263. case types$1._debugger:
  10264. return this.parseDebuggerStatement(node);
  10265. case types$1._do:
  10266. return this.parseDoStatement(node);
  10267. case types$1._for:
  10268. return this.parseForStatement(node);
  10269. case types$1._function:
  10270. if (this.lookaheadCharCode() === 46) break;
  10271. if (context) {
  10272. if (this.state.strict) {
  10273. this.raise(this.state.start, ErrorMessages.StrictFunction);
  10274. } else if (context !== "if" && context !== "label") {
  10275. this.raise(this.state.start, ErrorMessages.SloppyFunction);
  10276. }
  10277. }
  10278. return this.parseFunctionStatement(node, false, !context);
  10279. case types$1._class:
  10280. if (context) this.unexpected();
  10281. return this.parseClass(node, true);
  10282. case types$1._if:
  10283. return this.parseIfStatement(node);
  10284. case types$1._return:
  10285. return this.parseReturnStatement(node);
  10286. case types$1._switch:
  10287. return this.parseSwitchStatement(node);
  10288. case types$1._throw:
  10289. return this.parseThrowStatement(node);
  10290. case types$1._try:
  10291. return this.parseTryStatement(node);
  10292. case types$1._const:
  10293. case types$1._var:
  10294. kind = kind || this.state.value;
  10295. if (context && kind !== "var") {
  10296. this.raise(this.state.start, ErrorMessages.UnexpectedLexicalDeclaration);
  10297. }
  10298. return this.parseVarStatement(node, kind);
  10299. case types$1._while:
  10300. return this.parseWhileStatement(node);
  10301. case types$1._with:
  10302. return this.parseWithStatement(node);
  10303. case types$1.braceL:
  10304. return this.parseBlock();
  10305. case types$1.semi:
  10306. return this.parseEmptyStatement(node);
  10307. case types$1._import:
  10308. {
  10309. const nextTokenCharCode = this.lookaheadCharCode();
  10310. if (nextTokenCharCode === 40 || nextTokenCharCode === 46) {
  10311. break;
  10312. }
  10313. }
  10314. case types$1._export:
  10315. {
  10316. if (!this.options.allowImportExportEverywhere && !topLevel) {
  10317. this.raise(this.state.start, ErrorMessages.UnexpectedImportExport);
  10318. }
  10319. this.next();
  10320. let result;
  10321. if (starttype === types$1._import) {
  10322. result = this.parseImport(node);
  10323. if (result.type === "ImportDeclaration" && (!result.importKind || result.importKind === "value")) {
  10324. this.sawUnambiguousESM = true;
  10325. }
  10326. } else {
  10327. result = this.parseExport(node);
  10328. if (result.type === "ExportNamedDeclaration" && (!result.exportKind || result.exportKind === "value") || result.type === "ExportAllDeclaration" && (!result.exportKind || result.exportKind === "value") || result.type === "ExportDefaultDeclaration") {
  10329. this.sawUnambiguousESM = true;
  10330. }
  10331. }
  10332. this.assertModuleNodeAllowed(node);
  10333. return result;
  10334. }
  10335. default:
  10336. {
  10337. if (this.isAsyncFunction()) {
  10338. if (context) {
  10339. this.raise(this.state.start, ErrorMessages.AsyncFunctionInSingleStatementContext);
  10340. }
  10341. this.next();
  10342. return this.parseFunctionStatement(node, true, !context);
  10343. }
  10344. }
  10345. }
  10346. const maybeName = this.state.value;
  10347. const expr = this.parseExpression();
  10348. if (starttype === types$1.name && expr.type === "Identifier" && this.eat(types$1.colon)) {
  10349. return this.parseLabeledStatement(node, maybeName, expr, context);
  10350. } else {
  10351. return this.parseExpressionStatement(node, expr);
  10352. }
  10353. }
  10354. assertModuleNodeAllowed(node) {
  10355. if (!this.options.allowImportExportEverywhere && !this.inModule) {
  10356. this.raise(node.start, SourceTypeModuleErrorMessages.ImportOutsideModule);
  10357. }
  10358. }
  10359. takeDecorators(node) {
  10360. const decorators = this.state.decoratorStack[this.state.decoratorStack.length - 1];
  10361. if (decorators.length) {
  10362. node.decorators = decorators;
  10363. this.resetStartLocationFromNode(node, decorators[0]);
  10364. this.state.decoratorStack[this.state.decoratorStack.length - 1] = [];
  10365. }
  10366. }
  10367. canHaveLeadingDecorator() {
  10368. return this.match(types$1._class);
  10369. }
  10370. parseDecorators(allowExport) {
  10371. const currentContextDecorators = this.state.decoratorStack[this.state.decoratorStack.length - 1];
  10372. while (this.match(types$1.at)) {
  10373. const decorator = this.parseDecorator();
  10374. currentContextDecorators.push(decorator);
  10375. }
  10376. if (this.match(types$1._export)) {
  10377. if (!allowExport) {
  10378. this.unexpected();
  10379. }
  10380. if (this.hasPlugin("decorators") && !this.getPluginOption("decorators", "decoratorsBeforeExport")) {
  10381. this.raise(this.state.start, ErrorMessages.DecoratorExportClass);
  10382. }
  10383. } else if (!this.canHaveLeadingDecorator()) {
  10384. throw this.raise(this.state.start, ErrorMessages.UnexpectedLeadingDecorator);
  10385. }
  10386. }
  10387. parseDecorator() {
  10388. this.expectOnePlugin(["decorators-legacy", "decorators"]);
  10389. const node = this.startNode();
  10390. this.next();
  10391. if (this.hasPlugin("decorators")) {
  10392. this.state.decoratorStack.push([]);
  10393. const startPos = this.state.start;
  10394. const startLoc = this.state.startLoc;
  10395. let expr;
  10396. if (this.eat(types$1.parenL)) {
  10397. expr = this.parseExpression();
  10398. this.expect(types$1.parenR);
  10399. } else {
  10400. expr = this.parseIdentifier(false);
  10401. while (this.eat(types$1.dot)) {
  10402. const node = this.startNodeAt(startPos, startLoc);
  10403. node.object = expr;
  10404. node.property = this.parseIdentifier(true);
  10405. node.computed = false;
  10406. expr = this.finishNode(node, "MemberExpression");
  10407. }
  10408. }
  10409. node.expression = this.parseMaybeDecoratorArguments(expr);
  10410. this.state.decoratorStack.pop();
  10411. } else {
  10412. node.expression = this.parseExprSubscripts();
  10413. }
  10414. return this.finishNode(node, "Decorator");
  10415. }
  10416. parseMaybeDecoratorArguments(expr) {
  10417. if (this.eat(types$1.parenL)) {
  10418. const node = this.startNodeAtNode(expr);
  10419. node.callee = expr;
  10420. node.arguments = this.parseCallExpressionArguments(types$1.parenR, false);
  10421. this.toReferencedList(node.arguments);
  10422. return this.finishNode(node, "CallExpression");
  10423. }
  10424. return expr;
  10425. }
  10426. parseBreakContinueStatement(node, keyword) {
  10427. const isBreak = keyword === "break";
  10428. this.next();
  10429. if (this.isLineTerminator()) {
  10430. node.label = null;
  10431. } else {
  10432. node.label = this.parseIdentifier();
  10433. this.semicolon();
  10434. }
  10435. this.verifyBreakContinue(node, keyword);
  10436. return this.finishNode(node, isBreak ? "BreakStatement" : "ContinueStatement");
  10437. }
  10438. verifyBreakContinue(node, keyword) {
  10439. const isBreak = keyword === "break";
  10440. let i;
  10441. for (i = 0; i < this.state.labels.length; ++i) {
  10442. const lab = this.state.labels[i];
  10443. if (node.label == null || lab.name === node.label.name) {
  10444. if (lab.kind != null && (isBreak || lab.kind === "loop")) break;
  10445. if (node.label && isBreak) break;
  10446. }
  10447. }
  10448. if (i === this.state.labels.length) {
  10449. this.raise(node.start, ErrorMessages.IllegalBreakContinue, keyword);
  10450. }
  10451. }
  10452. parseDebuggerStatement(node) {
  10453. this.next();
  10454. this.semicolon();
  10455. return this.finishNode(node, "DebuggerStatement");
  10456. }
  10457. parseHeaderExpression() {
  10458. this.expect(types$1.parenL);
  10459. const val = this.parseExpression();
  10460. this.expect(types$1.parenR);
  10461. return val;
  10462. }
  10463. parseDoStatement(node) {
  10464. this.next();
  10465. this.state.labels.push(loopLabel);
  10466. node.body = this.withTopicForbiddingContext(() => this.parseStatement("do"));
  10467. this.state.labels.pop();
  10468. this.expect(types$1._while);
  10469. node.test = this.parseHeaderExpression();
  10470. this.eat(types$1.semi);
  10471. return this.finishNode(node, "DoWhileStatement");
  10472. }
  10473. parseForStatement(node) {
  10474. this.next();
  10475. this.state.labels.push(loopLabel);
  10476. let awaitAt = -1;
  10477. if (this.isAwaitAllowed() && this.eatContextual("await")) {
  10478. awaitAt = this.state.lastTokStart;
  10479. }
  10480. this.scope.enter(SCOPE_OTHER);
  10481. this.expect(types$1.parenL);
  10482. if (this.match(types$1.semi)) {
  10483. if (awaitAt > -1) {
  10484. this.unexpected(awaitAt);
  10485. }
  10486. return this.parseFor(node, null);
  10487. }
  10488. const startsWithLet = this.isContextual("let");
  10489. const isLet = startsWithLet && this.isLetKeyword();
  10490. if (this.match(types$1._var) || this.match(types$1._const) || isLet) {
  10491. const init = this.startNode();
  10492. const kind = isLet ? "let" : this.state.value;
  10493. this.next();
  10494. this.parseVar(init, true, kind);
  10495. this.finishNode(init, "VariableDeclaration");
  10496. if ((this.match(types$1._in) || this.isContextual("of")) && init.declarations.length === 1) {
  10497. return this.parseForIn(node, init, awaitAt);
  10498. }
  10499. if (awaitAt > -1) {
  10500. this.unexpected(awaitAt);
  10501. }
  10502. return this.parseFor(node, init);
  10503. }
  10504. const startsWithUnescapedName = this.match(types$1.name) && !this.state.containsEsc;
  10505. const refExpressionErrors = new ExpressionErrors();
  10506. const init = this.parseExpression(true, refExpressionErrors);
  10507. const isForOf = this.isContextual("of");
  10508. if (isForOf) {
  10509. if (startsWithLet) {
  10510. this.raise(init.start, ErrorMessages.ForOfLet);
  10511. } else if (awaitAt === -1 && startsWithUnescapedName && init.type === "Identifier" && init.name === "async") {
  10512. this.raise(init.start, ErrorMessages.ForOfAsync);
  10513. }
  10514. }
  10515. if (isForOf || this.match(types$1._in)) {
  10516. this.toAssignable(init, true);
  10517. const description = isForOf ? "for-of statement" : "for-in statement";
  10518. this.checkLVal(init, description);
  10519. return this.parseForIn(node, init, awaitAt);
  10520. } else {
  10521. this.checkExpressionErrors(refExpressionErrors, true);
  10522. }
  10523. if (awaitAt > -1) {
  10524. this.unexpected(awaitAt);
  10525. }
  10526. return this.parseFor(node, init);
  10527. }
  10528. parseFunctionStatement(node, isAsync, declarationPosition) {
  10529. this.next();
  10530. return this.parseFunction(node, FUNC_STATEMENT | (declarationPosition ? 0 : FUNC_HANGING_STATEMENT), isAsync);
  10531. }
  10532. parseIfStatement(node) {
  10533. this.next();
  10534. node.test = this.parseHeaderExpression();
  10535. node.consequent = this.parseStatement("if");
  10536. node.alternate = this.eat(types$1._else) ? this.parseStatement("if") : null;
  10537. return this.finishNode(node, "IfStatement");
  10538. }
  10539. parseReturnStatement(node) {
  10540. if (!this.prodParam.hasReturn && !this.options.allowReturnOutsideFunction) {
  10541. this.raise(this.state.start, ErrorMessages.IllegalReturn);
  10542. }
  10543. this.next();
  10544. if (this.isLineTerminator()) {
  10545. node.argument = null;
  10546. } else {
  10547. node.argument = this.parseExpression();
  10548. this.semicolon();
  10549. }
  10550. return this.finishNode(node, "ReturnStatement");
  10551. }
  10552. parseSwitchStatement(node) {
  10553. this.next();
  10554. node.discriminant = this.parseHeaderExpression();
  10555. const cases = node.cases = [];
  10556. this.expect(types$1.braceL);
  10557. this.state.labels.push(switchLabel);
  10558. this.scope.enter(SCOPE_OTHER);
  10559. let cur;
  10560. for (let sawDefault; !this.match(types$1.braceR);) {
  10561. if (this.match(types$1._case) || this.match(types$1._default)) {
  10562. const isCase = this.match(types$1._case);
  10563. if (cur) this.finishNode(cur, "SwitchCase");
  10564. cases.push(cur = this.startNode());
  10565. cur.consequent = [];
  10566. this.next();
  10567. if (isCase) {
  10568. cur.test = this.parseExpression();
  10569. } else {
  10570. if (sawDefault) {
  10571. this.raise(this.state.lastTokStart, ErrorMessages.MultipleDefaultsInSwitch);
  10572. }
  10573. sawDefault = true;
  10574. cur.test = null;
  10575. }
  10576. this.expect(types$1.colon);
  10577. } else {
  10578. if (cur) {
  10579. cur.consequent.push(this.parseStatement(null));
  10580. } else {
  10581. this.unexpected();
  10582. }
  10583. }
  10584. }
  10585. this.scope.exit();
  10586. if (cur) this.finishNode(cur, "SwitchCase");
  10587. this.next();
  10588. this.state.labels.pop();
  10589. return this.finishNode(node, "SwitchStatement");
  10590. }
  10591. parseThrowStatement(node) {
  10592. this.next();
  10593. if (this.hasPrecedingLineBreak()) {
  10594. this.raise(this.state.lastTokEnd, ErrorMessages.NewlineAfterThrow);
  10595. }
  10596. node.argument = this.parseExpression();
  10597. this.semicolon();
  10598. return this.finishNode(node, "ThrowStatement");
  10599. }
  10600. parseCatchClauseParam() {
  10601. const param = this.parseBindingAtom();
  10602. const simple = param.type === "Identifier";
  10603. this.scope.enter(simple ? SCOPE_SIMPLE_CATCH : 0);
  10604. this.checkLVal(param, "catch clause", BIND_LEXICAL);
  10605. return param;
  10606. }
  10607. parseTryStatement(node) {
  10608. this.next();
  10609. node.block = this.parseBlock();
  10610. node.handler = null;
  10611. if (this.match(types$1._catch)) {
  10612. const clause = this.startNode();
  10613. this.next();
  10614. if (this.match(types$1.parenL)) {
  10615. this.expect(types$1.parenL);
  10616. clause.param = this.parseCatchClauseParam();
  10617. this.expect(types$1.parenR);
  10618. } else {
  10619. clause.param = null;
  10620. this.scope.enter(SCOPE_OTHER);
  10621. }
  10622. clause.body = this.withTopicForbiddingContext(() => this.parseBlock(false, false));
  10623. this.scope.exit();
  10624. node.handler = this.finishNode(clause, "CatchClause");
  10625. }
  10626. node.finalizer = this.eat(types$1._finally) ? this.parseBlock() : null;
  10627. if (!node.handler && !node.finalizer) {
  10628. this.raise(node.start, ErrorMessages.NoCatchOrFinally);
  10629. }
  10630. return this.finishNode(node, "TryStatement");
  10631. }
  10632. parseVarStatement(node, kind) {
  10633. this.next();
  10634. this.parseVar(node, false, kind);
  10635. this.semicolon();
  10636. return this.finishNode(node, "VariableDeclaration");
  10637. }
  10638. parseWhileStatement(node) {
  10639. this.next();
  10640. node.test = this.parseHeaderExpression();
  10641. this.state.labels.push(loopLabel);
  10642. node.body = this.withTopicForbiddingContext(() => this.parseStatement("while"));
  10643. this.state.labels.pop();
  10644. return this.finishNode(node, "WhileStatement");
  10645. }
  10646. parseWithStatement(node) {
  10647. if (this.state.strict) {
  10648. this.raise(this.state.start, ErrorMessages.StrictWith);
  10649. }
  10650. this.next();
  10651. node.object = this.parseHeaderExpression();
  10652. node.body = this.withTopicForbiddingContext(() => this.parseStatement("with"));
  10653. return this.finishNode(node, "WithStatement");
  10654. }
  10655. parseEmptyStatement(node) {
  10656. this.next();
  10657. return this.finishNode(node, "EmptyStatement");
  10658. }
  10659. parseLabeledStatement(node, maybeName, expr, context) {
  10660. for (const label of this.state.labels) {
  10661. if (label.name === maybeName) {
  10662. this.raise(expr.start, ErrorMessages.LabelRedeclaration, maybeName);
  10663. }
  10664. }
  10665. const kind = this.state.type.isLoop ? "loop" : this.match(types$1._switch) ? "switch" : null;
  10666. for (let i = this.state.labels.length - 1; i >= 0; i--) {
  10667. const label = this.state.labels[i];
  10668. if (label.statementStart === node.start) {
  10669. label.statementStart = this.state.start;
  10670. label.kind = kind;
  10671. } else {
  10672. break;
  10673. }
  10674. }
  10675. this.state.labels.push({
  10676. name: maybeName,
  10677. kind: kind,
  10678. statementStart: this.state.start
  10679. });
  10680. node.body = this.parseStatement(context ? context.indexOf("label") === -1 ? context + "label" : context : "label");
  10681. this.state.labels.pop();
  10682. node.label = expr;
  10683. return this.finishNode(node, "LabeledStatement");
  10684. }
  10685. parseExpressionStatement(node, expr) {
  10686. node.expression = expr;
  10687. this.semicolon();
  10688. return this.finishNode(node, "ExpressionStatement");
  10689. }
  10690. parseBlock(allowDirectives = false, createNewLexicalScope = true, afterBlockParse) {
  10691. const node = this.startNode();
  10692. if (allowDirectives) {
  10693. this.state.strictErrors.clear();
  10694. }
  10695. this.expect(types$1.braceL);
  10696. if (createNewLexicalScope) {
  10697. this.scope.enter(SCOPE_OTHER);
  10698. }
  10699. this.parseBlockBody(node, allowDirectives, false, types$1.braceR, afterBlockParse);
  10700. if (createNewLexicalScope) {
  10701. this.scope.exit();
  10702. }
  10703. return this.finishNode(node, "BlockStatement");
  10704. }
  10705. isValidDirective(stmt) {
  10706. return stmt.type === "ExpressionStatement" && stmt.expression.type === "StringLiteral" && !stmt.expression.extra.parenthesized;
  10707. }
  10708. parseBlockBody(node, allowDirectives, topLevel, end, afterBlockParse) {
  10709. const body = node.body = [];
  10710. const directives = node.directives = [];
  10711. this.parseBlockOrModuleBlockBody(body, allowDirectives ? directives : undefined, topLevel, end, afterBlockParse);
  10712. }
  10713. parseBlockOrModuleBlockBody(body, directives, topLevel, end, afterBlockParse) {
  10714. const oldStrict = this.state.strict;
  10715. let hasStrictModeDirective = false;
  10716. let parsedNonDirective = false;
  10717. while (!this.match(end)) {
  10718. const stmt = this.parseStatement(null, topLevel);
  10719. if (directives && !parsedNonDirective) {
  10720. if (this.isValidDirective(stmt)) {
  10721. const directive = this.stmtToDirective(stmt);
  10722. directives.push(directive);
  10723. if (!hasStrictModeDirective && directive.value.value === "use strict") {
  10724. hasStrictModeDirective = true;
  10725. this.setStrict(true);
  10726. }
  10727. continue;
  10728. }
  10729. parsedNonDirective = true;
  10730. this.state.strictErrors.clear();
  10731. }
  10732. body.push(stmt);
  10733. }
  10734. if (afterBlockParse) {
  10735. afterBlockParse.call(this, hasStrictModeDirective);
  10736. }
  10737. if (!oldStrict) {
  10738. this.setStrict(false);
  10739. }
  10740. this.next();
  10741. }
  10742. parseFor(node, init) {
  10743. node.init = init;
  10744. this.semicolon(false);
  10745. node.test = this.match(types$1.semi) ? null : this.parseExpression();
  10746. this.semicolon(false);
  10747. node.update = this.match(types$1.parenR) ? null : this.parseExpression();
  10748. this.expect(types$1.parenR);
  10749. node.body = this.withTopicForbiddingContext(() => this.parseStatement("for"));
  10750. this.scope.exit();
  10751. this.state.labels.pop();
  10752. return this.finishNode(node, "ForStatement");
  10753. }
  10754. parseForIn(node, init, awaitAt) {
  10755. const isForIn = this.match(types$1._in);
  10756. this.next();
  10757. if (isForIn) {
  10758. if (awaitAt > -1) this.unexpected(awaitAt);
  10759. } else {
  10760. node.await = awaitAt > -1;
  10761. }
  10762. if (init.type === "VariableDeclaration" && init.declarations[0].init != null && (!isForIn || this.state.strict || init.kind !== "var" || init.declarations[0].id.type !== "Identifier")) {
  10763. this.raise(init.start, ErrorMessages.ForInOfLoopInitializer, isForIn ? "for-in" : "for-of");
  10764. } else if (init.type === "AssignmentPattern") {
  10765. this.raise(init.start, ErrorMessages.InvalidLhs, "for-loop");
  10766. }
  10767. node.left = init;
  10768. node.right = isForIn ? this.parseExpression() : this.parseMaybeAssignAllowIn();
  10769. this.expect(types$1.parenR);
  10770. node.body = this.withTopicForbiddingContext(() => this.parseStatement("for"));
  10771. this.scope.exit();
  10772. this.state.labels.pop();
  10773. return this.finishNode(node, isForIn ? "ForInStatement" : "ForOfStatement");
  10774. }
  10775. parseVar(node, isFor, kind) {
  10776. const declarations = node.declarations = [];
  10777. const isTypescript = this.hasPlugin("typescript");
  10778. node.kind = kind;
  10779. for (;;) {
  10780. const decl = this.startNode();
  10781. this.parseVarId(decl, kind);
  10782. if (this.eat(types$1.eq)) {
  10783. decl.init = isFor ? this.parseMaybeAssignDisallowIn() : this.parseMaybeAssignAllowIn();
  10784. } else {
  10785. if (kind === "const" && !(this.match(types$1._in) || this.isContextual("of"))) {
  10786. if (!isTypescript) {
  10787. this.raise(this.state.lastTokEnd, ErrorMessages.DeclarationMissingInitializer, "Const declarations");
  10788. }
  10789. } else if (decl.id.type !== "Identifier" && !(isFor && (this.match(types$1._in) || this.isContextual("of")))) {
  10790. this.raise(this.state.lastTokEnd, ErrorMessages.DeclarationMissingInitializer, "Complex binding patterns");
  10791. }
  10792. decl.init = null;
  10793. }
  10794. declarations.push(this.finishNode(decl, "VariableDeclarator"));
  10795. if (!this.eat(types$1.comma)) break;
  10796. }
  10797. return node;
  10798. }
  10799. parseVarId(decl, kind) {
  10800. decl.id = this.parseBindingAtom();
  10801. this.checkLVal(decl.id, "variable declaration", kind === "var" ? BIND_VAR : BIND_LEXICAL, undefined, kind !== "var");
  10802. }
  10803. parseFunction(node, statement = FUNC_NO_FLAGS, isAsync = false) {
  10804. const isStatement = statement & FUNC_STATEMENT;
  10805. const isHangingStatement = statement & FUNC_HANGING_STATEMENT;
  10806. const requireId = !!isStatement && !(statement & FUNC_NULLABLE_ID);
  10807. this.initFunction(node, isAsync);
  10808. if (this.match(types$1.star) && isHangingStatement) {
  10809. this.raise(this.state.start, ErrorMessages.GeneratorInSingleStatementContext);
  10810. }
  10811. node.generator = this.eat(types$1.star);
  10812. if (isStatement) {
  10813. node.id = this.parseFunctionId(requireId);
  10814. }
  10815. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  10816. this.state.maybeInArrowParameters = false;
  10817. this.scope.enter(SCOPE_FUNCTION);
  10818. this.prodParam.enter(functionFlags(isAsync, node.generator));
  10819. if (!isStatement) {
  10820. node.id = this.parseFunctionId();
  10821. }
  10822. this.parseFunctionParams(node, false);
  10823. this.withTopicForbiddingContext(() => {
  10824. this.parseFunctionBodyAndFinish(node, isStatement ? "FunctionDeclaration" : "FunctionExpression");
  10825. });
  10826. this.prodParam.exit();
  10827. this.scope.exit();
  10828. if (isStatement && !isHangingStatement) {
  10829. this.registerFunctionStatementId(node);
  10830. }
  10831. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  10832. return node;
  10833. }
  10834. parseFunctionId(requireId) {
  10835. return requireId || this.match(types$1.name) ? this.parseIdentifier() : null;
  10836. }
  10837. parseFunctionParams(node, allowModifiers) {
  10838. this.expect(types$1.parenL);
  10839. this.expressionScope.enter(newParameterDeclarationScope());
  10840. node.params = this.parseBindingList(types$1.parenR, 41, false, allowModifiers);
  10841. this.expressionScope.exit();
  10842. }
  10843. registerFunctionStatementId(node) {
  10844. if (!node.id) return;
  10845. this.scope.declareName(node.id.name, this.state.strict || node.generator || node.async ? this.scope.treatFunctionsAsVar ? BIND_VAR : BIND_LEXICAL : BIND_FUNCTION, node.id.start);
  10846. }
  10847. parseClass(node, isStatement, optionalId) {
  10848. this.next();
  10849. this.takeDecorators(node);
  10850. const oldStrict = this.state.strict;
  10851. this.state.strict = true;
  10852. this.parseClassId(node, isStatement, optionalId);
  10853. this.parseClassSuper(node);
  10854. node.body = this.parseClassBody(!!node.superClass, oldStrict);
  10855. return this.finishNode(node, isStatement ? "ClassDeclaration" : "ClassExpression");
  10856. }
  10857. isClassProperty() {
  10858. return this.match(types$1.eq) || this.match(types$1.semi) || this.match(types$1.braceR);
  10859. }
  10860. isClassMethod() {
  10861. return this.match(types$1.parenL);
  10862. }
  10863. isNonstaticConstructor(method) {
  10864. return !method.computed && !method.static && (method.key.name === "constructor" || method.key.value === "constructor");
  10865. }
  10866. parseClassBody(hadSuperClass, oldStrict) {
  10867. this.classScope.enter();
  10868. const state = {
  10869. hadConstructor: false,
  10870. hadSuperClass
  10871. };
  10872. let decorators = [];
  10873. const classBody = this.startNode();
  10874. classBody.body = [];
  10875. this.expect(types$1.braceL);
  10876. this.withTopicForbiddingContext(() => {
  10877. while (!this.match(types$1.braceR)) {
  10878. if (this.eat(types$1.semi)) {
  10879. if (decorators.length > 0) {
  10880. throw this.raise(this.state.lastTokEnd, ErrorMessages.DecoratorSemicolon);
  10881. }
  10882. continue;
  10883. }
  10884. if (this.match(types$1.at)) {
  10885. decorators.push(this.parseDecorator());
  10886. continue;
  10887. }
  10888. const member = this.startNode();
  10889. if (decorators.length) {
  10890. member.decorators = decorators;
  10891. this.resetStartLocationFromNode(member, decorators[0]);
  10892. decorators = [];
  10893. }
  10894. this.parseClassMember(classBody, member, state);
  10895. if (member.kind === "constructor" && member.decorators && member.decorators.length > 0) {
  10896. this.raise(member.start, ErrorMessages.DecoratorConstructor);
  10897. }
  10898. }
  10899. });
  10900. this.state.strict = oldStrict;
  10901. this.next();
  10902. if (decorators.length) {
  10903. throw this.raise(this.state.start, ErrorMessages.TrailingDecorator);
  10904. }
  10905. this.classScope.exit();
  10906. return this.finishNode(classBody, "ClassBody");
  10907. }
  10908. parseClassMemberFromModifier(classBody, member) {
  10909. const key = this.parseIdentifier(true);
  10910. if (this.isClassMethod()) {
  10911. const method = member;
  10912. method.kind = "method";
  10913. method.computed = false;
  10914. method.key = key;
  10915. method.static = false;
  10916. this.pushClassMethod(classBody, method, false, false, false, false);
  10917. return true;
  10918. } else if (this.isClassProperty()) {
  10919. const prop = member;
  10920. prop.computed = false;
  10921. prop.key = key;
  10922. prop.static = false;
  10923. classBody.body.push(this.parseClassProperty(prop));
  10924. return true;
  10925. }
  10926. return false;
  10927. }
  10928. parseClassMember(classBody, member, state) {
  10929. const isStatic = this.isContextual("static");
  10930. if (isStatic) {
  10931. if (this.parseClassMemberFromModifier(classBody, member)) {
  10932. return;
  10933. }
  10934. if (this.eat(types$1.braceL)) {
  10935. this.parseClassStaticBlock(classBody, member);
  10936. return;
  10937. }
  10938. }
  10939. this.parseClassMemberWithIsStatic(classBody, member, state, isStatic);
  10940. }
  10941. parseClassMemberWithIsStatic(classBody, member, state, isStatic) {
  10942. const publicMethod = member;
  10943. const privateMethod = member;
  10944. const publicProp = member;
  10945. const privateProp = member;
  10946. const method = publicMethod;
  10947. const publicMember = publicMethod;
  10948. member.static = isStatic;
  10949. if (this.eat(types$1.star)) {
  10950. method.kind = "method";
  10951. const isPrivateName = this.match(types$1.privateName);
  10952. this.parseClassElementName(method);
  10953. if (isPrivateName) {
  10954. this.pushClassPrivateMethod(classBody, privateMethod, true, false);
  10955. return;
  10956. }
  10957. if (this.isNonstaticConstructor(publicMethod)) {
  10958. this.raise(publicMethod.key.start, ErrorMessages.ConstructorIsGenerator);
  10959. }
  10960. this.pushClassMethod(classBody, publicMethod, true, false, false, false);
  10961. return;
  10962. }
  10963. const containsEsc = this.state.containsEsc;
  10964. const isPrivate = this.match(types$1.privateName);
  10965. const key = this.parseClassElementName(member);
  10966. const isSimple = key.type === "Identifier";
  10967. const maybeQuestionTokenStart = this.state.start;
  10968. this.parsePostMemberNameModifiers(publicMember);
  10969. if (this.isClassMethod()) {
  10970. method.kind = "method";
  10971. if (isPrivate) {
  10972. this.pushClassPrivateMethod(classBody, privateMethod, false, false);
  10973. return;
  10974. }
  10975. const isConstructor = this.isNonstaticConstructor(publicMethod);
  10976. let allowsDirectSuper = false;
  10977. if (isConstructor) {
  10978. publicMethod.kind = "constructor";
  10979. if (state.hadConstructor && !this.hasPlugin("typescript")) {
  10980. this.raise(key.start, ErrorMessages.DuplicateConstructor);
  10981. }
  10982. if (isConstructor && this.hasPlugin("typescript") && member.override) {
  10983. this.raise(key.start, ErrorMessages.OverrideOnConstructor);
  10984. }
  10985. state.hadConstructor = true;
  10986. allowsDirectSuper = state.hadSuperClass;
  10987. }
  10988. this.pushClassMethod(classBody, publicMethod, false, false, isConstructor, allowsDirectSuper);
  10989. } else if (this.isClassProperty()) {
  10990. if (isPrivate) {
  10991. this.pushClassPrivateProperty(classBody, privateProp);
  10992. } else {
  10993. this.pushClassProperty(classBody, publicProp);
  10994. }
  10995. } else if (isSimple && key.name === "async" && !containsEsc && !this.isLineTerminator()) {
  10996. const isGenerator = this.eat(types$1.star);
  10997. if (publicMember.optional) {
  10998. this.unexpected(maybeQuestionTokenStart);
  10999. }
  11000. method.kind = "method";
  11001. const isPrivate = this.match(types$1.privateName);
  11002. this.parseClassElementName(method);
  11003. this.parsePostMemberNameModifiers(publicMember);
  11004. if (isPrivate) {
  11005. this.pushClassPrivateMethod(classBody, privateMethod, isGenerator, true);
  11006. } else {
  11007. if (this.isNonstaticConstructor(publicMethod)) {
  11008. this.raise(publicMethod.key.start, ErrorMessages.ConstructorIsAsync);
  11009. }
  11010. this.pushClassMethod(classBody, publicMethod, isGenerator, true, false, false);
  11011. }
  11012. } else if (isSimple && (key.name === "get" || key.name === "set") && !containsEsc && !(this.match(types$1.star) && this.isLineTerminator())) {
  11013. method.kind = key.name;
  11014. const isPrivate = this.match(types$1.privateName);
  11015. this.parseClassElementName(publicMethod);
  11016. if (isPrivate) {
  11017. this.pushClassPrivateMethod(classBody, privateMethod, false, false);
  11018. } else {
  11019. if (this.isNonstaticConstructor(publicMethod)) {
  11020. this.raise(publicMethod.key.start, ErrorMessages.ConstructorIsAccessor);
  11021. }
  11022. this.pushClassMethod(classBody, publicMethod, false, false, false, false);
  11023. }
  11024. this.checkGetterSetterParams(publicMethod);
  11025. } else if (this.isLineTerminator()) {
  11026. if (isPrivate) {
  11027. this.pushClassPrivateProperty(classBody, privateProp);
  11028. } else {
  11029. this.pushClassProperty(classBody, publicProp);
  11030. }
  11031. } else {
  11032. this.unexpected();
  11033. }
  11034. }
  11035. parseClassElementName(member) {
  11036. const {
  11037. type,
  11038. value,
  11039. start
  11040. } = this.state;
  11041. if ((type === types$1.name || type === types$1.string) && member.static && value === "prototype") {
  11042. this.raise(start, ErrorMessages.StaticPrototype);
  11043. }
  11044. if (type === types$1.privateName && value === "constructor") {
  11045. this.raise(start, ErrorMessages.ConstructorClassPrivateField);
  11046. }
  11047. return this.parsePropertyName(member, true);
  11048. }
  11049. parseClassStaticBlock(classBody, member) {
  11050. var _member$decorators;
  11051. this.expectPlugin("classStaticBlock", member.start);
  11052. this.scope.enter(SCOPE_CLASS | SCOPE_STATIC_BLOCK | SCOPE_SUPER);
  11053. const oldLabels = this.state.labels;
  11054. this.state.labels = [];
  11055. this.prodParam.enter(PARAM);
  11056. const body = member.body = [];
  11057. this.parseBlockOrModuleBlockBody(body, undefined, false, types$1.braceR);
  11058. this.prodParam.exit();
  11059. this.scope.exit();
  11060. this.state.labels = oldLabels;
  11061. classBody.body.push(this.finishNode(member, "StaticBlock"));
  11062. if ((_member$decorators = member.decorators) != null && _member$decorators.length) {
  11063. this.raise(member.start, ErrorMessages.DecoratorStaticBlock);
  11064. }
  11065. }
  11066. pushClassProperty(classBody, prop) {
  11067. if (!prop.computed && (prop.key.name === "constructor" || prop.key.value === "constructor")) {
  11068. this.raise(prop.key.start, ErrorMessages.ConstructorClassField);
  11069. }
  11070. classBody.body.push(this.parseClassProperty(prop));
  11071. }
  11072. pushClassPrivateProperty(classBody, prop) {
  11073. const node = this.parseClassPrivateProperty(prop);
  11074. classBody.body.push(node);
  11075. this.classScope.declarePrivateName(this.getPrivateNameSV(node.key), CLASS_ELEMENT_OTHER, node.key.start);
  11076. }
  11077. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  11078. classBody.body.push(this.parseMethod(method, isGenerator, isAsync, isConstructor, allowsDirectSuper, "ClassMethod", true));
  11079. }
  11080. pushClassPrivateMethod(classBody, method, isGenerator, isAsync) {
  11081. const node = this.parseMethod(method, isGenerator, isAsync, false, false, "ClassPrivateMethod", true);
  11082. classBody.body.push(node);
  11083. const kind = node.kind === "get" ? node.static ? CLASS_ELEMENT_STATIC_GETTER : CLASS_ELEMENT_INSTANCE_GETTER : node.kind === "set" ? node.static ? CLASS_ELEMENT_STATIC_SETTER : CLASS_ELEMENT_INSTANCE_SETTER : CLASS_ELEMENT_OTHER;
  11084. this.classScope.declarePrivateName(this.getPrivateNameSV(node.key), kind, node.key.start);
  11085. }
  11086. parsePostMemberNameModifiers(methodOrProp) {}
  11087. parseClassPrivateProperty(node) {
  11088. this.parseInitializer(node);
  11089. this.semicolon();
  11090. return this.finishNode(node, "ClassPrivateProperty");
  11091. }
  11092. parseClassProperty(node) {
  11093. this.parseInitializer(node);
  11094. this.semicolon();
  11095. return this.finishNode(node, "ClassProperty");
  11096. }
  11097. parseInitializer(node) {
  11098. this.scope.enter(SCOPE_CLASS | SCOPE_SUPER);
  11099. this.expressionScope.enter(newExpressionScope());
  11100. this.prodParam.enter(PARAM);
  11101. node.value = this.eat(types$1.eq) ? this.parseMaybeAssignAllowIn() : null;
  11102. this.expressionScope.exit();
  11103. this.prodParam.exit();
  11104. this.scope.exit();
  11105. }
  11106. parseClassId(node, isStatement, optionalId, bindingType = BIND_CLASS) {
  11107. if (this.match(types$1.name)) {
  11108. node.id = this.parseIdentifier();
  11109. if (isStatement) {
  11110. this.checkLVal(node.id, "class name", bindingType);
  11111. }
  11112. } else {
  11113. if (optionalId || !isStatement) {
  11114. node.id = null;
  11115. } else {
  11116. this.unexpected(null, ErrorMessages.MissingClassName);
  11117. }
  11118. }
  11119. }
  11120. parseClassSuper(node) {
  11121. node.superClass = this.eat(types$1._extends) ? this.parseExprSubscripts() : null;
  11122. }
  11123. parseExport(node) {
  11124. const hasDefault = this.maybeParseExportDefaultSpecifier(node);
  11125. const parseAfterDefault = !hasDefault || this.eat(types$1.comma);
  11126. const hasStar = parseAfterDefault && this.eatExportStar(node);
  11127. const hasNamespace = hasStar && this.maybeParseExportNamespaceSpecifier(node);
  11128. const parseAfterNamespace = parseAfterDefault && (!hasNamespace || this.eat(types$1.comma));
  11129. const isFromRequired = hasDefault || hasStar;
  11130. if (hasStar && !hasNamespace) {
  11131. if (hasDefault) this.unexpected();
  11132. this.parseExportFrom(node, true);
  11133. return this.finishNode(node, "ExportAllDeclaration");
  11134. }
  11135. const hasSpecifiers = this.maybeParseExportNamedSpecifiers(node);
  11136. if (hasDefault && parseAfterDefault && !hasStar && !hasSpecifiers || hasNamespace && parseAfterNamespace && !hasSpecifiers) {
  11137. throw this.unexpected(null, types$1.braceL);
  11138. }
  11139. let hasDeclaration;
  11140. if (isFromRequired || hasSpecifiers) {
  11141. hasDeclaration = false;
  11142. this.parseExportFrom(node, isFromRequired);
  11143. } else {
  11144. hasDeclaration = this.maybeParseExportDeclaration(node);
  11145. }
  11146. if (isFromRequired || hasSpecifiers || hasDeclaration) {
  11147. this.checkExport(node, true, false, !!node.source);
  11148. return this.finishNode(node, "ExportNamedDeclaration");
  11149. }
  11150. if (this.eat(types$1._default)) {
  11151. node.declaration = this.parseExportDefaultExpression();
  11152. this.checkExport(node, true, true);
  11153. return this.finishNode(node, "ExportDefaultDeclaration");
  11154. }
  11155. throw this.unexpected(null, types$1.braceL);
  11156. }
  11157. eatExportStar(node) {
  11158. return this.eat(types$1.star);
  11159. }
  11160. maybeParseExportDefaultSpecifier(node) {
  11161. if (this.isExportDefaultSpecifier()) {
  11162. this.expectPlugin("exportDefaultFrom");
  11163. const specifier = this.startNode();
  11164. specifier.exported = this.parseIdentifier(true);
  11165. node.specifiers = [this.finishNode(specifier, "ExportDefaultSpecifier")];
  11166. return true;
  11167. }
  11168. return false;
  11169. }
  11170. maybeParseExportNamespaceSpecifier(node) {
  11171. if (this.isContextual("as")) {
  11172. if (!node.specifiers) node.specifiers = [];
  11173. const specifier = this.startNodeAt(this.state.lastTokStart, this.state.lastTokStartLoc);
  11174. this.next();
  11175. specifier.exported = this.parseModuleExportName();
  11176. node.specifiers.push(this.finishNode(specifier, "ExportNamespaceSpecifier"));
  11177. return true;
  11178. }
  11179. return false;
  11180. }
  11181. maybeParseExportNamedSpecifiers(node) {
  11182. if (this.match(types$1.braceL)) {
  11183. if (!node.specifiers) node.specifiers = [];
  11184. node.specifiers.push(...this.parseExportSpecifiers());
  11185. node.source = null;
  11186. node.declaration = null;
  11187. return true;
  11188. }
  11189. return false;
  11190. }
  11191. maybeParseExportDeclaration(node) {
  11192. if (this.shouldParseExportDeclaration()) {
  11193. node.specifiers = [];
  11194. node.source = null;
  11195. node.declaration = this.parseExportDeclaration(node);
  11196. return true;
  11197. }
  11198. return false;
  11199. }
  11200. isAsyncFunction() {
  11201. if (!this.isContextual("async")) return false;
  11202. const next = this.nextTokenStart();
  11203. return !lineBreak.test(this.input.slice(this.state.pos, next)) && this.isUnparsedContextual(next, "function");
  11204. }
  11205. parseExportDefaultExpression() {
  11206. const expr = this.startNode();
  11207. const isAsync = this.isAsyncFunction();
  11208. if (this.match(types$1._function) || isAsync) {
  11209. this.next();
  11210. if (isAsync) {
  11211. this.next();
  11212. }
  11213. return this.parseFunction(expr, FUNC_STATEMENT | FUNC_NULLABLE_ID, isAsync);
  11214. } else if (this.match(types$1._class)) {
  11215. return this.parseClass(expr, true, true);
  11216. } else if (this.match(types$1.at)) {
  11217. if (this.hasPlugin("decorators") && this.getPluginOption("decorators", "decoratorsBeforeExport")) {
  11218. this.raise(this.state.start, ErrorMessages.DecoratorBeforeExport);
  11219. }
  11220. this.parseDecorators(false);
  11221. return this.parseClass(expr, true, true);
  11222. } else if (this.match(types$1._const) || this.match(types$1._var) || this.isLet()) {
  11223. throw this.raise(this.state.start, ErrorMessages.UnsupportedDefaultExport);
  11224. } else {
  11225. const res = this.parseMaybeAssignAllowIn();
  11226. this.semicolon();
  11227. return res;
  11228. }
  11229. }
  11230. parseExportDeclaration(node) {
  11231. return this.parseStatement(null);
  11232. }
  11233. isExportDefaultSpecifier() {
  11234. if (this.match(types$1.name)) {
  11235. const value = this.state.value;
  11236. if (value === "async" && !this.state.containsEsc || value === "let") {
  11237. return false;
  11238. }
  11239. if ((value === "type" || value === "interface") && !this.state.containsEsc) {
  11240. const l = this.lookahead();
  11241. if (l.type === types$1.name && l.value !== "from" || l.type === types$1.braceL) {
  11242. this.expectOnePlugin(["flow", "typescript"]);
  11243. return false;
  11244. }
  11245. }
  11246. } else if (!this.match(types$1._default)) {
  11247. return false;
  11248. }
  11249. const next = this.nextTokenStart();
  11250. const hasFrom = this.isUnparsedContextual(next, "from");
  11251. if (this.input.charCodeAt(next) === 44 || this.match(types$1.name) && hasFrom) {
  11252. return true;
  11253. }
  11254. if (this.match(types$1._default) && hasFrom) {
  11255. const nextAfterFrom = this.input.charCodeAt(this.nextTokenStartSince(next + 4));
  11256. return nextAfterFrom === 34 || nextAfterFrom === 39;
  11257. }
  11258. return false;
  11259. }
  11260. parseExportFrom(node, expect) {
  11261. if (this.eatContextual("from")) {
  11262. node.source = this.parseImportSource();
  11263. this.checkExport(node);
  11264. const assertions = this.maybeParseImportAssertions();
  11265. if (assertions) {
  11266. node.assertions = assertions;
  11267. }
  11268. } else {
  11269. if (expect) {
  11270. this.unexpected();
  11271. } else {
  11272. node.source = null;
  11273. }
  11274. }
  11275. this.semicolon();
  11276. }
  11277. shouldParseExportDeclaration() {
  11278. if (this.match(types$1.at)) {
  11279. this.expectOnePlugin(["decorators", "decorators-legacy"]);
  11280. if (this.hasPlugin("decorators")) {
  11281. if (this.getPluginOption("decorators", "decoratorsBeforeExport")) {
  11282. this.unexpected(this.state.start, ErrorMessages.DecoratorBeforeExport);
  11283. } else {
  11284. return true;
  11285. }
  11286. }
  11287. }
  11288. return this.state.type.keyword === "var" || this.state.type.keyword === "const" || this.state.type.keyword === "function" || this.state.type.keyword === "class" || this.isLet() || this.isAsyncFunction();
  11289. }
  11290. checkExport(node, checkNames, isDefault, isFrom) {
  11291. if (checkNames) {
  11292. if (isDefault) {
  11293. this.checkDuplicateExports(node, "default");
  11294. if (this.hasPlugin("exportDefaultFrom")) {
  11295. var _declaration$extra;
  11296. const declaration = node.declaration;
  11297. if (declaration.type === "Identifier" && declaration.name === "from" && declaration.end - declaration.start === 4 && !((_declaration$extra = declaration.extra) != null && _declaration$extra.parenthesized)) {
  11298. this.raise(declaration.start, ErrorMessages.ExportDefaultFromAsIdentifier);
  11299. }
  11300. }
  11301. } else if (node.specifiers && node.specifiers.length) {
  11302. for (const specifier of node.specifiers) {
  11303. const {
  11304. exported
  11305. } = specifier;
  11306. const exportedName = exported.type === "Identifier" ? exported.name : exported.value;
  11307. this.checkDuplicateExports(specifier, exportedName);
  11308. if (!isFrom && specifier.local) {
  11309. const {
  11310. local
  11311. } = specifier;
  11312. if (local.type !== "Identifier") {
  11313. this.raise(specifier.start, ErrorMessages.ExportBindingIsString, local.value, exportedName);
  11314. } else {
  11315. this.checkReservedWord(local.name, local.start, true, false);
  11316. this.scope.checkLocalExport(local);
  11317. }
  11318. }
  11319. }
  11320. } else if (node.declaration) {
  11321. if (node.declaration.type === "FunctionDeclaration" || node.declaration.type === "ClassDeclaration") {
  11322. const id = node.declaration.id;
  11323. if (!id) throw new Error("Assertion failure");
  11324. this.checkDuplicateExports(node, id.name);
  11325. } else if (node.declaration.type === "VariableDeclaration") {
  11326. for (const declaration of node.declaration.declarations) {
  11327. this.checkDeclaration(declaration.id);
  11328. }
  11329. }
  11330. }
  11331. }
  11332. const currentContextDecorators = this.state.decoratorStack[this.state.decoratorStack.length - 1];
  11333. if (currentContextDecorators.length) {
  11334. throw this.raise(node.start, ErrorMessages.UnsupportedDecoratorExport);
  11335. }
  11336. }
  11337. checkDeclaration(node) {
  11338. if (node.type === "Identifier") {
  11339. this.checkDuplicateExports(node, node.name);
  11340. } else if (node.type === "ObjectPattern") {
  11341. for (const prop of node.properties) {
  11342. this.checkDeclaration(prop);
  11343. }
  11344. } else if (node.type === "ArrayPattern") {
  11345. for (const elem of node.elements) {
  11346. if (elem) {
  11347. this.checkDeclaration(elem);
  11348. }
  11349. }
  11350. } else if (node.type === "ObjectProperty") {
  11351. this.checkDeclaration(node.value);
  11352. } else if (node.type === "RestElement") {
  11353. this.checkDeclaration(node.argument);
  11354. } else if (node.type === "AssignmentPattern") {
  11355. this.checkDeclaration(node.left);
  11356. }
  11357. }
  11358. checkDuplicateExports(node, name) {
  11359. if (this.exportedIdentifiers.has(name)) {
  11360. this.raise(node.start, name === "default" ? ErrorMessages.DuplicateDefaultExport : ErrorMessages.DuplicateExport, name);
  11361. }
  11362. this.exportedIdentifiers.add(name);
  11363. }
  11364. parseExportSpecifiers() {
  11365. const nodes = [];
  11366. let first = true;
  11367. this.expect(types$1.braceL);
  11368. while (!this.eat(types$1.braceR)) {
  11369. if (first) {
  11370. first = false;
  11371. } else {
  11372. this.expect(types$1.comma);
  11373. if (this.eat(types$1.braceR)) break;
  11374. }
  11375. const node = this.startNode();
  11376. node.local = this.parseModuleExportName();
  11377. node.exported = this.eatContextual("as") ? this.parseModuleExportName() : node.local.__clone();
  11378. nodes.push(this.finishNode(node, "ExportSpecifier"));
  11379. }
  11380. return nodes;
  11381. }
  11382. parseModuleExportName() {
  11383. if (this.match(types$1.string)) {
  11384. const result = this.parseStringLiteral(this.state.value);
  11385. const surrogate = result.value.match(loneSurrogate);
  11386. if (surrogate) {
  11387. this.raise(result.start, ErrorMessages.ModuleExportNameHasLoneSurrogate, surrogate[0].charCodeAt(0).toString(16));
  11388. }
  11389. return result;
  11390. }
  11391. return this.parseIdentifier(true);
  11392. }
  11393. parseImport(node) {
  11394. node.specifiers = [];
  11395. if (!this.match(types$1.string)) {
  11396. const hasDefault = this.maybeParseDefaultImportSpecifier(node);
  11397. const parseNext = !hasDefault || this.eat(types$1.comma);
  11398. const hasStar = parseNext && this.maybeParseStarImportSpecifier(node);
  11399. if (parseNext && !hasStar) this.parseNamedImportSpecifiers(node);
  11400. this.expectContextual("from");
  11401. }
  11402. node.source = this.parseImportSource();
  11403. const assertions = this.maybeParseImportAssertions();
  11404. if (assertions) {
  11405. node.assertions = assertions;
  11406. } else {
  11407. const attributes = this.maybeParseModuleAttributes();
  11408. if (attributes) {
  11409. node.attributes = attributes;
  11410. }
  11411. }
  11412. this.semicolon();
  11413. return this.finishNode(node, "ImportDeclaration");
  11414. }
  11415. parseImportSource() {
  11416. if (!this.match(types$1.string)) this.unexpected();
  11417. return this.parseExprAtom();
  11418. }
  11419. shouldParseDefaultImport(node) {
  11420. return this.match(types$1.name);
  11421. }
  11422. parseImportSpecifierLocal(node, specifier, type, contextDescription) {
  11423. specifier.local = this.parseIdentifier();
  11424. this.checkLVal(specifier.local, contextDescription, BIND_LEXICAL);
  11425. node.specifiers.push(this.finishNode(specifier, type));
  11426. }
  11427. parseAssertEntries() {
  11428. const attrs = [];
  11429. const attrNames = new Set();
  11430. do {
  11431. if (this.match(types$1.braceR)) {
  11432. break;
  11433. }
  11434. const node = this.startNode();
  11435. const keyName = this.state.value;
  11436. if (attrNames.has(keyName)) {
  11437. this.raise(this.state.start, ErrorMessages.ModuleAttributesWithDuplicateKeys, keyName);
  11438. }
  11439. attrNames.add(keyName);
  11440. if (this.match(types$1.string)) {
  11441. node.key = this.parseStringLiteral(keyName);
  11442. } else {
  11443. node.key = this.parseIdentifier(true);
  11444. }
  11445. this.expect(types$1.colon);
  11446. if (!this.match(types$1.string)) {
  11447. throw this.unexpected(this.state.start, ErrorMessages.ModuleAttributeInvalidValue);
  11448. }
  11449. node.value = this.parseStringLiteral(this.state.value);
  11450. this.finishNode(node, "ImportAttribute");
  11451. attrs.push(node);
  11452. } while (this.eat(types$1.comma));
  11453. return attrs;
  11454. }
  11455. maybeParseModuleAttributes() {
  11456. if (this.match(types$1._with) && !this.hasPrecedingLineBreak()) {
  11457. this.expectPlugin("moduleAttributes");
  11458. this.next();
  11459. } else {
  11460. if (this.hasPlugin("moduleAttributes")) return [];
  11461. return null;
  11462. }
  11463. const attrs = [];
  11464. const attributes = new Set();
  11465. do {
  11466. const node = this.startNode();
  11467. node.key = this.parseIdentifier(true);
  11468. if (node.key.name !== "type") {
  11469. this.raise(node.key.start, ErrorMessages.ModuleAttributeDifferentFromType, node.key.name);
  11470. }
  11471. if (attributes.has(node.key.name)) {
  11472. this.raise(node.key.start, ErrorMessages.ModuleAttributesWithDuplicateKeys, node.key.name);
  11473. }
  11474. attributes.add(node.key.name);
  11475. this.expect(types$1.colon);
  11476. if (!this.match(types$1.string)) {
  11477. throw this.unexpected(this.state.start, ErrorMessages.ModuleAttributeInvalidValue);
  11478. }
  11479. node.value = this.parseStringLiteral(this.state.value);
  11480. this.finishNode(node, "ImportAttribute");
  11481. attrs.push(node);
  11482. } while (this.eat(types$1.comma));
  11483. return attrs;
  11484. }
  11485. maybeParseImportAssertions() {
  11486. if (this.isContextual("assert") && !this.hasPrecedingLineBreak()) {
  11487. this.expectPlugin("importAssertions");
  11488. this.next();
  11489. } else {
  11490. if (this.hasPlugin("importAssertions")) return [];
  11491. return null;
  11492. }
  11493. this.eat(types$1.braceL);
  11494. const attrs = this.parseAssertEntries();
  11495. this.eat(types$1.braceR);
  11496. return attrs;
  11497. }
  11498. maybeParseDefaultImportSpecifier(node) {
  11499. if (this.shouldParseDefaultImport(node)) {
  11500. this.parseImportSpecifierLocal(node, this.startNode(), "ImportDefaultSpecifier", "default import specifier");
  11501. return true;
  11502. }
  11503. return false;
  11504. }
  11505. maybeParseStarImportSpecifier(node) {
  11506. if (this.match(types$1.star)) {
  11507. const specifier = this.startNode();
  11508. this.next();
  11509. this.expectContextual("as");
  11510. this.parseImportSpecifierLocal(node, specifier, "ImportNamespaceSpecifier", "import namespace specifier");
  11511. return true;
  11512. }
  11513. return false;
  11514. }
  11515. parseNamedImportSpecifiers(node) {
  11516. let first = true;
  11517. this.expect(types$1.braceL);
  11518. while (!this.eat(types$1.braceR)) {
  11519. if (first) {
  11520. first = false;
  11521. } else {
  11522. if (this.eat(types$1.colon)) {
  11523. throw this.raise(this.state.start, ErrorMessages.DestructureNamedImport);
  11524. }
  11525. this.expect(types$1.comma);
  11526. if (this.eat(types$1.braceR)) break;
  11527. }
  11528. this.parseImportSpecifier(node);
  11529. }
  11530. }
  11531. parseImportSpecifier(node) {
  11532. const specifier = this.startNode();
  11533. const importedIsString = this.match(types$1.string);
  11534. specifier.imported = this.parseModuleExportName();
  11535. if (this.eatContextual("as")) {
  11536. specifier.local = this.parseIdentifier();
  11537. } else {
  11538. const {
  11539. imported
  11540. } = specifier;
  11541. if (importedIsString) {
  11542. throw this.raise(specifier.start, ErrorMessages.ImportBindingIsString, imported.value);
  11543. }
  11544. this.checkReservedWord(imported.name, specifier.start, true, true);
  11545. specifier.local = imported.__clone();
  11546. }
  11547. this.checkLVal(specifier.local, "import specifier", BIND_LEXICAL);
  11548. node.specifiers.push(this.finishNode(specifier, "ImportSpecifier"));
  11549. }
  11550. isThisParam(param) {
  11551. return param.type === "Identifier" && param.name === "this";
  11552. }
  11553. }
  11554. class Parser extends StatementParser {
  11555. constructor(options, input) {
  11556. options = getOptions(options);
  11557. super(options, input);
  11558. this.options = options;
  11559. this.initializeScopes();
  11560. this.plugins = pluginsMap(this.options.plugins);
  11561. this.filename = options.sourceFilename;
  11562. }
  11563. getScopeHandler() {
  11564. return ScopeHandler;
  11565. }
  11566. parse() {
  11567. this.enterInitialScopes();
  11568. const file = this.startNode();
  11569. const program = this.startNode();
  11570. this.nextToken();
  11571. file.errors = null;
  11572. this.parseTopLevel(file, program);
  11573. file.errors = this.state.errors;
  11574. return file;
  11575. }
  11576. }
  11577. function pluginsMap(plugins) {
  11578. const pluginMap = new Map();
  11579. for (const plugin of plugins) {
  11580. const [name, options] = Array.isArray(plugin) ? plugin : [plugin, {}];
  11581. if (!pluginMap.has(name)) pluginMap.set(name, options || {});
  11582. }
  11583. return pluginMap;
  11584. }
  11585. function parse(input, options) {
  11586. var _options;
  11587. if (((_options = options) == null ? void 0 : _options.sourceType) === "unambiguous") {
  11588. options = Object.assign({}, options);
  11589. try {
  11590. options.sourceType = "module";
  11591. const parser = getParser(options, input);
  11592. const ast = parser.parse();
  11593. if (parser.sawUnambiguousESM) {
  11594. return ast;
  11595. }
  11596. if (parser.ambiguousScriptDifferentAst) {
  11597. try {
  11598. options.sourceType = "script";
  11599. return getParser(options, input).parse();
  11600. } catch (_unused) {}
  11601. } else {
  11602. ast.program.sourceType = "script";
  11603. }
  11604. return ast;
  11605. } catch (moduleError) {
  11606. try {
  11607. options.sourceType = "script";
  11608. return getParser(options, input).parse();
  11609. } catch (_unused2) {}
  11610. throw moduleError;
  11611. }
  11612. } else {
  11613. return getParser(options, input).parse();
  11614. }
  11615. }
  11616. function parseExpression(input, options) {
  11617. const parser = getParser(options, input);
  11618. if (parser.options.strictMode) {
  11619. parser.state.strict = true;
  11620. }
  11621. return parser.getExpression();
  11622. }
  11623. function getParser(options, input) {
  11624. let cls = Parser;
  11625. if (options != null && options.plugins) {
  11626. validatePlugins(options.plugins);
  11627. cls = getParserClass(options.plugins);
  11628. }
  11629. return new cls(options, input);
  11630. }
  11631. const parserClassCache = {};
  11632. function getParserClass(pluginsFromOptions) {
  11633. const pluginList = mixinPluginNames.filter(name => hasPlugin(pluginsFromOptions, name));
  11634. const key = pluginList.join("/");
  11635. let cls = parserClassCache[key];
  11636. if (!cls) {
  11637. cls = Parser;
  11638. for (const plugin of pluginList) {
  11639. cls = mixinPlugins[plugin](cls);
  11640. }
  11641. parserClassCache[key] = cls;
  11642. }
  11643. return cls;
  11644. }
  11645. exports.parse = parse;
  11646. exports.parseExpression = parseExpression;
  11647. exports.tokTypes = types$1;
  11648. //# sourceMappingURL=index.js.map