index.js 477 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. function _objectWithoutPropertiesLoose(source, excluded) {
  4. if (source == null) return {};
  5. var target = {};
  6. var sourceKeys = Object.keys(source);
  7. var key, i;
  8. for (i = 0; i < sourceKeys.length; i++) {
  9. key = sourceKeys[i];
  10. if (excluded.indexOf(key) >= 0) continue;
  11. target[key] = source[key];
  12. }
  13. return target;
  14. }
  15. class Position {
  16. constructor(line, col, index) {
  17. this.line = void 0;
  18. this.column = void 0;
  19. this.index = void 0;
  20. this.line = line;
  21. this.column = col;
  22. this.index = index;
  23. }
  24. }
  25. class SourceLocation {
  26. constructor(start, end) {
  27. this.start = void 0;
  28. this.end = void 0;
  29. this.filename = void 0;
  30. this.identifierName = void 0;
  31. this.start = start;
  32. this.end = end;
  33. }
  34. }
  35. function createPositionWithColumnOffset(position, columnOffset) {
  36. const {
  37. line,
  38. column,
  39. index
  40. } = position;
  41. return new Position(line, column + columnOffset, index + columnOffset);
  42. }
  43. var ParseErrorCode = {
  44. SyntaxError: "BABEL_PARSER_SYNTAX_ERROR",
  45. SourceTypeModuleError: "BABEL_PARSER_SOURCETYPE_MODULE_REQUIRED"
  46. };
  47. const reflect = (keys, last = keys.length - 1) => ({
  48. get() {
  49. return keys.reduce((object, key) => object[key], this);
  50. },
  51. set(value) {
  52. keys.reduce((item, key, i) => i === last ? item[key] = value : item[key], this);
  53. }
  54. });
  55. const instantiate = (constructor, properties, descriptors) => Object.keys(descriptors).map(key => [key, descriptors[key]]).filter(([, descriptor]) => !!descriptor).map(([key, descriptor]) => [key, typeof descriptor === "function" ? {
  56. value: descriptor,
  57. enumerable: false
  58. } : typeof descriptor.reflect === "string" ? Object.assign({}, descriptor, reflect(descriptor.reflect.split("."))) : descriptor]).reduce((instance, [key, descriptor]) => Object.defineProperty(instance, key, Object.assign({
  59. configurable: true
  60. }, descriptor)), Object.assign(new constructor(), properties));
  61. var ModuleErrors = {
  62. ImportMetaOutsideModule: {
  63. message: `import.meta may appear only with 'sourceType: "module"'`,
  64. code: ParseErrorCode.SourceTypeModuleError
  65. },
  66. ImportOutsideModule: {
  67. message: `'import' and 'export' may appear only with 'sourceType: "module"'`,
  68. code: ParseErrorCode.SourceTypeModuleError
  69. }
  70. };
  71. const NodeDescriptions = {
  72. ArrayPattern: "array destructuring pattern",
  73. AssignmentExpression: "assignment expression",
  74. AssignmentPattern: "assignment expression",
  75. ArrowFunctionExpression: "arrow function expression",
  76. ConditionalExpression: "conditional expression",
  77. CatchClause: "catch clause",
  78. ForOfStatement: "for-of statement",
  79. ForInStatement: "for-in statement",
  80. ForStatement: "for-loop",
  81. FormalParameters: "function parameter list",
  82. Identifier: "identifier",
  83. ImportSpecifier: "import specifier",
  84. ImportDefaultSpecifier: "import default specifier",
  85. ImportNamespaceSpecifier: "import namespace specifier",
  86. ObjectPattern: "object destructuring pattern",
  87. ParenthesizedExpression: "parenthesized expression",
  88. RestElement: "rest element",
  89. UpdateExpression: {
  90. true: "prefix operation",
  91. false: "postfix operation"
  92. },
  93. VariableDeclarator: "variable declaration",
  94. YieldExpression: "yield expression"
  95. };
  96. const toNodeDescription = ({
  97. type,
  98. prefix
  99. }) => type === "UpdateExpression" ? NodeDescriptions.UpdateExpression[String(prefix)] : NodeDescriptions[type];
  100. var StandardErrors = {
  101. AccessorIsGenerator: ({
  102. kind
  103. }) => `A ${kind}ter cannot be a generator.`,
  104. ArgumentsInClass: "'arguments' is only allowed in functions and class methods.",
  105. AsyncFunctionInSingleStatementContext: "Async functions can only be declared at the top level or inside a block.",
  106. AwaitBindingIdentifier: "Can not use 'await' as identifier inside an async function.",
  107. AwaitBindingIdentifierInStaticBlock: "Can not use 'await' as identifier inside a static block.",
  108. AwaitExpressionFormalParameter: "'await' is not allowed in async function parameters.",
  109. AwaitInUsingBinding: "'await' is not allowed to be used as a name in 'using' declarations.",
  110. AwaitNotInAsyncContext: "'await' is only allowed within async functions and at the top levels of modules.",
  111. AwaitNotInAsyncFunction: "'await' is only allowed within async functions.",
  112. BadGetterArity: "A 'get' accessor must not have any formal parameters.",
  113. BadSetterArity: "A 'set' accessor must have exactly one formal parameter.",
  114. BadSetterRestParameter: "A 'set' accessor function argument must not be a rest parameter.",
  115. ConstructorClassField: "Classes may not have a field named 'constructor'.",
  116. ConstructorClassPrivateField: "Classes may not have a private field named '#constructor'.",
  117. ConstructorIsAccessor: "Class constructor may not be an accessor.",
  118. ConstructorIsAsync: "Constructor can't be an async function.",
  119. ConstructorIsGenerator: "Constructor can't be a generator.",
  120. DeclarationMissingInitializer: ({
  121. kind
  122. }) => `Missing initializer in ${kind} declaration.`,
  123. DecoratorArgumentsOutsideParentheses: "Decorator arguments must be moved inside parentheses: use '@(decorator(args))' instead of '@(decorator)(args)'.",
  124. DecoratorBeforeExport: "Decorators must be placed *before* the 'export' keyword. Remove the 'decoratorsBeforeExport: true' option to use the 'export @decorator class {}' syntax.",
  125. DecoratorsBeforeAfterExport: "Decorators can be placed *either* before or after the 'export' keyword, but not in both locations at the same time.",
  126. DecoratorConstructor: "Decorators can't be used with a constructor. Did you mean '@dec class { ... }'?",
  127. DecoratorExportClass: "Decorators must be placed *after* the 'export' keyword. Remove the 'decoratorsBeforeExport: false' option to use the '@decorator export class {}' syntax.",
  128. DecoratorSemicolon: "Decorators must not be followed by a semicolon.",
  129. DecoratorStaticBlock: "Decorators can't be used with a static block.",
  130. DeletePrivateField: "Deleting a private field is not allowed.",
  131. DestructureNamedImport: "ES2015 named imports do not destructure. Use another statement for destructuring after the import.",
  132. DuplicateConstructor: "Duplicate constructor in the same class.",
  133. DuplicateDefaultExport: "Only one default export allowed per module.",
  134. DuplicateExport: ({
  135. exportName
  136. }) => `\`${exportName}\` has already been exported. Exported identifiers must be unique.`,
  137. DuplicateProto: "Redefinition of __proto__ property.",
  138. DuplicateRegExpFlags: "Duplicate regular expression flag.",
  139. ElementAfterRest: "Rest element must be last element.",
  140. EscapedCharNotAnIdentifier: "Invalid Unicode escape.",
  141. ExportBindingIsString: ({
  142. localName,
  143. exportName
  144. }) => `A string literal cannot be used as an exported binding without \`from\`.\n- Did you mean \`export { '${localName}' as '${exportName}' } from 'some-module'\`?`,
  145. ExportDefaultFromAsIdentifier: "'from' is not allowed as an identifier after 'export default'.",
  146. ForInOfLoopInitializer: ({
  147. type
  148. }) => `'${type === "ForInStatement" ? "for-in" : "for-of"}' loop variable declaration may not have an initializer.`,
  149. ForInUsing: "For-in loop may not start with 'using' declaration.",
  150. ForOfAsync: "The left-hand side of a for-of loop may not be 'async'.",
  151. ForOfLet: "The left-hand side of a for-of loop may not start with 'let'.",
  152. GeneratorInSingleStatementContext: "Generators can only be declared at the top level or inside a block.",
  153. IllegalBreakContinue: ({
  154. type
  155. }) => `Unsyntactic ${type === "BreakStatement" ? "break" : "continue"}.`,
  156. IllegalLanguageModeDirective: "Illegal 'use strict' directive in function with non-simple parameter list.",
  157. IllegalReturn: "'return' outside of function.",
  158. ImportBindingIsString: ({
  159. importName
  160. }) => `A string literal cannot be used as an imported binding.\n- Did you mean \`import { "${importName}" as foo }\`?`,
  161. ImportCallArgumentTrailingComma: "Trailing comma is disallowed inside import(...) arguments.",
  162. ImportCallArity: ({
  163. maxArgumentCount
  164. }) => `\`import()\` requires exactly ${maxArgumentCount === 1 ? "one argument" : "one or two arguments"}.`,
  165. ImportCallNotNewExpression: "Cannot use new with import(...).",
  166. ImportCallSpreadArgument: "`...` is not allowed in `import()`.",
  167. ImportJSONBindingNotDefault: "A JSON module can only be imported with `default`.",
  168. ImportReflectionHasAssertion: "`import module x` cannot have assertions.",
  169. ImportReflectionNotBinding: 'Only `import module x from "./module"` is valid.',
  170. IncompatibleRegExpUVFlags: "The 'u' and 'v' regular expression flags cannot be enabled at the same time.",
  171. InvalidBigIntLiteral: "Invalid BigIntLiteral.",
  172. InvalidCodePoint: "Code point out of bounds.",
  173. InvalidCoverInitializedName: "Invalid shorthand property initializer.",
  174. InvalidDecimal: "Invalid decimal.",
  175. InvalidDigit: ({
  176. radix
  177. }) => `Expected number in radix ${radix}.`,
  178. InvalidEscapeSequence: "Bad character escape sequence.",
  179. InvalidEscapeSequenceTemplate: "Invalid escape sequence in template.",
  180. InvalidEscapedReservedWord: ({
  181. reservedWord
  182. }) => `Escape sequence in keyword ${reservedWord}.`,
  183. InvalidIdentifier: ({
  184. identifierName
  185. }) => `Invalid identifier ${identifierName}.`,
  186. InvalidLhs: ({
  187. ancestor
  188. }) => `Invalid left-hand side in ${toNodeDescription(ancestor)}.`,
  189. InvalidLhsBinding: ({
  190. ancestor
  191. }) => `Binding invalid left-hand side in ${toNodeDescription(ancestor)}.`,
  192. InvalidNumber: "Invalid number.",
  193. InvalidOrMissingExponent: "Floating-point numbers require a valid exponent after the 'e'.",
  194. InvalidOrUnexpectedToken: ({
  195. unexpected
  196. }) => `Unexpected character '${unexpected}'.`,
  197. InvalidParenthesizedAssignment: "Invalid parenthesized assignment pattern.",
  198. InvalidPrivateFieldResolution: ({
  199. identifierName
  200. }) => `Private name #${identifierName} is not defined.`,
  201. InvalidPropertyBindingPattern: "Binding member expression.",
  202. InvalidRecordProperty: "Only properties and spread elements are allowed in record definitions.",
  203. InvalidRestAssignmentPattern: "Invalid rest operator's argument.",
  204. LabelRedeclaration: ({
  205. labelName
  206. }) => `Label '${labelName}' is already declared.`,
  207. LetInLexicalBinding: "'let' is not allowed to be used as a name in 'let' or 'const' declarations.",
  208. LineTerminatorBeforeArrow: "No line break is allowed before '=>'.",
  209. MalformedRegExpFlags: "Invalid regular expression flag.",
  210. MissingClassName: "A class name is required.",
  211. MissingEqInAssignment: "Only '=' operator can be used for specifying default value.",
  212. MissingSemicolon: "Missing semicolon.",
  213. MissingPlugin: ({
  214. missingPlugin
  215. }) => `This experimental syntax requires enabling the parser plugin: ${missingPlugin.map(name => JSON.stringify(name)).join(", ")}.`,
  216. MissingOneOfPlugins: ({
  217. missingPlugin
  218. }) => `This experimental syntax requires enabling one of the following parser plugin(s): ${missingPlugin.map(name => JSON.stringify(name)).join(", ")}.`,
  219. MissingUnicodeEscape: "Expecting Unicode escape sequence \\uXXXX.",
  220. MixingCoalesceWithLogical: "Nullish coalescing operator(??) requires parens when mixing with logical operators.",
  221. ModuleAttributeDifferentFromType: "The only accepted module attribute is `type`.",
  222. ModuleAttributeInvalidValue: "Only string literals are allowed as module attribute values.",
  223. ModuleAttributesWithDuplicateKeys: ({
  224. key
  225. }) => `Duplicate key "${key}" is not allowed in module attributes.`,
  226. ModuleExportNameHasLoneSurrogate: ({
  227. surrogateCharCode
  228. }) => `An export name cannot include a lone surrogate, found '\\u${surrogateCharCode.toString(16)}'.`,
  229. ModuleExportUndefined: ({
  230. localName
  231. }) => `Export '${localName}' is not defined.`,
  232. MultipleDefaultsInSwitch: "Multiple default clauses.",
  233. NewlineAfterThrow: "Illegal newline after throw.",
  234. NoCatchOrFinally: "Missing catch or finally clause.",
  235. NumberIdentifier: "Identifier directly after number.",
  236. NumericSeparatorInEscapeSequence: "Numeric separators are not allowed inside unicode escape sequences or hex escape sequences.",
  237. ObsoleteAwaitStar: "'await*' has been removed from the async functions proposal. Use Promise.all() instead.",
  238. OptionalChainingNoNew: "Constructors in/after an Optional Chain are not allowed.",
  239. OptionalChainingNoTemplate: "Tagged Template Literals are not allowed in optionalChain.",
  240. OverrideOnConstructor: "'override' modifier cannot appear on a constructor declaration.",
  241. ParamDupe: "Argument name clash.",
  242. PatternHasAccessor: "Object pattern can't contain getter or setter.",
  243. PatternHasMethod: "Object pattern can't contain methods.",
  244. PrivateInExpectedIn: ({
  245. identifierName
  246. }) => `Private names are only allowed in property accesses (\`obj.#${identifierName}\`) or in \`in\` expressions (\`#${identifierName} in obj\`).`,
  247. PrivateNameRedeclaration: ({
  248. identifierName
  249. }) => `Duplicate private name #${identifierName}.`,
  250. RecordExpressionBarIncorrectEndSyntaxType: "Record expressions ending with '|}' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'bar'.",
  251. RecordExpressionBarIncorrectStartSyntaxType: "Record expressions starting with '{|' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'bar'.",
  252. RecordExpressionHashIncorrectStartSyntaxType: "Record expressions starting with '#{' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'hash'.",
  253. RecordNoProto: "'__proto__' is not allowed in Record expressions.",
  254. RestTrailingComma: "Unexpected trailing comma after rest element.",
  255. SloppyFunction: "In non-strict mode code, functions can only be declared at top level or inside a block.",
  256. SloppyFunctionAnnexB: "In non-strict mode code, functions can only be declared at top level, inside a block, or as the body of an if statement.",
  257. StaticPrototype: "Classes may not have static property named prototype.",
  258. 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?",
  259. SuperPrivateField: "Private fields can't be accessed on super.",
  260. TrailingDecorator: "Decorators must be attached to a class element.",
  261. TupleExpressionBarIncorrectEndSyntaxType: "Tuple expressions ending with '|]' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'bar'.",
  262. TupleExpressionBarIncorrectStartSyntaxType: "Tuple expressions starting with '[|' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'bar'.",
  263. TupleExpressionHashIncorrectStartSyntaxType: "Tuple expressions starting with '#[' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'hash'.",
  264. UnexpectedArgumentPlaceholder: "Unexpected argument placeholder.",
  265. UnexpectedAwaitAfterPipelineBody: 'Unexpected "await" after pipeline body; await must have parentheses in minimal proposal.',
  266. UnexpectedDigitAfterHash: "Unexpected digit after hash token.",
  267. UnexpectedImportExport: "'import' and 'export' may only appear at the top level.",
  268. UnexpectedKeyword: ({
  269. keyword
  270. }) => `Unexpected keyword '${keyword}'.`,
  271. UnexpectedLeadingDecorator: "Leading decorators must be attached to a class declaration.",
  272. UnexpectedLexicalDeclaration: "Lexical declaration cannot appear in a single-statement context.",
  273. UnexpectedNewTarget: "`new.target` can only be used in functions or class properties.",
  274. UnexpectedNumericSeparator: "A numeric separator is only allowed between two digits.",
  275. UnexpectedPrivateField: "Unexpected private name.",
  276. UnexpectedReservedWord: ({
  277. reservedWord
  278. }) => `Unexpected reserved word '${reservedWord}'.`,
  279. UnexpectedSuper: "'super' is only allowed in object methods and classes.",
  280. UnexpectedToken: ({
  281. expected,
  282. unexpected
  283. }) => `Unexpected token${unexpected ? ` '${unexpected}'.` : ""}${expected ? `, expected "${expected}"` : ""}`,
  284. UnexpectedTokenUnaryExponentiation: "Illegal expression. Wrap left hand side or entire exponentiation in parentheses.",
  285. UnexpectedUsingDeclaration: "Using declaration cannot appear in the top level when source type is `script`.",
  286. UnsupportedBind: "Binding should be performed on object property.",
  287. UnsupportedDecoratorExport: "A decorated export must export a class declaration.",
  288. UnsupportedDefaultExport: "Only expressions, functions or classes are allowed as the `default` export.",
  289. UnsupportedImport: "`import` can only be used in `import()` or `import.meta`.",
  290. UnsupportedMetaProperty: ({
  291. target,
  292. onlyValidPropertyName
  293. }) => `The only valid meta property for ${target} is ${target}.${onlyValidPropertyName}.`,
  294. UnsupportedParameterDecorator: "Decorators cannot be used to decorate parameters.",
  295. UnsupportedPropertyDecorator: "Decorators cannot be used to decorate object literal properties.",
  296. UnsupportedSuper: "'super' can only be used with function calls (i.e. super()) or in property accesses (i.e. super.prop or super[prop]).",
  297. UnterminatedComment: "Unterminated comment.",
  298. UnterminatedRegExp: "Unterminated regular expression.",
  299. UnterminatedString: "Unterminated string constant.",
  300. UnterminatedTemplate: "Unterminated template.",
  301. UsingDeclarationHasBindingPattern: "Using declaration cannot have destructuring patterns.",
  302. VarRedeclaration: ({
  303. identifierName
  304. }) => `Identifier '${identifierName}' has already been declared.`,
  305. YieldBindingIdentifier: "Can not use 'yield' as identifier inside a generator.",
  306. YieldInParameter: "Yield expression is not allowed in formal parameters.",
  307. ZeroDigitNumericSeparator: "Numeric separator can not be used after leading 0."
  308. };
  309. var StrictModeErrors = {
  310. StrictDelete: "Deleting local variable in strict mode.",
  311. StrictEvalArguments: ({
  312. referenceName
  313. }) => `Assigning to '${referenceName}' in strict mode.`,
  314. StrictEvalArgumentsBinding: ({
  315. bindingName
  316. }) => `Binding '${bindingName}' in strict mode.`,
  317. StrictFunction: "In strict mode code, functions can only be declared at top level or inside a block.",
  318. StrictNumericEscape: "The only valid numeric escape in strict mode is '\\0'.",
  319. StrictOctalLiteral: "Legacy octal literals are not allowed in strict mode.",
  320. StrictWith: "'with' in strict mode."
  321. };
  322. const UnparenthesizedPipeBodyDescriptions = new Set(["ArrowFunctionExpression", "AssignmentExpression", "ConditionalExpression", "YieldExpression"]);
  323. var PipelineOperatorErrors = {
  324. PipeBodyIsTighter: "Unexpected yield after pipeline body; any yield expression acting as Hack-style pipe body must be parenthesized due to its loose operator precedence.",
  325. PipeTopicRequiresHackPipes: 'Topic reference is used, but the pipelineOperator plugin was not passed a "proposal": "hack" or "smart" option.',
  326. PipeTopicUnbound: "Topic reference is unbound; it must be inside a pipe body.",
  327. PipeTopicUnconfiguredToken: ({
  328. token
  329. }) => `Invalid topic token ${token}. In order to use ${token} as a topic reference, the pipelineOperator plugin must be configured with { "proposal": "hack", "topicToken": "${token}" }.`,
  330. PipeTopicUnused: "Hack-style pipe body does not contain a topic reference; Hack-style pipes must use topic at least once.",
  331. PipeUnparenthesizedBody: ({
  332. type
  333. }) => `Hack-style pipe body cannot be an unparenthesized ${toNodeDescription({
  334. type
  335. })}; please wrap it in parentheses.`,
  336. PipelineBodyNoArrow: 'Unexpected arrow "=>" after pipeline body; arrow function in pipeline body must be parenthesized.',
  337. PipelineBodySequenceExpression: "Pipeline body may not be a comma-separated sequence expression.",
  338. PipelineHeadSequenceExpression: "Pipeline head should not be a comma-separated sequence expression.",
  339. PipelineTopicUnused: "Pipeline is in topic style but does not use topic reference.",
  340. PrimaryTopicNotAllowed: "Topic reference was used in a lexical context without topic binding.",
  341. PrimaryTopicRequiresSmartPipeline: 'Topic reference is used, but the pipelineOperator plugin was not passed a "proposal": "hack" or "smart" option.'
  342. };
  343. const _excluded$1 = ["toMessage"],
  344. _excluded2$1 = ["message"];
  345. function toParseErrorConstructor(_ref) {
  346. let {
  347. toMessage
  348. } = _ref,
  349. properties = _objectWithoutPropertiesLoose(_ref, _excluded$1);
  350. return function constructor({
  351. loc,
  352. details
  353. }) {
  354. return instantiate(SyntaxError, Object.assign({}, properties, {
  355. loc
  356. }), {
  357. clone(overrides = {}) {
  358. const loc = overrides.loc || {};
  359. return constructor({
  360. loc: new Position("line" in loc ? loc.line : this.loc.line, "column" in loc ? loc.column : this.loc.column, "index" in loc ? loc.index : this.loc.index),
  361. details: Object.assign({}, this.details, overrides.details)
  362. });
  363. },
  364. details: {
  365. value: details,
  366. enumerable: false
  367. },
  368. message: {
  369. get() {
  370. return `${toMessage(this.details)} (${this.loc.line}:${this.loc.column})`;
  371. },
  372. set(value) {
  373. Object.defineProperty(this, "message", {
  374. value
  375. });
  376. }
  377. },
  378. pos: {
  379. reflect: "loc.index",
  380. enumerable: true
  381. },
  382. missingPlugin: "missingPlugin" in details && {
  383. reflect: "details.missingPlugin",
  384. enumerable: true
  385. }
  386. });
  387. };
  388. }
  389. function ParseErrorEnum(argument, syntaxPlugin) {
  390. if (Array.isArray(argument)) {
  391. return parseErrorTemplates => ParseErrorEnum(parseErrorTemplates, argument[0]);
  392. }
  393. const ParseErrorConstructors = {};
  394. for (const reasonCode of Object.keys(argument)) {
  395. const template = argument[reasonCode];
  396. const _ref2 = typeof template === "string" ? {
  397. message: () => template
  398. } : typeof template === "function" ? {
  399. message: template
  400. } : template,
  401. {
  402. message
  403. } = _ref2,
  404. rest = _objectWithoutPropertiesLoose(_ref2, _excluded2$1);
  405. const toMessage = typeof message === "string" ? () => message : message;
  406. ParseErrorConstructors[reasonCode] = toParseErrorConstructor(Object.assign({
  407. code: ParseErrorCode.SyntaxError,
  408. reasonCode,
  409. toMessage
  410. }, syntaxPlugin ? {
  411. syntaxPlugin
  412. } : {}, rest));
  413. }
  414. return ParseErrorConstructors;
  415. }
  416. const Errors = Object.assign({}, ParseErrorEnum(ModuleErrors), ParseErrorEnum(StandardErrors), ParseErrorEnum(StrictModeErrors), ParseErrorEnum`pipelineOperator`(PipelineOperatorErrors));
  417. const {
  418. defineProperty
  419. } = Object;
  420. const toUnenumerable = (object, key) => defineProperty(object, key, {
  421. enumerable: false,
  422. value: object[key]
  423. });
  424. function toESTreeLocation(node) {
  425. node.loc.start && toUnenumerable(node.loc.start, "index");
  426. node.loc.end && toUnenumerable(node.loc.end, "index");
  427. return node;
  428. }
  429. var estree = (superClass => class ESTreeParserMixin extends superClass {
  430. parse() {
  431. const file = toESTreeLocation(super.parse());
  432. if (this.options.tokens) {
  433. file.tokens = file.tokens.map(toESTreeLocation);
  434. }
  435. return file;
  436. }
  437. parseRegExpLiteral({
  438. pattern,
  439. flags
  440. }) {
  441. let regex = null;
  442. try {
  443. regex = new RegExp(pattern, flags);
  444. } catch (e) {}
  445. const node = this.estreeParseLiteral(regex);
  446. node.regex = {
  447. pattern,
  448. flags
  449. };
  450. return node;
  451. }
  452. parseBigIntLiteral(value) {
  453. let bigInt;
  454. try {
  455. bigInt = BigInt(value);
  456. } catch (_unused) {
  457. bigInt = null;
  458. }
  459. const node = this.estreeParseLiteral(bigInt);
  460. node.bigint = String(node.value || value);
  461. return node;
  462. }
  463. parseDecimalLiteral(value) {
  464. const decimal = null;
  465. const node = this.estreeParseLiteral(decimal);
  466. node.decimal = String(node.value || value);
  467. return node;
  468. }
  469. estreeParseLiteral(value) {
  470. return this.parseLiteral(value, "Literal");
  471. }
  472. parseStringLiteral(value) {
  473. return this.estreeParseLiteral(value);
  474. }
  475. parseNumericLiteral(value) {
  476. return this.estreeParseLiteral(value);
  477. }
  478. parseNullLiteral() {
  479. return this.estreeParseLiteral(null);
  480. }
  481. parseBooleanLiteral(value) {
  482. return this.estreeParseLiteral(value);
  483. }
  484. directiveToStmt(directive) {
  485. const expression = directive.value;
  486. delete directive.value;
  487. expression.type = "Literal";
  488. expression.raw = expression.extra.raw;
  489. expression.value = expression.extra.expressionValue;
  490. const stmt = directive;
  491. stmt.type = "ExpressionStatement";
  492. stmt.expression = expression;
  493. stmt.directive = expression.extra.rawValue;
  494. delete expression.extra;
  495. return stmt;
  496. }
  497. initFunction(node, isAsync) {
  498. super.initFunction(node, isAsync);
  499. node.expression = false;
  500. }
  501. checkDeclaration(node) {
  502. if (node != null && this.isObjectProperty(node)) {
  503. this.checkDeclaration(node.value);
  504. } else {
  505. super.checkDeclaration(node);
  506. }
  507. }
  508. getObjectOrClassMethodParams(method) {
  509. return method.value.params;
  510. }
  511. isValidDirective(stmt) {
  512. var _stmt$expression$extr;
  513. return stmt.type === "ExpressionStatement" && stmt.expression.type === "Literal" && typeof stmt.expression.value === "string" && !((_stmt$expression$extr = stmt.expression.extra) != null && _stmt$expression$extr.parenthesized);
  514. }
  515. parseBlockBody(node, allowDirectives, topLevel, end, afterBlockParse) {
  516. super.parseBlockBody(node, allowDirectives, topLevel, end, afterBlockParse);
  517. const directiveStatements = node.directives.map(d => this.directiveToStmt(d));
  518. node.body = directiveStatements.concat(node.body);
  519. delete node.directives;
  520. }
  521. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  522. this.parseMethod(method, isGenerator, isAsync, isConstructor, allowsDirectSuper, "ClassMethod", true);
  523. if (method.typeParameters) {
  524. method.value.typeParameters = method.typeParameters;
  525. delete method.typeParameters;
  526. }
  527. classBody.body.push(method);
  528. }
  529. parsePrivateName() {
  530. const node = super.parsePrivateName();
  531. {
  532. if (!this.getPluginOption("estree", "classFeatures")) {
  533. return node;
  534. }
  535. }
  536. return this.convertPrivateNameToPrivateIdentifier(node);
  537. }
  538. convertPrivateNameToPrivateIdentifier(node) {
  539. const name = super.getPrivateNameSV(node);
  540. node = node;
  541. delete node.id;
  542. node.name = name;
  543. node.type = "PrivateIdentifier";
  544. return node;
  545. }
  546. isPrivateName(node) {
  547. {
  548. if (!this.getPluginOption("estree", "classFeatures")) {
  549. return super.isPrivateName(node);
  550. }
  551. }
  552. return node.type === "PrivateIdentifier";
  553. }
  554. getPrivateNameSV(node) {
  555. {
  556. if (!this.getPluginOption("estree", "classFeatures")) {
  557. return super.getPrivateNameSV(node);
  558. }
  559. }
  560. return node.name;
  561. }
  562. parseLiteral(value, type) {
  563. const node = super.parseLiteral(value, type);
  564. node.raw = node.extra.raw;
  565. delete node.extra;
  566. return node;
  567. }
  568. parseFunctionBody(node, allowExpression, isMethod = false) {
  569. super.parseFunctionBody(node, allowExpression, isMethod);
  570. node.expression = node.body.type !== "BlockStatement";
  571. }
  572. parseMethod(node, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope = false) {
  573. let funcNode = this.startNode();
  574. funcNode.kind = node.kind;
  575. funcNode = super.parseMethod(funcNode, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope);
  576. funcNode.type = "FunctionExpression";
  577. delete funcNode.kind;
  578. node.value = funcNode;
  579. if (type === "ClassPrivateMethod") {
  580. node.computed = false;
  581. }
  582. return this.finishNode(node, "MethodDefinition");
  583. }
  584. parseClassProperty(...args) {
  585. const propertyNode = super.parseClassProperty(...args);
  586. {
  587. if (!this.getPluginOption("estree", "classFeatures")) {
  588. return propertyNode;
  589. }
  590. }
  591. propertyNode.type = "PropertyDefinition";
  592. return propertyNode;
  593. }
  594. parseClassPrivateProperty(...args) {
  595. const propertyNode = super.parseClassPrivateProperty(...args);
  596. {
  597. if (!this.getPluginOption("estree", "classFeatures")) {
  598. return propertyNode;
  599. }
  600. }
  601. propertyNode.type = "PropertyDefinition";
  602. propertyNode.computed = false;
  603. return propertyNode;
  604. }
  605. parseObjectMethod(prop, isGenerator, isAsync, isPattern, isAccessor) {
  606. const node = super.parseObjectMethod(prop, isGenerator, isAsync, isPattern, isAccessor);
  607. if (node) {
  608. node.type = "Property";
  609. if (node.kind === "method") {
  610. node.kind = "init";
  611. }
  612. node.shorthand = false;
  613. }
  614. return node;
  615. }
  616. parseObjectProperty(prop, startLoc, isPattern, refExpressionErrors) {
  617. const node = super.parseObjectProperty(prop, startLoc, isPattern, refExpressionErrors);
  618. if (node) {
  619. node.kind = "init";
  620. node.type = "Property";
  621. }
  622. return node;
  623. }
  624. isValidLVal(type, isUnparenthesizedInAssign, binding) {
  625. return type === "Property" ? "value" : super.isValidLVal(type, isUnparenthesizedInAssign, binding);
  626. }
  627. isAssignable(node, isBinding) {
  628. if (node != null && this.isObjectProperty(node)) {
  629. return this.isAssignable(node.value, isBinding);
  630. }
  631. return super.isAssignable(node, isBinding);
  632. }
  633. toAssignable(node, isLHS = false) {
  634. if (node != null && this.isObjectProperty(node)) {
  635. const {
  636. key,
  637. value
  638. } = node;
  639. if (this.isPrivateName(key)) {
  640. this.classScope.usePrivateName(this.getPrivateNameSV(key), key.loc.start);
  641. }
  642. this.toAssignable(value, isLHS);
  643. } else {
  644. super.toAssignable(node, isLHS);
  645. }
  646. }
  647. toAssignableObjectExpressionProp(prop, isLast, isLHS) {
  648. if (prop.kind === "get" || prop.kind === "set") {
  649. this.raise(Errors.PatternHasAccessor, {
  650. at: prop.key
  651. });
  652. } else if (prop.method) {
  653. this.raise(Errors.PatternHasMethod, {
  654. at: prop.key
  655. });
  656. } else {
  657. super.toAssignableObjectExpressionProp(prop, isLast, isLHS);
  658. }
  659. }
  660. finishCallExpression(unfinished, optional) {
  661. const node = super.finishCallExpression(unfinished, optional);
  662. if (node.callee.type === "Import") {
  663. node.type = "ImportExpression";
  664. node.source = node.arguments[0];
  665. if (this.hasPlugin("importAssertions")) {
  666. var _node$arguments$;
  667. node.attributes = (_node$arguments$ = node.arguments[1]) != null ? _node$arguments$ : null;
  668. }
  669. delete node.arguments;
  670. delete node.callee;
  671. }
  672. return node;
  673. }
  674. toReferencedArguments(node) {
  675. if (node.type === "ImportExpression") {
  676. return;
  677. }
  678. super.toReferencedArguments(node);
  679. }
  680. parseExport(unfinished, decorators) {
  681. const exportStartLoc = this.state.lastTokStartLoc;
  682. const node = super.parseExport(unfinished, decorators);
  683. switch (node.type) {
  684. case "ExportAllDeclaration":
  685. node.exported = null;
  686. break;
  687. case "ExportNamedDeclaration":
  688. if (node.specifiers.length === 1 && node.specifiers[0].type === "ExportNamespaceSpecifier") {
  689. node.type = "ExportAllDeclaration";
  690. node.exported = node.specifiers[0].exported;
  691. delete node.specifiers;
  692. }
  693. case "ExportDefaultDeclaration":
  694. {
  695. var _declaration$decorato;
  696. const {
  697. declaration
  698. } = node;
  699. if ((declaration == null ? void 0 : declaration.type) === "ClassDeclaration" && ((_declaration$decorato = declaration.decorators) == null ? void 0 : _declaration$decorato.length) > 0 && declaration.start === node.start) {
  700. this.resetStartLocation(node, exportStartLoc);
  701. }
  702. }
  703. break;
  704. }
  705. return node;
  706. }
  707. parseSubscript(base, startLoc, noCalls, state) {
  708. const node = super.parseSubscript(base, startLoc, noCalls, state);
  709. if (state.optionalChainMember) {
  710. if (node.type === "OptionalMemberExpression" || node.type === "OptionalCallExpression") {
  711. node.type = node.type.substring(8);
  712. }
  713. if (state.stop) {
  714. const chain = this.startNodeAtNode(node);
  715. chain.expression = node;
  716. return this.finishNode(chain, "ChainExpression");
  717. }
  718. } else if (node.type === "MemberExpression" || node.type === "CallExpression") {
  719. node.optional = false;
  720. }
  721. return node;
  722. }
  723. hasPropertyAsPrivateName(node) {
  724. if (node.type === "ChainExpression") {
  725. node = node.expression;
  726. }
  727. return super.hasPropertyAsPrivateName(node);
  728. }
  729. isOptionalChain(node) {
  730. return node.type === "ChainExpression";
  731. }
  732. isObjectProperty(node) {
  733. return node.type === "Property" && node.kind === "init" && !node.method;
  734. }
  735. isObjectMethod(node) {
  736. return node.method || node.kind === "get" || node.kind === "set";
  737. }
  738. finishNodeAt(node, type, endLoc) {
  739. return toESTreeLocation(super.finishNodeAt(node, type, endLoc));
  740. }
  741. resetStartLocation(node, startLoc) {
  742. super.resetStartLocation(node, startLoc);
  743. toESTreeLocation(node);
  744. }
  745. resetEndLocation(node, endLoc = this.state.lastTokEndLoc) {
  746. super.resetEndLocation(node, endLoc);
  747. toESTreeLocation(node);
  748. }
  749. });
  750. class TokContext {
  751. constructor(token, preserveSpace) {
  752. this.token = void 0;
  753. this.preserveSpace = void 0;
  754. this.token = token;
  755. this.preserveSpace = !!preserveSpace;
  756. }
  757. }
  758. const types = {
  759. brace: new TokContext("{"),
  760. j_oTag: new TokContext("<tag"),
  761. j_cTag: new TokContext("</tag"),
  762. j_expr: new TokContext("<tag>...</tag>", true)
  763. };
  764. {
  765. types.template = new TokContext("`", true);
  766. }
  767. const beforeExpr = true;
  768. const startsExpr = true;
  769. const isLoop = true;
  770. const isAssign = true;
  771. const prefix = true;
  772. const postfix = true;
  773. class ExportedTokenType {
  774. constructor(label, conf = {}) {
  775. this.label = void 0;
  776. this.keyword = void 0;
  777. this.beforeExpr = void 0;
  778. this.startsExpr = void 0;
  779. this.rightAssociative = void 0;
  780. this.isLoop = void 0;
  781. this.isAssign = void 0;
  782. this.prefix = void 0;
  783. this.postfix = void 0;
  784. this.binop = void 0;
  785. this.label = label;
  786. this.keyword = conf.keyword;
  787. this.beforeExpr = !!conf.beforeExpr;
  788. this.startsExpr = !!conf.startsExpr;
  789. this.rightAssociative = !!conf.rightAssociative;
  790. this.isLoop = !!conf.isLoop;
  791. this.isAssign = !!conf.isAssign;
  792. this.prefix = !!conf.prefix;
  793. this.postfix = !!conf.postfix;
  794. this.binop = conf.binop != null ? conf.binop : null;
  795. {
  796. this.updateContext = null;
  797. }
  798. }
  799. }
  800. const keywords$1 = new Map();
  801. function createKeyword(name, options = {}) {
  802. options.keyword = name;
  803. const token = createToken(name, options);
  804. keywords$1.set(name, token);
  805. return token;
  806. }
  807. function createBinop(name, binop) {
  808. return createToken(name, {
  809. beforeExpr,
  810. binop
  811. });
  812. }
  813. let tokenTypeCounter = -1;
  814. const tokenTypes = [];
  815. const tokenLabels = [];
  816. const tokenBinops = [];
  817. const tokenBeforeExprs = [];
  818. const tokenStartsExprs = [];
  819. const tokenPrefixes = [];
  820. function createToken(name, options = {}) {
  821. var _options$binop, _options$beforeExpr, _options$startsExpr, _options$prefix;
  822. ++tokenTypeCounter;
  823. tokenLabels.push(name);
  824. tokenBinops.push((_options$binop = options.binop) != null ? _options$binop : -1);
  825. tokenBeforeExprs.push((_options$beforeExpr = options.beforeExpr) != null ? _options$beforeExpr : false);
  826. tokenStartsExprs.push((_options$startsExpr = options.startsExpr) != null ? _options$startsExpr : false);
  827. tokenPrefixes.push((_options$prefix = options.prefix) != null ? _options$prefix : false);
  828. tokenTypes.push(new ExportedTokenType(name, options));
  829. return tokenTypeCounter;
  830. }
  831. function createKeywordLike(name, options = {}) {
  832. var _options$binop2, _options$beforeExpr2, _options$startsExpr2, _options$prefix2;
  833. ++tokenTypeCounter;
  834. keywords$1.set(name, tokenTypeCounter);
  835. tokenLabels.push(name);
  836. tokenBinops.push((_options$binop2 = options.binop) != null ? _options$binop2 : -1);
  837. tokenBeforeExprs.push((_options$beforeExpr2 = options.beforeExpr) != null ? _options$beforeExpr2 : false);
  838. tokenStartsExprs.push((_options$startsExpr2 = options.startsExpr) != null ? _options$startsExpr2 : false);
  839. tokenPrefixes.push((_options$prefix2 = options.prefix) != null ? _options$prefix2 : false);
  840. tokenTypes.push(new ExportedTokenType("name", options));
  841. return tokenTypeCounter;
  842. }
  843. const tt = {
  844. bracketL: createToken("[", {
  845. beforeExpr,
  846. startsExpr
  847. }),
  848. bracketHashL: createToken("#[", {
  849. beforeExpr,
  850. startsExpr
  851. }),
  852. bracketBarL: createToken("[|", {
  853. beforeExpr,
  854. startsExpr
  855. }),
  856. bracketR: createToken("]"),
  857. bracketBarR: createToken("|]"),
  858. braceL: createToken("{", {
  859. beforeExpr,
  860. startsExpr
  861. }),
  862. braceBarL: createToken("{|", {
  863. beforeExpr,
  864. startsExpr
  865. }),
  866. braceHashL: createToken("#{", {
  867. beforeExpr,
  868. startsExpr
  869. }),
  870. braceR: createToken("}"),
  871. braceBarR: createToken("|}"),
  872. parenL: createToken("(", {
  873. beforeExpr,
  874. startsExpr
  875. }),
  876. parenR: createToken(")"),
  877. comma: createToken(",", {
  878. beforeExpr
  879. }),
  880. semi: createToken(";", {
  881. beforeExpr
  882. }),
  883. colon: createToken(":", {
  884. beforeExpr
  885. }),
  886. doubleColon: createToken("::", {
  887. beforeExpr
  888. }),
  889. dot: createToken("."),
  890. question: createToken("?", {
  891. beforeExpr
  892. }),
  893. questionDot: createToken("?."),
  894. arrow: createToken("=>", {
  895. beforeExpr
  896. }),
  897. template: createToken("template"),
  898. ellipsis: createToken("...", {
  899. beforeExpr
  900. }),
  901. backQuote: createToken("`", {
  902. startsExpr
  903. }),
  904. dollarBraceL: createToken("${", {
  905. beforeExpr,
  906. startsExpr
  907. }),
  908. templateTail: createToken("...`", {
  909. startsExpr
  910. }),
  911. templateNonTail: createToken("...${", {
  912. beforeExpr,
  913. startsExpr
  914. }),
  915. at: createToken("@"),
  916. hash: createToken("#", {
  917. startsExpr
  918. }),
  919. interpreterDirective: createToken("#!..."),
  920. eq: createToken("=", {
  921. beforeExpr,
  922. isAssign
  923. }),
  924. assign: createToken("_=", {
  925. beforeExpr,
  926. isAssign
  927. }),
  928. slashAssign: createToken("_=", {
  929. beforeExpr,
  930. isAssign
  931. }),
  932. xorAssign: createToken("_=", {
  933. beforeExpr,
  934. isAssign
  935. }),
  936. moduloAssign: createToken("_=", {
  937. beforeExpr,
  938. isAssign
  939. }),
  940. incDec: createToken("++/--", {
  941. prefix,
  942. postfix,
  943. startsExpr
  944. }),
  945. bang: createToken("!", {
  946. beforeExpr,
  947. prefix,
  948. startsExpr
  949. }),
  950. tilde: createToken("~", {
  951. beforeExpr,
  952. prefix,
  953. startsExpr
  954. }),
  955. doubleCaret: createToken("^^", {
  956. startsExpr
  957. }),
  958. doubleAt: createToken("@@", {
  959. startsExpr
  960. }),
  961. pipeline: createBinop("|>", 0),
  962. nullishCoalescing: createBinop("??", 1),
  963. logicalOR: createBinop("||", 1),
  964. logicalAND: createBinop("&&", 2),
  965. bitwiseOR: createBinop("|", 3),
  966. bitwiseXOR: createBinop("^", 4),
  967. bitwiseAND: createBinop("&", 5),
  968. equality: createBinop("==/!=/===/!==", 6),
  969. lt: createBinop("</>/<=/>=", 7),
  970. gt: createBinop("</>/<=/>=", 7),
  971. relational: createBinop("</>/<=/>=", 7),
  972. bitShift: createBinop("<</>>/>>>", 8),
  973. bitShiftL: createBinop("<</>>/>>>", 8),
  974. bitShiftR: createBinop("<</>>/>>>", 8),
  975. plusMin: createToken("+/-", {
  976. beforeExpr,
  977. binop: 9,
  978. prefix,
  979. startsExpr
  980. }),
  981. modulo: createToken("%", {
  982. binop: 10,
  983. startsExpr
  984. }),
  985. star: createToken("*", {
  986. binop: 10
  987. }),
  988. slash: createBinop("/", 10),
  989. exponent: createToken("**", {
  990. beforeExpr,
  991. binop: 11,
  992. rightAssociative: true
  993. }),
  994. _in: createKeyword("in", {
  995. beforeExpr,
  996. binop: 7
  997. }),
  998. _instanceof: createKeyword("instanceof", {
  999. beforeExpr,
  1000. binop: 7
  1001. }),
  1002. _break: createKeyword("break"),
  1003. _case: createKeyword("case", {
  1004. beforeExpr
  1005. }),
  1006. _catch: createKeyword("catch"),
  1007. _continue: createKeyword("continue"),
  1008. _debugger: createKeyword("debugger"),
  1009. _default: createKeyword("default", {
  1010. beforeExpr
  1011. }),
  1012. _else: createKeyword("else", {
  1013. beforeExpr
  1014. }),
  1015. _finally: createKeyword("finally"),
  1016. _function: createKeyword("function", {
  1017. startsExpr
  1018. }),
  1019. _if: createKeyword("if"),
  1020. _return: createKeyword("return", {
  1021. beforeExpr
  1022. }),
  1023. _switch: createKeyword("switch"),
  1024. _throw: createKeyword("throw", {
  1025. beforeExpr,
  1026. prefix,
  1027. startsExpr
  1028. }),
  1029. _try: createKeyword("try"),
  1030. _var: createKeyword("var"),
  1031. _const: createKeyword("const"),
  1032. _with: createKeyword("with"),
  1033. _new: createKeyword("new", {
  1034. beforeExpr,
  1035. startsExpr
  1036. }),
  1037. _this: createKeyword("this", {
  1038. startsExpr
  1039. }),
  1040. _super: createKeyword("super", {
  1041. startsExpr
  1042. }),
  1043. _class: createKeyword("class", {
  1044. startsExpr
  1045. }),
  1046. _extends: createKeyword("extends", {
  1047. beforeExpr
  1048. }),
  1049. _export: createKeyword("export"),
  1050. _import: createKeyword("import", {
  1051. startsExpr
  1052. }),
  1053. _null: createKeyword("null", {
  1054. startsExpr
  1055. }),
  1056. _true: createKeyword("true", {
  1057. startsExpr
  1058. }),
  1059. _false: createKeyword("false", {
  1060. startsExpr
  1061. }),
  1062. _typeof: createKeyword("typeof", {
  1063. beforeExpr,
  1064. prefix,
  1065. startsExpr
  1066. }),
  1067. _void: createKeyword("void", {
  1068. beforeExpr,
  1069. prefix,
  1070. startsExpr
  1071. }),
  1072. _delete: createKeyword("delete", {
  1073. beforeExpr,
  1074. prefix,
  1075. startsExpr
  1076. }),
  1077. _do: createKeyword("do", {
  1078. isLoop,
  1079. beforeExpr
  1080. }),
  1081. _for: createKeyword("for", {
  1082. isLoop
  1083. }),
  1084. _while: createKeyword("while", {
  1085. isLoop
  1086. }),
  1087. _as: createKeywordLike("as", {
  1088. startsExpr
  1089. }),
  1090. _assert: createKeywordLike("assert", {
  1091. startsExpr
  1092. }),
  1093. _async: createKeywordLike("async", {
  1094. startsExpr
  1095. }),
  1096. _await: createKeywordLike("await", {
  1097. startsExpr
  1098. }),
  1099. _from: createKeywordLike("from", {
  1100. startsExpr
  1101. }),
  1102. _get: createKeywordLike("get", {
  1103. startsExpr
  1104. }),
  1105. _let: createKeywordLike("let", {
  1106. startsExpr
  1107. }),
  1108. _meta: createKeywordLike("meta", {
  1109. startsExpr
  1110. }),
  1111. _of: createKeywordLike("of", {
  1112. startsExpr
  1113. }),
  1114. _sent: createKeywordLike("sent", {
  1115. startsExpr
  1116. }),
  1117. _set: createKeywordLike("set", {
  1118. startsExpr
  1119. }),
  1120. _static: createKeywordLike("static", {
  1121. startsExpr
  1122. }),
  1123. _using: createKeywordLike("using", {
  1124. startsExpr
  1125. }),
  1126. _yield: createKeywordLike("yield", {
  1127. startsExpr
  1128. }),
  1129. _asserts: createKeywordLike("asserts", {
  1130. startsExpr
  1131. }),
  1132. _checks: createKeywordLike("checks", {
  1133. startsExpr
  1134. }),
  1135. _exports: createKeywordLike("exports", {
  1136. startsExpr
  1137. }),
  1138. _global: createKeywordLike("global", {
  1139. startsExpr
  1140. }),
  1141. _implements: createKeywordLike("implements", {
  1142. startsExpr
  1143. }),
  1144. _intrinsic: createKeywordLike("intrinsic", {
  1145. startsExpr
  1146. }),
  1147. _infer: createKeywordLike("infer", {
  1148. startsExpr
  1149. }),
  1150. _is: createKeywordLike("is", {
  1151. startsExpr
  1152. }),
  1153. _mixins: createKeywordLike("mixins", {
  1154. startsExpr
  1155. }),
  1156. _proto: createKeywordLike("proto", {
  1157. startsExpr
  1158. }),
  1159. _require: createKeywordLike("require", {
  1160. startsExpr
  1161. }),
  1162. _satisfies: createKeywordLike("satisfies", {
  1163. startsExpr
  1164. }),
  1165. _keyof: createKeywordLike("keyof", {
  1166. startsExpr
  1167. }),
  1168. _readonly: createKeywordLike("readonly", {
  1169. startsExpr
  1170. }),
  1171. _unique: createKeywordLike("unique", {
  1172. startsExpr
  1173. }),
  1174. _abstract: createKeywordLike("abstract", {
  1175. startsExpr
  1176. }),
  1177. _declare: createKeywordLike("declare", {
  1178. startsExpr
  1179. }),
  1180. _enum: createKeywordLike("enum", {
  1181. startsExpr
  1182. }),
  1183. _module: createKeywordLike("module", {
  1184. startsExpr
  1185. }),
  1186. _namespace: createKeywordLike("namespace", {
  1187. startsExpr
  1188. }),
  1189. _interface: createKeywordLike("interface", {
  1190. startsExpr
  1191. }),
  1192. _type: createKeywordLike("type", {
  1193. startsExpr
  1194. }),
  1195. _opaque: createKeywordLike("opaque", {
  1196. startsExpr
  1197. }),
  1198. name: createToken("name", {
  1199. startsExpr
  1200. }),
  1201. string: createToken("string", {
  1202. startsExpr
  1203. }),
  1204. num: createToken("num", {
  1205. startsExpr
  1206. }),
  1207. bigint: createToken("bigint", {
  1208. startsExpr
  1209. }),
  1210. decimal: createToken("decimal", {
  1211. startsExpr
  1212. }),
  1213. regexp: createToken("regexp", {
  1214. startsExpr
  1215. }),
  1216. privateName: createToken("#name", {
  1217. startsExpr
  1218. }),
  1219. eof: createToken("eof"),
  1220. jsxName: createToken("jsxName"),
  1221. jsxText: createToken("jsxText", {
  1222. beforeExpr: true
  1223. }),
  1224. jsxTagStart: createToken("jsxTagStart", {
  1225. startsExpr: true
  1226. }),
  1227. jsxTagEnd: createToken("jsxTagEnd"),
  1228. placeholder: createToken("%%", {
  1229. startsExpr: true
  1230. })
  1231. };
  1232. function tokenIsIdentifier(token) {
  1233. return token >= 93 && token <= 130;
  1234. }
  1235. function tokenKeywordOrIdentifierIsKeyword(token) {
  1236. return token <= 92;
  1237. }
  1238. function tokenIsKeywordOrIdentifier(token) {
  1239. return token >= 58 && token <= 130;
  1240. }
  1241. function tokenIsLiteralPropertyName(token) {
  1242. return token >= 58 && token <= 134;
  1243. }
  1244. function tokenComesBeforeExpression(token) {
  1245. return tokenBeforeExprs[token];
  1246. }
  1247. function tokenCanStartExpression(token) {
  1248. return tokenStartsExprs[token];
  1249. }
  1250. function tokenIsAssignment(token) {
  1251. return token >= 29 && token <= 33;
  1252. }
  1253. function tokenIsFlowInterfaceOrTypeOrOpaque(token) {
  1254. return token >= 127 && token <= 129;
  1255. }
  1256. function tokenIsLoop(token) {
  1257. return token >= 90 && token <= 92;
  1258. }
  1259. function tokenIsKeyword(token) {
  1260. return token >= 58 && token <= 92;
  1261. }
  1262. function tokenIsOperator(token) {
  1263. return token >= 39 && token <= 59;
  1264. }
  1265. function tokenIsPostfix(token) {
  1266. return token === 34;
  1267. }
  1268. function tokenIsPrefix(token) {
  1269. return tokenPrefixes[token];
  1270. }
  1271. function tokenIsTSTypeOperator(token) {
  1272. return token >= 119 && token <= 121;
  1273. }
  1274. function tokenIsTSDeclarationStart(token) {
  1275. return token >= 122 && token <= 128;
  1276. }
  1277. function tokenLabelName(token) {
  1278. return tokenLabels[token];
  1279. }
  1280. function tokenOperatorPrecedence(token) {
  1281. return tokenBinops[token];
  1282. }
  1283. function tokenIsRightAssociative(token) {
  1284. return token === 57;
  1285. }
  1286. function tokenIsTemplate(token) {
  1287. return token >= 24 && token <= 25;
  1288. }
  1289. function getExportedToken(token) {
  1290. return tokenTypes[token];
  1291. }
  1292. {
  1293. tokenTypes[8].updateContext = context => {
  1294. context.pop();
  1295. };
  1296. tokenTypes[5].updateContext = tokenTypes[7].updateContext = tokenTypes[23].updateContext = context => {
  1297. context.push(types.brace);
  1298. };
  1299. tokenTypes[22].updateContext = context => {
  1300. if (context[context.length - 1] === types.template) {
  1301. context.pop();
  1302. } else {
  1303. context.push(types.template);
  1304. }
  1305. };
  1306. tokenTypes[140].updateContext = context => {
  1307. context.push(types.j_expr, types.j_oTag);
  1308. };
  1309. }
  1310. let nonASCIIidentifierStartChars = "\xaa\xb5\xba\xc0-\xd6\xd8-\xf6\xf8-\u02c1\u02c6-\u02d1\u02e0-\u02e4\u02ec\u02ee\u0370-\u0374\u0376\u0377\u037a-\u037d\u037f\u0386\u0388-\u038a\u038c\u038e-\u03a1\u03a3-\u03f5\u03f7-\u0481\u048a-\u052f\u0531-\u0556\u0559\u0560-\u0588\u05d0-\u05ea\u05ef-\u05f2\u0620-\u064a\u066e\u066f\u0671-\u06d3\u06d5\u06e5\u06e6\u06ee\u06ef\u06fa-\u06fc\u06ff\u0710\u0712-\u072f\u074d-\u07a5\u07b1\u07ca-\u07ea\u07f4\u07f5\u07fa\u0800-\u0815\u081a\u0824\u0828\u0840-\u0858\u0860-\u086a\u0870-\u0887\u0889-\u088e\u08a0-\u08c9\u0904-\u0939\u093d\u0950\u0958-\u0961\u0971-\u0980\u0985-\u098c\u098f\u0990\u0993-\u09a8\u09aa-\u09b0\u09b2\u09b6-\u09b9\u09bd\u09ce\u09dc\u09dd\u09df-\u09e1\u09f0\u09f1\u09fc\u0a05-\u0a0a\u0a0f\u0a10\u0a13-\u0a28\u0a2a-\u0a30\u0a32\u0a33\u0a35\u0a36\u0a38\u0a39\u0a59-\u0a5c\u0a5e\u0a72-\u0a74\u0a85-\u0a8d\u0a8f-\u0a91\u0a93-\u0aa8\u0aaa-\u0ab0\u0ab2\u0ab3\u0ab5-\u0ab9\u0abd\u0ad0\u0ae0\u0ae1\u0af9\u0b05-\u0b0c\u0b0f\u0b10\u0b13-\u0b28\u0b2a-\u0b30\u0b32\u0b33\u0b35-\u0b39\u0b3d\u0b5c\u0b5d\u0b5f-\u0b61\u0b71\u0b83\u0b85-\u0b8a\u0b8e-\u0b90\u0b92-\u0b95\u0b99\u0b9a\u0b9c\u0b9e\u0b9f\u0ba3\u0ba4\u0ba8-\u0baa\u0bae-\u0bb9\u0bd0\u0c05-\u0c0c\u0c0e-\u0c10\u0c12-\u0c28\u0c2a-\u0c39\u0c3d\u0c58-\u0c5a\u0c5d\u0c60\u0c61\u0c80\u0c85-\u0c8c\u0c8e-\u0c90\u0c92-\u0ca8\u0caa-\u0cb3\u0cb5-\u0cb9\u0cbd\u0cdd\u0cde\u0ce0\u0ce1\u0cf1\u0cf2\u0d04-\u0d0c\u0d0e-\u0d10\u0d12-\u0d3a\u0d3d\u0d4e\u0d54-\u0d56\u0d5f-\u0d61\u0d7a-\u0d7f\u0d85-\u0d96\u0d9a-\u0db1\u0db3-\u0dbb\u0dbd\u0dc0-\u0dc6\u0e01-\u0e30\u0e32\u0e33\u0e40-\u0e46\u0e81\u0e82\u0e84\u0e86-\u0e8a\u0e8c-\u0ea3\u0ea5\u0ea7-\u0eb0\u0eb2\u0eb3\u0ebd\u0ec0-\u0ec4\u0ec6\u0edc-\u0edf\u0f00\u0f40-\u0f47\u0f49-\u0f6c\u0f88-\u0f8c\u1000-\u102a\u103f\u1050-\u1055\u105a-\u105d\u1061\u1065\u1066\u106e-\u1070\u1075-\u1081\u108e\u10a0-\u10c5\u10c7\u10cd\u10d0-\u10fa\u10fc-\u1248\u124a-\u124d\u1250-\u1256\u1258\u125a-\u125d\u1260-\u1288\u128a-\u128d\u1290-\u12b0\u12b2-\u12b5\u12b8-\u12be\u12c0\u12c2-\u12c5\u12c8-\u12d6\u12d8-\u1310\u1312-\u1315\u1318-\u135a\u1380-\u138f\u13a0-\u13f5\u13f8-\u13fd\u1401-\u166c\u166f-\u167f\u1681-\u169a\u16a0-\u16ea\u16ee-\u16f8\u1700-\u1711\u171f-\u1731\u1740-\u1751\u1760-\u176c\u176e-\u1770\u1780-\u17b3\u17d7\u17dc\u1820-\u1878\u1880-\u18a8\u18aa\u18b0-\u18f5\u1900-\u191e\u1950-\u196d\u1970-\u1974\u1980-\u19ab\u19b0-\u19c9\u1a00-\u1a16\u1a20-\u1a54\u1aa7\u1b05-\u1b33\u1b45-\u1b4c\u1b83-\u1ba0\u1bae\u1baf\u1bba-\u1be5\u1c00-\u1c23\u1c4d-\u1c4f\u1c5a-\u1c7d\u1c80-\u1c88\u1c90-\u1cba\u1cbd-\u1cbf\u1ce9-\u1cec\u1cee-\u1cf3\u1cf5\u1cf6\u1cfa\u1d00-\u1dbf\u1e00-\u1f15\u1f18-\u1f1d\u1f20-\u1f45\u1f48-\u1f4d\u1f50-\u1f57\u1f59\u1f5b\u1f5d\u1f5f-\u1f7d\u1f80-\u1fb4\u1fb6-\u1fbc\u1fbe\u1fc2-\u1fc4\u1fc6-\u1fcc\u1fd0-\u1fd3\u1fd6-\u1fdb\u1fe0-\u1fec\u1ff2-\u1ff4\u1ff6-\u1ffc\u2071\u207f\u2090-\u209c\u2102\u2107\u210a-\u2113\u2115\u2118-\u211d\u2124\u2126\u2128\u212a-\u2139\u213c-\u213f\u2145-\u2149\u214e\u2160-\u2188\u2c00-\u2ce4\u2ceb-\u2cee\u2cf2\u2cf3\u2d00-\u2d25\u2d27\u2d2d\u2d30-\u2d67\u2d6f\u2d80-\u2d96\u2da0-\u2da6\u2da8-\u2dae\u2db0-\u2db6\u2db8-\u2dbe\u2dc0-\u2dc6\u2dc8-\u2dce\u2dd0-\u2dd6\u2dd8-\u2dde\u3005-\u3007\u3021-\u3029\u3031-\u3035\u3038-\u303c\u3041-\u3096\u309b-\u309f\u30a1-\u30fa\u30fc-\u30ff\u3105-\u312f\u3131-\u318e\u31a0-\u31bf\u31f0-\u31ff\u3400-\u4dbf\u4e00-\ua48c\ua4d0-\ua4fd\ua500-\ua60c\ua610-\ua61f\ua62a\ua62b\ua640-\ua66e\ua67f-\ua69d\ua6a0-\ua6ef\ua717-\ua71f\ua722-\ua788\ua78b-\ua7ca\ua7d0\ua7d1\ua7d3\ua7d5-\ua7d9\ua7f2-\ua801\ua803-\ua805\ua807-\ua80a\ua80c-\ua822\ua840-\ua873\ua882-\ua8b3\ua8f2-\ua8f7\ua8fb\ua8fd\ua8fe\ua90a-\ua925\ua930-\ua946\ua960-\ua97c\ua984-\ua9b2\ua9cf\ua9e0-\ua9e4\ua9e6-\ua9ef\ua9fa-\ua9fe\uaa00-\uaa28\uaa40-\uaa42\uaa44-\uaa4b\uaa60-\uaa76\uaa7a\uaa7e-\uaaaf\uaab1\uaab5\uaab6\uaab9-\uaabd\uaac0\uaac2\uaadb-\uaadd\uaae0-\uaaea\uaaf2-\uaaf4\uab01-\uab06\uab09-\uab0e\uab11-\uab16\uab20-\uab26\uab28-\uab2e\uab30-\uab5a\uab5c-\uab69\uab70-\uabe2\uac00-\ud7a3\ud7b0-\ud7c6\ud7cb-\ud7fb\uf900-\ufa6d\ufa70-\ufad9\ufb00-\ufb06\ufb13-\ufb17\ufb1d\ufb1f-\ufb28\ufb2a-\ufb36\ufb38-\ufb3c\ufb3e\ufb40\ufb41\ufb43\ufb44\ufb46-\ufbb1\ufbd3-\ufd3d\ufd50-\ufd8f\ufd92-\ufdc7\ufdf0-\ufdfb\ufe70-\ufe74\ufe76-\ufefc\uff21-\uff3a\uff41-\uff5a\uff66-\uffbe\uffc2-\uffc7\uffca-\uffcf\uffd2-\uffd7\uffda-\uffdc";
  1311. let nonASCIIidentifierChars = "\u200c\u200d\xb7\u0300-\u036f\u0387\u0483-\u0487\u0591-\u05bd\u05bf\u05c1\u05c2\u05c4\u05c5\u05c7\u0610-\u061a\u064b-\u0669\u0670\u06d6-\u06dc\u06df-\u06e4\u06e7\u06e8\u06ea-\u06ed\u06f0-\u06f9\u0711\u0730-\u074a\u07a6-\u07b0\u07c0-\u07c9\u07eb-\u07f3\u07fd\u0816-\u0819\u081b-\u0823\u0825-\u0827\u0829-\u082d\u0859-\u085b\u0898-\u089f\u08ca-\u08e1\u08e3-\u0903\u093a-\u093c\u093e-\u094f\u0951-\u0957\u0962\u0963\u0966-\u096f\u0981-\u0983\u09bc\u09be-\u09c4\u09c7\u09c8\u09cb-\u09cd\u09d7\u09e2\u09e3\u09e6-\u09ef\u09fe\u0a01-\u0a03\u0a3c\u0a3e-\u0a42\u0a47\u0a48\u0a4b-\u0a4d\u0a51\u0a66-\u0a71\u0a75\u0a81-\u0a83\u0abc\u0abe-\u0ac5\u0ac7-\u0ac9\u0acb-\u0acd\u0ae2\u0ae3\u0ae6-\u0aef\u0afa-\u0aff\u0b01-\u0b03\u0b3c\u0b3e-\u0b44\u0b47\u0b48\u0b4b-\u0b4d\u0b55-\u0b57\u0b62\u0b63\u0b66-\u0b6f\u0b82\u0bbe-\u0bc2\u0bc6-\u0bc8\u0bca-\u0bcd\u0bd7\u0be6-\u0bef\u0c00-\u0c04\u0c3c\u0c3e-\u0c44\u0c46-\u0c48\u0c4a-\u0c4d\u0c55\u0c56\u0c62\u0c63\u0c66-\u0c6f\u0c81-\u0c83\u0cbc\u0cbe-\u0cc4\u0cc6-\u0cc8\u0cca-\u0ccd\u0cd5\u0cd6\u0ce2\u0ce3\u0ce6-\u0cef\u0cf3\u0d00-\u0d03\u0d3b\u0d3c\u0d3e-\u0d44\u0d46-\u0d48\u0d4a-\u0d4d\u0d57\u0d62\u0d63\u0d66-\u0d6f\u0d81-\u0d83\u0dca\u0dcf-\u0dd4\u0dd6\u0dd8-\u0ddf\u0de6-\u0def\u0df2\u0df3\u0e31\u0e34-\u0e3a\u0e47-\u0e4e\u0e50-\u0e59\u0eb1\u0eb4-\u0ebc\u0ec8-\u0ece\u0ed0-\u0ed9\u0f18\u0f19\u0f20-\u0f29\u0f35\u0f37\u0f39\u0f3e\u0f3f\u0f71-\u0f84\u0f86\u0f87\u0f8d-\u0f97\u0f99-\u0fbc\u0fc6\u102b-\u103e\u1040-\u1049\u1056-\u1059\u105e-\u1060\u1062-\u1064\u1067-\u106d\u1071-\u1074\u1082-\u108d\u108f-\u109d\u135d-\u135f\u1369-\u1371\u1712-\u1715\u1732-\u1734\u1752\u1753\u1772\u1773\u17b4-\u17d3\u17dd\u17e0-\u17e9\u180b-\u180d\u180f-\u1819\u18a9\u1920-\u192b\u1930-\u193b\u1946-\u194f\u19d0-\u19da\u1a17-\u1a1b\u1a55-\u1a5e\u1a60-\u1a7c\u1a7f-\u1a89\u1a90-\u1a99\u1ab0-\u1abd\u1abf-\u1ace\u1b00-\u1b04\u1b34-\u1b44\u1b50-\u1b59\u1b6b-\u1b73\u1b80-\u1b82\u1ba1-\u1bad\u1bb0-\u1bb9\u1be6-\u1bf3\u1c24-\u1c37\u1c40-\u1c49\u1c50-\u1c59\u1cd0-\u1cd2\u1cd4-\u1ce8\u1ced\u1cf4\u1cf7-\u1cf9\u1dc0-\u1dff\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";
  1312. const nonASCIIidentifierStart = new RegExp("[" + nonASCIIidentifierStartChars + "]");
  1313. const nonASCIIidentifier = new RegExp("[" + nonASCIIidentifierStartChars + nonASCIIidentifierChars + "]");
  1314. nonASCIIidentifierStartChars = nonASCIIidentifierChars = null;
  1315. const astralIdentifierStartCodes = [0, 11, 2, 25, 2, 18, 2, 1, 2, 14, 3, 13, 35, 122, 70, 52, 268, 28, 4, 48, 48, 31, 14, 29, 6, 37, 11, 29, 3, 35, 5, 7, 2, 4, 43, 157, 19, 35, 5, 35, 5, 39, 9, 51, 13, 10, 2, 14, 2, 6, 2, 1, 2, 10, 2, 14, 2, 6, 2, 1, 68, 310, 10, 21, 11, 7, 25, 5, 2, 41, 2, 8, 70, 5, 3, 0, 2, 43, 2, 1, 4, 0, 3, 22, 11, 22, 10, 30, 66, 18, 2, 1, 11, 21, 11, 25, 71, 55, 7, 1, 65, 0, 16, 3, 2, 2, 2, 28, 43, 28, 4, 28, 36, 7, 2, 27, 28, 53, 11, 21, 11, 18, 14, 17, 111, 72, 56, 50, 14, 50, 14, 35, 349, 41, 7, 1, 79, 28, 11, 0, 9, 21, 43, 17, 47, 20, 28, 22, 13, 52, 58, 1, 3, 0, 14, 44, 33, 24, 27, 35, 30, 0, 3, 0, 9, 34, 4, 0, 13, 47, 15, 3, 22, 0, 2, 0, 36, 17, 2, 24, 20, 1, 64, 6, 2, 0, 2, 3, 2, 14, 2, 9, 8, 46, 39, 7, 3, 1, 3, 21, 2, 6, 2, 1, 2, 4, 4, 0, 19, 0, 13, 4, 159, 52, 19, 3, 21, 2, 31, 47, 21, 1, 2, 0, 185, 46, 42, 3, 37, 47, 21, 0, 60, 42, 14, 0, 72, 26, 38, 6, 186, 43, 117, 63, 32, 7, 3, 0, 3, 7, 2, 1, 2, 23, 16, 0, 2, 0, 95, 7, 3, 38, 17, 0, 2, 0, 29, 0, 11, 39, 8, 0, 22, 0, 12, 45, 20, 0, 19, 72, 264, 8, 2, 36, 18, 0, 50, 29, 113, 6, 2, 1, 2, 37, 22, 0, 26, 5, 2, 1, 2, 31, 15, 0, 328, 18, 16, 0, 2, 12, 2, 33, 125, 0, 80, 921, 103, 110, 18, 195, 2637, 96, 16, 1071, 18, 5, 4026, 582, 8634, 568, 8, 30, 18, 78, 18, 29, 19, 47, 17, 3, 32, 20, 6, 18, 689, 63, 129, 74, 6, 0, 67, 12, 65, 1, 2, 0, 29, 6135, 9, 1237, 43, 8, 8936, 3, 2, 6, 2, 1, 2, 290, 16, 0, 30, 2, 3, 0, 15, 3, 9, 395, 2309, 106, 6, 12, 4, 8, 8, 9, 5991, 84, 2, 70, 2, 1, 3, 0, 3, 1, 3, 3, 2, 11, 2, 0, 2, 6, 2, 64, 2, 3, 3, 7, 2, 6, 2, 27, 2, 3, 2, 4, 2, 0, 4, 6, 2, 339, 3, 24, 2, 24, 2, 30, 2, 24, 2, 30, 2, 24, 2, 30, 2, 24, 2, 30, 2, 24, 2, 7, 1845, 30, 7, 5, 262, 61, 147, 44, 11, 6, 17, 0, 322, 29, 19, 43, 485, 27, 757, 6, 2, 3, 2, 1, 2, 14, 2, 196, 60, 67, 8, 0, 1205, 3, 2, 26, 2, 1, 2, 0, 3, 0, 2, 9, 2, 3, 2, 0, 2, 0, 7, 0, 5, 0, 2, 0, 2, 0, 2, 2, 2, 1, 2, 0, 3, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 1, 2, 0, 3, 3, 2, 6, 2, 3, 2, 3, 2, 0, 2, 9, 2, 16, 6, 2, 2, 4, 2, 16, 4421, 42719, 33, 4153, 7, 221, 3, 5761, 15, 7472, 3104, 541, 1507, 4938, 6, 4191];
  1316. const astralIdentifierCodes = [509, 0, 227, 0, 150, 4, 294, 9, 1368, 2, 2, 1, 6, 3, 41, 2, 5, 0, 166, 1, 574, 3, 9, 9, 370, 1, 81, 2, 71, 10, 50, 3, 123, 2, 54, 14, 32, 10, 3, 1, 11, 3, 46, 10, 8, 0, 46, 9, 7, 2, 37, 13, 2, 9, 6, 1, 45, 0, 13, 2, 49, 13, 9, 3, 2, 11, 83, 11, 7, 0, 3, 0, 158, 11, 6, 9, 7, 3, 56, 1, 2, 6, 3, 1, 3, 2, 10, 0, 11, 1, 3, 6, 4, 4, 193, 17, 10, 9, 5, 0, 82, 19, 13, 9, 214, 6, 3, 8, 28, 1, 83, 16, 16, 9, 82, 12, 9, 9, 84, 14, 5, 9, 243, 14, 166, 9, 71, 5, 2, 1, 3, 3, 2, 0, 2, 1, 13, 9, 120, 6, 3, 6, 4, 0, 29, 9, 41, 6, 2, 3, 9, 0, 10, 10, 47, 15, 406, 7, 2, 7, 17, 9, 57, 21, 2, 13, 123, 5, 4, 0, 2, 1, 2, 6, 2, 0, 9, 9, 49, 4, 2, 1, 2, 4, 9, 9, 330, 3, 10, 1, 2, 0, 49, 6, 4, 4, 14, 9, 5351, 0, 7, 14, 13835, 9, 87, 9, 39, 4, 60, 6, 26, 9, 1014, 0, 2, 54, 8, 3, 82, 0, 12, 1, 19628, 1, 4706, 45, 3, 22, 543, 4, 4, 5, 9, 7, 3, 6, 31, 3, 149, 2, 1418, 49, 513, 54, 5, 49, 9, 0, 15, 0, 23, 4, 2, 14, 1361, 6, 2, 16, 3, 6, 2, 1, 2, 4, 101, 0, 161, 6, 10, 9, 357, 0, 62, 13, 499, 13, 983, 6, 110, 6, 6, 9, 4759, 9, 787719, 239];
  1317. function isInAstralSet(code, set) {
  1318. let pos = 0x10000;
  1319. for (let i = 0, length = set.length; i < length; i += 2) {
  1320. pos += set[i];
  1321. if (pos > code) return false;
  1322. pos += set[i + 1];
  1323. if (pos >= code) return true;
  1324. }
  1325. return false;
  1326. }
  1327. function isIdentifierStart(code) {
  1328. if (code < 65) return code === 36;
  1329. if (code <= 90) return true;
  1330. if (code < 97) return code === 95;
  1331. if (code <= 122) return true;
  1332. if (code <= 0xffff) {
  1333. return code >= 0xaa && nonASCIIidentifierStart.test(String.fromCharCode(code));
  1334. }
  1335. return isInAstralSet(code, astralIdentifierStartCodes);
  1336. }
  1337. function isIdentifierChar(code) {
  1338. if (code < 48) return code === 36;
  1339. if (code < 58) return true;
  1340. if (code < 65) return false;
  1341. if (code <= 90) return true;
  1342. if (code < 97) return code === 95;
  1343. if (code <= 122) return true;
  1344. if (code <= 0xffff) {
  1345. return code >= 0xaa && nonASCIIidentifier.test(String.fromCharCode(code));
  1346. }
  1347. return isInAstralSet(code, astralIdentifierStartCodes) || isInAstralSet(code, astralIdentifierCodes);
  1348. }
  1349. const reservedWords = {
  1350. 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"],
  1351. strict: ["implements", "interface", "let", "package", "private", "protected", "public", "static", "yield"],
  1352. strictBind: ["eval", "arguments"]
  1353. };
  1354. const keywords = new Set(reservedWords.keyword);
  1355. const reservedWordsStrictSet = new Set(reservedWords.strict);
  1356. const reservedWordsStrictBindSet = new Set(reservedWords.strictBind);
  1357. function isReservedWord(word, inModule) {
  1358. return inModule && word === "await" || word === "enum";
  1359. }
  1360. function isStrictReservedWord(word, inModule) {
  1361. return isReservedWord(word, inModule) || reservedWordsStrictSet.has(word);
  1362. }
  1363. function isStrictBindOnlyReservedWord(word) {
  1364. return reservedWordsStrictBindSet.has(word);
  1365. }
  1366. function isStrictBindReservedWord(word, inModule) {
  1367. return isStrictReservedWord(word, inModule) || isStrictBindOnlyReservedWord(word);
  1368. }
  1369. function isKeyword(word) {
  1370. return keywords.has(word);
  1371. }
  1372. function isIteratorStart(current, next, next2) {
  1373. return current === 64 && next === 64 && isIdentifierStart(next2);
  1374. }
  1375. const reservedWordLikeSet = new Set(["break", "case", "catch", "continue", "debugger", "default", "do", "else", "finally", "for", "function", "if", "return", "switch", "throw", "try", "var", "const", "while", "with", "new", "this", "super", "class", "extends", "export", "import", "null", "true", "false", "in", "instanceof", "typeof", "void", "delete", "implements", "interface", "let", "package", "private", "protected", "public", "static", "yield", "eval", "arguments", "enum", "await"]);
  1376. function canBeReservedWord(word) {
  1377. return reservedWordLikeSet.has(word);
  1378. }
  1379. const SCOPE_OTHER = 0b000000000,
  1380. SCOPE_PROGRAM = 0b000000001,
  1381. SCOPE_FUNCTION = 0b000000010,
  1382. SCOPE_ARROW = 0b000000100,
  1383. SCOPE_SIMPLE_CATCH = 0b000001000,
  1384. SCOPE_SUPER = 0b000010000,
  1385. SCOPE_DIRECT_SUPER = 0b000100000,
  1386. SCOPE_CLASS = 0b001000000,
  1387. SCOPE_STATIC_BLOCK = 0b010000000,
  1388. SCOPE_TS_MODULE = 0b100000000,
  1389. SCOPE_VAR = SCOPE_PROGRAM | SCOPE_FUNCTION | SCOPE_STATIC_BLOCK | SCOPE_TS_MODULE;
  1390. const BIND_KIND_VALUE = 0b0000000000001,
  1391. BIND_KIND_TYPE = 0b0000000000010,
  1392. BIND_SCOPE_VAR = 0b0000000000100,
  1393. BIND_SCOPE_LEXICAL = 0b0000000001000,
  1394. BIND_SCOPE_FUNCTION = 0b0000000010000,
  1395. BIND_FLAGS_NONE = 0b00000001000000,
  1396. BIND_FLAGS_CLASS = 0b00000010000000,
  1397. BIND_FLAGS_TS_ENUM = 0b00000100000000,
  1398. BIND_FLAGS_TS_CONST_ENUM = 0b00001000000000,
  1399. BIND_FLAGS_TS_EXPORT_ONLY = 0b00010000000000,
  1400. BIND_FLAGS_FLOW_DECLARE_FN = 0b00100000000000,
  1401. BIND_FLAGS_TS_IMPORT = 0b01000000000000,
  1402. BIND_FLAGS_NO_LET_IN_LEXICAL = 0b10000000000000;
  1403. const BIND_CLASS = BIND_KIND_VALUE | BIND_KIND_TYPE | BIND_SCOPE_LEXICAL | BIND_FLAGS_CLASS | BIND_FLAGS_NO_LET_IN_LEXICAL,
  1404. BIND_LEXICAL = BIND_KIND_VALUE | 0 | BIND_SCOPE_LEXICAL | BIND_FLAGS_NO_LET_IN_LEXICAL,
  1405. BIND_CATCH_PARAM = BIND_KIND_VALUE | 0 | BIND_SCOPE_LEXICAL | 0,
  1406. BIND_VAR = BIND_KIND_VALUE | 0 | BIND_SCOPE_VAR | 0,
  1407. BIND_FUNCTION = BIND_KIND_VALUE | 0 | BIND_SCOPE_FUNCTION | 0,
  1408. BIND_TS_INTERFACE = 0 | BIND_KIND_TYPE | 0 | BIND_FLAGS_CLASS,
  1409. BIND_TS_TYPE = 0 | BIND_KIND_TYPE | 0 | 0,
  1410. BIND_TS_ENUM = BIND_KIND_VALUE | BIND_KIND_TYPE | BIND_SCOPE_LEXICAL | BIND_FLAGS_TS_ENUM | BIND_FLAGS_NO_LET_IN_LEXICAL,
  1411. BIND_TS_AMBIENT = 0 | 0 | 0 | BIND_FLAGS_TS_EXPORT_ONLY,
  1412. BIND_NONE = 0 | 0 | 0 | BIND_FLAGS_NONE,
  1413. BIND_OUTSIDE = BIND_KIND_VALUE | 0 | 0 | BIND_FLAGS_NONE,
  1414. BIND_TS_CONST_ENUM = BIND_TS_ENUM | BIND_FLAGS_TS_CONST_ENUM,
  1415. BIND_TS_NAMESPACE = 0 | 0 | 0 | BIND_FLAGS_TS_EXPORT_ONLY,
  1416. BIND_TS_TYPE_IMPORT = 0 | BIND_KIND_TYPE | 0 | BIND_FLAGS_TS_IMPORT,
  1417. BIND_FLOW_DECLARE_FN = BIND_FLAGS_FLOW_DECLARE_FN;
  1418. const CLASS_ELEMENT_FLAG_STATIC = 0b100,
  1419. CLASS_ELEMENT_KIND_GETTER = 0b010,
  1420. CLASS_ELEMENT_KIND_SETTER = 0b001,
  1421. CLASS_ELEMENT_KIND_ACCESSOR = CLASS_ELEMENT_KIND_GETTER | CLASS_ELEMENT_KIND_SETTER;
  1422. const CLASS_ELEMENT_STATIC_GETTER = CLASS_ELEMENT_KIND_GETTER | CLASS_ELEMENT_FLAG_STATIC,
  1423. CLASS_ELEMENT_STATIC_SETTER = CLASS_ELEMENT_KIND_SETTER | CLASS_ELEMENT_FLAG_STATIC,
  1424. CLASS_ELEMENT_INSTANCE_GETTER = CLASS_ELEMENT_KIND_GETTER,
  1425. CLASS_ELEMENT_INSTANCE_SETTER = CLASS_ELEMENT_KIND_SETTER,
  1426. CLASS_ELEMENT_OTHER = 0;
  1427. class Scope {
  1428. constructor(flags) {
  1429. this.var = new Set();
  1430. this.lexical = new Set();
  1431. this.functions = new Set();
  1432. this.flags = flags;
  1433. }
  1434. }
  1435. class ScopeHandler {
  1436. constructor(parser, inModule) {
  1437. this.parser = void 0;
  1438. this.scopeStack = [];
  1439. this.inModule = void 0;
  1440. this.undefinedExports = new Map();
  1441. this.parser = parser;
  1442. this.inModule = inModule;
  1443. }
  1444. get inTopLevel() {
  1445. return (this.currentScope().flags & SCOPE_PROGRAM) > 0;
  1446. }
  1447. get inFunction() {
  1448. return (this.currentVarScopeFlags() & SCOPE_FUNCTION) > 0;
  1449. }
  1450. get allowSuper() {
  1451. return (this.currentThisScopeFlags() & SCOPE_SUPER) > 0;
  1452. }
  1453. get allowDirectSuper() {
  1454. return (this.currentThisScopeFlags() & SCOPE_DIRECT_SUPER) > 0;
  1455. }
  1456. get inClass() {
  1457. return (this.currentThisScopeFlags() & SCOPE_CLASS) > 0;
  1458. }
  1459. get inClassAndNotInNonArrowFunction() {
  1460. const flags = this.currentThisScopeFlags();
  1461. return (flags & SCOPE_CLASS) > 0 && (flags & SCOPE_FUNCTION) === 0;
  1462. }
  1463. get inStaticBlock() {
  1464. for (let i = this.scopeStack.length - 1;; i--) {
  1465. const {
  1466. flags
  1467. } = this.scopeStack[i];
  1468. if (flags & SCOPE_STATIC_BLOCK) {
  1469. return true;
  1470. }
  1471. if (flags & (SCOPE_VAR | SCOPE_CLASS)) {
  1472. return false;
  1473. }
  1474. }
  1475. }
  1476. get inNonArrowFunction() {
  1477. return (this.currentThisScopeFlags() & SCOPE_FUNCTION) > 0;
  1478. }
  1479. get treatFunctionsAsVar() {
  1480. return this.treatFunctionsAsVarInScope(this.currentScope());
  1481. }
  1482. createScope(flags) {
  1483. return new Scope(flags);
  1484. }
  1485. enter(flags) {
  1486. this.scopeStack.push(this.createScope(flags));
  1487. }
  1488. exit() {
  1489. const scope = this.scopeStack.pop();
  1490. return scope.flags;
  1491. }
  1492. treatFunctionsAsVarInScope(scope) {
  1493. return !!(scope.flags & (SCOPE_FUNCTION | SCOPE_STATIC_BLOCK) || !this.parser.inModule && scope.flags & SCOPE_PROGRAM);
  1494. }
  1495. declareName(name, bindingType, loc) {
  1496. let scope = this.currentScope();
  1497. if (bindingType & BIND_SCOPE_LEXICAL || bindingType & BIND_SCOPE_FUNCTION) {
  1498. this.checkRedeclarationInScope(scope, name, bindingType, loc);
  1499. if (bindingType & BIND_SCOPE_FUNCTION) {
  1500. scope.functions.add(name);
  1501. } else {
  1502. scope.lexical.add(name);
  1503. }
  1504. if (bindingType & BIND_SCOPE_LEXICAL) {
  1505. this.maybeExportDefined(scope, name);
  1506. }
  1507. } else if (bindingType & BIND_SCOPE_VAR) {
  1508. for (let i = this.scopeStack.length - 1; i >= 0; --i) {
  1509. scope = this.scopeStack[i];
  1510. this.checkRedeclarationInScope(scope, name, bindingType, loc);
  1511. scope.var.add(name);
  1512. this.maybeExportDefined(scope, name);
  1513. if (scope.flags & SCOPE_VAR) break;
  1514. }
  1515. }
  1516. if (this.parser.inModule && scope.flags & SCOPE_PROGRAM) {
  1517. this.undefinedExports.delete(name);
  1518. }
  1519. }
  1520. maybeExportDefined(scope, name) {
  1521. if (this.parser.inModule && scope.flags & SCOPE_PROGRAM) {
  1522. this.undefinedExports.delete(name);
  1523. }
  1524. }
  1525. checkRedeclarationInScope(scope, name, bindingType, loc) {
  1526. if (this.isRedeclaredInScope(scope, name, bindingType)) {
  1527. this.parser.raise(Errors.VarRedeclaration, {
  1528. at: loc,
  1529. identifierName: name
  1530. });
  1531. }
  1532. }
  1533. isRedeclaredInScope(scope, name, bindingType) {
  1534. if (!(bindingType & BIND_KIND_VALUE)) return false;
  1535. if (bindingType & BIND_SCOPE_LEXICAL) {
  1536. return scope.lexical.has(name) || scope.functions.has(name) || scope.var.has(name);
  1537. }
  1538. if (bindingType & BIND_SCOPE_FUNCTION) {
  1539. return scope.lexical.has(name) || !this.treatFunctionsAsVarInScope(scope) && scope.var.has(name);
  1540. }
  1541. return scope.lexical.has(name) && !(scope.flags & SCOPE_SIMPLE_CATCH && scope.lexical.values().next().value === name) || !this.treatFunctionsAsVarInScope(scope) && scope.functions.has(name);
  1542. }
  1543. checkLocalExport(id) {
  1544. const {
  1545. name
  1546. } = id;
  1547. const topLevelScope = this.scopeStack[0];
  1548. if (!topLevelScope.lexical.has(name) && !topLevelScope.var.has(name) && !topLevelScope.functions.has(name)) {
  1549. this.undefinedExports.set(name, id.loc.start);
  1550. }
  1551. }
  1552. currentScope() {
  1553. return this.scopeStack[this.scopeStack.length - 1];
  1554. }
  1555. currentVarScopeFlags() {
  1556. for (let i = this.scopeStack.length - 1;; i--) {
  1557. const {
  1558. flags
  1559. } = this.scopeStack[i];
  1560. if (flags & SCOPE_VAR) {
  1561. return flags;
  1562. }
  1563. }
  1564. }
  1565. currentThisScopeFlags() {
  1566. for (let i = this.scopeStack.length - 1;; i--) {
  1567. const {
  1568. flags
  1569. } = this.scopeStack[i];
  1570. if (flags & (SCOPE_VAR | SCOPE_CLASS) && !(flags & SCOPE_ARROW)) {
  1571. return flags;
  1572. }
  1573. }
  1574. }
  1575. }
  1576. class FlowScope extends Scope {
  1577. constructor(...args) {
  1578. super(...args);
  1579. this.declareFunctions = new Set();
  1580. }
  1581. }
  1582. class FlowScopeHandler extends ScopeHandler {
  1583. createScope(flags) {
  1584. return new FlowScope(flags);
  1585. }
  1586. declareName(name, bindingType, loc) {
  1587. const scope = this.currentScope();
  1588. if (bindingType & BIND_FLAGS_FLOW_DECLARE_FN) {
  1589. this.checkRedeclarationInScope(scope, name, bindingType, loc);
  1590. this.maybeExportDefined(scope, name);
  1591. scope.declareFunctions.add(name);
  1592. return;
  1593. }
  1594. super.declareName(name, bindingType, loc);
  1595. }
  1596. isRedeclaredInScope(scope, name, bindingType) {
  1597. if (super.isRedeclaredInScope(scope, name, bindingType)) return true;
  1598. if (bindingType & BIND_FLAGS_FLOW_DECLARE_FN) {
  1599. return !scope.declareFunctions.has(name) && (scope.lexical.has(name) || scope.functions.has(name));
  1600. }
  1601. return false;
  1602. }
  1603. checkLocalExport(id) {
  1604. if (!this.scopeStack[0].declareFunctions.has(id.name)) {
  1605. super.checkLocalExport(id);
  1606. }
  1607. }
  1608. }
  1609. class BaseParser {
  1610. constructor() {
  1611. this.sawUnambiguousESM = false;
  1612. this.ambiguousScriptDifferentAst = false;
  1613. }
  1614. hasPlugin(pluginConfig) {
  1615. if (typeof pluginConfig === "string") {
  1616. return this.plugins.has(pluginConfig);
  1617. } else {
  1618. const [pluginName, pluginOptions] = pluginConfig;
  1619. if (!this.hasPlugin(pluginName)) {
  1620. return false;
  1621. }
  1622. const actualOptions = this.plugins.get(pluginName);
  1623. for (const key of Object.keys(pluginOptions)) {
  1624. if ((actualOptions == null ? void 0 : actualOptions[key]) !== pluginOptions[key]) {
  1625. return false;
  1626. }
  1627. }
  1628. return true;
  1629. }
  1630. }
  1631. getPluginOption(plugin, name) {
  1632. var _this$plugins$get;
  1633. return (_this$plugins$get = this.plugins.get(plugin)) == null ? void 0 : _this$plugins$get[name];
  1634. }
  1635. }
  1636. function setTrailingComments(node, comments) {
  1637. if (node.trailingComments === undefined) {
  1638. node.trailingComments = comments;
  1639. } else {
  1640. node.trailingComments.unshift(...comments);
  1641. }
  1642. }
  1643. function setLeadingComments(node, comments) {
  1644. if (node.leadingComments === undefined) {
  1645. node.leadingComments = comments;
  1646. } else {
  1647. node.leadingComments.unshift(...comments);
  1648. }
  1649. }
  1650. function setInnerComments(node, comments) {
  1651. if (node.innerComments === undefined) {
  1652. node.innerComments = comments;
  1653. } else {
  1654. node.innerComments.unshift(...comments);
  1655. }
  1656. }
  1657. function adjustInnerComments(node, elements, commentWS) {
  1658. let lastElement = null;
  1659. let i = elements.length;
  1660. while (lastElement === null && i > 0) {
  1661. lastElement = elements[--i];
  1662. }
  1663. if (lastElement === null || lastElement.start > commentWS.start) {
  1664. setInnerComments(node, commentWS.comments);
  1665. } else {
  1666. setTrailingComments(lastElement, commentWS.comments);
  1667. }
  1668. }
  1669. class CommentsParser extends BaseParser {
  1670. addComment(comment) {
  1671. if (this.filename) comment.loc.filename = this.filename;
  1672. this.state.comments.push(comment);
  1673. }
  1674. processComment(node) {
  1675. const {
  1676. commentStack
  1677. } = this.state;
  1678. const commentStackLength = commentStack.length;
  1679. if (commentStackLength === 0) return;
  1680. let i = commentStackLength - 1;
  1681. const lastCommentWS = commentStack[i];
  1682. if (lastCommentWS.start === node.end) {
  1683. lastCommentWS.leadingNode = node;
  1684. i--;
  1685. }
  1686. const {
  1687. start: nodeStart
  1688. } = node;
  1689. for (; i >= 0; i--) {
  1690. const commentWS = commentStack[i];
  1691. const commentEnd = commentWS.end;
  1692. if (commentEnd > nodeStart) {
  1693. commentWS.containingNode = node;
  1694. this.finalizeComment(commentWS);
  1695. commentStack.splice(i, 1);
  1696. } else {
  1697. if (commentEnd === nodeStart) {
  1698. commentWS.trailingNode = node;
  1699. }
  1700. break;
  1701. }
  1702. }
  1703. }
  1704. finalizeComment(commentWS) {
  1705. const {
  1706. comments
  1707. } = commentWS;
  1708. if (commentWS.leadingNode !== null || commentWS.trailingNode !== null) {
  1709. if (commentWS.leadingNode !== null) {
  1710. setTrailingComments(commentWS.leadingNode, comments);
  1711. }
  1712. if (commentWS.trailingNode !== null) {
  1713. setLeadingComments(commentWS.trailingNode, comments);
  1714. }
  1715. } else {
  1716. const {
  1717. containingNode: node,
  1718. start: commentStart
  1719. } = commentWS;
  1720. if (this.input.charCodeAt(commentStart - 1) === 44) {
  1721. switch (node.type) {
  1722. case "ObjectExpression":
  1723. case "ObjectPattern":
  1724. case "RecordExpression":
  1725. adjustInnerComments(node, node.properties, commentWS);
  1726. break;
  1727. case "CallExpression":
  1728. case "OptionalCallExpression":
  1729. adjustInnerComments(node, node.arguments, commentWS);
  1730. break;
  1731. case "FunctionDeclaration":
  1732. case "FunctionExpression":
  1733. case "ArrowFunctionExpression":
  1734. case "ObjectMethod":
  1735. case "ClassMethod":
  1736. case "ClassPrivateMethod":
  1737. adjustInnerComments(node, node.params, commentWS);
  1738. break;
  1739. case "ArrayExpression":
  1740. case "ArrayPattern":
  1741. case "TupleExpression":
  1742. adjustInnerComments(node, node.elements, commentWS);
  1743. break;
  1744. case "ExportNamedDeclaration":
  1745. case "ImportDeclaration":
  1746. adjustInnerComments(node, node.specifiers, commentWS);
  1747. break;
  1748. default:
  1749. {
  1750. setInnerComments(node, comments);
  1751. }
  1752. }
  1753. } else {
  1754. setInnerComments(node, comments);
  1755. }
  1756. }
  1757. }
  1758. finalizeRemainingComments() {
  1759. const {
  1760. commentStack
  1761. } = this.state;
  1762. for (let i = commentStack.length - 1; i >= 0; i--) {
  1763. this.finalizeComment(commentStack[i]);
  1764. }
  1765. this.state.commentStack = [];
  1766. }
  1767. resetPreviousNodeTrailingComments(node) {
  1768. const {
  1769. commentStack
  1770. } = this.state;
  1771. const {
  1772. length
  1773. } = commentStack;
  1774. if (length === 0) return;
  1775. const commentWS = commentStack[length - 1];
  1776. if (commentWS.leadingNode === node) {
  1777. commentWS.leadingNode = null;
  1778. }
  1779. }
  1780. takeSurroundingComments(node, start, end) {
  1781. const {
  1782. commentStack
  1783. } = this.state;
  1784. const commentStackLength = commentStack.length;
  1785. if (commentStackLength === 0) return;
  1786. let i = commentStackLength - 1;
  1787. for (; i >= 0; i--) {
  1788. const commentWS = commentStack[i];
  1789. const commentEnd = commentWS.end;
  1790. const commentStart = commentWS.start;
  1791. if (commentStart === end) {
  1792. commentWS.leadingNode = node;
  1793. } else if (commentEnd === start) {
  1794. commentWS.trailingNode = node;
  1795. } else if (commentEnd < start) {
  1796. break;
  1797. }
  1798. }
  1799. }
  1800. }
  1801. const lineBreak = /\r\n?|[\n\u2028\u2029]/;
  1802. const lineBreakG = new RegExp(lineBreak.source, "g");
  1803. function isNewLine(code) {
  1804. switch (code) {
  1805. case 10:
  1806. case 13:
  1807. case 8232:
  1808. case 8233:
  1809. return true;
  1810. default:
  1811. return false;
  1812. }
  1813. }
  1814. const skipWhiteSpace = /(?:\s|\/\/.*|\/\*[^]*?\*\/)*/g;
  1815. const skipWhiteSpaceInLine = /(?:[^\S\n\r\u2028\u2029]|\/\/.*|\/\*.*?\*\/)*/y;
  1816. const skipWhiteSpaceToLineBreak = new RegExp("(?=(" + skipWhiteSpaceInLine.source + "))\\1" + /(?=[\n\r\u2028\u2029]|\/\*(?!.*?\*\/)|$)/.source, "y");
  1817. function isWhitespace(code) {
  1818. switch (code) {
  1819. case 0x0009:
  1820. case 0x000b:
  1821. case 0x000c:
  1822. case 32:
  1823. case 160:
  1824. case 5760:
  1825. case 0x2000:
  1826. case 0x2001:
  1827. case 0x2002:
  1828. case 0x2003:
  1829. case 0x2004:
  1830. case 0x2005:
  1831. case 0x2006:
  1832. case 0x2007:
  1833. case 0x2008:
  1834. case 0x2009:
  1835. case 0x200a:
  1836. case 0x202f:
  1837. case 0x205f:
  1838. case 0x3000:
  1839. case 0xfeff:
  1840. return true;
  1841. default:
  1842. return false;
  1843. }
  1844. }
  1845. class State {
  1846. constructor() {
  1847. this.strict = void 0;
  1848. this.curLine = void 0;
  1849. this.lineStart = void 0;
  1850. this.startLoc = void 0;
  1851. this.endLoc = void 0;
  1852. this.errors = [];
  1853. this.potentialArrowAt = -1;
  1854. this.noArrowAt = [];
  1855. this.noArrowParamsConversionAt = [];
  1856. this.maybeInArrowParameters = false;
  1857. this.inType = false;
  1858. this.noAnonFunctionType = false;
  1859. this.hasFlowComment = false;
  1860. this.isAmbientContext = false;
  1861. this.inAbstractClass = false;
  1862. this.inDisallowConditionalTypesContext = false;
  1863. this.topicContext = {
  1864. maxNumOfResolvableTopics: 0,
  1865. maxTopicIndex: null
  1866. };
  1867. this.soloAwait = false;
  1868. this.inFSharpPipelineDirectBody = false;
  1869. this.labels = [];
  1870. this.comments = [];
  1871. this.commentStack = [];
  1872. this.pos = 0;
  1873. this.type = 137;
  1874. this.value = null;
  1875. this.start = 0;
  1876. this.end = 0;
  1877. this.lastTokEndLoc = null;
  1878. this.lastTokStartLoc = null;
  1879. this.lastTokStart = 0;
  1880. this.context = [types.brace];
  1881. this.canStartJSXElement = true;
  1882. this.containsEsc = false;
  1883. this.firstInvalidTemplateEscapePos = null;
  1884. this.strictErrors = new Map();
  1885. this.tokensLength = 0;
  1886. }
  1887. init({
  1888. strictMode,
  1889. sourceType,
  1890. startLine,
  1891. startColumn
  1892. }) {
  1893. this.strict = strictMode === false ? false : strictMode === true ? true : sourceType === "module";
  1894. this.curLine = startLine;
  1895. this.lineStart = -startColumn;
  1896. this.startLoc = this.endLoc = new Position(startLine, startColumn, 0);
  1897. }
  1898. curPosition() {
  1899. return new Position(this.curLine, this.pos - this.lineStart, this.pos);
  1900. }
  1901. clone(skipArrays) {
  1902. const state = new State();
  1903. const keys = Object.keys(this);
  1904. for (let i = 0, length = keys.length; i < length; i++) {
  1905. const key = keys[i];
  1906. let val = this[key];
  1907. if (!skipArrays && Array.isArray(val)) {
  1908. val = val.slice();
  1909. }
  1910. state[key] = val;
  1911. }
  1912. return state;
  1913. }
  1914. }
  1915. var _isDigit = function isDigit(code) {
  1916. return code >= 48 && code <= 57;
  1917. };
  1918. const forbiddenNumericSeparatorSiblings = {
  1919. decBinOct: new Set([46, 66, 69, 79, 95, 98, 101, 111]),
  1920. hex: new Set([46, 88, 95, 120])
  1921. };
  1922. const isAllowedNumericSeparatorSibling = {
  1923. bin: ch => ch === 48 || ch === 49,
  1924. oct: ch => ch >= 48 && ch <= 55,
  1925. dec: ch => ch >= 48 && ch <= 57,
  1926. hex: ch => ch >= 48 && ch <= 57 || ch >= 65 && ch <= 70 || ch >= 97 && ch <= 102
  1927. };
  1928. function readStringContents(type, input, pos, lineStart, curLine, errors) {
  1929. const initialPos = pos;
  1930. const initialLineStart = lineStart;
  1931. const initialCurLine = curLine;
  1932. let out = "";
  1933. let firstInvalidLoc = null;
  1934. let chunkStart = pos;
  1935. const {
  1936. length
  1937. } = input;
  1938. for (;;) {
  1939. if (pos >= length) {
  1940. errors.unterminated(initialPos, initialLineStart, initialCurLine);
  1941. out += input.slice(chunkStart, pos);
  1942. break;
  1943. }
  1944. const ch = input.charCodeAt(pos);
  1945. if (isStringEnd(type, ch, input, pos)) {
  1946. out += input.slice(chunkStart, pos);
  1947. break;
  1948. }
  1949. if (ch === 92) {
  1950. out += input.slice(chunkStart, pos);
  1951. const res = readEscapedChar(input, pos, lineStart, curLine, type === "template", errors);
  1952. if (res.ch === null && !firstInvalidLoc) {
  1953. firstInvalidLoc = {
  1954. pos,
  1955. lineStart,
  1956. curLine
  1957. };
  1958. } else {
  1959. out += res.ch;
  1960. }
  1961. ({
  1962. pos,
  1963. lineStart,
  1964. curLine
  1965. } = res);
  1966. chunkStart = pos;
  1967. } else if (ch === 8232 || ch === 8233) {
  1968. ++pos;
  1969. ++curLine;
  1970. lineStart = pos;
  1971. } else if (ch === 10 || ch === 13) {
  1972. if (type === "template") {
  1973. out += input.slice(chunkStart, pos) + "\n";
  1974. ++pos;
  1975. if (ch === 13 && input.charCodeAt(pos) === 10) {
  1976. ++pos;
  1977. }
  1978. ++curLine;
  1979. chunkStart = lineStart = pos;
  1980. } else {
  1981. errors.unterminated(initialPos, initialLineStart, initialCurLine);
  1982. }
  1983. } else {
  1984. ++pos;
  1985. }
  1986. }
  1987. return {
  1988. pos,
  1989. str: out,
  1990. firstInvalidLoc,
  1991. lineStart,
  1992. curLine,
  1993. containsInvalid: !!firstInvalidLoc
  1994. };
  1995. }
  1996. function isStringEnd(type, ch, input, pos) {
  1997. if (type === "template") {
  1998. return ch === 96 || ch === 36 && input.charCodeAt(pos + 1) === 123;
  1999. }
  2000. return ch === (type === "double" ? 34 : 39);
  2001. }
  2002. function readEscapedChar(input, pos, lineStart, curLine, inTemplate, errors) {
  2003. const throwOnInvalid = !inTemplate;
  2004. pos++;
  2005. const res = ch => ({
  2006. pos,
  2007. ch,
  2008. lineStart,
  2009. curLine
  2010. });
  2011. const ch = input.charCodeAt(pos++);
  2012. switch (ch) {
  2013. case 110:
  2014. return res("\n");
  2015. case 114:
  2016. return res("\r");
  2017. case 120:
  2018. {
  2019. let code;
  2020. ({
  2021. code,
  2022. pos
  2023. } = readHexChar(input, pos, lineStart, curLine, 2, false, throwOnInvalid, errors));
  2024. return res(code === null ? null : String.fromCharCode(code));
  2025. }
  2026. case 117:
  2027. {
  2028. let code;
  2029. ({
  2030. code,
  2031. pos
  2032. } = readCodePoint(input, pos, lineStart, curLine, throwOnInvalid, errors));
  2033. return res(code === null ? null : String.fromCodePoint(code));
  2034. }
  2035. case 116:
  2036. return res("\t");
  2037. case 98:
  2038. return res("\b");
  2039. case 118:
  2040. return res("\u000b");
  2041. case 102:
  2042. return res("\f");
  2043. case 13:
  2044. if (input.charCodeAt(pos) === 10) {
  2045. ++pos;
  2046. }
  2047. case 10:
  2048. lineStart = pos;
  2049. ++curLine;
  2050. case 8232:
  2051. case 8233:
  2052. return res("");
  2053. case 56:
  2054. case 57:
  2055. if (inTemplate) {
  2056. return res(null);
  2057. } else {
  2058. errors.strictNumericEscape(pos - 1, lineStart, curLine);
  2059. }
  2060. default:
  2061. if (ch >= 48 && ch <= 55) {
  2062. const startPos = pos - 1;
  2063. const match = input.slice(startPos, pos + 2).match(/^[0-7]+/);
  2064. let octalStr = match[0];
  2065. let octal = parseInt(octalStr, 8);
  2066. if (octal > 255) {
  2067. octalStr = octalStr.slice(0, -1);
  2068. octal = parseInt(octalStr, 8);
  2069. }
  2070. pos += octalStr.length - 1;
  2071. const next = input.charCodeAt(pos);
  2072. if (octalStr !== "0" || next === 56 || next === 57) {
  2073. if (inTemplate) {
  2074. return res(null);
  2075. } else {
  2076. errors.strictNumericEscape(startPos, lineStart, curLine);
  2077. }
  2078. }
  2079. return res(String.fromCharCode(octal));
  2080. }
  2081. return res(String.fromCharCode(ch));
  2082. }
  2083. }
  2084. function readHexChar(input, pos, lineStart, curLine, len, forceLen, throwOnInvalid, errors) {
  2085. const initialPos = pos;
  2086. let n;
  2087. ({
  2088. n,
  2089. pos
  2090. } = readInt(input, pos, lineStart, curLine, 16, len, forceLen, false, errors, !throwOnInvalid));
  2091. if (n === null) {
  2092. if (throwOnInvalid) {
  2093. errors.invalidEscapeSequence(initialPos, lineStart, curLine);
  2094. } else {
  2095. pos = initialPos - 1;
  2096. }
  2097. }
  2098. return {
  2099. code: n,
  2100. pos
  2101. };
  2102. }
  2103. function readInt(input, pos, lineStart, curLine, radix, len, forceLen, allowNumSeparator, errors, bailOnError) {
  2104. const start = pos;
  2105. const forbiddenSiblings = radix === 16 ? forbiddenNumericSeparatorSiblings.hex : forbiddenNumericSeparatorSiblings.decBinOct;
  2106. const isAllowedSibling = radix === 16 ? isAllowedNumericSeparatorSibling.hex : radix === 10 ? isAllowedNumericSeparatorSibling.dec : radix === 8 ? isAllowedNumericSeparatorSibling.oct : isAllowedNumericSeparatorSibling.bin;
  2107. let invalid = false;
  2108. let total = 0;
  2109. for (let i = 0, e = len == null ? Infinity : len; i < e; ++i) {
  2110. const code = input.charCodeAt(pos);
  2111. let val;
  2112. if (code === 95 && allowNumSeparator !== "bail") {
  2113. const prev = input.charCodeAt(pos - 1);
  2114. const next = input.charCodeAt(pos + 1);
  2115. if (!allowNumSeparator) {
  2116. if (bailOnError) return {
  2117. n: null,
  2118. pos
  2119. };
  2120. errors.numericSeparatorInEscapeSequence(pos, lineStart, curLine);
  2121. } else if (Number.isNaN(next) || !isAllowedSibling(next) || forbiddenSiblings.has(prev) || forbiddenSiblings.has(next)) {
  2122. if (bailOnError) return {
  2123. n: null,
  2124. pos
  2125. };
  2126. errors.unexpectedNumericSeparator(pos, lineStart, curLine);
  2127. }
  2128. ++pos;
  2129. continue;
  2130. }
  2131. if (code >= 97) {
  2132. val = code - 97 + 10;
  2133. } else if (code >= 65) {
  2134. val = code - 65 + 10;
  2135. } else if (_isDigit(code)) {
  2136. val = code - 48;
  2137. } else {
  2138. val = Infinity;
  2139. }
  2140. if (val >= radix) {
  2141. if (val <= 9 && bailOnError) {
  2142. return {
  2143. n: null,
  2144. pos
  2145. };
  2146. } else if (val <= 9 && errors.invalidDigit(pos, lineStart, curLine, radix)) {
  2147. val = 0;
  2148. } else if (forceLen) {
  2149. val = 0;
  2150. invalid = true;
  2151. } else {
  2152. break;
  2153. }
  2154. }
  2155. ++pos;
  2156. total = total * radix + val;
  2157. }
  2158. if (pos === start || len != null && pos - start !== len || invalid) {
  2159. return {
  2160. n: null,
  2161. pos
  2162. };
  2163. }
  2164. return {
  2165. n: total,
  2166. pos
  2167. };
  2168. }
  2169. function readCodePoint(input, pos, lineStart, curLine, throwOnInvalid, errors) {
  2170. const ch = input.charCodeAt(pos);
  2171. let code;
  2172. if (ch === 123) {
  2173. ++pos;
  2174. ({
  2175. code,
  2176. pos
  2177. } = readHexChar(input, pos, lineStart, curLine, input.indexOf("}", pos) - pos, true, throwOnInvalid, errors));
  2178. ++pos;
  2179. if (code !== null && code > 0x10ffff) {
  2180. if (throwOnInvalid) {
  2181. errors.invalidCodePoint(pos, lineStart, curLine);
  2182. } else {
  2183. return {
  2184. code: null,
  2185. pos
  2186. };
  2187. }
  2188. }
  2189. } else {
  2190. ({
  2191. code,
  2192. pos
  2193. } = readHexChar(input, pos, lineStart, curLine, 4, false, throwOnInvalid, errors));
  2194. }
  2195. return {
  2196. code,
  2197. pos
  2198. };
  2199. }
  2200. const _excluded = ["at"],
  2201. _excluded2 = ["at"];
  2202. function buildPosition(pos, lineStart, curLine) {
  2203. return new Position(curLine, pos - lineStart, pos);
  2204. }
  2205. const VALID_REGEX_FLAGS = new Set([103, 109, 115, 105, 121, 117, 100, 118]);
  2206. class Token {
  2207. constructor(state) {
  2208. this.type = state.type;
  2209. this.value = state.value;
  2210. this.start = state.start;
  2211. this.end = state.end;
  2212. this.loc = new SourceLocation(state.startLoc, state.endLoc);
  2213. }
  2214. }
  2215. class Tokenizer extends CommentsParser {
  2216. constructor(options, input) {
  2217. super();
  2218. this.isLookahead = void 0;
  2219. this.tokens = [];
  2220. this.errorHandlers_readInt = {
  2221. invalidDigit: (pos, lineStart, curLine, radix) => {
  2222. if (!this.options.errorRecovery) return false;
  2223. this.raise(Errors.InvalidDigit, {
  2224. at: buildPosition(pos, lineStart, curLine),
  2225. radix
  2226. });
  2227. return true;
  2228. },
  2229. numericSeparatorInEscapeSequence: this.errorBuilder(Errors.NumericSeparatorInEscapeSequence),
  2230. unexpectedNumericSeparator: this.errorBuilder(Errors.UnexpectedNumericSeparator)
  2231. };
  2232. this.errorHandlers_readCodePoint = Object.assign({}, this.errorHandlers_readInt, {
  2233. invalidEscapeSequence: this.errorBuilder(Errors.InvalidEscapeSequence),
  2234. invalidCodePoint: this.errorBuilder(Errors.InvalidCodePoint)
  2235. });
  2236. this.errorHandlers_readStringContents_string = Object.assign({}, this.errorHandlers_readCodePoint, {
  2237. strictNumericEscape: (pos, lineStart, curLine) => {
  2238. this.recordStrictModeErrors(Errors.StrictNumericEscape, {
  2239. at: buildPosition(pos, lineStart, curLine)
  2240. });
  2241. },
  2242. unterminated: (pos, lineStart, curLine) => {
  2243. throw this.raise(Errors.UnterminatedString, {
  2244. at: buildPosition(pos - 1, lineStart, curLine)
  2245. });
  2246. }
  2247. });
  2248. this.errorHandlers_readStringContents_template = Object.assign({}, this.errorHandlers_readCodePoint, {
  2249. strictNumericEscape: this.errorBuilder(Errors.StrictNumericEscape),
  2250. unterminated: (pos, lineStart, curLine) => {
  2251. throw this.raise(Errors.UnterminatedTemplate, {
  2252. at: buildPosition(pos, lineStart, curLine)
  2253. });
  2254. }
  2255. });
  2256. this.state = new State();
  2257. this.state.init(options);
  2258. this.input = input;
  2259. this.length = input.length;
  2260. this.isLookahead = false;
  2261. }
  2262. pushToken(token) {
  2263. this.tokens.length = this.state.tokensLength;
  2264. this.tokens.push(token);
  2265. ++this.state.tokensLength;
  2266. }
  2267. next() {
  2268. this.checkKeywordEscapes();
  2269. if (this.options.tokens) {
  2270. this.pushToken(new Token(this.state));
  2271. }
  2272. this.state.lastTokStart = this.state.start;
  2273. this.state.lastTokEndLoc = this.state.endLoc;
  2274. this.state.lastTokStartLoc = this.state.startLoc;
  2275. this.nextToken();
  2276. }
  2277. eat(type) {
  2278. if (this.match(type)) {
  2279. this.next();
  2280. return true;
  2281. } else {
  2282. return false;
  2283. }
  2284. }
  2285. match(type) {
  2286. return this.state.type === type;
  2287. }
  2288. createLookaheadState(state) {
  2289. return {
  2290. pos: state.pos,
  2291. value: null,
  2292. type: state.type,
  2293. start: state.start,
  2294. end: state.end,
  2295. context: [this.curContext()],
  2296. inType: state.inType,
  2297. startLoc: state.startLoc,
  2298. lastTokEndLoc: state.lastTokEndLoc,
  2299. curLine: state.curLine,
  2300. lineStart: state.lineStart,
  2301. curPosition: state.curPosition
  2302. };
  2303. }
  2304. lookahead() {
  2305. const old = this.state;
  2306. this.state = this.createLookaheadState(old);
  2307. this.isLookahead = true;
  2308. this.nextToken();
  2309. this.isLookahead = false;
  2310. const curr = this.state;
  2311. this.state = old;
  2312. return curr;
  2313. }
  2314. nextTokenStart() {
  2315. return this.nextTokenStartSince(this.state.pos);
  2316. }
  2317. nextTokenStartSince(pos) {
  2318. skipWhiteSpace.lastIndex = pos;
  2319. return skipWhiteSpace.test(this.input) ? skipWhiteSpace.lastIndex : pos;
  2320. }
  2321. lookaheadCharCode() {
  2322. return this.input.charCodeAt(this.nextTokenStart());
  2323. }
  2324. codePointAtPos(pos) {
  2325. let cp = this.input.charCodeAt(pos);
  2326. if ((cp & 0xfc00) === 0xd800 && ++pos < this.input.length) {
  2327. const trail = this.input.charCodeAt(pos);
  2328. if ((trail & 0xfc00) === 0xdc00) {
  2329. cp = 0x10000 + ((cp & 0x3ff) << 10) + (trail & 0x3ff);
  2330. }
  2331. }
  2332. return cp;
  2333. }
  2334. setStrict(strict) {
  2335. this.state.strict = strict;
  2336. if (strict) {
  2337. this.state.strictErrors.forEach(([toParseError, at]) => this.raise(toParseError, {
  2338. at
  2339. }));
  2340. this.state.strictErrors.clear();
  2341. }
  2342. }
  2343. curContext() {
  2344. return this.state.context[this.state.context.length - 1];
  2345. }
  2346. nextToken() {
  2347. this.skipSpace();
  2348. this.state.start = this.state.pos;
  2349. if (!this.isLookahead) this.state.startLoc = this.state.curPosition();
  2350. if (this.state.pos >= this.length) {
  2351. this.finishToken(137);
  2352. return;
  2353. }
  2354. this.getTokenFromCode(this.codePointAtPos(this.state.pos));
  2355. }
  2356. skipBlockComment(commentEnd) {
  2357. let startLoc;
  2358. if (!this.isLookahead) startLoc = this.state.curPosition();
  2359. const start = this.state.pos;
  2360. const end = this.input.indexOf(commentEnd, start + 2);
  2361. if (end === -1) {
  2362. throw this.raise(Errors.UnterminatedComment, {
  2363. at: this.state.curPosition()
  2364. });
  2365. }
  2366. this.state.pos = end + commentEnd.length;
  2367. lineBreakG.lastIndex = start + 2;
  2368. while (lineBreakG.test(this.input) && lineBreakG.lastIndex <= end) {
  2369. ++this.state.curLine;
  2370. this.state.lineStart = lineBreakG.lastIndex;
  2371. }
  2372. if (this.isLookahead) return;
  2373. const comment = {
  2374. type: "CommentBlock",
  2375. value: this.input.slice(start + 2, end),
  2376. start,
  2377. end: end + commentEnd.length,
  2378. loc: new SourceLocation(startLoc, this.state.curPosition())
  2379. };
  2380. if (this.options.tokens) this.pushToken(comment);
  2381. return comment;
  2382. }
  2383. skipLineComment(startSkip) {
  2384. const start = this.state.pos;
  2385. let startLoc;
  2386. if (!this.isLookahead) startLoc = this.state.curPosition();
  2387. let ch = this.input.charCodeAt(this.state.pos += startSkip);
  2388. if (this.state.pos < this.length) {
  2389. while (!isNewLine(ch) && ++this.state.pos < this.length) {
  2390. ch = this.input.charCodeAt(this.state.pos);
  2391. }
  2392. }
  2393. if (this.isLookahead) return;
  2394. const end = this.state.pos;
  2395. const value = this.input.slice(start + startSkip, end);
  2396. const comment = {
  2397. type: "CommentLine",
  2398. value,
  2399. start,
  2400. end,
  2401. loc: new SourceLocation(startLoc, this.state.curPosition())
  2402. };
  2403. if (this.options.tokens) this.pushToken(comment);
  2404. return comment;
  2405. }
  2406. skipSpace() {
  2407. const spaceStart = this.state.pos;
  2408. const comments = [];
  2409. loop: while (this.state.pos < this.length) {
  2410. const ch = this.input.charCodeAt(this.state.pos);
  2411. switch (ch) {
  2412. case 32:
  2413. case 160:
  2414. case 9:
  2415. ++this.state.pos;
  2416. break;
  2417. case 13:
  2418. if (this.input.charCodeAt(this.state.pos + 1) === 10) {
  2419. ++this.state.pos;
  2420. }
  2421. case 10:
  2422. case 8232:
  2423. case 8233:
  2424. ++this.state.pos;
  2425. ++this.state.curLine;
  2426. this.state.lineStart = this.state.pos;
  2427. break;
  2428. case 47:
  2429. switch (this.input.charCodeAt(this.state.pos + 1)) {
  2430. case 42:
  2431. {
  2432. const comment = this.skipBlockComment("*/");
  2433. if (comment !== undefined) {
  2434. this.addComment(comment);
  2435. if (this.options.attachComment) comments.push(comment);
  2436. }
  2437. break;
  2438. }
  2439. case 47:
  2440. {
  2441. const comment = this.skipLineComment(2);
  2442. if (comment !== undefined) {
  2443. this.addComment(comment);
  2444. if (this.options.attachComment) comments.push(comment);
  2445. }
  2446. break;
  2447. }
  2448. default:
  2449. break loop;
  2450. }
  2451. break;
  2452. default:
  2453. if (isWhitespace(ch)) {
  2454. ++this.state.pos;
  2455. } else if (ch === 45 && !this.inModule && this.options.annexB) {
  2456. const pos = this.state.pos;
  2457. if (this.input.charCodeAt(pos + 1) === 45 && this.input.charCodeAt(pos + 2) === 62 && (spaceStart === 0 || this.state.lineStart > spaceStart)) {
  2458. const comment = this.skipLineComment(3);
  2459. if (comment !== undefined) {
  2460. this.addComment(comment);
  2461. if (this.options.attachComment) comments.push(comment);
  2462. }
  2463. } else {
  2464. break loop;
  2465. }
  2466. } else if (ch === 60 && !this.inModule && this.options.annexB) {
  2467. const pos = this.state.pos;
  2468. if (this.input.charCodeAt(pos + 1) === 33 && this.input.charCodeAt(pos + 2) === 45 && this.input.charCodeAt(pos + 3) === 45) {
  2469. const comment = this.skipLineComment(4);
  2470. if (comment !== undefined) {
  2471. this.addComment(comment);
  2472. if (this.options.attachComment) comments.push(comment);
  2473. }
  2474. } else {
  2475. break loop;
  2476. }
  2477. } else {
  2478. break loop;
  2479. }
  2480. }
  2481. }
  2482. if (comments.length > 0) {
  2483. const end = this.state.pos;
  2484. const commentWhitespace = {
  2485. start: spaceStart,
  2486. end,
  2487. comments,
  2488. leadingNode: null,
  2489. trailingNode: null,
  2490. containingNode: null
  2491. };
  2492. this.state.commentStack.push(commentWhitespace);
  2493. }
  2494. }
  2495. finishToken(type, val) {
  2496. this.state.end = this.state.pos;
  2497. this.state.endLoc = this.state.curPosition();
  2498. const prevType = this.state.type;
  2499. this.state.type = type;
  2500. this.state.value = val;
  2501. if (!this.isLookahead) {
  2502. this.updateContext(prevType);
  2503. }
  2504. }
  2505. replaceToken(type) {
  2506. this.state.type = type;
  2507. this.updateContext();
  2508. }
  2509. readToken_numberSign() {
  2510. if (this.state.pos === 0 && this.readToken_interpreter()) {
  2511. return;
  2512. }
  2513. const nextPos = this.state.pos + 1;
  2514. const next = this.codePointAtPos(nextPos);
  2515. if (next >= 48 && next <= 57) {
  2516. throw this.raise(Errors.UnexpectedDigitAfterHash, {
  2517. at: this.state.curPosition()
  2518. });
  2519. }
  2520. if (next === 123 || next === 91 && this.hasPlugin("recordAndTuple")) {
  2521. this.expectPlugin("recordAndTuple");
  2522. if (this.getPluginOption("recordAndTuple", "syntaxType") === "bar") {
  2523. throw this.raise(next === 123 ? Errors.RecordExpressionHashIncorrectStartSyntaxType : Errors.TupleExpressionHashIncorrectStartSyntaxType, {
  2524. at: this.state.curPosition()
  2525. });
  2526. }
  2527. this.state.pos += 2;
  2528. if (next === 123) {
  2529. this.finishToken(7);
  2530. } else {
  2531. this.finishToken(1);
  2532. }
  2533. } else if (isIdentifierStart(next)) {
  2534. ++this.state.pos;
  2535. this.finishToken(136, this.readWord1(next));
  2536. } else if (next === 92) {
  2537. ++this.state.pos;
  2538. this.finishToken(136, this.readWord1());
  2539. } else {
  2540. this.finishOp(27, 1);
  2541. }
  2542. }
  2543. readToken_dot() {
  2544. const next = this.input.charCodeAt(this.state.pos + 1);
  2545. if (next >= 48 && next <= 57) {
  2546. this.readNumber(true);
  2547. return;
  2548. }
  2549. if (next === 46 && this.input.charCodeAt(this.state.pos + 2) === 46) {
  2550. this.state.pos += 3;
  2551. this.finishToken(21);
  2552. } else {
  2553. ++this.state.pos;
  2554. this.finishToken(16);
  2555. }
  2556. }
  2557. readToken_slash() {
  2558. const next = this.input.charCodeAt(this.state.pos + 1);
  2559. if (next === 61) {
  2560. this.finishOp(31, 2);
  2561. } else {
  2562. this.finishOp(56, 1);
  2563. }
  2564. }
  2565. readToken_interpreter() {
  2566. if (this.state.pos !== 0 || this.length < 2) return false;
  2567. let ch = this.input.charCodeAt(this.state.pos + 1);
  2568. if (ch !== 33) return false;
  2569. const start = this.state.pos;
  2570. this.state.pos += 1;
  2571. while (!isNewLine(ch) && ++this.state.pos < this.length) {
  2572. ch = this.input.charCodeAt(this.state.pos);
  2573. }
  2574. const value = this.input.slice(start + 2, this.state.pos);
  2575. this.finishToken(28, value);
  2576. return true;
  2577. }
  2578. readToken_mult_modulo(code) {
  2579. let type = code === 42 ? 55 : 54;
  2580. let width = 1;
  2581. let next = this.input.charCodeAt(this.state.pos + 1);
  2582. if (code === 42 && next === 42) {
  2583. width++;
  2584. next = this.input.charCodeAt(this.state.pos + 2);
  2585. type = 57;
  2586. }
  2587. if (next === 61 && !this.state.inType) {
  2588. width++;
  2589. type = code === 37 ? 33 : 30;
  2590. }
  2591. this.finishOp(type, width);
  2592. }
  2593. readToken_pipe_amp(code) {
  2594. const next = this.input.charCodeAt(this.state.pos + 1);
  2595. if (next === code) {
  2596. if (this.input.charCodeAt(this.state.pos + 2) === 61) {
  2597. this.finishOp(30, 3);
  2598. } else {
  2599. this.finishOp(code === 124 ? 41 : 42, 2);
  2600. }
  2601. return;
  2602. }
  2603. if (code === 124) {
  2604. if (next === 62) {
  2605. this.finishOp(39, 2);
  2606. return;
  2607. }
  2608. if (this.hasPlugin("recordAndTuple") && next === 125) {
  2609. if (this.getPluginOption("recordAndTuple", "syntaxType") !== "bar") {
  2610. throw this.raise(Errors.RecordExpressionBarIncorrectEndSyntaxType, {
  2611. at: this.state.curPosition()
  2612. });
  2613. }
  2614. this.state.pos += 2;
  2615. this.finishToken(9);
  2616. return;
  2617. }
  2618. if (this.hasPlugin("recordAndTuple") && next === 93) {
  2619. if (this.getPluginOption("recordAndTuple", "syntaxType") !== "bar") {
  2620. throw this.raise(Errors.TupleExpressionBarIncorrectEndSyntaxType, {
  2621. at: this.state.curPosition()
  2622. });
  2623. }
  2624. this.state.pos += 2;
  2625. this.finishToken(4);
  2626. return;
  2627. }
  2628. }
  2629. if (next === 61) {
  2630. this.finishOp(30, 2);
  2631. return;
  2632. }
  2633. this.finishOp(code === 124 ? 43 : 45, 1);
  2634. }
  2635. readToken_caret() {
  2636. const next = this.input.charCodeAt(this.state.pos + 1);
  2637. if (next === 61 && !this.state.inType) {
  2638. this.finishOp(32, 2);
  2639. } else if (next === 94 && this.hasPlugin(["pipelineOperator", {
  2640. proposal: "hack",
  2641. topicToken: "^^"
  2642. }])) {
  2643. this.finishOp(37, 2);
  2644. const lookaheadCh = this.input.codePointAt(this.state.pos);
  2645. if (lookaheadCh === 94) {
  2646. throw this.unexpected();
  2647. }
  2648. } else {
  2649. this.finishOp(44, 1);
  2650. }
  2651. }
  2652. readToken_atSign() {
  2653. const next = this.input.charCodeAt(this.state.pos + 1);
  2654. if (next === 64 && this.hasPlugin(["pipelineOperator", {
  2655. proposal: "hack",
  2656. topicToken: "@@"
  2657. }])) {
  2658. this.finishOp(38, 2);
  2659. } else {
  2660. this.finishOp(26, 1);
  2661. }
  2662. }
  2663. readToken_plus_min(code) {
  2664. const next = this.input.charCodeAt(this.state.pos + 1);
  2665. if (next === code) {
  2666. this.finishOp(34, 2);
  2667. return;
  2668. }
  2669. if (next === 61) {
  2670. this.finishOp(30, 2);
  2671. } else {
  2672. this.finishOp(53, 1);
  2673. }
  2674. }
  2675. readToken_lt() {
  2676. const {
  2677. pos
  2678. } = this.state;
  2679. const next = this.input.charCodeAt(pos + 1);
  2680. if (next === 60) {
  2681. if (this.input.charCodeAt(pos + 2) === 61) {
  2682. this.finishOp(30, 3);
  2683. return;
  2684. }
  2685. this.finishOp(51, 2);
  2686. return;
  2687. }
  2688. if (next === 61) {
  2689. this.finishOp(49, 2);
  2690. return;
  2691. }
  2692. this.finishOp(47, 1);
  2693. }
  2694. readToken_gt() {
  2695. const {
  2696. pos
  2697. } = this.state;
  2698. const next = this.input.charCodeAt(pos + 1);
  2699. if (next === 62) {
  2700. const size = this.input.charCodeAt(pos + 2) === 62 ? 3 : 2;
  2701. if (this.input.charCodeAt(pos + size) === 61) {
  2702. this.finishOp(30, size + 1);
  2703. return;
  2704. }
  2705. this.finishOp(52, size);
  2706. return;
  2707. }
  2708. if (next === 61) {
  2709. this.finishOp(49, 2);
  2710. return;
  2711. }
  2712. this.finishOp(48, 1);
  2713. }
  2714. readToken_eq_excl(code) {
  2715. const next = this.input.charCodeAt(this.state.pos + 1);
  2716. if (next === 61) {
  2717. this.finishOp(46, this.input.charCodeAt(this.state.pos + 2) === 61 ? 3 : 2);
  2718. return;
  2719. }
  2720. if (code === 61 && next === 62) {
  2721. this.state.pos += 2;
  2722. this.finishToken(19);
  2723. return;
  2724. }
  2725. this.finishOp(code === 61 ? 29 : 35, 1);
  2726. }
  2727. readToken_question() {
  2728. const next = this.input.charCodeAt(this.state.pos + 1);
  2729. const next2 = this.input.charCodeAt(this.state.pos + 2);
  2730. if (next === 63) {
  2731. if (next2 === 61) {
  2732. this.finishOp(30, 3);
  2733. } else {
  2734. this.finishOp(40, 2);
  2735. }
  2736. } else if (next === 46 && !(next2 >= 48 && next2 <= 57)) {
  2737. this.state.pos += 2;
  2738. this.finishToken(18);
  2739. } else {
  2740. ++this.state.pos;
  2741. this.finishToken(17);
  2742. }
  2743. }
  2744. getTokenFromCode(code) {
  2745. switch (code) {
  2746. case 46:
  2747. this.readToken_dot();
  2748. return;
  2749. case 40:
  2750. ++this.state.pos;
  2751. this.finishToken(10);
  2752. return;
  2753. case 41:
  2754. ++this.state.pos;
  2755. this.finishToken(11);
  2756. return;
  2757. case 59:
  2758. ++this.state.pos;
  2759. this.finishToken(13);
  2760. return;
  2761. case 44:
  2762. ++this.state.pos;
  2763. this.finishToken(12);
  2764. return;
  2765. case 91:
  2766. if (this.hasPlugin("recordAndTuple") && this.input.charCodeAt(this.state.pos + 1) === 124) {
  2767. if (this.getPluginOption("recordAndTuple", "syntaxType") !== "bar") {
  2768. throw this.raise(Errors.TupleExpressionBarIncorrectStartSyntaxType, {
  2769. at: this.state.curPosition()
  2770. });
  2771. }
  2772. this.state.pos += 2;
  2773. this.finishToken(2);
  2774. } else {
  2775. ++this.state.pos;
  2776. this.finishToken(0);
  2777. }
  2778. return;
  2779. case 93:
  2780. ++this.state.pos;
  2781. this.finishToken(3);
  2782. return;
  2783. case 123:
  2784. if (this.hasPlugin("recordAndTuple") && this.input.charCodeAt(this.state.pos + 1) === 124) {
  2785. if (this.getPluginOption("recordAndTuple", "syntaxType") !== "bar") {
  2786. throw this.raise(Errors.RecordExpressionBarIncorrectStartSyntaxType, {
  2787. at: this.state.curPosition()
  2788. });
  2789. }
  2790. this.state.pos += 2;
  2791. this.finishToken(6);
  2792. } else {
  2793. ++this.state.pos;
  2794. this.finishToken(5);
  2795. }
  2796. return;
  2797. case 125:
  2798. ++this.state.pos;
  2799. this.finishToken(8);
  2800. return;
  2801. case 58:
  2802. if (this.hasPlugin("functionBind") && this.input.charCodeAt(this.state.pos + 1) === 58) {
  2803. this.finishOp(15, 2);
  2804. } else {
  2805. ++this.state.pos;
  2806. this.finishToken(14);
  2807. }
  2808. return;
  2809. case 63:
  2810. this.readToken_question();
  2811. return;
  2812. case 96:
  2813. this.readTemplateToken();
  2814. return;
  2815. case 48:
  2816. {
  2817. const next = this.input.charCodeAt(this.state.pos + 1);
  2818. if (next === 120 || next === 88) {
  2819. this.readRadixNumber(16);
  2820. return;
  2821. }
  2822. if (next === 111 || next === 79) {
  2823. this.readRadixNumber(8);
  2824. return;
  2825. }
  2826. if (next === 98 || next === 66) {
  2827. this.readRadixNumber(2);
  2828. return;
  2829. }
  2830. }
  2831. case 49:
  2832. case 50:
  2833. case 51:
  2834. case 52:
  2835. case 53:
  2836. case 54:
  2837. case 55:
  2838. case 56:
  2839. case 57:
  2840. this.readNumber(false);
  2841. return;
  2842. case 34:
  2843. case 39:
  2844. this.readString(code);
  2845. return;
  2846. case 47:
  2847. this.readToken_slash();
  2848. return;
  2849. case 37:
  2850. case 42:
  2851. this.readToken_mult_modulo(code);
  2852. return;
  2853. case 124:
  2854. case 38:
  2855. this.readToken_pipe_amp(code);
  2856. return;
  2857. case 94:
  2858. this.readToken_caret();
  2859. return;
  2860. case 43:
  2861. case 45:
  2862. this.readToken_plus_min(code);
  2863. return;
  2864. case 60:
  2865. this.readToken_lt();
  2866. return;
  2867. case 62:
  2868. this.readToken_gt();
  2869. return;
  2870. case 61:
  2871. case 33:
  2872. this.readToken_eq_excl(code);
  2873. return;
  2874. case 126:
  2875. this.finishOp(36, 1);
  2876. return;
  2877. case 64:
  2878. this.readToken_atSign();
  2879. return;
  2880. case 35:
  2881. this.readToken_numberSign();
  2882. return;
  2883. case 92:
  2884. this.readWord();
  2885. return;
  2886. default:
  2887. if (isIdentifierStart(code)) {
  2888. this.readWord(code);
  2889. return;
  2890. }
  2891. }
  2892. throw this.raise(Errors.InvalidOrUnexpectedToken, {
  2893. at: this.state.curPosition(),
  2894. unexpected: String.fromCodePoint(code)
  2895. });
  2896. }
  2897. finishOp(type, size) {
  2898. const str = this.input.slice(this.state.pos, this.state.pos + size);
  2899. this.state.pos += size;
  2900. this.finishToken(type, str);
  2901. }
  2902. readRegexp() {
  2903. const startLoc = this.state.startLoc;
  2904. const start = this.state.start + 1;
  2905. let escaped, inClass;
  2906. let {
  2907. pos
  2908. } = this.state;
  2909. for (;; ++pos) {
  2910. if (pos >= this.length) {
  2911. throw this.raise(Errors.UnterminatedRegExp, {
  2912. at: createPositionWithColumnOffset(startLoc, 1)
  2913. });
  2914. }
  2915. const ch = this.input.charCodeAt(pos);
  2916. if (isNewLine(ch)) {
  2917. throw this.raise(Errors.UnterminatedRegExp, {
  2918. at: createPositionWithColumnOffset(startLoc, 1)
  2919. });
  2920. }
  2921. if (escaped) {
  2922. escaped = false;
  2923. } else {
  2924. if (ch === 91) {
  2925. inClass = true;
  2926. } else if (ch === 93 && inClass) {
  2927. inClass = false;
  2928. } else if (ch === 47 && !inClass) {
  2929. break;
  2930. }
  2931. escaped = ch === 92;
  2932. }
  2933. }
  2934. const content = this.input.slice(start, pos);
  2935. ++pos;
  2936. let mods = "";
  2937. const nextPos = () => createPositionWithColumnOffset(startLoc, pos + 2 - start);
  2938. while (pos < this.length) {
  2939. const cp = this.codePointAtPos(pos);
  2940. const char = String.fromCharCode(cp);
  2941. if (VALID_REGEX_FLAGS.has(cp)) {
  2942. if (cp === 118) {
  2943. this.expectPlugin("regexpUnicodeSets", nextPos());
  2944. if (mods.includes("u")) {
  2945. this.raise(Errors.IncompatibleRegExpUVFlags, {
  2946. at: nextPos()
  2947. });
  2948. }
  2949. } else if (cp === 117) {
  2950. if (mods.includes("v")) {
  2951. this.raise(Errors.IncompatibleRegExpUVFlags, {
  2952. at: nextPos()
  2953. });
  2954. }
  2955. }
  2956. if (mods.includes(char)) {
  2957. this.raise(Errors.DuplicateRegExpFlags, {
  2958. at: nextPos()
  2959. });
  2960. }
  2961. } else if (isIdentifierChar(cp) || cp === 92) {
  2962. this.raise(Errors.MalformedRegExpFlags, {
  2963. at: nextPos()
  2964. });
  2965. } else {
  2966. break;
  2967. }
  2968. ++pos;
  2969. mods += char;
  2970. }
  2971. this.state.pos = pos;
  2972. this.finishToken(135, {
  2973. pattern: content,
  2974. flags: mods
  2975. });
  2976. }
  2977. readInt(radix, len, forceLen = false, allowNumSeparator = true) {
  2978. const {
  2979. n,
  2980. pos
  2981. } = readInt(this.input, this.state.pos, this.state.lineStart, this.state.curLine, radix, len, forceLen, allowNumSeparator, this.errorHandlers_readInt, false);
  2982. this.state.pos = pos;
  2983. return n;
  2984. }
  2985. readRadixNumber(radix) {
  2986. const startLoc = this.state.curPosition();
  2987. let isBigInt = false;
  2988. this.state.pos += 2;
  2989. const val = this.readInt(radix);
  2990. if (val == null) {
  2991. this.raise(Errors.InvalidDigit, {
  2992. at: createPositionWithColumnOffset(startLoc, 2),
  2993. radix
  2994. });
  2995. }
  2996. const next = this.input.charCodeAt(this.state.pos);
  2997. if (next === 110) {
  2998. ++this.state.pos;
  2999. isBigInt = true;
  3000. } else if (next === 109) {
  3001. throw this.raise(Errors.InvalidDecimal, {
  3002. at: startLoc
  3003. });
  3004. }
  3005. if (isIdentifierStart(this.codePointAtPos(this.state.pos))) {
  3006. throw this.raise(Errors.NumberIdentifier, {
  3007. at: this.state.curPosition()
  3008. });
  3009. }
  3010. if (isBigInt) {
  3011. const str = this.input.slice(startLoc.index, this.state.pos).replace(/[_n]/g, "");
  3012. this.finishToken(133, str);
  3013. return;
  3014. }
  3015. this.finishToken(132, val);
  3016. }
  3017. readNumber(startsWithDot) {
  3018. const start = this.state.pos;
  3019. const startLoc = this.state.curPosition();
  3020. let isFloat = false;
  3021. let isBigInt = false;
  3022. let isDecimal = false;
  3023. let hasExponent = false;
  3024. let isOctal = false;
  3025. if (!startsWithDot && this.readInt(10) === null) {
  3026. this.raise(Errors.InvalidNumber, {
  3027. at: this.state.curPosition()
  3028. });
  3029. }
  3030. const hasLeadingZero = this.state.pos - start >= 2 && this.input.charCodeAt(start) === 48;
  3031. if (hasLeadingZero) {
  3032. const integer = this.input.slice(start, this.state.pos);
  3033. this.recordStrictModeErrors(Errors.StrictOctalLiteral, {
  3034. at: startLoc
  3035. });
  3036. if (!this.state.strict) {
  3037. const underscorePos = integer.indexOf("_");
  3038. if (underscorePos > 0) {
  3039. this.raise(Errors.ZeroDigitNumericSeparator, {
  3040. at: createPositionWithColumnOffset(startLoc, underscorePos)
  3041. });
  3042. }
  3043. }
  3044. isOctal = hasLeadingZero && !/[89]/.test(integer);
  3045. }
  3046. let next = this.input.charCodeAt(this.state.pos);
  3047. if (next === 46 && !isOctal) {
  3048. ++this.state.pos;
  3049. this.readInt(10);
  3050. isFloat = true;
  3051. next = this.input.charCodeAt(this.state.pos);
  3052. }
  3053. if ((next === 69 || next === 101) && !isOctal) {
  3054. next = this.input.charCodeAt(++this.state.pos);
  3055. if (next === 43 || next === 45) {
  3056. ++this.state.pos;
  3057. }
  3058. if (this.readInt(10) === null) {
  3059. this.raise(Errors.InvalidOrMissingExponent, {
  3060. at: startLoc
  3061. });
  3062. }
  3063. isFloat = true;
  3064. hasExponent = true;
  3065. next = this.input.charCodeAt(this.state.pos);
  3066. }
  3067. if (next === 110) {
  3068. if (isFloat || hasLeadingZero) {
  3069. this.raise(Errors.InvalidBigIntLiteral, {
  3070. at: startLoc
  3071. });
  3072. }
  3073. ++this.state.pos;
  3074. isBigInt = true;
  3075. }
  3076. if (next === 109) {
  3077. this.expectPlugin("decimal", this.state.curPosition());
  3078. if (hasExponent || hasLeadingZero) {
  3079. this.raise(Errors.InvalidDecimal, {
  3080. at: startLoc
  3081. });
  3082. }
  3083. ++this.state.pos;
  3084. isDecimal = true;
  3085. }
  3086. if (isIdentifierStart(this.codePointAtPos(this.state.pos))) {
  3087. throw this.raise(Errors.NumberIdentifier, {
  3088. at: this.state.curPosition()
  3089. });
  3090. }
  3091. const str = this.input.slice(start, this.state.pos).replace(/[_mn]/g, "");
  3092. if (isBigInt) {
  3093. this.finishToken(133, str);
  3094. return;
  3095. }
  3096. if (isDecimal) {
  3097. this.finishToken(134, str);
  3098. return;
  3099. }
  3100. const val = isOctal ? parseInt(str, 8) : parseFloat(str);
  3101. this.finishToken(132, val);
  3102. }
  3103. readCodePoint(throwOnInvalid) {
  3104. const {
  3105. code,
  3106. pos
  3107. } = readCodePoint(this.input, this.state.pos, this.state.lineStart, this.state.curLine, throwOnInvalid, this.errorHandlers_readCodePoint);
  3108. this.state.pos = pos;
  3109. return code;
  3110. }
  3111. readString(quote) {
  3112. const {
  3113. str,
  3114. pos,
  3115. curLine,
  3116. lineStart
  3117. } = readStringContents(quote === 34 ? "double" : "single", this.input, this.state.pos + 1, this.state.lineStart, this.state.curLine, this.errorHandlers_readStringContents_string);
  3118. this.state.pos = pos + 1;
  3119. this.state.lineStart = lineStart;
  3120. this.state.curLine = curLine;
  3121. this.finishToken(131, str);
  3122. }
  3123. readTemplateContinuation() {
  3124. if (!this.match(8)) {
  3125. this.unexpected(null, 8);
  3126. }
  3127. this.state.pos--;
  3128. this.readTemplateToken();
  3129. }
  3130. readTemplateToken() {
  3131. const opening = this.input[this.state.pos];
  3132. const {
  3133. str,
  3134. firstInvalidLoc,
  3135. pos,
  3136. curLine,
  3137. lineStart
  3138. } = readStringContents("template", this.input, this.state.pos + 1, this.state.lineStart, this.state.curLine, this.errorHandlers_readStringContents_template);
  3139. this.state.pos = pos + 1;
  3140. this.state.lineStart = lineStart;
  3141. this.state.curLine = curLine;
  3142. if (firstInvalidLoc) {
  3143. this.state.firstInvalidTemplateEscapePos = new Position(firstInvalidLoc.curLine, firstInvalidLoc.pos - firstInvalidLoc.lineStart, firstInvalidLoc.pos);
  3144. }
  3145. if (this.input.codePointAt(pos) === 96) {
  3146. this.finishToken(24, firstInvalidLoc ? null : opening + str + "`");
  3147. } else {
  3148. this.state.pos++;
  3149. this.finishToken(25, firstInvalidLoc ? null : opening + str + "${");
  3150. }
  3151. }
  3152. recordStrictModeErrors(toParseError, {
  3153. at
  3154. }) {
  3155. const index = at.index;
  3156. if (this.state.strict && !this.state.strictErrors.has(index)) {
  3157. this.raise(toParseError, {
  3158. at
  3159. });
  3160. } else {
  3161. this.state.strictErrors.set(index, [toParseError, at]);
  3162. }
  3163. }
  3164. readWord1(firstCode) {
  3165. this.state.containsEsc = false;
  3166. let word = "";
  3167. const start = this.state.pos;
  3168. let chunkStart = this.state.pos;
  3169. if (firstCode !== undefined) {
  3170. this.state.pos += firstCode <= 0xffff ? 1 : 2;
  3171. }
  3172. while (this.state.pos < this.length) {
  3173. const ch = this.codePointAtPos(this.state.pos);
  3174. if (isIdentifierChar(ch)) {
  3175. this.state.pos += ch <= 0xffff ? 1 : 2;
  3176. } else if (ch === 92) {
  3177. this.state.containsEsc = true;
  3178. word += this.input.slice(chunkStart, this.state.pos);
  3179. const escStart = this.state.curPosition();
  3180. const identifierCheck = this.state.pos === start ? isIdentifierStart : isIdentifierChar;
  3181. if (this.input.charCodeAt(++this.state.pos) !== 117) {
  3182. this.raise(Errors.MissingUnicodeEscape, {
  3183. at: this.state.curPosition()
  3184. });
  3185. chunkStart = this.state.pos - 1;
  3186. continue;
  3187. }
  3188. ++this.state.pos;
  3189. const esc = this.readCodePoint(true);
  3190. if (esc !== null) {
  3191. if (!identifierCheck(esc)) {
  3192. this.raise(Errors.EscapedCharNotAnIdentifier, {
  3193. at: escStart
  3194. });
  3195. }
  3196. word += String.fromCodePoint(esc);
  3197. }
  3198. chunkStart = this.state.pos;
  3199. } else {
  3200. break;
  3201. }
  3202. }
  3203. return word + this.input.slice(chunkStart, this.state.pos);
  3204. }
  3205. readWord(firstCode) {
  3206. const word = this.readWord1(firstCode);
  3207. const type = keywords$1.get(word);
  3208. if (type !== undefined) {
  3209. this.finishToken(type, tokenLabelName(type));
  3210. } else {
  3211. this.finishToken(130, word);
  3212. }
  3213. }
  3214. checkKeywordEscapes() {
  3215. const {
  3216. type
  3217. } = this.state;
  3218. if (tokenIsKeyword(type) && this.state.containsEsc) {
  3219. this.raise(Errors.InvalidEscapedReservedWord, {
  3220. at: this.state.startLoc,
  3221. reservedWord: tokenLabelName(type)
  3222. });
  3223. }
  3224. }
  3225. raise(toParseError, raiseProperties) {
  3226. const {
  3227. at
  3228. } = raiseProperties,
  3229. details = _objectWithoutPropertiesLoose(raiseProperties, _excluded);
  3230. const loc = at instanceof Position ? at : at.loc.start;
  3231. const error = toParseError({
  3232. loc,
  3233. details
  3234. });
  3235. if (!this.options.errorRecovery) throw error;
  3236. if (!this.isLookahead) this.state.errors.push(error);
  3237. return error;
  3238. }
  3239. raiseOverwrite(toParseError, raiseProperties) {
  3240. const {
  3241. at
  3242. } = raiseProperties,
  3243. details = _objectWithoutPropertiesLoose(raiseProperties, _excluded2);
  3244. const loc = at instanceof Position ? at : at.loc.start;
  3245. const pos = loc.index;
  3246. const errors = this.state.errors;
  3247. for (let i = errors.length - 1; i >= 0; i--) {
  3248. const error = errors[i];
  3249. if (error.loc.index === pos) {
  3250. return errors[i] = toParseError({
  3251. loc,
  3252. details
  3253. });
  3254. }
  3255. if (error.loc.index < pos) break;
  3256. }
  3257. return this.raise(toParseError, raiseProperties);
  3258. }
  3259. updateContext(prevType) {}
  3260. unexpected(loc, type) {
  3261. throw this.raise(Errors.UnexpectedToken, {
  3262. expected: type ? tokenLabelName(type) : null,
  3263. at: loc != null ? loc : this.state.startLoc
  3264. });
  3265. }
  3266. expectPlugin(pluginName, loc) {
  3267. if (this.hasPlugin(pluginName)) {
  3268. return true;
  3269. }
  3270. throw this.raise(Errors.MissingPlugin, {
  3271. at: loc != null ? loc : this.state.startLoc,
  3272. missingPlugin: [pluginName]
  3273. });
  3274. }
  3275. expectOnePlugin(pluginNames) {
  3276. if (!pluginNames.some(name => this.hasPlugin(name))) {
  3277. throw this.raise(Errors.MissingOneOfPlugins, {
  3278. at: this.state.startLoc,
  3279. missingPlugin: pluginNames
  3280. });
  3281. }
  3282. }
  3283. errorBuilder(error) {
  3284. return (pos, lineStart, curLine) => {
  3285. this.raise(error, {
  3286. at: buildPosition(pos, lineStart, curLine)
  3287. });
  3288. };
  3289. }
  3290. }
  3291. class ClassScope {
  3292. constructor() {
  3293. this.privateNames = new Set();
  3294. this.loneAccessors = new Map();
  3295. this.undefinedPrivateNames = new Map();
  3296. }
  3297. }
  3298. class ClassScopeHandler {
  3299. constructor(parser) {
  3300. this.parser = void 0;
  3301. this.stack = [];
  3302. this.undefinedPrivateNames = new Map();
  3303. this.parser = parser;
  3304. }
  3305. current() {
  3306. return this.stack[this.stack.length - 1];
  3307. }
  3308. enter() {
  3309. this.stack.push(new ClassScope());
  3310. }
  3311. exit() {
  3312. const oldClassScope = this.stack.pop();
  3313. const current = this.current();
  3314. for (const [name, loc] of Array.from(oldClassScope.undefinedPrivateNames)) {
  3315. if (current) {
  3316. if (!current.undefinedPrivateNames.has(name)) {
  3317. current.undefinedPrivateNames.set(name, loc);
  3318. }
  3319. } else {
  3320. this.parser.raise(Errors.InvalidPrivateFieldResolution, {
  3321. at: loc,
  3322. identifierName: name
  3323. });
  3324. }
  3325. }
  3326. }
  3327. declarePrivateName(name, elementType, loc) {
  3328. const {
  3329. privateNames,
  3330. loneAccessors,
  3331. undefinedPrivateNames
  3332. } = this.current();
  3333. let redefined = privateNames.has(name);
  3334. if (elementType & CLASS_ELEMENT_KIND_ACCESSOR) {
  3335. const accessor = redefined && loneAccessors.get(name);
  3336. if (accessor) {
  3337. const oldStatic = accessor & CLASS_ELEMENT_FLAG_STATIC;
  3338. const newStatic = elementType & CLASS_ELEMENT_FLAG_STATIC;
  3339. const oldKind = accessor & CLASS_ELEMENT_KIND_ACCESSOR;
  3340. const newKind = elementType & CLASS_ELEMENT_KIND_ACCESSOR;
  3341. redefined = oldKind === newKind || oldStatic !== newStatic;
  3342. if (!redefined) loneAccessors.delete(name);
  3343. } else if (!redefined) {
  3344. loneAccessors.set(name, elementType);
  3345. }
  3346. }
  3347. if (redefined) {
  3348. this.parser.raise(Errors.PrivateNameRedeclaration, {
  3349. at: loc,
  3350. identifierName: name
  3351. });
  3352. }
  3353. privateNames.add(name);
  3354. undefinedPrivateNames.delete(name);
  3355. }
  3356. usePrivateName(name, loc) {
  3357. let classScope;
  3358. for (classScope of this.stack) {
  3359. if (classScope.privateNames.has(name)) return;
  3360. }
  3361. if (classScope) {
  3362. classScope.undefinedPrivateNames.set(name, loc);
  3363. } else {
  3364. this.parser.raise(Errors.InvalidPrivateFieldResolution, {
  3365. at: loc,
  3366. identifierName: name
  3367. });
  3368. }
  3369. }
  3370. }
  3371. const kExpression = 0,
  3372. kMaybeArrowParameterDeclaration = 1,
  3373. kMaybeAsyncArrowParameterDeclaration = 2,
  3374. kParameterDeclaration = 3;
  3375. class ExpressionScope {
  3376. constructor(type = kExpression) {
  3377. this.type = void 0;
  3378. this.type = type;
  3379. }
  3380. canBeArrowParameterDeclaration() {
  3381. return this.type === kMaybeAsyncArrowParameterDeclaration || this.type === kMaybeArrowParameterDeclaration;
  3382. }
  3383. isCertainlyParameterDeclaration() {
  3384. return this.type === kParameterDeclaration;
  3385. }
  3386. }
  3387. class ArrowHeadParsingScope extends ExpressionScope {
  3388. constructor(type) {
  3389. super(type);
  3390. this.declarationErrors = new Map();
  3391. }
  3392. recordDeclarationError(ParsingErrorClass, {
  3393. at
  3394. }) {
  3395. const index = at.index;
  3396. this.declarationErrors.set(index, [ParsingErrorClass, at]);
  3397. }
  3398. clearDeclarationError(index) {
  3399. this.declarationErrors.delete(index);
  3400. }
  3401. iterateErrors(iterator) {
  3402. this.declarationErrors.forEach(iterator);
  3403. }
  3404. }
  3405. class ExpressionScopeHandler {
  3406. constructor(parser) {
  3407. this.parser = void 0;
  3408. this.stack = [new ExpressionScope()];
  3409. this.parser = parser;
  3410. }
  3411. enter(scope) {
  3412. this.stack.push(scope);
  3413. }
  3414. exit() {
  3415. this.stack.pop();
  3416. }
  3417. recordParameterInitializerError(toParseError, {
  3418. at: node
  3419. }) {
  3420. const origin = {
  3421. at: node.loc.start
  3422. };
  3423. const {
  3424. stack
  3425. } = this;
  3426. let i = stack.length - 1;
  3427. let scope = stack[i];
  3428. while (!scope.isCertainlyParameterDeclaration()) {
  3429. if (scope.canBeArrowParameterDeclaration()) {
  3430. scope.recordDeclarationError(toParseError, origin);
  3431. } else {
  3432. return;
  3433. }
  3434. scope = stack[--i];
  3435. }
  3436. this.parser.raise(toParseError, origin);
  3437. }
  3438. recordArrowParameterBindingError(error, {
  3439. at: node
  3440. }) {
  3441. const {
  3442. stack
  3443. } = this;
  3444. const scope = stack[stack.length - 1];
  3445. const origin = {
  3446. at: node.loc.start
  3447. };
  3448. if (scope.isCertainlyParameterDeclaration()) {
  3449. this.parser.raise(error, origin);
  3450. } else if (scope.canBeArrowParameterDeclaration()) {
  3451. scope.recordDeclarationError(error, origin);
  3452. } else {
  3453. return;
  3454. }
  3455. }
  3456. recordAsyncArrowParametersError({
  3457. at
  3458. }) {
  3459. const {
  3460. stack
  3461. } = this;
  3462. let i = stack.length - 1;
  3463. let scope = stack[i];
  3464. while (scope.canBeArrowParameterDeclaration()) {
  3465. if (scope.type === kMaybeAsyncArrowParameterDeclaration) {
  3466. scope.recordDeclarationError(Errors.AwaitBindingIdentifier, {
  3467. at
  3468. });
  3469. }
  3470. scope = stack[--i];
  3471. }
  3472. }
  3473. validateAsPattern() {
  3474. const {
  3475. stack
  3476. } = this;
  3477. const currentScope = stack[stack.length - 1];
  3478. if (!currentScope.canBeArrowParameterDeclaration()) return;
  3479. currentScope.iterateErrors(([toParseError, loc]) => {
  3480. this.parser.raise(toParseError, {
  3481. at: loc
  3482. });
  3483. let i = stack.length - 2;
  3484. let scope = stack[i];
  3485. while (scope.canBeArrowParameterDeclaration()) {
  3486. scope.clearDeclarationError(loc.index);
  3487. scope = stack[--i];
  3488. }
  3489. });
  3490. }
  3491. }
  3492. function newParameterDeclarationScope() {
  3493. return new ExpressionScope(kParameterDeclaration);
  3494. }
  3495. function newArrowHeadScope() {
  3496. return new ArrowHeadParsingScope(kMaybeArrowParameterDeclaration);
  3497. }
  3498. function newAsyncArrowScope() {
  3499. return new ArrowHeadParsingScope(kMaybeAsyncArrowParameterDeclaration);
  3500. }
  3501. function newExpressionScope() {
  3502. return new ExpressionScope();
  3503. }
  3504. const PARAM = 0b0000,
  3505. PARAM_YIELD = 0b0001,
  3506. PARAM_AWAIT = 0b0010,
  3507. PARAM_RETURN = 0b0100,
  3508. PARAM_IN = 0b1000;
  3509. class ProductionParameterHandler {
  3510. constructor() {
  3511. this.stacks = [];
  3512. }
  3513. enter(flags) {
  3514. this.stacks.push(flags);
  3515. }
  3516. exit() {
  3517. this.stacks.pop();
  3518. }
  3519. currentFlags() {
  3520. return this.stacks[this.stacks.length - 1];
  3521. }
  3522. get hasAwait() {
  3523. return (this.currentFlags() & PARAM_AWAIT) > 0;
  3524. }
  3525. get hasYield() {
  3526. return (this.currentFlags() & PARAM_YIELD) > 0;
  3527. }
  3528. get hasReturn() {
  3529. return (this.currentFlags() & PARAM_RETURN) > 0;
  3530. }
  3531. get hasIn() {
  3532. return (this.currentFlags() & PARAM_IN) > 0;
  3533. }
  3534. }
  3535. function functionFlags(isAsync, isGenerator) {
  3536. return (isAsync ? PARAM_AWAIT : 0) | (isGenerator ? PARAM_YIELD : 0);
  3537. }
  3538. class UtilParser extends Tokenizer {
  3539. addExtra(node, key, value, enumerable = true) {
  3540. if (!node) return;
  3541. const extra = node.extra = node.extra || {};
  3542. if (enumerable) {
  3543. extra[key] = value;
  3544. } else {
  3545. Object.defineProperty(extra, key, {
  3546. enumerable,
  3547. value
  3548. });
  3549. }
  3550. }
  3551. isContextual(token) {
  3552. return this.state.type === token && !this.state.containsEsc;
  3553. }
  3554. isUnparsedContextual(nameStart, name) {
  3555. const nameEnd = nameStart + name.length;
  3556. if (this.input.slice(nameStart, nameEnd) === name) {
  3557. const nextCh = this.input.charCodeAt(nameEnd);
  3558. return !(isIdentifierChar(nextCh) || (nextCh & 0xfc00) === 0xd800);
  3559. }
  3560. return false;
  3561. }
  3562. isLookaheadContextual(name) {
  3563. const next = this.nextTokenStart();
  3564. return this.isUnparsedContextual(next, name);
  3565. }
  3566. eatContextual(token) {
  3567. if (this.isContextual(token)) {
  3568. this.next();
  3569. return true;
  3570. }
  3571. return false;
  3572. }
  3573. expectContextual(token, toParseError) {
  3574. if (!this.eatContextual(token)) {
  3575. if (toParseError != null) {
  3576. throw this.raise(toParseError, {
  3577. at: this.state.startLoc
  3578. });
  3579. }
  3580. throw this.unexpected(null, token);
  3581. }
  3582. }
  3583. canInsertSemicolon() {
  3584. return this.match(137) || this.match(8) || this.hasPrecedingLineBreak();
  3585. }
  3586. hasPrecedingLineBreak() {
  3587. return lineBreak.test(this.input.slice(this.state.lastTokEndLoc.index, this.state.start));
  3588. }
  3589. hasFollowingLineBreak() {
  3590. skipWhiteSpaceToLineBreak.lastIndex = this.state.end;
  3591. return skipWhiteSpaceToLineBreak.test(this.input);
  3592. }
  3593. isLineTerminator() {
  3594. return this.eat(13) || this.canInsertSemicolon();
  3595. }
  3596. semicolon(allowAsi = true) {
  3597. if (allowAsi ? this.isLineTerminator() : this.eat(13)) return;
  3598. this.raise(Errors.MissingSemicolon, {
  3599. at: this.state.lastTokEndLoc
  3600. });
  3601. }
  3602. expect(type, loc) {
  3603. this.eat(type) || this.unexpected(loc, type);
  3604. }
  3605. tryParse(fn, oldState = this.state.clone()) {
  3606. const abortSignal = {
  3607. node: null
  3608. };
  3609. try {
  3610. const node = fn((node = null) => {
  3611. abortSignal.node = node;
  3612. throw abortSignal;
  3613. });
  3614. if (this.state.errors.length > oldState.errors.length) {
  3615. const failState = this.state;
  3616. this.state = oldState;
  3617. this.state.tokensLength = failState.tokensLength;
  3618. return {
  3619. node,
  3620. error: failState.errors[oldState.errors.length],
  3621. thrown: false,
  3622. aborted: false,
  3623. failState
  3624. };
  3625. }
  3626. return {
  3627. node,
  3628. error: null,
  3629. thrown: false,
  3630. aborted: false,
  3631. failState: null
  3632. };
  3633. } catch (error) {
  3634. const failState = this.state;
  3635. this.state = oldState;
  3636. if (error instanceof SyntaxError) {
  3637. return {
  3638. node: null,
  3639. error,
  3640. thrown: true,
  3641. aborted: false,
  3642. failState
  3643. };
  3644. }
  3645. if (error === abortSignal) {
  3646. return {
  3647. node: abortSignal.node,
  3648. error: null,
  3649. thrown: false,
  3650. aborted: true,
  3651. failState
  3652. };
  3653. }
  3654. throw error;
  3655. }
  3656. }
  3657. checkExpressionErrors(refExpressionErrors, andThrow) {
  3658. if (!refExpressionErrors) return false;
  3659. const {
  3660. shorthandAssignLoc,
  3661. doubleProtoLoc,
  3662. privateKeyLoc,
  3663. optionalParametersLoc
  3664. } = refExpressionErrors;
  3665. const hasErrors = !!shorthandAssignLoc || !!doubleProtoLoc || !!optionalParametersLoc || !!privateKeyLoc;
  3666. if (!andThrow) {
  3667. return hasErrors;
  3668. }
  3669. if (shorthandAssignLoc != null) {
  3670. this.raise(Errors.InvalidCoverInitializedName, {
  3671. at: shorthandAssignLoc
  3672. });
  3673. }
  3674. if (doubleProtoLoc != null) {
  3675. this.raise(Errors.DuplicateProto, {
  3676. at: doubleProtoLoc
  3677. });
  3678. }
  3679. if (privateKeyLoc != null) {
  3680. this.raise(Errors.UnexpectedPrivateField, {
  3681. at: privateKeyLoc
  3682. });
  3683. }
  3684. if (optionalParametersLoc != null) {
  3685. this.unexpected(optionalParametersLoc);
  3686. }
  3687. }
  3688. isLiteralPropertyName() {
  3689. return tokenIsLiteralPropertyName(this.state.type);
  3690. }
  3691. isPrivateName(node) {
  3692. return node.type === "PrivateName";
  3693. }
  3694. getPrivateNameSV(node) {
  3695. return node.id.name;
  3696. }
  3697. hasPropertyAsPrivateName(node) {
  3698. return (node.type === "MemberExpression" || node.type === "OptionalMemberExpression") && this.isPrivateName(node.property);
  3699. }
  3700. isOptionalChain(node) {
  3701. return node.type === "OptionalMemberExpression" || node.type === "OptionalCallExpression";
  3702. }
  3703. isObjectProperty(node) {
  3704. return node.type === "ObjectProperty";
  3705. }
  3706. isObjectMethod(node) {
  3707. return node.type === "ObjectMethod";
  3708. }
  3709. initializeScopes(inModule = this.options.sourceType === "module") {
  3710. const oldLabels = this.state.labels;
  3711. this.state.labels = [];
  3712. const oldExportedIdentifiers = this.exportedIdentifiers;
  3713. this.exportedIdentifiers = new Set();
  3714. const oldInModule = this.inModule;
  3715. this.inModule = inModule;
  3716. const oldScope = this.scope;
  3717. const ScopeHandler = this.getScopeHandler();
  3718. this.scope = new ScopeHandler(this, inModule);
  3719. const oldProdParam = this.prodParam;
  3720. this.prodParam = new ProductionParameterHandler();
  3721. const oldClassScope = this.classScope;
  3722. this.classScope = new ClassScopeHandler(this);
  3723. const oldExpressionScope = this.expressionScope;
  3724. this.expressionScope = new ExpressionScopeHandler(this);
  3725. return () => {
  3726. this.state.labels = oldLabels;
  3727. this.exportedIdentifiers = oldExportedIdentifiers;
  3728. this.inModule = oldInModule;
  3729. this.scope = oldScope;
  3730. this.prodParam = oldProdParam;
  3731. this.classScope = oldClassScope;
  3732. this.expressionScope = oldExpressionScope;
  3733. };
  3734. }
  3735. enterInitialScopes() {
  3736. let paramFlags = PARAM;
  3737. if (this.inModule) {
  3738. paramFlags |= PARAM_AWAIT;
  3739. }
  3740. this.scope.enter(SCOPE_PROGRAM);
  3741. this.prodParam.enter(paramFlags);
  3742. }
  3743. checkDestructuringPrivate(refExpressionErrors) {
  3744. const {
  3745. privateKeyLoc
  3746. } = refExpressionErrors;
  3747. if (privateKeyLoc !== null) {
  3748. this.expectPlugin("destructuringPrivate", privateKeyLoc);
  3749. }
  3750. }
  3751. }
  3752. class ExpressionErrors {
  3753. constructor() {
  3754. this.shorthandAssignLoc = null;
  3755. this.doubleProtoLoc = null;
  3756. this.privateKeyLoc = null;
  3757. this.optionalParametersLoc = null;
  3758. }
  3759. }
  3760. class Node {
  3761. constructor(parser, pos, loc) {
  3762. this.type = "";
  3763. this.start = pos;
  3764. this.end = 0;
  3765. this.loc = new SourceLocation(loc);
  3766. if (parser != null && parser.options.ranges) this.range = [pos, 0];
  3767. if (parser != null && parser.filename) this.loc.filename = parser.filename;
  3768. }
  3769. }
  3770. const NodePrototype = Node.prototype;
  3771. {
  3772. NodePrototype.__clone = function () {
  3773. const newNode = new Node(undefined, this.start, this.loc.start);
  3774. const keys = Object.keys(this);
  3775. for (let i = 0, length = keys.length; i < length; i++) {
  3776. const key = keys[i];
  3777. if (key !== "leadingComments" && key !== "trailingComments" && key !== "innerComments") {
  3778. newNode[key] = this[key];
  3779. }
  3780. }
  3781. return newNode;
  3782. };
  3783. }
  3784. function clonePlaceholder(node) {
  3785. return cloneIdentifier(node);
  3786. }
  3787. function cloneIdentifier(node) {
  3788. const {
  3789. type,
  3790. start,
  3791. end,
  3792. loc,
  3793. range,
  3794. extra,
  3795. name
  3796. } = node;
  3797. const cloned = Object.create(NodePrototype);
  3798. cloned.type = type;
  3799. cloned.start = start;
  3800. cloned.end = end;
  3801. cloned.loc = loc;
  3802. cloned.range = range;
  3803. cloned.extra = extra;
  3804. cloned.name = name;
  3805. if (type === "Placeholder") {
  3806. cloned.expectedNode = node.expectedNode;
  3807. }
  3808. return cloned;
  3809. }
  3810. function cloneStringLiteral(node) {
  3811. const {
  3812. type,
  3813. start,
  3814. end,
  3815. loc,
  3816. range,
  3817. extra
  3818. } = node;
  3819. if (type === "Placeholder") {
  3820. return clonePlaceholder(node);
  3821. }
  3822. const cloned = Object.create(NodePrototype);
  3823. cloned.type = type;
  3824. cloned.start = start;
  3825. cloned.end = end;
  3826. cloned.loc = loc;
  3827. cloned.range = range;
  3828. if (node.raw !== undefined) {
  3829. cloned.raw = node.raw;
  3830. } else {
  3831. cloned.extra = extra;
  3832. }
  3833. cloned.value = node.value;
  3834. return cloned;
  3835. }
  3836. class NodeUtils extends UtilParser {
  3837. startNode() {
  3838. return new Node(this, this.state.start, this.state.startLoc);
  3839. }
  3840. startNodeAt(loc) {
  3841. return new Node(this, loc.index, loc);
  3842. }
  3843. startNodeAtNode(type) {
  3844. return this.startNodeAt(type.loc.start);
  3845. }
  3846. finishNode(node, type) {
  3847. return this.finishNodeAt(node, type, this.state.lastTokEndLoc);
  3848. }
  3849. finishNodeAt(node, type, endLoc) {
  3850. node.type = type;
  3851. node.end = endLoc.index;
  3852. node.loc.end = endLoc;
  3853. if (this.options.ranges) node.range[1] = endLoc.index;
  3854. if (this.options.attachComment) this.processComment(node);
  3855. return node;
  3856. }
  3857. resetStartLocation(node, startLoc) {
  3858. node.start = startLoc.index;
  3859. node.loc.start = startLoc;
  3860. if (this.options.ranges) node.range[0] = startLoc.index;
  3861. }
  3862. resetEndLocation(node, endLoc = this.state.lastTokEndLoc) {
  3863. node.end = endLoc.index;
  3864. node.loc.end = endLoc;
  3865. if (this.options.ranges) node.range[1] = endLoc.index;
  3866. }
  3867. resetStartLocationFromNode(node, locationNode) {
  3868. this.resetStartLocation(node, locationNode.loc.start);
  3869. }
  3870. }
  3871. const reservedTypes = new Set(["_", "any", "bool", "boolean", "empty", "extends", "false", "interface", "mixed", "null", "number", "static", "string", "true", "typeof", "void"]);
  3872. const FlowErrors = ParseErrorEnum`flow`({
  3873. AmbiguousConditionalArrow: "Ambiguous expression: wrap the arrow functions in parentheses to disambiguate.",
  3874. 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.",
  3875. AssignReservedType: ({
  3876. reservedType
  3877. }) => `Cannot overwrite reserved type ${reservedType}.`,
  3878. DeclareClassElement: "The `declare` modifier can only appear on class fields.",
  3879. DeclareClassFieldInitializer: "Initializers are not allowed in fields with the `declare` modifier.",
  3880. DuplicateDeclareModuleExports: "Duplicate `declare module.exports` statement.",
  3881. EnumBooleanMemberNotInitialized: ({
  3882. memberName,
  3883. enumName
  3884. }) => `Boolean enum members need to be initialized. Use either \`${memberName} = true,\` or \`${memberName} = false,\` in enum \`${enumName}\`.`,
  3885. EnumDuplicateMemberName: ({
  3886. memberName,
  3887. enumName
  3888. }) => `Enum member names need to be unique, but the name \`${memberName}\` has already been used before in enum \`${enumName}\`.`,
  3889. EnumInconsistentMemberValues: ({
  3890. enumName
  3891. }) => `Enum \`${enumName}\` has inconsistent member initializers. Either use no initializers, or consistently use literals (either booleans, numbers, or strings) for all member initializers.`,
  3892. EnumInvalidExplicitType: ({
  3893. invalidEnumType,
  3894. enumName
  3895. }) => `Enum type \`${invalidEnumType}\` is not valid. Use one of \`boolean\`, \`number\`, \`string\`, or \`symbol\` in enum \`${enumName}\`.`,
  3896. EnumInvalidExplicitTypeUnknownSupplied: ({
  3897. enumName
  3898. }) => `Supplied enum type is not valid. Use one of \`boolean\`, \`number\`, \`string\`, or \`symbol\` in enum \`${enumName}\`.`,
  3899. EnumInvalidMemberInitializerPrimaryType: ({
  3900. enumName,
  3901. memberName,
  3902. explicitType
  3903. }) => `Enum \`${enumName}\` has type \`${explicitType}\`, so the initializer of \`${memberName}\` needs to be a ${explicitType} literal.`,
  3904. EnumInvalidMemberInitializerSymbolType: ({
  3905. enumName,
  3906. memberName
  3907. }) => `Symbol enum members cannot be initialized. Use \`${memberName},\` in enum \`${enumName}\`.`,
  3908. EnumInvalidMemberInitializerUnknownType: ({
  3909. enumName,
  3910. memberName
  3911. }) => `The enum member initializer for \`${memberName}\` needs to be a literal (either a boolean, number, or string) in enum \`${enumName}\`.`,
  3912. EnumInvalidMemberName: ({
  3913. enumName,
  3914. memberName,
  3915. suggestion
  3916. }) => `Enum member names cannot start with lowercase 'a' through 'z'. Instead of using \`${memberName}\`, consider using \`${suggestion}\`, in enum \`${enumName}\`.`,
  3917. EnumNumberMemberNotInitialized: ({
  3918. enumName,
  3919. memberName
  3920. }) => `Number enum members need to be initialized, e.g. \`${memberName} = 1\` in enum \`${enumName}\`.`,
  3921. EnumStringMemberInconsistentlyInitailized: ({
  3922. enumName
  3923. }) => `String enum members need to consistently either all use initializers, or use no initializers, in enum \`${enumName}\`.`,
  3924. GetterMayNotHaveThisParam: "A getter cannot have a `this` parameter.",
  3925. ImportReflectionHasImportType: "An `import module` declaration can not use `type` or `typeof` keyword.",
  3926. 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.",
  3927. InexactInsideExact: "Explicit inexact syntax cannot appear inside an explicit exact object type.",
  3928. InexactInsideNonObject: "Explicit inexact syntax cannot appear in class or interface definitions.",
  3929. InexactVariance: "Explicit inexact syntax cannot have variance.",
  3930. InvalidNonTypeImportInDeclareModule: "Imports within a `declare module` body must always be `import type` or `import typeof`.",
  3931. MissingTypeParamDefault: "Type parameter declaration needs a default, since a preceding type parameter declaration has a default.",
  3932. NestedDeclareModule: "`declare module` cannot be used inside another `declare module`.",
  3933. NestedFlowComment: "Cannot have a flow comment inside another flow comment.",
  3934. PatternIsOptional: Object.assign({
  3935. message: "A binding pattern parameter cannot be optional in an implementation signature."
  3936. }, {
  3937. reasonCode: "OptionalBindingPattern"
  3938. }),
  3939. SetterMayNotHaveThisParam: "A setter cannot have a `this` parameter.",
  3940. SpreadVariance: "Spread properties cannot have variance.",
  3941. ThisParamAnnotationRequired: "A type annotation is required for the `this` parameter.",
  3942. ThisParamBannedInConstructor: "Constructors cannot have a `this` parameter; constructors don't bind `this` like other functions.",
  3943. ThisParamMayNotBeOptional: "The `this` parameter cannot be optional.",
  3944. ThisParamMustBeFirst: "The `this` parameter must be the first function parameter.",
  3945. ThisParamNoDefault: "The `this` parameter may not have a default value.",
  3946. TypeBeforeInitializer: "Type annotations must come before default assignments, e.g. instead of `age = 25: number` use `age: number = 25`.",
  3947. TypeCastInPattern: "The type cast expression is expected to be wrapped with parenthesis.",
  3948. UnexpectedExplicitInexactInObject: "Explicit inexact syntax must appear at the end of an inexact object.",
  3949. UnexpectedReservedType: ({
  3950. reservedType
  3951. }) => `Unexpected reserved type ${reservedType}.`,
  3952. UnexpectedReservedUnderscore: "`_` is only allowed as a type argument to call or new.",
  3953. UnexpectedSpaceBetweenModuloChecks: "Spaces between `%` and `checks` are not allowed here.",
  3954. UnexpectedSpreadType: "Spread operator cannot appear in class or interface definitions.",
  3955. UnexpectedSubtractionOperand: 'Unexpected token, expected "number" or "bigint".',
  3956. UnexpectedTokenAfterTypeParameter: "Expected an arrow function after this type parameter declaration.",
  3957. UnexpectedTypeParameterBeforeAsyncArrowFunction: "Type parameters must come after the async keyword, e.g. instead of `<T> async () => {}`, use `async <T>() => {}`.",
  3958. UnsupportedDeclareExportKind: ({
  3959. unsupportedExportKind,
  3960. suggestion
  3961. }) => `\`declare export ${unsupportedExportKind}\` is not supported. Use \`${suggestion}\` instead.`,
  3962. UnsupportedStatementInDeclareModule: "Only declares and type imports are allowed inside declare module.",
  3963. UnterminatedFlowComment: "Unterminated flow-comment."
  3964. });
  3965. function isEsModuleType(bodyElement) {
  3966. return bodyElement.type === "DeclareExportAllDeclaration" || bodyElement.type === "DeclareExportDeclaration" && (!bodyElement.declaration || bodyElement.declaration.type !== "TypeAlias" && bodyElement.declaration.type !== "InterfaceDeclaration");
  3967. }
  3968. function hasTypeImportKind(node) {
  3969. return node.importKind === "type" || node.importKind === "typeof";
  3970. }
  3971. function isMaybeDefaultImport(type) {
  3972. return tokenIsKeywordOrIdentifier(type) && type !== 97;
  3973. }
  3974. const exportSuggestions = {
  3975. const: "declare export var",
  3976. let: "declare export var",
  3977. type: "export type",
  3978. interface: "export interface"
  3979. };
  3980. function partition(list, test) {
  3981. const list1 = [];
  3982. const list2 = [];
  3983. for (let i = 0; i < list.length; i++) {
  3984. (test(list[i], i, list) ? list1 : list2).push(list[i]);
  3985. }
  3986. return [list1, list2];
  3987. }
  3988. const FLOW_PRAGMA_REGEX = /\*?\s*@((?:no)?flow)\b/;
  3989. var flow = (superClass => class FlowParserMixin extends superClass {
  3990. constructor(...args) {
  3991. super(...args);
  3992. this.flowPragma = undefined;
  3993. }
  3994. getScopeHandler() {
  3995. return FlowScopeHandler;
  3996. }
  3997. shouldParseTypes() {
  3998. return this.getPluginOption("flow", "all") || this.flowPragma === "flow";
  3999. }
  4000. shouldParseEnums() {
  4001. return !!this.getPluginOption("flow", "enums");
  4002. }
  4003. finishToken(type, val) {
  4004. if (type !== 131 && type !== 13 && type !== 28) {
  4005. if (this.flowPragma === undefined) {
  4006. this.flowPragma = null;
  4007. }
  4008. }
  4009. return super.finishToken(type, val);
  4010. }
  4011. addComment(comment) {
  4012. if (this.flowPragma === undefined) {
  4013. const matches = FLOW_PRAGMA_REGEX.exec(comment.value);
  4014. if (!matches) ; else if (matches[1] === "flow") {
  4015. this.flowPragma = "flow";
  4016. } else if (matches[1] === "noflow") {
  4017. this.flowPragma = "noflow";
  4018. } else {
  4019. throw new Error("Unexpected flow pragma");
  4020. }
  4021. }
  4022. return super.addComment(comment);
  4023. }
  4024. flowParseTypeInitialiser(tok) {
  4025. const oldInType = this.state.inType;
  4026. this.state.inType = true;
  4027. this.expect(tok || 14);
  4028. const type = this.flowParseType();
  4029. this.state.inType = oldInType;
  4030. return type;
  4031. }
  4032. flowParsePredicate() {
  4033. const node = this.startNode();
  4034. const moduloLoc = this.state.startLoc;
  4035. this.next();
  4036. this.expectContextual(108);
  4037. if (this.state.lastTokStart > moduloLoc.index + 1) {
  4038. this.raise(FlowErrors.UnexpectedSpaceBetweenModuloChecks, {
  4039. at: moduloLoc
  4040. });
  4041. }
  4042. if (this.eat(10)) {
  4043. node.value = super.parseExpression();
  4044. this.expect(11);
  4045. return this.finishNode(node, "DeclaredPredicate");
  4046. } else {
  4047. return this.finishNode(node, "InferredPredicate");
  4048. }
  4049. }
  4050. flowParseTypeAndPredicateInitialiser() {
  4051. const oldInType = this.state.inType;
  4052. this.state.inType = true;
  4053. this.expect(14);
  4054. let type = null;
  4055. let predicate = null;
  4056. if (this.match(54)) {
  4057. this.state.inType = oldInType;
  4058. predicate = this.flowParsePredicate();
  4059. } else {
  4060. type = this.flowParseType();
  4061. this.state.inType = oldInType;
  4062. if (this.match(54)) {
  4063. predicate = this.flowParsePredicate();
  4064. }
  4065. }
  4066. return [type, predicate];
  4067. }
  4068. flowParseDeclareClass(node) {
  4069. this.next();
  4070. this.flowParseInterfaceish(node, true);
  4071. return this.finishNode(node, "DeclareClass");
  4072. }
  4073. flowParseDeclareFunction(node) {
  4074. this.next();
  4075. const id = node.id = this.parseIdentifier();
  4076. const typeNode = this.startNode();
  4077. const typeContainer = this.startNode();
  4078. if (this.match(47)) {
  4079. typeNode.typeParameters = this.flowParseTypeParameterDeclaration();
  4080. } else {
  4081. typeNode.typeParameters = null;
  4082. }
  4083. this.expect(10);
  4084. const tmp = this.flowParseFunctionTypeParams();
  4085. typeNode.params = tmp.params;
  4086. typeNode.rest = tmp.rest;
  4087. typeNode.this = tmp._this;
  4088. this.expect(11);
  4089. [typeNode.returnType, node.predicate] = this.flowParseTypeAndPredicateInitialiser();
  4090. typeContainer.typeAnnotation = this.finishNode(typeNode, "FunctionTypeAnnotation");
  4091. id.typeAnnotation = this.finishNode(typeContainer, "TypeAnnotation");
  4092. this.resetEndLocation(id);
  4093. this.semicolon();
  4094. this.scope.declareName(node.id.name, BIND_FLOW_DECLARE_FN, node.id.loc.start);
  4095. return this.finishNode(node, "DeclareFunction");
  4096. }
  4097. flowParseDeclare(node, insideModule) {
  4098. if (this.match(80)) {
  4099. return this.flowParseDeclareClass(node);
  4100. } else if (this.match(68)) {
  4101. return this.flowParseDeclareFunction(node);
  4102. } else if (this.match(74)) {
  4103. return this.flowParseDeclareVariable(node);
  4104. } else if (this.eatContextual(125)) {
  4105. if (this.match(16)) {
  4106. return this.flowParseDeclareModuleExports(node);
  4107. } else {
  4108. if (insideModule) {
  4109. this.raise(FlowErrors.NestedDeclareModule, {
  4110. at: this.state.lastTokStartLoc
  4111. });
  4112. }
  4113. return this.flowParseDeclareModule(node);
  4114. }
  4115. } else if (this.isContextual(128)) {
  4116. return this.flowParseDeclareTypeAlias(node);
  4117. } else if (this.isContextual(129)) {
  4118. return this.flowParseDeclareOpaqueType(node);
  4119. } else if (this.isContextual(127)) {
  4120. return this.flowParseDeclareInterface(node);
  4121. } else if (this.match(82)) {
  4122. return this.flowParseDeclareExportDeclaration(node, insideModule);
  4123. } else {
  4124. throw this.unexpected();
  4125. }
  4126. }
  4127. flowParseDeclareVariable(node) {
  4128. this.next();
  4129. node.id = this.flowParseTypeAnnotatableIdentifier(true);
  4130. this.scope.declareName(node.id.name, BIND_VAR, node.id.loc.start);
  4131. this.semicolon();
  4132. return this.finishNode(node, "DeclareVariable");
  4133. }
  4134. flowParseDeclareModule(node) {
  4135. this.scope.enter(SCOPE_OTHER);
  4136. if (this.match(131)) {
  4137. node.id = super.parseExprAtom();
  4138. } else {
  4139. node.id = this.parseIdentifier();
  4140. }
  4141. const bodyNode = node.body = this.startNode();
  4142. const body = bodyNode.body = [];
  4143. this.expect(5);
  4144. while (!this.match(8)) {
  4145. let bodyNode = this.startNode();
  4146. if (this.match(83)) {
  4147. this.next();
  4148. if (!this.isContextual(128) && !this.match(87)) {
  4149. this.raise(FlowErrors.InvalidNonTypeImportInDeclareModule, {
  4150. at: this.state.lastTokStartLoc
  4151. });
  4152. }
  4153. super.parseImport(bodyNode);
  4154. } else {
  4155. this.expectContextual(123, FlowErrors.UnsupportedStatementInDeclareModule);
  4156. bodyNode = this.flowParseDeclare(bodyNode, true);
  4157. }
  4158. body.push(bodyNode);
  4159. }
  4160. this.scope.exit();
  4161. this.expect(8);
  4162. this.finishNode(bodyNode, "BlockStatement");
  4163. let kind = null;
  4164. let hasModuleExport = false;
  4165. body.forEach(bodyElement => {
  4166. if (isEsModuleType(bodyElement)) {
  4167. if (kind === "CommonJS") {
  4168. this.raise(FlowErrors.AmbiguousDeclareModuleKind, {
  4169. at: bodyElement
  4170. });
  4171. }
  4172. kind = "ES";
  4173. } else if (bodyElement.type === "DeclareModuleExports") {
  4174. if (hasModuleExport) {
  4175. this.raise(FlowErrors.DuplicateDeclareModuleExports, {
  4176. at: bodyElement
  4177. });
  4178. }
  4179. if (kind === "ES") {
  4180. this.raise(FlowErrors.AmbiguousDeclareModuleKind, {
  4181. at: bodyElement
  4182. });
  4183. }
  4184. kind = "CommonJS";
  4185. hasModuleExport = true;
  4186. }
  4187. });
  4188. node.kind = kind || "CommonJS";
  4189. return this.finishNode(node, "DeclareModule");
  4190. }
  4191. flowParseDeclareExportDeclaration(node, insideModule) {
  4192. this.expect(82);
  4193. if (this.eat(65)) {
  4194. if (this.match(68) || this.match(80)) {
  4195. node.declaration = this.flowParseDeclare(this.startNode());
  4196. } else {
  4197. node.declaration = this.flowParseType();
  4198. this.semicolon();
  4199. }
  4200. node.default = true;
  4201. return this.finishNode(node, "DeclareExportDeclaration");
  4202. } else {
  4203. if (this.match(75) || this.isLet() || (this.isContextual(128) || this.isContextual(127)) && !insideModule) {
  4204. const label = this.state.value;
  4205. throw this.raise(FlowErrors.UnsupportedDeclareExportKind, {
  4206. at: this.state.startLoc,
  4207. unsupportedExportKind: label,
  4208. suggestion: exportSuggestions[label]
  4209. });
  4210. }
  4211. if (this.match(74) || this.match(68) || this.match(80) || this.isContextual(129)) {
  4212. node.declaration = this.flowParseDeclare(this.startNode());
  4213. node.default = false;
  4214. return this.finishNode(node, "DeclareExportDeclaration");
  4215. } else if (this.match(55) || this.match(5) || this.isContextual(127) || this.isContextual(128) || this.isContextual(129)) {
  4216. node = this.parseExport(node, null);
  4217. if (node.type === "ExportNamedDeclaration") {
  4218. node.type = "ExportDeclaration";
  4219. node.default = false;
  4220. delete node.exportKind;
  4221. }
  4222. node.type = "Declare" + node.type;
  4223. return node;
  4224. }
  4225. }
  4226. throw this.unexpected();
  4227. }
  4228. flowParseDeclareModuleExports(node) {
  4229. this.next();
  4230. this.expectContextual(109);
  4231. node.typeAnnotation = this.flowParseTypeAnnotation();
  4232. this.semicolon();
  4233. return this.finishNode(node, "DeclareModuleExports");
  4234. }
  4235. flowParseDeclareTypeAlias(node) {
  4236. this.next();
  4237. const finished = this.flowParseTypeAlias(node);
  4238. finished.type = "DeclareTypeAlias";
  4239. return finished;
  4240. }
  4241. flowParseDeclareOpaqueType(node) {
  4242. this.next();
  4243. const finished = this.flowParseOpaqueType(node, true);
  4244. finished.type = "DeclareOpaqueType";
  4245. return finished;
  4246. }
  4247. flowParseDeclareInterface(node) {
  4248. this.next();
  4249. this.flowParseInterfaceish(node);
  4250. return this.finishNode(node, "DeclareInterface");
  4251. }
  4252. flowParseInterfaceish(node, isClass = false) {
  4253. node.id = this.flowParseRestrictedIdentifier(!isClass, true);
  4254. this.scope.declareName(node.id.name, isClass ? BIND_FUNCTION : BIND_LEXICAL, node.id.loc.start);
  4255. if (this.match(47)) {
  4256. node.typeParameters = this.flowParseTypeParameterDeclaration();
  4257. } else {
  4258. node.typeParameters = null;
  4259. }
  4260. node.extends = [];
  4261. node.implements = [];
  4262. node.mixins = [];
  4263. if (this.eat(81)) {
  4264. do {
  4265. node.extends.push(this.flowParseInterfaceExtends());
  4266. } while (!isClass && this.eat(12));
  4267. }
  4268. if (this.isContextual(115)) {
  4269. this.next();
  4270. do {
  4271. node.mixins.push(this.flowParseInterfaceExtends());
  4272. } while (this.eat(12));
  4273. }
  4274. if (this.isContextual(111)) {
  4275. this.next();
  4276. do {
  4277. node.implements.push(this.flowParseInterfaceExtends());
  4278. } while (this.eat(12));
  4279. }
  4280. node.body = this.flowParseObjectType({
  4281. allowStatic: isClass,
  4282. allowExact: false,
  4283. allowSpread: false,
  4284. allowProto: isClass,
  4285. allowInexact: false
  4286. });
  4287. }
  4288. flowParseInterfaceExtends() {
  4289. const node = this.startNode();
  4290. node.id = this.flowParseQualifiedTypeIdentifier();
  4291. if (this.match(47)) {
  4292. node.typeParameters = this.flowParseTypeParameterInstantiation();
  4293. } else {
  4294. node.typeParameters = null;
  4295. }
  4296. return this.finishNode(node, "InterfaceExtends");
  4297. }
  4298. flowParseInterface(node) {
  4299. this.flowParseInterfaceish(node);
  4300. return this.finishNode(node, "InterfaceDeclaration");
  4301. }
  4302. checkNotUnderscore(word) {
  4303. if (word === "_") {
  4304. this.raise(FlowErrors.UnexpectedReservedUnderscore, {
  4305. at: this.state.startLoc
  4306. });
  4307. }
  4308. }
  4309. checkReservedType(word, startLoc, declaration) {
  4310. if (!reservedTypes.has(word)) return;
  4311. this.raise(declaration ? FlowErrors.AssignReservedType : FlowErrors.UnexpectedReservedType, {
  4312. at: startLoc,
  4313. reservedType: word
  4314. });
  4315. }
  4316. flowParseRestrictedIdentifier(liberal, declaration) {
  4317. this.checkReservedType(this.state.value, this.state.startLoc, declaration);
  4318. return this.parseIdentifier(liberal);
  4319. }
  4320. flowParseTypeAlias(node) {
  4321. node.id = this.flowParseRestrictedIdentifier(false, true);
  4322. this.scope.declareName(node.id.name, BIND_LEXICAL, node.id.loc.start);
  4323. if (this.match(47)) {
  4324. node.typeParameters = this.flowParseTypeParameterDeclaration();
  4325. } else {
  4326. node.typeParameters = null;
  4327. }
  4328. node.right = this.flowParseTypeInitialiser(29);
  4329. this.semicolon();
  4330. return this.finishNode(node, "TypeAlias");
  4331. }
  4332. flowParseOpaqueType(node, declare) {
  4333. this.expectContextual(128);
  4334. node.id = this.flowParseRestrictedIdentifier(true, true);
  4335. this.scope.declareName(node.id.name, BIND_LEXICAL, node.id.loc.start);
  4336. if (this.match(47)) {
  4337. node.typeParameters = this.flowParseTypeParameterDeclaration();
  4338. } else {
  4339. node.typeParameters = null;
  4340. }
  4341. node.supertype = null;
  4342. if (this.match(14)) {
  4343. node.supertype = this.flowParseTypeInitialiser(14);
  4344. }
  4345. node.impltype = null;
  4346. if (!declare) {
  4347. node.impltype = this.flowParseTypeInitialiser(29);
  4348. }
  4349. this.semicolon();
  4350. return this.finishNode(node, "OpaqueType");
  4351. }
  4352. flowParseTypeParameter(requireDefault = false) {
  4353. const nodeStartLoc = this.state.startLoc;
  4354. const node = this.startNode();
  4355. const variance = this.flowParseVariance();
  4356. const ident = this.flowParseTypeAnnotatableIdentifier();
  4357. node.name = ident.name;
  4358. node.variance = variance;
  4359. node.bound = ident.typeAnnotation;
  4360. if (this.match(29)) {
  4361. this.eat(29);
  4362. node.default = this.flowParseType();
  4363. } else {
  4364. if (requireDefault) {
  4365. this.raise(FlowErrors.MissingTypeParamDefault, {
  4366. at: nodeStartLoc
  4367. });
  4368. }
  4369. }
  4370. return this.finishNode(node, "TypeParameter");
  4371. }
  4372. flowParseTypeParameterDeclaration() {
  4373. const oldInType = this.state.inType;
  4374. const node = this.startNode();
  4375. node.params = [];
  4376. this.state.inType = true;
  4377. if (this.match(47) || this.match(140)) {
  4378. this.next();
  4379. } else {
  4380. this.unexpected();
  4381. }
  4382. let defaultRequired = false;
  4383. do {
  4384. const typeParameter = this.flowParseTypeParameter(defaultRequired);
  4385. node.params.push(typeParameter);
  4386. if (typeParameter.default) {
  4387. defaultRequired = true;
  4388. }
  4389. if (!this.match(48)) {
  4390. this.expect(12);
  4391. }
  4392. } while (!this.match(48));
  4393. this.expect(48);
  4394. this.state.inType = oldInType;
  4395. return this.finishNode(node, "TypeParameterDeclaration");
  4396. }
  4397. flowParseTypeParameterInstantiation() {
  4398. const node = this.startNode();
  4399. const oldInType = this.state.inType;
  4400. node.params = [];
  4401. this.state.inType = true;
  4402. this.expect(47);
  4403. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  4404. this.state.noAnonFunctionType = false;
  4405. while (!this.match(48)) {
  4406. node.params.push(this.flowParseType());
  4407. if (!this.match(48)) {
  4408. this.expect(12);
  4409. }
  4410. }
  4411. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  4412. this.expect(48);
  4413. this.state.inType = oldInType;
  4414. return this.finishNode(node, "TypeParameterInstantiation");
  4415. }
  4416. flowParseTypeParameterInstantiationCallOrNew() {
  4417. const node = this.startNode();
  4418. const oldInType = this.state.inType;
  4419. node.params = [];
  4420. this.state.inType = true;
  4421. this.expect(47);
  4422. while (!this.match(48)) {
  4423. node.params.push(this.flowParseTypeOrImplicitInstantiation());
  4424. if (!this.match(48)) {
  4425. this.expect(12);
  4426. }
  4427. }
  4428. this.expect(48);
  4429. this.state.inType = oldInType;
  4430. return this.finishNode(node, "TypeParameterInstantiation");
  4431. }
  4432. flowParseInterfaceType() {
  4433. const node = this.startNode();
  4434. this.expectContextual(127);
  4435. node.extends = [];
  4436. if (this.eat(81)) {
  4437. do {
  4438. node.extends.push(this.flowParseInterfaceExtends());
  4439. } while (this.eat(12));
  4440. }
  4441. node.body = this.flowParseObjectType({
  4442. allowStatic: false,
  4443. allowExact: false,
  4444. allowSpread: false,
  4445. allowProto: false,
  4446. allowInexact: false
  4447. });
  4448. return this.finishNode(node, "InterfaceTypeAnnotation");
  4449. }
  4450. flowParseObjectPropertyKey() {
  4451. return this.match(132) || this.match(131) ? super.parseExprAtom() : this.parseIdentifier(true);
  4452. }
  4453. flowParseObjectTypeIndexer(node, isStatic, variance) {
  4454. node.static = isStatic;
  4455. if (this.lookahead().type === 14) {
  4456. node.id = this.flowParseObjectPropertyKey();
  4457. node.key = this.flowParseTypeInitialiser();
  4458. } else {
  4459. node.id = null;
  4460. node.key = this.flowParseType();
  4461. }
  4462. this.expect(3);
  4463. node.value = this.flowParseTypeInitialiser();
  4464. node.variance = variance;
  4465. return this.finishNode(node, "ObjectTypeIndexer");
  4466. }
  4467. flowParseObjectTypeInternalSlot(node, isStatic) {
  4468. node.static = isStatic;
  4469. node.id = this.flowParseObjectPropertyKey();
  4470. this.expect(3);
  4471. this.expect(3);
  4472. if (this.match(47) || this.match(10)) {
  4473. node.method = true;
  4474. node.optional = false;
  4475. node.value = this.flowParseObjectTypeMethodish(this.startNodeAt(node.loc.start));
  4476. } else {
  4477. node.method = false;
  4478. if (this.eat(17)) {
  4479. node.optional = true;
  4480. }
  4481. node.value = this.flowParseTypeInitialiser();
  4482. }
  4483. return this.finishNode(node, "ObjectTypeInternalSlot");
  4484. }
  4485. flowParseObjectTypeMethodish(node) {
  4486. node.params = [];
  4487. node.rest = null;
  4488. node.typeParameters = null;
  4489. node.this = null;
  4490. if (this.match(47)) {
  4491. node.typeParameters = this.flowParseTypeParameterDeclaration();
  4492. }
  4493. this.expect(10);
  4494. if (this.match(78)) {
  4495. node.this = this.flowParseFunctionTypeParam(true);
  4496. node.this.name = null;
  4497. if (!this.match(11)) {
  4498. this.expect(12);
  4499. }
  4500. }
  4501. while (!this.match(11) && !this.match(21)) {
  4502. node.params.push(this.flowParseFunctionTypeParam(false));
  4503. if (!this.match(11)) {
  4504. this.expect(12);
  4505. }
  4506. }
  4507. if (this.eat(21)) {
  4508. node.rest = this.flowParseFunctionTypeParam(false);
  4509. }
  4510. this.expect(11);
  4511. node.returnType = this.flowParseTypeInitialiser();
  4512. return this.finishNode(node, "FunctionTypeAnnotation");
  4513. }
  4514. flowParseObjectTypeCallProperty(node, isStatic) {
  4515. const valueNode = this.startNode();
  4516. node.static = isStatic;
  4517. node.value = this.flowParseObjectTypeMethodish(valueNode);
  4518. return this.finishNode(node, "ObjectTypeCallProperty");
  4519. }
  4520. flowParseObjectType({
  4521. allowStatic,
  4522. allowExact,
  4523. allowSpread,
  4524. allowProto,
  4525. allowInexact
  4526. }) {
  4527. const oldInType = this.state.inType;
  4528. this.state.inType = true;
  4529. const nodeStart = this.startNode();
  4530. nodeStart.callProperties = [];
  4531. nodeStart.properties = [];
  4532. nodeStart.indexers = [];
  4533. nodeStart.internalSlots = [];
  4534. let endDelim;
  4535. let exact;
  4536. let inexact = false;
  4537. if (allowExact && this.match(6)) {
  4538. this.expect(6);
  4539. endDelim = 9;
  4540. exact = true;
  4541. } else {
  4542. this.expect(5);
  4543. endDelim = 8;
  4544. exact = false;
  4545. }
  4546. nodeStart.exact = exact;
  4547. while (!this.match(endDelim)) {
  4548. let isStatic = false;
  4549. let protoStartLoc = null;
  4550. let inexactStartLoc = null;
  4551. const node = this.startNode();
  4552. if (allowProto && this.isContextual(116)) {
  4553. const lookahead = this.lookahead();
  4554. if (lookahead.type !== 14 && lookahead.type !== 17) {
  4555. this.next();
  4556. protoStartLoc = this.state.startLoc;
  4557. allowStatic = false;
  4558. }
  4559. }
  4560. if (allowStatic && this.isContextual(104)) {
  4561. const lookahead = this.lookahead();
  4562. if (lookahead.type !== 14 && lookahead.type !== 17) {
  4563. this.next();
  4564. isStatic = true;
  4565. }
  4566. }
  4567. const variance = this.flowParseVariance();
  4568. if (this.eat(0)) {
  4569. if (protoStartLoc != null) {
  4570. this.unexpected(protoStartLoc);
  4571. }
  4572. if (this.eat(0)) {
  4573. if (variance) {
  4574. this.unexpected(variance.loc.start);
  4575. }
  4576. nodeStart.internalSlots.push(this.flowParseObjectTypeInternalSlot(node, isStatic));
  4577. } else {
  4578. nodeStart.indexers.push(this.flowParseObjectTypeIndexer(node, isStatic, variance));
  4579. }
  4580. } else if (this.match(10) || this.match(47)) {
  4581. if (protoStartLoc != null) {
  4582. this.unexpected(protoStartLoc);
  4583. }
  4584. if (variance) {
  4585. this.unexpected(variance.loc.start);
  4586. }
  4587. nodeStart.callProperties.push(this.flowParseObjectTypeCallProperty(node, isStatic));
  4588. } else {
  4589. let kind = "init";
  4590. if (this.isContextual(98) || this.isContextual(103)) {
  4591. const lookahead = this.lookahead();
  4592. if (tokenIsLiteralPropertyName(lookahead.type)) {
  4593. kind = this.state.value;
  4594. this.next();
  4595. }
  4596. }
  4597. const propOrInexact = this.flowParseObjectTypeProperty(node, isStatic, protoStartLoc, variance, kind, allowSpread, allowInexact != null ? allowInexact : !exact);
  4598. if (propOrInexact === null) {
  4599. inexact = true;
  4600. inexactStartLoc = this.state.lastTokStartLoc;
  4601. } else {
  4602. nodeStart.properties.push(propOrInexact);
  4603. }
  4604. }
  4605. this.flowObjectTypeSemicolon();
  4606. if (inexactStartLoc && !this.match(8) && !this.match(9)) {
  4607. this.raise(FlowErrors.UnexpectedExplicitInexactInObject, {
  4608. at: inexactStartLoc
  4609. });
  4610. }
  4611. }
  4612. this.expect(endDelim);
  4613. if (allowSpread) {
  4614. nodeStart.inexact = inexact;
  4615. }
  4616. const out = this.finishNode(nodeStart, "ObjectTypeAnnotation");
  4617. this.state.inType = oldInType;
  4618. return out;
  4619. }
  4620. flowParseObjectTypeProperty(node, isStatic, protoStartLoc, variance, kind, allowSpread, allowInexact) {
  4621. if (this.eat(21)) {
  4622. const isInexactToken = this.match(12) || this.match(13) || this.match(8) || this.match(9);
  4623. if (isInexactToken) {
  4624. if (!allowSpread) {
  4625. this.raise(FlowErrors.InexactInsideNonObject, {
  4626. at: this.state.lastTokStartLoc
  4627. });
  4628. } else if (!allowInexact) {
  4629. this.raise(FlowErrors.InexactInsideExact, {
  4630. at: this.state.lastTokStartLoc
  4631. });
  4632. }
  4633. if (variance) {
  4634. this.raise(FlowErrors.InexactVariance, {
  4635. at: variance
  4636. });
  4637. }
  4638. return null;
  4639. }
  4640. if (!allowSpread) {
  4641. this.raise(FlowErrors.UnexpectedSpreadType, {
  4642. at: this.state.lastTokStartLoc
  4643. });
  4644. }
  4645. if (protoStartLoc != null) {
  4646. this.unexpected(protoStartLoc);
  4647. }
  4648. if (variance) {
  4649. this.raise(FlowErrors.SpreadVariance, {
  4650. at: variance
  4651. });
  4652. }
  4653. node.argument = this.flowParseType();
  4654. return this.finishNode(node, "ObjectTypeSpreadProperty");
  4655. } else {
  4656. node.key = this.flowParseObjectPropertyKey();
  4657. node.static = isStatic;
  4658. node.proto = protoStartLoc != null;
  4659. node.kind = kind;
  4660. let optional = false;
  4661. if (this.match(47) || this.match(10)) {
  4662. node.method = true;
  4663. if (protoStartLoc != null) {
  4664. this.unexpected(protoStartLoc);
  4665. }
  4666. if (variance) {
  4667. this.unexpected(variance.loc.start);
  4668. }
  4669. node.value = this.flowParseObjectTypeMethodish(this.startNodeAt(node.loc.start));
  4670. if (kind === "get" || kind === "set") {
  4671. this.flowCheckGetterSetterParams(node);
  4672. }
  4673. if (!allowSpread && node.key.name === "constructor" && node.value.this) {
  4674. this.raise(FlowErrors.ThisParamBannedInConstructor, {
  4675. at: node.value.this
  4676. });
  4677. }
  4678. } else {
  4679. if (kind !== "init") this.unexpected();
  4680. node.method = false;
  4681. if (this.eat(17)) {
  4682. optional = true;
  4683. }
  4684. node.value = this.flowParseTypeInitialiser();
  4685. node.variance = variance;
  4686. }
  4687. node.optional = optional;
  4688. return this.finishNode(node, "ObjectTypeProperty");
  4689. }
  4690. }
  4691. flowCheckGetterSetterParams(property) {
  4692. const paramCount = property.kind === "get" ? 0 : 1;
  4693. const length = property.value.params.length + (property.value.rest ? 1 : 0);
  4694. if (property.value.this) {
  4695. this.raise(property.kind === "get" ? FlowErrors.GetterMayNotHaveThisParam : FlowErrors.SetterMayNotHaveThisParam, {
  4696. at: property.value.this
  4697. });
  4698. }
  4699. if (length !== paramCount) {
  4700. this.raise(property.kind === "get" ? Errors.BadGetterArity : Errors.BadSetterArity, {
  4701. at: property
  4702. });
  4703. }
  4704. if (property.kind === "set" && property.value.rest) {
  4705. this.raise(Errors.BadSetterRestParameter, {
  4706. at: property
  4707. });
  4708. }
  4709. }
  4710. flowObjectTypeSemicolon() {
  4711. if (!this.eat(13) && !this.eat(12) && !this.match(8) && !this.match(9)) {
  4712. this.unexpected();
  4713. }
  4714. }
  4715. flowParseQualifiedTypeIdentifier(startLoc, id) {
  4716. var _startLoc;
  4717. (_startLoc = startLoc) != null ? _startLoc : startLoc = this.state.startLoc;
  4718. let node = id || this.flowParseRestrictedIdentifier(true);
  4719. while (this.eat(16)) {
  4720. const node2 = this.startNodeAt(startLoc);
  4721. node2.qualification = node;
  4722. node2.id = this.flowParseRestrictedIdentifier(true);
  4723. node = this.finishNode(node2, "QualifiedTypeIdentifier");
  4724. }
  4725. return node;
  4726. }
  4727. flowParseGenericType(startLoc, id) {
  4728. const node = this.startNodeAt(startLoc);
  4729. node.typeParameters = null;
  4730. node.id = this.flowParseQualifiedTypeIdentifier(startLoc, id);
  4731. if (this.match(47)) {
  4732. node.typeParameters = this.flowParseTypeParameterInstantiation();
  4733. }
  4734. return this.finishNode(node, "GenericTypeAnnotation");
  4735. }
  4736. flowParseTypeofType() {
  4737. const node = this.startNode();
  4738. this.expect(87);
  4739. node.argument = this.flowParsePrimaryType();
  4740. return this.finishNode(node, "TypeofTypeAnnotation");
  4741. }
  4742. flowParseTupleType() {
  4743. const node = this.startNode();
  4744. node.types = [];
  4745. this.expect(0);
  4746. while (this.state.pos < this.length && !this.match(3)) {
  4747. node.types.push(this.flowParseType());
  4748. if (this.match(3)) break;
  4749. this.expect(12);
  4750. }
  4751. this.expect(3);
  4752. return this.finishNode(node, "TupleTypeAnnotation");
  4753. }
  4754. flowParseFunctionTypeParam(first) {
  4755. let name = null;
  4756. let optional = false;
  4757. let typeAnnotation = null;
  4758. const node = this.startNode();
  4759. const lh = this.lookahead();
  4760. const isThis = this.state.type === 78;
  4761. if (lh.type === 14 || lh.type === 17) {
  4762. if (isThis && !first) {
  4763. this.raise(FlowErrors.ThisParamMustBeFirst, {
  4764. at: node
  4765. });
  4766. }
  4767. name = this.parseIdentifier(isThis);
  4768. if (this.eat(17)) {
  4769. optional = true;
  4770. if (isThis) {
  4771. this.raise(FlowErrors.ThisParamMayNotBeOptional, {
  4772. at: node
  4773. });
  4774. }
  4775. }
  4776. typeAnnotation = this.flowParseTypeInitialiser();
  4777. } else {
  4778. typeAnnotation = this.flowParseType();
  4779. }
  4780. node.name = name;
  4781. node.optional = optional;
  4782. node.typeAnnotation = typeAnnotation;
  4783. return this.finishNode(node, "FunctionTypeParam");
  4784. }
  4785. reinterpretTypeAsFunctionTypeParam(type) {
  4786. const node = this.startNodeAt(type.loc.start);
  4787. node.name = null;
  4788. node.optional = false;
  4789. node.typeAnnotation = type;
  4790. return this.finishNode(node, "FunctionTypeParam");
  4791. }
  4792. flowParseFunctionTypeParams(params = []) {
  4793. let rest = null;
  4794. let _this = null;
  4795. if (this.match(78)) {
  4796. _this = this.flowParseFunctionTypeParam(true);
  4797. _this.name = null;
  4798. if (!this.match(11)) {
  4799. this.expect(12);
  4800. }
  4801. }
  4802. while (!this.match(11) && !this.match(21)) {
  4803. params.push(this.flowParseFunctionTypeParam(false));
  4804. if (!this.match(11)) {
  4805. this.expect(12);
  4806. }
  4807. }
  4808. if (this.eat(21)) {
  4809. rest = this.flowParseFunctionTypeParam(false);
  4810. }
  4811. return {
  4812. params,
  4813. rest,
  4814. _this
  4815. };
  4816. }
  4817. flowIdentToTypeAnnotation(startLoc, node, id) {
  4818. switch (id.name) {
  4819. case "any":
  4820. return this.finishNode(node, "AnyTypeAnnotation");
  4821. case "bool":
  4822. case "boolean":
  4823. return this.finishNode(node, "BooleanTypeAnnotation");
  4824. case "mixed":
  4825. return this.finishNode(node, "MixedTypeAnnotation");
  4826. case "empty":
  4827. return this.finishNode(node, "EmptyTypeAnnotation");
  4828. case "number":
  4829. return this.finishNode(node, "NumberTypeAnnotation");
  4830. case "string":
  4831. return this.finishNode(node, "StringTypeAnnotation");
  4832. case "symbol":
  4833. return this.finishNode(node, "SymbolTypeAnnotation");
  4834. default:
  4835. this.checkNotUnderscore(id.name);
  4836. return this.flowParseGenericType(startLoc, id);
  4837. }
  4838. }
  4839. flowParsePrimaryType() {
  4840. const startLoc = this.state.startLoc;
  4841. const node = this.startNode();
  4842. let tmp;
  4843. let type;
  4844. let isGroupedType = false;
  4845. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  4846. switch (this.state.type) {
  4847. case 5:
  4848. return this.flowParseObjectType({
  4849. allowStatic: false,
  4850. allowExact: false,
  4851. allowSpread: true,
  4852. allowProto: false,
  4853. allowInexact: true
  4854. });
  4855. case 6:
  4856. return this.flowParseObjectType({
  4857. allowStatic: false,
  4858. allowExact: true,
  4859. allowSpread: true,
  4860. allowProto: false,
  4861. allowInexact: false
  4862. });
  4863. case 0:
  4864. this.state.noAnonFunctionType = false;
  4865. type = this.flowParseTupleType();
  4866. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  4867. return type;
  4868. case 47:
  4869. node.typeParameters = this.flowParseTypeParameterDeclaration();
  4870. this.expect(10);
  4871. tmp = this.flowParseFunctionTypeParams();
  4872. node.params = tmp.params;
  4873. node.rest = tmp.rest;
  4874. node.this = tmp._this;
  4875. this.expect(11);
  4876. this.expect(19);
  4877. node.returnType = this.flowParseType();
  4878. return this.finishNode(node, "FunctionTypeAnnotation");
  4879. case 10:
  4880. this.next();
  4881. if (!this.match(11) && !this.match(21)) {
  4882. if (tokenIsIdentifier(this.state.type) || this.match(78)) {
  4883. const token = this.lookahead().type;
  4884. isGroupedType = token !== 17 && token !== 14;
  4885. } else {
  4886. isGroupedType = true;
  4887. }
  4888. }
  4889. if (isGroupedType) {
  4890. this.state.noAnonFunctionType = false;
  4891. type = this.flowParseType();
  4892. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  4893. if (this.state.noAnonFunctionType || !(this.match(12) || this.match(11) && this.lookahead().type === 19)) {
  4894. this.expect(11);
  4895. return type;
  4896. } else {
  4897. this.eat(12);
  4898. }
  4899. }
  4900. if (type) {
  4901. tmp = this.flowParseFunctionTypeParams([this.reinterpretTypeAsFunctionTypeParam(type)]);
  4902. } else {
  4903. tmp = this.flowParseFunctionTypeParams();
  4904. }
  4905. node.params = tmp.params;
  4906. node.rest = tmp.rest;
  4907. node.this = tmp._this;
  4908. this.expect(11);
  4909. this.expect(19);
  4910. node.returnType = this.flowParseType();
  4911. node.typeParameters = null;
  4912. return this.finishNode(node, "FunctionTypeAnnotation");
  4913. case 131:
  4914. return this.parseLiteral(this.state.value, "StringLiteralTypeAnnotation");
  4915. case 85:
  4916. case 86:
  4917. node.value = this.match(85);
  4918. this.next();
  4919. return this.finishNode(node, "BooleanLiteralTypeAnnotation");
  4920. case 53:
  4921. if (this.state.value === "-") {
  4922. this.next();
  4923. if (this.match(132)) {
  4924. return this.parseLiteralAtNode(-this.state.value, "NumberLiteralTypeAnnotation", node);
  4925. }
  4926. if (this.match(133)) {
  4927. return this.parseLiteralAtNode(-this.state.value, "BigIntLiteralTypeAnnotation", node);
  4928. }
  4929. throw this.raise(FlowErrors.UnexpectedSubtractionOperand, {
  4930. at: this.state.startLoc
  4931. });
  4932. }
  4933. throw this.unexpected();
  4934. case 132:
  4935. return this.parseLiteral(this.state.value, "NumberLiteralTypeAnnotation");
  4936. case 133:
  4937. return this.parseLiteral(this.state.value, "BigIntLiteralTypeAnnotation");
  4938. case 88:
  4939. this.next();
  4940. return this.finishNode(node, "VoidTypeAnnotation");
  4941. case 84:
  4942. this.next();
  4943. return this.finishNode(node, "NullLiteralTypeAnnotation");
  4944. case 78:
  4945. this.next();
  4946. return this.finishNode(node, "ThisTypeAnnotation");
  4947. case 55:
  4948. this.next();
  4949. return this.finishNode(node, "ExistsTypeAnnotation");
  4950. case 87:
  4951. return this.flowParseTypeofType();
  4952. default:
  4953. if (tokenIsKeyword(this.state.type)) {
  4954. const label = tokenLabelName(this.state.type);
  4955. this.next();
  4956. return super.createIdentifier(node, label);
  4957. } else if (tokenIsIdentifier(this.state.type)) {
  4958. if (this.isContextual(127)) {
  4959. return this.flowParseInterfaceType();
  4960. }
  4961. return this.flowIdentToTypeAnnotation(startLoc, node, this.parseIdentifier());
  4962. }
  4963. }
  4964. throw this.unexpected();
  4965. }
  4966. flowParsePostfixType() {
  4967. const startLoc = this.state.startLoc;
  4968. let type = this.flowParsePrimaryType();
  4969. let seenOptionalIndexedAccess = false;
  4970. while ((this.match(0) || this.match(18)) && !this.canInsertSemicolon()) {
  4971. const node = this.startNodeAt(startLoc);
  4972. const optional = this.eat(18);
  4973. seenOptionalIndexedAccess = seenOptionalIndexedAccess || optional;
  4974. this.expect(0);
  4975. if (!optional && this.match(3)) {
  4976. node.elementType = type;
  4977. this.next();
  4978. type = this.finishNode(node, "ArrayTypeAnnotation");
  4979. } else {
  4980. node.objectType = type;
  4981. node.indexType = this.flowParseType();
  4982. this.expect(3);
  4983. if (seenOptionalIndexedAccess) {
  4984. node.optional = optional;
  4985. type = this.finishNode(node, "OptionalIndexedAccessType");
  4986. } else {
  4987. type = this.finishNode(node, "IndexedAccessType");
  4988. }
  4989. }
  4990. }
  4991. return type;
  4992. }
  4993. flowParsePrefixType() {
  4994. const node = this.startNode();
  4995. if (this.eat(17)) {
  4996. node.typeAnnotation = this.flowParsePrefixType();
  4997. return this.finishNode(node, "NullableTypeAnnotation");
  4998. } else {
  4999. return this.flowParsePostfixType();
  5000. }
  5001. }
  5002. flowParseAnonFunctionWithoutParens() {
  5003. const param = this.flowParsePrefixType();
  5004. if (!this.state.noAnonFunctionType && this.eat(19)) {
  5005. const node = this.startNodeAt(param.loc.start);
  5006. node.params = [this.reinterpretTypeAsFunctionTypeParam(param)];
  5007. node.rest = null;
  5008. node.this = null;
  5009. node.returnType = this.flowParseType();
  5010. node.typeParameters = null;
  5011. return this.finishNode(node, "FunctionTypeAnnotation");
  5012. }
  5013. return param;
  5014. }
  5015. flowParseIntersectionType() {
  5016. const node = this.startNode();
  5017. this.eat(45);
  5018. const type = this.flowParseAnonFunctionWithoutParens();
  5019. node.types = [type];
  5020. while (this.eat(45)) {
  5021. node.types.push(this.flowParseAnonFunctionWithoutParens());
  5022. }
  5023. return node.types.length === 1 ? type : this.finishNode(node, "IntersectionTypeAnnotation");
  5024. }
  5025. flowParseUnionType() {
  5026. const node = this.startNode();
  5027. this.eat(43);
  5028. const type = this.flowParseIntersectionType();
  5029. node.types = [type];
  5030. while (this.eat(43)) {
  5031. node.types.push(this.flowParseIntersectionType());
  5032. }
  5033. return node.types.length === 1 ? type : this.finishNode(node, "UnionTypeAnnotation");
  5034. }
  5035. flowParseType() {
  5036. const oldInType = this.state.inType;
  5037. this.state.inType = true;
  5038. const type = this.flowParseUnionType();
  5039. this.state.inType = oldInType;
  5040. return type;
  5041. }
  5042. flowParseTypeOrImplicitInstantiation() {
  5043. if (this.state.type === 130 && this.state.value === "_") {
  5044. const startLoc = this.state.startLoc;
  5045. const node = this.parseIdentifier();
  5046. return this.flowParseGenericType(startLoc, node);
  5047. } else {
  5048. return this.flowParseType();
  5049. }
  5050. }
  5051. flowParseTypeAnnotation() {
  5052. const node = this.startNode();
  5053. node.typeAnnotation = this.flowParseTypeInitialiser();
  5054. return this.finishNode(node, "TypeAnnotation");
  5055. }
  5056. flowParseTypeAnnotatableIdentifier(allowPrimitiveOverride) {
  5057. const ident = allowPrimitiveOverride ? this.parseIdentifier() : this.flowParseRestrictedIdentifier();
  5058. if (this.match(14)) {
  5059. ident.typeAnnotation = this.flowParseTypeAnnotation();
  5060. this.resetEndLocation(ident);
  5061. }
  5062. return ident;
  5063. }
  5064. typeCastToParameter(node) {
  5065. node.expression.typeAnnotation = node.typeAnnotation;
  5066. this.resetEndLocation(node.expression, node.typeAnnotation.loc.end);
  5067. return node.expression;
  5068. }
  5069. flowParseVariance() {
  5070. let variance = null;
  5071. if (this.match(53)) {
  5072. variance = this.startNode();
  5073. if (this.state.value === "+") {
  5074. variance.kind = "plus";
  5075. } else {
  5076. variance.kind = "minus";
  5077. }
  5078. this.next();
  5079. return this.finishNode(variance, "Variance");
  5080. }
  5081. return variance;
  5082. }
  5083. parseFunctionBody(node, allowExpressionBody, isMethod = false) {
  5084. if (allowExpressionBody) {
  5085. return this.forwardNoArrowParamsConversionAt(node, () => super.parseFunctionBody(node, true, isMethod));
  5086. }
  5087. return super.parseFunctionBody(node, false, isMethod);
  5088. }
  5089. parseFunctionBodyAndFinish(node, type, isMethod = false) {
  5090. if (this.match(14)) {
  5091. const typeNode = this.startNode();
  5092. [typeNode.typeAnnotation, node.predicate] = this.flowParseTypeAndPredicateInitialiser();
  5093. node.returnType = typeNode.typeAnnotation ? this.finishNode(typeNode, "TypeAnnotation") : null;
  5094. }
  5095. return super.parseFunctionBodyAndFinish(node, type, isMethod);
  5096. }
  5097. parseStatementLike(flags) {
  5098. if (this.state.strict && this.isContextual(127)) {
  5099. const lookahead = this.lookahead();
  5100. if (tokenIsKeywordOrIdentifier(lookahead.type)) {
  5101. const node = this.startNode();
  5102. this.next();
  5103. return this.flowParseInterface(node);
  5104. }
  5105. } else if (this.shouldParseEnums() && this.isContextual(124)) {
  5106. const node = this.startNode();
  5107. this.next();
  5108. return this.flowParseEnumDeclaration(node);
  5109. }
  5110. const stmt = super.parseStatementLike(flags);
  5111. if (this.flowPragma === undefined && !this.isValidDirective(stmt)) {
  5112. this.flowPragma = null;
  5113. }
  5114. return stmt;
  5115. }
  5116. parseExpressionStatement(node, expr, decorators) {
  5117. if (expr.type === "Identifier") {
  5118. if (expr.name === "declare") {
  5119. if (this.match(80) || tokenIsIdentifier(this.state.type) || this.match(68) || this.match(74) || this.match(82)) {
  5120. return this.flowParseDeclare(node);
  5121. }
  5122. } else if (tokenIsIdentifier(this.state.type)) {
  5123. if (expr.name === "interface") {
  5124. return this.flowParseInterface(node);
  5125. } else if (expr.name === "type") {
  5126. return this.flowParseTypeAlias(node);
  5127. } else if (expr.name === "opaque") {
  5128. return this.flowParseOpaqueType(node, false);
  5129. }
  5130. }
  5131. }
  5132. return super.parseExpressionStatement(node, expr, decorators);
  5133. }
  5134. shouldParseExportDeclaration() {
  5135. const {
  5136. type
  5137. } = this.state;
  5138. if (tokenIsFlowInterfaceOrTypeOrOpaque(type) || this.shouldParseEnums() && type === 124) {
  5139. return !this.state.containsEsc;
  5140. }
  5141. return super.shouldParseExportDeclaration();
  5142. }
  5143. isExportDefaultSpecifier() {
  5144. const {
  5145. type
  5146. } = this.state;
  5147. if (tokenIsFlowInterfaceOrTypeOrOpaque(type) || this.shouldParseEnums() && type === 124) {
  5148. return this.state.containsEsc;
  5149. }
  5150. return super.isExportDefaultSpecifier();
  5151. }
  5152. parseExportDefaultExpression() {
  5153. if (this.shouldParseEnums() && this.isContextual(124)) {
  5154. const node = this.startNode();
  5155. this.next();
  5156. return this.flowParseEnumDeclaration(node);
  5157. }
  5158. return super.parseExportDefaultExpression();
  5159. }
  5160. parseConditional(expr, startLoc, refExpressionErrors) {
  5161. if (!this.match(17)) return expr;
  5162. if (this.state.maybeInArrowParameters) {
  5163. const nextCh = this.lookaheadCharCode();
  5164. if (nextCh === 44 || nextCh === 61 || nextCh === 58 || nextCh === 41) {
  5165. this.setOptionalParametersError(refExpressionErrors);
  5166. return expr;
  5167. }
  5168. }
  5169. this.expect(17);
  5170. const state = this.state.clone();
  5171. const originalNoArrowAt = this.state.noArrowAt;
  5172. const node = this.startNodeAt(startLoc);
  5173. let {
  5174. consequent,
  5175. failed
  5176. } = this.tryParseConditionalConsequent();
  5177. let [valid, invalid] = this.getArrowLikeExpressions(consequent);
  5178. if (failed || invalid.length > 0) {
  5179. const noArrowAt = [...originalNoArrowAt];
  5180. if (invalid.length > 0) {
  5181. this.state = state;
  5182. this.state.noArrowAt = noArrowAt;
  5183. for (let i = 0; i < invalid.length; i++) {
  5184. noArrowAt.push(invalid[i].start);
  5185. }
  5186. ({
  5187. consequent,
  5188. failed
  5189. } = this.tryParseConditionalConsequent());
  5190. [valid, invalid] = this.getArrowLikeExpressions(consequent);
  5191. }
  5192. if (failed && valid.length > 1) {
  5193. this.raise(FlowErrors.AmbiguousConditionalArrow, {
  5194. at: state.startLoc
  5195. });
  5196. }
  5197. if (failed && valid.length === 1) {
  5198. this.state = state;
  5199. noArrowAt.push(valid[0].start);
  5200. this.state.noArrowAt = noArrowAt;
  5201. ({
  5202. consequent,
  5203. failed
  5204. } = this.tryParseConditionalConsequent());
  5205. }
  5206. }
  5207. this.getArrowLikeExpressions(consequent, true);
  5208. this.state.noArrowAt = originalNoArrowAt;
  5209. this.expect(14);
  5210. node.test = expr;
  5211. node.consequent = consequent;
  5212. node.alternate = this.forwardNoArrowParamsConversionAt(node, () => this.parseMaybeAssign(undefined, undefined));
  5213. return this.finishNode(node, "ConditionalExpression");
  5214. }
  5215. tryParseConditionalConsequent() {
  5216. this.state.noArrowParamsConversionAt.push(this.state.start);
  5217. const consequent = this.parseMaybeAssignAllowIn();
  5218. const failed = !this.match(14);
  5219. this.state.noArrowParamsConversionAt.pop();
  5220. return {
  5221. consequent,
  5222. failed
  5223. };
  5224. }
  5225. getArrowLikeExpressions(node, disallowInvalid) {
  5226. const stack = [node];
  5227. const arrows = [];
  5228. while (stack.length !== 0) {
  5229. const node = stack.pop();
  5230. if (node.type === "ArrowFunctionExpression") {
  5231. if (node.typeParameters || !node.returnType) {
  5232. this.finishArrowValidation(node);
  5233. } else {
  5234. arrows.push(node);
  5235. }
  5236. stack.push(node.body);
  5237. } else if (node.type === "ConditionalExpression") {
  5238. stack.push(node.consequent);
  5239. stack.push(node.alternate);
  5240. }
  5241. }
  5242. if (disallowInvalid) {
  5243. arrows.forEach(node => this.finishArrowValidation(node));
  5244. return [arrows, []];
  5245. }
  5246. return partition(arrows, node => node.params.every(param => this.isAssignable(param, true)));
  5247. }
  5248. finishArrowValidation(node) {
  5249. var _node$extra;
  5250. this.toAssignableList(node.params, (_node$extra = node.extra) == null ? void 0 : _node$extra.trailingCommaLoc, false);
  5251. this.scope.enter(SCOPE_FUNCTION | SCOPE_ARROW);
  5252. super.checkParams(node, false, true);
  5253. this.scope.exit();
  5254. }
  5255. forwardNoArrowParamsConversionAt(node, parse) {
  5256. let result;
  5257. if (this.state.noArrowParamsConversionAt.indexOf(node.start) !== -1) {
  5258. this.state.noArrowParamsConversionAt.push(this.state.start);
  5259. result = parse();
  5260. this.state.noArrowParamsConversionAt.pop();
  5261. } else {
  5262. result = parse();
  5263. }
  5264. return result;
  5265. }
  5266. parseParenItem(node, startLoc) {
  5267. node = super.parseParenItem(node, startLoc);
  5268. if (this.eat(17)) {
  5269. node.optional = true;
  5270. this.resetEndLocation(node);
  5271. }
  5272. if (this.match(14)) {
  5273. const typeCastNode = this.startNodeAt(startLoc);
  5274. typeCastNode.expression = node;
  5275. typeCastNode.typeAnnotation = this.flowParseTypeAnnotation();
  5276. return this.finishNode(typeCastNode, "TypeCastExpression");
  5277. }
  5278. return node;
  5279. }
  5280. assertModuleNodeAllowed(node) {
  5281. if (node.type === "ImportDeclaration" && (node.importKind === "type" || node.importKind === "typeof") || node.type === "ExportNamedDeclaration" && node.exportKind === "type" || node.type === "ExportAllDeclaration" && node.exportKind === "type") {
  5282. return;
  5283. }
  5284. super.assertModuleNodeAllowed(node);
  5285. }
  5286. parseExport(node, decorators) {
  5287. const decl = super.parseExport(node, decorators);
  5288. if (decl.type === "ExportNamedDeclaration" || decl.type === "ExportAllDeclaration") {
  5289. decl.exportKind = decl.exportKind || "value";
  5290. }
  5291. return decl;
  5292. }
  5293. parseExportDeclaration(node) {
  5294. if (this.isContextual(128)) {
  5295. node.exportKind = "type";
  5296. const declarationNode = this.startNode();
  5297. this.next();
  5298. if (this.match(5)) {
  5299. node.specifiers = this.parseExportSpecifiers(true);
  5300. super.parseExportFrom(node);
  5301. return null;
  5302. } else {
  5303. return this.flowParseTypeAlias(declarationNode);
  5304. }
  5305. } else if (this.isContextual(129)) {
  5306. node.exportKind = "type";
  5307. const declarationNode = this.startNode();
  5308. this.next();
  5309. return this.flowParseOpaqueType(declarationNode, false);
  5310. } else if (this.isContextual(127)) {
  5311. node.exportKind = "type";
  5312. const declarationNode = this.startNode();
  5313. this.next();
  5314. return this.flowParseInterface(declarationNode);
  5315. } else if (this.shouldParseEnums() && this.isContextual(124)) {
  5316. node.exportKind = "value";
  5317. const declarationNode = this.startNode();
  5318. this.next();
  5319. return this.flowParseEnumDeclaration(declarationNode);
  5320. } else {
  5321. return super.parseExportDeclaration(node);
  5322. }
  5323. }
  5324. eatExportStar(node) {
  5325. if (super.eatExportStar(node)) return true;
  5326. if (this.isContextual(128) && this.lookahead().type === 55) {
  5327. node.exportKind = "type";
  5328. this.next();
  5329. this.next();
  5330. return true;
  5331. }
  5332. return false;
  5333. }
  5334. maybeParseExportNamespaceSpecifier(node) {
  5335. const {
  5336. startLoc
  5337. } = this.state;
  5338. const hasNamespace = super.maybeParseExportNamespaceSpecifier(node);
  5339. if (hasNamespace && node.exportKind === "type") {
  5340. this.unexpected(startLoc);
  5341. }
  5342. return hasNamespace;
  5343. }
  5344. parseClassId(node, isStatement, optionalId) {
  5345. super.parseClassId(node, isStatement, optionalId);
  5346. if (this.match(47)) {
  5347. node.typeParameters = this.flowParseTypeParameterDeclaration();
  5348. }
  5349. }
  5350. parseClassMember(classBody, member, state) {
  5351. const {
  5352. startLoc
  5353. } = this.state;
  5354. if (this.isContextual(123)) {
  5355. if (super.parseClassMemberFromModifier(classBody, member)) {
  5356. return;
  5357. }
  5358. member.declare = true;
  5359. }
  5360. super.parseClassMember(classBody, member, state);
  5361. if (member.declare) {
  5362. if (member.type !== "ClassProperty" && member.type !== "ClassPrivateProperty" && member.type !== "PropertyDefinition") {
  5363. this.raise(FlowErrors.DeclareClassElement, {
  5364. at: startLoc
  5365. });
  5366. } else if (member.value) {
  5367. this.raise(FlowErrors.DeclareClassFieldInitializer, {
  5368. at: member.value
  5369. });
  5370. }
  5371. }
  5372. }
  5373. isIterator(word) {
  5374. return word === "iterator" || word === "asyncIterator";
  5375. }
  5376. readIterator() {
  5377. const word = super.readWord1();
  5378. const fullWord = "@@" + word;
  5379. if (!this.isIterator(word) || !this.state.inType) {
  5380. this.raise(Errors.InvalidIdentifier, {
  5381. at: this.state.curPosition(),
  5382. identifierName: fullWord
  5383. });
  5384. }
  5385. this.finishToken(130, fullWord);
  5386. }
  5387. getTokenFromCode(code) {
  5388. const next = this.input.charCodeAt(this.state.pos + 1);
  5389. if (code === 123 && next === 124) {
  5390. return this.finishOp(6, 2);
  5391. } else if (this.state.inType && (code === 62 || code === 60)) {
  5392. return this.finishOp(code === 62 ? 48 : 47, 1);
  5393. } else if (this.state.inType && code === 63) {
  5394. if (next === 46) {
  5395. return this.finishOp(18, 2);
  5396. }
  5397. return this.finishOp(17, 1);
  5398. } else if (isIteratorStart(code, next, this.input.charCodeAt(this.state.pos + 2))) {
  5399. this.state.pos += 2;
  5400. return this.readIterator();
  5401. } else {
  5402. return super.getTokenFromCode(code);
  5403. }
  5404. }
  5405. isAssignable(node, isBinding) {
  5406. if (node.type === "TypeCastExpression") {
  5407. return this.isAssignable(node.expression, isBinding);
  5408. } else {
  5409. return super.isAssignable(node, isBinding);
  5410. }
  5411. }
  5412. toAssignable(node, isLHS = false) {
  5413. if (!isLHS && node.type === "AssignmentExpression" && node.left.type === "TypeCastExpression") {
  5414. node.left = this.typeCastToParameter(node.left);
  5415. }
  5416. super.toAssignable(node, isLHS);
  5417. }
  5418. toAssignableList(exprList, trailingCommaLoc, isLHS) {
  5419. for (let i = 0; i < exprList.length; i++) {
  5420. const expr = exprList[i];
  5421. if ((expr == null ? void 0 : expr.type) === "TypeCastExpression") {
  5422. exprList[i] = this.typeCastToParameter(expr);
  5423. }
  5424. }
  5425. super.toAssignableList(exprList, trailingCommaLoc, isLHS);
  5426. }
  5427. toReferencedList(exprList, isParenthesizedExpr) {
  5428. for (let i = 0; i < exprList.length; i++) {
  5429. var _expr$extra;
  5430. const expr = exprList[i];
  5431. if (expr && expr.type === "TypeCastExpression" && !((_expr$extra = expr.extra) != null && _expr$extra.parenthesized) && (exprList.length > 1 || !isParenthesizedExpr)) {
  5432. this.raise(FlowErrors.TypeCastInPattern, {
  5433. at: expr.typeAnnotation
  5434. });
  5435. }
  5436. }
  5437. return exprList;
  5438. }
  5439. parseArrayLike(close, canBePattern, isTuple, refExpressionErrors) {
  5440. const node = super.parseArrayLike(close, canBePattern, isTuple, refExpressionErrors);
  5441. if (canBePattern && !this.state.maybeInArrowParameters) {
  5442. this.toReferencedList(node.elements);
  5443. }
  5444. return node;
  5445. }
  5446. isValidLVal(type, isParenthesized, binding) {
  5447. return type === "TypeCastExpression" || super.isValidLVal(type, isParenthesized, binding);
  5448. }
  5449. parseClassProperty(node) {
  5450. if (this.match(14)) {
  5451. node.typeAnnotation = this.flowParseTypeAnnotation();
  5452. }
  5453. return super.parseClassProperty(node);
  5454. }
  5455. parseClassPrivateProperty(node) {
  5456. if (this.match(14)) {
  5457. node.typeAnnotation = this.flowParseTypeAnnotation();
  5458. }
  5459. return super.parseClassPrivateProperty(node);
  5460. }
  5461. isClassMethod() {
  5462. return this.match(47) || super.isClassMethod();
  5463. }
  5464. isClassProperty() {
  5465. return this.match(14) || super.isClassProperty();
  5466. }
  5467. isNonstaticConstructor(method) {
  5468. return !this.match(14) && super.isNonstaticConstructor(method);
  5469. }
  5470. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  5471. if (method.variance) {
  5472. this.unexpected(method.variance.loc.start);
  5473. }
  5474. delete method.variance;
  5475. if (this.match(47)) {
  5476. method.typeParameters = this.flowParseTypeParameterDeclaration();
  5477. }
  5478. super.pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper);
  5479. if (method.params && isConstructor) {
  5480. const params = method.params;
  5481. if (params.length > 0 && this.isThisParam(params[0])) {
  5482. this.raise(FlowErrors.ThisParamBannedInConstructor, {
  5483. at: method
  5484. });
  5485. }
  5486. } else if (method.type === "MethodDefinition" && isConstructor && method.value.params) {
  5487. const params = method.value.params;
  5488. if (params.length > 0 && this.isThisParam(params[0])) {
  5489. this.raise(FlowErrors.ThisParamBannedInConstructor, {
  5490. at: method
  5491. });
  5492. }
  5493. }
  5494. }
  5495. pushClassPrivateMethod(classBody, method, isGenerator, isAsync) {
  5496. if (method.variance) {
  5497. this.unexpected(method.variance.loc.start);
  5498. }
  5499. delete method.variance;
  5500. if (this.match(47)) {
  5501. method.typeParameters = this.flowParseTypeParameterDeclaration();
  5502. }
  5503. super.pushClassPrivateMethod(classBody, method, isGenerator, isAsync);
  5504. }
  5505. parseClassSuper(node) {
  5506. super.parseClassSuper(node);
  5507. if (node.superClass && this.match(47)) {
  5508. node.superTypeParameters = this.flowParseTypeParameterInstantiation();
  5509. }
  5510. if (this.isContextual(111)) {
  5511. this.next();
  5512. const implemented = node.implements = [];
  5513. do {
  5514. const node = this.startNode();
  5515. node.id = this.flowParseRestrictedIdentifier(true);
  5516. if (this.match(47)) {
  5517. node.typeParameters = this.flowParseTypeParameterInstantiation();
  5518. } else {
  5519. node.typeParameters = null;
  5520. }
  5521. implemented.push(this.finishNode(node, "ClassImplements"));
  5522. } while (this.eat(12));
  5523. }
  5524. }
  5525. checkGetterSetterParams(method) {
  5526. super.checkGetterSetterParams(method);
  5527. const params = this.getObjectOrClassMethodParams(method);
  5528. if (params.length > 0) {
  5529. const param = params[0];
  5530. if (this.isThisParam(param) && method.kind === "get") {
  5531. this.raise(FlowErrors.GetterMayNotHaveThisParam, {
  5532. at: param
  5533. });
  5534. } else if (this.isThisParam(param)) {
  5535. this.raise(FlowErrors.SetterMayNotHaveThisParam, {
  5536. at: param
  5537. });
  5538. }
  5539. }
  5540. }
  5541. parsePropertyNamePrefixOperator(node) {
  5542. node.variance = this.flowParseVariance();
  5543. }
  5544. parseObjPropValue(prop, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors) {
  5545. if (prop.variance) {
  5546. this.unexpected(prop.variance.loc.start);
  5547. }
  5548. delete prop.variance;
  5549. let typeParameters;
  5550. if (this.match(47) && !isAccessor) {
  5551. typeParameters = this.flowParseTypeParameterDeclaration();
  5552. if (!this.match(10)) this.unexpected();
  5553. }
  5554. const result = super.parseObjPropValue(prop, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors);
  5555. if (typeParameters) {
  5556. (result.value || result).typeParameters = typeParameters;
  5557. }
  5558. return result;
  5559. }
  5560. parseAssignableListItemTypes(param) {
  5561. if (this.eat(17)) {
  5562. if (param.type !== "Identifier") {
  5563. this.raise(FlowErrors.PatternIsOptional, {
  5564. at: param
  5565. });
  5566. }
  5567. if (this.isThisParam(param)) {
  5568. this.raise(FlowErrors.ThisParamMayNotBeOptional, {
  5569. at: param
  5570. });
  5571. }
  5572. param.optional = true;
  5573. }
  5574. if (this.match(14)) {
  5575. param.typeAnnotation = this.flowParseTypeAnnotation();
  5576. } else if (this.isThisParam(param)) {
  5577. this.raise(FlowErrors.ThisParamAnnotationRequired, {
  5578. at: param
  5579. });
  5580. }
  5581. if (this.match(29) && this.isThisParam(param)) {
  5582. this.raise(FlowErrors.ThisParamNoDefault, {
  5583. at: param
  5584. });
  5585. }
  5586. this.resetEndLocation(param);
  5587. return param;
  5588. }
  5589. parseMaybeDefault(startLoc, left) {
  5590. const node = super.parseMaybeDefault(startLoc, left);
  5591. if (node.type === "AssignmentPattern" && node.typeAnnotation && node.right.start < node.typeAnnotation.start) {
  5592. this.raise(FlowErrors.TypeBeforeInitializer, {
  5593. at: node.typeAnnotation
  5594. });
  5595. }
  5596. return node;
  5597. }
  5598. shouldParseDefaultImport(node) {
  5599. if (!hasTypeImportKind(node)) {
  5600. return super.shouldParseDefaultImport(node);
  5601. }
  5602. return isMaybeDefaultImport(this.state.type);
  5603. }
  5604. checkImportReflection(node) {
  5605. super.checkImportReflection(node);
  5606. if (node.module && node.importKind !== "value") {
  5607. this.raise(FlowErrors.ImportReflectionHasImportType, {
  5608. at: node.specifiers[0].loc.start
  5609. });
  5610. }
  5611. }
  5612. parseImportSpecifierLocal(node, specifier, type) {
  5613. specifier.local = hasTypeImportKind(node) ? this.flowParseRestrictedIdentifier(true, true) : this.parseIdentifier();
  5614. node.specifiers.push(this.finishImportSpecifier(specifier, type));
  5615. }
  5616. maybeParseDefaultImportSpecifier(node) {
  5617. node.importKind = "value";
  5618. let kind = null;
  5619. if (this.match(87)) {
  5620. kind = "typeof";
  5621. } else if (this.isContextual(128)) {
  5622. kind = "type";
  5623. }
  5624. if (kind) {
  5625. const lh = this.lookahead();
  5626. const {
  5627. type
  5628. } = lh;
  5629. if (kind === "type" && type === 55) {
  5630. this.unexpected(null, lh.type);
  5631. }
  5632. if (isMaybeDefaultImport(type) || type === 5 || type === 55) {
  5633. this.next();
  5634. node.importKind = kind;
  5635. }
  5636. }
  5637. return super.maybeParseDefaultImportSpecifier(node);
  5638. }
  5639. parseImportSpecifier(specifier, importedIsString, isInTypeOnlyImport, isMaybeTypeOnly, bindingType) {
  5640. const firstIdent = specifier.imported;
  5641. let specifierTypeKind = null;
  5642. if (firstIdent.type === "Identifier") {
  5643. if (firstIdent.name === "type") {
  5644. specifierTypeKind = "type";
  5645. } else if (firstIdent.name === "typeof") {
  5646. specifierTypeKind = "typeof";
  5647. }
  5648. }
  5649. let isBinding = false;
  5650. if (this.isContextual(93) && !this.isLookaheadContextual("as")) {
  5651. const as_ident = this.parseIdentifier(true);
  5652. if (specifierTypeKind !== null && !tokenIsKeywordOrIdentifier(this.state.type)) {
  5653. specifier.imported = as_ident;
  5654. specifier.importKind = specifierTypeKind;
  5655. specifier.local = cloneIdentifier(as_ident);
  5656. } else {
  5657. specifier.imported = firstIdent;
  5658. specifier.importKind = null;
  5659. specifier.local = this.parseIdentifier();
  5660. }
  5661. } else {
  5662. if (specifierTypeKind !== null && tokenIsKeywordOrIdentifier(this.state.type)) {
  5663. specifier.imported = this.parseIdentifier(true);
  5664. specifier.importKind = specifierTypeKind;
  5665. } else {
  5666. if (importedIsString) {
  5667. throw this.raise(Errors.ImportBindingIsString, {
  5668. at: specifier,
  5669. importName: firstIdent.value
  5670. });
  5671. }
  5672. specifier.imported = firstIdent;
  5673. specifier.importKind = null;
  5674. }
  5675. if (this.eatContextual(93)) {
  5676. specifier.local = this.parseIdentifier();
  5677. } else {
  5678. isBinding = true;
  5679. specifier.local = cloneIdentifier(specifier.imported);
  5680. }
  5681. }
  5682. const specifierIsTypeImport = hasTypeImportKind(specifier);
  5683. if (isInTypeOnlyImport && specifierIsTypeImport) {
  5684. this.raise(FlowErrors.ImportTypeShorthandOnlyInPureImport, {
  5685. at: specifier
  5686. });
  5687. }
  5688. if (isInTypeOnlyImport || specifierIsTypeImport) {
  5689. this.checkReservedType(specifier.local.name, specifier.local.loc.start, true);
  5690. }
  5691. if (isBinding && !isInTypeOnlyImport && !specifierIsTypeImport) {
  5692. this.checkReservedWord(specifier.local.name, specifier.loc.start, true, true);
  5693. }
  5694. return this.finishImportSpecifier(specifier, "ImportSpecifier");
  5695. }
  5696. parseBindingAtom() {
  5697. switch (this.state.type) {
  5698. case 78:
  5699. return this.parseIdentifier(true);
  5700. default:
  5701. return super.parseBindingAtom();
  5702. }
  5703. }
  5704. parseFunctionParams(node, isConstructor) {
  5705. const kind = node.kind;
  5706. if (kind !== "get" && kind !== "set" && this.match(47)) {
  5707. node.typeParameters = this.flowParseTypeParameterDeclaration();
  5708. }
  5709. super.parseFunctionParams(node, isConstructor);
  5710. }
  5711. parseVarId(decl, kind) {
  5712. super.parseVarId(decl, kind);
  5713. if (this.match(14)) {
  5714. decl.id.typeAnnotation = this.flowParseTypeAnnotation();
  5715. this.resetEndLocation(decl.id);
  5716. }
  5717. }
  5718. parseAsyncArrowFromCallExpression(node, call) {
  5719. if (this.match(14)) {
  5720. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  5721. this.state.noAnonFunctionType = true;
  5722. node.returnType = this.flowParseTypeAnnotation();
  5723. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  5724. }
  5725. return super.parseAsyncArrowFromCallExpression(node, call);
  5726. }
  5727. shouldParseAsyncArrow() {
  5728. return this.match(14) || super.shouldParseAsyncArrow();
  5729. }
  5730. parseMaybeAssign(refExpressionErrors, afterLeftParse) {
  5731. var _jsx;
  5732. let state = null;
  5733. let jsx;
  5734. if (this.hasPlugin("jsx") && (this.match(140) || this.match(47))) {
  5735. state = this.state.clone();
  5736. jsx = this.tryParse(() => super.parseMaybeAssign(refExpressionErrors, afterLeftParse), state);
  5737. if (!jsx.error) return jsx.node;
  5738. const {
  5739. context
  5740. } = this.state;
  5741. const currentContext = context[context.length - 1];
  5742. if (currentContext === types.j_oTag || currentContext === types.j_expr) {
  5743. context.pop();
  5744. }
  5745. }
  5746. if ((_jsx = jsx) != null && _jsx.error || this.match(47)) {
  5747. var _jsx2, _jsx3;
  5748. state = state || this.state.clone();
  5749. let typeParameters;
  5750. const arrow = this.tryParse(abort => {
  5751. var _arrowExpression$extr;
  5752. typeParameters = this.flowParseTypeParameterDeclaration();
  5753. const arrowExpression = this.forwardNoArrowParamsConversionAt(typeParameters, () => {
  5754. const result = super.parseMaybeAssign(refExpressionErrors, afterLeftParse);
  5755. this.resetStartLocationFromNode(result, typeParameters);
  5756. return result;
  5757. });
  5758. if ((_arrowExpression$extr = arrowExpression.extra) != null && _arrowExpression$extr.parenthesized) abort();
  5759. const expr = this.maybeUnwrapTypeCastExpression(arrowExpression);
  5760. if (expr.type !== "ArrowFunctionExpression") abort();
  5761. expr.typeParameters = typeParameters;
  5762. this.resetStartLocationFromNode(expr, typeParameters);
  5763. return arrowExpression;
  5764. }, state);
  5765. let arrowExpression = null;
  5766. if (arrow.node && this.maybeUnwrapTypeCastExpression(arrow.node).type === "ArrowFunctionExpression") {
  5767. if (!arrow.error && !arrow.aborted) {
  5768. if (arrow.node.async) {
  5769. this.raise(FlowErrors.UnexpectedTypeParameterBeforeAsyncArrowFunction, {
  5770. at: typeParameters
  5771. });
  5772. }
  5773. return arrow.node;
  5774. }
  5775. arrowExpression = arrow.node;
  5776. }
  5777. if ((_jsx2 = jsx) != null && _jsx2.node) {
  5778. this.state = jsx.failState;
  5779. return jsx.node;
  5780. }
  5781. if (arrowExpression) {
  5782. this.state = arrow.failState;
  5783. return arrowExpression;
  5784. }
  5785. if ((_jsx3 = jsx) != null && _jsx3.thrown) throw jsx.error;
  5786. if (arrow.thrown) throw arrow.error;
  5787. throw this.raise(FlowErrors.UnexpectedTokenAfterTypeParameter, {
  5788. at: typeParameters
  5789. });
  5790. }
  5791. return super.parseMaybeAssign(refExpressionErrors, afterLeftParse);
  5792. }
  5793. parseArrow(node) {
  5794. if (this.match(14)) {
  5795. const result = this.tryParse(() => {
  5796. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  5797. this.state.noAnonFunctionType = true;
  5798. const typeNode = this.startNode();
  5799. [typeNode.typeAnnotation, node.predicate] = this.flowParseTypeAndPredicateInitialiser();
  5800. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  5801. if (this.canInsertSemicolon()) this.unexpected();
  5802. if (!this.match(19)) this.unexpected();
  5803. return typeNode;
  5804. });
  5805. if (result.thrown) return null;
  5806. if (result.error) this.state = result.failState;
  5807. node.returnType = result.node.typeAnnotation ? this.finishNode(result.node, "TypeAnnotation") : null;
  5808. }
  5809. return super.parseArrow(node);
  5810. }
  5811. shouldParseArrow(params) {
  5812. return this.match(14) || super.shouldParseArrow(params);
  5813. }
  5814. setArrowFunctionParameters(node, params) {
  5815. if (this.state.noArrowParamsConversionAt.indexOf(node.start) !== -1) {
  5816. node.params = params;
  5817. } else {
  5818. super.setArrowFunctionParameters(node, params);
  5819. }
  5820. }
  5821. checkParams(node, allowDuplicates, isArrowFunction, strictModeChanged = true) {
  5822. if (isArrowFunction && this.state.noArrowParamsConversionAt.indexOf(node.start) !== -1) {
  5823. return;
  5824. }
  5825. for (let i = 0; i < node.params.length; i++) {
  5826. if (this.isThisParam(node.params[i]) && i > 0) {
  5827. this.raise(FlowErrors.ThisParamMustBeFirst, {
  5828. at: node.params[i]
  5829. });
  5830. }
  5831. }
  5832. return super.checkParams(node, allowDuplicates, isArrowFunction, strictModeChanged);
  5833. }
  5834. parseParenAndDistinguishExpression(canBeArrow) {
  5835. return super.parseParenAndDistinguishExpression(canBeArrow && this.state.noArrowAt.indexOf(this.state.start) === -1);
  5836. }
  5837. parseSubscripts(base, startLoc, noCalls) {
  5838. if (base.type === "Identifier" && base.name === "async" && this.state.noArrowAt.indexOf(startLoc.index) !== -1) {
  5839. this.next();
  5840. const node = this.startNodeAt(startLoc);
  5841. node.callee = base;
  5842. node.arguments = super.parseCallExpressionArguments(11, false);
  5843. base = this.finishNode(node, "CallExpression");
  5844. } else if (base.type === "Identifier" && base.name === "async" && this.match(47)) {
  5845. const state = this.state.clone();
  5846. const arrow = this.tryParse(abort => this.parseAsyncArrowWithTypeParameters(startLoc) || abort(), state);
  5847. if (!arrow.error && !arrow.aborted) return arrow.node;
  5848. const result = this.tryParse(() => super.parseSubscripts(base, startLoc, noCalls), state);
  5849. if (result.node && !result.error) return result.node;
  5850. if (arrow.node) {
  5851. this.state = arrow.failState;
  5852. return arrow.node;
  5853. }
  5854. if (result.node) {
  5855. this.state = result.failState;
  5856. return result.node;
  5857. }
  5858. throw arrow.error || result.error;
  5859. }
  5860. return super.parseSubscripts(base, startLoc, noCalls);
  5861. }
  5862. parseSubscript(base, startLoc, noCalls, subscriptState) {
  5863. if (this.match(18) && this.isLookaheadToken_lt()) {
  5864. subscriptState.optionalChainMember = true;
  5865. if (noCalls) {
  5866. subscriptState.stop = true;
  5867. return base;
  5868. }
  5869. this.next();
  5870. const node = this.startNodeAt(startLoc);
  5871. node.callee = base;
  5872. node.typeArguments = this.flowParseTypeParameterInstantiation();
  5873. this.expect(10);
  5874. node.arguments = this.parseCallExpressionArguments(11, false);
  5875. node.optional = true;
  5876. return this.finishCallExpression(node, true);
  5877. } else if (!noCalls && this.shouldParseTypes() && this.match(47)) {
  5878. const node = this.startNodeAt(startLoc);
  5879. node.callee = base;
  5880. const result = this.tryParse(() => {
  5881. node.typeArguments = this.flowParseTypeParameterInstantiationCallOrNew();
  5882. this.expect(10);
  5883. node.arguments = super.parseCallExpressionArguments(11, false);
  5884. if (subscriptState.optionalChainMember) {
  5885. node.optional = false;
  5886. }
  5887. return this.finishCallExpression(node, subscriptState.optionalChainMember);
  5888. });
  5889. if (result.node) {
  5890. if (result.error) this.state = result.failState;
  5891. return result.node;
  5892. }
  5893. }
  5894. return super.parseSubscript(base, startLoc, noCalls, subscriptState);
  5895. }
  5896. parseNewCallee(node) {
  5897. super.parseNewCallee(node);
  5898. let targs = null;
  5899. if (this.shouldParseTypes() && this.match(47)) {
  5900. targs = this.tryParse(() => this.flowParseTypeParameterInstantiationCallOrNew()).node;
  5901. }
  5902. node.typeArguments = targs;
  5903. }
  5904. parseAsyncArrowWithTypeParameters(startLoc) {
  5905. const node = this.startNodeAt(startLoc);
  5906. this.parseFunctionParams(node, false);
  5907. if (!this.parseArrow(node)) return;
  5908. return super.parseArrowExpression(node, undefined, true);
  5909. }
  5910. readToken_mult_modulo(code) {
  5911. const next = this.input.charCodeAt(this.state.pos + 1);
  5912. if (code === 42 && next === 47 && this.state.hasFlowComment) {
  5913. this.state.hasFlowComment = false;
  5914. this.state.pos += 2;
  5915. this.nextToken();
  5916. return;
  5917. }
  5918. super.readToken_mult_modulo(code);
  5919. }
  5920. readToken_pipe_amp(code) {
  5921. const next = this.input.charCodeAt(this.state.pos + 1);
  5922. if (code === 124 && next === 125) {
  5923. this.finishOp(9, 2);
  5924. return;
  5925. }
  5926. super.readToken_pipe_amp(code);
  5927. }
  5928. parseTopLevel(file, program) {
  5929. const fileNode = super.parseTopLevel(file, program);
  5930. if (this.state.hasFlowComment) {
  5931. this.raise(FlowErrors.UnterminatedFlowComment, {
  5932. at: this.state.curPosition()
  5933. });
  5934. }
  5935. return fileNode;
  5936. }
  5937. skipBlockComment() {
  5938. if (this.hasPlugin("flowComments") && this.skipFlowComment()) {
  5939. if (this.state.hasFlowComment) {
  5940. throw this.raise(FlowErrors.NestedFlowComment, {
  5941. at: this.state.startLoc
  5942. });
  5943. }
  5944. this.hasFlowCommentCompletion();
  5945. const commentSkip = this.skipFlowComment();
  5946. if (commentSkip) {
  5947. this.state.pos += commentSkip;
  5948. this.state.hasFlowComment = true;
  5949. }
  5950. return;
  5951. }
  5952. return super.skipBlockComment(this.state.hasFlowComment ? "*-/" : "*/");
  5953. }
  5954. skipFlowComment() {
  5955. const {
  5956. pos
  5957. } = this.state;
  5958. let shiftToFirstNonWhiteSpace = 2;
  5959. while ([32, 9].includes(this.input.charCodeAt(pos + shiftToFirstNonWhiteSpace))) {
  5960. shiftToFirstNonWhiteSpace++;
  5961. }
  5962. const ch2 = this.input.charCodeAt(shiftToFirstNonWhiteSpace + pos);
  5963. const ch3 = this.input.charCodeAt(shiftToFirstNonWhiteSpace + pos + 1);
  5964. if (ch2 === 58 && ch3 === 58) {
  5965. return shiftToFirstNonWhiteSpace + 2;
  5966. }
  5967. if (this.input.slice(shiftToFirstNonWhiteSpace + pos, shiftToFirstNonWhiteSpace + pos + 12) === "flow-include") {
  5968. return shiftToFirstNonWhiteSpace + 12;
  5969. }
  5970. if (ch2 === 58 && ch3 !== 58) {
  5971. return shiftToFirstNonWhiteSpace;
  5972. }
  5973. return false;
  5974. }
  5975. hasFlowCommentCompletion() {
  5976. const end = this.input.indexOf("*/", this.state.pos);
  5977. if (end === -1) {
  5978. throw this.raise(Errors.UnterminatedComment, {
  5979. at: this.state.curPosition()
  5980. });
  5981. }
  5982. }
  5983. flowEnumErrorBooleanMemberNotInitialized(loc, {
  5984. enumName,
  5985. memberName
  5986. }) {
  5987. this.raise(FlowErrors.EnumBooleanMemberNotInitialized, {
  5988. at: loc,
  5989. memberName,
  5990. enumName
  5991. });
  5992. }
  5993. flowEnumErrorInvalidMemberInitializer(loc, enumContext) {
  5994. return this.raise(!enumContext.explicitType ? FlowErrors.EnumInvalidMemberInitializerUnknownType : enumContext.explicitType === "symbol" ? FlowErrors.EnumInvalidMemberInitializerSymbolType : FlowErrors.EnumInvalidMemberInitializerPrimaryType, Object.assign({
  5995. at: loc
  5996. }, enumContext));
  5997. }
  5998. flowEnumErrorNumberMemberNotInitialized(loc, {
  5999. enumName,
  6000. memberName
  6001. }) {
  6002. this.raise(FlowErrors.EnumNumberMemberNotInitialized, {
  6003. at: loc,
  6004. enumName,
  6005. memberName
  6006. });
  6007. }
  6008. flowEnumErrorStringMemberInconsistentlyInitailized(node, {
  6009. enumName
  6010. }) {
  6011. this.raise(FlowErrors.EnumStringMemberInconsistentlyInitailized, {
  6012. at: node,
  6013. enumName
  6014. });
  6015. }
  6016. flowEnumMemberInit() {
  6017. const startLoc = this.state.startLoc;
  6018. const endOfInit = () => this.match(12) || this.match(8);
  6019. switch (this.state.type) {
  6020. case 132:
  6021. {
  6022. const literal = this.parseNumericLiteral(this.state.value);
  6023. if (endOfInit()) {
  6024. return {
  6025. type: "number",
  6026. loc: literal.loc.start,
  6027. value: literal
  6028. };
  6029. }
  6030. return {
  6031. type: "invalid",
  6032. loc: startLoc
  6033. };
  6034. }
  6035. case 131:
  6036. {
  6037. const literal = this.parseStringLiteral(this.state.value);
  6038. if (endOfInit()) {
  6039. return {
  6040. type: "string",
  6041. loc: literal.loc.start,
  6042. value: literal
  6043. };
  6044. }
  6045. return {
  6046. type: "invalid",
  6047. loc: startLoc
  6048. };
  6049. }
  6050. case 85:
  6051. case 86:
  6052. {
  6053. const literal = this.parseBooleanLiteral(this.match(85));
  6054. if (endOfInit()) {
  6055. return {
  6056. type: "boolean",
  6057. loc: literal.loc.start,
  6058. value: literal
  6059. };
  6060. }
  6061. return {
  6062. type: "invalid",
  6063. loc: startLoc
  6064. };
  6065. }
  6066. default:
  6067. return {
  6068. type: "invalid",
  6069. loc: startLoc
  6070. };
  6071. }
  6072. }
  6073. flowEnumMemberRaw() {
  6074. const loc = this.state.startLoc;
  6075. const id = this.parseIdentifier(true);
  6076. const init = this.eat(29) ? this.flowEnumMemberInit() : {
  6077. type: "none",
  6078. loc
  6079. };
  6080. return {
  6081. id,
  6082. init
  6083. };
  6084. }
  6085. flowEnumCheckExplicitTypeMismatch(loc, context, expectedType) {
  6086. const {
  6087. explicitType
  6088. } = context;
  6089. if (explicitType === null) {
  6090. return;
  6091. }
  6092. if (explicitType !== expectedType) {
  6093. this.flowEnumErrorInvalidMemberInitializer(loc, context);
  6094. }
  6095. }
  6096. flowEnumMembers({
  6097. enumName,
  6098. explicitType
  6099. }) {
  6100. const seenNames = new Set();
  6101. const members = {
  6102. booleanMembers: [],
  6103. numberMembers: [],
  6104. stringMembers: [],
  6105. defaultedMembers: []
  6106. };
  6107. let hasUnknownMembers = false;
  6108. while (!this.match(8)) {
  6109. if (this.eat(21)) {
  6110. hasUnknownMembers = true;
  6111. break;
  6112. }
  6113. const memberNode = this.startNode();
  6114. const {
  6115. id,
  6116. init
  6117. } = this.flowEnumMemberRaw();
  6118. const memberName = id.name;
  6119. if (memberName === "") {
  6120. continue;
  6121. }
  6122. if (/^[a-z]/.test(memberName)) {
  6123. this.raise(FlowErrors.EnumInvalidMemberName, {
  6124. at: id,
  6125. memberName,
  6126. suggestion: memberName[0].toUpperCase() + memberName.slice(1),
  6127. enumName
  6128. });
  6129. }
  6130. if (seenNames.has(memberName)) {
  6131. this.raise(FlowErrors.EnumDuplicateMemberName, {
  6132. at: id,
  6133. memberName,
  6134. enumName
  6135. });
  6136. }
  6137. seenNames.add(memberName);
  6138. const context = {
  6139. enumName,
  6140. explicitType,
  6141. memberName
  6142. };
  6143. memberNode.id = id;
  6144. switch (init.type) {
  6145. case "boolean":
  6146. {
  6147. this.flowEnumCheckExplicitTypeMismatch(init.loc, context, "boolean");
  6148. memberNode.init = init.value;
  6149. members.booleanMembers.push(this.finishNode(memberNode, "EnumBooleanMember"));
  6150. break;
  6151. }
  6152. case "number":
  6153. {
  6154. this.flowEnumCheckExplicitTypeMismatch(init.loc, context, "number");
  6155. memberNode.init = init.value;
  6156. members.numberMembers.push(this.finishNode(memberNode, "EnumNumberMember"));
  6157. break;
  6158. }
  6159. case "string":
  6160. {
  6161. this.flowEnumCheckExplicitTypeMismatch(init.loc, context, "string");
  6162. memberNode.init = init.value;
  6163. members.stringMembers.push(this.finishNode(memberNode, "EnumStringMember"));
  6164. break;
  6165. }
  6166. case "invalid":
  6167. {
  6168. throw this.flowEnumErrorInvalidMemberInitializer(init.loc, context);
  6169. }
  6170. case "none":
  6171. {
  6172. switch (explicitType) {
  6173. case "boolean":
  6174. this.flowEnumErrorBooleanMemberNotInitialized(init.loc, context);
  6175. break;
  6176. case "number":
  6177. this.flowEnumErrorNumberMemberNotInitialized(init.loc, context);
  6178. break;
  6179. default:
  6180. members.defaultedMembers.push(this.finishNode(memberNode, "EnumDefaultedMember"));
  6181. }
  6182. }
  6183. }
  6184. if (!this.match(8)) {
  6185. this.expect(12);
  6186. }
  6187. }
  6188. return {
  6189. members,
  6190. hasUnknownMembers
  6191. };
  6192. }
  6193. flowEnumStringMembers(initializedMembers, defaultedMembers, {
  6194. enumName
  6195. }) {
  6196. if (initializedMembers.length === 0) {
  6197. return defaultedMembers;
  6198. } else if (defaultedMembers.length === 0) {
  6199. return initializedMembers;
  6200. } else if (defaultedMembers.length > initializedMembers.length) {
  6201. for (const member of initializedMembers) {
  6202. this.flowEnumErrorStringMemberInconsistentlyInitailized(member, {
  6203. enumName
  6204. });
  6205. }
  6206. return defaultedMembers;
  6207. } else {
  6208. for (const member of defaultedMembers) {
  6209. this.flowEnumErrorStringMemberInconsistentlyInitailized(member, {
  6210. enumName
  6211. });
  6212. }
  6213. return initializedMembers;
  6214. }
  6215. }
  6216. flowEnumParseExplicitType({
  6217. enumName
  6218. }) {
  6219. if (!this.eatContextual(101)) return null;
  6220. if (!tokenIsIdentifier(this.state.type)) {
  6221. throw this.raise(FlowErrors.EnumInvalidExplicitTypeUnknownSupplied, {
  6222. at: this.state.startLoc,
  6223. enumName
  6224. });
  6225. }
  6226. const {
  6227. value
  6228. } = this.state;
  6229. this.next();
  6230. if (value !== "boolean" && value !== "number" && value !== "string" && value !== "symbol") {
  6231. this.raise(FlowErrors.EnumInvalidExplicitType, {
  6232. at: this.state.startLoc,
  6233. enumName,
  6234. invalidEnumType: value
  6235. });
  6236. }
  6237. return value;
  6238. }
  6239. flowEnumBody(node, id) {
  6240. const enumName = id.name;
  6241. const nameLoc = id.loc.start;
  6242. const explicitType = this.flowEnumParseExplicitType({
  6243. enumName
  6244. });
  6245. this.expect(5);
  6246. const {
  6247. members,
  6248. hasUnknownMembers
  6249. } = this.flowEnumMembers({
  6250. enumName,
  6251. explicitType
  6252. });
  6253. node.hasUnknownMembers = hasUnknownMembers;
  6254. switch (explicitType) {
  6255. case "boolean":
  6256. node.explicitType = true;
  6257. node.members = members.booleanMembers;
  6258. this.expect(8);
  6259. return this.finishNode(node, "EnumBooleanBody");
  6260. case "number":
  6261. node.explicitType = true;
  6262. node.members = members.numberMembers;
  6263. this.expect(8);
  6264. return this.finishNode(node, "EnumNumberBody");
  6265. case "string":
  6266. node.explicitType = true;
  6267. node.members = this.flowEnumStringMembers(members.stringMembers, members.defaultedMembers, {
  6268. enumName
  6269. });
  6270. this.expect(8);
  6271. return this.finishNode(node, "EnumStringBody");
  6272. case "symbol":
  6273. node.members = members.defaultedMembers;
  6274. this.expect(8);
  6275. return this.finishNode(node, "EnumSymbolBody");
  6276. default:
  6277. {
  6278. const empty = () => {
  6279. node.members = [];
  6280. this.expect(8);
  6281. return this.finishNode(node, "EnumStringBody");
  6282. };
  6283. node.explicitType = false;
  6284. const boolsLen = members.booleanMembers.length;
  6285. const numsLen = members.numberMembers.length;
  6286. const strsLen = members.stringMembers.length;
  6287. const defaultedLen = members.defaultedMembers.length;
  6288. if (!boolsLen && !numsLen && !strsLen && !defaultedLen) {
  6289. return empty();
  6290. } else if (!boolsLen && !numsLen) {
  6291. node.members = this.flowEnumStringMembers(members.stringMembers, members.defaultedMembers, {
  6292. enumName
  6293. });
  6294. this.expect(8);
  6295. return this.finishNode(node, "EnumStringBody");
  6296. } else if (!numsLen && !strsLen && boolsLen >= defaultedLen) {
  6297. for (const member of members.defaultedMembers) {
  6298. this.flowEnumErrorBooleanMemberNotInitialized(member.loc.start, {
  6299. enumName,
  6300. memberName: member.id.name
  6301. });
  6302. }
  6303. node.members = members.booleanMembers;
  6304. this.expect(8);
  6305. return this.finishNode(node, "EnumBooleanBody");
  6306. } else if (!boolsLen && !strsLen && numsLen >= defaultedLen) {
  6307. for (const member of members.defaultedMembers) {
  6308. this.flowEnumErrorNumberMemberNotInitialized(member.loc.start, {
  6309. enumName,
  6310. memberName: member.id.name
  6311. });
  6312. }
  6313. node.members = members.numberMembers;
  6314. this.expect(8);
  6315. return this.finishNode(node, "EnumNumberBody");
  6316. } else {
  6317. this.raise(FlowErrors.EnumInconsistentMemberValues, {
  6318. at: nameLoc,
  6319. enumName
  6320. });
  6321. return empty();
  6322. }
  6323. }
  6324. }
  6325. }
  6326. flowParseEnumDeclaration(node) {
  6327. const id = this.parseIdentifier();
  6328. node.id = id;
  6329. node.body = this.flowEnumBody(this.startNode(), id);
  6330. return this.finishNode(node, "EnumDeclaration");
  6331. }
  6332. isLookaheadToken_lt() {
  6333. const next = this.nextTokenStart();
  6334. if (this.input.charCodeAt(next) === 60) {
  6335. const afterNext = this.input.charCodeAt(next + 1);
  6336. return afterNext !== 60 && afterNext !== 61;
  6337. }
  6338. return false;
  6339. }
  6340. maybeUnwrapTypeCastExpression(node) {
  6341. return node.type === "TypeCastExpression" ? node.expression : node;
  6342. }
  6343. });
  6344. const entities = {
  6345. __proto__: null,
  6346. quot: "\u0022",
  6347. amp: "&",
  6348. apos: "\u0027",
  6349. lt: "<",
  6350. gt: ">",
  6351. nbsp: "\u00A0",
  6352. iexcl: "\u00A1",
  6353. cent: "\u00A2",
  6354. pound: "\u00A3",
  6355. curren: "\u00A4",
  6356. yen: "\u00A5",
  6357. brvbar: "\u00A6",
  6358. sect: "\u00A7",
  6359. uml: "\u00A8",
  6360. copy: "\u00A9",
  6361. ordf: "\u00AA",
  6362. laquo: "\u00AB",
  6363. not: "\u00AC",
  6364. shy: "\u00AD",
  6365. reg: "\u00AE",
  6366. macr: "\u00AF",
  6367. deg: "\u00B0",
  6368. plusmn: "\u00B1",
  6369. sup2: "\u00B2",
  6370. sup3: "\u00B3",
  6371. acute: "\u00B4",
  6372. micro: "\u00B5",
  6373. para: "\u00B6",
  6374. middot: "\u00B7",
  6375. cedil: "\u00B8",
  6376. sup1: "\u00B9",
  6377. ordm: "\u00BA",
  6378. raquo: "\u00BB",
  6379. frac14: "\u00BC",
  6380. frac12: "\u00BD",
  6381. frac34: "\u00BE",
  6382. iquest: "\u00BF",
  6383. Agrave: "\u00C0",
  6384. Aacute: "\u00C1",
  6385. Acirc: "\u00C2",
  6386. Atilde: "\u00C3",
  6387. Auml: "\u00C4",
  6388. Aring: "\u00C5",
  6389. AElig: "\u00C6",
  6390. Ccedil: "\u00C7",
  6391. Egrave: "\u00C8",
  6392. Eacute: "\u00C9",
  6393. Ecirc: "\u00CA",
  6394. Euml: "\u00CB",
  6395. Igrave: "\u00CC",
  6396. Iacute: "\u00CD",
  6397. Icirc: "\u00CE",
  6398. Iuml: "\u00CF",
  6399. ETH: "\u00D0",
  6400. Ntilde: "\u00D1",
  6401. Ograve: "\u00D2",
  6402. Oacute: "\u00D3",
  6403. Ocirc: "\u00D4",
  6404. Otilde: "\u00D5",
  6405. Ouml: "\u00D6",
  6406. times: "\u00D7",
  6407. Oslash: "\u00D8",
  6408. Ugrave: "\u00D9",
  6409. Uacute: "\u00DA",
  6410. Ucirc: "\u00DB",
  6411. Uuml: "\u00DC",
  6412. Yacute: "\u00DD",
  6413. THORN: "\u00DE",
  6414. szlig: "\u00DF",
  6415. agrave: "\u00E0",
  6416. aacute: "\u00E1",
  6417. acirc: "\u00E2",
  6418. atilde: "\u00E3",
  6419. auml: "\u00E4",
  6420. aring: "\u00E5",
  6421. aelig: "\u00E6",
  6422. ccedil: "\u00E7",
  6423. egrave: "\u00E8",
  6424. eacute: "\u00E9",
  6425. ecirc: "\u00EA",
  6426. euml: "\u00EB",
  6427. igrave: "\u00EC",
  6428. iacute: "\u00ED",
  6429. icirc: "\u00EE",
  6430. iuml: "\u00EF",
  6431. eth: "\u00F0",
  6432. ntilde: "\u00F1",
  6433. ograve: "\u00F2",
  6434. oacute: "\u00F3",
  6435. ocirc: "\u00F4",
  6436. otilde: "\u00F5",
  6437. ouml: "\u00F6",
  6438. divide: "\u00F7",
  6439. oslash: "\u00F8",
  6440. ugrave: "\u00F9",
  6441. uacute: "\u00FA",
  6442. ucirc: "\u00FB",
  6443. uuml: "\u00FC",
  6444. yacute: "\u00FD",
  6445. thorn: "\u00FE",
  6446. yuml: "\u00FF",
  6447. OElig: "\u0152",
  6448. oelig: "\u0153",
  6449. Scaron: "\u0160",
  6450. scaron: "\u0161",
  6451. Yuml: "\u0178",
  6452. fnof: "\u0192",
  6453. circ: "\u02C6",
  6454. tilde: "\u02DC",
  6455. Alpha: "\u0391",
  6456. Beta: "\u0392",
  6457. Gamma: "\u0393",
  6458. Delta: "\u0394",
  6459. Epsilon: "\u0395",
  6460. Zeta: "\u0396",
  6461. Eta: "\u0397",
  6462. Theta: "\u0398",
  6463. Iota: "\u0399",
  6464. Kappa: "\u039A",
  6465. Lambda: "\u039B",
  6466. Mu: "\u039C",
  6467. Nu: "\u039D",
  6468. Xi: "\u039E",
  6469. Omicron: "\u039F",
  6470. Pi: "\u03A0",
  6471. Rho: "\u03A1",
  6472. Sigma: "\u03A3",
  6473. Tau: "\u03A4",
  6474. Upsilon: "\u03A5",
  6475. Phi: "\u03A6",
  6476. Chi: "\u03A7",
  6477. Psi: "\u03A8",
  6478. Omega: "\u03A9",
  6479. alpha: "\u03B1",
  6480. beta: "\u03B2",
  6481. gamma: "\u03B3",
  6482. delta: "\u03B4",
  6483. epsilon: "\u03B5",
  6484. zeta: "\u03B6",
  6485. eta: "\u03B7",
  6486. theta: "\u03B8",
  6487. iota: "\u03B9",
  6488. kappa: "\u03BA",
  6489. lambda: "\u03BB",
  6490. mu: "\u03BC",
  6491. nu: "\u03BD",
  6492. xi: "\u03BE",
  6493. omicron: "\u03BF",
  6494. pi: "\u03C0",
  6495. rho: "\u03C1",
  6496. sigmaf: "\u03C2",
  6497. sigma: "\u03C3",
  6498. tau: "\u03C4",
  6499. upsilon: "\u03C5",
  6500. phi: "\u03C6",
  6501. chi: "\u03C7",
  6502. psi: "\u03C8",
  6503. omega: "\u03C9",
  6504. thetasym: "\u03D1",
  6505. upsih: "\u03D2",
  6506. piv: "\u03D6",
  6507. ensp: "\u2002",
  6508. emsp: "\u2003",
  6509. thinsp: "\u2009",
  6510. zwnj: "\u200C",
  6511. zwj: "\u200D",
  6512. lrm: "\u200E",
  6513. rlm: "\u200F",
  6514. ndash: "\u2013",
  6515. mdash: "\u2014",
  6516. lsquo: "\u2018",
  6517. rsquo: "\u2019",
  6518. sbquo: "\u201A",
  6519. ldquo: "\u201C",
  6520. rdquo: "\u201D",
  6521. bdquo: "\u201E",
  6522. dagger: "\u2020",
  6523. Dagger: "\u2021",
  6524. bull: "\u2022",
  6525. hellip: "\u2026",
  6526. permil: "\u2030",
  6527. prime: "\u2032",
  6528. Prime: "\u2033",
  6529. lsaquo: "\u2039",
  6530. rsaquo: "\u203A",
  6531. oline: "\u203E",
  6532. frasl: "\u2044",
  6533. euro: "\u20AC",
  6534. image: "\u2111",
  6535. weierp: "\u2118",
  6536. real: "\u211C",
  6537. trade: "\u2122",
  6538. alefsym: "\u2135",
  6539. larr: "\u2190",
  6540. uarr: "\u2191",
  6541. rarr: "\u2192",
  6542. darr: "\u2193",
  6543. harr: "\u2194",
  6544. crarr: "\u21B5",
  6545. lArr: "\u21D0",
  6546. uArr: "\u21D1",
  6547. rArr: "\u21D2",
  6548. dArr: "\u21D3",
  6549. hArr: "\u21D4",
  6550. forall: "\u2200",
  6551. part: "\u2202",
  6552. exist: "\u2203",
  6553. empty: "\u2205",
  6554. nabla: "\u2207",
  6555. isin: "\u2208",
  6556. notin: "\u2209",
  6557. ni: "\u220B",
  6558. prod: "\u220F",
  6559. sum: "\u2211",
  6560. minus: "\u2212",
  6561. lowast: "\u2217",
  6562. radic: "\u221A",
  6563. prop: "\u221D",
  6564. infin: "\u221E",
  6565. ang: "\u2220",
  6566. and: "\u2227",
  6567. or: "\u2228",
  6568. cap: "\u2229",
  6569. cup: "\u222A",
  6570. int: "\u222B",
  6571. there4: "\u2234",
  6572. sim: "\u223C",
  6573. cong: "\u2245",
  6574. asymp: "\u2248",
  6575. ne: "\u2260",
  6576. equiv: "\u2261",
  6577. le: "\u2264",
  6578. ge: "\u2265",
  6579. sub: "\u2282",
  6580. sup: "\u2283",
  6581. nsub: "\u2284",
  6582. sube: "\u2286",
  6583. supe: "\u2287",
  6584. oplus: "\u2295",
  6585. otimes: "\u2297",
  6586. perp: "\u22A5",
  6587. sdot: "\u22C5",
  6588. lceil: "\u2308",
  6589. rceil: "\u2309",
  6590. lfloor: "\u230A",
  6591. rfloor: "\u230B",
  6592. lang: "\u2329",
  6593. rang: "\u232A",
  6594. loz: "\u25CA",
  6595. spades: "\u2660",
  6596. clubs: "\u2663",
  6597. hearts: "\u2665",
  6598. diams: "\u2666"
  6599. };
  6600. const JsxErrors = ParseErrorEnum`jsx`({
  6601. AttributeIsEmpty: "JSX attributes must only be assigned a non-empty expression.",
  6602. MissingClosingTagElement: ({
  6603. openingTagName
  6604. }) => `Expected corresponding JSX closing tag for <${openingTagName}>.`,
  6605. MissingClosingTagFragment: "Expected corresponding JSX closing tag for <>.",
  6606. UnexpectedSequenceExpression: "Sequence expressions cannot be directly nested inside JSX. Did you mean to wrap it in parentheses (...)?",
  6607. UnexpectedToken: ({
  6608. unexpected,
  6609. HTMLEntity
  6610. }) => `Unexpected token \`${unexpected}\`. Did you mean \`${HTMLEntity}\` or \`{'${unexpected}'}\`?`,
  6611. UnsupportedJsxValue: "JSX value should be either an expression or a quoted JSX text.",
  6612. UnterminatedJsxContent: "Unterminated JSX contents.",
  6613. UnwrappedAdjacentJSXElements: "Adjacent JSX elements must be wrapped in an enclosing tag. Did you want a JSX fragment <>...</>?"
  6614. });
  6615. function isFragment(object) {
  6616. return object ? object.type === "JSXOpeningFragment" || object.type === "JSXClosingFragment" : false;
  6617. }
  6618. function getQualifiedJSXName(object) {
  6619. if (object.type === "JSXIdentifier") {
  6620. return object.name;
  6621. }
  6622. if (object.type === "JSXNamespacedName") {
  6623. return object.namespace.name + ":" + object.name.name;
  6624. }
  6625. if (object.type === "JSXMemberExpression") {
  6626. return getQualifiedJSXName(object.object) + "." + getQualifiedJSXName(object.property);
  6627. }
  6628. throw new Error("Node had unexpected type: " + object.type);
  6629. }
  6630. var jsx = (superClass => class JSXParserMixin extends superClass {
  6631. jsxReadToken() {
  6632. let out = "";
  6633. let chunkStart = this.state.pos;
  6634. for (;;) {
  6635. if (this.state.pos >= this.length) {
  6636. throw this.raise(JsxErrors.UnterminatedJsxContent, {
  6637. at: this.state.startLoc
  6638. });
  6639. }
  6640. const ch = this.input.charCodeAt(this.state.pos);
  6641. switch (ch) {
  6642. case 60:
  6643. case 123:
  6644. if (this.state.pos === this.state.start) {
  6645. if (ch === 60 && this.state.canStartJSXElement) {
  6646. ++this.state.pos;
  6647. return this.finishToken(140);
  6648. }
  6649. return super.getTokenFromCode(ch);
  6650. }
  6651. out += this.input.slice(chunkStart, this.state.pos);
  6652. return this.finishToken(139, out);
  6653. case 38:
  6654. out += this.input.slice(chunkStart, this.state.pos);
  6655. out += this.jsxReadEntity();
  6656. chunkStart = this.state.pos;
  6657. break;
  6658. case 62:
  6659. case 125:
  6660. default:
  6661. if (isNewLine(ch)) {
  6662. out += this.input.slice(chunkStart, this.state.pos);
  6663. out += this.jsxReadNewLine(true);
  6664. chunkStart = this.state.pos;
  6665. } else {
  6666. ++this.state.pos;
  6667. }
  6668. }
  6669. }
  6670. }
  6671. jsxReadNewLine(normalizeCRLF) {
  6672. const ch = this.input.charCodeAt(this.state.pos);
  6673. let out;
  6674. ++this.state.pos;
  6675. if (ch === 13 && this.input.charCodeAt(this.state.pos) === 10) {
  6676. ++this.state.pos;
  6677. out = normalizeCRLF ? "\n" : "\r\n";
  6678. } else {
  6679. out = String.fromCharCode(ch);
  6680. }
  6681. ++this.state.curLine;
  6682. this.state.lineStart = this.state.pos;
  6683. return out;
  6684. }
  6685. jsxReadString(quote) {
  6686. let out = "";
  6687. let chunkStart = ++this.state.pos;
  6688. for (;;) {
  6689. if (this.state.pos >= this.length) {
  6690. throw this.raise(Errors.UnterminatedString, {
  6691. at: this.state.startLoc
  6692. });
  6693. }
  6694. const ch = this.input.charCodeAt(this.state.pos);
  6695. if (ch === quote) break;
  6696. if (ch === 38) {
  6697. out += this.input.slice(chunkStart, this.state.pos);
  6698. out += this.jsxReadEntity();
  6699. chunkStart = this.state.pos;
  6700. } else if (isNewLine(ch)) {
  6701. out += this.input.slice(chunkStart, this.state.pos);
  6702. out += this.jsxReadNewLine(false);
  6703. chunkStart = this.state.pos;
  6704. } else {
  6705. ++this.state.pos;
  6706. }
  6707. }
  6708. out += this.input.slice(chunkStart, this.state.pos++);
  6709. return this.finishToken(131, out);
  6710. }
  6711. jsxReadEntity() {
  6712. const startPos = ++this.state.pos;
  6713. if (this.codePointAtPos(this.state.pos) === 35) {
  6714. ++this.state.pos;
  6715. let radix = 10;
  6716. if (this.codePointAtPos(this.state.pos) === 120) {
  6717. radix = 16;
  6718. ++this.state.pos;
  6719. }
  6720. const codePoint = this.readInt(radix, undefined, false, "bail");
  6721. if (codePoint !== null && this.codePointAtPos(this.state.pos) === 59) {
  6722. ++this.state.pos;
  6723. return String.fromCodePoint(codePoint);
  6724. }
  6725. } else {
  6726. let count = 0;
  6727. let semi = false;
  6728. while (count++ < 10 && this.state.pos < this.length && !(semi = this.codePointAtPos(this.state.pos) == 59)) {
  6729. ++this.state.pos;
  6730. }
  6731. if (semi) {
  6732. const desc = this.input.slice(startPos, this.state.pos);
  6733. const entity = entities[desc];
  6734. ++this.state.pos;
  6735. if (entity) {
  6736. return entity;
  6737. }
  6738. }
  6739. }
  6740. this.state.pos = startPos;
  6741. return "&";
  6742. }
  6743. jsxReadWord() {
  6744. let ch;
  6745. const start = this.state.pos;
  6746. do {
  6747. ch = this.input.charCodeAt(++this.state.pos);
  6748. } while (isIdentifierChar(ch) || ch === 45);
  6749. return this.finishToken(138, this.input.slice(start, this.state.pos));
  6750. }
  6751. jsxParseIdentifier() {
  6752. const node = this.startNode();
  6753. if (this.match(138)) {
  6754. node.name = this.state.value;
  6755. } else if (tokenIsKeyword(this.state.type)) {
  6756. node.name = tokenLabelName(this.state.type);
  6757. } else {
  6758. this.unexpected();
  6759. }
  6760. this.next();
  6761. return this.finishNode(node, "JSXIdentifier");
  6762. }
  6763. jsxParseNamespacedName() {
  6764. const startLoc = this.state.startLoc;
  6765. const name = this.jsxParseIdentifier();
  6766. if (!this.eat(14)) return name;
  6767. const node = this.startNodeAt(startLoc);
  6768. node.namespace = name;
  6769. node.name = this.jsxParseIdentifier();
  6770. return this.finishNode(node, "JSXNamespacedName");
  6771. }
  6772. jsxParseElementName() {
  6773. const startLoc = this.state.startLoc;
  6774. let node = this.jsxParseNamespacedName();
  6775. if (node.type === "JSXNamespacedName") {
  6776. return node;
  6777. }
  6778. while (this.eat(16)) {
  6779. const newNode = this.startNodeAt(startLoc);
  6780. newNode.object = node;
  6781. newNode.property = this.jsxParseIdentifier();
  6782. node = this.finishNode(newNode, "JSXMemberExpression");
  6783. }
  6784. return node;
  6785. }
  6786. jsxParseAttributeValue() {
  6787. let node;
  6788. switch (this.state.type) {
  6789. case 5:
  6790. node = this.startNode();
  6791. this.setContext(types.brace);
  6792. this.next();
  6793. node = this.jsxParseExpressionContainer(node, types.j_oTag);
  6794. if (node.expression.type === "JSXEmptyExpression") {
  6795. this.raise(JsxErrors.AttributeIsEmpty, {
  6796. at: node
  6797. });
  6798. }
  6799. return node;
  6800. case 140:
  6801. case 131:
  6802. return this.parseExprAtom();
  6803. default:
  6804. throw this.raise(JsxErrors.UnsupportedJsxValue, {
  6805. at: this.state.startLoc
  6806. });
  6807. }
  6808. }
  6809. jsxParseEmptyExpression() {
  6810. const node = this.startNodeAt(this.state.lastTokEndLoc);
  6811. return this.finishNodeAt(node, "JSXEmptyExpression", this.state.startLoc);
  6812. }
  6813. jsxParseSpreadChild(node) {
  6814. this.next();
  6815. node.expression = this.parseExpression();
  6816. this.setContext(types.j_expr);
  6817. this.state.canStartJSXElement = true;
  6818. this.expect(8);
  6819. return this.finishNode(node, "JSXSpreadChild");
  6820. }
  6821. jsxParseExpressionContainer(node, previousContext) {
  6822. if (this.match(8)) {
  6823. node.expression = this.jsxParseEmptyExpression();
  6824. } else {
  6825. const expression = this.parseExpression();
  6826. node.expression = expression;
  6827. }
  6828. this.setContext(previousContext);
  6829. this.state.canStartJSXElement = true;
  6830. this.expect(8);
  6831. return this.finishNode(node, "JSXExpressionContainer");
  6832. }
  6833. jsxParseAttribute() {
  6834. const node = this.startNode();
  6835. if (this.match(5)) {
  6836. this.setContext(types.brace);
  6837. this.next();
  6838. this.expect(21);
  6839. node.argument = this.parseMaybeAssignAllowIn();
  6840. this.setContext(types.j_oTag);
  6841. this.state.canStartJSXElement = true;
  6842. this.expect(8);
  6843. return this.finishNode(node, "JSXSpreadAttribute");
  6844. }
  6845. node.name = this.jsxParseNamespacedName();
  6846. node.value = this.eat(29) ? this.jsxParseAttributeValue() : null;
  6847. return this.finishNode(node, "JSXAttribute");
  6848. }
  6849. jsxParseOpeningElementAt(startLoc) {
  6850. const node = this.startNodeAt(startLoc);
  6851. if (this.eat(141)) {
  6852. return this.finishNode(node, "JSXOpeningFragment");
  6853. }
  6854. node.name = this.jsxParseElementName();
  6855. return this.jsxParseOpeningElementAfterName(node);
  6856. }
  6857. jsxParseOpeningElementAfterName(node) {
  6858. const attributes = [];
  6859. while (!this.match(56) && !this.match(141)) {
  6860. attributes.push(this.jsxParseAttribute());
  6861. }
  6862. node.attributes = attributes;
  6863. node.selfClosing = this.eat(56);
  6864. this.expect(141);
  6865. return this.finishNode(node, "JSXOpeningElement");
  6866. }
  6867. jsxParseClosingElementAt(startLoc) {
  6868. const node = this.startNodeAt(startLoc);
  6869. if (this.eat(141)) {
  6870. return this.finishNode(node, "JSXClosingFragment");
  6871. }
  6872. node.name = this.jsxParseElementName();
  6873. this.expect(141);
  6874. return this.finishNode(node, "JSXClosingElement");
  6875. }
  6876. jsxParseElementAt(startLoc) {
  6877. const node = this.startNodeAt(startLoc);
  6878. const children = [];
  6879. const openingElement = this.jsxParseOpeningElementAt(startLoc);
  6880. let closingElement = null;
  6881. if (!openingElement.selfClosing) {
  6882. contents: for (;;) {
  6883. switch (this.state.type) {
  6884. case 140:
  6885. startLoc = this.state.startLoc;
  6886. this.next();
  6887. if (this.eat(56)) {
  6888. closingElement = this.jsxParseClosingElementAt(startLoc);
  6889. break contents;
  6890. }
  6891. children.push(this.jsxParseElementAt(startLoc));
  6892. break;
  6893. case 139:
  6894. children.push(this.parseExprAtom());
  6895. break;
  6896. case 5:
  6897. {
  6898. const node = this.startNode();
  6899. this.setContext(types.brace);
  6900. this.next();
  6901. if (this.match(21)) {
  6902. children.push(this.jsxParseSpreadChild(node));
  6903. } else {
  6904. children.push(this.jsxParseExpressionContainer(node, types.j_expr));
  6905. }
  6906. break;
  6907. }
  6908. default:
  6909. throw this.unexpected();
  6910. }
  6911. }
  6912. if (isFragment(openingElement) && !isFragment(closingElement) && closingElement !== null) {
  6913. this.raise(JsxErrors.MissingClosingTagFragment, {
  6914. at: closingElement
  6915. });
  6916. } else if (!isFragment(openingElement) && isFragment(closingElement)) {
  6917. this.raise(JsxErrors.MissingClosingTagElement, {
  6918. at: closingElement,
  6919. openingTagName: getQualifiedJSXName(openingElement.name)
  6920. });
  6921. } else if (!isFragment(openingElement) && !isFragment(closingElement)) {
  6922. if (getQualifiedJSXName(closingElement.name) !== getQualifiedJSXName(openingElement.name)) {
  6923. this.raise(JsxErrors.MissingClosingTagElement, {
  6924. at: closingElement,
  6925. openingTagName: getQualifiedJSXName(openingElement.name)
  6926. });
  6927. }
  6928. }
  6929. }
  6930. if (isFragment(openingElement)) {
  6931. node.openingFragment = openingElement;
  6932. node.closingFragment = closingElement;
  6933. } else {
  6934. node.openingElement = openingElement;
  6935. node.closingElement = closingElement;
  6936. }
  6937. node.children = children;
  6938. if (this.match(47)) {
  6939. throw this.raise(JsxErrors.UnwrappedAdjacentJSXElements, {
  6940. at: this.state.startLoc
  6941. });
  6942. }
  6943. return isFragment(openingElement) ? this.finishNode(node, "JSXFragment") : this.finishNode(node, "JSXElement");
  6944. }
  6945. jsxParseElement() {
  6946. const startLoc = this.state.startLoc;
  6947. this.next();
  6948. return this.jsxParseElementAt(startLoc);
  6949. }
  6950. setContext(newContext) {
  6951. const {
  6952. context
  6953. } = this.state;
  6954. context[context.length - 1] = newContext;
  6955. }
  6956. parseExprAtom(refExpressionErrors) {
  6957. if (this.match(139)) {
  6958. return this.parseLiteral(this.state.value, "JSXText");
  6959. } else if (this.match(140)) {
  6960. return this.jsxParseElement();
  6961. } else if (this.match(47) && this.input.charCodeAt(this.state.pos) !== 33) {
  6962. this.replaceToken(140);
  6963. return this.jsxParseElement();
  6964. } else {
  6965. return super.parseExprAtom(refExpressionErrors);
  6966. }
  6967. }
  6968. skipSpace() {
  6969. const curContext = this.curContext();
  6970. if (!curContext.preserveSpace) super.skipSpace();
  6971. }
  6972. getTokenFromCode(code) {
  6973. const context = this.curContext();
  6974. if (context === types.j_expr) {
  6975. return this.jsxReadToken();
  6976. }
  6977. if (context === types.j_oTag || context === types.j_cTag) {
  6978. if (isIdentifierStart(code)) {
  6979. return this.jsxReadWord();
  6980. }
  6981. if (code === 62) {
  6982. ++this.state.pos;
  6983. return this.finishToken(141);
  6984. }
  6985. if ((code === 34 || code === 39) && context === types.j_oTag) {
  6986. return this.jsxReadString(code);
  6987. }
  6988. }
  6989. if (code === 60 && this.state.canStartJSXElement && this.input.charCodeAt(this.state.pos + 1) !== 33) {
  6990. ++this.state.pos;
  6991. return this.finishToken(140);
  6992. }
  6993. return super.getTokenFromCode(code);
  6994. }
  6995. updateContext(prevType) {
  6996. const {
  6997. context,
  6998. type
  6999. } = this.state;
  7000. if (type === 56 && prevType === 140) {
  7001. context.splice(-2, 2, types.j_cTag);
  7002. this.state.canStartJSXElement = false;
  7003. } else if (type === 140) {
  7004. context.push(types.j_oTag);
  7005. } else if (type === 141) {
  7006. const out = context[context.length - 1];
  7007. if (out === types.j_oTag && prevType === 56 || out === types.j_cTag) {
  7008. context.pop();
  7009. this.state.canStartJSXElement = context[context.length - 1] === types.j_expr;
  7010. } else {
  7011. this.setContext(types.j_expr);
  7012. this.state.canStartJSXElement = true;
  7013. }
  7014. } else {
  7015. this.state.canStartJSXElement = tokenComesBeforeExpression(type);
  7016. }
  7017. }
  7018. });
  7019. class TypeScriptScope extends Scope {
  7020. constructor(...args) {
  7021. super(...args);
  7022. this.types = new Set();
  7023. this.enums = new Set();
  7024. this.constEnums = new Set();
  7025. this.classes = new Set();
  7026. this.exportOnlyBindings = new Set();
  7027. }
  7028. }
  7029. class TypeScriptScopeHandler extends ScopeHandler {
  7030. constructor(...args) {
  7031. super(...args);
  7032. this.importsStack = [];
  7033. }
  7034. createScope(flags) {
  7035. this.importsStack.push(new Set());
  7036. return new TypeScriptScope(flags);
  7037. }
  7038. enter(flags) {
  7039. if (flags == SCOPE_TS_MODULE) {
  7040. this.importsStack.push(new Set());
  7041. }
  7042. super.enter(flags);
  7043. }
  7044. exit() {
  7045. const flags = super.exit();
  7046. if (flags == SCOPE_TS_MODULE) {
  7047. this.importsStack.pop();
  7048. }
  7049. return flags;
  7050. }
  7051. hasImport(name, allowShadow) {
  7052. const len = this.importsStack.length;
  7053. if (this.importsStack[len - 1].has(name)) {
  7054. return true;
  7055. }
  7056. if (!allowShadow && len > 1) {
  7057. for (let i = 0; i < len - 1; i++) {
  7058. if (this.importsStack[i].has(name)) return true;
  7059. }
  7060. }
  7061. return false;
  7062. }
  7063. declareName(name, bindingType, loc) {
  7064. if (bindingType & BIND_FLAGS_TS_IMPORT) {
  7065. if (this.hasImport(name, true)) {
  7066. this.parser.raise(Errors.VarRedeclaration, {
  7067. at: loc,
  7068. identifierName: name
  7069. });
  7070. }
  7071. this.importsStack[this.importsStack.length - 1].add(name);
  7072. return;
  7073. }
  7074. const scope = this.currentScope();
  7075. if (bindingType & BIND_FLAGS_TS_EXPORT_ONLY) {
  7076. this.maybeExportDefined(scope, name);
  7077. scope.exportOnlyBindings.add(name);
  7078. return;
  7079. }
  7080. super.declareName(name, bindingType, loc);
  7081. if (bindingType & BIND_KIND_TYPE) {
  7082. if (!(bindingType & BIND_KIND_VALUE)) {
  7083. this.checkRedeclarationInScope(scope, name, bindingType, loc);
  7084. this.maybeExportDefined(scope, name);
  7085. }
  7086. scope.types.add(name);
  7087. }
  7088. if (bindingType & BIND_FLAGS_TS_ENUM) scope.enums.add(name);
  7089. if (bindingType & BIND_FLAGS_TS_CONST_ENUM) scope.constEnums.add(name);
  7090. if (bindingType & BIND_FLAGS_CLASS) scope.classes.add(name);
  7091. }
  7092. isRedeclaredInScope(scope, name, bindingType) {
  7093. if (scope.enums.has(name)) {
  7094. if (bindingType & BIND_FLAGS_TS_ENUM) {
  7095. const isConst = !!(bindingType & BIND_FLAGS_TS_CONST_ENUM);
  7096. const wasConst = scope.constEnums.has(name);
  7097. return isConst !== wasConst;
  7098. }
  7099. return true;
  7100. }
  7101. if (bindingType & BIND_FLAGS_CLASS && scope.classes.has(name)) {
  7102. if (scope.lexical.has(name)) {
  7103. return !!(bindingType & BIND_KIND_VALUE);
  7104. } else {
  7105. return false;
  7106. }
  7107. }
  7108. if (bindingType & BIND_KIND_TYPE && scope.types.has(name)) {
  7109. return true;
  7110. }
  7111. return super.isRedeclaredInScope(scope, name, bindingType);
  7112. }
  7113. checkLocalExport(id) {
  7114. const {
  7115. name
  7116. } = id;
  7117. if (this.hasImport(name)) return;
  7118. const len = this.scopeStack.length;
  7119. for (let i = len - 1; i >= 0; i--) {
  7120. const scope = this.scopeStack[i];
  7121. if (scope.types.has(name) || scope.exportOnlyBindings.has(name)) return;
  7122. }
  7123. super.checkLocalExport(id);
  7124. }
  7125. }
  7126. const getOwn$1 = (object, key) => Object.hasOwnProperty.call(object, key) && object[key];
  7127. const unwrapParenthesizedExpression = node => {
  7128. return node.type === "ParenthesizedExpression" ? unwrapParenthesizedExpression(node.expression) : node;
  7129. };
  7130. var ParseBindingListFlags = {
  7131. ALLOW_EMPTY: 1,
  7132. IS_FUNCTION_PARAMS: 2,
  7133. IS_CONSTRUCTOR_PARAMS: 4
  7134. };
  7135. class LValParser extends NodeUtils {
  7136. toAssignable(node, isLHS = false) {
  7137. var _node$extra, _node$extra3;
  7138. let parenthesized = undefined;
  7139. if (node.type === "ParenthesizedExpression" || (_node$extra = node.extra) != null && _node$extra.parenthesized) {
  7140. parenthesized = unwrapParenthesizedExpression(node);
  7141. if (isLHS) {
  7142. if (parenthesized.type === "Identifier") {
  7143. this.expressionScope.recordArrowParameterBindingError(Errors.InvalidParenthesizedAssignment, {
  7144. at: node
  7145. });
  7146. } else if (parenthesized.type !== "MemberExpression") {
  7147. this.raise(Errors.InvalidParenthesizedAssignment, {
  7148. at: node
  7149. });
  7150. }
  7151. } else {
  7152. this.raise(Errors.InvalidParenthesizedAssignment, {
  7153. at: node
  7154. });
  7155. }
  7156. }
  7157. switch (node.type) {
  7158. case "Identifier":
  7159. case "ObjectPattern":
  7160. case "ArrayPattern":
  7161. case "AssignmentPattern":
  7162. case "RestElement":
  7163. break;
  7164. case "ObjectExpression":
  7165. node.type = "ObjectPattern";
  7166. for (let i = 0, length = node.properties.length, last = length - 1; i < length; i++) {
  7167. var _node$extra2;
  7168. const prop = node.properties[i];
  7169. const isLast = i === last;
  7170. this.toAssignableObjectExpressionProp(prop, isLast, isLHS);
  7171. if (isLast && prop.type === "RestElement" && (_node$extra2 = node.extra) != null && _node$extra2.trailingCommaLoc) {
  7172. this.raise(Errors.RestTrailingComma, {
  7173. at: node.extra.trailingCommaLoc
  7174. });
  7175. }
  7176. }
  7177. break;
  7178. case "ObjectProperty":
  7179. {
  7180. const {
  7181. key,
  7182. value
  7183. } = node;
  7184. if (this.isPrivateName(key)) {
  7185. this.classScope.usePrivateName(this.getPrivateNameSV(key), key.loc.start);
  7186. }
  7187. this.toAssignable(value, isLHS);
  7188. break;
  7189. }
  7190. case "SpreadElement":
  7191. {
  7192. throw new Error("Internal @babel/parser error (this is a bug, please report it)." + " SpreadElement should be converted by .toAssignable's caller.");
  7193. }
  7194. case "ArrayExpression":
  7195. node.type = "ArrayPattern";
  7196. this.toAssignableList(node.elements, (_node$extra3 = node.extra) == null ? void 0 : _node$extra3.trailingCommaLoc, isLHS);
  7197. break;
  7198. case "AssignmentExpression":
  7199. if (node.operator !== "=") {
  7200. this.raise(Errors.MissingEqInAssignment, {
  7201. at: node.left.loc.end
  7202. });
  7203. }
  7204. node.type = "AssignmentPattern";
  7205. delete node.operator;
  7206. this.toAssignable(node.left, isLHS);
  7207. break;
  7208. case "ParenthesizedExpression":
  7209. this.toAssignable(parenthesized, isLHS);
  7210. break;
  7211. }
  7212. }
  7213. toAssignableObjectExpressionProp(prop, isLast, isLHS) {
  7214. if (prop.type === "ObjectMethod") {
  7215. this.raise(prop.kind === "get" || prop.kind === "set" ? Errors.PatternHasAccessor : Errors.PatternHasMethod, {
  7216. at: prop.key
  7217. });
  7218. } else if (prop.type === "SpreadElement") {
  7219. prop.type = "RestElement";
  7220. const arg = prop.argument;
  7221. this.checkToRestConversion(arg, false);
  7222. this.toAssignable(arg, isLHS);
  7223. if (!isLast) {
  7224. this.raise(Errors.RestTrailingComma, {
  7225. at: prop
  7226. });
  7227. }
  7228. } else {
  7229. this.toAssignable(prop, isLHS);
  7230. }
  7231. }
  7232. toAssignableList(exprList, trailingCommaLoc, isLHS) {
  7233. const end = exprList.length - 1;
  7234. for (let i = 0; i <= end; i++) {
  7235. const elt = exprList[i];
  7236. if (!elt) continue;
  7237. if (elt.type === "SpreadElement") {
  7238. elt.type = "RestElement";
  7239. const arg = elt.argument;
  7240. this.checkToRestConversion(arg, true);
  7241. this.toAssignable(arg, isLHS);
  7242. } else {
  7243. this.toAssignable(elt, isLHS);
  7244. }
  7245. if (elt.type === "RestElement") {
  7246. if (i < end) {
  7247. this.raise(Errors.RestTrailingComma, {
  7248. at: elt
  7249. });
  7250. } else if (trailingCommaLoc) {
  7251. this.raise(Errors.RestTrailingComma, {
  7252. at: trailingCommaLoc
  7253. });
  7254. }
  7255. }
  7256. }
  7257. }
  7258. isAssignable(node, isBinding) {
  7259. switch (node.type) {
  7260. case "Identifier":
  7261. case "ObjectPattern":
  7262. case "ArrayPattern":
  7263. case "AssignmentPattern":
  7264. case "RestElement":
  7265. return true;
  7266. case "ObjectExpression":
  7267. {
  7268. const last = node.properties.length - 1;
  7269. return node.properties.every((prop, i) => {
  7270. return prop.type !== "ObjectMethod" && (i === last || prop.type !== "SpreadElement") && this.isAssignable(prop);
  7271. });
  7272. }
  7273. case "ObjectProperty":
  7274. return this.isAssignable(node.value);
  7275. case "SpreadElement":
  7276. return this.isAssignable(node.argument);
  7277. case "ArrayExpression":
  7278. return node.elements.every(element => element === null || this.isAssignable(element));
  7279. case "AssignmentExpression":
  7280. return node.operator === "=";
  7281. case "ParenthesizedExpression":
  7282. return this.isAssignable(node.expression);
  7283. case "MemberExpression":
  7284. case "OptionalMemberExpression":
  7285. return !isBinding;
  7286. default:
  7287. return false;
  7288. }
  7289. }
  7290. toReferencedList(exprList, isParenthesizedExpr) {
  7291. return exprList;
  7292. }
  7293. toReferencedListDeep(exprList, isParenthesizedExpr) {
  7294. this.toReferencedList(exprList, isParenthesizedExpr);
  7295. for (const expr of exprList) {
  7296. if ((expr == null ? void 0 : expr.type) === "ArrayExpression") {
  7297. this.toReferencedListDeep(expr.elements);
  7298. }
  7299. }
  7300. }
  7301. parseSpread(refExpressionErrors) {
  7302. const node = this.startNode();
  7303. this.next();
  7304. node.argument = this.parseMaybeAssignAllowIn(refExpressionErrors, undefined);
  7305. return this.finishNode(node, "SpreadElement");
  7306. }
  7307. parseRestBinding() {
  7308. const node = this.startNode();
  7309. this.next();
  7310. node.argument = this.parseBindingAtom();
  7311. return this.finishNode(node, "RestElement");
  7312. }
  7313. parseBindingAtom() {
  7314. switch (this.state.type) {
  7315. case 0:
  7316. {
  7317. const node = this.startNode();
  7318. this.next();
  7319. node.elements = this.parseBindingList(3, 93, ParseBindingListFlags.ALLOW_EMPTY);
  7320. return this.finishNode(node, "ArrayPattern");
  7321. }
  7322. case 5:
  7323. return this.parseObjectLike(8, true);
  7324. }
  7325. return this.parseIdentifier();
  7326. }
  7327. parseBindingList(close, closeCharCode, flags) {
  7328. const allowEmpty = flags & ParseBindingListFlags.ALLOW_EMPTY;
  7329. const elts = [];
  7330. let first = true;
  7331. while (!this.eat(close)) {
  7332. if (first) {
  7333. first = false;
  7334. } else {
  7335. this.expect(12);
  7336. }
  7337. if (allowEmpty && this.match(12)) {
  7338. elts.push(null);
  7339. } else if (this.eat(close)) {
  7340. break;
  7341. } else if (this.match(21)) {
  7342. elts.push(this.parseAssignableListItemTypes(this.parseRestBinding(), flags));
  7343. if (!this.checkCommaAfterRest(closeCharCode)) {
  7344. this.expect(close);
  7345. break;
  7346. }
  7347. } else {
  7348. const decorators = [];
  7349. if (this.match(26) && this.hasPlugin("decorators")) {
  7350. this.raise(Errors.UnsupportedParameterDecorator, {
  7351. at: this.state.startLoc
  7352. });
  7353. }
  7354. while (this.match(26)) {
  7355. decorators.push(this.parseDecorator());
  7356. }
  7357. elts.push(this.parseAssignableListItem(flags, decorators));
  7358. }
  7359. }
  7360. return elts;
  7361. }
  7362. parseBindingRestProperty(prop) {
  7363. this.next();
  7364. prop.argument = this.parseIdentifier();
  7365. this.checkCommaAfterRest(125);
  7366. return this.finishNode(prop, "RestElement");
  7367. }
  7368. parseBindingProperty() {
  7369. const prop = this.startNode();
  7370. const {
  7371. type,
  7372. startLoc
  7373. } = this.state;
  7374. if (type === 21) {
  7375. return this.parseBindingRestProperty(prop);
  7376. } else if (type === 136) {
  7377. this.expectPlugin("destructuringPrivate", startLoc);
  7378. this.classScope.usePrivateName(this.state.value, startLoc);
  7379. prop.key = this.parsePrivateName();
  7380. } else {
  7381. this.parsePropertyName(prop);
  7382. }
  7383. prop.method = false;
  7384. return this.parseObjPropValue(prop, startLoc, false, false, true, false);
  7385. }
  7386. parseAssignableListItem(flags, decorators) {
  7387. const left = this.parseMaybeDefault();
  7388. this.parseAssignableListItemTypes(left, flags);
  7389. const elt = this.parseMaybeDefault(left.loc.start, left);
  7390. if (decorators.length) {
  7391. left.decorators = decorators;
  7392. }
  7393. return elt;
  7394. }
  7395. parseAssignableListItemTypes(param, flags) {
  7396. return param;
  7397. }
  7398. parseMaybeDefault(startLoc, left) {
  7399. var _startLoc, _left;
  7400. (_startLoc = startLoc) != null ? _startLoc : startLoc = this.state.startLoc;
  7401. left = (_left = left) != null ? _left : this.parseBindingAtom();
  7402. if (!this.eat(29)) return left;
  7403. const node = this.startNodeAt(startLoc);
  7404. node.left = left;
  7405. node.right = this.parseMaybeAssignAllowIn();
  7406. return this.finishNode(node, "AssignmentPattern");
  7407. }
  7408. isValidLVal(type, isUnparenthesizedInAssign, binding) {
  7409. return getOwn$1({
  7410. AssignmentPattern: "left",
  7411. RestElement: "argument",
  7412. ObjectProperty: "value",
  7413. ParenthesizedExpression: "expression",
  7414. ArrayPattern: "elements",
  7415. ObjectPattern: "properties"
  7416. }, type);
  7417. }
  7418. checkLVal(expression, {
  7419. in: ancestor,
  7420. binding = BIND_NONE,
  7421. checkClashes = false,
  7422. strictModeChanged = false,
  7423. hasParenthesizedAncestor = false
  7424. }) {
  7425. var _expression$extra;
  7426. const type = expression.type;
  7427. if (this.isObjectMethod(expression)) return;
  7428. if (type === "MemberExpression") {
  7429. if (binding !== BIND_NONE) {
  7430. this.raise(Errors.InvalidPropertyBindingPattern, {
  7431. at: expression
  7432. });
  7433. }
  7434. return;
  7435. }
  7436. if (type === "Identifier") {
  7437. this.checkIdentifier(expression, binding, strictModeChanged);
  7438. const {
  7439. name
  7440. } = expression;
  7441. if (checkClashes) {
  7442. if (checkClashes.has(name)) {
  7443. this.raise(Errors.ParamDupe, {
  7444. at: expression
  7445. });
  7446. } else {
  7447. checkClashes.add(name);
  7448. }
  7449. }
  7450. return;
  7451. }
  7452. const validity = this.isValidLVal(type, !(hasParenthesizedAncestor || (_expression$extra = expression.extra) != null && _expression$extra.parenthesized) && ancestor.type === "AssignmentExpression", binding);
  7453. if (validity === true) return;
  7454. if (validity === false) {
  7455. const ParseErrorClass = binding === BIND_NONE ? Errors.InvalidLhs : Errors.InvalidLhsBinding;
  7456. this.raise(ParseErrorClass, {
  7457. at: expression,
  7458. ancestor
  7459. });
  7460. return;
  7461. }
  7462. const [key, isParenthesizedExpression] = Array.isArray(validity) ? validity : [validity, type === "ParenthesizedExpression"];
  7463. const nextAncestor = type === "ArrayPattern" || type === "ObjectPattern" || type === "ParenthesizedExpression" ? {
  7464. type
  7465. } : ancestor;
  7466. for (const child of [].concat(expression[key])) {
  7467. if (child) {
  7468. this.checkLVal(child, {
  7469. in: nextAncestor,
  7470. binding,
  7471. checkClashes,
  7472. strictModeChanged,
  7473. hasParenthesizedAncestor: isParenthesizedExpression
  7474. });
  7475. }
  7476. }
  7477. }
  7478. checkIdentifier(at, bindingType, strictModeChanged = false) {
  7479. if (this.state.strict && (strictModeChanged ? isStrictBindReservedWord(at.name, this.inModule) : isStrictBindOnlyReservedWord(at.name))) {
  7480. if (bindingType === BIND_NONE) {
  7481. this.raise(Errors.StrictEvalArguments, {
  7482. at,
  7483. referenceName: at.name
  7484. });
  7485. } else {
  7486. this.raise(Errors.StrictEvalArgumentsBinding, {
  7487. at,
  7488. bindingName: at.name
  7489. });
  7490. }
  7491. }
  7492. if (bindingType & BIND_FLAGS_NO_LET_IN_LEXICAL && at.name === "let") {
  7493. this.raise(Errors.LetInLexicalBinding, {
  7494. at
  7495. });
  7496. }
  7497. if (!(bindingType & BIND_NONE)) {
  7498. this.declareNameFromIdentifier(at, bindingType);
  7499. }
  7500. }
  7501. declareNameFromIdentifier(identifier, binding) {
  7502. this.scope.declareName(identifier.name, binding, identifier.loc.start);
  7503. }
  7504. checkToRestConversion(node, allowPattern) {
  7505. switch (node.type) {
  7506. case "ParenthesizedExpression":
  7507. this.checkToRestConversion(node.expression, allowPattern);
  7508. break;
  7509. case "Identifier":
  7510. case "MemberExpression":
  7511. break;
  7512. case "ArrayExpression":
  7513. case "ObjectExpression":
  7514. if (allowPattern) break;
  7515. default:
  7516. this.raise(Errors.InvalidRestAssignmentPattern, {
  7517. at: node
  7518. });
  7519. }
  7520. }
  7521. checkCommaAfterRest(close) {
  7522. if (!this.match(12)) {
  7523. return false;
  7524. }
  7525. this.raise(this.lookaheadCharCode() === close ? Errors.RestTrailingComma : Errors.ElementAfterRest, {
  7526. at: this.state.startLoc
  7527. });
  7528. return true;
  7529. }
  7530. }
  7531. const getOwn = (object, key) => Object.hasOwnProperty.call(object, key) && object[key];
  7532. function nonNull(x) {
  7533. if (x == null) {
  7534. throw new Error(`Unexpected ${x} value.`);
  7535. }
  7536. return x;
  7537. }
  7538. function assert(x) {
  7539. if (!x) {
  7540. throw new Error("Assert fail");
  7541. }
  7542. }
  7543. const TSErrors = ParseErrorEnum`typescript`({
  7544. AbstractMethodHasImplementation: ({
  7545. methodName
  7546. }) => `Method '${methodName}' cannot have an implementation because it is marked abstract.`,
  7547. AbstractPropertyHasInitializer: ({
  7548. propertyName
  7549. }) => `Property '${propertyName}' cannot have an initializer because it is marked abstract.`,
  7550. AccesorCannotDeclareThisParameter: "'get' and 'set' accessors cannot declare 'this' parameters.",
  7551. AccesorCannotHaveTypeParameters: "An accessor cannot have type parameters.",
  7552. AccessorCannotBeOptional: "An 'accessor' property cannot be declared optional.",
  7553. ClassMethodHasDeclare: "Class methods cannot have the 'declare' modifier.",
  7554. ClassMethodHasReadonly: "Class methods cannot have the 'readonly' modifier.",
  7555. ConstInitiailizerMustBeStringOrNumericLiteralOrLiteralEnumReference: "A 'const' initializer in an ambient context must be a string or numeric literal or literal enum reference.",
  7556. ConstructorHasTypeParameters: "Type parameters cannot appear on a constructor declaration.",
  7557. DeclareAccessor: ({
  7558. kind
  7559. }) => `'declare' is not allowed in ${kind}ters.`,
  7560. DeclareClassFieldHasInitializer: "Initializers are not allowed in ambient contexts.",
  7561. DeclareFunctionHasImplementation: "An implementation cannot be declared in ambient contexts.",
  7562. DuplicateAccessibilityModifier: ({
  7563. modifier
  7564. }) => `Accessibility modifier already seen.`,
  7565. DuplicateModifier: ({
  7566. modifier
  7567. }) => `Duplicate modifier: '${modifier}'.`,
  7568. EmptyHeritageClauseType: ({
  7569. token
  7570. }) => `'${token}' list cannot be empty.`,
  7571. EmptyTypeArguments: "Type argument list cannot be empty.",
  7572. EmptyTypeParameters: "Type parameter list cannot be empty.",
  7573. ExpectedAmbientAfterExportDeclare: "'export declare' must be followed by an ambient declaration.",
  7574. ImportAliasHasImportType: "An import alias can not use 'import type'.",
  7575. ImportReflectionHasImportType: "An `import module` declaration can not use `type` modifier",
  7576. IncompatibleModifiers: ({
  7577. modifiers
  7578. }) => `'${modifiers[0]}' modifier cannot be used with '${modifiers[1]}' modifier.`,
  7579. IndexSignatureHasAbstract: "Index signatures cannot have the 'abstract' modifier.",
  7580. IndexSignatureHasAccessibility: ({
  7581. modifier
  7582. }) => `Index signatures cannot have an accessibility modifier ('${modifier}').`,
  7583. IndexSignatureHasDeclare: "Index signatures cannot have the 'declare' modifier.",
  7584. IndexSignatureHasOverride: "'override' modifier cannot appear on an index signature.",
  7585. IndexSignatureHasStatic: "Index signatures cannot have the 'static' modifier.",
  7586. InitializerNotAllowedInAmbientContext: "Initializers are not allowed in ambient contexts.",
  7587. InvalidModifierOnTypeMember: ({
  7588. modifier
  7589. }) => `'${modifier}' modifier cannot appear on a type member.`,
  7590. InvalidModifierOnTypeParameter: ({
  7591. modifier
  7592. }) => `'${modifier}' modifier cannot appear on a type parameter.`,
  7593. InvalidModifierOnTypeParameterPositions: ({
  7594. modifier
  7595. }) => `'${modifier}' modifier can only appear on a type parameter of a class, interface or type alias.`,
  7596. InvalidModifiersOrder: ({
  7597. orderedModifiers
  7598. }) => `'${orderedModifiers[0]}' modifier must precede '${orderedModifiers[1]}' modifier.`,
  7599. InvalidPropertyAccessAfterInstantiationExpression: "Invalid property access after an instantiation expression. " + "You can either wrap the instantiation expression in parentheses, or delete the type arguments.",
  7600. InvalidTupleMemberLabel: "Tuple members must be labeled with a simple identifier.",
  7601. MissingInterfaceName: "'interface' declarations must be followed by an identifier.",
  7602. MixedLabeledAndUnlabeledElements: "Tuple members must all have names or all not have names.",
  7603. NonAbstractClassHasAbstractMethod: "Abstract methods can only appear within an abstract class.",
  7604. NonClassMethodPropertyHasAbstractModifer: "'abstract' modifier can only appear on a class, method, or property declaration.",
  7605. OptionalTypeBeforeRequired: "A required element cannot follow an optional element.",
  7606. OverrideNotInSubClass: "This member cannot have an 'override' modifier because its containing class does not extend another class.",
  7607. PatternIsOptional: "A binding pattern parameter cannot be optional in an implementation signature.",
  7608. PrivateElementHasAbstract: "Private elements cannot have the 'abstract' modifier.",
  7609. PrivateElementHasAccessibility: ({
  7610. modifier
  7611. }) => `Private elements cannot have an accessibility modifier ('${modifier}').`,
  7612. ReadonlyForMethodSignature: "'readonly' modifier can only appear on a property declaration or index signature.",
  7613. ReservedArrowTypeParam: "This syntax is reserved in files with the .mts or .cts extension. Add a trailing comma, as in `<T,>() => ...`.",
  7614. ReservedTypeAssertion: "This syntax is reserved in files with the .mts or .cts extension. Use an `as` expression instead.",
  7615. SetAccesorCannotHaveOptionalParameter: "A 'set' accessor cannot have an optional parameter.",
  7616. SetAccesorCannotHaveRestParameter: "A 'set' accessor cannot have rest parameter.",
  7617. SetAccesorCannotHaveReturnType: "A 'set' accessor cannot have a return type annotation.",
  7618. SingleTypeParameterWithoutTrailingComma: ({
  7619. typeParameterName
  7620. }) => `Single type parameter ${typeParameterName} should have a trailing comma. Example usage: <${typeParameterName},>.`,
  7621. StaticBlockCannotHaveModifier: "Static class blocks cannot have any modifier.",
  7622. TypeAnnotationAfterAssign: "Type annotations must come before default assignments, e.g. instead of `age = 25: number` use `age: number = 25`.",
  7623. TypeImportCannotSpecifyDefaultAndNamed: "A type-only import can specify a default import or named bindings, but not both.",
  7624. TypeModifierIsUsedInTypeExports: "The 'type' modifier cannot be used on a named export when 'export type' is used on its export statement.",
  7625. TypeModifierIsUsedInTypeImports: "The 'type' modifier cannot be used on a named import when 'import type' is used on its import statement.",
  7626. UnexpectedParameterModifier: "A parameter property is only allowed in a constructor implementation.",
  7627. UnexpectedReadonly: "'readonly' type modifier is only permitted on array and tuple literal types.",
  7628. UnexpectedTypeAnnotation: "Did not expect a type annotation here.",
  7629. UnexpectedTypeCastInParameter: "Unexpected type cast in parameter position.",
  7630. UnsupportedImportTypeArgument: "Argument in a type import must be a string literal.",
  7631. UnsupportedParameterPropertyKind: "A parameter property may not be declared using a binding pattern.",
  7632. UnsupportedSignatureParameterKind: ({
  7633. type
  7634. }) => `Name in a signature must be an Identifier, ObjectPattern or ArrayPattern, instead got ${type}.`
  7635. });
  7636. function keywordTypeFromName(value) {
  7637. switch (value) {
  7638. case "any":
  7639. return "TSAnyKeyword";
  7640. case "boolean":
  7641. return "TSBooleanKeyword";
  7642. case "bigint":
  7643. return "TSBigIntKeyword";
  7644. case "never":
  7645. return "TSNeverKeyword";
  7646. case "number":
  7647. return "TSNumberKeyword";
  7648. case "object":
  7649. return "TSObjectKeyword";
  7650. case "string":
  7651. return "TSStringKeyword";
  7652. case "symbol":
  7653. return "TSSymbolKeyword";
  7654. case "undefined":
  7655. return "TSUndefinedKeyword";
  7656. case "unknown":
  7657. return "TSUnknownKeyword";
  7658. default:
  7659. return undefined;
  7660. }
  7661. }
  7662. function tsIsAccessModifier(modifier) {
  7663. return modifier === "private" || modifier === "public" || modifier === "protected";
  7664. }
  7665. function tsIsVarianceAnnotations(modifier) {
  7666. return modifier === "in" || modifier === "out";
  7667. }
  7668. var typescript = (superClass => class TypeScriptParserMixin extends superClass {
  7669. constructor(...args) {
  7670. super(...args);
  7671. this.tsParseInOutModifiers = this.tsParseModifiers.bind(this, {
  7672. allowedModifiers: ["in", "out"],
  7673. disallowedModifiers: ["const", "public", "private", "protected", "readonly", "declare", "abstract", "override"],
  7674. errorTemplate: TSErrors.InvalidModifierOnTypeParameter
  7675. });
  7676. this.tsParseConstModifier = this.tsParseModifiers.bind(this, {
  7677. allowedModifiers: ["const"],
  7678. disallowedModifiers: ["in", "out"],
  7679. errorTemplate: TSErrors.InvalidModifierOnTypeParameterPositions
  7680. });
  7681. this.tsParseInOutConstModifiers = this.tsParseModifiers.bind(this, {
  7682. allowedModifiers: ["in", "out", "const"],
  7683. disallowedModifiers: ["public", "private", "protected", "readonly", "declare", "abstract", "override"],
  7684. errorTemplate: TSErrors.InvalidModifierOnTypeParameter
  7685. });
  7686. }
  7687. getScopeHandler() {
  7688. return TypeScriptScopeHandler;
  7689. }
  7690. tsIsIdentifier() {
  7691. return tokenIsIdentifier(this.state.type);
  7692. }
  7693. tsTokenCanFollowModifier() {
  7694. return (this.match(0) || this.match(5) || this.match(55) || this.match(21) || this.match(136) || this.isLiteralPropertyName()) && !this.hasPrecedingLineBreak();
  7695. }
  7696. tsNextTokenCanFollowModifier() {
  7697. this.next();
  7698. return this.tsTokenCanFollowModifier();
  7699. }
  7700. tsParseModifier(allowedModifiers, stopOnStartOfClassStaticBlock) {
  7701. if (!tokenIsIdentifier(this.state.type) && this.state.type !== 58 && this.state.type !== 75) {
  7702. return undefined;
  7703. }
  7704. const modifier = this.state.value;
  7705. if (allowedModifiers.indexOf(modifier) !== -1) {
  7706. if (stopOnStartOfClassStaticBlock && this.tsIsStartOfStaticBlocks()) {
  7707. return undefined;
  7708. }
  7709. if (this.tsTryParse(this.tsNextTokenCanFollowModifier.bind(this))) {
  7710. return modifier;
  7711. }
  7712. }
  7713. return undefined;
  7714. }
  7715. tsParseModifiers({
  7716. allowedModifiers,
  7717. disallowedModifiers,
  7718. stopOnStartOfClassStaticBlock,
  7719. errorTemplate = TSErrors.InvalidModifierOnTypeMember
  7720. }, modified) {
  7721. const enforceOrder = (loc, modifier, before, after) => {
  7722. if (modifier === before && modified[after]) {
  7723. this.raise(TSErrors.InvalidModifiersOrder, {
  7724. at: loc,
  7725. orderedModifiers: [before, after]
  7726. });
  7727. }
  7728. };
  7729. const incompatible = (loc, modifier, mod1, mod2) => {
  7730. if (modified[mod1] && modifier === mod2 || modified[mod2] && modifier === mod1) {
  7731. this.raise(TSErrors.IncompatibleModifiers, {
  7732. at: loc,
  7733. modifiers: [mod1, mod2]
  7734. });
  7735. }
  7736. };
  7737. for (;;) {
  7738. const {
  7739. startLoc
  7740. } = this.state;
  7741. const modifier = this.tsParseModifier(allowedModifiers.concat(disallowedModifiers != null ? disallowedModifiers : []), stopOnStartOfClassStaticBlock);
  7742. if (!modifier) break;
  7743. if (tsIsAccessModifier(modifier)) {
  7744. if (modified.accessibility) {
  7745. this.raise(TSErrors.DuplicateAccessibilityModifier, {
  7746. at: startLoc,
  7747. modifier
  7748. });
  7749. } else {
  7750. enforceOrder(startLoc, modifier, modifier, "override");
  7751. enforceOrder(startLoc, modifier, modifier, "static");
  7752. enforceOrder(startLoc, modifier, modifier, "readonly");
  7753. modified.accessibility = modifier;
  7754. }
  7755. } else if (tsIsVarianceAnnotations(modifier)) {
  7756. if (modified[modifier]) {
  7757. this.raise(TSErrors.DuplicateModifier, {
  7758. at: startLoc,
  7759. modifier
  7760. });
  7761. }
  7762. modified[modifier] = true;
  7763. enforceOrder(startLoc, modifier, "in", "out");
  7764. } else {
  7765. if (Object.hasOwnProperty.call(modified, modifier)) {
  7766. this.raise(TSErrors.DuplicateModifier, {
  7767. at: startLoc,
  7768. modifier
  7769. });
  7770. } else {
  7771. enforceOrder(startLoc, modifier, "static", "readonly");
  7772. enforceOrder(startLoc, modifier, "static", "override");
  7773. enforceOrder(startLoc, modifier, "override", "readonly");
  7774. enforceOrder(startLoc, modifier, "abstract", "override");
  7775. incompatible(startLoc, modifier, "declare", "override");
  7776. incompatible(startLoc, modifier, "static", "abstract");
  7777. }
  7778. modified[modifier] = true;
  7779. }
  7780. if (disallowedModifiers != null && disallowedModifiers.includes(modifier)) {
  7781. this.raise(errorTemplate, {
  7782. at: startLoc,
  7783. modifier
  7784. });
  7785. }
  7786. }
  7787. }
  7788. tsIsListTerminator(kind) {
  7789. switch (kind) {
  7790. case "EnumMembers":
  7791. case "TypeMembers":
  7792. return this.match(8);
  7793. case "HeritageClauseElement":
  7794. return this.match(5);
  7795. case "TupleElementTypes":
  7796. return this.match(3);
  7797. case "TypeParametersOrArguments":
  7798. return this.match(48);
  7799. }
  7800. throw new Error("Unreachable");
  7801. }
  7802. tsParseList(kind, parseElement) {
  7803. const result = [];
  7804. while (!this.tsIsListTerminator(kind)) {
  7805. result.push(parseElement());
  7806. }
  7807. return result;
  7808. }
  7809. tsParseDelimitedList(kind, parseElement, refTrailingCommaPos) {
  7810. return nonNull(this.tsParseDelimitedListWorker(kind, parseElement, true, refTrailingCommaPos));
  7811. }
  7812. tsParseDelimitedListWorker(kind, parseElement, expectSuccess, refTrailingCommaPos) {
  7813. const result = [];
  7814. let trailingCommaPos = -1;
  7815. for (;;) {
  7816. if (this.tsIsListTerminator(kind)) {
  7817. break;
  7818. }
  7819. trailingCommaPos = -1;
  7820. const element = parseElement();
  7821. if (element == null) {
  7822. return undefined;
  7823. }
  7824. result.push(element);
  7825. if (this.eat(12)) {
  7826. trailingCommaPos = this.state.lastTokStart;
  7827. continue;
  7828. }
  7829. if (this.tsIsListTerminator(kind)) {
  7830. break;
  7831. }
  7832. if (expectSuccess) {
  7833. this.expect(12);
  7834. }
  7835. return undefined;
  7836. }
  7837. if (refTrailingCommaPos) {
  7838. refTrailingCommaPos.value = trailingCommaPos;
  7839. }
  7840. return result;
  7841. }
  7842. tsParseBracketedList(kind, parseElement, bracket, skipFirstToken, refTrailingCommaPos) {
  7843. if (!skipFirstToken) {
  7844. if (bracket) {
  7845. this.expect(0);
  7846. } else {
  7847. this.expect(47);
  7848. }
  7849. }
  7850. const result = this.tsParseDelimitedList(kind, parseElement, refTrailingCommaPos);
  7851. if (bracket) {
  7852. this.expect(3);
  7853. } else {
  7854. this.expect(48);
  7855. }
  7856. return result;
  7857. }
  7858. tsParseImportType() {
  7859. const node = this.startNode();
  7860. this.expect(83);
  7861. this.expect(10);
  7862. if (!this.match(131)) {
  7863. this.raise(TSErrors.UnsupportedImportTypeArgument, {
  7864. at: this.state.startLoc
  7865. });
  7866. }
  7867. node.argument = super.parseExprAtom();
  7868. this.expect(11);
  7869. if (this.eat(16)) {
  7870. node.qualifier = this.tsParseEntityName();
  7871. }
  7872. if (this.match(47)) {
  7873. node.typeParameters = this.tsParseTypeArguments();
  7874. }
  7875. return this.finishNode(node, "TSImportType");
  7876. }
  7877. tsParseEntityName(allowReservedWords = true) {
  7878. let entity = this.parseIdentifier(allowReservedWords);
  7879. while (this.eat(16)) {
  7880. const node = this.startNodeAtNode(entity);
  7881. node.left = entity;
  7882. node.right = this.parseIdentifier(allowReservedWords);
  7883. entity = this.finishNode(node, "TSQualifiedName");
  7884. }
  7885. return entity;
  7886. }
  7887. tsParseTypeReference() {
  7888. const node = this.startNode();
  7889. node.typeName = this.tsParseEntityName();
  7890. if (!this.hasPrecedingLineBreak() && this.match(47)) {
  7891. node.typeParameters = this.tsParseTypeArguments();
  7892. }
  7893. return this.finishNode(node, "TSTypeReference");
  7894. }
  7895. tsParseThisTypePredicate(lhs) {
  7896. this.next();
  7897. const node = this.startNodeAtNode(lhs);
  7898. node.parameterName = lhs;
  7899. node.typeAnnotation = this.tsParseTypeAnnotation(false);
  7900. node.asserts = false;
  7901. return this.finishNode(node, "TSTypePredicate");
  7902. }
  7903. tsParseThisTypeNode() {
  7904. const node = this.startNode();
  7905. this.next();
  7906. return this.finishNode(node, "TSThisType");
  7907. }
  7908. tsParseTypeQuery() {
  7909. const node = this.startNode();
  7910. this.expect(87);
  7911. if (this.match(83)) {
  7912. node.exprName = this.tsParseImportType();
  7913. } else {
  7914. node.exprName = this.tsParseEntityName();
  7915. }
  7916. if (!this.hasPrecedingLineBreak() && this.match(47)) {
  7917. node.typeParameters = this.tsParseTypeArguments();
  7918. }
  7919. return this.finishNode(node, "TSTypeQuery");
  7920. }
  7921. tsParseTypeParameter(parseModifiers) {
  7922. const node = this.startNode();
  7923. parseModifiers(node);
  7924. node.name = this.tsParseTypeParameterName();
  7925. node.constraint = this.tsEatThenParseType(81);
  7926. node.default = this.tsEatThenParseType(29);
  7927. return this.finishNode(node, "TSTypeParameter");
  7928. }
  7929. tsTryParseTypeParameters(parseModifiers) {
  7930. if (this.match(47)) {
  7931. return this.tsParseTypeParameters(parseModifiers);
  7932. }
  7933. }
  7934. tsParseTypeParameters(parseModifiers) {
  7935. const node = this.startNode();
  7936. if (this.match(47) || this.match(140)) {
  7937. this.next();
  7938. } else {
  7939. this.unexpected();
  7940. }
  7941. const refTrailingCommaPos = {
  7942. value: -1
  7943. };
  7944. node.params = this.tsParseBracketedList("TypeParametersOrArguments", this.tsParseTypeParameter.bind(this, parseModifiers), false, true, refTrailingCommaPos);
  7945. if (node.params.length === 0) {
  7946. this.raise(TSErrors.EmptyTypeParameters, {
  7947. at: node
  7948. });
  7949. }
  7950. if (refTrailingCommaPos.value !== -1) {
  7951. this.addExtra(node, "trailingComma", refTrailingCommaPos.value);
  7952. }
  7953. return this.finishNode(node, "TSTypeParameterDeclaration");
  7954. }
  7955. tsFillSignature(returnToken, signature) {
  7956. const returnTokenRequired = returnToken === 19;
  7957. const paramsKey = "parameters";
  7958. const returnTypeKey = "typeAnnotation";
  7959. signature.typeParameters = this.tsTryParseTypeParameters(this.tsParseConstModifier);
  7960. this.expect(10);
  7961. signature[paramsKey] = this.tsParseBindingListForSignature();
  7962. if (returnTokenRequired) {
  7963. signature[returnTypeKey] = this.tsParseTypeOrTypePredicateAnnotation(returnToken);
  7964. } else if (this.match(returnToken)) {
  7965. signature[returnTypeKey] = this.tsParseTypeOrTypePredicateAnnotation(returnToken);
  7966. }
  7967. }
  7968. tsParseBindingListForSignature() {
  7969. return super.parseBindingList(11, 41, ParseBindingListFlags.IS_FUNCTION_PARAMS).map(pattern => {
  7970. if (pattern.type !== "Identifier" && pattern.type !== "RestElement" && pattern.type !== "ObjectPattern" && pattern.type !== "ArrayPattern") {
  7971. this.raise(TSErrors.UnsupportedSignatureParameterKind, {
  7972. at: pattern,
  7973. type: pattern.type
  7974. });
  7975. }
  7976. return pattern;
  7977. });
  7978. }
  7979. tsParseTypeMemberSemicolon() {
  7980. if (!this.eat(12) && !this.isLineTerminator()) {
  7981. this.expect(13);
  7982. }
  7983. }
  7984. tsParseSignatureMember(kind, node) {
  7985. this.tsFillSignature(14, node);
  7986. this.tsParseTypeMemberSemicolon();
  7987. return this.finishNode(node, kind);
  7988. }
  7989. tsIsUnambiguouslyIndexSignature() {
  7990. this.next();
  7991. if (tokenIsIdentifier(this.state.type)) {
  7992. this.next();
  7993. return this.match(14);
  7994. }
  7995. return false;
  7996. }
  7997. tsTryParseIndexSignature(node) {
  7998. if (!(this.match(0) && this.tsLookAhead(this.tsIsUnambiguouslyIndexSignature.bind(this)))) {
  7999. return undefined;
  8000. }
  8001. this.expect(0);
  8002. const id = this.parseIdentifier();
  8003. id.typeAnnotation = this.tsParseTypeAnnotation();
  8004. this.resetEndLocation(id);
  8005. this.expect(3);
  8006. node.parameters = [id];
  8007. const type = this.tsTryParseTypeAnnotation();
  8008. if (type) node.typeAnnotation = type;
  8009. this.tsParseTypeMemberSemicolon();
  8010. return this.finishNode(node, "TSIndexSignature");
  8011. }
  8012. tsParsePropertyOrMethodSignature(node, readonly) {
  8013. if (this.eat(17)) node.optional = true;
  8014. const nodeAny = node;
  8015. if (this.match(10) || this.match(47)) {
  8016. if (readonly) {
  8017. this.raise(TSErrors.ReadonlyForMethodSignature, {
  8018. at: node
  8019. });
  8020. }
  8021. const method = nodeAny;
  8022. if (method.kind && this.match(47)) {
  8023. this.raise(TSErrors.AccesorCannotHaveTypeParameters, {
  8024. at: this.state.curPosition()
  8025. });
  8026. }
  8027. this.tsFillSignature(14, method);
  8028. this.tsParseTypeMemberSemicolon();
  8029. const paramsKey = "parameters";
  8030. const returnTypeKey = "typeAnnotation";
  8031. if (method.kind === "get") {
  8032. if (method[paramsKey].length > 0) {
  8033. this.raise(Errors.BadGetterArity, {
  8034. at: this.state.curPosition()
  8035. });
  8036. if (this.isThisParam(method[paramsKey][0])) {
  8037. this.raise(TSErrors.AccesorCannotDeclareThisParameter, {
  8038. at: this.state.curPosition()
  8039. });
  8040. }
  8041. }
  8042. } else if (method.kind === "set") {
  8043. if (method[paramsKey].length !== 1) {
  8044. this.raise(Errors.BadSetterArity, {
  8045. at: this.state.curPosition()
  8046. });
  8047. } else {
  8048. const firstParameter = method[paramsKey][0];
  8049. if (this.isThisParam(firstParameter)) {
  8050. this.raise(TSErrors.AccesorCannotDeclareThisParameter, {
  8051. at: this.state.curPosition()
  8052. });
  8053. }
  8054. if (firstParameter.type === "Identifier" && firstParameter.optional) {
  8055. this.raise(TSErrors.SetAccesorCannotHaveOptionalParameter, {
  8056. at: this.state.curPosition()
  8057. });
  8058. }
  8059. if (firstParameter.type === "RestElement") {
  8060. this.raise(TSErrors.SetAccesorCannotHaveRestParameter, {
  8061. at: this.state.curPosition()
  8062. });
  8063. }
  8064. }
  8065. if (method[returnTypeKey]) {
  8066. this.raise(TSErrors.SetAccesorCannotHaveReturnType, {
  8067. at: method[returnTypeKey]
  8068. });
  8069. }
  8070. } else {
  8071. method.kind = "method";
  8072. }
  8073. return this.finishNode(method, "TSMethodSignature");
  8074. } else {
  8075. const property = nodeAny;
  8076. if (readonly) property.readonly = true;
  8077. const type = this.tsTryParseTypeAnnotation();
  8078. if (type) property.typeAnnotation = type;
  8079. this.tsParseTypeMemberSemicolon();
  8080. return this.finishNode(property, "TSPropertySignature");
  8081. }
  8082. }
  8083. tsParseTypeMember() {
  8084. const node = this.startNode();
  8085. if (this.match(10) || this.match(47)) {
  8086. return this.tsParseSignatureMember("TSCallSignatureDeclaration", node);
  8087. }
  8088. if (this.match(77)) {
  8089. const id = this.startNode();
  8090. this.next();
  8091. if (this.match(10) || this.match(47)) {
  8092. return this.tsParseSignatureMember("TSConstructSignatureDeclaration", node);
  8093. } else {
  8094. node.key = this.createIdentifier(id, "new");
  8095. return this.tsParsePropertyOrMethodSignature(node, false);
  8096. }
  8097. }
  8098. this.tsParseModifiers({
  8099. allowedModifiers: ["readonly"],
  8100. disallowedModifiers: ["declare", "abstract", "private", "protected", "public", "static", "override"]
  8101. }, node);
  8102. const idx = this.tsTryParseIndexSignature(node);
  8103. if (idx) {
  8104. return idx;
  8105. }
  8106. super.parsePropertyName(node);
  8107. if (!node.computed && node.key.type === "Identifier" && (node.key.name === "get" || node.key.name === "set") && this.tsTokenCanFollowModifier()) {
  8108. node.kind = node.key.name;
  8109. super.parsePropertyName(node);
  8110. }
  8111. return this.tsParsePropertyOrMethodSignature(node, !!node.readonly);
  8112. }
  8113. tsParseTypeLiteral() {
  8114. const node = this.startNode();
  8115. node.members = this.tsParseObjectTypeMembers();
  8116. return this.finishNode(node, "TSTypeLiteral");
  8117. }
  8118. tsParseObjectTypeMembers() {
  8119. this.expect(5);
  8120. const members = this.tsParseList("TypeMembers", this.tsParseTypeMember.bind(this));
  8121. this.expect(8);
  8122. return members;
  8123. }
  8124. tsIsStartOfMappedType() {
  8125. this.next();
  8126. if (this.eat(53)) {
  8127. return this.isContextual(120);
  8128. }
  8129. if (this.isContextual(120)) {
  8130. this.next();
  8131. }
  8132. if (!this.match(0)) {
  8133. return false;
  8134. }
  8135. this.next();
  8136. if (!this.tsIsIdentifier()) {
  8137. return false;
  8138. }
  8139. this.next();
  8140. return this.match(58);
  8141. }
  8142. tsParseMappedTypeParameter() {
  8143. const node = this.startNode();
  8144. node.name = this.tsParseTypeParameterName();
  8145. node.constraint = this.tsExpectThenParseType(58);
  8146. return this.finishNode(node, "TSTypeParameter");
  8147. }
  8148. tsParseMappedType() {
  8149. const node = this.startNode();
  8150. this.expect(5);
  8151. if (this.match(53)) {
  8152. node.readonly = this.state.value;
  8153. this.next();
  8154. this.expectContextual(120);
  8155. } else if (this.eatContextual(120)) {
  8156. node.readonly = true;
  8157. }
  8158. this.expect(0);
  8159. node.typeParameter = this.tsParseMappedTypeParameter();
  8160. node.nameType = this.eatContextual(93) ? this.tsParseType() : null;
  8161. this.expect(3);
  8162. if (this.match(53)) {
  8163. node.optional = this.state.value;
  8164. this.next();
  8165. this.expect(17);
  8166. } else if (this.eat(17)) {
  8167. node.optional = true;
  8168. }
  8169. node.typeAnnotation = this.tsTryParseType();
  8170. this.semicolon();
  8171. this.expect(8);
  8172. return this.finishNode(node, "TSMappedType");
  8173. }
  8174. tsParseTupleType() {
  8175. const node = this.startNode();
  8176. node.elementTypes = this.tsParseBracketedList("TupleElementTypes", this.tsParseTupleElementType.bind(this), true, false);
  8177. let seenOptionalElement = false;
  8178. let labeledElements = null;
  8179. node.elementTypes.forEach(elementNode => {
  8180. var _labeledElements;
  8181. const {
  8182. type
  8183. } = elementNode;
  8184. if (seenOptionalElement && type !== "TSRestType" && type !== "TSOptionalType" && !(type === "TSNamedTupleMember" && elementNode.optional)) {
  8185. this.raise(TSErrors.OptionalTypeBeforeRequired, {
  8186. at: elementNode
  8187. });
  8188. }
  8189. seenOptionalElement || (seenOptionalElement = type === "TSNamedTupleMember" && elementNode.optional || type === "TSOptionalType");
  8190. let checkType = type;
  8191. if (type === "TSRestType") {
  8192. elementNode = elementNode.typeAnnotation;
  8193. checkType = elementNode.type;
  8194. }
  8195. const isLabeled = checkType === "TSNamedTupleMember";
  8196. (_labeledElements = labeledElements) != null ? _labeledElements : labeledElements = isLabeled;
  8197. if (labeledElements !== isLabeled) {
  8198. this.raise(TSErrors.MixedLabeledAndUnlabeledElements, {
  8199. at: elementNode
  8200. });
  8201. }
  8202. });
  8203. return this.finishNode(node, "TSTupleType");
  8204. }
  8205. tsParseTupleElementType() {
  8206. const {
  8207. startLoc
  8208. } = this.state;
  8209. const rest = this.eat(21);
  8210. let type = this.tsParseType();
  8211. const optional = this.eat(17);
  8212. const labeled = this.eat(14);
  8213. if (labeled) {
  8214. const labeledNode = this.startNodeAtNode(type);
  8215. labeledNode.optional = optional;
  8216. if (type.type === "TSTypeReference" && !type.typeParameters && type.typeName.type === "Identifier") {
  8217. labeledNode.label = type.typeName;
  8218. } else {
  8219. this.raise(TSErrors.InvalidTupleMemberLabel, {
  8220. at: type
  8221. });
  8222. labeledNode.label = type;
  8223. }
  8224. labeledNode.elementType = this.tsParseType();
  8225. type = this.finishNode(labeledNode, "TSNamedTupleMember");
  8226. } else if (optional) {
  8227. const optionalTypeNode = this.startNodeAtNode(type);
  8228. optionalTypeNode.typeAnnotation = type;
  8229. type = this.finishNode(optionalTypeNode, "TSOptionalType");
  8230. }
  8231. if (rest) {
  8232. const restNode = this.startNodeAt(startLoc);
  8233. restNode.typeAnnotation = type;
  8234. type = this.finishNode(restNode, "TSRestType");
  8235. }
  8236. return type;
  8237. }
  8238. tsParseParenthesizedType() {
  8239. const node = this.startNode();
  8240. this.expect(10);
  8241. node.typeAnnotation = this.tsParseType();
  8242. this.expect(11);
  8243. return this.finishNode(node, "TSParenthesizedType");
  8244. }
  8245. tsParseFunctionOrConstructorType(type, abstract) {
  8246. const node = this.startNode();
  8247. if (type === "TSConstructorType") {
  8248. node.abstract = !!abstract;
  8249. if (abstract) this.next();
  8250. this.next();
  8251. }
  8252. this.tsInAllowConditionalTypesContext(() => this.tsFillSignature(19, node));
  8253. return this.finishNode(node, type);
  8254. }
  8255. tsParseLiteralTypeNode() {
  8256. const node = this.startNode();
  8257. node.literal = (() => {
  8258. switch (this.state.type) {
  8259. case 132:
  8260. case 133:
  8261. case 131:
  8262. case 85:
  8263. case 86:
  8264. return super.parseExprAtom();
  8265. default:
  8266. throw this.unexpected();
  8267. }
  8268. })();
  8269. return this.finishNode(node, "TSLiteralType");
  8270. }
  8271. tsParseTemplateLiteralType() {
  8272. const node = this.startNode();
  8273. node.literal = super.parseTemplate(false);
  8274. return this.finishNode(node, "TSLiteralType");
  8275. }
  8276. parseTemplateSubstitution() {
  8277. if (this.state.inType) return this.tsParseType();
  8278. return super.parseTemplateSubstitution();
  8279. }
  8280. tsParseThisTypeOrThisTypePredicate() {
  8281. const thisKeyword = this.tsParseThisTypeNode();
  8282. if (this.isContextual(114) && !this.hasPrecedingLineBreak()) {
  8283. return this.tsParseThisTypePredicate(thisKeyword);
  8284. } else {
  8285. return thisKeyword;
  8286. }
  8287. }
  8288. tsParseNonArrayType() {
  8289. switch (this.state.type) {
  8290. case 131:
  8291. case 132:
  8292. case 133:
  8293. case 85:
  8294. case 86:
  8295. return this.tsParseLiteralTypeNode();
  8296. case 53:
  8297. if (this.state.value === "-") {
  8298. const node = this.startNode();
  8299. const nextToken = this.lookahead();
  8300. if (nextToken.type !== 132 && nextToken.type !== 133) {
  8301. throw this.unexpected();
  8302. }
  8303. node.literal = this.parseMaybeUnary();
  8304. return this.finishNode(node, "TSLiteralType");
  8305. }
  8306. break;
  8307. case 78:
  8308. return this.tsParseThisTypeOrThisTypePredicate();
  8309. case 87:
  8310. return this.tsParseTypeQuery();
  8311. case 83:
  8312. return this.tsParseImportType();
  8313. case 5:
  8314. return this.tsLookAhead(this.tsIsStartOfMappedType.bind(this)) ? this.tsParseMappedType() : this.tsParseTypeLiteral();
  8315. case 0:
  8316. return this.tsParseTupleType();
  8317. case 10:
  8318. return this.tsParseParenthesizedType();
  8319. case 25:
  8320. case 24:
  8321. return this.tsParseTemplateLiteralType();
  8322. default:
  8323. {
  8324. const {
  8325. type
  8326. } = this.state;
  8327. if (tokenIsIdentifier(type) || type === 88 || type === 84) {
  8328. const nodeType = type === 88 ? "TSVoidKeyword" : type === 84 ? "TSNullKeyword" : keywordTypeFromName(this.state.value);
  8329. if (nodeType !== undefined && this.lookaheadCharCode() !== 46) {
  8330. const node = this.startNode();
  8331. this.next();
  8332. return this.finishNode(node, nodeType);
  8333. }
  8334. return this.tsParseTypeReference();
  8335. }
  8336. }
  8337. }
  8338. throw this.unexpected();
  8339. }
  8340. tsParseArrayTypeOrHigher() {
  8341. let type = this.tsParseNonArrayType();
  8342. while (!this.hasPrecedingLineBreak() && this.eat(0)) {
  8343. if (this.match(3)) {
  8344. const node = this.startNodeAtNode(type);
  8345. node.elementType = type;
  8346. this.expect(3);
  8347. type = this.finishNode(node, "TSArrayType");
  8348. } else {
  8349. const node = this.startNodeAtNode(type);
  8350. node.objectType = type;
  8351. node.indexType = this.tsParseType();
  8352. this.expect(3);
  8353. type = this.finishNode(node, "TSIndexedAccessType");
  8354. }
  8355. }
  8356. return type;
  8357. }
  8358. tsParseTypeOperator() {
  8359. const node = this.startNode();
  8360. const operator = this.state.value;
  8361. this.next();
  8362. node.operator = operator;
  8363. node.typeAnnotation = this.tsParseTypeOperatorOrHigher();
  8364. if (operator === "readonly") {
  8365. this.tsCheckTypeAnnotationForReadOnly(node);
  8366. }
  8367. return this.finishNode(node, "TSTypeOperator");
  8368. }
  8369. tsCheckTypeAnnotationForReadOnly(node) {
  8370. switch (node.typeAnnotation.type) {
  8371. case "TSTupleType":
  8372. case "TSArrayType":
  8373. return;
  8374. default:
  8375. this.raise(TSErrors.UnexpectedReadonly, {
  8376. at: node
  8377. });
  8378. }
  8379. }
  8380. tsParseInferType() {
  8381. const node = this.startNode();
  8382. this.expectContextual(113);
  8383. const typeParameter = this.startNode();
  8384. typeParameter.name = this.tsParseTypeParameterName();
  8385. typeParameter.constraint = this.tsTryParse(() => this.tsParseConstraintForInferType());
  8386. node.typeParameter = this.finishNode(typeParameter, "TSTypeParameter");
  8387. return this.finishNode(node, "TSInferType");
  8388. }
  8389. tsParseConstraintForInferType() {
  8390. if (this.eat(81)) {
  8391. const constraint = this.tsInDisallowConditionalTypesContext(() => this.tsParseType());
  8392. if (this.state.inDisallowConditionalTypesContext || !this.match(17)) {
  8393. return constraint;
  8394. }
  8395. }
  8396. }
  8397. tsParseTypeOperatorOrHigher() {
  8398. const isTypeOperator = tokenIsTSTypeOperator(this.state.type) && !this.state.containsEsc;
  8399. return isTypeOperator ? this.tsParseTypeOperator() : this.isContextual(113) ? this.tsParseInferType() : this.tsInAllowConditionalTypesContext(() => this.tsParseArrayTypeOrHigher());
  8400. }
  8401. tsParseUnionOrIntersectionType(kind, parseConstituentType, operator) {
  8402. const node = this.startNode();
  8403. const hasLeadingOperator = this.eat(operator);
  8404. const types = [];
  8405. do {
  8406. types.push(parseConstituentType());
  8407. } while (this.eat(operator));
  8408. if (types.length === 1 && !hasLeadingOperator) {
  8409. return types[0];
  8410. }
  8411. node.types = types;
  8412. return this.finishNode(node, kind);
  8413. }
  8414. tsParseIntersectionTypeOrHigher() {
  8415. return this.tsParseUnionOrIntersectionType("TSIntersectionType", this.tsParseTypeOperatorOrHigher.bind(this), 45);
  8416. }
  8417. tsParseUnionTypeOrHigher() {
  8418. return this.tsParseUnionOrIntersectionType("TSUnionType", this.tsParseIntersectionTypeOrHigher.bind(this), 43);
  8419. }
  8420. tsIsStartOfFunctionType() {
  8421. if (this.match(47)) {
  8422. return true;
  8423. }
  8424. return this.match(10) && this.tsLookAhead(this.tsIsUnambiguouslyStartOfFunctionType.bind(this));
  8425. }
  8426. tsSkipParameterStart() {
  8427. if (tokenIsIdentifier(this.state.type) || this.match(78)) {
  8428. this.next();
  8429. return true;
  8430. }
  8431. if (this.match(5)) {
  8432. const {
  8433. errors
  8434. } = this.state;
  8435. const previousErrorCount = errors.length;
  8436. try {
  8437. this.parseObjectLike(8, true);
  8438. return errors.length === previousErrorCount;
  8439. } catch (_unused) {
  8440. return false;
  8441. }
  8442. }
  8443. if (this.match(0)) {
  8444. this.next();
  8445. const {
  8446. errors
  8447. } = this.state;
  8448. const previousErrorCount = errors.length;
  8449. try {
  8450. super.parseBindingList(3, 93, ParseBindingListFlags.ALLOW_EMPTY);
  8451. return errors.length === previousErrorCount;
  8452. } catch (_unused2) {
  8453. return false;
  8454. }
  8455. }
  8456. return false;
  8457. }
  8458. tsIsUnambiguouslyStartOfFunctionType() {
  8459. this.next();
  8460. if (this.match(11) || this.match(21)) {
  8461. return true;
  8462. }
  8463. if (this.tsSkipParameterStart()) {
  8464. if (this.match(14) || this.match(12) || this.match(17) || this.match(29)) {
  8465. return true;
  8466. }
  8467. if (this.match(11)) {
  8468. this.next();
  8469. if (this.match(19)) {
  8470. return true;
  8471. }
  8472. }
  8473. }
  8474. return false;
  8475. }
  8476. tsParseTypeOrTypePredicateAnnotation(returnToken) {
  8477. return this.tsInType(() => {
  8478. const t = this.startNode();
  8479. this.expect(returnToken);
  8480. const node = this.startNode();
  8481. const asserts = !!this.tsTryParse(this.tsParseTypePredicateAsserts.bind(this));
  8482. if (asserts && this.match(78)) {
  8483. let thisTypePredicate = this.tsParseThisTypeOrThisTypePredicate();
  8484. if (thisTypePredicate.type === "TSThisType") {
  8485. node.parameterName = thisTypePredicate;
  8486. node.asserts = true;
  8487. node.typeAnnotation = null;
  8488. thisTypePredicate = this.finishNode(node, "TSTypePredicate");
  8489. } else {
  8490. this.resetStartLocationFromNode(thisTypePredicate, node);
  8491. thisTypePredicate.asserts = true;
  8492. }
  8493. t.typeAnnotation = thisTypePredicate;
  8494. return this.finishNode(t, "TSTypeAnnotation");
  8495. }
  8496. const typePredicateVariable = this.tsIsIdentifier() && this.tsTryParse(this.tsParseTypePredicatePrefix.bind(this));
  8497. if (!typePredicateVariable) {
  8498. if (!asserts) {
  8499. return this.tsParseTypeAnnotation(false, t);
  8500. }
  8501. node.parameterName = this.parseIdentifier();
  8502. node.asserts = asserts;
  8503. node.typeAnnotation = null;
  8504. t.typeAnnotation = this.finishNode(node, "TSTypePredicate");
  8505. return this.finishNode(t, "TSTypeAnnotation");
  8506. }
  8507. const type = this.tsParseTypeAnnotation(false);
  8508. node.parameterName = typePredicateVariable;
  8509. node.typeAnnotation = type;
  8510. node.asserts = asserts;
  8511. t.typeAnnotation = this.finishNode(node, "TSTypePredicate");
  8512. return this.finishNode(t, "TSTypeAnnotation");
  8513. });
  8514. }
  8515. tsTryParseTypeOrTypePredicateAnnotation() {
  8516. return this.match(14) ? this.tsParseTypeOrTypePredicateAnnotation(14) : undefined;
  8517. }
  8518. tsTryParseTypeAnnotation() {
  8519. return this.match(14) ? this.tsParseTypeAnnotation() : undefined;
  8520. }
  8521. tsTryParseType() {
  8522. return this.tsEatThenParseType(14);
  8523. }
  8524. tsParseTypePredicatePrefix() {
  8525. const id = this.parseIdentifier();
  8526. if (this.isContextual(114) && !this.hasPrecedingLineBreak()) {
  8527. this.next();
  8528. return id;
  8529. }
  8530. }
  8531. tsParseTypePredicateAsserts() {
  8532. if (this.state.type !== 107) {
  8533. return false;
  8534. }
  8535. const containsEsc = this.state.containsEsc;
  8536. this.next();
  8537. if (!tokenIsIdentifier(this.state.type) && !this.match(78)) {
  8538. return false;
  8539. }
  8540. if (containsEsc) {
  8541. this.raise(Errors.InvalidEscapedReservedWord, {
  8542. at: this.state.lastTokStartLoc,
  8543. reservedWord: "asserts"
  8544. });
  8545. }
  8546. return true;
  8547. }
  8548. tsParseTypeAnnotation(eatColon = true, t = this.startNode()) {
  8549. this.tsInType(() => {
  8550. if (eatColon) this.expect(14);
  8551. t.typeAnnotation = this.tsParseType();
  8552. });
  8553. return this.finishNode(t, "TSTypeAnnotation");
  8554. }
  8555. tsParseType() {
  8556. assert(this.state.inType);
  8557. const type = this.tsParseNonConditionalType();
  8558. if (this.state.inDisallowConditionalTypesContext || this.hasPrecedingLineBreak() || !this.eat(81)) {
  8559. return type;
  8560. }
  8561. const node = this.startNodeAtNode(type);
  8562. node.checkType = type;
  8563. node.extendsType = this.tsInDisallowConditionalTypesContext(() => this.tsParseNonConditionalType());
  8564. this.expect(17);
  8565. node.trueType = this.tsInAllowConditionalTypesContext(() => this.tsParseType());
  8566. this.expect(14);
  8567. node.falseType = this.tsInAllowConditionalTypesContext(() => this.tsParseType());
  8568. return this.finishNode(node, "TSConditionalType");
  8569. }
  8570. isAbstractConstructorSignature() {
  8571. return this.isContextual(122) && this.lookahead().type === 77;
  8572. }
  8573. tsParseNonConditionalType() {
  8574. if (this.tsIsStartOfFunctionType()) {
  8575. return this.tsParseFunctionOrConstructorType("TSFunctionType");
  8576. }
  8577. if (this.match(77)) {
  8578. return this.tsParseFunctionOrConstructorType("TSConstructorType");
  8579. } else if (this.isAbstractConstructorSignature()) {
  8580. return this.tsParseFunctionOrConstructorType("TSConstructorType", true);
  8581. }
  8582. return this.tsParseUnionTypeOrHigher();
  8583. }
  8584. tsParseTypeAssertion() {
  8585. if (this.getPluginOption("typescript", "disallowAmbiguousJSXLike")) {
  8586. this.raise(TSErrors.ReservedTypeAssertion, {
  8587. at: this.state.startLoc
  8588. });
  8589. }
  8590. const node = this.startNode();
  8591. node.typeAnnotation = this.tsInType(() => {
  8592. this.next();
  8593. return this.match(75) ? this.tsParseTypeReference() : this.tsParseType();
  8594. });
  8595. this.expect(48);
  8596. node.expression = this.parseMaybeUnary();
  8597. return this.finishNode(node, "TSTypeAssertion");
  8598. }
  8599. tsParseHeritageClause(token) {
  8600. const originalStartLoc = this.state.startLoc;
  8601. const delimitedList = this.tsParseDelimitedList("HeritageClauseElement", () => {
  8602. const node = this.startNode();
  8603. node.expression = this.tsParseEntityName();
  8604. if (this.match(47)) {
  8605. node.typeParameters = this.tsParseTypeArguments();
  8606. }
  8607. return this.finishNode(node, "TSExpressionWithTypeArguments");
  8608. });
  8609. if (!delimitedList.length) {
  8610. this.raise(TSErrors.EmptyHeritageClauseType, {
  8611. at: originalStartLoc,
  8612. token
  8613. });
  8614. }
  8615. return delimitedList;
  8616. }
  8617. tsParseInterfaceDeclaration(node, properties = {}) {
  8618. if (this.hasFollowingLineBreak()) return null;
  8619. this.expectContextual(127);
  8620. if (properties.declare) node.declare = true;
  8621. if (tokenIsIdentifier(this.state.type)) {
  8622. node.id = this.parseIdentifier();
  8623. this.checkIdentifier(node.id, BIND_TS_INTERFACE);
  8624. } else {
  8625. node.id = null;
  8626. this.raise(TSErrors.MissingInterfaceName, {
  8627. at: this.state.startLoc
  8628. });
  8629. }
  8630. node.typeParameters = this.tsTryParseTypeParameters(this.tsParseInOutConstModifiers);
  8631. if (this.eat(81)) {
  8632. node.extends = this.tsParseHeritageClause("extends");
  8633. }
  8634. const body = this.startNode();
  8635. body.body = this.tsInType(this.tsParseObjectTypeMembers.bind(this));
  8636. node.body = this.finishNode(body, "TSInterfaceBody");
  8637. return this.finishNode(node, "TSInterfaceDeclaration");
  8638. }
  8639. tsParseTypeAliasDeclaration(node) {
  8640. node.id = this.parseIdentifier();
  8641. this.checkIdentifier(node.id, BIND_TS_TYPE);
  8642. node.typeAnnotation = this.tsInType(() => {
  8643. node.typeParameters = this.tsTryParseTypeParameters(this.tsParseInOutModifiers);
  8644. this.expect(29);
  8645. if (this.isContextual(112) && this.lookahead().type !== 16) {
  8646. const node = this.startNode();
  8647. this.next();
  8648. return this.finishNode(node, "TSIntrinsicKeyword");
  8649. }
  8650. return this.tsParseType();
  8651. });
  8652. this.semicolon();
  8653. return this.finishNode(node, "TSTypeAliasDeclaration");
  8654. }
  8655. tsInNoContext(cb) {
  8656. const oldContext = this.state.context;
  8657. this.state.context = [oldContext[0]];
  8658. try {
  8659. return cb();
  8660. } finally {
  8661. this.state.context = oldContext;
  8662. }
  8663. }
  8664. tsInType(cb) {
  8665. const oldInType = this.state.inType;
  8666. this.state.inType = true;
  8667. try {
  8668. return cb();
  8669. } finally {
  8670. this.state.inType = oldInType;
  8671. }
  8672. }
  8673. tsInDisallowConditionalTypesContext(cb) {
  8674. const oldInDisallowConditionalTypesContext = this.state.inDisallowConditionalTypesContext;
  8675. this.state.inDisallowConditionalTypesContext = true;
  8676. try {
  8677. return cb();
  8678. } finally {
  8679. this.state.inDisallowConditionalTypesContext = oldInDisallowConditionalTypesContext;
  8680. }
  8681. }
  8682. tsInAllowConditionalTypesContext(cb) {
  8683. const oldInDisallowConditionalTypesContext = this.state.inDisallowConditionalTypesContext;
  8684. this.state.inDisallowConditionalTypesContext = false;
  8685. try {
  8686. return cb();
  8687. } finally {
  8688. this.state.inDisallowConditionalTypesContext = oldInDisallowConditionalTypesContext;
  8689. }
  8690. }
  8691. tsEatThenParseType(token) {
  8692. return !this.match(token) ? undefined : this.tsNextThenParseType();
  8693. }
  8694. tsExpectThenParseType(token) {
  8695. return this.tsDoThenParseType(() => this.expect(token));
  8696. }
  8697. tsNextThenParseType() {
  8698. return this.tsDoThenParseType(() => this.next());
  8699. }
  8700. tsDoThenParseType(cb) {
  8701. return this.tsInType(() => {
  8702. cb();
  8703. return this.tsParseType();
  8704. });
  8705. }
  8706. tsParseEnumMember() {
  8707. const node = this.startNode();
  8708. node.id = this.match(131) ? super.parseStringLiteral(this.state.value) : this.parseIdentifier(true);
  8709. if (this.eat(29)) {
  8710. node.initializer = super.parseMaybeAssignAllowIn();
  8711. }
  8712. return this.finishNode(node, "TSEnumMember");
  8713. }
  8714. tsParseEnumDeclaration(node, properties = {}) {
  8715. if (properties.const) node.const = true;
  8716. if (properties.declare) node.declare = true;
  8717. this.expectContextual(124);
  8718. node.id = this.parseIdentifier();
  8719. this.checkIdentifier(node.id, node.const ? BIND_TS_CONST_ENUM : BIND_TS_ENUM);
  8720. this.expect(5);
  8721. node.members = this.tsParseDelimitedList("EnumMembers", this.tsParseEnumMember.bind(this));
  8722. this.expect(8);
  8723. return this.finishNode(node, "TSEnumDeclaration");
  8724. }
  8725. tsParseModuleBlock() {
  8726. const node = this.startNode();
  8727. this.scope.enter(SCOPE_OTHER);
  8728. this.expect(5);
  8729. super.parseBlockOrModuleBlockBody(node.body = [], undefined, true, 8);
  8730. this.scope.exit();
  8731. return this.finishNode(node, "TSModuleBlock");
  8732. }
  8733. tsParseModuleOrNamespaceDeclaration(node, nested = false) {
  8734. node.id = this.parseIdentifier();
  8735. if (!nested) {
  8736. this.checkIdentifier(node.id, BIND_TS_NAMESPACE);
  8737. }
  8738. if (this.eat(16)) {
  8739. const inner = this.startNode();
  8740. this.tsParseModuleOrNamespaceDeclaration(inner, true);
  8741. node.body = inner;
  8742. } else {
  8743. this.scope.enter(SCOPE_TS_MODULE);
  8744. this.prodParam.enter(PARAM);
  8745. node.body = this.tsParseModuleBlock();
  8746. this.prodParam.exit();
  8747. this.scope.exit();
  8748. }
  8749. return this.finishNode(node, "TSModuleDeclaration");
  8750. }
  8751. tsParseAmbientExternalModuleDeclaration(node) {
  8752. if (this.isContextual(110)) {
  8753. node.global = true;
  8754. node.id = this.parseIdentifier();
  8755. } else if (this.match(131)) {
  8756. node.id = super.parseStringLiteral(this.state.value);
  8757. } else {
  8758. this.unexpected();
  8759. }
  8760. if (this.match(5)) {
  8761. this.scope.enter(SCOPE_TS_MODULE);
  8762. this.prodParam.enter(PARAM);
  8763. node.body = this.tsParseModuleBlock();
  8764. this.prodParam.exit();
  8765. this.scope.exit();
  8766. } else {
  8767. this.semicolon();
  8768. }
  8769. return this.finishNode(node, "TSModuleDeclaration");
  8770. }
  8771. tsParseImportEqualsDeclaration(node, isExport) {
  8772. node.isExport = isExport || false;
  8773. node.id = this.parseIdentifier();
  8774. this.checkIdentifier(node.id, BIND_FLAGS_TS_IMPORT);
  8775. this.expect(29);
  8776. const moduleReference = this.tsParseModuleReference();
  8777. if (node.importKind === "type" && moduleReference.type !== "TSExternalModuleReference") {
  8778. this.raise(TSErrors.ImportAliasHasImportType, {
  8779. at: moduleReference
  8780. });
  8781. }
  8782. node.moduleReference = moduleReference;
  8783. this.semicolon();
  8784. return this.finishNode(node, "TSImportEqualsDeclaration");
  8785. }
  8786. tsIsExternalModuleReference() {
  8787. return this.isContextual(117) && this.lookaheadCharCode() === 40;
  8788. }
  8789. tsParseModuleReference() {
  8790. return this.tsIsExternalModuleReference() ? this.tsParseExternalModuleReference() : this.tsParseEntityName(false);
  8791. }
  8792. tsParseExternalModuleReference() {
  8793. const node = this.startNode();
  8794. this.expectContextual(117);
  8795. this.expect(10);
  8796. if (!this.match(131)) {
  8797. throw this.unexpected();
  8798. }
  8799. node.expression = super.parseExprAtom();
  8800. this.expect(11);
  8801. return this.finishNode(node, "TSExternalModuleReference");
  8802. }
  8803. tsLookAhead(f) {
  8804. const state = this.state.clone();
  8805. const res = f();
  8806. this.state = state;
  8807. return res;
  8808. }
  8809. tsTryParseAndCatch(f) {
  8810. const result = this.tryParse(abort => f() || abort());
  8811. if (result.aborted || !result.node) return undefined;
  8812. if (result.error) this.state = result.failState;
  8813. return result.node;
  8814. }
  8815. tsTryParse(f) {
  8816. const state = this.state.clone();
  8817. const result = f();
  8818. if (result !== undefined && result !== false) {
  8819. return result;
  8820. } else {
  8821. this.state = state;
  8822. return undefined;
  8823. }
  8824. }
  8825. tsTryParseDeclare(nany) {
  8826. if (this.isLineTerminator()) {
  8827. return;
  8828. }
  8829. let starttype = this.state.type;
  8830. let kind;
  8831. if (this.isContextual(99)) {
  8832. starttype = 74;
  8833. kind = "let";
  8834. }
  8835. return this.tsInAmbientContext(() => {
  8836. if (starttype === 68) {
  8837. nany.declare = true;
  8838. return super.parseFunctionStatement(nany, false, false);
  8839. }
  8840. if (starttype === 80) {
  8841. nany.declare = true;
  8842. return this.parseClass(nany, true, false);
  8843. }
  8844. if (starttype === 124) {
  8845. return this.tsParseEnumDeclaration(nany, {
  8846. declare: true
  8847. });
  8848. }
  8849. if (starttype === 110) {
  8850. return this.tsParseAmbientExternalModuleDeclaration(nany);
  8851. }
  8852. if (starttype === 75 || starttype === 74) {
  8853. if (!this.match(75) || !this.isLookaheadContextual("enum")) {
  8854. nany.declare = true;
  8855. return this.parseVarStatement(nany, kind || this.state.value, true);
  8856. }
  8857. this.expect(75);
  8858. return this.tsParseEnumDeclaration(nany, {
  8859. const: true,
  8860. declare: true
  8861. });
  8862. }
  8863. if (starttype === 127) {
  8864. const result = this.tsParseInterfaceDeclaration(nany, {
  8865. declare: true
  8866. });
  8867. if (result) return result;
  8868. }
  8869. if (tokenIsIdentifier(starttype)) {
  8870. return this.tsParseDeclaration(nany, this.state.value, true, null);
  8871. }
  8872. });
  8873. }
  8874. tsTryParseExportDeclaration() {
  8875. return this.tsParseDeclaration(this.startNode(), this.state.value, true, null);
  8876. }
  8877. tsParseExpressionStatement(node, expr, decorators) {
  8878. switch (expr.name) {
  8879. case "declare":
  8880. {
  8881. const declaration = this.tsTryParseDeclare(node);
  8882. if (declaration) {
  8883. declaration.declare = true;
  8884. return declaration;
  8885. }
  8886. break;
  8887. }
  8888. case "global":
  8889. if (this.match(5)) {
  8890. this.scope.enter(SCOPE_TS_MODULE);
  8891. this.prodParam.enter(PARAM);
  8892. const mod = node;
  8893. mod.global = true;
  8894. mod.id = expr;
  8895. mod.body = this.tsParseModuleBlock();
  8896. this.scope.exit();
  8897. this.prodParam.exit();
  8898. return this.finishNode(mod, "TSModuleDeclaration");
  8899. }
  8900. break;
  8901. default:
  8902. return this.tsParseDeclaration(node, expr.name, false, decorators);
  8903. }
  8904. }
  8905. tsParseDeclaration(node, value, next, decorators) {
  8906. switch (value) {
  8907. case "abstract":
  8908. if (this.tsCheckLineTerminator(next) && (this.match(80) || tokenIsIdentifier(this.state.type))) {
  8909. return this.tsParseAbstractDeclaration(node, decorators);
  8910. }
  8911. break;
  8912. case "module":
  8913. if (this.tsCheckLineTerminator(next)) {
  8914. if (this.match(131)) {
  8915. return this.tsParseAmbientExternalModuleDeclaration(node);
  8916. } else if (tokenIsIdentifier(this.state.type)) {
  8917. return this.tsParseModuleOrNamespaceDeclaration(node);
  8918. }
  8919. }
  8920. break;
  8921. case "namespace":
  8922. if (this.tsCheckLineTerminator(next) && tokenIsIdentifier(this.state.type)) {
  8923. return this.tsParseModuleOrNamespaceDeclaration(node);
  8924. }
  8925. break;
  8926. case "type":
  8927. if (this.tsCheckLineTerminator(next) && tokenIsIdentifier(this.state.type)) {
  8928. return this.tsParseTypeAliasDeclaration(node);
  8929. }
  8930. break;
  8931. }
  8932. }
  8933. tsCheckLineTerminator(next) {
  8934. if (next) {
  8935. if (this.hasFollowingLineBreak()) return false;
  8936. this.next();
  8937. return true;
  8938. }
  8939. return !this.isLineTerminator();
  8940. }
  8941. tsTryParseGenericAsyncArrowFunction(startLoc) {
  8942. if (!this.match(47)) {
  8943. return undefined;
  8944. }
  8945. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  8946. this.state.maybeInArrowParameters = true;
  8947. const res = this.tsTryParseAndCatch(() => {
  8948. const node = this.startNodeAt(startLoc);
  8949. node.typeParameters = this.tsParseTypeParameters(this.tsParseConstModifier);
  8950. super.parseFunctionParams(node);
  8951. node.returnType = this.tsTryParseTypeOrTypePredicateAnnotation();
  8952. this.expect(19);
  8953. return node;
  8954. });
  8955. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  8956. if (!res) {
  8957. return undefined;
  8958. }
  8959. return super.parseArrowExpression(res, null, true);
  8960. }
  8961. tsParseTypeArgumentsInExpression() {
  8962. if (this.reScan_lt() !== 47) {
  8963. return undefined;
  8964. }
  8965. return this.tsParseTypeArguments();
  8966. }
  8967. tsParseTypeArguments() {
  8968. const node = this.startNode();
  8969. node.params = this.tsInType(() => this.tsInNoContext(() => {
  8970. this.expect(47);
  8971. return this.tsParseDelimitedList("TypeParametersOrArguments", this.tsParseType.bind(this));
  8972. }));
  8973. if (node.params.length === 0) {
  8974. this.raise(TSErrors.EmptyTypeArguments, {
  8975. at: node
  8976. });
  8977. }
  8978. this.expect(48);
  8979. return this.finishNode(node, "TSTypeParameterInstantiation");
  8980. }
  8981. tsIsDeclarationStart() {
  8982. return tokenIsTSDeclarationStart(this.state.type);
  8983. }
  8984. isExportDefaultSpecifier() {
  8985. if (this.tsIsDeclarationStart()) return false;
  8986. return super.isExportDefaultSpecifier();
  8987. }
  8988. parseAssignableListItem(flags, decorators) {
  8989. const startLoc = this.state.startLoc;
  8990. const modified = {};
  8991. this.tsParseModifiers({
  8992. allowedModifiers: ["public", "private", "protected", "override", "readonly"]
  8993. }, modified);
  8994. const accessibility = modified.accessibility;
  8995. const override = modified.override;
  8996. const readonly = modified.readonly;
  8997. if (!(flags & ParseBindingListFlags.IS_CONSTRUCTOR_PARAMS) && (accessibility || readonly || override)) {
  8998. this.raise(TSErrors.UnexpectedParameterModifier, {
  8999. at: startLoc
  9000. });
  9001. }
  9002. const left = this.parseMaybeDefault();
  9003. this.parseAssignableListItemTypes(left, flags);
  9004. const elt = this.parseMaybeDefault(left.loc.start, left);
  9005. if (accessibility || readonly || override) {
  9006. const pp = this.startNodeAt(startLoc);
  9007. if (decorators.length) {
  9008. pp.decorators = decorators;
  9009. }
  9010. if (accessibility) pp.accessibility = accessibility;
  9011. if (readonly) pp.readonly = readonly;
  9012. if (override) pp.override = override;
  9013. if (elt.type !== "Identifier" && elt.type !== "AssignmentPattern") {
  9014. this.raise(TSErrors.UnsupportedParameterPropertyKind, {
  9015. at: pp
  9016. });
  9017. }
  9018. pp.parameter = elt;
  9019. return this.finishNode(pp, "TSParameterProperty");
  9020. }
  9021. if (decorators.length) {
  9022. left.decorators = decorators;
  9023. }
  9024. return elt;
  9025. }
  9026. isSimpleParameter(node) {
  9027. return node.type === "TSParameterProperty" && super.isSimpleParameter(node.parameter) || super.isSimpleParameter(node);
  9028. }
  9029. tsDisallowOptionalPattern(node) {
  9030. for (const param of node.params) {
  9031. if (param.type !== "Identifier" && param.optional && !this.state.isAmbientContext) {
  9032. this.raise(TSErrors.PatternIsOptional, {
  9033. at: param
  9034. });
  9035. }
  9036. }
  9037. }
  9038. setArrowFunctionParameters(node, params, trailingCommaLoc) {
  9039. super.setArrowFunctionParameters(node, params, trailingCommaLoc);
  9040. this.tsDisallowOptionalPattern(node);
  9041. }
  9042. parseFunctionBodyAndFinish(node, type, isMethod = false) {
  9043. if (this.match(14)) {
  9044. node.returnType = this.tsParseTypeOrTypePredicateAnnotation(14);
  9045. }
  9046. const bodilessType = type === "FunctionDeclaration" ? "TSDeclareFunction" : type === "ClassMethod" || type === "ClassPrivateMethod" ? "TSDeclareMethod" : undefined;
  9047. if (bodilessType && !this.match(5) && this.isLineTerminator()) {
  9048. return this.finishNode(node, bodilessType);
  9049. }
  9050. if (bodilessType === "TSDeclareFunction" && this.state.isAmbientContext) {
  9051. this.raise(TSErrors.DeclareFunctionHasImplementation, {
  9052. at: node
  9053. });
  9054. if (node.declare) {
  9055. return super.parseFunctionBodyAndFinish(node, bodilessType, isMethod);
  9056. }
  9057. }
  9058. this.tsDisallowOptionalPattern(node);
  9059. return super.parseFunctionBodyAndFinish(node, type, isMethod);
  9060. }
  9061. registerFunctionStatementId(node) {
  9062. if (!node.body && node.id) {
  9063. this.checkIdentifier(node.id, BIND_TS_AMBIENT);
  9064. } else {
  9065. super.registerFunctionStatementId(node);
  9066. }
  9067. }
  9068. tsCheckForInvalidTypeCasts(items) {
  9069. items.forEach(node => {
  9070. if ((node == null ? void 0 : node.type) === "TSTypeCastExpression") {
  9071. this.raise(TSErrors.UnexpectedTypeAnnotation, {
  9072. at: node.typeAnnotation
  9073. });
  9074. }
  9075. });
  9076. }
  9077. toReferencedList(exprList, isInParens) {
  9078. this.tsCheckForInvalidTypeCasts(exprList);
  9079. return exprList;
  9080. }
  9081. parseArrayLike(close, canBePattern, isTuple, refExpressionErrors) {
  9082. const node = super.parseArrayLike(close, canBePattern, isTuple, refExpressionErrors);
  9083. if (node.type === "ArrayExpression") {
  9084. this.tsCheckForInvalidTypeCasts(node.elements);
  9085. }
  9086. return node;
  9087. }
  9088. parseSubscript(base, startLoc, noCalls, state) {
  9089. if (!this.hasPrecedingLineBreak() && this.match(35)) {
  9090. this.state.canStartJSXElement = false;
  9091. this.next();
  9092. const nonNullExpression = this.startNodeAt(startLoc);
  9093. nonNullExpression.expression = base;
  9094. return this.finishNode(nonNullExpression, "TSNonNullExpression");
  9095. }
  9096. let isOptionalCall = false;
  9097. if (this.match(18) && this.lookaheadCharCode() === 60) {
  9098. if (noCalls) {
  9099. state.stop = true;
  9100. return base;
  9101. }
  9102. state.optionalChainMember = isOptionalCall = true;
  9103. this.next();
  9104. }
  9105. if (this.match(47) || this.match(51)) {
  9106. let missingParenErrorLoc;
  9107. const result = this.tsTryParseAndCatch(() => {
  9108. if (!noCalls && this.atPossibleAsyncArrow(base)) {
  9109. const asyncArrowFn = this.tsTryParseGenericAsyncArrowFunction(startLoc);
  9110. if (asyncArrowFn) {
  9111. return asyncArrowFn;
  9112. }
  9113. }
  9114. const typeArguments = this.tsParseTypeArgumentsInExpression();
  9115. if (!typeArguments) return;
  9116. if (isOptionalCall && !this.match(10)) {
  9117. missingParenErrorLoc = this.state.curPosition();
  9118. return;
  9119. }
  9120. if (tokenIsTemplate(this.state.type)) {
  9121. const result = super.parseTaggedTemplateExpression(base, startLoc, state);
  9122. result.typeParameters = typeArguments;
  9123. return result;
  9124. }
  9125. if (!noCalls && this.eat(10)) {
  9126. const node = this.startNodeAt(startLoc);
  9127. node.callee = base;
  9128. node.arguments = this.parseCallExpressionArguments(11, false);
  9129. this.tsCheckForInvalidTypeCasts(node.arguments);
  9130. node.typeParameters = typeArguments;
  9131. if (state.optionalChainMember) {
  9132. node.optional = isOptionalCall;
  9133. }
  9134. return this.finishCallExpression(node, state.optionalChainMember);
  9135. }
  9136. const tokenType = this.state.type;
  9137. if (tokenType === 48 || tokenType === 52 || tokenType !== 10 && tokenCanStartExpression(tokenType) && !this.hasPrecedingLineBreak()) {
  9138. return;
  9139. }
  9140. const node = this.startNodeAt(startLoc);
  9141. node.expression = base;
  9142. node.typeParameters = typeArguments;
  9143. return this.finishNode(node, "TSInstantiationExpression");
  9144. });
  9145. if (missingParenErrorLoc) {
  9146. this.unexpected(missingParenErrorLoc, 10);
  9147. }
  9148. if (result) {
  9149. if (result.type === "TSInstantiationExpression" && (this.match(16) || this.match(18) && this.lookaheadCharCode() !== 40)) {
  9150. this.raise(TSErrors.InvalidPropertyAccessAfterInstantiationExpression, {
  9151. at: this.state.startLoc
  9152. });
  9153. }
  9154. return result;
  9155. }
  9156. }
  9157. return super.parseSubscript(base, startLoc, noCalls, state);
  9158. }
  9159. parseNewCallee(node) {
  9160. var _callee$extra;
  9161. super.parseNewCallee(node);
  9162. const {
  9163. callee
  9164. } = node;
  9165. if (callee.type === "TSInstantiationExpression" && !((_callee$extra = callee.extra) != null && _callee$extra.parenthesized)) {
  9166. node.typeParameters = callee.typeParameters;
  9167. node.callee = callee.expression;
  9168. }
  9169. }
  9170. parseExprOp(left, leftStartLoc, minPrec) {
  9171. let isSatisfies;
  9172. if (tokenOperatorPrecedence(58) > minPrec && !this.hasPrecedingLineBreak() && (this.isContextual(93) || (isSatisfies = this.isContextual(118)))) {
  9173. const node = this.startNodeAt(leftStartLoc);
  9174. node.expression = left;
  9175. node.typeAnnotation = this.tsInType(() => {
  9176. this.next();
  9177. if (this.match(75)) {
  9178. if (isSatisfies) {
  9179. this.raise(Errors.UnexpectedKeyword, {
  9180. at: this.state.startLoc,
  9181. keyword: "const"
  9182. });
  9183. }
  9184. return this.tsParseTypeReference();
  9185. }
  9186. return this.tsParseType();
  9187. });
  9188. this.finishNode(node, isSatisfies ? "TSSatisfiesExpression" : "TSAsExpression");
  9189. this.reScan_lt_gt();
  9190. return this.parseExprOp(node, leftStartLoc, minPrec);
  9191. }
  9192. return super.parseExprOp(left, leftStartLoc, minPrec);
  9193. }
  9194. checkReservedWord(word, startLoc, checkKeywords, isBinding) {
  9195. if (!this.state.isAmbientContext) {
  9196. super.checkReservedWord(word, startLoc, checkKeywords, isBinding);
  9197. }
  9198. }
  9199. checkImportReflection(node) {
  9200. super.checkImportReflection(node);
  9201. if (node.module && node.importKind !== "value") {
  9202. this.raise(TSErrors.ImportReflectionHasImportType, {
  9203. at: node.specifiers[0].loc.start
  9204. });
  9205. }
  9206. }
  9207. checkDuplicateExports() {}
  9208. parseImport(node) {
  9209. node.importKind = "value";
  9210. if (tokenIsIdentifier(this.state.type) || this.match(55) || this.match(5)) {
  9211. let ahead = this.lookahead();
  9212. if (this.isContextual(128) && ahead.type !== 12 && ahead.type !== 97 && ahead.type !== 29) {
  9213. node.importKind = "type";
  9214. this.next();
  9215. ahead = this.lookahead();
  9216. }
  9217. if (tokenIsIdentifier(this.state.type) && ahead.type === 29) {
  9218. return this.tsParseImportEqualsDeclaration(node);
  9219. }
  9220. }
  9221. const importNode = super.parseImport(node);
  9222. if (importNode.importKind === "type" && importNode.specifiers.length > 1 && importNode.specifiers[0].type === "ImportDefaultSpecifier") {
  9223. this.raise(TSErrors.TypeImportCannotSpecifyDefaultAndNamed, {
  9224. at: importNode
  9225. });
  9226. }
  9227. return importNode;
  9228. }
  9229. parseExport(node, decorators) {
  9230. if (this.match(83)) {
  9231. this.next();
  9232. if (this.isContextual(128) && this.lookaheadCharCode() !== 61) {
  9233. node.importKind = "type";
  9234. this.next();
  9235. } else {
  9236. node.importKind = "value";
  9237. }
  9238. return this.tsParseImportEqualsDeclaration(node, true);
  9239. } else if (this.eat(29)) {
  9240. const assign = node;
  9241. assign.expression = super.parseExpression();
  9242. this.semicolon();
  9243. return this.finishNode(assign, "TSExportAssignment");
  9244. } else if (this.eatContextual(93)) {
  9245. const decl = node;
  9246. this.expectContextual(126);
  9247. decl.id = this.parseIdentifier();
  9248. this.semicolon();
  9249. return this.finishNode(decl, "TSNamespaceExportDeclaration");
  9250. } else {
  9251. node.exportKind = "value";
  9252. if (this.isContextual(128)) {
  9253. const ch = this.lookaheadCharCode();
  9254. if (ch === 123 || ch === 42) {
  9255. this.next();
  9256. node.exportKind = "type";
  9257. }
  9258. }
  9259. return super.parseExport(node, decorators);
  9260. }
  9261. }
  9262. isAbstractClass() {
  9263. return this.isContextual(122) && this.lookahead().type === 80;
  9264. }
  9265. parseExportDefaultExpression() {
  9266. if (this.isAbstractClass()) {
  9267. const cls = this.startNode();
  9268. this.next();
  9269. cls.abstract = true;
  9270. return this.parseClass(cls, true, true);
  9271. }
  9272. if (this.match(127)) {
  9273. const result = this.tsParseInterfaceDeclaration(this.startNode());
  9274. if (result) return result;
  9275. }
  9276. return super.parseExportDefaultExpression();
  9277. }
  9278. parseVarStatement(node, kind, allowMissingInitializer = false) {
  9279. const {
  9280. isAmbientContext
  9281. } = this.state;
  9282. const declaration = super.parseVarStatement(node, kind, allowMissingInitializer || isAmbientContext);
  9283. if (!isAmbientContext) return declaration;
  9284. for (const {
  9285. id,
  9286. init
  9287. } of declaration.declarations) {
  9288. if (!init) continue;
  9289. if (kind !== "const" || !!id.typeAnnotation) {
  9290. this.raise(TSErrors.InitializerNotAllowedInAmbientContext, {
  9291. at: init
  9292. });
  9293. } else if (!isValidAmbientConstInitializer(init, this.hasPlugin("estree"))) {
  9294. this.raise(TSErrors.ConstInitiailizerMustBeStringOrNumericLiteralOrLiteralEnumReference, {
  9295. at: init
  9296. });
  9297. }
  9298. }
  9299. return declaration;
  9300. }
  9301. parseStatementContent(flags, decorators) {
  9302. if (this.match(75) && this.isLookaheadContextual("enum")) {
  9303. const node = this.startNode();
  9304. this.expect(75);
  9305. return this.tsParseEnumDeclaration(node, {
  9306. const: true
  9307. });
  9308. }
  9309. if (this.isContextual(124)) {
  9310. return this.tsParseEnumDeclaration(this.startNode());
  9311. }
  9312. if (this.isContextual(127)) {
  9313. const result = this.tsParseInterfaceDeclaration(this.startNode());
  9314. if (result) return result;
  9315. }
  9316. return super.parseStatementContent(flags, decorators);
  9317. }
  9318. parseAccessModifier() {
  9319. return this.tsParseModifier(["public", "protected", "private"]);
  9320. }
  9321. tsHasSomeModifiers(member, modifiers) {
  9322. return modifiers.some(modifier => {
  9323. if (tsIsAccessModifier(modifier)) {
  9324. return member.accessibility === modifier;
  9325. }
  9326. return !!member[modifier];
  9327. });
  9328. }
  9329. tsIsStartOfStaticBlocks() {
  9330. return this.isContextual(104) && this.lookaheadCharCode() === 123;
  9331. }
  9332. parseClassMember(classBody, member, state) {
  9333. const modifiers = ["declare", "private", "public", "protected", "override", "abstract", "readonly", "static"];
  9334. this.tsParseModifiers({
  9335. allowedModifiers: modifiers,
  9336. disallowedModifiers: ["in", "out"],
  9337. stopOnStartOfClassStaticBlock: true,
  9338. errorTemplate: TSErrors.InvalidModifierOnTypeParameterPositions
  9339. }, member);
  9340. const callParseClassMemberWithIsStatic = () => {
  9341. if (this.tsIsStartOfStaticBlocks()) {
  9342. this.next();
  9343. this.next();
  9344. if (this.tsHasSomeModifiers(member, modifiers)) {
  9345. this.raise(TSErrors.StaticBlockCannotHaveModifier, {
  9346. at: this.state.curPosition()
  9347. });
  9348. }
  9349. super.parseClassStaticBlock(classBody, member);
  9350. } else {
  9351. this.parseClassMemberWithIsStatic(classBody, member, state, !!member.static);
  9352. }
  9353. };
  9354. if (member.declare) {
  9355. this.tsInAmbientContext(callParseClassMemberWithIsStatic);
  9356. } else {
  9357. callParseClassMemberWithIsStatic();
  9358. }
  9359. }
  9360. parseClassMemberWithIsStatic(classBody, member, state, isStatic) {
  9361. const idx = this.tsTryParseIndexSignature(member);
  9362. if (idx) {
  9363. classBody.body.push(idx);
  9364. if (member.abstract) {
  9365. this.raise(TSErrors.IndexSignatureHasAbstract, {
  9366. at: member
  9367. });
  9368. }
  9369. if (member.accessibility) {
  9370. this.raise(TSErrors.IndexSignatureHasAccessibility, {
  9371. at: member,
  9372. modifier: member.accessibility
  9373. });
  9374. }
  9375. if (member.declare) {
  9376. this.raise(TSErrors.IndexSignatureHasDeclare, {
  9377. at: member
  9378. });
  9379. }
  9380. if (member.override) {
  9381. this.raise(TSErrors.IndexSignatureHasOverride, {
  9382. at: member
  9383. });
  9384. }
  9385. return;
  9386. }
  9387. if (!this.state.inAbstractClass && member.abstract) {
  9388. this.raise(TSErrors.NonAbstractClassHasAbstractMethod, {
  9389. at: member
  9390. });
  9391. }
  9392. if (member.override) {
  9393. if (!state.hadSuperClass) {
  9394. this.raise(TSErrors.OverrideNotInSubClass, {
  9395. at: member
  9396. });
  9397. }
  9398. }
  9399. super.parseClassMemberWithIsStatic(classBody, member, state, isStatic);
  9400. }
  9401. parsePostMemberNameModifiers(methodOrProp) {
  9402. const optional = this.eat(17);
  9403. if (optional) methodOrProp.optional = true;
  9404. if (methodOrProp.readonly && this.match(10)) {
  9405. this.raise(TSErrors.ClassMethodHasReadonly, {
  9406. at: methodOrProp
  9407. });
  9408. }
  9409. if (methodOrProp.declare && this.match(10)) {
  9410. this.raise(TSErrors.ClassMethodHasDeclare, {
  9411. at: methodOrProp
  9412. });
  9413. }
  9414. }
  9415. parseExpressionStatement(node, expr, decorators) {
  9416. const decl = expr.type === "Identifier" ? this.tsParseExpressionStatement(node, expr, decorators) : undefined;
  9417. return decl || super.parseExpressionStatement(node, expr, decorators);
  9418. }
  9419. shouldParseExportDeclaration() {
  9420. if (this.tsIsDeclarationStart()) return true;
  9421. return super.shouldParseExportDeclaration();
  9422. }
  9423. parseConditional(expr, startLoc, refExpressionErrors) {
  9424. if (!this.state.maybeInArrowParameters || !this.match(17)) {
  9425. return super.parseConditional(expr, startLoc, refExpressionErrors);
  9426. }
  9427. const result = this.tryParse(() => super.parseConditional(expr, startLoc));
  9428. if (!result.node) {
  9429. if (result.error) {
  9430. super.setOptionalParametersError(refExpressionErrors, result.error);
  9431. }
  9432. return expr;
  9433. }
  9434. if (result.error) this.state = result.failState;
  9435. return result.node;
  9436. }
  9437. parseParenItem(node, startLoc) {
  9438. node = super.parseParenItem(node, startLoc);
  9439. if (this.eat(17)) {
  9440. node.optional = true;
  9441. this.resetEndLocation(node);
  9442. }
  9443. if (this.match(14)) {
  9444. const typeCastNode = this.startNodeAt(startLoc);
  9445. typeCastNode.expression = node;
  9446. typeCastNode.typeAnnotation = this.tsParseTypeAnnotation();
  9447. return this.finishNode(typeCastNode, "TSTypeCastExpression");
  9448. }
  9449. return node;
  9450. }
  9451. parseExportDeclaration(node) {
  9452. if (!this.state.isAmbientContext && this.isContextual(123)) {
  9453. return this.tsInAmbientContext(() => this.parseExportDeclaration(node));
  9454. }
  9455. const startLoc = this.state.startLoc;
  9456. const isDeclare = this.eatContextual(123);
  9457. if (isDeclare && (this.isContextual(123) || !this.shouldParseExportDeclaration())) {
  9458. throw this.raise(TSErrors.ExpectedAmbientAfterExportDeclare, {
  9459. at: this.state.startLoc
  9460. });
  9461. }
  9462. const isIdentifier = tokenIsIdentifier(this.state.type);
  9463. const declaration = isIdentifier && this.tsTryParseExportDeclaration() || super.parseExportDeclaration(node);
  9464. if (!declaration) return null;
  9465. if (declaration.type === "TSInterfaceDeclaration" || declaration.type === "TSTypeAliasDeclaration" || isDeclare) {
  9466. node.exportKind = "type";
  9467. }
  9468. if (isDeclare) {
  9469. this.resetStartLocation(declaration, startLoc);
  9470. declaration.declare = true;
  9471. }
  9472. return declaration;
  9473. }
  9474. parseClassId(node, isStatement, optionalId, bindingType) {
  9475. if ((!isStatement || optionalId) && this.isContextual(111)) {
  9476. return;
  9477. }
  9478. super.parseClassId(node, isStatement, optionalId, node.declare ? BIND_TS_AMBIENT : BIND_CLASS);
  9479. const typeParameters = this.tsTryParseTypeParameters(this.tsParseInOutConstModifiers);
  9480. if (typeParameters) node.typeParameters = typeParameters;
  9481. }
  9482. parseClassPropertyAnnotation(node) {
  9483. if (!node.optional) {
  9484. if (this.eat(35)) {
  9485. node.definite = true;
  9486. } else if (this.eat(17)) {
  9487. node.optional = true;
  9488. }
  9489. }
  9490. const type = this.tsTryParseTypeAnnotation();
  9491. if (type) node.typeAnnotation = type;
  9492. }
  9493. parseClassProperty(node) {
  9494. this.parseClassPropertyAnnotation(node);
  9495. if (this.state.isAmbientContext && !(node.readonly && !node.typeAnnotation) && this.match(29)) {
  9496. this.raise(TSErrors.DeclareClassFieldHasInitializer, {
  9497. at: this.state.startLoc
  9498. });
  9499. }
  9500. if (node.abstract && this.match(29)) {
  9501. const {
  9502. key
  9503. } = node;
  9504. this.raise(TSErrors.AbstractPropertyHasInitializer, {
  9505. at: this.state.startLoc,
  9506. propertyName: key.type === "Identifier" && !node.computed ? key.name : `[${this.input.slice(key.start, key.end)}]`
  9507. });
  9508. }
  9509. return super.parseClassProperty(node);
  9510. }
  9511. parseClassPrivateProperty(node) {
  9512. if (node.abstract) {
  9513. this.raise(TSErrors.PrivateElementHasAbstract, {
  9514. at: node
  9515. });
  9516. }
  9517. if (node.accessibility) {
  9518. this.raise(TSErrors.PrivateElementHasAccessibility, {
  9519. at: node,
  9520. modifier: node.accessibility
  9521. });
  9522. }
  9523. this.parseClassPropertyAnnotation(node);
  9524. return super.parseClassPrivateProperty(node);
  9525. }
  9526. parseClassAccessorProperty(node) {
  9527. this.parseClassPropertyAnnotation(node);
  9528. if (node.optional) {
  9529. this.raise(TSErrors.AccessorCannotBeOptional, {
  9530. at: node
  9531. });
  9532. }
  9533. return super.parseClassAccessorProperty(node);
  9534. }
  9535. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  9536. const typeParameters = this.tsTryParseTypeParameters(this.tsParseConstModifier);
  9537. if (typeParameters && isConstructor) {
  9538. this.raise(TSErrors.ConstructorHasTypeParameters, {
  9539. at: typeParameters
  9540. });
  9541. }
  9542. const {
  9543. declare = false,
  9544. kind
  9545. } = method;
  9546. if (declare && (kind === "get" || kind === "set")) {
  9547. this.raise(TSErrors.DeclareAccessor, {
  9548. at: method,
  9549. kind
  9550. });
  9551. }
  9552. if (typeParameters) method.typeParameters = typeParameters;
  9553. super.pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper);
  9554. }
  9555. pushClassPrivateMethod(classBody, method, isGenerator, isAsync) {
  9556. const typeParameters = this.tsTryParseTypeParameters(this.tsParseConstModifier);
  9557. if (typeParameters) method.typeParameters = typeParameters;
  9558. super.pushClassPrivateMethod(classBody, method, isGenerator, isAsync);
  9559. }
  9560. declareClassPrivateMethodInScope(node, kind) {
  9561. if (node.type === "TSDeclareMethod") return;
  9562. if (node.type === "MethodDefinition" && !node.value.body) return;
  9563. super.declareClassPrivateMethodInScope(node, kind);
  9564. }
  9565. parseClassSuper(node) {
  9566. super.parseClassSuper(node);
  9567. if (node.superClass && (this.match(47) || this.match(51))) {
  9568. node.superTypeParameters = this.tsParseTypeArgumentsInExpression();
  9569. }
  9570. if (this.eatContextual(111)) {
  9571. node.implements = this.tsParseHeritageClause("implements");
  9572. }
  9573. }
  9574. parseObjPropValue(prop, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors) {
  9575. const typeParameters = this.tsTryParseTypeParameters(this.tsParseConstModifier);
  9576. if (typeParameters) prop.typeParameters = typeParameters;
  9577. return super.parseObjPropValue(prop, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors);
  9578. }
  9579. parseFunctionParams(node, isConstructor) {
  9580. const typeParameters = this.tsTryParseTypeParameters(this.tsParseConstModifier);
  9581. if (typeParameters) node.typeParameters = typeParameters;
  9582. super.parseFunctionParams(node, isConstructor);
  9583. }
  9584. parseVarId(decl, kind) {
  9585. super.parseVarId(decl, kind);
  9586. if (decl.id.type === "Identifier" && !this.hasPrecedingLineBreak() && this.eat(35)) {
  9587. decl.definite = true;
  9588. }
  9589. const type = this.tsTryParseTypeAnnotation();
  9590. if (type) {
  9591. decl.id.typeAnnotation = type;
  9592. this.resetEndLocation(decl.id);
  9593. }
  9594. }
  9595. parseAsyncArrowFromCallExpression(node, call) {
  9596. if (this.match(14)) {
  9597. node.returnType = this.tsParseTypeAnnotation();
  9598. }
  9599. return super.parseAsyncArrowFromCallExpression(node, call);
  9600. }
  9601. parseMaybeAssign(refExpressionErrors, afterLeftParse) {
  9602. var _jsx, _jsx2, _typeCast, _jsx3, _typeCast2, _jsx4, _typeCast3;
  9603. let state;
  9604. let jsx;
  9605. let typeCast;
  9606. if (this.hasPlugin("jsx") && (this.match(140) || this.match(47))) {
  9607. state = this.state.clone();
  9608. jsx = this.tryParse(() => super.parseMaybeAssign(refExpressionErrors, afterLeftParse), state);
  9609. if (!jsx.error) return jsx.node;
  9610. const {
  9611. context
  9612. } = this.state;
  9613. const currentContext = context[context.length - 1];
  9614. if (currentContext === types.j_oTag || currentContext === types.j_expr) {
  9615. context.pop();
  9616. }
  9617. }
  9618. if (!((_jsx = jsx) != null && _jsx.error) && !this.match(47)) {
  9619. return super.parseMaybeAssign(refExpressionErrors, afterLeftParse);
  9620. }
  9621. if (!state || state === this.state) state = this.state.clone();
  9622. let typeParameters;
  9623. const arrow = this.tryParse(abort => {
  9624. var _expr$extra, _typeParameters;
  9625. typeParameters = this.tsParseTypeParameters(this.tsParseConstModifier);
  9626. const expr = super.parseMaybeAssign(refExpressionErrors, afterLeftParse);
  9627. if (expr.type !== "ArrowFunctionExpression" || (_expr$extra = expr.extra) != null && _expr$extra.parenthesized) {
  9628. abort();
  9629. }
  9630. if (((_typeParameters = typeParameters) == null ? void 0 : _typeParameters.params.length) !== 0) {
  9631. this.resetStartLocationFromNode(expr, typeParameters);
  9632. }
  9633. expr.typeParameters = typeParameters;
  9634. return expr;
  9635. }, state);
  9636. if (!arrow.error && !arrow.aborted) {
  9637. if (typeParameters) this.reportReservedArrowTypeParam(typeParameters);
  9638. return arrow.node;
  9639. }
  9640. if (!jsx) {
  9641. assert(!this.hasPlugin("jsx"));
  9642. typeCast = this.tryParse(() => super.parseMaybeAssign(refExpressionErrors, afterLeftParse), state);
  9643. if (!typeCast.error) return typeCast.node;
  9644. }
  9645. if ((_jsx2 = jsx) != null && _jsx2.node) {
  9646. this.state = jsx.failState;
  9647. return jsx.node;
  9648. }
  9649. if (arrow.node) {
  9650. this.state = arrow.failState;
  9651. if (typeParameters) this.reportReservedArrowTypeParam(typeParameters);
  9652. return arrow.node;
  9653. }
  9654. if ((_typeCast = typeCast) != null && _typeCast.node) {
  9655. this.state = typeCast.failState;
  9656. return typeCast.node;
  9657. }
  9658. if ((_jsx3 = jsx) != null && _jsx3.thrown) throw jsx.error;
  9659. if (arrow.thrown) throw arrow.error;
  9660. if ((_typeCast2 = typeCast) != null && _typeCast2.thrown) throw typeCast.error;
  9661. throw ((_jsx4 = jsx) == null ? void 0 : _jsx4.error) || arrow.error || ((_typeCast3 = typeCast) == null ? void 0 : _typeCast3.error);
  9662. }
  9663. reportReservedArrowTypeParam(node) {
  9664. var _node$extra;
  9665. if (node.params.length === 1 && !node.params[0].constraint && !((_node$extra = node.extra) != null && _node$extra.trailingComma) && this.getPluginOption("typescript", "disallowAmbiguousJSXLike")) {
  9666. this.raise(TSErrors.ReservedArrowTypeParam, {
  9667. at: node
  9668. });
  9669. }
  9670. }
  9671. parseMaybeUnary(refExpressionErrors, sawUnary) {
  9672. if (!this.hasPlugin("jsx") && this.match(47)) {
  9673. return this.tsParseTypeAssertion();
  9674. } else {
  9675. return super.parseMaybeUnary(refExpressionErrors, sawUnary);
  9676. }
  9677. }
  9678. parseArrow(node) {
  9679. if (this.match(14)) {
  9680. const result = this.tryParse(abort => {
  9681. const returnType = this.tsParseTypeOrTypePredicateAnnotation(14);
  9682. if (this.canInsertSemicolon() || !this.match(19)) abort();
  9683. return returnType;
  9684. });
  9685. if (result.aborted) return;
  9686. if (!result.thrown) {
  9687. if (result.error) this.state = result.failState;
  9688. node.returnType = result.node;
  9689. }
  9690. }
  9691. return super.parseArrow(node);
  9692. }
  9693. parseAssignableListItemTypes(param, flags) {
  9694. if (!(flags & ParseBindingListFlags.IS_FUNCTION_PARAMS)) return param;
  9695. if (this.eat(17)) {
  9696. param.optional = true;
  9697. }
  9698. const type = this.tsTryParseTypeAnnotation();
  9699. if (type) param.typeAnnotation = type;
  9700. this.resetEndLocation(param);
  9701. return param;
  9702. }
  9703. isAssignable(node, isBinding) {
  9704. switch (node.type) {
  9705. case "TSTypeCastExpression":
  9706. return this.isAssignable(node.expression, isBinding);
  9707. case "TSParameterProperty":
  9708. return true;
  9709. default:
  9710. return super.isAssignable(node, isBinding);
  9711. }
  9712. }
  9713. toAssignable(node, isLHS = false) {
  9714. switch (node.type) {
  9715. case "ParenthesizedExpression":
  9716. this.toAssignableParenthesizedExpression(node, isLHS);
  9717. break;
  9718. case "TSAsExpression":
  9719. case "TSSatisfiesExpression":
  9720. case "TSNonNullExpression":
  9721. case "TSTypeAssertion":
  9722. if (isLHS) {
  9723. this.expressionScope.recordArrowParameterBindingError(TSErrors.UnexpectedTypeCastInParameter, {
  9724. at: node
  9725. });
  9726. } else {
  9727. this.raise(TSErrors.UnexpectedTypeCastInParameter, {
  9728. at: node
  9729. });
  9730. }
  9731. this.toAssignable(node.expression, isLHS);
  9732. break;
  9733. case "AssignmentExpression":
  9734. if (!isLHS && node.left.type === "TSTypeCastExpression") {
  9735. node.left = this.typeCastToParameter(node.left);
  9736. }
  9737. default:
  9738. super.toAssignable(node, isLHS);
  9739. }
  9740. }
  9741. toAssignableParenthesizedExpression(node, isLHS) {
  9742. switch (node.expression.type) {
  9743. case "TSAsExpression":
  9744. case "TSSatisfiesExpression":
  9745. case "TSNonNullExpression":
  9746. case "TSTypeAssertion":
  9747. case "ParenthesizedExpression":
  9748. this.toAssignable(node.expression, isLHS);
  9749. break;
  9750. default:
  9751. super.toAssignable(node, isLHS);
  9752. }
  9753. }
  9754. checkToRestConversion(node, allowPattern) {
  9755. switch (node.type) {
  9756. case "TSAsExpression":
  9757. case "TSSatisfiesExpression":
  9758. case "TSTypeAssertion":
  9759. case "TSNonNullExpression":
  9760. this.checkToRestConversion(node.expression, false);
  9761. break;
  9762. default:
  9763. super.checkToRestConversion(node, allowPattern);
  9764. }
  9765. }
  9766. isValidLVal(type, isUnparenthesizedInAssign, binding) {
  9767. return getOwn({
  9768. TSTypeCastExpression: true,
  9769. TSParameterProperty: "parameter",
  9770. TSNonNullExpression: "expression",
  9771. TSAsExpression: (binding !== BIND_NONE || !isUnparenthesizedInAssign) && ["expression", true],
  9772. TSSatisfiesExpression: (binding !== BIND_NONE || !isUnparenthesizedInAssign) && ["expression", true],
  9773. TSTypeAssertion: (binding !== BIND_NONE || !isUnparenthesizedInAssign) && ["expression", true]
  9774. }, type) || super.isValidLVal(type, isUnparenthesizedInAssign, binding);
  9775. }
  9776. parseBindingAtom() {
  9777. switch (this.state.type) {
  9778. case 78:
  9779. return this.parseIdentifier(true);
  9780. default:
  9781. return super.parseBindingAtom();
  9782. }
  9783. }
  9784. parseMaybeDecoratorArguments(expr) {
  9785. if (this.match(47) || this.match(51)) {
  9786. const typeArguments = this.tsParseTypeArgumentsInExpression();
  9787. if (this.match(10)) {
  9788. const call = super.parseMaybeDecoratorArguments(expr);
  9789. call.typeParameters = typeArguments;
  9790. return call;
  9791. }
  9792. this.unexpected(null, 10);
  9793. }
  9794. return super.parseMaybeDecoratorArguments(expr);
  9795. }
  9796. checkCommaAfterRest(close) {
  9797. if (this.state.isAmbientContext && this.match(12) && this.lookaheadCharCode() === close) {
  9798. this.next();
  9799. return false;
  9800. } else {
  9801. return super.checkCommaAfterRest(close);
  9802. }
  9803. }
  9804. isClassMethod() {
  9805. return this.match(47) || super.isClassMethod();
  9806. }
  9807. isClassProperty() {
  9808. return this.match(35) || this.match(14) || super.isClassProperty();
  9809. }
  9810. parseMaybeDefault(startLoc, left) {
  9811. const node = super.parseMaybeDefault(startLoc, left);
  9812. if (node.type === "AssignmentPattern" && node.typeAnnotation && node.right.start < node.typeAnnotation.start) {
  9813. this.raise(TSErrors.TypeAnnotationAfterAssign, {
  9814. at: node.typeAnnotation
  9815. });
  9816. }
  9817. return node;
  9818. }
  9819. getTokenFromCode(code) {
  9820. if (this.state.inType) {
  9821. if (code === 62) {
  9822. return this.finishOp(48, 1);
  9823. }
  9824. if (code === 60) {
  9825. return this.finishOp(47, 1);
  9826. }
  9827. }
  9828. return super.getTokenFromCode(code);
  9829. }
  9830. reScan_lt_gt() {
  9831. const {
  9832. type
  9833. } = this.state;
  9834. if (type === 47) {
  9835. this.state.pos -= 1;
  9836. this.readToken_lt();
  9837. } else if (type === 48) {
  9838. this.state.pos -= 1;
  9839. this.readToken_gt();
  9840. }
  9841. }
  9842. reScan_lt() {
  9843. const {
  9844. type
  9845. } = this.state;
  9846. if (type === 51) {
  9847. this.state.pos -= 2;
  9848. this.finishOp(47, 1);
  9849. return 47;
  9850. }
  9851. return type;
  9852. }
  9853. toAssignableList(exprList, trailingCommaLoc, isLHS) {
  9854. for (let i = 0; i < exprList.length; i++) {
  9855. const expr = exprList[i];
  9856. if ((expr == null ? void 0 : expr.type) === "TSTypeCastExpression") {
  9857. exprList[i] = this.typeCastToParameter(expr);
  9858. }
  9859. }
  9860. super.toAssignableList(exprList, trailingCommaLoc, isLHS);
  9861. }
  9862. typeCastToParameter(node) {
  9863. node.expression.typeAnnotation = node.typeAnnotation;
  9864. this.resetEndLocation(node.expression, node.typeAnnotation.loc.end);
  9865. return node.expression;
  9866. }
  9867. shouldParseArrow(params) {
  9868. if (this.match(14)) {
  9869. return params.every(expr => this.isAssignable(expr, true));
  9870. }
  9871. return super.shouldParseArrow(params);
  9872. }
  9873. shouldParseAsyncArrow() {
  9874. return this.match(14) || super.shouldParseAsyncArrow();
  9875. }
  9876. canHaveLeadingDecorator() {
  9877. return super.canHaveLeadingDecorator() || this.isAbstractClass();
  9878. }
  9879. jsxParseOpeningElementAfterName(node) {
  9880. if (this.match(47) || this.match(51)) {
  9881. const typeArguments = this.tsTryParseAndCatch(() => this.tsParseTypeArgumentsInExpression());
  9882. if (typeArguments) node.typeParameters = typeArguments;
  9883. }
  9884. return super.jsxParseOpeningElementAfterName(node);
  9885. }
  9886. getGetterSetterExpectedParamCount(method) {
  9887. const baseCount = super.getGetterSetterExpectedParamCount(method);
  9888. const params = this.getObjectOrClassMethodParams(method);
  9889. const firstParam = params[0];
  9890. const hasContextParam = firstParam && this.isThisParam(firstParam);
  9891. return hasContextParam ? baseCount + 1 : baseCount;
  9892. }
  9893. parseCatchClauseParam() {
  9894. const param = super.parseCatchClauseParam();
  9895. const type = this.tsTryParseTypeAnnotation();
  9896. if (type) {
  9897. param.typeAnnotation = type;
  9898. this.resetEndLocation(param);
  9899. }
  9900. return param;
  9901. }
  9902. tsInAmbientContext(cb) {
  9903. const oldIsAmbientContext = this.state.isAmbientContext;
  9904. this.state.isAmbientContext = true;
  9905. try {
  9906. return cb();
  9907. } finally {
  9908. this.state.isAmbientContext = oldIsAmbientContext;
  9909. }
  9910. }
  9911. parseClass(node, isStatement, optionalId) {
  9912. const oldInAbstractClass = this.state.inAbstractClass;
  9913. this.state.inAbstractClass = !!node.abstract;
  9914. try {
  9915. return super.parseClass(node, isStatement, optionalId);
  9916. } finally {
  9917. this.state.inAbstractClass = oldInAbstractClass;
  9918. }
  9919. }
  9920. tsParseAbstractDeclaration(node, decorators) {
  9921. if (this.match(80)) {
  9922. node.abstract = true;
  9923. return this.maybeTakeDecorators(decorators, this.parseClass(node, true, false));
  9924. } else if (this.isContextual(127)) {
  9925. if (!this.hasFollowingLineBreak()) {
  9926. node.abstract = true;
  9927. this.raise(TSErrors.NonClassMethodPropertyHasAbstractModifer, {
  9928. at: node
  9929. });
  9930. return this.tsParseInterfaceDeclaration(node);
  9931. }
  9932. } else {
  9933. this.unexpected(null, 80);
  9934. }
  9935. }
  9936. parseMethod(node, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope) {
  9937. const method = super.parseMethod(node, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope);
  9938. if (method.abstract) {
  9939. const hasBody = this.hasPlugin("estree") ? !!method.value.body : !!method.body;
  9940. if (hasBody) {
  9941. const {
  9942. key
  9943. } = method;
  9944. this.raise(TSErrors.AbstractMethodHasImplementation, {
  9945. at: method,
  9946. methodName: key.type === "Identifier" && !method.computed ? key.name : `[${this.input.slice(key.start, key.end)}]`
  9947. });
  9948. }
  9949. }
  9950. return method;
  9951. }
  9952. tsParseTypeParameterName() {
  9953. const typeName = this.parseIdentifier();
  9954. return typeName.name;
  9955. }
  9956. shouldParseAsAmbientContext() {
  9957. return !!this.getPluginOption("typescript", "dts");
  9958. }
  9959. parse() {
  9960. if (this.shouldParseAsAmbientContext()) {
  9961. this.state.isAmbientContext = true;
  9962. }
  9963. return super.parse();
  9964. }
  9965. getExpression() {
  9966. if (this.shouldParseAsAmbientContext()) {
  9967. this.state.isAmbientContext = true;
  9968. }
  9969. return super.getExpression();
  9970. }
  9971. parseExportSpecifier(node, isString, isInTypeExport, isMaybeTypeOnly) {
  9972. if (!isString && isMaybeTypeOnly) {
  9973. this.parseTypeOnlyImportExportSpecifier(node, false, isInTypeExport);
  9974. return this.finishNode(node, "ExportSpecifier");
  9975. }
  9976. node.exportKind = "value";
  9977. return super.parseExportSpecifier(node, isString, isInTypeExport, isMaybeTypeOnly);
  9978. }
  9979. parseImportSpecifier(specifier, importedIsString, isInTypeOnlyImport, isMaybeTypeOnly, bindingType) {
  9980. if (!importedIsString && isMaybeTypeOnly) {
  9981. this.parseTypeOnlyImportExportSpecifier(specifier, true, isInTypeOnlyImport);
  9982. return this.finishNode(specifier, "ImportSpecifier");
  9983. }
  9984. specifier.importKind = "value";
  9985. return super.parseImportSpecifier(specifier, importedIsString, isInTypeOnlyImport, isMaybeTypeOnly, isInTypeOnlyImport ? BIND_TS_TYPE_IMPORT : BIND_FLAGS_TS_IMPORT);
  9986. }
  9987. parseTypeOnlyImportExportSpecifier(node, isImport, isInTypeOnlyImportExport) {
  9988. const leftOfAsKey = isImport ? "imported" : "local";
  9989. const rightOfAsKey = isImport ? "local" : "exported";
  9990. let leftOfAs = node[leftOfAsKey];
  9991. let rightOfAs;
  9992. let hasTypeSpecifier = false;
  9993. let canParseAsKeyword = true;
  9994. const loc = leftOfAs.loc.start;
  9995. if (this.isContextual(93)) {
  9996. const firstAs = this.parseIdentifier();
  9997. if (this.isContextual(93)) {
  9998. const secondAs = this.parseIdentifier();
  9999. if (tokenIsKeywordOrIdentifier(this.state.type)) {
  10000. hasTypeSpecifier = true;
  10001. leftOfAs = firstAs;
  10002. rightOfAs = isImport ? this.parseIdentifier() : this.parseModuleExportName();
  10003. canParseAsKeyword = false;
  10004. } else {
  10005. rightOfAs = secondAs;
  10006. canParseAsKeyword = false;
  10007. }
  10008. } else if (tokenIsKeywordOrIdentifier(this.state.type)) {
  10009. canParseAsKeyword = false;
  10010. rightOfAs = isImport ? this.parseIdentifier() : this.parseModuleExportName();
  10011. } else {
  10012. hasTypeSpecifier = true;
  10013. leftOfAs = firstAs;
  10014. }
  10015. } else if (tokenIsKeywordOrIdentifier(this.state.type)) {
  10016. hasTypeSpecifier = true;
  10017. if (isImport) {
  10018. leftOfAs = this.parseIdentifier(true);
  10019. if (!this.isContextual(93)) {
  10020. this.checkReservedWord(leftOfAs.name, leftOfAs.loc.start, true, true);
  10021. }
  10022. } else {
  10023. leftOfAs = this.parseModuleExportName();
  10024. }
  10025. }
  10026. if (hasTypeSpecifier && isInTypeOnlyImportExport) {
  10027. this.raise(isImport ? TSErrors.TypeModifierIsUsedInTypeImports : TSErrors.TypeModifierIsUsedInTypeExports, {
  10028. at: loc
  10029. });
  10030. }
  10031. node[leftOfAsKey] = leftOfAs;
  10032. node[rightOfAsKey] = rightOfAs;
  10033. const kindKey = isImport ? "importKind" : "exportKind";
  10034. node[kindKey] = hasTypeSpecifier ? "type" : "value";
  10035. if (canParseAsKeyword && this.eatContextual(93)) {
  10036. node[rightOfAsKey] = isImport ? this.parseIdentifier() : this.parseModuleExportName();
  10037. }
  10038. if (!node[rightOfAsKey]) {
  10039. node[rightOfAsKey] = cloneIdentifier(node[leftOfAsKey]);
  10040. }
  10041. if (isImport) {
  10042. this.checkIdentifier(node[rightOfAsKey], hasTypeSpecifier ? BIND_TS_TYPE_IMPORT : BIND_FLAGS_TS_IMPORT);
  10043. }
  10044. }
  10045. });
  10046. function isPossiblyLiteralEnum(expression) {
  10047. if (expression.type !== "MemberExpression") return false;
  10048. const {
  10049. computed,
  10050. property
  10051. } = expression;
  10052. if (computed && property.type !== "StringLiteral" && (property.type !== "TemplateLiteral" || property.expressions.length > 0)) {
  10053. return false;
  10054. }
  10055. return isUncomputedMemberExpressionChain(expression.object);
  10056. }
  10057. function isValidAmbientConstInitializer(expression, estree) {
  10058. var _expression$extra;
  10059. const {
  10060. type
  10061. } = expression;
  10062. if ((_expression$extra = expression.extra) != null && _expression$extra.parenthesized) {
  10063. return false;
  10064. }
  10065. if (estree) {
  10066. if (type === "Literal") {
  10067. const {
  10068. value
  10069. } = expression;
  10070. if (typeof value === "string" || typeof value === "boolean") {
  10071. return true;
  10072. }
  10073. }
  10074. } else {
  10075. if (type === "StringLiteral" || type === "BooleanLiteral") {
  10076. return true;
  10077. }
  10078. }
  10079. if (isNumber(expression, estree) || isNegativeNumber(expression, estree)) {
  10080. return true;
  10081. }
  10082. if (type === "TemplateLiteral" && expression.expressions.length === 0) {
  10083. return true;
  10084. }
  10085. if (isPossiblyLiteralEnum(expression)) {
  10086. return true;
  10087. }
  10088. return false;
  10089. }
  10090. function isNumber(expression, estree) {
  10091. if (estree) {
  10092. return expression.type === "Literal" && (typeof expression.value === "number" || "bigint" in expression);
  10093. } else {
  10094. return expression.type === "NumericLiteral" || expression.type === "BigIntLiteral";
  10095. }
  10096. }
  10097. function isNegativeNumber(expression, estree) {
  10098. if (expression.type === "UnaryExpression") {
  10099. const {
  10100. operator,
  10101. argument
  10102. } = expression;
  10103. if (operator === "-" && isNumber(argument, estree)) {
  10104. return true;
  10105. }
  10106. }
  10107. return false;
  10108. }
  10109. function isUncomputedMemberExpressionChain(expression) {
  10110. if (expression.type === "Identifier") return true;
  10111. if (expression.type !== "MemberExpression") return false;
  10112. if (expression.computed) return false;
  10113. return isUncomputedMemberExpressionChain(expression.object);
  10114. }
  10115. const PlaceholderErrors = ParseErrorEnum`placeholders`({
  10116. ClassNameIsRequired: "A class name is required.",
  10117. UnexpectedSpace: "Unexpected space in placeholder."
  10118. });
  10119. var placeholders = (superClass => class PlaceholdersParserMixin extends superClass {
  10120. parsePlaceholder(expectedNode) {
  10121. if (this.match(142)) {
  10122. const node = this.startNode();
  10123. this.next();
  10124. this.assertNoSpace();
  10125. node.name = super.parseIdentifier(true);
  10126. this.assertNoSpace();
  10127. this.expect(142);
  10128. return this.finishPlaceholder(node, expectedNode);
  10129. }
  10130. }
  10131. finishPlaceholder(node, expectedNode) {
  10132. const isFinished = !!(node.expectedNode && node.type === "Placeholder");
  10133. node.expectedNode = expectedNode;
  10134. return isFinished ? node : this.finishNode(node, "Placeholder");
  10135. }
  10136. getTokenFromCode(code) {
  10137. if (code === 37 && this.input.charCodeAt(this.state.pos + 1) === 37) {
  10138. return this.finishOp(142, 2);
  10139. }
  10140. return super.getTokenFromCode(code);
  10141. }
  10142. parseExprAtom(refExpressionErrors) {
  10143. return this.parsePlaceholder("Expression") || super.parseExprAtom(refExpressionErrors);
  10144. }
  10145. parseIdentifier(liberal) {
  10146. return this.parsePlaceholder("Identifier") || super.parseIdentifier(liberal);
  10147. }
  10148. checkReservedWord(word, startLoc, checkKeywords, isBinding) {
  10149. if (word !== undefined) {
  10150. super.checkReservedWord(word, startLoc, checkKeywords, isBinding);
  10151. }
  10152. }
  10153. parseBindingAtom() {
  10154. return this.parsePlaceholder("Pattern") || super.parseBindingAtom();
  10155. }
  10156. isValidLVal(type, isParenthesized, binding) {
  10157. return type === "Placeholder" || super.isValidLVal(type, isParenthesized, binding);
  10158. }
  10159. toAssignable(node, isLHS) {
  10160. if (node && node.type === "Placeholder" && node.expectedNode === "Expression") {
  10161. node.expectedNode = "Pattern";
  10162. } else {
  10163. super.toAssignable(node, isLHS);
  10164. }
  10165. }
  10166. chStartsBindingIdentifier(ch, pos) {
  10167. if (super.chStartsBindingIdentifier(ch, pos)) {
  10168. return true;
  10169. }
  10170. const nextToken = this.lookahead();
  10171. if (nextToken.type === 142) {
  10172. return true;
  10173. }
  10174. return false;
  10175. }
  10176. verifyBreakContinue(node, isBreak) {
  10177. if (node.label && node.label.type === "Placeholder") return;
  10178. super.verifyBreakContinue(node, isBreak);
  10179. }
  10180. parseExpressionStatement(node, expr) {
  10181. if (expr.type !== "Placeholder" || expr.extra && expr.extra.parenthesized) {
  10182. return super.parseExpressionStatement(node, expr);
  10183. }
  10184. if (this.match(14)) {
  10185. const stmt = node;
  10186. stmt.label = this.finishPlaceholder(expr, "Identifier");
  10187. this.next();
  10188. stmt.body = super.parseStatementOrSloppyAnnexBFunctionDeclaration();
  10189. return this.finishNode(stmt, "LabeledStatement");
  10190. }
  10191. this.semicolon();
  10192. node.name = expr.name;
  10193. return this.finishPlaceholder(node, "Statement");
  10194. }
  10195. parseBlock(allowDirectives, createNewLexicalScope, afterBlockParse) {
  10196. return this.parsePlaceholder("BlockStatement") || super.parseBlock(allowDirectives, createNewLexicalScope, afterBlockParse);
  10197. }
  10198. parseFunctionId(requireId) {
  10199. return this.parsePlaceholder("Identifier") || super.parseFunctionId(requireId);
  10200. }
  10201. parseClass(node, isStatement, optionalId) {
  10202. const type = isStatement ? "ClassDeclaration" : "ClassExpression";
  10203. this.next();
  10204. const oldStrict = this.state.strict;
  10205. const placeholder = this.parsePlaceholder("Identifier");
  10206. if (placeholder) {
  10207. if (this.match(81) || this.match(142) || this.match(5)) {
  10208. node.id = placeholder;
  10209. } else if (optionalId || !isStatement) {
  10210. node.id = null;
  10211. node.body = this.finishPlaceholder(placeholder, "ClassBody");
  10212. return this.finishNode(node, type);
  10213. } else {
  10214. throw this.raise(PlaceholderErrors.ClassNameIsRequired, {
  10215. at: this.state.startLoc
  10216. });
  10217. }
  10218. } else {
  10219. this.parseClassId(node, isStatement, optionalId);
  10220. }
  10221. super.parseClassSuper(node);
  10222. node.body = this.parsePlaceholder("ClassBody") || super.parseClassBody(!!node.superClass, oldStrict);
  10223. return this.finishNode(node, type);
  10224. }
  10225. parseExport(node, decorators) {
  10226. const placeholder = this.parsePlaceholder("Identifier");
  10227. if (!placeholder) return super.parseExport(node, decorators);
  10228. if (!this.isContextual(97) && !this.match(12)) {
  10229. node.specifiers = [];
  10230. node.source = null;
  10231. node.declaration = this.finishPlaceholder(placeholder, "Declaration");
  10232. return this.finishNode(node, "ExportNamedDeclaration");
  10233. }
  10234. this.expectPlugin("exportDefaultFrom");
  10235. const specifier = this.startNode();
  10236. specifier.exported = placeholder;
  10237. node.specifiers = [this.finishNode(specifier, "ExportDefaultSpecifier")];
  10238. return super.parseExport(node, decorators);
  10239. }
  10240. isExportDefaultSpecifier() {
  10241. if (this.match(65)) {
  10242. const next = this.nextTokenStart();
  10243. if (this.isUnparsedContextual(next, "from")) {
  10244. if (this.input.startsWith(tokenLabelName(142), this.nextTokenStartSince(next + 4))) {
  10245. return true;
  10246. }
  10247. }
  10248. }
  10249. return super.isExportDefaultSpecifier();
  10250. }
  10251. maybeParseExportDefaultSpecifier(node) {
  10252. if (node.specifiers && node.specifiers.length > 0) {
  10253. return true;
  10254. }
  10255. return super.maybeParseExportDefaultSpecifier(node);
  10256. }
  10257. checkExport(node) {
  10258. const {
  10259. specifiers
  10260. } = node;
  10261. if (specifiers != null && specifiers.length) {
  10262. node.specifiers = specifiers.filter(node => node.exported.type === "Placeholder");
  10263. }
  10264. super.checkExport(node);
  10265. node.specifiers = specifiers;
  10266. }
  10267. parseImport(node) {
  10268. const placeholder = this.parsePlaceholder("Identifier");
  10269. if (!placeholder) return super.parseImport(node);
  10270. node.specifiers = [];
  10271. if (!this.isContextual(97) && !this.match(12)) {
  10272. node.source = this.finishPlaceholder(placeholder, "StringLiteral");
  10273. this.semicolon();
  10274. return this.finishNode(node, "ImportDeclaration");
  10275. }
  10276. const specifier = this.startNodeAtNode(placeholder);
  10277. specifier.local = placeholder;
  10278. node.specifiers.push(this.finishNode(specifier, "ImportDefaultSpecifier"));
  10279. if (this.eat(12)) {
  10280. const hasStarImport = this.maybeParseStarImportSpecifier(node);
  10281. if (!hasStarImport) this.parseNamedImportSpecifiers(node);
  10282. }
  10283. this.expectContextual(97);
  10284. node.source = this.parseImportSource();
  10285. this.semicolon();
  10286. return this.finishNode(node, "ImportDeclaration");
  10287. }
  10288. parseImportSource() {
  10289. return this.parsePlaceholder("StringLiteral") || super.parseImportSource();
  10290. }
  10291. assertNoSpace() {
  10292. if (this.state.start > this.state.lastTokEndLoc.index) {
  10293. this.raise(PlaceholderErrors.UnexpectedSpace, {
  10294. at: this.state.lastTokEndLoc
  10295. });
  10296. }
  10297. }
  10298. });
  10299. var v8intrinsic = (superClass => class V8IntrinsicMixin extends superClass {
  10300. parseV8Intrinsic() {
  10301. if (this.match(54)) {
  10302. const v8IntrinsicStartLoc = this.state.startLoc;
  10303. const node = this.startNode();
  10304. this.next();
  10305. if (tokenIsIdentifier(this.state.type)) {
  10306. const name = this.parseIdentifierName();
  10307. const identifier = this.createIdentifier(node, name);
  10308. identifier.type = "V8IntrinsicIdentifier";
  10309. if (this.match(10)) {
  10310. return identifier;
  10311. }
  10312. }
  10313. this.unexpected(v8IntrinsicStartLoc);
  10314. }
  10315. }
  10316. parseExprAtom(refExpressionErrors) {
  10317. return this.parseV8Intrinsic() || super.parseExprAtom(refExpressionErrors);
  10318. }
  10319. });
  10320. function hasPlugin(plugins, expectedConfig) {
  10321. const [expectedName, expectedOptions] = typeof expectedConfig === "string" ? [expectedConfig, {}] : expectedConfig;
  10322. const expectedKeys = Object.keys(expectedOptions);
  10323. const expectedOptionsIsEmpty = expectedKeys.length === 0;
  10324. return plugins.some(p => {
  10325. if (typeof p === "string") {
  10326. return expectedOptionsIsEmpty && p === expectedName;
  10327. } else {
  10328. const [pluginName, pluginOptions] = p;
  10329. if (pluginName !== expectedName) {
  10330. return false;
  10331. }
  10332. for (const key of expectedKeys) {
  10333. if (pluginOptions[key] !== expectedOptions[key]) {
  10334. return false;
  10335. }
  10336. }
  10337. return true;
  10338. }
  10339. });
  10340. }
  10341. function getPluginOption(plugins, name, option) {
  10342. const plugin = plugins.find(plugin => {
  10343. if (Array.isArray(plugin)) {
  10344. return plugin[0] === name;
  10345. } else {
  10346. return plugin === name;
  10347. }
  10348. });
  10349. if (plugin && Array.isArray(plugin) && plugin.length > 1) {
  10350. return plugin[1][option];
  10351. }
  10352. return null;
  10353. }
  10354. const PIPELINE_PROPOSALS = ["minimal", "fsharp", "hack", "smart"];
  10355. const TOPIC_TOKENS = ["^^", "@@", "^", "%", "#"];
  10356. const RECORD_AND_TUPLE_SYNTAX_TYPES = ["hash", "bar"];
  10357. function validatePlugins(plugins) {
  10358. if (hasPlugin(plugins, "decorators")) {
  10359. if (hasPlugin(plugins, "decorators-legacy")) {
  10360. throw new Error("Cannot use the decorators and decorators-legacy plugin together");
  10361. }
  10362. const decoratorsBeforeExport = getPluginOption(plugins, "decorators", "decoratorsBeforeExport");
  10363. if (decoratorsBeforeExport != null && typeof decoratorsBeforeExport !== "boolean") {
  10364. throw new Error("'decoratorsBeforeExport' must be a boolean, if specified.");
  10365. }
  10366. const allowCallParenthesized = getPluginOption(plugins, "decorators", "allowCallParenthesized");
  10367. if (allowCallParenthesized != null && typeof allowCallParenthesized !== "boolean") {
  10368. throw new Error("'allowCallParenthesized' must be a boolean.");
  10369. }
  10370. }
  10371. if (hasPlugin(plugins, "flow") && hasPlugin(plugins, "typescript")) {
  10372. throw new Error("Cannot combine flow and typescript plugins.");
  10373. }
  10374. if (hasPlugin(plugins, "placeholders") && hasPlugin(plugins, "v8intrinsic")) {
  10375. throw new Error("Cannot combine placeholders and v8intrinsic plugins.");
  10376. }
  10377. if (hasPlugin(plugins, "pipelineOperator")) {
  10378. const proposal = getPluginOption(plugins, "pipelineOperator", "proposal");
  10379. if (!PIPELINE_PROPOSALS.includes(proposal)) {
  10380. const proposalList = PIPELINE_PROPOSALS.map(p => `"${p}"`).join(", ");
  10381. throw new Error(`"pipelineOperator" requires "proposal" option whose value must be one of: ${proposalList}.`);
  10382. }
  10383. const tupleSyntaxIsHash = hasPlugin(plugins, ["recordAndTuple", {
  10384. syntaxType: "hash"
  10385. }]);
  10386. if (proposal === "hack") {
  10387. if (hasPlugin(plugins, "placeholders")) {
  10388. throw new Error("Cannot combine placeholders plugin and Hack-style pipes.");
  10389. }
  10390. if (hasPlugin(plugins, "v8intrinsic")) {
  10391. throw new Error("Cannot combine v8intrinsic plugin and Hack-style pipes.");
  10392. }
  10393. const topicToken = getPluginOption(plugins, "pipelineOperator", "topicToken");
  10394. if (!TOPIC_TOKENS.includes(topicToken)) {
  10395. const tokenList = TOPIC_TOKENS.map(t => `"${t}"`).join(", ");
  10396. throw new Error(`"pipelineOperator" in "proposal": "hack" mode also requires a "topicToken" option whose value must be one of: ${tokenList}.`);
  10397. }
  10398. if (topicToken === "#" && tupleSyntaxIsHash) {
  10399. throw new Error('Plugin conflict between `["pipelineOperator", { proposal: "hack", topicToken: "#" }]` and `["recordAndtuple", { syntaxType: "hash"}]`.');
  10400. }
  10401. } else if (proposal === "smart" && tupleSyntaxIsHash) {
  10402. throw new Error('Plugin conflict between `["pipelineOperator", { proposal: "smart" }]` and `["recordAndtuple", { syntaxType: "hash"}]`.');
  10403. }
  10404. }
  10405. if (hasPlugin(plugins, "moduleAttributes")) {
  10406. {
  10407. if (hasPlugin(plugins, "importAssertions")) {
  10408. throw new Error("Cannot combine importAssertions and moduleAttributes plugins.");
  10409. }
  10410. const moduleAttributesVersionPluginOption = getPluginOption(plugins, "moduleAttributes", "version");
  10411. if (moduleAttributesVersionPluginOption !== "may-2020") {
  10412. throw new Error("The 'moduleAttributes' plugin requires a 'version' option," + " representing the last proposal update. Currently, the" + " only supported value is 'may-2020'.");
  10413. }
  10414. }
  10415. }
  10416. if (hasPlugin(plugins, "recordAndTuple") && getPluginOption(plugins, "recordAndTuple", "syntaxType") != null && !RECORD_AND_TUPLE_SYNTAX_TYPES.includes(getPluginOption(plugins, "recordAndTuple", "syntaxType"))) {
  10417. throw new Error("The 'syntaxType' option of the 'recordAndTuple' plugin must be one of: " + RECORD_AND_TUPLE_SYNTAX_TYPES.map(p => `'${p}'`).join(", "));
  10418. }
  10419. if (hasPlugin(plugins, "asyncDoExpressions") && !hasPlugin(plugins, "doExpressions")) {
  10420. const error = new Error("'asyncDoExpressions' requires 'doExpressions', please add 'doExpressions' to parser plugins.");
  10421. error.missingPlugins = "doExpressions";
  10422. throw error;
  10423. }
  10424. }
  10425. const mixinPlugins = {
  10426. estree,
  10427. jsx,
  10428. flow,
  10429. typescript,
  10430. v8intrinsic,
  10431. placeholders
  10432. };
  10433. const mixinPluginNames = Object.keys(mixinPlugins);
  10434. const defaultOptions = {
  10435. sourceType: "script",
  10436. sourceFilename: undefined,
  10437. startColumn: 0,
  10438. startLine: 1,
  10439. allowAwaitOutsideFunction: false,
  10440. allowReturnOutsideFunction: false,
  10441. allowNewTargetOutsideFunction: false,
  10442. allowImportExportEverywhere: false,
  10443. allowSuperOutsideMethod: false,
  10444. allowUndeclaredExports: false,
  10445. plugins: [],
  10446. strictMode: null,
  10447. ranges: false,
  10448. tokens: false,
  10449. createParenthesizedExpressions: false,
  10450. errorRecovery: false,
  10451. attachComment: true,
  10452. annexB: true
  10453. };
  10454. function getOptions(opts) {
  10455. if (opts && opts.annexB != null && opts.annexB !== false) {
  10456. throw new Error("The `annexB` option can only be set to `false`.");
  10457. }
  10458. const options = {};
  10459. for (const key of Object.keys(defaultOptions)) {
  10460. options[key] = opts && opts[key] != null ? opts[key] : defaultOptions[key];
  10461. }
  10462. return options;
  10463. }
  10464. class ExpressionParser extends LValParser {
  10465. checkProto(prop, isRecord, protoRef, refExpressionErrors) {
  10466. if (prop.type === "SpreadElement" || this.isObjectMethod(prop) || prop.computed || prop.shorthand) {
  10467. return;
  10468. }
  10469. const key = prop.key;
  10470. const name = key.type === "Identifier" ? key.name : key.value;
  10471. if (name === "__proto__") {
  10472. if (isRecord) {
  10473. this.raise(Errors.RecordNoProto, {
  10474. at: key
  10475. });
  10476. return;
  10477. }
  10478. if (protoRef.used) {
  10479. if (refExpressionErrors) {
  10480. if (refExpressionErrors.doubleProtoLoc === null) {
  10481. refExpressionErrors.doubleProtoLoc = key.loc.start;
  10482. }
  10483. } else {
  10484. this.raise(Errors.DuplicateProto, {
  10485. at: key
  10486. });
  10487. }
  10488. }
  10489. protoRef.used = true;
  10490. }
  10491. }
  10492. shouldExitDescending(expr, potentialArrowAt) {
  10493. return expr.type === "ArrowFunctionExpression" && expr.start === potentialArrowAt;
  10494. }
  10495. getExpression() {
  10496. this.enterInitialScopes();
  10497. this.nextToken();
  10498. const expr = this.parseExpression();
  10499. if (!this.match(137)) {
  10500. this.unexpected();
  10501. }
  10502. this.finalizeRemainingComments();
  10503. expr.comments = this.state.comments;
  10504. expr.errors = this.state.errors;
  10505. if (this.options.tokens) {
  10506. expr.tokens = this.tokens;
  10507. }
  10508. return expr;
  10509. }
  10510. parseExpression(disallowIn, refExpressionErrors) {
  10511. if (disallowIn) {
  10512. return this.disallowInAnd(() => this.parseExpressionBase(refExpressionErrors));
  10513. }
  10514. return this.allowInAnd(() => this.parseExpressionBase(refExpressionErrors));
  10515. }
  10516. parseExpressionBase(refExpressionErrors) {
  10517. const startLoc = this.state.startLoc;
  10518. const expr = this.parseMaybeAssign(refExpressionErrors);
  10519. if (this.match(12)) {
  10520. const node = this.startNodeAt(startLoc);
  10521. node.expressions = [expr];
  10522. while (this.eat(12)) {
  10523. node.expressions.push(this.parseMaybeAssign(refExpressionErrors));
  10524. }
  10525. this.toReferencedList(node.expressions);
  10526. return this.finishNode(node, "SequenceExpression");
  10527. }
  10528. return expr;
  10529. }
  10530. parseMaybeAssignDisallowIn(refExpressionErrors, afterLeftParse) {
  10531. return this.disallowInAnd(() => this.parseMaybeAssign(refExpressionErrors, afterLeftParse));
  10532. }
  10533. parseMaybeAssignAllowIn(refExpressionErrors, afterLeftParse) {
  10534. return this.allowInAnd(() => this.parseMaybeAssign(refExpressionErrors, afterLeftParse));
  10535. }
  10536. setOptionalParametersError(refExpressionErrors, resultError) {
  10537. var _resultError$loc;
  10538. refExpressionErrors.optionalParametersLoc = (_resultError$loc = resultError == null ? void 0 : resultError.loc) != null ? _resultError$loc : this.state.startLoc;
  10539. }
  10540. parseMaybeAssign(refExpressionErrors, afterLeftParse) {
  10541. const startLoc = this.state.startLoc;
  10542. if (this.isContextual(106)) {
  10543. if (this.prodParam.hasYield) {
  10544. let left = this.parseYield();
  10545. if (afterLeftParse) {
  10546. left = afterLeftParse.call(this, left, startLoc);
  10547. }
  10548. return left;
  10549. }
  10550. }
  10551. let ownExpressionErrors;
  10552. if (refExpressionErrors) {
  10553. ownExpressionErrors = false;
  10554. } else {
  10555. refExpressionErrors = new ExpressionErrors();
  10556. ownExpressionErrors = true;
  10557. }
  10558. const {
  10559. type
  10560. } = this.state;
  10561. if (type === 10 || tokenIsIdentifier(type)) {
  10562. this.state.potentialArrowAt = this.state.start;
  10563. }
  10564. let left = this.parseMaybeConditional(refExpressionErrors);
  10565. if (afterLeftParse) {
  10566. left = afterLeftParse.call(this, left, startLoc);
  10567. }
  10568. if (tokenIsAssignment(this.state.type)) {
  10569. const node = this.startNodeAt(startLoc);
  10570. const operator = this.state.value;
  10571. node.operator = operator;
  10572. if (this.match(29)) {
  10573. this.toAssignable(left, true);
  10574. node.left = left;
  10575. const startIndex = startLoc.index;
  10576. if (refExpressionErrors.doubleProtoLoc != null && refExpressionErrors.doubleProtoLoc.index >= startIndex) {
  10577. refExpressionErrors.doubleProtoLoc = null;
  10578. }
  10579. if (refExpressionErrors.shorthandAssignLoc != null && refExpressionErrors.shorthandAssignLoc.index >= startIndex) {
  10580. refExpressionErrors.shorthandAssignLoc = null;
  10581. }
  10582. if (refExpressionErrors.privateKeyLoc != null && refExpressionErrors.privateKeyLoc.index >= startIndex) {
  10583. this.checkDestructuringPrivate(refExpressionErrors);
  10584. refExpressionErrors.privateKeyLoc = null;
  10585. }
  10586. } else {
  10587. node.left = left;
  10588. }
  10589. this.next();
  10590. node.right = this.parseMaybeAssign();
  10591. this.checkLVal(left, {
  10592. in: this.finishNode(node, "AssignmentExpression")
  10593. });
  10594. return node;
  10595. } else if (ownExpressionErrors) {
  10596. this.checkExpressionErrors(refExpressionErrors, true);
  10597. }
  10598. return left;
  10599. }
  10600. parseMaybeConditional(refExpressionErrors) {
  10601. const startLoc = this.state.startLoc;
  10602. const potentialArrowAt = this.state.potentialArrowAt;
  10603. const expr = this.parseExprOps(refExpressionErrors);
  10604. if (this.shouldExitDescending(expr, potentialArrowAt)) {
  10605. return expr;
  10606. }
  10607. return this.parseConditional(expr, startLoc, refExpressionErrors);
  10608. }
  10609. parseConditional(expr, startLoc, refExpressionErrors) {
  10610. if (this.eat(17)) {
  10611. const node = this.startNodeAt(startLoc);
  10612. node.test = expr;
  10613. node.consequent = this.parseMaybeAssignAllowIn();
  10614. this.expect(14);
  10615. node.alternate = this.parseMaybeAssign();
  10616. return this.finishNode(node, "ConditionalExpression");
  10617. }
  10618. return expr;
  10619. }
  10620. parseMaybeUnaryOrPrivate(refExpressionErrors) {
  10621. return this.match(136) ? this.parsePrivateName() : this.parseMaybeUnary(refExpressionErrors);
  10622. }
  10623. parseExprOps(refExpressionErrors) {
  10624. const startLoc = this.state.startLoc;
  10625. const potentialArrowAt = this.state.potentialArrowAt;
  10626. const expr = this.parseMaybeUnaryOrPrivate(refExpressionErrors);
  10627. if (this.shouldExitDescending(expr, potentialArrowAt)) {
  10628. return expr;
  10629. }
  10630. return this.parseExprOp(expr, startLoc, -1);
  10631. }
  10632. parseExprOp(left, leftStartLoc, minPrec) {
  10633. if (this.isPrivateName(left)) {
  10634. const value = this.getPrivateNameSV(left);
  10635. if (minPrec >= tokenOperatorPrecedence(58) || !this.prodParam.hasIn || !this.match(58)) {
  10636. this.raise(Errors.PrivateInExpectedIn, {
  10637. at: left,
  10638. identifierName: value
  10639. });
  10640. }
  10641. this.classScope.usePrivateName(value, left.loc.start);
  10642. }
  10643. const op = this.state.type;
  10644. if (tokenIsOperator(op) && (this.prodParam.hasIn || !this.match(58))) {
  10645. let prec = tokenOperatorPrecedence(op);
  10646. if (prec > minPrec) {
  10647. if (op === 39) {
  10648. this.expectPlugin("pipelineOperator");
  10649. if (this.state.inFSharpPipelineDirectBody) {
  10650. return left;
  10651. }
  10652. this.checkPipelineAtInfixOperator(left, leftStartLoc);
  10653. }
  10654. const node = this.startNodeAt(leftStartLoc);
  10655. node.left = left;
  10656. node.operator = this.state.value;
  10657. const logical = op === 41 || op === 42;
  10658. const coalesce = op === 40;
  10659. if (coalesce) {
  10660. prec = tokenOperatorPrecedence(42);
  10661. }
  10662. this.next();
  10663. if (op === 39 && this.hasPlugin(["pipelineOperator", {
  10664. proposal: "minimal"
  10665. }])) {
  10666. if (this.state.type === 96 && this.prodParam.hasAwait) {
  10667. throw this.raise(Errors.UnexpectedAwaitAfterPipelineBody, {
  10668. at: this.state.startLoc
  10669. });
  10670. }
  10671. }
  10672. node.right = this.parseExprOpRightExpr(op, prec);
  10673. const finishedNode = this.finishNode(node, logical || coalesce ? "LogicalExpression" : "BinaryExpression");
  10674. const nextOp = this.state.type;
  10675. if (coalesce && (nextOp === 41 || nextOp === 42) || logical && nextOp === 40) {
  10676. throw this.raise(Errors.MixingCoalesceWithLogical, {
  10677. at: this.state.startLoc
  10678. });
  10679. }
  10680. return this.parseExprOp(finishedNode, leftStartLoc, minPrec);
  10681. }
  10682. }
  10683. return left;
  10684. }
  10685. parseExprOpRightExpr(op, prec) {
  10686. const startLoc = this.state.startLoc;
  10687. switch (op) {
  10688. case 39:
  10689. switch (this.getPluginOption("pipelineOperator", "proposal")) {
  10690. case "hack":
  10691. return this.withTopicBindingContext(() => {
  10692. return this.parseHackPipeBody();
  10693. });
  10694. case "smart":
  10695. return this.withTopicBindingContext(() => {
  10696. if (this.prodParam.hasYield && this.isContextual(106)) {
  10697. throw this.raise(Errors.PipeBodyIsTighter, {
  10698. at: this.state.startLoc
  10699. });
  10700. }
  10701. return this.parseSmartPipelineBodyInStyle(this.parseExprOpBaseRightExpr(op, prec), startLoc);
  10702. });
  10703. case "fsharp":
  10704. return this.withSoloAwaitPermittingContext(() => {
  10705. return this.parseFSharpPipelineBody(prec);
  10706. });
  10707. }
  10708. default:
  10709. return this.parseExprOpBaseRightExpr(op, prec);
  10710. }
  10711. }
  10712. parseExprOpBaseRightExpr(op, prec) {
  10713. const startLoc = this.state.startLoc;
  10714. return this.parseExprOp(this.parseMaybeUnaryOrPrivate(), startLoc, tokenIsRightAssociative(op) ? prec - 1 : prec);
  10715. }
  10716. parseHackPipeBody() {
  10717. var _body$extra;
  10718. const {
  10719. startLoc
  10720. } = this.state;
  10721. const body = this.parseMaybeAssign();
  10722. const requiredParentheses = UnparenthesizedPipeBodyDescriptions.has(body.type);
  10723. if (requiredParentheses && !((_body$extra = body.extra) != null && _body$extra.parenthesized)) {
  10724. this.raise(Errors.PipeUnparenthesizedBody, {
  10725. at: startLoc,
  10726. type: body.type
  10727. });
  10728. }
  10729. if (!this.topicReferenceWasUsedInCurrentContext()) {
  10730. this.raise(Errors.PipeTopicUnused, {
  10731. at: startLoc
  10732. });
  10733. }
  10734. return body;
  10735. }
  10736. checkExponentialAfterUnary(node) {
  10737. if (this.match(57)) {
  10738. this.raise(Errors.UnexpectedTokenUnaryExponentiation, {
  10739. at: node.argument
  10740. });
  10741. }
  10742. }
  10743. parseMaybeUnary(refExpressionErrors, sawUnary) {
  10744. const startLoc = this.state.startLoc;
  10745. const isAwait = this.isContextual(96);
  10746. if (isAwait && this.isAwaitAllowed()) {
  10747. this.next();
  10748. const expr = this.parseAwait(startLoc);
  10749. if (!sawUnary) this.checkExponentialAfterUnary(expr);
  10750. return expr;
  10751. }
  10752. const update = this.match(34);
  10753. const node = this.startNode();
  10754. if (tokenIsPrefix(this.state.type)) {
  10755. node.operator = this.state.value;
  10756. node.prefix = true;
  10757. if (this.match(72)) {
  10758. this.expectPlugin("throwExpressions");
  10759. }
  10760. const isDelete = this.match(89);
  10761. this.next();
  10762. node.argument = this.parseMaybeUnary(null, true);
  10763. this.checkExpressionErrors(refExpressionErrors, true);
  10764. if (this.state.strict && isDelete) {
  10765. const arg = node.argument;
  10766. if (arg.type === "Identifier") {
  10767. this.raise(Errors.StrictDelete, {
  10768. at: node
  10769. });
  10770. } else if (this.hasPropertyAsPrivateName(arg)) {
  10771. this.raise(Errors.DeletePrivateField, {
  10772. at: node
  10773. });
  10774. }
  10775. }
  10776. if (!update) {
  10777. if (!sawUnary) {
  10778. this.checkExponentialAfterUnary(node);
  10779. }
  10780. return this.finishNode(node, "UnaryExpression");
  10781. }
  10782. }
  10783. const expr = this.parseUpdate(node, update, refExpressionErrors);
  10784. if (isAwait) {
  10785. const {
  10786. type
  10787. } = this.state;
  10788. const startsExpr = this.hasPlugin("v8intrinsic") ? tokenCanStartExpression(type) : tokenCanStartExpression(type) && !this.match(54);
  10789. if (startsExpr && !this.isAmbiguousAwait()) {
  10790. this.raiseOverwrite(Errors.AwaitNotInAsyncContext, {
  10791. at: startLoc
  10792. });
  10793. return this.parseAwait(startLoc);
  10794. }
  10795. }
  10796. return expr;
  10797. }
  10798. parseUpdate(node, update, refExpressionErrors) {
  10799. if (update) {
  10800. const updateExpressionNode = node;
  10801. this.checkLVal(updateExpressionNode.argument, {
  10802. in: this.finishNode(updateExpressionNode, "UpdateExpression")
  10803. });
  10804. return node;
  10805. }
  10806. const startLoc = this.state.startLoc;
  10807. let expr = this.parseExprSubscripts(refExpressionErrors);
  10808. if (this.checkExpressionErrors(refExpressionErrors, false)) return expr;
  10809. while (tokenIsPostfix(this.state.type) && !this.canInsertSemicolon()) {
  10810. const node = this.startNodeAt(startLoc);
  10811. node.operator = this.state.value;
  10812. node.prefix = false;
  10813. node.argument = expr;
  10814. this.next();
  10815. this.checkLVal(expr, {
  10816. in: expr = this.finishNode(node, "UpdateExpression")
  10817. });
  10818. }
  10819. return expr;
  10820. }
  10821. parseExprSubscripts(refExpressionErrors) {
  10822. const startLoc = this.state.startLoc;
  10823. const potentialArrowAt = this.state.potentialArrowAt;
  10824. const expr = this.parseExprAtom(refExpressionErrors);
  10825. if (this.shouldExitDescending(expr, potentialArrowAt)) {
  10826. return expr;
  10827. }
  10828. return this.parseSubscripts(expr, startLoc);
  10829. }
  10830. parseSubscripts(base, startLoc, noCalls) {
  10831. const state = {
  10832. optionalChainMember: false,
  10833. maybeAsyncArrow: this.atPossibleAsyncArrow(base),
  10834. stop: false
  10835. };
  10836. do {
  10837. base = this.parseSubscript(base, startLoc, noCalls, state);
  10838. state.maybeAsyncArrow = false;
  10839. } while (!state.stop);
  10840. return base;
  10841. }
  10842. parseSubscript(base, startLoc, noCalls, state) {
  10843. const {
  10844. type
  10845. } = this.state;
  10846. if (!noCalls && type === 15) {
  10847. return this.parseBind(base, startLoc, noCalls, state);
  10848. } else if (tokenIsTemplate(type)) {
  10849. return this.parseTaggedTemplateExpression(base, startLoc, state);
  10850. }
  10851. let optional = false;
  10852. if (type === 18) {
  10853. if (noCalls && this.lookaheadCharCode() === 40) {
  10854. state.stop = true;
  10855. return base;
  10856. }
  10857. state.optionalChainMember = optional = true;
  10858. this.next();
  10859. }
  10860. if (!noCalls && this.match(10)) {
  10861. return this.parseCoverCallAndAsyncArrowHead(base, startLoc, state, optional);
  10862. } else {
  10863. const computed = this.eat(0);
  10864. if (computed || optional || this.eat(16)) {
  10865. return this.parseMember(base, startLoc, state, computed, optional);
  10866. } else {
  10867. state.stop = true;
  10868. return base;
  10869. }
  10870. }
  10871. }
  10872. parseMember(base, startLoc, state, computed, optional) {
  10873. const node = this.startNodeAt(startLoc);
  10874. node.object = base;
  10875. node.computed = computed;
  10876. if (computed) {
  10877. node.property = this.parseExpression();
  10878. this.expect(3);
  10879. } else if (this.match(136)) {
  10880. if (base.type === "Super") {
  10881. this.raise(Errors.SuperPrivateField, {
  10882. at: startLoc
  10883. });
  10884. }
  10885. this.classScope.usePrivateName(this.state.value, this.state.startLoc);
  10886. node.property = this.parsePrivateName();
  10887. } else {
  10888. node.property = this.parseIdentifier(true);
  10889. }
  10890. if (state.optionalChainMember) {
  10891. node.optional = optional;
  10892. return this.finishNode(node, "OptionalMemberExpression");
  10893. } else {
  10894. return this.finishNode(node, "MemberExpression");
  10895. }
  10896. }
  10897. parseBind(base, startLoc, noCalls, state) {
  10898. const node = this.startNodeAt(startLoc);
  10899. node.object = base;
  10900. this.next();
  10901. node.callee = this.parseNoCallExpr();
  10902. state.stop = true;
  10903. return this.parseSubscripts(this.finishNode(node, "BindExpression"), startLoc, noCalls);
  10904. }
  10905. parseCoverCallAndAsyncArrowHead(base, startLoc, state, optional) {
  10906. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  10907. let refExpressionErrors = null;
  10908. this.state.maybeInArrowParameters = true;
  10909. this.next();
  10910. const node = this.startNodeAt(startLoc);
  10911. node.callee = base;
  10912. const {
  10913. maybeAsyncArrow,
  10914. optionalChainMember
  10915. } = state;
  10916. if (maybeAsyncArrow) {
  10917. this.expressionScope.enter(newAsyncArrowScope());
  10918. refExpressionErrors = new ExpressionErrors();
  10919. }
  10920. if (optionalChainMember) {
  10921. node.optional = optional;
  10922. }
  10923. if (optional) {
  10924. node.arguments = this.parseCallExpressionArguments(11);
  10925. } else {
  10926. node.arguments = this.parseCallExpressionArguments(11, base.type === "Import", base.type !== "Super", node, refExpressionErrors);
  10927. }
  10928. let finishedNode = this.finishCallExpression(node, optionalChainMember);
  10929. if (maybeAsyncArrow && this.shouldParseAsyncArrow() && !optional) {
  10930. state.stop = true;
  10931. this.checkDestructuringPrivate(refExpressionErrors);
  10932. this.expressionScope.validateAsPattern();
  10933. this.expressionScope.exit();
  10934. finishedNode = this.parseAsyncArrowFromCallExpression(this.startNodeAt(startLoc), finishedNode);
  10935. } else {
  10936. if (maybeAsyncArrow) {
  10937. this.checkExpressionErrors(refExpressionErrors, true);
  10938. this.expressionScope.exit();
  10939. }
  10940. this.toReferencedArguments(finishedNode);
  10941. }
  10942. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  10943. return finishedNode;
  10944. }
  10945. toReferencedArguments(node, isParenthesizedExpr) {
  10946. this.toReferencedListDeep(node.arguments, isParenthesizedExpr);
  10947. }
  10948. parseTaggedTemplateExpression(base, startLoc, state) {
  10949. const node = this.startNodeAt(startLoc);
  10950. node.tag = base;
  10951. node.quasi = this.parseTemplate(true);
  10952. if (state.optionalChainMember) {
  10953. this.raise(Errors.OptionalChainingNoTemplate, {
  10954. at: startLoc
  10955. });
  10956. }
  10957. return this.finishNode(node, "TaggedTemplateExpression");
  10958. }
  10959. atPossibleAsyncArrow(base) {
  10960. return base.type === "Identifier" && base.name === "async" && this.state.lastTokEndLoc.index === base.end && !this.canInsertSemicolon() && base.end - base.start === 5 && base.start === this.state.potentialArrowAt;
  10961. }
  10962. finishCallExpression(node, optional) {
  10963. if (node.callee.type === "Import") {
  10964. if (node.arguments.length === 2) {
  10965. {
  10966. if (!this.hasPlugin("moduleAttributes")) {
  10967. this.expectPlugin("importAssertions");
  10968. }
  10969. }
  10970. }
  10971. if (node.arguments.length === 0 || node.arguments.length > 2) {
  10972. this.raise(Errors.ImportCallArity, {
  10973. at: node,
  10974. maxArgumentCount: this.hasPlugin("importAssertions") || this.hasPlugin("moduleAttributes") ? 2 : 1
  10975. });
  10976. } else {
  10977. for (const arg of node.arguments) {
  10978. if (arg.type === "SpreadElement") {
  10979. this.raise(Errors.ImportCallSpreadArgument, {
  10980. at: arg
  10981. });
  10982. }
  10983. }
  10984. }
  10985. }
  10986. return this.finishNode(node, optional ? "OptionalCallExpression" : "CallExpression");
  10987. }
  10988. parseCallExpressionArguments(close, dynamicImport, allowPlaceholder, nodeForExtra, refExpressionErrors) {
  10989. const elts = [];
  10990. let first = true;
  10991. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  10992. this.state.inFSharpPipelineDirectBody = false;
  10993. while (!this.eat(close)) {
  10994. if (first) {
  10995. first = false;
  10996. } else {
  10997. this.expect(12);
  10998. if (this.match(close)) {
  10999. if (dynamicImport && !this.hasPlugin("importAssertions") && !this.hasPlugin("moduleAttributes")) {
  11000. this.raise(Errors.ImportCallArgumentTrailingComma, {
  11001. at: this.state.lastTokStartLoc
  11002. });
  11003. }
  11004. if (nodeForExtra) {
  11005. this.addTrailingCommaExtraToNode(nodeForExtra);
  11006. }
  11007. this.next();
  11008. break;
  11009. }
  11010. }
  11011. elts.push(this.parseExprListItem(false, refExpressionErrors, allowPlaceholder));
  11012. }
  11013. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  11014. return elts;
  11015. }
  11016. shouldParseAsyncArrow() {
  11017. return this.match(19) && !this.canInsertSemicolon();
  11018. }
  11019. parseAsyncArrowFromCallExpression(node, call) {
  11020. var _call$extra;
  11021. this.resetPreviousNodeTrailingComments(call);
  11022. this.expect(19);
  11023. this.parseArrowExpression(node, call.arguments, true, (_call$extra = call.extra) == null ? void 0 : _call$extra.trailingCommaLoc);
  11024. if (call.innerComments) {
  11025. setInnerComments(node, call.innerComments);
  11026. }
  11027. if (call.callee.trailingComments) {
  11028. setInnerComments(node, call.callee.trailingComments);
  11029. }
  11030. return node;
  11031. }
  11032. parseNoCallExpr() {
  11033. const startLoc = this.state.startLoc;
  11034. return this.parseSubscripts(this.parseExprAtom(), startLoc, true);
  11035. }
  11036. parseExprAtom(refExpressionErrors) {
  11037. let node;
  11038. let decorators = null;
  11039. const {
  11040. type
  11041. } = this.state;
  11042. switch (type) {
  11043. case 79:
  11044. return this.parseSuper();
  11045. case 83:
  11046. node = this.startNode();
  11047. this.next();
  11048. if (this.match(16)) {
  11049. return this.parseImportMetaProperty(node);
  11050. }
  11051. if (!this.match(10)) {
  11052. this.raise(Errors.UnsupportedImport, {
  11053. at: this.state.lastTokStartLoc
  11054. });
  11055. }
  11056. return this.finishNode(node, "Import");
  11057. case 78:
  11058. node = this.startNode();
  11059. this.next();
  11060. return this.finishNode(node, "ThisExpression");
  11061. case 90:
  11062. {
  11063. return this.parseDo(this.startNode(), false);
  11064. }
  11065. case 56:
  11066. case 31:
  11067. {
  11068. this.readRegexp();
  11069. return this.parseRegExpLiteral(this.state.value);
  11070. }
  11071. case 132:
  11072. return this.parseNumericLiteral(this.state.value);
  11073. case 133:
  11074. return this.parseBigIntLiteral(this.state.value);
  11075. case 134:
  11076. return this.parseDecimalLiteral(this.state.value);
  11077. case 131:
  11078. return this.parseStringLiteral(this.state.value);
  11079. case 84:
  11080. return this.parseNullLiteral();
  11081. case 85:
  11082. return this.parseBooleanLiteral(true);
  11083. case 86:
  11084. return this.parseBooleanLiteral(false);
  11085. case 10:
  11086. {
  11087. const canBeArrow = this.state.potentialArrowAt === this.state.start;
  11088. return this.parseParenAndDistinguishExpression(canBeArrow);
  11089. }
  11090. case 2:
  11091. case 1:
  11092. {
  11093. return this.parseArrayLike(this.state.type === 2 ? 4 : 3, false, true);
  11094. }
  11095. case 0:
  11096. {
  11097. return this.parseArrayLike(3, true, false, refExpressionErrors);
  11098. }
  11099. case 6:
  11100. case 7:
  11101. {
  11102. return this.parseObjectLike(this.state.type === 6 ? 9 : 8, false, true);
  11103. }
  11104. case 5:
  11105. {
  11106. return this.parseObjectLike(8, false, false, refExpressionErrors);
  11107. }
  11108. case 68:
  11109. return this.parseFunctionOrFunctionSent();
  11110. case 26:
  11111. decorators = this.parseDecorators();
  11112. case 80:
  11113. return this.parseClass(this.maybeTakeDecorators(decorators, this.startNode()), false);
  11114. case 77:
  11115. return this.parseNewOrNewTarget();
  11116. case 25:
  11117. case 24:
  11118. return this.parseTemplate(false);
  11119. case 15:
  11120. {
  11121. node = this.startNode();
  11122. this.next();
  11123. node.object = null;
  11124. const callee = node.callee = this.parseNoCallExpr();
  11125. if (callee.type === "MemberExpression") {
  11126. return this.finishNode(node, "BindExpression");
  11127. } else {
  11128. throw this.raise(Errors.UnsupportedBind, {
  11129. at: callee
  11130. });
  11131. }
  11132. }
  11133. case 136:
  11134. {
  11135. this.raise(Errors.PrivateInExpectedIn, {
  11136. at: this.state.startLoc,
  11137. identifierName: this.state.value
  11138. });
  11139. return this.parsePrivateName();
  11140. }
  11141. case 33:
  11142. {
  11143. return this.parseTopicReferenceThenEqualsSign(54, "%");
  11144. }
  11145. case 32:
  11146. {
  11147. return this.parseTopicReferenceThenEqualsSign(44, "^");
  11148. }
  11149. case 37:
  11150. case 38:
  11151. {
  11152. return this.parseTopicReference("hack");
  11153. }
  11154. case 44:
  11155. case 54:
  11156. case 27:
  11157. {
  11158. const pipeProposal = this.getPluginOption("pipelineOperator", "proposal");
  11159. if (pipeProposal) {
  11160. return this.parseTopicReference(pipeProposal);
  11161. } else {
  11162. throw this.unexpected();
  11163. }
  11164. }
  11165. case 47:
  11166. {
  11167. const lookaheadCh = this.input.codePointAt(this.nextTokenStart());
  11168. if (isIdentifierStart(lookaheadCh) || lookaheadCh === 62) {
  11169. this.expectOnePlugin(["jsx", "flow", "typescript"]);
  11170. break;
  11171. } else {
  11172. throw this.unexpected();
  11173. }
  11174. }
  11175. default:
  11176. if (tokenIsIdentifier(type)) {
  11177. if (this.isContextual(125) && this.lookaheadCharCode() === 123 && !this.hasFollowingLineBreak()) {
  11178. return this.parseModuleExpression();
  11179. }
  11180. const canBeArrow = this.state.potentialArrowAt === this.state.start;
  11181. const containsEsc = this.state.containsEsc;
  11182. const id = this.parseIdentifier();
  11183. if (!containsEsc && id.name === "async" && !this.canInsertSemicolon()) {
  11184. const {
  11185. type
  11186. } = this.state;
  11187. if (type === 68) {
  11188. this.resetPreviousNodeTrailingComments(id);
  11189. this.next();
  11190. return this.parseAsyncFunctionExpression(this.startNodeAtNode(id));
  11191. } else if (tokenIsIdentifier(type)) {
  11192. if (this.lookaheadCharCode() === 61) {
  11193. return this.parseAsyncArrowUnaryFunction(this.startNodeAtNode(id));
  11194. } else {
  11195. return id;
  11196. }
  11197. } else if (type === 90) {
  11198. this.resetPreviousNodeTrailingComments(id);
  11199. return this.parseDo(this.startNodeAtNode(id), true);
  11200. }
  11201. }
  11202. if (canBeArrow && this.match(19) && !this.canInsertSemicolon()) {
  11203. this.next();
  11204. return this.parseArrowExpression(this.startNodeAtNode(id), [id], false);
  11205. }
  11206. return id;
  11207. } else {
  11208. throw this.unexpected();
  11209. }
  11210. }
  11211. }
  11212. parseTopicReferenceThenEqualsSign(topicTokenType, topicTokenValue) {
  11213. const pipeProposal = this.getPluginOption("pipelineOperator", "proposal");
  11214. if (pipeProposal) {
  11215. this.state.type = topicTokenType;
  11216. this.state.value = topicTokenValue;
  11217. this.state.pos--;
  11218. this.state.end--;
  11219. this.state.endLoc = createPositionWithColumnOffset(this.state.endLoc, -1);
  11220. return this.parseTopicReference(pipeProposal);
  11221. } else {
  11222. throw this.unexpected();
  11223. }
  11224. }
  11225. parseTopicReference(pipeProposal) {
  11226. const node = this.startNode();
  11227. const startLoc = this.state.startLoc;
  11228. const tokenType = this.state.type;
  11229. this.next();
  11230. return this.finishTopicReference(node, startLoc, pipeProposal, tokenType);
  11231. }
  11232. finishTopicReference(node, startLoc, pipeProposal, tokenType) {
  11233. if (this.testTopicReferenceConfiguration(pipeProposal, startLoc, tokenType)) {
  11234. const nodeType = pipeProposal === "smart" ? "PipelinePrimaryTopicReference" : "TopicReference";
  11235. if (!this.topicReferenceIsAllowedInCurrentContext()) {
  11236. this.raise(pipeProposal === "smart" ? Errors.PrimaryTopicNotAllowed : Errors.PipeTopicUnbound, {
  11237. at: startLoc
  11238. });
  11239. }
  11240. this.registerTopicReference();
  11241. return this.finishNode(node, nodeType);
  11242. } else {
  11243. throw this.raise(Errors.PipeTopicUnconfiguredToken, {
  11244. at: startLoc,
  11245. token: tokenLabelName(tokenType)
  11246. });
  11247. }
  11248. }
  11249. testTopicReferenceConfiguration(pipeProposal, startLoc, tokenType) {
  11250. switch (pipeProposal) {
  11251. case "hack":
  11252. {
  11253. return this.hasPlugin(["pipelineOperator", {
  11254. topicToken: tokenLabelName(tokenType)
  11255. }]);
  11256. }
  11257. case "smart":
  11258. return tokenType === 27;
  11259. default:
  11260. throw this.raise(Errors.PipeTopicRequiresHackPipes, {
  11261. at: startLoc
  11262. });
  11263. }
  11264. }
  11265. parseAsyncArrowUnaryFunction(node) {
  11266. this.prodParam.enter(functionFlags(true, this.prodParam.hasYield));
  11267. const params = [this.parseIdentifier()];
  11268. this.prodParam.exit();
  11269. if (this.hasPrecedingLineBreak()) {
  11270. this.raise(Errors.LineTerminatorBeforeArrow, {
  11271. at: this.state.curPosition()
  11272. });
  11273. }
  11274. this.expect(19);
  11275. return this.parseArrowExpression(node, params, true);
  11276. }
  11277. parseDo(node, isAsync) {
  11278. this.expectPlugin("doExpressions");
  11279. if (isAsync) {
  11280. this.expectPlugin("asyncDoExpressions");
  11281. }
  11282. node.async = isAsync;
  11283. this.next();
  11284. const oldLabels = this.state.labels;
  11285. this.state.labels = [];
  11286. if (isAsync) {
  11287. this.prodParam.enter(PARAM_AWAIT);
  11288. node.body = this.parseBlock();
  11289. this.prodParam.exit();
  11290. } else {
  11291. node.body = this.parseBlock();
  11292. }
  11293. this.state.labels = oldLabels;
  11294. return this.finishNode(node, "DoExpression");
  11295. }
  11296. parseSuper() {
  11297. const node = this.startNode();
  11298. this.next();
  11299. if (this.match(10) && !this.scope.allowDirectSuper && !this.options.allowSuperOutsideMethod) {
  11300. this.raise(Errors.SuperNotAllowed, {
  11301. at: node
  11302. });
  11303. } else if (!this.scope.allowSuper && !this.options.allowSuperOutsideMethod) {
  11304. this.raise(Errors.UnexpectedSuper, {
  11305. at: node
  11306. });
  11307. }
  11308. if (!this.match(10) && !this.match(0) && !this.match(16)) {
  11309. this.raise(Errors.UnsupportedSuper, {
  11310. at: node
  11311. });
  11312. }
  11313. return this.finishNode(node, "Super");
  11314. }
  11315. parsePrivateName() {
  11316. const node = this.startNode();
  11317. const id = this.startNodeAt(createPositionWithColumnOffset(this.state.startLoc, 1));
  11318. const name = this.state.value;
  11319. this.next();
  11320. node.id = this.createIdentifier(id, name);
  11321. return this.finishNode(node, "PrivateName");
  11322. }
  11323. parseFunctionOrFunctionSent() {
  11324. const node = this.startNode();
  11325. this.next();
  11326. if (this.prodParam.hasYield && this.match(16)) {
  11327. const meta = this.createIdentifier(this.startNodeAtNode(node), "function");
  11328. this.next();
  11329. if (this.match(102)) {
  11330. this.expectPlugin("functionSent");
  11331. } else if (!this.hasPlugin("functionSent")) {
  11332. this.unexpected();
  11333. }
  11334. return this.parseMetaProperty(node, meta, "sent");
  11335. }
  11336. return this.parseFunction(node);
  11337. }
  11338. parseMetaProperty(node, meta, propertyName) {
  11339. node.meta = meta;
  11340. const containsEsc = this.state.containsEsc;
  11341. node.property = this.parseIdentifier(true);
  11342. if (node.property.name !== propertyName || containsEsc) {
  11343. this.raise(Errors.UnsupportedMetaProperty, {
  11344. at: node.property,
  11345. target: meta.name,
  11346. onlyValidPropertyName: propertyName
  11347. });
  11348. }
  11349. return this.finishNode(node, "MetaProperty");
  11350. }
  11351. parseImportMetaProperty(node) {
  11352. const id = this.createIdentifier(this.startNodeAtNode(node), "import");
  11353. this.next();
  11354. if (this.isContextual(100)) {
  11355. if (!this.inModule) {
  11356. this.raise(Errors.ImportMetaOutsideModule, {
  11357. at: id
  11358. });
  11359. }
  11360. this.sawUnambiguousESM = true;
  11361. }
  11362. return this.parseMetaProperty(node, id, "meta");
  11363. }
  11364. parseLiteralAtNode(value, type, node) {
  11365. this.addExtra(node, "rawValue", value);
  11366. this.addExtra(node, "raw", this.input.slice(node.start, this.state.end));
  11367. node.value = value;
  11368. this.next();
  11369. return this.finishNode(node, type);
  11370. }
  11371. parseLiteral(value, type) {
  11372. const node = this.startNode();
  11373. return this.parseLiteralAtNode(value, type, node);
  11374. }
  11375. parseStringLiteral(value) {
  11376. return this.parseLiteral(value, "StringLiteral");
  11377. }
  11378. parseNumericLiteral(value) {
  11379. return this.parseLiteral(value, "NumericLiteral");
  11380. }
  11381. parseBigIntLiteral(value) {
  11382. return this.parseLiteral(value, "BigIntLiteral");
  11383. }
  11384. parseDecimalLiteral(value) {
  11385. return this.parseLiteral(value, "DecimalLiteral");
  11386. }
  11387. parseRegExpLiteral(value) {
  11388. const node = this.parseLiteral(value.value, "RegExpLiteral");
  11389. node.pattern = value.pattern;
  11390. node.flags = value.flags;
  11391. return node;
  11392. }
  11393. parseBooleanLiteral(value) {
  11394. const node = this.startNode();
  11395. node.value = value;
  11396. this.next();
  11397. return this.finishNode(node, "BooleanLiteral");
  11398. }
  11399. parseNullLiteral() {
  11400. const node = this.startNode();
  11401. this.next();
  11402. return this.finishNode(node, "NullLiteral");
  11403. }
  11404. parseParenAndDistinguishExpression(canBeArrow) {
  11405. const startLoc = this.state.startLoc;
  11406. let val;
  11407. this.next();
  11408. this.expressionScope.enter(newArrowHeadScope());
  11409. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  11410. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  11411. this.state.maybeInArrowParameters = true;
  11412. this.state.inFSharpPipelineDirectBody = false;
  11413. const innerStartLoc = this.state.startLoc;
  11414. const exprList = [];
  11415. const refExpressionErrors = new ExpressionErrors();
  11416. let first = true;
  11417. let spreadStartLoc;
  11418. let optionalCommaStartLoc;
  11419. while (!this.match(11)) {
  11420. if (first) {
  11421. first = false;
  11422. } else {
  11423. this.expect(12, refExpressionErrors.optionalParametersLoc === null ? null : refExpressionErrors.optionalParametersLoc);
  11424. if (this.match(11)) {
  11425. optionalCommaStartLoc = this.state.startLoc;
  11426. break;
  11427. }
  11428. }
  11429. if (this.match(21)) {
  11430. const spreadNodeStartLoc = this.state.startLoc;
  11431. spreadStartLoc = this.state.startLoc;
  11432. exprList.push(this.parseParenItem(this.parseRestBinding(), spreadNodeStartLoc));
  11433. if (!this.checkCommaAfterRest(41)) {
  11434. break;
  11435. }
  11436. } else {
  11437. exprList.push(this.parseMaybeAssignAllowIn(refExpressionErrors, this.parseParenItem));
  11438. }
  11439. }
  11440. const innerEndLoc = this.state.lastTokEndLoc;
  11441. this.expect(11);
  11442. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  11443. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  11444. let arrowNode = this.startNodeAt(startLoc);
  11445. if (canBeArrow && this.shouldParseArrow(exprList) && (arrowNode = this.parseArrow(arrowNode))) {
  11446. this.checkDestructuringPrivate(refExpressionErrors);
  11447. this.expressionScope.validateAsPattern();
  11448. this.expressionScope.exit();
  11449. this.parseArrowExpression(arrowNode, exprList, false);
  11450. return arrowNode;
  11451. }
  11452. this.expressionScope.exit();
  11453. if (!exprList.length) {
  11454. this.unexpected(this.state.lastTokStartLoc);
  11455. }
  11456. if (optionalCommaStartLoc) this.unexpected(optionalCommaStartLoc);
  11457. if (spreadStartLoc) this.unexpected(spreadStartLoc);
  11458. this.checkExpressionErrors(refExpressionErrors, true);
  11459. this.toReferencedListDeep(exprList, true);
  11460. if (exprList.length > 1) {
  11461. val = this.startNodeAt(innerStartLoc);
  11462. val.expressions = exprList;
  11463. this.finishNode(val, "SequenceExpression");
  11464. this.resetEndLocation(val, innerEndLoc);
  11465. } else {
  11466. val = exprList[0];
  11467. }
  11468. return this.wrapParenthesis(startLoc, val);
  11469. }
  11470. wrapParenthesis(startLoc, expression) {
  11471. if (!this.options.createParenthesizedExpressions) {
  11472. this.addExtra(expression, "parenthesized", true);
  11473. this.addExtra(expression, "parenStart", startLoc.index);
  11474. this.takeSurroundingComments(expression, startLoc.index, this.state.lastTokEndLoc.index);
  11475. return expression;
  11476. }
  11477. const parenExpression = this.startNodeAt(startLoc);
  11478. parenExpression.expression = expression;
  11479. return this.finishNode(parenExpression, "ParenthesizedExpression");
  11480. }
  11481. shouldParseArrow(params) {
  11482. return !this.canInsertSemicolon();
  11483. }
  11484. parseArrow(node) {
  11485. if (this.eat(19)) {
  11486. return node;
  11487. }
  11488. }
  11489. parseParenItem(node, startLoc) {
  11490. return node;
  11491. }
  11492. parseNewOrNewTarget() {
  11493. const node = this.startNode();
  11494. this.next();
  11495. if (this.match(16)) {
  11496. const meta = this.createIdentifier(this.startNodeAtNode(node), "new");
  11497. this.next();
  11498. const metaProp = this.parseMetaProperty(node, meta, "target");
  11499. if (!this.scope.inNonArrowFunction && !this.scope.inClass && !this.options.allowNewTargetOutsideFunction) {
  11500. this.raise(Errors.UnexpectedNewTarget, {
  11501. at: metaProp
  11502. });
  11503. }
  11504. return metaProp;
  11505. }
  11506. return this.parseNew(node);
  11507. }
  11508. parseNew(node) {
  11509. this.parseNewCallee(node);
  11510. if (this.eat(10)) {
  11511. const args = this.parseExprList(11);
  11512. this.toReferencedList(args);
  11513. node.arguments = args;
  11514. } else {
  11515. node.arguments = [];
  11516. }
  11517. return this.finishNode(node, "NewExpression");
  11518. }
  11519. parseNewCallee(node) {
  11520. var _node$callee$extra;
  11521. node.callee = this.parseNoCallExpr();
  11522. if (node.callee.type === "Import") {
  11523. this.raise(Errors.ImportCallNotNewExpression, {
  11524. at: node.callee
  11525. });
  11526. } else if (this.isOptionalChain(node.callee) && !((_node$callee$extra = node.callee.extra) != null && _node$callee$extra.parenthesized)) {
  11527. this.raise(Errors.OptionalChainingNoNew, {
  11528. at: this.state.lastTokEndLoc
  11529. });
  11530. } else if (this.eat(18)) {
  11531. this.raise(Errors.OptionalChainingNoNew, {
  11532. at: this.state.startLoc
  11533. });
  11534. }
  11535. }
  11536. parseTemplateElement(isTagged) {
  11537. const {
  11538. start,
  11539. startLoc,
  11540. end,
  11541. value
  11542. } = this.state;
  11543. const elemStart = start + 1;
  11544. const elem = this.startNodeAt(createPositionWithColumnOffset(startLoc, 1));
  11545. if (value === null) {
  11546. if (!isTagged) {
  11547. this.raise(Errors.InvalidEscapeSequenceTemplate, {
  11548. at: createPositionWithColumnOffset(this.state.firstInvalidTemplateEscapePos, 1)
  11549. });
  11550. }
  11551. }
  11552. const isTail = this.match(24);
  11553. const endOffset = isTail ? -1 : -2;
  11554. const elemEnd = end + endOffset;
  11555. elem.value = {
  11556. raw: this.input.slice(elemStart, elemEnd).replace(/\r\n?/g, "\n"),
  11557. cooked: value === null ? null : value.slice(1, endOffset)
  11558. };
  11559. elem.tail = isTail;
  11560. this.next();
  11561. const finishedNode = this.finishNode(elem, "TemplateElement");
  11562. this.resetEndLocation(finishedNode, createPositionWithColumnOffset(this.state.lastTokEndLoc, endOffset));
  11563. return finishedNode;
  11564. }
  11565. parseTemplate(isTagged) {
  11566. const node = this.startNode();
  11567. node.expressions = [];
  11568. let curElt = this.parseTemplateElement(isTagged);
  11569. node.quasis = [curElt];
  11570. while (!curElt.tail) {
  11571. node.expressions.push(this.parseTemplateSubstitution());
  11572. this.readTemplateContinuation();
  11573. node.quasis.push(curElt = this.parseTemplateElement(isTagged));
  11574. }
  11575. return this.finishNode(node, "TemplateLiteral");
  11576. }
  11577. parseTemplateSubstitution() {
  11578. return this.parseExpression();
  11579. }
  11580. parseObjectLike(close, isPattern, isRecord, refExpressionErrors) {
  11581. if (isRecord) {
  11582. this.expectPlugin("recordAndTuple");
  11583. }
  11584. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  11585. this.state.inFSharpPipelineDirectBody = false;
  11586. const propHash = Object.create(null);
  11587. let first = true;
  11588. const node = this.startNode();
  11589. node.properties = [];
  11590. this.next();
  11591. while (!this.match(close)) {
  11592. if (first) {
  11593. first = false;
  11594. } else {
  11595. this.expect(12);
  11596. if (this.match(close)) {
  11597. this.addTrailingCommaExtraToNode(node);
  11598. break;
  11599. }
  11600. }
  11601. let prop;
  11602. if (isPattern) {
  11603. prop = this.parseBindingProperty();
  11604. } else {
  11605. prop = this.parsePropertyDefinition(refExpressionErrors);
  11606. this.checkProto(prop, isRecord, propHash, refExpressionErrors);
  11607. }
  11608. if (isRecord && !this.isObjectProperty(prop) && prop.type !== "SpreadElement") {
  11609. this.raise(Errors.InvalidRecordProperty, {
  11610. at: prop
  11611. });
  11612. }
  11613. if (prop.shorthand) {
  11614. this.addExtra(prop, "shorthand", true);
  11615. }
  11616. node.properties.push(prop);
  11617. }
  11618. this.next();
  11619. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  11620. let type = "ObjectExpression";
  11621. if (isPattern) {
  11622. type = "ObjectPattern";
  11623. } else if (isRecord) {
  11624. type = "RecordExpression";
  11625. }
  11626. return this.finishNode(node, type);
  11627. }
  11628. addTrailingCommaExtraToNode(node) {
  11629. this.addExtra(node, "trailingComma", this.state.lastTokStart);
  11630. this.addExtra(node, "trailingCommaLoc", this.state.lastTokStartLoc, false);
  11631. }
  11632. maybeAsyncOrAccessorProp(prop) {
  11633. return !prop.computed && prop.key.type === "Identifier" && (this.isLiteralPropertyName() || this.match(0) || this.match(55));
  11634. }
  11635. parsePropertyDefinition(refExpressionErrors) {
  11636. let decorators = [];
  11637. if (this.match(26)) {
  11638. if (this.hasPlugin("decorators")) {
  11639. this.raise(Errors.UnsupportedPropertyDecorator, {
  11640. at: this.state.startLoc
  11641. });
  11642. }
  11643. while (this.match(26)) {
  11644. decorators.push(this.parseDecorator());
  11645. }
  11646. }
  11647. const prop = this.startNode();
  11648. let isAsync = false;
  11649. let isAccessor = false;
  11650. let startLoc;
  11651. if (this.match(21)) {
  11652. if (decorators.length) this.unexpected();
  11653. return this.parseSpread();
  11654. }
  11655. if (decorators.length) {
  11656. prop.decorators = decorators;
  11657. decorators = [];
  11658. }
  11659. prop.method = false;
  11660. if (refExpressionErrors) {
  11661. startLoc = this.state.startLoc;
  11662. }
  11663. let isGenerator = this.eat(55);
  11664. this.parsePropertyNamePrefixOperator(prop);
  11665. const containsEsc = this.state.containsEsc;
  11666. const key = this.parsePropertyName(prop, refExpressionErrors);
  11667. if (!isGenerator && !containsEsc && this.maybeAsyncOrAccessorProp(prop)) {
  11668. const keyName = key.name;
  11669. if (keyName === "async" && !this.hasPrecedingLineBreak()) {
  11670. isAsync = true;
  11671. this.resetPreviousNodeTrailingComments(key);
  11672. isGenerator = this.eat(55);
  11673. this.parsePropertyName(prop);
  11674. }
  11675. if (keyName === "get" || keyName === "set") {
  11676. isAccessor = true;
  11677. this.resetPreviousNodeTrailingComments(key);
  11678. prop.kind = keyName;
  11679. if (this.match(55)) {
  11680. isGenerator = true;
  11681. this.raise(Errors.AccessorIsGenerator, {
  11682. at: this.state.curPosition(),
  11683. kind: keyName
  11684. });
  11685. this.next();
  11686. }
  11687. this.parsePropertyName(prop);
  11688. }
  11689. }
  11690. return this.parseObjPropValue(prop, startLoc, isGenerator, isAsync, false, isAccessor, refExpressionErrors);
  11691. }
  11692. getGetterSetterExpectedParamCount(method) {
  11693. return method.kind === "get" ? 0 : 1;
  11694. }
  11695. getObjectOrClassMethodParams(method) {
  11696. return method.params;
  11697. }
  11698. checkGetterSetterParams(method) {
  11699. var _params;
  11700. const paramCount = this.getGetterSetterExpectedParamCount(method);
  11701. const params = this.getObjectOrClassMethodParams(method);
  11702. if (params.length !== paramCount) {
  11703. this.raise(method.kind === "get" ? Errors.BadGetterArity : Errors.BadSetterArity, {
  11704. at: method
  11705. });
  11706. }
  11707. if (method.kind === "set" && ((_params = params[params.length - 1]) == null ? void 0 : _params.type) === "RestElement") {
  11708. this.raise(Errors.BadSetterRestParameter, {
  11709. at: method
  11710. });
  11711. }
  11712. }
  11713. parseObjectMethod(prop, isGenerator, isAsync, isPattern, isAccessor) {
  11714. if (isAccessor) {
  11715. const finishedProp = this.parseMethod(prop, isGenerator, false, false, false, "ObjectMethod");
  11716. this.checkGetterSetterParams(finishedProp);
  11717. return finishedProp;
  11718. }
  11719. if (isAsync || isGenerator || this.match(10)) {
  11720. if (isPattern) this.unexpected();
  11721. prop.kind = "method";
  11722. prop.method = true;
  11723. return this.parseMethod(prop, isGenerator, isAsync, false, false, "ObjectMethod");
  11724. }
  11725. }
  11726. parseObjectProperty(prop, startLoc, isPattern, refExpressionErrors) {
  11727. prop.shorthand = false;
  11728. if (this.eat(14)) {
  11729. prop.value = isPattern ? this.parseMaybeDefault(this.state.startLoc) : this.parseMaybeAssignAllowIn(refExpressionErrors);
  11730. return this.finishNode(prop, "ObjectProperty");
  11731. }
  11732. if (!prop.computed && prop.key.type === "Identifier") {
  11733. this.checkReservedWord(prop.key.name, prop.key.loc.start, true, false);
  11734. if (isPattern) {
  11735. prop.value = this.parseMaybeDefault(startLoc, cloneIdentifier(prop.key));
  11736. } else if (this.match(29)) {
  11737. const shorthandAssignLoc = this.state.startLoc;
  11738. if (refExpressionErrors != null) {
  11739. if (refExpressionErrors.shorthandAssignLoc === null) {
  11740. refExpressionErrors.shorthandAssignLoc = shorthandAssignLoc;
  11741. }
  11742. } else {
  11743. this.raise(Errors.InvalidCoverInitializedName, {
  11744. at: shorthandAssignLoc
  11745. });
  11746. }
  11747. prop.value = this.parseMaybeDefault(startLoc, cloneIdentifier(prop.key));
  11748. } else {
  11749. prop.value = cloneIdentifier(prop.key);
  11750. }
  11751. prop.shorthand = true;
  11752. return this.finishNode(prop, "ObjectProperty");
  11753. }
  11754. }
  11755. parseObjPropValue(prop, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors) {
  11756. const node = this.parseObjectMethod(prop, isGenerator, isAsync, isPattern, isAccessor) || this.parseObjectProperty(prop, startLoc, isPattern, refExpressionErrors);
  11757. if (!node) this.unexpected();
  11758. return node;
  11759. }
  11760. parsePropertyName(prop, refExpressionErrors) {
  11761. if (this.eat(0)) {
  11762. prop.computed = true;
  11763. prop.key = this.parseMaybeAssignAllowIn();
  11764. this.expect(3);
  11765. } else {
  11766. const {
  11767. type,
  11768. value
  11769. } = this.state;
  11770. let key;
  11771. if (tokenIsKeywordOrIdentifier(type)) {
  11772. key = this.parseIdentifier(true);
  11773. } else {
  11774. switch (type) {
  11775. case 132:
  11776. key = this.parseNumericLiteral(value);
  11777. break;
  11778. case 131:
  11779. key = this.parseStringLiteral(value);
  11780. break;
  11781. case 133:
  11782. key = this.parseBigIntLiteral(value);
  11783. break;
  11784. case 134:
  11785. key = this.parseDecimalLiteral(value);
  11786. break;
  11787. case 136:
  11788. {
  11789. const privateKeyLoc = this.state.startLoc;
  11790. if (refExpressionErrors != null) {
  11791. if (refExpressionErrors.privateKeyLoc === null) {
  11792. refExpressionErrors.privateKeyLoc = privateKeyLoc;
  11793. }
  11794. } else {
  11795. this.raise(Errors.UnexpectedPrivateField, {
  11796. at: privateKeyLoc
  11797. });
  11798. }
  11799. key = this.parsePrivateName();
  11800. break;
  11801. }
  11802. default:
  11803. throw this.unexpected();
  11804. }
  11805. }
  11806. prop.key = key;
  11807. if (type !== 136) {
  11808. prop.computed = false;
  11809. }
  11810. }
  11811. return prop.key;
  11812. }
  11813. initFunction(node, isAsync) {
  11814. node.id = null;
  11815. node.generator = false;
  11816. node.async = isAsync;
  11817. }
  11818. parseMethod(node, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope = false) {
  11819. this.initFunction(node, isAsync);
  11820. node.generator = isGenerator;
  11821. this.scope.enter(SCOPE_FUNCTION | SCOPE_SUPER | (inClassScope ? SCOPE_CLASS : 0) | (allowDirectSuper ? SCOPE_DIRECT_SUPER : 0));
  11822. this.prodParam.enter(functionFlags(isAsync, node.generator));
  11823. this.parseFunctionParams(node, isConstructor);
  11824. const finishedNode = this.parseFunctionBodyAndFinish(node, type, true);
  11825. this.prodParam.exit();
  11826. this.scope.exit();
  11827. return finishedNode;
  11828. }
  11829. parseArrayLike(close, canBePattern, isTuple, refExpressionErrors) {
  11830. if (isTuple) {
  11831. this.expectPlugin("recordAndTuple");
  11832. }
  11833. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  11834. this.state.inFSharpPipelineDirectBody = false;
  11835. const node = this.startNode();
  11836. this.next();
  11837. node.elements = this.parseExprList(close, !isTuple, refExpressionErrors, node);
  11838. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  11839. return this.finishNode(node, isTuple ? "TupleExpression" : "ArrayExpression");
  11840. }
  11841. parseArrowExpression(node, params, isAsync, trailingCommaLoc) {
  11842. this.scope.enter(SCOPE_FUNCTION | SCOPE_ARROW);
  11843. let flags = functionFlags(isAsync, false);
  11844. if (!this.match(5) && this.prodParam.hasIn) {
  11845. flags |= PARAM_IN;
  11846. }
  11847. this.prodParam.enter(flags);
  11848. this.initFunction(node, isAsync);
  11849. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  11850. if (params) {
  11851. this.state.maybeInArrowParameters = true;
  11852. this.setArrowFunctionParameters(node, params, trailingCommaLoc);
  11853. }
  11854. this.state.maybeInArrowParameters = false;
  11855. this.parseFunctionBody(node, true);
  11856. this.prodParam.exit();
  11857. this.scope.exit();
  11858. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  11859. return this.finishNode(node, "ArrowFunctionExpression");
  11860. }
  11861. setArrowFunctionParameters(node, params, trailingCommaLoc) {
  11862. this.toAssignableList(params, trailingCommaLoc, false);
  11863. node.params = params;
  11864. }
  11865. parseFunctionBodyAndFinish(node, type, isMethod = false) {
  11866. this.parseFunctionBody(node, false, isMethod);
  11867. return this.finishNode(node, type);
  11868. }
  11869. parseFunctionBody(node, allowExpression, isMethod = false) {
  11870. const isExpression = allowExpression && !this.match(5);
  11871. this.expressionScope.enter(newExpressionScope());
  11872. if (isExpression) {
  11873. node.body = this.parseMaybeAssign();
  11874. this.checkParams(node, false, allowExpression, false);
  11875. } else {
  11876. const oldStrict = this.state.strict;
  11877. const oldLabels = this.state.labels;
  11878. this.state.labels = [];
  11879. this.prodParam.enter(this.prodParam.currentFlags() | PARAM_RETURN);
  11880. node.body = this.parseBlock(true, false, hasStrictModeDirective => {
  11881. const nonSimple = !this.isSimpleParamList(node.params);
  11882. if (hasStrictModeDirective && nonSimple) {
  11883. this.raise(Errors.IllegalLanguageModeDirective, {
  11884. at: (node.kind === "method" || node.kind === "constructor") && !!node.key ? node.key.loc.end : node
  11885. });
  11886. }
  11887. const strictModeChanged = !oldStrict && this.state.strict;
  11888. this.checkParams(node, !this.state.strict && !allowExpression && !isMethod && !nonSimple, allowExpression, strictModeChanged);
  11889. if (this.state.strict && node.id) {
  11890. this.checkIdentifier(node.id, BIND_OUTSIDE, strictModeChanged);
  11891. }
  11892. });
  11893. this.prodParam.exit();
  11894. this.state.labels = oldLabels;
  11895. }
  11896. this.expressionScope.exit();
  11897. }
  11898. isSimpleParameter(node) {
  11899. return node.type === "Identifier";
  11900. }
  11901. isSimpleParamList(params) {
  11902. for (let i = 0, len = params.length; i < len; i++) {
  11903. if (!this.isSimpleParameter(params[i])) return false;
  11904. }
  11905. return true;
  11906. }
  11907. checkParams(node, allowDuplicates, isArrowFunction, strictModeChanged = true) {
  11908. const checkClashes = !allowDuplicates && new Set();
  11909. const formalParameters = {
  11910. type: "FormalParameters"
  11911. };
  11912. for (const param of node.params) {
  11913. this.checkLVal(param, {
  11914. in: formalParameters,
  11915. binding: BIND_VAR,
  11916. checkClashes,
  11917. strictModeChanged
  11918. });
  11919. }
  11920. }
  11921. parseExprList(close, allowEmpty, refExpressionErrors, nodeForExtra) {
  11922. const elts = [];
  11923. let first = true;
  11924. while (!this.eat(close)) {
  11925. if (first) {
  11926. first = false;
  11927. } else {
  11928. this.expect(12);
  11929. if (this.match(close)) {
  11930. if (nodeForExtra) {
  11931. this.addTrailingCommaExtraToNode(nodeForExtra);
  11932. }
  11933. this.next();
  11934. break;
  11935. }
  11936. }
  11937. elts.push(this.parseExprListItem(allowEmpty, refExpressionErrors));
  11938. }
  11939. return elts;
  11940. }
  11941. parseExprListItem(allowEmpty, refExpressionErrors, allowPlaceholder) {
  11942. let elt;
  11943. if (this.match(12)) {
  11944. if (!allowEmpty) {
  11945. this.raise(Errors.UnexpectedToken, {
  11946. at: this.state.curPosition(),
  11947. unexpected: ","
  11948. });
  11949. }
  11950. elt = null;
  11951. } else if (this.match(21)) {
  11952. const spreadNodeStartLoc = this.state.startLoc;
  11953. elt = this.parseParenItem(this.parseSpread(refExpressionErrors), spreadNodeStartLoc);
  11954. } else if (this.match(17)) {
  11955. this.expectPlugin("partialApplication");
  11956. if (!allowPlaceholder) {
  11957. this.raise(Errors.UnexpectedArgumentPlaceholder, {
  11958. at: this.state.startLoc
  11959. });
  11960. }
  11961. const node = this.startNode();
  11962. this.next();
  11963. elt = this.finishNode(node, "ArgumentPlaceholder");
  11964. } else {
  11965. elt = this.parseMaybeAssignAllowIn(refExpressionErrors, this.parseParenItem);
  11966. }
  11967. return elt;
  11968. }
  11969. parseIdentifier(liberal) {
  11970. const node = this.startNode();
  11971. const name = this.parseIdentifierName(liberal);
  11972. return this.createIdentifier(node, name);
  11973. }
  11974. createIdentifier(node, name) {
  11975. node.name = name;
  11976. node.loc.identifierName = name;
  11977. return this.finishNode(node, "Identifier");
  11978. }
  11979. parseIdentifierName(liberal) {
  11980. let name;
  11981. const {
  11982. startLoc,
  11983. type
  11984. } = this.state;
  11985. if (tokenIsKeywordOrIdentifier(type)) {
  11986. name = this.state.value;
  11987. } else {
  11988. throw this.unexpected();
  11989. }
  11990. const tokenIsKeyword = tokenKeywordOrIdentifierIsKeyword(type);
  11991. if (liberal) {
  11992. if (tokenIsKeyword) {
  11993. this.replaceToken(130);
  11994. }
  11995. } else {
  11996. this.checkReservedWord(name, startLoc, tokenIsKeyword, false);
  11997. }
  11998. this.next();
  11999. return name;
  12000. }
  12001. checkReservedWord(word, startLoc, checkKeywords, isBinding) {
  12002. if (word.length > 10) {
  12003. return;
  12004. }
  12005. if (!canBeReservedWord(word)) {
  12006. return;
  12007. }
  12008. if (checkKeywords && isKeyword(word)) {
  12009. this.raise(Errors.UnexpectedKeyword, {
  12010. at: startLoc,
  12011. keyword: word
  12012. });
  12013. return;
  12014. }
  12015. const reservedTest = !this.state.strict ? isReservedWord : isBinding ? isStrictBindReservedWord : isStrictReservedWord;
  12016. if (reservedTest(word, this.inModule)) {
  12017. this.raise(Errors.UnexpectedReservedWord, {
  12018. at: startLoc,
  12019. reservedWord: word
  12020. });
  12021. return;
  12022. } else if (word === "yield") {
  12023. if (this.prodParam.hasYield) {
  12024. this.raise(Errors.YieldBindingIdentifier, {
  12025. at: startLoc
  12026. });
  12027. return;
  12028. }
  12029. } else if (word === "await") {
  12030. if (this.prodParam.hasAwait) {
  12031. this.raise(Errors.AwaitBindingIdentifier, {
  12032. at: startLoc
  12033. });
  12034. return;
  12035. }
  12036. if (this.scope.inStaticBlock) {
  12037. this.raise(Errors.AwaitBindingIdentifierInStaticBlock, {
  12038. at: startLoc
  12039. });
  12040. return;
  12041. }
  12042. this.expressionScope.recordAsyncArrowParametersError({
  12043. at: startLoc
  12044. });
  12045. } else if (word === "arguments") {
  12046. if (this.scope.inClassAndNotInNonArrowFunction) {
  12047. this.raise(Errors.ArgumentsInClass, {
  12048. at: startLoc
  12049. });
  12050. return;
  12051. }
  12052. }
  12053. }
  12054. isAwaitAllowed() {
  12055. if (this.prodParam.hasAwait) return true;
  12056. if (this.options.allowAwaitOutsideFunction && !this.scope.inFunction) {
  12057. return true;
  12058. }
  12059. return false;
  12060. }
  12061. parseAwait(startLoc) {
  12062. const node = this.startNodeAt(startLoc);
  12063. this.expressionScope.recordParameterInitializerError(Errors.AwaitExpressionFormalParameter, {
  12064. at: node
  12065. });
  12066. if (this.eat(55)) {
  12067. this.raise(Errors.ObsoleteAwaitStar, {
  12068. at: node
  12069. });
  12070. }
  12071. if (!this.scope.inFunction && !this.options.allowAwaitOutsideFunction) {
  12072. if (this.isAmbiguousAwait()) {
  12073. this.ambiguousScriptDifferentAst = true;
  12074. } else {
  12075. this.sawUnambiguousESM = true;
  12076. }
  12077. }
  12078. if (!this.state.soloAwait) {
  12079. node.argument = this.parseMaybeUnary(null, true);
  12080. }
  12081. return this.finishNode(node, "AwaitExpression");
  12082. }
  12083. isAmbiguousAwait() {
  12084. if (this.hasPrecedingLineBreak()) return true;
  12085. const {
  12086. type
  12087. } = this.state;
  12088. return type === 53 || type === 10 || type === 0 || tokenIsTemplate(type) || type === 101 && !this.state.containsEsc || type === 135 || type === 56 || this.hasPlugin("v8intrinsic") && type === 54;
  12089. }
  12090. parseYield() {
  12091. const node = this.startNode();
  12092. this.expressionScope.recordParameterInitializerError(Errors.YieldInParameter, {
  12093. at: node
  12094. });
  12095. this.next();
  12096. let delegating = false;
  12097. let argument = null;
  12098. if (!this.hasPrecedingLineBreak()) {
  12099. delegating = this.eat(55);
  12100. switch (this.state.type) {
  12101. case 13:
  12102. case 137:
  12103. case 8:
  12104. case 11:
  12105. case 3:
  12106. case 9:
  12107. case 14:
  12108. case 12:
  12109. if (!delegating) break;
  12110. default:
  12111. argument = this.parseMaybeAssign();
  12112. }
  12113. }
  12114. node.delegate = delegating;
  12115. node.argument = argument;
  12116. return this.finishNode(node, "YieldExpression");
  12117. }
  12118. checkPipelineAtInfixOperator(left, leftStartLoc) {
  12119. if (this.hasPlugin(["pipelineOperator", {
  12120. proposal: "smart"
  12121. }])) {
  12122. if (left.type === "SequenceExpression") {
  12123. this.raise(Errors.PipelineHeadSequenceExpression, {
  12124. at: leftStartLoc
  12125. });
  12126. }
  12127. }
  12128. }
  12129. parseSmartPipelineBodyInStyle(childExpr, startLoc) {
  12130. if (this.isSimpleReference(childExpr)) {
  12131. const bodyNode = this.startNodeAt(startLoc);
  12132. bodyNode.callee = childExpr;
  12133. return this.finishNode(bodyNode, "PipelineBareFunction");
  12134. } else {
  12135. const bodyNode = this.startNodeAt(startLoc);
  12136. this.checkSmartPipeTopicBodyEarlyErrors(startLoc);
  12137. bodyNode.expression = childExpr;
  12138. return this.finishNode(bodyNode, "PipelineTopicExpression");
  12139. }
  12140. }
  12141. isSimpleReference(expression) {
  12142. switch (expression.type) {
  12143. case "MemberExpression":
  12144. return !expression.computed && this.isSimpleReference(expression.object);
  12145. case "Identifier":
  12146. return true;
  12147. default:
  12148. return false;
  12149. }
  12150. }
  12151. checkSmartPipeTopicBodyEarlyErrors(startLoc) {
  12152. if (this.match(19)) {
  12153. throw this.raise(Errors.PipelineBodyNoArrow, {
  12154. at: this.state.startLoc
  12155. });
  12156. }
  12157. if (!this.topicReferenceWasUsedInCurrentContext()) {
  12158. this.raise(Errors.PipelineTopicUnused, {
  12159. at: startLoc
  12160. });
  12161. }
  12162. }
  12163. withTopicBindingContext(callback) {
  12164. const outerContextTopicState = this.state.topicContext;
  12165. this.state.topicContext = {
  12166. maxNumOfResolvableTopics: 1,
  12167. maxTopicIndex: null
  12168. };
  12169. try {
  12170. return callback();
  12171. } finally {
  12172. this.state.topicContext = outerContextTopicState;
  12173. }
  12174. }
  12175. withSmartMixTopicForbiddingContext(callback) {
  12176. if (this.hasPlugin(["pipelineOperator", {
  12177. proposal: "smart"
  12178. }])) {
  12179. const outerContextTopicState = this.state.topicContext;
  12180. this.state.topicContext = {
  12181. maxNumOfResolvableTopics: 0,
  12182. maxTopicIndex: null
  12183. };
  12184. try {
  12185. return callback();
  12186. } finally {
  12187. this.state.topicContext = outerContextTopicState;
  12188. }
  12189. } else {
  12190. return callback();
  12191. }
  12192. }
  12193. withSoloAwaitPermittingContext(callback) {
  12194. const outerContextSoloAwaitState = this.state.soloAwait;
  12195. this.state.soloAwait = true;
  12196. try {
  12197. return callback();
  12198. } finally {
  12199. this.state.soloAwait = outerContextSoloAwaitState;
  12200. }
  12201. }
  12202. allowInAnd(callback) {
  12203. const flags = this.prodParam.currentFlags();
  12204. const prodParamToSet = PARAM_IN & ~flags;
  12205. if (prodParamToSet) {
  12206. this.prodParam.enter(flags | PARAM_IN);
  12207. try {
  12208. return callback();
  12209. } finally {
  12210. this.prodParam.exit();
  12211. }
  12212. }
  12213. return callback();
  12214. }
  12215. disallowInAnd(callback) {
  12216. const flags = this.prodParam.currentFlags();
  12217. const prodParamToClear = PARAM_IN & flags;
  12218. if (prodParamToClear) {
  12219. this.prodParam.enter(flags & ~PARAM_IN);
  12220. try {
  12221. return callback();
  12222. } finally {
  12223. this.prodParam.exit();
  12224. }
  12225. }
  12226. return callback();
  12227. }
  12228. registerTopicReference() {
  12229. this.state.topicContext.maxTopicIndex = 0;
  12230. }
  12231. topicReferenceIsAllowedInCurrentContext() {
  12232. return this.state.topicContext.maxNumOfResolvableTopics >= 1;
  12233. }
  12234. topicReferenceWasUsedInCurrentContext() {
  12235. return this.state.topicContext.maxTopicIndex != null && this.state.topicContext.maxTopicIndex >= 0;
  12236. }
  12237. parseFSharpPipelineBody(prec) {
  12238. const startLoc = this.state.startLoc;
  12239. this.state.potentialArrowAt = this.state.start;
  12240. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  12241. this.state.inFSharpPipelineDirectBody = true;
  12242. const ret = this.parseExprOp(this.parseMaybeUnaryOrPrivate(), startLoc, prec);
  12243. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  12244. return ret;
  12245. }
  12246. parseModuleExpression() {
  12247. this.expectPlugin("moduleBlocks");
  12248. const node = this.startNode();
  12249. this.next();
  12250. if (!this.match(5)) {
  12251. this.unexpected(null, 5);
  12252. }
  12253. const program = this.startNodeAt(this.state.endLoc);
  12254. this.next();
  12255. const revertScopes = this.initializeScopes(true);
  12256. this.enterInitialScopes();
  12257. try {
  12258. node.body = this.parseProgram(program, 8, "module");
  12259. } finally {
  12260. revertScopes();
  12261. }
  12262. return this.finishNode(node, "ModuleExpression");
  12263. }
  12264. parsePropertyNamePrefixOperator(prop) {}
  12265. }
  12266. const loopLabel = {
  12267. kind: "loop"
  12268. },
  12269. switchLabel = {
  12270. kind: "switch"
  12271. };
  12272. var ParseFunctionFlag = {
  12273. Expression: 0,
  12274. Declaration: 1,
  12275. HangingDeclaration: 2,
  12276. NullableId: 4,
  12277. Async: 8
  12278. };
  12279. var ParseStatementFlag = {
  12280. StatementOnly: 0,
  12281. AllowImportExport: 1,
  12282. AllowDeclaration: 2,
  12283. AllowFunctionDeclaration: 4,
  12284. AllowLabeledFunction: 8
  12285. };
  12286. const loneSurrogate = /[\uD800-\uDFFF]/u;
  12287. const keywordRelationalOperator = /in(?:stanceof)?/y;
  12288. function babel7CompatTokens(tokens, input) {
  12289. for (let i = 0; i < tokens.length; i++) {
  12290. const token = tokens[i];
  12291. const {
  12292. type
  12293. } = token;
  12294. if (typeof type === "number") {
  12295. {
  12296. if (type === 136) {
  12297. const {
  12298. loc,
  12299. start,
  12300. value,
  12301. end
  12302. } = token;
  12303. const hashEndPos = start + 1;
  12304. const hashEndLoc = createPositionWithColumnOffset(loc.start, 1);
  12305. tokens.splice(i, 1, new Token({
  12306. type: getExportedToken(27),
  12307. value: "#",
  12308. start: start,
  12309. end: hashEndPos,
  12310. startLoc: loc.start,
  12311. endLoc: hashEndLoc
  12312. }), new Token({
  12313. type: getExportedToken(130),
  12314. value: value,
  12315. start: hashEndPos,
  12316. end: end,
  12317. startLoc: hashEndLoc,
  12318. endLoc: loc.end
  12319. }));
  12320. i++;
  12321. continue;
  12322. }
  12323. if (tokenIsTemplate(type)) {
  12324. const {
  12325. loc,
  12326. start,
  12327. value,
  12328. end
  12329. } = token;
  12330. const backquoteEnd = start + 1;
  12331. const backquoteEndLoc = createPositionWithColumnOffset(loc.start, 1);
  12332. let startToken;
  12333. if (input.charCodeAt(start) === 96) {
  12334. startToken = new Token({
  12335. type: getExportedToken(22),
  12336. value: "`",
  12337. start: start,
  12338. end: backquoteEnd,
  12339. startLoc: loc.start,
  12340. endLoc: backquoteEndLoc
  12341. });
  12342. } else {
  12343. startToken = new Token({
  12344. type: getExportedToken(8),
  12345. value: "}",
  12346. start: start,
  12347. end: backquoteEnd,
  12348. startLoc: loc.start,
  12349. endLoc: backquoteEndLoc
  12350. });
  12351. }
  12352. let templateValue, templateElementEnd, templateElementEndLoc, endToken;
  12353. if (type === 24) {
  12354. templateElementEnd = end - 1;
  12355. templateElementEndLoc = createPositionWithColumnOffset(loc.end, -1);
  12356. templateValue = value === null ? null : value.slice(1, -1);
  12357. endToken = new Token({
  12358. type: getExportedToken(22),
  12359. value: "`",
  12360. start: templateElementEnd,
  12361. end: end,
  12362. startLoc: templateElementEndLoc,
  12363. endLoc: loc.end
  12364. });
  12365. } else {
  12366. templateElementEnd = end - 2;
  12367. templateElementEndLoc = createPositionWithColumnOffset(loc.end, -2);
  12368. templateValue = value === null ? null : value.slice(1, -2);
  12369. endToken = new Token({
  12370. type: getExportedToken(23),
  12371. value: "${",
  12372. start: templateElementEnd,
  12373. end: end,
  12374. startLoc: templateElementEndLoc,
  12375. endLoc: loc.end
  12376. });
  12377. }
  12378. tokens.splice(i, 1, startToken, new Token({
  12379. type: getExportedToken(20),
  12380. value: templateValue,
  12381. start: backquoteEnd,
  12382. end: templateElementEnd,
  12383. startLoc: backquoteEndLoc,
  12384. endLoc: templateElementEndLoc
  12385. }), endToken);
  12386. i += 2;
  12387. continue;
  12388. }
  12389. }
  12390. token.type = getExportedToken(type);
  12391. }
  12392. }
  12393. return tokens;
  12394. }
  12395. class StatementParser extends ExpressionParser {
  12396. parseTopLevel(file, program) {
  12397. file.program = this.parseProgram(program);
  12398. file.comments = this.state.comments;
  12399. if (this.options.tokens) {
  12400. file.tokens = babel7CompatTokens(this.tokens, this.input);
  12401. }
  12402. return this.finishNode(file, "File");
  12403. }
  12404. parseProgram(program, end = 137, sourceType = this.options.sourceType) {
  12405. program.sourceType = sourceType;
  12406. program.interpreter = this.parseInterpreterDirective();
  12407. this.parseBlockBody(program, true, true, end);
  12408. if (this.inModule && !this.options.allowUndeclaredExports && this.scope.undefinedExports.size > 0) {
  12409. for (const [localName, at] of Array.from(this.scope.undefinedExports)) {
  12410. this.raise(Errors.ModuleExportUndefined, {
  12411. at,
  12412. localName
  12413. });
  12414. }
  12415. }
  12416. let finishedProgram;
  12417. if (end === 137) {
  12418. finishedProgram = this.finishNode(program, "Program");
  12419. } else {
  12420. finishedProgram = this.finishNodeAt(program, "Program", createPositionWithColumnOffset(this.state.startLoc, -1));
  12421. }
  12422. return finishedProgram;
  12423. }
  12424. stmtToDirective(stmt) {
  12425. const directive = stmt;
  12426. directive.type = "Directive";
  12427. directive.value = directive.expression;
  12428. delete directive.expression;
  12429. const directiveLiteral = directive.value;
  12430. const expressionValue = directiveLiteral.value;
  12431. const raw = this.input.slice(directiveLiteral.start, directiveLiteral.end);
  12432. const val = directiveLiteral.value = raw.slice(1, -1);
  12433. this.addExtra(directiveLiteral, "raw", raw);
  12434. this.addExtra(directiveLiteral, "rawValue", val);
  12435. this.addExtra(directiveLiteral, "expressionValue", expressionValue);
  12436. directiveLiteral.type = "DirectiveLiteral";
  12437. return directive;
  12438. }
  12439. parseInterpreterDirective() {
  12440. if (!this.match(28)) {
  12441. return null;
  12442. }
  12443. const node = this.startNode();
  12444. node.value = this.state.value;
  12445. this.next();
  12446. return this.finishNode(node, "InterpreterDirective");
  12447. }
  12448. isLet() {
  12449. if (!this.isContextual(99)) {
  12450. return false;
  12451. }
  12452. return this.hasFollowingBindingAtom();
  12453. }
  12454. chStartsBindingIdentifier(ch, pos) {
  12455. if (isIdentifierStart(ch)) {
  12456. keywordRelationalOperator.lastIndex = pos;
  12457. if (keywordRelationalOperator.test(this.input)) {
  12458. const endCh = this.codePointAtPos(keywordRelationalOperator.lastIndex);
  12459. if (!isIdentifierChar(endCh) && endCh !== 92) {
  12460. return false;
  12461. }
  12462. }
  12463. return true;
  12464. } else if (ch === 92) {
  12465. return true;
  12466. } else {
  12467. return false;
  12468. }
  12469. }
  12470. chStartsBindingPattern(ch) {
  12471. return ch === 91 || ch === 123;
  12472. }
  12473. hasFollowingBindingAtom() {
  12474. const next = this.nextTokenStart();
  12475. const nextCh = this.codePointAtPos(next);
  12476. return this.chStartsBindingPattern(nextCh) || this.chStartsBindingIdentifier(nextCh, next);
  12477. }
  12478. hasFollowingBindingIdentifier() {
  12479. const next = this.nextTokenStart();
  12480. const nextCh = this.codePointAtPos(next);
  12481. return this.chStartsBindingIdentifier(nextCh, next);
  12482. }
  12483. startsUsingForOf() {
  12484. const lookahead = this.lookahead();
  12485. if (lookahead.type === 101 && !lookahead.containsEsc) {
  12486. return false;
  12487. } else {
  12488. this.expectPlugin("explicitResourceManagement");
  12489. return true;
  12490. }
  12491. }
  12492. parseModuleItem() {
  12493. return this.parseStatementLike(ParseStatementFlag.AllowImportExport | ParseStatementFlag.AllowDeclaration | ParseStatementFlag.AllowFunctionDeclaration | ParseStatementFlag.AllowLabeledFunction);
  12494. }
  12495. parseStatementListItem() {
  12496. return this.parseStatementLike(ParseStatementFlag.AllowDeclaration | ParseStatementFlag.AllowFunctionDeclaration | (!this.options.annexB || this.state.strict ? 0 : ParseStatementFlag.AllowLabeledFunction));
  12497. }
  12498. parseStatementOrSloppyAnnexBFunctionDeclaration(allowLabeledFunction = false) {
  12499. let flags = ParseStatementFlag.StatementOnly;
  12500. if (this.options.annexB && !this.state.strict) {
  12501. flags |= ParseStatementFlag.AllowFunctionDeclaration;
  12502. if (allowLabeledFunction) {
  12503. flags |= ParseStatementFlag.AllowLabeledFunction;
  12504. }
  12505. }
  12506. return this.parseStatementLike(flags);
  12507. }
  12508. parseStatement() {
  12509. return this.parseStatementLike(ParseStatementFlag.StatementOnly);
  12510. }
  12511. parseStatementLike(flags) {
  12512. let decorators = null;
  12513. if (this.match(26)) {
  12514. decorators = this.parseDecorators(true);
  12515. }
  12516. return this.parseStatementContent(flags, decorators);
  12517. }
  12518. parseStatementContent(flags, decorators) {
  12519. const starttype = this.state.type;
  12520. const node = this.startNode();
  12521. const allowDeclaration = !!(flags & ParseStatementFlag.AllowDeclaration);
  12522. const allowFunctionDeclaration = !!(flags & ParseStatementFlag.AllowFunctionDeclaration);
  12523. const topLevel = flags & ParseStatementFlag.AllowImportExport;
  12524. switch (starttype) {
  12525. case 60:
  12526. return this.parseBreakContinueStatement(node, true);
  12527. case 63:
  12528. return this.parseBreakContinueStatement(node, false);
  12529. case 64:
  12530. return this.parseDebuggerStatement(node);
  12531. case 90:
  12532. return this.parseDoWhileStatement(node);
  12533. case 91:
  12534. return this.parseForStatement(node);
  12535. case 68:
  12536. if (this.lookaheadCharCode() === 46) break;
  12537. if (!allowFunctionDeclaration) {
  12538. this.raise(this.state.strict ? Errors.StrictFunction : this.options.annexB ? Errors.SloppyFunctionAnnexB : Errors.SloppyFunction, {
  12539. at: this.state.startLoc
  12540. });
  12541. }
  12542. return this.parseFunctionStatement(node, false, !allowDeclaration && allowFunctionDeclaration);
  12543. case 80:
  12544. if (!allowDeclaration) this.unexpected();
  12545. return this.parseClass(this.maybeTakeDecorators(decorators, node), true);
  12546. case 69:
  12547. return this.parseIfStatement(node);
  12548. case 70:
  12549. return this.parseReturnStatement(node);
  12550. case 71:
  12551. return this.parseSwitchStatement(node);
  12552. case 72:
  12553. return this.parseThrowStatement(node);
  12554. case 73:
  12555. return this.parseTryStatement(node);
  12556. case 105:
  12557. if (this.hasFollowingLineBreak() || this.state.containsEsc || !this.hasFollowingBindingIdentifier()) {
  12558. break;
  12559. }
  12560. this.expectPlugin("explicitResourceManagement");
  12561. if (!this.scope.inModule && this.scope.inTopLevel) {
  12562. this.raise(Errors.UnexpectedUsingDeclaration, {
  12563. at: this.state.startLoc
  12564. });
  12565. } else if (!allowDeclaration) {
  12566. this.raise(Errors.UnexpectedLexicalDeclaration, {
  12567. at: this.state.startLoc
  12568. });
  12569. }
  12570. return this.parseVarStatement(node, "using");
  12571. case 99:
  12572. {
  12573. if (this.state.containsEsc) {
  12574. break;
  12575. }
  12576. const next = this.nextTokenStart();
  12577. const nextCh = this.codePointAtPos(next);
  12578. if (nextCh !== 91) {
  12579. if (!allowDeclaration && this.hasFollowingLineBreak()) break;
  12580. if (!this.chStartsBindingIdentifier(nextCh, next) && nextCh !== 123) {
  12581. break;
  12582. }
  12583. }
  12584. }
  12585. case 75:
  12586. {
  12587. if (!allowDeclaration) {
  12588. this.raise(Errors.UnexpectedLexicalDeclaration, {
  12589. at: this.state.startLoc
  12590. });
  12591. }
  12592. }
  12593. case 74:
  12594. {
  12595. const kind = this.state.value;
  12596. return this.parseVarStatement(node, kind);
  12597. }
  12598. case 92:
  12599. return this.parseWhileStatement(node);
  12600. case 76:
  12601. return this.parseWithStatement(node);
  12602. case 5:
  12603. return this.parseBlock();
  12604. case 13:
  12605. return this.parseEmptyStatement(node);
  12606. case 83:
  12607. {
  12608. const nextTokenCharCode = this.lookaheadCharCode();
  12609. if (nextTokenCharCode === 40 || nextTokenCharCode === 46) {
  12610. break;
  12611. }
  12612. }
  12613. case 82:
  12614. {
  12615. if (!this.options.allowImportExportEverywhere && !topLevel) {
  12616. this.raise(Errors.UnexpectedImportExport, {
  12617. at: this.state.startLoc
  12618. });
  12619. }
  12620. this.next();
  12621. let result;
  12622. if (starttype === 83) {
  12623. result = this.parseImport(node);
  12624. if (result.type === "ImportDeclaration" && (!result.importKind || result.importKind === "value")) {
  12625. this.sawUnambiguousESM = true;
  12626. }
  12627. } else {
  12628. result = this.parseExport(node, decorators);
  12629. if (result.type === "ExportNamedDeclaration" && (!result.exportKind || result.exportKind === "value") || result.type === "ExportAllDeclaration" && (!result.exportKind || result.exportKind === "value") || result.type === "ExportDefaultDeclaration") {
  12630. this.sawUnambiguousESM = true;
  12631. }
  12632. }
  12633. this.assertModuleNodeAllowed(result);
  12634. return result;
  12635. }
  12636. default:
  12637. {
  12638. if (this.isAsyncFunction()) {
  12639. if (!allowDeclaration) {
  12640. this.raise(Errors.AsyncFunctionInSingleStatementContext, {
  12641. at: this.state.startLoc
  12642. });
  12643. }
  12644. this.next();
  12645. return this.parseFunctionStatement(node, true, !allowDeclaration && allowFunctionDeclaration);
  12646. }
  12647. }
  12648. }
  12649. const maybeName = this.state.value;
  12650. const expr = this.parseExpression();
  12651. if (tokenIsIdentifier(starttype) && expr.type === "Identifier" && this.eat(14)) {
  12652. return this.parseLabeledStatement(node, maybeName, expr, flags);
  12653. } else {
  12654. return this.parseExpressionStatement(node, expr, decorators);
  12655. }
  12656. }
  12657. assertModuleNodeAllowed(node) {
  12658. if (!this.options.allowImportExportEverywhere && !this.inModule) {
  12659. this.raise(Errors.ImportOutsideModule, {
  12660. at: node
  12661. });
  12662. }
  12663. }
  12664. decoratorsEnabledBeforeExport() {
  12665. if (this.hasPlugin("decorators-legacy")) return true;
  12666. return this.hasPlugin("decorators") && this.getPluginOption("decorators", "decoratorsBeforeExport") !== false;
  12667. }
  12668. maybeTakeDecorators(maybeDecorators, classNode, exportNode) {
  12669. if (maybeDecorators) {
  12670. if (classNode.decorators && classNode.decorators.length > 0) {
  12671. if (typeof this.getPluginOption("decorators", "decoratorsBeforeExport") !== "boolean") {
  12672. this.raise(Errors.DecoratorsBeforeAfterExport, {
  12673. at: classNode.decorators[0]
  12674. });
  12675. }
  12676. classNode.decorators.unshift(...maybeDecorators);
  12677. } else {
  12678. classNode.decorators = maybeDecorators;
  12679. }
  12680. this.resetStartLocationFromNode(classNode, maybeDecorators[0]);
  12681. if (exportNode) this.resetStartLocationFromNode(exportNode, classNode);
  12682. }
  12683. return classNode;
  12684. }
  12685. canHaveLeadingDecorator() {
  12686. return this.match(80);
  12687. }
  12688. parseDecorators(allowExport) {
  12689. const decorators = [];
  12690. do {
  12691. decorators.push(this.parseDecorator());
  12692. } while (this.match(26));
  12693. if (this.match(82)) {
  12694. if (!allowExport) {
  12695. this.unexpected();
  12696. }
  12697. if (!this.decoratorsEnabledBeforeExport()) {
  12698. this.raise(Errors.DecoratorExportClass, {
  12699. at: this.state.startLoc
  12700. });
  12701. }
  12702. } else if (!this.canHaveLeadingDecorator()) {
  12703. throw this.raise(Errors.UnexpectedLeadingDecorator, {
  12704. at: this.state.startLoc
  12705. });
  12706. }
  12707. return decorators;
  12708. }
  12709. parseDecorator() {
  12710. this.expectOnePlugin(["decorators", "decorators-legacy"]);
  12711. const node = this.startNode();
  12712. this.next();
  12713. if (this.hasPlugin("decorators")) {
  12714. const startLoc = this.state.startLoc;
  12715. let expr;
  12716. if (this.match(10)) {
  12717. const startLoc = this.state.startLoc;
  12718. this.next();
  12719. expr = this.parseExpression();
  12720. this.expect(11);
  12721. expr = this.wrapParenthesis(startLoc, expr);
  12722. const paramsStartLoc = this.state.startLoc;
  12723. node.expression = this.parseMaybeDecoratorArguments(expr);
  12724. if (this.getPluginOption("decorators", "allowCallParenthesized") === false && node.expression !== expr) {
  12725. this.raise(Errors.DecoratorArgumentsOutsideParentheses, {
  12726. at: paramsStartLoc
  12727. });
  12728. }
  12729. } else {
  12730. expr = this.parseIdentifier(false);
  12731. while (this.eat(16)) {
  12732. const node = this.startNodeAt(startLoc);
  12733. node.object = expr;
  12734. if (this.match(136)) {
  12735. this.classScope.usePrivateName(this.state.value, this.state.startLoc);
  12736. node.property = this.parsePrivateName();
  12737. } else {
  12738. node.property = this.parseIdentifier(true);
  12739. }
  12740. node.computed = false;
  12741. expr = this.finishNode(node, "MemberExpression");
  12742. }
  12743. node.expression = this.parseMaybeDecoratorArguments(expr);
  12744. }
  12745. } else {
  12746. node.expression = this.parseExprSubscripts();
  12747. }
  12748. return this.finishNode(node, "Decorator");
  12749. }
  12750. parseMaybeDecoratorArguments(expr) {
  12751. if (this.eat(10)) {
  12752. const node = this.startNodeAtNode(expr);
  12753. node.callee = expr;
  12754. node.arguments = this.parseCallExpressionArguments(11, false);
  12755. this.toReferencedList(node.arguments);
  12756. return this.finishNode(node, "CallExpression");
  12757. }
  12758. return expr;
  12759. }
  12760. parseBreakContinueStatement(node, isBreak) {
  12761. this.next();
  12762. if (this.isLineTerminator()) {
  12763. node.label = null;
  12764. } else {
  12765. node.label = this.parseIdentifier();
  12766. this.semicolon();
  12767. }
  12768. this.verifyBreakContinue(node, isBreak);
  12769. return this.finishNode(node, isBreak ? "BreakStatement" : "ContinueStatement");
  12770. }
  12771. verifyBreakContinue(node, isBreak) {
  12772. let i;
  12773. for (i = 0; i < this.state.labels.length; ++i) {
  12774. const lab = this.state.labels[i];
  12775. if (node.label == null || lab.name === node.label.name) {
  12776. if (lab.kind != null && (isBreak || lab.kind === "loop")) break;
  12777. if (node.label && isBreak) break;
  12778. }
  12779. }
  12780. if (i === this.state.labels.length) {
  12781. const type = isBreak ? "BreakStatement" : "ContinueStatement";
  12782. this.raise(Errors.IllegalBreakContinue, {
  12783. at: node,
  12784. type
  12785. });
  12786. }
  12787. }
  12788. parseDebuggerStatement(node) {
  12789. this.next();
  12790. this.semicolon();
  12791. return this.finishNode(node, "DebuggerStatement");
  12792. }
  12793. parseHeaderExpression() {
  12794. this.expect(10);
  12795. const val = this.parseExpression();
  12796. this.expect(11);
  12797. return val;
  12798. }
  12799. parseDoWhileStatement(node) {
  12800. this.next();
  12801. this.state.labels.push(loopLabel);
  12802. node.body = this.withSmartMixTopicForbiddingContext(() => this.parseStatement());
  12803. this.state.labels.pop();
  12804. this.expect(92);
  12805. node.test = this.parseHeaderExpression();
  12806. this.eat(13);
  12807. return this.finishNode(node, "DoWhileStatement");
  12808. }
  12809. parseForStatement(node) {
  12810. this.next();
  12811. this.state.labels.push(loopLabel);
  12812. let awaitAt = null;
  12813. if (this.isAwaitAllowed() && this.eatContextual(96)) {
  12814. awaitAt = this.state.lastTokStartLoc;
  12815. }
  12816. this.scope.enter(SCOPE_OTHER);
  12817. this.expect(10);
  12818. if (this.match(13)) {
  12819. if (awaitAt !== null) {
  12820. this.unexpected(awaitAt);
  12821. }
  12822. return this.parseFor(node, null);
  12823. }
  12824. const startsWithLet = this.isContextual(99);
  12825. const startsWithUsing = this.isContextual(105) && !this.hasFollowingLineBreak();
  12826. const isLetOrUsing = startsWithLet && this.hasFollowingBindingAtom() || startsWithUsing && this.hasFollowingBindingIdentifier() && this.startsUsingForOf();
  12827. if (this.match(74) || this.match(75) || isLetOrUsing) {
  12828. const initNode = this.startNode();
  12829. const kind = this.state.value;
  12830. this.next();
  12831. this.parseVar(initNode, true, kind);
  12832. const init = this.finishNode(initNode, "VariableDeclaration");
  12833. const isForIn = this.match(58);
  12834. if (isForIn && startsWithUsing) {
  12835. this.raise(Errors.ForInUsing, {
  12836. at: init
  12837. });
  12838. }
  12839. if ((isForIn || this.isContextual(101)) && init.declarations.length === 1) {
  12840. return this.parseForIn(node, init, awaitAt);
  12841. }
  12842. if (awaitAt !== null) {
  12843. this.unexpected(awaitAt);
  12844. }
  12845. return this.parseFor(node, init);
  12846. }
  12847. const startsWithAsync = this.isContextual(95);
  12848. const refExpressionErrors = new ExpressionErrors();
  12849. const init = this.parseExpression(true, refExpressionErrors);
  12850. const isForOf = this.isContextual(101);
  12851. if (isForOf) {
  12852. if (startsWithLet) {
  12853. this.raise(Errors.ForOfLet, {
  12854. at: init
  12855. });
  12856. }
  12857. if (awaitAt === null && startsWithAsync && init.type === "Identifier") {
  12858. this.raise(Errors.ForOfAsync, {
  12859. at: init
  12860. });
  12861. }
  12862. }
  12863. if (isForOf || this.match(58)) {
  12864. this.checkDestructuringPrivate(refExpressionErrors);
  12865. this.toAssignable(init, true);
  12866. const type = isForOf ? "ForOfStatement" : "ForInStatement";
  12867. this.checkLVal(init, {
  12868. in: {
  12869. type
  12870. }
  12871. });
  12872. return this.parseForIn(node, init, awaitAt);
  12873. } else {
  12874. this.checkExpressionErrors(refExpressionErrors, true);
  12875. }
  12876. if (awaitAt !== null) {
  12877. this.unexpected(awaitAt);
  12878. }
  12879. return this.parseFor(node, init);
  12880. }
  12881. parseFunctionStatement(node, isAsync, isHangingDeclaration) {
  12882. this.next();
  12883. return this.parseFunction(node, ParseFunctionFlag.Declaration | (isHangingDeclaration ? ParseFunctionFlag.HangingDeclaration : 0) | (isAsync ? ParseFunctionFlag.Async : 0));
  12884. }
  12885. parseIfStatement(node) {
  12886. this.next();
  12887. node.test = this.parseHeaderExpression();
  12888. node.consequent = this.parseStatementOrSloppyAnnexBFunctionDeclaration();
  12889. node.alternate = this.eat(66) ? this.parseStatementOrSloppyAnnexBFunctionDeclaration() : null;
  12890. return this.finishNode(node, "IfStatement");
  12891. }
  12892. parseReturnStatement(node) {
  12893. if (!this.prodParam.hasReturn && !this.options.allowReturnOutsideFunction) {
  12894. this.raise(Errors.IllegalReturn, {
  12895. at: this.state.startLoc
  12896. });
  12897. }
  12898. this.next();
  12899. if (this.isLineTerminator()) {
  12900. node.argument = null;
  12901. } else {
  12902. node.argument = this.parseExpression();
  12903. this.semicolon();
  12904. }
  12905. return this.finishNode(node, "ReturnStatement");
  12906. }
  12907. parseSwitchStatement(node) {
  12908. this.next();
  12909. node.discriminant = this.parseHeaderExpression();
  12910. const cases = node.cases = [];
  12911. this.expect(5);
  12912. this.state.labels.push(switchLabel);
  12913. this.scope.enter(SCOPE_OTHER);
  12914. let cur;
  12915. for (let sawDefault; !this.match(8);) {
  12916. if (this.match(61) || this.match(65)) {
  12917. const isCase = this.match(61);
  12918. if (cur) this.finishNode(cur, "SwitchCase");
  12919. cases.push(cur = this.startNode());
  12920. cur.consequent = [];
  12921. this.next();
  12922. if (isCase) {
  12923. cur.test = this.parseExpression();
  12924. } else {
  12925. if (sawDefault) {
  12926. this.raise(Errors.MultipleDefaultsInSwitch, {
  12927. at: this.state.lastTokStartLoc
  12928. });
  12929. }
  12930. sawDefault = true;
  12931. cur.test = null;
  12932. }
  12933. this.expect(14);
  12934. } else {
  12935. if (cur) {
  12936. cur.consequent.push(this.parseStatementListItem());
  12937. } else {
  12938. this.unexpected();
  12939. }
  12940. }
  12941. }
  12942. this.scope.exit();
  12943. if (cur) this.finishNode(cur, "SwitchCase");
  12944. this.next();
  12945. this.state.labels.pop();
  12946. return this.finishNode(node, "SwitchStatement");
  12947. }
  12948. parseThrowStatement(node) {
  12949. this.next();
  12950. if (this.hasPrecedingLineBreak()) {
  12951. this.raise(Errors.NewlineAfterThrow, {
  12952. at: this.state.lastTokEndLoc
  12953. });
  12954. }
  12955. node.argument = this.parseExpression();
  12956. this.semicolon();
  12957. return this.finishNode(node, "ThrowStatement");
  12958. }
  12959. parseCatchClauseParam() {
  12960. const param = this.parseBindingAtom();
  12961. this.scope.enter(this.options.annexB && param.type === "Identifier" ? SCOPE_SIMPLE_CATCH : 0);
  12962. this.checkLVal(param, {
  12963. in: {
  12964. type: "CatchClause"
  12965. },
  12966. binding: BIND_CATCH_PARAM
  12967. });
  12968. return param;
  12969. }
  12970. parseTryStatement(node) {
  12971. this.next();
  12972. node.block = this.parseBlock();
  12973. node.handler = null;
  12974. if (this.match(62)) {
  12975. const clause = this.startNode();
  12976. this.next();
  12977. if (this.match(10)) {
  12978. this.expect(10);
  12979. clause.param = this.parseCatchClauseParam();
  12980. this.expect(11);
  12981. } else {
  12982. clause.param = null;
  12983. this.scope.enter(SCOPE_OTHER);
  12984. }
  12985. clause.body = this.withSmartMixTopicForbiddingContext(() => this.parseBlock(false, false));
  12986. this.scope.exit();
  12987. node.handler = this.finishNode(clause, "CatchClause");
  12988. }
  12989. node.finalizer = this.eat(67) ? this.parseBlock() : null;
  12990. if (!node.handler && !node.finalizer) {
  12991. this.raise(Errors.NoCatchOrFinally, {
  12992. at: node
  12993. });
  12994. }
  12995. return this.finishNode(node, "TryStatement");
  12996. }
  12997. parseVarStatement(node, kind, allowMissingInitializer = false) {
  12998. this.next();
  12999. this.parseVar(node, false, kind, allowMissingInitializer);
  13000. this.semicolon();
  13001. return this.finishNode(node, "VariableDeclaration");
  13002. }
  13003. parseWhileStatement(node) {
  13004. this.next();
  13005. node.test = this.parseHeaderExpression();
  13006. this.state.labels.push(loopLabel);
  13007. node.body = this.withSmartMixTopicForbiddingContext(() => this.parseStatement());
  13008. this.state.labels.pop();
  13009. return this.finishNode(node, "WhileStatement");
  13010. }
  13011. parseWithStatement(node) {
  13012. if (this.state.strict) {
  13013. this.raise(Errors.StrictWith, {
  13014. at: this.state.startLoc
  13015. });
  13016. }
  13017. this.next();
  13018. node.object = this.parseHeaderExpression();
  13019. node.body = this.withSmartMixTopicForbiddingContext(() => this.parseStatement());
  13020. return this.finishNode(node, "WithStatement");
  13021. }
  13022. parseEmptyStatement(node) {
  13023. this.next();
  13024. return this.finishNode(node, "EmptyStatement");
  13025. }
  13026. parseLabeledStatement(node, maybeName, expr, flags) {
  13027. for (const label of this.state.labels) {
  13028. if (label.name === maybeName) {
  13029. this.raise(Errors.LabelRedeclaration, {
  13030. at: expr,
  13031. labelName: maybeName
  13032. });
  13033. }
  13034. }
  13035. const kind = tokenIsLoop(this.state.type) ? "loop" : this.match(71) ? "switch" : null;
  13036. for (let i = this.state.labels.length - 1; i >= 0; i--) {
  13037. const label = this.state.labels[i];
  13038. if (label.statementStart === node.start) {
  13039. label.statementStart = this.state.start;
  13040. label.kind = kind;
  13041. } else {
  13042. break;
  13043. }
  13044. }
  13045. this.state.labels.push({
  13046. name: maybeName,
  13047. kind: kind,
  13048. statementStart: this.state.start
  13049. });
  13050. node.body = flags & ParseStatementFlag.AllowLabeledFunction ? this.parseStatementOrSloppyAnnexBFunctionDeclaration(true) : this.parseStatement();
  13051. this.state.labels.pop();
  13052. node.label = expr;
  13053. return this.finishNode(node, "LabeledStatement");
  13054. }
  13055. parseExpressionStatement(node, expr, decorators) {
  13056. node.expression = expr;
  13057. this.semicolon();
  13058. return this.finishNode(node, "ExpressionStatement");
  13059. }
  13060. parseBlock(allowDirectives = false, createNewLexicalScope = true, afterBlockParse) {
  13061. const node = this.startNode();
  13062. if (allowDirectives) {
  13063. this.state.strictErrors.clear();
  13064. }
  13065. this.expect(5);
  13066. if (createNewLexicalScope) {
  13067. this.scope.enter(SCOPE_OTHER);
  13068. }
  13069. this.parseBlockBody(node, allowDirectives, false, 8, afterBlockParse);
  13070. if (createNewLexicalScope) {
  13071. this.scope.exit();
  13072. }
  13073. return this.finishNode(node, "BlockStatement");
  13074. }
  13075. isValidDirective(stmt) {
  13076. return stmt.type === "ExpressionStatement" && stmt.expression.type === "StringLiteral" && !stmt.expression.extra.parenthesized;
  13077. }
  13078. parseBlockBody(node, allowDirectives, topLevel, end, afterBlockParse) {
  13079. const body = node.body = [];
  13080. const directives = node.directives = [];
  13081. this.parseBlockOrModuleBlockBody(body, allowDirectives ? directives : undefined, topLevel, end, afterBlockParse);
  13082. }
  13083. parseBlockOrModuleBlockBody(body, directives, topLevel, end, afterBlockParse) {
  13084. const oldStrict = this.state.strict;
  13085. let hasStrictModeDirective = false;
  13086. let parsedNonDirective = false;
  13087. while (!this.match(end)) {
  13088. const stmt = topLevel ? this.parseModuleItem() : this.parseStatementListItem();
  13089. if (directives && !parsedNonDirective) {
  13090. if (this.isValidDirective(stmt)) {
  13091. const directive = this.stmtToDirective(stmt);
  13092. directives.push(directive);
  13093. if (!hasStrictModeDirective && directive.value.value === "use strict") {
  13094. hasStrictModeDirective = true;
  13095. this.setStrict(true);
  13096. }
  13097. continue;
  13098. }
  13099. parsedNonDirective = true;
  13100. this.state.strictErrors.clear();
  13101. }
  13102. body.push(stmt);
  13103. }
  13104. if (afterBlockParse) {
  13105. afterBlockParse.call(this, hasStrictModeDirective);
  13106. }
  13107. if (!oldStrict) {
  13108. this.setStrict(false);
  13109. }
  13110. this.next();
  13111. }
  13112. parseFor(node, init) {
  13113. node.init = init;
  13114. this.semicolon(false);
  13115. node.test = this.match(13) ? null : this.parseExpression();
  13116. this.semicolon(false);
  13117. node.update = this.match(11) ? null : this.parseExpression();
  13118. this.expect(11);
  13119. node.body = this.withSmartMixTopicForbiddingContext(() => this.parseStatement());
  13120. this.scope.exit();
  13121. this.state.labels.pop();
  13122. return this.finishNode(node, "ForStatement");
  13123. }
  13124. parseForIn(node, init, awaitAt) {
  13125. const isForIn = this.match(58);
  13126. this.next();
  13127. if (isForIn) {
  13128. if (awaitAt !== null) this.unexpected(awaitAt);
  13129. } else {
  13130. node.await = awaitAt !== null;
  13131. }
  13132. if (init.type === "VariableDeclaration" && init.declarations[0].init != null && (!isForIn || !this.options.annexB || this.state.strict || init.kind !== "var" || init.declarations[0].id.type !== "Identifier")) {
  13133. this.raise(Errors.ForInOfLoopInitializer, {
  13134. at: init,
  13135. type: isForIn ? "ForInStatement" : "ForOfStatement"
  13136. });
  13137. }
  13138. if (init.type === "AssignmentPattern") {
  13139. this.raise(Errors.InvalidLhs, {
  13140. at: init,
  13141. ancestor: {
  13142. type: "ForStatement"
  13143. }
  13144. });
  13145. }
  13146. node.left = init;
  13147. node.right = isForIn ? this.parseExpression() : this.parseMaybeAssignAllowIn();
  13148. this.expect(11);
  13149. node.body = this.withSmartMixTopicForbiddingContext(() => this.parseStatement());
  13150. this.scope.exit();
  13151. this.state.labels.pop();
  13152. return this.finishNode(node, isForIn ? "ForInStatement" : "ForOfStatement");
  13153. }
  13154. parseVar(node, isFor, kind, allowMissingInitializer = false) {
  13155. const declarations = node.declarations = [];
  13156. node.kind = kind;
  13157. for (;;) {
  13158. const decl = this.startNode();
  13159. this.parseVarId(decl, kind);
  13160. decl.init = !this.eat(29) ? null : isFor ? this.parseMaybeAssignDisallowIn() : this.parseMaybeAssignAllowIn();
  13161. if (decl.init === null && !allowMissingInitializer) {
  13162. if (decl.id.type !== "Identifier" && !(isFor && (this.match(58) || this.isContextual(101)))) {
  13163. this.raise(Errors.DeclarationMissingInitializer, {
  13164. at: this.state.lastTokEndLoc,
  13165. kind: "destructuring"
  13166. });
  13167. } else if (kind === "const" && !(this.match(58) || this.isContextual(101))) {
  13168. this.raise(Errors.DeclarationMissingInitializer, {
  13169. at: this.state.lastTokEndLoc,
  13170. kind: "const"
  13171. });
  13172. }
  13173. }
  13174. declarations.push(this.finishNode(decl, "VariableDeclarator"));
  13175. if (!this.eat(12)) break;
  13176. }
  13177. return node;
  13178. }
  13179. parseVarId(decl, kind) {
  13180. if (kind === "using" && !this.inModule && this.match(96)) {
  13181. this.raise(Errors.AwaitInUsingBinding, {
  13182. at: this.state.startLoc
  13183. });
  13184. }
  13185. const id = this.parseBindingAtom();
  13186. this.checkLVal(id, {
  13187. in: {
  13188. type: "VariableDeclarator"
  13189. },
  13190. binding: kind === "var" ? BIND_VAR : BIND_LEXICAL
  13191. });
  13192. decl.id = id;
  13193. }
  13194. parseAsyncFunctionExpression(node) {
  13195. return this.parseFunction(node, ParseFunctionFlag.Async);
  13196. }
  13197. parseFunction(node, flags = ParseFunctionFlag.Expression) {
  13198. const hangingDeclaration = flags & ParseFunctionFlag.HangingDeclaration;
  13199. const isDeclaration = !!(flags & ParseFunctionFlag.Declaration);
  13200. const requireId = isDeclaration && !(flags & ParseFunctionFlag.NullableId);
  13201. const isAsync = !!(flags & ParseFunctionFlag.Async);
  13202. this.initFunction(node, isAsync);
  13203. if (this.match(55)) {
  13204. if (hangingDeclaration) {
  13205. this.raise(Errors.GeneratorInSingleStatementContext, {
  13206. at: this.state.startLoc
  13207. });
  13208. }
  13209. this.next();
  13210. node.generator = true;
  13211. }
  13212. if (isDeclaration) {
  13213. node.id = this.parseFunctionId(requireId);
  13214. }
  13215. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  13216. this.state.maybeInArrowParameters = false;
  13217. this.scope.enter(SCOPE_FUNCTION);
  13218. this.prodParam.enter(functionFlags(isAsync, node.generator));
  13219. if (!isDeclaration) {
  13220. node.id = this.parseFunctionId();
  13221. }
  13222. this.parseFunctionParams(node, false);
  13223. this.withSmartMixTopicForbiddingContext(() => {
  13224. this.parseFunctionBodyAndFinish(node, isDeclaration ? "FunctionDeclaration" : "FunctionExpression");
  13225. });
  13226. this.prodParam.exit();
  13227. this.scope.exit();
  13228. if (isDeclaration && !hangingDeclaration) {
  13229. this.registerFunctionStatementId(node);
  13230. }
  13231. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  13232. return node;
  13233. }
  13234. parseFunctionId(requireId) {
  13235. return requireId || tokenIsIdentifier(this.state.type) ? this.parseIdentifier() : null;
  13236. }
  13237. parseFunctionParams(node, isConstructor) {
  13238. this.expect(10);
  13239. this.expressionScope.enter(newParameterDeclarationScope());
  13240. node.params = this.parseBindingList(11, 41, ParseBindingListFlags.IS_FUNCTION_PARAMS | (isConstructor ? ParseBindingListFlags.IS_CONSTRUCTOR_PARAMS : 0));
  13241. this.expressionScope.exit();
  13242. }
  13243. registerFunctionStatementId(node) {
  13244. if (!node.id) return;
  13245. this.scope.declareName(node.id.name, !this.options.annexB || this.state.strict || node.generator || node.async ? this.scope.treatFunctionsAsVar ? BIND_VAR : BIND_LEXICAL : BIND_FUNCTION, node.id.loc.start);
  13246. }
  13247. parseClass(node, isStatement, optionalId) {
  13248. this.next();
  13249. const oldStrict = this.state.strict;
  13250. this.state.strict = true;
  13251. this.parseClassId(node, isStatement, optionalId);
  13252. this.parseClassSuper(node);
  13253. node.body = this.parseClassBody(!!node.superClass, oldStrict);
  13254. return this.finishNode(node, isStatement ? "ClassDeclaration" : "ClassExpression");
  13255. }
  13256. isClassProperty() {
  13257. return this.match(29) || this.match(13) || this.match(8);
  13258. }
  13259. isClassMethod() {
  13260. return this.match(10);
  13261. }
  13262. isNonstaticConstructor(method) {
  13263. return !method.computed && !method.static && (method.key.name === "constructor" || method.key.value === "constructor");
  13264. }
  13265. parseClassBody(hadSuperClass, oldStrict) {
  13266. this.classScope.enter();
  13267. const state = {
  13268. hadConstructor: false,
  13269. hadSuperClass
  13270. };
  13271. let decorators = [];
  13272. const classBody = this.startNode();
  13273. classBody.body = [];
  13274. this.expect(5);
  13275. this.withSmartMixTopicForbiddingContext(() => {
  13276. while (!this.match(8)) {
  13277. if (this.eat(13)) {
  13278. if (decorators.length > 0) {
  13279. throw this.raise(Errors.DecoratorSemicolon, {
  13280. at: this.state.lastTokEndLoc
  13281. });
  13282. }
  13283. continue;
  13284. }
  13285. if (this.match(26)) {
  13286. decorators.push(this.parseDecorator());
  13287. continue;
  13288. }
  13289. const member = this.startNode();
  13290. if (decorators.length) {
  13291. member.decorators = decorators;
  13292. this.resetStartLocationFromNode(member, decorators[0]);
  13293. decorators = [];
  13294. }
  13295. this.parseClassMember(classBody, member, state);
  13296. if (member.kind === "constructor" && member.decorators && member.decorators.length > 0) {
  13297. this.raise(Errors.DecoratorConstructor, {
  13298. at: member
  13299. });
  13300. }
  13301. }
  13302. });
  13303. this.state.strict = oldStrict;
  13304. this.next();
  13305. if (decorators.length) {
  13306. throw this.raise(Errors.TrailingDecorator, {
  13307. at: this.state.startLoc
  13308. });
  13309. }
  13310. this.classScope.exit();
  13311. return this.finishNode(classBody, "ClassBody");
  13312. }
  13313. parseClassMemberFromModifier(classBody, member) {
  13314. const key = this.parseIdentifier(true);
  13315. if (this.isClassMethod()) {
  13316. const method = member;
  13317. method.kind = "method";
  13318. method.computed = false;
  13319. method.key = key;
  13320. method.static = false;
  13321. this.pushClassMethod(classBody, method, false, false, false, false);
  13322. return true;
  13323. } else if (this.isClassProperty()) {
  13324. const prop = member;
  13325. prop.computed = false;
  13326. prop.key = key;
  13327. prop.static = false;
  13328. classBody.body.push(this.parseClassProperty(prop));
  13329. return true;
  13330. }
  13331. this.resetPreviousNodeTrailingComments(key);
  13332. return false;
  13333. }
  13334. parseClassMember(classBody, member, state) {
  13335. const isStatic = this.isContextual(104);
  13336. if (isStatic) {
  13337. if (this.parseClassMemberFromModifier(classBody, member)) {
  13338. return;
  13339. }
  13340. if (this.eat(5)) {
  13341. this.parseClassStaticBlock(classBody, member);
  13342. return;
  13343. }
  13344. }
  13345. this.parseClassMemberWithIsStatic(classBody, member, state, isStatic);
  13346. }
  13347. parseClassMemberWithIsStatic(classBody, member, state, isStatic) {
  13348. const publicMethod = member;
  13349. const privateMethod = member;
  13350. const publicProp = member;
  13351. const privateProp = member;
  13352. const accessorProp = member;
  13353. const method = publicMethod;
  13354. const publicMember = publicMethod;
  13355. member.static = isStatic;
  13356. this.parsePropertyNamePrefixOperator(member);
  13357. if (this.eat(55)) {
  13358. method.kind = "method";
  13359. const isPrivateName = this.match(136);
  13360. this.parseClassElementName(method);
  13361. if (isPrivateName) {
  13362. this.pushClassPrivateMethod(classBody, privateMethod, true, false);
  13363. return;
  13364. }
  13365. if (this.isNonstaticConstructor(publicMethod)) {
  13366. this.raise(Errors.ConstructorIsGenerator, {
  13367. at: publicMethod.key
  13368. });
  13369. }
  13370. this.pushClassMethod(classBody, publicMethod, true, false, false, false);
  13371. return;
  13372. }
  13373. const isContextual = tokenIsIdentifier(this.state.type) && !this.state.containsEsc;
  13374. const isPrivate = this.match(136);
  13375. const key = this.parseClassElementName(member);
  13376. const maybeQuestionTokenStartLoc = this.state.startLoc;
  13377. this.parsePostMemberNameModifiers(publicMember);
  13378. if (this.isClassMethod()) {
  13379. method.kind = "method";
  13380. if (isPrivate) {
  13381. this.pushClassPrivateMethod(classBody, privateMethod, false, false);
  13382. return;
  13383. }
  13384. const isConstructor = this.isNonstaticConstructor(publicMethod);
  13385. let allowsDirectSuper = false;
  13386. if (isConstructor) {
  13387. publicMethod.kind = "constructor";
  13388. if (state.hadConstructor && !this.hasPlugin("typescript")) {
  13389. this.raise(Errors.DuplicateConstructor, {
  13390. at: key
  13391. });
  13392. }
  13393. if (isConstructor && this.hasPlugin("typescript") && member.override) {
  13394. this.raise(Errors.OverrideOnConstructor, {
  13395. at: key
  13396. });
  13397. }
  13398. state.hadConstructor = true;
  13399. allowsDirectSuper = state.hadSuperClass;
  13400. }
  13401. this.pushClassMethod(classBody, publicMethod, false, false, isConstructor, allowsDirectSuper);
  13402. } else if (this.isClassProperty()) {
  13403. if (isPrivate) {
  13404. this.pushClassPrivateProperty(classBody, privateProp);
  13405. } else {
  13406. this.pushClassProperty(classBody, publicProp);
  13407. }
  13408. } else if (isContextual && key.name === "async" && !this.isLineTerminator()) {
  13409. this.resetPreviousNodeTrailingComments(key);
  13410. const isGenerator = this.eat(55);
  13411. if (publicMember.optional) {
  13412. this.unexpected(maybeQuestionTokenStartLoc);
  13413. }
  13414. method.kind = "method";
  13415. const isPrivate = this.match(136);
  13416. this.parseClassElementName(method);
  13417. this.parsePostMemberNameModifiers(publicMember);
  13418. if (isPrivate) {
  13419. this.pushClassPrivateMethod(classBody, privateMethod, isGenerator, true);
  13420. } else {
  13421. if (this.isNonstaticConstructor(publicMethod)) {
  13422. this.raise(Errors.ConstructorIsAsync, {
  13423. at: publicMethod.key
  13424. });
  13425. }
  13426. this.pushClassMethod(classBody, publicMethod, isGenerator, true, false, false);
  13427. }
  13428. } else if (isContextual && (key.name === "get" || key.name === "set") && !(this.match(55) && this.isLineTerminator())) {
  13429. this.resetPreviousNodeTrailingComments(key);
  13430. method.kind = key.name;
  13431. const isPrivate = this.match(136);
  13432. this.parseClassElementName(publicMethod);
  13433. if (isPrivate) {
  13434. this.pushClassPrivateMethod(classBody, privateMethod, false, false);
  13435. } else {
  13436. if (this.isNonstaticConstructor(publicMethod)) {
  13437. this.raise(Errors.ConstructorIsAccessor, {
  13438. at: publicMethod.key
  13439. });
  13440. }
  13441. this.pushClassMethod(classBody, publicMethod, false, false, false, false);
  13442. }
  13443. this.checkGetterSetterParams(publicMethod);
  13444. } else if (isContextual && key.name === "accessor" && !this.isLineTerminator()) {
  13445. this.expectPlugin("decoratorAutoAccessors");
  13446. this.resetPreviousNodeTrailingComments(key);
  13447. const isPrivate = this.match(136);
  13448. this.parseClassElementName(publicProp);
  13449. this.pushClassAccessorProperty(classBody, accessorProp, isPrivate);
  13450. } else if (this.isLineTerminator()) {
  13451. if (isPrivate) {
  13452. this.pushClassPrivateProperty(classBody, privateProp);
  13453. } else {
  13454. this.pushClassProperty(classBody, publicProp);
  13455. }
  13456. } else {
  13457. this.unexpected();
  13458. }
  13459. }
  13460. parseClassElementName(member) {
  13461. const {
  13462. type,
  13463. value
  13464. } = this.state;
  13465. if ((type === 130 || type === 131) && member.static && value === "prototype") {
  13466. this.raise(Errors.StaticPrototype, {
  13467. at: this.state.startLoc
  13468. });
  13469. }
  13470. if (type === 136) {
  13471. if (value === "constructor") {
  13472. this.raise(Errors.ConstructorClassPrivateField, {
  13473. at: this.state.startLoc
  13474. });
  13475. }
  13476. const key = this.parsePrivateName();
  13477. member.key = key;
  13478. return key;
  13479. }
  13480. return this.parsePropertyName(member);
  13481. }
  13482. parseClassStaticBlock(classBody, member) {
  13483. var _member$decorators;
  13484. this.scope.enter(SCOPE_CLASS | SCOPE_STATIC_BLOCK | SCOPE_SUPER);
  13485. const oldLabels = this.state.labels;
  13486. this.state.labels = [];
  13487. this.prodParam.enter(PARAM);
  13488. const body = member.body = [];
  13489. this.parseBlockOrModuleBlockBody(body, undefined, false, 8);
  13490. this.prodParam.exit();
  13491. this.scope.exit();
  13492. this.state.labels = oldLabels;
  13493. classBody.body.push(this.finishNode(member, "StaticBlock"));
  13494. if ((_member$decorators = member.decorators) != null && _member$decorators.length) {
  13495. this.raise(Errors.DecoratorStaticBlock, {
  13496. at: member
  13497. });
  13498. }
  13499. }
  13500. pushClassProperty(classBody, prop) {
  13501. if (!prop.computed && (prop.key.name === "constructor" || prop.key.value === "constructor")) {
  13502. this.raise(Errors.ConstructorClassField, {
  13503. at: prop.key
  13504. });
  13505. }
  13506. classBody.body.push(this.parseClassProperty(prop));
  13507. }
  13508. pushClassPrivateProperty(classBody, prop) {
  13509. const node = this.parseClassPrivateProperty(prop);
  13510. classBody.body.push(node);
  13511. this.classScope.declarePrivateName(this.getPrivateNameSV(node.key), CLASS_ELEMENT_OTHER, node.key.loc.start);
  13512. }
  13513. pushClassAccessorProperty(classBody, prop, isPrivate) {
  13514. if (!isPrivate && !prop.computed) {
  13515. const key = prop.key;
  13516. if (key.name === "constructor" || key.value === "constructor") {
  13517. this.raise(Errors.ConstructorClassField, {
  13518. at: key
  13519. });
  13520. }
  13521. }
  13522. const node = this.parseClassAccessorProperty(prop);
  13523. classBody.body.push(node);
  13524. if (isPrivate) {
  13525. this.classScope.declarePrivateName(this.getPrivateNameSV(node.key), CLASS_ELEMENT_OTHER, node.key.loc.start);
  13526. }
  13527. }
  13528. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  13529. classBody.body.push(this.parseMethod(method, isGenerator, isAsync, isConstructor, allowsDirectSuper, "ClassMethod", true));
  13530. }
  13531. pushClassPrivateMethod(classBody, method, isGenerator, isAsync) {
  13532. const node = this.parseMethod(method, isGenerator, isAsync, false, false, "ClassPrivateMethod", true);
  13533. classBody.body.push(node);
  13534. 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;
  13535. this.declareClassPrivateMethodInScope(node, kind);
  13536. }
  13537. declareClassPrivateMethodInScope(node, kind) {
  13538. this.classScope.declarePrivateName(this.getPrivateNameSV(node.key), kind, node.key.loc.start);
  13539. }
  13540. parsePostMemberNameModifiers(methodOrProp) {}
  13541. parseClassPrivateProperty(node) {
  13542. this.parseInitializer(node);
  13543. this.semicolon();
  13544. return this.finishNode(node, "ClassPrivateProperty");
  13545. }
  13546. parseClassProperty(node) {
  13547. this.parseInitializer(node);
  13548. this.semicolon();
  13549. return this.finishNode(node, "ClassProperty");
  13550. }
  13551. parseClassAccessorProperty(node) {
  13552. this.parseInitializer(node);
  13553. this.semicolon();
  13554. return this.finishNode(node, "ClassAccessorProperty");
  13555. }
  13556. parseInitializer(node) {
  13557. this.scope.enter(SCOPE_CLASS | SCOPE_SUPER);
  13558. this.expressionScope.enter(newExpressionScope());
  13559. this.prodParam.enter(PARAM);
  13560. node.value = this.eat(29) ? this.parseMaybeAssignAllowIn() : null;
  13561. this.expressionScope.exit();
  13562. this.prodParam.exit();
  13563. this.scope.exit();
  13564. }
  13565. parseClassId(node, isStatement, optionalId, bindingType = BIND_CLASS) {
  13566. if (tokenIsIdentifier(this.state.type)) {
  13567. node.id = this.parseIdentifier();
  13568. if (isStatement) {
  13569. this.declareNameFromIdentifier(node.id, bindingType);
  13570. }
  13571. } else {
  13572. if (optionalId || !isStatement) {
  13573. node.id = null;
  13574. } else {
  13575. throw this.raise(Errors.MissingClassName, {
  13576. at: this.state.startLoc
  13577. });
  13578. }
  13579. }
  13580. }
  13581. parseClassSuper(node) {
  13582. node.superClass = this.eat(81) ? this.parseExprSubscripts() : null;
  13583. }
  13584. parseExport(node, decorators) {
  13585. const hasDefault = this.maybeParseExportDefaultSpecifier(node);
  13586. const parseAfterDefault = !hasDefault || this.eat(12);
  13587. const hasStar = parseAfterDefault && this.eatExportStar(node);
  13588. const hasNamespace = hasStar && this.maybeParseExportNamespaceSpecifier(node);
  13589. const parseAfterNamespace = parseAfterDefault && (!hasNamespace || this.eat(12));
  13590. const isFromRequired = hasDefault || hasStar;
  13591. if (hasStar && !hasNamespace) {
  13592. if (hasDefault) this.unexpected();
  13593. if (decorators) {
  13594. throw this.raise(Errors.UnsupportedDecoratorExport, {
  13595. at: node
  13596. });
  13597. }
  13598. this.parseExportFrom(node, true);
  13599. return this.finishNode(node, "ExportAllDeclaration");
  13600. }
  13601. const hasSpecifiers = this.maybeParseExportNamedSpecifiers(node);
  13602. if (hasDefault && parseAfterDefault && !hasStar && !hasSpecifiers) {
  13603. throw this.unexpected(null, 5);
  13604. }
  13605. if (hasNamespace && parseAfterNamespace) {
  13606. throw this.unexpected(null, 97);
  13607. }
  13608. let hasDeclaration;
  13609. if (isFromRequired || hasSpecifiers) {
  13610. hasDeclaration = false;
  13611. if (decorators) {
  13612. throw this.raise(Errors.UnsupportedDecoratorExport, {
  13613. at: node
  13614. });
  13615. }
  13616. this.parseExportFrom(node, isFromRequired);
  13617. } else {
  13618. hasDeclaration = this.maybeParseExportDeclaration(node);
  13619. }
  13620. if (isFromRequired || hasSpecifiers || hasDeclaration) {
  13621. var _node2$declaration;
  13622. const node2 = node;
  13623. this.checkExport(node2, true, false, !!node2.source);
  13624. if (((_node2$declaration = node2.declaration) == null ? void 0 : _node2$declaration.type) === "ClassDeclaration") {
  13625. this.maybeTakeDecorators(decorators, node2.declaration, node2);
  13626. } else if (decorators) {
  13627. throw this.raise(Errors.UnsupportedDecoratorExport, {
  13628. at: node
  13629. });
  13630. }
  13631. return this.finishNode(node2, "ExportNamedDeclaration");
  13632. }
  13633. if (this.eat(65)) {
  13634. const node2 = node;
  13635. const decl = this.parseExportDefaultExpression();
  13636. node2.declaration = decl;
  13637. if (decl.type === "ClassDeclaration") {
  13638. this.maybeTakeDecorators(decorators, decl, node2);
  13639. } else if (decorators) {
  13640. throw this.raise(Errors.UnsupportedDecoratorExport, {
  13641. at: node
  13642. });
  13643. }
  13644. this.checkExport(node2, true, true);
  13645. return this.finishNode(node2, "ExportDefaultDeclaration");
  13646. }
  13647. throw this.unexpected(null, 5);
  13648. }
  13649. eatExportStar(node) {
  13650. return this.eat(55);
  13651. }
  13652. maybeParseExportDefaultSpecifier(node) {
  13653. if (this.isExportDefaultSpecifier()) {
  13654. this.expectPlugin("exportDefaultFrom");
  13655. const specifier = this.startNode();
  13656. specifier.exported = this.parseIdentifier(true);
  13657. node.specifiers = [this.finishNode(specifier, "ExportDefaultSpecifier")];
  13658. return true;
  13659. }
  13660. return false;
  13661. }
  13662. maybeParseExportNamespaceSpecifier(node) {
  13663. if (this.isContextual(93)) {
  13664. if (!node.specifiers) node.specifiers = [];
  13665. const specifier = this.startNodeAt(this.state.lastTokStartLoc);
  13666. this.next();
  13667. specifier.exported = this.parseModuleExportName();
  13668. node.specifiers.push(this.finishNode(specifier, "ExportNamespaceSpecifier"));
  13669. return true;
  13670. }
  13671. return false;
  13672. }
  13673. maybeParseExportNamedSpecifiers(node) {
  13674. if (this.match(5)) {
  13675. if (!node.specifiers) node.specifiers = [];
  13676. const isTypeExport = node.exportKind === "type";
  13677. node.specifiers.push(...this.parseExportSpecifiers(isTypeExport));
  13678. node.source = null;
  13679. node.declaration = null;
  13680. if (this.hasPlugin("importAssertions")) {
  13681. node.assertions = [];
  13682. }
  13683. return true;
  13684. }
  13685. return false;
  13686. }
  13687. maybeParseExportDeclaration(node) {
  13688. if (this.shouldParseExportDeclaration()) {
  13689. node.specifiers = [];
  13690. node.source = null;
  13691. if (this.hasPlugin("importAssertions")) {
  13692. node.assertions = [];
  13693. }
  13694. node.declaration = this.parseExportDeclaration(node);
  13695. return true;
  13696. }
  13697. return false;
  13698. }
  13699. isAsyncFunction() {
  13700. if (!this.isContextual(95)) return false;
  13701. const next = this.nextTokenStart();
  13702. return !lineBreak.test(this.input.slice(this.state.pos, next)) && this.isUnparsedContextual(next, "function");
  13703. }
  13704. parseExportDefaultExpression() {
  13705. const expr = this.startNode();
  13706. if (this.match(68)) {
  13707. this.next();
  13708. return this.parseFunction(expr, ParseFunctionFlag.Declaration | ParseFunctionFlag.NullableId);
  13709. } else if (this.isAsyncFunction()) {
  13710. this.next();
  13711. this.next();
  13712. return this.parseFunction(expr, ParseFunctionFlag.Declaration | ParseFunctionFlag.NullableId | ParseFunctionFlag.Async);
  13713. }
  13714. if (this.match(80)) {
  13715. return this.parseClass(expr, true, true);
  13716. }
  13717. if (this.match(26)) {
  13718. if (this.hasPlugin("decorators") && this.getPluginOption("decorators", "decoratorsBeforeExport") === true) {
  13719. this.raise(Errors.DecoratorBeforeExport, {
  13720. at: this.state.startLoc
  13721. });
  13722. }
  13723. return this.parseClass(this.maybeTakeDecorators(this.parseDecorators(false), this.startNode()), true, true);
  13724. }
  13725. if (this.match(75) || this.match(74) || this.isLet()) {
  13726. throw this.raise(Errors.UnsupportedDefaultExport, {
  13727. at: this.state.startLoc
  13728. });
  13729. }
  13730. const res = this.parseMaybeAssignAllowIn();
  13731. this.semicolon();
  13732. return res;
  13733. }
  13734. parseExportDeclaration(node) {
  13735. if (this.match(80)) {
  13736. const node = this.parseClass(this.startNode(), true, false);
  13737. return node;
  13738. }
  13739. return this.parseStatementListItem();
  13740. }
  13741. isExportDefaultSpecifier() {
  13742. const {
  13743. type
  13744. } = this.state;
  13745. if (tokenIsIdentifier(type)) {
  13746. if (type === 95 && !this.state.containsEsc || type === 99) {
  13747. return false;
  13748. }
  13749. if ((type === 128 || type === 127) && !this.state.containsEsc) {
  13750. const {
  13751. type: nextType
  13752. } = this.lookahead();
  13753. if (tokenIsIdentifier(nextType) && nextType !== 97 || nextType === 5) {
  13754. this.expectOnePlugin(["flow", "typescript"]);
  13755. return false;
  13756. }
  13757. }
  13758. } else if (!this.match(65)) {
  13759. return false;
  13760. }
  13761. const next = this.nextTokenStart();
  13762. const hasFrom = this.isUnparsedContextual(next, "from");
  13763. if (this.input.charCodeAt(next) === 44 || tokenIsIdentifier(this.state.type) && hasFrom) {
  13764. return true;
  13765. }
  13766. if (this.match(65) && hasFrom) {
  13767. const nextAfterFrom = this.input.charCodeAt(this.nextTokenStartSince(next + 4));
  13768. return nextAfterFrom === 34 || nextAfterFrom === 39;
  13769. }
  13770. return false;
  13771. }
  13772. parseExportFrom(node, expect) {
  13773. if (this.eatContextual(97)) {
  13774. node.source = this.parseImportSource();
  13775. this.checkExport(node);
  13776. const assertions = this.maybeParseImportAssertions();
  13777. if (assertions) {
  13778. node.assertions = assertions;
  13779. this.checkJSONModuleImport(node);
  13780. }
  13781. } else if (expect) {
  13782. this.unexpected();
  13783. }
  13784. this.semicolon();
  13785. }
  13786. shouldParseExportDeclaration() {
  13787. const {
  13788. type
  13789. } = this.state;
  13790. if (type === 26) {
  13791. this.expectOnePlugin(["decorators", "decorators-legacy"]);
  13792. if (this.hasPlugin("decorators")) {
  13793. if (this.getPluginOption("decorators", "decoratorsBeforeExport") === true) {
  13794. this.raise(Errors.DecoratorBeforeExport, {
  13795. at: this.state.startLoc
  13796. });
  13797. }
  13798. return true;
  13799. }
  13800. }
  13801. return type === 74 || type === 75 || type === 68 || type === 80 || this.isLet() || this.isAsyncFunction();
  13802. }
  13803. checkExport(node, checkNames, isDefault, isFrom) {
  13804. if (checkNames) {
  13805. if (isDefault) {
  13806. this.checkDuplicateExports(node, "default");
  13807. if (this.hasPlugin("exportDefaultFrom")) {
  13808. var _declaration$extra;
  13809. const declaration = node.declaration;
  13810. if (declaration.type === "Identifier" && declaration.name === "from" && declaration.end - declaration.start === 4 && !((_declaration$extra = declaration.extra) != null && _declaration$extra.parenthesized)) {
  13811. this.raise(Errors.ExportDefaultFromAsIdentifier, {
  13812. at: declaration
  13813. });
  13814. }
  13815. }
  13816. } else if (node.specifiers && node.specifiers.length) {
  13817. for (const specifier of node.specifiers) {
  13818. const {
  13819. exported
  13820. } = specifier;
  13821. const exportName = exported.type === "Identifier" ? exported.name : exported.value;
  13822. this.checkDuplicateExports(specifier, exportName);
  13823. if (!isFrom && specifier.local) {
  13824. const {
  13825. local
  13826. } = specifier;
  13827. if (local.type !== "Identifier") {
  13828. this.raise(Errors.ExportBindingIsString, {
  13829. at: specifier,
  13830. localName: local.value,
  13831. exportName
  13832. });
  13833. } else {
  13834. this.checkReservedWord(local.name, local.loc.start, true, false);
  13835. this.scope.checkLocalExport(local);
  13836. }
  13837. }
  13838. }
  13839. } else if (node.declaration) {
  13840. if (node.declaration.type === "FunctionDeclaration" || node.declaration.type === "ClassDeclaration") {
  13841. const id = node.declaration.id;
  13842. if (!id) throw new Error("Assertion failure");
  13843. this.checkDuplicateExports(node, id.name);
  13844. } else if (node.declaration.type === "VariableDeclaration") {
  13845. for (const declaration of node.declaration.declarations) {
  13846. this.checkDeclaration(declaration.id);
  13847. }
  13848. }
  13849. }
  13850. }
  13851. }
  13852. checkDeclaration(node) {
  13853. if (node.type === "Identifier") {
  13854. this.checkDuplicateExports(node, node.name);
  13855. } else if (node.type === "ObjectPattern") {
  13856. for (const prop of node.properties) {
  13857. this.checkDeclaration(prop);
  13858. }
  13859. } else if (node.type === "ArrayPattern") {
  13860. for (const elem of node.elements) {
  13861. if (elem) {
  13862. this.checkDeclaration(elem);
  13863. }
  13864. }
  13865. } else if (node.type === "ObjectProperty") {
  13866. this.checkDeclaration(node.value);
  13867. } else if (node.type === "RestElement") {
  13868. this.checkDeclaration(node.argument);
  13869. } else if (node.type === "AssignmentPattern") {
  13870. this.checkDeclaration(node.left);
  13871. }
  13872. }
  13873. checkDuplicateExports(node, exportName) {
  13874. if (this.exportedIdentifiers.has(exportName)) {
  13875. if (exportName === "default") {
  13876. this.raise(Errors.DuplicateDefaultExport, {
  13877. at: node
  13878. });
  13879. } else {
  13880. this.raise(Errors.DuplicateExport, {
  13881. at: node,
  13882. exportName
  13883. });
  13884. }
  13885. }
  13886. this.exportedIdentifiers.add(exportName);
  13887. }
  13888. parseExportSpecifiers(isInTypeExport) {
  13889. const nodes = [];
  13890. let first = true;
  13891. this.expect(5);
  13892. while (!this.eat(8)) {
  13893. if (first) {
  13894. first = false;
  13895. } else {
  13896. this.expect(12);
  13897. if (this.eat(8)) break;
  13898. }
  13899. const isMaybeTypeOnly = this.isContextual(128);
  13900. const isString = this.match(131);
  13901. const node = this.startNode();
  13902. node.local = this.parseModuleExportName();
  13903. nodes.push(this.parseExportSpecifier(node, isString, isInTypeExport, isMaybeTypeOnly));
  13904. }
  13905. return nodes;
  13906. }
  13907. parseExportSpecifier(node, isString, isInTypeExport, isMaybeTypeOnly) {
  13908. if (this.eatContextual(93)) {
  13909. node.exported = this.parseModuleExportName();
  13910. } else if (isString) {
  13911. node.exported = cloneStringLiteral(node.local);
  13912. } else if (!node.exported) {
  13913. node.exported = cloneIdentifier(node.local);
  13914. }
  13915. return this.finishNode(node, "ExportSpecifier");
  13916. }
  13917. parseModuleExportName() {
  13918. if (this.match(131)) {
  13919. const result = this.parseStringLiteral(this.state.value);
  13920. const surrogate = result.value.match(loneSurrogate);
  13921. if (surrogate) {
  13922. this.raise(Errors.ModuleExportNameHasLoneSurrogate, {
  13923. at: result,
  13924. surrogateCharCode: surrogate[0].charCodeAt(0)
  13925. });
  13926. }
  13927. return result;
  13928. }
  13929. return this.parseIdentifier(true);
  13930. }
  13931. isJSONModuleImport(node) {
  13932. if (node.assertions != null) {
  13933. return node.assertions.some(({
  13934. key,
  13935. value
  13936. }) => {
  13937. return value.value === "json" && (key.type === "Identifier" ? key.name === "type" : key.value === "type");
  13938. });
  13939. }
  13940. return false;
  13941. }
  13942. checkImportReflection(node) {
  13943. if (node.module) {
  13944. var _node$assertions;
  13945. if (node.specifiers.length !== 1 || node.specifiers[0].type !== "ImportDefaultSpecifier") {
  13946. this.raise(Errors.ImportReflectionNotBinding, {
  13947. at: node.specifiers[0].loc.start
  13948. });
  13949. }
  13950. if (((_node$assertions = node.assertions) == null ? void 0 : _node$assertions.length) > 0) {
  13951. this.raise(Errors.ImportReflectionHasAssertion, {
  13952. at: node.specifiers[0].loc.start
  13953. });
  13954. }
  13955. }
  13956. }
  13957. checkJSONModuleImport(node) {
  13958. if (this.isJSONModuleImport(node) && node.type !== "ExportAllDeclaration") {
  13959. const {
  13960. specifiers
  13961. } = node;
  13962. if (specifiers != null) {
  13963. const nonDefaultNamedSpecifier = specifiers.find(specifier => {
  13964. let imported;
  13965. if (specifier.type === "ExportSpecifier") {
  13966. imported = specifier.local;
  13967. } else if (specifier.type === "ImportSpecifier") {
  13968. imported = specifier.imported;
  13969. }
  13970. if (imported !== undefined) {
  13971. return imported.type === "Identifier" ? imported.name !== "default" : imported.value !== "default";
  13972. }
  13973. });
  13974. if (nonDefaultNamedSpecifier !== undefined) {
  13975. this.raise(Errors.ImportJSONBindingNotDefault, {
  13976. at: nonDefaultNamedSpecifier.loc.start
  13977. });
  13978. }
  13979. }
  13980. }
  13981. }
  13982. parseMaybeImportReflection(node) {
  13983. let isImportReflection = false;
  13984. if (this.isContextual(125)) {
  13985. const lookahead = this.lookahead();
  13986. const nextType = lookahead.type;
  13987. if (tokenIsIdentifier(nextType)) {
  13988. if (nextType !== 97) {
  13989. isImportReflection = true;
  13990. } else {
  13991. const nextNextTokenFirstChar = this.input.charCodeAt(this.nextTokenStartSince(lookahead.end));
  13992. if (nextNextTokenFirstChar === 102) {
  13993. isImportReflection = true;
  13994. }
  13995. }
  13996. } else if (nextType !== 12) {
  13997. isImportReflection = true;
  13998. }
  13999. }
  14000. if (isImportReflection) {
  14001. this.expectPlugin("importReflection");
  14002. this.next();
  14003. node.module = true;
  14004. } else if (this.hasPlugin("importReflection")) {
  14005. node.module = false;
  14006. }
  14007. }
  14008. parseImport(node) {
  14009. node.specifiers = [];
  14010. if (!this.match(131)) {
  14011. this.parseMaybeImportReflection(node);
  14012. const hasDefault = this.maybeParseDefaultImportSpecifier(node);
  14013. const parseNext = !hasDefault || this.eat(12);
  14014. const hasStar = parseNext && this.maybeParseStarImportSpecifier(node);
  14015. if (parseNext && !hasStar) this.parseNamedImportSpecifiers(node);
  14016. this.expectContextual(97);
  14017. }
  14018. node.source = this.parseImportSource();
  14019. const assertions = this.maybeParseImportAssertions();
  14020. if (assertions) {
  14021. node.assertions = assertions;
  14022. } else {
  14023. const attributes = this.maybeParseModuleAttributes();
  14024. if (attributes) {
  14025. node.attributes = attributes;
  14026. }
  14027. }
  14028. this.checkImportReflection(node);
  14029. this.checkJSONModuleImport(node);
  14030. this.semicolon();
  14031. return this.finishNode(node, "ImportDeclaration");
  14032. }
  14033. parseImportSource() {
  14034. if (!this.match(131)) this.unexpected();
  14035. return this.parseExprAtom();
  14036. }
  14037. shouldParseDefaultImport(node) {
  14038. return tokenIsIdentifier(this.state.type);
  14039. }
  14040. parseImportSpecifierLocal(node, specifier, type) {
  14041. specifier.local = this.parseIdentifier();
  14042. node.specifiers.push(this.finishImportSpecifier(specifier, type));
  14043. }
  14044. finishImportSpecifier(specifier, type, bindingType = BIND_LEXICAL) {
  14045. this.checkLVal(specifier.local, {
  14046. in: {
  14047. type
  14048. },
  14049. binding: bindingType
  14050. });
  14051. return this.finishNode(specifier, type);
  14052. }
  14053. parseAssertEntries() {
  14054. const attrs = [];
  14055. const attrNames = new Set();
  14056. do {
  14057. if (this.match(8)) {
  14058. break;
  14059. }
  14060. const node = this.startNode();
  14061. const keyName = this.state.value;
  14062. if (attrNames.has(keyName)) {
  14063. this.raise(Errors.ModuleAttributesWithDuplicateKeys, {
  14064. at: this.state.startLoc,
  14065. key: keyName
  14066. });
  14067. }
  14068. attrNames.add(keyName);
  14069. if (this.match(131)) {
  14070. node.key = this.parseStringLiteral(keyName);
  14071. } else {
  14072. node.key = this.parseIdentifier(true);
  14073. }
  14074. this.expect(14);
  14075. if (!this.match(131)) {
  14076. throw this.raise(Errors.ModuleAttributeInvalidValue, {
  14077. at: this.state.startLoc
  14078. });
  14079. }
  14080. node.value = this.parseStringLiteral(this.state.value);
  14081. attrs.push(this.finishNode(node, "ImportAttribute"));
  14082. } while (this.eat(12));
  14083. return attrs;
  14084. }
  14085. maybeParseModuleAttributes() {
  14086. if (this.match(76) && !this.hasPrecedingLineBreak()) {
  14087. this.expectPlugin("moduleAttributes");
  14088. this.next();
  14089. } else {
  14090. if (this.hasPlugin("moduleAttributes")) return [];
  14091. return null;
  14092. }
  14093. const attrs = [];
  14094. const attributes = new Set();
  14095. do {
  14096. const node = this.startNode();
  14097. node.key = this.parseIdentifier(true);
  14098. if (node.key.name !== "type") {
  14099. this.raise(Errors.ModuleAttributeDifferentFromType, {
  14100. at: node.key
  14101. });
  14102. }
  14103. if (attributes.has(node.key.name)) {
  14104. this.raise(Errors.ModuleAttributesWithDuplicateKeys, {
  14105. at: node.key,
  14106. key: node.key.name
  14107. });
  14108. }
  14109. attributes.add(node.key.name);
  14110. this.expect(14);
  14111. if (!this.match(131)) {
  14112. throw this.raise(Errors.ModuleAttributeInvalidValue, {
  14113. at: this.state.startLoc
  14114. });
  14115. }
  14116. node.value = this.parseStringLiteral(this.state.value);
  14117. this.finishNode(node, "ImportAttribute");
  14118. attrs.push(node);
  14119. } while (this.eat(12));
  14120. return attrs;
  14121. }
  14122. maybeParseImportAssertions() {
  14123. if (this.isContextual(94) && !this.hasPrecedingLineBreak()) {
  14124. this.expectPlugin("importAssertions");
  14125. this.next();
  14126. } else {
  14127. if (this.hasPlugin("importAssertions")) return [];
  14128. return null;
  14129. }
  14130. this.eat(5);
  14131. const attrs = this.parseAssertEntries();
  14132. this.eat(8);
  14133. return attrs;
  14134. }
  14135. maybeParseDefaultImportSpecifier(node) {
  14136. if (this.shouldParseDefaultImport(node)) {
  14137. this.parseImportSpecifierLocal(node, this.startNode(), "ImportDefaultSpecifier");
  14138. return true;
  14139. }
  14140. return false;
  14141. }
  14142. maybeParseStarImportSpecifier(node) {
  14143. if (this.match(55)) {
  14144. const specifier = this.startNode();
  14145. this.next();
  14146. this.expectContextual(93);
  14147. this.parseImportSpecifierLocal(node, specifier, "ImportNamespaceSpecifier");
  14148. return true;
  14149. }
  14150. return false;
  14151. }
  14152. parseNamedImportSpecifiers(node) {
  14153. let first = true;
  14154. this.expect(5);
  14155. while (!this.eat(8)) {
  14156. if (first) {
  14157. first = false;
  14158. } else {
  14159. if (this.eat(14)) {
  14160. throw this.raise(Errors.DestructureNamedImport, {
  14161. at: this.state.startLoc
  14162. });
  14163. }
  14164. this.expect(12);
  14165. if (this.eat(8)) break;
  14166. }
  14167. const specifier = this.startNode();
  14168. const importedIsString = this.match(131);
  14169. const isMaybeTypeOnly = this.isContextual(128);
  14170. specifier.imported = this.parseModuleExportName();
  14171. const importSpecifier = this.parseImportSpecifier(specifier, importedIsString, node.importKind === "type" || node.importKind === "typeof", isMaybeTypeOnly, undefined);
  14172. node.specifiers.push(importSpecifier);
  14173. }
  14174. }
  14175. parseImportSpecifier(specifier, importedIsString, isInTypeOnlyImport, isMaybeTypeOnly, bindingType) {
  14176. if (this.eatContextual(93)) {
  14177. specifier.local = this.parseIdentifier();
  14178. } else {
  14179. const {
  14180. imported
  14181. } = specifier;
  14182. if (importedIsString) {
  14183. throw this.raise(Errors.ImportBindingIsString, {
  14184. at: specifier,
  14185. importName: imported.value
  14186. });
  14187. }
  14188. this.checkReservedWord(imported.name, specifier.loc.start, true, true);
  14189. if (!specifier.local) {
  14190. specifier.local = cloneIdentifier(imported);
  14191. }
  14192. }
  14193. return this.finishImportSpecifier(specifier, "ImportSpecifier", bindingType);
  14194. }
  14195. isThisParam(param) {
  14196. return param.type === "Identifier" && param.name === "this";
  14197. }
  14198. }
  14199. class Parser extends StatementParser {
  14200. constructor(options, input) {
  14201. options = getOptions(options);
  14202. super(options, input);
  14203. this.options = options;
  14204. this.initializeScopes();
  14205. this.plugins = pluginsMap(this.options.plugins);
  14206. this.filename = options.sourceFilename;
  14207. }
  14208. getScopeHandler() {
  14209. return ScopeHandler;
  14210. }
  14211. parse() {
  14212. this.enterInitialScopes();
  14213. const file = this.startNode();
  14214. const program = this.startNode();
  14215. this.nextToken();
  14216. file.errors = null;
  14217. this.parseTopLevel(file, program);
  14218. file.errors = this.state.errors;
  14219. return file;
  14220. }
  14221. }
  14222. function pluginsMap(plugins) {
  14223. const pluginMap = new Map();
  14224. for (const plugin of plugins) {
  14225. const [name, options] = Array.isArray(plugin) ? plugin : [plugin, {}];
  14226. if (!pluginMap.has(name)) pluginMap.set(name, options || {});
  14227. }
  14228. return pluginMap;
  14229. }
  14230. function parse(input, options) {
  14231. var _options;
  14232. if (((_options = options) == null ? void 0 : _options.sourceType) === "unambiguous") {
  14233. options = Object.assign({}, options);
  14234. try {
  14235. options.sourceType = "module";
  14236. const parser = getParser(options, input);
  14237. const ast = parser.parse();
  14238. if (parser.sawUnambiguousESM) {
  14239. return ast;
  14240. }
  14241. if (parser.ambiguousScriptDifferentAst) {
  14242. try {
  14243. options.sourceType = "script";
  14244. return getParser(options, input).parse();
  14245. } catch (_unused) {}
  14246. } else {
  14247. ast.program.sourceType = "script";
  14248. }
  14249. return ast;
  14250. } catch (moduleError) {
  14251. try {
  14252. options.sourceType = "script";
  14253. return getParser(options, input).parse();
  14254. } catch (_unused2) {}
  14255. throw moduleError;
  14256. }
  14257. } else {
  14258. return getParser(options, input).parse();
  14259. }
  14260. }
  14261. function parseExpression(input, options) {
  14262. const parser = getParser(options, input);
  14263. if (parser.options.strictMode) {
  14264. parser.state.strict = true;
  14265. }
  14266. return parser.getExpression();
  14267. }
  14268. function generateExportedTokenTypes(internalTokenTypes) {
  14269. const tokenTypes = {};
  14270. for (const typeName of Object.keys(internalTokenTypes)) {
  14271. tokenTypes[typeName] = getExportedToken(internalTokenTypes[typeName]);
  14272. }
  14273. return tokenTypes;
  14274. }
  14275. const tokTypes = generateExportedTokenTypes(tt);
  14276. function getParser(options, input) {
  14277. let cls = Parser;
  14278. if (options != null && options.plugins) {
  14279. validatePlugins(options.plugins);
  14280. cls = getParserClass(options.plugins);
  14281. }
  14282. return new cls(options, input);
  14283. }
  14284. const parserClassCache = {};
  14285. function getParserClass(pluginsFromOptions) {
  14286. const pluginList = mixinPluginNames.filter(name => hasPlugin(pluginsFromOptions, name));
  14287. const key = pluginList.join("/");
  14288. let cls = parserClassCache[key];
  14289. if (!cls) {
  14290. cls = Parser;
  14291. for (const plugin of pluginList) {
  14292. cls = mixinPlugins[plugin](cls);
  14293. }
  14294. parserClassCache[key] = cls;
  14295. }
  14296. return cls;
  14297. }
  14298. exports.parse = parse;
  14299. exports.parseExpression = parseExpression;
  14300. exports.tokTypes = tokTypes;
  14301. //# sourceMappingURL=index.js.map