index.js 423 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340
  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. braceBarR: new TokenType("|}"),
  99. parenL: new TokenType("(", {
  100. beforeExpr,
  101. startsExpr
  102. }),
  103. parenR: new TokenType(")"),
  104. comma: new TokenType(",", {
  105. beforeExpr
  106. }),
  107. semi: new TokenType(";", {
  108. beforeExpr
  109. }),
  110. colon: new TokenType(":", {
  111. beforeExpr
  112. }),
  113. doubleColon: new TokenType("::", {
  114. beforeExpr
  115. }),
  116. dot: new TokenType("."),
  117. question: new TokenType("?", {
  118. beforeExpr
  119. }),
  120. questionDot: new TokenType("?."),
  121. arrow: new TokenType("=>", {
  122. beforeExpr
  123. }),
  124. template: new TokenType("template"),
  125. ellipsis: new TokenType("...", {
  126. beforeExpr
  127. }),
  128. backQuote: new TokenType("`", {
  129. startsExpr
  130. }),
  131. dollarBraceL: new TokenType("${", {
  132. beforeExpr,
  133. startsExpr
  134. }),
  135. at: new TokenType("@"),
  136. hash: new TokenType("#", {
  137. startsExpr
  138. }),
  139. interpreterDirective: new TokenType("#!..."),
  140. eq: new TokenType("=", {
  141. beforeExpr,
  142. isAssign
  143. }),
  144. assign: new TokenType("_=", {
  145. beforeExpr,
  146. isAssign
  147. }),
  148. incDec: new TokenType("++/--", {
  149. prefix,
  150. postfix,
  151. startsExpr
  152. }),
  153. bang: new TokenType("!", {
  154. beforeExpr,
  155. prefix,
  156. startsExpr
  157. }),
  158. tilde: new TokenType("~", {
  159. beforeExpr,
  160. prefix,
  161. startsExpr
  162. }),
  163. pipeline: createBinop("|>", 0),
  164. nullishCoalescing: createBinop("??", 1),
  165. logicalOR: createBinop("||", 1),
  166. logicalAND: createBinop("&&", 2),
  167. bitwiseOR: createBinop("|", 3),
  168. bitwiseXOR: createBinop("^", 4),
  169. bitwiseAND: createBinop("&", 5),
  170. equality: createBinop("==/!=/===/!==", 6),
  171. relational: createBinop("</>/<=/>=", 7),
  172. bitShift: createBinop("<</>>/>>>", 8),
  173. plusMin: new TokenType("+/-", {
  174. beforeExpr,
  175. binop: 9,
  176. prefix,
  177. startsExpr
  178. }),
  179. modulo: new TokenType("%", {
  180. beforeExpr,
  181. binop: 10,
  182. startsExpr
  183. }),
  184. star: new TokenType("*", {
  185. binop: 10
  186. }),
  187. slash: createBinop("/", 10),
  188. exponent: new TokenType("**", {
  189. beforeExpr,
  190. binop: 11,
  191. rightAssociative: true
  192. }),
  193. _break: createKeyword("break"),
  194. _case: createKeyword("case", {
  195. beforeExpr
  196. }),
  197. _catch: createKeyword("catch"),
  198. _continue: createKeyword("continue"),
  199. _debugger: createKeyword("debugger"),
  200. _default: createKeyword("default", {
  201. beforeExpr
  202. }),
  203. _do: createKeyword("do", {
  204. isLoop,
  205. beforeExpr
  206. }),
  207. _else: createKeyword("else", {
  208. beforeExpr
  209. }),
  210. _finally: createKeyword("finally"),
  211. _for: createKeyword("for", {
  212. isLoop
  213. }),
  214. _function: createKeyword("function", {
  215. startsExpr
  216. }),
  217. _if: createKeyword("if"),
  218. _return: createKeyword("return", {
  219. beforeExpr
  220. }),
  221. _switch: createKeyword("switch"),
  222. _throw: createKeyword("throw", {
  223. beforeExpr,
  224. prefix,
  225. startsExpr
  226. }),
  227. _try: createKeyword("try"),
  228. _var: createKeyword("var"),
  229. _const: createKeyword("const"),
  230. _while: createKeyword("while", {
  231. isLoop
  232. }),
  233. _with: createKeyword("with"),
  234. _new: createKeyword("new", {
  235. beforeExpr,
  236. startsExpr
  237. }),
  238. _this: createKeyword("this", {
  239. startsExpr
  240. }),
  241. _super: createKeyword("super", {
  242. startsExpr
  243. }),
  244. _class: createKeyword("class", {
  245. startsExpr
  246. }),
  247. _extends: createKeyword("extends", {
  248. beforeExpr
  249. }),
  250. _export: createKeyword("export"),
  251. _import: createKeyword("import", {
  252. startsExpr
  253. }),
  254. _null: createKeyword("null", {
  255. startsExpr
  256. }),
  257. _true: createKeyword("true", {
  258. startsExpr
  259. }),
  260. _false: createKeyword("false", {
  261. startsExpr
  262. }),
  263. _in: createKeyword("in", {
  264. beforeExpr,
  265. binop: 7
  266. }),
  267. _instanceof: createKeyword("instanceof", {
  268. beforeExpr,
  269. binop: 7
  270. }),
  271. _typeof: createKeyword("typeof", {
  272. beforeExpr,
  273. prefix,
  274. startsExpr
  275. }),
  276. _void: createKeyword("void", {
  277. beforeExpr,
  278. prefix,
  279. startsExpr
  280. }),
  281. _delete: createKeyword("delete", {
  282. beforeExpr,
  283. prefix,
  284. startsExpr
  285. })
  286. };
  287. const lineBreak = /\r\n?|[\n\u2028\u2029]/;
  288. const lineBreakG = new RegExp(lineBreak.source, "g");
  289. function isNewLine(code) {
  290. switch (code) {
  291. case 10:
  292. case 13:
  293. case 8232:
  294. case 8233:
  295. return true;
  296. default:
  297. return false;
  298. }
  299. }
  300. const skipWhiteSpace = /(?:\s|\/\/.*|\/\*[^]*?\*\/)*/g;
  301. function isWhitespace(code) {
  302. switch (code) {
  303. case 0x0009:
  304. case 0x000b:
  305. case 0x000c:
  306. case 32:
  307. case 160:
  308. case 5760:
  309. case 0x2000:
  310. case 0x2001:
  311. case 0x2002:
  312. case 0x2003:
  313. case 0x2004:
  314. case 0x2005:
  315. case 0x2006:
  316. case 0x2007:
  317. case 0x2008:
  318. case 0x2009:
  319. case 0x200a:
  320. case 0x202f:
  321. case 0x205f:
  322. case 0x3000:
  323. case 0xfeff:
  324. return true;
  325. default:
  326. return false;
  327. }
  328. }
  329. class Position {
  330. constructor(line, col) {
  331. this.line = void 0;
  332. this.column = void 0;
  333. this.line = line;
  334. this.column = col;
  335. }
  336. }
  337. class SourceLocation {
  338. constructor(start, end) {
  339. this.start = void 0;
  340. this.end = void 0;
  341. this.filename = void 0;
  342. this.identifierName = void 0;
  343. this.start = start;
  344. this.end = end;
  345. }
  346. }
  347. function getLineInfo(input, offset) {
  348. let line = 1;
  349. let lineStart = 0;
  350. let match;
  351. lineBreakG.lastIndex = 0;
  352. while ((match = lineBreakG.exec(input)) && match.index < offset) {
  353. line++;
  354. lineStart = lineBreakG.lastIndex;
  355. }
  356. return new Position(line, offset - lineStart);
  357. }
  358. class BaseParser {
  359. constructor() {
  360. this.sawUnambiguousESM = false;
  361. this.ambiguousScriptDifferentAst = false;
  362. }
  363. hasPlugin(name) {
  364. return this.plugins.has(name);
  365. }
  366. getPluginOption(plugin, name) {
  367. if (this.hasPlugin(plugin)) return this.plugins.get(plugin)[name];
  368. }
  369. }
  370. function last(stack) {
  371. return stack[stack.length - 1];
  372. }
  373. class CommentsParser extends BaseParser {
  374. addComment(comment) {
  375. if (this.filename) comment.loc.filename = this.filename;
  376. this.state.trailingComments.push(comment);
  377. this.state.leadingComments.push(comment);
  378. }
  379. adjustCommentsAfterTrailingComma(node, elements, takeAllComments) {
  380. if (this.state.leadingComments.length === 0) {
  381. return;
  382. }
  383. let lastElement = null;
  384. let i = elements.length;
  385. while (lastElement === null && i > 0) {
  386. lastElement = elements[--i];
  387. }
  388. if (lastElement === null) {
  389. return;
  390. }
  391. for (let j = 0; j < this.state.leadingComments.length; j++) {
  392. if (this.state.leadingComments[j].end < this.state.commentPreviousNode.end) {
  393. this.state.leadingComments.splice(j, 1);
  394. j--;
  395. }
  396. }
  397. const newTrailingComments = [];
  398. for (let i = 0; i < this.state.leadingComments.length; i++) {
  399. const leadingComment = this.state.leadingComments[i];
  400. if (leadingComment.end < node.end) {
  401. newTrailingComments.push(leadingComment);
  402. if (!takeAllComments) {
  403. this.state.leadingComments.splice(i, 1);
  404. i--;
  405. }
  406. } else {
  407. if (node.trailingComments === undefined) {
  408. node.trailingComments = [];
  409. }
  410. node.trailingComments.push(leadingComment);
  411. }
  412. }
  413. if (takeAllComments) this.state.leadingComments = [];
  414. if (newTrailingComments.length > 0) {
  415. lastElement.trailingComments = newTrailingComments;
  416. } else if (lastElement.trailingComments !== undefined) {
  417. lastElement.trailingComments = [];
  418. }
  419. }
  420. processComment(node) {
  421. if (node.type === "Program" && node.body.length > 0) return;
  422. const stack = this.state.commentStack;
  423. let firstChild, lastChild, trailingComments, i, j;
  424. if (this.state.trailingComments.length > 0) {
  425. if (this.state.trailingComments[0].start >= node.end) {
  426. trailingComments = this.state.trailingComments;
  427. this.state.trailingComments = [];
  428. } else {
  429. this.state.trailingComments.length = 0;
  430. }
  431. } else if (stack.length > 0) {
  432. const lastInStack = last(stack);
  433. if (lastInStack.trailingComments && lastInStack.trailingComments[0].start >= node.end) {
  434. trailingComments = lastInStack.trailingComments;
  435. delete lastInStack.trailingComments;
  436. }
  437. }
  438. if (stack.length > 0 && last(stack).start >= node.start) {
  439. firstChild = stack.pop();
  440. }
  441. while (stack.length > 0 && last(stack).start >= node.start) {
  442. lastChild = stack.pop();
  443. }
  444. if (!lastChild && firstChild) lastChild = firstChild;
  445. if (firstChild) {
  446. switch (node.type) {
  447. case "ObjectExpression":
  448. this.adjustCommentsAfterTrailingComma(node, node.properties);
  449. break;
  450. case "ObjectPattern":
  451. this.adjustCommentsAfterTrailingComma(node, node.properties, true);
  452. break;
  453. case "CallExpression":
  454. this.adjustCommentsAfterTrailingComma(node, node.arguments);
  455. break;
  456. case "ArrayExpression":
  457. this.adjustCommentsAfterTrailingComma(node, node.elements);
  458. break;
  459. case "ArrayPattern":
  460. this.adjustCommentsAfterTrailingComma(node, node.elements, true);
  461. break;
  462. }
  463. } else if (this.state.commentPreviousNode && (this.state.commentPreviousNode.type === "ImportSpecifier" && node.type !== "ImportSpecifier" || this.state.commentPreviousNode.type === "ExportSpecifier" && node.type !== "ExportSpecifier")) {
  464. this.adjustCommentsAfterTrailingComma(node, [this.state.commentPreviousNode]);
  465. }
  466. if (lastChild) {
  467. if (lastChild.leadingComments) {
  468. if (lastChild !== node && lastChild.leadingComments.length > 0 && last(lastChild.leadingComments).end <= node.start) {
  469. node.leadingComments = lastChild.leadingComments;
  470. delete lastChild.leadingComments;
  471. } else {
  472. for (i = lastChild.leadingComments.length - 2; i >= 0; --i) {
  473. if (lastChild.leadingComments[i].end <= node.start) {
  474. node.leadingComments = lastChild.leadingComments.splice(0, i + 1);
  475. break;
  476. }
  477. }
  478. }
  479. }
  480. } else if (this.state.leadingComments.length > 0) {
  481. if (last(this.state.leadingComments).end <= node.start) {
  482. if (this.state.commentPreviousNode) {
  483. for (j = 0; j < this.state.leadingComments.length; j++) {
  484. if (this.state.leadingComments[j].end < this.state.commentPreviousNode.end) {
  485. this.state.leadingComments.splice(j, 1);
  486. j--;
  487. }
  488. }
  489. }
  490. if (this.state.leadingComments.length > 0) {
  491. node.leadingComments = this.state.leadingComments;
  492. this.state.leadingComments = [];
  493. }
  494. } else {
  495. for (i = 0; i < this.state.leadingComments.length; i++) {
  496. if (this.state.leadingComments[i].end > node.start) {
  497. break;
  498. }
  499. }
  500. const leadingComments = this.state.leadingComments.slice(0, i);
  501. if (leadingComments.length) {
  502. node.leadingComments = leadingComments;
  503. }
  504. trailingComments = this.state.leadingComments.slice(i);
  505. if (trailingComments.length === 0) {
  506. trailingComments = null;
  507. }
  508. }
  509. }
  510. this.state.commentPreviousNode = node;
  511. if (trailingComments) {
  512. if (trailingComments.length && trailingComments[0].start >= node.start && last(trailingComments).end <= node.end) {
  513. node.innerComments = trailingComments;
  514. } else {
  515. const firstTrailingCommentIndex = trailingComments.findIndex(comment => comment.end >= node.end);
  516. if (firstTrailingCommentIndex > 0) {
  517. node.innerComments = trailingComments.slice(0, firstTrailingCommentIndex);
  518. node.trailingComments = trailingComments.slice(firstTrailingCommentIndex);
  519. } else {
  520. node.trailingComments = trailingComments;
  521. }
  522. }
  523. }
  524. stack.push(node);
  525. }
  526. }
  527. const ErrorCodes = Object.freeze({
  528. SyntaxError: "BABEL_PARSER_SYNTAX_ERROR",
  529. SourceTypeModuleError: "BABEL_PARSER_SOURCETYPE_MODULE_REQUIRED"
  530. });
  531. const ErrorMessages = makeErrorTemplates({
  532. AccessorIsGenerator: "A %0ter cannot be a generator.",
  533. ArgumentsInClass: "'arguments' is only allowed in functions and class methods.",
  534. AsyncFunctionInSingleStatementContext: "Async functions can only be declared at the top level or inside a block.",
  535. AwaitBindingIdentifier: "Can not use 'await' as identifier inside an async function.",
  536. AwaitBindingIdentifierInStaticBlock: "Can not use 'await' as identifier inside a static block.",
  537. AwaitExpressionFormalParameter: "'await' is not allowed in async function parameters.",
  538. AwaitNotInAsyncContext: "'await' is only allowed within async functions and at the top levels of modules.",
  539. AwaitNotInAsyncFunction: "'await' is only allowed within async functions.",
  540. BadGetterArity: "A 'get' accesor must not have any formal parameters.",
  541. BadSetterArity: "A 'set' accesor must have exactly one formal parameter.",
  542. BadSetterRestParameter: "A 'set' accesor function argument must not be a rest parameter.",
  543. ConstructorClassField: "Classes may not have a field named 'constructor'.",
  544. ConstructorClassPrivateField: "Classes may not have a private field named '#constructor'.",
  545. ConstructorIsAccessor: "Class constructor may not be an accessor.",
  546. ConstructorIsAsync: "Constructor can't be an async function.",
  547. ConstructorIsGenerator: "Constructor can't be a generator.",
  548. DeclarationMissingInitializer: "'%0' require an initialization value.",
  549. DecoratorBeforeExport: "Decorators must be placed *before* the 'export' keyword. You can set the 'decoratorsBeforeExport' option to false to use the 'export @decorator class {}' syntax.",
  550. DecoratorConstructor: "Decorators can't be used with a constructor. Did you mean '@dec class { ... }'?",
  551. DecoratorExportClass: "Using the export keyword between a decorator and a class is not allowed. Please use `export @dec class` instead.",
  552. DecoratorSemicolon: "Decorators must not be followed by a semicolon.",
  553. DecoratorStaticBlock: "Decorators can't be used with a static block.",
  554. DeletePrivateField: "Deleting a private field is not allowed.",
  555. DestructureNamedImport: "ES2015 named imports do not destructure. Use another statement for destructuring after the import.",
  556. DuplicateConstructor: "Duplicate constructor in the same class.",
  557. DuplicateDefaultExport: "Only one default export allowed per module.",
  558. DuplicateExport: "`%0` has already been exported. Exported identifiers must be unique.",
  559. DuplicateProto: "Redefinition of __proto__ property.",
  560. DuplicateRegExpFlags: "Duplicate regular expression flag.",
  561. ElementAfterRest: "Rest element must be last element.",
  562. EscapedCharNotAnIdentifier: "Invalid Unicode escape.",
  563. ExportBindingIsString: "A string literal cannot be used as an exported binding without `from`.\n- Did you mean `export { '%0' as '%1' } from 'some-module'`?",
  564. ExportDefaultFromAsIdentifier: "'from' is not allowed as an identifier after 'export default'.",
  565. ForInOfLoopInitializer: "'%0' loop variable declaration may not have an initializer.",
  566. ForOfAsync: "The left-hand side of a for-of loop may not be 'async'.",
  567. ForOfLet: "The left-hand side of a for-of loop may not start with 'let'.",
  568. GeneratorInSingleStatementContext: "Generators can only be declared at the top level or inside a block.",
  569. IllegalBreakContinue: "Unsyntactic %0.",
  570. IllegalLanguageModeDirective: "Illegal 'use strict' directive in function with non-simple parameter list.",
  571. IllegalReturn: "'return' outside of function.",
  572. ImportBindingIsString: 'A string literal cannot be used as an imported binding.\n- Did you mean `import { "%0" as foo }`?',
  573. ImportCallArgumentTrailingComma: "Trailing comma is disallowed inside import(...) arguments.",
  574. ImportCallArity: "`import()` requires exactly %0.",
  575. ImportCallNotNewExpression: "Cannot use new with import(...).",
  576. ImportCallSpreadArgument: "`...` is not allowed in `import()`.",
  577. InvalidBigIntLiteral: "Invalid BigIntLiteral.",
  578. InvalidCodePoint: "Code point out of bounds.",
  579. InvalidDecimal: "Invalid decimal.",
  580. InvalidDigit: "Expected number in radix %0.",
  581. InvalidEscapeSequence: "Bad character escape sequence.",
  582. InvalidEscapeSequenceTemplate: "Invalid escape sequence in template.",
  583. InvalidEscapedReservedWord: "Escape sequence in keyword %0.",
  584. InvalidIdentifier: "Invalid identifier %0.",
  585. InvalidLhs: "Invalid left-hand side in %0.",
  586. InvalidLhsBinding: "Binding invalid left-hand side in %0.",
  587. InvalidNumber: "Invalid number.",
  588. InvalidOrMissingExponent: "Floating-point numbers require a valid exponent after the 'e'.",
  589. InvalidOrUnexpectedToken: "Unexpected character '%0'.",
  590. InvalidParenthesizedAssignment: "Invalid parenthesized assignment pattern.",
  591. InvalidPrivateFieldResolution: "Private name #%0 is not defined.",
  592. InvalidPropertyBindingPattern: "Binding member expression.",
  593. InvalidRecordProperty: "Only properties and spread elements are allowed in record definitions.",
  594. InvalidRestAssignmentPattern: "Invalid rest operator's argument.",
  595. LabelRedeclaration: "Label '%0' is already declared.",
  596. LetInLexicalBinding: "'let' is not allowed to be used as a name in 'let' or 'const' declarations.",
  597. LineTerminatorBeforeArrow: "No line break is allowed before '=>'.",
  598. MalformedRegExpFlags: "Invalid regular expression flag.",
  599. MissingClassName: "A class name is required.",
  600. MissingEqInAssignment: "Only '=' operator can be used for specifying default value.",
  601. MissingSemicolon: "Missing semicolon.",
  602. MissingUnicodeEscape: "Expecting Unicode escape sequence \\uXXXX.",
  603. MixingCoalesceWithLogical: "Nullish coalescing operator(??) requires parens when mixing with logical operators.",
  604. ModuleAttributeDifferentFromType: "The only accepted module attribute is `type`.",
  605. ModuleAttributeInvalidValue: "Only string literals are allowed as module attribute values.",
  606. ModuleAttributesWithDuplicateKeys: 'Duplicate key "%0" is not allowed in module attributes.',
  607. ModuleExportNameHasLoneSurrogate: "An export name cannot include a lone surrogate, found '\\u%0'.",
  608. ModuleExportUndefined: "Export '%0' is not defined.",
  609. MultipleDefaultsInSwitch: "Multiple default clauses.",
  610. NewlineAfterThrow: "Illegal newline after throw.",
  611. NoCatchOrFinally: "Missing catch or finally clause.",
  612. NumberIdentifier: "Identifier directly after number.",
  613. NumericSeparatorInEscapeSequence: "Numeric separators are not allowed inside unicode escape sequences or hex escape sequences.",
  614. ObsoleteAwaitStar: "'await*' has been removed from the async functions proposal. Use Promise.all() instead.",
  615. OptionalChainingNoNew: "Constructors in/after an Optional Chain are not allowed.",
  616. OptionalChainingNoTemplate: "Tagged Template Literals are not allowed in optionalChain.",
  617. OverrideOnConstructor: "'override' modifier cannot appear on a constructor declaration.",
  618. ParamDupe: "Argument name clash.",
  619. PatternHasAccessor: "Object pattern can't contain getter or setter.",
  620. PatternHasMethod: "Object pattern can't contain methods.",
  621. PipelineBodyNoArrow: 'Unexpected arrow "=>" after pipeline body; arrow function in pipeline body must be parenthesized.',
  622. PipelineBodySequenceExpression: "Pipeline body may not be a comma-separated sequence expression.",
  623. PipelineHeadSequenceExpression: "Pipeline head should not be a comma-separated sequence expression.",
  624. PipelineTopicUnused: "Pipeline is in topic style but does not use topic reference.",
  625. PrimaryTopicNotAllowed: "Topic reference was used in a lexical context without topic binding.",
  626. PrimaryTopicRequiresSmartPipeline: "Primary Topic Reference found but pipelineOperator not passed 'smart' for 'proposal' option.",
  627. PrivateInExpectedIn: "Private names are only allowed in property accesses (`obj.#%0`) or in `in` expressions (`#%0 in obj`).",
  628. PrivateNameRedeclaration: "Duplicate private name #%0.",
  629. RecordExpressionBarIncorrectEndSyntaxType: "Record expressions ending with '|}' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'bar'.",
  630. RecordExpressionBarIncorrectStartSyntaxType: "Record expressions starting with '{|' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'bar'.",
  631. RecordExpressionHashIncorrectStartSyntaxType: "Record expressions starting with '#{' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'hash'.",
  632. RecordNoProto: "'__proto__' is not allowed in Record expressions.",
  633. RestTrailingComma: "Unexpected trailing comma after rest element.",
  634. 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.",
  635. StaticPrototype: "Classes may not have static property named prototype.",
  636. StrictDelete: "Deleting local variable in strict mode.",
  637. StrictEvalArguments: "Assigning to '%0' in strict mode.",
  638. StrictEvalArgumentsBinding: "Binding '%0' in strict mode.",
  639. StrictFunction: "In strict mode code, functions can only be declared at top level or inside a block.",
  640. StrictNumericEscape: "The only valid numeric escape in strict mode is '\\0'.",
  641. StrictOctalLiteral: "Legacy octal literals are not allowed in strict mode.",
  642. StrictWith: "'with' in strict mode.",
  643. 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?",
  644. SuperPrivateField: "Private fields can't be accessed on super.",
  645. TrailingDecorator: "Decorators must be attached to a class element.",
  646. TupleExpressionBarIncorrectEndSyntaxType: "Tuple expressions ending with '|]' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'bar'.",
  647. TupleExpressionBarIncorrectStartSyntaxType: "Tuple expressions starting with '[|' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'bar'.",
  648. TupleExpressionHashIncorrectStartSyntaxType: "Tuple expressions starting with '#[' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'hash'.",
  649. UnexpectedArgumentPlaceholder: "Unexpected argument placeholder.",
  650. UnexpectedAwaitAfterPipelineBody: 'Unexpected "await" after pipeline body; await must have parentheses in minimal proposal.',
  651. UnexpectedDigitAfterHash: "Unexpected digit after hash token.",
  652. UnexpectedImportExport: "'import' and 'export' may only appear at the top level.",
  653. UnexpectedKeyword: "Unexpected keyword '%0'.",
  654. UnexpectedLeadingDecorator: "Leading decorators must be attached to a class declaration.",
  655. UnexpectedLexicalDeclaration: "Lexical declaration cannot appear in a single-statement context.",
  656. UnexpectedNewTarget: "`new.target` can only be used in functions or class properties.",
  657. UnexpectedNumericSeparator: "A numeric separator is only allowed between two digits.",
  658. 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).",
  659. UnexpectedReservedWord: "Unexpected reserved word '%0'.",
  660. UnexpectedSuper: "'super' is only allowed in object methods and classes.",
  661. UnexpectedToken: "Unexpected token '%0'.",
  662. UnexpectedTokenUnaryExponentiation: "Illegal expression. Wrap left hand side or entire exponentiation in parentheses.",
  663. UnsupportedBind: "Binding should be performed on object property.",
  664. UnsupportedDecoratorExport: "A decorated export must export a class declaration.",
  665. UnsupportedDefaultExport: "Only expressions, functions or classes are allowed as the `default` export.",
  666. UnsupportedImport: "`import` can only be used in `import()` or `import.meta`.",
  667. UnsupportedMetaProperty: "The only valid meta property for %0 is %0.%1.",
  668. UnsupportedParameterDecorator: "Decorators cannot be used to decorate parameters.",
  669. UnsupportedPropertyDecorator: "Decorators cannot be used to decorate object literal properties.",
  670. UnsupportedSuper: "'super' can only be used with function calls (i.e. super()) or in property accesses (i.e. super.prop or super[prop]).",
  671. UnterminatedComment: "Unterminated comment.",
  672. UnterminatedRegExp: "Unterminated regular expression.",
  673. UnterminatedString: "Unterminated string constant.",
  674. UnterminatedTemplate: "Unterminated template.",
  675. VarRedeclaration: "Identifier '%0' has already been declared.",
  676. YieldBindingIdentifier: "Can not use 'yield' as identifier inside a generator.",
  677. YieldInParameter: "Yield expression is not allowed in formal parameters.",
  678. ZeroDigitNumericSeparator: "Numeric separator can not be used after leading 0."
  679. }, ErrorCodes.SyntaxError);
  680. const SourceTypeModuleErrorMessages = makeErrorTemplates({
  681. ImportMetaOutsideModule: `import.meta may appear only with 'sourceType: "module"'`,
  682. ImportOutsideModule: `'import' and 'export' may appear only with 'sourceType: "module"'`
  683. }, ErrorCodes.SourceTypeModuleError);
  684. function makeErrorTemplates(messages, code) {
  685. const templates = {};
  686. Object.keys(messages).forEach(reasonCode => {
  687. templates[reasonCode] = Object.freeze({
  688. code,
  689. reasonCode,
  690. template: messages[reasonCode]
  691. });
  692. });
  693. return Object.freeze(templates);
  694. }
  695. class ParserError extends CommentsParser {
  696. getLocationForPosition(pos) {
  697. let loc;
  698. 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);
  699. return loc;
  700. }
  701. raise(pos, {
  702. code,
  703. reasonCode,
  704. template
  705. }, ...params) {
  706. return this.raiseWithData(pos, {
  707. code,
  708. reasonCode
  709. }, template, ...params);
  710. }
  711. raiseOverwrite(pos, {
  712. code,
  713. template
  714. }, ...params) {
  715. const loc = this.getLocationForPosition(pos);
  716. const message = template.replace(/%(\d+)/g, (_, i) => params[i]) + ` (${loc.line}:${loc.column})`;
  717. if (this.options.errorRecovery) {
  718. const errors = this.state.errors;
  719. for (let i = errors.length - 1; i >= 0; i--) {
  720. const error = errors[i];
  721. if (error.pos === pos) {
  722. return Object.assign(error, {
  723. message
  724. });
  725. } else if (error.pos < pos) {
  726. break;
  727. }
  728. }
  729. }
  730. return this._raise({
  731. code,
  732. loc,
  733. pos
  734. }, message);
  735. }
  736. raiseWithData(pos, data, errorTemplate, ...params) {
  737. const loc = this.getLocationForPosition(pos);
  738. const message = errorTemplate.replace(/%(\d+)/g, (_, i) => params[i]) + ` (${loc.line}:${loc.column})`;
  739. return this._raise(Object.assign({
  740. loc,
  741. pos
  742. }, data), message);
  743. }
  744. _raise(errorContext, message) {
  745. const err = new SyntaxError(message);
  746. Object.assign(err, errorContext);
  747. if (this.options.errorRecovery) {
  748. if (!this.isLookahead) this.state.errors.push(err);
  749. return err;
  750. } else {
  751. throw err;
  752. }
  753. }
  754. }
  755. var estree = (superClass => class extends superClass {
  756. parseRegExpLiteral({
  757. pattern,
  758. flags
  759. }) {
  760. let regex = null;
  761. try {
  762. regex = new RegExp(pattern, flags);
  763. } catch (e) {}
  764. const node = this.estreeParseLiteral(regex);
  765. node.regex = {
  766. pattern,
  767. flags
  768. };
  769. return node;
  770. }
  771. parseBigIntLiteral(value) {
  772. let bigInt;
  773. try {
  774. bigInt = BigInt(value);
  775. } catch (_unused) {
  776. bigInt = null;
  777. }
  778. const node = this.estreeParseLiteral(bigInt);
  779. node.bigint = String(node.value || value);
  780. return node;
  781. }
  782. parseDecimalLiteral(value) {
  783. const decimal = null;
  784. const node = this.estreeParseLiteral(decimal);
  785. node.decimal = String(node.value || value);
  786. return node;
  787. }
  788. estreeParseLiteral(value) {
  789. return this.parseLiteral(value, "Literal");
  790. }
  791. parseStringLiteral(value) {
  792. return this.estreeParseLiteral(value);
  793. }
  794. parseNumericLiteral(value) {
  795. return this.estreeParseLiteral(value);
  796. }
  797. parseNullLiteral() {
  798. return this.estreeParseLiteral(null);
  799. }
  800. parseBooleanLiteral(value) {
  801. return this.estreeParseLiteral(value);
  802. }
  803. directiveToStmt(directive) {
  804. const directiveLiteral = directive.value;
  805. const stmt = this.startNodeAt(directive.start, directive.loc.start);
  806. const expression = this.startNodeAt(directiveLiteral.start, directiveLiteral.loc.start);
  807. expression.value = directiveLiteral.extra.expressionValue;
  808. expression.raw = directiveLiteral.extra.raw;
  809. stmt.expression = this.finishNodeAt(expression, "Literal", directiveLiteral.end, directiveLiteral.loc.end);
  810. stmt.directive = directiveLiteral.extra.raw.slice(1, -1);
  811. return this.finishNodeAt(stmt, "ExpressionStatement", directive.end, directive.loc.end);
  812. }
  813. initFunction(node, isAsync) {
  814. super.initFunction(node, isAsync);
  815. node.expression = false;
  816. }
  817. checkDeclaration(node) {
  818. if (node != null && this.isObjectProperty(node)) {
  819. this.checkDeclaration(node.value);
  820. } else {
  821. super.checkDeclaration(node);
  822. }
  823. }
  824. getObjectOrClassMethodParams(method) {
  825. return method.value.params;
  826. }
  827. isValidDirective(stmt) {
  828. var _stmt$expression$extr;
  829. return stmt.type === "ExpressionStatement" && stmt.expression.type === "Literal" && typeof stmt.expression.value === "string" && !((_stmt$expression$extr = stmt.expression.extra) != null && _stmt$expression$extr.parenthesized);
  830. }
  831. stmtToDirective(stmt) {
  832. const directive = super.stmtToDirective(stmt);
  833. const value = stmt.expression.value;
  834. this.addExtra(directive.value, "expressionValue", value);
  835. return directive;
  836. }
  837. parseBlockBody(node, ...args) {
  838. super.parseBlockBody(node, ...args);
  839. const directiveStatements = node.directives.map(d => this.directiveToStmt(d));
  840. node.body = directiveStatements.concat(node.body);
  841. delete node.directives;
  842. }
  843. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  844. this.parseMethod(method, isGenerator, isAsync, isConstructor, allowsDirectSuper, "ClassMethod", true);
  845. if (method.typeParameters) {
  846. method.value.typeParameters = method.typeParameters;
  847. delete method.typeParameters;
  848. }
  849. classBody.body.push(method);
  850. }
  851. parseMaybePrivateName(...args) {
  852. const node = super.parseMaybePrivateName(...args);
  853. if (node.type === "PrivateName" && this.getPluginOption("estree", "classFeatures")) {
  854. return this.convertPrivateNameToPrivateIdentifier(node);
  855. }
  856. return node;
  857. }
  858. convertPrivateNameToPrivateIdentifier(node) {
  859. const name = super.getPrivateNameSV(node);
  860. node = node;
  861. delete node.id;
  862. node.name = name;
  863. node.type = "PrivateIdentifier";
  864. return node;
  865. }
  866. isPrivateName(node) {
  867. if (!this.getPluginOption("estree", "classFeatures")) {
  868. return super.isPrivateName(node);
  869. }
  870. return node.type === "PrivateIdentifier";
  871. }
  872. getPrivateNameSV(node) {
  873. if (!this.getPluginOption("estree", "classFeatures")) {
  874. return super.getPrivateNameSV(node);
  875. }
  876. return node.name;
  877. }
  878. parseLiteral(value, type) {
  879. const node = super.parseLiteral(value, type);
  880. node.raw = node.extra.raw;
  881. delete node.extra;
  882. return node;
  883. }
  884. parseFunctionBody(node, allowExpression, isMethod = false) {
  885. super.parseFunctionBody(node, allowExpression, isMethod);
  886. node.expression = node.body.type !== "BlockStatement";
  887. }
  888. parseMethod(node, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope = false) {
  889. let funcNode = this.startNode();
  890. funcNode.kind = node.kind;
  891. funcNode = super.parseMethod(funcNode, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope);
  892. funcNode.type = "FunctionExpression";
  893. delete funcNode.kind;
  894. node.value = funcNode;
  895. if (type === "ClassPrivateMethod") {
  896. node.computed = false;
  897. }
  898. type = "MethodDefinition";
  899. return this.finishNode(node, type);
  900. }
  901. parseClassProperty(...args) {
  902. const propertyNode = super.parseClassProperty(...args);
  903. if (this.getPluginOption("estree", "classFeatures")) {
  904. propertyNode.type = "PropertyDefinition";
  905. }
  906. return propertyNode;
  907. }
  908. parseClassPrivateProperty(...args) {
  909. const propertyNode = super.parseClassPrivateProperty(...args);
  910. if (this.getPluginOption("estree", "classFeatures")) {
  911. propertyNode.type = "PropertyDefinition";
  912. propertyNode.computed = false;
  913. }
  914. return propertyNode;
  915. }
  916. parseObjectMethod(prop, isGenerator, isAsync, isPattern, isAccessor) {
  917. const node = super.parseObjectMethod(prop, isGenerator, isAsync, isPattern, isAccessor);
  918. if (node) {
  919. node.type = "Property";
  920. if (node.kind === "method") node.kind = "init";
  921. node.shorthand = false;
  922. }
  923. return node;
  924. }
  925. parseObjectProperty(prop, startPos, startLoc, isPattern, refExpressionErrors) {
  926. const node = super.parseObjectProperty(prop, startPos, startLoc, isPattern, refExpressionErrors);
  927. if (node) {
  928. node.kind = "init";
  929. node.type = "Property";
  930. }
  931. return node;
  932. }
  933. toAssignable(node, isLHS = false) {
  934. if (node != null && this.isObjectProperty(node)) {
  935. this.toAssignable(node.value, isLHS);
  936. return node;
  937. }
  938. return super.toAssignable(node, isLHS);
  939. }
  940. toAssignableObjectExpressionProp(prop, ...args) {
  941. if (prop.kind === "get" || prop.kind === "set") {
  942. this.raise(prop.key.start, ErrorMessages.PatternHasAccessor);
  943. } else if (prop.method) {
  944. this.raise(prop.key.start, ErrorMessages.PatternHasMethod);
  945. } else {
  946. super.toAssignableObjectExpressionProp(prop, ...args);
  947. }
  948. }
  949. finishCallExpression(node, optional) {
  950. super.finishCallExpression(node, optional);
  951. if (node.callee.type === "Import") {
  952. node.type = "ImportExpression";
  953. node.source = node.arguments[0];
  954. if (this.hasPlugin("importAssertions")) {
  955. var _node$arguments$;
  956. node.attributes = (_node$arguments$ = node.arguments[1]) != null ? _node$arguments$ : null;
  957. }
  958. delete node.arguments;
  959. delete node.callee;
  960. }
  961. return node;
  962. }
  963. toReferencedArguments(node) {
  964. if (node.type === "ImportExpression") {
  965. return;
  966. }
  967. super.toReferencedArguments(node);
  968. }
  969. parseExport(node) {
  970. super.parseExport(node);
  971. switch (node.type) {
  972. case "ExportAllDeclaration":
  973. node.exported = null;
  974. break;
  975. case "ExportNamedDeclaration":
  976. if (node.specifiers.length === 1 && node.specifiers[0].type === "ExportNamespaceSpecifier") {
  977. node.type = "ExportAllDeclaration";
  978. node.exported = node.specifiers[0].exported;
  979. delete node.specifiers;
  980. }
  981. break;
  982. }
  983. return node;
  984. }
  985. parseSubscript(base, startPos, startLoc, noCalls, state) {
  986. const node = super.parseSubscript(base, startPos, startLoc, noCalls, state);
  987. if (state.optionalChainMember) {
  988. if (node.type === "OptionalMemberExpression" || node.type === "OptionalCallExpression") {
  989. node.type = node.type.substring(8);
  990. }
  991. if (state.stop) {
  992. const chain = this.startNodeAtNode(node);
  993. chain.expression = node;
  994. return this.finishNode(chain, "ChainExpression");
  995. }
  996. } else if (node.type === "MemberExpression" || node.type === "CallExpression") {
  997. node.optional = false;
  998. }
  999. return node;
  1000. }
  1001. hasPropertyAsPrivateName(node) {
  1002. if (node.type === "ChainExpression") {
  1003. node = node.expression;
  1004. }
  1005. return super.hasPropertyAsPrivateName(node);
  1006. }
  1007. isOptionalChain(node) {
  1008. return node.type === "ChainExpression";
  1009. }
  1010. isObjectProperty(node) {
  1011. return node.type === "Property" && node.kind === "init" && !node.method;
  1012. }
  1013. isObjectMethod(node) {
  1014. return node.method || node.kind === "get" || node.kind === "set";
  1015. }
  1016. });
  1017. class TokContext {
  1018. constructor(token, isExpr, preserveSpace) {
  1019. this.token = void 0;
  1020. this.isExpr = void 0;
  1021. this.preserveSpace = void 0;
  1022. this.token = token;
  1023. this.isExpr = !!isExpr;
  1024. this.preserveSpace = !!preserveSpace;
  1025. }
  1026. }
  1027. const types = {
  1028. braceStatement: new TokContext("{", false),
  1029. braceExpression: new TokContext("{", true),
  1030. recordExpression: new TokContext("#{", true),
  1031. templateQuasi: new TokContext("${", false),
  1032. parenStatement: new TokContext("(", false),
  1033. parenExpression: new TokContext("(", true),
  1034. template: new TokContext("`", true, true),
  1035. functionExpression: new TokContext("function", true),
  1036. functionStatement: new TokContext("function", false)
  1037. };
  1038. types$1.parenR.updateContext = types$1.braceR.updateContext = function () {
  1039. if (this.state.context.length === 1) {
  1040. this.state.exprAllowed = true;
  1041. return;
  1042. }
  1043. let out = this.state.context.pop();
  1044. if (out === types.braceStatement && this.curContext().token === "function") {
  1045. out = this.state.context.pop();
  1046. }
  1047. this.state.exprAllowed = !out.isExpr;
  1048. };
  1049. types$1.name.updateContext = function (prevType) {
  1050. let allowed = false;
  1051. if (prevType !== types$1.dot) {
  1052. if (this.state.value === "of" && !this.state.exprAllowed && prevType !== types$1._function && prevType !== types$1._class) {
  1053. allowed = true;
  1054. }
  1055. }
  1056. this.state.exprAllowed = allowed;
  1057. };
  1058. types$1.braceL.updateContext = function (prevType) {
  1059. this.state.context.push(this.braceIsBlock(prevType) ? types.braceStatement : types.braceExpression);
  1060. this.state.exprAllowed = true;
  1061. };
  1062. types$1.dollarBraceL.updateContext = function () {
  1063. this.state.context.push(types.templateQuasi);
  1064. this.state.exprAllowed = true;
  1065. };
  1066. types$1.parenL.updateContext = function (prevType) {
  1067. const statementParens = prevType === types$1._if || prevType === types$1._for || prevType === types$1._with || prevType === types$1._while;
  1068. this.state.context.push(statementParens ? types.parenStatement : types.parenExpression);
  1069. this.state.exprAllowed = true;
  1070. };
  1071. types$1.incDec.updateContext = function () {};
  1072. types$1._function.updateContext = types$1._class.updateContext = function (prevType) {
  1073. if (prevType.beforeExpr && prevType !== types$1.semi && prevType !== types$1._else && !(prevType === types$1._return && this.hasPrecedingLineBreak()) && !((prevType === types$1.colon || prevType === types$1.braceL) && this.curContext() === types.b_stat)) {
  1074. this.state.context.push(types.functionExpression);
  1075. } else {
  1076. this.state.context.push(types.functionStatement);
  1077. }
  1078. this.state.exprAllowed = false;
  1079. };
  1080. types$1.backQuote.updateContext = function () {
  1081. if (this.curContext() === types.template) {
  1082. this.state.context.pop();
  1083. } else {
  1084. this.state.context.push(types.template);
  1085. }
  1086. this.state.exprAllowed = false;
  1087. };
  1088. types$1.braceHashL.updateContext = function () {
  1089. this.state.context.push(types.recordExpression);
  1090. this.state.exprAllowed = true;
  1091. };
  1092. 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";
  1093. 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";
  1094. const nonASCIIidentifierStart = new RegExp("[" + nonASCIIidentifierStartChars + "]");
  1095. const nonASCIIidentifier = new RegExp("[" + nonASCIIidentifierStartChars + nonASCIIidentifierChars + "]");
  1096. nonASCIIidentifierStartChars = nonASCIIidentifierChars = null;
  1097. 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];
  1098. 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];
  1099. function isInAstralSet(code, set) {
  1100. let pos = 0x10000;
  1101. for (let i = 0, length = set.length; i < length; i += 2) {
  1102. pos += set[i];
  1103. if (pos > code) return false;
  1104. pos += set[i + 1];
  1105. if (pos >= code) return true;
  1106. }
  1107. return false;
  1108. }
  1109. function isIdentifierStart(code) {
  1110. if (code < 65) return code === 36;
  1111. if (code <= 90) return true;
  1112. if (code < 97) return code === 95;
  1113. if (code <= 122) return true;
  1114. if (code <= 0xffff) {
  1115. return code >= 0xaa && nonASCIIidentifierStart.test(String.fromCharCode(code));
  1116. }
  1117. return isInAstralSet(code, astralIdentifierStartCodes);
  1118. }
  1119. function isIdentifierChar(code) {
  1120. if (code < 48) return code === 36;
  1121. if (code < 58) return true;
  1122. if (code < 65) return false;
  1123. if (code <= 90) return true;
  1124. if (code < 97) return code === 95;
  1125. if (code <= 122) return true;
  1126. if (code <= 0xffff) {
  1127. return code >= 0xaa && nonASCIIidentifier.test(String.fromCharCode(code));
  1128. }
  1129. return isInAstralSet(code, astralIdentifierStartCodes) || isInAstralSet(code, astralIdentifierCodes);
  1130. }
  1131. const reservedWords = {
  1132. 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"],
  1133. strict: ["implements", "interface", "let", "package", "private", "protected", "public", "static", "yield"],
  1134. strictBind: ["eval", "arguments"]
  1135. };
  1136. const keywords = new Set(reservedWords.keyword);
  1137. const reservedWordsStrictSet = new Set(reservedWords.strict);
  1138. const reservedWordsStrictBindSet = new Set(reservedWords.strictBind);
  1139. function isReservedWord(word, inModule) {
  1140. return inModule && word === "await" || word === "enum";
  1141. }
  1142. function isStrictReservedWord(word, inModule) {
  1143. return isReservedWord(word, inModule) || reservedWordsStrictSet.has(word);
  1144. }
  1145. function isStrictBindOnlyReservedWord(word) {
  1146. return reservedWordsStrictBindSet.has(word);
  1147. }
  1148. function isStrictBindReservedWord(word, inModule) {
  1149. return isStrictReservedWord(word, inModule) || isStrictBindOnlyReservedWord(word);
  1150. }
  1151. function isKeyword(word) {
  1152. return keywords.has(word);
  1153. }
  1154. function isIteratorStart(current, next) {
  1155. return current === 64 && next === 64;
  1156. }
  1157. const SCOPE_OTHER = 0b000000000,
  1158. SCOPE_PROGRAM = 0b000000001,
  1159. SCOPE_FUNCTION = 0b000000010,
  1160. SCOPE_ARROW = 0b000000100,
  1161. SCOPE_SIMPLE_CATCH = 0b000001000,
  1162. SCOPE_SUPER = 0b000010000,
  1163. SCOPE_DIRECT_SUPER = 0b000100000,
  1164. SCOPE_CLASS = 0b001000000,
  1165. SCOPE_STATIC_BLOCK = 0b010000000,
  1166. SCOPE_TS_MODULE = 0b100000000,
  1167. SCOPE_VAR = SCOPE_PROGRAM | SCOPE_FUNCTION | SCOPE_TS_MODULE;
  1168. const BIND_KIND_VALUE = 0b000000000001,
  1169. BIND_KIND_TYPE = 0b000000000010,
  1170. BIND_SCOPE_VAR = 0b000000000100,
  1171. BIND_SCOPE_LEXICAL = 0b000000001000,
  1172. BIND_SCOPE_FUNCTION = 0b000000010000,
  1173. BIND_FLAGS_NONE = 0b000001000000,
  1174. BIND_FLAGS_CLASS = 0b000010000000,
  1175. BIND_FLAGS_TS_ENUM = 0b000100000000,
  1176. BIND_FLAGS_TS_CONST_ENUM = 0b001000000000,
  1177. BIND_FLAGS_TS_EXPORT_ONLY = 0b010000000000,
  1178. BIND_FLAGS_FLOW_DECLARE_FN = 0b100000000000;
  1179. const BIND_CLASS = BIND_KIND_VALUE | BIND_KIND_TYPE | BIND_SCOPE_LEXICAL | BIND_FLAGS_CLASS,
  1180. BIND_LEXICAL = BIND_KIND_VALUE | 0 | BIND_SCOPE_LEXICAL | 0,
  1181. BIND_VAR = BIND_KIND_VALUE | 0 | BIND_SCOPE_VAR | 0,
  1182. BIND_FUNCTION = BIND_KIND_VALUE | 0 | BIND_SCOPE_FUNCTION | 0,
  1183. BIND_TS_INTERFACE = 0 | BIND_KIND_TYPE | 0 | BIND_FLAGS_CLASS,
  1184. BIND_TS_TYPE = 0 | BIND_KIND_TYPE | 0 | 0,
  1185. BIND_TS_ENUM = BIND_KIND_VALUE | BIND_KIND_TYPE | BIND_SCOPE_LEXICAL | BIND_FLAGS_TS_ENUM,
  1186. BIND_TS_AMBIENT = 0 | 0 | 0 | BIND_FLAGS_TS_EXPORT_ONLY,
  1187. BIND_NONE = 0 | 0 | 0 | BIND_FLAGS_NONE,
  1188. BIND_OUTSIDE = BIND_KIND_VALUE | 0 | 0 | BIND_FLAGS_NONE,
  1189. BIND_TS_CONST_ENUM = BIND_TS_ENUM | BIND_FLAGS_TS_CONST_ENUM,
  1190. BIND_TS_NAMESPACE = 0 | 0 | 0 | BIND_FLAGS_TS_EXPORT_ONLY,
  1191. BIND_FLOW_DECLARE_FN = BIND_FLAGS_FLOW_DECLARE_FN;
  1192. const CLASS_ELEMENT_FLAG_STATIC = 0b100,
  1193. CLASS_ELEMENT_KIND_GETTER = 0b010,
  1194. CLASS_ELEMENT_KIND_SETTER = 0b001,
  1195. CLASS_ELEMENT_KIND_ACCESSOR = CLASS_ELEMENT_KIND_GETTER | CLASS_ELEMENT_KIND_SETTER;
  1196. const CLASS_ELEMENT_STATIC_GETTER = CLASS_ELEMENT_KIND_GETTER | CLASS_ELEMENT_FLAG_STATIC,
  1197. CLASS_ELEMENT_STATIC_SETTER = CLASS_ELEMENT_KIND_SETTER | CLASS_ELEMENT_FLAG_STATIC,
  1198. CLASS_ELEMENT_INSTANCE_GETTER = CLASS_ELEMENT_KIND_GETTER,
  1199. CLASS_ELEMENT_INSTANCE_SETTER = CLASS_ELEMENT_KIND_SETTER,
  1200. CLASS_ELEMENT_OTHER = 0;
  1201. class Scope {
  1202. constructor(flags) {
  1203. this.flags = void 0;
  1204. this.var = [];
  1205. this.lexical = [];
  1206. this.functions = [];
  1207. this.flags = flags;
  1208. }
  1209. }
  1210. class ScopeHandler {
  1211. constructor(raise, inModule) {
  1212. this.scopeStack = [];
  1213. this.undefinedExports = new Map();
  1214. this.undefinedPrivateNames = new Map();
  1215. this.raise = raise;
  1216. this.inModule = inModule;
  1217. }
  1218. get inFunction() {
  1219. return (this.currentVarScope().flags & SCOPE_FUNCTION) > 0;
  1220. }
  1221. get allowSuper() {
  1222. return (this.currentThisScope().flags & SCOPE_SUPER) > 0;
  1223. }
  1224. get allowDirectSuper() {
  1225. return (this.currentThisScope().flags & SCOPE_DIRECT_SUPER) > 0;
  1226. }
  1227. get inClass() {
  1228. return (this.currentThisScope().flags & SCOPE_CLASS) > 0;
  1229. }
  1230. get inStaticBlock() {
  1231. return (this.currentThisScope().flags & SCOPE_STATIC_BLOCK) > 0;
  1232. }
  1233. get inNonArrowFunction() {
  1234. return (this.currentThisScope().flags & SCOPE_FUNCTION) > 0;
  1235. }
  1236. get treatFunctionsAsVar() {
  1237. return this.treatFunctionsAsVarInScope(this.currentScope());
  1238. }
  1239. createScope(flags) {
  1240. return new Scope(flags);
  1241. }
  1242. enter(flags) {
  1243. this.scopeStack.push(this.createScope(flags));
  1244. }
  1245. exit() {
  1246. this.scopeStack.pop();
  1247. }
  1248. treatFunctionsAsVarInScope(scope) {
  1249. return !!(scope.flags & SCOPE_FUNCTION || !this.inModule && scope.flags & SCOPE_PROGRAM);
  1250. }
  1251. declareName(name, bindingType, pos) {
  1252. let scope = this.currentScope();
  1253. if (bindingType & BIND_SCOPE_LEXICAL || bindingType & BIND_SCOPE_FUNCTION) {
  1254. this.checkRedeclarationInScope(scope, name, bindingType, pos);
  1255. if (bindingType & BIND_SCOPE_FUNCTION) {
  1256. scope.functions.push(name);
  1257. } else {
  1258. scope.lexical.push(name);
  1259. }
  1260. if (bindingType & BIND_SCOPE_LEXICAL) {
  1261. this.maybeExportDefined(scope, name);
  1262. }
  1263. } else if (bindingType & BIND_SCOPE_VAR) {
  1264. for (let i = this.scopeStack.length - 1; i >= 0; --i) {
  1265. scope = this.scopeStack[i];
  1266. this.checkRedeclarationInScope(scope, name, bindingType, pos);
  1267. scope.var.push(name);
  1268. this.maybeExportDefined(scope, name);
  1269. if (scope.flags & SCOPE_VAR) break;
  1270. }
  1271. }
  1272. if (this.inModule && scope.flags & SCOPE_PROGRAM) {
  1273. this.undefinedExports.delete(name);
  1274. }
  1275. }
  1276. maybeExportDefined(scope, name) {
  1277. if (this.inModule && scope.flags & SCOPE_PROGRAM) {
  1278. this.undefinedExports.delete(name);
  1279. }
  1280. }
  1281. checkRedeclarationInScope(scope, name, bindingType, pos) {
  1282. if (this.isRedeclaredInScope(scope, name, bindingType)) {
  1283. this.raise(pos, ErrorMessages.VarRedeclaration, name);
  1284. }
  1285. }
  1286. isRedeclaredInScope(scope, name, bindingType) {
  1287. if (!(bindingType & BIND_KIND_VALUE)) return false;
  1288. if (bindingType & BIND_SCOPE_LEXICAL) {
  1289. return scope.lexical.indexOf(name) > -1 || scope.functions.indexOf(name) > -1 || scope.var.indexOf(name) > -1;
  1290. }
  1291. if (bindingType & BIND_SCOPE_FUNCTION) {
  1292. return scope.lexical.indexOf(name) > -1 || !this.treatFunctionsAsVarInScope(scope) && scope.var.indexOf(name) > -1;
  1293. }
  1294. return scope.lexical.indexOf(name) > -1 && !(scope.flags & SCOPE_SIMPLE_CATCH && scope.lexical[0] === name) || !this.treatFunctionsAsVarInScope(scope) && scope.functions.indexOf(name) > -1;
  1295. }
  1296. checkLocalExport(id) {
  1297. if (this.scopeStack[0].lexical.indexOf(id.name) === -1 && this.scopeStack[0].var.indexOf(id.name) === -1 && this.scopeStack[0].functions.indexOf(id.name) === -1) {
  1298. this.undefinedExports.set(id.name, id.start);
  1299. }
  1300. }
  1301. currentScope() {
  1302. return this.scopeStack[this.scopeStack.length - 1];
  1303. }
  1304. currentVarScope() {
  1305. for (let i = this.scopeStack.length - 1;; i--) {
  1306. const scope = this.scopeStack[i];
  1307. if (scope.flags & SCOPE_VAR) {
  1308. return scope;
  1309. }
  1310. }
  1311. }
  1312. currentThisScope() {
  1313. for (let i = this.scopeStack.length - 1;; i--) {
  1314. const scope = this.scopeStack[i];
  1315. if ((scope.flags & SCOPE_VAR || scope.flags & SCOPE_CLASS) && !(scope.flags & SCOPE_ARROW)) {
  1316. return scope;
  1317. }
  1318. }
  1319. }
  1320. }
  1321. class FlowScope extends Scope {
  1322. constructor(...args) {
  1323. super(...args);
  1324. this.declareFunctions = [];
  1325. }
  1326. }
  1327. class FlowScopeHandler extends ScopeHandler {
  1328. createScope(flags) {
  1329. return new FlowScope(flags);
  1330. }
  1331. declareName(name, bindingType, pos) {
  1332. const scope = this.currentScope();
  1333. if (bindingType & BIND_FLAGS_FLOW_DECLARE_FN) {
  1334. this.checkRedeclarationInScope(scope, name, bindingType, pos);
  1335. this.maybeExportDefined(scope, name);
  1336. scope.declareFunctions.push(name);
  1337. return;
  1338. }
  1339. super.declareName(...arguments);
  1340. }
  1341. isRedeclaredInScope(scope, name, bindingType) {
  1342. if (super.isRedeclaredInScope(...arguments)) return true;
  1343. if (bindingType & BIND_FLAGS_FLOW_DECLARE_FN) {
  1344. return !scope.declareFunctions.includes(name) && (scope.lexical.includes(name) || scope.functions.includes(name));
  1345. }
  1346. return false;
  1347. }
  1348. checkLocalExport(id) {
  1349. if (this.scopeStack[0].declareFunctions.indexOf(id.name) === -1) {
  1350. super.checkLocalExport(id);
  1351. }
  1352. }
  1353. }
  1354. const reservedTypes = new Set(["_", "any", "bool", "boolean", "empty", "extends", "false", "interface", "mixed", "null", "number", "static", "string", "true", "typeof", "void"]);
  1355. const FlowErrors = makeErrorTemplates({
  1356. AmbiguousConditionalArrow: "Ambiguous expression: wrap the arrow functions in parentheses to disambiguate.",
  1357. 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.",
  1358. AssignReservedType: "Cannot overwrite reserved type %0.",
  1359. DeclareClassElement: "The `declare` modifier can only appear on class fields.",
  1360. DeclareClassFieldInitializer: "Initializers are not allowed in fields with the `declare` modifier.",
  1361. DuplicateDeclareModuleExports: "Duplicate `declare module.exports` statement.",
  1362. EnumBooleanMemberNotInitialized: "Boolean enum members need to be initialized. Use either `%0 = true,` or `%0 = false,` in enum `%1`.",
  1363. EnumDuplicateMemberName: "Enum member names need to be unique, but the name `%0` has already been used before in enum `%1`.",
  1364. EnumInconsistentMemberValues: "Enum `%0` has inconsistent member initializers. Either use no initializers, or consistently use literals (either booleans, numbers, or strings) for all member initializers.",
  1365. EnumInvalidExplicitType: "Enum type `%1` is not valid. Use one of `boolean`, `number`, `string`, or `symbol` in enum `%0`.",
  1366. EnumInvalidExplicitTypeUnknownSupplied: "Supplied enum type is not valid. Use one of `boolean`, `number`, `string`, or `symbol` in enum `%0`.",
  1367. EnumInvalidMemberInitializerPrimaryType: "Enum `%0` has type `%2`, so the initializer of `%1` needs to be a %2 literal.",
  1368. EnumInvalidMemberInitializerSymbolType: "Symbol enum members cannot be initialized. Use `%1,` in enum `%0`.",
  1369. EnumInvalidMemberInitializerUnknownType: "The enum member initializer for `%1` needs to be a literal (either a boolean, number, or string) in enum `%0`.",
  1370. EnumInvalidMemberName: "Enum member names cannot start with lowercase 'a' through 'z'. Instead of using `%0`, consider using `%1`, in enum `%2`.",
  1371. EnumNumberMemberNotInitialized: "Number enum members need to be initialized, e.g. `%1 = 1` in enum `%0`.",
  1372. EnumStringMemberInconsistentlyInitailized: "String enum members need to consistently either all use initializers, or use no initializers, in enum `%0`.",
  1373. GetterMayNotHaveThisParam: "A getter cannot have a `this` parameter.",
  1374. 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.",
  1375. InexactInsideExact: "Explicit inexact syntax cannot appear inside an explicit exact object type.",
  1376. InexactInsideNonObject: "Explicit inexact syntax cannot appear in class or interface definitions.",
  1377. InexactVariance: "Explicit inexact syntax cannot have variance.",
  1378. InvalidNonTypeImportInDeclareModule: "Imports within a `declare module` body must always be `import type` or `import typeof`.",
  1379. MissingTypeParamDefault: "Type parameter declaration needs a default, since a preceding type parameter declaration has a default.",
  1380. NestedDeclareModule: "`declare module` cannot be used inside another `declare module`.",
  1381. NestedFlowComment: "Cannot have a flow comment inside another flow comment.",
  1382. OptionalBindingPattern: "A binding pattern parameter cannot be optional in an implementation signature.",
  1383. SetterMayNotHaveThisParam: "A setter cannot have a `this` parameter.",
  1384. SpreadVariance: "Spread properties cannot have variance.",
  1385. ThisParamAnnotationRequired: "A type annotation is required for the `this` parameter.",
  1386. ThisParamBannedInConstructor: "Constructors cannot have a `this` parameter; constructors don't bind `this` like other functions.",
  1387. ThisParamMayNotBeOptional: "The `this` parameter cannot be optional.",
  1388. ThisParamMustBeFirst: "The `this` parameter must be the first function parameter.",
  1389. ThisParamNoDefault: "The `this` parameter may not have a default value.",
  1390. TypeBeforeInitializer: "Type annotations must come before default assignments, e.g. instead of `age = 25: number` use `age: number = 25`.",
  1391. TypeCastInPattern: "The type cast expression is expected to be wrapped with parenthesis.",
  1392. UnexpectedExplicitInexactInObject: "Explicit inexact syntax must appear at the end of an inexact object.",
  1393. UnexpectedReservedType: "Unexpected reserved type %0.",
  1394. UnexpectedReservedUnderscore: "`_` is only allowed as a type argument to call or new.",
  1395. UnexpectedSpaceBetweenModuloChecks: "Spaces between `%` and `checks` are not allowed here.",
  1396. UnexpectedSpreadType: "Spread operator cannot appear in class or interface definitions.",
  1397. UnexpectedSubtractionOperand: 'Unexpected token, expected "number" or "bigint".',
  1398. UnexpectedTokenAfterTypeParameter: "Expected an arrow function after this type parameter declaration.",
  1399. UnexpectedTypeParameterBeforeAsyncArrowFunction: "Type parameters must come after the async keyword, e.g. instead of `<T> async () => {}`, use `async <T>() => {}`.",
  1400. UnsupportedDeclareExportKind: "`declare export %0` is not supported. Use `%1` instead.",
  1401. UnsupportedStatementInDeclareModule: "Only declares and type imports are allowed inside declare module.",
  1402. UnterminatedFlowComment: "Unterminated flow-comment."
  1403. }, ErrorCodes.SyntaxError);
  1404. function isEsModuleType(bodyElement) {
  1405. return bodyElement.type === "DeclareExportAllDeclaration" || bodyElement.type === "DeclareExportDeclaration" && (!bodyElement.declaration || bodyElement.declaration.type !== "TypeAlias" && bodyElement.declaration.type !== "InterfaceDeclaration");
  1406. }
  1407. function hasTypeImportKind(node) {
  1408. return node.importKind === "type" || node.importKind === "typeof";
  1409. }
  1410. function isMaybeDefaultImport(state) {
  1411. return (state.type === types$1.name || !!state.type.keyword) && state.value !== "from";
  1412. }
  1413. const exportSuggestions = {
  1414. const: "declare export var",
  1415. let: "declare export var",
  1416. type: "export type",
  1417. interface: "export interface"
  1418. };
  1419. function partition(list, test) {
  1420. const list1 = [];
  1421. const list2 = [];
  1422. for (let i = 0; i < list.length; i++) {
  1423. (test(list[i], i, list) ? list1 : list2).push(list[i]);
  1424. }
  1425. return [list1, list2];
  1426. }
  1427. const FLOW_PRAGMA_REGEX = /\*?\s*@((?:no)?flow)\b/;
  1428. var flow = (superClass => class extends superClass {
  1429. constructor(...args) {
  1430. super(...args);
  1431. this.flowPragma = undefined;
  1432. }
  1433. getScopeHandler() {
  1434. return FlowScopeHandler;
  1435. }
  1436. shouldParseTypes() {
  1437. return this.getPluginOption("flow", "all") || this.flowPragma === "flow";
  1438. }
  1439. shouldParseEnums() {
  1440. return !!this.getPluginOption("flow", "enums");
  1441. }
  1442. finishToken(type, val) {
  1443. if (type !== types$1.string && type !== types$1.semi && type !== types$1.interpreterDirective) {
  1444. if (this.flowPragma === undefined) {
  1445. this.flowPragma = null;
  1446. }
  1447. }
  1448. return super.finishToken(type, val);
  1449. }
  1450. addComment(comment) {
  1451. if (this.flowPragma === undefined) {
  1452. const matches = FLOW_PRAGMA_REGEX.exec(comment.value);
  1453. if (!matches) ; else if (matches[1] === "flow") {
  1454. this.flowPragma = "flow";
  1455. } else if (matches[1] === "noflow") {
  1456. this.flowPragma = "noflow";
  1457. } else {
  1458. throw new Error("Unexpected flow pragma");
  1459. }
  1460. }
  1461. return super.addComment(comment);
  1462. }
  1463. flowParseTypeInitialiser(tok) {
  1464. const oldInType = this.state.inType;
  1465. this.state.inType = true;
  1466. this.expect(tok || types$1.colon);
  1467. const type = this.flowParseType();
  1468. this.state.inType = oldInType;
  1469. return type;
  1470. }
  1471. flowParsePredicate() {
  1472. const node = this.startNode();
  1473. const moduloPos = this.state.start;
  1474. this.next();
  1475. this.expectContextual("checks");
  1476. if (this.state.lastTokStart > moduloPos + 1) {
  1477. this.raise(moduloPos, FlowErrors.UnexpectedSpaceBetweenModuloChecks);
  1478. }
  1479. if (this.eat(types$1.parenL)) {
  1480. node.value = this.parseExpression();
  1481. this.expect(types$1.parenR);
  1482. return this.finishNode(node, "DeclaredPredicate");
  1483. } else {
  1484. return this.finishNode(node, "InferredPredicate");
  1485. }
  1486. }
  1487. flowParseTypeAndPredicateInitialiser() {
  1488. const oldInType = this.state.inType;
  1489. this.state.inType = true;
  1490. this.expect(types$1.colon);
  1491. let type = null;
  1492. let predicate = null;
  1493. if (this.match(types$1.modulo)) {
  1494. this.state.inType = oldInType;
  1495. predicate = this.flowParsePredicate();
  1496. } else {
  1497. type = this.flowParseType();
  1498. this.state.inType = oldInType;
  1499. if (this.match(types$1.modulo)) {
  1500. predicate = this.flowParsePredicate();
  1501. }
  1502. }
  1503. return [type, predicate];
  1504. }
  1505. flowParseDeclareClass(node) {
  1506. this.next();
  1507. this.flowParseInterfaceish(node, true);
  1508. return this.finishNode(node, "DeclareClass");
  1509. }
  1510. flowParseDeclareFunction(node) {
  1511. this.next();
  1512. const id = node.id = this.parseIdentifier();
  1513. const typeNode = this.startNode();
  1514. const typeContainer = this.startNode();
  1515. if (this.isRelational("<")) {
  1516. typeNode.typeParameters = this.flowParseTypeParameterDeclaration();
  1517. } else {
  1518. typeNode.typeParameters = null;
  1519. }
  1520. this.expect(types$1.parenL);
  1521. const tmp = this.flowParseFunctionTypeParams();
  1522. typeNode.params = tmp.params;
  1523. typeNode.rest = tmp.rest;
  1524. typeNode.this = tmp._this;
  1525. this.expect(types$1.parenR);
  1526. [typeNode.returnType, node.predicate] = this.flowParseTypeAndPredicateInitialiser();
  1527. typeContainer.typeAnnotation = this.finishNode(typeNode, "FunctionTypeAnnotation");
  1528. id.typeAnnotation = this.finishNode(typeContainer, "TypeAnnotation");
  1529. this.resetEndLocation(id);
  1530. this.semicolon();
  1531. this.scope.declareName(node.id.name, BIND_FLOW_DECLARE_FN, node.id.start);
  1532. return this.finishNode(node, "DeclareFunction");
  1533. }
  1534. flowParseDeclare(node, insideModule) {
  1535. if (this.match(types$1._class)) {
  1536. return this.flowParseDeclareClass(node);
  1537. } else if (this.match(types$1._function)) {
  1538. return this.flowParseDeclareFunction(node);
  1539. } else if (this.match(types$1._var)) {
  1540. return this.flowParseDeclareVariable(node);
  1541. } else if (this.eatContextual("module")) {
  1542. if (this.match(types$1.dot)) {
  1543. return this.flowParseDeclareModuleExports(node);
  1544. } else {
  1545. if (insideModule) {
  1546. this.raise(this.state.lastTokStart, FlowErrors.NestedDeclareModule);
  1547. }
  1548. return this.flowParseDeclareModule(node);
  1549. }
  1550. } else if (this.isContextual("type")) {
  1551. return this.flowParseDeclareTypeAlias(node);
  1552. } else if (this.isContextual("opaque")) {
  1553. return this.flowParseDeclareOpaqueType(node);
  1554. } else if (this.isContextual("interface")) {
  1555. return this.flowParseDeclareInterface(node);
  1556. } else if (this.match(types$1._export)) {
  1557. return this.flowParseDeclareExportDeclaration(node, insideModule);
  1558. } else {
  1559. throw this.unexpected();
  1560. }
  1561. }
  1562. flowParseDeclareVariable(node) {
  1563. this.next();
  1564. node.id = this.flowParseTypeAnnotatableIdentifier(true);
  1565. this.scope.declareName(node.id.name, BIND_VAR, node.id.start);
  1566. this.semicolon();
  1567. return this.finishNode(node, "DeclareVariable");
  1568. }
  1569. flowParseDeclareModule(node) {
  1570. this.scope.enter(SCOPE_OTHER);
  1571. if (this.match(types$1.string)) {
  1572. node.id = this.parseExprAtom();
  1573. } else {
  1574. node.id = this.parseIdentifier();
  1575. }
  1576. const bodyNode = node.body = this.startNode();
  1577. const body = bodyNode.body = [];
  1578. this.expect(types$1.braceL);
  1579. while (!this.match(types$1.braceR)) {
  1580. let bodyNode = this.startNode();
  1581. if (this.match(types$1._import)) {
  1582. this.next();
  1583. if (!this.isContextual("type") && !this.match(types$1._typeof)) {
  1584. this.raise(this.state.lastTokStart, FlowErrors.InvalidNonTypeImportInDeclareModule);
  1585. }
  1586. this.parseImport(bodyNode);
  1587. } else {
  1588. this.expectContextual("declare", FlowErrors.UnsupportedStatementInDeclareModule);
  1589. bodyNode = this.flowParseDeclare(bodyNode, true);
  1590. }
  1591. body.push(bodyNode);
  1592. }
  1593. this.scope.exit();
  1594. this.expect(types$1.braceR);
  1595. this.finishNode(bodyNode, "BlockStatement");
  1596. let kind = null;
  1597. let hasModuleExport = false;
  1598. body.forEach(bodyElement => {
  1599. if (isEsModuleType(bodyElement)) {
  1600. if (kind === "CommonJS") {
  1601. this.raise(bodyElement.start, FlowErrors.AmbiguousDeclareModuleKind);
  1602. }
  1603. kind = "ES";
  1604. } else if (bodyElement.type === "DeclareModuleExports") {
  1605. if (hasModuleExport) {
  1606. this.raise(bodyElement.start, FlowErrors.DuplicateDeclareModuleExports);
  1607. }
  1608. if (kind === "ES") {
  1609. this.raise(bodyElement.start, FlowErrors.AmbiguousDeclareModuleKind);
  1610. }
  1611. kind = "CommonJS";
  1612. hasModuleExport = true;
  1613. }
  1614. });
  1615. node.kind = kind || "CommonJS";
  1616. return this.finishNode(node, "DeclareModule");
  1617. }
  1618. flowParseDeclareExportDeclaration(node, insideModule) {
  1619. this.expect(types$1._export);
  1620. if (this.eat(types$1._default)) {
  1621. if (this.match(types$1._function) || this.match(types$1._class)) {
  1622. node.declaration = this.flowParseDeclare(this.startNode());
  1623. } else {
  1624. node.declaration = this.flowParseType();
  1625. this.semicolon();
  1626. }
  1627. node.default = true;
  1628. return this.finishNode(node, "DeclareExportDeclaration");
  1629. } else {
  1630. if (this.match(types$1._const) || this.isLet() || (this.isContextual("type") || this.isContextual("interface")) && !insideModule) {
  1631. const label = this.state.value;
  1632. const suggestion = exportSuggestions[label];
  1633. throw this.raise(this.state.start, FlowErrors.UnsupportedDeclareExportKind, label, suggestion);
  1634. }
  1635. if (this.match(types$1._var) || this.match(types$1._function) || this.match(types$1._class) || this.isContextual("opaque")) {
  1636. node.declaration = this.flowParseDeclare(this.startNode());
  1637. node.default = false;
  1638. return this.finishNode(node, "DeclareExportDeclaration");
  1639. } else if (this.match(types$1.star) || this.match(types$1.braceL) || this.isContextual("interface") || this.isContextual("type") || this.isContextual("opaque")) {
  1640. node = this.parseExport(node);
  1641. if (node.type === "ExportNamedDeclaration") {
  1642. node.type = "ExportDeclaration";
  1643. node.default = false;
  1644. delete node.exportKind;
  1645. }
  1646. node.type = "Declare" + node.type;
  1647. return node;
  1648. }
  1649. }
  1650. throw this.unexpected();
  1651. }
  1652. flowParseDeclareModuleExports(node) {
  1653. this.next();
  1654. this.expectContextual("exports");
  1655. node.typeAnnotation = this.flowParseTypeAnnotation();
  1656. this.semicolon();
  1657. return this.finishNode(node, "DeclareModuleExports");
  1658. }
  1659. flowParseDeclareTypeAlias(node) {
  1660. this.next();
  1661. this.flowParseTypeAlias(node);
  1662. node.type = "DeclareTypeAlias";
  1663. return node;
  1664. }
  1665. flowParseDeclareOpaqueType(node) {
  1666. this.next();
  1667. this.flowParseOpaqueType(node, true);
  1668. node.type = "DeclareOpaqueType";
  1669. return node;
  1670. }
  1671. flowParseDeclareInterface(node) {
  1672. this.next();
  1673. this.flowParseInterfaceish(node);
  1674. return this.finishNode(node, "DeclareInterface");
  1675. }
  1676. flowParseInterfaceish(node, isClass = false) {
  1677. node.id = this.flowParseRestrictedIdentifier(!isClass, true);
  1678. this.scope.declareName(node.id.name, isClass ? BIND_FUNCTION : BIND_LEXICAL, node.id.start);
  1679. if (this.isRelational("<")) {
  1680. node.typeParameters = this.flowParseTypeParameterDeclaration();
  1681. } else {
  1682. node.typeParameters = null;
  1683. }
  1684. node.extends = [];
  1685. node.implements = [];
  1686. node.mixins = [];
  1687. if (this.eat(types$1._extends)) {
  1688. do {
  1689. node.extends.push(this.flowParseInterfaceExtends());
  1690. } while (!isClass && this.eat(types$1.comma));
  1691. }
  1692. if (this.isContextual("mixins")) {
  1693. this.next();
  1694. do {
  1695. node.mixins.push(this.flowParseInterfaceExtends());
  1696. } while (this.eat(types$1.comma));
  1697. }
  1698. if (this.isContextual("implements")) {
  1699. this.next();
  1700. do {
  1701. node.implements.push(this.flowParseInterfaceExtends());
  1702. } while (this.eat(types$1.comma));
  1703. }
  1704. node.body = this.flowParseObjectType({
  1705. allowStatic: isClass,
  1706. allowExact: false,
  1707. allowSpread: false,
  1708. allowProto: isClass,
  1709. allowInexact: false
  1710. });
  1711. }
  1712. flowParseInterfaceExtends() {
  1713. const node = this.startNode();
  1714. node.id = this.flowParseQualifiedTypeIdentifier();
  1715. if (this.isRelational("<")) {
  1716. node.typeParameters = this.flowParseTypeParameterInstantiation();
  1717. } else {
  1718. node.typeParameters = null;
  1719. }
  1720. return this.finishNode(node, "InterfaceExtends");
  1721. }
  1722. flowParseInterface(node) {
  1723. this.flowParseInterfaceish(node);
  1724. return this.finishNode(node, "InterfaceDeclaration");
  1725. }
  1726. checkNotUnderscore(word) {
  1727. if (word === "_") {
  1728. this.raise(this.state.start, FlowErrors.UnexpectedReservedUnderscore);
  1729. }
  1730. }
  1731. checkReservedType(word, startLoc, declaration) {
  1732. if (!reservedTypes.has(word)) return;
  1733. this.raise(startLoc, declaration ? FlowErrors.AssignReservedType : FlowErrors.UnexpectedReservedType, word);
  1734. }
  1735. flowParseRestrictedIdentifier(liberal, declaration) {
  1736. this.checkReservedType(this.state.value, this.state.start, declaration);
  1737. return this.parseIdentifier(liberal);
  1738. }
  1739. flowParseTypeAlias(node) {
  1740. node.id = this.flowParseRestrictedIdentifier(false, true);
  1741. this.scope.declareName(node.id.name, BIND_LEXICAL, node.id.start);
  1742. if (this.isRelational("<")) {
  1743. node.typeParameters = this.flowParseTypeParameterDeclaration();
  1744. } else {
  1745. node.typeParameters = null;
  1746. }
  1747. node.right = this.flowParseTypeInitialiser(types$1.eq);
  1748. this.semicolon();
  1749. return this.finishNode(node, "TypeAlias");
  1750. }
  1751. flowParseOpaqueType(node, declare) {
  1752. this.expectContextual("type");
  1753. node.id = this.flowParseRestrictedIdentifier(true, true);
  1754. this.scope.declareName(node.id.name, BIND_LEXICAL, node.id.start);
  1755. if (this.isRelational("<")) {
  1756. node.typeParameters = this.flowParseTypeParameterDeclaration();
  1757. } else {
  1758. node.typeParameters = null;
  1759. }
  1760. node.supertype = null;
  1761. if (this.match(types$1.colon)) {
  1762. node.supertype = this.flowParseTypeInitialiser(types$1.colon);
  1763. }
  1764. node.impltype = null;
  1765. if (!declare) {
  1766. node.impltype = this.flowParseTypeInitialiser(types$1.eq);
  1767. }
  1768. this.semicolon();
  1769. return this.finishNode(node, "OpaqueType");
  1770. }
  1771. flowParseTypeParameter(requireDefault = false) {
  1772. const nodeStart = this.state.start;
  1773. const node = this.startNode();
  1774. const variance = this.flowParseVariance();
  1775. const ident = this.flowParseTypeAnnotatableIdentifier();
  1776. node.name = ident.name;
  1777. node.variance = variance;
  1778. node.bound = ident.typeAnnotation;
  1779. if (this.match(types$1.eq)) {
  1780. this.eat(types$1.eq);
  1781. node.default = this.flowParseType();
  1782. } else {
  1783. if (requireDefault) {
  1784. this.raise(nodeStart, FlowErrors.MissingTypeParamDefault);
  1785. }
  1786. }
  1787. return this.finishNode(node, "TypeParameter");
  1788. }
  1789. flowParseTypeParameterDeclaration() {
  1790. const oldInType = this.state.inType;
  1791. const node = this.startNode();
  1792. node.params = [];
  1793. this.state.inType = true;
  1794. if (this.isRelational("<") || this.match(types$1.jsxTagStart)) {
  1795. this.next();
  1796. } else {
  1797. this.unexpected();
  1798. }
  1799. let defaultRequired = false;
  1800. do {
  1801. const typeParameter = this.flowParseTypeParameter(defaultRequired);
  1802. node.params.push(typeParameter);
  1803. if (typeParameter.default) {
  1804. defaultRequired = true;
  1805. }
  1806. if (!this.isRelational(">")) {
  1807. this.expect(types$1.comma);
  1808. }
  1809. } while (!this.isRelational(">"));
  1810. this.expectRelational(">");
  1811. this.state.inType = oldInType;
  1812. return this.finishNode(node, "TypeParameterDeclaration");
  1813. }
  1814. flowParseTypeParameterInstantiation() {
  1815. const node = this.startNode();
  1816. const oldInType = this.state.inType;
  1817. node.params = [];
  1818. this.state.inType = true;
  1819. this.expectRelational("<");
  1820. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  1821. this.state.noAnonFunctionType = false;
  1822. while (!this.isRelational(">")) {
  1823. node.params.push(this.flowParseType());
  1824. if (!this.isRelational(">")) {
  1825. this.expect(types$1.comma);
  1826. }
  1827. }
  1828. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  1829. this.expectRelational(">");
  1830. this.state.inType = oldInType;
  1831. return this.finishNode(node, "TypeParameterInstantiation");
  1832. }
  1833. flowParseTypeParameterInstantiationCallOrNew() {
  1834. const node = this.startNode();
  1835. const oldInType = this.state.inType;
  1836. node.params = [];
  1837. this.state.inType = true;
  1838. this.expectRelational("<");
  1839. while (!this.isRelational(">")) {
  1840. node.params.push(this.flowParseTypeOrImplicitInstantiation());
  1841. if (!this.isRelational(">")) {
  1842. this.expect(types$1.comma);
  1843. }
  1844. }
  1845. this.expectRelational(">");
  1846. this.state.inType = oldInType;
  1847. return this.finishNode(node, "TypeParameterInstantiation");
  1848. }
  1849. flowParseInterfaceType() {
  1850. const node = this.startNode();
  1851. this.expectContextual("interface");
  1852. node.extends = [];
  1853. if (this.eat(types$1._extends)) {
  1854. do {
  1855. node.extends.push(this.flowParseInterfaceExtends());
  1856. } while (this.eat(types$1.comma));
  1857. }
  1858. node.body = this.flowParseObjectType({
  1859. allowStatic: false,
  1860. allowExact: false,
  1861. allowSpread: false,
  1862. allowProto: false,
  1863. allowInexact: false
  1864. });
  1865. return this.finishNode(node, "InterfaceTypeAnnotation");
  1866. }
  1867. flowParseObjectPropertyKey() {
  1868. return this.match(types$1.num) || this.match(types$1.string) ? this.parseExprAtom() : this.parseIdentifier(true);
  1869. }
  1870. flowParseObjectTypeIndexer(node, isStatic, variance) {
  1871. node.static = isStatic;
  1872. if (this.lookahead().type === types$1.colon) {
  1873. node.id = this.flowParseObjectPropertyKey();
  1874. node.key = this.flowParseTypeInitialiser();
  1875. } else {
  1876. node.id = null;
  1877. node.key = this.flowParseType();
  1878. }
  1879. this.expect(types$1.bracketR);
  1880. node.value = this.flowParseTypeInitialiser();
  1881. node.variance = variance;
  1882. return this.finishNode(node, "ObjectTypeIndexer");
  1883. }
  1884. flowParseObjectTypeInternalSlot(node, isStatic) {
  1885. node.static = isStatic;
  1886. node.id = this.flowParseObjectPropertyKey();
  1887. this.expect(types$1.bracketR);
  1888. this.expect(types$1.bracketR);
  1889. if (this.isRelational("<") || this.match(types$1.parenL)) {
  1890. node.method = true;
  1891. node.optional = false;
  1892. node.value = this.flowParseObjectTypeMethodish(this.startNodeAt(node.start, node.loc.start));
  1893. } else {
  1894. node.method = false;
  1895. if (this.eat(types$1.question)) {
  1896. node.optional = true;
  1897. }
  1898. node.value = this.flowParseTypeInitialiser();
  1899. }
  1900. return this.finishNode(node, "ObjectTypeInternalSlot");
  1901. }
  1902. flowParseObjectTypeMethodish(node) {
  1903. node.params = [];
  1904. node.rest = null;
  1905. node.typeParameters = null;
  1906. node.this = null;
  1907. if (this.isRelational("<")) {
  1908. node.typeParameters = this.flowParseTypeParameterDeclaration();
  1909. }
  1910. this.expect(types$1.parenL);
  1911. if (this.match(types$1._this)) {
  1912. node.this = this.flowParseFunctionTypeParam(true);
  1913. node.this.name = null;
  1914. if (!this.match(types$1.parenR)) {
  1915. this.expect(types$1.comma);
  1916. }
  1917. }
  1918. while (!this.match(types$1.parenR) && !this.match(types$1.ellipsis)) {
  1919. node.params.push(this.flowParseFunctionTypeParam(false));
  1920. if (!this.match(types$1.parenR)) {
  1921. this.expect(types$1.comma);
  1922. }
  1923. }
  1924. if (this.eat(types$1.ellipsis)) {
  1925. node.rest = this.flowParseFunctionTypeParam(false);
  1926. }
  1927. this.expect(types$1.parenR);
  1928. node.returnType = this.flowParseTypeInitialiser();
  1929. return this.finishNode(node, "FunctionTypeAnnotation");
  1930. }
  1931. flowParseObjectTypeCallProperty(node, isStatic) {
  1932. const valueNode = this.startNode();
  1933. node.static = isStatic;
  1934. node.value = this.flowParseObjectTypeMethodish(valueNode);
  1935. return this.finishNode(node, "ObjectTypeCallProperty");
  1936. }
  1937. flowParseObjectType({
  1938. allowStatic,
  1939. allowExact,
  1940. allowSpread,
  1941. allowProto,
  1942. allowInexact
  1943. }) {
  1944. const oldInType = this.state.inType;
  1945. this.state.inType = true;
  1946. const nodeStart = this.startNode();
  1947. nodeStart.callProperties = [];
  1948. nodeStart.properties = [];
  1949. nodeStart.indexers = [];
  1950. nodeStart.internalSlots = [];
  1951. let endDelim;
  1952. let exact;
  1953. let inexact = false;
  1954. if (allowExact && this.match(types$1.braceBarL)) {
  1955. this.expect(types$1.braceBarL);
  1956. endDelim = types$1.braceBarR;
  1957. exact = true;
  1958. } else {
  1959. this.expect(types$1.braceL);
  1960. endDelim = types$1.braceR;
  1961. exact = false;
  1962. }
  1963. nodeStart.exact = exact;
  1964. while (!this.match(endDelim)) {
  1965. let isStatic = false;
  1966. let protoStart = null;
  1967. let inexactStart = null;
  1968. const node = this.startNode();
  1969. if (allowProto && this.isContextual("proto")) {
  1970. const lookahead = this.lookahead();
  1971. if (lookahead.type !== types$1.colon && lookahead.type !== types$1.question) {
  1972. this.next();
  1973. protoStart = this.state.start;
  1974. allowStatic = false;
  1975. }
  1976. }
  1977. if (allowStatic && this.isContextual("static")) {
  1978. const lookahead = this.lookahead();
  1979. if (lookahead.type !== types$1.colon && lookahead.type !== types$1.question) {
  1980. this.next();
  1981. isStatic = true;
  1982. }
  1983. }
  1984. const variance = this.flowParseVariance();
  1985. if (this.eat(types$1.bracketL)) {
  1986. if (protoStart != null) {
  1987. this.unexpected(protoStart);
  1988. }
  1989. if (this.eat(types$1.bracketL)) {
  1990. if (variance) {
  1991. this.unexpected(variance.start);
  1992. }
  1993. nodeStart.internalSlots.push(this.flowParseObjectTypeInternalSlot(node, isStatic));
  1994. } else {
  1995. nodeStart.indexers.push(this.flowParseObjectTypeIndexer(node, isStatic, variance));
  1996. }
  1997. } else if (this.match(types$1.parenL) || this.isRelational("<")) {
  1998. if (protoStart != null) {
  1999. this.unexpected(protoStart);
  2000. }
  2001. if (variance) {
  2002. this.unexpected(variance.start);
  2003. }
  2004. nodeStart.callProperties.push(this.flowParseObjectTypeCallProperty(node, isStatic));
  2005. } else {
  2006. let kind = "init";
  2007. if (this.isContextual("get") || this.isContextual("set")) {
  2008. const lookahead = this.lookahead();
  2009. if (lookahead.type === types$1.name || lookahead.type === types$1.string || lookahead.type === types$1.num) {
  2010. kind = this.state.value;
  2011. this.next();
  2012. }
  2013. }
  2014. const propOrInexact = this.flowParseObjectTypeProperty(node, isStatic, protoStart, variance, kind, allowSpread, allowInexact != null ? allowInexact : !exact);
  2015. if (propOrInexact === null) {
  2016. inexact = true;
  2017. inexactStart = this.state.lastTokStart;
  2018. } else {
  2019. nodeStart.properties.push(propOrInexact);
  2020. }
  2021. }
  2022. this.flowObjectTypeSemicolon();
  2023. if (inexactStart && !this.match(types$1.braceR) && !this.match(types$1.braceBarR)) {
  2024. this.raise(inexactStart, FlowErrors.UnexpectedExplicitInexactInObject);
  2025. }
  2026. }
  2027. this.expect(endDelim);
  2028. if (allowSpread) {
  2029. nodeStart.inexact = inexact;
  2030. }
  2031. const out = this.finishNode(nodeStart, "ObjectTypeAnnotation");
  2032. this.state.inType = oldInType;
  2033. return out;
  2034. }
  2035. flowParseObjectTypeProperty(node, isStatic, protoStart, variance, kind, allowSpread, allowInexact) {
  2036. if (this.eat(types$1.ellipsis)) {
  2037. const isInexactToken = this.match(types$1.comma) || this.match(types$1.semi) || this.match(types$1.braceR) || this.match(types$1.braceBarR);
  2038. if (isInexactToken) {
  2039. if (!allowSpread) {
  2040. this.raise(this.state.lastTokStart, FlowErrors.InexactInsideNonObject);
  2041. } else if (!allowInexact) {
  2042. this.raise(this.state.lastTokStart, FlowErrors.InexactInsideExact);
  2043. }
  2044. if (variance) {
  2045. this.raise(variance.start, FlowErrors.InexactVariance);
  2046. }
  2047. return null;
  2048. }
  2049. if (!allowSpread) {
  2050. this.raise(this.state.lastTokStart, FlowErrors.UnexpectedSpreadType);
  2051. }
  2052. if (protoStart != null) {
  2053. this.unexpected(protoStart);
  2054. }
  2055. if (variance) {
  2056. this.raise(variance.start, FlowErrors.SpreadVariance);
  2057. }
  2058. node.argument = this.flowParseType();
  2059. return this.finishNode(node, "ObjectTypeSpreadProperty");
  2060. } else {
  2061. node.key = this.flowParseObjectPropertyKey();
  2062. node.static = isStatic;
  2063. node.proto = protoStart != null;
  2064. node.kind = kind;
  2065. let optional = false;
  2066. if (this.isRelational("<") || this.match(types$1.parenL)) {
  2067. node.method = true;
  2068. if (protoStart != null) {
  2069. this.unexpected(protoStart);
  2070. }
  2071. if (variance) {
  2072. this.unexpected(variance.start);
  2073. }
  2074. node.value = this.flowParseObjectTypeMethodish(this.startNodeAt(node.start, node.loc.start));
  2075. if (kind === "get" || kind === "set") {
  2076. this.flowCheckGetterSetterParams(node);
  2077. }
  2078. if (!allowSpread && node.key.name === "constructor" && node.value.this) {
  2079. this.raise(node.value.this.start, FlowErrors.ThisParamBannedInConstructor);
  2080. }
  2081. } else {
  2082. if (kind !== "init") this.unexpected();
  2083. node.method = false;
  2084. if (this.eat(types$1.question)) {
  2085. optional = true;
  2086. }
  2087. node.value = this.flowParseTypeInitialiser();
  2088. node.variance = variance;
  2089. }
  2090. node.optional = optional;
  2091. return this.finishNode(node, "ObjectTypeProperty");
  2092. }
  2093. }
  2094. flowCheckGetterSetterParams(property) {
  2095. const paramCount = property.kind === "get" ? 0 : 1;
  2096. const start = property.start;
  2097. const length = property.value.params.length + (property.value.rest ? 1 : 0);
  2098. if (property.value.this) {
  2099. this.raise(property.value.this.start, property.kind === "get" ? FlowErrors.GetterMayNotHaveThisParam : FlowErrors.SetterMayNotHaveThisParam);
  2100. }
  2101. if (length !== paramCount) {
  2102. if (property.kind === "get") {
  2103. this.raise(start, ErrorMessages.BadGetterArity);
  2104. } else {
  2105. this.raise(start, ErrorMessages.BadSetterArity);
  2106. }
  2107. }
  2108. if (property.kind === "set" && property.value.rest) {
  2109. this.raise(start, ErrorMessages.BadSetterRestParameter);
  2110. }
  2111. }
  2112. flowObjectTypeSemicolon() {
  2113. if (!this.eat(types$1.semi) && !this.eat(types$1.comma) && !this.match(types$1.braceR) && !this.match(types$1.braceBarR)) {
  2114. this.unexpected();
  2115. }
  2116. }
  2117. flowParseQualifiedTypeIdentifier(startPos, startLoc, id) {
  2118. startPos = startPos || this.state.start;
  2119. startLoc = startLoc || this.state.startLoc;
  2120. let node = id || this.flowParseRestrictedIdentifier(true);
  2121. while (this.eat(types$1.dot)) {
  2122. const node2 = this.startNodeAt(startPos, startLoc);
  2123. node2.qualification = node;
  2124. node2.id = this.flowParseRestrictedIdentifier(true);
  2125. node = this.finishNode(node2, "QualifiedTypeIdentifier");
  2126. }
  2127. return node;
  2128. }
  2129. flowParseGenericType(startPos, startLoc, id) {
  2130. const node = this.startNodeAt(startPos, startLoc);
  2131. node.typeParameters = null;
  2132. node.id = this.flowParseQualifiedTypeIdentifier(startPos, startLoc, id);
  2133. if (this.isRelational("<")) {
  2134. node.typeParameters = this.flowParseTypeParameterInstantiation();
  2135. }
  2136. return this.finishNode(node, "GenericTypeAnnotation");
  2137. }
  2138. flowParseTypeofType() {
  2139. const node = this.startNode();
  2140. this.expect(types$1._typeof);
  2141. node.argument = this.flowParsePrimaryType();
  2142. return this.finishNode(node, "TypeofTypeAnnotation");
  2143. }
  2144. flowParseTupleType() {
  2145. const node = this.startNode();
  2146. node.types = [];
  2147. this.expect(types$1.bracketL);
  2148. while (this.state.pos < this.length && !this.match(types$1.bracketR)) {
  2149. node.types.push(this.flowParseType());
  2150. if (this.match(types$1.bracketR)) break;
  2151. this.expect(types$1.comma);
  2152. }
  2153. this.expect(types$1.bracketR);
  2154. return this.finishNode(node, "TupleTypeAnnotation");
  2155. }
  2156. flowParseFunctionTypeParam(first) {
  2157. let name = null;
  2158. let optional = false;
  2159. let typeAnnotation = null;
  2160. const node = this.startNode();
  2161. const lh = this.lookahead();
  2162. const isThis = this.state.type === types$1._this;
  2163. if (lh.type === types$1.colon || lh.type === types$1.question) {
  2164. if (isThis && !first) {
  2165. this.raise(node.start, FlowErrors.ThisParamMustBeFirst);
  2166. }
  2167. name = this.parseIdentifier(isThis);
  2168. if (this.eat(types$1.question)) {
  2169. optional = true;
  2170. if (isThis) {
  2171. this.raise(node.start, FlowErrors.ThisParamMayNotBeOptional);
  2172. }
  2173. }
  2174. typeAnnotation = this.flowParseTypeInitialiser();
  2175. } else {
  2176. typeAnnotation = this.flowParseType();
  2177. }
  2178. node.name = name;
  2179. node.optional = optional;
  2180. node.typeAnnotation = typeAnnotation;
  2181. return this.finishNode(node, "FunctionTypeParam");
  2182. }
  2183. reinterpretTypeAsFunctionTypeParam(type) {
  2184. const node = this.startNodeAt(type.start, type.loc.start);
  2185. node.name = null;
  2186. node.optional = false;
  2187. node.typeAnnotation = type;
  2188. return this.finishNode(node, "FunctionTypeParam");
  2189. }
  2190. flowParseFunctionTypeParams(params = []) {
  2191. let rest = null;
  2192. let _this = null;
  2193. if (this.match(types$1._this)) {
  2194. _this = this.flowParseFunctionTypeParam(true);
  2195. _this.name = null;
  2196. if (!this.match(types$1.parenR)) {
  2197. this.expect(types$1.comma);
  2198. }
  2199. }
  2200. while (!this.match(types$1.parenR) && !this.match(types$1.ellipsis)) {
  2201. params.push(this.flowParseFunctionTypeParam(false));
  2202. if (!this.match(types$1.parenR)) {
  2203. this.expect(types$1.comma);
  2204. }
  2205. }
  2206. if (this.eat(types$1.ellipsis)) {
  2207. rest = this.flowParseFunctionTypeParam(false);
  2208. }
  2209. return {
  2210. params,
  2211. rest,
  2212. _this
  2213. };
  2214. }
  2215. flowIdentToTypeAnnotation(startPos, startLoc, node, id) {
  2216. switch (id.name) {
  2217. case "any":
  2218. return this.finishNode(node, "AnyTypeAnnotation");
  2219. case "bool":
  2220. case "boolean":
  2221. return this.finishNode(node, "BooleanTypeAnnotation");
  2222. case "mixed":
  2223. return this.finishNode(node, "MixedTypeAnnotation");
  2224. case "empty":
  2225. return this.finishNode(node, "EmptyTypeAnnotation");
  2226. case "number":
  2227. return this.finishNode(node, "NumberTypeAnnotation");
  2228. case "string":
  2229. return this.finishNode(node, "StringTypeAnnotation");
  2230. case "symbol":
  2231. return this.finishNode(node, "SymbolTypeAnnotation");
  2232. default:
  2233. this.checkNotUnderscore(id.name);
  2234. return this.flowParseGenericType(startPos, startLoc, id);
  2235. }
  2236. }
  2237. flowParsePrimaryType() {
  2238. const startPos = this.state.start;
  2239. const startLoc = this.state.startLoc;
  2240. const node = this.startNode();
  2241. let tmp;
  2242. let type;
  2243. let isGroupedType = false;
  2244. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  2245. switch (this.state.type) {
  2246. case types$1.name:
  2247. if (this.isContextual("interface")) {
  2248. return this.flowParseInterfaceType();
  2249. }
  2250. return this.flowIdentToTypeAnnotation(startPos, startLoc, node, this.parseIdentifier());
  2251. case types$1.braceL:
  2252. return this.flowParseObjectType({
  2253. allowStatic: false,
  2254. allowExact: false,
  2255. allowSpread: true,
  2256. allowProto: false,
  2257. allowInexact: true
  2258. });
  2259. case types$1.braceBarL:
  2260. return this.flowParseObjectType({
  2261. allowStatic: false,
  2262. allowExact: true,
  2263. allowSpread: true,
  2264. allowProto: false,
  2265. allowInexact: false
  2266. });
  2267. case types$1.bracketL:
  2268. this.state.noAnonFunctionType = false;
  2269. type = this.flowParseTupleType();
  2270. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  2271. return type;
  2272. case types$1.relational:
  2273. if (this.state.value === "<") {
  2274. node.typeParameters = this.flowParseTypeParameterDeclaration();
  2275. this.expect(types$1.parenL);
  2276. tmp = this.flowParseFunctionTypeParams();
  2277. node.params = tmp.params;
  2278. node.rest = tmp.rest;
  2279. node.this = tmp._this;
  2280. this.expect(types$1.parenR);
  2281. this.expect(types$1.arrow);
  2282. node.returnType = this.flowParseType();
  2283. return this.finishNode(node, "FunctionTypeAnnotation");
  2284. }
  2285. break;
  2286. case types$1.parenL:
  2287. this.next();
  2288. if (!this.match(types$1.parenR) && !this.match(types$1.ellipsis)) {
  2289. if (this.match(types$1.name) || this.match(types$1._this)) {
  2290. const token = this.lookahead().type;
  2291. isGroupedType = token !== types$1.question && token !== types$1.colon;
  2292. } else {
  2293. isGroupedType = true;
  2294. }
  2295. }
  2296. if (isGroupedType) {
  2297. this.state.noAnonFunctionType = false;
  2298. type = this.flowParseType();
  2299. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  2300. if (this.state.noAnonFunctionType || !(this.match(types$1.comma) || this.match(types$1.parenR) && this.lookahead().type === types$1.arrow)) {
  2301. this.expect(types$1.parenR);
  2302. return type;
  2303. } else {
  2304. this.eat(types$1.comma);
  2305. }
  2306. }
  2307. if (type) {
  2308. tmp = this.flowParseFunctionTypeParams([this.reinterpretTypeAsFunctionTypeParam(type)]);
  2309. } else {
  2310. tmp = this.flowParseFunctionTypeParams();
  2311. }
  2312. node.params = tmp.params;
  2313. node.rest = tmp.rest;
  2314. node.this = tmp._this;
  2315. this.expect(types$1.parenR);
  2316. this.expect(types$1.arrow);
  2317. node.returnType = this.flowParseType();
  2318. node.typeParameters = null;
  2319. return this.finishNode(node, "FunctionTypeAnnotation");
  2320. case types$1.string:
  2321. return this.parseLiteral(this.state.value, "StringLiteralTypeAnnotation");
  2322. case types$1._true:
  2323. case types$1._false:
  2324. node.value = this.match(types$1._true);
  2325. this.next();
  2326. return this.finishNode(node, "BooleanLiteralTypeAnnotation");
  2327. case types$1.plusMin:
  2328. if (this.state.value === "-") {
  2329. this.next();
  2330. if (this.match(types$1.num)) {
  2331. return this.parseLiteralAtNode(-this.state.value, "NumberLiteralTypeAnnotation", node);
  2332. }
  2333. if (this.match(types$1.bigint)) {
  2334. return this.parseLiteralAtNode(-this.state.value, "BigIntLiteralTypeAnnotation", node);
  2335. }
  2336. throw this.raise(this.state.start, FlowErrors.UnexpectedSubtractionOperand);
  2337. }
  2338. throw this.unexpected();
  2339. case types$1.num:
  2340. return this.parseLiteral(this.state.value, "NumberLiteralTypeAnnotation");
  2341. case types$1.bigint:
  2342. return this.parseLiteral(this.state.value, "BigIntLiteralTypeAnnotation");
  2343. case types$1._void:
  2344. this.next();
  2345. return this.finishNode(node, "VoidTypeAnnotation");
  2346. case types$1._null:
  2347. this.next();
  2348. return this.finishNode(node, "NullLiteralTypeAnnotation");
  2349. case types$1._this:
  2350. this.next();
  2351. return this.finishNode(node, "ThisTypeAnnotation");
  2352. case types$1.star:
  2353. this.next();
  2354. return this.finishNode(node, "ExistsTypeAnnotation");
  2355. default:
  2356. if (this.state.type.keyword === "typeof") {
  2357. return this.flowParseTypeofType();
  2358. } else if (this.state.type.keyword) {
  2359. const label = this.state.type.label;
  2360. this.next();
  2361. return super.createIdentifier(node, label);
  2362. }
  2363. }
  2364. throw this.unexpected();
  2365. }
  2366. flowParsePostfixType() {
  2367. const startPos = this.state.start;
  2368. const startLoc = this.state.startLoc;
  2369. let type = this.flowParsePrimaryType();
  2370. let seenOptionalIndexedAccess = false;
  2371. while ((this.match(types$1.bracketL) || this.match(types$1.questionDot)) && !this.canInsertSemicolon()) {
  2372. const node = this.startNodeAt(startPos, startLoc);
  2373. const optional = this.eat(types$1.questionDot);
  2374. seenOptionalIndexedAccess = seenOptionalIndexedAccess || optional;
  2375. this.expect(types$1.bracketL);
  2376. if (!optional && this.match(types$1.bracketR)) {
  2377. node.elementType = type;
  2378. this.next();
  2379. type = this.finishNode(node, "ArrayTypeAnnotation");
  2380. } else {
  2381. node.objectType = type;
  2382. node.indexType = this.flowParseType();
  2383. this.expect(types$1.bracketR);
  2384. if (seenOptionalIndexedAccess) {
  2385. node.optional = optional;
  2386. type = this.finishNode(node, "OptionalIndexedAccessType");
  2387. } else {
  2388. type = this.finishNode(node, "IndexedAccessType");
  2389. }
  2390. }
  2391. }
  2392. return type;
  2393. }
  2394. flowParsePrefixType() {
  2395. const node = this.startNode();
  2396. if (this.eat(types$1.question)) {
  2397. node.typeAnnotation = this.flowParsePrefixType();
  2398. return this.finishNode(node, "NullableTypeAnnotation");
  2399. } else {
  2400. return this.flowParsePostfixType();
  2401. }
  2402. }
  2403. flowParseAnonFunctionWithoutParens() {
  2404. const param = this.flowParsePrefixType();
  2405. if (!this.state.noAnonFunctionType && this.eat(types$1.arrow)) {
  2406. const node = this.startNodeAt(param.start, param.loc.start);
  2407. node.params = [this.reinterpretTypeAsFunctionTypeParam(param)];
  2408. node.rest = null;
  2409. node.this = null;
  2410. node.returnType = this.flowParseType();
  2411. node.typeParameters = null;
  2412. return this.finishNode(node, "FunctionTypeAnnotation");
  2413. }
  2414. return param;
  2415. }
  2416. flowParseIntersectionType() {
  2417. const node = this.startNode();
  2418. this.eat(types$1.bitwiseAND);
  2419. const type = this.flowParseAnonFunctionWithoutParens();
  2420. node.types = [type];
  2421. while (this.eat(types$1.bitwiseAND)) {
  2422. node.types.push(this.flowParseAnonFunctionWithoutParens());
  2423. }
  2424. return node.types.length === 1 ? type : this.finishNode(node, "IntersectionTypeAnnotation");
  2425. }
  2426. flowParseUnionType() {
  2427. const node = this.startNode();
  2428. this.eat(types$1.bitwiseOR);
  2429. const type = this.flowParseIntersectionType();
  2430. node.types = [type];
  2431. while (this.eat(types$1.bitwiseOR)) {
  2432. node.types.push(this.flowParseIntersectionType());
  2433. }
  2434. return node.types.length === 1 ? type : this.finishNode(node, "UnionTypeAnnotation");
  2435. }
  2436. flowParseType() {
  2437. const oldInType = this.state.inType;
  2438. this.state.inType = true;
  2439. const type = this.flowParseUnionType();
  2440. this.state.inType = oldInType;
  2441. this.state.exprAllowed = this.state.exprAllowed || this.state.noAnonFunctionType;
  2442. return type;
  2443. }
  2444. flowParseTypeOrImplicitInstantiation() {
  2445. if (this.state.type === types$1.name && this.state.value === "_") {
  2446. const startPos = this.state.start;
  2447. const startLoc = this.state.startLoc;
  2448. const node = this.parseIdentifier();
  2449. return this.flowParseGenericType(startPos, startLoc, node);
  2450. } else {
  2451. return this.flowParseType();
  2452. }
  2453. }
  2454. flowParseTypeAnnotation() {
  2455. const node = this.startNode();
  2456. node.typeAnnotation = this.flowParseTypeInitialiser();
  2457. return this.finishNode(node, "TypeAnnotation");
  2458. }
  2459. flowParseTypeAnnotatableIdentifier(allowPrimitiveOverride) {
  2460. const ident = allowPrimitiveOverride ? this.parseIdentifier() : this.flowParseRestrictedIdentifier();
  2461. if (this.match(types$1.colon)) {
  2462. ident.typeAnnotation = this.flowParseTypeAnnotation();
  2463. this.resetEndLocation(ident);
  2464. }
  2465. return ident;
  2466. }
  2467. typeCastToParameter(node) {
  2468. node.expression.typeAnnotation = node.typeAnnotation;
  2469. this.resetEndLocation(node.expression, node.typeAnnotation.end, node.typeAnnotation.loc.end);
  2470. return node.expression;
  2471. }
  2472. flowParseVariance() {
  2473. let variance = null;
  2474. if (this.match(types$1.plusMin)) {
  2475. variance = this.startNode();
  2476. if (this.state.value === "+") {
  2477. variance.kind = "plus";
  2478. } else {
  2479. variance.kind = "minus";
  2480. }
  2481. this.next();
  2482. this.finishNode(variance, "Variance");
  2483. }
  2484. return variance;
  2485. }
  2486. parseFunctionBody(node, allowExpressionBody, isMethod = false) {
  2487. if (allowExpressionBody) {
  2488. return this.forwardNoArrowParamsConversionAt(node, () => super.parseFunctionBody(node, true, isMethod));
  2489. }
  2490. return super.parseFunctionBody(node, false, isMethod);
  2491. }
  2492. parseFunctionBodyAndFinish(node, type, isMethod = false) {
  2493. if (this.match(types$1.colon)) {
  2494. const typeNode = this.startNode();
  2495. [typeNode.typeAnnotation, node.predicate] = this.flowParseTypeAndPredicateInitialiser();
  2496. node.returnType = typeNode.typeAnnotation ? this.finishNode(typeNode, "TypeAnnotation") : null;
  2497. }
  2498. super.parseFunctionBodyAndFinish(node, type, isMethod);
  2499. }
  2500. parseStatement(context, topLevel) {
  2501. if (this.state.strict && this.match(types$1.name) && this.state.value === "interface") {
  2502. const lookahead = this.lookahead();
  2503. if (lookahead.type === types$1.name || isKeyword(lookahead.value)) {
  2504. const node = this.startNode();
  2505. this.next();
  2506. return this.flowParseInterface(node);
  2507. }
  2508. } else if (this.shouldParseEnums() && this.isContextual("enum")) {
  2509. const node = this.startNode();
  2510. this.next();
  2511. return this.flowParseEnumDeclaration(node);
  2512. }
  2513. const stmt = super.parseStatement(context, topLevel);
  2514. if (this.flowPragma === undefined && !this.isValidDirective(stmt)) {
  2515. this.flowPragma = null;
  2516. }
  2517. return stmt;
  2518. }
  2519. parseExpressionStatement(node, expr) {
  2520. if (expr.type === "Identifier") {
  2521. if (expr.name === "declare") {
  2522. 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)) {
  2523. return this.flowParseDeclare(node);
  2524. }
  2525. } else if (this.match(types$1.name)) {
  2526. if (expr.name === "interface") {
  2527. return this.flowParseInterface(node);
  2528. } else if (expr.name === "type") {
  2529. return this.flowParseTypeAlias(node);
  2530. } else if (expr.name === "opaque") {
  2531. return this.flowParseOpaqueType(node, false);
  2532. }
  2533. }
  2534. }
  2535. return super.parseExpressionStatement(node, expr);
  2536. }
  2537. shouldParseExportDeclaration() {
  2538. return this.isContextual("type") || this.isContextual("interface") || this.isContextual("opaque") || this.shouldParseEnums() && this.isContextual("enum") || super.shouldParseExportDeclaration();
  2539. }
  2540. isExportDefaultSpecifier() {
  2541. if (this.match(types$1.name) && (this.state.value === "type" || this.state.value === "interface" || this.state.value === "opaque" || this.shouldParseEnums() && this.state.value === "enum")) {
  2542. return false;
  2543. }
  2544. return super.isExportDefaultSpecifier();
  2545. }
  2546. parseExportDefaultExpression() {
  2547. if (this.shouldParseEnums() && this.isContextual("enum")) {
  2548. const node = this.startNode();
  2549. this.next();
  2550. return this.flowParseEnumDeclaration(node);
  2551. }
  2552. return super.parseExportDefaultExpression();
  2553. }
  2554. parseConditional(expr, startPos, startLoc, refNeedsArrowPos) {
  2555. if (!this.match(types$1.question)) return expr;
  2556. if (refNeedsArrowPos) {
  2557. const result = this.tryParse(() => super.parseConditional(expr, startPos, startLoc));
  2558. if (!result.node) {
  2559. refNeedsArrowPos.start = result.error.pos || this.state.start;
  2560. return expr;
  2561. }
  2562. if (result.error) this.state = result.failState;
  2563. return result.node;
  2564. }
  2565. this.expect(types$1.question);
  2566. const state = this.state.clone();
  2567. const originalNoArrowAt = this.state.noArrowAt;
  2568. const node = this.startNodeAt(startPos, startLoc);
  2569. let {
  2570. consequent,
  2571. failed
  2572. } = this.tryParseConditionalConsequent();
  2573. let [valid, invalid] = this.getArrowLikeExpressions(consequent);
  2574. if (failed || invalid.length > 0) {
  2575. const noArrowAt = [...originalNoArrowAt];
  2576. if (invalid.length > 0) {
  2577. this.state = state;
  2578. this.state.noArrowAt = noArrowAt;
  2579. for (let i = 0; i < invalid.length; i++) {
  2580. noArrowAt.push(invalid[i].start);
  2581. }
  2582. ({
  2583. consequent,
  2584. failed
  2585. } = this.tryParseConditionalConsequent());
  2586. [valid, invalid] = this.getArrowLikeExpressions(consequent);
  2587. }
  2588. if (failed && valid.length > 1) {
  2589. this.raise(state.start, FlowErrors.AmbiguousConditionalArrow);
  2590. }
  2591. if (failed && valid.length === 1) {
  2592. this.state = state;
  2593. this.state.noArrowAt = noArrowAt.concat(valid[0].start);
  2594. ({
  2595. consequent,
  2596. failed
  2597. } = this.tryParseConditionalConsequent());
  2598. }
  2599. }
  2600. this.getArrowLikeExpressions(consequent, true);
  2601. this.state.noArrowAt = originalNoArrowAt;
  2602. this.expect(types$1.colon);
  2603. node.test = expr;
  2604. node.consequent = consequent;
  2605. node.alternate = this.forwardNoArrowParamsConversionAt(node, () => this.parseMaybeAssign(undefined, undefined, undefined));
  2606. return this.finishNode(node, "ConditionalExpression");
  2607. }
  2608. tryParseConditionalConsequent() {
  2609. this.state.noArrowParamsConversionAt.push(this.state.start);
  2610. const consequent = this.parseMaybeAssignAllowIn();
  2611. const failed = !this.match(types$1.colon);
  2612. this.state.noArrowParamsConversionAt.pop();
  2613. return {
  2614. consequent,
  2615. failed
  2616. };
  2617. }
  2618. getArrowLikeExpressions(node, disallowInvalid) {
  2619. const stack = [node];
  2620. const arrows = [];
  2621. while (stack.length !== 0) {
  2622. const node = stack.pop();
  2623. if (node.type === "ArrowFunctionExpression") {
  2624. if (node.typeParameters || !node.returnType) {
  2625. this.finishArrowValidation(node);
  2626. } else {
  2627. arrows.push(node);
  2628. }
  2629. stack.push(node.body);
  2630. } else if (node.type === "ConditionalExpression") {
  2631. stack.push(node.consequent);
  2632. stack.push(node.alternate);
  2633. }
  2634. }
  2635. if (disallowInvalid) {
  2636. arrows.forEach(node => this.finishArrowValidation(node));
  2637. return [arrows, []];
  2638. }
  2639. return partition(arrows, node => node.params.every(param => this.isAssignable(param, true)));
  2640. }
  2641. finishArrowValidation(node) {
  2642. var _node$extra;
  2643. this.toAssignableList(node.params, (_node$extra = node.extra) == null ? void 0 : _node$extra.trailingComma, false);
  2644. this.scope.enter(SCOPE_FUNCTION | SCOPE_ARROW);
  2645. super.checkParams(node, false, true);
  2646. this.scope.exit();
  2647. }
  2648. forwardNoArrowParamsConversionAt(node, parse) {
  2649. let result;
  2650. if (this.state.noArrowParamsConversionAt.indexOf(node.start) !== -1) {
  2651. this.state.noArrowParamsConversionAt.push(this.state.start);
  2652. result = parse();
  2653. this.state.noArrowParamsConversionAt.pop();
  2654. } else {
  2655. result = parse();
  2656. }
  2657. return result;
  2658. }
  2659. parseParenItem(node, startPos, startLoc) {
  2660. node = super.parseParenItem(node, startPos, startLoc);
  2661. if (this.eat(types$1.question)) {
  2662. node.optional = true;
  2663. this.resetEndLocation(node);
  2664. }
  2665. if (this.match(types$1.colon)) {
  2666. const typeCastNode = this.startNodeAt(startPos, startLoc);
  2667. typeCastNode.expression = node;
  2668. typeCastNode.typeAnnotation = this.flowParseTypeAnnotation();
  2669. return this.finishNode(typeCastNode, "TypeCastExpression");
  2670. }
  2671. return node;
  2672. }
  2673. assertModuleNodeAllowed(node) {
  2674. if (node.type === "ImportDeclaration" && (node.importKind === "type" || node.importKind === "typeof") || node.type === "ExportNamedDeclaration" && node.exportKind === "type" || node.type === "ExportAllDeclaration" && node.exportKind === "type") {
  2675. return;
  2676. }
  2677. super.assertModuleNodeAllowed(node);
  2678. }
  2679. parseExport(node) {
  2680. const decl = super.parseExport(node);
  2681. if (decl.type === "ExportNamedDeclaration" || decl.type === "ExportAllDeclaration") {
  2682. decl.exportKind = decl.exportKind || "value";
  2683. }
  2684. return decl;
  2685. }
  2686. parseExportDeclaration(node) {
  2687. if (this.isContextual("type")) {
  2688. node.exportKind = "type";
  2689. const declarationNode = this.startNode();
  2690. this.next();
  2691. if (this.match(types$1.braceL)) {
  2692. node.specifiers = this.parseExportSpecifiers();
  2693. this.parseExportFrom(node);
  2694. return null;
  2695. } else {
  2696. return this.flowParseTypeAlias(declarationNode);
  2697. }
  2698. } else if (this.isContextual("opaque")) {
  2699. node.exportKind = "type";
  2700. const declarationNode = this.startNode();
  2701. this.next();
  2702. return this.flowParseOpaqueType(declarationNode, false);
  2703. } else if (this.isContextual("interface")) {
  2704. node.exportKind = "type";
  2705. const declarationNode = this.startNode();
  2706. this.next();
  2707. return this.flowParseInterface(declarationNode);
  2708. } else if (this.shouldParseEnums() && this.isContextual("enum")) {
  2709. node.exportKind = "value";
  2710. const declarationNode = this.startNode();
  2711. this.next();
  2712. return this.flowParseEnumDeclaration(declarationNode);
  2713. } else {
  2714. return super.parseExportDeclaration(node);
  2715. }
  2716. }
  2717. eatExportStar(node) {
  2718. if (super.eatExportStar(...arguments)) return true;
  2719. if (this.isContextual("type") && this.lookahead().type === types$1.star) {
  2720. node.exportKind = "type";
  2721. this.next();
  2722. this.next();
  2723. return true;
  2724. }
  2725. return false;
  2726. }
  2727. maybeParseExportNamespaceSpecifier(node) {
  2728. const pos = this.state.start;
  2729. const hasNamespace = super.maybeParseExportNamespaceSpecifier(node);
  2730. if (hasNamespace && node.exportKind === "type") {
  2731. this.unexpected(pos);
  2732. }
  2733. return hasNamespace;
  2734. }
  2735. parseClassId(node, isStatement, optionalId) {
  2736. super.parseClassId(node, isStatement, optionalId);
  2737. if (this.isRelational("<")) {
  2738. node.typeParameters = this.flowParseTypeParameterDeclaration();
  2739. }
  2740. }
  2741. parseClassMember(classBody, member, state) {
  2742. const pos = this.state.start;
  2743. if (this.isContextual("declare")) {
  2744. if (this.parseClassMemberFromModifier(classBody, member)) {
  2745. return;
  2746. }
  2747. member.declare = true;
  2748. }
  2749. super.parseClassMember(classBody, member, state);
  2750. if (member.declare) {
  2751. if (member.type !== "ClassProperty" && member.type !== "ClassPrivateProperty" && member.type !== "PropertyDefinition") {
  2752. this.raise(pos, FlowErrors.DeclareClassElement);
  2753. } else if (member.value) {
  2754. this.raise(member.value.start, FlowErrors.DeclareClassFieldInitializer);
  2755. }
  2756. }
  2757. }
  2758. isIterator(word) {
  2759. return word === "iterator" || word === "asyncIterator";
  2760. }
  2761. readIterator() {
  2762. const word = super.readWord1();
  2763. const fullWord = "@@" + word;
  2764. if (!this.isIterator(word) || !this.state.inType) {
  2765. this.raise(this.state.pos, ErrorMessages.InvalidIdentifier, fullWord);
  2766. }
  2767. this.finishToken(types$1.name, fullWord);
  2768. }
  2769. getTokenFromCode(code) {
  2770. const next = this.input.charCodeAt(this.state.pos + 1);
  2771. if (code === 123 && next === 124) {
  2772. return this.finishOp(types$1.braceBarL, 2);
  2773. } else if (this.state.inType && (code === 62 || code === 60)) {
  2774. return this.finishOp(types$1.relational, 1);
  2775. } else if (this.state.inType && code === 63) {
  2776. if (next === 46) {
  2777. return this.finishOp(types$1.questionDot, 2);
  2778. }
  2779. return this.finishOp(types$1.question, 1);
  2780. } else if (isIteratorStart(code, next)) {
  2781. this.state.pos += 2;
  2782. return this.readIterator();
  2783. } else {
  2784. return super.getTokenFromCode(code);
  2785. }
  2786. }
  2787. isAssignable(node, isBinding) {
  2788. switch (node.type) {
  2789. case "Identifier":
  2790. case "ObjectPattern":
  2791. case "ArrayPattern":
  2792. case "AssignmentPattern":
  2793. return true;
  2794. case "ObjectExpression":
  2795. {
  2796. const last = node.properties.length - 1;
  2797. return node.properties.every((prop, i) => {
  2798. return prop.type !== "ObjectMethod" && (i === last || prop.type === "SpreadElement") && this.isAssignable(prop);
  2799. });
  2800. }
  2801. case "ObjectProperty":
  2802. return this.isAssignable(node.value);
  2803. case "SpreadElement":
  2804. return this.isAssignable(node.argument);
  2805. case "ArrayExpression":
  2806. return node.elements.every(element => this.isAssignable(element));
  2807. case "AssignmentExpression":
  2808. return node.operator === "=";
  2809. case "ParenthesizedExpression":
  2810. case "TypeCastExpression":
  2811. return this.isAssignable(node.expression);
  2812. case "MemberExpression":
  2813. case "OptionalMemberExpression":
  2814. return !isBinding;
  2815. default:
  2816. return false;
  2817. }
  2818. }
  2819. toAssignable(node, isLHS = false) {
  2820. if (node.type === "TypeCastExpression") {
  2821. return super.toAssignable(this.typeCastToParameter(node), isLHS);
  2822. } else {
  2823. return super.toAssignable(node, isLHS);
  2824. }
  2825. }
  2826. toAssignableList(exprList, trailingCommaPos, isLHS) {
  2827. for (let i = 0; i < exprList.length; i++) {
  2828. const expr = exprList[i];
  2829. if ((expr == null ? void 0 : expr.type) === "TypeCastExpression") {
  2830. exprList[i] = this.typeCastToParameter(expr);
  2831. }
  2832. }
  2833. return super.toAssignableList(exprList, trailingCommaPos, isLHS);
  2834. }
  2835. toReferencedList(exprList, isParenthesizedExpr) {
  2836. for (let i = 0; i < exprList.length; i++) {
  2837. var _expr$extra;
  2838. const expr = exprList[i];
  2839. if (expr && expr.type === "TypeCastExpression" && !((_expr$extra = expr.extra) != null && _expr$extra.parenthesized) && (exprList.length > 1 || !isParenthesizedExpr)) {
  2840. this.raise(expr.typeAnnotation.start, FlowErrors.TypeCastInPattern);
  2841. }
  2842. }
  2843. return exprList;
  2844. }
  2845. parseArrayLike(close, canBePattern, isTuple, refExpressionErrors) {
  2846. const node = super.parseArrayLike(close, canBePattern, isTuple, refExpressionErrors);
  2847. if (canBePattern && !this.state.maybeInArrowParameters) {
  2848. this.toReferencedList(node.elements);
  2849. }
  2850. return node;
  2851. }
  2852. checkLVal(expr, ...args) {
  2853. if (expr.type !== "TypeCastExpression") {
  2854. return super.checkLVal(expr, ...args);
  2855. }
  2856. }
  2857. parseClassProperty(node) {
  2858. if (this.match(types$1.colon)) {
  2859. node.typeAnnotation = this.flowParseTypeAnnotation();
  2860. }
  2861. return super.parseClassProperty(node);
  2862. }
  2863. parseClassPrivateProperty(node) {
  2864. if (this.match(types$1.colon)) {
  2865. node.typeAnnotation = this.flowParseTypeAnnotation();
  2866. }
  2867. return super.parseClassPrivateProperty(node);
  2868. }
  2869. isClassMethod() {
  2870. return this.isRelational("<") || super.isClassMethod();
  2871. }
  2872. isClassProperty() {
  2873. return this.match(types$1.colon) || super.isClassProperty();
  2874. }
  2875. isNonstaticConstructor(method) {
  2876. return !this.match(types$1.colon) && super.isNonstaticConstructor(method);
  2877. }
  2878. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  2879. if (method.variance) {
  2880. this.unexpected(method.variance.start);
  2881. }
  2882. delete method.variance;
  2883. if (this.isRelational("<")) {
  2884. method.typeParameters = this.flowParseTypeParameterDeclaration();
  2885. }
  2886. super.pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper);
  2887. if (method.params && isConstructor) {
  2888. const params = method.params;
  2889. if (params.length > 0 && this.isThisParam(params[0])) {
  2890. this.raise(method.start, FlowErrors.ThisParamBannedInConstructor);
  2891. }
  2892. } else if (method.type === "MethodDefinition" && isConstructor && method.value.params) {
  2893. const params = method.value.params;
  2894. if (params.length > 0 && this.isThisParam(params[0])) {
  2895. this.raise(method.start, FlowErrors.ThisParamBannedInConstructor);
  2896. }
  2897. }
  2898. }
  2899. pushClassPrivateMethod(classBody, method, isGenerator, isAsync) {
  2900. if (method.variance) {
  2901. this.unexpected(method.variance.start);
  2902. }
  2903. delete method.variance;
  2904. if (this.isRelational("<")) {
  2905. method.typeParameters = this.flowParseTypeParameterDeclaration();
  2906. }
  2907. super.pushClassPrivateMethod(classBody, method, isGenerator, isAsync);
  2908. }
  2909. parseClassSuper(node) {
  2910. super.parseClassSuper(node);
  2911. if (node.superClass && this.isRelational("<")) {
  2912. node.superTypeParameters = this.flowParseTypeParameterInstantiation();
  2913. }
  2914. if (this.isContextual("implements")) {
  2915. this.next();
  2916. const implemented = node.implements = [];
  2917. do {
  2918. const node = this.startNode();
  2919. node.id = this.flowParseRestrictedIdentifier(true);
  2920. if (this.isRelational("<")) {
  2921. node.typeParameters = this.flowParseTypeParameterInstantiation();
  2922. } else {
  2923. node.typeParameters = null;
  2924. }
  2925. implemented.push(this.finishNode(node, "ClassImplements"));
  2926. } while (this.eat(types$1.comma));
  2927. }
  2928. }
  2929. checkGetterSetterParams(method) {
  2930. super.checkGetterSetterParams(method);
  2931. const params = this.getObjectOrClassMethodParams(method);
  2932. if (params.length > 0) {
  2933. const param = params[0];
  2934. if (this.isThisParam(param) && method.kind === "get") {
  2935. this.raise(param.start, FlowErrors.GetterMayNotHaveThisParam);
  2936. } else if (this.isThisParam(param)) {
  2937. this.raise(param.start, FlowErrors.SetterMayNotHaveThisParam);
  2938. }
  2939. }
  2940. }
  2941. parsePropertyName(node, isPrivateNameAllowed) {
  2942. const variance = this.flowParseVariance();
  2943. const key = super.parsePropertyName(node, isPrivateNameAllowed);
  2944. node.variance = variance;
  2945. return key;
  2946. }
  2947. parseObjPropValue(prop, startPos, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors) {
  2948. if (prop.variance) {
  2949. this.unexpected(prop.variance.start);
  2950. }
  2951. delete prop.variance;
  2952. let typeParameters;
  2953. if (this.isRelational("<") && !isAccessor) {
  2954. typeParameters = this.flowParseTypeParameterDeclaration();
  2955. if (!this.match(types$1.parenL)) this.unexpected();
  2956. }
  2957. super.parseObjPropValue(prop, startPos, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors);
  2958. if (typeParameters) {
  2959. (prop.value || prop).typeParameters = typeParameters;
  2960. }
  2961. }
  2962. parseAssignableListItemTypes(param) {
  2963. if (this.eat(types$1.question)) {
  2964. if (param.type !== "Identifier") {
  2965. this.raise(param.start, FlowErrors.OptionalBindingPattern);
  2966. }
  2967. if (this.isThisParam(param)) {
  2968. this.raise(param.start, FlowErrors.ThisParamMayNotBeOptional);
  2969. }
  2970. param.optional = true;
  2971. }
  2972. if (this.match(types$1.colon)) {
  2973. param.typeAnnotation = this.flowParseTypeAnnotation();
  2974. } else if (this.isThisParam(param)) {
  2975. this.raise(param.start, FlowErrors.ThisParamAnnotationRequired);
  2976. }
  2977. if (this.match(types$1.eq) && this.isThisParam(param)) {
  2978. this.raise(param.start, FlowErrors.ThisParamNoDefault);
  2979. }
  2980. this.resetEndLocation(param);
  2981. return param;
  2982. }
  2983. parseMaybeDefault(startPos, startLoc, left) {
  2984. const node = super.parseMaybeDefault(startPos, startLoc, left);
  2985. if (node.type === "AssignmentPattern" && node.typeAnnotation && node.right.start < node.typeAnnotation.start) {
  2986. this.raise(node.typeAnnotation.start, FlowErrors.TypeBeforeInitializer);
  2987. }
  2988. return node;
  2989. }
  2990. shouldParseDefaultImport(node) {
  2991. if (!hasTypeImportKind(node)) {
  2992. return super.shouldParseDefaultImport(node);
  2993. }
  2994. return isMaybeDefaultImport(this.state);
  2995. }
  2996. parseImportSpecifierLocal(node, specifier, type, contextDescription) {
  2997. specifier.local = hasTypeImportKind(node) ? this.flowParseRestrictedIdentifier(true, true) : this.parseIdentifier();
  2998. this.checkLVal(specifier.local, contextDescription, BIND_LEXICAL);
  2999. node.specifiers.push(this.finishNode(specifier, type));
  3000. }
  3001. maybeParseDefaultImportSpecifier(node) {
  3002. node.importKind = "value";
  3003. let kind = null;
  3004. if (this.match(types$1._typeof)) {
  3005. kind = "typeof";
  3006. } else if (this.isContextual("type")) {
  3007. kind = "type";
  3008. }
  3009. if (kind) {
  3010. const lh = this.lookahead();
  3011. if (kind === "type" && lh.type === types$1.star) {
  3012. this.unexpected(lh.start);
  3013. }
  3014. if (isMaybeDefaultImport(lh) || lh.type === types$1.braceL || lh.type === types$1.star) {
  3015. this.next();
  3016. node.importKind = kind;
  3017. }
  3018. }
  3019. return super.maybeParseDefaultImportSpecifier(node);
  3020. }
  3021. parseImportSpecifier(node) {
  3022. const specifier = this.startNode();
  3023. const firstIdentIsString = this.match(types$1.string);
  3024. const firstIdent = this.parseModuleExportName();
  3025. let specifierTypeKind = null;
  3026. if (firstIdent.type === "Identifier") {
  3027. if (firstIdent.name === "type") {
  3028. specifierTypeKind = "type";
  3029. } else if (firstIdent.name === "typeof") {
  3030. specifierTypeKind = "typeof";
  3031. }
  3032. }
  3033. let isBinding = false;
  3034. if (this.isContextual("as") && !this.isLookaheadContextual("as")) {
  3035. const as_ident = this.parseIdentifier(true);
  3036. if (specifierTypeKind !== null && !this.match(types$1.name) && !this.state.type.keyword) {
  3037. specifier.imported = as_ident;
  3038. specifier.importKind = specifierTypeKind;
  3039. specifier.local = as_ident.__clone();
  3040. } else {
  3041. specifier.imported = firstIdent;
  3042. specifier.importKind = null;
  3043. specifier.local = this.parseIdentifier();
  3044. }
  3045. } else if (specifierTypeKind !== null && (this.match(types$1.name) || this.state.type.keyword)) {
  3046. specifier.imported = this.parseIdentifier(true);
  3047. specifier.importKind = specifierTypeKind;
  3048. if (this.eatContextual("as")) {
  3049. specifier.local = this.parseIdentifier();
  3050. } else {
  3051. isBinding = true;
  3052. specifier.local = specifier.imported.__clone();
  3053. }
  3054. } else {
  3055. if (firstIdentIsString) {
  3056. throw this.raise(specifier.start, ErrorMessages.ImportBindingIsString, firstIdent.value);
  3057. }
  3058. isBinding = true;
  3059. specifier.imported = firstIdent;
  3060. specifier.importKind = null;
  3061. specifier.local = specifier.imported.__clone();
  3062. }
  3063. const nodeIsTypeImport = hasTypeImportKind(node);
  3064. const specifierIsTypeImport = hasTypeImportKind(specifier);
  3065. if (nodeIsTypeImport && specifierIsTypeImport) {
  3066. this.raise(specifier.start, FlowErrors.ImportTypeShorthandOnlyInPureImport);
  3067. }
  3068. if (nodeIsTypeImport || specifierIsTypeImport) {
  3069. this.checkReservedType(specifier.local.name, specifier.local.start, true);
  3070. }
  3071. if (isBinding && !nodeIsTypeImport && !specifierIsTypeImport) {
  3072. this.checkReservedWord(specifier.local.name, specifier.start, true, true);
  3073. }
  3074. this.checkLVal(specifier.local, "import specifier", BIND_LEXICAL);
  3075. node.specifiers.push(this.finishNode(specifier, "ImportSpecifier"));
  3076. }
  3077. parseBindingAtom() {
  3078. switch (this.state.type) {
  3079. case types$1._this:
  3080. return this.parseIdentifier(true);
  3081. default:
  3082. return super.parseBindingAtom();
  3083. }
  3084. }
  3085. parseFunctionParams(node, allowModifiers) {
  3086. const kind = node.kind;
  3087. if (kind !== "get" && kind !== "set" && this.isRelational("<")) {
  3088. node.typeParameters = this.flowParseTypeParameterDeclaration();
  3089. }
  3090. super.parseFunctionParams(node, allowModifiers);
  3091. }
  3092. parseVarId(decl, kind) {
  3093. super.parseVarId(decl, kind);
  3094. if (this.match(types$1.colon)) {
  3095. decl.id.typeAnnotation = this.flowParseTypeAnnotation();
  3096. this.resetEndLocation(decl.id);
  3097. }
  3098. }
  3099. parseAsyncArrowFromCallExpression(node, call) {
  3100. if (this.match(types$1.colon)) {
  3101. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  3102. this.state.noAnonFunctionType = true;
  3103. node.returnType = this.flowParseTypeAnnotation();
  3104. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  3105. }
  3106. return super.parseAsyncArrowFromCallExpression(node, call);
  3107. }
  3108. shouldParseAsyncArrow() {
  3109. return this.match(types$1.colon) || super.shouldParseAsyncArrow();
  3110. }
  3111. parseMaybeAssign(refExpressionErrors, afterLeftParse, refNeedsArrowPos) {
  3112. var _jsx;
  3113. let state = null;
  3114. let jsx;
  3115. if (this.hasPlugin("jsx") && (this.match(types$1.jsxTagStart) || this.isRelational("<"))) {
  3116. state = this.state.clone();
  3117. jsx = this.tryParse(() => super.parseMaybeAssign(refExpressionErrors, afterLeftParse, refNeedsArrowPos), state);
  3118. if (!jsx.error) return jsx.node;
  3119. const {
  3120. context
  3121. } = this.state;
  3122. if (context[context.length - 1] === types.j_oTag) {
  3123. context.length -= 2;
  3124. } else if (context[context.length - 1] === types.j_expr) {
  3125. context.length -= 1;
  3126. }
  3127. }
  3128. if ((_jsx = jsx) != null && _jsx.error || this.isRelational("<")) {
  3129. var _jsx2, _jsx3;
  3130. state = state || this.state.clone();
  3131. let typeParameters;
  3132. const arrow = this.tryParse(abort => {
  3133. var _arrowExpression$extr;
  3134. typeParameters = this.flowParseTypeParameterDeclaration();
  3135. const arrowExpression = this.forwardNoArrowParamsConversionAt(typeParameters, () => {
  3136. const result = super.parseMaybeAssign(refExpressionErrors, afterLeftParse, refNeedsArrowPos);
  3137. this.resetStartLocationFromNode(result, typeParameters);
  3138. return result;
  3139. });
  3140. if (arrowExpression.type !== "ArrowFunctionExpression" && (_arrowExpression$extr = arrowExpression.extra) != null && _arrowExpression$extr.parenthesized) {
  3141. abort();
  3142. }
  3143. const expr = this.maybeUnwrapTypeCastExpression(arrowExpression);
  3144. expr.typeParameters = typeParameters;
  3145. this.resetStartLocationFromNode(expr, typeParameters);
  3146. return arrowExpression;
  3147. }, state);
  3148. let arrowExpression = null;
  3149. if (arrow.node && this.maybeUnwrapTypeCastExpression(arrow.node).type === "ArrowFunctionExpression") {
  3150. if (!arrow.error && !arrow.aborted) {
  3151. if (arrow.node.async) {
  3152. this.raise(typeParameters.start, FlowErrors.UnexpectedTypeParameterBeforeAsyncArrowFunction);
  3153. }
  3154. return arrow.node;
  3155. }
  3156. arrowExpression = arrow.node;
  3157. }
  3158. if ((_jsx2 = jsx) != null && _jsx2.node) {
  3159. this.state = jsx.failState;
  3160. return jsx.node;
  3161. }
  3162. if (arrowExpression) {
  3163. this.state = arrow.failState;
  3164. return arrowExpression;
  3165. }
  3166. if ((_jsx3 = jsx) != null && _jsx3.thrown) throw jsx.error;
  3167. if (arrow.thrown) throw arrow.error;
  3168. throw this.raise(typeParameters.start, FlowErrors.UnexpectedTokenAfterTypeParameter);
  3169. }
  3170. return super.parseMaybeAssign(refExpressionErrors, afterLeftParse, refNeedsArrowPos);
  3171. }
  3172. parseArrow(node) {
  3173. if (this.match(types$1.colon)) {
  3174. const result = this.tryParse(() => {
  3175. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  3176. this.state.noAnonFunctionType = true;
  3177. const typeNode = this.startNode();
  3178. [typeNode.typeAnnotation, node.predicate] = this.flowParseTypeAndPredicateInitialiser();
  3179. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  3180. if (this.canInsertSemicolon()) this.unexpected();
  3181. if (!this.match(types$1.arrow)) this.unexpected();
  3182. return typeNode;
  3183. });
  3184. if (result.thrown) return null;
  3185. if (result.error) this.state = result.failState;
  3186. node.returnType = result.node.typeAnnotation ? this.finishNode(result.node, "TypeAnnotation") : null;
  3187. }
  3188. return super.parseArrow(node);
  3189. }
  3190. shouldParseArrow() {
  3191. return this.match(types$1.colon) || super.shouldParseArrow();
  3192. }
  3193. setArrowFunctionParameters(node, params) {
  3194. if (this.state.noArrowParamsConversionAt.indexOf(node.start) !== -1) {
  3195. node.params = params;
  3196. } else {
  3197. super.setArrowFunctionParameters(node, params);
  3198. }
  3199. }
  3200. checkParams(node, allowDuplicates, isArrowFunction) {
  3201. if (isArrowFunction && this.state.noArrowParamsConversionAt.indexOf(node.start) !== -1) {
  3202. return;
  3203. }
  3204. for (let i = 0; i < node.params.length; i++) {
  3205. if (this.isThisParam(node.params[i]) && i > 0) {
  3206. this.raise(node.params[i].start, FlowErrors.ThisParamMustBeFirst);
  3207. }
  3208. }
  3209. return super.checkParams(...arguments);
  3210. }
  3211. parseParenAndDistinguishExpression(canBeArrow) {
  3212. return super.parseParenAndDistinguishExpression(canBeArrow && this.state.noArrowAt.indexOf(this.state.start) === -1);
  3213. }
  3214. parseSubscripts(base, startPos, startLoc, noCalls) {
  3215. if (base.type === "Identifier" && base.name === "async" && this.state.noArrowAt.indexOf(startPos) !== -1) {
  3216. this.next();
  3217. const node = this.startNodeAt(startPos, startLoc);
  3218. node.callee = base;
  3219. node.arguments = this.parseCallExpressionArguments(types$1.parenR, false);
  3220. base = this.finishNode(node, "CallExpression");
  3221. } else if (base.type === "Identifier" && base.name === "async" && this.isRelational("<")) {
  3222. const state = this.state.clone();
  3223. const arrow = this.tryParse(abort => this.parseAsyncArrowWithTypeParameters(startPos, startLoc) || abort(), state);
  3224. if (!arrow.error && !arrow.aborted) return arrow.node;
  3225. const result = this.tryParse(() => super.parseSubscripts(base, startPos, startLoc, noCalls), state);
  3226. if (result.node && !result.error) return result.node;
  3227. if (arrow.node) {
  3228. this.state = arrow.failState;
  3229. return arrow.node;
  3230. }
  3231. if (result.node) {
  3232. this.state = result.failState;
  3233. return result.node;
  3234. }
  3235. throw arrow.error || result.error;
  3236. }
  3237. return super.parseSubscripts(base, startPos, startLoc, noCalls);
  3238. }
  3239. parseSubscript(base, startPos, startLoc, noCalls, subscriptState) {
  3240. if (this.match(types$1.questionDot) && this.isLookaheadToken_lt()) {
  3241. subscriptState.optionalChainMember = true;
  3242. if (noCalls) {
  3243. subscriptState.stop = true;
  3244. return base;
  3245. }
  3246. this.next();
  3247. const node = this.startNodeAt(startPos, startLoc);
  3248. node.callee = base;
  3249. node.typeArguments = this.flowParseTypeParameterInstantiation();
  3250. this.expect(types$1.parenL);
  3251. node.arguments = this.parseCallExpressionArguments(types$1.parenR, false);
  3252. node.optional = true;
  3253. return this.finishCallExpression(node, true);
  3254. } else if (!noCalls && this.shouldParseTypes() && this.isRelational("<")) {
  3255. const node = this.startNodeAt(startPos, startLoc);
  3256. node.callee = base;
  3257. const result = this.tryParse(() => {
  3258. node.typeArguments = this.flowParseTypeParameterInstantiationCallOrNew();
  3259. this.expect(types$1.parenL);
  3260. node.arguments = this.parseCallExpressionArguments(types$1.parenR, false);
  3261. if (subscriptState.optionalChainMember) node.optional = false;
  3262. return this.finishCallExpression(node, subscriptState.optionalChainMember);
  3263. });
  3264. if (result.node) {
  3265. if (result.error) this.state = result.failState;
  3266. return result.node;
  3267. }
  3268. }
  3269. return super.parseSubscript(base, startPos, startLoc, noCalls, subscriptState);
  3270. }
  3271. parseNewArguments(node) {
  3272. let targs = null;
  3273. if (this.shouldParseTypes() && this.isRelational("<")) {
  3274. targs = this.tryParse(() => this.flowParseTypeParameterInstantiationCallOrNew()).node;
  3275. }
  3276. node.typeArguments = targs;
  3277. super.parseNewArguments(node);
  3278. }
  3279. parseAsyncArrowWithTypeParameters(startPos, startLoc) {
  3280. const node = this.startNodeAt(startPos, startLoc);
  3281. this.parseFunctionParams(node);
  3282. if (!this.parseArrow(node)) return;
  3283. return this.parseArrowExpression(node, undefined, true);
  3284. }
  3285. readToken_mult_modulo(code) {
  3286. const next = this.input.charCodeAt(this.state.pos + 1);
  3287. if (code === 42 && next === 47 && this.state.hasFlowComment) {
  3288. this.state.hasFlowComment = false;
  3289. this.state.pos += 2;
  3290. this.nextToken();
  3291. return;
  3292. }
  3293. super.readToken_mult_modulo(code);
  3294. }
  3295. readToken_pipe_amp(code) {
  3296. const next = this.input.charCodeAt(this.state.pos + 1);
  3297. if (code === 124 && next === 125) {
  3298. this.finishOp(types$1.braceBarR, 2);
  3299. return;
  3300. }
  3301. super.readToken_pipe_amp(code);
  3302. }
  3303. parseTopLevel(file, program) {
  3304. const fileNode = super.parseTopLevel(file, program);
  3305. if (this.state.hasFlowComment) {
  3306. this.raise(this.state.pos, FlowErrors.UnterminatedFlowComment);
  3307. }
  3308. return fileNode;
  3309. }
  3310. skipBlockComment() {
  3311. if (this.hasPlugin("flowComments") && this.skipFlowComment()) {
  3312. if (this.state.hasFlowComment) {
  3313. this.unexpected(null, FlowErrors.NestedFlowComment);
  3314. }
  3315. this.hasFlowCommentCompletion();
  3316. this.state.pos += this.skipFlowComment();
  3317. this.state.hasFlowComment = true;
  3318. return;
  3319. }
  3320. if (this.state.hasFlowComment) {
  3321. const end = this.input.indexOf("*-/", this.state.pos += 2);
  3322. if (end === -1) {
  3323. throw this.raise(this.state.pos - 2, ErrorMessages.UnterminatedComment);
  3324. }
  3325. this.state.pos = end + 3;
  3326. return;
  3327. }
  3328. super.skipBlockComment();
  3329. }
  3330. skipFlowComment() {
  3331. const {
  3332. pos
  3333. } = this.state;
  3334. let shiftToFirstNonWhiteSpace = 2;
  3335. while ([32, 9].includes(this.input.charCodeAt(pos + shiftToFirstNonWhiteSpace))) {
  3336. shiftToFirstNonWhiteSpace++;
  3337. }
  3338. const ch2 = this.input.charCodeAt(shiftToFirstNonWhiteSpace + pos);
  3339. const ch3 = this.input.charCodeAt(shiftToFirstNonWhiteSpace + pos + 1);
  3340. if (ch2 === 58 && ch3 === 58) {
  3341. return shiftToFirstNonWhiteSpace + 2;
  3342. }
  3343. if (this.input.slice(shiftToFirstNonWhiteSpace + pos, shiftToFirstNonWhiteSpace + pos + 12) === "flow-include") {
  3344. return shiftToFirstNonWhiteSpace + 12;
  3345. }
  3346. if (ch2 === 58 && ch3 !== 58) {
  3347. return shiftToFirstNonWhiteSpace;
  3348. }
  3349. return false;
  3350. }
  3351. hasFlowCommentCompletion() {
  3352. const end = this.input.indexOf("*/", this.state.pos);
  3353. if (end === -1) {
  3354. throw this.raise(this.state.pos, ErrorMessages.UnterminatedComment);
  3355. }
  3356. }
  3357. flowEnumErrorBooleanMemberNotInitialized(pos, {
  3358. enumName,
  3359. memberName
  3360. }) {
  3361. this.raise(pos, FlowErrors.EnumBooleanMemberNotInitialized, memberName, enumName);
  3362. }
  3363. flowEnumErrorInvalidMemberName(pos, {
  3364. enumName,
  3365. memberName
  3366. }) {
  3367. const suggestion = memberName[0].toUpperCase() + memberName.slice(1);
  3368. this.raise(pos, FlowErrors.EnumInvalidMemberName, memberName, suggestion, enumName);
  3369. }
  3370. flowEnumErrorDuplicateMemberName(pos, {
  3371. enumName,
  3372. memberName
  3373. }) {
  3374. this.raise(pos, FlowErrors.EnumDuplicateMemberName, memberName, enumName);
  3375. }
  3376. flowEnumErrorInconsistentMemberValues(pos, {
  3377. enumName
  3378. }) {
  3379. this.raise(pos, FlowErrors.EnumInconsistentMemberValues, enumName);
  3380. }
  3381. flowEnumErrorInvalidExplicitType(pos, {
  3382. enumName,
  3383. suppliedType
  3384. }) {
  3385. return this.raise(pos, suppliedType === null ? FlowErrors.EnumInvalidExplicitTypeUnknownSupplied : FlowErrors.EnumInvalidExplicitType, enumName, suppliedType);
  3386. }
  3387. flowEnumErrorInvalidMemberInitializer(pos, {
  3388. enumName,
  3389. explicitType,
  3390. memberName
  3391. }) {
  3392. let message = null;
  3393. switch (explicitType) {
  3394. case "boolean":
  3395. case "number":
  3396. case "string":
  3397. message = FlowErrors.EnumInvalidMemberInitializerPrimaryType;
  3398. break;
  3399. case "symbol":
  3400. message = FlowErrors.EnumInvalidMemberInitializerSymbolType;
  3401. break;
  3402. default:
  3403. message = FlowErrors.EnumInvalidMemberInitializerUnknownType;
  3404. }
  3405. return this.raise(pos, message, enumName, memberName, explicitType);
  3406. }
  3407. flowEnumErrorNumberMemberNotInitialized(pos, {
  3408. enumName,
  3409. memberName
  3410. }) {
  3411. this.raise(pos, FlowErrors.EnumNumberMemberNotInitialized, enumName, memberName);
  3412. }
  3413. flowEnumErrorStringMemberInconsistentlyInitailized(pos, {
  3414. enumName
  3415. }) {
  3416. this.raise(pos, FlowErrors.EnumStringMemberInconsistentlyInitailized, enumName);
  3417. }
  3418. flowEnumMemberInit() {
  3419. const startPos = this.state.start;
  3420. const endOfInit = () => this.match(types$1.comma) || this.match(types$1.braceR);
  3421. switch (this.state.type) {
  3422. case types$1.num:
  3423. {
  3424. const literal = this.parseNumericLiteral(this.state.value);
  3425. if (endOfInit()) {
  3426. return {
  3427. type: "number",
  3428. pos: literal.start,
  3429. value: literal
  3430. };
  3431. }
  3432. return {
  3433. type: "invalid",
  3434. pos: startPos
  3435. };
  3436. }
  3437. case types$1.string:
  3438. {
  3439. const literal = this.parseStringLiteral(this.state.value);
  3440. if (endOfInit()) {
  3441. return {
  3442. type: "string",
  3443. pos: literal.start,
  3444. value: literal
  3445. };
  3446. }
  3447. return {
  3448. type: "invalid",
  3449. pos: startPos
  3450. };
  3451. }
  3452. case types$1._true:
  3453. case types$1._false:
  3454. {
  3455. const literal = this.parseBooleanLiteral(this.match(types$1._true));
  3456. if (endOfInit()) {
  3457. return {
  3458. type: "boolean",
  3459. pos: literal.start,
  3460. value: literal
  3461. };
  3462. }
  3463. return {
  3464. type: "invalid",
  3465. pos: startPos
  3466. };
  3467. }
  3468. default:
  3469. return {
  3470. type: "invalid",
  3471. pos: startPos
  3472. };
  3473. }
  3474. }
  3475. flowEnumMemberRaw() {
  3476. const pos = this.state.start;
  3477. const id = this.parseIdentifier(true);
  3478. const init = this.eat(types$1.eq) ? this.flowEnumMemberInit() : {
  3479. type: "none",
  3480. pos
  3481. };
  3482. return {
  3483. id,
  3484. init
  3485. };
  3486. }
  3487. flowEnumCheckExplicitTypeMismatch(pos, context, expectedType) {
  3488. const {
  3489. explicitType
  3490. } = context;
  3491. if (explicitType === null) {
  3492. return;
  3493. }
  3494. if (explicitType !== expectedType) {
  3495. this.flowEnumErrorInvalidMemberInitializer(pos, context);
  3496. }
  3497. }
  3498. flowEnumMembers({
  3499. enumName,
  3500. explicitType
  3501. }) {
  3502. const seenNames = new Set();
  3503. const members = {
  3504. booleanMembers: [],
  3505. numberMembers: [],
  3506. stringMembers: [],
  3507. defaultedMembers: []
  3508. };
  3509. let hasUnknownMembers = false;
  3510. while (!this.match(types$1.braceR)) {
  3511. if (this.eat(types$1.ellipsis)) {
  3512. hasUnknownMembers = true;
  3513. break;
  3514. }
  3515. const memberNode = this.startNode();
  3516. const {
  3517. id,
  3518. init
  3519. } = this.flowEnumMemberRaw();
  3520. const memberName = id.name;
  3521. if (memberName === "") {
  3522. continue;
  3523. }
  3524. if (/^[a-z]/.test(memberName)) {
  3525. this.flowEnumErrorInvalidMemberName(id.start, {
  3526. enumName,
  3527. memberName
  3528. });
  3529. }
  3530. if (seenNames.has(memberName)) {
  3531. this.flowEnumErrorDuplicateMemberName(id.start, {
  3532. enumName,
  3533. memberName
  3534. });
  3535. }
  3536. seenNames.add(memberName);
  3537. const context = {
  3538. enumName,
  3539. explicitType,
  3540. memberName
  3541. };
  3542. memberNode.id = id;
  3543. switch (init.type) {
  3544. case "boolean":
  3545. {
  3546. this.flowEnumCheckExplicitTypeMismatch(init.pos, context, "boolean");
  3547. memberNode.init = init.value;
  3548. members.booleanMembers.push(this.finishNode(memberNode, "EnumBooleanMember"));
  3549. break;
  3550. }
  3551. case "number":
  3552. {
  3553. this.flowEnumCheckExplicitTypeMismatch(init.pos, context, "number");
  3554. memberNode.init = init.value;
  3555. members.numberMembers.push(this.finishNode(memberNode, "EnumNumberMember"));
  3556. break;
  3557. }
  3558. case "string":
  3559. {
  3560. this.flowEnumCheckExplicitTypeMismatch(init.pos, context, "string");
  3561. memberNode.init = init.value;
  3562. members.stringMembers.push(this.finishNode(memberNode, "EnumStringMember"));
  3563. break;
  3564. }
  3565. case "invalid":
  3566. {
  3567. throw this.flowEnumErrorInvalidMemberInitializer(init.pos, context);
  3568. }
  3569. case "none":
  3570. {
  3571. switch (explicitType) {
  3572. case "boolean":
  3573. this.flowEnumErrorBooleanMemberNotInitialized(init.pos, context);
  3574. break;
  3575. case "number":
  3576. this.flowEnumErrorNumberMemberNotInitialized(init.pos, context);
  3577. break;
  3578. default:
  3579. members.defaultedMembers.push(this.finishNode(memberNode, "EnumDefaultedMember"));
  3580. }
  3581. }
  3582. }
  3583. if (!this.match(types$1.braceR)) {
  3584. this.expect(types$1.comma);
  3585. }
  3586. }
  3587. return {
  3588. members,
  3589. hasUnknownMembers
  3590. };
  3591. }
  3592. flowEnumStringMembers(initializedMembers, defaultedMembers, {
  3593. enumName
  3594. }) {
  3595. if (initializedMembers.length === 0) {
  3596. return defaultedMembers;
  3597. } else if (defaultedMembers.length === 0) {
  3598. return initializedMembers;
  3599. } else if (defaultedMembers.length > initializedMembers.length) {
  3600. for (const member of initializedMembers) {
  3601. this.flowEnumErrorStringMemberInconsistentlyInitailized(member.start, {
  3602. enumName
  3603. });
  3604. }
  3605. return defaultedMembers;
  3606. } else {
  3607. for (const member of defaultedMembers) {
  3608. this.flowEnumErrorStringMemberInconsistentlyInitailized(member.start, {
  3609. enumName
  3610. });
  3611. }
  3612. return initializedMembers;
  3613. }
  3614. }
  3615. flowEnumParseExplicitType({
  3616. enumName
  3617. }) {
  3618. if (this.eatContextual("of")) {
  3619. if (!this.match(types$1.name)) {
  3620. throw this.flowEnumErrorInvalidExplicitType(this.state.start, {
  3621. enumName,
  3622. suppliedType: null
  3623. });
  3624. }
  3625. const {
  3626. value
  3627. } = this.state;
  3628. this.next();
  3629. if (value !== "boolean" && value !== "number" && value !== "string" && value !== "symbol") {
  3630. this.flowEnumErrorInvalidExplicitType(this.state.start, {
  3631. enumName,
  3632. suppliedType: value
  3633. });
  3634. }
  3635. return value;
  3636. }
  3637. return null;
  3638. }
  3639. flowEnumBody(node, {
  3640. enumName,
  3641. nameLoc
  3642. }) {
  3643. const explicitType = this.flowEnumParseExplicitType({
  3644. enumName
  3645. });
  3646. this.expect(types$1.braceL);
  3647. const {
  3648. members,
  3649. hasUnknownMembers
  3650. } = this.flowEnumMembers({
  3651. enumName,
  3652. explicitType
  3653. });
  3654. node.hasUnknownMembers = hasUnknownMembers;
  3655. switch (explicitType) {
  3656. case "boolean":
  3657. node.explicitType = true;
  3658. node.members = members.booleanMembers;
  3659. this.expect(types$1.braceR);
  3660. return this.finishNode(node, "EnumBooleanBody");
  3661. case "number":
  3662. node.explicitType = true;
  3663. node.members = members.numberMembers;
  3664. this.expect(types$1.braceR);
  3665. return this.finishNode(node, "EnumNumberBody");
  3666. case "string":
  3667. node.explicitType = true;
  3668. node.members = this.flowEnumStringMembers(members.stringMembers, members.defaultedMembers, {
  3669. enumName
  3670. });
  3671. this.expect(types$1.braceR);
  3672. return this.finishNode(node, "EnumStringBody");
  3673. case "symbol":
  3674. node.members = members.defaultedMembers;
  3675. this.expect(types$1.braceR);
  3676. return this.finishNode(node, "EnumSymbolBody");
  3677. default:
  3678. {
  3679. const empty = () => {
  3680. node.members = [];
  3681. this.expect(types$1.braceR);
  3682. return this.finishNode(node, "EnumStringBody");
  3683. };
  3684. node.explicitType = false;
  3685. const boolsLen = members.booleanMembers.length;
  3686. const numsLen = members.numberMembers.length;
  3687. const strsLen = members.stringMembers.length;
  3688. const defaultedLen = members.defaultedMembers.length;
  3689. if (!boolsLen && !numsLen && !strsLen && !defaultedLen) {
  3690. return empty();
  3691. } else if (!boolsLen && !numsLen) {
  3692. node.members = this.flowEnumStringMembers(members.stringMembers, members.defaultedMembers, {
  3693. enumName
  3694. });
  3695. this.expect(types$1.braceR);
  3696. return this.finishNode(node, "EnumStringBody");
  3697. } else if (!numsLen && !strsLen && boolsLen >= defaultedLen) {
  3698. for (const member of members.defaultedMembers) {
  3699. this.flowEnumErrorBooleanMemberNotInitialized(member.start, {
  3700. enumName,
  3701. memberName: member.id.name
  3702. });
  3703. }
  3704. node.members = members.booleanMembers;
  3705. this.expect(types$1.braceR);
  3706. return this.finishNode(node, "EnumBooleanBody");
  3707. } else if (!boolsLen && !strsLen && numsLen >= defaultedLen) {
  3708. for (const member of members.defaultedMembers) {
  3709. this.flowEnumErrorNumberMemberNotInitialized(member.start, {
  3710. enumName,
  3711. memberName: member.id.name
  3712. });
  3713. }
  3714. node.members = members.numberMembers;
  3715. this.expect(types$1.braceR);
  3716. return this.finishNode(node, "EnumNumberBody");
  3717. } else {
  3718. this.flowEnumErrorInconsistentMemberValues(nameLoc, {
  3719. enumName
  3720. });
  3721. return empty();
  3722. }
  3723. }
  3724. }
  3725. }
  3726. flowParseEnumDeclaration(node) {
  3727. const id = this.parseIdentifier();
  3728. node.id = id;
  3729. node.body = this.flowEnumBody(this.startNode(), {
  3730. enumName: id.name,
  3731. nameLoc: id.start
  3732. });
  3733. return this.finishNode(node, "EnumDeclaration");
  3734. }
  3735. updateContext(prevType) {
  3736. if (this.match(types$1.name) && this.state.value === "of" && prevType === types$1.name && this.input.slice(this.state.lastTokStart, this.state.lastTokEnd) === "interface") {
  3737. this.state.exprAllowed = false;
  3738. } else {
  3739. super.updateContext(prevType);
  3740. }
  3741. }
  3742. isLookaheadToken_lt() {
  3743. const next = this.nextTokenStart();
  3744. if (this.input.charCodeAt(next) === 60) {
  3745. const afterNext = this.input.charCodeAt(next + 1);
  3746. return afterNext !== 60 && afterNext !== 61;
  3747. }
  3748. return false;
  3749. }
  3750. maybeUnwrapTypeCastExpression(node) {
  3751. return node.type === "TypeCastExpression" ? node.expression : node;
  3752. }
  3753. });
  3754. const entities = {
  3755. quot: "\u0022",
  3756. amp: "&",
  3757. apos: "\u0027",
  3758. lt: "<",
  3759. gt: ">",
  3760. nbsp: "\u00A0",
  3761. iexcl: "\u00A1",
  3762. cent: "\u00A2",
  3763. pound: "\u00A3",
  3764. curren: "\u00A4",
  3765. yen: "\u00A5",
  3766. brvbar: "\u00A6",
  3767. sect: "\u00A7",
  3768. uml: "\u00A8",
  3769. copy: "\u00A9",
  3770. ordf: "\u00AA",
  3771. laquo: "\u00AB",
  3772. not: "\u00AC",
  3773. shy: "\u00AD",
  3774. reg: "\u00AE",
  3775. macr: "\u00AF",
  3776. deg: "\u00B0",
  3777. plusmn: "\u00B1",
  3778. sup2: "\u00B2",
  3779. sup3: "\u00B3",
  3780. acute: "\u00B4",
  3781. micro: "\u00B5",
  3782. para: "\u00B6",
  3783. middot: "\u00B7",
  3784. cedil: "\u00B8",
  3785. sup1: "\u00B9",
  3786. ordm: "\u00BA",
  3787. raquo: "\u00BB",
  3788. frac14: "\u00BC",
  3789. frac12: "\u00BD",
  3790. frac34: "\u00BE",
  3791. iquest: "\u00BF",
  3792. Agrave: "\u00C0",
  3793. Aacute: "\u00C1",
  3794. Acirc: "\u00C2",
  3795. Atilde: "\u00C3",
  3796. Auml: "\u00C4",
  3797. Aring: "\u00C5",
  3798. AElig: "\u00C6",
  3799. Ccedil: "\u00C7",
  3800. Egrave: "\u00C8",
  3801. Eacute: "\u00C9",
  3802. Ecirc: "\u00CA",
  3803. Euml: "\u00CB",
  3804. Igrave: "\u00CC",
  3805. Iacute: "\u00CD",
  3806. Icirc: "\u00CE",
  3807. Iuml: "\u00CF",
  3808. ETH: "\u00D0",
  3809. Ntilde: "\u00D1",
  3810. Ograve: "\u00D2",
  3811. Oacute: "\u00D3",
  3812. Ocirc: "\u00D4",
  3813. Otilde: "\u00D5",
  3814. Ouml: "\u00D6",
  3815. times: "\u00D7",
  3816. Oslash: "\u00D8",
  3817. Ugrave: "\u00D9",
  3818. Uacute: "\u00DA",
  3819. Ucirc: "\u00DB",
  3820. Uuml: "\u00DC",
  3821. Yacute: "\u00DD",
  3822. THORN: "\u00DE",
  3823. szlig: "\u00DF",
  3824. agrave: "\u00E0",
  3825. aacute: "\u00E1",
  3826. acirc: "\u00E2",
  3827. atilde: "\u00E3",
  3828. auml: "\u00E4",
  3829. aring: "\u00E5",
  3830. aelig: "\u00E6",
  3831. ccedil: "\u00E7",
  3832. egrave: "\u00E8",
  3833. eacute: "\u00E9",
  3834. ecirc: "\u00EA",
  3835. euml: "\u00EB",
  3836. igrave: "\u00EC",
  3837. iacute: "\u00ED",
  3838. icirc: "\u00EE",
  3839. iuml: "\u00EF",
  3840. eth: "\u00F0",
  3841. ntilde: "\u00F1",
  3842. ograve: "\u00F2",
  3843. oacute: "\u00F3",
  3844. ocirc: "\u00F4",
  3845. otilde: "\u00F5",
  3846. ouml: "\u00F6",
  3847. divide: "\u00F7",
  3848. oslash: "\u00F8",
  3849. ugrave: "\u00F9",
  3850. uacute: "\u00FA",
  3851. ucirc: "\u00FB",
  3852. uuml: "\u00FC",
  3853. yacute: "\u00FD",
  3854. thorn: "\u00FE",
  3855. yuml: "\u00FF",
  3856. OElig: "\u0152",
  3857. oelig: "\u0153",
  3858. Scaron: "\u0160",
  3859. scaron: "\u0161",
  3860. Yuml: "\u0178",
  3861. fnof: "\u0192",
  3862. circ: "\u02C6",
  3863. tilde: "\u02DC",
  3864. Alpha: "\u0391",
  3865. Beta: "\u0392",
  3866. Gamma: "\u0393",
  3867. Delta: "\u0394",
  3868. Epsilon: "\u0395",
  3869. Zeta: "\u0396",
  3870. Eta: "\u0397",
  3871. Theta: "\u0398",
  3872. Iota: "\u0399",
  3873. Kappa: "\u039A",
  3874. Lambda: "\u039B",
  3875. Mu: "\u039C",
  3876. Nu: "\u039D",
  3877. Xi: "\u039E",
  3878. Omicron: "\u039F",
  3879. Pi: "\u03A0",
  3880. Rho: "\u03A1",
  3881. Sigma: "\u03A3",
  3882. Tau: "\u03A4",
  3883. Upsilon: "\u03A5",
  3884. Phi: "\u03A6",
  3885. Chi: "\u03A7",
  3886. Psi: "\u03A8",
  3887. Omega: "\u03A9",
  3888. alpha: "\u03B1",
  3889. beta: "\u03B2",
  3890. gamma: "\u03B3",
  3891. delta: "\u03B4",
  3892. epsilon: "\u03B5",
  3893. zeta: "\u03B6",
  3894. eta: "\u03B7",
  3895. theta: "\u03B8",
  3896. iota: "\u03B9",
  3897. kappa: "\u03BA",
  3898. lambda: "\u03BB",
  3899. mu: "\u03BC",
  3900. nu: "\u03BD",
  3901. xi: "\u03BE",
  3902. omicron: "\u03BF",
  3903. pi: "\u03C0",
  3904. rho: "\u03C1",
  3905. sigmaf: "\u03C2",
  3906. sigma: "\u03C3",
  3907. tau: "\u03C4",
  3908. upsilon: "\u03C5",
  3909. phi: "\u03C6",
  3910. chi: "\u03C7",
  3911. psi: "\u03C8",
  3912. omega: "\u03C9",
  3913. thetasym: "\u03D1",
  3914. upsih: "\u03D2",
  3915. piv: "\u03D6",
  3916. ensp: "\u2002",
  3917. emsp: "\u2003",
  3918. thinsp: "\u2009",
  3919. zwnj: "\u200C",
  3920. zwj: "\u200D",
  3921. lrm: "\u200E",
  3922. rlm: "\u200F",
  3923. ndash: "\u2013",
  3924. mdash: "\u2014",
  3925. lsquo: "\u2018",
  3926. rsquo: "\u2019",
  3927. sbquo: "\u201A",
  3928. ldquo: "\u201C",
  3929. rdquo: "\u201D",
  3930. bdquo: "\u201E",
  3931. dagger: "\u2020",
  3932. Dagger: "\u2021",
  3933. bull: "\u2022",
  3934. hellip: "\u2026",
  3935. permil: "\u2030",
  3936. prime: "\u2032",
  3937. Prime: "\u2033",
  3938. lsaquo: "\u2039",
  3939. rsaquo: "\u203A",
  3940. oline: "\u203E",
  3941. frasl: "\u2044",
  3942. euro: "\u20AC",
  3943. image: "\u2111",
  3944. weierp: "\u2118",
  3945. real: "\u211C",
  3946. trade: "\u2122",
  3947. alefsym: "\u2135",
  3948. larr: "\u2190",
  3949. uarr: "\u2191",
  3950. rarr: "\u2192",
  3951. darr: "\u2193",
  3952. harr: "\u2194",
  3953. crarr: "\u21B5",
  3954. lArr: "\u21D0",
  3955. uArr: "\u21D1",
  3956. rArr: "\u21D2",
  3957. dArr: "\u21D3",
  3958. hArr: "\u21D4",
  3959. forall: "\u2200",
  3960. part: "\u2202",
  3961. exist: "\u2203",
  3962. empty: "\u2205",
  3963. nabla: "\u2207",
  3964. isin: "\u2208",
  3965. notin: "\u2209",
  3966. ni: "\u220B",
  3967. prod: "\u220F",
  3968. sum: "\u2211",
  3969. minus: "\u2212",
  3970. lowast: "\u2217",
  3971. radic: "\u221A",
  3972. prop: "\u221D",
  3973. infin: "\u221E",
  3974. ang: "\u2220",
  3975. and: "\u2227",
  3976. or: "\u2228",
  3977. cap: "\u2229",
  3978. cup: "\u222A",
  3979. int: "\u222B",
  3980. there4: "\u2234",
  3981. sim: "\u223C",
  3982. cong: "\u2245",
  3983. asymp: "\u2248",
  3984. ne: "\u2260",
  3985. equiv: "\u2261",
  3986. le: "\u2264",
  3987. ge: "\u2265",
  3988. sub: "\u2282",
  3989. sup: "\u2283",
  3990. nsub: "\u2284",
  3991. sube: "\u2286",
  3992. supe: "\u2287",
  3993. oplus: "\u2295",
  3994. otimes: "\u2297",
  3995. perp: "\u22A5",
  3996. sdot: "\u22C5",
  3997. lceil: "\u2308",
  3998. rceil: "\u2309",
  3999. lfloor: "\u230A",
  4000. rfloor: "\u230B",
  4001. lang: "\u2329",
  4002. rang: "\u232A",
  4003. loz: "\u25CA",
  4004. spades: "\u2660",
  4005. clubs: "\u2663",
  4006. hearts: "\u2665",
  4007. diams: "\u2666"
  4008. };
  4009. class State {
  4010. constructor() {
  4011. this.strict = void 0;
  4012. this.curLine = void 0;
  4013. this.startLoc = void 0;
  4014. this.endLoc = void 0;
  4015. this.errors = [];
  4016. this.potentialArrowAt = -1;
  4017. this.noArrowAt = [];
  4018. this.noArrowParamsConversionAt = [];
  4019. this.maybeInArrowParameters = false;
  4020. this.inPipeline = false;
  4021. this.inType = false;
  4022. this.noAnonFunctionType = false;
  4023. this.inPropertyName = false;
  4024. this.hasFlowComment = false;
  4025. this.isAmbientContext = false;
  4026. this.inAbstractClass = false;
  4027. this.topicContext = {
  4028. maxNumOfResolvableTopics: 0,
  4029. maxTopicIndex: null
  4030. };
  4031. this.soloAwait = false;
  4032. this.inFSharpPipelineDirectBody = false;
  4033. this.labels = [];
  4034. this.decoratorStack = [[]];
  4035. this.comments = [];
  4036. this.trailingComments = [];
  4037. this.leadingComments = [];
  4038. this.commentStack = [];
  4039. this.commentPreviousNode = null;
  4040. this.pos = 0;
  4041. this.lineStart = 0;
  4042. this.type = types$1.eof;
  4043. this.value = null;
  4044. this.start = 0;
  4045. this.end = 0;
  4046. this.lastTokEndLoc = null;
  4047. this.lastTokStartLoc = null;
  4048. this.lastTokStart = 0;
  4049. this.lastTokEnd = 0;
  4050. this.context = [types.braceStatement];
  4051. this.exprAllowed = true;
  4052. this.containsEsc = false;
  4053. this.strictErrors = new Map();
  4054. this.exportedIdentifiers = [];
  4055. this.tokensLength = 0;
  4056. }
  4057. init(options) {
  4058. this.strict = options.strictMode === false ? false : options.sourceType === "module";
  4059. this.curLine = options.startLine;
  4060. this.startLoc = this.endLoc = this.curPosition();
  4061. }
  4062. curPosition() {
  4063. return new Position(this.curLine, this.pos - this.lineStart);
  4064. }
  4065. clone(skipArrays) {
  4066. const state = new State();
  4067. const keys = Object.keys(this);
  4068. for (let i = 0, length = keys.length; i < length; i++) {
  4069. const key = keys[i];
  4070. let val = this[key];
  4071. if (!skipArrays && Array.isArray(val)) {
  4072. val = val.slice();
  4073. }
  4074. state[key] = val;
  4075. }
  4076. return state;
  4077. }
  4078. }
  4079. const HEX_NUMBER = /^[\da-fA-F]+$/;
  4080. const DECIMAL_NUMBER = /^\d+$/;
  4081. const JsxErrors = makeErrorTemplates({
  4082. AttributeIsEmpty: "JSX attributes must only be assigned a non-empty expression.",
  4083. MissingClosingTagElement: "Expected corresponding JSX closing tag for <%0>.",
  4084. MissingClosingTagFragment: "Expected corresponding JSX closing tag for <>.",
  4085. UnexpectedSequenceExpression: "Sequence expressions cannot be directly nested inside JSX. Did you mean to wrap it in parentheses (...)?",
  4086. UnsupportedJsxValue: "JSX value should be either an expression or a quoted JSX text.",
  4087. UnterminatedJsxContent: "Unterminated JSX contents.",
  4088. UnwrappedAdjacentJSXElements: "Adjacent JSX elements must be wrapped in an enclosing tag. Did you want a JSX fragment <>...</>?"
  4089. }, ErrorCodes.SyntaxError);
  4090. types.j_oTag = new TokContext("<tag", false);
  4091. types.j_cTag = new TokContext("</tag", false);
  4092. types.j_expr = new TokContext("<tag>...</tag>", true, true);
  4093. types$1.jsxName = new TokenType("jsxName");
  4094. types$1.jsxText = new TokenType("jsxText", {
  4095. beforeExpr: true
  4096. });
  4097. types$1.jsxTagStart = new TokenType("jsxTagStart", {
  4098. startsExpr: true
  4099. });
  4100. types$1.jsxTagEnd = new TokenType("jsxTagEnd");
  4101. types$1.jsxTagStart.updateContext = function () {
  4102. this.state.context.push(types.j_expr);
  4103. this.state.context.push(types.j_oTag);
  4104. this.state.exprAllowed = false;
  4105. };
  4106. types$1.jsxTagEnd.updateContext = function (prevType) {
  4107. const out = this.state.context.pop();
  4108. if (out === types.j_oTag && prevType === types$1.slash || out === types.j_cTag) {
  4109. this.state.context.pop();
  4110. this.state.exprAllowed = this.curContext() === types.j_expr;
  4111. } else {
  4112. this.state.exprAllowed = true;
  4113. }
  4114. };
  4115. function isFragment(object) {
  4116. return object ? object.type === "JSXOpeningFragment" || object.type === "JSXClosingFragment" : false;
  4117. }
  4118. function getQualifiedJSXName(object) {
  4119. if (object.type === "JSXIdentifier") {
  4120. return object.name;
  4121. }
  4122. if (object.type === "JSXNamespacedName") {
  4123. return object.namespace.name + ":" + object.name.name;
  4124. }
  4125. if (object.type === "JSXMemberExpression") {
  4126. return getQualifiedJSXName(object.object) + "." + getQualifiedJSXName(object.property);
  4127. }
  4128. throw new Error("Node had unexpected type: " + object.type);
  4129. }
  4130. var jsx = (superClass => class extends superClass {
  4131. jsxReadToken() {
  4132. let out = "";
  4133. let chunkStart = this.state.pos;
  4134. for (;;) {
  4135. if (this.state.pos >= this.length) {
  4136. throw this.raise(this.state.start, JsxErrors.UnterminatedJsxContent);
  4137. }
  4138. const ch = this.input.charCodeAt(this.state.pos);
  4139. switch (ch) {
  4140. case 60:
  4141. case 123:
  4142. if (this.state.pos === this.state.start) {
  4143. if (ch === 60 && this.state.exprAllowed) {
  4144. ++this.state.pos;
  4145. return this.finishToken(types$1.jsxTagStart);
  4146. }
  4147. return super.getTokenFromCode(ch);
  4148. }
  4149. out += this.input.slice(chunkStart, this.state.pos);
  4150. return this.finishToken(types$1.jsxText, out);
  4151. case 38:
  4152. out += this.input.slice(chunkStart, this.state.pos);
  4153. out += this.jsxReadEntity();
  4154. chunkStart = this.state.pos;
  4155. break;
  4156. case 62:
  4157. case 125:
  4158. default:
  4159. if (isNewLine(ch)) {
  4160. out += this.input.slice(chunkStart, this.state.pos);
  4161. out += this.jsxReadNewLine(true);
  4162. chunkStart = this.state.pos;
  4163. } else {
  4164. ++this.state.pos;
  4165. }
  4166. }
  4167. }
  4168. }
  4169. jsxReadNewLine(normalizeCRLF) {
  4170. const ch = this.input.charCodeAt(this.state.pos);
  4171. let out;
  4172. ++this.state.pos;
  4173. if (ch === 13 && this.input.charCodeAt(this.state.pos) === 10) {
  4174. ++this.state.pos;
  4175. out = normalizeCRLF ? "\n" : "\r\n";
  4176. } else {
  4177. out = String.fromCharCode(ch);
  4178. }
  4179. ++this.state.curLine;
  4180. this.state.lineStart = this.state.pos;
  4181. return out;
  4182. }
  4183. jsxReadString(quote) {
  4184. let out = "";
  4185. let chunkStart = ++this.state.pos;
  4186. for (;;) {
  4187. if (this.state.pos >= this.length) {
  4188. throw this.raise(this.state.start, ErrorMessages.UnterminatedString);
  4189. }
  4190. const ch = this.input.charCodeAt(this.state.pos);
  4191. if (ch === quote) break;
  4192. if (ch === 38) {
  4193. out += this.input.slice(chunkStart, this.state.pos);
  4194. out += this.jsxReadEntity();
  4195. chunkStart = this.state.pos;
  4196. } else if (isNewLine(ch)) {
  4197. out += this.input.slice(chunkStart, this.state.pos);
  4198. out += this.jsxReadNewLine(false);
  4199. chunkStart = this.state.pos;
  4200. } else {
  4201. ++this.state.pos;
  4202. }
  4203. }
  4204. out += this.input.slice(chunkStart, this.state.pos++);
  4205. return this.finishToken(types$1.string, out);
  4206. }
  4207. jsxReadEntity() {
  4208. let str = "";
  4209. let count = 0;
  4210. let entity;
  4211. let ch = this.input[this.state.pos];
  4212. const startPos = ++this.state.pos;
  4213. while (this.state.pos < this.length && count++ < 10) {
  4214. ch = this.input[this.state.pos++];
  4215. if (ch === ";") {
  4216. if (str[0] === "#") {
  4217. if (str[1] === "x") {
  4218. str = str.substr(2);
  4219. if (HEX_NUMBER.test(str)) {
  4220. entity = String.fromCodePoint(parseInt(str, 16));
  4221. }
  4222. } else {
  4223. str = str.substr(1);
  4224. if (DECIMAL_NUMBER.test(str)) {
  4225. entity = String.fromCodePoint(parseInt(str, 10));
  4226. }
  4227. }
  4228. } else {
  4229. entity = entities[str];
  4230. }
  4231. break;
  4232. }
  4233. str += ch;
  4234. }
  4235. if (!entity) {
  4236. this.state.pos = startPos;
  4237. return "&";
  4238. }
  4239. return entity;
  4240. }
  4241. jsxReadWord() {
  4242. let ch;
  4243. const start = this.state.pos;
  4244. do {
  4245. ch = this.input.charCodeAt(++this.state.pos);
  4246. } while (isIdentifierChar(ch) || ch === 45);
  4247. return this.finishToken(types$1.jsxName, this.input.slice(start, this.state.pos));
  4248. }
  4249. jsxParseIdentifier() {
  4250. const node = this.startNode();
  4251. if (this.match(types$1.jsxName)) {
  4252. node.name = this.state.value;
  4253. } else if (this.state.type.keyword) {
  4254. node.name = this.state.type.keyword;
  4255. } else {
  4256. this.unexpected();
  4257. }
  4258. this.next();
  4259. return this.finishNode(node, "JSXIdentifier");
  4260. }
  4261. jsxParseNamespacedName() {
  4262. const startPos = this.state.start;
  4263. const startLoc = this.state.startLoc;
  4264. const name = this.jsxParseIdentifier();
  4265. if (!this.eat(types$1.colon)) return name;
  4266. const node = this.startNodeAt(startPos, startLoc);
  4267. node.namespace = name;
  4268. node.name = this.jsxParseIdentifier();
  4269. return this.finishNode(node, "JSXNamespacedName");
  4270. }
  4271. jsxParseElementName() {
  4272. const startPos = this.state.start;
  4273. const startLoc = this.state.startLoc;
  4274. let node = this.jsxParseNamespacedName();
  4275. if (node.type === "JSXNamespacedName") {
  4276. return node;
  4277. }
  4278. while (this.eat(types$1.dot)) {
  4279. const newNode = this.startNodeAt(startPos, startLoc);
  4280. newNode.object = node;
  4281. newNode.property = this.jsxParseIdentifier();
  4282. node = this.finishNode(newNode, "JSXMemberExpression");
  4283. }
  4284. return node;
  4285. }
  4286. jsxParseAttributeValue() {
  4287. let node;
  4288. switch (this.state.type) {
  4289. case types$1.braceL:
  4290. node = this.startNode();
  4291. this.next();
  4292. node = this.jsxParseExpressionContainer(node);
  4293. if (node.expression.type === "JSXEmptyExpression") {
  4294. this.raise(node.start, JsxErrors.AttributeIsEmpty);
  4295. }
  4296. return node;
  4297. case types$1.jsxTagStart:
  4298. case types$1.string:
  4299. return this.parseExprAtom();
  4300. default:
  4301. throw this.raise(this.state.start, JsxErrors.UnsupportedJsxValue);
  4302. }
  4303. }
  4304. jsxParseEmptyExpression() {
  4305. const node = this.startNodeAt(this.state.lastTokEnd, this.state.lastTokEndLoc);
  4306. return this.finishNodeAt(node, "JSXEmptyExpression", this.state.start, this.state.startLoc);
  4307. }
  4308. jsxParseSpreadChild(node) {
  4309. this.next();
  4310. node.expression = this.parseExpression();
  4311. this.expect(types$1.braceR);
  4312. return this.finishNode(node, "JSXSpreadChild");
  4313. }
  4314. jsxParseExpressionContainer(node) {
  4315. if (this.match(types$1.braceR)) {
  4316. node.expression = this.jsxParseEmptyExpression();
  4317. } else {
  4318. const expression = this.parseExpression();
  4319. node.expression = expression;
  4320. }
  4321. this.expect(types$1.braceR);
  4322. return this.finishNode(node, "JSXExpressionContainer");
  4323. }
  4324. jsxParseAttribute() {
  4325. const node = this.startNode();
  4326. if (this.eat(types$1.braceL)) {
  4327. this.expect(types$1.ellipsis);
  4328. node.argument = this.parseMaybeAssignAllowIn();
  4329. this.expect(types$1.braceR);
  4330. return this.finishNode(node, "JSXSpreadAttribute");
  4331. }
  4332. node.name = this.jsxParseNamespacedName();
  4333. node.value = this.eat(types$1.eq) ? this.jsxParseAttributeValue() : null;
  4334. return this.finishNode(node, "JSXAttribute");
  4335. }
  4336. jsxParseOpeningElementAt(startPos, startLoc) {
  4337. const node = this.startNodeAt(startPos, startLoc);
  4338. if (this.match(types$1.jsxTagEnd)) {
  4339. this.expect(types$1.jsxTagEnd);
  4340. return this.finishNode(node, "JSXOpeningFragment");
  4341. }
  4342. node.name = this.jsxParseElementName();
  4343. return this.jsxParseOpeningElementAfterName(node);
  4344. }
  4345. jsxParseOpeningElementAfterName(node) {
  4346. const attributes = [];
  4347. while (!this.match(types$1.slash) && !this.match(types$1.jsxTagEnd)) {
  4348. attributes.push(this.jsxParseAttribute());
  4349. }
  4350. node.attributes = attributes;
  4351. node.selfClosing = this.eat(types$1.slash);
  4352. this.expect(types$1.jsxTagEnd);
  4353. return this.finishNode(node, "JSXOpeningElement");
  4354. }
  4355. jsxParseClosingElementAt(startPos, startLoc) {
  4356. const node = this.startNodeAt(startPos, startLoc);
  4357. if (this.match(types$1.jsxTagEnd)) {
  4358. this.expect(types$1.jsxTagEnd);
  4359. return this.finishNode(node, "JSXClosingFragment");
  4360. }
  4361. node.name = this.jsxParseElementName();
  4362. this.expect(types$1.jsxTagEnd);
  4363. return this.finishNode(node, "JSXClosingElement");
  4364. }
  4365. jsxParseElementAt(startPos, startLoc) {
  4366. const node = this.startNodeAt(startPos, startLoc);
  4367. const children = [];
  4368. const openingElement = this.jsxParseOpeningElementAt(startPos, startLoc);
  4369. let closingElement = null;
  4370. if (!openingElement.selfClosing) {
  4371. contents: for (;;) {
  4372. switch (this.state.type) {
  4373. case types$1.jsxTagStart:
  4374. startPos = this.state.start;
  4375. startLoc = this.state.startLoc;
  4376. this.next();
  4377. if (this.eat(types$1.slash)) {
  4378. closingElement = this.jsxParseClosingElementAt(startPos, startLoc);
  4379. break contents;
  4380. }
  4381. children.push(this.jsxParseElementAt(startPos, startLoc));
  4382. break;
  4383. case types$1.jsxText:
  4384. children.push(this.parseExprAtom());
  4385. break;
  4386. case types$1.braceL:
  4387. {
  4388. const node = this.startNode();
  4389. this.next();
  4390. if (this.match(types$1.ellipsis)) {
  4391. children.push(this.jsxParseSpreadChild(node));
  4392. } else {
  4393. children.push(this.jsxParseExpressionContainer(node));
  4394. }
  4395. break;
  4396. }
  4397. default:
  4398. throw this.unexpected();
  4399. }
  4400. }
  4401. if (isFragment(openingElement) && !isFragment(closingElement)) {
  4402. this.raise(closingElement.start, JsxErrors.MissingClosingTagFragment);
  4403. } else if (!isFragment(openingElement) && isFragment(closingElement)) {
  4404. this.raise(closingElement.start, JsxErrors.MissingClosingTagElement, getQualifiedJSXName(openingElement.name));
  4405. } else if (!isFragment(openingElement) && !isFragment(closingElement)) {
  4406. if (getQualifiedJSXName(closingElement.name) !== getQualifiedJSXName(openingElement.name)) {
  4407. this.raise(closingElement.start, JsxErrors.MissingClosingTagElement, getQualifiedJSXName(openingElement.name));
  4408. }
  4409. }
  4410. }
  4411. if (isFragment(openingElement)) {
  4412. node.openingFragment = openingElement;
  4413. node.closingFragment = closingElement;
  4414. } else {
  4415. node.openingElement = openingElement;
  4416. node.closingElement = closingElement;
  4417. }
  4418. node.children = children;
  4419. if (this.isRelational("<")) {
  4420. throw this.raise(this.state.start, JsxErrors.UnwrappedAdjacentJSXElements);
  4421. }
  4422. return isFragment(openingElement) ? this.finishNode(node, "JSXFragment") : this.finishNode(node, "JSXElement");
  4423. }
  4424. jsxParseElement() {
  4425. const startPos = this.state.start;
  4426. const startLoc = this.state.startLoc;
  4427. this.next();
  4428. return this.jsxParseElementAt(startPos, startLoc);
  4429. }
  4430. parseExprAtom(refExpressionErrors) {
  4431. if (this.match(types$1.jsxText)) {
  4432. return this.parseLiteral(this.state.value, "JSXText");
  4433. } else if (this.match(types$1.jsxTagStart)) {
  4434. return this.jsxParseElement();
  4435. } else if (this.isRelational("<") && this.input.charCodeAt(this.state.pos) !== 33) {
  4436. this.finishToken(types$1.jsxTagStart);
  4437. return this.jsxParseElement();
  4438. } else {
  4439. return super.parseExprAtom(refExpressionErrors);
  4440. }
  4441. }
  4442. createLookaheadState(state) {
  4443. const lookaheadState = super.createLookaheadState(state);
  4444. lookaheadState.inPropertyName = state.inPropertyName;
  4445. return lookaheadState;
  4446. }
  4447. getTokenFromCode(code) {
  4448. if (this.state.inPropertyName) return super.getTokenFromCode(code);
  4449. const context = this.curContext();
  4450. if (context === types.j_expr) {
  4451. return this.jsxReadToken();
  4452. }
  4453. if (context === types.j_oTag || context === types.j_cTag) {
  4454. if (isIdentifierStart(code)) {
  4455. return this.jsxReadWord();
  4456. }
  4457. if (code === 62) {
  4458. ++this.state.pos;
  4459. return this.finishToken(types$1.jsxTagEnd);
  4460. }
  4461. if ((code === 34 || code === 39) && context === types.j_oTag) {
  4462. return this.jsxReadString(code);
  4463. }
  4464. }
  4465. if (code === 60 && this.state.exprAllowed && this.input.charCodeAt(this.state.pos + 1) !== 33) {
  4466. ++this.state.pos;
  4467. return this.finishToken(types$1.jsxTagStart);
  4468. }
  4469. return super.getTokenFromCode(code);
  4470. }
  4471. updateContext(prevType) {
  4472. if (this.match(types$1.braceL)) {
  4473. const curContext = this.curContext();
  4474. if (curContext === types.j_oTag) {
  4475. this.state.context.push(types.braceExpression);
  4476. } else if (curContext === types.j_expr) {
  4477. this.state.context.push(types.templateQuasi);
  4478. } else {
  4479. super.updateContext(prevType);
  4480. }
  4481. this.state.exprAllowed = true;
  4482. } else if (this.match(types$1.slash) && prevType === types$1.jsxTagStart) {
  4483. this.state.context.length -= 2;
  4484. this.state.context.push(types.j_cTag);
  4485. this.state.exprAllowed = false;
  4486. } else {
  4487. return super.updateContext(prevType);
  4488. }
  4489. }
  4490. });
  4491. class TypeScriptScope extends Scope {
  4492. constructor(...args) {
  4493. super(...args);
  4494. this.types = [];
  4495. this.enums = [];
  4496. this.constEnums = [];
  4497. this.classes = [];
  4498. this.exportOnlyBindings = [];
  4499. }
  4500. }
  4501. class TypeScriptScopeHandler extends ScopeHandler {
  4502. createScope(flags) {
  4503. return new TypeScriptScope(flags);
  4504. }
  4505. declareName(name, bindingType, pos) {
  4506. const scope = this.currentScope();
  4507. if (bindingType & BIND_FLAGS_TS_EXPORT_ONLY) {
  4508. this.maybeExportDefined(scope, name);
  4509. scope.exportOnlyBindings.push(name);
  4510. return;
  4511. }
  4512. super.declareName(...arguments);
  4513. if (bindingType & BIND_KIND_TYPE) {
  4514. if (!(bindingType & BIND_KIND_VALUE)) {
  4515. this.checkRedeclarationInScope(scope, name, bindingType, pos);
  4516. this.maybeExportDefined(scope, name);
  4517. }
  4518. scope.types.push(name);
  4519. }
  4520. if (bindingType & BIND_FLAGS_TS_ENUM) scope.enums.push(name);
  4521. if (bindingType & BIND_FLAGS_TS_CONST_ENUM) scope.constEnums.push(name);
  4522. if (bindingType & BIND_FLAGS_CLASS) scope.classes.push(name);
  4523. }
  4524. isRedeclaredInScope(scope, name, bindingType) {
  4525. if (scope.enums.indexOf(name) > -1) {
  4526. if (bindingType & BIND_FLAGS_TS_ENUM) {
  4527. const isConst = !!(bindingType & BIND_FLAGS_TS_CONST_ENUM);
  4528. const wasConst = scope.constEnums.indexOf(name) > -1;
  4529. return isConst !== wasConst;
  4530. }
  4531. return true;
  4532. }
  4533. if (bindingType & BIND_FLAGS_CLASS && scope.classes.indexOf(name) > -1) {
  4534. if (scope.lexical.indexOf(name) > -1) {
  4535. return !!(bindingType & BIND_KIND_VALUE);
  4536. } else {
  4537. return false;
  4538. }
  4539. }
  4540. if (bindingType & BIND_KIND_TYPE && scope.types.indexOf(name) > -1) {
  4541. return true;
  4542. }
  4543. return super.isRedeclaredInScope(...arguments);
  4544. }
  4545. checkLocalExport(id) {
  4546. if (this.scopeStack[0].types.indexOf(id.name) === -1 && this.scopeStack[0].exportOnlyBindings.indexOf(id.name) === -1) {
  4547. super.checkLocalExport(id);
  4548. }
  4549. }
  4550. }
  4551. const PARAM = 0b0000,
  4552. PARAM_YIELD = 0b0001,
  4553. PARAM_AWAIT = 0b0010,
  4554. PARAM_RETURN = 0b0100,
  4555. PARAM_IN = 0b1000;
  4556. class ProductionParameterHandler {
  4557. constructor() {
  4558. this.stacks = [];
  4559. }
  4560. enter(flags) {
  4561. this.stacks.push(flags);
  4562. }
  4563. exit() {
  4564. this.stacks.pop();
  4565. }
  4566. currentFlags() {
  4567. return this.stacks[this.stacks.length - 1];
  4568. }
  4569. get hasAwait() {
  4570. return (this.currentFlags() & PARAM_AWAIT) > 0;
  4571. }
  4572. get hasYield() {
  4573. return (this.currentFlags() & PARAM_YIELD) > 0;
  4574. }
  4575. get hasReturn() {
  4576. return (this.currentFlags() & PARAM_RETURN) > 0;
  4577. }
  4578. get hasIn() {
  4579. return (this.currentFlags() & PARAM_IN) > 0;
  4580. }
  4581. }
  4582. function functionFlags(isAsync, isGenerator) {
  4583. return (isAsync ? PARAM_AWAIT : 0) | (isGenerator ? PARAM_YIELD : 0);
  4584. }
  4585. function nonNull(x) {
  4586. if (x == null) {
  4587. throw new Error(`Unexpected ${x} value.`);
  4588. }
  4589. return x;
  4590. }
  4591. function assert(x) {
  4592. if (!x) {
  4593. throw new Error("Assert fail");
  4594. }
  4595. }
  4596. const TSErrors = makeErrorTemplates({
  4597. AbstractMethodHasImplementation: "Method '%0' cannot have an implementation because it is marked abstract.",
  4598. AccesorCannotDeclareThisParameter: "'get' and 'set' accessors cannot declare 'this' parameters.",
  4599. AccesorCannotHaveTypeParameters: "An accessor cannot have type parameters.",
  4600. ClassMethodHasDeclare: "Class methods cannot have the 'declare' modifier.",
  4601. ClassMethodHasReadonly: "Class methods cannot have the 'readonly' modifier.",
  4602. ConstructorHasTypeParameters: "Type parameters cannot appear on a constructor declaration.",
  4603. DeclareAccessor: "'declare' is not allowed in %0ters.",
  4604. DeclareClassFieldHasInitializer: "Initializers are not allowed in ambient contexts.",
  4605. DeclareFunctionHasImplementation: "An implementation cannot be declared in ambient contexts.",
  4606. DuplicateAccessibilityModifier: "Accessibility modifier already seen.",
  4607. DuplicateModifier: "Duplicate modifier: '%0'.",
  4608. EmptyHeritageClauseType: "'%0' list cannot be empty.",
  4609. EmptyTypeArguments: "Type argument list cannot be empty.",
  4610. EmptyTypeParameters: "Type parameter list cannot be empty.",
  4611. ExpectedAmbientAfterExportDeclare: "'export declare' must be followed by an ambient declaration.",
  4612. ImportAliasHasImportType: "An import alias can not use 'import type'.",
  4613. IncompatibleModifiers: "'%0' modifier cannot be used with '%1' modifier.",
  4614. IndexSignatureHasAbstract: "Index signatures cannot have the 'abstract' modifier.",
  4615. IndexSignatureHasAccessibility: "Index signatures cannot have an accessibility modifier ('%0').",
  4616. IndexSignatureHasDeclare: "Index signatures cannot have the 'declare' modifier.",
  4617. IndexSignatureHasOverride: "'override' modifier cannot appear on an index signature.",
  4618. IndexSignatureHasStatic: "Index signatures cannot have the 'static' modifier.",
  4619. InvalidModifierOnTypeMember: "'%0' modifier cannot appear on a type member.",
  4620. InvalidModifiersOrder: "'%0' modifier must precede '%1' modifier.",
  4621. InvalidTupleMemberLabel: "Tuple members must be labeled with a simple identifier.",
  4622. MixedLabeledAndUnlabeledElements: "Tuple members must all have names or all not have names.",
  4623. NonAbstractClassHasAbstractMethod: "Abstract methods can only appear within an abstract class.",
  4624. NonClassMethodPropertyHasAbstractModifer: "'abstract' modifier can only appear on a class, method, or property declaration.",
  4625. OptionalTypeBeforeRequired: "A required element cannot follow an optional element.",
  4626. OverrideNotInSubClass: "This member cannot have an 'override' modifier because its containing class does not extend another class.",
  4627. PatternIsOptional: "A binding pattern parameter cannot be optional in an implementation signature.",
  4628. PrivateElementHasAbstract: "Private elements cannot have the 'abstract' modifier.",
  4629. PrivateElementHasAccessibility: "Private elements cannot have an accessibility modifier ('%0').",
  4630. ReadonlyForMethodSignature: "'readonly' modifier can only appear on a property declaration or index signature.",
  4631. SetAccesorCannotHaveOptionalParameter: "A 'set' accessor cannot have an optional parameter.",
  4632. SetAccesorCannotHaveRestParameter: "A 'set' accessor cannot have rest parameter.",
  4633. SetAccesorCannotHaveReturnType: "A 'set' accessor cannot have a return type annotation.",
  4634. StaticBlockCannotHaveModifier: "Static class blocks cannot have any modifier.",
  4635. TypeAnnotationAfterAssign: "Type annotations must come before default assignments, e.g. instead of `age = 25: number` use `age: number = 25`.",
  4636. TypeImportCannotSpecifyDefaultAndNamed: "A type-only import can specify a default import or named bindings, but not both.",
  4637. UnexpectedParameterModifier: "A parameter property is only allowed in a constructor implementation.",
  4638. UnexpectedReadonly: "'readonly' type modifier is only permitted on array and tuple literal types.",
  4639. UnexpectedTypeAnnotation: "Did not expect a type annotation here.",
  4640. UnexpectedTypeCastInParameter: "Unexpected type cast in parameter position.",
  4641. UnsupportedImportTypeArgument: "Argument in a type import must be a string literal.",
  4642. UnsupportedParameterPropertyKind: "A parameter property may not be declared using a binding pattern.",
  4643. UnsupportedSignatureParameterKind: "Name in a signature must be an Identifier, ObjectPattern or ArrayPattern, instead got %0."
  4644. }, ErrorCodes.SyntaxError);
  4645. function keywordTypeFromName(value) {
  4646. switch (value) {
  4647. case "any":
  4648. return "TSAnyKeyword";
  4649. case "boolean":
  4650. return "TSBooleanKeyword";
  4651. case "bigint":
  4652. return "TSBigIntKeyword";
  4653. case "never":
  4654. return "TSNeverKeyword";
  4655. case "number":
  4656. return "TSNumberKeyword";
  4657. case "object":
  4658. return "TSObjectKeyword";
  4659. case "string":
  4660. return "TSStringKeyword";
  4661. case "symbol":
  4662. return "TSSymbolKeyword";
  4663. case "undefined":
  4664. return "TSUndefinedKeyword";
  4665. case "unknown":
  4666. return "TSUnknownKeyword";
  4667. default:
  4668. return undefined;
  4669. }
  4670. }
  4671. function tsIsAccessModifier(modifier) {
  4672. return modifier === "private" || modifier === "public" || modifier === "protected";
  4673. }
  4674. var typescript = (superClass => class extends superClass {
  4675. getScopeHandler() {
  4676. return TypeScriptScopeHandler;
  4677. }
  4678. tsIsIdentifier() {
  4679. return this.match(types$1.name);
  4680. }
  4681. tsTokenCanFollowModifier() {
  4682. 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();
  4683. }
  4684. tsNextTokenCanFollowModifier() {
  4685. this.next();
  4686. return this.tsTokenCanFollowModifier();
  4687. }
  4688. tsParseModifier(allowedModifiers) {
  4689. if (!this.match(types$1.name)) {
  4690. return undefined;
  4691. }
  4692. const modifier = this.state.value;
  4693. if (allowedModifiers.indexOf(modifier) !== -1 && this.tsTryParse(this.tsNextTokenCanFollowModifier.bind(this))) {
  4694. return modifier;
  4695. }
  4696. return undefined;
  4697. }
  4698. tsParseModifiers(modified, allowedModifiers, disallowedModifiers, errorTemplate) {
  4699. const enforceOrder = (pos, modifier, before, after) => {
  4700. if (modifier === before && modified[after]) {
  4701. this.raise(pos, TSErrors.InvalidModifiersOrder, before, after);
  4702. }
  4703. };
  4704. const incompatible = (pos, modifier, mod1, mod2) => {
  4705. if (modified[mod1] && modifier === mod2 || modified[mod2] && modifier === mod1) {
  4706. this.raise(pos, TSErrors.IncompatibleModifiers, mod1, mod2);
  4707. }
  4708. };
  4709. for (;;) {
  4710. const startPos = this.state.start;
  4711. const modifier = this.tsParseModifier(allowedModifiers.concat(disallowedModifiers != null ? disallowedModifiers : []));
  4712. if (!modifier) break;
  4713. if (tsIsAccessModifier(modifier)) {
  4714. if (modified.accessibility) {
  4715. this.raise(startPos, TSErrors.DuplicateAccessibilityModifier);
  4716. } else {
  4717. enforceOrder(startPos, modifier, modifier, "override");
  4718. enforceOrder(startPos, modifier, modifier, "static");
  4719. modified.accessibility = modifier;
  4720. }
  4721. } else {
  4722. if (Object.hasOwnProperty.call(modified, modifier)) {
  4723. this.raise(startPos, TSErrors.DuplicateModifier, modifier);
  4724. } else {
  4725. enforceOrder(startPos, modifier, "static", "readonly");
  4726. enforceOrder(startPos, modifier, "static", "override");
  4727. enforceOrder(startPos, modifier, "override", "readonly");
  4728. enforceOrder(startPos, modifier, "abstract", "override");
  4729. incompatible(startPos, modifier, "declare", "override");
  4730. incompatible(startPos, modifier, "static", "abstract");
  4731. }
  4732. modified[modifier] = true;
  4733. }
  4734. if (disallowedModifiers != null && disallowedModifiers.includes(modifier)) {
  4735. this.raise(startPos, errorTemplate, modifier);
  4736. }
  4737. }
  4738. }
  4739. tsIsListTerminator(kind) {
  4740. switch (kind) {
  4741. case "EnumMembers":
  4742. case "TypeMembers":
  4743. return this.match(types$1.braceR);
  4744. case "HeritageClauseElement":
  4745. return this.match(types$1.braceL);
  4746. case "TupleElementTypes":
  4747. return this.match(types$1.bracketR);
  4748. case "TypeParametersOrArguments":
  4749. return this.isRelational(">");
  4750. }
  4751. throw new Error("Unreachable");
  4752. }
  4753. tsParseList(kind, parseElement) {
  4754. const result = [];
  4755. while (!this.tsIsListTerminator(kind)) {
  4756. result.push(parseElement());
  4757. }
  4758. return result;
  4759. }
  4760. tsParseDelimitedList(kind, parseElement) {
  4761. return nonNull(this.tsParseDelimitedListWorker(kind, parseElement, true));
  4762. }
  4763. tsParseDelimitedListWorker(kind, parseElement, expectSuccess) {
  4764. const result = [];
  4765. for (;;) {
  4766. if (this.tsIsListTerminator(kind)) {
  4767. break;
  4768. }
  4769. const element = parseElement();
  4770. if (element == null) {
  4771. return undefined;
  4772. }
  4773. result.push(element);
  4774. if (this.eat(types$1.comma)) {
  4775. continue;
  4776. }
  4777. if (this.tsIsListTerminator(kind)) {
  4778. break;
  4779. }
  4780. if (expectSuccess) {
  4781. this.expect(types$1.comma);
  4782. }
  4783. return undefined;
  4784. }
  4785. return result;
  4786. }
  4787. tsParseBracketedList(kind, parseElement, bracket, skipFirstToken) {
  4788. if (!skipFirstToken) {
  4789. if (bracket) {
  4790. this.expect(types$1.bracketL);
  4791. } else {
  4792. this.expectRelational("<");
  4793. }
  4794. }
  4795. const result = this.tsParseDelimitedList(kind, parseElement);
  4796. if (bracket) {
  4797. this.expect(types$1.bracketR);
  4798. } else {
  4799. this.expectRelational(">");
  4800. }
  4801. return result;
  4802. }
  4803. tsParseImportType() {
  4804. const node = this.startNode();
  4805. this.expect(types$1._import);
  4806. this.expect(types$1.parenL);
  4807. if (!this.match(types$1.string)) {
  4808. this.raise(this.state.start, TSErrors.UnsupportedImportTypeArgument);
  4809. }
  4810. node.argument = this.parseExprAtom();
  4811. this.expect(types$1.parenR);
  4812. if (this.eat(types$1.dot)) {
  4813. node.qualifier = this.tsParseEntityName(true);
  4814. }
  4815. if (this.isRelational("<")) {
  4816. node.typeParameters = this.tsParseTypeArguments();
  4817. }
  4818. return this.finishNode(node, "TSImportType");
  4819. }
  4820. tsParseEntityName(allowReservedWords) {
  4821. let entity = this.parseIdentifier();
  4822. while (this.eat(types$1.dot)) {
  4823. const node = this.startNodeAtNode(entity);
  4824. node.left = entity;
  4825. node.right = this.parseIdentifier(allowReservedWords);
  4826. entity = this.finishNode(node, "TSQualifiedName");
  4827. }
  4828. return entity;
  4829. }
  4830. tsParseTypeReference() {
  4831. const node = this.startNode();
  4832. node.typeName = this.tsParseEntityName(false);
  4833. if (!this.hasPrecedingLineBreak() && this.isRelational("<")) {
  4834. node.typeParameters = this.tsParseTypeArguments();
  4835. }
  4836. return this.finishNode(node, "TSTypeReference");
  4837. }
  4838. tsParseThisTypePredicate(lhs) {
  4839. this.next();
  4840. const node = this.startNodeAtNode(lhs);
  4841. node.parameterName = lhs;
  4842. node.typeAnnotation = this.tsParseTypeAnnotation(false);
  4843. node.asserts = false;
  4844. return this.finishNode(node, "TSTypePredicate");
  4845. }
  4846. tsParseThisTypeNode() {
  4847. const node = this.startNode();
  4848. this.next();
  4849. return this.finishNode(node, "TSThisType");
  4850. }
  4851. tsParseTypeQuery() {
  4852. const node = this.startNode();
  4853. this.expect(types$1._typeof);
  4854. if (this.match(types$1._import)) {
  4855. node.exprName = this.tsParseImportType();
  4856. } else {
  4857. node.exprName = this.tsParseEntityName(true);
  4858. }
  4859. return this.finishNode(node, "TSTypeQuery");
  4860. }
  4861. tsParseTypeParameter() {
  4862. const node = this.startNode();
  4863. node.name = this.parseIdentifierName(node.start);
  4864. node.constraint = this.tsEatThenParseType(types$1._extends);
  4865. node.default = this.tsEatThenParseType(types$1.eq);
  4866. return this.finishNode(node, "TSTypeParameter");
  4867. }
  4868. tsTryParseTypeParameters() {
  4869. if (this.isRelational("<")) {
  4870. return this.tsParseTypeParameters();
  4871. }
  4872. }
  4873. tsParseTypeParameters() {
  4874. const node = this.startNode();
  4875. if (this.isRelational("<") || this.match(types$1.jsxTagStart)) {
  4876. this.next();
  4877. } else {
  4878. this.unexpected();
  4879. }
  4880. node.params = this.tsParseBracketedList("TypeParametersOrArguments", this.tsParseTypeParameter.bind(this), false, true);
  4881. if (node.params.length === 0) {
  4882. this.raise(node.start, TSErrors.EmptyTypeParameters);
  4883. }
  4884. return this.finishNode(node, "TSTypeParameterDeclaration");
  4885. }
  4886. tsTryNextParseConstantContext() {
  4887. if (this.lookahead().type === types$1._const) {
  4888. this.next();
  4889. return this.tsParseTypeReference();
  4890. }
  4891. return null;
  4892. }
  4893. tsFillSignature(returnToken, signature) {
  4894. const returnTokenRequired = returnToken === types$1.arrow;
  4895. signature.typeParameters = this.tsTryParseTypeParameters();
  4896. this.expect(types$1.parenL);
  4897. signature.parameters = this.tsParseBindingListForSignature();
  4898. if (returnTokenRequired) {
  4899. signature.typeAnnotation = this.tsParseTypeOrTypePredicateAnnotation(returnToken);
  4900. } else if (this.match(returnToken)) {
  4901. signature.typeAnnotation = this.tsParseTypeOrTypePredicateAnnotation(returnToken);
  4902. }
  4903. }
  4904. tsParseBindingListForSignature() {
  4905. return this.parseBindingList(types$1.parenR, 41).map(pattern => {
  4906. if (pattern.type !== "Identifier" && pattern.type !== "RestElement" && pattern.type !== "ObjectPattern" && pattern.type !== "ArrayPattern") {
  4907. this.raise(pattern.start, TSErrors.UnsupportedSignatureParameterKind, pattern.type);
  4908. }
  4909. return pattern;
  4910. });
  4911. }
  4912. tsParseTypeMemberSemicolon() {
  4913. if (!this.eat(types$1.comma) && !this.isLineTerminator()) {
  4914. this.expect(types$1.semi);
  4915. }
  4916. }
  4917. tsParseSignatureMember(kind, node) {
  4918. this.tsFillSignature(types$1.colon, node);
  4919. this.tsParseTypeMemberSemicolon();
  4920. return this.finishNode(node, kind);
  4921. }
  4922. tsIsUnambiguouslyIndexSignature() {
  4923. this.next();
  4924. return this.eat(types$1.name) && this.match(types$1.colon);
  4925. }
  4926. tsTryParseIndexSignature(node) {
  4927. if (!(this.match(types$1.bracketL) && this.tsLookAhead(this.tsIsUnambiguouslyIndexSignature.bind(this)))) {
  4928. return undefined;
  4929. }
  4930. this.expect(types$1.bracketL);
  4931. const id = this.parseIdentifier();
  4932. id.typeAnnotation = this.tsParseTypeAnnotation();
  4933. this.resetEndLocation(id);
  4934. this.expect(types$1.bracketR);
  4935. node.parameters = [id];
  4936. const type = this.tsTryParseTypeAnnotation();
  4937. if (type) node.typeAnnotation = type;
  4938. this.tsParseTypeMemberSemicolon();
  4939. return this.finishNode(node, "TSIndexSignature");
  4940. }
  4941. tsParsePropertyOrMethodSignature(node, readonly) {
  4942. if (this.eat(types$1.question)) node.optional = true;
  4943. const nodeAny = node;
  4944. if (this.match(types$1.parenL) || this.isRelational("<")) {
  4945. if (readonly) {
  4946. this.raise(node.start, TSErrors.ReadonlyForMethodSignature);
  4947. }
  4948. const method = nodeAny;
  4949. if (method.kind && this.isRelational("<")) {
  4950. this.raise(this.state.pos, TSErrors.AccesorCannotHaveTypeParameters);
  4951. }
  4952. this.tsFillSignature(types$1.colon, method);
  4953. this.tsParseTypeMemberSemicolon();
  4954. if (method.kind === "get") {
  4955. if (method.parameters.length > 0) {
  4956. this.raise(this.state.pos, ErrorMessages.BadGetterArity);
  4957. if (this.isThisParam(method.parameters[0])) {
  4958. this.raise(this.state.pos, TSErrors.AccesorCannotDeclareThisParameter);
  4959. }
  4960. }
  4961. } else if (method.kind === "set") {
  4962. if (method.parameters.length !== 1) {
  4963. this.raise(this.state.pos, ErrorMessages.BadSetterArity);
  4964. } else {
  4965. const firstParameter = method.parameters[0];
  4966. if (this.isThisParam(firstParameter)) {
  4967. this.raise(this.state.pos, TSErrors.AccesorCannotDeclareThisParameter);
  4968. }
  4969. if (firstParameter.type === "Identifier" && firstParameter.optional) {
  4970. this.raise(this.state.pos, TSErrors.SetAccesorCannotHaveOptionalParameter);
  4971. }
  4972. if (firstParameter.type === "RestElement") {
  4973. this.raise(this.state.pos, TSErrors.SetAccesorCannotHaveRestParameter);
  4974. }
  4975. }
  4976. if (method.typeAnnotation) {
  4977. this.raise(method.typeAnnotation.start, TSErrors.SetAccesorCannotHaveReturnType);
  4978. }
  4979. } else {
  4980. method.kind = "method";
  4981. }
  4982. return this.finishNode(method, "TSMethodSignature");
  4983. } else {
  4984. const property = nodeAny;
  4985. if (readonly) property.readonly = true;
  4986. const type = this.tsTryParseTypeAnnotation();
  4987. if (type) property.typeAnnotation = type;
  4988. this.tsParseTypeMemberSemicolon();
  4989. return this.finishNode(property, "TSPropertySignature");
  4990. }
  4991. }
  4992. tsParseTypeMember() {
  4993. const node = this.startNode();
  4994. if (this.match(types$1.parenL) || this.isRelational("<")) {
  4995. return this.tsParseSignatureMember("TSCallSignatureDeclaration", node);
  4996. }
  4997. if (this.match(types$1._new)) {
  4998. const id = this.startNode();
  4999. this.next();
  5000. if (this.match(types$1.parenL) || this.isRelational("<")) {
  5001. return this.tsParseSignatureMember("TSConstructSignatureDeclaration", node);
  5002. } else {
  5003. node.key = this.createIdentifier(id, "new");
  5004. return this.tsParsePropertyOrMethodSignature(node, false);
  5005. }
  5006. }
  5007. this.tsParseModifiers(node, ["readonly"], ["declare", "abstract", "private", "protected", "public", "static", "override"], TSErrors.InvalidModifierOnTypeMember);
  5008. const idx = this.tsTryParseIndexSignature(node);
  5009. if (idx) {
  5010. return idx;
  5011. }
  5012. this.parsePropertyName(node, false);
  5013. if (!node.computed && node.key.type === "Identifier" && (node.key.name === "get" || node.key.name === "set") && this.tsTokenCanFollowModifier()) {
  5014. node.kind = node.key.name;
  5015. this.parsePropertyName(node, false);
  5016. }
  5017. return this.tsParsePropertyOrMethodSignature(node, !!node.readonly);
  5018. }
  5019. tsParseTypeLiteral() {
  5020. const node = this.startNode();
  5021. node.members = this.tsParseObjectTypeMembers();
  5022. return this.finishNode(node, "TSTypeLiteral");
  5023. }
  5024. tsParseObjectTypeMembers() {
  5025. this.expect(types$1.braceL);
  5026. const members = this.tsParseList("TypeMembers", this.tsParseTypeMember.bind(this));
  5027. this.expect(types$1.braceR);
  5028. return members;
  5029. }
  5030. tsIsStartOfMappedType() {
  5031. this.next();
  5032. if (this.eat(types$1.plusMin)) {
  5033. return this.isContextual("readonly");
  5034. }
  5035. if (this.isContextual("readonly")) {
  5036. this.next();
  5037. }
  5038. if (!this.match(types$1.bracketL)) {
  5039. return false;
  5040. }
  5041. this.next();
  5042. if (!this.tsIsIdentifier()) {
  5043. return false;
  5044. }
  5045. this.next();
  5046. return this.match(types$1._in);
  5047. }
  5048. tsParseMappedTypeParameter() {
  5049. const node = this.startNode();
  5050. node.name = this.parseIdentifierName(node.start);
  5051. node.constraint = this.tsExpectThenParseType(types$1._in);
  5052. return this.finishNode(node, "TSTypeParameter");
  5053. }
  5054. tsParseMappedType() {
  5055. const node = this.startNode();
  5056. this.expect(types$1.braceL);
  5057. if (this.match(types$1.plusMin)) {
  5058. node.readonly = this.state.value;
  5059. this.next();
  5060. this.expectContextual("readonly");
  5061. } else if (this.eatContextual("readonly")) {
  5062. node.readonly = true;
  5063. }
  5064. this.expect(types$1.bracketL);
  5065. node.typeParameter = this.tsParseMappedTypeParameter();
  5066. node.nameType = this.eatContextual("as") ? this.tsParseType() : null;
  5067. this.expect(types$1.bracketR);
  5068. if (this.match(types$1.plusMin)) {
  5069. node.optional = this.state.value;
  5070. this.next();
  5071. this.expect(types$1.question);
  5072. } else if (this.eat(types$1.question)) {
  5073. node.optional = true;
  5074. }
  5075. node.typeAnnotation = this.tsTryParseType();
  5076. this.semicolon();
  5077. this.expect(types$1.braceR);
  5078. return this.finishNode(node, "TSMappedType");
  5079. }
  5080. tsParseTupleType() {
  5081. const node = this.startNode();
  5082. node.elementTypes = this.tsParseBracketedList("TupleElementTypes", this.tsParseTupleElementType.bind(this), true, false);
  5083. let seenOptionalElement = false;
  5084. let labeledElements = null;
  5085. node.elementTypes.forEach(elementNode => {
  5086. var _labeledElements;
  5087. let {
  5088. type
  5089. } = elementNode;
  5090. if (seenOptionalElement && type !== "TSRestType" && type !== "TSOptionalType" && !(type === "TSNamedTupleMember" && elementNode.optional)) {
  5091. this.raise(elementNode.start, TSErrors.OptionalTypeBeforeRequired);
  5092. }
  5093. seenOptionalElement = seenOptionalElement || type === "TSNamedTupleMember" && elementNode.optional || type === "TSOptionalType";
  5094. if (type === "TSRestType") {
  5095. elementNode = elementNode.typeAnnotation;
  5096. type = elementNode.type;
  5097. }
  5098. const isLabeled = type === "TSNamedTupleMember";
  5099. labeledElements = (_labeledElements = labeledElements) != null ? _labeledElements : isLabeled;
  5100. if (labeledElements !== isLabeled) {
  5101. this.raise(elementNode.start, TSErrors.MixedLabeledAndUnlabeledElements);
  5102. }
  5103. });
  5104. return this.finishNode(node, "TSTupleType");
  5105. }
  5106. tsParseTupleElementType() {
  5107. const {
  5108. start: startPos,
  5109. startLoc
  5110. } = this.state;
  5111. const rest = this.eat(types$1.ellipsis);
  5112. let type = this.tsParseType();
  5113. const optional = this.eat(types$1.question);
  5114. const labeled = this.eat(types$1.colon);
  5115. if (labeled) {
  5116. const labeledNode = this.startNodeAtNode(type);
  5117. labeledNode.optional = optional;
  5118. if (type.type === "TSTypeReference" && !type.typeParameters && type.typeName.type === "Identifier") {
  5119. labeledNode.label = type.typeName;
  5120. } else {
  5121. this.raise(type.start, TSErrors.InvalidTupleMemberLabel);
  5122. labeledNode.label = type;
  5123. }
  5124. labeledNode.elementType = this.tsParseType();
  5125. type = this.finishNode(labeledNode, "TSNamedTupleMember");
  5126. } else if (optional) {
  5127. const optionalTypeNode = this.startNodeAtNode(type);
  5128. optionalTypeNode.typeAnnotation = type;
  5129. type = this.finishNode(optionalTypeNode, "TSOptionalType");
  5130. }
  5131. if (rest) {
  5132. const restNode = this.startNodeAt(startPos, startLoc);
  5133. restNode.typeAnnotation = type;
  5134. type = this.finishNode(restNode, "TSRestType");
  5135. }
  5136. return type;
  5137. }
  5138. tsParseParenthesizedType() {
  5139. const node = this.startNode();
  5140. this.expect(types$1.parenL);
  5141. node.typeAnnotation = this.tsParseType();
  5142. this.expect(types$1.parenR);
  5143. return this.finishNode(node, "TSParenthesizedType");
  5144. }
  5145. tsParseFunctionOrConstructorType(type, abstract) {
  5146. const node = this.startNode();
  5147. if (type === "TSConstructorType") {
  5148. node.abstract = !!abstract;
  5149. if (abstract) this.next();
  5150. this.next();
  5151. }
  5152. this.tsFillSignature(types$1.arrow, node);
  5153. return this.finishNode(node, type);
  5154. }
  5155. tsParseLiteralTypeNode() {
  5156. const node = this.startNode();
  5157. node.literal = (() => {
  5158. switch (this.state.type) {
  5159. case types$1.num:
  5160. case types$1.bigint:
  5161. case types$1.string:
  5162. case types$1._true:
  5163. case types$1._false:
  5164. return this.parseExprAtom();
  5165. default:
  5166. throw this.unexpected();
  5167. }
  5168. })();
  5169. return this.finishNode(node, "TSLiteralType");
  5170. }
  5171. tsParseTemplateLiteralType() {
  5172. const node = this.startNode();
  5173. node.literal = this.parseTemplate(false);
  5174. return this.finishNode(node, "TSLiteralType");
  5175. }
  5176. parseTemplateSubstitution() {
  5177. if (this.state.inType) return this.tsParseType();
  5178. return super.parseTemplateSubstitution();
  5179. }
  5180. tsParseThisTypeOrThisTypePredicate() {
  5181. const thisKeyword = this.tsParseThisTypeNode();
  5182. if (this.isContextual("is") && !this.hasPrecedingLineBreak()) {
  5183. return this.tsParseThisTypePredicate(thisKeyword);
  5184. } else {
  5185. return thisKeyword;
  5186. }
  5187. }
  5188. tsParseNonArrayType() {
  5189. switch (this.state.type) {
  5190. case types$1.name:
  5191. case types$1._void:
  5192. case types$1._null:
  5193. {
  5194. const type = this.match(types$1._void) ? "TSVoidKeyword" : this.match(types$1._null) ? "TSNullKeyword" : keywordTypeFromName(this.state.value);
  5195. if (type !== undefined && this.lookaheadCharCode() !== 46) {
  5196. const node = this.startNode();
  5197. this.next();
  5198. return this.finishNode(node, type);
  5199. }
  5200. return this.tsParseTypeReference();
  5201. }
  5202. case types$1.string:
  5203. case types$1.num:
  5204. case types$1.bigint:
  5205. case types$1._true:
  5206. case types$1._false:
  5207. return this.tsParseLiteralTypeNode();
  5208. case types$1.plusMin:
  5209. if (this.state.value === "-") {
  5210. const node = this.startNode();
  5211. const nextToken = this.lookahead();
  5212. if (nextToken.type !== types$1.num && nextToken.type !== types$1.bigint) {
  5213. throw this.unexpected();
  5214. }
  5215. node.literal = this.parseMaybeUnary();
  5216. return this.finishNode(node, "TSLiteralType");
  5217. }
  5218. break;
  5219. case types$1._this:
  5220. return this.tsParseThisTypeOrThisTypePredicate();
  5221. case types$1._typeof:
  5222. return this.tsParseTypeQuery();
  5223. case types$1._import:
  5224. return this.tsParseImportType();
  5225. case types$1.braceL:
  5226. return this.tsLookAhead(this.tsIsStartOfMappedType.bind(this)) ? this.tsParseMappedType() : this.tsParseTypeLiteral();
  5227. case types$1.bracketL:
  5228. return this.tsParseTupleType();
  5229. case types$1.parenL:
  5230. return this.tsParseParenthesizedType();
  5231. case types$1.backQuote:
  5232. return this.tsParseTemplateLiteralType();
  5233. }
  5234. throw this.unexpected();
  5235. }
  5236. tsParseArrayTypeOrHigher() {
  5237. let type = this.tsParseNonArrayType();
  5238. while (!this.hasPrecedingLineBreak() && this.eat(types$1.bracketL)) {
  5239. if (this.match(types$1.bracketR)) {
  5240. const node = this.startNodeAtNode(type);
  5241. node.elementType = type;
  5242. this.expect(types$1.bracketR);
  5243. type = this.finishNode(node, "TSArrayType");
  5244. } else {
  5245. const node = this.startNodeAtNode(type);
  5246. node.objectType = type;
  5247. node.indexType = this.tsParseType();
  5248. this.expect(types$1.bracketR);
  5249. type = this.finishNode(node, "TSIndexedAccessType");
  5250. }
  5251. }
  5252. return type;
  5253. }
  5254. tsParseTypeOperator(operator) {
  5255. const node = this.startNode();
  5256. this.expectContextual(operator);
  5257. node.operator = operator;
  5258. node.typeAnnotation = this.tsParseTypeOperatorOrHigher();
  5259. if (operator === "readonly") {
  5260. this.tsCheckTypeAnnotationForReadOnly(node);
  5261. }
  5262. return this.finishNode(node, "TSTypeOperator");
  5263. }
  5264. tsCheckTypeAnnotationForReadOnly(node) {
  5265. switch (node.typeAnnotation.type) {
  5266. case "TSTupleType":
  5267. case "TSArrayType":
  5268. return;
  5269. default:
  5270. this.raise(node.start, TSErrors.UnexpectedReadonly);
  5271. }
  5272. }
  5273. tsParseInferType() {
  5274. const node = this.startNode();
  5275. this.expectContextual("infer");
  5276. const typeParameter = this.startNode();
  5277. typeParameter.name = this.parseIdentifierName(typeParameter.start);
  5278. node.typeParameter = this.finishNode(typeParameter, "TSTypeParameter");
  5279. return this.finishNode(node, "TSInferType");
  5280. }
  5281. tsParseTypeOperatorOrHigher() {
  5282. const operator = ["keyof", "unique", "readonly"].find(kw => this.isContextual(kw));
  5283. return operator ? this.tsParseTypeOperator(operator) : this.isContextual("infer") ? this.tsParseInferType() : this.tsParseArrayTypeOrHigher();
  5284. }
  5285. tsParseUnionOrIntersectionType(kind, parseConstituentType, operator) {
  5286. const node = this.startNode();
  5287. const hasLeadingOperator = this.eat(operator);
  5288. const types = [];
  5289. do {
  5290. types.push(parseConstituentType());
  5291. } while (this.eat(operator));
  5292. if (types.length === 1 && !hasLeadingOperator) {
  5293. return types[0];
  5294. }
  5295. node.types = types;
  5296. return this.finishNode(node, kind);
  5297. }
  5298. tsParseIntersectionTypeOrHigher() {
  5299. return this.tsParseUnionOrIntersectionType("TSIntersectionType", this.tsParseTypeOperatorOrHigher.bind(this), types$1.bitwiseAND);
  5300. }
  5301. tsParseUnionTypeOrHigher() {
  5302. return this.tsParseUnionOrIntersectionType("TSUnionType", this.tsParseIntersectionTypeOrHigher.bind(this), types$1.bitwiseOR);
  5303. }
  5304. tsIsStartOfFunctionType() {
  5305. if (this.isRelational("<")) {
  5306. return true;
  5307. }
  5308. return this.match(types$1.parenL) && this.tsLookAhead(this.tsIsUnambiguouslyStartOfFunctionType.bind(this));
  5309. }
  5310. tsSkipParameterStart() {
  5311. if (this.match(types$1.name) || this.match(types$1._this)) {
  5312. this.next();
  5313. return true;
  5314. }
  5315. if (this.match(types$1.braceL)) {
  5316. let braceStackCounter = 1;
  5317. this.next();
  5318. while (braceStackCounter > 0) {
  5319. if (this.match(types$1.braceL)) {
  5320. ++braceStackCounter;
  5321. } else if (this.match(types$1.braceR)) {
  5322. --braceStackCounter;
  5323. }
  5324. this.next();
  5325. }
  5326. return true;
  5327. }
  5328. if (this.match(types$1.bracketL)) {
  5329. let braceStackCounter = 1;
  5330. this.next();
  5331. while (braceStackCounter > 0) {
  5332. if (this.match(types$1.bracketL)) {
  5333. ++braceStackCounter;
  5334. } else if (this.match(types$1.bracketR)) {
  5335. --braceStackCounter;
  5336. }
  5337. this.next();
  5338. }
  5339. return true;
  5340. }
  5341. return false;
  5342. }
  5343. tsIsUnambiguouslyStartOfFunctionType() {
  5344. this.next();
  5345. if (this.match(types$1.parenR) || this.match(types$1.ellipsis)) {
  5346. return true;
  5347. }
  5348. if (this.tsSkipParameterStart()) {
  5349. if (this.match(types$1.colon) || this.match(types$1.comma) || this.match(types$1.question) || this.match(types$1.eq)) {
  5350. return true;
  5351. }
  5352. if (this.match(types$1.parenR)) {
  5353. this.next();
  5354. if (this.match(types$1.arrow)) {
  5355. return true;
  5356. }
  5357. }
  5358. }
  5359. return false;
  5360. }
  5361. tsParseTypeOrTypePredicateAnnotation(returnToken) {
  5362. return this.tsInType(() => {
  5363. const t = this.startNode();
  5364. this.expect(returnToken);
  5365. const node = this.startNode();
  5366. const asserts = !!this.tsTryParse(this.tsParseTypePredicateAsserts.bind(this));
  5367. if (asserts && this.match(types$1._this)) {
  5368. let thisTypePredicate = this.tsParseThisTypeOrThisTypePredicate();
  5369. if (thisTypePredicate.type === "TSThisType") {
  5370. node.parameterName = thisTypePredicate;
  5371. node.asserts = true;
  5372. node.typeAnnotation = null;
  5373. thisTypePredicate = this.finishNode(node, "TSTypePredicate");
  5374. } else {
  5375. this.resetStartLocationFromNode(thisTypePredicate, node);
  5376. thisTypePredicate.asserts = true;
  5377. }
  5378. t.typeAnnotation = thisTypePredicate;
  5379. return this.finishNode(t, "TSTypeAnnotation");
  5380. }
  5381. const typePredicateVariable = this.tsIsIdentifier() && this.tsTryParse(this.tsParseTypePredicatePrefix.bind(this));
  5382. if (!typePredicateVariable) {
  5383. if (!asserts) {
  5384. return this.tsParseTypeAnnotation(false, t);
  5385. }
  5386. node.parameterName = this.parseIdentifier();
  5387. node.asserts = asserts;
  5388. node.typeAnnotation = null;
  5389. t.typeAnnotation = this.finishNode(node, "TSTypePredicate");
  5390. return this.finishNode(t, "TSTypeAnnotation");
  5391. }
  5392. const type = this.tsParseTypeAnnotation(false);
  5393. node.parameterName = typePredicateVariable;
  5394. node.typeAnnotation = type;
  5395. node.asserts = asserts;
  5396. t.typeAnnotation = this.finishNode(node, "TSTypePredicate");
  5397. return this.finishNode(t, "TSTypeAnnotation");
  5398. });
  5399. }
  5400. tsTryParseTypeOrTypePredicateAnnotation() {
  5401. return this.match(types$1.colon) ? this.tsParseTypeOrTypePredicateAnnotation(types$1.colon) : undefined;
  5402. }
  5403. tsTryParseTypeAnnotation() {
  5404. return this.match(types$1.colon) ? this.tsParseTypeAnnotation() : undefined;
  5405. }
  5406. tsTryParseType() {
  5407. return this.tsEatThenParseType(types$1.colon);
  5408. }
  5409. tsParseTypePredicatePrefix() {
  5410. const id = this.parseIdentifier();
  5411. if (this.isContextual("is") && !this.hasPrecedingLineBreak()) {
  5412. this.next();
  5413. return id;
  5414. }
  5415. }
  5416. tsParseTypePredicateAsserts() {
  5417. if (!this.match(types$1.name) || this.state.value !== "asserts" || this.hasPrecedingLineBreak()) {
  5418. return false;
  5419. }
  5420. const containsEsc = this.state.containsEsc;
  5421. this.next();
  5422. if (!this.match(types$1.name) && !this.match(types$1._this)) {
  5423. return false;
  5424. }
  5425. if (containsEsc) {
  5426. this.raise(this.state.lastTokStart, ErrorMessages.InvalidEscapedReservedWord, "asserts");
  5427. }
  5428. return true;
  5429. }
  5430. tsParseTypeAnnotation(eatColon = true, t = this.startNode()) {
  5431. this.tsInType(() => {
  5432. if (eatColon) this.expect(types$1.colon);
  5433. t.typeAnnotation = this.tsParseType();
  5434. });
  5435. return this.finishNode(t, "TSTypeAnnotation");
  5436. }
  5437. tsParseType() {
  5438. assert(this.state.inType);
  5439. const type = this.tsParseNonConditionalType();
  5440. if (this.hasPrecedingLineBreak() || !this.eat(types$1._extends)) {
  5441. return type;
  5442. }
  5443. const node = this.startNodeAtNode(type);
  5444. node.checkType = type;
  5445. node.extendsType = this.tsParseNonConditionalType();
  5446. this.expect(types$1.question);
  5447. node.trueType = this.tsParseType();
  5448. this.expect(types$1.colon);
  5449. node.falseType = this.tsParseType();
  5450. return this.finishNode(node, "TSConditionalType");
  5451. }
  5452. isAbstractConstructorSignature() {
  5453. return this.isContextual("abstract") && this.lookahead().type === types$1._new;
  5454. }
  5455. tsParseNonConditionalType() {
  5456. if (this.tsIsStartOfFunctionType()) {
  5457. return this.tsParseFunctionOrConstructorType("TSFunctionType");
  5458. }
  5459. if (this.match(types$1._new)) {
  5460. return this.tsParseFunctionOrConstructorType("TSConstructorType");
  5461. } else if (this.isAbstractConstructorSignature()) {
  5462. return this.tsParseFunctionOrConstructorType("TSConstructorType", true);
  5463. }
  5464. return this.tsParseUnionTypeOrHigher();
  5465. }
  5466. tsParseTypeAssertion() {
  5467. const node = this.startNode();
  5468. const _const = this.tsTryNextParseConstantContext();
  5469. node.typeAnnotation = _const || this.tsNextThenParseType();
  5470. this.expectRelational(">");
  5471. node.expression = this.parseMaybeUnary();
  5472. return this.finishNode(node, "TSTypeAssertion");
  5473. }
  5474. tsParseHeritageClause(descriptor) {
  5475. const originalStart = this.state.start;
  5476. const delimitedList = this.tsParseDelimitedList("HeritageClauseElement", this.tsParseExpressionWithTypeArguments.bind(this));
  5477. if (!delimitedList.length) {
  5478. this.raise(originalStart, TSErrors.EmptyHeritageClauseType, descriptor);
  5479. }
  5480. return delimitedList;
  5481. }
  5482. tsParseExpressionWithTypeArguments() {
  5483. const node = this.startNode();
  5484. node.expression = this.tsParseEntityName(false);
  5485. if (this.isRelational("<")) {
  5486. node.typeParameters = this.tsParseTypeArguments();
  5487. }
  5488. return this.finishNode(node, "TSExpressionWithTypeArguments");
  5489. }
  5490. tsParseInterfaceDeclaration(node) {
  5491. node.id = this.parseIdentifier();
  5492. this.checkLVal(node.id, "typescript interface declaration", BIND_TS_INTERFACE);
  5493. node.typeParameters = this.tsTryParseTypeParameters();
  5494. if (this.eat(types$1._extends)) {
  5495. node.extends = this.tsParseHeritageClause("extends");
  5496. }
  5497. const body = this.startNode();
  5498. body.body = this.tsInType(this.tsParseObjectTypeMembers.bind(this));
  5499. node.body = this.finishNode(body, "TSInterfaceBody");
  5500. return this.finishNode(node, "TSInterfaceDeclaration");
  5501. }
  5502. tsParseTypeAliasDeclaration(node) {
  5503. node.id = this.parseIdentifier();
  5504. this.checkLVal(node.id, "typescript type alias", BIND_TS_TYPE);
  5505. node.typeParameters = this.tsTryParseTypeParameters();
  5506. node.typeAnnotation = this.tsInType(() => {
  5507. this.expect(types$1.eq);
  5508. if (this.isContextual("intrinsic") && this.lookahead().type !== types$1.dot) {
  5509. const node = this.startNode();
  5510. this.next();
  5511. return this.finishNode(node, "TSIntrinsicKeyword");
  5512. }
  5513. return this.tsParseType();
  5514. });
  5515. this.semicolon();
  5516. return this.finishNode(node, "TSTypeAliasDeclaration");
  5517. }
  5518. tsInNoContext(cb) {
  5519. const oldContext = this.state.context;
  5520. this.state.context = [oldContext[0]];
  5521. try {
  5522. return cb();
  5523. } finally {
  5524. this.state.context = oldContext;
  5525. }
  5526. }
  5527. tsInType(cb) {
  5528. const oldInType = this.state.inType;
  5529. this.state.inType = true;
  5530. try {
  5531. return cb();
  5532. } finally {
  5533. this.state.inType = oldInType;
  5534. }
  5535. }
  5536. tsEatThenParseType(token) {
  5537. return !this.match(token) ? undefined : this.tsNextThenParseType();
  5538. }
  5539. tsExpectThenParseType(token) {
  5540. return this.tsDoThenParseType(() => this.expect(token));
  5541. }
  5542. tsNextThenParseType() {
  5543. return this.tsDoThenParseType(() => this.next());
  5544. }
  5545. tsDoThenParseType(cb) {
  5546. return this.tsInType(() => {
  5547. cb();
  5548. return this.tsParseType();
  5549. });
  5550. }
  5551. tsParseEnumMember() {
  5552. const node = this.startNode();
  5553. node.id = this.match(types$1.string) ? this.parseExprAtom() : this.parseIdentifier(true);
  5554. if (this.eat(types$1.eq)) {
  5555. node.initializer = this.parseMaybeAssignAllowIn();
  5556. }
  5557. return this.finishNode(node, "TSEnumMember");
  5558. }
  5559. tsParseEnumDeclaration(node, isConst) {
  5560. if (isConst) node.const = true;
  5561. node.id = this.parseIdentifier();
  5562. this.checkLVal(node.id, "typescript enum declaration", isConst ? BIND_TS_CONST_ENUM : BIND_TS_ENUM);
  5563. this.expect(types$1.braceL);
  5564. node.members = this.tsParseDelimitedList("EnumMembers", this.tsParseEnumMember.bind(this));
  5565. this.expect(types$1.braceR);
  5566. return this.finishNode(node, "TSEnumDeclaration");
  5567. }
  5568. tsParseModuleBlock() {
  5569. const node = this.startNode();
  5570. this.scope.enter(SCOPE_OTHER);
  5571. this.expect(types$1.braceL);
  5572. this.parseBlockOrModuleBlockBody(node.body = [], undefined, true, types$1.braceR);
  5573. this.scope.exit();
  5574. return this.finishNode(node, "TSModuleBlock");
  5575. }
  5576. tsParseModuleOrNamespaceDeclaration(node, nested = false) {
  5577. node.id = this.parseIdentifier();
  5578. if (!nested) {
  5579. this.checkLVal(node.id, "module or namespace declaration", BIND_TS_NAMESPACE);
  5580. }
  5581. if (this.eat(types$1.dot)) {
  5582. const inner = this.startNode();
  5583. this.tsParseModuleOrNamespaceDeclaration(inner, true);
  5584. node.body = inner;
  5585. } else {
  5586. this.scope.enter(SCOPE_TS_MODULE);
  5587. this.prodParam.enter(PARAM);
  5588. node.body = this.tsParseModuleBlock();
  5589. this.prodParam.exit();
  5590. this.scope.exit();
  5591. }
  5592. return this.finishNode(node, "TSModuleDeclaration");
  5593. }
  5594. tsParseAmbientExternalModuleDeclaration(node) {
  5595. if (this.isContextual("global")) {
  5596. node.global = true;
  5597. node.id = this.parseIdentifier();
  5598. } else if (this.match(types$1.string)) {
  5599. node.id = this.parseExprAtom();
  5600. } else {
  5601. this.unexpected();
  5602. }
  5603. if (this.match(types$1.braceL)) {
  5604. this.scope.enter(SCOPE_TS_MODULE);
  5605. this.prodParam.enter(PARAM);
  5606. node.body = this.tsParseModuleBlock();
  5607. this.prodParam.exit();
  5608. this.scope.exit();
  5609. } else {
  5610. this.semicolon();
  5611. }
  5612. return this.finishNode(node, "TSModuleDeclaration");
  5613. }
  5614. tsParseImportEqualsDeclaration(node, isExport) {
  5615. node.isExport = isExport || false;
  5616. node.id = this.parseIdentifier();
  5617. this.checkLVal(node.id, "import equals declaration", BIND_LEXICAL);
  5618. this.expect(types$1.eq);
  5619. const moduleReference = this.tsParseModuleReference();
  5620. if (node.importKind === "type" && moduleReference.type !== "TSExternalModuleReference") {
  5621. this.raise(moduleReference.start, TSErrors.ImportAliasHasImportType);
  5622. }
  5623. node.moduleReference = moduleReference;
  5624. this.semicolon();
  5625. return this.finishNode(node, "TSImportEqualsDeclaration");
  5626. }
  5627. tsIsExternalModuleReference() {
  5628. return this.isContextual("require") && this.lookaheadCharCode() === 40;
  5629. }
  5630. tsParseModuleReference() {
  5631. return this.tsIsExternalModuleReference() ? this.tsParseExternalModuleReference() : this.tsParseEntityName(false);
  5632. }
  5633. tsParseExternalModuleReference() {
  5634. const node = this.startNode();
  5635. this.expectContextual("require");
  5636. this.expect(types$1.parenL);
  5637. if (!this.match(types$1.string)) {
  5638. throw this.unexpected();
  5639. }
  5640. node.expression = this.parseExprAtom();
  5641. this.expect(types$1.parenR);
  5642. return this.finishNode(node, "TSExternalModuleReference");
  5643. }
  5644. tsLookAhead(f) {
  5645. const state = this.state.clone();
  5646. const res = f();
  5647. this.state = state;
  5648. return res;
  5649. }
  5650. tsTryParseAndCatch(f) {
  5651. const result = this.tryParse(abort => f() || abort());
  5652. if (result.aborted || !result.node) return undefined;
  5653. if (result.error) this.state = result.failState;
  5654. return result.node;
  5655. }
  5656. tsTryParse(f) {
  5657. const state = this.state.clone();
  5658. const result = f();
  5659. if (result !== undefined && result !== false) {
  5660. return result;
  5661. } else {
  5662. this.state = state;
  5663. return undefined;
  5664. }
  5665. }
  5666. tsTryParseDeclare(nany) {
  5667. if (this.isLineTerminator()) {
  5668. return;
  5669. }
  5670. let starttype = this.state.type;
  5671. let kind;
  5672. if (this.isContextual("let")) {
  5673. starttype = types$1._var;
  5674. kind = "let";
  5675. }
  5676. return this.tsInAmbientContext(() => {
  5677. switch (starttype) {
  5678. case types$1._function:
  5679. nany.declare = true;
  5680. return this.parseFunctionStatement(nany, false, true);
  5681. case types$1._class:
  5682. nany.declare = true;
  5683. return this.parseClass(nany, true, false);
  5684. case types$1._const:
  5685. if (this.match(types$1._const) && this.isLookaheadContextual("enum")) {
  5686. this.expect(types$1._const);
  5687. this.expectContextual("enum");
  5688. return this.tsParseEnumDeclaration(nany, true);
  5689. }
  5690. case types$1._var:
  5691. kind = kind || this.state.value;
  5692. return this.parseVarStatement(nany, kind);
  5693. case types$1.name:
  5694. {
  5695. const value = this.state.value;
  5696. if (value === "global") {
  5697. return this.tsParseAmbientExternalModuleDeclaration(nany);
  5698. } else {
  5699. return this.tsParseDeclaration(nany, value, true);
  5700. }
  5701. }
  5702. }
  5703. });
  5704. }
  5705. tsTryParseExportDeclaration() {
  5706. return this.tsParseDeclaration(this.startNode(), this.state.value, true);
  5707. }
  5708. tsParseExpressionStatement(node, expr) {
  5709. switch (expr.name) {
  5710. case "declare":
  5711. {
  5712. const declaration = this.tsTryParseDeclare(node);
  5713. if (declaration) {
  5714. declaration.declare = true;
  5715. return declaration;
  5716. }
  5717. break;
  5718. }
  5719. case "global":
  5720. if (this.match(types$1.braceL)) {
  5721. this.scope.enter(SCOPE_TS_MODULE);
  5722. this.prodParam.enter(PARAM);
  5723. const mod = node;
  5724. mod.global = true;
  5725. mod.id = expr;
  5726. mod.body = this.tsParseModuleBlock();
  5727. this.scope.exit();
  5728. this.prodParam.exit();
  5729. return this.finishNode(mod, "TSModuleDeclaration");
  5730. }
  5731. break;
  5732. default:
  5733. return this.tsParseDeclaration(node, expr.name, false);
  5734. }
  5735. }
  5736. tsParseDeclaration(node, value, next) {
  5737. switch (value) {
  5738. case "abstract":
  5739. if (this.tsCheckLineTerminator(next) && (this.match(types$1._class) || this.match(types$1.name))) {
  5740. return this.tsParseAbstractDeclaration(node);
  5741. }
  5742. break;
  5743. case "enum":
  5744. if (next || this.match(types$1.name)) {
  5745. if (next) this.next();
  5746. return this.tsParseEnumDeclaration(node, false);
  5747. }
  5748. break;
  5749. case "interface":
  5750. if (this.tsCheckLineTerminator(next) && this.match(types$1.name)) {
  5751. return this.tsParseInterfaceDeclaration(node);
  5752. }
  5753. break;
  5754. case "module":
  5755. if (this.tsCheckLineTerminator(next)) {
  5756. if (this.match(types$1.string)) {
  5757. return this.tsParseAmbientExternalModuleDeclaration(node);
  5758. } else if (this.match(types$1.name)) {
  5759. return this.tsParseModuleOrNamespaceDeclaration(node);
  5760. }
  5761. }
  5762. break;
  5763. case "namespace":
  5764. if (this.tsCheckLineTerminator(next) && this.match(types$1.name)) {
  5765. return this.tsParseModuleOrNamespaceDeclaration(node);
  5766. }
  5767. break;
  5768. case "type":
  5769. if (this.tsCheckLineTerminator(next) && this.match(types$1.name)) {
  5770. return this.tsParseTypeAliasDeclaration(node);
  5771. }
  5772. break;
  5773. }
  5774. }
  5775. tsCheckLineTerminator(next) {
  5776. if (next) {
  5777. if (this.hasFollowingLineBreak()) return false;
  5778. this.next();
  5779. return true;
  5780. }
  5781. return !this.isLineTerminator();
  5782. }
  5783. tsTryParseGenericAsyncArrowFunction(startPos, startLoc) {
  5784. if (!this.isRelational("<")) {
  5785. return undefined;
  5786. }
  5787. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  5788. this.state.maybeInArrowParameters = true;
  5789. const res = this.tsTryParseAndCatch(() => {
  5790. const node = this.startNodeAt(startPos, startLoc);
  5791. node.typeParameters = this.tsParseTypeParameters();
  5792. super.parseFunctionParams(node);
  5793. node.returnType = this.tsTryParseTypeOrTypePredicateAnnotation();
  5794. this.expect(types$1.arrow);
  5795. return node;
  5796. });
  5797. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  5798. if (!res) {
  5799. return undefined;
  5800. }
  5801. return this.parseArrowExpression(res, null, true);
  5802. }
  5803. tsParseTypeArguments() {
  5804. const node = this.startNode();
  5805. node.params = this.tsInType(() => this.tsInNoContext(() => {
  5806. this.expectRelational("<");
  5807. return this.tsParseDelimitedList("TypeParametersOrArguments", this.tsParseType.bind(this));
  5808. }));
  5809. if (node.params.length === 0) {
  5810. this.raise(node.start, TSErrors.EmptyTypeArguments);
  5811. }
  5812. this.state.exprAllowed = false;
  5813. this.expectRelational(">");
  5814. return this.finishNode(node, "TSTypeParameterInstantiation");
  5815. }
  5816. tsIsDeclarationStart() {
  5817. if (this.match(types$1.name)) {
  5818. switch (this.state.value) {
  5819. case "abstract":
  5820. case "declare":
  5821. case "enum":
  5822. case "interface":
  5823. case "module":
  5824. case "namespace":
  5825. case "type":
  5826. return true;
  5827. }
  5828. }
  5829. return false;
  5830. }
  5831. isExportDefaultSpecifier() {
  5832. if (this.tsIsDeclarationStart()) return false;
  5833. return super.isExportDefaultSpecifier();
  5834. }
  5835. parseAssignableListItem(allowModifiers, decorators) {
  5836. const startPos = this.state.start;
  5837. const startLoc = this.state.startLoc;
  5838. let accessibility;
  5839. let readonly = false;
  5840. if (allowModifiers !== undefined) {
  5841. accessibility = this.parseAccessModifier();
  5842. readonly = !!this.tsParseModifier(["readonly"]);
  5843. if (allowModifiers === false && (accessibility || readonly)) {
  5844. this.raise(startPos, TSErrors.UnexpectedParameterModifier);
  5845. }
  5846. }
  5847. const left = this.parseMaybeDefault();
  5848. this.parseAssignableListItemTypes(left);
  5849. const elt = this.parseMaybeDefault(left.start, left.loc.start, left);
  5850. if (accessibility || readonly) {
  5851. const pp = this.startNodeAt(startPos, startLoc);
  5852. if (decorators.length) {
  5853. pp.decorators = decorators;
  5854. }
  5855. if (accessibility) pp.accessibility = accessibility;
  5856. if (readonly) pp.readonly = readonly;
  5857. if (elt.type !== "Identifier" && elt.type !== "AssignmentPattern") {
  5858. this.raise(pp.start, TSErrors.UnsupportedParameterPropertyKind);
  5859. }
  5860. pp.parameter = elt;
  5861. return this.finishNode(pp, "TSParameterProperty");
  5862. }
  5863. if (decorators.length) {
  5864. left.decorators = decorators;
  5865. }
  5866. return elt;
  5867. }
  5868. parseFunctionBodyAndFinish(node, type, isMethod = false) {
  5869. if (this.match(types$1.colon)) {
  5870. node.returnType = this.tsParseTypeOrTypePredicateAnnotation(types$1.colon);
  5871. }
  5872. const bodilessType = type === "FunctionDeclaration" ? "TSDeclareFunction" : type === "ClassMethod" ? "TSDeclareMethod" : undefined;
  5873. if (bodilessType && !this.match(types$1.braceL) && this.isLineTerminator()) {
  5874. this.finishNode(node, bodilessType);
  5875. return;
  5876. }
  5877. if (bodilessType === "TSDeclareFunction" && this.state.isAmbientContext) {
  5878. this.raise(node.start, TSErrors.DeclareFunctionHasImplementation);
  5879. if (node.declare) {
  5880. super.parseFunctionBodyAndFinish(node, bodilessType, isMethod);
  5881. return;
  5882. }
  5883. }
  5884. super.parseFunctionBodyAndFinish(node, type, isMethod);
  5885. }
  5886. registerFunctionStatementId(node) {
  5887. if (!node.body && node.id) {
  5888. this.checkLVal(node.id, "function name", BIND_TS_AMBIENT);
  5889. } else {
  5890. super.registerFunctionStatementId(...arguments);
  5891. }
  5892. }
  5893. tsCheckForInvalidTypeCasts(items) {
  5894. items.forEach(node => {
  5895. if ((node == null ? void 0 : node.type) === "TSTypeCastExpression") {
  5896. this.raise(node.typeAnnotation.start, TSErrors.UnexpectedTypeAnnotation);
  5897. }
  5898. });
  5899. }
  5900. toReferencedList(exprList, isInParens) {
  5901. this.tsCheckForInvalidTypeCasts(exprList);
  5902. return exprList;
  5903. }
  5904. parseArrayLike(...args) {
  5905. const node = super.parseArrayLike(...args);
  5906. if (node.type === "ArrayExpression") {
  5907. this.tsCheckForInvalidTypeCasts(node.elements);
  5908. }
  5909. return node;
  5910. }
  5911. parseSubscript(base, startPos, startLoc, noCalls, state) {
  5912. if (!this.hasPrecedingLineBreak() && this.match(types$1.bang)) {
  5913. this.state.exprAllowed = false;
  5914. this.next();
  5915. const nonNullExpression = this.startNodeAt(startPos, startLoc);
  5916. nonNullExpression.expression = base;
  5917. return this.finishNode(nonNullExpression, "TSNonNullExpression");
  5918. }
  5919. if (this.isRelational("<")) {
  5920. const result = this.tsTryParseAndCatch(() => {
  5921. if (!noCalls && this.atPossibleAsyncArrow(base)) {
  5922. const asyncArrowFn = this.tsTryParseGenericAsyncArrowFunction(startPos, startLoc);
  5923. if (asyncArrowFn) {
  5924. return asyncArrowFn;
  5925. }
  5926. }
  5927. const node = this.startNodeAt(startPos, startLoc);
  5928. node.callee = base;
  5929. const typeArguments = this.tsParseTypeArguments();
  5930. if (typeArguments) {
  5931. if (!noCalls && this.eat(types$1.parenL)) {
  5932. node.arguments = this.parseCallExpressionArguments(types$1.parenR, false);
  5933. this.tsCheckForInvalidTypeCasts(node.arguments);
  5934. node.typeParameters = typeArguments;
  5935. if (state.optionalChainMember) {
  5936. node.optional = false;
  5937. }
  5938. return this.finishCallExpression(node, state.optionalChainMember);
  5939. } else if (this.match(types$1.backQuote)) {
  5940. const result = this.parseTaggedTemplateExpression(base, startPos, startLoc, state);
  5941. result.typeParameters = typeArguments;
  5942. return result;
  5943. }
  5944. }
  5945. this.unexpected();
  5946. });
  5947. if (result) return result;
  5948. }
  5949. return super.parseSubscript(base, startPos, startLoc, noCalls, state);
  5950. }
  5951. parseNewArguments(node) {
  5952. if (this.isRelational("<")) {
  5953. const typeParameters = this.tsTryParseAndCatch(() => {
  5954. const args = this.tsParseTypeArguments();
  5955. if (!this.match(types$1.parenL)) this.unexpected();
  5956. return args;
  5957. });
  5958. if (typeParameters) {
  5959. node.typeParameters = typeParameters;
  5960. }
  5961. }
  5962. super.parseNewArguments(node);
  5963. }
  5964. parseExprOp(left, leftStartPos, leftStartLoc, minPrec) {
  5965. if (nonNull(types$1._in.binop) > minPrec && !this.hasPrecedingLineBreak() && this.isContextual("as")) {
  5966. const node = this.startNodeAt(leftStartPos, leftStartLoc);
  5967. node.expression = left;
  5968. const _const = this.tsTryNextParseConstantContext();
  5969. if (_const) {
  5970. node.typeAnnotation = _const;
  5971. } else {
  5972. node.typeAnnotation = this.tsNextThenParseType();
  5973. }
  5974. this.finishNode(node, "TSAsExpression");
  5975. this.reScan_lt_gt();
  5976. return this.parseExprOp(node, leftStartPos, leftStartLoc, minPrec);
  5977. }
  5978. return super.parseExprOp(left, leftStartPos, leftStartLoc, minPrec);
  5979. }
  5980. checkReservedWord(word, startLoc, checkKeywords, isBinding) {}
  5981. checkDuplicateExports() {}
  5982. parseImport(node) {
  5983. node.importKind = "value";
  5984. if (this.match(types$1.name) || this.match(types$1.star) || this.match(types$1.braceL)) {
  5985. let ahead = this.lookahead();
  5986. if (this.isContextual("type") && ahead.type !== types$1.comma && !(ahead.type === types$1.name && ahead.value === "from") && ahead.type !== types$1.eq) {
  5987. node.importKind = "type";
  5988. this.next();
  5989. ahead = this.lookahead();
  5990. }
  5991. if (this.match(types$1.name) && ahead.type === types$1.eq) {
  5992. return this.tsParseImportEqualsDeclaration(node);
  5993. }
  5994. }
  5995. const importNode = super.parseImport(node);
  5996. if (importNode.importKind === "type" && importNode.specifiers.length > 1 && importNode.specifiers[0].type === "ImportDefaultSpecifier") {
  5997. this.raise(importNode.start, TSErrors.TypeImportCannotSpecifyDefaultAndNamed);
  5998. }
  5999. return importNode;
  6000. }
  6001. parseExport(node) {
  6002. if (this.match(types$1._import)) {
  6003. this.next();
  6004. if (this.isContextual("type") && this.lookaheadCharCode() !== 61) {
  6005. node.importKind = "type";
  6006. this.next();
  6007. } else {
  6008. node.importKind = "value";
  6009. }
  6010. return this.tsParseImportEqualsDeclaration(node, true);
  6011. } else if (this.eat(types$1.eq)) {
  6012. const assign = node;
  6013. assign.expression = this.parseExpression();
  6014. this.semicolon();
  6015. return this.finishNode(assign, "TSExportAssignment");
  6016. } else if (this.eatContextual("as")) {
  6017. const decl = node;
  6018. this.expectContextual("namespace");
  6019. decl.id = this.parseIdentifier();
  6020. this.semicolon();
  6021. return this.finishNode(decl, "TSNamespaceExportDeclaration");
  6022. } else {
  6023. if (this.isContextual("type") && this.lookahead().type === types$1.braceL) {
  6024. this.next();
  6025. node.exportKind = "type";
  6026. } else {
  6027. node.exportKind = "value";
  6028. }
  6029. return super.parseExport(node);
  6030. }
  6031. }
  6032. isAbstractClass() {
  6033. return this.isContextual("abstract") && this.lookahead().type === types$1._class;
  6034. }
  6035. parseExportDefaultExpression() {
  6036. if (this.isAbstractClass()) {
  6037. const cls = this.startNode();
  6038. this.next();
  6039. cls.abstract = true;
  6040. this.parseClass(cls, true, true);
  6041. return cls;
  6042. }
  6043. if (this.state.value === "interface") {
  6044. const result = this.tsParseDeclaration(this.startNode(), this.state.value, true);
  6045. if (result) return result;
  6046. }
  6047. return super.parseExportDefaultExpression();
  6048. }
  6049. parseStatementContent(context, topLevel) {
  6050. if (this.state.type === types$1._const) {
  6051. const ahead = this.lookahead();
  6052. if (ahead.type === types$1.name && ahead.value === "enum") {
  6053. const node = this.startNode();
  6054. this.expect(types$1._const);
  6055. this.expectContextual("enum");
  6056. return this.tsParseEnumDeclaration(node, true);
  6057. }
  6058. }
  6059. return super.parseStatementContent(context, topLevel);
  6060. }
  6061. parseAccessModifier() {
  6062. return this.tsParseModifier(["public", "protected", "private"]);
  6063. }
  6064. tsHasSomeModifiers(member, modifiers) {
  6065. return modifiers.some(modifier => {
  6066. if (tsIsAccessModifier(modifier)) {
  6067. return member.accessibility === modifier;
  6068. }
  6069. return !!member[modifier];
  6070. });
  6071. }
  6072. parseClassMember(classBody, member, state) {
  6073. const invalidModifersForStaticBlocks = ["declare", "private", "public", "protected", "override", "abstract", "readonly"];
  6074. this.tsParseModifiers(member, invalidModifersForStaticBlocks.concat(["static"]));
  6075. const callParseClassMemberWithIsStatic = () => {
  6076. const isStatic = !!member.static;
  6077. if (isStatic && this.eat(types$1.braceL)) {
  6078. if (this.tsHasSomeModifiers(member, invalidModifersForStaticBlocks)) {
  6079. this.raise(this.state.pos, TSErrors.StaticBlockCannotHaveModifier);
  6080. }
  6081. this.parseClassStaticBlock(classBody, member);
  6082. } else {
  6083. this.parseClassMemberWithIsStatic(classBody, member, state, isStatic);
  6084. }
  6085. };
  6086. if (member.declare) {
  6087. this.tsInAmbientContext(callParseClassMemberWithIsStatic);
  6088. } else {
  6089. callParseClassMemberWithIsStatic();
  6090. }
  6091. }
  6092. parseClassMemberWithIsStatic(classBody, member, state, isStatic) {
  6093. const idx = this.tsTryParseIndexSignature(member);
  6094. if (idx) {
  6095. classBody.body.push(idx);
  6096. if (member.abstract) {
  6097. this.raise(member.start, TSErrors.IndexSignatureHasAbstract);
  6098. }
  6099. if (member.accessibility) {
  6100. this.raise(member.start, TSErrors.IndexSignatureHasAccessibility, member.accessibility);
  6101. }
  6102. if (member.declare) {
  6103. this.raise(member.start, TSErrors.IndexSignatureHasDeclare);
  6104. }
  6105. if (member.override) {
  6106. this.raise(member.start, TSErrors.IndexSignatureHasOverride);
  6107. }
  6108. return;
  6109. }
  6110. if (!this.state.inAbstractClass && member.abstract) {
  6111. this.raise(member.start, TSErrors.NonAbstractClassHasAbstractMethod);
  6112. }
  6113. if (member.override) {
  6114. if (!state.hadSuperClass) {
  6115. this.raise(member.start, TSErrors.OverrideNotInSubClass);
  6116. }
  6117. }
  6118. super.parseClassMemberWithIsStatic(classBody, member, state, isStatic);
  6119. }
  6120. parsePostMemberNameModifiers(methodOrProp) {
  6121. const optional = this.eat(types$1.question);
  6122. if (optional) methodOrProp.optional = true;
  6123. if (methodOrProp.readonly && this.match(types$1.parenL)) {
  6124. this.raise(methodOrProp.start, TSErrors.ClassMethodHasReadonly);
  6125. }
  6126. if (methodOrProp.declare && this.match(types$1.parenL)) {
  6127. this.raise(methodOrProp.start, TSErrors.ClassMethodHasDeclare);
  6128. }
  6129. }
  6130. parseExpressionStatement(node, expr) {
  6131. const decl = expr.type === "Identifier" ? this.tsParseExpressionStatement(node, expr) : undefined;
  6132. return decl || super.parseExpressionStatement(node, expr);
  6133. }
  6134. shouldParseExportDeclaration() {
  6135. if (this.tsIsDeclarationStart()) return true;
  6136. return super.shouldParseExportDeclaration();
  6137. }
  6138. parseConditional(expr, startPos, startLoc, refNeedsArrowPos) {
  6139. if (!refNeedsArrowPos || !this.match(types$1.question)) {
  6140. return super.parseConditional(expr, startPos, startLoc, refNeedsArrowPos);
  6141. }
  6142. const result = this.tryParse(() => super.parseConditional(expr, startPos, startLoc));
  6143. if (!result.node) {
  6144. refNeedsArrowPos.start = result.error.pos || this.state.start;
  6145. return expr;
  6146. }
  6147. if (result.error) this.state = result.failState;
  6148. return result.node;
  6149. }
  6150. parseParenItem(node, startPos, startLoc) {
  6151. node = super.parseParenItem(node, startPos, startLoc);
  6152. if (this.eat(types$1.question)) {
  6153. node.optional = true;
  6154. this.resetEndLocation(node);
  6155. }
  6156. if (this.match(types$1.colon)) {
  6157. const typeCastNode = this.startNodeAt(startPos, startLoc);
  6158. typeCastNode.expression = node;
  6159. typeCastNode.typeAnnotation = this.tsParseTypeAnnotation();
  6160. return this.finishNode(typeCastNode, "TSTypeCastExpression");
  6161. }
  6162. return node;
  6163. }
  6164. parseExportDeclaration(node) {
  6165. const startPos = this.state.start;
  6166. const startLoc = this.state.startLoc;
  6167. const isDeclare = this.eatContextual("declare");
  6168. if (isDeclare && (this.isContextual("declare") || !this.shouldParseExportDeclaration())) {
  6169. throw this.raise(this.state.start, TSErrors.ExpectedAmbientAfterExportDeclare);
  6170. }
  6171. let declaration;
  6172. if (this.match(types$1.name)) {
  6173. declaration = this.tsTryParseExportDeclaration();
  6174. }
  6175. if (!declaration) {
  6176. declaration = super.parseExportDeclaration(node);
  6177. }
  6178. if (declaration && (declaration.type === "TSInterfaceDeclaration" || declaration.type === "TSTypeAliasDeclaration" || isDeclare)) {
  6179. node.exportKind = "type";
  6180. }
  6181. if (declaration && isDeclare) {
  6182. this.resetStartLocation(declaration, startPos, startLoc);
  6183. declaration.declare = true;
  6184. }
  6185. return declaration;
  6186. }
  6187. parseClassId(node, isStatement, optionalId) {
  6188. if ((!isStatement || optionalId) && this.isContextual("implements")) {
  6189. return;
  6190. }
  6191. super.parseClassId(node, isStatement, optionalId, node.declare ? BIND_TS_AMBIENT : BIND_CLASS);
  6192. const typeParameters = this.tsTryParseTypeParameters();
  6193. if (typeParameters) node.typeParameters = typeParameters;
  6194. }
  6195. parseClassPropertyAnnotation(node) {
  6196. if (!node.optional && this.eat(types$1.bang)) {
  6197. node.definite = true;
  6198. }
  6199. const type = this.tsTryParseTypeAnnotation();
  6200. if (type) node.typeAnnotation = type;
  6201. }
  6202. parseClassProperty(node) {
  6203. this.parseClassPropertyAnnotation(node);
  6204. if (this.state.isAmbientContext && this.match(types$1.eq)) {
  6205. this.raise(this.state.start, TSErrors.DeclareClassFieldHasInitializer);
  6206. }
  6207. return super.parseClassProperty(node);
  6208. }
  6209. parseClassPrivateProperty(node) {
  6210. if (node.abstract) {
  6211. this.raise(node.start, TSErrors.PrivateElementHasAbstract);
  6212. }
  6213. if (node.accessibility) {
  6214. this.raise(node.start, TSErrors.PrivateElementHasAccessibility, node.accessibility);
  6215. }
  6216. this.parseClassPropertyAnnotation(node);
  6217. return super.parseClassPrivateProperty(node);
  6218. }
  6219. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  6220. const typeParameters = this.tsTryParseTypeParameters();
  6221. if (typeParameters && isConstructor) {
  6222. this.raise(typeParameters.start, TSErrors.ConstructorHasTypeParameters);
  6223. }
  6224. if (method.declare && (method.kind === "get" || method.kind === "set")) {
  6225. this.raise(method.start, TSErrors.DeclareAccessor, method.kind);
  6226. }
  6227. if (typeParameters) method.typeParameters = typeParameters;
  6228. super.pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper);
  6229. }
  6230. pushClassPrivateMethod(classBody, method, isGenerator, isAsync) {
  6231. const typeParameters = this.tsTryParseTypeParameters();
  6232. if (typeParameters) method.typeParameters = typeParameters;
  6233. super.pushClassPrivateMethod(classBody, method, isGenerator, isAsync);
  6234. }
  6235. parseClassSuper(node) {
  6236. super.parseClassSuper(node);
  6237. if (node.superClass && this.isRelational("<")) {
  6238. node.superTypeParameters = this.tsParseTypeArguments();
  6239. }
  6240. if (this.eatContextual("implements")) {
  6241. node.implements = this.tsParseHeritageClause("implements");
  6242. }
  6243. }
  6244. parseObjPropValue(prop, ...args) {
  6245. const typeParameters = this.tsTryParseTypeParameters();
  6246. if (typeParameters) prop.typeParameters = typeParameters;
  6247. super.parseObjPropValue(prop, ...args);
  6248. }
  6249. parseFunctionParams(node, allowModifiers) {
  6250. const typeParameters = this.tsTryParseTypeParameters();
  6251. if (typeParameters) node.typeParameters = typeParameters;
  6252. super.parseFunctionParams(node, allowModifiers);
  6253. }
  6254. parseVarId(decl, kind) {
  6255. super.parseVarId(decl, kind);
  6256. if (decl.id.type === "Identifier" && this.eat(types$1.bang)) {
  6257. decl.definite = true;
  6258. }
  6259. const type = this.tsTryParseTypeAnnotation();
  6260. if (type) {
  6261. decl.id.typeAnnotation = type;
  6262. this.resetEndLocation(decl.id);
  6263. }
  6264. }
  6265. parseAsyncArrowFromCallExpression(node, call) {
  6266. if (this.match(types$1.colon)) {
  6267. node.returnType = this.tsParseTypeAnnotation();
  6268. }
  6269. return super.parseAsyncArrowFromCallExpression(node, call);
  6270. }
  6271. parseMaybeAssign(...args) {
  6272. var _jsx, _jsx2, _typeCast, _jsx3, _typeCast2, _jsx4, _typeCast3;
  6273. let state;
  6274. let jsx;
  6275. let typeCast;
  6276. if (this.hasPlugin("jsx") && (this.match(types$1.jsxTagStart) || this.isRelational("<"))) {
  6277. state = this.state.clone();
  6278. jsx = this.tryParse(() => super.parseMaybeAssign(...args), state);
  6279. if (!jsx.error) return jsx.node;
  6280. const {
  6281. context
  6282. } = this.state;
  6283. if (context[context.length - 1] === types.j_oTag) {
  6284. context.length -= 2;
  6285. } else if (context[context.length - 1] === types.j_expr) {
  6286. context.length -= 1;
  6287. }
  6288. }
  6289. if (!((_jsx = jsx) != null && _jsx.error) && !this.isRelational("<")) {
  6290. return super.parseMaybeAssign(...args);
  6291. }
  6292. let typeParameters;
  6293. state = state || this.state.clone();
  6294. const arrow = this.tryParse(abort => {
  6295. var _expr$extra, _typeParameters;
  6296. typeParameters = this.tsParseTypeParameters();
  6297. const expr = super.parseMaybeAssign(...args);
  6298. if (expr.type !== "ArrowFunctionExpression" || (_expr$extra = expr.extra) != null && _expr$extra.parenthesized) {
  6299. abort();
  6300. }
  6301. if (((_typeParameters = typeParameters) == null ? void 0 : _typeParameters.params.length) !== 0) {
  6302. this.resetStartLocationFromNode(expr, typeParameters);
  6303. }
  6304. expr.typeParameters = typeParameters;
  6305. return expr;
  6306. }, state);
  6307. if (!arrow.error && !arrow.aborted) return arrow.node;
  6308. if (!jsx) {
  6309. assert(!this.hasPlugin("jsx"));
  6310. typeCast = this.tryParse(() => super.parseMaybeAssign(...args), state);
  6311. if (!typeCast.error) return typeCast.node;
  6312. }
  6313. if ((_jsx2 = jsx) != null && _jsx2.node) {
  6314. this.state = jsx.failState;
  6315. return jsx.node;
  6316. }
  6317. if (arrow.node) {
  6318. this.state = arrow.failState;
  6319. return arrow.node;
  6320. }
  6321. if ((_typeCast = typeCast) != null && _typeCast.node) {
  6322. this.state = typeCast.failState;
  6323. return typeCast.node;
  6324. }
  6325. if ((_jsx3 = jsx) != null && _jsx3.thrown) throw jsx.error;
  6326. if (arrow.thrown) throw arrow.error;
  6327. if ((_typeCast2 = typeCast) != null && _typeCast2.thrown) throw typeCast.error;
  6328. throw ((_jsx4 = jsx) == null ? void 0 : _jsx4.error) || arrow.error || ((_typeCast3 = typeCast) == null ? void 0 : _typeCast3.error);
  6329. }
  6330. parseMaybeUnary(refExpressionErrors) {
  6331. if (!this.hasPlugin("jsx") && this.isRelational("<")) {
  6332. return this.tsParseTypeAssertion();
  6333. } else {
  6334. return super.parseMaybeUnary(refExpressionErrors);
  6335. }
  6336. }
  6337. parseArrow(node) {
  6338. if (this.match(types$1.colon)) {
  6339. const result = this.tryParse(abort => {
  6340. const returnType = this.tsParseTypeOrTypePredicateAnnotation(types$1.colon);
  6341. if (this.canInsertSemicolon() || !this.match(types$1.arrow)) abort();
  6342. return returnType;
  6343. });
  6344. if (result.aborted) return;
  6345. if (!result.thrown) {
  6346. if (result.error) this.state = result.failState;
  6347. node.returnType = result.node;
  6348. }
  6349. }
  6350. return super.parseArrow(node);
  6351. }
  6352. parseAssignableListItemTypes(param) {
  6353. if (this.eat(types$1.question)) {
  6354. if (param.type !== "Identifier" && !this.state.isAmbientContext && !this.state.inType) {
  6355. this.raise(param.start, TSErrors.PatternIsOptional);
  6356. }
  6357. param.optional = true;
  6358. }
  6359. const type = this.tsTryParseTypeAnnotation();
  6360. if (type) param.typeAnnotation = type;
  6361. this.resetEndLocation(param);
  6362. return param;
  6363. }
  6364. toAssignable(node, isLHS = false) {
  6365. switch (node.type) {
  6366. case "TSTypeCastExpression":
  6367. return super.toAssignable(this.typeCastToParameter(node), isLHS);
  6368. case "TSParameterProperty":
  6369. return super.toAssignable(node, isLHS);
  6370. case "ParenthesizedExpression":
  6371. return this.toAssignableParenthesizedExpression(node, isLHS);
  6372. case "TSAsExpression":
  6373. case "TSNonNullExpression":
  6374. case "TSTypeAssertion":
  6375. node.expression = this.toAssignable(node.expression, isLHS);
  6376. return node;
  6377. default:
  6378. return super.toAssignable(node, isLHS);
  6379. }
  6380. }
  6381. toAssignableParenthesizedExpression(node, isLHS) {
  6382. switch (node.expression.type) {
  6383. case "TSAsExpression":
  6384. case "TSNonNullExpression":
  6385. case "TSTypeAssertion":
  6386. case "ParenthesizedExpression":
  6387. node.expression = this.toAssignable(node.expression, isLHS);
  6388. return node;
  6389. default:
  6390. return super.toAssignable(node, isLHS);
  6391. }
  6392. }
  6393. checkLVal(expr, contextDescription, ...args) {
  6394. var _expr$extra2;
  6395. switch (expr.type) {
  6396. case "TSTypeCastExpression":
  6397. return;
  6398. case "TSParameterProperty":
  6399. this.checkLVal(expr.parameter, "parameter property", ...args);
  6400. return;
  6401. case "TSAsExpression":
  6402. case "TSTypeAssertion":
  6403. if (!args[0] && contextDescription !== "parenthesized expression" && !((_expr$extra2 = expr.extra) != null && _expr$extra2.parenthesized)) {
  6404. this.raise(expr.start, ErrorMessages.InvalidLhs, contextDescription);
  6405. break;
  6406. }
  6407. this.checkLVal(expr.expression, "parenthesized expression", ...args);
  6408. return;
  6409. case "TSNonNullExpression":
  6410. this.checkLVal(expr.expression, contextDescription, ...args);
  6411. return;
  6412. default:
  6413. super.checkLVal(expr, contextDescription, ...args);
  6414. return;
  6415. }
  6416. }
  6417. parseBindingAtom() {
  6418. switch (this.state.type) {
  6419. case types$1._this:
  6420. return this.parseIdentifier(true);
  6421. default:
  6422. return super.parseBindingAtom();
  6423. }
  6424. }
  6425. parseMaybeDecoratorArguments(expr) {
  6426. if (this.isRelational("<")) {
  6427. const typeArguments = this.tsParseTypeArguments();
  6428. if (this.match(types$1.parenL)) {
  6429. const call = super.parseMaybeDecoratorArguments(expr);
  6430. call.typeParameters = typeArguments;
  6431. return call;
  6432. }
  6433. this.unexpected(this.state.start, types$1.parenL);
  6434. }
  6435. return super.parseMaybeDecoratorArguments(expr);
  6436. }
  6437. checkCommaAfterRest(close) {
  6438. if (this.state.isAmbientContext && this.match(types$1.comma) && this.lookaheadCharCode() === close) {
  6439. this.next();
  6440. } else {
  6441. super.checkCommaAfterRest(close);
  6442. }
  6443. }
  6444. isClassMethod() {
  6445. return this.isRelational("<") || super.isClassMethod();
  6446. }
  6447. isClassProperty() {
  6448. return this.match(types$1.bang) || this.match(types$1.colon) || super.isClassProperty();
  6449. }
  6450. parseMaybeDefault(...args) {
  6451. const node = super.parseMaybeDefault(...args);
  6452. if (node.type === "AssignmentPattern" && node.typeAnnotation && node.right.start < node.typeAnnotation.start) {
  6453. this.raise(node.typeAnnotation.start, TSErrors.TypeAnnotationAfterAssign);
  6454. }
  6455. return node;
  6456. }
  6457. getTokenFromCode(code) {
  6458. if (this.state.inType && (code === 62 || code === 60)) {
  6459. return this.finishOp(types$1.relational, 1);
  6460. } else {
  6461. return super.getTokenFromCode(code);
  6462. }
  6463. }
  6464. reScan_lt_gt() {
  6465. if (this.match(types$1.relational)) {
  6466. const code = this.input.charCodeAt(this.state.start);
  6467. if (code === 60 || code === 62) {
  6468. this.state.pos -= 1;
  6469. this.readToken_lt_gt(code);
  6470. }
  6471. }
  6472. }
  6473. toAssignableList(exprList) {
  6474. for (let i = 0; i < exprList.length; i++) {
  6475. const expr = exprList[i];
  6476. if (!expr) continue;
  6477. switch (expr.type) {
  6478. case "TSTypeCastExpression":
  6479. exprList[i] = this.typeCastToParameter(expr);
  6480. break;
  6481. case "TSAsExpression":
  6482. case "TSTypeAssertion":
  6483. if (!this.state.maybeInArrowParameters) {
  6484. exprList[i] = this.typeCastToParameter(expr);
  6485. } else {
  6486. this.raise(expr.start, TSErrors.UnexpectedTypeCastInParameter);
  6487. }
  6488. break;
  6489. }
  6490. }
  6491. return super.toAssignableList(...arguments);
  6492. }
  6493. typeCastToParameter(node) {
  6494. node.expression.typeAnnotation = node.typeAnnotation;
  6495. this.resetEndLocation(node.expression, node.typeAnnotation.end, node.typeAnnotation.loc.end);
  6496. return node.expression;
  6497. }
  6498. shouldParseArrow() {
  6499. return this.match(types$1.colon) || super.shouldParseArrow();
  6500. }
  6501. shouldParseAsyncArrow() {
  6502. return this.match(types$1.colon) || super.shouldParseAsyncArrow();
  6503. }
  6504. canHaveLeadingDecorator() {
  6505. return super.canHaveLeadingDecorator() || this.isAbstractClass();
  6506. }
  6507. jsxParseOpeningElementAfterName(node) {
  6508. if (this.isRelational("<")) {
  6509. const typeArguments = this.tsTryParseAndCatch(() => this.tsParseTypeArguments());
  6510. if (typeArguments) node.typeParameters = typeArguments;
  6511. }
  6512. return super.jsxParseOpeningElementAfterName(node);
  6513. }
  6514. getGetterSetterExpectedParamCount(method) {
  6515. const baseCount = super.getGetterSetterExpectedParamCount(method);
  6516. const params = this.getObjectOrClassMethodParams(method);
  6517. const firstParam = params[0];
  6518. const hasContextParam = firstParam && this.isThisParam(firstParam);
  6519. return hasContextParam ? baseCount + 1 : baseCount;
  6520. }
  6521. parseCatchClauseParam() {
  6522. const param = super.parseCatchClauseParam();
  6523. const type = this.tsTryParseTypeAnnotation();
  6524. if (type) {
  6525. param.typeAnnotation = type;
  6526. this.resetEndLocation(param);
  6527. }
  6528. return param;
  6529. }
  6530. tsInAmbientContext(cb) {
  6531. const oldIsAmbientContext = this.state.isAmbientContext;
  6532. this.state.isAmbientContext = true;
  6533. try {
  6534. return cb();
  6535. } finally {
  6536. this.state.isAmbientContext = oldIsAmbientContext;
  6537. }
  6538. }
  6539. parseClass(node, ...args) {
  6540. const oldInAbstractClass = this.state.inAbstractClass;
  6541. this.state.inAbstractClass = !!node.abstract;
  6542. try {
  6543. return super.parseClass(node, ...args);
  6544. } finally {
  6545. this.state.inAbstractClass = oldInAbstractClass;
  6546. }
  6547. }
  6548. tsParseAbstractDeclaration(node) {
  6549. if (this.match(types$1._class)) {
  6550. node.abstract = true;
  6551. return this.parseClass(node, true, false);
  6552. } else if (this.isContextual("interface")) {
  6553. if (!this.hasFollowingLineBreak()) {
  6554. node.abstract = true;
  6555. this.raise(node.start, TSErrors.NonClassMethodPropertyHasAbstractModifer);
  6556. this.next();
  6557. return this.tsParseInterfaceDeclaration(node);
  6558. }
  6559. } else {
  6560. this.unexpected(null, types$1._class);
  6561. }
  6562. }
  6563. parseMethod(...args) {
  6564. const method = super.parseMethod(...args);
  6565. if (method.abstract) {
  6566. const hasBody = this.hasPlugin("estree") ? !!method.value.body : !!method.body;
  6567. if (hasBody) {
  6568. const {
  6569. key
  6570. } = method;
  6571. this.raise(method.start, TSErrors.AbstractMethodHasImplementation, key.type === "Identifier" ? key.name : `[${this.input.slice(key.start, key.end)}]`);
  6572. }
  6573. }
  6574. return method;
  6575. }
  6576. shouldParseAsAmbientContext() {
  6577. return !!this.getPluginOption("typescript", "dts");
  6578. }
  6579. parse() {
  6580. if (this.shouldParseAsAmbientContext()) {
  6581. this.state.isAmbientContext = true;
  6582. }
  6583. return super.parse();
  6584. }
  6585. getExpression() {
  6586. if (this.shouldParseAsAmbientContext()) {
  6587. this.state.isAmbientContext = true;
  6588. }
  6589. return super.getExpression();
  6590. }
  6591. });
  6592. types$1.placeholder = new TokenType("%%", {
  6593. startsExpr: true
  6594. });
  6595. const PlaceHolderErrors = makeErrorTemplates({
  6596. ClassNameIsRequired: "A class name is required."
  6597. }, ErrorCodes.SyntaxError);
  6598. var placeholders = (superClass => class extends superClass {
  6599. parsePlaceholder(expectedNode) {
  6600. if (this.match(types$1.placeholder)) {
  6601. const node = this.startNode();
  6602. this.next();
  6603. this.assertNoSpace("Unexpected space in placeholder.");
  6604. node.name = super.parseIdentifier(true);
  6605. this.assertNoSpace("Unexpected space in placeholder.");
  6606. this.expect(types$1.placeholder);
  6607. return this.finishPlaceholder(node, expectedNode);
  6608. }
  6609. }
  6610. finishPlaceholder(node, expectedNode) {
  6611. const isFinished = !!(node.expectedNode && node.type === "Placeholder");
  6612. node.expectedNode = expectedNode;
  6613. return isFinished ? node : this.finishNode(node, "Placeholder");
  6614. }
  6615. getTokenFromCode(code) {
  6616. if (code === 37 && this.input.charCodeAt(this.state.pos + 1) === 37) {
  6617. return this.finishOp(types$1.placeholder, 2);
  6618. }
  6619. return super.getTokenFromCode(...arguments);
  6620. }
  6621. parseExprAtom() {
  6622. return this.parsePlaceholder("Expression") || super.parseExprAtom(...arguments);
  6623. }
  6624. parseIdentifier() {
  6625. return this.parsePlaceholder("Identifier") || super.parseIdentifier(...arguments);
  6626. }
  6627. checkReservedWord(word) {
  6628. if (word !== undefined) super.checkReservedWord(...arguments);
  6629. }
  6630. parseBindingAtom() {
  6631. return this.parsePlaceholder("Pattern") || super.parseBindingAtom(...arguments);
  6632. }
  6633. checkLVal(expr) {
  6634. if (expr.type !== "Placeholder") super.checkLVal(...arguments);
  6635. }
  6636. toAssignable(node) {
  6637. if (node && node.type === "Placeholder" && node.expectedNode === "Expression") {
  6638. node.expectedNode = "Pattern";
  6639. return node;
  6640. }
  6641. return super.toAssignable(...arguments);
  6642. }
  6643. isLet(context) {
  6644. if (super.isLet(context)) {
  6645. return true;
  6646. }
  6647. if (!this.isContextual("let")) {
  6648. return false;
  6649. }
  6650. if (context) return false;
  6651. const nextToken = this.lookahead();
  6652. if (nextToken.type === types$1.placeholder) {
  6653. return true;
  6654. }
  6655. return false;
  6656. }
  6657. verifyBreakContinue(node) {
  6658. if (node.label && node.label.type === "Placeholder") return;
  6659. super.verifyBreakContinue(...arguments);
  6660. }
  6661. parseExpressionStatement(node, expr) {
  6662. if (expr.type !== "Placeholder" || expr.extra && expr.extra.parenthesized) {
  6663. return super.parseExpressionStatement(...arguments);
  6664. }
  6665. if (this.match(types$1.colon)) {
  6666. const stmt = node;
  6667. stmt.label = this.finishPlaceholder(expr, "Identifier");
  6668. this.next();
  6669. stmt.body = this.parseStatement("label");
  6670. return this.finishNode(stmt, "LabeledStatement");
  6671. }
  6672. this.semicolon();
  6673. node.name = expr.name;
  6674. return this.finishPlaceholder(node, "Statement");
  6675. }
  6676. parseBlock() {
  6677. return this.parsePlaceholder("BlockStatement") || super.parseBlock(...arguments);
  6678. }
  6679. parseFunctionId() {
  6680. return this.parsePlaceholder("Identifier") || super.parseFunctionId(...arguments);
  6681. }
  6682. parseClass(node, isStatement, optionalId) {
  6683. const type = isStatement ? "ClassDeclaration" : "ClassExpression";
  6684. this.next();
  6685. this.takeDecorators(node);
  6686. const oldStrict = this.state.strict;
  6687. const placeholder = this.parsePlaceholder("Identifier");
  6688. if (placeholder) {
  6689. if (this.match(types$1._extends) || this.match(types$1.placeholder) || this.match(types$1.braceL)) {
  6690. node.id = placeholder;
  6691. } else if (optionalId || !isStatement) {
  6692. node.id = null;
  6693. node.body = this.finishPlaceholder(placeholder, "ClassBody");
  6694. return this.finishNode(node, type);
  6695. } else {
  6696. this.unexpected(null, PlaceHolderErrors.ClassNameIsRequired);
  6697. }
  6698. } else {
  6699. this.parseClassId(node, isStatement, optionalId);
  6700. }
  6701. this.parseClassSuper(node);
  6702. node.body = this.parsePlaceholder("ClassBody") || this.parseClassBody(!!node.superClass, oldStrict);
  6703. return this.finishNode(node, type);
  6704. }
  6705. parseExport(node) {
  6706. const placeholder = this.parsePlaceholder("Identifier");
  6707. if (!placeholder) return super.parseExport(...arguments);
  6708. if (!this.isContextual("from") && !this.match(types$1.comma)) {
  6709. node.specifiers = [];
  6710. node.source = null;
  6711. node.declaration = this.finishPlaceholder(placeholder, "Declaration");
  6712. return this.finishNode(node, "ExportNamedDeclaration");
  6713. }
  6714. this.expectPlugin("exportDefaultFrom");
  6715. const specifier = this.startNode();
  6716. specifier.exported = placeholder;
  6717. node.specifiers = [this.finishNode(specifier, "ExportDefaultSpecifier")];
  6718. return super.parseExport(node);
  6719. }
  6720. isExportDefaultSpecifier() {
  6721. if (this.match(types$1._default)) {
  6722. const next = this.nextTokenStart();
  6723. if (this.isUnparsedContextual(next, "from")) {
  6724. if (this.input.startsWith(types$1.placeholder.label, this.nextTokenStartSince(next + 4))) {
  6725. return true;
  6726. }
  6727. }
  6728. }
  6729. return super.isExportDefaultSpecifier();
  6730. }
  6731. maybeParseExportDefaultSpecifier(node) {
  6732. if (node.specifiers && node.specifiers.length > 0) {
  6733. return true;
  6734. }
  6735. return super.maybeParseExportDefaultSpecifier(...arguments);
  6736. }
  6737. checkExport(node) {
  6738. const {
  6739. specifiers
  6740. } = node;
  6741. if (specifiers != null && specifiers.length) {
  6742. node.specifiers = specifiers.filter(node => node.exported.type === "Placeholder");
  6743. }
  6744. super.checkExport(node);
  6745. node.specifiers = specifiers;
  6746. }
  6747. parseImport(node) {
  6748. const placeholder = this.parsePlaceholder("Identifier");
  6749. if (!placeholder) return super.parseImport(...arguments);
  6750. node.specifiers = [];
  6751. if (!this.isContextual("from") && !this.match(types$1.comma)) {
  6752. node.source = this.finishPlaceholder(placeholder, "StringLiteral");
  6753. this.semicolon();
  6754. return this.finishNode(node, "ImportDeclaration");
  6755. }
  6756. const specifier = this.startNodeAtNode(placeholder);
  6757. specifier.local = placeholder;
  6758. this.finishNode(specifier, "ImportDefaultSpecifier");
  6759. node.specifiers.push(specifier);
  6760. if (this.eat(types$1.comma)) {
  6761. const hasStarImport = this.maybeParseStarImportSpecifier(node);
  6762. if (!hasStarImport) this.parseNamedImportSpecifiers(node);
  6763. }
  6764. this.expectContextual("from");
  6765. node.source = this.parseImportSource();
  6766. this.semicolon();
  6767. return this.finishNode(node, "ImportDeclaration");
  6768. }
  6769. parseImportSource() {
  6770. return this.parsePlaceholder("StringLiteral") || super.parseImportSource(...arguments);
  6771. }
  6772. });
  6773. var v8intrinsic = (superClass => class extends superClass {
  6774. parseV8Intrinsic() {
  6775. if (this.match(types$1.modulo)) {
  6776. const v8IntrinsicStart = this.state.start;
  6777. const node = this.startNode();
  6778. this.eat(types$1.modulo);
  6779. if (this.match(types$1.name)) {
  6780. const name = this.parseIdentifierName(this.state.start);
  6781. const identifier = this.createIdentifier(node, name);
  6782. identifier.type = "V8IntrinsicIdentifier";
  6783. if (this.match(types$1.parenL)) {
  6784. return identifier;
  6785. }
  6786. }
  6787. this.unexpected(v8IntrinsicStart);
  6788. }
  6789. }
  6790. parseExprAtom() {
  6791. return this.parseV8Intrinsic() || super.parseExprAtom(...arguments);
  6792. }
  6793. });
  6794. function hasPlugin(plugins, name) {
  6795. return plugins.some(plugin => {
  6796. if (Array.isArray(plugin)) {
  6797. return plugin[0] === name;
  6798. } else {
  6799. return plugin === name;
  6800. }
  6801. });
  6802. }
  6803. function getPluginOption(plugins, name, option) {
  6804. const plugin = plugins.find(plugin => {
  6805. if (Array.isArray(plugin)) {
  6806. return plugin[0] === name;
  6807. } else {
  6808. return plugin === name;
  6809. }
  6810. });
  6811. if (plugin && Array.isArray(plugin)) {
  6812. return plugin[1][option];
  6813. }
  6814. return null;
  6815. }
  6816. const PIPELINE_PROPOSALS = ["minimal", "smart", "fsharp"];
  6817. const RECORD_AND_TUPLE_SYNTAX_TYPES = ["hash", "bar"];
  6818. function validatePlugins(plugins) {
  6819. if (hasPlugin(plugins, "decorators")) {
  6820. if (hasPlugin(plugins, "decorators-legacy")) {
  6821. throw new Error("Cannot use the decorators and decorators-legacy plugin together");
  6822. }
  6823. const decoratorsBeforeExport = getPluginOption(plugins, "decorators", "decoratorsBeforeExport");
  6824. if (decoratorsBeforeExport == null) {
  6825. 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'.");
  6826. } else if (typeof decoratorsBeforeExport !== "boolean") {
  6827. throw new Error("'decoratorsBeforeExport' must be a boolean.");
  6828. }
  6829. }
  6830. if (hasPlugin(plugins, "flow") && hasPlugin(plugins, "typescript")) {
  6831. throw new Error("Cannot combine flow and typescript plugins.");
  6832. }
  6833. if (hasPlugin(plugins, "placeholders") && hasPlugin(plugins, "v8intrinsic")) {
  6834. throw new Error("Cannot combine placeholders and v8intrinsic plugins.");
  6835. }
  6836. if (hasPlugin(plugins, "pipelineOperator") && !PIPELINE_PROPOSALS.includes(getPluginOption(plugins, "pipelineOperator", "proposal"))) {
  6837. throw new Error("'pipelineOperator' requires 'proposal' option whose value should be one of: " + PIPELINE_PROPOSALS.map(p => `'${p}'`).join(", "));
  6838. }
  6839. if (hasPlugin(plugins, "moduleAttributes")) {
  6840. {
  6841. if (hasPlugin(plugins, "importAssertions")) {
  6842. throw new Error("Cannot combine importAssertions and moduleAttributes plugins.");
  6843. }
  6844. const moduleAttributesVerionPluginOption = getPluginOption(plugins, "moduleAttributes", "version");
  6845. if (moduleAttributesVerionPluginOption !== "may-2020") {
  6846. throw new Error("The 'moduleAttributes' plugin requires a 'version' option," + " representing the last proposal update. Currently, the" + " only supported value is 'may-2020'.");
  6847. }
  6848. }
  6849. }
  6850. if (hasPlugin(plugins, "recordAndTuple") && !RECORD_AND_TUPLE_SYNTAX_TYPES.includes(getPluginOption(plugins, "recordAndTuple", "syntaxType"))) {
  6851. throw new Error("'recordAndTuple' requires 'syntaxType' option whose value should be one of: " + RECORD_AND_TUPLE_SYNTAX_TYPES.map(p => `'${p}'`).join(", "));
  6852. }
  6853. if (hasPlugin(plugins, "asyncDoExpressions") && !hasPlugin(plugins, "doExpressions")) {
  6854. const error = new Error("'asyncDoExpressions' requires 'doExpressions', please add 'doExpressions' to parser plugins.");
  6855. error.missingPlugins = "doExpressions";
  6856. throw error;
  6857. }
  6858. }
  6859. const mixinPlugins = {
  6860. estree,
  6861. jsx,
  6862. flow,
  6863. typescript,
  6864. v8intrinsic,
  6865. placeholders
  6866. };
  6867. const mixinPluginNames = Object.keys(mixinPlugins);
  6868. const defaultOptions = {
  6869. sourceType: "script",
  6870. sourceFilename: undefined,
  6871. startLine: 1,
  6872. allowAwaitOutsideFunction: false,
  6873. allowReturnOutsideFunction: false,
  6874. allowImportExportEverywhere: false,
  6875. allowSuperOutsideMethod: false,
  6876. allowUndeclaredExports: false,
  6877. plugins: [],
  6878. strictMode: null,
  6879. ranges: false,
  6880. tokens: false,
  6881. createParenthesizedExpressions: false,
  6882. errorRecovery: false
  6883. };
  6884. function getOptions(opts) {
  6885. const options = {};
  6886. for (const key of Object.keys(defaultOptions)) {
  6887. options[key] = opts && opts[key] != null ? opts[key] : defaultOptions[key];
  6888. }
  6889. return options;
  6890. }
  6891. var _isDigit = function isDigit(code) {
  6892. return code >= 48 && code <= 57;
  6893. };
  6894. const VALID_REGEX_FLAGS = new Set(["g", "m", "s", "i", "y", "u"]);
  6895. const forbiddenNumericSeparatorSiblings = {
  6896. decBinOct: [46, 66, 69, 79, 95, 98, 101, 111],
  6897. hex: [46, 88, 95, 120]
  6898. };
  6899. const allowedNumericSeparatorSiblings = {};
  6900. allowedNumericSeparatorSiblings.bin = [48, 49];
  6901. allowedNumericSeparatorSiblings.oct = [...allowedNumericSeparatorSiblings.bin, 50, 51, 52, 53, 54, 55];
  6902. allowedNumericSeparatorSiblings.dec = [...allowedNumericSeparatorSiblings.oct, 56, 57];
  6903. allowedNumericSeparatorSiblings.hex = [...allowedNumericSeparatorSiblings.dec, 65, 66, 67, 68, 69, 70, 97, 98, 99, 100, 101, 102];
  6904. class Token {
  6905. constructor(state) {
  6906. this.type = state.type;
  6907. this.value = state.value;
  6908. this.start = state.start;
  6909. this.end = state.end;
  6910. this.loc = new SourceLocation(state.startLoc, state.endLoc);
  6911. }
  6912. }
  6913. class Tokenizer extends ParserError {
  6914. constructor(options, input) {
  6915. super();
  6916. this.isLookahead = void 0;
  6917. this.tokens = [];
  6918. this.state = new State();
  6919. this.state.init(options);
  6920. this.input = input;
  6921. this.length = input.length;
  6922. this.isLookahead = false;
  6923. }
  6924. pushToken(token) {
  6925. this.tokens.length = this.state.tokensLength;
  6926. this.tokens.push(token);
  6927. ++this.state.tokensLength;
  6928. }
  6929. next() {
  6930. this.checkKeywordEscapes();
  6931. if (this.options.tokens) {
  6932. this.pushToken(new Token(this.state));
  6933. }
  6934. this.state.lastTokEnd = this.state.end;
  6935. this.state.lastTokStart = this.state.start;
  6936. this.state.lastTokEndLoc = this.state.endLoc;
  6937. this.state.lastTokStartLoc = this.state.startLoc;
  6938. this.nextToken();
  6939. }
  6940. eat(type) {
  6941. if (this.match(type)) {
  6942. this.next();
  6943. return true;
  6944. } else {
  6945. return false;
  6946. }
  6947. }
  6948. match(type) {
  6949. return this.state.type === type;
  6950. }
  6951. createLookaheadState(state) {
  6952. return {
  6953. pos: state.pos,
  6954. value: null,
  6955. type: state.type,
  6956. start: state.start,
  6957. end: state.end,
  6958. lastTokEnd: state.end,
  6959. context: [this.curContext()],
  6960. exprAllowed: state.exprAllowed,
  6961. inType: state.inType
  6962. };
  6963. }
  6964. lookahead() {
  6965. const old = this.state;
  6966. this.state = this.createLookaheadState(old);
  6967. this.isLookahead = true;
  6968. this.nextToken();
  6969. this.isLookahead = false;
  6970. const curr = this.state;
  6971. this.state = old;
  6972. return curr;
  6973. }
  6974. nextTokenStart() {
  6975. return this.nextTokenStartSince(this.state.pos);
  6976. }
  6977. nextTokenStartSince(pos) {
  6978. skipWhiteSpace.lastIndex = pos;
  6979. const skip = skipWhiteSpace.exec(this.input);
  6980. return pos + skip[0].length;
  6981. }
  6982. lookaheadCharCode() {
  6983. return this.input.charCodeAt(this.nextTokenStart());
  6984. }
  6985. codePointAtPos(pos) {
  6986. let cp = this.input.charCodeAt(pos);
  6987. if ((cp & 0xfc00) === 0xd800 && ++pos < this.input.length) {
  6988. const trail = this.input.charCodeAt(pos);
  6989. if ((trail & 0xfc00) === 0xdc00) {
  6990. cp = 0x10000 + ((cp & 0x3ff) << 10) + (trail & 0x3ff);
  6991. }
  6992. }
  6993. return cp;
  6994. }
  6995. setStrict(strict) {
  6996. this.state.strict = strict;
  6997. if (strict) {
  6998. this.state.strictErrors.forEach((message, pos) => this.raise(pos, message));
  6999. this.state.strictErrors.clear();
  7000. }
  7001. }
  7002. curContext() {
  7003. return this.state.context[this.state.context.length - 1];
  7004. }
  7005. nextToken() {
  7006. const curContext = this.curContext();
  7007. if (!curContext.preserveSpace) this.skipSpace();
  7008. this.state.start = this.state.pos;
  7009. if (!this.isLookahead) this.state.startLoc = this.state.curPosition();
  7010. if (this.state.pos >= this.length) {
  7011. this.finishToken(types$1.eof);
  7012. return;
  7013. }
  7014. if (curContext === types.template) {
  7015. this.readTmplToken();
  7016. } else {
  7017. this.getTokenFromCode(this.codePointAtPos(this.state.pos));
  7018. }
  7019. }
  7020. pushComment(block, text, start, end, startLoc, endLoc) {
  7021. const comment = {
  7022. type: block ? "CommentBlock" : "CommentLine",
  7023. value: text,
  7024. start: start,
  7025. end: end,
  7026. loc: new SourceLocation(startLoc, endLoc)
  7027. };
  7028. if (this.options.tokens) this.pushToken(comment);
  7029. this.state.comments.push(comment);
  7030. this.addComment(comment);
  7031. }
  7032. skipBlockComment() {
  7033. let startLoc;
  7034. if (!this.isLookahead) startLoc = this.state.curPosition();
  7035. const start = this.state.pos;
  7036. const end = this.input.indexOf("*/", this.state.pos + 2);
  7037. if (end === -1) throw this.raise(start, ErrorMessages.UnterminatedComment);
  7038. this.state.pos = end + 2;
  7039. lineBreakG.lastIndex = start;
  7040. let match;
  7041. while ((match = lineBreakG.exec(this.input)) && match.index < this.state.pos) {
  7042. ++this.state.curLine;
  7043. this.state.lineStart = match.index + match[0].length;
  7044. }
  7045. if (this.isLookahead) return;
  7046. this.pushComment(true, this.input.slice(start + 2, end), start, this.state.pos, startLoc, this.state.curPosition());
  7047. }
  7048. skipLineComment(startSkip) {
  7049. const start = this.state.pos;
  7050. let startLoc;
  7051. if (!this.isLookahead) startLoc = this.state.curPosition();
  7052. let ch = this.input.charCodeAt(this.state.pos += startSkip);
  7053. if (this.state.pos < this.length) {
  7054. while (!isNewLine(ch) && ++this.state.pos < this.length) {
  7055. ch = this.input.charCodeAt(this.state.pos);
  7056. }
  7057. }
  7058. if (this.isLookahead) return;
  7059. this.pushComment(false, this.input.slice(start + startSkip, this.state.pos), start, this.state.pos, startLoc, this.state.curPosition());
  7060. }
  7061. skipSpace() {
  7062. loop: while (this.state.pos < this.length) {
  7063. const ch = this.input.charCodeAt(this.state.pos);
  7064. switch (ch) {
  7065. case 32:
  7066. case 160:
  7067. case 9:
  7068. ++this.state.pos;
  7069. break;
  7070. case 13:
  7071. if (this.input.charCodeAt(this.state.pos + 1) === 10) {
  7072. ++this.state.pos;
  7073. }
  7074. case 10:
  7075. case 8232:
  7076. case 8233:
  7077. ++this.state.pos;
  7078. ++this.state.curLine;
  7079. this.state.lineStart = this.state.pos;
  7080. break;
  7081. case 47:
  7082. switch (this.input.charCodeAt(this.state.pos + 1)) {
  7083. case 42:
  7084. this.skipBlockComment();
  7085. break;
  7086. case 47:
  7087. this.skipLineComment(2);
  7088. break;
  7089. default:
  7090. break loop;
  7091. }
  7092. break;
  7093. default:
  7094. if (isWhitespace(ch)) {
  7095. ++this.state.pos;
  7096. } else {
  7097. break loop;
  7098. }
  7099. }
  7100. }
  7101. }
  7102. finishToken(type, val) {
  7103. this.state.end = this.state.pos;
  7104. const prevType = this.state.type;
  7105. this.state.type = type;
  7106. this.state.value = val;
  7107. if (!this.isLookahead) {
  7108. this.state.endLoc = this.state.curPosition();
  7109. this.updateContext(prevType);
  7110. }
  7111. }
  7112. readToken_numberSign() {
  7113. if (this.state.pos === 0 && this.readToken_interpreter()) {
  7114. return;
  7115. }
  7116. const nextPos = this.state.pos + 1;
  7117. const next = this.codePointAtPos(nextPos);
  7118. if (next >= 48 && next <= 57) {
  7119. throw this.raise(this.state.pos, ErrorMessages.UnexpectedDigitAfterHash);
  7120. }
  7121. if (next === 123 || next === 91 && this.hasPlugin("recordAndTuple")) {
  7122. this.expectPlugin("recordAndTuple");
  7123. if (this.getPluginOption("recordAndTuple", "syntaxType") !== "hash") {
  7124. throw this.raise(this.state.pos, next === 123 ? ErrorMessages.RecordExpressionHashIncorrectStartSyntaxType : ErrorMessages.TupleExpressionHashIncorrectStartSyntaxType);
  7125. }
  7126. if (next === 123) {
  7127. this.finishToken(types$1.braceHashL);
  7128. } else {
  7129. this.finishToken(types$1.bracketHashL);
  7130. }
  7131. this.state.pos += 2;
  7132. } else if (isIdentifierStart(next)) {
  7133. ++this.state.pos;
  7134. this.finishToken(types$1.privateName, this.readWord1(next));
  7135. } else if (next === 92) {
  7136. ++this.state.pos;
  7137. this.finishToken(types$1.privateName, this.readWord1());
  7138. } else {
  7139. this.finishOp(types$1.hash, 1);
  7140. }
  7141. }
  7142. readToken_dot() {
  7143. const next = this.input.charCodeAt(this.state.pos + 1);
  7144. if (next >= 48 && next <= 57) {
  7145. this.readNumber(true);
  7146. return;
  7147. }
  7148. if (next === 46 && this.input.charCodeAt(this.state.pos + 2) === 46) {
  7149. this.state.pos += 3;
  7150. this.finishToken(types$1.ellipsis);
  7151. } else {
  7152. ++this.state.pos;
  7153. this.finishToken(types$1.dot);
  7154. }
  7155. }
  7156. readToken_slash() {
  7157. if (this.state.exprAllowed && !this.state.inType) {
  7158. ++this.state.pos;
  7159. this.readRegexp();
  7160. return;
  7161. }
  7162. const next = this.input.charCodeAt(this.state.pos + 1);
  7163. if (next === 61) {
  7164. this.finishOp(types$1.assign, 2);
  7165. } else {
  7166. this.finishOp(types$1.slash, 1);
  7167. }
  7168. }
  7169. readToken_interpreter() {
  7170. if (this.state.pos !== 0 || this.length < 2) return false;
  7171. let ch = this.input.charCodeAt(this.state.pos + 1);
  7172. if (ch !== 33) return false;
  7173. const start = this.state.pos;
  7174. this.state.pos += 1;
  7175. while (!isNewLine(ch) && ++this.state.pos < this.length) {
  7176. ch = this.input.charCodeAt(this.state.pos);
  7177. }
  7178. const value = this.input.slice(start + 2, this.state.pos);
  7179. this.finishToken(types$1.interpreterDirective, value);
  7180. return true;
  7181. }
  7182. readToken_mult_modulo(code) {
  7183. let type = code === 42 ? types$1.star : types$1.modulo;
  7184. let width = 1;
  7185. let next = this.input.charCodeAt(this.state.pos + 1);
  7186. const exprAllowed = this.state.exprAllowed;
  7187. if (code === 42 && next === 42) {
  7188. width++;
  7189. next = this.input.charCodeAt(this.state.pos + 2);
  7190. type = types$1.exponent;
  7191. }
  7192. if (next === 61 && !exprAllowed) {
  7193. width++;
  7194. type = types$1.assign;
  7195. }
  7196. this.finishOp(type, width);
  7197. }
  7198. readToken_pipe_amp(code) {
  7199. const next = this.input.charCodeAt(this.state.pos + 1);
  7200. if (next === code) {
  7201. if (this.input.charCodeAt(this.state.pos + 2) === 61) {
  7202. this.finishOp(types$1.assign, 3);
  7203. } else {
  7204. this.finishOp(code === 124 ? types$1.logicalOR : types$1.logicalAND, 2);
  7205. }
  7206. return;
  7207. }
  7208. if (code === 124) {
  7209. if (next === 62) {
  7210. this.finishOp(types$1.pipeline, 2);
  7211. return;
  7212. }
  7213. if (this.hasPlugin("recordAndTuple") && next === 125) {
  7214. if (this.getPluginOption("recordAndTuple", "syntaxType") !== "bar") {
  7215. throw this.raise(this.state.pos, ErrorMessages.RecordExpressionBarIncorrectEndSyntaxType);
  7216. }
  7217. this.finishOp(types$1.braceBarR, 2);
  7218. return;
  7219. }
  7220. if (this.hasPlugin("recordAndTuple") && next === 93) {
  7221. if (this.getPluginOption("recordAndTuple", "syntaxType") !== "bar") {
  7222. throw this.raise(this.state.pos, ErrorMessages.TupleExpressionBarIncorrectEndSyntaxType);
  7223. }
  7224. this.finishOp(types$1.bracketBarR, 2);
  7225. return;
  7226. }
  7227. }
  7228. if (next === 61) {
  7229. this.finishOp(types$1.assign, 2);
  7230. return;
  7231. }
  7232. this.finishOp(code === 124 ? types$1.bitwiseOR : types$1.bitwiseAND, 1);
  7233. }
  7234. readToken_caret() {
  7235. const next = this.input.charCodeAt(this.state.pos + 1);
  7236. if (next === 61) {
  7237. this.finishOp(types$1.assign, 2);
  7238. } else {
  7239. this.finishOp(types$1.bitwiseXOR, 1);
  7240. }
  7241. }
  7242. readToken_plus_min(code) {
  7243. const next = this.input.charCodeAt(this.state.pos + 1);
  7244. if (next === code) {
  7245. if (next === 45 && !this.inModule && this.input.charCodeAt(this.state.pos + 2) === 62 && (this.state.lastTokEnd === 0 || this.hasPrecedingLineBreak())) {
  7246. this.skipLineComment(3);
  7247. this.skipSpace();
  7248. this.nextToken();
  7249. return;
  7250. }
  7251. this.finishOp(types$1.incDec, 2);
  7252. return;
  7253. }
  7254. if (next === 61) {
  7255. this.finishOp(types$1.assign, 2);
  7256. } else {
  7257. this.finishOp(types$1.plusMin, 1);
  7258. }
  7259. }
  7260. readToken_lt_gt(code) {
  7261. const next = this.input.charCodeAt(this.state.pos + 1);
  7262. let size = 1;
  7263. if (next === code) {
  7264. size = code === 62 && this.input.charCodeAt(this.state.pos + 2) === 62 ? 3 : 2;
  7265. if (this.input.charCodeAt(this.state.pos + size) === 61) {
  7266. this.finishOp(types$1.assign, size + 1);
  7267. return;
  7268. }
  7269. this.finishOp(types$1.bitShift, size);
  7270. return;
  7271. }
  7272. if (next === 33 && code === 60 && !this.inModule && this.input.charCodeAt(this.state.pos + 2) === 45 && this.input.charCodeAt(this.state.pos + 3) === 45) {
  7273. this.skipLineComment(4);
  7274. this.skipSpace();
  7275. this.nextToken();
  7276. return;
  7277. }
  7278. if (next === 61) {
  7279. size = 2;
  7280. }
  7281. this.finishOp(types$1.relational, size);
  7282. }
  7283. readToken_eq_excl(code) {
  7284. const next = this.input.charCodeAt(this.state.pos + 1);
  7285. if (next === 61) {
  7286. this.finishOp(types$1.equality, this.input.charCodeAt(this.state.pos + 2) === 61 ? 3 : 2);
  7287. return;
  7288. }
  7289. if (code === 61 && next === 62) {
  7290. this.state.pos += 2;
  7291. this.finishToken(types$1.arrow);
  7292. return;
  7293. }
  7294. this.finishOp(code === 61 ? types$1.eq : types$1.bang, 1);
  7295. }
  7296. readToken_question() {
  7297. const next = this.input.charCodeAt(this.state.pos + 1);
  7298. const next2 = this.input.charCodeAt(this.state.pos + 2);
  7299. if (next === 63) {
  7300. if (next2 === 61) {
  7301. this.finishOp(types$1.assign, 3);
  7302. } else {
  7303. this.finishOp(types$1.nullishCoalescing, 2);
  7304. }
  7305. } else if (next === 46 && !(next2 >= 48 && next2 <= 57)) {
  7306. this.state.pos += 2;
  7307. this.finishToken(types$1.questionDot);
  7308. } else {
  7309. ++this.state.pos;
  7310. this.finishToken(types$1.question);
  7311. }
  7312. }
  7313. getTokenFromCode(code) {
  7314. switch (code) {
  7315. case 46:
  7316. this.readToken_dot();
  7317. return;
  7318. case 40:
  7319. ++this.state.pos;
  7320. this.finishToken(types$1.parenL);
  7321. return;
  7322. case 41:
  7323. ++this.state.pos;
  7324. this.finishToken(types$1.parenR);
  7325. return;
  7326. case 59:
  7327. ++this.state.pos;
  7328. this.finishToken(types$1.semi);
  7329. return;
  7330. case 44:
  7331. ++this.state.pos;
  7332. this.finishToken(types$1.comma);
  7333. return;
  7334. case 91:
  7335. if (this.hasPlugin("recordAndTuple") && this.input.charCodeAt(this.state.pos + 1) === 124) {
  7336. if (this.getPluginOption("recordAndTuple", "syntaxType") !== "bar") {
  7337. throw this.raise(this.state.pos, ErrorMessages.TupleExpressionBarIncorrectStartSyntaxType);
  7338. }
  7339. this.finishToken(types$1.bracketBarL);
  7340. this.state.pos += 2;
  7341. } else {
  7342. ++this.state.pos;
  7343. this.finishToken(types$1.bracketL);
  7344. }
  7345. return;
  7346. case 93:
  7347. ++this.state.pos;
  7348. this.finishToken(types$1.bracketR);
  7349. return;
  7350. case 123:
  7351. if (this.hasPlugin("recordAndTuple") && this.input.charCodeAt(this.state.pos + 1) === 124) {
  7352. if (this.getPluginOption("recordAndTuple", "syntaxType") !== "bar") {
  7353. throw this.raise(this.state.pos, ErrorMessages.RecordExpressionBarIncorrectStartSyntaxType);
  7354. }
  7355. this.finishToken(types$1.braceBarL);
  7356. this.state.pos += 2;
  7357. } else {
  7358. ++this.state.pos;
  7359. this.finishToken(types$1.braceL);
  7360. }
  7361. return;
  7362. case 125:
  7363. ++this.state.pos;
  7364. this.finishToken(types$1.braceR);
  7365. return;
  7366. case 58:
  7367. if (this.hasPlugin("functionBind") && this.input.charCodeAt(this.state.pos + 1) === 58) {
  7368. this.finishOp(types$1.doubleColon, 2);
  7369. } else {
  7370. ++this.state.pos;
  7371. this.finishToken(types$1.colon);
  7372. }
  7373. return;
  7374. case 63:
  7375. this.readToken_question();
  7376. return;
  7377. case 96:
  7378. ++this.state.pos;
  7379. this.finishToken(types$1.backQuote);
  7380. return;
  7381. case 48:
  7382. {
  7383. const next = this.input.charCodeAt(this.state.pos + 1);
  7384. if (next === 120 || next === 88) {
  7385. this.readRadixNumber(16);
  7386. return;
  7387. }
  7388. if (next === 111 || next === 79) {
  7389. this.readRadixNumber(8);
  7390. return;
  7391. }
  7392. if (next === 98 || next === 66) {
  7393. this.readRadixNumber(2);
  7394. return;
  7395. }
  7396. }
  7397. case 49:
  7398. case 50:
  7399. case 51:
  7400. case 52:
  7401. case 53:
  7402. case 54:
  7403. case 55:
  7404. case 56:
  7405. case 57:
  7406. this.readNumber(false);
  7407. return;
  7408. case 34:
  7409. case 39:
  7410. this.readString(code);
  7411. return;
  7412. case 47:
  7413. this.readToken_slash();
  7414. return;
  7415. case 37:
  7416. case 42:
  7417. this.readToken_mult_modulo(code);
  7418. return;
  7419. case 124:
  7420. case 38:
  7421. this.readToken_pipe_amp(code);
  7422. return;
  7423. case 94:
  7424. this.readToken_caret();
  7425. return;
  7426. case 43:
  7427. case 45:
  7428. this.readToken_plus_min(code);
  7429. return;
  7430. case 60:
  7431. case 62:
  7432. this.readToken_lt_gt(code);
  7433. return;
  7434. case 61:
  7435. case 33:
  7436. this.readToken_eq_excl(code);
  7437. return;
  7438. case 126:
  7439. this.finishOp(types$1.tilde, 1);
  7440. return;
  7441. case 64:
  7442. ++this.state.pos;
  7443. this.finishToken(types$1.at);
  7444. return;
  7445. case 35:
  7446. this.readToken_numberSign();
  7447. return;
  7448. case 92:
  7449. this.readWord();
  7450. return;
  7451. default:
  7452. if (isIdentifierStart(code)) {
  7453. this.readWord(code);
  7454. return;
  7455. }
  7456. }
  7457. throw this.raise(this.state.pos, ErrorMessages.InvalidOrUnexpectedToken, String.fromCodePoint(code));
  7458. }
  7459. finishOp(type, size) {
  7460. const str = this.input.slice(this.state.pos, this.state.pos + size);
  7461. this.state.pos += size;
  7462. this.finishToken(type, str);
  7463. }
  7464. readRegexp() {
  7465. const start = this.state.pos;
  7466. let escaped, inClass;
  7467. for (;;) {
  7468. if (this.state.pos >= this.length) {
  7469. throw this.raise(start, ErrorMessages.UnterminatedRegExp);
  7470. }
  7471. const ch = this.input.charAt(this.state.pos);
  7472. if (lineBreak.test(ch)) {
  7473. throw this.raise(start, ErrorMessages.UnterminatedRegExp);
  7474. }
  7475. if (escaped) {
  7476. escaped = false;
  7477. } else {
  7478. if (ch === "[") {
  7479. inClass = true;
  7480. } else if (ch === "]" && inClass) {
  7481. inClass = false;
  7482. } else if (ch === "/" && !inClass) {
  7483. break;
  7484. }
  7485. escaped = ch === "\\";
  7486. }
  7487. ++this.state.pos;
  7488. }
  7489. const content = this.input.slice(start, this.state.pos);
  7490. ++this.state.pos;
  7491. let mods = "";
  7492. while (this.state.pos < this.length) {
  7493. const char = this.input[this.state.pos];
  7494. const charCode = this.codePointAtPos(this.state.pos);
  7495. if (VALID_REGEX_FLAGS.has(char)) {
  7496. if (mods.indexOf(char) > -1) {
  7497. this.raise(this.state.pos + 1, ErrorMessages.DuplicateRegExpFlags);
  7498. }
  7499. } else if (isIdentifierChar(charCode) || charCode === 92) {
  7500. this.raise(this.state.pos + 1, ErrorMessages.MalformedRegExpFlags);
  7501. } else {
  7502. break;
  7503. }
  7504. ++this.state.pos;
  7505. mods += char;
  7506. }
  7507. this.finishToken(types$1.regexp, {
  7508. pattern: content,
  7509. flags: mods
  7510. });
  7511. }
  7512. readInt(radix, len, forceLen, allowNumSeparator = true) {
  7513. const start = this.state.pos;
  7514. const forbiddenSiblings = radix === 16 ? forbiddenNumericSeparatorSiblings.hex : forbiddenNumericSeparatorSiblings.decBinOct;
  7515. const allowedSiblings = radix === 16 ? allowedNumericSeparatorSiblings.hex : radix === 10 ? allowedNumericSeparatorSiblings.dec : radix === 8 ? allowedNumericSeparatorSiblings.oct : allowedNumericSeparatorSiblings.bin;
  7516. let invalid = false;
  7517. let total = 0;
  7518. for (let i = 0, e = len == null ? Infinity : len; i < e; ++i) {
  7519. const code = this.input.charCodeAt(this.state.pos);
  7520. let val;
  7521. if (code === 95) {
  7522. const prev = this.input.charCodeAt(this.state.pos - 1);
  7523. const next = this.input.charCodeAt(this.state.pos + 1);
  7524. if (allowedSiblings.indexOf(next) === -1) {
  7525. this.raise(this.state.pos, ErrorMessages.UnexpectedNumericSeparator);
  7526. } else if (forbiddenSiblings.indexOf(prev) > -1 || forbiddenSiblings.indexOf(next) > -1 || Number.isNaN(next)) {
  7527. this.raise(this.state.pos, ErrorMessages.UnexpectedNumericSeparator);
  7528. }
  7529. if (!allowNumSeparator) {
  7530. this.raise(this.state.pos, ErrorMessages.NumericSeparatorInEscapeSequence);
  7531. }
  7532. ++this.state.pos;
  7533. continue;
  7534. }
  7535. if (code >= 97) {
  7536. val = code - 97 + 10;
  7537. } else if (code >= 65) {
  7538. val = code - 65 + 10;
  7539. } else if (_isDigit(code)) {
  7540. val = code - 48;
  7541. } else {
  7542. val = Infinity;
  7543. }
  7544. if (val >= radix) {
  7545. if (this.options.errorRecovery && val <= 9) {
  7546. val = 0;
  7547. this.raise(this.state.start + i + 2, ErrorMessages.InvalidDigit, radix);
  7548. } else if (forceLen) {
  7549. val = 0;
  7550. invalid = true;
  7551. } else {
  7552. break;
  7553. }
  7554. }
  7555. ++this.state.pos;
  7556. total = total * radix + val;
  7557. }
  7558. if (this.state.pos === start || len != null && this.state.pos - start !== len || invalid) {
  7559. return null;
  7560. }
  7561. return total;
  7562. }
  7563. readRadixNumber(radix) {
  7564. const start = this.state.pos;
  7565. let isBigInt = false;
  7566. this.state.pos += 2;
  7567. const val = this.readInt(radix);
  7568. if (val == null) {
  7569. this.raise(this.state.start + 2, ErrorMessages.InvalidDigit, radix);
  7570. }
  7571. const next = this.input.charCodeAt(this.state.pos);
  7572. if (next === 110) {
  7573. ++this.state.pos;
  7574. isBigInt = true;
  7575. } else if (next === 109) {
  7576. throw this.raise(start, ErrorMessages.InvalidDecimal);
  7577. }
  7578. if (isIdentifierStart(this.codePointAtPos(this.state.pos))) {
  7579. throw this.raise(this.state.pos, ErrorMessages.NumberIdentifier);
  7580. }
  7581. if (isBigInt) {
  7582. const str = this.input.slice(start, this.state.pos).replace(/[_n]/g, "");
  7583. this.finishToken(types$1.bigint, str);
  7584. return;
  7585. }
  7586. this.finishToken(types$1.num, val);
  7587. }
  7588. readNumber(startsWithDot) {
  7589. const start = this.state.pos;
  7590. let isFloat = false;
  7591. let isBigInt = false;
  7592. let isDecimal = false;
  7593. let hasExponent = false;
  7594. let isOctal = false;
  7595. if (!startsWithDot && this.readInt(10) === null) {
  7596. this.raise(start, ErrorMessages.InvalidNumber);
  7597. }
  7598. const hasLeadingZero = this.state.pos - start >= 2 && this.input.charCodeAt(start) === 48;
  7599. if (hasLeadingZero) {
  7600. const integer = this.input.slice(start, this.state.pos);
  7601. this.recordStrictModeErrors(start, ErrorMessages.StrictOctalLiteral);
  7602. if (!this.state.strict) {
  7603. const underscorePos = integer.indexOf("_");
  7604. if (underscorePos > 0) {
  7605. this.raise(underscorePos + start, ErrorMessages.ZeroDigitNumericSeparator);
  7606. }
  7607. }
  7608. isOctal = hasLeadingZero && !/[89]/.test(integer);
  7609. }
  7610. let next = this.input.charCodeAt(this.state.pos);
  7611. if (next === 46 && !isOctal) {
  7612. ++this.state.pos;
  7613. this.readInt(10);
  7614. isFloat = true;
  7615. next = this.input.charCodeAt(this.state.pos);
  7616. }
  7617. if ((next === 69 || next === 101) && !isOctal) {
  7618. next = this.input.charCodeAt(++this.state.pos);
  7619. if (next === 43 || next === 45) {
  7620. ++this.state.pos;
  7621. }
  7622. if (this.readInt(10) === null) {
  7623. this.raise(start, ErrorMessages.InvalidOrMissingExponent);
  7624. }
  7625. isFloat = true;
  7626. hasExponent = true;
  7627. next = this.input.charCodeAt(this.state.pos);
  7628. }
  7629. if (next === 110) {
  7630. if (isFloat || hasLeadingZero) {
  7631. this.raise(start, ErrorMessages.InvalidBigIntLiteral);
  7632. }
  7633. ++this.state.pos;
  7634. isBigInt = true;
  7635. }
  7636. if (next === 109) {
  7637. this.expectPlugin("decimal", this.state.pos);
  7638. if (hasExponent || hasLeadingZero) {
  7639. this.raise(start, ErrorMessages.InvalidDecimal);
  7640. }
  7641. ++this.state.pos;
  7642. isDecimal = true;
  7643. }
  7644. if (isIdentifierStart(this.codePointAtPos(this.state.pos))) {
  7645. throw this.raise(this.state.pos, ErrorMessages.NumberIdentifier);
  7646. }
  7647. const str = this.input.slice(start, this.state.pos).replace(/[_mn]/g, "");
  7648. if (isBigInt) {
  7649. this.finishToken(types$1.bigint, str);
  7650. return;
  7651. }
  7652. if (isDecimal) {
  7653. this.finishToken(types$1.decimal, str);
  7654. return;
  7655. }
  7656. const val = isOctal ? parseInt(str, 8) : parseFloat(str);
  7657. this.finishToken(types$1.num, val);
  7658. }
  7659. readCodePoint(throwOnInvalid) {
  7660. const ch = this.input.charCodeAt(this.state.pos);
  7661. let code;
  7662. if (ch === 123) {
  7663. const codePos = ++this.state.pos;
  7664. code = this.readHexChar(this.input.indexOf("}", this.state.pos) - this.state.pos, true, throwOnInvalid);
  7665. ++this.state.pos;
  7666. if (code !== null && code > 0x10ffff) {
  7667. if (throwOnInvalid) {
  7668. this.raise(codePos, ErrorMessages.InvalidCodePoint);
  7669. } else {
  7670. return null;
  7671. }
  7672. }
  7673. } else {
  7674. code = this.readHexChar(4, false, throwOnInvalid);
  7675. }
  7676. return code;
  7677. }
  7678. readString(quote) {
  7679. let out = "",
  7680. chunkStart = ++this.state.pos;
  7681. for (;;) {
  7682. if (this.state.pos >= this.length) {
  7683. throw this.raise(this.state.start, ErrorMessages.UnterminatedString);
  7684. }
  7685. const ch = this.input.charCodeAt(this.state.pos);
  7686. if (ch === quote) break;
  7687. if (ch === 92) {
  7688. out += this.input.slice(chunkStart, this.state.pos);
  7689. out += this.readEscapedChar(false);
  7690. chunkStart = this.state.pos;
  7691. } else if (ch === 8232 || ch === 8233) {
  7692. ++this.state.pos;
  7693. ++this.state.curLine;
  7694. this.state.lineStart = this.state.pos;
  7695. } else if (isNewLine(ch)) {
  7696. throw this.raise(this.state.start, ErrorMessages.UnterminatedString);
  7697. } else {
  7698. ++this.state.pos;
  7699. }
  7700. }
  7701. out += this.input.slice(chunkStart, this.state.pos++);
  7702. this.finishToken(types$1.string, out);
  7703. }
  7704. readTmplToken() {
  7705. let out = "",
  7706. chunkStart = this.state.pos,
  7707. containsInvalid = false;
  7708. for (;;) {
  7709. if (this.state.pos >= this.length) {
  7710. throw this.raise(this.state.start, ErrorMessages.UnterminatedTemplate);
  7711. }
  7712. const ch = this.input.charCodeAt(this.state.pos);
  7713. if (ch === 96 || ch === 36 && this.input.charCodeAt(this.state.pos + 1) === 123) {
  7714. if (this.state.pos === this.state.start && this.match(types$1.template)) {
  7715. if (ch === 36) {
  7716. this.state.pos += 2;
  7717. this.finishToken(types$1.dollarBraceL);
  7718. return;
  7719. } else {
  7720. ++this.state.pos;
  7721. this.finishToken(types$1.backQuote);
  7722. return;
  7723. }
  7724. }
  7725. out += this.input.slice(chunkStart, this.state.pos);
  7726. this.finishToken(types$1.template, containsInvalid ? null : out);
  7727. return;
  7728. }
  7729. if (ch === 92) {
  7730. out += this.input.slice(chunkStart, this.state.pos);
  7731. const escaped = this.readEscapedChar(true);
  7732. if (escaped === null) {
  7733. containsInvalid = true;
  7734. } else {
  7735. out += escaped;
  7736. }
  7737. chunkStart = this.state.pos;
  7738. } else if (isNewLine(ch)) {
  7739. out += this.input.slice(chunkStart, this.state.pos);
  7740. ++this.state.pos;
  7741. switch (ch) {
  7742. case 13:
  7743. if (this.input.charCodeAt(this.state.pos) === 10) {
  7744. ++this.state.pos;
  7745. }
  7746. case 10:
  7747. out += "\n";
  7748. break;
  7749. default:
  7750. out += String.fromCharCode(ch);
  7751. break;
  7752. }
  7753. ++this.state.curLine;
  7754. this.state.lineStart = this.state.pos;
  7755. chunkStart = this.state.pos;
  7756. } else {
  7757. ++this.state.pos;
  7758. }
  7759. }
  7760. }
  7761. recordStrictModeErrors(pos, message) {
  7762. if (this.state.strict && !this.state.strictErrors.has(pos)) {
  7763. this.raise(pos, message);
  7764. } else {
  7765. this.state.strictErrors.set(pos, message);
  7766. }
  7767. }
  7768. readEscapedChar(inTemplate) {
  7769. const throwOnInvalid = !inTemplate;
  7770. const ch = this.input.charCodeAt(++this.state.pos);
  7771. ++this.state.pos;
  7772. switch (ch) {
  7773. case 110:
  7774. return "\n";
  7775. case 114:
  7776. return "\r";
  7777. case 120:
  7778. {
  7779. const code = this.readHexChar(2, false, throwOnInvalid);
  7780. return code === null ? null : String.fromCharCode(code);
  7781. }
  7782. case 117:
  7783. {
  7784. const code = this.readCodePoint(throwOnInvalid);
  7785. return code === null ? null : String.fromCodePoint(code);
  7786. }
  7787. case 116:
  7788. return "\t";
  7789. case 98:
  7790. return "\b";
  7791. case 118:
  7792. return "\u000b";
  7793. case 102:
  7794. return "\f";
  7795. case 13:
  7796. if (this.input.charCodeAt(this.state.pos) === 10) {
  7797. ++this.state.pos;
  7798. }
  7799. case 10:
  7800. this.state.lineStart = this.state.pos;
  7801. ++this.state.curLine;
  7802. case 8232:
  7803. case 8233:
  7804. return "";
  7805. case 56:
  7806. case 57:
  7807. if (inTemplate) {
  7808. return null;
  7809. } else {
  7810. this.recordStrictModeErrors(this.state.pos - 1, ErrorMessages.StrictNumericEscape);
  7811. }
  7812. default:
  7813. if (ch >= 48 && ch <= 55) {
  7814. const codePos = this.state.pos - 1;
  7815. const match = this.input.substr(this.state.pos - 1, 3).match(/^[0-7]+/);
  7816. let octalStr = match[0];
  7817. let octal = parseInt(octalStr, 8);
  7818. if (octal > 255) {
  7819. octalStr = octalStr.slice(0, -1);
  7820. octal = parseInt(octalStr, 8);
  7821. }
  7822. this.state.pos += octalStr.length - 1;
  7823. const next = this.input.charCodeAt(this.state.pos);
  7824. if (octalStr !== "0" || next === 56 || next === 57) {
  7825. if (inTemplate) {
  7826. return null;
  7827. } else {
  7828. this.recordStrictModeErrors(codePos, ErrorMessages.StrictNumericEscape);
  7829. }
  7830. }
  7831. return String.fromCharCode(octal);
  7832. }
  7833. return String.fromCharCode(ch);
  7834. }
  7835. }
  7836. readHexChar(len, forceLen, throwOnInvalid) {
  7837. const codePos = this.state.pos;
  7838. const n = this.readInt(16, len, forceLen, false);
  7839. if (n === null) {
  7840. if (throwOnInvalid) {
  7841. this.raise(codePos, ErrorMessages.InvalidEscapeSequence);
  7842. } else {
  7843. this.state.pos = codePos - 1;
  7844. }
  7845. }
  7846. return n;
  7847. }
  7848. readWord1(firstCode) {
  7849. this.state.containsEsc = false;
  7850. let word = "";
  7851. const start = this.state.pos;
  7852. let chunkStart = this.state.pos;
  7853. if (firstCode !== undefined) {
  7854. this.state.pos += firstCode <= 0xffff ? 1 : 2;
  7855. }
  7856. while (this.state.pos < this.length) {
  7857. const ch = this.codePointAtPos(this.state.pos);
  7858. if (isIdentifierChar(ch)) {
  7859. this.state.pos += ch <= 0xffff ? 1 : 2;
  7860. } else if (ch === 92) {
  7861. this.state.containsEsc = true;
  7862. word += this.input.slice(chunkStart, this.state.pos);
  7863. const escStart = this.state.pos;
  7864. const identifierCheck = this.state.pos === start ? isIdentifierStart : isIdentifierChar;
  7865. if (this.input.charCodeAt(++this.state.pos) !== 117) {
  7866. this.raise(this.state.pos, ErrorMessages.MissingUnicodeEscape);
  7867. chunkStart = this.state.pos - 1;
  7868. continue;
  7869. }
  7870. ++this.state.pos;
  7871. const esc = this.readCodePoint(true);
  7872. if (esc !== null) {
  7873. if (!identifierCheck(esc)) {
  7874. this.raise(escStart, ErrorMessages.EscapedCharNotAnIdentifier);
  7875. }
  7876. word += String.fromCodePoint(esc);
  7877. }
  7878. chunkStart = this.state.pos;
  7879. } else {
  7880. break;
  7881. }
  7882. }
  7883. return word + this.input.slice(chunkStart, this.state.pos);
  7884. }
  7885. readWord(firstCode) {
  7886. const word = this.readWord1(firstCode);
  7887. const type = keywords$1.get(word) || types$1.name;
  7888. this.finishToken(type, word);
  7889. }
  7890. checkKeywordEscapes() {
  7891. const kw = this.state.type.keyword;
  7892. if (kw && this.state.containsEsc) {
  7893. this.raise(this.state.start, ErrorMessages.InvalidEscapedReservedWord, kw);
  7894. }
  7895. }
  7896. braceIsBlock(prevType) {
  7897. const parent = this.curContext();
  7898. if (parent === types.functionExpression || parent === types.functionStatement) {
  7899. return true;
  7900. }
  7901. if (prevType === types$1.colon && (parent === types.braceStatement || parent === types.braceExpression)) {
  7902. return !parent.isExpr;
  7903. }
  7904. if (prevType === types$1._return || prevType === types$1.name && this.state.exprAllowed) {
  7905. return this.hasPrecedingLineBreak();
  7906. }
  7907. if (prevType === types$1._else || prevType === types$1.semi || prevType === types$1.eof || prevType === types$1.parenR || prevType === types$1.arrow) {
  7908. return true;
  7909. }
  7910. if (prevType === types$1.braceL) {
  7911. return parent === types.braceStatement;
  7912. }
  7913. if (prevType === types$1._var || prevType === types$1._const || prevType === types$1.name) {
  7914. return false;
  7915. }
  7916. if (prevType === types$1.relational) {
  7917. return true;
  7918. }
  7919. return !this.state.exprAllowed;
  7920. }
  7921. updateContext(prevType) {
  7922. const type = this.state.type;
  7923. let update;
  7924. if (type.keyword && (prevType === types$1.dot || prevType === types$1.questionDot)) {
  7925. this.state.exprAllowed = false;
  7926. } else if (update = type.updateContext) {
  7927. update.call(this, prevType);
  7928. } else {
  7929. this.state.exprAllowed = type.beforeExpr;
  7930. }
  7931. }
  7932. }
  7933. class ClassScope {
  7934. constructor() {
  7935. this.privateNames = new Set();
  7936. this.loneAccessors = new Map();
  7937. this.undefinedPrivateNames = new Map();
  7938. }
  7939. }
  7940. class ClassScopeHandler {
  7941. constructor(raise) {
  7942. this.stack = [];
  7943. this.undefinedPrivateNames = new Map();
  7944. this.raise = raise;
  7945. }
  7946. current() {
  7947. return this.stack[this.stack.length - 1];
  7948. }
  7949. enter() {
  7950. this.stack.push(new ClassScope());
  7951. }
  7952. exit() {
  7953. const oldClassScope = this.stack.pop();
  7954. const current = this.current();
  7955. for (const [name, pos] of Array.from(oldClassScope.undefinedPrivateNames)) {
  7956. if (current) {
  7957. if (!current.undefinedPrivateNames.has(name)) {
  7958. current.undefinedPrivateNames.set(name, pos);
  7959. }
  7960. } else {
  7961. this.raise(pos, ErrorMessages.InvalidPrivateFieldResolution, name);
  7962. }
  7963. }
  7964. }
  7965. declarePrivateName(name, elementType, pos) {
  7966. const classScope = this.current();
  7967. let redefined = classScope.privateNames.has(name);
  7968. if (elementType & CLASS_ELEMENT_KIND_ACCESSOR) {
  7969. const accessor = redefined && classScope.loneAccessors.get(name);
  7970. if (accessor) {
  7971. const oldStatic = accessor & CLASS_ELEMENT_FLAG_STATIC;
  7972. const newStatic = elementType & CLASS_ELEMENT_FLAG_STATIC;
  7973. const oldKind = accessor & CLASS_ELEMENT_KIND_ACCESSOR;
  7974. const newKind = elementType & CLASS_ELEMENT_KIND_ACCESSOR;
  7975. redefined = oldKind === newKind || oldStatic !== newStatic;
  7976. if (!redefined) classScope.loneAccessors.delete(name);
  7977. } else if (!redefined) {
  7978. classScope.loneAccessors.set(name, elementType);
  7979. }
  7980. }
  7981. if (redefined) {
  7982. this.raise(pos, ErrorMessages.PrivateNameRedeclaration, name);
  7983. }
  7984. classScope.privateNames.add(name);
  7985. classScope.undefinedPrivateNames.delete(name);
  7986. }
  7987. usePrivateName(name, pos) {
  7988. let classScope;
  7989. for (classScope of this.stack) {
  7990. if (classScope.privateNames.has(name)) return;
  7991. }
  7992. if (classScope) {
  7993. classScope.undefinedPrivateNames.set(name, pos);
  7994. } else {
  7995. this.raise(pos, ErrorMessages.InvalidPrivateFieldResolution, name);
  7996. }
  7997. }
  7998. }
  7999. const kExpression = 0,
  8000. kMaybeArrowParameterDeclaration = 1,
  8001. kMaybeAsyncArrowParameterDeclaration = 2,
  8002. kParameterDeclaration = 3;
  8003. class ExpressionScope {
  8004. constructor(type = kExpression) {
  8005. this.type = void 0;
  8006. this.type = type;
  8007. }
  8008. canBeArrowParameterDeclaration() {
  8009. return this.type === kMaybeAsyncArrowParameterDeclaration || this.type === kMaybeArrowParameterDeclaration;
  8010. }
  8011. isCertainlyParameterDeclaration() {
  8012. return this.type === kParameterDeclaration;
  8013. }
  8014. }
  8015. class ArrowHeadParsingScope extends ExpressionScope {
  8016. constructor(type) {
  8017. super(type);
  8018. this.errors = new Map();
  8019. }
  8020. recordDeclarationError(pos, template) {
  8021. this.errors.set(pos, template);
  8022. }
  8023. clearDeclarationError(pos) {
  8024. this.errors.delete(pos);
  8025. }
  8026. iterateErrors(iterator) {
  8027. this.errors.forEach(iterator);
  8028. }
  8029. }
  8030. class ExpressionScopeHandler {
  8031. constructor(raise) {
  8032. this.stack = [new ExpressionScope()];
  8033. this.raise = raise;
  8034. }
  8035. enter(scope) {
  8036. this.stack.push(scope);
  8037. }
  8038. exit() {
  8039. this.stack.pop();
  8040. }
  8041. recordParameterInitializerError(pos, template) {
  8042. const {
  8043. stack
  8044. } = this;
  8045. let i = stack.length - 1;
  8046. let scope = stack[i];
  8047. while (!scope.isCertainlyParameterDeclaration()) {
  8048. if (scope.canBeArrowParameterDeclaration()) {
  8049. scope.recordDeclarationError(pos, template);
  8050. } else {
  8051. return;
  8052. }
  8053. scope = stack[--i];
  8054. }
  8055. this.raise(pos, template);
  8056. }
  8057. recordParenthesizedIdentifierError(pos, template) {
  8058. const {
  8059. stack
  8060. } = this;
  8061. const scope = stack[stack.length - 1];
  8062. if (scope.isCertainlyParameterDeclaration()) {
  8063. this.raise(pos, template);
  8064. } else if (scope.canBeArrowParameterDeclaration()) {
  8065. scope.recordDeclarationError(pos, template);
  8066. } else {
  8067. return;
  8068. }
  8069. }
  8070. recordAsyncArrowParametersError(pos, template) {
  8071. const {
  8072. stack
  8073. } = this;
  8074. let i = stack.length - 1;
  8075. let scope = stack[i];
  8076. while (scope.canBeArrowParameterDeclaration()) {
  8077. if (scope.type === kMaybeAsyncArrowParameterDeclaration) {
  8078. scope.recordDeclarationError(pos, template);
  8079. }
  8080. scope = stack[--i];
  8081. }
  8082. }
  8083. validateAsPattern() {
  8084. const {
  8085. stack
  8086. } = this;
  8087. const currentScope = stack[stack.length - 1];
  8088. if (!currentScope.canBeArrowParameterDeclaration()) return;
  8089. currentScope.iterateErrors((template, pos) => {
  8090. this.raise(pos, template);
  8091. let i = stack.length - 2;
  8092. let scope = stack[i];
  8093. while (scope.canBeArrowParameterDeclaration()) {
  8094. scope.clearDeclarationError(pos);
  8095. scope = stack[--i];
  8096. }
  8097. });
  8098. }
  8099. }
  8100. function newParameterDeclarationScope() {
  8101. return new ExpressionScope(kParameterDeclaration);
  8102. }
  8103. function newArrowHeadScope() {
  8104. return new ArrowHeadParsingScope(kMaybeArrowParameterDeclaration);
  8105. }
  8106. function newAsyncArrowScope() {
  8107. return new ArrowHeadParsingScope(kMaybeAsyncArrowParameterDeclaration);
  8108. }
  8109. function newExpressionScope() {
  8110. return new ExpressionScope();
  8111. }
  8112. class UtilParser extends Tokenizer {
  8113. addExtra(node, key, val) {
  8114. if (!node) return;
  8115. const extra = node.extra = node.extra || {};
  8116. extra[key] = val;
  8117. }
  8118. isRelational(op) {
  8119. return this.match(types$1.relational) && this.state.value === op;
  8120. }
  8121. expectRelational(op) {
  8122. if (this.isRelational(op)) {
  8123. this.next();
  8124. } else {
  8125. this.unexpected(null, types$1.relational);
  8126. }
  8127. }
  8128. isContextual(name) {
  8129. return this.match(types$1.name) && this.state.value === name && !this.state.containsEsc;
  8130. }
  8131. isUnparsedContextual(nameStart, name) {
  8132. const nameEnd = nameStart + name.length;
  8133. if (this.input.slice(nameStart, nameEnd) === name) {
  8134. const nextCh = this.input.charCodeAt(nameEnd);
  8135. return !(isIdentifierChar(nextCh) || (nextCh & 0xfc00) === 0xd800);
  8136. }
  8137. return false;
  8138. }
  8139. isLookaheadContextual(name) {
  8140. const next = this.nextTokenStart();
  8141. return this.isUnparsedContextual(next, name);
  8142. }
  8143. eatContextual(name) {
  8144. return this.isContextual(name) && this.eat(types$1.name);
  8145. }
  8146. expectContextual(name, template) {
  8147. if (!this.eatContextual(name)) this.unexpected(null, template);
  8148. }
  8149. canInsertSemicolon() {
  8150. return this.match(types$1.eof) || this.match(types$1.braceR) || this.hasPrecedingLineBreak();
  8151. }
  8152. hasPrecedingLineBreak() {
  8153. return lineBreak.test(this.input.slice(this.state.lastTokEnd, this.state.start));
  8154. }
  8155. hasFollowingLineBreak() {
  8156. return lineBreak.test(this.input.slice(this.state.end, this.nextTokenStart()));
  8157. }
  8158. isLineTerminator() {
  8159. return this.eat(types$1.semi) || this.canInsertSemicolon();
  8160. }
  8161. semicolon(allowAsi = true) {
  8162. if (allowAsi ? this.isLineTerminator() : this.eat(types$1.semi)) return;
  8163. this.raise(this.state.lastTokEnd, ErrorMessages.MissingSemicolon);
  8164. }
  8165. expect(type, pos) {
  8166. this.eat(type) || this.unexpected(pos, type);
  8167. }
  8168. assertNoSpace(message = "Unexpected space.") {
  8169. if (this.state.start > this.state.lastTokEnd) {
  8170. this.raise(this.state.lastTokEnd, {
  8171. code: ErrorCodes.SyntaxError,
  8172. reasonCode: "UnexpectedSpace",
  8173. template: message
  8174. });
  8175. }
  8176. }
  8177. unexpected(pos, messageOrType = {
  8178. code: ErrorCodes.SyntaxError,
  8179. reasonCode: "UnexpectedToken",
  8180. template: "Unexpected token"
  8181. }) {
  8182. if (messageOrType instanceof TokenType) {
  8183. messageOrType = {
  8184. code: ErrorCodes.SyntaxError,
  8185. reasonCode: "UnexpectedToken",
  8186. template: `Unexpected token, expected "${messageOrType.label}"`
  8187. };
  8188. }
  8189. throw this.raise(pos != null ? pos : this.state.start, messageOrType);
  8190. }
  8191. expectPlugin(name, pos) {
  8192. if (!this.hasPlugin(name)) {
  8193. throw this.raiseWithData(pos != null ? pos : this.state.start, {
  8194. missingPlugin: [name]
  8195. }, `This experimental syntax requires enabling the parser plugin: '${name}'`);
  8196. }
  8197. return true;
  8198. }
  8199. expectOnePlugin(names, pos) {
  8200. if (!names.some(n => this.hasPlugin(n))) {
  8201. throw this.raiseWithData(pos != null ? pos : this.state.start, {
  8202. missingPlugin: names
  8203. }, `This experimental syntax requires enabling one of the following parser plugin(s): '${names.join(", ")}'`);
  8204. }
  8205. }
  8206. tryParse(fn, oldState = this.state.clone()) {
  8207. const abortSignal = {
  8208. node: null
  8209. };
  8210. try {
  8211. const node = fn((node = null) => {
  8212. abortSignal.node = node;
  8213. throw abortSignal;
  8214. });
  8215. if (this.state.errors.length > oldState.errors.length) {
  8216. const failState = this.state;
  8217. this.state = oldState;
  8218. this.state.tokensLength = failState.tokensLength;
  8219. return {
  8220. node,
  8221. error: failState.errors[oldState.errors.length],
  8222. thrown: false,
  8223. aborted: false,
  8224. failState
  8225. };
  8226. }
  8227. return {
  8228. node,
  8229. error: null,
  8230. thrown: false,
  8231. aborted: false,
  8232. failState: null
  8233. };
  8234. } catch (error) {
  8235. const failState = this.state;
  8236. this.state = oldState;
  8237. if (error instanceof SyntaxError) {
  8238. return {
  8239. node: null,
  8240. error,
  8241. thrown: true,
  8242. aborted: false,
  8243. failState
  8244. };
  8245. }
  8246. if (error === abortSignal) {
  8247. return {
  8248. node: abortSignal.node,
  8249. error: null,
  8250. thrown: false,
  8251. aborted: true,
  8252. failState
  8253. };
  8254. }
  8255. throw error;
  8256. }
  8257. }
  8258. checkExpressionErrors(refExpressionErrors, andThrow) {
  8259. if (!refExpressionErrors) return false;
  8260. const {
  8261. shorthandAssign,
  8262. doubleProto
  8263. } = refExpressionErrors;
  8264. if (!andThrow) return shorthandAssign >= 0 || doubleProto >= 0;
  8265. if (shorthandAssign >= 0) {
  8266. this.unexpected(shorthandAssign);
  8267. }
  8268. if (doubleProto >= 0) {
  8269. this.raise(doubleProto, ErrorMessages.DuplicateProto);
  8270. }
  8271. }
  8272. isLiteralPropertyName() {
  8273. 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);
  8274. }
  8275. isPrivateName(node) {
  8276. return node.type === "PrivateName";
  8277. }
  8278. getPrivateNameSV(node) {
  8279. return node.id.name;
  8280. }
  8281. hasPropertyAsPrivateName(node) {
  8282. return (node.type === "MemberExpression" || node.type === "OptionalMemberExpression") && this.isPrivateName(node.property);
  8283. }
  8284. isOptionalChain(node) {
  8285. return node.type === "OptionalMemberExpression" || node.type === "OptionalCallExpression";
  8286. }
  8287. isObjectProperty(node) {
  8288. return node.type === "ObjectProperty";
  8289. }
  8290. isObjectMethod(node) {
  8291. return node.type === "ObjectMethod";
  8292. }
  8293. initializeScopes(inModule = this.options.sourceType === "module") {
  8294. const oldLabels = this.state.labels;
  8295. this.state.labels = [];
  8296. const oldExportedIdentifiers = this.state.exportedIdentifiers;
  8297. this.state.exportedIdentifiers = [];
  8298. const oldInModule = this.inModule;
  8299. this.inModule = inModule;
  8300. const oldScope = this.scope;
  8301. const ScopeHandler = this.getScopeHandler();
  8302. this.scope = new ScopeHandler(this.raise.bind(this), this.inModule);
  8303. const oldProdParam = this.prodParam;
  8304. this.prodParam = new ProductionParameterHandler();
  8305. const oldClassScope = this.classScope;
  8306. this.classScope = new ClassScopeHandler(this.raise.bind(this));
  8307. const oldExpressionScope = this.expressionScope;
  8308. this.expressionScope = new ExpressionScopeHandler(this.raise.bind(this));
  8309. return () => {
  8310. this.state.labels = oldLabels;
  8311. this.state.exportedIdentifiers = oldExportedIdentifiers;
  8312. this.inModule = oldInModule;
  8313. this.scope = oldScope;
  8314. this.prodParam = oldProdParam;
  8315. this.classScope = oldClassScope;
  8316. this.expressionScope = oldExpressionScope;
  8317. };
  8318. }
  8319. enterInitialScopes() {
  8320. let paramFlags = PARAM;
  8321. if (this.hasPlugin("topLevelAwait") && this.inModule) {
  8322. paramFlags |= PARAM_AWAIT;
  8323. }
  8324. this.scope.enter(SCOPE_PROGRAM);
  8325. this.prodParam.enter(paramFlags);
  8326. }
  8327. }
  8328. class ExpressionErrors {
  8329. constructor() {
  8330. this.shorthandAssign = -1;
  8331. this.doubleProto = -1;
  8332. }
  8333. }
  8334. class Node {
  8335. constructor(parser, pos, loc) {
  8336. this.type = void 0;
  8337. this.start = void 0;
  8338. this.end = void 0;
  8339. this.loc = void 0;
  8340. this.range = void 0;
  8341. this.leadingComments = void 0;
  8342. this.trailingComments = void 0;
  8343. this.innerComments = void 0;
  8344. this.extra = void 0;
  8345. this.type = "";
  8346. this.start = pos;
  8347. this.end = 0;
  8348. this.loc = new SourceLocation(loc);
  8349. if (parser != null && parser.options.ranges) this.range = [pos, 0];
  8350. if (parser != null && parser.filename) this.loc.filename = parser.filename;
  8351. }
  8352. __clone() {
  8353. const newNode = new Node();
  8354. const keys = Object.keys(this);
  8355. for (let i = 0, length = keys.length; i < length; i++) {
  8356. const key = keys[i];
  8357. if (key !== "leadingComments" && key !== "trailingComments" && key !== "innerComments") {
  8358. newNode[key] = this[key];
  8359. }
  8360. }
  8361. return newNode;
  8362. }
  8363. }
  8364. class NodeUtils extends UtilParser {
  8365. startNode() {
  8366. return new Node(this, this.state.start, this.state.startLoc);
  8367. }
  8368. startNodeAt(pos, loc) {
  8369. return new Node(this, pos, loc);
  8370. }
  8371. startNodeAtNode(type) {
  8372. return this.startNodeAt(type.start, type.loc.start);
  8373. }
  8374. finishNode(node, type) {
  8375. return this.finishNodeAt(node, type, this.state.lastTokEnd, this.state.lastTokEndLoc);
  8376. }
  8377. finishNodeAt(node, type, pos, loc) {
  8378. node.type = type;
  8379. node.end = pos;
  8380. node.loc.end = loc;
  8381. if (this.options.ranges) node.range[1] = pos;
  8382. this.processComment(node);
  8383. return node;
  8384. }
  8385. resetStartLocation(node, start, startLoc) {
  8386. node.start = start;
  8387. node.loc.start = startLoc;
  8388. if (this.options.ranges) node.range[0] = start;
  8389. }
  8390. resetEndLocation(node, end = this.state.lastTokEnd, endLoc = this.state.lastTokEndLoc) {
  8391. node.end = end;
  8392. node.loc.end = endLoc;
  8393. if (this.options.ranges) node.range[1] = end;
  8394. }
  8395. resetStartLocationFromNode(node, locationNode) {
  8396. this.resetStartLocation(node, locationNode.start, locationNode.loc.start);
  8397. }
  8398. }
  8399. const unwrapParenthesizedExpression = node => {
  8400. return node.type === "ParenthesizedExpression" ? unwrapParenthesizedExpression(node.expression) : node;
  8401. };
  8402. class LValParser extends NodeUtils {
  8403. toAssignable(node, isLHS = false) {
  8404. var _node$extra, _node$extra3;
  8405. let parenthesized = undefined;
  8406. if (node.type === "ParenthesizedExpression" || (_node$extra = node.extra) != null && _node$extra.parenthesized) {
  8407. parenthesized = unwrapParenthesizedExpression(node);
  8408. if (isLHS) {
  8409. if (parenthesized.type === "Identifier") {
  8410. this.expressionScope.recordParenthesizedIdentifierError(node.start, ErrorMessages.InvalidParenthesizedAssignment);
  8411. } else if (parenthesized.type !== "MemberExpression") {
  8412. this.raise(node.start, ErrorMessages.InvalidParenthesizedAssignment);
  8413. }
  8414. } else {
  8415. this.raise(node.start, ErrorMessages.InvalidParenthesizedAssignment);
  8416. }
  8417. }
  8418. switch (node.type) {
  8419. case "Identifier":
  8420. case "ObjectPattern":
  8421. case "ArrayPattern":
  8422. case "AssignmentPattern":
  8423. break;
  8424. case "ObjectExpression":
  8425. node.type = "ObjectPattern";
  8426. for (let i = 0, length = node.properties.length, last = length - 1; i < length; i++) {
  8427. var _node$extra2;
  8428. const prop = node.properties[i];
  8429. const isLast = i === last;
  8430. this.toAssignableObjectExpressionProp(prop, isLast, isLHS);
  8431. if (isLast && prop.type === "RestElement" && (_node$extra2 = node.extra) != null && _node$extra2.trailingComma) {
  8432. this.raiseRestNotLast(node.extra.trailingComma);
  8433. }
  8434. }
  8435. break;
  8436. case "ObjectProperty":
  8437. this.toAssignable(node.value, isLHS);
  8438. break;
  8439. case "SpreadElement":
  8440. {
  8441. this.checkToRestConversion(node);
  8442. node.type = "RestElement";
  8443. const arg = node.argument;
  8444. this.toAssignable(arg, isLHS);
  8445. break;
  8446. }
  8447. case "ArrayExpression":
  8448. node.type = "ArrayPattern";
  8449. this.toAssignableList(node.elements, (_node$extra3 = node.extra) == null ? void 0 : _node$extra3.trailingComma, isLHS);
  8450. break;
  8451. case "AssignmentExpression":
  8452. if (node.operator !== "=") {
  8453. this.raise(node.left.end, ErrorMessages.MissingEqInAssignment);
  8454. }
  8455. node.type = "AssignmentPattern";
  8456. delete node.operator;
  8457. this.toAssignable(node.left, isLHS);
  8458. break;
  8459. case "ParenthesizedExpression":
  8460. this.toAssignable(parenthesized, isLHS);
  8461. break;
  8462. }
  8463. return node;
  8464. }
  8465. toAssignableObjectExpressionProp(prop, isLast, isLHS) {
  8466. if (prop.type === "ObjectMethod") {
  8467. const error = prop.kind === "get" || prop.kind === "set" ? ErrorMessages.PatternHasAccessor : ErrorMessages.PatternHasMethod;
  8468. this.raise(prop.key.start, error);
  8469. } else if (prop.type === "SpreadElement" && !isLast) {
  8470. this.raiseRestNotLast(prop.start);
  8471. } else {
  8472. this.toAssignable(prop, isLHS);
  8473. }
  8474. }
  8475. toAssignableList(exprList, trailingCommaPos, isLHS) {
  8476. let end = exprList.length;
  8477. if (end) {
  8478. const last = exprList[end - 1];
  8479. if ((last == null ? void 0 : last.type) === "RestElement") {
  8480. --end;
  8481. } else if ((last == null ? void 0 : last.type) === "SpreadElement") {
  8482. last.type = "RestElement";
  8483. let arg = last.argument;
  8484. this.toAssignable(arg, isLHS);
  8485. arg = unwrapParenthesizedExpression(arg);
  8486. if (arg.type !== "Identifier" && arg.type !== "MemberExpression" && arg.type !== "ArrayPattern" && arg.type !== "ObjectPattern") {
  8487. this.unexpected(arg.start);
  8488. }
  8489. if (trailingCommaPos) {
  8490. this.raiseTrailingCommaAfterRest(trailingCommaPos);
  8491. }
  8492. --end;
  8493. }
  8494. }
  8495. for (let i = 0; i < end; i++) {
  8496. const elt = exprList[i];
  8497. if (elt) {
  8498. this.toAssignable(elt, isLHS);
  8499. if (elt.type === "RestElement") {
  8500. this.raiseRestNotLast(elt.start);
  8501. }
  8502. }
  8503. }
  8504. return exprList;
  8505. }
  8506. toReferencedList(exprList, isParenthesizedExpr) {
  8507. return exprList;
  8508. }
  8509. toReferencedListDeep(exprList, isParenthesizedExpr) {
  8510. this.toReferencedList(exprList, isParenthesizedExpr);
  8511. for (const expr of exprList) {
  8512. if ((expr == null ? void 0 : expr.type) === "ArrayExpression") {
  8513. this.toReferencedListDeep(expr.elements);
  8514. }
  8515. }
  8516. }
  8517. parseSpread(refExpressionErrors, refNeedsArrowPos) {
  8518. const node = this.startNode();
  8519. this.next();
  8520. node.argument = this.parseMaybeAssignAllowIn(refExpressionErrors, undefined, refNeedsArrowPos);
  8521. return this.finishNode(node, "SpreadElement");
  8522. }
  8523. parseRestBinding() {
  8524. const node = this.startNode();
  8525. this.next();
  8526. node.argument = this.parseBindingAtom();
  8527. return this.finishNode(node, "RestElement");
  8528. }
  8529. parseBindingAtom() {
  8530. switch (this.state.type) {
  8531. case types$1.bracketL:
  8532. {
  8533. const node = this.startNode();
  8534. this.next();
  8535. node.elements = this.parseBindingList(types$1.bracketR, 93, true);
  8536. return this.finishNode(node, "ArrayPattern");
  8537. }
  8538. case types$1.braceL:
  8539. return this.parseObjectLike(types$1.braceR, true);
  8540. }
  8541. return this.parseIdentifier();
  8542. }
  8543. parseBindingList(close, closeCharCode, allowEmpty, allowModifiers) {
  8544. const elts = [];
  8545. let first = true;
  8546. while (!this.eat(close)) {
  8547. if (first) {
  8548. first = false;
  8549. } else {
  8550. this.expect(types$1.comma);
  8551. }
  8552. if (allowEmpty && this.match(types$1.comma)) {
  8553. elts.push(null);
  8554. } else if (this.eat(close)) {
  8555. break;
  8556. } else if (this.match(types$1.ellipsis)) {
  8557. elts.push(this.parseAssignableListItemTypes(this.parseRestBinding()));
  8558. this.checkCommaAfterRest(closeCharCode);
  8559. this.expect(close);
  8560. break;
  8561. } else {
  8562. const decorators = [];
  8563. if (this.match(types$1.at) && this.hasPlugin("decorators")) {
  8564. this.raise(this.state.start, ErrorMessages.UnsupportedParameterDecorator);
  8565. }
  8566. while (this.match(types$1.at)) {
  8567. decorators.push(this.parseDecorator());
  8568. }
  8569. elts.push(this.parseAssignableListItem(allowModifiers, decorators));
  8570. }
  8571. }
  8572. return elts;
  8573. }
  8574. parseAssignableListItem(allowModifiers, decorators) {
  8575. const left = this.parseMaybeDefault();
  8576. this.parseAssignableListItemTypes(left);
  8577. const elt = this.parseMaybeDefault(left.start, left.loc.start, left);
  8578. if (decorators.length) {
  8579. left.decorators = decorators;
  8580. }
  8581. return elt;
  8582. }
  8583. parseAssignableListItemTypes(param) {
  8584. return param;
  8585. }
  8586. parseMaybeDefault(startPos, startLoc, left) {
  8587. var _startLoc, _startPos, _left;
  8588. startLoc = (_startLoc = startLoc) != null ? _startLoc : this.state.startLoc;
  8589. startPos = (_startPos = startPos) != null ? _startPos : this.state.start;
  8590. left = (_left = left) != null ? _left : this.parseBindingAtom();
  8591. if (!this.eat(types$1.eq)) return left;
  8592. const node = this.startNodeAt(startPos, startLoc);
  8593. node.left = left;
  8594. node.right = this.parseMaybeAssignAllowIn();
  8595. return this.finishNode(node, "AssignmentPattern");
  8596. }
  8597. checkLVal(expr, contextDescription, bindingType = BIND_NONE, checkClashes, disallowLetBinding, strictModeChanged = false) {
  8598. switch (expr.type) {
  8599. case "Identifier":
  8600. {
  8601. const {
  8602. name
  8603. } = expr;
  8604. if (this.state.strict && (strictModeChanged ? isStrictBindReservedWord(name, this.inModule) : isStrictBindOnlyReservedWord(name))) {
  8605. this.raise(expr.start, bindingType === BIND_NONE ? ErrorMessages.StrictEvalArguments : ErrorMessages.StrictEvalArgumentsBinding, name);
  8606. }
  8607. if (checkClashes) {
  8608. if (checkClashes.has(name)) {
  8609. this.raise(expr.start, ErrorMessages.ParamDupe);
  8610. } else {
  8611. checkClashes.add(name);
  8612. }
  8613. }
  8614. if (disallowLetBinding && name === "let") {
  8615. this.raise(expr.start, ErrorMessages.LetInLexicalBinding);
  8616. }
  8617. if (!(bindingType & BIND_NONE)) {
  8618. this.scope.declareName(name, bindingType, expr.start);
  8619. }
  8620. break;
  8621. }
  8622. case "MemberExpression":
  8623. if (bindingType !== BIND_NONE) {
  8624. this.raise(expr.start, ErrorMessages.InvalidPropertyBindingPattern);
  8625. }
  8626. break;
  8627. case "ObjectPattern":
  8628. for (let prop of expr.properties) {
  8629. if (this.isObjectProperty(prop)) prop = prop.value;else if (this.isObjectMethod(prop)) continue;
  8630. this.checkLVal(prop, "object destructuring pattern", bindingType, checkClashes, disallowLetBinding);
  8631. }
  8632. break;
  8633. case "ArrayPattern":
  8634. for (const elem of expr.elements) {
  8635. if (elem) {
  8636. this.checkLVal(elem, "array destructuring pattern", bindingType, checkClashes, disallowLetBinding);
  8637. }
  8638. }
  8639. break;
  8640. case "AssignmentPattern":
  8641. this.checkLVal(expr.left, "assignment pattern", bindingType, checkClashes);
  8642. break;
  8643. case "RestElement":
  8644. this.checkLVal(expr.argument, "rest element", bindingType, checkClashes);
  8645. break;
  8646. case "ParenthesizedExpression":
  8647. this.checkLVal(expr.expression, "parenthesized expression", bindingType, checkClashes);
  8648. break;
  8649. default:
  8650. {
  8651. this.raise(expr.start, bindingType === BIND_NONE ? ErrorMessages.InvalidLhs : ErrorMessages.InvalidLhsBinding, contextDescription);
  8652. }
  8653. }
  8654. }
  8655. checkToRestConversion(node) {
  8656. if (node.argument.type !== "Identifier" && node.argument.type !== "MemberExpression") {
  8657. this.raise(node.argument.start, ErrorMessages.InvalidRestAssignmentPattern);
  8658. }
  8659. }
  8660. checkCommaAfterRest(close) {
  8661. if (this.match(types$1.comma)) {
  8662. if (this.lookaheadCharCode() === close) {
  8663. this.raiseTrailingCommaAfterRest(this.state.start);
  8664. } else {
  8665. this.raiseRestNotLast(this.state.start);
  8666. }
  8667. }
  8668. }
  8669. raiseRestNotLast(pos) {
  8670. throw this.raise(pos, ErrorMessages.ElementAfterRest);
  8671. }
  8672. raiseTrailingCommaAfterRest(pos) {
  8673. this.raise(pos, ErrorMessages.RestTrailingComma);
  8674. }
  8675. }
  8676. class ExpressionParser extends LValParser {
  8677. checkProto(prop, isRecord, protoRef, refExpressionErrors) {
  8678. if (prop.type === "SpreadElement" || this.isObjectMethod(prop) || prop.computed || prop.shorthand) {
  8679. return;
  8680. }
  8681. const key = prop.key;
  8682. const name = key.type === "Identifier" ? key.name : key.value;
  8683. if (name === "__proto__") {
  8684. if (isRecord) {
  8685. this.raise(key.start, ErrorMessages.RecordNoProto);
  8686. return;
  8687. }
  8688. if (protoRef.used) {
  8689. if (refExpressionErrors) {
  8690. if (refExpressionErrors.doubleProto === -1) {
  8691. refExpressionErrors.doubleProto = key.start;
  8692. }
  8693. } else {
  8694. this.raise(key.start, ErrorMessages.DuplicateProto);
  8695. }
  8696. }
  8697. protoRef.used = true;
  8698. }
  8699. }
  8700. shouldExitDescending(expr, potentialArrowAt) {
  8701. return expr.type === "ArrowFunctionExpression" && expr.start === potentialArrowAt;
  8702. }
  8703. getExpression() {
  8704. let paramFlags = PARAM;
  8705. if (this.hasPlugin("topLevelAwait") && this.inModule) {
  8706. paramFlags |= PARAM_AWAIT;
  8707. }
  8708. this.scope.enter(SCOPE_PROGRAM);
  8709. this.prodParam.enter(paramFlags);
  8710. this.nextToken();
  8711. const expr = this.parseExpression();
  8712. if (!this.match(types$1.eof)) {
  8713. this.unexpected();
  8714. }
  8715. expr.comments = this.state.comments;
  8716. expr.errors = this.state.errors;
  8717. if (this.options.tokens) {
  8718. expr.tokens = this.tokens;
  8719. }
  8720. return expr;
  8721. }
  8722. parseExpression(disallowIn, refExpressionErrors) {
  8723. if (disallowIn) {
  8724. return this.disallowInAnd(() => this.parseExpressionBase(refExpressionErrors));
  8725. }
  8726. return this.allowInAnd(() => this.parseExpressionBase(refExpressionErrors));
  8727. }
  8728. parseExpressionBase(refExpressionErrors) {
  8729. const startPos = this.state.start;
  8730. const startLoc = this.state.startLoc;
  8731. const expr = this.parseMaybeAssign(refExpressionErrors);
  8732. if (this.match(types$1.comma)) {
  8733. const node = this.startNodeAt(startPos, startLoc);
  8734. node.expressions = [expr];
  8735. while (this.eat(types$1.comma)) {
  8736. node.expressions.push(this.parseMaybeAssign(refExpressionErrors));
  8737. }
  8738. this.toReferencedList(node.expressions);
  8739. return this.finishNode(node, "SequenceExpression");
  8740. }
  8741. return expr;
  8742. }
  8743. parseMaybeAssignDisallowIn(refExpressionErrors, afterLeftParse, refNeedsArrowPos) {
  8744. return this.disallowInAnd(() => this.parseMaybeAssign(refExpressionErrors, afterLeftParse, refNeedsArrowPos));
  8745. }
  8746. parseMaybeAssignAllowIn(refExpressionErrors, afterLeftParse, refNeedsArrowPos) {
  8747. return this.allowInAnd(() => this.parseMaybeAssign(refExpressionErrors, afterLeftParse, refNeedsArrowPos));
  8748. }
  8749. parseMaybeAssign(refExpressionErrors, afterLeftParse, refNeedsArrowPos) {
  8750. const startPos = this.state.start;
  8751. const startLoc = this.state.startLoc;
  8752. if (this.isContextual("yield")) {
  8753. if (this.prodParam.hasYield) {
  8754. this.state.exprAllowed = true;
  8755. let left = this.parseYield();
  8756. if (afterLeftParse) {
  8757. left = afterLeftParse.call(this, left, startPos, startLoc);
  8758. }
  8759. return left;
  8760. }
  8761. }
  8762. let ownExpressionErrors;
  8763. if (refExpressionErrors) {
  8764. ownExpressionErrors = false;
  8765. } else {
  8766. refExpressionErrors = new ExpressionErrors();
  8767. ownExpressionErrors = true;
  8768. }
  8769. if (this.match(types$1.parenL) || this.match(types$1.name)) {
  8770. this.state.potentialArrowAt = this.state.start;
  8771. }
  8772. let left = this.parseMaybeConditional(refExpressionErrors, refNeedsArrowPos);
  8773. if (afterLeftParse) {
  8774. left = afterLeftParse.call(this, left, startPos, startLoc);
  8775. }
  8776. if (this.state.type.isAssign) {
  8777. const node = this.startNodeAt(startPos, startLoc);
  8778. const operator = this.state.value;
  8779. node.operator = operator;
  8780. if (this.match(types$1.eq)) {
  8781. node.left = this.toAssignable(left, true);
  8782. refExpressionErrors.doubleProto = -1;
  8783. } else {
  8784. node.left = left;
  8785. }
  8786. if (refExpressionErrors.shorthandAssign >= node.left.start) {
  8787. refExpressionErrors.shorthandAssign = -1;
  8788. }
  8789. this.checkLVal(left, "assignment expression");
  8790. this.next();
  8791. node.right = this.parseMaybeAssign();
  8792. return this.finishNode(node, "AssignmentExpression");
  8793. } else if (ownExpressionErrors) {
  8794. this.checkExpressionErrors(refExpressionErrors, true);
  8795. }
  8796. return left;
  8797. }
  8798. parseMaybeConditional(refExpressionErrors, refNeedsArrowPos) {
  8799. const startPos = this.state.start;
  8800. const startLoc = this.state.startLoc;
  8801. const potentialArrowAt = this.state.potentialArrowAt;
  8802. const expr = this.parseExprOps(refExpressionErrors);
  8803. if (this.shouldExitDescending(expr, potentialArrowAt)) {
  8804. return expr;
  8805. }
  8806. return this.parseConditional(expr, startPos, startLoc, refNeedsArrowPos);
  8807. }
  8808. parseConditional(expr, startPos, startLoc, refNeedsArrowPos) {
  8809. if (this.eat(types$1.question)) {
  8810. const node = this.startNodeAt(startPos, startLoc);
  8811. node.test = expr;
  8812. node.consequent = this.parseMaybeAssignAllowIn();
  8813. this.expect(types$1.colon);
  8814. node.alternate = this.parseMaybeAssign();
  8815. return this.finishNode(node, "ConditionalExpression");
  8816. }
  8817. return expr;
  8818. }
  8819. parseExprOps(refExpressionErrors) {
  8820. const startPos = this.state.start;
  8821. const startLoc = this.state.startLoc;
  8822. const potentialArrowAt = this.state.potentialArrowAt;
  8823. const expr = this.parseMaybeUnary(refExpressionErrors);
  8824. if (this.shouldExitDescending(expr, potentialArrowAt)) {
  8825. return expr;
  8826. }
  8827. return this.parseExprOp(expr, startPos, startLoc, -1);
  8828. }
  8829. parseExprOp(left, leftStartPos, leftStartLoc, minPrec) {
  8830. let prec = this.state.type.binop;
  8831. if (prec != null && (this.prodParam.hasIn || !this.match(types$1._in))) {
  8832. if (prec > minPrec) {
  8833. const op = this.state.type;
  8834. if (op === types$1.pipeline) {
  8835. this.expectPlugin("pipelineOperator");
  8836. if (this.state.inFSharpPipelineDirectBody) {
  8837. return left;
  8838. }
  8839. this.state.inPipeline = true;
  8840. this.checkPipelineAtInfixOperator(left, leftStartPos);
  8841. }
  8842. const node = this.startNodeAt(leftStartPos, leftStartLoc);
  8843. node.left = left;
  8844. node.operator = this.state.value;
  8845. const logical = op === types$1.logicalOR || op === types$1.logicalAND;
  8846. const coalesce = op === types$1.nullishCoalescing;
  8847. if (coalesce) {
  8848. prec = types$1.logicalAND.binop;
  8849. }
  8850. this.next();
  8851. if (op === types$1.pipeline && this.getPluginOption("pipelineOperator", "proposal") === "minimal") {
  8852. if (this.match(types$1.name) && this.state.value === "await" && this.prodParam.hasAwait) {
  8853. throw this.raise(this.state.start, ErrorMessages.UnexpectedAwaitAfterPipelineBody);
  8854. }
  8855. }
  8856. node.right = this.parseExprOpRightExpr(op, prec);
  8857. this.finishNode(node, logical || coalesce ? "LogicalExpression" : "BinaryExpression");
  8858. const nextOp = this.state.type;
  8859. if (coalesce && (nextOp === types$1.logicalOR || nextOp === types$1.logicalAND) || logical && nextOp === types$1.nullishCoalescing) {
  8860. throw this.raise(this.state.start, ErrorMessages.MixingCoalesceWithLogical);
  8861. }
  8862. return this.parseExprOp(node, leftStartPos, leftStartLoc, minPrec);
  8863. }
  8864. }
  8865. return left;
  8866. }
  8867. parseExprOpRightExpr(op, prec) {
  8868. const startPos = this.state.start;
  8869. const startLoc = this.state.startLoc;
  8870. switch (op) {
  8871. case types$1.pipeline:
  8872. switch (this.getPluginOption("pipelineOperator", "proposal")) {
  8873. case "smart":
  8874. return this.withTopicPermittingContext(() => {
  8875. return this.parseSmartPipelineBody(this.parseExprOpBaseRightExpr(op, prec), startPos, startLoc);
  8876. });
  8877. case "fsharp":
  8878. return this.withSoloAwaitPermittingContext(() => {
  8879. return this.parseFSharpPipelineBody(prec);
  8880. });
  8881. }
  8882. default:
  8883. return this.parseExprOpBaseRightExpr(op, prec);
  8884. }
  8885. }
  8886. parseExprOpBaseRightExpr(op, prec) {
  8887. const startPos = this.state.start;
  8888. const startLoc = this.state.startLoc;
  8889. return this.parseExprOp(this.parseMaybeUnary(), startPos, startLoc, op.rightAssociative ? prec - 1 : prec);
  8890. }
  8891. checkExponentialAfterUnary(node) {
  8892. if (this.match(types$1.exponent)) {
  8893. this.raise(node.argument.start, ErrorMessages.UnexpectedTokenUnaryExponentiation);
  8894. }
  8895. }
  8896. parseMaybeUnary(refExpressionErrors, sawUnary) {
  8897. const startPos = this.state.start;
  8898. const startLoc = this.state.startLoc;
  8899. const isAwait = this.isContextual("await");
  8900. if (isAwait && this.isAwaitAllowed()) {
  8901. this.next();
  8902. const expr = this.parseAwait(startPos, startLoc);
  8903. if (!sawUnary) this.checkExponentialAfterUnary(expr);
  8904. return expr;
  8905. }
  8906. if (this.isContextual("module") && this.lookaheadCharCode() === 123 && !this.hasFollowingLineBreak()) {
  8907. return this.parseModuleExpression();
  8908. }
  8909. const update = this.match(types$1.incDec);
  8910. const node = this.startNode();
  8911. if (this.state.type.prefix) {
  8912. node.operator = this.state.value;
  8913. node.prefix = true;
  8914. if (this.match(types$1._throw)) {
  8915. this.expectPlugin("throwExpressions");
  8916. }
  8917. const isDelete = this.match(types$1._delete);
  8918. this.next();
  8919. node.argument = this.parseMaybeUnary(null, true);
  8920. this.checkExpressionErrors(refExpressionErrors, true);
  8921. if (this.state.strict && isDelete) {
  8922. const arg = node.argument;
  8923. if (arg.type === "Identifier") {
  8924. this.raise(node.start, ErrorMessages.StrictDelete);
  8925. } else if (this.hasPropertyAsPrivateName(arg)) {
  8926. this.raise(node.start, ErrorMessages.DeletePrivateField);
  8927. }
  8928. }
  8929. if (!update) {
  8930. if (!sawUnary) this.checkExponentialAfterUnary(node);
  8931. return this.finishNode(node, "UnaryExpression");
  8932. }
  8933. }
  8934. const expr = this.parseUpdate(node, update, refExpressionErrors);
  8935. if (isAwait) {
  8936. const startsExpr = this.hasPlugin("v8intrinsic") ? this.state.type.startsExpr : this.state.type.startsExpr && !this.match(types$1.modulo);
  8937. if (startsExpr && !this.isAmbiguousAwait()) {
  8938. this.raiseOverwrite(startPos, this.hasPlugin("topLevelAwait") ? ErrorMessages.AwaitNotInAsyncContext : ErrorMessages.AwaitNotInAsyncFunction);
  8939. return this.parseAwait(startPos, startLoc);
  8940. }
  8941. }
  8942. return expr;
  8943. }
  8944. parseUpdate(node, update, refExpressionErrors) {
  8945. if (update) {
  8946. this.checkLVal(node.argument, "prefix operation");
  8947. return this.finishNode(node, "UpdateExpression");
  8948. }
  8949. const startPos = this.state.start;
  8950. const startLoc = this.state.startLoc;
  8951. let expr = this.parseExprSubscripts(refExpressionErrors);
  8952. if (this.checkExpressionErrors(refExpressionErrors, false)) return expr;
  8953. while (this.state.type.postfix && !this.canInsertSemicolon()) {
  8954. const node = this.startNodeAt(startPos, startLoc);
  8955. node.operator = this.state.value;
  8956. node.prefix = false;
  8957. node.argument = expr;
  8958. this.checkLVal(expr, "postfix operation");
  8959. this.next();
  8960. expr = this.finishNode(node, "UpdateExpression");
  8961. }
  8962. return expr;
  8963. }
  8964. parseExprSubscripts(refExpressionErrors) {
  8965. const startPos = this.state.start;
  8966. const startLoc = this.state.startLoc;
  8967. const potentialArrowAt = this.state.potentialArrowAt;
  8968. const expr = this.parseExprAtom(refExpressionErrors);
  8969. if (this.shouldExitDescending(expr, potentialArrowAt)) {
  8970. return expr;
  8971. }
  8972. return this.parseSubscripts(expr, startPos, startLoc);
  8973. }
  8974. parseSubscripts(base, startPos, startLoc, noCalls) {
  8975. const state = {
  8976. optionalChainMember: false,
  8977. maybeAsyncArrow: this.atPossibleAsyncArrow(base),
  8978. stop: false
  8979. };
  8980. do {
  8981. base = this.parseSubscript(base, startPos, startLoc, noCalls, state);
  8982. state.maybeAsyncArrow = false;
  8983. } while (!state.stop);
  8984. return base;
  8985. }
  8986. parseSubscript(base, startPos, startLoc, noCalls, state) {
  8987. if (!noCalls && this.eat(types$1.doubleColon)) {
  8988. return this.parseBind(base, startPos, startLoc, noCalls, state);
  8989. } else if (this.match(types$1.backQuote)) {
  8990. return this.parseTaggedTemplateExpression(base, startPos, startLoc, state);
  8991. }
  8992. let optional = false;
  8993. if (this.match(types$1.questionDot)) {
  8994. if (noCalls && this.lookaheadCharCode() === 40) {
  8995. state.stop = true;
  8996. return base;
  8997. }
  8998. state.optionalChainMember = optional = true;
  8999. this.next();
  9000. }
  9001. if (!noCalls && this.match(types$1.parenL)) {
  9002. return this.parseCoverCallAndAsyncArrowHead(base, startPos, startLoc, state, optional);
  9003. } else if (optional || this.match(types$1.bracketL) || this.eat(types$1.dot)) {
  9004. return this.parseMember(base, startPos, startLoc, state, optional);
  9005. } else {
  9006. state.stop = true;
  9007. return base;
  9008. }
  9009. }
  9010. parseMember(base, startPos, startLoc, state, optional) {
  9011. const node = this.startNodeAt(startPos, startLoc);
  9012. const computed = this.eat(types$1.bracketL);
  9013. node.object = base;
  9014. node.computed = computed;
  9015. const privateName = !computed && this.match(types$1.privateName) && this.state.value;
  9016. const property = computed ? this.parseExpression() : privateName ? this.parsePrivateName() : this.parseIdentifier(true);
  9017. if (privateName !== false) {
  9018. if (node.object.type === "Super") {
  9019. this.raise(startPos, ErrorMessages.SuperPrivateField);
  9020. }
  9021. this.classScope.usePrivateName(privateName, property.start);
  9022. }
  9023. node.property = property;
  9024. if (computed) {
  9025. this.expect(types$1.bracketR);
  9026. }
  9027. if (state.optionalChainMember) {
  9028. node.optional = optional;
  9029. return this.finishNode(node, "OptionalMemberExpression");
  9030. } else {
  9031. return this.finishNode(node, "MemberExpression");
  9032. }
  9033. }
  9034. parseBind(base, startPos, startLoc, noCalls, state) {
  9035. const node = this.startNodeAt(startPos, startLoc);
  9036. node.object = base;
  9037. node.callee = this.parseNoCallExpr();
  9038. state.stop = true;
  9039. return this.parseSubscripts(this.finishNode(node, "BindExpression"), startPos, startLoc, noCalls);
  9040. }
  9041. parseCoverCallAndAsyncArrowHead(base, startPos, startLoc, state, optional) {
  9042. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  9043. this.state.maybeInArrowParameters = true;
  9044. this.next();
  9045. let node = this.startNodeAt(startPos, startLoc);
  9046. node.callee = base;
  9047. if (state.maybeAsyncArrow) {
  9048. this.expressionScope.enter(newAsyncArrowScope());
  9049. }
  9050. if (state.optionalChainMember) {
  9051. node.optional = optional;
  9052. }
  9053. if (optional) {
  9054. node.arguments = this.parseCallExpressionArguments(types$1.parenR, false);
  9055. } else {
  9056. node.arguments = this.parseCallExpressionArguments(types$1.parenR, state.maybeAsyncArrow, base.type === "Import", base.type !== "Super", node);
  9057. }
  9058. this.finishCallExpression(node, state.optionalChainMember);
  9059. if (state.maybeAsyncArrow && this.shouldParseAsyncArrow() && !optional) {
  9060. state.stop = true;
  9061. this.expressionScope.validateAsPattern();
  9062. this.expressionScope.exit();
  9063. node = this.parseAsyncArrowFromCallExpression(this.startNodeAt(startPos, startLoc), node);
  9064. } else {
  9065. if (state.maybeAsyncArrow) {
  9066. this.expressionScope.exit();
  9067. }
  9068. this.toReferencedArguments(node);
  9069. }
  9070. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  9071. return node;
  9072. }
  9073. toReferencedArguments(node, isParenthesizedExpr) {
  9074. this.toReferencedListDeep(node.arguments, isParenthesizedExpr);
  9075. }
  9076. parseTaggedTemplateExpression(base, startPos, startLoc, state) {
  9077. const node = this.startNodeAt(startPos, startLoc);
  9078. node.tag = base;
  9079. node.quasi = this.parseTemplate(true);
  9080. if (state.optionalChainMember) {
  9081. this.raise(startPos, ErrorMessages.OptionalChainingNoTemplate);
  9082. }
  9083. return this.finishNode(node, "TaggedTemplateExpression");
  9084. }
  9085. atPossibleAsyncArrow(base) {
  9086. return base.type === "Identifier" && base.name === "async" && this.state.lastTokEnd === base.end && !this.canInsertSemicolon() && base.end - base.start === 5 && base.start === this.state.potentialArrowAt;
  9087. }
  9088. finishCallExpression(node, optional) {
  9089. if (node.callee.type === "Import") {
  9090. if (node.arguments.length === 2) {
  9091. {
  9092. if (!this.hasPlugin("moduleAttributes")) {
  9093. this.expectPlugin("importAssertions");
  9094. }
  9095. }
  9096. }
  9097. if (node.arguments.length === 0 || node.arguments.length > 2) {
  9098. this.raise(node.start, ErrorMessages.ImportCallArity, this.hasPlugin("importAssertions") || this.hasPlugin("moduleAttributes") ? "one or two arguments" : "one argument");
  9099. } else {
  9100. for (const arg of node.arguments) {
  9101. if (arg.type === "SpreadElement") {
  9102. this.raise(arg.start, ErrorMessages.ImportCallSpreadArgument);
  9103. }
  9104. }
  9105. }
  9106. }
  9107. return this.finishNode(node, optional ? "OptionalCallExpression" : "CallExpression");
  9108. }
  9109. parseCallExpressionArguments(close, possibleAsyncArrow, dynamicImport, allowPlaceholder, nodeForExtra) {
  9110. const elts = [];
  9111. let first = true;
  9112. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  9113. this.state.inFSharpPipelineDirectBody = false;
  9114. while (!this.eat(close)) {
  9115. if (first) {
  9116. first = false;
  9117. } else {
  9118. this.expect(types$1.comma);
  9119. if (this.match(close)) {
  9120. if (dynamicImport && !this.hasPlugin("importAssertions") && !this.hasPlugin("moduleAttributes")) {
  9121. this.raise(this.state.lastTokStart, ErrorMessages.ImportCallArgumentTrailingComma);
  9122. }
  9123. if (nodeForExtra) {
  9124. this.addExtra(nodeForExtra, "trailingComma", this.state.lastTokStart);
  9125. }
  9126. this.next();
  9127. break;
  9128. }
  9129. }
  9130. elts.push(this.parseExprListItem(false, possibleAsyncArrow ? new ExpressionErrors() : undefined, possibleAsyncArrow ? {
  9131. start: 0
  9132. } : undefined, allowPlaceholder));
  9133. }
  9134. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  9135. return elts;
  9136. }
  9137. shouldParseAsyncArrow() {
  9138. return this.match(types$1.arrow) && !this.canInsertSemicolon();
  9139. }
  9140. parseAsyncArrowFromCallExpression(node, call) {
  9141. var _call$extra;
  9142. this.expect(types$1.arrow);
  9143. this.parseArrowExpression(node, call.arguments, true, (_call$extra = call.extra) == null ? void 0 : _call$extra.trailingComma);
  9144. return node;
  9145. }
  9146. parseNoCallExpr() {
  9147. const startPos = this.state.start;
  9148. const startLoc = this.state.startLoc;
  9149. return this.parseSubscripts(this.parseExprAtom(), startPos, startLoc, true);
  9150. }
  9151. parseExprAtom(refExpressionErrors) {
  9152. if (this.state.type === types$1.slash) this.readRegexp();
  9153. const canBeArrow = this.state.potentialArrowAt === this.state.start;
  9154. let node;
  9155. switch (this.state.type) {
  9156. case types$1._super:
  9157. return this.parseSuper();
  9158. case types$1._import:
  9159. node = this.startNode();
  9160. this.next();
  9161. if (this.match(types$1.dot)) {
  9162. return this.parseImportMetaProperty(node);
  9163. }
  9164. if (!this.match(types$1.parenL)) {
  9165. this.raise(this.state.lastTokStart, ErrorMessages.UnsupportedImport);
  9166. }
  9167. return this.finishNode(node, "Import");
  9168. case types$1._this:
  9169. node = this.startNode();
  9170. this.next();
  9171. return this.finishNode(node, "ThisExpression");
  9172. case types$1.name:
  9173. {
  9174. const containsEsc = this.state.containsEsc;
  9175. const id = this.parseIdentifier();
  9176. if (!containsEsc && id.name === "async" && !this.canInsertSemicolon()) {
  9177. if (this.match(types$1._function)) {
  9178. const last = this.state.context.length - 1;
  9179. if (this.state.context[last] !== types.functionStatement) {
  9180. throw new Error("Internal error");
  9181. }
  9182. this.state.context[last] = types.functionExpression;
  9183. this.next();
  9184. return this.parseFunction(this.startNodeAtNode(id), undefined, true);
  9185. } else if (this.match(types$1.name)) {
  9186. if (this.lookaheadCharCode() === 61) {
  9187. return this.parseAsyncArrowUnaryFunction(id);
  9188. } else {
  9189. return id;
  9190. }
  9191. } else if (this.match(types$1._do)) {
  9192. return this.parseDo(true);
  9193. }
  9194. }
  9195. if (canBeArrow && this.match(types$1.arrow) && !this.canInsertSemicolon()) {
  9196. this.next();
  9197. return this.parseArrowExpression(this.startNodeAtNode(id), [id], false);
  9198. }
  9199. return id;
  9200. }
  9201. case types$1._do:
  9202. {
  9203. return this.parseDo(false);
  9204. }
  9205. case types$1.regexp:
  9206. {
  9207. return this.parseRegExpLiteral(this.state.value);
  9208. }
  9209. case types$1.num:
  9210. return this.parseNumericLiteral(this.state.value);
  9211. case types$1.bigint:
  9212. return this.parseBigIntLiteral(this.state.value);
  9213. case types$1.decimal:
  9214. return this.parseDecimalLiteral(this.state.value);
  9215. case types$1.string:
  9216. return this.parseStringLiteral(this.state.value);
  9217. case types$1._null:
  9218. return this.parseNullLiteral();
  9219. case types$1._true:
  9220. return this.parseBooleanLiteral(true);
  9221. case types$1._false:
  9222. return this.parseBooleanLiteral(false);
  9223. case types$1.parenL:
  9224. return this.parseParenAndDistinguishExpression(canBeArrow);
  9225. case types$1.bracketBarL:
  9226. case types$1.bracketHashL:
  9227. {
  9228. return this.parseArrayLike(this.state.type === types$1.bracketBarL ? types$1.bracketBarR : types$1.bracketR, false, true, refExpressionErrors);
  9229. }
  9230. case types$1.bracketL:
  9231. {
  9232. return this.parseArrayLike(types$1.bracketR, true, false, refExpressionErrors);
  9233. }
  9234. case types$1.braceBarL:
  9235. case types$1.braceHashL:
  9236. {
  9237. return this.parseObjectLike(this.state.type === types$1.braceBarL ? types$1.braceBarR : types$1.braceR, false, true, refExpressionErrors);
  9238. }
  9239. case types$1.braceL:
  9240. {
  9241. return this.parseObjectLike(types$1.braceR, false, false, refExpressionErrors);
  9242. }
  9243. case types$1._function:
  9244. return this.parseFunctionOrFunctionSent();
  9245. case types$1.at:
  9246. this.parseDecorators();
  9247. case types$1._class:
  9248. node = this.startNode();
  9249. this.takeDecorators(node);
  9250. return this.parseClass(node, false);
  9251. case types$1._new:
  9252. return this.parseNewOrNewTarget();
  9253. case types$1.backQuote:
  9254. return this.parseTemplate(false);
  9255. case types$1.doubleColon:
  9256. {
  9257. node = this.startNode();
  9258. this.next();
  9259. node.object = null;
  9260. const callee = node.callee = this.parseNoCallExpr();
  9261. if (callee.type === "MemberExpression") {
  9262. return this.finishNode(node, "BindExpression");
  9263. } else {
  9264. throw this.raise(callee.start, ErrorMessages.UnsupportedBind);
  9265. }
  9266. }
  9267. case types$1.privateName:
  9268. {
  9269. const start = this.state.start;
  9270. const value = this.state.value;
  9271. node = this.parsePrivateName();
  9272. if (this.match(types$1._in)) {
  9273. this.expectPlugin("privateIn");
  9274. this.classScope.usePrivateName(value, node.start);
  9275. } else if (this.hasPlugin("privateIn")) {
  9276. this.raise(this.state.start, ErrorMessages.PrivateInExpectedIn, value);
  9277. } else {
  9278. throw this.unexpected(start);
  9279. }
  9280. return node;
  9281. }
  9282. case types$1.hash:
  9283. {
  9284. if (this.state.inPipeline) {
  9285. node = this.startNode();
  9286. if (this.getPluginOption("pipelineOperator", "proposal") !== "smart") {
  9287. this.raise(node.start, ErrorMessages.PrimaryTopicRequiresSmartPipeline);
  9288. }
  9289. this.next();
  9290. if (!this.primaryTopicReferenceIsAllowedInCurrentTopicContext()) {
  9291. this.raise(node.start, ErrorMessages.PrimaryTopicNotAllowed);
  9292. }
  9293. this.registerTopicReference();
  9294. return this.finishNode(node, "PipelinePrimaryTopicReference");
  9295. }
  9296. }
  9297. case types$1.relational:
  9298. {
  9299. if (this.state.value === "<") {
  9300. const lookaheadCh = this.input.codePointAt(this.nextTokenStart());
  9301. if (isIdentifierStart(lookaheadCh) || lookaheadCh === 62) {
  9302. this.expectOnePlugin(["jsx", "flow", "typescript"]);
  9303. }
  9304. }
  9305. }
  9306. default:
  9307. throw this.unexpected();
  9308. }
  9309. }
  9310. parseAsyncArrowUnaryFunction(id) {
  9311. const node = this.startNodeAtNode(id);
  9312. this.prodParam.enter(functionFlags(true, this.prodParam.hasYield));
  9313. const params = [this.parseIdentifier()];
  9314. this.prodParam.exit();
  9315. if (this.hasPrecedingLineBreak()) {
  9316. this.raise(this.state.pos, ErrorMessages.LineTerminatorBeforeArrow);
  9317. }
  9318. this.expect(types$1.arrow);
  9319. this.parseArrowExpression(node, params, true);
  9320. return node;
  9321. }
  9322. parseDo(isAsync) {
  9323. this.expectPlugin("doExpressions");
  9324. if (isAsync) {
  9325. this.expectPlugin("asyncDoExpressions");
  9326. }
  9327. const node = this.startNode();
  9328. node.async = isAsync;
  9329. this.next();
  9330. const oldLabels = this.state.labels;
  9331. this.state.labels = [];
  9332. if (isAsync) {
  9333. this.prodParam.enter(PARAM_AWAIT);
  9334. node.body = this.parseBlock();
  9335. this.prodParam.exit();
  9336. } else {
  9337. node.body = this.parseBlock();
  9338. }
  9339. this.state.labels = oldLabels;
  9340. return this.finishNode(node, "DoExpression");
  9341. }
  9342. parseSuper() {
  9343. const node = this.startNode();
  9344. this.next();
  9345. if (this.match(types$1.parenL) && !this.scope.allowDirectSuper && !this.options.allowSuperOutsideMethod) {
  9346. this.raise(node.start, ErrorMessages.SuperNotAllowed);
  9347. } else if (!this.scope.allowSuper && !this.options.allowSuperOutsideMethod) {
  9348. this.raise(node.start, ErrorMessages.UnexpectedSuper);
  9349. }
  9350. if (!this.match(types$1.parenL) && !this.match(types$1.bracketL) && !this.match(types$1.dot)) {
  9351. this.raise(node.start, ErrorMessages.UnsupportedSuper);
  9352. }
  9353. return this.finishNode(node, "Super");
  9354. }
  9355. parseMaybePrivateName(isPrivateNameAllowed) {
  9356. const isPrivate = this.match(types$1.privateName);
  9357. if (isPrivate) {
  9358. if (!isPrivateNameAllowed) {
  9359. this.raise(this.state.start + 1, ErrorMessages.UnexpectedPrivateField);
  9360. }
  9361. return this.parsePrivateName();
  9362. } else {
  9363. return this.parseIdentifier(true);
  9364. }
  9365. }
  9366. parsePrivateName() {
  9367. const node = this.startNode();
  9368. const id = this.startNodeAt(this.state.start + 1, new Position(this.state.curLine, this.state.start + 1 - this.state.lineStart));
  9369. const name = this.state.value;
  9370. this.next();
  9371. node.id = this.createIdentifier(id, name);
  9372. return this.finishNode(node, "PrivateName");
  9373. }
  9374. parseFunctionOrFunctionSent() {
  9375. const node = this.startNode();
  9376. this.next();
  9377. if (this.prodParam.hasYield && this.match(types$1.dot)) {
  9378. const meta = this.createIdentifier(this.startNodeAtNode(node), "function");
  9379. this.next();
  9380. return this.parseMetaProperty(node, meta, "sent");
  9381. }
  9382. return this.parseFunction(node);
  9383. }
  9384. parseMetaProperty(node, meta, propertyName) {
  9385. node.meta = meta;
  9386. if (meta.name === "function" && propertyName === "sent") {
  9387. if (this.isContextual(propertyName)) {
  9388. this.expectPlugin("functionSent");
  9389. } else if (!this.hasPlugin("functionSent")) {
  9390. this.unexpected();
  9391. }
  9392. }
  9393. const containsEsc = this.state.containsEsc;
  9394. node.property = this.parseIdentifier(true);
  9395. if (node.property.name !== propertyName || containsEsc) {
  9396. this.raise(node.property.start, ErrorMessages.UnsupportedMetaProperty, meta.name, propertyName);
  9397. }
  9398. return this.finishNode(node, "MetaProperty");
  9399. }
  9400. parseImportMetaProperty(node) {
  9401. const id = this.createIdentifier(this.startNodeAtNode(node), "import");
  9402. this.next();
  9403. if (this.isContextual("meta")) {
  9404. if (!this.inModule) {
  9405. this.raise(id.start, SourceTypeModuleErrorMessages.ImportMetaOutsideModule);
  9406. }
  9407. this.sawUnambiguousESM = true;
  9408. }
  9409. return this.parseMetaProperty(node, id, "meta");
  9410. }
  9411. parseLiteralAtNode(value, type, node) {
  9412. this.addExtra(node, "rawValue", value);
  9413. this.addExtra(node, "raw", this.input.slice(node.start, this.state.end));
  9414. node.value = value;
  9415. this.next();
  9416. return this.finishNode(node, type);
  9417. }
  9418. parseLiteral(value, type) {
  9419. const node = this.startNode();
  9420. return this.parseLiteralAtNode(value, type, node);
  9421. }
  9422. parseStringLiteral(value) {
  9423. return this.parseLiteral(value, "StringLiteral");
  9424. }
  9425. parseNumericLiteral(value) {
  9426. return this.parseLiteral(value, "NumericLiteral");
  9427. }
  9428. parseBigIntLiteral(value) {
  9429. return this.parseLiteral(value, "BigIntLiteral");
  9430. }
  9431. parseDecimalLiteral(value) {
  9432. return this.parseLiteral(value, "DecimalLiteral");
  9433. }
  9434. parseRegExpLiteral(value) {
  9435. const node = this.parseLiteral(value.value, "RegExpLiteral");
  9436. node.pattern = value.pattern;
  9437. node.flags = value.flags;
  9438. return node;
  9439. }
  9440. parseBooleanLiteral(value) {
  9441. const node = this.startNode();
  9442. node.value = value;
  9443. this.next();
  9444. return this.finishNode(node, "BooleanLiteral");
  9445. }
  9446. parseNullLiteral() {
  9447. const node = this.startNode();
  9448. this.next();
  9449. return this.finishNode(node, "NullLiteral");
  9450. }
  9451. parseParenAndDistinguishExpression(canBeArrow) {
  9452. const startPos = this.state.start;
  9453. const startLoc = this.state.startLoc;
  9454. let val;
  9455. this.next();
  9456. this.expressionScope.enter(newArrowHeadScope());
  9457. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  9458. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  9459. this.state.maybeInArrowParameters = true;
  9460. this.state.inFSharpPipelineDirectBody = false;
  9461. const innerStartPos = this.state.start;
  9462. const innerStartLoc = this.state.startLoc;
  9463. const exprList = [];
  9464. const refExpressionErrors = new ExpressionErrors();
  9465. const refNeedsArrowPos = {
  9466. start: 0
  9467. };
  9468. let first = true;
  9469. let spreadStart;
  9470. let optionalCommaStart;
  9471. while (!this.match(types$1.parenR)) {
  9472. if (first) {
  9473. first = false;
  9474. } else {
  9475. this.expect(types$1.comma, refNeedsArrowPos.start || null);
  9476. if (this.match(types$1.parenR)) {
  9477. optionalCommaStart = this.state.start;
  9478. break;
  9479. }
  9480. }
  9481. if (this.match(types$1.ellipsis)) {
  9482. const spreadNodeStartPos = this.state.start;
  9483. const spreadNodeStartLoc = this.state.startLoc;
  9484. spreadStart = this.state.start;
  9485. exprList.push(this.parseParenItem(this.parseRestBinding(), spreadNodeStartPos, spreadNodeStartLoc));
  9486. this.checkCommaAfterRest(41);
  9487. break;
  9488. } else {
  9489. exprList.push(this.parseMaybeAssignAllowIn(refExpressionErrors, this.parseParenItem, refNeedsArrowPos));
  9490. }
  9491. }
  9492. const innerEndPos = this.state.lastTokEnd;
  9493. const innerEndLoc = this.state.lastTokEndLoc;
  9494. this.expect(types$1.parenR);
  9495. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  9496. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  9497. let arrowNode = this.startNodeAt(startPos, startLoc);
  9498. if (canBeArrow && this.shouldParseArrow() && (arrowNode = this.parseArrow(arrowNode))) {
  9499. this.expressionScope.validateAsPattern();
  9500. this.expressionScope.exit();
  9501. this.parseArrowExpression(arrowNode, exprList, false);
  9502. return arrowNode;
  9503. }
  9504. this.expressionScope.exit();
  9505. if (!exprList.length) {
  9506. this.unexpected(this.state.lastTokStart);
  9507. }
  9508. if (optionalCommaStart) this.unexpected(optionalCommaStart);
  9509. if (spreadStart) this.unexpected(spreadStart);
  9510. this.checkExpressionErrors(refExpressionErrors, true);
  9511. if (refNeedsArrowPos.start) this.unexpected(refNeedsArrowPos.start);
  9512. this.toReferencedListDeep(exprList, true);
  9513. if (exprList.length > 1) {
  9514. val = this.startNodeAt(innerStartPos, innerStartLoc);
  9515. val.expressions = exprList;
  9516. this.finishNodeAt(val, "SequenceExpression", innerEndPos, innerEndLoc);
  9517. } else {
  9518. val = exprList[0];
  9519. }
  9520. if (!this.options.createParenthesizedExpressions) {
  9521. this.addExtra(val, "parenthesized", true);
  9522. this.addExtra(val, "parenStart", startPos);
  9523. return val;
  9524. }
  9525. const parenExpression = this.startNodeAt(startPos, startLoc);
  9526. parenExpression.expression = val;
  9527. this.finishNode(parenExpression, "ParenthesizedExpression");
  9528. return parenExpression;
  9529. }
  9530. shouldParseArrow() {
  9531. return !this.canInsertSemicolon();
  9532. }
  9533. parseArrow(node) {
  9534. if (this.eat(types$1.arrow)) {
  9535. return node;
  9536. }
  9537. }
  9538. parseParenItem(node, startPos, startLoc) {
  9539. return node;
  9540. }
  9541. parseNewOrNewTarget() {
  9542. const node = this.startNode();
  9543. this.next();
  9544. if (this.match(types$1.dot)) {
  9545. const meta = this.createIdentifier(this.startNodeAtNode(node), "new");
  9546. this.next();
  9547. const metaProp = this.parseMetaProperty(node, meta, "target");
  9548. if (!this.scope.inNonArrowFunction && !this.scope.inClass) {
  9549. this.raise(metaProp.start, ErrorMessages.UnexpectedNewTarget);
  9550. }
  9551. return metaProp;
  9552. }
  9553. return this.parseNew(node);
  9554. }
  9555. parseNew(node) {
  9556. node.callee = this.parseNoCallExpr();
  9557. if (node.callee.type === "Import") {
  9558. this.raise(node.callee.start, ErrorMessages.ImportCallNotNewExpression);
  9559. } else if (this.isOptionalChain(node.callee)) {
  9560. this.raise(this.state.lastTokEnd, ErrorMessages.OptionalChainingNoNew);
  9561. } else if (this.eat(types$1.questionDot)) {
  9562. this.raise(this.state.start, ErrorMessages.OptionalChainingNoNew);
  9563. }
  9564. this.parseNewArguments(node);
  9565. return this.finishNode(node, "NewExpression");
  9566. }
  9567. parseNewArguments(node) {
  9568. if (this.eat(types$1.parenL)) {
  9569. const args = this.parseExprList(types$1.parenR);
  9570. this.toReferencedList(args);
  9571. node.arguments = args;
  9572. } else {
  9573. node.arguments = [];
  9574. }
  9575. }
  9576. parseTemplateElement(isTagged) {
  9577. const elem = this.startNode();
  9578. if (this.state.value === null) {
  9579. if (!isTagged) {
  9580. this.raise(this.state.start + 1, ErrorMessages.InvalidEscapeSequenceTemplate);
  9581. }
  9582. }
  9583. elem.value = {
  9584. raw: this.input.slice(this.state.start, this.state.end).replace(/\r\n?/g, "\n"),
  9585. cooked: this.state.value
  9586. };
  9587. this.next();
  9588. elem.tail = this.match(types$1.backQuote);
  9589. return this.finishNode(elem, "TemplateElement");
  9590. }
  9591. parseTemplate(isTagged) {
  9592. const node = this.startNode();
  9593. this.next();
  9594. node.expressions = [];
  9595. let curElt = this.parseTemplateElement(isTagged);
  9596. node.quasis = [curElt];
  9597. while (!curElt.tail) {
  9598. this.expect(types$1.dollarBraceL);
  9599. node.expressions.push(this.parseTemplateSubstitution());
  9600. this.expect(types$1.braceR);
  9601. node.quasis.push(curElt = this.parseTemplateElement(isTagged));
  9602. }
  9603. this.next();
  9604. return this.finishNode(node, "TemplateLiteral");
  9605. }
  9606. parseTemplateSubstitution() {
  9607. return this.parseExpression();
  9608. }
  9609. parseObjectLike(close, isPattern, isRecord, refExpressionErrors) {
  9610. if (isRecord) {
  9611. this.expectPlugin("recordAndTuple");
  9612. }
  9613. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  9614. this.state.inFSharpPipelineDirectBody = false;
  9615. const propHash = Object.create(null);
  9616. let first = true;
  9617. const node = this.startNode();
  9618. node.properties = [];
  9619. this.next();
  9620. while (!this.match(close)) {
  9621. if (first) {
  9622. first = false;
  9623. } else {
  9624. this.expect(types$1.comma);
  9625. if (this.match(close)) {
  9626. this.addExtra(node, "trailingComma", this.state.lastTokStart);
  9627. break;
  9628. }
  9629. }
  9630. const prop = this.parsePropertyDefinition(isPattern, refExpressionErrors);
  9631. if (!isPattern) {
  9632. this.checkProto(prop, isRecord, propHash, refExpressionErrors);
  9633. }
  9634. if (isRecord && !this.isObjectProperty(prop) && prop.type !== "SpreadElement") {
  9635. this.raise(prop.start, ErrorMessages.InvalidRecordProperty);
  9636. }
  9637. if (prop.shorthand) {
  9638. this.addExtra(prop, "shorthand", true);
  9639. }
  9640. node.properties.push(prop);
  9641. }
  9642. this.state.exprAllowed = false;
  9643. this.next();
  9644. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  9645. let type = "ObjectExpression";
  9646. if (isPattern) {
  9647. type = "ObjectPattern";
  9648. } else if (isRecord) {
  9649. type = "RecordExpression";
  9650. }
  9651. return this.finishNode(node, type);
  9652. }
  9653. maybeAsyncOrAccessorProp(prop) {
  9654. return !prop.computed && prop.key.type === "Identifier" && (this.isLiteralPropertyName() || this.match(types$1.bracketL) || this.match(types$1.star));
  9655. }
  9656. parsePropertyDefinition(isPattern, refExpressionErrors) {
  9657. let decorators = [];
  9658. if (this.match(types$1.at)) {
  9659. if (this.hasPlugin("decorators")) {
  9660. this.raise(this.state.start, ErrorMessages.UnsupportedPropertyDecorator);
  9661. }
  9662. while (this.match(types$1.at)) {
  9663. decorators.push(this.parseDecorator());
  9664. }
  9665. }
  9666. const prop = this.startNode();
  9667. let isGenerator = false;
  9668. let isAsync = false;
  9669. let isAccessor = false;
  9670. let startPos;
  9671. let startLoc;
  9672. if (this.match(types$1.ellipsis)) {
  9673. if (decorators.length) this.unexpected();
  9674. if (isPattern) {
  9675. this.next();
  9676. prop.argument = this.parseIdentifier();
  9677. this.checkCommaAfterRest(125);
  9678. return this.finishNode(prop, "RestElement");
  9679. }
  9680. return this.parseSpread();
  9681. }
  9682. if (decorators.length) {
  9683. prop.decorators = decorators;
  9684. decorators = [];
  9685. }
  9686. prop.method = false;
  9687. if (isPattern || refExpressionErrors) {
  9688. startPos = this.state.start;
  9689. startLoc = this.state.startLoc;
  9690. }
  9691. if (!isPattern) {
  9692. isGenerator = this.eat(types$1.star);
  9693. }
  9694. const containsEsc = this.state.containsEsc;
  9695. const key = this.parsePropertyName(prop, false);
  9696. if (!isPattern && !isGenerator && !containsEsc && this.maybeAsyncOrAccessorProp(prop)) {
  9697. const keyName = key.name;
  9698. if (keyName === "async" && !this.hasPrecedingLineBreak()) {
  9699. isAsync = true;
  9700. isGenerator = this.eat(types$1.star);
  9701. this.parsePropertyName(prop, false);
  9702. }
  9703. if (keyName === "get" || keyName === "set") {
  9704. isAccessor = true;
  9705. prop.kind = keyName;
  9706. if (this.match(types$1.star)) {
  9707. isGenerator = true;
  9708. this.raise(this.state.pos, ErrorMessages.AccessorIsGenerator, keyName);
  9709. this.next();
  9710. }
  9711. this.parsePropertyName(prop, false);
  9712. }
  9713. }
  9714. this.parseObjPropValue(prop, startPos, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors);
  9715. return prop;
  9716. }
  9717. getGetterSetterExpectedParamCount(method) {
  9718. return method.kind === "get" ? 0 : 1;
  9719. }
  9720. getObjectOrClassMethodParams(method) {
  9721. return method.params;
  9722. }
  9723. checkGetterSetterParams(method) {
  9724. var _params;
  9725. const paramCount = this.getGetterSetterExpectedParamCount(method);
  9726. const params = this.getObjectOrClassMethodParams(method);
  9727. const start = method.start;
  9728. if (params.length !== paramCount) {
  9729. if (method.kind === "get") {
  9730. this.raise(start, ErrorMessages.BadGetterArity);
  9731. } else {
  9732. this.raise(start, ErrorMessages.BadSetterArity);
  9733. }
  9734. }
  9735. if (method.kind === "set" && ((_params = params[params.length - 1]) == null ? void 0 : _params.type) === "RestElement") {
  9736. this.raise(start, ErrorMessages.BadSetterRestParameter);
  9737. }
  9738. }
  9739. parseObjectMethod(prop, isGenerator, isAsync, isPattern, isAccessor) {
  9740. if (isAccessor) {
  9741. this.parseMethod(prop, isGenerator, false, false, false, "ObjectMethod");
  9742. this.checkGetterSetterParams(prop);
  9743. return prop;
  9744. }
  9745. if (isAsync || isGenerator || this.match(types$1.parenL)) {
  9746. if (isPattern) this.unexpected();
  9747. prop.kind = "method";
  9748. prop.method = true;
  9749. return this.parseMethod(prop, isGenerator, isAsync, false, false, "ObjectMethod");
  9750. }
  9751. }
  9752. parseObjectProperty(prop, startPos, startLoc, isPattern, refExpressionErrors) {
  9753. prop.shorthand = false;
  9754. if (this.eat(types$1.colon)) {
  9755. prop.value = isPattern ? this.parseMaybeDefault(this.state.start, this.state.startLoc) : this.parseMaybeAssignAllowIn(refExpressionErrors);
  9756. return this.finishNode(prop, "ObjectProperty");
  9757. }
  9758. if (!prop.computed && prop.key.type === "Identifier") {
  9759. this.checkReservedWord(prop.key.name, prop.key.start, true, false);
  9760. if (isPattern) {
  9761. prop.value = this.parseMaybeDefault(startPos, startLoc, prop.key.__clone());
  9762. } else if (this.match(types$1.eq) && refExpressionErrors) {
  9763. if (refExpressionErrors.shorthandAssign === -1) {
  9764. refExpressionErrors.shorthandAssign = this.state.start;
  9765. }
  9766. prop.value = this.parseMaybeDefault(startPos, startLoc, prop.key.__clone());
  9767. } else {
  9768. prop.value = prop.key.__clone();
  9769. }
  9770. prop.shorthand = true;
  9771. return this.finishNode(prop, "ObjectProperty");
  9772. }
  9773. }
  9774. parseObjPropValue(prop, startPos, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors) {
  9775. const node = this.parseObjectMethod(prop, isGenerator, isAsync, isPattern, isAccessor) || this.parseObjectProperty(prop, startPos, startLoc, isPattern, refExpressionErrors);
  9776. if (!node) this.unexpected();
  9777. return node;
  9778. }
  9779. parsePropertyName(prop, isPrivateNameAllowed) {
  9780. if (this.eat(types$1.bracketL)) {
  9781. prop.computed = true;
  9782. prop.key = this.parseMaybeAssignAllowIn();
  9783. this.expect(types$1.bracketR);
  9784. } else {
  9785. const oldInPropertyName = this.state.inPropertyName;
  9786. this.state.inPropertyName = true;
  9787. const type = this.state.type;
  9788. prop.key = type === types$1.num || type === types$1.string || type === types$1.bigint || type === types$1.decimal ? this.parseExprAtom() : this.parseMaybePrivateName(isPrivateNameAllowed);
  9789. if (type !== types$1.privateName) {
  9790. prop.computed = false;
  9791. }
  9792. this.state.inPropertyName = oldInPropertyName;
  9793. }
  9794. return prop.key;
  9795. }
  9796. initFunction(node, isAsync) {
  9797. node.id = null;
  9798. node.generator = false;
  9799. node.async = !!isAsync;
  9800. }
  9801. parseMethod(node, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope = false) {
  9802. this.initFunction(node, isAsync);
  9803. node.generator = !!isGenerator;
  9804. const allowModifiers = isConstructor;
  9805. this.scope.enter(SCOPE_FUNCTION | SCOPE_SUPER | (inClassScope ? SCOPE_CLASS : 0) | (allowDirectSuper ? SCOPE_DIRECT_SUPER : 0));
  9806. this.prodParam.enter(functionFlags(isAsync, node.generator));
  9807. this.parseFunctionParams(node, allowModifiers);
  9808. this.parseFunctionBodyAndFinish(node, type, true);
  9809. this.prodParam.exit();
  9810. this.scope.exit();
  9811. return node;
  9812. }
  9813. parseArrayLike(close, canBePattern, isTuple, refExpressionErrors) {
  9814. if (isTuple) {
  9815. this.expectPlugin("recordAndTuple");
  9816. }
  9817. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  9818. this.state.inFSharpPipelineDirectBody = false;
  9819. const node = this.startNode();
  9820. this.next();
  9821. node.elements = this.parseExprList(close, !isTuple, refExpressionErrors, node);
  9822. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  9823. return this.finishNode(node, isTuple ? "TupleExpression" : "ArrayExpression");
  9824. }
  9825. parseArrowExpression(node, params, isAsync, trailingCommaPos) {
  9826. this.scope.enter(SCOPE_FUNCTION | SCOPE_ARROW);
  9827. let flags = functionFlags(isAsync, false);
  9828. if (!this.match(types$1.bracketL) && this.prodParam.hasIn) {
  9829. flags |= PARAM_IN;
  9830. }
  9831. this.prodParam.enter(flags);
  9832. this.initFunction(node, isAsync);
  9833. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  9834. if (params) {
  9835. this.state.maybeInArrowParameters = true;
  9836. this.setArrowFunctionParameters(node, params, trailingCommaPos);
  9837. }
  9838. this.state.maybeInArrowParameters = false;
  9839. this.parseFunctionBody(node, true);
  9840. this.prodParam.exit();
  9841. this.scope.exit();
  9842. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  9843. return this.finishNode(node, "ArrowFunctionExpression");
  9844. }
  9845. setArrowFunctionParameters(node, params, trailingCommaPos) {
  9846. node.params = this.toAssignableList(params, trailingCommaPos, false);
  9847. }
  9848. parseFunctionBodyAndFinish(node, type, isMethod = false) {
  9849. this.parseFunctionBody(node, false, isMethod);
  9850. this.finishNode(node, type);
  9851. }
  9852. parseFunctionBody(node, allowExpression, isMethod = false) {
  9853. const isExpression = allowExpression && !this.match(types$1.braceL);
  9854. this.expressionScope.enter(newExpressionScope());
  9855. if (isExpression) {
  9856. node.body = this.parseMaybeAssign();
  9857. this.checkParams(node, false, allowExpression, false);
  9858. } else {
  9859. const oldStrict = this.state.strict;
  9860. const oldLabels = this.state.labels;
  9861. this.state.labels = [];
  9862. this.prodParam.enter(this.prodParam.currentFlags() | PARAM_RETURN);
  9863. node.body = this.parseBlock(true, false, hasStrictModeDirective => {
  9864. const nonSimple = !this.isSimpleParamList(node.params);
  9865. if (hasStrictModeDirective && nonSimple) {
  9866. const errorPos = (node.kind === "method" || node.kind === "constructor") && !!node.key ? node.key.end : node.start;
  9867. this.raise(errorPos, ErrorMessages.IllegalLanguageModeDirective);
  9868. }
  9869. const strictModeChanged = !oldStrict && this.state.strict;
  9870. this.checkParams(node, !this.state.strict && !allowExpression && !isMethod && !nonSimple, allowExpression, strictModeChanged);
  9871. if (this.state.strict && node.id) {
  9872. this.checkLVal(node.id, "function name", BIND_OUTSIDE, undefined, undefined, strictModeChanged);
  9873. }
  9874. });
  9875. this.prodParam.exit();
  9876. this.expressionScope.exit();
  9877. this.state.labels = oldLabels;
  9878. }
  9879. }
  9880. isSimpleParamList(params) {
  9881. for (let i = 0, len = params.length; i < len; i++) {
  9882. if (params[i].type !== "Identifier") return false;
  9883. }
  9884. return true;
  9885. }
  9886. checkParams(node, allowDuplicates, isArrowFunction, strictModeChanged = true) {
  9887. const checkClashes = new Set();
  9888. for (const param of node.params) {
  9889. this.checkLVal(param, "function parameter list", BIND_VAR, allowDuplicates ? null : checkClashes, undefined, strictModeChanged);
  9890. }
  9891. }
  9892. parseExprList(close, allowEmpty, refExpressionErrors, nodeForExtra) {
  9893. const elts = [];
  9894. let first = true;
  9895. while (!this.eat(close)) {
  9896. if (first) {
  9897. first = false;
  9898. } else {
  9899. this.expect(types$1.comma);
  9900. if (this.match(close)) {
  9901. if (nodeForExtra) {
  9902. this.addExtra(nodeForExtra, "trailingComma", this.state.lastTokStart);
  9903. }
  9904. this.next();
  9905. break;
  9906. }
  9907. }
  9908. elts.push(this.parseExprListItem(allowEmpty, refExpressionErrors));
  9909. }
  9910. return elts;
  9911. }
  9912. parseExprListItem(allowEmpty, refExpressionErrors, refNeedsArrowPos, allowPlaceholder) {
  9913. let elt;
  9914. if (this.match(types$1.comma)) {
  9915. if (!allowEmpty) {
  9916. this.raise(this.state.pos, ErrorMessages.UnexpectedToken, ",");
  9917. }
  9918. elt = null;
  9919. } else if (this.match(types$1.ellipsis)) {
  9920. const spreadNodeStartPos = this.state.start;
  9921. const spreadNodeStartLoc = this.state.startLoc;
  9922. elt = this.parseParenItem(this.parseSpread(refExpressionErrors, refNeedsArrowPos), spreadNodeStartPos, spreadNodeStartLoc);
  9923. } else if (this.match(types$1.question)) {
  9924. this.expectPlugin("partialApplication");
  9925. if (!allowPlaceholder) {
  9926. this.raise(this.state.start, ErrorMessages.UnexpectedArgumentPlaceholder);
  9927. }
  9928. const node = this.startNode();
  9929. this.next();
  9930. elt = this.finishNode(node, "ArgumentPlaceholder");
  9931. } else {
  9932. elt = this.parseMaybeAssignAllowIn(refExpressionErrors, this.parseParenItem, refNeedsArrowPos);
  9933. }
  9934. return elt;
  9935. }
  9936. parseIdentifier(liberal) {
  9937. const node = this.startNode();
  9938. const name = this.parseIdentifierName(node.start, liberal);
  9939. return this.createIdentifier(node, name);
  9940. }
  9941. createIdentifier(node, name) {
  9942. node.name = name;
  9943. node.loc.identifierName = name;
  9944. return this.finishNode(node, "Identifier");
  9945. }
  9946. parseIdentifierName(pos, liberal) {
  9947. let name;
  9948. const {
  9949. start,
  9950. type
  9951. } = this.state;
  9952. if (type === types$1.name) {
  9953. name = this.state.value;
  9954. } else if (type.keyword) {
  9955. name = type.keyword;
  9956. const curContext = this.curContext();
  9957. if ((type === types$1._class || type === types$1._function) && (curContext === types.functionStatement || curContext === types.functionExpression)) {
  9958. this.state.context.pop();
  9959. }
  9960. } else {
  9961. throw this.unexpected();
  9962. }
  9963. if (liberal) {
  9964. this.state.type = types$1.name;
  9965. } else {
  9966. this.checkReservedWord(name, start, !!type.keyword, false);
  9967. }
  9968. this.next();
  9969. return name;
  9970. }
  9971. checkReservedWord(word, startLoc, checkKeywords, isBinding) {
  9972. if (this.prodParam.hasYield && word === "yield") {
  9973. this.raise(startLoc, ErrorMessages.YieldBindingIdentifier);
  9974. return;
  9975. }
  9976. if (word === "await") {
  9977. if (this.prodParam.hasAwait) {
  9978. this.raise(startLoc, ErrorMessages.AwaitBindingIdentifier);
  9979. return;
  9980. } else if (this.scope.inStaticBlock && !this.scope.inNonArrowFunction) {
  9981. this.raise(startLoc, ErrorMessages.AwaitBindingIdentifierInStaticBlock);
  9982. return;
  9983. } else {
  9984. this.expressionScope.recordAsyncArrowParametersError(startLoc, ErrorMessages.AwaitBindingIdentifier);
  9985. }
  9986. }
  9987. if (this.scope.inClass && !this.scope.inNonArrowFunction && word === "arguments") {
  9988. this.raise(startLoc, ErrorMessages.ArgumentsInClass);
  9989. return;
  9990. }
  9991. if (checkKeywords && isKeyword(word)) {
  9992. this.raise(startLoc, ErrorMessages.UnexpectedKeyword, word);
  9993. return;
  9994. }
  9995. const reservedTest = !this.state.strict ? isReservedWord : isBinding ? isStrictBindReservedWord : isStrictReservedWord;
  9996. if (reservedTest(word, this.inModule)) {
  9997. this.raise(startLoc, ErrorMessages.UnexpectedReservedWord, word);
  9998. }
  9999. }
  10000. isAwaitAllowed() {
  10001. if (this.prodParam.hasAwait) return true;
  10002. if (this.options.allowAwaitOutsideFunction && !this.scope.inFunction) {
  10003. return true;
  10004. }
  10005. return false;
  10006. }
  10007. parseAwait(startPos, startLoc) {
  10008. const node = this.startNodeAt(startPos, startLoc);
  10009. this.expressionScope.recordParameterInitializerError(node.start, ErrorMessages.AwaitExpressionFormalParameter);
  10010. if (this.eat(types$1.star)) {
  10011. this.raise(node.start, ErrorMessages.ObsoleteAwaitStar);
  10012. }
  10013. if (!this.scope.inFunction && !this.options.allowAwaitOutsideFunction) {
  10014. if (this.isAmbiguousAwait()) {
  10015. this.ambiguousScriptDifferentAst = true;
  10016. } else {
  10017. this.sawUnambiguousESM = true;
  10018. }
  10019. }
  10020. if (!this.state.soloAwait) {
  10021. node.argument = this.parseMaybeUnary(null, true);
  10022. }
  10023. return this.finishNode(node, "AwaitExpression");
  10024. }
  10025. isAmbiguousAwait() {
  10026. 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);
  10027. }
  10028. parseYield() {
  10029. const node = this.startNode();
  10030. this.expressionScope.recordParameterInitializerError(node.start, ErrorMessages.YieldInParameter);
  10031. this.next();
  10032. if (this.match(types$1.semi) || !this.match(types$1.star) && !this.state.type.startsExpr || this.hasPrecedingLineBreak()) {
  10033. node.delegate = false;
  10034. node.argument = null;
  10035. } else {
  10036. node.delegate = this.eat(types$1.star);
  10037. node.argument = this.parseMaybeAssign();
  10038. }
  10039. return this.finishNode(node, "YieldExpression");
  10040. }
  10041. checkPipelineAtInfixOperator(left, leftStartPos) {
  10042. if (this.getPluginOption("pipelineOperator", "proposal") === "smart") {
  10043. if (left.type === "SequenceExpression") {
  10044. this.raise(leftStartPos, ErrorMessages.PipelineHeadSequenceExpression);
  10045. }
  10046. }
  10047. }
  10048. parseSmartPipelineBody(childExpression, startPos, startLoc) {
  10049. this.checkSmartPipelineBodyEarlyErrors(childExpression, startPos);
  10050. return this.parseSmartPipelineBodyInStyle(childExpression, startPos, startLoc);
  10051. }
  10052. checkSmartPipelineBodyEarlyErrors(childExpression, startPos) {
  10053. if (this.match(types$1.arrow)) {
  10054. throw this.raise(this.state.start, ErrorMessages.PipelineBodyNoArrow);
  10055. } else if (childExpression.type === "SequenceExpression") {
  10056. this.raise(startPos, ErrorMessages.PipelineBodySequenceExpression);
  10057. }
  10058. }
  10059. parseSmartPipelineBodyInStyle(childExpression, startPos, startLoc) {
  10060. const bodyNode = this.startNodeAt(startPos, startLoc);
  10061. const isSimpleReference = this.isSimpleReference(childExpression);
  10062. if (isSimpleReference) {
  10063. bodyNode.callee = childExpression;
  10064. } else {
  10065. if (!this.topicReferenceWasUsedInCurrentTopicContext()) {
  10066. this.raise(startPos, ErrorMessages.PipelineTopicUnused);
  10067. }
  10068. bodyNode.expression = childExpression;
  10069. }
  10070. return this.finishNode(bodyNode, isSimpleReference ? "PipelineBareFunction" : "PipelineTopicExpression");
  10071. }
  10072. isSimpleReference(expression) {
  10073. switch (expression.type) {
  10074. case "MemberExpression":
  10075. return !expression.computed && this.isSimpleReference(expression.object);
  10076. case "Identifier":
  10077. return true;
  10078. default:
  10079. return false;
  10080. }
  10081. }
  10082. withTopicPermittingContext(callback) {
  10083. const outerContextTopicState = this.state.topicContext;
  10084. this.state.topicContext = {
  10085. maxNumOfResolvableTopics: 1,
  10086. maxTopicIndex: null
  10087. };
  10088. try {
  10089. return callback();
  10090. } finally {
  10091. this.state.topicContext = outerContextTopicState;
  10092. }
  10093. }
  10094. withTopicForbiddingContext(callback) {
  10095. const outerContextTopicState = this.state.topicContext;
  10096. this.state.topicContext = {
  10097. maxNumOfResolvableTopics: 0,
  10098. maxTopicIndex: null
  10099. };
  10100. try {
  10101. return callback();
  10102. } finally {
  10103. this.state.topicContext = outerContextTopicState;
  10104. }
  10105. }
  10106. withSoloAwaitPermittingContext(callback) {
  10107. const outerContextSoloAwaitState = this.state.soloAwait;
  10108. this.state.soloAwait = true;
  10109. try {
  10110. return callback();
  10111. } finally {
  10112. this.state.soloAwait = outerContextSoloAwaitState;
  10113. }
  10114. }
  10115. allowInAnd(callback) {
  10116. const flags = this.prodParam.currentFlags();
  10117. const prodParamToSet = PARAM_IN & ~flags;
  10118. if (prodParamToSet) {
  10119. this.prodParam.enter(flags | PARAM_IN);
  10120. try {
  10121. return callback();
  10122. } finally {
  10123. this.prodParam.exit();
  10124. }
  10125. }
  10126. return callback();
  10127. }
  10128. disallowInAnd(callback) {
  10129. const flags = this.prodParam.currentFlags();
  10130. const prodParamToClear = PARAM_IN & flags;
  10131. if (prodParamToClear) {
  10132. this.prodParam.enter(flags & ~PARAM_IN);
  10133. try {
  10134. return callback();
  10135. } finally {
  10136. this.prodParam.exit();
  10137. }
  10138. }
  10139. return callback();
  10140. }
  10141. registerTopicReference() {
  10142. this.state.topicContext.maxTopicIndex = 0;
  10143. }
  10144. primaryTopicReferenceIsAllowedInCurrentTopicContext() {
  10145. return this.state.topicContext.maxNumOfResolvableTopics >= 1;
  10146. }
  10147. topicReferenceWasUsedInCurrentTopicContext() {
  10148. return this.state.topicContext.maxTopicIndex != null && this.state.topicContext.maxTopicIndex >= 0;
  10149. }
  10150. parseFSharpPipelineBody(prec) {
  10151. const startPos = this.state.start;
  10152. const startLoc = this.state.startLoc;
  10153. this.state.potentialArrowAt = this.state.start;
  10154. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  10155. this.state.inFSharpPipelineDirectBody = true;
  10156. const ret = this.parseExprOp(this.parseMaybeUnary(), startPos, startLoc, prec);
  10157. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  10158. return ret;
  10159. }
  10160. parseModuleExpression() {
  10161. this.expectPlugin("moduleBlocks");
  10162. const node = this.startNode();
  10163. this.next();
  10164. this.eat(types$1.braceL);
  10165. const revertScopes = this.initializeScopes(true);
  10166. this.enterInitialScopes();
  10167. const program = this.startNode();
  10168. try {
  10169. node.body = this.parseProgram(program, types$1.braceR, "module");
  10170. } finally {
  10171. revertScopes();
  10172. }
  10173. this.eat(types$1.braceR);
  10174. return this.finishNode(node, "ModuleExpression");
  10175. }
  10176. }
  10177. const loopLabel = {
  10178. kind: "loop"
  10179. },
  10180. switchLabel = {
  10181. kind: "switch"
  10182. };
  10183. const FUNC_NO_FLAGS = 0b000,
  10184. FUNC_STATEMENT = 0b001,
  10185. FUNC_HANGING_STATEMENT = 0b010,
  10186. FUNC_NULLABLE_ID = 0b100;
  10187. const loneSurrogate = /[\uD800-\uDFFF]/u;
  10188. const keywordRelationalOperator = /in(?:stanceof)?/y;
  10189. function babel7CompatTokens(tokens) {
  10190. {
  10191. for (let i = 0; i < tokens.length; i++) {
  10192. const token = tokens[i];
  10193. if (token.type === types$1.privateName) {
  10194. const {
  10195. loc,
  10196. start,
  10197. value,
  10198. end
  10199. } = token;
  10200. const hashEndPos = start + 1;
  10201. const hashEndLoc = new Position(loc.start.line, loc.start.column + 1);
  10202. tokens.splice(i, 1, new Token({
  10203. type: types$1.hash,
  10204. value: "#",
  10205. start: start,
  10206. end: hashEndPos,
  10207. startLoc: loc.start,
  10208. endLoc: hashEndLoc
  10209. }), new Token({
  10210. type: types$1.name,
  10211. value: value,
  10212. start: hashEndPos,
  10213. end: end,
  10214. startLoc: hashEndLoc,
  10215. endLoc: loc.end
  10216. }));
  10217. }
  10218. }
  10219. }
  10220. return tokens;
  10221. }
  10222. class StatementParser extends ExpressionParser {
  10223. parseTopLevel(file, program) {
  10224. file.program = this.parseProgram(program);
  10225. file.comments = this.state.comments;
  10226. if (this.options.tokens) file.tokens = babel7CompatTokens(this.tokens);
  10227. return this.finishNode(file, "File");
  10228. }
  10229. parseProgram(program, end = types$1.eof, sourceType = this.options.sourceType) {
  10230. program.sourceType = sourceType;
  10231. program.interpreter = this.parseInterpreterDirective();
  10232. this.parseBlockBody(program, true, true, end);
  10233. if (this.inModule && !this.options.allowUndeclaredExports && this.scope.undefinedExports.size > 0) {
  10234. for (const [name] of Array.from(this.scope.undefinedExports)) {
  10235. const pos = this.scope.undefinedExports.get(name);
  10236. this.raise(pos, ErrorMessages.ModuleExportUndefined, name);
  10237. }
  10238. }
  10239. return this.finishNode(program, "Program");
  10240. }
  10241. stmtToDirective(stmt) {
  10242. const expr = stmt.expression;
  10243. const directiveLiteral = this.startNodeAt(expr.start, expr.loc.start);
  10244. const directive = this.startNodeAt(stmt.start, stmt.loc.start);
  10245. const raw = this.input.slice(expr.start, expr.end);
  10246. const val = directiveLiteral.value = raw.slice(1, -1);
  10247. this.addExtra(directiveLiteral, "raw", raw);
  10248. this.addExtra(directiveLiteral, "rawValue", val);
  10249. directive.value = this.finishNodeAt(directiveLiteral, "DirectiveLiteral", expr.end, expr.loc.end);
  10250. return this.finishNodeAt(directive, "Directive", stmt.end, stmt.loc.end);
  10251. }
  10252. parseInterpreterDirective() {
  10253. if (!this.match(types$1.interpreterDirective)) {
  10254. return null;
  10255. }
  10256. const node = this.startNode();
  10257. node.value = this.state.value;
  10258. this.next();
  10259. return this.finishNode(node, "InterpreterDirective");
  10260. }
  10261. isLet(context) {
  10262. if (!this.isContextual("let")) {
  10263. return false;
  10264. }
  10265. return this.isLetKeyword(context);
  10266. }
  10267. isLetKeyword(context) {
  10268. const next = this.nextTokenStart();
  10269. const nextCh = this.codePointAtPos(next);
  10270. if (nextCh === 92 || nextCh === 91) {
  10271. return true;
  10272. }
  10273. if (context) return false;
  10274. if (nextCh === 123) return true;
  10275. if (isIdentifierStart(nextCh)) {
  10276. keywordRelationalOperator.lastIndex = next;
  10277. const matched = keywordRelationalOperator.exec(this.input);
  10278. if (matched !== null) {
  10279. const endCh = this.codePointAtPos(next + matched[0].length);
  10280. if (!isIdentifierChar(endCh) && endCh !== 92) {
  10281. return false;
  10282. }
  10283. }
  10284. return true;
  10285. }
  10286. return false;
  10287. }
  10288. parseStatement(context, topLevel) {
  10289. if (this.match(types$1.at)) {
  10290. this.parseDecorators(true);
  10291. }
  10292. return this.parseStatementContent(context, topLevel);
  10293. }
  10294. parseStatementContent(context, topLevel) {
  10295. let starttype = this.state.type;
  10296. const node = this.startNode();
  10297. let kind;
  10298. if (this.isLet(context)) {
  10299. starttype = types$1._var;
  10300. kind = "let";
  10301. }
  10302. switch (starttype) {
  10303. case types$1._break:
  10304. case types$1._continue:
  10305. return this.parseBreakContinueStatement(node, starttype.keyword);
  10306. case types$1._debugger:
  10307. return this.parseDebuggerStatement(node);
  10308. case types$1._do:
  10309. return this.parseDoStatement(node);
  10310. case types$1._for:
  10311. return this.parseForStatement(node);
  10312. case types$1._function:
  10313. if (this.lookaheadCharCode() === 46) break;
  10314. if (context) {
  10315. if (this.state.strict) {
  10316. this.raise(this.state.start, ErrorMessages.StrictFunction);
  10317. } else if (context !== "if" && context !== "label") {
  10318. this.raise(this.state.start, ErrorMessages.SloppyFunction);
  10319. }
  10320. }
  10321. return this.parseFunctionStatement(node, false, !context);
  10322. case types$1._class:
  10323. if (context) this.unexpected();
  10324. return this.parseClass(node, true);
  10325. case types$1._if:
  10326. return this.parseIfStatement(node);
  10327. case types$1._return:
  10328. return this.parseReturnStatement(node);
  10329. case types$1._switch:
  10330. return this.parseSwitchStatement(node);
  10331. case types$1._throw:
  10332. return this.parseThrowStatement(node);
  10333. case types$1._try:
  10334. return this.parseTryStatement(node);
  10335. case types$1._const:
  10336. case types$1._var:
  10337. kind = kind || this.state.value;
  10338. if (context && kind !== "var") {
  10339. this.raise(this.state.start, ErrorMessages.UnexpectedLexicalDeclaration);
  10340. }
  10341. return this.parseVarStatement(node, kind);
  10342. case types$1._while:
  10343. return this.parseWhileStatement(node);
  10344. case types$1._with:
  10345. return this.parseWithStatement(node);
  10346. case types$1.braceL:
  10347. return this.parseBlock();
  10348. case types$1.semi:
  10349. return this.parseEmptyStatement(node);
  10350. case types$1._import:
  10351. {
  10352. const nextTokenCharCode = this.lookaheadCharCode();
  10353. if (nextTokenCharCode === 40 || nextTokenCharCode === 46) {
  10354. break;
  10355. }
  10356. }
  10357. case types$1._export:
  10358. {
  10359. if (!this.options.allowImportExportEverywhere && !topLevel) {
  10360. this.raise(this.state.start, ErrorMessages.UnexpectedImportExport);
  10361. }
  10362. this.next();
  10363. let result;
  10364. if (starttype === types$1._import) {
  10365. result = this.parseImport(node);
  10366. if (result.type === "ImportDeclaration" && (!result.importKind || result.importKind === "value")) {
  10367. this.sawUnambiguousESM = true;
  10368. }
  10369. } else {
  10370. result = this.parseExport(node);
  10371. if (result.type === "ExportNamedDeclaration" && (!result.exportKind || result.exportKind === "value") || result.type === "ExportAllDeclaration" && (!result.exportKind || result.exportKind === "value") || result.type === "ExportDefaultDeclaration") {
  10372. this.sawUnambiguousESM = true;
  10373. }
  10374. }
  10375. this.assertModuleNodeAllowed(node);
  10376. return result;
  10377. }
  10378. default:
  10379. {
  10380. if (this.isAsyncFunction()) {
  10381. if (context) {
  10382. this.raise(this.state.start, ErrorMessages.AsyncFunctionInSingleStatementContext);
  10383. }
  10384. this.next();
  10385. return this.parseFunctionStatement(node, true, !context);
  10386. }
  10387. }
  10388. }
  10389. const maybeName = this.state.value;
  10390. const expr = this.parseExpression();
  10391. if (starttype === types$1.name && expr.type === "Identifier" && this.eat(types$1.colon)) {
  10392. return this.parseLabeledStatement(node, maybeName, expr, context);
  10393. } else {
  10394. return this.parseExpressionStatement(node, expr);
  10395. }
  10396. }
  10397. assertModuleNodeAllowed(node) {
  10398. if (!this.options.allowImportExportEverywhere && !this.inModule) {
  10399. this.raise(node.start, SourceTypeModuleErrorMessages.ImportOutsideModule);
  10400. }
  10401. }
  10402. takeDecorators(node) {
  10403. const decorators = this.state.decoratorStack[this.state.decoratorStack.length - 1];
  10404. if (decorators.length) {
  10405. node.decorators = decorators;
  10406. this.resetStartLocationFromNode(node, decorators[0]);
  10407. this.state.decoratorStack[this.state.decoratorStack.length - 1] = [];
  10408. }
  10409. }
  10410. canHaveLeadingDecorator() {
  10411. return this.match(types$1._class);
  10412. }
  10413. parseDecorators(allowExport) {
  10414. const currentContextDecorators = this.state.decoratorStack[this.state.decoratorStack.length - 1];
  10415. while (this.match(types$1.at)) {
  10416. const decorator = this.parseDecorator();
  10417. currentContextDecorators.push(decorator);
  10418. }
  10419. if (this.match(types$1._export)) {
  10420. if (!allowExport) {
  10421. this.unexpected();
  10422. }
  10423. if (this.hasPlugin("decorators") && !this.getPluginOption("decorators", "decoratorsBeforeExport")) {
  10424. this.raise(this.state.start, ErrorMessages.DecoratorExportClass);
  10425. }
  10426. } else if (!this.canHaveLeadingDecorator()) {
  10427. throw this.raise(this.state.start, ErrorMessages.UnexpectedLeadingDecorator);
  10428. }
  10429. }
  10430. parseDecorator() {
  10431. this.expectOnePlugin(["decorators-legacy", "decorators"]);
  10432. const node = this.startNode();
  10433. this.next();
  10434. if (this.hasPlugin("decorators")) {
  10435. this.state.decoratorStack.push([]);
  10436. const startPos = this.state.start;
  10437. const startLoc = this.state.startLoc;
  10438. let expr;
  10439. if (this.eat(types$1.parenL)) {
  10440. expr = this.parseExpression();
  10441. this.expect(types$1.parenR);
  10442. } else {
  10443. expr = this.parseIdentifier(false);
  10444. while (this.eat(types$1.dot)) {
  10445. const node = this.startNodeAt(startPos, startLoc);
  10446. node.object = expr;
  10447. node.property = this.parseIdentifier(true);
  10448. node.computed = false;
  10449. expr = this.finishNode(node, "MemberExpression");
  10450. }
  10451. }
  10452. node.expression = this.parseMaybeDecoratorArguments(expr);
  10453. this.state.decoratorStack.pop();
  10454. } else {
  10455. node.expression = this.parseExprSubscripts();
  10456. }
  10457. return this.finishNode(node, "Decorator");
  10458. }
  10459. parseMaybeDecoratorArguments(expr) {
  10460. if (this.eat(types$1.parenL)) {
  10461. const node = this.startNodeAtNode(expr);
  10462. node.callee = expr;
  10463. node.arguments = this.parseCallExpressionArguments(types$1.parenR, false);
  10464. this.toReferencedList(node.arguments);
  10465. return this.finishNode(node, "CallExpression");
  10466. }
  10467. return expr;
  10468. }
  10469. parseBreakContinueStatement(node, keyword) {
  10470. const isBreak = keyword === "break";
  10471. this.next();
  10472. if (this.isLineTerminator()) {
  10473. node.label = null;
  10474. } else {
  10475. node.label = this.parseIdentifier();
  10476. this.semicolon();
  10477. }
  10478. this.verifyBreakContinue(node, keyword);
  10479. return this.finishNode(node, isBreak ? "BreakStatement" : "ContinueStatement");
  10480. }
  10481. verifyBreakContinue(node, keyword) {
  10482. const isBreak = keyword === "break";
  10483. let i;
  10484. for (i = 0; i < this.state.labels.length; ++i) {
  10485. const lab = this.state.labels[i];
  10486. if (node.label == null || lab.name === node.label.name) {
  10487. if (lab.kind != null && (isBreak || lab.kind === "loop")) break;
  10488. if (node.label && isBreak) break;
  10489. }
  10490. }
  10491. if (i === this.state.labels.length) {
  10492. this.raise(node.start, ErrorMessages.IllegalBreakContinue, keyword);
  10493. }
  10494. }
  10495. parseDebuggerStatement(node) {
  10496. this.next();
  10497. this.semicolon();
  10498. return this.finishNode(node, "DebuggerStatement");
  10499. }
  10500. parseHeaderExpression() {
  10501. this.expect(types$1.parenL);
  10502. const val = this.parseExpression();
  10503. this.expect(types$1.parenR);
  10504. return val;
  10505. }
  10506. parseDoStatement(node) {
  10507. this.next();
  10508. this.state.labels.push(loopLabel);
  10509. node.body = this.withTopicForbiddingContext(() => this.parseStatement("do"));
  10510. this.state.labels.pop();
  10511. this.expect(types$1._while);
  10512. node.test = this.parseHeaderExpression();
  10513. this.eat(types$1.semi);
  10514. return this.finishNode(node, "DoWhileStatement");
  10515. }
  10516. parseForStatement(node) {
  10517. this.next();
  10518. this.state.labels.push(loopLabel);
  10519. let awaitAt = -1;
  10520. if (this.isAwaitAllowed() && this.eatContextual("await")) {
  10521. awaitAt = this.state.lastTokStart;
  10522. }
  10523. this.scope.enter(SCOPE_OTHER);
  10524. this.expect(types$1.parenL);
  10525. if (this.match(types$1.semi)) {
  10526. if (awaitAt > -1) {
  10527. this.unexpected(awaitAt);
  10528. }
  10529. return this.parseFor(node, null);
  10530. }
  10531. const startsWithLet = this.isContextual("let");
  10532. const isLet = startsWithLet && this.isLetKeyword();
  10533. if (this.match(types$1._var) || this.match(types$1._const) || isLet) {
  10534. const init = this.startNode();
  10535. const kind = isLet ? "let" : this.state.value;
  10536. this.next();
  10537. this.parseVar(init, true, kind);
  10538. this.finishNode(init, "VariableDeclaration");
  10539. if ((this.match(types$1._in) || this.isContextual("of")) && init.declarations.length === 1) {
  10540. return this.parseForIn(node, init, awaitAt);
  10541. }
  10542. if (awaitAt > -1) {
  10543. this.unexpected(awaitAt);
  10544. }
  10545. return this.parseFor(node, init);
  10546. }
  10547. const startsWithUnescapedName = this.match(types$1.name) && !this.state.containsEsc;
  10548. const refExpressionErrors = new ExpressionErrors();
  10549. const init = this.parseExpression(true, refExpressionErrors);
  10550. const isForOf = this.isContextual("of");
  10551. if (isForOf) {
  10552. if (startsWithLet) {
  10553. this.raise(init.start, ErrorMessages.ForOfLet);
  10554. } else if (awaitAt === -1 && startsWithUnescapedName && init.type === "Identifier" && init.name === "async") {
  10555. this.raise(init.start, ErrorMessages.ForOfAsync);
  10556. }
  10557. }
  10558. if (isForOf || this.match(types$1._in)) {
  10559. this.toAssignable(init, true);
  10560. const description = isForOf ? "for-of statement" : "for-in statement";
  10561. this.checkLVal(init, description);
  10562. return this.parseForIn(node, init, awaitAt);
  10563. } else {
  10564. this.checkExpressionErrors(refExpressionErrors, true);
  10565. }
  10566. if (awaitAt > -1) {
  10567. this.unexpected(awaitAt);
  10568. }
  10569. return this.parseFor(node, init);
  10570. }
  10571. parseFunctionStatement(node, isAsync, declarationPosition) {
  10572. this.next();
  10573. return this.parseFunction(node, FUNC_STATEMENT | (declarationPosition ? 0 : FUNC_HANGING_STATEMENT), isAsync);
  10574. }
  10575. parseIfStatement(node) {
  10576. this.next();
  10577. node.test = this.parseHeaderExpression();
  10578. node.consequent = this.parseStatement("if");
  10579. node.alternate = this.eat(types$1._else) ? this.parseStatement("if") : null;
  10580. return this.finishNode(node, "IfStatement");
  10581. }
  10582. parseReturnStatement(node) {
  10583. if (!this.prodParam.hasReturn && !this.options.allowReturnOutsideFunction) {
  10584. this.raise(this.state.start, ErrorMessages.IllegalReturn);
  10585. }
  10586. this.next();
  10587. if (this.isLineTerminator()) {
  10588. node.argument = null;
  10589. } else {
  10590. node.argument = this.parseExpression();
  10591. this.semicolon();
  10592. }
  10593. return this.finishNode(node, "ReturnStatement");
  10594. }
  10595. parseSwitchStatement(node) {
  10596. this.next();
  10597. node.discriminant = this.parseHeaderExpression();
  10598. const cases = node.cases = [];
  10599. this.expect(types$1.braceL);
  10600. this.state.labels.push(switchLabel);
  10601. this.scope.enter(SCOPE_OTHER);
  10602. let cur;
  10603. for (let sawDefault; !this.match(types$1.braceR);) {
  10604. if (this.match(types$1._case) || this.match(types$1._default)) {
  10605. const isCase = this.match(types$1._case);
  10606. if (cur) this.finishNode(cur, "SwitchCase");
  10607. cases.push(cur = this.startNode());
  10608. cur.consequent = [];
  10609. this.next();
  10610. if (isCase) {
  10611. cur.test = this.parseExpression();
  10612. } else {
  10613. if (sawDefault) {
  10614. this.raise(this.state.lastTokStart, ErrorMessages.MultipleDefaultsInSwitch);
  10615. }
  10616. sawDefault = true;
  10617. cur.test = null;
  10618. }
  10619. this.expect(types$1.colon);
  10620. } else {
  10621. if (cur) {
  10622. cur.consequent.push(this.parseStatement(null));
  10623. } else {
  10624. this.unexpected();
  10625. }
  10626. }
  10627. }
  10628. this.scope.exit();
  10629. if (cur) this.finishNode(cur, "SwitchCase");
  10630. this.next();
  10631. this.state.labels.pop();
  10632. return this.finishNode(node, "SwitchStatement");
  10633. }
  10634. parseThrowStatement(node) {
  10635. this.next();
  10636. if (this.hasPrecedingLineBreak()) {
  10637. this.raise(this.state.lastTokEnd, ErrorMessages.NewlineAfterThrow);
  10638. }
  10639. node.argument = this.parseExpression();
  10640. this.semicolon();
  10641. return this.finishNode(node, "ThrowStatement");
  10642. }
  10643. parseCatchClauseParam() {
  10644. const param = this.parseBindingAtom();
  10645. const simple = param.type === "Identifier";
  10646. this.scope.enter(simple ? SCOPE_SIMPLE_CATCH : 0);
  10647. this.checkLVal(param, "catch clause", BIND_LEXICAL);
  10648. return param;
  10649. }
  10650. parseTryStatement(node) {
  10651. this.next();
  10652. node.block = this.parseBlock();
  10653. node.handler = null;
  10654. if (this.match(types$1._catch)) {
  10655. const clause = this.startNode();
  10656. this.next();
  10657. if (this.match(types$1.parenL)) {
  10658. this.expect(types$1.parenL);
  10659. clause.param = this.parseCatchClauseParam();
  10660. this.expect(types$1.parenR);
  10661. } else {
  10662. clause.param = null;
  10663. this.scope.enter(SCOPE_OTHER);
  10664. }
  10665. clause.body = this.withTopicForbiddingContext(() => this.parseBlock(false, false));
  10666. this.scope.exit();
  10667. node.handler = this.finishNode(clause, "CatchClause");
  10668. }
  10669. node.finalizer = this.eat(types$1._finally) ? this.parseBlock() : null;
  10670. if (!node.handler && !node.finalizer) {
  10671. this.raise(node.start, ErrorMessages.NoCatchOrFinally);
  10672. }
  10673. return this.finishNode(node, "TryStatement");
  10674. }
  10675. parseVarStatement(node, kind) {
  10676. this.next();
  10677. this.parseVar(node, false, kind);
  10678. this.semicolon();
  10679. return this.finishNode(node, "VariableDeclaration");
  10680. }
  10681. parseWhileStatement(node) {
  10682. this.next();
  10683. node.test = this.parseHeaderExpression();
  10684. this.state.labels.push(loopLabel);
  10685. node.body = this.withTopicForbiddingContext(() => this.parseStatement("while"));
  10686. this.state.labels.pop();
  10687. return this.finishNode(node, "WhileStatement");
  10688. }
  10689. parseWithStatement(node) {
  10690. if (this.state.strict) {
  10691. this.raise(this.state.start, ErrorMessages.StrictWith);
  10692. }
  10693. this.next();
  10694. node.object = this.parseHeaderExpression();
  10695. node.body = this.withTopicForbiddingContext(() => this.parseStatement("with"));
  10696. return this.finishNode(node, "WithStatement");
  10697. }
  10698. parseEmptyStatement(node) {
  10699. this.next();
  10700. return this.finishNode(node, "EmptyStatement");
  10701. }
  10702. parseLabeledStatement(node, maybeName, expr, context) {
  10703. for (const label of this.state.labels) {
  10704. if (label.name === maybeName) {
  10705. this.raise(expr.start, ErrorMessages.LabelRedeclaration, maybeName);
  10706. }
  10707. }
  10708. const kind = this.state.type.isLoop ? "loop" : this.match(types$1._switch) ? "switch" : null;
  10709. for (let i = this.state.labels.length - 1; i >= 0; i--) {
  10710. const label = this.state.labels[i];
  10711. if (label.statementStart === node.start) {
  10712. label.statementStart = this.state.start;
  10713. label.kind = kind;
  10714. } else {
  10715. break;
  10716. }
  10717. }
  10718. this.state.labels.push({
  10719. name: maybeName,
  10720. kind: kind,
  10721. statementStart: this.state.start
  10722. });
  10723. node.body = this.parseStatement(context ? context.indexOf("label") === -1 ? context + "label" : context : "label");
  10724. this.state.labels.pop();
  10725. node.label = expr;
  10726. return this.finishNode(node, "LabeledStatement");
  10727. }
  10728. parseExpressionStatement(node, expr) {
  10729. node.expression = expr;
  10730. this.semicolon();
  10731. return this.finishNode(node, "ExpressionStatement");
  10732. }
  10733. parseBlock(allowDirectives = false, createNewLexicalScope = true, afterBlockParse) {
  10734. const node = this.startNode();
  10735. if (allowDirectives) {
  10736. this.state.strictErrors.clear();
  10737. }
  10738. this.expect(types$1.braceL);
  10739. if (createNewLexicalScope) {
  10740. this.scope.enter(SCOPE_OTHER);
  10741. }
  10742. this.parseBlockBody(node, allowDirectives, false, types$1.braceR, afterBlockParse);
  10743. if (createNewLexicalScope) {
  10744. this.scope.exit();
  10745. }
  10746. return this.finishNode(node, "BlockStatement");
  10747. }
  10748. isValidDirective(stmt) {
  10749. return stmt.type === "ExpressionStatement" && stmt.expression.type === "StringLiteral" && !stmt.expression.extra.parenthesized;
  10750. }
  10751. parseBlockBody(node, allowDirectives, topLevel, end, afterBlockParse) {
  10752. const body = node.body = [];
  10753. const directives = node.directives = [];
  10754. this.parseBlockOrModuleBlockBody(body, allowDirectives ? directives : undefined, topLevel, end, afterBlockParse);
  10755. }
  10756. parseBlockOrModuleBlockBody(body, directives, topLevel, end, afterBlockParse) {
  10757. const oldStrict = this.state.strict;
  10758. let hasStrictModeDirective = false;
  10759. let parsedNonDirective = false;
  10760. while (!this.match(end)) {
  10761. const stmt = this.parseStatement(null, topLevel);
  10762. if (directives && !parsedNonDirective) {
  10763. if (this.isValidDirective(stmt)) {
  10764. const directive = this.stmtToDirective(stmt);
  10765. directives.push(directive);
  10766. if (!hasStrictModeDirective && directive.value.value === "use strict") {
  10767. hasStrictModeDirective = true;
  10768. this.setStrict(true);
  10769. }
  10770. continue;
  10771. }
  10772. parsedNonDirective = true;
  10773. this.state.strictErrors.clear();
  10774. }
  10775. body.push(stmt);
  10776. }
  10777. if (afterBlockParse) {
  10778. afterBlockParse.call(this, hasStrictModeDirective);
  10779. }
  10780. if (!oldStrict) {
  10781. this.setStrict(false);
  10782. }
  10783. this.next();
  10784. }
  10785. parseFor(node, init) {
  10786. node.init = init;
  10787. this.semicolon(false);
  10788. node.test = this.match(types$1.semi) ? null : this.parseExpression();
  10789. this.semicolon(false);
  10790. node.update = this.match(types$1.parenR) ? null : this.parseExpression();
  10791. this.expect(types$1.parenR);
  10792. node.body = this.withTopicForbiddingContext(() => this.parseStatement("for"));
  10793. this.scope.exit();
  10794. this.state.labels.pop();
  10795. return this.finishNode(node, "ForStatement");
  10796. }
  10797. parseForIn(node, init, awaitAt) {
  10798. const isForIn = this.match(types$1._in);
  10799. this.next();
  10800. if (isForIn) {
  10801. if (awaitAt > -1) this.unexpected(awaitAt);
  10802. } else {
  10803. node.await = awaitAt > -1;
  10804. }
  10805. if (init.type === "VariableDeclaration" && init.declarations[0].init != null && (!isForIn || this.state.strict || init.kind !== "var" || init.declarations[0].id.type !== "Identifier")) {
  10806. this.raise(init.start, ErrorMessages.ForInOfLoopInitializer, isForIn ? "for-in" : "for-of");
  10807. } else if (init.type === "AssignmentPattern") {
  10808. this.raise(init.start, ErrorMessages.InvalidLhs, "for-loop");
  10809. }
  10810. node.left = init;
  10811. node.right = isForIn ? this.parseExpression() : this.parseMaybeAssignAllowIn();
  10812. this.expect(types$1.parenR);
  10813. node.body = this.withTopicForbiddingContext(() => this.parseStatement("for"));
  10814. this.scope.exit();
  10815. this.state.labels.pop();
  10816. return this.finishNode(node, isForIn ? "ForInStatement" : "ForOfStatement");
  10817. }
  10818. parseVar(node, isFor, kind) {
  10819. const declarations = node.declarations = [];
  10820. const isTypescript = this.hasPlugin("typescript");
  10821. node.kind = kind;
  10822. for (;;) {
  10823. const decl = this.startNode();
  10824. this.parseVarId(decl, kind);
  10825. if (this.eat(types$1.eq)) {
  10826. decl.init = isFor ? this.parseMaybeAssignDisallowIn() : this.parseMaybeAssignAllowIn();
  10827. } else {
  10828. if (kind === "const" && !(this.match(types$1._in) || this.isContextual("of"))) {
  10829. if (!isTypescript) {
  10830. this.raise(this.state.lastTokEnd, ErrorMessages.DeclarationMissingInitializer, "Const declarations");
  10831. }
  10832. } else if (decl.id.type !== "Identifier" && !(isFor && (this.match(types$1._in) || this.isContextual("of")))) {
  10833. this.raise(this.state.lastTokEnd, ErrorMessages.DeclarationMissingInitializer, "Complex binding patterns");
  10834. }
  10835. decl.init = null;
  10836. }
  10837. declarations.push(this.finishNode(decl, "VariableDeclarator"));
  10838. if (!this.eat(types$1.comma)) break;
  10839. }
  10840. return node;
  10841. }
  10842. parseVarId(decl, kind) {
  10843. decl.id = this.parseBindingAtom();
  10844. this.checkLVal(decl.id, "variable declaration", kind === "var" ? BIND_VAR : BIND_LEXICAL, undefined, kind !== "var");
  10845. }
  10846. parseFunction(node, statement = FUNC_NO_FLAGS, isAsync = false) {
  10847. const isStatement = statement & FUNC_STATEMENT;
  10848. const isHangingStatement = statement & FUNC_HANGING_STATEMENT;
  10849. const requireId = !!isStatement && !(statement & FUNC_NULLABLE_ID);
  10850. this.initFunction(node, isAsync);
  10851. if (this.match(types$1.star) && isHangingStatement) {
  10852. this.raise(this.state.start, ErrorMessages.GeneratorInSingleStatementContext);
  10853. }
  10854. node.generator = this.eat(types$1.star);
  10855. if (isStatement) {
  10856. node.id = this.parseFunctionId(requireId);
  10857. }
  10858. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  10859. this.state.maybeInArrowParameters = false;
  10860. this.scope.enter(SCOPE_FUNCTION);
  10861. this.prodParam.enter(functionFlags(isAsync, node.generator));
  10862. if (!isStatement) {
  10863. node.id = this.parseFunctionId();
  10864. }
  10865. this.parseFunctionParams(node, false);
  10866. this.withTopicForbiddingContext(() => {
  10867. this.parseFunctionBodyAndFinish(node, isStatement ? "FunctionDeclaration" : "FunctionExpression");
  10868. });
  10869. this.prodParam.exit();
  10870. this.scope.exit();
  10871. if (isStatement && !isHangingStatement) {
  10872. this.registerFunctionStatementId(node);
  10873. }
  10874. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  10875. return node;
  10876. }
  10877. parseFunctionId(requireId) {
  10878. return requireId || this.match(types$1.name) ? this.parseIdentifier() : null;
  10879. }
  10880. parseFunctionParams(node, allowModifiers) {
  10881. this.expect(types$1.parenL);
  10882. this.expressionScope.enter(newParameterDeclarationScope());
  10883. node.params = this.parseBindingList(types$1.parenR, 41, false, allowModifiers);
  10884. this.expressionScope.exit();
  10885. }
  10886. registerFunctionStatementId(node) {
  10887. if (!node.id) return;
  10888. 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);
  10889. }
  10890. parseClass(node, isStatement, optionalId) {
  10891. this.next();
  10892. this.takeDecorators(node);
  10893. const oldStrict = this.state.strict;
  10894. this.state.strict = true;
  10895. this.parseClassId(node, isStatement, optionalId);
  10896. this.parseClassSuper(node);
  10897. node.body = this.parseClassBody(!!node.superClass, oldStrict);
  10898. return this.finishNode(node, isStatement ? "ClassDeclaration" : "ClassExpression");
  10899. }
  10900. isClassProperty() {
  10901. return this.match(types$1.eq) || this.match(types$1.semi) || this.match(types$1.braceR);
  10902. }
  10903. isClassMethod() {
  10904. return this.match(types$1.parenL);
  10905. }
  10906. isNonstaticConstructor(method) {
  10907. return !method.computed && !method.static && (method.key.name === "constructor" || method.key.value === "constructor");
  10908. }
  10909. parseClassBody(hadSuperClass, oldStrict) {
  10910. this.classScope.enter();
  10911. const state = {
  10912. hadConstructor: false,
  10913. hadSuperClass
  10914. };
  10915. let decorators = [];
  10916. const classBody = this.startNode();
  10917. classBody.body = [];
  10918. this.expect(types$1.braceL);
  10919. this.withTopicForbiddingContext(() => {
  10920. while (!this.match(types$1.braceR)) {
  10921. if (this.eat(types$1.semi)) {
  10922. if (decorators.length > 0) {
  10923. throw this.raise(this.state.lastTokEnd, ErrorMessages.DecoratorSemicolon);
  10924. }
  10925. continue;
  10926. }
  10927. if (this.match(types$1.at)) {
  10928. decorators.push(this.parseDecorator());
  10929. continue;
  10930. }
  10931. const member = this.startNode();
  10932. if (decorators.length) {
  10933. member.decorators = decorators;
  10934. this.resetStartLocationFromNode(member, decorators[0]);
  10935. decorators = [];
  10936. }
  10937. this.parseClassMember(classBody, member, state);
  10938. if (member.kind === "constructor" && member.decorators && member.decorators.length > 0) {
  10939. this.raise(member.start, ErrorMessages.DecoratorConstructor);
  10940. }
  10941. }
  10942. });
  10943. this.state.strict = oldStrict;
  10944. this.next();
  10945. if (decorators.length) {
  10946. throw this.raise(this.state.start, ErrorMessages.TrailingDecorator);
  10947. }
  10948. this.classScope.exit();
  10949. return this.finishNode(classBody, "ClassBody");
  10950. }
  10951. parseClassMemberFromModifier(classBody, member) {
  10952. const key = this.parseIdentifier(true);
  10953. if (this.isClassMethod()) {
  10954. const method = member;
  10955. method.kind = "method";
  10956. method.computed = false;
  10957. method.key = key;
  10958. method.static = false;
  10959. this.pushClassMethod(classBody, method, false, false, false, false);
  10960. return true;
  10961. } else if (this.isClassProperty()) {
  10962. const prop = member;
  10963. prop.computed = false;
  10964. prop.key = key;
  10965. prop.static = false;
  10966. classBody.body.push(this.parseClassProperty(prop));
  10967. return true;
  10968. }
  10969. return false;
  10970. }
  10971. parseClassMember(classBody, member, state) {
  10972. const isStatic = this.isContextual("static");
  10973. if (isStatic) {
  10974. if (this.parseClassMemberFromModifier(classBody, member)) {
  10975. return;
  10976. }
  10977. if (this.eat(types$1.braceL)) {
  10978. this.parseClassStaticBlock(classBody, member);
  10979. return;
  10980. }
  10981. }
  10982. this.parseClassMemberWithIsStatic(classBody, member, state, isStatic);
  10983. }
  10984. parseClassMemberWithIsStatic(classBody, member, state, isStatic) {
  10985. const publicMethod = member;
  10986. const privateMethod = member;
  10987. const publicProp = member;
  10988. const privateProp = member;
  10989. const method = publicMethod;
  10990. const publicMember = publicMethod;
  10991. member.static = isStatic;
  10992. if (this.eat(types$1.star)) {
  10993. method.kind = "method";
  10994. const isPrivateName = this.match(types$1.privateName);
  10995. this.parseClassElementName(method);
  10996. if (isPrivateName) {
  10997. this.pushClassPrivateMethod(classBody, privateMethod, true, false);
  10998. return;
  10999. }
  11000. if (this.isNonstaticConstructor(publicMethod)) {
  11001. this.raise(publicMethod.key.start, ErrorMessages.ConstructorIsGenerator);
  11002. }
  11003. this.pushClassMethod(classBody, publicMethod, true, false, false, false);
  11004. return;
  11005. }
  11006. const containsEsc = this.state.containsEsc;
  11007. const isPrivate = this.match(types$1.privateName);
  11008. const key = this.parseClassElementName(member);
  11009. const isSimple = key.type === "Identifier";
  11010. const maybeQuestionTokenStart = this.state.start;
  11011. this.parsePostMemberNameModifiers(publicMember);
  11012. if (this.isClassMethod()) {
  11013. method.kind = "method";
  11014. if (isPrivate) {
  11015. this.pushClassPrivateMethod(classBody, privateMethod, false, false);
  11016. return;
  11017. }
  11018. const isConstructor = this.isNonstaticConstructor(publicMethod);
  11019. let allowsDirectSuper = false;
  11020. if (isConstructor) {
  11021. publicMethod.kind = "constructor";
  11022. if (state.hadConstructor && !this.hasPlugin("typescript")) {
  11023. this.raise(key.start, ErrorMessages.DuplicateConstructor);
  11024. }
  11025. if (isConstructor && this.hasPlugin("typescript") && member.override) {
  11026. this.raise(key.start, ErrorMessages.OverrideOnConstructor);
  11027. }
  11028. state.hadConstructor = true;
  11029. allowsDirectSuper = state.hadSuperClass;
  11030. }
  11031. this.pushClassMethod(classBody, publicMethod, false, false, isConstructor, allowsDirectSuper);
  11032. } else if (this.isClassProperty()) {
  11033. if (isPrivate) {
  11034. this.pushClassPrivateProperty(classBody, privateProp);
  11035. } else {
  11036. this.pushClassProperty(classBody, publicProp);
  11037. }
  11038. } else if (isSimple && key.name === "async" && !containsEsc && !this.isLineTerminator()) {
  11039. const isGenerator = this.eat(types$1.star);
  11040. if (publicMember.optional) {
  11041. this.unexpected(maybeQuestionTokenStart);
  11042. }
  11043. method.kind = "method";
  11044. const isPrivate = this.match(types$1.privateName);
  11045. this.parseClassElementName(method);
  11046. this.parsePostMemberNameModifiers(publicMember);
  11047. if (isPrivate) {
  11048. this.pushClassPrivateMethod(classBody, privateMethod, isGenerator, true);
  11049. } else {
  11050. if (this.isNonstaticConstructor(publicMethod)) {
  11051. this.raise(publicMethod.key.start, ErrorMessages.ConstructorIsAsync);
  11052. }
  11053. this.pushClassMethod(classBody, publicMethod, isGenerator, true, false, false);
  11054. }
  11055. } else if (isSimple && (key.name === "get" || key.name === "set") && !containsEsc && !(this.match(types$1.star) && this.isLineTerminator())) {
  11056. method.kind = key.name;
  11057. const isPrivate = this.match(types$1.privateName);
  11058. this.parseClassElementName(publicMethod);
  11059. if (isPrivate) {
  11060. this.pushClassPrivateMethod(classBody, privateMethod, false, false);
  11061. } else {
  11062. if (this.isNonstaticConstructor(publicMethod)) {
  11063. this.raise(publicMethod.key.start, ErrorMessages.ConstructorIsAccessor);
  11064. }
  11065. this.pushClassMethod(classBody, publicMethod, false, false, false, false);
  11066. }
  11067. this.checkGetterSetterParams(publicMethod);
  11068. } else if (this.isLineTerminator()) {
  11069. if (isPrivate) {
  11070. this.pushClassPrivateProperty(classBody, privateProp);
  11071. } else {
  11072. this.pushClassProperty(classBody, publicProp);
  11073. }
  11074. } else {
  11075. this.unexpected();
  11076. }
  11077. }
  11078. parseClassElementName(member) {
  11079. const {
  11080. type,
  11081. value,
  11082. start
  11083. } = this.state;
  11084. if ((type === types$1.name || type === types$1.string) && member.static && value === "prototype") {
  11085. this.raise(start, ErrorMessages.StaticPrototype);
  11086. }
  11087. if (type === types$1.privateName && value === "constructor") {
  11088. this.raise(start, ErrorMessages.ConstructorClassPrivateField);
  11089. }
  11090. return this.parsePropertyName(member, true);
  11091. }
  11092. parseClassStaticBlock(classBody, member) {
  11093. var _member$decorators;
  11094. this.expectPlugin("classStaticBlock", member.start);
  11095. this.scope.enter(SCOPE_CLASS | SCOPE_STATIC_BLOCK | SCOPE_SUPER);
  11096. const oldLabels = this.state.labels;
  11097. this.state.labels = [];
  11098. this.prodParam.enter(PARAM);
  11099. const body = member.body = [];
  11100. this.parseBlockOrModuleBlockBody(body, undefined, false, types$1.braceR);
  11101. this.prodParam.exit();
  11102. this.scope.exit();
  11103. this.state.labels = oldLabels;
  11104. classBody.body.push(this.finishNode(member, "StaticBlock"));
  11105. if ((_member$decorators = member.decorators) != null && _member$decorators.length) {
  11106. this.raise(member.start, ErrorMessages.DecoratorStaticBlock);
  11107. }
  11108. }
  11109. pushClassProperty(classBody, prop) {
  11110. if (!prop.computed && (prop.key.name === "constructor" || prop.key.value === "constructor")) {
  11111. this.raise(prop.key.start, ErrorMessages.ConstructorClassField);
  11112. }
  11113. classBody.body.push(this.parseClassProperty(prop));
  11114. }
  11115. pushClassPrivateProperty(classBody, prop) {
  11116. const node = this.parseClassPrivateProperty(prop);
  11117. classBody.body.push(node);
  11118. this.classScope.declarePrivateName(this.getPrivateNameSV(node.key), CLASS_ELEMENT_OTHER, node.key.start);
  11119. }
  11120. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  11121. classBody.body.push(this.parseMethod(method, isGenerator, isAsync, isConstructor, allowsDirectSuper, "ClassMethod", true));
  11122. }
  11123. pushClassPrivateMethod(classBody, method, isGenerator, isAsync) {
  11124. const node = this.parseMethod(method, isGenerator, isAsync, false, false, "ClassPrivateMethod", true);
  11125. classBody.body.push(node);
  11126. 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;
  11127. this.classScope.declarePrivateName(this.getPrivateNameSV(node.key), kind, node.key.start);
  11128. }
  11129. parsePostMemberNameModifiers(methodOrProp) {}
  11130. parseClassPrivateProperty(node) {
  11131. this.parseInitializer(node);
  11132. this.semicolon();
  11133. return this.finishNode(node, "ClassPrivateProperty");
  11134. }
  11135. parseClassProperty(node) {
  11136. this.parseInitializer(node);
  11137. this.semicolon();
  11138. return this.finishNode(node, "ClassProperty");
  11139. }
  11140. parseInitializer(node) {
  11141. this.scope.enter(SCOPE_CLASS | SCOPE_SUPER);
  11142. this.expressionScope.enter(newExpressionScope());
  11143. this.prodParam.enter(PARAM);
  11144. node.value = this.eat(types$1.eq) ? this.parseMaybeAssignAllowIn() : null;
  11145. this.expressionScope.exit();
  11146. this.prodParam.exit();
  11147. this.scope.exit();
  11148. }
  11149. parseClassId(node, isStatement, optionalId, bindingType = BIND_CLASS) {
  11150. if (this.match(types$1.name)) {
  11151. node.id = this.parseIdentifier();
  11152. if (isStatement) {
  11153. this.checkLVal(node.id, "class name", bindingType);
  11154. }
  11155. } else {
  11156. if (optionalId || !isStatement) {
  11157. node.id = null;
  11158. } else {
  11159. this.unexpected(null, ErrorMessages.MissingClassName);
  11160. }
  11161. }
  11162. }
  11163. parseClassSuper(node) {
  11164. node.superClass = this.eat(types$1._extends) ? this.parseExprSubscripts() : null;
  11165. }
  11166. parseExport(node) {
  11167. const hasDefault = this.maybeParseExportDefaultSpecifier(node);
  11168. const parseAfterDefault = !hasDefault || this.eat(types$1.comma);
  11169. const hasStar = parseAfterDefault && this.eatExportStar(node);
  11170. const hasNamespace = hasStar && this.maybeParseExportNamespaceSpecifier(node);
  11171. const parseAfterNamespace = parseAfterDefault && (!hasNamespace || this.eat(types$1.comma));
  11172. const isFromRequired = hasDefault || hasStar;
  11173. if (hasStar && !hasNamespace) {
  11174. if (hasDefault) this.unexpected();
  11175. this.parseExportFrom(node, true);
  11176. return this.finishNode(node, "ExportAllDeclaration");
  11177. }
  11178. const hasSpecifiers = this.maybeParseExportNamedSpecifiers(node);
  11179. if (hasDefault && parseAfterDefault && !hasStar && !hasSpecifiers || hasNamespace && parseAfterNamespace && !hasSpecifiers) {
  11180. throw this.unexpected(null, types$1.braceL);
  11181. }
  11182. let hasDeclaration;
  11183. if (isFromRequired || hasSpecifiers) {
  11184. hasDeclaration = false;
  11185. this.parseExportFrom(node, isFromRequired);
  11186. } else {
  11187. hasDeclaration = this.maybeParseExportDeclaration(node);
  11188. }
  11189. if (isFromRequired || hasSpecifiers || hasDeclaration) {
  11190. this.checkExport(node, true, false, !!node.source);
  11191. return this.finishNode(node, "ExportNamedDeclaration");
  11192. }
  11193. if (this.eat(types$1._default)) {
  11194. node.declaration = this.parseExportDefaultExpression();
  11195. this.checkExport(node, true, true);
  11196. return this.finishNode(node, "ExportDefaultDeclaration");
  11197. }
  11198. throw this.unexpected(null, types$1.braceL);
  11199. }
  11200. eatExportStar(node) {
  11201. return this.eat(types$1.star);
  11202. }
  11203. maybeParseExportDefaultSpecifier(node) {
  11204. if (this.isExportDefaultSpecifier()) {
  11205. this.expectPlugin("exportDefaultFrom");
  11206. const specifier = this.startNode();
  11207. specifier.exported = this.parseIdentifier(true);
  11208. node.specifiers = [this.finishNode(specifier, "ExportDefaultSpecifier")];
  11209. return true;
  11210. }
  11211. return false;
  11212. }
  11213. maybeParseExportNamespaceSpecifier(node) {
  11214. if (this.isContextual("as")) {
  11215. if (!node.specifiers) node.specifiers = [];
  11216. const specifier = this.startNodeAt(this.state.lastTokStart, this.state.lastTokStartLoc);
  11217. this.next();
  11218. specifier.exported = this.parseModuleExportName();
  11219. node.specifiers.push(this.finishNode(specifier, "ExportNamespaceSpecifier"));
  11220. return true;
  11221. }
  11222. return false;
  11223. }
  11224. maybeParseExportNamedSpecifiers(node) {
  11225. if (this.match(types$1.braceL)) {
  11226. if (!node.specifiers) node.specifiers = [];
  11227. node.specifiers.push(...this.parseExportSpecifiers());
  11228. node.source = null;
  11229. node.declaration = null;
  11230. return true;
  11231. }
  11232. return false;
  11233. }
  11234. maybeParseExportDeclaration(node) {
  11235. if (this.shouldParseExportDeclaration()) {
  11236. node.specifiers = [];
  11237. node.source = null;
  11238. node.declaration = this.parseExportDeclaration(node);
  11239. return true;
  11240. }
  11241. return false;
  11242. }
  11243. isAsyncFunction() {
  11244. if (!this.isContextual("async")) return false;
  11245. const next = this.nextTokenStart();
  11246. return !lineBreak.test(this.input.slice(this.state.pos, next)) && this.isUnparsedContextual(next, "function");
  11247. }
  11248. parseExportDefaultExpression() {
  11249. const expr = this.startNode();
  11250. const isAsync = this.isAsyncFunction();
  11251. if (this.match(types$1._function) || isAsync) {
  11252. this.next();
  11253. if (isAsync) {
  11254. this.next();
  11255. }
  11256. return this.parseFunction(expr, FUNC_STATEMENT | FUNC_NULLABLE_ID, isAsync);
  11257. } else if (this.match(types$1._class)) {
  11258. return this.parseClass(expr, true, true);
  11259. } else if (this.match(types$1.at)) {
  11260. if (this.hasPlugin("decorators") && this.getPluginOption("decorators", "decoratorsBeforeExport")) {
  11261. this.raise(this.state.start, ErrorMessages.DecoratorBeforeExport);
  11262. }
  11263. this.parseDecorators(false);
  11264. return this.parseClass(expr, true, true);
  11265. } else if (this.match(types$1._const) || this.match(types$1._var) || this.isLet()) {
  11266. throw this.raise(this.state.start, ErrorMessages.UnsupportedDefaultExport);
  11267. } else {
  11268. const res = this.parseMaybeAssignAllowIn();
  11269. this.semicolon();
  11270. return res;
  11271. }
  11272. }
  11273. parseExportDeclaration(node) {
  11274. return this.parseStatement(null);
  11275. }
  11276. isExportDefaultSpecifier() {
  11277. if (this.match(types$1.name)) {
  11278. const value = this.state.value;
  11279. if (value === "async" && !this.state.containsEsc || value === "let") {
  11280. return false;
  11281. }
  11282. if ((value === "type" || value === "interface") && !this.state.containsEsc) {
  11283. const l = this.lookahead();
  11284. if (l.type === types$1.name && l.value !== "from" || l.type === types$1.braceL) {
  11285. this.expectOnePlugin(["flow", "typescript"]);
  11286. return false;
  11287. }
  11288. }
  11289. } else if (!this.match(types$1._default)) {
  11290. return false;
  11291. }
  11292. const next = this.nextTokenStart();
  11293. const hasFrom = this.isUnparsedContextual(next, "from");
  11294. if (this.input.charCodeAt(next) === 44 || this.match(types$1.name) && hasFrom) {
  11295. return true;
  11296. }
  11297. if (this.match(types$1._default) && hasFrom) {
  11298. const nextAfterFrom = this.input.charCodeAt(this.nextTokenStartSince(next + 4));
  11299. return nextAfterFrom === 34 || nextAfterFrom === 39;
  11300. }
  11301. return false;
  11302. }
  11303. parseExportFrom(node, expect) {
  11304. if (this.eatContextual("from")) {
  11305. node.source = this.parseImportSource();
  11306. this.checkExport(node);
  11307. const assertions = this.maybeParseImportAssertions();
  11308. if (assertions) {
  11309. node.assertions = assertions;
  11310. }
  11311. } else {
  11312. if (expect) {
  11313. this.unexpected();
  11314. } else {
  11315. node.source = null;
  11316. }
  11317. }
  11318. this.semicolon();
  11319. }
  11320. shouldParseExportDeclaration() {
  11321. if (this.match(types$1.at)) {
  11322. this.expectOnePlugin(["decorators", "decorators-legacy"]);
  11323. if (this.hasPlugin("decorators")) {
  11324. if (this.getPluginOption("decorators", "decoratorsBeforeExport")) {
  11325. this.unexpected(this.state.start, ErrorMessages.DecoratorBeforeExport);
  11326. } else {
  11327. return true;
  11328. }
  11329. }
  11330. }
  11331. 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();
  11332. }
  11333. checkExport(node, checkNames, isDefault, isFrom) {
  11334. if (checkNames) {
  11335. if (isDefault) {
  11336. this.checkDuplicateExports(node, "default");
  11337. if (this.hasPlugin("exportDefaultFrom")) {
  11338. var _declaration$extra;
  11339. const declaration = node.declaration;
  11340. if (declaration.type === "Identifier" && declaration.name === "from" && declaration.end - declaration.start === 4 && !((_declaration$extra = declaration.extra) != null && _declaration$extra.parenthesized)) {
  11341. this.raise(declaration.start, ErrorMessages.ExportDefaultFromAsIdentifier);
  11342. }
  11343. }
  11344. } else if (node.specifiers && node.specifiers.length) {
  11345. for (const specifier of node.specifiers) {
  11346. const {
  11347. exported
  11348. } = specifier;
  11349. const exportedName = exported.type === "Identifier" ? exported.name : exported.value;
  11350. this.checkDuplicateExports(specifier, exportedName);
  11351. if (!isFrom && specifier.local) {
  11352. const {
  11353. local
  11354. } = specifier;
  11355. if (local.type !== "Identifier") {
  11356. this.raise(specifier.start, ErrorMessages.ExportBindingIsString, local.value, exportedName);
  11357. } else {
  11358. this.checkReservedWord(local.name, local.start, true, false);
  11359. this.scope.checkLocalExport(local);
  11360. }
  11361. }
  11362. }
  11363. } else if (node.declaration) {
  11364. if (node.declaration.type === "FunctionDeclaration" || node.declaration.type === "ClassDeclaration") {
  11365. const id = node.declaration.id;
  11366. if (!id) throw new Error("Assertion failure");
  11367. this.checkDuplicateExports(node, id.name);
  11368. } else if (node.declaration.type === "VariableDeclaration") {
  11369. for (const declaration of node.declaration.declarations) {
  11370. this.checkDeclaration(declaration.id);
  11371. }
  11372. }
  11373. }
  11374. }
  11375. const currentContextDecorators = this.state.decoratorStack[this.state.decoratorStack.length - 1];
  11376. if (currentContextDecorators.length) {
  11377. throw this.raise(node.start, ErrorMessages.UnsupportedDecoratorExport);
  11378. }
  11379. }
  11380. checkDeclaration(node) {
  11381. if (node.type === "Identifier") {
  11382. this.checkDuplicateExports(node, node.name);
  11383. } else if (node.type === "ObjectPattern") {
  11384. for (const prop of node.properties) {
  11385. this.checkDeclaration(prop);
  11386. }
  11387. } else if (node.type === "ArrayPattern") {
  11388. for (const elem of node.elements) {
  11389. if (elem) {
  11390. this.checkDeclaration(elem);
  11391. }
  11392. }
  11393. } else if (node.type === "ObjectProperty") {
  11394. this.checkDeclaration(node.value);
  11395. } else if (node.type === "RestElement") {
  11396. this.checkDeclaration(node.argument);
  11397. } else if (node.type === "AssignmentPattern") {
  11398. this.checkDeclaration(node.left);
  11399. }
  11400. }
  11401. checkDuplicateExports(node, name) {
  11402. if (this.state.exportedIdentifiers.indexOf(name) > -1) {
  11403. this.raise(node.start, name === "default" ? ErrorMessages.DuplicateDefaultExport : ErrorMessages.DuplicateExport, name);
  11404. }
  11405. this.state.exportedIdentifiers.push(name);
  11406. }
  11407. parseExportSpecifiers() {
  11408. const nodes = [];
  11409. let first = true;
  11410. this.expect(types$1.braceL);
  11411. while (!this.eat(types$1.braceR)) {
  11412. if (first) {
  11413. first = false;
  11414. } else {
  11415. this.expect(types$1.comma);
  11416. if (this.eat(types$1.braceR)) break;
  11417. }
  11418. const node = this.startNode();
  11419. node.local = this.parseModuleExportName();
  11420. node.exported = this.eatContextual("as") ? this.parseModuleExportName() : node.local.__clone();
  11421. nodes.push(this.finishNode(node, "ExportSpecifier"));
  11422. }
  11423. return nodes;
  11424. }
  11425. parseModuleExportName() {
  11426. if (this.match(types$1.string)) {
  11427. const result = this.parseStringLiteral(this.state.value);
  11428. const surrogate = result.value.match(loneSurrogate);
  11429. if (surrogate) {
  11430. this.raise(result.start, ErrorMessages.ModuleExportNameHasLoneSurrogate, surrogate[0].charCodeAt(0).toString(16));
  11431. }
  11432. return result;
  11433. }
  11434. return this.parseIdentifier(true);
  11435. }
  11436. parseImport(node) {
  11437. node.specifiers = [];
  11438. if (!this.match(types$1.string)) {
  11439. const hasDefault = this.maybeParseDefaultImportSpecifier(node);
  11440. const parseNext = !hasDefault || this.eat(types$1.comma);
  11441. const hasStar = parseNext && this.maybeParseStarImportSpecifier(node);
  11442. if (parseNext && !hasStar) this.parseNamedImportSpecifiers(node);
  11443. this.expectContextual("from");
  11444. }
  11445. node.source = this.parseImportSource();
  11446. const assertions = this.maybeParseImportAssertions();
  11447. if (assertions) {
  11448. node.assertions = assertions;
  11449. } else {
  11450. const attributes = this.maybeParseModuleAttributes();
  11451. if (attributes) {
  11452. node.attributes = attributes;
  11453. }
  11454. }
  11455. this.semicolon();
  11456. return this.finishNode(node, "ImportDeclaration");
  11457. }
  11458. parseImportSource() {
  11459. if (!this.match(types$1.string)) this.unexpected();
  11460. return this.parseExprAtom();
  11461. }
  11462. shouldParseDefaultImport(node) {
  11463. return this.match(types$1.name);
  11464. }
  11465. parseImportSpecifierLocal(node, specifier, type, contextDescription) {
  11466. specifier.local = this.parseIdentifier();
  11467. this.checkLVal(specifier.local, contextDescription, BIND_LEXICAL);
  11468. node.specifiers.push(this.finishNode(specifier, type));
  11469. }
  11470. parseAssertEntries() {
  11471. const attrs = [];
  11472. const attrNames = new Set();
  11473. do {
  11474. if (this.match(types$1.braceR)) {
  11475. break;
  11476. }
  11477. const node = this.startNode();
  11478. const keyName = this.state.value;
  11479. if (this.match(types$1.string)) {
  11480. node.key = this.parseStringLiteral(keyName);
  11481. } else {
  11482. node.key = this.parseIdentifier(true);
  11483. }
  11484. this.expect(types$1.colon);
  11485. if (keyName !== "type") {
  11486. this.raise(node.key.start, ErrorMessages.ModuleAttributeDifferentFromType, keyName);
  11487. }
  11488. if (attrNames.has(keyName)) {
  11489. this.raise(node.key.start, ErrorMessages.ModuleAttributesWithDuplicateKeys, keyName);
  11490. }
  11491. attrNames.add(keyName);
  11492. if (!this.match(types$1.string)) {
  11493. throw this.unexpected(this.state.start, ErrorMessages.ModuleAttributeInvalidValue);
  11494. }
  11495. node.value = this.parseStringLiteral(this.state.value);
  11496. this.finishNode(node, "ImportAttribute");
  11497. attrs.push(node);
  11498. } while (this.eat(types$1.comma));
  11499. return attrs;
  11500. }
  11501. maybeParseModuleAttributes() {
  11502. if (this.match(types$1._with) && !this.hasPrecedingLineBreak()) {
  11503. this.expectPlugin("moduleAttributes");
  11504. this.next();
  11505. } else {
  11506. if (this.hasPlugin("moduleAttributes")) return [];
  11507. return null;
  11508. }
  11509. const attrs = [];
  11510. const attributes = new Set();
  11511. do {
  11512. const node = this.startNode();
  11513. node.key = this.parseIdentifier(true);
  11514. if (node.key.name !== "type") {
  11515. this.raise(node.key.start, ErrorMessages.ModuleAttributeDifferentFromType, node.key.name);
  11516. }
  11517. if (attributes.has(node.key.name)) {
  11518. this.raise(node.key.start, ErrorMessages.ModuleAttributesWithDuplicateKeys, node.key.name);
  11519. }
  11520. attributes.add(node.key.name);
  11521. this.expect(types$1.colon);
  11522. if (!this.match(types$1.string)) {
  11523. throw this.unexpected(this.state.start, ErrorMessages.ModuleAttributeInvalidValue);
  11524. }
  11525. node.value = this.parseStringLiteral(this.state.value);
  11526. this.finishNode(node, "ImportAttribute");
  11527. attrs.push(node);
  11528. } while (this.eat(types$1.comma));
  11529. return attrs;
  11530. }
  11531. maybeParseImportAssertions() {
  11532. if (this.isContextual("assert") && !this.hasPrecedingLineBreak()) {
  11533. this.expectPlugin("importAssertions");
  11534. this.next();
  11535. } else {
  11536. if (this.hasPlugin("importAssertions")) return [];
  11537. return null;
  11538. }
  11539. this.eat(types$1.braceL);
  11540. const attrs = this.parseAssertEntries();
  11541. this.eat(types$1.braceR);
  11542. return attrs;
  11543. }
  11544. maybeParseDefaultImportSpecifier(node) {
  11545. if (this.shouldParseDefaultImport(node)) {
  11546. this.parseImportSpecifierLocal(node, this.startNode(), "ImportDefaultSpecifier", "default import specifier");
  11547. return true;
  11548. }
  11549. return false;
  11550. }
  11551. maybeParseStarImportSpecifier(node) {
  11552. if (this.match(types$1.star)) {
  11553. const specifier = this.startNode();
  11554. this.next();
  11555. this.expectContextual("as");
  11556. this.parseImportSpecifierLocal(node, specifier, "ImportNamespaceSpecifier", "import namespace specifier");
  11557. return true;
  11558. }
  11559. return false;
  11560. }
  11561. parseNamedImportSpecifiers(node) {
  11562. let first = true;
  11563. this.expect(types$1.braceL);
  11564. while (!this.eat(types$1.braceR)) {
  11565. if (first) {
  11566. first = false;
  11567. } else {
  11568. if (this.eat(types$1.colon)) {
  11569. throw this.raise(this.state.start, ErrorMessages.DestructureNamedImport);
  11570. }
  11571. this.expect(types$1.comma);
  11572. if (this.eat(types$1.braceR)) break;
  11573. }
  11574. this.parseImportSpecifier(node);
  11575. }
  11576. }
  11577. parseImportSpecifier(node) {
  11578. const specifier = this.startNode();
  11579. const importedIsString = this.match(types$1.string);
  11580. specifier.imported = this.parseModuleExportName();
  11581. if (this.eatContextual("as")) {
  11582. specifier.local = this.parseIdentifier();
  11583. } else {
  11584. const {
  11585. imported
  11586. } = specifier;
  11587. if (importedIsString) {
  11588. throw this.raise(specifier.start, ErrorMessages.ImportBindingIsString, imported.value);
  11589. }
  11590. this.checkReservedWord(imported.name, specifier.start, true, true);
  11591. specifier.local = imported.__clone();
  11592. }
  11593. this.checkLVal(specifier.local, "import specifier", BIND_LEXICAL);
  11594. node.specifiers.push(this.finishNode(specifier, "ImportSpecifier"));
  11595. }
  11596. isThisParam(param) {
  11597. return param.type === "Identifier" && param.name === "this";
  11598. }
  11599. }
  11600. class Parser extends StatementParser {
  11601. constructor(options, input) {
  11602. options = getOptions(options);
  11603. super(options, input);
  11604. this.options = options;
  11605. this.initializeScopes();
  11606. this.plugins = pluginsMap(this.options.plugins);
  11607. this.filename = options.sourceFilename;
  11608. }
  11609. getScopeHandler() {
  11610. return ScopeHandler;
  11611. }
  11612. parse() {
  11613. this.enterInitialScopes();
  11614. const file = this.startNode();
  11615. const program = this.startNode();
  11616. this.nextToken();
  11617. file.errors = null;
  11618. this.parseTopLevel(file, program);
  11619. file.errors = this.state.errors;
  11620. return file;
  11621. }
  11622. }
  11623. function pluginsMap(plugins) {
  11624. const pluginMap = new Map();
  11625. for (const plugin of plugins) {
  11626. const [name, options] = Array.isArray(plugin) ? plugin : [plugin, {}];
  11627. if (!pluginMap.has(name)) pluginMap.set(name, options || {});
  11628. }
  11629. return pluginMap;
  11630. }
  11631. function parse(input, options) {
  11632. var _options;
  11633. if (((_options = options) == null ? void 0 : _options.sourceType) === "unambiguous") {
  11634. options = Object.assign({}, options);
  11635. try {
  11636. options.sourceType = "module";
  11637. const parser = getParser(options, input);
  11638. const ast = parser.parse();
  11639. if (parser.sawUnambiguousESM) {
  11640. return ast;
  11641. }
  11642. if (parser.ambiguousScriptDifferentAst) {
  11643. try {
  11644. options.sourceType = "script";
  11645. return getParser(options, input).parse();
  11646. } catch (_unused) {}
  11647. } else {
  11648. ast.program.sourceType = "script";
  11649. }
  11650. return ast;
  11651. } catch (moduleError) {
  11652. try {
  11653. options.sourceType = "script";
  11654. return getParser(options, input).parse();
  11655. } catch (_unused2) {}
  11656. throw moduleError;
  11657. }
  11658. } else {
  11659. return getParser(options, input).parse();
  11660. }
  11661. }
  11662. function parseExpression(input, options) {
  11663. const parser = getParser(options, input);
  11664. if (parser.options.strictMode) {
  11665. parser.state.strict = true;
  11666. }
  11667. return parser.getExpression();
  11668. }
  11669. function getParser(options, input) {
  11670. let cls = Parser;
  11671. if (options != null && options.plugins) {
  11672. validatePlugins(options.plugins);
  11673. cls = getParserClass(options.plugins);
  11674. }
  11675. return new cls(options, input);
  11676. }
  11677. const parserClassCache = {};
  11678. function getParserClass(pluginsFromOptions) {
  11679. const pluginList = mixinPluginNames.filter(name => hasPlugin(pluginsFromOptions, name));
  11680. const key = pluginList.join("/");
  11681. let cls = parserClassCache[key];
  11682. if (!cls) {
  11683. cls = Parser;
  11684. for (const plugin of pluginList) {
  11685. cls = mixinPlugins[plugin](cls);
  11686. }
  11687. parserClassCache[key] = cls;
  11688. }
  11689. return cls;
  11690. }
  11691. exports.parse = parse;
  11692. exports.parseExpression = parseExpression;
  11693. exports.tokTypes = types$1;
  11694. //# sourceMappingURL=index.js.map