index.js 327 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. const beforeExpr = true;
  4. const startsExpr = true;
  5. const isLoop = true;
  6. const isAssign = true;
  7. const prefix = true;
  8. const postfix = true;
  9. class TokenType {
  10. constructor(label, conf = {}) {
  11. this.label = label;
  12. this.keyword = conf.keyword;
  13. this.beforeExpr = !!conf.beforeExpr;
  14. this.startsExpr = !!conf.startsExpr;
  15. this.rightAssociative = !!conf.rightAssociative;
  16. this.isLoop = !!conf.isLoop;
  17. this.isAssign = !!conf.isAssign;
  18. this.prefix = !!conf.prefix;
  19. this.postfix = !!conf.postfix;
  20. this.binop = conf.binop != null ? conf.binop : null;
  21. this.updateContext = null;
  22. }
  23. }
  24. const keywords = new Map();
  25. function createKeyword(name, options = {}) {
  26. options.keyword = name;
  27. const token = new TokenType(name, options);
  28. keywords.set(name, token);
  29. return token;
  30. }
  31. function createBinop(name, binop) {
  32. return new TokenType(name, {
  33. beforeExpr,
  34. binop
  35. });
  36. }
  37. const types = {
  38. num: new TokenType("num", {
  39. startsExpr
  40. }),
  41. bigint: new TokenType("bigint", {
  42. startsExpr
  43. }),
  44. regexp: new TokenType("regexp", {
  45. startsExpr
  46. }),
  47. string: new TokenType("string", {
  48. startsExpr
  49. }),
  50. name: new TokenType("name", {
  51. startsExpr
  52. }),
  53. eof: new TokenType("eof"),
  54. bracketL: new TokenType("[", {
  55. beforeExpr,
  56. startsExpr
  57. }),
  58. bracketR: new TokenType("]"),
  59. braceL: new TokenType("{", {
  60. beforeExpr,
  61. startsExpr
  62. }),
  63. braceBarL: new TokenType("{|", {
  64. beforeExpr,
  65. startsExpr
  66. }),
  67. braceR: new TokenType("}"),
  68. braceBarR: new TokenType("|}"),
  69. parenL: new TokenType("(", {
  70. beforeExpr,
  71. startsExpr
  72. }),
  73. parenR: new TokenType(")"),
  74. comma: new TokenType(",", {
  75. beforeExpr
  76. }),
  77. semi: new TokenType(";", {
  78. beforeExpr
  79. }),
  80. colon: new TokenType(":", {
  81. beforeExpr
  82. }),
  83. doubleColon: new TokenType("::", {
  84. beforeExpr
  85. }),
  86. dot: new TokenType("."),
  87. question: new TokenType("?", {
  88. beforeExpr
  89. }),
  90. questionDot: new TokenType("?."),
  91. arrow: new TokenType("=>", {
  92. beforeExpr
  93. }),
  94. template: new TokenType("template"),
  95. ellipsis: new TokenType("...", {
  96. beforeExpr
  97. }),
  98. backQuote: new TokenType("`", {
  99. startsExpr
  100. }),
  101. dollarBraceL: new TokenType("${", {
  102. beforeExpr,
  103. startsExpr
  104. }),
  105. at: new TokenType("@"),
  106. hash: new TokenType("#", {
  107. startsExpr
  108. }),
  109. interpreterDirective: new TokenType("#!..."),
  110. eq: new TokenType("=", {
  111. beforeExpr,
  112. isAssign
  113. }),
  114. assign: new TokenType("_=", {
  115. beforeExpr,
  116. isAssign
  117. }),
  118. incDec: new TokenType("++/--", {
  119. prefix,
  120. postfix,
  121. startsExpr
  122. }),
  123. bang: new TokenType("!", {
  124. beforeExpr,
  125. prefix,
  126. startsExpr
  127. }),
  128. tilde: new TokenType("~", {
  129. beforeExpr,
  130. prefix,
  131. startsExpr
  132. }),
  133. pipeline: createBinop("|>", 0),
  134. nullishCoalescing: createBinop("??", 1),
  135. logicalOR: createBinop("||", 1),
  136. logicalAND: createBinop("&&", 2),
  137. bitwiseOR: createBinop("|", 3),
  138. bitwiseXOR: createBinop("^", 4),
  139. bitwiseAND: createBinop("&", 5),
  140. equality: createBinop("==/!=/===/!==", 6),
  141. relational: createBinop("</>/<=/>=", 7),
  142. bitShift: createBinop("<</>>/>>>", 8),
  143. plusMin: new TokenType("+/-", {
  144. beforeExpr,
  145. binop: 9,
  146. prefix,
  147. startsExpr
  148. }),
  149. modulo: createBinop("%", 10),
  150. star: createBinop("*", 10),
  151. slash: createBinop("/", 10),
  152. exponent: new TokenType("**", {
  153. beforeExpr,
  154. binop: 11,
  155. rightAssociative: true
  156. }),
  157. _break: createKeyword("break"),
  158. _case: createKeyword("case", {
  159. beforeExpr
  160. }),
  161. _catch: createKeyword("catch"),
  162. _continue: createKeyword("continue"),
  163. _debugger: createKeyword("debugger"),
  164. _default: createKeyword("default", {
  165. beforeExpr
  166. }),
  167. _do: createKeyword("do", {
  168. isLoop,
  169. beforeExpr
  170. }),
  171. _else: createKeyword("else", {
  172. beforeExpr
  173. }),
  174. _finally: createKeyword("finally"),
  175. _for: createKeyword("for", {
  176. isLoop
  177. }),
  178. _function: createKeyword("function", {
  179. startsExpr
  180. }),
  181. _if: createKeyword("if"),
  182. _return: createKeyword("return", {
  183. beforeExpr
  184. }),
  185. _switch: createKeyword("switch"),
  186. _throw: createKeyword("throw", {
  187. beforeExpr,
  188. prefix,
  189. startsExpr
  190. }),
  191. _try: createKeyword("try"),
  192. _var: createKeyword("var"),
  193. _const: createKeyword("const"),
  194. _while: createKeyword("while", {
  195. isLoop
  196. }),
  197. _with: createKeyword("with"),
  198. _new: createKeyword("new", {
  199. beforeExpr,
  200. startsExpr
  201. }),
  202. _this: createKeyword("this", {
  203. startsExpr
  204. }),
  205. _super: createKeyword("super", {
  206. startsExpr
  207. }),
  208. _class: createKeyword("class", {
  209. startsExpr
  210. }),
  211. _extends: createKeyword("extends", {
  212. beforeExpr
  213. }),
  214. _export: createKeyword("export"),
  215. _import: createKeyword("import", {
  216. startsExpr
  217. }),
  218. _null: createKeyword("null", {
  219. startsExpr
  220. }),
  221. _true: createKeyword("true", {
  222. startsExpr
  223. }),
  224. _false: createKeyword("false", {
  225. startsExpr
  226. }),
  227. _in: createKeyword("in", {
  228. beforeExpr,
  229. binop: 7
  230. }),
  231. _instanceof: createKeyword("instanceof", {
  232. beforeExpr,
  233. binop: 7
  234. }),
  235. _typeof: createKeyword("typeof", {
  236. beforeExpr,
  237. prefix,
  238. startsExpr
  239. }),
  240. _void: createKeyword("void", {
  241. beforeExpr,
  242. prefix,
  243. startsExpr
  244. }),
  245. _delete: createKeyword("delete", {
  246. beforeExpr,
  247. prefix,
  248. startsExpr
  249. })
  250. };
  251. const SCOPE_OTHER = 0b000000000,
  252. SCOPE_PROGRAM = 0b000000001,
  253. SCOPE_FUNCTION = 0b000000010,
  254. SCOPE_ASYNC = 0b000000100,
  255. SCOPE_GENERATOR = 0b000001000,
  256. SCOPE_ARROW = 0b000010000,
  257. SCOPE_SIMPLE_CATCH = 0b000100000,
  258. SCOPE_SUPER = 0b001000000,
  259. SCOPE_DIRECT_SUPER = 0b010000000,
  260. SCOPE_CLASS = 0b100000000,
  261. SCOPE_VAR = SCOPE_PROGRAM | SCOPE_FUNCTION;
  262. function functionFlags(isAsync, isGenerator) {
  263. return SCOPE_FUNCTION | (isAsync ? SCOPE_ASYNC : 0) | (isGenerator ? SCOPE_GENERATOR : 0);
  264. }
  265. const BIND_KIND_VALUE = 0b00000000001,
  266. BIND_KIND_TYPE = 0b00000000010,
  267. BIND_SCOPE_VAR = 0b00000000100,
  268. BIND_SCOPE_LEXICAL = 0b00000001000,
  269. BIND_SCOPE_FUNCTION = 0b00000010000,
  270. BIND_FLAGS_NONE = 0b00001000000,
  271. BIND_FLAGS_CLASS = 0b00010000000,
  272. BIND_FLAGS_TS_ENUM = 0b00100000000,
  273. BIND_FLAGS_TS_CONST_ENUM = 0b01000000000,
  274. BIND_FLAGS_TS_EXPORT_ONLY = 0b10000000000;
  275. const BIND_CLASS = BIND_KIND_VALUE | BIND_KIND_TYPE | BIND_SCOPE_LEXICAL | BIND_FLAGS_CLASS,
  276. BIND_LEXICAL = BIND_KIND_VALUE | 0 | BIND_SCOPE_LEXICAL | 0,
  277. BIND_VAR = BIND_KIND_VALUE | 0 | BIND_SCOPE_VAR | 0,
  278. BIND_FUNCTION = BIND_KIND_VALUE | 0 | BIND_SCOPE_FUNCTION | 0,
  279. BIND_TS_INTERFACE = 0 | BIND_KIND_TYPE | 0 | BIND_FLAGS_CLASS,
  280. BIND_TS_TYPE = 0 | BIND_KIND_TYPE | 0 | 0,
  281. BIND_TS_ENUM = BIND_KIND_VALUE | BIND_KIND_TYPE | BIND_SCOPE_LEXICAL | BIND_FLAGS_TS_ENUM,
  282. BIND_TS_FN_TYPE = 0 | 0 | 0 | BIND_FLAGS_TS_EXPORT_ONLY,
  283. BIND_NONE = 0 | 0 | 0 | BIND_FLAGS_NONE,
  284. BIND_OUTSIDE = BIND_KIND_VALUE | 0 | 0 | BIND_FLAGS_NONE,
  285. BIND_TS_CONST_ENUM = BIND_TS_ENUM | BIND_FLAGS_TS_CONST_ENUM,
  286. BIND_TS_NAMESPACE = BIND_TS_FN_TYPE;
  287. function isSimpleProperty(node) {
  288. return node != null && node.type === "Property" && node.kind === "init" && node.method === false;
  289. }
  290. var estree = (superClass => class extends superClass {
  291. estreeParseRegExpLiteral({
  292. pattern,
  293. flags
  294. }) {
  295. let regex = null;
  296. try {
  297. regex = new RegExp(pattern, flags);
  298. } catch (e) {}
  299. const node = this.estreeParseLiteral(regex);
  300. node.regex = {
  301. pattern,
  302. flags
  303. };
  304. return node;
  305. }
  306. estreeParseLiteral(value) {
  307. return this.parseLiteral(value, "Literal");
  308. }
  309. directiveToStmt(directive) {
  310. const directiveLiteral = directive.value;
  311. const stmt = this.startNodeAt(directive.start, directive.loc.start);
  312. const expression = this.startNodeAt(directiveLiteral.start, directiveLiteral.loc.start);
  313. expression.value = directiveLiteral.value;
  314. expression.raw = directiveLiteral.extra.raw;
  315. stmt.expression = this.finishNodeAt(expression, "Literal", directiveLiteral.end, directiveLiteral.loc.end);
  316. stmt.directive = directiveLiteral.extra.raw.slice(1, -1);
  317. return this.finishNodeAt(stmt, "ExpressionStatement", directive.end, directive.loc.end);
  318. }
  319. initFunction(node, isAsync) {
  320. super.initFunction(node, isAsync);
  321. node.expression = false;
  322. }
  323. checkDeclaration(node) {
  324. if (isSimpleProperty(node)) {
  325. this.checkDeclaration(node.value);
  326. } else {
  327. super.checkDeclaration(node);
  328. }
  329. }
  330. checkGetterSetterParams(method) {
  331. const prop = method;
  332. const paramCount = prop.kind === "get" ? 0 : 1;
  333. const start = prop.start;
  334. if (prop.value.params.length !== paramCount) {
  335. if (prop.kind === "get") {
  336. this.raise(start, "getter must not have any formal parameters");
  337. } else {
  338. this.raise(start, "setter must have exactly one formal parameter");
  339. }
  340. }
  341. if (prop.kind === "set" && prop.value.params[0].type === "RestElement") {
  342. this.raise(start, "setter function argument must not be a rest parameter");
  343. }
  344. }
  345. checkLVal(expr, bindingType = BIND_NONE, checkClashes, contextDescription) {
  346. switch (expr.type) {
  347. case "ObjectPattern":
  348. expr.properties.forEach(prop => {
  349. this.checkLVal(prop.type === "Property" ? prop.value : prop, bindingType, checkClashes, "object destructuring pattern");
  350. });
  351. break;
  352. default:
  353. super.checkLVal(expr, bindingType, checkClashes, contextDescription);
  354. }
  355. }
  356. checkPropClash(prop, propHash) {
  357. if (prop.type === "SpreadElement" || prop.computed || prop.method || prop.shorthand) {
  358. return;
  359. }
  360. const key = prop.key;
  361. const name = key.type === "Identifier" ? key.name : String(key.value);
  362. if (name === "__proto__" && prop.kind === "init") {
  363. if (propHash.proto) {
  364. this.raise(key.start, "Redefinition of __proto__ property");
  365. }
  366. propHash.proto = true;
  367. }
  368. }
  369. isStrictBody(node) {
  370. const isBlockStatement = node.body.type === "BlockStatement";
  371. if (isBlockStatement && node.body.body.length > 0) {
  372. for (let _i = 0, _node$body$body = node.body.body; _i < _node$body$body.length; _i++) {
  373. const directive = _node$body$body[_i];
  374. if (directive.type === "ExpressionStatement" && directive.expression.type === "Literal") {
  375. if (directive.expression.value === "use strict") return true;
  376. } else {
  377. break;
  378. }
  379. }
  380. }
  381. return false;
  382. }
  383. isValidDirective(stmt) {
  384. return stmt.type === "ExpressionStatement" && stmt.expression.type === "Literal" && typeof stmt.expression.value === "string" && (!stmt.expression.extra || !stmt.expression.extra.parenthesized);
  385. }
  386. stmtToDirective(stmt) {
  387. const directive = super.stmtToDirective(stmt);
  388. const value = stmt.expression.value;
  389. directive.value.value = value;
  390. return directive;
  391. }
  392. parseBlockBody(node, allowDirectives, topLevel, end) {
  393. super.parseBlockBody(node, allowDirectives, topLevel, end);
  394. const directiveStatements = node.directives.map(d => this.directiveToStmt(d));
  395. node.body = directiveStatements.concat(node.body);
  396. delete node.directives;
  397. }
  398. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  399. this.parseMethod(method, isGenerator, isAsync, isConstructor, allowsDirectSuper, "ClassMethod", true);
  400. if (method.typeParameters) {
  401. method.value.typeParameters = method.typeParameters;
  402. delete method.typeParameters;
  403. }
  404. classBody.body.push(method);
  405. }
  406. parseExprAtom(refShorthandDefaultPos) {
  407. switch (this.state.type) {
  408. case types.regexp:
  409. return this.estreeParseRegExpLiteral(this.state.value);
  410. case types.num:
  411. case types.string:
  412. return this.estreeParseLiteral(this.state.value);
  413. case types._null:
  414. return this.estreeParseLiteral(null);
  415. case types._true:
  416. return this.estreeParseLiteral(true);
  417. case types._false:
  418. return this.estreeParseLiteral(false);
  419. default:
  420. return super.parseExprAtom(refShorthandDefaultPos);
  421. }
  422. }
  423. parseLiteral(value, type, startPos, startLoc) {
  424. const node = super.parseLiteral(value, type, startPos, startLoc);
  425. node.raw = node.extra.raw;
  426. delete node.extra;
  427. return node;
  428. }
  429. parseFunctionBody(node, allowExpression, isMethod = false) {
  430. super.parseFunctionBody(node, allowExpression, isMethod);
  431. node.expression = node.body.type !== "BlockStatement";
  432. }
  433. parseMethod(node, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope = false) {
  434. let funcNode = this.startNode();
  435. funcNode.kind = node.kind;
  436. funcNode = super.parseMethod(funcNode, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope);
  437. funcNode.type = "FunctionExpression";
  438. delete funcNode.kind;
  439. node.value = funcNode;
  440. type = type === "ClassMethod" ? "MethodDefinition" : type;
  441. return this.finishNode(node, type);
  442. }
  443. parseObjectMethod(prop, isGenerator, isAsync, isPattern, containsEsc) {
  444. const node = super.parseObjectMethod(prop, isGenerator, isAsync, isPattern, containsEsc);
  445. if (node) {
  446. node.type = "Property";
  447. if (node.kind === "method") node.kind = "init";
  448. node.shorthand = false;
  449. }
  450. return node;
  451. }
  452. parseObjectProperty(prop, startPos, startLoc, isPattern, refShorthandDefaultPos) {
  453. const node = super.parseObjectProperty(prop, startPos, startLoc, isPattern, refShorthandDefaultPos);
  454. if (node) {
  455. node.kind = "init";
  456. node.type = "Property";
  457. }
  458. return node;
  459. }
  460. toAssignable(node, isBinding, contextDescription) {
  461. if (isSimpleProperty(node)) {
  462. this.toAssignable(node.value, isBinding, contextDescription);
  463. return node;
  464. }
  465. return super.toAssignable(node, isBinding, contextDescription);
  466. }
  467. toAssignableObjectExpressionProp(prop, isBinding, isLast) {
  468. if (prop.kind === "get" || prop.kind === "set") {
  469. this.raise(prop.key.start, "Object pattern can't contain getter or setter");
  470. } else if (prop.method) {
  471. this.raise(prop.key.start, "Object pattern can't contain methods");
  472. } else {
  473. super.toAssignableObjectExpressionProp(prop, isBinding, isLast);
  474. }
  475. }
  476. });
  477. const lineBreak = /\r\n?|[\n\u2028\u2029]/;
  478. const lineBreakG = new RegExp(lineBreak.source, "g");
  479. function isNewLine(code) {
  480. switch (code) {
  481. case 10:
  482. case 13:
  483. case 8232:
  484. case 8233:
  485. return true;
  486. default:
  487. return false;
  488. }
  489. }
  490. const skipWhiteSpace = /(?:\s|\/\/.*|\/\*[^]*?\*\/)*/g;
  491. function isWhitespace(code) {
  492. switch (code) {
  493. case 0x0009:
  494. case 0x000b:
  495. case 0x000c:
  496. case 32:
  497. case 160:
  498. case 5760:
  499. case 0x2000:
  500. case 0x2001:
  501. case 0x2002:
  502. case 0x2003:
  503. case 0x2004:
  504. case 0x2005:
  505. case 0x2006:
  506. case 0x2007:
  507. case 0x2008:
  508. case 0x2009:
  509. case 0x200a:
  510. case 0x202f:
  511. case 0x205f:
  512. case 0x3000:
  513. case 0xfeff:
  514. return true;
  515. default:
  516. return false;
  517. }
  518. }
  519. class TokContext {
  520. constructor(token, isExpr, preserveSpace, override) {
  521. this.token = token;
  522. this.isExpr = !!isExpr;
  523. this.preserveSpace = !!preserveSpace;
  524. this.override = override;
  525. }
  526. }
  527. const types$1 = {
  528. braceStatement: new TokContext("{", false),
  529. braceExpression: new TokContext("{", true),
  530. templateQuasi: new TokContext("${", false),
  531. parenStatement: new TokContext("(", false),
  532. parenExpression: new TokContext("(", true),
  533. template: new TokContext("`", true, true, p => p.readTmplToken()),
  534. functionExpression: new TokContext("function", true),
  535. functionStatement: new TokContext("function", false)
  536. };
  537. types.parenR.updateContext = types.braceR.updateContext = function () {
  538. if (this.state.context.length === 1) {
  539. this.state.exprAllowed = true;
  540. return;
  541. }
  542. let out = this.state.context.pop();
  543. if (out === types$1.braceStatement && this.curContext().token === "function") {
  544. out = this.state.context.pop();
  545. }
  546. this.state.exprAllowed = !out.isExpr;
  547. };
  548. types.name.updateContext = function (prevType) {
  549. let allowed = false;
  550. if (prevType !== types.dot) {
  551. if (this.state.value === "of" && !this.state.exprAllowed || this.state.value === "yield" && this.scope.inGenerator) {
  552. allowed = true;
  553. }
  554. }
  555. this.state.exprAllowed = allowed;
  556. if (this.state.isIterator) {
  557. this.state.isIterator = false;
  558. }
  559. };
  560. types.braceL.updateContext = function (prevType) {
  561. this.state.context.push(this.braceIsBlock(prevType) ? types$1.braceStatement : types$1.braceExpression);
  562. this.state.exprAllowed = true;
  563. };
  564. types.dollarBraceL.updateContext = function () {
  565. this.state.context.push(types$1.templateQuasi);
  566. this.state.exprAllowed = true;
  567. };
  568. types.parenL.updateContext = function (prevType) {
  569. const statementParens = prevType === types._if || prevType === types._for || prevType === types._with || prevType === types._while;
  570. this.state.context.push(statementParens ? types$1.parenStatement : types$1.parenExpression);
  571. this.state.exprAllowed = true;
  572. };
  573. types.incDec.updateContext = function () {};
  574. types._function.updateContext = types._class.updateContext = function (prevType) {
  575. if (prevType.beforeExpr && prevType !== types.semi && prevType !== types._else && !(prevType === types._return && lineBreak.test(this.input.slice(this.state.lastTokEnd, this.state.start))) && !((prevType === types.colon || prevType === types.braceL) && this.curContext() === types$1.b_stat)) {
  576. this.state.context.push(types$1.functionExpression);
  577. } else {
  578. this.state.context.push(types$1.functionStatement);
  579. }
  580. this.state.exprAllowed = false;
  581. };
  582. types.backQuote.updateContext = function () {
  583. if (this.curContext() === types$1.template) {
  584. this.state.context.pop();
  585. } else {
  586. this.state.context.push(types$1.template);
  587. }
  588. this.state.exprAllowed = false;
  589. };
  590. const reservedWords = {
  591. strict: ["implements", "interface", "let", "package", "private", "protected", "public", "static", "yield"],
  592. strictBind: ["eval", "arguments"]
  593. };
  594. const reservedWordsStrictSet = new Set(reservedWords.strict);
  595. const reservedWordsStrictBindSet = new Set(reservedWords.strict.concat(reservedWords.strictBind));
  596. const isReservedWord = (word, inModule) => {
  597. return inModule && word === "await" || word === "enum";
  598. };
  599. function isStrictReservedWord(word, inModule) {
  600. return isReservedWord(word, inModule) || reservedWordsStrictSet.has(word);
  601. }
  602. function isStrictBindReservedWord(word, inModule) {
  603. return isReservedWord(word, inModule) || reservedWordsStrictBindSet.has(word);
  604. }
  605. function isKeyword(word) {
  606. return keywords.has(word);
  607. }
  608. const keywordRelationalOperator = /^in(stanceof)?$/;
  609. let nonASCIIidentifierStartChars = "\xaa\xb5\xba\xc0-\xd6\xd8-\xf6\xf8-\u02c1\u02c6-\u02d1\u02e0-\u02e4\u02ec\u02ee\u0370-\u0374\u0376\u0377\u037a-\u037d\u037f\u0386\u0388-\u038a\u038c\u038e-\u03a1\u03a3-\u03f5\u03f7-\u0481\u048a-\u052f\u0531-\u0556\u0559\u0560-\u0588\u05d0-\u05ea\u05ef-\u05f2\u0620-\u064a\u066e\u066f\u0671-\u06d3\u06d5\u06e5\u06e6\u06ee\u06ef\u06fa-\u06fc\u06ff\u0710\u0712-\u072f\u074d-\u07a5\u07b1\u07ca-\u07ea\u07f4\u07f5\u07fa\u0800-\u0815\u081a\u0824\u0828\u0840-\u0858\u0860-\u086a\u08a0-\u08b4\u08b6-\u08bd\u0904-\u0939\u093d\u0950\u0958-\u0961\u0971-\u0980\u0985-\u098c\u098f\u0990\u0993-\u09a8\u09aa-\u09b0\u09b2\u09b6-\u09b9\u09bd\u09ce\u09dc\u09dd\u09df-\u09e1\u09f0\u09f1\u09fc\u0a05-\u0a0a\u0a0f\u0a10\u0a13-\u0a28\u0a2a-\u0a30\u0a32\u0a33\u0a35\u0a36\u0a38\u0a39\u0a59-\u0a5c\u0a5e\u0a72-\u0a74\u0a85-\u0a8d\u0a8f-\u0a91\u0a93-\u0aa8\u0aaa-\u0ab0\u0ab2\u0ab3\u0ab5-\u0ab9\u0abd\u0ad0\u0ae0\u0ae1\u0af9\u0b05-\u0b0c\u0b0f\u0b10\u0b13-\u0b28\u0b2a-\u0b30\u0b32\u0b33\u0b35-\u0b39\u0b3d\u0b5c\u0b5d\u0b5f-\u0b61\u0b71\u0b83\u0b85-\u0b8a\u0b8e-\u0b90\u0b92-\u0b95\u0b99\u0b9a\u0b9c\u0b9e\u0b9f\u0ba3\u0ba4\u0ba8-\u0baa\u0bae-\u0bb9\u0bd0\u0c05-\u0c0c\u0c0e-\u0c10\u0c12-\u0c28\u0c2a-\u0c39\u0c3d\u0c58-\u0c5a\u0c60\u0c61\u0c80\u0c85-\u0c8c\u0c8e-\u0c90\u0c92-\u0ca8\u0caa-\u0cb3\u0cb5-\u0cb9\u0cbd\u0cde\u0ce0\u0ce1\u0cf1\u0cf2\u0d05-\u0d0c\u0d0e-\u0d10\u0d12-\u0d3a\u0d3d\u0d4e\u0d54-\u0d56\u0d5f-\u0d61\u0d7a-\u0d7f\u0d85-\u0d96\u0d9a-\u0db1\u0db3-\u0dbb\u0dbd\u0dc0-\u0dc6\u0e01-\u0e30\u0e32\u0e33\u0e40-\u0e46\u0e81\u0e82\u0e84\u0e86-\u0e8a\u0e8c-\u0ea3\u0ea5\u0ea7-\u0eb0\u0eb2\u0eb3\u0ebd\u0ec0-\u0ec4\u0ec6\u0edc-\u0edf\u0f00\u0f40-\u0f47\u0f49-\u0f6c\u0f88-\u0f8c\u1000-\u102a\u103f\u1050-\u1055\u105a-\u105d\u1061\u1065\u1066\u106e-\u1070\u1075-\u1081\u108e\u10a0-\u10c5\u10c7\u10cd\u10d0-\u10fa\u10fc-\u1248\u124a-\u124d\u1250-\u1256\u1258\u125a-\u125d\u1260-\u1288\u128a-\u128d\u1290-\u12b0\u12b2-\u12b5\u12b8-\u12be\u12c0\u12c2-\u12c5\u12c8-\u12d6\u12d8-\u1310\u1312-\u1315\u1318-\u135a\u1380-\u138f\u13a0-\u13f5\u13f8-\u13fd\u1401-\u166c\u166f-\u167f\u1681-\u169a\u16a0-\u16ea\u16ee-\u16f8\u1700-\u170c\u170e-\u1711\u1720-\u1731\u1740-\u1751\u1760-\u176c\u176e-\u1770\u1780-\u17b3\u17d7\u17dc\u1820-\u1878\u1880-\u18a8\u18aa\u18b0-\u18f5\u1900-\u191e\u1950-\u196d\u1970-\u1974\u1980-\u19ab\u19b0-\u19c9\u1a00-\u1a16\u1a20-\u1a54\u1aa7\u1b05-\u1b33\u1b45-\u1b4b\u1b83-\u1ba0\u1bae\u1baf\u1bba-\u1be5\u1c00-\u1c23\u1c4d-\u1c4f\u1c5a-\u1c7d\u1c80-\u1c88\u1c90-\u1cba\u1cbd-\u1cbf\u1ce9-\u1cec\u1cee-\u1cf3\u1cf5\u1cf6\u1cfa\u1d00-\u1dbf\u1e00-\u1f15\u1f18-\u1f1d\u1f20-\u1f45\u1f48-\u1f4d\u1f50-\u1f57\u1f59\u1f5b\u1f5d\u1f5f-\u1f7d\u1f80-\u1fb4\u1fb6-\u1fbc\u1fbe\u1fc2-\u1fc4\u1fc6-\u1fcc\u1fd0-\u1fd3\u1fd6-\u1fdb\u1fe0-\u1fec\u1ff2-\u1ff4\u1ff6-\u1ffc\u2071\u207f\u2090-\u209c\u2102\u2107\u210a-\u2113\u2115\u2118-\u211d\u2124\u2126\u2128\u212a-\u2139\u213c-\u213f\u2145-\u2149\u214e\u2160-\u2188\u2c00-\u2c2e\u2c30-\u2c5e\u2c60-\u2ce4\u2ceb-\u2cee\u2cf2\u2cf3\u2d00-\u2d25\u2d27\u2d2d\u2d30-\u2d67\u2d6f\u2d80-\u2d96\u2da0-\u2da6\u2da8-\u2dae\u2db0-\u2db6\u2db8-\u2dbe\u2dc0-\u2dc6\u2dc8-\u2dce\u2dd0-\u2dd6\u2dd8-\u2dde\u3005-\u3007\u3021-\u3029\u3031-\u3035\u3038-\u303c\u3041-\u3096\u309b-\u309f\u30a1-\u30fa\u30fc-\u30ff\u3105-\u312f\u3131-\u318e\u31a0-\u31ba\u31f0-\u31ff\u3400-\u4db5\u4e00-\u9fef\ua000-\ua48c\ua4d0-\ua4fd\ua500-\ua60c\ua610-\ua61f\ua62a\ua62b\ua640-\ua66e\ua67f-\ua69d\ua6a0-\ua6ef\ua717-\ua71f\ua722-\ua788\ua78b-\ua7bf\ua7c2-\ua7c6\ua7f7-\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-\uab67\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";
  610. let nonASCIIidentifierChars = "\u200c\u200d\xb7\u0300-\u036f\u0387\u0483-\u0487\u0591-\u05bd\u05bf\u05c1\u05c2\u05c4\u05c5\u05c7\u0610-\u061a\u064b-\u0669\u0670\u06d6-\u06dc\u06df-\u06e4\u06e7\u06e8\u06ea-\u06ed\u06f0-\u06f9\u0711\u0730-\u074a\u07a6-\u07b0\u07c0-\u07c9\u07eb-\u07f3\u07fd\u0816-\u0819\u081b-\u0823\u0825-\u0827\u0829-\u082d\u0859-\u085b\u08d3-\u08e1\u08e3-\u0903\u093a-\u093c\u093e-\u094f\u0951-\u0957\u0962\u0963\u0966-\u096f\u0981-\u0983\u09bc\u09be-\u09c4\u09c7\u09c8\u09cb-\u09cd\u09d7\u09e2\u09e3\u09e6-\u09ef\u09fe\u0a01-\u0a03\u0a3c\u0a3e-\u0a42\u0a47\u0a48\u0a4b-\u0a4d\u0a51\u0a66-\u0a71\u0a75\u0a81-\u0a83\u0abc\u0abe-\u0ac5\u0ac7-\u0ac9\u0acb-\u0acd\u0ae2\u0ae3\u0ae6-\u0aef\u0afa-\u0aff\u0b01-\u0b03\u0b3c\u0b3e-\u0b44\u0b47\u0b48\u0b4b-\u0b4d\u0b56\u0b57\u0b62\u0b63\u0b66-\u0b6f\u0b82\u0bbe-\u0bc2\u0bc6-\u0bc8\u0bca-\u0bcd\u0bd7\u0be6-\u0bef\u0c00-\u0c04\u0c3e-\u0c44\u0c46-\u0c48\u0c4a-\u0c4d\u0c55\u0c56\u0c62\u0c63\u0c66-\u0c6f\u0c81-\u0c83\u0cbc\u0cbe-\u0cc4\u0cc6-\u0cc8\u0cca-\u0ccd\u0cd5\u0cd6\u0ce2\u0ce3\u0ce6-\u0cef\u0d00-\u0d03\u0d3b\u0d3c\u0d3e-\u0d44\u0d46-\u0d48\u0d4a-\u0d4d\u0d57\u0d62\u0d63\u0d66-\u0d6f\u0d82\u0d83\u0dca\u0dcf-\u0dd4\u0dd6\u0dd8-\u0ddf\u0de6-\u0def\u0df2\u0df3\u0e31\u0e34-\u0e3a\u0e47-\u0e4e\u0e50-\u0e59\u0eb1\u0eb4-\u0ebc\u0ec8-\u0ecd\u0ed0-\u0ed9\u0f18\u0f19\u0f20-\u0f29\u0f35\u0f37\u0f39\u0f3e\u0f3f\u0f71-\u0f84\u0f86\u0f87\u0f8d-\u0f97\u0f99-\u0fbc\u0fc6\u102b-\u103e\u1040-\u1049\u1056-\u1059\u105e-\u1060\u1062-\u1064\u1067-\u106d\u1071-\u1074\u1082-\u108d\u108f-\u109d\u135d-\u135f\u1369-\u1371\u1712-\u1714\u1732-\u1734\u1752\u1753\u1772\u1773\u17b4-\u17d3\u17dd\u17e0-\u17e9\u180b-\u180d\u1810-\u1819\u18a9\u1920-\u192b\u1930-\u193b\u1946-\u194f\u19d0-\u19da\u1a17-\u1a1b\u1a55-\u1a5e\u1a60-\u1a7c\u1a7f-\u1a89\u1a90-\u1a99\u1ab0-\u1abd\u1b00-\u1b04\u1b34-\u1b44\u1b50-\u1b59\u1b6b-\u1b73\u1b80-\u1b82\u1ba1-\u1bad\u1bb0-\u1bb9\u1be6-\u1bf3\u1c24-\u1c37\u1c40-\u1c49\u1c50-\u1c59\u1cd0-\u1cd2\u1cd4-\u1ce8\u1ced\u1cf4\u1cf7-\u1cf9\u1dc0-\u1df9\u1dfb-\u1dff\u203f\u2040\u2054\u20d0-\u20dc\u20e1\u20e5-\u20f0\u2cef-\u2cf1\u2d7f\u2de0-\u2dff\u302a-\u302f\u3099\u309a\ua620-\ua629\ua66f\ua674-\ua67d\ua69e\ua69f\ua6f0\ua6f1\ua802\ua806\ua80b\ua823-\ua827\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";
  611. const nonASCIIidentifierStart = new RegExp("[" + nonASCIIidentifierStartChars + "]");
  612. const nonASCIIidentifier = new RegExp("[" + nonASCIIidentifierStartChars + nonASCIIidentifierChars + "]");
  613. nonASCIIidentifierStartChars = nonASCIIidentifierChars = null;
  614. const astralIdentifierStartCodes = [0, 11, 2, 25, 2, 18, 2, 1, 2, 14, 3, 13, 35, 122, 70, 52, 268, 28, 4, 48, 48, 31, 14, 29, 6, 37, 11, 29, 3, 35, 5, 7, 2, 4, 43, 157, 19, 35, 5, 35, 5, 39, 9, 51, 157, 310, 10, 21, 11, 7, 153, 5, 3, 0, 2, 43, 2, 1, 4, 0, 3, 22, 11, 22, 10, 30, 66, 18, 2, 1, 11, 21, 11, 25, 71, 55, 7, 1, 65, 0, 16, 3, 2, 2, 2, 28, 43, 28, 4, 28, 36, 7, 2, 27, 28, 53, 11, 21, 11, 18, 14, 17, 111, 72, 56, 50, 14, 50, 14, 35, 477, 28, 11, 0, 9, 21, 155, 22, 13, 52, 76, 44, 33, 24, 27, 35, 30, 0, 12, 34, 4, 0, 13, 47, 15, 3, 22, 0, 2, 0, 36, 17, 2, 24, 85, 6, 2, 0, 2, 3, 2, 14, 2, 9, 8, 46, 39, 7, 3, 1, 3, 21, 2, 6, 2, 1, 2, 4, 4, 0, 19, 0, 13, 4, 159, 52, 19, 3, 21, 0, 33, 47, 21, 1, 2, 0, 185, 46, 42, 3, 37, 47, 21, 0, 60, 42, 14, 0, 72, 26, 230, 43, 117, 63, 32, 0, 161, 7, 3, 38, 17, 0, 2, 0, 29, 0, 11, 39, 8, 0, 22, 0, 12, 45, 20, 0, 35, 56, 264, 8, 2, 36, 18, 0, 50, 29, 113, 6, 2, 1, 2, 37, 22, 0, 26, 5, 2, 1, 2, 31, 15, 0, 328, 18, 270, 921, 103, 110, 18, 195, 2749, 1070, 4050, 582, 8634, 568, 8, 30, 114, 29, 19, 47, 17, 3, 32, 20, 6, 18, 689, 63, 129, 74, 6, 0, 67, 12, 65, 1, 2, 0, 29, 6135, 9, 754, 9486, 286, 50, 2, 18, 3, 9, 395, 2309, 106, 6, 12, 4, 8, 8, 9, 5991, 84, 2, 70, 2, 1, 3, 0, 3, 1, 3, 3, 2, 11, 2, 0, 2, 6, 2, 64, 2, 3, 3, 7, 2, 6, 2, 27, 2, 3, 2, 4, 2, 0, 4, 6, 2, 339, 3, 24, 2, 24, 2, 30, 2, 24, 2, 30, 2, 24, 2, 30, 2, 24, 2, 30, 2, 24, 2, 7, 2357, 44, 11, 6, 17, 0, 370, 43, 1301, 196, 60, 67, 8, 0, 1205, 3, 2, 26, 2, 1, 2, 0, 3, 0, 2, 9, 2, 3, 2, 0, 2, 0, 7, 0, 5, 0, 2, 0, 2, 0, 2, 2, 2, 1, 2, 0, 3, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 1, 2, 0, 3, 3, 2, 6, 2, 3, 2, 3, 2, 0, 2, 9, 2, 16, 6, 2, 2, 4, 2, 16, 4421, 42710, 42, 4148, 12, 221, 3, 5761, 15, 7472, 3104, 541];
  615. 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, 525, 10, 176, 2, 54, 14, 32, 9, 16, 3, 46, 10, 54, 9, 7, 2, 37, 13, 2, 9, 6, 1, 45, 0, 13, 2, 49, 13, 9, 3, 4, 9, 83, 11, 7, 0, 161, 11, 6, 9, 7, 3, 56, 1, 2, 6, 3, 1, 3, 2, 10, 0, 11, 1, 3, 6, 4, 4, 193, 17, 10, 9, 5, 0, 82, 19, 13, 9, 214, 6, 3, 8, 28, 1, 83, 16, 16, 9, 82, 12, 9, 9, 84, 14, 5, 9, 243, 14, 166, 9, 232, 6, 3, 6, 4, 0, 29, 9, 41, 6, 2, 3, 9, 0, 10, 10, 47, 15, 406, 7, 2, 7, 17, 9, 57, 21, 2, 13, 123, 5, 4, 0, 2, 1, 2, 6, 2, 0, 9, 9, 49, 4, 2, 1, 2, 4, 9, 9, 330, 3, 19306, 9, 135, 4, 60, 6, 26, 9, 1014, 0, 2, 54, 8, 3, 19723, 1, 5319, 4, 4, 5, 9, 7, 3, 6, 31, 3, 149, 2, 1418, 49, 513, 54, 5, 49, 9, 0, 15, 0, 23, 4, 2, 14, 1361, 6, 2, 16, 3, 6, 2, 1, 2, 4, 262, 6, 10, 9, 419, 13, 1495, 6, 110, 6, 6, 9, 792487, 239];
  616. function isInAstralSet(code, set) {
  617. let pos = 0x10000;
  618. for (let i = 0, length = set.length; i < length; i += 2) {
  619. pos += set[i];
  620. if (pos > code) return false;
  621. pos += set[i + 1];
  622. if (pos >= code) return true;
  623. }
  624. return false;
  625. }
  626. function isIdentifierStart(code) {
  627. if (code < 65) return code === 36;
  628. if (code <= 90) return true;
  629. if (code < 97) return code === 95;
  630. if (code <= 122) return true;
  631. if (code <= 0xffff) {
  632. return code >= 0xaa && nonASCIIidentifierStart.test(String.fromCharCode(code));
  633. }
  634. return isInAstralSet(code, astralIdentifierStartCodes);
  635. }
  636. function isIteratorStart(current, next) {
  637. return current === 64 && next === 64;
  638. }
  639. function isIdentifierChar(code) {
  640. if (code < 48) return code === 36;
  641. if (code < 58) return true;
  642. if (code < 65) return false;
  643. if (code <= 90) return true;
  644. if (code < 97) return code === 95;
  645. if (code <= 122) return true;
  646. if (code <= 0xffff) {
  647. return code >= 0xaa && nonASCIIidentifier.test(String.fromCharCode(code));
  648. }
  649. return isInAstralSet(code, astralIdentifierStartCodes) || isInAstralSet(code, astralIdentifierCodes);
  650. }
  651. const reservedTypes = ["any", "bool", "boolean", "empty", "false", "mixed", "null", "number", "static", "string", "true", "typeof", "void", "interface", "extends", "_"];
  652. function isEsModuleType(bodyElement) {
  653. return bodyElement.type === "DeclareExportAllDeclaration" || bodyElement.type === "DeclareExportDeclaration" && (!bodyElement.declaration || bodyElement.declaration.type !== "TypeAlias" && bodyElement.declaration.type !== "InterfaceDeclaration");
  654. }
  655. function hasTypeImportKind(node) {
  656. return node.importKind === "type" || node.importKind === "typeof";
  657. }
  658. function isMaybeDefaultImport(state) {
  659. return (state.type === types.name || !!state.type.keyword) && state.value !== "from";
  660. }
  661. const exportSuggestions = {
  662. const: "declare export var",
  663. let: "declare export var",
  664. type: "export type",
  665. interface: "export interface"
  666. };
  667. function partition(list, test) {
  668. const list1 = [];
  669. const list2 = [];
  670. for (let i = 0; i < list.length; i++) {
  671. (test(list[i], i, list) ? list1 : list2).push(list[i]);
  672. }
  673. return [list1, list2];
  674. }
  675. const FLOW_PRAGMA_REGEX = /\*?\s*@((?:no)?flow)\b/;
  676. var flow = (superClass => class extends superClass {
  677. constructor(options, input) {
  678. super(options, input);
  679. this.flowPragma = undefined;
  680. }
  681. shouldParseTypes() {
  682. return this.getPluginOption("flow", "all") || this.flowPragma === "flow";
  683. }
  684. finishToken(type, val) {
  685. if (type !== types.string && type !== types.semi && type !== types.interpreterDirective) {
  686. if (this.flowPragma === undefined) {
  687. this.flowPragma = null;
  688. }
  689. }
  690. return super.finishToken(type, val);
  691. }
  692. addComment(comment) {
  693. if (this.flowPragma === undefined) {
  694. const matches = FLOW_PRAGMA_REGEX.exec(comment.value);
  695. if (!matches) ; else if (matches[1] === "flow") {
  696. this.flowPragma = "flow";
  697. } else if (matches[1] === "noflow") {
  698. this.flowPragma = "noflow";
  699. } else {
  700. throw new Error("Unexpected flow pragma");
  701. }
  702. }
  703. return super.addComment(comment);
  704. }
  705. flowParseTypeInitialiser(tok) {
  706. const oldInType = this.state.inType;
  707. this.state.inType = true;
  708. this.expect(tok || types.colon);
  709. const type = this.flowParseType();
  710. this.state.inType = oldInType;
  711. return type;
  712. }
  713. flowParsePredicate() {
  714. const node = this.startNode();
  715. const moduloLoc = this.state.startLoc;
  716. const moduloPos = this.state.start;
  717. this.expect(types.modulo);
  718. const checksLoc = this.state.startLoc;
  719. this.expectContextual("checks");
  720. if (moduloLoc.line !== checksLoc.line || moduloLoc.column !== checksLoc.column - 1) {
  721. this.raise(moduloPos, "Spaces between ´%´ and ´checks´ are not allowed here.");
  722. }
  723. if (this.eat(types.parenL)) {
  724. node.value = this.parseExpression();
  725. this.expect(types.parenR);
  726. return this.finishNode(node, "DeclaredPredicate");
  727. } else {
  728. return this.finishNode(node, "InferredPredicate");
  729. }
  730. }
  731. flowParseTypeAndPredicateInitialiser() {
  732. const oldInType = this.state.inType;
  733. this.state.inType = true;
  734. this.expect(types.colon);
  735. let type = null;
  736. let predicate = null;
  737. if (this.match(types.modulo)) {
  738. this.state.inType = oldInType;
  739. predicate = this.flowParsePredicate();
  740. } else {
  741. type = this.flowParseType();
  742. this.state.inType = oldInType;
  743. if (this.match(types.modulo)) {
  744. predicate = this.flowParsePredicate();
  745. }
  746. }
  747. return [type, predicate];
  748. }
  749. flowParseDeclareClass(node) {
  750. this.next();
  751. this.flowParseInterfaceish(node, true);
  752. return this.finishNode(node, "DeclareClass");
  753. }
  754. flowParseDeclareFunction(node) {
  755. this.next();
  756. const id = node.id = this.parseIdentifier();
  757. const typeNode = this.startNode();
  758. const typeContainer = this.startNode();
  759. if (this.isRelational("<")) {
  760. typeNode.typeParameters = this.flowParseTypeParameterDeclaration();
  761. } else {
  762. typeNode.typeParameters = null;
  763. }
  764. this.expect(types.parenL);
  765. const tmp = this.flowParseFunctionTypeParams();
  766. typeNode.params = tmp.params;
  767. typeNode.rest = tmp.rest;
  768. this.expect(types.parenR);
  769. [typeNode.returnType, node.predicate] = this.flowParseTypeAndPredicateInitialiser();
  770. typeContainer.typeAnnotation = this.finishNode(typeNode, "FunctionTypeAnnotation");
  771. id.typeAnnotation = this.finishNode(typeContainer, "TypeAnnotation");
  772. this.resetEndLocation(id);
  773. this.semicolon();
  774. return this.finishNode(node, "DeclareFunction");
  775. }
  776. flowParseDeclare(node, insideModule) {
  777. if (this.match(types._class)) {
  778. return this.flowParseDeclareClass(node);
  779. } else if (this.match(types._function)) {
  780. return this.flowParseDeclareFunction(node);
  781. } else if (this.match(types._var)) {
  782. return this.flowParseDeclareVariable(node);
  783. } else if (this.eatContextual("module")) {
  784. if (this.match(types.dot)) {
  785. return this.flowParseDeclareModuleExports(node);
  786. } else {
  787. if (insideModule) {
  788. this.unexpected(this.state.lastTokStart, "`declare module` cannot be used inside another `declare module`");
  789. }
  790. return this.flowParseDeclareModule(node);
  791. }
  792. } else if (this.isContextual("type")) {
  793. return this.flowParseDeclareTypeAlias(node);
  794. } else if (this.isContextual("opaque")) {
  795. return this.flowParseDeclareOpaqueType(node);
  796. } else if (this.isContextual("interface")) {
  797. return this.flowParseDeclareInterface(node);
  798. } else if (this.match(types._export)) {
  799. return this.flowParseDeclareExportDeclaration(node, insideModule);
  800. } else {
  801. throw this.unexpected();
  802. }
  803. }
  804. flowParseDeclareVariable(node) {
  805. this.next();
  806. node.id = this.flowParseTypeAnnotatableIdentifier(true);
  807. this.semicolon();
  808. return this.finishNode(node, "DeclareVariable");
  809. }
  810. flowParseDeclareModule(node) {
  811. this.scope.enter(SCOPE_OTHER);
  812. if (this.match(types.string)) {
  813. node.id = this.parseExprAtom();
  814. } else {
  815. node.id = this.parseIdentifier();
  816. }
  817. const bodyNode = node.body = this.startNode();
  818. const body = bodyNode.body = [];
  819. this.expect(types.braceL);
  820. while (!this.match(types.braceR)) {
  821. let bodyNode = this.startNode();
  822. if (this.match(types._import)) {
  823. this.next();
  824. if (!this.isContextual("type") && !this.isContextual("typeof")) {
  825. this.unexpected(this.state.lastTokStart, "Imports within a `declare module` body must always be `import type` or `import typeof`");
  826. }
  827. this.parseImport(bodyNode);
  828. } else {
  829. this.expectContextual("declare", "Only declares and type imports are allowed inside declare module");
  830. bodyNode = this.flowParseDeclare(bodyNode, true);
  831. }
  832. body.push(bodyNode);
  833. }
  834. this.scope.exit();
  835. this.expect(types.braceR);
  836. this.finishNode(bodyNode, "BlockStatement");
  837. let kind = null;
  838. let hasModuleExport = false;
  839. const errorMessage = "Found both `declare module.exports` and `declare export` in the same module. " + "Modules can only have 1 since they are either an ES module or they are a CommonJS module";
  840. body.forEach(bodyElement => {
  841. if (isEsModuleType(bodyElement)) {
  842. if (kind === "CommonJS") {
  843. this.unexpected(bodyElement.start, errorMessage);
  844. }
  845. kind = "ES";
  846. } else if (bodyElement.type === "DeclareModuleExports") {
  847. if (hasModuleExport) {
  848. this.unexpected(bodyElement.start, "Duplicate `declare module.exports` statement");
  849. }
  850. if (kind === "ES") this.unexpected(bodyElement.start, errorMessage);
  851. kind = "CommonJS";
  852. hasModuleExport = true;
  853. }
  854. });
  855. node.kind = kind || "CommonJS";
  856. return this.finishNode(node, "DeclareModule");
  857. }
  858. flowParseDeclareExportDeclaration(node, insideModule) {
  859. this.expect(types._export);
  860. if (this.eat(types._default)) {
  861. if (this.match(types._function) || this.match(types._class)) {
  862. node.declaration = this.flowParseDeclare(this.startNode());
  863. } else {
  864. node.declaration = this.flowParseType();
  865. this.semicolon();
  866. }
  867. node.default = true;
  868. return this.finishNode(node, "DeclareExportDeclaration");
  869. } else {
  870. if (this.match(types._const) || this.isLet() || (this.isContextual("type") || this.isContextual("interface")) && !insideModule) {
  871. const label = this.state.value;
  872. const suggestion = exportSuggestions[label];
  873. this.unexpected(this.state.start, `\`declare export ${label}\` is not supported. Use \`${suggestion}\` instead`);
  874. }
  875. if (this.match(types._var) || this.match(types._function) || this.match(types._class) || this.isContextual("opaque")) {
  876. node.declaration = this.flowParseDeclare(this.startNode());
  877. node.default = false;
  878. return this.finishNode(node, "DeclareExportDeclaration");
  879. } else if (this.match(types.star) || this.match(types.braceL) || this.isContextual("interface") || this.isContextual("type") || this.isContextual("opaque")) {
  880. node = this.parseExport(node);
  881. if (node.type === "ExportNamedDeclaration") {
  882. node.type = "ExportDeclaration";
  883. node.default = false;
  884. delete node.exportKind;
  885. }
  886. node.type = "Declare" + node.type;
  887. return node;
  888. }
  889. }
  890. throw this.unexpected();
  891. }
  892. flowParseDeclareModuleExports(node) {
  893. this.next();
  894. this.expectContextual("exports");
  895. node.typeAnnotation = this.flowParseTypeAnnotation();
  896. this.semicolon();
  897. return this.finishNode(node, "DeclareModuleExports");
  898. }
  899. flowParseDeclareTypeAlias(node) {
  900. this.next();
  901. this.flowParseTypeAlias(node);
  902. node.type = "DeclareTypeAlias";
  903. return node;
  904. }
  905. flowParseDeclareOpaqueType(node) {
  906. this.next();
  907. this.flowParseOpaqueType(node, true);
  908. node.type = "DeclareOpaqueType";
  909. return node;
  910. }
  911. flowParseDeclareInterface(node) {
  912. this.next();
  913. this.flowParseInterfaceish(node);
  914. return this.finishNode(node, "DeclareInterface");
  915. }
  916. flowParseInterfaceish(node, isClass = false) {
  917. node.id = this.flowParseRestrictedIdentifier(!isClass);
  918. if (this.isRelational("<")) {
  919. node.typeParameters = this.flowParseTypeParameterDeclaration();
  920. } else {
  921. node.typeParameters = null;
  922. }
  923. node.extends = [];
  924. node.implements = [];
  925. node.mixins = [];
  926. if (this.eat(types._extends)) {
  927. do {
  928. node.extends.push(this.flowParseInterfaceExtends());
  929. } while (!isClass && this.eat(types.comma));
  930. }
  931. if (this.isContextual("mixins")) {
  932. this.next();
  933. do {
  934. node.mixins.push(this.flowParseInterfaceExtends());
  935. } while (this.eat(types.comma));
  936. }
  937. if (this.isContextual("implements")) {
  938. this.next();
  939. do {
  940. node.implements.push(this.flowParseInterfaceExtends());
  941. } while (this.eat(types.comma));
  942. }
  943. node.body = this.flowParseObjectType({
  944. allowStatic: isClass,
  945. allowExact: false,
  946. allowSpread: false,
  947. allowProto: isClass,
  948. allowInexact: false
  949. });
  950. }
  951. flowParseInterfaceExtends() {
  952. const node = this.startNode();
  953. node.id = this.flowParseQualifiedTypeIdentifier();
  954. if (this.isRelational("<")) {
  955. node.typeParameters = this.flowParseTypeParameterInstantiation();
  956. } else {
  957. node.typeParameters = null;
  958. }
  959. return this.finishNode(node, "InterfaceExtends");
  960. }
  961. flowParseInterface(node) {
  962. this.flowParseInterfaceish(node);
  963. return this.finishNode(node, "InterfaceDeclaration");
  964. }
  965. checkNotUnderscore(word) {
  966. if (word === "_") {
  967. throw this.unexpected(null, "`_` is only allowed as a type argument to call or new");
  968. }
  969. }
  970. checkReservedType(word, startLoc) {
  971. if (reservedTypes.indexOf(word) > -1) {
  972. this.raise(startLoc, `Cannot overwrite reserved type ${word}`);
  973. }
  974. }
  975. flowParseRestrictedIdentifier(liberal) {
  976. this.checkReservedType(this.state.value, this.state.start);
  977. return this.parseIdentifier(liberal);
  978. }
  979. flowParseTypeAlias(node) {
  980. node.id = this.flowParseRestrictedIdentifier();
  981. this.scope.declareName(node.id.name, BIND_LEXICAL, node.id.start);
  982. if (this.isRelational("<")) {
  983. node.typeParameters = this.flowParseTypeParameterDeclaration();
  984. } else {
  985. node.typeParameters = null;
  986. }
  987. node.right = this.flowParseTypeInitialiser(types.eq);
  988. this.semicolon();
  989. return this.finishNode(node, "TypeAlias");
  990. }
  991. flowParseOpaqueType(node, declare) {
  992. this.expectContextual("type");
  993. node.id = this.flowParseRestrictedIdentifier(true);
  994. this.scope.declareName(node.id.name, BIND_LEXICAL, node.id.start);
  995. if (this.isRelational("<")) {
  996. node.typeParameters = this.flowParseTypeParameterDeclaration();
  997. } else {
  998. node.typeParameters = null;
  999. }
  1000. node.supertype = null;
  1001. if (this.match(types.colon)) {
  1002. node.supertype = this.flowParseTypeInitialiser(types.colon);
  1003. }
  1004. node.impltype = null;
  1005. if (!declare) {
  1006. node.impltype = this.flowParseTypeInitialiser(types.eq);
  1007. }
  1008. this.semicolon();
  1009. return this.finishNode(node, "OpaqueType");
  1010. }
  1011. flowParseTypeParameter(allowDefault = true, requireDefault = false) {
  1012. if (!allowDefault && requireDefault) {
  1013. throw new Error("Cannot disallow a default value (`allowDefault`) while also requiring it (`requireDefault`).");
  1014. }
  1015. const nodeStart = this.state.start;
  1016. const node = this.startNode();
  1017. const variance = this.flowParseVariance();
  1018. const ident = this.flowParseTypeAnnotatableIdentifier();
  1019. node.name = ident.name;
  1020. node.variance = variance;
  1021. node.bound = ident.typeAnnotation;
  1022. if (this.match(types.eq)) {
  1023. if (allowDefault) {
  1024. this.eat(types.eq);
  1025. node.default = this.flowParseType();
  1026. } else {
  1027. this.unexpected();
  1028. }
  1029. } else {
  1030. if (requireDefault) {
  1031. this.unexpected(nodeStart, "Type parameter declaration needs a default, since a preceding type parameter declaration has a default.");
  1032. }
  1033. }
  1034. return this.finishNode(node, "TypeParameter");
  1035. }
  1036. flowParseTypeParameterDeclaration(allowDefault = true) {
  1037. const oldInType = this.state.inType;
  1038. const node = this.startNode();
  1039. node.params = [];
  1040. this.state.inType = true;
  1041. if (this.isRelational("<") || this.match(types.jsxTagStart)) {
  1042. this.next();
  1043. } else {
  1044. this.unexpected();
  1045. }
  1046. let defaultRequired = false;
  1047. do {
  1048. const typeParameter = this.flowParseTypeParameter(allowDefault, defaultRequired);
  1049. node.params.push(typeParameter);
  1050. if (typeParameter.default) {
  1051. defaultRequired = true;
  1052. }
  1053. if (!this.isRelational(">")) {
  1054. this.expect(types.comma);
  1055. }
  1056. } while (!this.isRelational(">"));
  1057. this.expectRelational(">");
  1058. this.state.inType = oldInType;
  1059. return this.finishNode(node, "TypeParameterDeclaration");
  1060. }
  1061. flowParseTypeParameterInstantiation() {
  1062. const node = this.startNode();
  1063. const oldInType = this.state.inType;
  1064. node.params = [];
  1065. this.state.inType = true;
  1066. this.expectRelational("<");
  1067. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  1068. this.state.noAnonFunctionType = false;
  1069. while (!this.isRelational(">")) {
  1070. node.params.push(this.flowParseType());
  1071. if (!this.isRelational(">")) {
  1072. this.expect(types.comma);
  1073. }
  1074. }
  1075. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  1076. this.expectRelational(">");
  1077. this.state.inType = oldInType;
  1078. return this.finishNode(node, "TypeParameterInstantiation");
  1079. }
  1080. flowParseTypeParameterInstantiationCallOrNew() {
  1081. const node = this.startNode();
  1082. const oldInType = this.state.inType;
  1083. node.params = [];
  1084. this.state.inType = true;
  1085. this.expectRelational("<");
  1086. while (!this.isRelational(">")) {
  1087. node.params.push(this.flowParseTypeOrImplicitInstantiation());
  1088. if (!this.isRelational(">")) {
  1089. this.expect(types.comma);
  1090. }
  1091. }
  1092. this.expectRelational(">");
  1093. this.state.inType = oldInType;
  1094. return this.finishNode(node, "TypeParameterInstantiation");
  1095. }
  1096. flowParseInterfaceType() {
  1097. const node = this.startNode();
  1098. this.expectContextual("interface");
  1099. node.extends = [];
  1100. if (this.eat(types._extends)) {
  1101. do {
  1102. node.extends.push(this.flowParseInterfaceExtends());
  1103. } while (this.eat(types.comma));
  1104. }
  1105. node.body = this.flowParseObjectType({
  1106. allowStatic: false,
  1107. allowExact: false,
  1108. allowSpread: false,
  1109. allowProto: false,
  1110. allowInexact: false
  1111. });
  1112. return this.finishNode(node, "InterfaceTypeAnnotation");
  1113. }
  1114. flowParseObjectPropertyKey() {
  1115. return this.match(types.num) || this.match(types.string) ? this.parseExprAtom() : this.parseIdentifier(true);
  1116. }
  1117. flowParseObjectTypeIndexer(node, isStatic, variance) {
  1118. node.static = isStatic;
  1119. if (this.lookahead().type === types.colon) {
  1120. node.id = this.flowParseObjectPropertyKey();
  1121. node.key = this.flowParseTypeInitialiser();
  1122. } else {
  1123. node.id = null;
  1124. node.key = this.flowParseType();
  1125. }
  1126. this.expect(types.bracketR);
  1127. node.value = this.flowParseTypeInitialiser();
  1128. node.variance = variance;
  1129. return this.finishNode(node, "ObjectTypeIndexer");
  1130. }
  1131. flowParseObjectTypeInternalSlot(node, isStatic) {
  1132. node.static = isStatic;
  1133. node.id = this.flowParseObjectPropertyKey();
  1134. this.expect(types.bracketR);
  1135. this.expect(types.bracketR);
  1136. if (this.isRelational("<") || this.match(types.parenL)) {
  1137. node.method = true;
  1138. node.optional = false;
  1139. node.value = this.flowParseObjectTypeMethodish(this.startNodeAt(node.start, node.loc.start));
  1140. } else {
  1141. node.method = false;
  1142. if (this.eat(types.question)) {
  1143. node.optional = true;
  1144. }
  1145. node.value = this.flowParseTypeInitialiser();
  1146. }
  1147. return this.finishNode(node, "ObjectTypeInternalSlot");
  1148. }
  1149. flowParseObjectTypeMethodish(node) {
  1150. node.params = [];
  1151. node.rest = null;
  1152. node.typeParameters = null;
  1153. if (this.isRelational("<")) {
  1154. node.typeParameters = this.flowParseTypeParameterDeclaration(false);
  1155. }
  1156. this.expect(types.parenL);
  1157. while (!this.match(types.parenR) && !this.match(types.ellipsis)) {
  1158. node.params.push(this.flowParseFunctionTypeParam());
  1159. if (!this.match(types.parenR)) {
  1160. this.expect(types.comma);
  1161. }
  1162. }
  1163. if (this.eat(types.ellipsis)) {
  1164. node.rest = this.flowParseFunctionTypeParam();
  1165. }
  1166. this.expect(types.parenR);
  1167. node.returnType = this.flowParseTypeInitialiser();
  1168. return this.finishNode(node, "FunctionTypeAnnotation");
  1169. }
  1170. flowParseObjectTypeCallProperty(node, isStatic) {
  1171. const valueNode = this.startNode();
  1172. node.static = isStatic;
  1173. node.value = this.flowParseObjectTypeMethodish(valueNode);
  1174. return this.finishNode(node, "ObjectTypeCallProperty");
  1175. }
  1176. flowParseObjectType({
  1177. allowStatic,
  1178. allowExact,
  1179. allowSpread,
  1180. allowProto,
  1181. allowInexact
  1182. }) {
  1183. const oldInType = this.state.inType;
  1184. this.state.inType = true;
  1185. const nodeStart = this.startNode();
  1186. nodeStart.callProperties = [];
  1187. nodeStart.properties = [];
  1188. nodeStart.indexers = [];
  1189. nodeStart.internalSlots = [];
  1190. let endDelim;
  1191. let exact;
  1192. let inexact = false;
  1193. if (allowExact && this.match(types.braceBarL)) {
  1194. this.expect(types.braceBarL);
  1195. endDelim = types.braceBarR;
  1196. exact = true;
  1197. } else {
  1198. this.expect(types.braceL);
  1199. endDelim = types.braceR;
  1200. exact = false;
  1201. }
  1202. nodeStart.exact = exact;
  1203. while (!this.match(endDelim)) {
  1204. let isStatic = false;
  1205. let protoStart = null;
  1206. const node = this.startNode();
  1207. if (allowProto && this.isContextual("proto")) {
  1208. const lookahead = this.lookahead();
  1209. if (lookahead.type !== types.colon && lookahead.type !== types.question) {
  1210. this.next();
  1211. protoStart = this.state.start;
  1212. allowStatic = false;
  1213. }
  1214. }
  1215. if (allowStatic && this.isContextual("static")) {
  1216. const lookahead = this.lookahead();
  1217. if (lookahead.type !== types.colon && lookahead.type !== types.question) {
  1218. this.next();
  1219. isStatic = true;
  1220. }
  1221. }
  1222. const variance = this.flowParseVariance();
  1223. if (this.eat(types.bracketL)) {
  1224. if (protoStart != null) {
  1225. this.unexpected(protoStart);
  1226. }
  1227. if (this.eat(types.bracketL)) {
  1228. if (variance) {
  1229. this.unexpected(variance.start);
  1230. }
  1231. nodeStart.internalSlots.push(this.flowParseObjectTypeInternalSlot(node, isStatic));
  1232. } else {
  1233. nodeStart.indexers.push(this.flowParseObjectTypeIndexer(node, isStatic, variance));
  1234. }
  1235. } else if (this.match(types.parenL) || this.isRelational("<")) {
  1236. if (protoStart != null) {
  1237. this.unexpected(protoStart);
  1238. }
  1239. if (variance) {
  1240. this.unexpected(variance.start);
  1241. }
  1242. nodeStart.callProperties.push(this.flowParseObjectTypeCallProperty(node, isStatic));
  1243. } else {
  1244. let kind = "init";
  1245. if (this.isContextual("get") || this.isContextual("set")) {
  1246. const lookahead = this.lookahead();
  1247. if (lookahead.type === types.name || lookahead.type === types.string || lookahead.type === types.num) {
  1248. kind = this.state.value;
  1249. this.next();
  1250. }
  1251. }
  1252. const propOrInexact = this.flowParseObjectTypeProperty(node, isStatic, protoStart, variance, kind, allowSpread, allowInexact);
  1253. if (propOrInexact === null) {
  1254. inexact = true;
  1255. } else {
  1256. nodeStart.properties.push(propOrInexact);
  1257. }
  1258. }
  1259. this.flowObjectTypeSemicolon();
  1260. }
  1261. this.expect(endDelim);
  1262. if (allowSpread) {
  1263. nodeStart.inexact = inexact;
  1264. }
  1265. const out = this.finishNode(nodeStart, "ObjectTypeAnnotation");
  1266. this.state.inType = oldInType;
  1267. return out;
  1268. }
  1269. flowParseObjectTypeProperty(node, isStatic, protoStart, variance, kind, allowSpread, allowInexact) {
  1270. if (this.match(types.ellipsis)) {
  1271. if (!allowSpread) {
  1272. this.unexpected(null, "Spread operator cannot appear in class or interface definitions");
  1273. }
  1274. if (protoStart != null) {
  1275. this.unexpected(protoStart);
  1276. }
  1277. if (variance) {
  1278. this.unexpected(variance.start, "Spread properties cannot have variance");
  1279. }
  1280. this.expect(types.ellipsis);
  1281. const isInexactToken = this.eat(types.comma) || this.eat(types.semi);
  1282. if (this.match(types.braceR)) {
  1283. if (allowInexact) return null;
  1284. this.unexpected(null, "Explicit inexact syntax is only allowed inside inexact objects");
  1285. }
  1286. if (this.match(types.braceBarR)) {
  1287. this.unexpected(null, "Explicit inexact syntax cannot appear inside an explicit exact object type");
  1288. }
  1289. if (isInexactToken) {
  1290. this.unexpected(null, "Explicit inexact syntax must appear at the end of an inexact object");
  1291. }
  1292. node.argument = this.flowParseType();
  1293. return this.finishNode(node, "ObjectTypeSpreadProperty");
  1294. } else {
  1295. node.key = this.flowParseObjectPropertyKey();
  1296. node.static = isStatic;
  1297. node.proto = protoStart != null;
  1298. node.kind = kind;
  1299. let optional = false;
  1300. if (this.isRelational("<") || this.match(types.parenL)) {
  1301. node.method = true;
  1302. if (protoStart != null) {
  1303. this.unexpected(protoStart);
  1304. }
  1305. if (variance) {
  1306. this.unexpected(variance.start);
  1307. }
  1308. node.value = this.flowParseObjectTypeMethodish(this.startNodeAt(node.start, node.loc.start));
  1309. if (kind === "get" || kind === "set") {
  1310. this.flowCheckGetterSetterParams(node);
  1311. }
  1312. } else {
  1313. if (kind !== "init") this.unexpected();
  1314. node.method = false;
  1315. if (this.eat(types.question)) {
  1316. optional = true;
  1317. }
  1318. node.value = this.flowParseTypeInitialiser();
  1319. node.variance = variance;
  1320. }
  1321. node.optional = optional;
  1322. return this.finishNode(node, "ObjectTypeProperty");
  1323. }
  1324. }
  1325. flowCheckGetterSetterParams(property) {
  1326. const paramCount = property.kind === "get" ? 0 : 1;
  1327. const start = property.start;
  1328. const length = property.value.params.length + (property.value.rest ? 1 : 0);
  1329. if (length !== paramCount) {
  1330. if (property.kind === "get") {
  1331. this.raise(start, "getter must not have any formal parameters");
  1332. } else {
  1333. this.raise(start, "setter must have exactly one formal parameter");
  1334. }
  1335. }
  1336. if (property.kind === "set" && property.value.rest) {
  1337. this.raise(start, "setter function argument must not be a rest parameter");
  1338. }
  1339. }
  1340. flowObjectTypeSemicolon() {
  1341. if (!this.eat(types.semi) && !this.eat(types.comma) && !this.match(types.braceR) && !this.match(types.braceBarR)) {
  1342. this.unexpected();
  1343. }
  1344. }
  1345. flowParseQualifiedTypeIdentifier(startPos, startLoc, id) {
  1346. startPos = startPos || this.state.start;
  1347. startLoc = startLoc || this.state.startLoc;
  1348. let node = id || this.parseIdentifier();
  1349. while (this.eat(types.dot)) {
  1350. const node2 = this.startNodeAt(startPos, startLoc);
  1351. node2.qualification = node;
  1352. node2.id = this.parseIdentifier();
  1353. node = this.finishNode(node2, "QualifiedTypeIdentifier");
  1354. }
  1355. return node;
  1356. }
  1357. flowParseGenericType(startPos, startLoc, id) {
  1358. const node = this.startNodeAt(startPos, startLoc);
  1359. node.typeParameters = null;
  1360. node.id = this.flowParseQualifiedTypeIdentifier(startPos, startLoc, id);
  1361. if (this.isRelational("<")) {
  1362. node.typeParameters = this.flowParseTypeParameterInstantiation();
  1363. }
  1364. return this.finishNode(node, "GenericTypeAnnotation");
  1365. }
  1366. flowParseTypeofType() {
  1367. const node = this.startNode();
  1368. this.expect(types._typeof);
  1369. node.argument = this.flowParsePrimaryType();
  1370. return this.finishNode(node, "TypeofTypeAnnotation");
  1371. }
  1372. flowParseTupleType() {
  1373. const node = this.startNode();
  1374. node.types = [];
  1375. this.expect(types.bracketL);
  1376. while (this.state.pos < this.length && !this.match(types.bracketR)) {
  1377. node.types.push(this.flowParseType());
  1378. if (this.match(types.bracketR)) break;
  1379. this.expect(types.comma);
  1380. }
  1381. this.expect(types.bracketR);
  1382. return this.finishNode(node, "TupleTypeAnnotation");
  1383. }
  1384. flowParseFunctionTypeParam() {
  1385. let name = null;
  1386. let optional = false;
  1387. let typeAnnotation = null;
  1388. const node = this.startNode();
  1389. const lh = this.lookahead();
  1390. if (lh.type === types.colon || lh.type === types.question) {
  1391. name = this.parseIdentifier();
  1392. if (this.eat(types.question)) {
  1393. optional = true;
  1394. }
  1395. typeAnnotation = this.flowParseTypeInitialiser();
  1396. } else {
  1397. typeAnnotation = this.flowParseType();
  1398. }
  1399. node.name = name;
  1400. node.optional = optional;
  1401. node.typeAnnotation = typeAnnotation;
  1402. return this.finishNode(node, "FunctionTypeParam");
  1403. }
  1404. reinterpretTypeAsFunctionTypeParam(type) {
  1405. const node = this.startNodeAt(type.start, type.loc.start);
  1406. node.name = null;
  1407. node.optional = false;
  1408. node.typeAnnotation = type;
  1409. return this.finishNode(node, "FunctionTypeParam");
  1410. }
  1411. flowParseFunctionTypeParams(params = []) {
  1412. let rest = null;
  1413. while (!this.match(types.parenR) && !this.match(types.ellipsis)) {
  1414. params.push(this.flowParseFunctionTypeParam());
  1415. if (!this.match(types.parenR)) {
  1416. this.expect(types.comma);
  1417. }
  1418. }
  1419. if (this.eat(types.ellipsis)) {
  1420. rest = this.flowParseFunctionTypeParam();
  1421. }
  1422. return {
  1423. params,
  1424. rest
  1425. };
  1426. }
  1427. flowIdentToTypeAnnotation(startPos, startLoc, node, id) {
  1428. switch (id.name) {
  1429. case "any":
  1430. return this.finishNode(node, "AnyTypeAnnotation");
  1431. case "bool":
  1432. case "boolean":
  1433. return this.finishNode(node, "BooleanTypeAnnotation");
  1434. case "mixed":
  1435. return this.finishNode(node, "MixedTypeAnnotation");
  1436. case "empty":
  1437. return this.finishNode(node, "EmptyTypeAnnotation");
  1438. case "number":
  1439. return this.finishNode(node, "NumberTypeAnnotation");
  1440. case "string":
  1441. return this.finishNode(node, "StringTypeAnnotation");
  1442. default:
  1443. this.checkNotUnderscore(id.name);
  1444. return this.flowParseGenericType(startPos, startLoc, id);
  1445. }
  1446. }
  1447. flowParsePrimaryType() {
  1448. const startPos = this.state.start;
  1449. const startLoc = this.state.startLoc;
  1450. const node = this.startNode();
  1451. let tmp;
  1452. let type;
  1453. let isGroupedType = false;
  1454. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  1455. switch (this.state.type) {
  1456. case types.name:
  1457. if (this.isContextual("interface")) {
  1458. return this.flowParseInterfaceType();
  1459. }
  1460. return this.flowIdentToTypeAnnotation(startPos, startLoc, node, this.parseIdentifier());
  1461. case types.braceL:
  1462. return this.flowParseObjectType({
  1463. allowStatic: false,
  1464. allowExact: false,
  1465. allowSpread: true,
  1466. allowProto: false,
  1467. allowInexact: true
  1468. });
  1469. case types.braceBarL:
  1470. return this.flowParseObjectType({
  1471. allowStatic: false,
  1472. allowExact: true,
  1473. allowSpread: true,
  1474. allowProto: false,
  1475. allowInexact: false
  1476. });
  1477. case types.bracketL:
  1478. return this.flowParseTupleType();
  1479. case types.relational:
  1480. if (this.state.value === "<") {
  1481. node.typeParameters = this.flowParseTypeParameterDeclaration(false);
  1482. this.expect(types.parenL);
  1483. tmp = this.flowParseFunctionTypeParams();
  1484. node.params = tmp.params;
  1485. node.rest = tmp.rest;
  1486. this.expect(types.parenR);
  1487. this.expect(types.arrow);
  1488. node.returnType = this.flowParseType();
  1489. return this.finishNode(node, "FunctionTypeAnnotation");
  1490. }
  1491. break;
  1492. case types.parenL:
  1493. this.next();
  1494. if (!this.match(types.parenR) && !this.match(types.ellipsis)) {
  1495. if (this.match(types.name)) {
  1496. const token = this.lookahead().type;
  1497. isGroupedType = token !== types.question && token !== types.colon;
  1498. } else {
  1499. isGroupedType = true;
  1500. }
  1501. }
  1502. if (isGroupedType) {
  1503. this.state.noAnonFunctionType = false;
  1504. type = this.flowParseType();
  1505. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  1506. if (this.state.noAnonFunctionType || !(this.match(types.comma) || this.match(types.parenR) && this.lookahead().type === types.arrow)) {
  1507. this.expect(types.parenR);
  1508. return type;
  1509. } else {
  1510. this.eat(types.comma);
  1511. }
  1512. }
  1513. if (type) {
  1514. tmp = this.flowParseFunctionTypeParams([this.reinterpretTypeAsFunctionTypeParam(type)]);
  1515. } else {
  1516. tmp = this.flowParseFunctionTypeParams();
  1517. }
  1518. node.params = tmp.params;
  1519. node.rest = tmp.rest;
  1520. this.expect(types.parenR);
  1521. this.expect(types.arrow);
  1522. node.returnType = this.flowParseType();
  1523. node.typeParameters = null;
  1524. return this.finishNode(node, "FunctionTypeAnnotation");
  1525. case types.string:
  1526. return this.parseLiteral(this.state.value, "StringLiteralTypeAnnotation");
  1527. case types._true:
  1528. case types._false:
  1529. node.value = this.match(types._true);
  1530. this.next();
  1531. return this.finishNode(node, "BooleanLiteralTypeAnnotation");
  1532. case types.plusMin:
  1533. if (this.state.value === "-") {
  1534. this.next();
  1535. if (!this.match(types.num)) {
  1536. this.unexpected(null, `Unexpected token, expected "number"`);
  1537. }
  1538. return this.parseLiteral(-this.state.value, "NumberLiteralTypeAnnotation", node.start, node.loc.start);
  1539. }
  1540. this.unexpected();
  1541. case types.num:
  1542. return this.parseLiteral(this.state.value, "NumberLiteralTypeAnnotation");
  1543. case types._void:
  1544. this.next();
  1545. return this.finishNode(node, "VoidTypeAnnotation");
  1546. case types._null:
  1547. this.next();
  1548. return this.finishNode(node, "NullLiteralTypeAnnotation");
  1549. case types._this:
  1550. this.next();
  1551. return this.finishNode(node, "ThisTypeAnnotation");
  1552. case types.star:
  1553. this.next();
  1554. return this.finishNode(node, "ExistsTypeAnnotation");
  1555. default:
  1556. if (this.state.type.keyword === "typeof") {
  1557. return this.flowParseTypeofType();
  1558. } else if (this.state.type.keyword) {
  1559. const label = this.state.type.label;
  1560. this.next();
  1561. return super.createIdentifier(node, label);
  1562. }
  1563. }
  1564. throw this.unexpected();
  1565. }
  1566. flowParsePostfixType() {
  1567. const startPos = this.state.start,
  1568. startLoc = this.state.startLoc;
  1569. let type = this.flowParsePrimaryType();
  1570. while (this.match(types.bracketL) && !this.canInsertSemicolon()) {
  1571. const node = this.startNodeAt(startPos, startLoc);
  1572. node.elementType = type;
  1573. this.expect(types.bracketL);
  1574. this.expect(types.bracketR);
  1575. type = this.finishNode(node, "ArrayTypeAnnotation");
  1576. }
  1577. return type;
  1578. }
  1579. flowParsePrefixType() {
  1580. const node = this.startNode();
  1581. if (this.eat(types.question)) {
  1582. node.typeAnnotation = this.flowParsePrefixType();
  1583. return this.finishNode(node, "NullableTypeAnnotation");
  1584. } else {
  1585. return this.flowParsePostfixType();
  1586. }
  1587. }
  1588. flowParseAnonFunctionWithoutParens() {
  1589. const param = this.flowParsePrefixType();
  1590. if (!this.state.noAnonFunctionType && this.eat(types.arrow)) {
  1591. const node = this.startNodeAt(param.start, param.loc.start);
  1592. node.params = [this.reinterpretTypeAsFunctionTypeParam(param)];
  1593. node.rest = null;
  1594. node.returnType = this.flowParseType();
  1595. node.typeParameters = null;
  1596. return this.finishNode(node, "FunctionTypeAnnotation");
  1597. }
  1598. return param;
  1599. }
  1600. flowParseIntersectionType() {
  1601. const node = this.startNode();
  1602. this.eat(types.bitwiseAND);
  1603. const type = this.flowParseAnonFunctionWithoutParens();
  1604. node.types = [type];
  1605. while (this.eat(types.bitwiseAND)) {
  1606. node.types.push(this.flowParseAnonFunctionWithoutParens());
  1607. }
  1608. return node.types.length === 1 ? type : this.finishNode(node, "IntersectionTypeAnnotation");
  1609. }
  1610. flowParseUnionType() {
  1611. const node = this.startNode();
  1612. this.eat(types.bitwiseOR);
  1613. const type = this.flowParseIntersectionType();
  1614. node.types = [type];
  1615. while (this.eat(types.bitwiseOR)) {
  1616. node.types.push(this.flowParseIntersectionType());
  1617. }
  1618. return node.types.length === 1 ? type : this.finishNode(node, "UnionTypeAnnotation");
  1619. }
  1620. flowParseType() {
  1621. const oldInType = this.state.inType;
  1622. this.state.inType = true;
  1623. const type = this.flowParseUnionType();
  1624. this.state.inType = oldInType;
  1625. this.state.exprAllowed = this.state.exprAllowed || this.state.noAnonFunctionType;
  1626. return type;
  1627. }
  1628. flowParseTypeOrImplicitInstantiation() {
  1629. if (this.state.type === types.name && this.state.value === "_") {
  1630. const startPos = this.state.start;
  1631. const startLoc = this.state.startLoc;
  1632. const node = this.parseIdentifier();
  1633. return this.flowParseGenericType(startPos, startLoc, node);
  1634. } else {
  1635. return this.flowParseType();
  1636. }
  1637. }
  1638. flowParseTypeAnnotation() {
  1639. const node = this.startNode();
  1640. node.typeAnnotation = this.flowParseTypeInitialiser();
  1641. return this.finishNode(node, "TypeAnnotation");
  1642. }
  1643. flowParseTypeAnnotatableIdentifier(allowPrimitiveOverride) {
  1644. const ident = allowPrimitiveOverride ? this.parseIdentifier() : this.flowParseRestrictedIdentifier();
  1645. if (this.match(types.colon)) {
  1646. ident.typeAnnotation = this.flowParseTypeAnnotation();
  1647. this.resetEndLocation(ident);
  1648. }
  1649. return ident;
  1650. }
  1651. typeCastToParameter(node) {
  1652. node.expression.typeAnnotation = node.typeAnnotation;
  1653. this.resetEndLocation(node.expression, node.typeAnnotation.end, node.typeAnnotation.loc.end);
  1654. return node.expression;
  1655. }
  1656. flowParseVariance() {
  1657. let variance = null;
  1658. if (this.match(types.plusMin)) {
  1659. variance = this.startNode();
  1660. if (this.state.value === "+") {
  1661. variance.kind = "plus";
  1662. } else {
  1663. variance.kind = "minus";
  1664. }
  1665. this.next();
  1666. this.finishNode(variance, "Variance");
  1667. }
  1668. return variance;
  1669. }
  1670. parseFunctionBody(node, allowExpressionBody, isMethod = false) {
  1671. if (allowExpressionBody) {
  1672. return this.forwardNoArrowParamsConversionAt(node, () => super.parseFunctionBody(node, true, isMethod));
  1673. }
  1674. return super.parseFunctionBody(node, false, isMethod);
  1675. }
  1676. parseFunctionBodyAndFinish(node, type, isMethod = false) {
  1677. if (this.match(types.colon)) {
  1678. const typeNode = this.startNode();
  1679. [typeNode.typeAnnotation, node.predicate] = this.flowParseTypeAndPredicateInitialiser();
  1680. node.returnType = typeNode.typeAnnotation ? this.finishNode(typeNode, "TypeAnnotation") : null;
  1681. }
  1682. super.parseFunctionBodyAndFinish(node, type, isMethod);
  1683. }
  1684. parseStatement(context, topLevel) {
  1685. if (this.state.strict && this.match(types.name) && this.state.value === "interface") {
  1686. const node = this.startNode();
  1687. this.next();
  1688. return this.flowParseInterface(node);
  1689. } else {
  1690. const stmt = super.parseStatement(context, topLevel);
  1691. if (this.flowPragma === undefined && !this.isValidDirective(stmt)) {
  1692. this.flowPragma = null;
  1693. }
  1694. return stmt;
  1695. }
  1696. }
  1697. parseExpressionStatement(node, expr) {
  1698. if (expr.type === "Identifier") {
  1699. if (expr.name === "declare") {
  1700. if (this.match(types._class) || this.match(types.name) || this.match(types._function) || this.match(types._var) || this.match(types._export)) {
  1701. return this.flowParseDeclare(node);
  1702. }
  1703. } else if (this.match(types.name)) {
  1704. if (expr.name === "interface") {
  1705. return this.flowParseInterface(node);
  1706. } else if (expr.name === "type") {
  1707. return this.flowParseTypeAlias(node);
  1708. } else if (expr.name === "opaque") {
  1709. return this.flowParseOpaqueType(node, false);
  1710. }
  1711. }
  1712. }
  1713. return super.parseExpressionStatement(node, expr);
  1714. }
  1715. shouldParseExportDeclaration() {
  1716. return this.isContextual("type") || this.isContextual("interface") || this.isContextual("opaque") || super.shouldParseExportDeclaration();
  1717. }
  1718. isExportDefaultSpecifier() {
  1719. if (this.match(types.name) && (this.state.value === "type" || this.state.value === "interface" || this.state.value === "opaque")) {
  1720. return false;
  1721. }
  1722. return super.isExportDefaultSpecifier();
  1723. }
  1724. parseConditional(expr, noIn, startPos, startLoc, refNeedsArrowPos) {
  1725. if (!this.match(types.question)) return expr;
  1726. if (refNeedsArrowPos) {
  1727. const state = this.state.clone();
  1728. try {
  1729. return super.parseConditional(expr, noIn, startPos, startLoc);
  1730. } catch (err) {
  1731. if (err instanceof SyntaxError) {
  1732. this.state = state;
  1733. refNeedsArrowPos.start = err.pos || this.state.start;
  1734. return expr;
  1735. } else {
  1736. throw err;
  1737. }
  1738. }
  1739. }
  1740. this.expect(types.question);
  1741. const state = this.state.clone();
  1742. const originalNoArrowAt = this.state.noArrowAt;
  1743. const node = this.startNodeAt(startPos, startLoc);
  1744. let {
  1745. consequent,
  1746. failed
  1747. } = this.tryParseConditionalConsequent();
  1748. let [valid, invalid] = this.getArrowLikeExpressions(consequent);
  1749. if (failed || invalid.length > 0) {
  1750. const noArrowAt = [...originalNoArrowAt];
  1751. if (invalid.length > 0) {
  1752. this.state = state;
  1753. this.state.noArrowAt = noArrowAt;
  1754. for (let i = 0; i < invalid.length; i++) {
  1755. noArrowAt.push(invalid[i].start);
  1756. }
  1757. ({
  1758. consequent,
  1759. failed
  1760. } = this.tryParseConditionalConsequent());
  1761. [valid, invalid] = this.getArrowLikeExpressions(consequent);
  1762. }
  1763. if (failed && valid.length > 1) {
  1764. this.raise(state.start, "Ambiguous expression: wrap the arrow functions in parentheses to disambiguate.");
  1765. }
  1766. if (failed && valid.length === 1) {
  1767. this.state = state;
  1768. this.state.noArrowAt = noArrowAt.concat(valid[0].start);
  1769. ({
  1770. consequent,
  1771. failed
  1772. } = this.tryParseConditionalConsequent());
  1773. }
  1774. this.getArrowLikeExpressions(consequent, true);
  1775. }
  1776. this.state.noArrowAt = originalNoArrowAt;
  1777. this.expect(types.colon);
  1778. node.test = expr;
  1779. node.consequent = consequent;
  1780. node.alternate = this.forwardNoArrowParamsConversionAt(node, () => this.parseMaybeAssign(noIn, undefined, undefined, undefined));
  1781. return this.finishNode(node, "ConditionalExpression");
  1782. }
  1783. tryParseConditionalConsequent() {
  1784. this.state.noArrowParamsConversionAt.push(this.state.start);
  1785. const consequent = this.parseMaybeAssign();
  1786. const failed = !this.match(types.colon);
  1787. this.state.noArrowParamsConversionAt.pop();
  1788. return {
  1789. consequent,
  1790. failed
  1791. };
  1792. }
  1793. getArrowLikeExpressions(node, disallowInvalid) {
  1794. const stack = [node];
  1795. const arrows = [];
  1796. while (stack.length !== 0) {
  1797. const node = stack.pop();
  1798. if (node.type === "ArrowFunctionExpression") {
  1799. if (node.typeParameters || !node.returnType) {
  1800. this.toAssignableList(node.params, true, "arrow function parameters");
  1801. this.scope.enter(functionFlags(false, false) | SCOPE_ARROW);
  1802. super.checkParams(node, false, true);
  1803. this.scope.exit();
  1804. } else {
  1805. arrows.push(node);
  1806. }
  1807. stack.push(node.body);
  1808. } else if (node.type === "ConditionalExpression") {
  1809. stack.push(node.consequent);
  1810. stack.push(node.alternate);
  1811. }
  1812. }
  1813. if (disallowInvalid) {
  1814. for (let i = 0; i < arrows.length; i++) {
  1815. this.toAssignableList(node.params, true, "arrow function parameters");
  1816. }
  1817. return [arrows, []];
  1818. }
  1819. return partition(arrows, node => {
  1820. try {
  1821. this.toAssignableList(node.params, true, "arrow function parameters");
  1822. return true;
  1823. } catch (err) {
  1824. return false;
  1825. }
  1826. });
  1827. }
  1828. forwardNoArrowParamsConversionAt(node, parse) {
  1829. let result;
  1830. if (this.state.noArrowParamsConversionAt.indexOf(node.start) !== -1) {
  1831. this.state.noArrowParamsConversionAt.push(this.state.start);
  1832. result = parse();
  1833. this.state.noArrowParamsConversionAt.pop();
  1834. } else {
  1835. result = parse();
  1836. }
  1837. return result;
  1838. }
  1839. parseParenItem(node, startPos, startLoc) {
  1840. node = super.parseParenItem(node, startPos, startLoc);
  1841. if (this.eat(types.question)) {
  1842. node.optional = true;
  1843. this.resetEndLocation(node);
  1844. }
  1845. if (this.match(types.colon)) {
  1846. const typeCastNode = this.startNodeAt(startPos, startLoc);
  1847. typeCastNode.expression = node;
  1848. typeCastNode.typeAnnotation = this.flowParseTypeAnnotation();
  1849. return this.finishNode(typeCastNode, "TypeCastExpression");
  1850. }
  1851. return node;
  1852. }
  1853. assertModuleNodeAllowed(node) {
  1854. if (node.type === "ImportDeclaration" && (node.importKind === "type" || node.importKind === "typeof") || node.type === "ExportNamedDeclaration" && node.exportKind === "type" || node.type === "ExportAllDeclaration" && node.exportKind === "type") {
  1855. return;
  1856. }
  1857. super.assertModuleNodeAllowed(node);
  1858. }
  1859. parseExport(node) {
  1860. const decl = super.parseExport(node);
  1861. if (decl.type === "ExportNamedDeclaration" || decl.type === "ExportAllDeclaration") {
  1862. decl.exportKind = decl.exportKind || "value";
  1863. }
  1864. return decl;
  1865. }
  1866. parseExportDeclaration(node) {
  1867. if (this.isContextual("type")) {
  1868. node.exportKind = "type";
  1869. const declarationNode = this.startNode();
  1870. this.next();
  1871. if (this.match(types.braceL)) {
  1872. node.specifiers = this.parseExportSpecifiers();
  1873. this.parseExportFrom(node);
  1874. return null;
  1875. } else {
  1876. return this.flowParseTypeAlias(declarationNode);
  1877. }
  1878. } else if (this.isContextual("opaque")) {
  1879. node.exportKind = "type";
  1880. const declarationNode = this.startNode();
  1881. this.next();
  1882. return this.flowParseOpaqueType(declarationNode, false);
  1883. } else if (this.isContextual("interface")) {
  1884. node.exportKind = "type";
  1885. const declarationNode = this.startNode();
  1886. this.next();
  1887. return this.flowParseInterface(declarationNode);
  1888. } else {
  1889. return super.parseExportDeclaration(node);
  1890. }
  1891. }
  1892. eatExportStar(node) {
  1893. if (super.eatExportStar(...arguments)) return true;
  1894. if (this.isContextual("type") && this.lookahead().type === types.star) {
  1895. node.exportKind = "type";
  1896. this.next();
  1897. this.next();
  1898. return true;
  1899. }
  1900. return false;
  1901. }
  1902. maybeParseExportNamespaceSpecifier(node) {
  1903. const pos = this.state.start;
  1904. const hasNamespace = super.maybeParseExportNamespaceSpecifier(node);
  1905. if (hasNamespace && node.exportKind === "type") {
  1906. this.unexpected(pos);
  1907. }
  1908. return hasNamespace;
  1909. }
  1910. parseClassId(node, isStatement, optionalId) {
  1911. super.parseClassId(node, isStatement, optionalId);
  1912. if (this.isRelational("<")) {
  1913. node.typeParameters = this.flowParseTypeParameterDeclaration();
  1914. }
  1915. }
  1916. getTokenFromCode(code) {
  1917. const next = this.input.charCodeAt(this.state.pos + 1);
  1918. if (code === 123 && next === 124) {
  1919. return this.finishOp(types.braceBarL, 2);
  1920. } else if (this.state.inType && (code === 62 || code === 60)) {
  1921. return this.finishOp(types.relational, 1);
  1922. } else if (isIteratorStart(code, next)) {
  1923. this.state.isIterator = true;
  1924. return super.readWord();
  1925. } else {
  1926. return super.getTokenFromCode(code);
  1927. }
  1928. }
  1929. toAssignable(node, isBinding, contextDescription) {
  1930. if (node.type === "TypeCastExpression") {
  1931. return super.toAssignable(this.typeCastToParameter(node), isBinding, contextDescription);
  1932. } else {
  1933. return super.toAssignable(node, isBinding, contextDescription);
  1934. }
  1935. }
  1936. toAssignableList(exprList, isBinding, contextDescription) {
  1937. for (let i = 0; i < exprList.length; i++) {
  1938. const expr = exprList[i];
  1939. if (expr && expr.type === "TypeCastExpression") {
  1940. exprList[i] = this.typeCastToParameter(expr);
  1941. }
  1942. }
  1943. return super.toAssignableList(exprList, isBinding, contextDescription);
  1944. }
  1945. toReferencedList(exprList, isParenthesizedExpr) {
  1946. for (let i = 0; i < exprList.length; i++) {
  1947. const expr = exprList[i];
  1948. if (expr && expr.type === "TypeCastExpression" && (!expr.extra || !expr.extra.parenthesized) && (exprList.length > 1 || !isParenthesizedExpr)) {
  1949. this.raise(expr.typeAnnotation.start, "The type cast expression is expected to be wrapped with parenthesis");
  1950. }
  1951. }
  1952. return exprList;
  1953. }
  1954. checkLVal(expr, bindingType = BIND_NONE, checkClashes, contextDescription) {
  1955. if (expr.type !== "TypeCastExpression") {
  1956. return super.checkLVal(expr, bindingType, checkClashes, contextDescription);
  1957. }
  1958. }
  1959. parseClassProperty(node) {
  1960. if (this.match(types.colon)) {
  1961. node.typeAnnotation = this.flowParseTypeAnnotation();
  1962. }
  1963. return super.parseClassProperty(node);
  1964. }
  1965. parseClassPrivateProperty(node) {
  1966. if (this.match(types.colon)) {
  1967. node.typeAnnotation = this.flowParseTypeAnnotation();
  1968. }
  1969. return super.parseClassPrivateProperty(node);
  1970. }
  1971. isClassMethod() {
  1972. return this.isRelational("<") || super.isClassMethod();
  1973. }
  1974. isClassProperty() {
  1975. return this.match(types.colon) || super.isClassProperty();
  1976. }
  1977. isNonstaticConstructor(method) {
  1978. return !this.match(types.colon) && super.isNonstaticConstructor(method);
  1979. }
  1980. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  1981. if (method.variance) {
  1982. this.unexpected(method.variance.start);
  1983. }
  1984. delete method.variance;
  1985. if (this.isRelational("<")) {
  1986. method.typeParameters = this.flowParseTypeParameterDeclaration(false);
  1987. }
  1988. super.pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper);
  1989. }
  1990. pushClassPrivateMethod(classBody, method, isGenerator, isAsync) {
  1991. if (method.variance) {
  1992. this.unexpected(method.variance.start);
  1993. }
  1994. delete method.variance;
  1995. if (this.isRelational("<")) {
  1996. method.typeParameters = this.flowParseTypeParameterDeclaration();
  1997. }
  1998. super.pushClassPrivateMethod(classBody, method, isGenerator, isAsync);
  1999. }
  2000. parseClassSuper(node) {
  2001. super.parseClassSuper(node);
  2002. if (node.superClass && this.isRelational("<")) {
  2003. node.superTypeParameters = this.flowParseTypeParameterInstantiation();
  2004. }
  2005. if (this.isContextual("implements")) {
  2006. this.next();
  2007. const implemented = node.implements = [];
  2008. do {
  2009. const node = this.startNode();
  2010. node.id = this.flowParseRestrictedIdentifier(true);
  2011. if (this.isRelational("<")) {
  2012. node.typeParameters = this.flowParseTypeParameterInstantiation();
  2013. } else {
  2014. node.typeParameters = null;
  2015. }
  2016. implemented.push(this.finishNode(node, "ClassImplements"));
  2017. } while (this.eat(types.comma));
  2018. }
  2019. }
  2020. parsePropertyName(node) {
  2021. const variance = this.flowParseVariance();
  2022. const key = super.parsePropertyName(node);
  2023. node.variance = variance;
  2024. return key;
  2025. }
  2026. parseObjPropValue(prop, startPos, startLoc, isGenerator, isAsync, isPattern, refShorthandDefaultPos, containsEsc) {
  2027. if (prop.variance) {
  2028. this.unexpected(prop.variance.start);
  2029. }
  2030. delete prop.variance;
  2031. let typeParameters;
  2032. if (this.isRelational("<")) {
  2033. typeParameters = this.flowParseTypeParameterDeclaration(false);
  2034. if (!this.match(types.parenL)) this.unexpected();
  2035. }
  2036. super.parseObjPropValue(prop, startPos, startLoc, isGenerator, isAsync, isPattern, refShorthandDefaultPos, containsEsc);
  2037. if (typeParameters) {
  2038. (prop.value || prop).typeParameters = typeParameters;
  2039. }
  2040. }
  2041. parseAssignableListItemTypes(param) {
  2042. if (this.eat(types.question)) {
  2043. if (param.type !== "Identifier") {
  2044. throw this.raise(param.start, "A binding pattern parameter cannot be optional in an implementation signature.");
  2045. }
  2046. param.optional = true;
  2047. }
  2048. if (this.match(types.colon)) {
  2049. param.typeAnnotation = this.flowParseTypeAnnotation();
  2050. }
  2051. this.resetEndLocation(param);
  2052. return param;
  2053. }
  2054. parseMaybeDefault(startPos, startLoc, left) {
  2055. const node = super.parseMaybeDefault(startPos, startLoc, left);
  2056. if (node.type === "AssignmentPattern" && node.typeAnnotation && node.right.start < node.typeAnnotation.start) {
  2057. this.raise(node.typeAnnotation.start, "Type annotations must come before default assignments, " + "e.g. instead of `age = 25: number` use `age: number = 25`");
  2058. }
  2059. return node;
  2060. }
  2061. shouldParseDefaultImport(node) {
  2062. if (!hasTypeImportKind(node)) {
  2063. return super.shouldParseDefaultImport(node);
  2064. }
  2065. return isMaybeDefaultImport(this.state);
  2066. }
  2067. parseImportSpecifierLocal(node, specifier, type, contextDescription) {
  2068. specifier.local = hasTypeImportKind(node) ? this.flowParseRestrictedIdentifier(true) : this.parseIdentifier();
  2069. this.checkLVal(specifier.local, BIND_LEXICAL, undefined, contextDescription);
  2070. node.specifiers.push(this.finishNode(specifier, type));
  2071. }
  2072. maybeParseDefaultImportSpecifier(node) {
  2073. node.importKind = "value";
  2074. let kind = null;
  2075. if (this.match(types._typeof)) {
  2076. kind = "typeof";
  2077. } else if (this.isContextual("type")) {
  2078. kind = "type";
  2079. }
  2080. if (kind) {
  2081. const lh = this.lookahead();
  2082. if (kind === "type" && lh.type === types.star) {
  2083. this.unexpected(lh.start);
  2084. }
  2085. if (isMaybeDefaultImport(lh) || lh.type === types.braceL || lh.type === types.star) {
  2086. this.next();
  2087. node.importKind = kind;
  2088. }
  2089. }
  2090. return super.maybeParseDefaultImportSpecifier(node);
  2091. }
  2092. parseImportSpecifier(node) {
  2093. const specifier = this.startNode();
  2094. const firstIdentLoc = this.state.start;
  2095. const firstIdent = this.parseIdentifier(true);
  2096. let specifierTypeKind = null;
  2097. if (firstIdent.name === "type") {
  2098. specifierTypeKind = "type";
  2099. } else if (firstIdent.name === "typeof") {
  2100. specifierTypeKind = "typeof";
  2101. }
  2102. let isBinding = false;
  2103. if (this.isContextual("as") && !this.isLookaheadContextual("as")) {
  2104. const as_ident = this.parseIdentifier(true);
  2105. if (specifierTypeKind !== null && !this.match(types.name) && !this.state.type.keyword) {
  2106. specifier.imported = as_ident;
  2107. specifier.importKind = specifierTypeKind;
  2108. specifier.local = as_ident.__clone();
  2109. } else {
  2110. specifier.imported = firstIdent;
  2111. specifier.importKind = null;
  2112. specifier.local = this.parseIdentifier();
  2113. }
  2114. } else if (specifierTypeKind !== null && (this.match(types.name) || this.state.type.keyword)) {
  2115. specifier.imported = this.parseIdentifier(true);
  2116. specifier.importKind = specifierTypeKind;
  2117. if (this.eatContextual("as")) {
  2118. specifier.local = this.parseIdentifier();
  2119. } else {
  2120. isBinding = true;
  2121. specifier.local = specifier.imported.__clone();
  2122. }
  2123. } else {
  2124. isBinding = true;
  2125. specifier.imported = firstIdent;
  2126. specifier.importKind = null;
  2127. specifier.local = specifier.imported.__clone();
  2128. }
  2129. const nodeIsTypeImport = hasTypeImportKind(node);
  2130. const specifierIsTypeImport = hasTypeImportKind(specifier);
  2131. if (nodeIsTypeImport && specifierIsTypeImport) {
  2132. this.raise(firstIdentLoc, "The `type` and `typeof` keywords on named imports can only be used on regular " + "`import` statements. It cannot be used with `import type` or `import typeof` statements");
  2133. }
  2134. if (nodeIsTypeImport || specifierIsTypeImport) {
  2135. this.checkReservedType(specifier.local.name, specifier.local.start);
  2136. }
  2137. if (isBinding && !nodeIsTypeImport && !specifierIsTypeImport) {
  2138. this.checkReservedWord(specifier.local.name, specifier.start, true, true);
  2139. }
  2140. this.checkLVal(specifier.local, BIND_LEXICAL, undefined, "import specifier");
  2141. node.specifiers.push(this.finishNode(specifier, "ImportSpecifier"));
  2142. }
  2143. parseFunctionParams(node, allowModifiers) {
  2144. const kind = node.kind;
  2145. if (kind !== "get" && kind !== "set" && this.isRelational("<")) {
  2146. node.typeParameters = this.flowParseTypeParameterDeclaration(false);
  2147. }
  2148. super.parseFunctionParams(node, allowModifiers);
  2149. }
  2150. parseVarId(decl, kind) {
  2151. super.parseVarId(decl, kind);
  2152. if (this.match(types.colon)) {
  2153. decl.id.typeAnnotation = this.flowParseTypeAnnotation();
  2154. this.resetEndLocation(decl.id);
  2155. }
  2156. }
  2157. parseAsyncArrowFromCallExpression(node, call) {
  2158. if (this.match(types.colon)) {
  2159. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  2160. this.state.noAnonFunctionType = true;
  2161. node.returnType = this.flowParseTypeAnnotation();
  2162. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  2163. }
  2164. return super.parseAsyncArrowFromCallExpression(node, call);
  2165. }
  2166. shouldParseAsyncArrow() {
  2167. return this.match(types.colon) || super.shouldParseAsyncArrow();
  2168. }
  2169. parseMaybeAssign(noIn, refShorthandDefaultPos, afterLeftParse, refNeedsArrowPos) {
  2170. let jsxError = null;
  2171. if (this.hasPlugin("jsx") && (this.match(types.jsxTagStart) || this.isRelational("<"))) {
  2172. const state = this.state.clone();
  2173. try {
  2174. return super.parseMaybeAssign(noIn, refShorthandDefaultPos, afterLeftParse, refNeedsArrowPos);
  2175. } catch (err) {
  2176. if (err instanceof SyntaxError) {
  2177. this.state = state;
  2178. const cLength = this.state.context.length;
  2179. if (this.state.context[cLength - 1] === types$1.j_oTag) {
  2180. this.state.context.length -= 2;
  2181. }
  2182. jsxError = err;
  2183. } else {
  2184. throw err;
  2185. }
  2186. }
  2187. }
  2188. if (jsxError != null || this.isRelational("<")) {
  2189. let arrowExpression;
  2190. let typeParameters;
  2191. try {
  2192. typeParameters = this.flowParseTypeParameterDeclaration();
  2193. arrowExpression = this.forwardNoArrowParamsConversionAt(typeParameters, () => super.parseMaybeAssign(noIn, refShorthandDefaultPos, afterLeftParse, refNeedsArrowPos));
  2194. arrowExpression.typeParameters = typeParameters;
  2195. this.resetStartLocationFromNode(arrowExpression, typeParameters);
  2196. } catch (err) {
  2197. throw jsxError || err;
  2198. }
  2199. if (arrowExpression.type === "ArrowFunctionExpression") {
  2200. return arrowExpression;
  2201. } else if (jsxError != null) {
  2202. throw jsxError;
  2203. } else {
  2204. this.raise(typeParameters.start, "Expected an arrow function after this type parameter declaration");
  2205. }
  2206. }
  2207. return super.parseMaybeAssign(noIn, refShorthandDefaultPos, afterLeftParse, refNeedsArrowPos);
  2208. }
  2209. parseArrow(node) {
  2210. if (this.match(types.colon)) {
  2211. const state = this.state.clone();
  2212. try {
  2213. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  2214. this.state.noAnonFunctionType = true;
  2215. const typeNode = this.startNode();
  2216. [typeNode.typeAnnotation, node.predicate] = this.flowParseTypeAndPredicateInitialiser();
  2217. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  2218. if (this.canInsertSemicolon()) this.unexpected();
  2219. if (!this.match(types.arrow)) this.unexpected();
  2220. node.returnType = typeNode.typeAnnotation ? this.finishNode(typeNode, "TypeAnnotation") : null;
  2221. } catch (err) {
  2222. if (err instanceof SyntaxError) {
  2223. this.state = state;
  2224. } else {
  2225. throw err;
  2226. }
  2227. }
  2228. }
  2229. return super.parseArrow(node);
  2230. }
  2231. shouldParseArrow() {
  2232. return this.match(types.colon) || super.shouldParseArrow();
  2233. }
  2234. setArrowFunctionParameters(node, params) {
  2235. if (this.state.noArrowParamsConversionAt.indexOf(node.start) !== -1) {
  2236. node.params = params;
  2237. } else {
  2238. super.setArrowFunctionParameters(node, params);
  2239. }
  2240. }
  2241. checkParams(node, allowDuplicates, isArrowFunction) {
  2242. if (isArrowFunction && this.state.noArrowParamsConversionAt.indexOf(node.start) !== -1) {
  2243. return;
  2244. }
  2245. return super.checkParams(node, allowDuplicates, isArrowFunction);
  2246. }
  2247. parseParenAndDistinguishExpression(canBeArrow) {
  2248. return super.parseParenAndDistinguishExpression(canBeArrow && this.state.noArrowAt.indexOf(this.state.start) === -1);
  2249. }
  2250. parseSubscripts(base, startPos, startLoc, noCalls) {
  2251. if (base.type === "Identifier" && base.name === "async" && this.state.noArrowAt.indexOf(startPos) !== -1) {
  2252. this.next();
  2253. const node = this.startNodeAt(startPos, startLoc);
  2254. node.callee = base;
  2255. node.arguments = this.parseCallExpressionArguments(types.parenR, false);
  2256. base = this.finishNode(node, "CallExpression");
  2257. } else if (base.type === "Identifier" && base.name === "async" && this.isRelational("<")) {
  2258. const state = this.state.clone();
  2259. let error;
  2260. try {
  2261. const node = this.parseAsyncArrowWithTypeParameters(startPos, startLoc);
  2262. if (node) return node;
  2263. } catch (e) {
  2264. error = e;
  2265. }
  2266. this.state = state;
  2267. try {
  2268. return super.parseSubscripts(base, startPos, startLoc, noCalls);
  2269. } catch (e) {
  2270. throw error || e;
  2271. }
  2272. }
  2273. return super.parseSubscripts(base, startPos, startLoc, noCalls);
  2274. }
  2275. parseSubscript(base, startPos, startLoc, noCalls, subscriptState, maybeAsyncArrow) {
  2276. if (this.match(types.questionDot) && this.isLookaheadRelational("<")) {
  2277. this.expectPlugin("optionalChaining");
  2278. subscriptState.optionalChainMember = true;
  2279. if (noCalls) {
  2280. subscriptState.stop = true;
  2281. return base;
  2282. }
  2283. this.next();
  2284. const node = this.startNodeAt(startPos, startLoc);
  2285. node.callee = base;
  2286. node.typeArguments = this.flowParseTypeParameterInstantiation();
  2287. this.expect(types.parenL);
  2288. node.arguments = this.parseCallExpressionArguments(types.parenR, false);
  2289. node.optional = true;
  2290. return this.finishNode(node, "OptionalCallExpression");
  2291. } else if (!noCalls && this.shouldParseTypes() && this.isRelational("<")) {
  2292. const node = this.startNodeAt(startPos, startLoc);
  2293. node.callee = base;
  2294. const state = this.state.clone();
  2295. try {
  2296. node.typeArguments = this.flowParseTypeParameterInstantiationCallOrNew();
  2297. this.expect(types.parenL);
  2298. node.arguments = this.parseCallExpressionArguments(types.parenR, false);
  2299. if (subscriptState.optionalChainMember) {
  2300. node.optional = false;
  2301. return this.finishNode(node, "OptionalCallExpression");
  2302. }
  2303. return this.finishNode(node, "CallExpression");
  2304. } catch (e) {
  2305. if (e instanceof SyntaxError) {
  2306. this.state = state;
  2307. } else {
  2308. throw e;
  2309. }
  2310. }
  2311. }
  2312. return super.parseSubscript(base, startPos, startLoc, noCalls, subscriptState, maybeAsyncArrow);
  2313. }
  2314. parseNewArguments(node) {
  2315. let targs = null;
  2316. if (this.shouldParseTypes() && this.isRelational("<")) {
  2317. const state = this.state.clone();
  2318. try {
  2319. targs = this.flowParseTypeParameterInstantiationCallOrNew();
  2320. } catch (e) {
  2321. if (e instanceof SyntaxError) {
  2322. this.state = state;
  2323. } else {
  2324. throw e;
  2325. }
  2326. }
  2327. }
  2328. node.typeArguments = targs;
  2329. super.parseNewArguments(node);
  2330. }
  2331. parseAsyncArrowWithTypeParameters(startPos, startLoc) {
  2332. const node = this.startNodeAt(startPos, startLoc);
  2333. this.parseFunctionParams(node);
  2334. if (!this.parseArrow(node)) return;
  2335. return this.parseArrowExpression(node, undefined, true);
  2336. }
  2337. readToken_mult_modulo(code) {
  2338. const next = this.input.charCodeAt(this.state.pos + 1);
  2339. if (code === 42 && next === 47 && this.state.hasFlowComment) {
  2340. this.state.hasFlowComment = false;
  2341. this.state.pos += 2;
  2342. this.nextToken();
  2343. return;
  2344. }
  2345. super.readToken_mult_modulo(code);
  2346. }
  2347. readToken_pipe_amp(code) {
  2348. const next = this.input.charCodeAt(this.state.pos + 1);
  2349. if (code === 124 && next === 125) {
  2350. this.finishOp(types.braceBarR, 2);
  2351. return;
  2352. }
  2353. super.readToken_pipe_amp(code);
  2354. }
  2355. parseTopLevel(file, program) {
  2356. const fileNode = super.parseTopLevel(file, program);
  2357. if (this.state.hasFlowComment) {
  2358. this.unexpected(null, "Unterminated flow-comment");
  2359. }
  2360. return fileNode;
  2361. }
  2362. skipBlockComment() {
  2363. if (this.hasPlugin("flowComments") && this.skipFlowComment()) {
  2364. if (this.state.hasFlowComment) {
  2365. this.unexpected(null, "Cannot have a flow comment inside another flow comment");
  2366. }
  2367. this.hasFlowCommentCompletion();
  2368. this.state.pos += this.skipFlowComment();
  2369. this.state.hasFlowComment = true;
  2370. return;
  2371. }
  2372. if (this.state.hasFlowComment) {
  2373. const end = this.input.indexOf("*-/", this.state.pos += 2);
  2374. if (end === -1) this.raise(this.state.pos - 2, "Unterminated comment");
  2375. this.state.pos = end + 3;
  2376. return;
  2377. }
  2378. super.skipBlockComment();
  2379. }
  2380. skipFlowComment() {
  2381. const {
  2382. pos
  2383. } = this.state;
  2384. let shiftToFirstNonWhiteSpace = 2;
  2385. while ([32, 9].includes(this.input.charCodeAt(pos + shiftToFirstNonWhiteSpace))) {
  2386. shiftToFirstNonWhiteSpace++;
  2387. }
  2388. const ch2 = this.input.charCodeAt(shiftToFirstNonWhiteSpace + pos);
  2389. const ch3 = this.input.charCodeAt(shiftToFirstNonWhiteSpace + pos + 1);
  2390. if (ch2 === 58 && ch3 === 58) {
  2391. return shiftToFirstNonWhiteSpace + 2;
  2392. }
  2393. if (this.input.slice(shiftToFirstNonWhiteSpace + pos, shiftToFirstNonWhiteSpace + pos + 12) === "flow-include") {
  2394. return shiftToFirstNonWhiteSpace + 12;
  2395. }
  2396. if (ch2 === 58 && ch3 !== 58) {
  2397. return shiftToFirstNonWhiteSpace;
  2398. }
  2399. return false;
  2400. }
  2401. hasFlowCommentCompletion() {
  2402. const end = this.input.indexOf("*/", this.state.pos);
  2403. if (end === -1) {
  2404. this.raise(this.state.pos, "Unterminated comment");
  2405. }
  2406. }
  2407. });
  2408. const entities = {
  2409. quot: "\u0022",
  2410. amp: "&",
  2411. apos: "\u0027",
  2412. lt: "<",
  2413. gt: ">",
  2414. nbsp: "\u00A0",
  2415. iexcl: "\u00A1",
  2416. cent: "\u00A2",
  2417. pound: "\u00A3",
  2418. curren: "\u00A4",
  2419. yen: "\u00A5",
  2420. brvbar: "\u00A6",
  2421. sect: "\u00A7",
  2422. uml: "\u00A8",
  2423. copy: "\u00A9",
  2424. ordf: "\u00AA",
  2425. laquo: "\u00AB",
  2426. not: "\u00AC",
  2427. shy: "\u00AD",
  2428. reg: "\u00AE",
  2429. macr: "\u00AF",
  2430. deg: "\u00B0",
  2431. plusmn: "\u00B1",
  2432. sup2: "\u00B2",
  2433. sup3: "\u00B3",
  2434. acute: "\u00B4",
  2435. micro: "\u00B5",
  2436. para: "\u00B6",
  2437. middot: "\u00B7",
  2438. cedil: "\u00B8",
  2439. sup1: "\u00B9",
  2440. ordm: "\u00BA",
  2441. raquo: "\u00BB",
  2442. frac14: "\u00BC",
  2443. frac12: "\u00BD",
  2444. frac34: "\u00BE",
  2445. iquest: "\u00BF",
  2446. Agrave: "\u00C0",
  2447. Aacute: "\u00C1",
  2448. Acirc: "\u00C2",
  2449. Atilde: "\u00C3",
  2450. Auml: "\u00C4",
  2451. Aring: "\u00C5",
  2452. AElig: "\u00C6",
  2453. Ccedil: "\u00C7",
  2454. Egrave: "\u00C8",
  2455. Eacute: "\u00C9",
  2456. Ecirc: "\u00CA",
  2457. Euml: "\u00CB",
  2458. Igrave: "\u00CC",
  2459. Iacute: "\u00CD",
  2460. Icirc: "\u00CE",
  2461. Iuml: "\u00CF",
  2462. ETH: "\u00D0",
  2463. Ntilde: "\u00D1",
  2464. Ograve: "\u00D2",
  2465. Oacute: "\u00D3",
  2466. Ocirc: "\u00D4",
  2467. Otilde: "\u00D5",
  2468. Ouml: "\u00D6",
  2469. times: "\u00D7",
  2470. Oslash: "\u00D8",
  2471. Ugrave: "\u00D9",
  2472. Uacute: "\u00DA",
  2473. Ucirc: "\u00DB",
  2474. Uuml: "\u00DC",
  2475. Yacute: "\u00DD",
  2476. THORN: "\u00DE",
  2477. szlig: "\u00DF",
  2478. agrave: "\u00E0",
  2479. aacute: "\u00E1",
  2480. acirc: "\u00E2",
  2481. atilde: "\u00E3",
  2482. auml: "\u00E4",
  2483. aring: "\u00E5",
  2484. aelig: "\u00E6",
  2485. ccedil: "\u00E7",
  2486. egrave: "\u00E8",
  2487. eacute: "\u00E9",
  2488. ecirc: "\u00EA",
  2489. euml: "\u00EB",
  2490. igrave: "\u00EC",
  2491. iacute: "\u00ED",
  2492. icirc: "\u00EE",
  2493. iuml: "\u00EF",
  2494. eth: "\u00F0",
  2495. ntilde: "\u00F1",
  2496. ograve: "\u00F2",
  2497. oacute: "\u00F3",
  2498. ocirc: "\u00F4",
  2499. otilde: "\u00F5",
  2500. ouml: "\u00F6",
  2501. divide: "\u00F7",
  2502. oslash: "\u00F8",
  2503. ugrave: "\u00F9",
  2504. uacute: "\u00FA",
  2505. ucirc: "\u00FB",
  2506. uuml: "\u00FC",
  2507. yacute: "\u00FD",
  2508. thorn: "\u00FE",
  2509. yuml: "\u00FF",
  2510. OElig: "\u0152",
  2511. oelig: "\u0153",
  2512. Scaron: "\u0160",
  2513. scaron: "\u0161",
  2514. Yuml: "\u0178",
  2515. fnof: "\u0192",
  2516. circ: "\u02C6",
  2517. tilde: "\u02DC",
  2518. Alpha: "\u0391",
  2519. Beta: "\u0392",
  2520. Gamma: "\u0393",
  2521. Delta: "\u0394",
  2522. Epsilon: "\u0395",
  2523. Zeta: "\u0396",
  2524. Eta: "\u0397",
  2525. Theta: "\u0398",
  2526. Iota: "\u0399",
  2527. Kappa: "\u039A",
  2528. Lambda: "\u039B",
  2529. Mu: "\u039C",
  2530. Nu: "\u039D",
  2531. Xi: "\u039E",
  2532. Omicron: "\u039F",
  2533. Pi: "\u03A0",
  2534. Rho: "\u03A1",
  2535. Sigma: "\u03A3",
  2536. Tau: "\u03A4",
  2537. Upsilon: "\u03A5",
  2538. Phi: "\u03A6",
  2539. Chi: "\u03A7",
  2540. Psi: "\u03A8",
  2541. Omega: "\u03A9",
  2542. alpha: "\u03B1",
  2543. beta: "\u03B2",
  2544. gamma: "\u03B3",
  2545. delta: "\u03B4",
  2546. epsilon: "\u03B5",
  2547. zeta: "\u03B6",
  2548. eta: "\u03B7",
  2549. theta: "\u03B8",
  2550. iota: "\u03B9",
  2551. kappa: "\u03BA",
  2552. lambda: "\u03BB",
  2553. mu: "\u03BC",
  2554. nu: "\u03BD",
  2555. xi: "\u03BE",
  2556. omicron: "\u03BF",
  2557. pi: "\u03C0",
  2558. rho: "\u03C1",
  2559. sigmaf: "\u03C2",
  2560. sigma: "\u03C3",
  2561. tau: "\u03C4",
  2562. upsilon: "\u03C5",
  2563. phi: "\u03C6",
  2564. chi: "\u03C7",
  2565. psi: "\u03C8",
  2566. omega: "\u03C9",
  2567. thetasym: "\u03D1",
  2568. upsih: "\u03D2",
  2569. piv: "\u03D6",
  2570. ensp: "\u2002",
  2571. emsp: "\u2003",
  2572. thinsp: "\u2009",
  2573. zwnj: "\u200C",
  2574. zwj: "\u200D",
  2575. lrm: "\u200E",
  2576. rlm: "\u200F",
  2577. ndash: "\u2013",
  2578. mdash: "\u2014",
  2579. lsquo: "\u2018",
  2580. rsquo: "\u2019",
  2581. sbquo: "\u201A",
  2582. ldquo: "\u201C",
  2583. rdquo: "\u201D",
  2584. bdquo: "\u201E",
  2585. dagger: "\u2020",
  2586. Dagger: "\u2021",
  2587. bull: "\u2022",
  2588. hellip: "\u2026",
  2589. permil: "\u2030",
  2590. prime: "\u2032",
  2591. Prime: "\u2033",
  2592. lsaquo: "\u2039",
  2593. rsaquo: "\u203A",
  2594. oline: "\u203E",
  2595. frasl: "\u2044",
  2596. euro: "\u20AC",
  2597. image: "\u2111",
  2598. weierp: "\u2118",
  2599. real: "\u211C",
  2600. trade: "\u2122",
  2601. alefsym: "\u2135",
  2602. larr: "\u2190",
  2603. uarr: "\u2191",
  2604. rarr: "\u2192",
  2605. darr: "\u2193",
  2606. harr: "\u2194",
  2607. crarr: "\u21B5",
  2608. lArr: "\u21D0",
  2609. uArr: "\u21D1",
  2610. rArr: "\u21D2",
  2611. dArr: "\u21D3",
  2612. hArr: "\u21D4",
  2613. forall: "\u2200",
  2614. part: "\u2202",
  2615. exist: "\u2203",
  2616. empty: "\u2205",
  2617. nabla: "\u2207",
  2618. isin: "\u2208",
  2619. notin: "\u2209",
  2620. ni: "\u220B",
  2621. prod: "\u220F",
  2622. sum: "\u2211",
  2623. minus: "\u2212",
  2624. lowast: "\u2217",
  2625. radic: "\u221A",
  2626. prop: "\u221D",
  2627. infin: "\u221E",
  2628. ang: "\u2220",
  2629. and: "\u2227",
  2630. or: "\u2228",
  2631. cap: "\u2229",
  2632. cup: "\u222A",
  2633. int: "\u222B",
  2634. there4: "\u2234",
  2635. sim: "\u223C",
  2636. cong: "\u2245",
  2637. asymp: "\u2248",
  2638. ne: "\u2260",
  2639. equiv: "\u2261",
  2640. le: "\u2264",
  2641. ge: "\u2265",
  2642. sub: "\u2282",
  2643. sup: "\u2283",
  2644. nsub: "\u2284",
  2645. sube: "\u2286",
  2646. supe: "\u2287",
  2647. oplus: "\u2295",
  2648. otimes: "\u2297",
  2649. perp: "\u22A5",
  2650. sdot: "\u22C5",
  2651. lceil: "\u2308",
  2652. rceil: "\u2309",
  2653. lfloor: "\u230A",
  2654. rfloor: "\u230B",
  2655. lang: "\u2329",
  2656. rang: "\u232A",
  2657. loz: "\u25CA",
  2658. spades: "\u2660",
  2659. clubs: "\u2663",
  2660. hearts: "\u2665",
  2661. diams: "\u2666"
  2662. };
  2663. const HEX_NUMBER = /^[\da-fA-F]+$/;
  2664. const DECIMAL_NUMBER = /^\d+$/;
  2665. types$1.j_oTag = new TokContext("<tag", false);
  2666. types$1.j_cTag = new TokContext("</tag", false);
  2667. types$1.j_expr = new TokContext("<tag>...</tag>", true, true);
  2668. types.jsxName = new TokenType("jsxName");
  2669. types.jsxText = new TokenType("jsxText", {
  2670. beforeExpr: true
  2671. });
  2672. types.jsxTagStart = new TokenType("jsxTagStart", {
  2673. startsExpr: true
  2674. });
  2675. types.jsxTagEnd = new TokenType("jsxTagEnd");
  2676. types.jsxTagStart.updateContext = function () {
  2677. this.state.context.push(types$1.j_expr);
  2678. this.state.context.push(types$1.j_oTag);
  2679. this.state.exprAllowed = false;
  2680. };
  2681. types.jsxTagEnd.updateContext = function (prevType) {
  2682. const out = this.state.context.pop();
  2683. if (out === types$1.j_oTag && prevType === types.slash || out === types$1.j_cTag) {
  2684. this.state.context.pop();
  2685. this.state.exprAllowed = this.curContext() === types$1.j_expr;
  2686. } else {
  2687. this.state.exprAllowed = true;
  2688. }
  2689. };
  2690. function isFragment(object) {
  2691. return object ? object.type === "JSXOpeningFragment" || object.type === "JSXClosingFragment" : false;
  2692. }
  2693. function getQualifiedJSXName(object) {
  2694. if (object.type === "JSXIdentifier") {
  2695. return object.name;
  2696. }
  2697. if (object.type === "JSXNamespacedName") {
  2698. return object.namespace.name + ":" + object.name.name;
  2699. }
  2700. if (object.type === "JSXMemberExpression") {
  2701. return getQualifiedJSXName(object.object) + "." + getQualifiedJSXName(object.property);
  2702. }
  2703. throw new Error("Node had unexpected type: " + object.type);
  2704. }
  2705. var jsx = (superClass => class extends superClass {
  2706. jsxReadToken() {
  2707. let out = "";
  2708. let chunkStart = this.state.pos;
  2709. for (;;) {
  2710. if (this.state.pos >= this.length) {
  2711. this.raise(this.state.start, "Unterminated JSX contents");
  2712. }
  2713. const ch = this.input.charCodeAt(this.state.pos);
  2714. switch (ch) {
  2715. case 60:
  2716. case 123:
  2717. if (this.state.pos === this.state.start) {
  2718. if (ch === 60 && this.state.exprAllowed) {
  2719. ++this.state.pos;
  2720. return this.finishToken(types.jsxTagStart);
  2721. }
  2722. return super.getTokenFromCode(ch);
  2723. }
  2724. out += this.input.slice(chunkStart, this.state.pos);
  2725. return this.finishToken(types.jsxText, out);
  2726. case 38:
  2727. out += this.input.slice(chunkStart, this.state.pos);
  2728. out += this.jsxReadEntity();
  2729. chunkStart = this.state.pos;
  2730. break;
  2731. default:
  2732. if (isNewLine(ch)) {
  2733. out += this.input.slice(chunkStart, this.state.pos);
  2734. out += this.jsxReadNewLine(true);
  2735. chunkStart = this.state.pos;
  2736. } else {
  2737. ++this.state.pos;
  2738. }
  2739. }
  2740. }
  2741. }
  2742. jsxReadNewLine(normalizeCRLF) {
  2743. const ch = this.input.charCodeAt(this.state.pos);
  2744. let out;
  2745. ++this.state.pos;
  2746. if (ch === 13 && this.input.charCodeAt(this.state.pos) === 10) {
  2747. ++this.state.pos;
  2748. out = normalizeCRLF ? "\n" : "\r\n";
  2749. } else {
  2750. out = String.fromCharCode(ch);
  2751. }
  2752. ++this.state.curLine;
  2753. this.state.lineStart = this.state.pos;
  2754. return out;
  2755. }
  2756. jsxReadString(quote) {
  2757. let out = "";
  2758. let chunkStart = ++this.state.pos;
  2759. for (;;) {
  2760. if (this.state.pos >= this.length) {
  2761. this.raise(this.state.start, "Unterminated string constant");
  2762. }
  2763. const ch = this.input.charCodeAt(this.state.pos);
  2764. if (ch === quote) break;
  2765. if (ch === 38) {
  2766. out += this.input.slice(chunkStart, this.state.pos);
  2767. out += this.jsxReadEntity();
  2768. chunkStart = this.state.pos;
  2769. } else if (isNewLine(ch)) {
  2770. out += this.input.slice(chunkStart, this.state.pos);
  2771. out += this.jsxReadNewLine(false);
  2772. chunkStart = this.state.pos;
  2773. } else {
  2774. ++this.state.pos;
  2775. }
  2776. }
  2777. out += this.input.slice(chunkStart, this.state.pos++);
  2778. return this.finishToken(types.string, out);
  2779. }
  2780. jsxReadEntity() {
  2781. let str = "";
  2782. let count = 0;
  2783. let entity;
  2784. let ch = this.input[this.state.pos];
  2785. const startPos = ++this.state.pos;
  2786. while (this.state.pos < this.length && count++ < 10) {
  2787. ch = this.input[this.state.pos++];
  2788. if (ch === ";") {
  2789. if (str[0] === "#") {
  2790. if (str[1] === "x") {
  2791. str = str.substr(2);
  2792. if (HEX_NUMBER.test(str)) {
  2793. entity = String.fromCodePoint(parseInt(str, 16));
  2794. }
  2795. } else {
  2796. str = str.substr(1);
  2797. if (DECIMAL_NUMBER.test(str)) {
  2798. entity = String.fromCodePoint(parseInt(str, 10));
  2799. }
  2800. }
  2801. } else {
  2802. entity = entities[str];
  2803. }
  2804. break;
  2805. }
  2806. str += ch;
  2807. }
  2808. if (!entity) {
  2809. this.state.pos = startPos;
  2810. return "&";
  2811. }
  2812. return entity;
  2813. }
  2814. jsxReadWord() {
  2815. let ch;
  2816. const start = this.state.pos;
  2817. do {
  2818. ch = this.input.charCodeAt(++this.state.pos);
  2819. } while (isIdentifierChar(ch) || ch === 45);
  2820. return this.finishToken(types.jsxName, this.input.slice(start, this.state.pos));
  2821. }
  2822. jsxParseIdentifier() {
  2823. const node = this.startNode();
  2824. if (this.match(types.jsxName)) {
  2825. node.name = this.state.value;
  2826. } else if (this.state.type.keyword) {
  2827. node.name = this.state.type.keyword;
  2828. } else {
  2829. this.unexpected();
  2830. }
  2831. this.next();
  2832. return this.finishNode(node, "JSXIdentifier");
  2833. }
  2834. jsxParseNamespacedName() {
  2835. const startPos = this.state.start;
  2836. const startLoc = this.state.startLoc;
  2837. const name = this.jsxParseIdentifier();
  2838. if (!this.eat(types.colon)) return name;
  2839. const node = this.startNodeAt(startPos, startLoc);
  2840. node.namespace = name;
  2841. node.name = this.jsxParseIdentifier();
  2842. return this.finishNode(node, "JSXNamespacedName");
  2843. }
  2844. jsxParseElementName() {
  2845. const startPos = this.state.start;
  2846. const startLoc = this.state.startLoc;
  2847. let node = this.jsxParseNamespacedName();
  2848. while (this.eat(types.dot)) {
  2849. const newNode = this.startNodeAt(startPos, startLoc);
  2850. newNode.object = node;
  2851. newNode.property = this.jsxParseIdentifier();
  2852. node = this.finishNode(newNode, "JSXMemberExpression");
  2853. }
  2854. return node;
  2855. }
  2856. jsxParseAttributeValue() {
  2857. let node;
  2858. switch (this.state.type) {
  2859. case types.braceL:
  2860. node = this.startNode();
  2861. this.next();
  2862. node = this.jsxParseExpressionContainer(node);
  2863. if (node.expression.type === "JSXEmptyExpression") {
  2864. throw this.raise(node.start, "JSX attributes must only be assigned a non-empty expression");
  2865. } else {
  2866. return node;
  2867. }
  2868. case types.jsxTagStart:
  2869. case types.string:
  2870. return this.parseExprAtom();
  2871. default:
  2872. throw this.raise(this.state.start, "JSX value should be either an expression or a quoted JSX text");
  2873. }
  2874. }
  2875. jsxParseEmptyExpression() {
  2876. const node = this.startNodeAt(this.state.lastTokEnd, this.state.lastTokEndLoc);
  2877. return this.finishNodeAt(node, "JSXEmptyExpression", this.state.start, this.state.startLoc);
  2878. }
  2879. jsxParseSpreadChild(node) {
  2880. this.next();
  2881. node.expression = this.parseExpression();
  2882. this.expect(types.braceR);
  2883. return this.finishNode(node, "JSXSpreadChild");
  2884. }
  2885. jsxParseExpressionContainer(node) {
  2886. if (this.match(types.braceR)) {
  2887. node.expression = this.jsxParseEmptyExpression();
  2888. } else {
  2889. node.expression = this.parseExpression();
  2890. }
  2891. this.expect(types.braceR);
  2892. return this.finishNode(node, "JSXExpressionContainer");
  2893. }
  2894. jsxParseAttribute() {
  2895. const node = this.startNode();
  2896. if (this.eat(types.braceL)) {
  2897. this.expect(types.ellipsis);
  2898. node.argument = this.parseMaybeAssign();
  2899. this.expect(types.braceR);
  2900. return this.finishNode(node, "JSXSpreadAttribute");
  2901. }
  2902. node.name = this.jsxParseNamespacedName();
  2903. node.value = this.eat(types.eq) ? this.jsxParseAttributeValue() : null;
  2904. return this.finishNode(node, "JSXAttribute");
  2905. }
  2906. jsxParseOpeningElementAt(startPos, startLoc) {
  2907. const node = this.startNodeAt(startPos, startLoc);
  2908. if (this.match(types.jsxTagEnd)) {
  2909. this.expect(types.jsxTagEnd);
  2910. return this.finishNode(node, "JSXOpeningFragment");
  2911. }
  2912. node.name = this.jsxParseElementName();
  2913. return this.jsxParseOpeningElementAfterName(node);
  2914. }
  2915. jsxParseOpeningElementAfterName(node) {
  2916. const attributes = [];
  2917. while (!this.match(types.slash) && !this.match(types.jsxTagEnd)) {
  2918. attributes.push(this.jsxParseAttribute());
  2919. }
  2920. node.attributes = attributes;
  2921. node.selfClosing = this.eat(types.slash);
  2922. this.expect(types.jsxTagEnd);
  2923. return this.finishNode(node, "JSXOpeningElement");
  2924. }
  2925. jsxParseClosingElementAt(startPos, startLoc) {
  2926. const node = this.startNodeAt(startPos, startLoc);
  2927. if (this.match(types.jsxTagEnd)) {
  2928. this.expect(types.jsxTagEnd);
  2929. return this.finishNode(node, "JSXClosingFragment");
  2930. }
  2931. node.name = this.jsxParseElementName();
  2932. this.expect(types.jsxTagEnd);
  2933. return this.finishNode(node, "JSXClosingElement");
  2934. }
  2935. jsxParseElementAt(startPos, startLoc) {
  2936. const node = this.startNodeAt(startPos, startLoc);
  2937. const children = [];
  2938. const openingElement = this.jsxParseOpeningElementAt(startPos, startLoc);
  2939. let closingElement = null;
  2940. if (!openingElement.selfClosing) {
  2941. contents: for (;;) {
  2942. switch (this.state.type) {
  2943. case types.jsxTagStart:
  2944. startPos = this.state.start;
  2945. startLoc = this.state.startLoc;
  2946. this.next();
  2947. if (this.eat(types.slash)) {
  2948. closingElement = this.jsxParseClosingElementAt(startPos, startLoc);
  2949. break contents;
  2950. }
  2951. children.push(this.jsxParseElementAt(startPos, startLoc));
  2952. break;
  2953. case types.jsxText:
  2954. children.push(this.parseExprAtom());
  2955. break;
  2956. case types.braceL:
  2957. {
  2958. const node = this.startNode();
  2959. this.next();
  2960. if (this.match(types.ellipsis)) {
  2961. children.push(this.jsxParseSpreadChild(node));
  2962. } else {
  2963. children.push(this.jsxParseExpressionContainer(node));
  2964. }
  2965. break;
  2966. }
  2967. default:
  2968. throw this.unexpected();
  2969. }
  2970. }
  2971. if (isFragment(openingElement) && !isFragment(closingElement)) {
  2972. this.raise(closingElement.start, "Expected corresponding JSX closing tag for <>");
  2973. } else if (!isFragment(openingElement) && isFragment(closingElement)) {
  2974. this.raise(closingElement.start, "Expected corresponding JSX closing tag for <" + getQualifiedJSXName(openingElement.name) + ">");
  2975. } else if (!isFragment(openingElement) && !isFragment(closingElement)) {
  2976. if (getQualifiedJSXName(closingElement.name) !== getQualifiedJSXName(openingElement.name)) {
  2977. this.raise(closingElement.start, "Expected corresponding JSX closing tag for <" + getQualifiedJSXName(openingElement.name) + ">");
  2978. }
  2979. }
  2980. }
  2981. if (isFragment(openingElement)) {
  2982. node.openingFragment = openingElement;
  2983. node.closingFragment = closingElement;
  2984. } else {
  2985. node.openingElement = openingElement;
  2986. node.closingElement = closingElement;
  2987. }
  2988. node.children = children;
  2989. if (this.match(types.relational) && this.state.value === "<") {
  2990. this.raise(this.state.start, "Adjacent JSX elements must be wrapped in an enclosing tag. " + "Did you want a JSX fragment <>...</>?");
  2991. }
  2992. return isFragment(openingElement) ? this.finishNode(node, "JSXFragment") : this.finishNode(node, "JSXElement");
  2993. }
  2994. jsxParseElement() {
  2995. const startPos = this.state.start;
  2996. const startLoc = this.state.startLoc;
  2997. this.next();
  2998. return this.jsxParseElementAt(startPos, startLoc);
  2999. }
  3000. parseExprAtom(refShortHandDefaultPos) {
  3001. if (this.match(types.jsxText)) {
  3002. return this.parseLiteral(this.state.value, "JSXText");
  3003. } else if (this.match(types.jsxTagStart)) {
  3004. return this.jsxParseElement();
  3005. } else if (this.isRelational("<") && this.input.charCodeAt(this.state.pos) !== 33) {
  3006. this.finishToken(types.jsxTagStart);
  3007. return this.jsxParseElement();
  3008. } else {
  3009. return super.parseExprAtom(refShortHandDefaultPos);
  3010. }
  3011. }
  3012. getTokenFromCode(code) {
  3013. if (this.state.inPropertyName) return super.getTokenFromCode(code);
  3014. const context = this.curContext();
  3015. if (context === types$1.j_expr) {
  3016. return this.jsxReadToken();
  3017. }
  3018. if (context === types$1.j_oTag || context === types$1.j_cTag) {
  3019. if (isIdentifierStart(code)) {
  3020. return this.jsxReadWord();
  3021. }
  3022. if (code === 62) {
  3023. ++this.state.pos;
  3024. return this.finishToken(types.jsxTagEnd);
  3025. }
  3026. if ((code === 34 || code === 39) && context === types$1.j_oTag) {
  3027. return this.jsxReadString(code);
  3028. }
  3029. }
  3030. if (code === 60 && this.state.exprAllowed && this.input.charCodeAt(this.state.pos + 1) !== 33) {
  3031. ++this.state.pos;
  3032. return this.finishToken(types.jsxTagStart);
  3033. }
  3034. return super.getTokenFromCode(code);
  3035. }
  3036. updateContext(prevType) {
  3037. if (this.match(types.braceL)) {
  3038. const curContext = this.curContext();
  3039. if (curContext === types$1.j_oTag) {
  3040. this.state.context.push(types$1.braceExpression);
  3041. } else if (curContext === types$1.j_expr) {
  3042. this.state.context.push(types$1.templateQuasi);
  3043. } else {
  3044. super.updateContext(prevType);
  3045. }
  3046. this.state.exprAllowed = true;
  3047. } else if (this.match(types.slash) && prevType === types.jsxTagStart) {
  3048. this.state.context.length -= 2;
  3049. this.state.context.push(types$1.j_cTag);
  3050. this.state.exprAllowed = false;
  3051. } else {
  3052. return super.updateContext(prevType);
  3053. }
  3054. }
  3055. });
  3056. class Scope {
  3057. constructor(flags) {
  3058. this.var = [];
  3059. this.lexical = [];
  3060. this.functions = [];
  3061. this.flags = flags;
  3062. }
  3063. }
  3064. class ScopeHandler {
  3065. constructor(raise, inModule) {
  3066. this.scopeStack = [];
  3067. this.undefinedExports = new Map();
  3068. this.raise = raise;
  3069. this.inModule = inModule;
  3070. }
  3071. get inFunction() {
  3072. return (this.currentVarScope().flags & SCOPE_FUNCTION) > 0;
  3073. }
  3074. get inGenerator() {
  3075. return (this.currentVarScope().flags & SCOPE_GENERATOR) > 0;
  3076. }
  3077. get inAsync() {
  3078. return (this.currentVarScope().flags & SCOPE_ASYNC) > 0;
  3079. }
  3080. get allowSuper() {
  3081. return (this.currentThisScope().flags & SCOPE_SUPER) > 0;
  3082. }
  3083. get allowDirectSuper() {
  3084. return (this.currentThisScope().flags & SCOPE_DIRECT_SUPER) > 0;
  3085. }
  3086. get inNonArrowFunction() {
  3087. return (this.currentThisScope().flags & SCOPE_FUNCTION) > 0;
  3088. }
  3089. get treatFunctionsAsVar() {
  3090. return this.treatFunctionsAsVarInScope(this.currentScope());
  3091. }
  3092. createScope(flags) {
  3093. return new Scope(flags);
  3094. }
  3095. enter(flags) {
  3096. this.scopeStack.push(this.createScope(flags));
  3097. }
  3098. exit() {
  3099. this.scopeStack.pop();
  3100. }
  3101. treatFunctionsAsVarInScope(scope) {
  3102. return !!(scope.flags & SCOPE_FUNCTION || !this.inModule && scope.flags & SCOPE_PROGRAM);
  3103. }
  3104. declareName(name, bindingType, pos) {
  3105. let scope = this.currentScope();
  3106. if (bindingType & BIND_SCOPE_LEXICAL || bindingType & BIND_SCOPE_FUNCTION) {
  3107. this.checkRedeclarationInScope(scope, name, bindingType, pos);
  3108. if (bindingType & BIND_SCOPE_FUNCTION) {
  3109. scope.functions.push(name);
  3110. } else {
  3111. scope.lexical.push(name);
  3112. }
  3113. if (bindingType & BIND_SCOPE_LEXICAL) {
  3114. this.maybeExportDefined(scope, name);
  3115. }
  3116. } else if (bindingType & BIND_SCOPE_VAR) {
  3117. for (let i = this.scopeStack.length - 1; i >= 0; --i) {
  3118. scope = this.scopeStack[i];
  3119. this.checkRedeclarationInScope(scope, name, bindingType, pos);
  3120. scope.var.push(name);
  3121. this.maybeExportDefined(scope, name);
  3122. if (scope.flags & SCOPE_VAR) break;
  3123. }
  3124. }
  3125. if (this.inModule && scope.flags & SCOPE_PROGRAM) {
  3126. this.undefinedExports.delete(name);
  3127. }
  3128. }
  3129. maybeExportDefined(scope, name) {
  3130. if (this.inModule && scope.flags & SCOPE_PROGRAM) {
  3131. this.undefinedExports.delete(name);
  3132. }
  3133. }
  3134. checkRedeclarationInScope(scope, name, bindingType, pos) {
  3135. if (this.isRedeclaredInScope(scope, name, bindingType)) {
  3136. this.raise(pos, `Identifier '${name}' has already been declared`);
  3137. }
  3138. }
  3139. isRedeclaredInScope(scope, name, bindingType) {
  3140. if (!(bindingType & BIND_KIND_VALUE)) return false;
  3141. if (bindingType & BIND_SCOPE_LEXICAL) {
  3142. return scope.lexical.indexOf(name) > -1 || scope.functions.indexOf(name) > -1 || scope.var.indexOf(name) > -1;
  3143. }
  3144. if (bindingType & BIND_SCOPE_FUNCTION) {
  3145. return scope.lexical.indexOf(name) > -1 || !this.treatFunctionsAsVarInScope(scope) && scope.var.indexOf(name) > -1;
  3146. }
  3147. return scope.lexical.indexOf(name) > -1 && !(scope.flags & SCOPE_SIMPLE_CATCH && scope.lexical[0] === name) || !this.treatFunctionsAsVarInScope(scope) && scope.functions.indexOf(name) > -1;
  3148. }
  3149. checkLocalExport(id) {
  3150. if (this.scopeStack[0].lexical.indexOf(id.name) === -1 && this.scopeStack[0].var.indexOf(id.name) === -1 && this.scopeStack[0].functions.indexOf(id.name) === -1) {
  3151. this.undefinedExports.set(id.name, id.start);
  3152. }
  3153. }
  3154. currentScope() {
  3155. return this.scopeStack[this.scopeStack.length - 1];
  3156. }
  3157. currentVarScope() {
  3158. for (let i = this.scopeStack.length - 1;; i--) {
  3159. const scope = this.scopeStack[i];
  3160. if (scope.flags & SCOPE_VAR) {
  3161. return scope;
  3162. }
  3163. }
  3164. }
  3165. currentThisScope() {
  3166. for (let i = this.scopeStack.length - 1;; i--) {
  3167. const scope = this.scopeStack[i];
  3168. if ((scope.flags & SCOPE_VAR || scope.flags & SCOPE_CLASS) && !(scope.flags & SCOPE_ARROW)) {
  3169. return scope;
  3170. }
  3171. }
  3172. }
  3173. }
  3174. class TypeScriptScope extends Scope {
  3175. constructor(...args) {
  3176. super(...args);
  3177. this.types = [];
  3178. this.enums = [];
  3179. this.constEnums = [];
  3180. this.classes = [];
  3181. this.exportOnlyBindings = [];
  3182. }
  3183. }
  3184. class TypeScriptScopeHandler extends ScopeHandler {
  3185. createScope(flags) {
  3186. return new TypeScriptScope(flags);
  3187. }
  3188. declareName(name, bindingType, pos) {
  3189. const scope = this.currentScope();
  3190. if (bindingType & BIND_FLAGS_TS_EXPORT_ONLY) {
  3191. this.maybeExportDefined(scope, name);
  3192. scope.exportOnlyBindings.push(name);
  3193. return;
  3194. }
  3195. super.declareName(...arguments);
  3196. if (bindingType & BIND_KIND_TYPE) {
  3197. if (!(bindingType & BIND_KIND_VALUE)) {
  3198. this.checkRedeclarationInScope(scope, name, bindingType, pos);
  3199. this.maybeExportDefined(scope, name);
  3200. }
  3201. scope.types.push(name);
  3202. }
  3203. if (bindingType & BIND_FLAGS_TS_ENUM) scope.enums.push(name);
  3204. if (bindingType & BIND_FLAGS_TS_CONST_ENUM) scope.constEnums.push(name);
  3205. if (bindingType & BIND_FLAGS_CLASS) scope.classes.push(name);
  3206. }
  3207. isRedeclaredInScope(scope, name, bindingType) {
  3208. if (scope.enums.indexOf(name) > -1) {
  3209. if (bindingType & BIND_FLAGS_TS_ENUM) {
  3210. const isConst = !!(bindingType & BIND_FLAGS_TS_CONST_ENUM);
  3211. const wasConst = scope.constEnums.indexOf(name) > -1;
  3212. return isConst !== wasConst;
  3213. }
  3214. return true;
  3215. }
  3216. if (bindingType & BIND_FLAGS_CLASS && scope.classes.indexOf(name) > -1) {
  3217. if (scope.lexical.indexOf(name) > -1) {
  3218. return !!(bindingType & BIND_KIND_VALUE);
  3219. } else {
  3220. return false;
  3221. }
  3222. }
  3223. if (bindingType & BIND_KIND_TYPE && scope.types.indexOf(name) > -1) {
  3224. return true;
  3225. }
  3226. return super.isRedeclaredInScope(...arguments);
  3227. }
  3228. checkLocalExport(id) {
  3229. if (this.scopeStack[0].types.indexOf(id.name) === -1 && this.scopeStack[0].exportOnlyBindings.indexOf(id.name) === -1) {
  3230. super.checkLocalExport(id);
  3231. }
  3232. }
  3233. }
  3234. function nonNull(x) {
  3235. if (x == null) {
  3236. throw new Error(`Unexpected ${x} value.`);
  3237. }
  3238. return x;
  3239. }
  3240. function assert(x) {
  3241. if (!x) {
  3242. throw new Error("Assert fail");
  3243. }
  3244. }
  3245. function keywordTypeFromName(value) {
  3246. switch (value) {
  3247. case "any":
  3248. return "TSAnyKeyword";
  3249. case "boolean":
  3250. return "TSBooleanKeyword";
  3251. case "bigint":
  3252. return "TSBigIntKeyword";
  3253. case "never":
  3254. return "TSNeverKeyword";
  3255. case "number":
  3256. return "TSNumberKeyword";
  3257. case "object":
  3258. return "TSObjectKeyword";
  3259. case "string":
  3260. return "TSStringKeyword";
  3261. case "symbol":
  3262. return "TSSymbolKeyword";
  3263. case "undefined":
  3264. return "TSUndefinedKeyword";
  3265. case "unknown":
  3266. return "TSUnknownKeyword";
  3267. default:
  3268. return undefined;
  3269. }
  3270. }
  3271. var typescript = (superClass => class extends superClass {
  3272. getScopeHandler() {
  3273. return TypeScriptScopeHandler;
  3274. }
  3275. tsIsIdentifier() {
  3276. return this.match(types.name);
  3277. }
  3278. tsNextTokenCanFollowModifier() {
  3279. this.next();
  3280. return !this.hasPrecedingLineBreak() && !this.match(types.parenL) && !this.match(types.parenR) && !this.match(types.colon) && !this.match(types.eq) && !this.match(types.question) && !this.match(types.bang);
  3281. }
  3282. tsParseModifier(allowedModifiers) {
  3283. if (!this.match(types.name)) {
  3284. return undefined;
  3285. }
  3286. const modifier = this.state.value;
  3287. if (allowedModifiers.indexOf(modifier) !== -1 && this.tsTryParse(this.tsNextTokenCanFollowModifier.bind(this))) {
  3288. return modifier;
  3289. }
  3290. return undefined;
  3291. }
  3292. tsIsListTerminator(kind) {
  3293. switch (kind) {
  3294. case "EnumMembers":
  3295. case "TypeMembers":
  3296. return this.match(types.braceR);
  3297. case "HeritageClauseElement":
  3298. return this.match(types.braceL);
  3299. case "TupleElementTypes":
  3300. return this.match(types.bracketR);
  3301. case "TypeParametersOrArguments":
  3302. return this.isRelational(">");
  3303. }
  3304. throw new Error("Unreachable");
  3305. }
  3306. tsParseList(kind, parseElement) {
  3307. const result = [];
  3308. while (!this.tsIsListTerminator(kind)) {
  3309. result.push(parseElement());
  3310. }
  3311. return result;
  3312. }
  3313. tsParseDelimitedList(kind, parseElement) {
  3314. return nonNull(this.tsParseDelimitedListWorker(kind, parseElement, true));
  3315. }
  3316. tsTryParseDelimitedList(kind, parseElement) {
  3317. return this.tsParseDelimitedListWorker(kind, parseElement, false);
  3318. }
  3319. tsParseDelimitedListWorker(kind, parseElement, expectSuccess) {
  3320. const result = [];
  3321. while (true) {
  3322. if (this.tsIsListTerminator(kind)) {
  3323. break;
  3324. }
  3325. const element = parseElement();
  3326. if (element == null) {
  3327. return undefined;
  3328. }
  3329. result.push(element);
  3330. if (this.eat(types.comma)) {
  3331. continue;
  3332. }
  3333. if (this.tsIsListTerminator(kind)) {
  3334. break;
  3335. }
  3336. if (expectSuccess) {
  3337. this.expect(types.comma);
  3338. }
  3339. return undefined;
  3340. }
  3341. return result;
  3342. }
  3343. tsParseBracketedList(kind, parseElement, bracket, skipFirstToken) {
  3344. if (!skipFirstToken) {
  3345. if (bracket) {
  3346. this.expect(types.bracketL);
  3347. } else {
  3348. this.expectRelational("<");
  3349. }
  3350. }
  3351. const result = this.tsParseDelimitedList(kind, parseElement);
  3352. if (bracket) {
  3353. this.expect(types.bracketR);
  3354. } else {
  3355. this.expectRelational(">");
  3356. }
  3357. return result;
  3358. }
  3359. tsParseImportType() {
  3360. const node = this.startNode();
  3361. this.expect(types._import);
  3362. this.expect(types.parenL);
  3363. if (!this.match(types.string)) {
  3364. throw this.unexpected(null, "Argument in a type import must be a string literal");
  3365. }
  3366. node.argument = this.parseExprAtom();
  3367. this.expect(types.parenR);
  3368. if (this.eat(types.dot)) {
  3369. node.qualifier = this.tsParseEntityName(true);
  3370. }
  3371. if (this.isRelational("<")) {
  3372. node.typeParameters = this.tsParseTypeArguments();
  3373. }
  3374. return this.finishNode(node, "TSImportType");
  3375. }
  3376. tsParseEntityName(allowReservedWords) {
  3377. let entity = this.parseIdentifier();
  3378. while (this.eat(types.dot)) {
  3379. const node = this.startNodeAtNode(entity);
  3380. node.left = entity;
  3381. node.right = this.parseIdentifier(allowReservedWords);
  3382. entity = this.finishNode(node, "TSQualifiedName");
  3383. }
  3384. return entity;
  3385. }
  3386. tsParseTypeReference() {
  3387. const node = this.startNode();
  3388. node.typeName = this.tsParseEntityName(false);
  3389. if (!this.hasPrecedingLineBreak() && this.isRelational("<")) {
  3390. node.typeParameters = this.tsParseTypeArguments();
  3391. }
  3392. return this.finishNode(node, "TSTypeReference");
  3393. }
  3394. tsParseThisTypePredicate(lhs) {
  3395. this.next();
  3396. const node = this.startNodeAtNode(lhs);
  3397. node.parameterName = lhs;
  3398. node.typeAnnotation = this.tsParseTypeAnnotation(false);
  3399. return this.finishNode(node, "TSTypePredicate");
  3400. }
  3401. tsParseThisTypeNode() {
  3402. const node = this.startNode();
  3403. this.next();
  3404. return this.finishNode(node, "TSThisType");
  3405. }
  3406. tsParseTypeQuery() {
  3407. const node = this.startNode();
  3408. this.expect(types._typeof);
  3409. if (this.match(types._import)) {
  3410. node.exprName = this.tsParseImportType();
  3411. } else {
  3412. node.exprName = this.tsParseEntityName(true);
  3413. }
  3414. return this.finishNode(node, "TSTypeQuery");
  3415. }
  3416. tsParseTypeParameter() {
  3417. const node = this.startNode();
  3418. node.name = this.parseIdentifierName(node.start);
  3419. node.constraint = this.tsEatThenParseType(types._extends);
  3420. node.default = this.tsEatThenParseType(types.eq);
  3421. return this.finishNode(node, "TSTypeParameter");
  3422. }
  3423. tsTryParseTypeParameters() {
  3424. if (this.isRelational("<")) {
  3425. return this.tsParseTypeParameters();
  3426. }
  3427. }
  3428. tsParseTypeParameters() {
  3429. const node = this.startNode();
  3430. if (this.isRelational("<") || this.match(types.jsxTagStart)) {
  3431. this.next();
  3432. } else {
  3433. this.unexpected();
  3434. }
  3435. node.params = this.tsParseBracketedList("TypeParametersOrArguments", this.tsParseTypeParameter.bind(this), false, true);
  3436. return this.finishNode(node, "TSTypeParameterDeclaration");
  3437. }
  3438. tsTryNextParseConstantContext() {
  3439. if (this.lookahead().type === types._const) {
  3440. this.next();
  3441. return this.tsParseTypeReference();
  3442. }
  3443. return null;
  3444. }
  3445. tsFillSignature(returnToken, signature) {
  3446. const returnTokenRequired = returnToken === types.arrow;
  3447. signature.typeParameters = this.tsTryParseTypeParameters();
  3448. this.expect(types.parenL);
  3449. signature.parameters = this.tsParseBindingListForSignature();
  3450. if (returnTokenRequired) {
  3451. signature.typeAnnotation = this.tsParseTypeOrTypePredicateAnnotation(returnToken);
  3452. } else if (this.match(returnToken)) {
  3453. signature.typeAnnotation = this.tsParseTypeOrTypePredicateAnnotation(returnToken);
  3454. }
  3455. }
  3456. tsParseBindingListForSignature() {
  3457. return this.parseBindingList(types.parenR).map(pattern => {
  3458. if (pattern.type !== "Identifier" && pattern.type !== "RestElement" && pattern.type !== "ObjectPattern" && pattern.type !== "ArrayPattern") {
  3459. throw this.unexpected(pattern.start, `Name in a signature must be an Identifier, ObjectPattern or ArrayPattern, instead got ${pattern.type}`);
  3460. }
  3461. return pattern;
  3462. });
  3463. }
  3464. tsParseTypeMemberSemicolon() {
  3465. if (!this.eat(types.comma)) {
  3466. this.semicolon();
  3467. }
  3468. }
  3469. tsParseSignatureMember(kind, node) {
  3470. this.tsFillSignature(types.colon, node);
  3471. this.tsParseTypeMemberSemicolon();
  3472. return this.finishNode(node, kind);
  3473. }
  3474. tsIsUnambiguouslyIndexSignature() {
  3475. this.next();
  3476. return this.eat(types.name) && this.match(types.colon);
  3477. }
  3478. tsTryParseIndexSignature(node) {
  3479. if (!(this.match(types.bracketL) && this.tsLookAhead(this.tsIsUnambiguouslyIndexSignature.bind(this)))) {
  3480. return undefined;
  3481. }
  3482. this.expect(types.bracketL);
  3483. const id = this.parseIdentifier();
  3484. id.typeAnnotation = this.tsParseTypeAnnotation();
  3485. this.resetEndLocation(id);
  3486. this.expect(types.bracketR);
  3487. node.parameters = [id];
  3488. const type = this.tsTryParseTypeAnnotation();
  3489. if (type) node.typeAnnotation = type;
  3490. this.tsParseTypeMemberSemicolon();
  3491. return this.finishNode(node, "TSIndexSignature");
  3492. }
  3493. tsParsePropertyOrMethodSignature(node, readonly) {
  3494. if (this.eat(types.question)) node.optional = true;
  3495. const nodeAny = node;
  3496. if (!readonly && (this.match(types.parenL) || this.isRelational("<"))) {
  3497. const method = nodeAny;
  3498. this.tsFillSignature(types.colon, method);
  3499. this.tsParseTypeMemberSemicolon();
  3500. return this.finishNode(method, "TSMethodSignature");
  3501. } else {
  3502. const property = nodeAny;
  3503. if (readonly) property.readonly = true;
  3504. const type = this.tsTryParseTypeAnnotation();
  3505. if (type) property.typeAnnotation = type;
  3506. this.tsParseTypeMemberSemicolon();
  3507. return this.finishNode(property, "TSPropertySignature");
  3508. }
  3509. }
  3510. tsParseTypeMember() {
  3511. const node = this.startNode();
  3512. if (this.match(types.parenL) || this.isRelational("<")) {
  3513. return this.tsParseSignatureMember("TSCallSignatureDeclaration", node);
  3514. }
  3515. if (this.match(types._new)) {
  3516. const id = this.startNode();
  3517. this.next();
  3518. if (this.match(types.parenL) || this.isRelational("<")) {
  3519. return this.tsParseSignatureMember("TSConstructSignatureDeclaration", node);
  3520. } else {
  3521. node.key = this.createIdentifier(id, "new");
  3522. return this.tsParsePropertyOrMethodSignature(node, false);
  3523. }
  3524. }
  3525. const readonly = !!this.tsParseModifier(["readonly"]);
  3526. const idx = this.tsTryParseIndexSignature(node);
  3527. if (idx) {
  3528. if (readonly) node.readonly = true;
  3529. return idx;
  3530. }
  3531. this.parsePropertyName(node);
  3532. return this.tsParsePropertyOrMethodSignature(node, readonly);
  3533. }
  3534. tsParseTypeLiteral() {
  3535. const node = this.startNode();
  3536. node.members = this.tsParseObjectTypeMembers();
  3537. return this.finishNode(node, "TSTypeLiteral");
  3538. }
  3539. tsParseObjectTypeMembers() {
  3540. this.expect(types.braceL);
  3541. const members = this.tsParseList("TypeMembers", this.tsParseTypeMember.bind(this));
  3542. this.expect(types.braceR);
  3543. return members;
  3544. }
  3545. tsIsStartOfMappedType() {
  3546. this.next();
  3547. if (this.eat(types.plusMin)) {
  3548. return this.isContextual("readonly");
  3549. }
  3550. if (this.isContextual("readonly")) {
  3551. this.next();
  3552. }
  3553. if (!this.match(types.bracketL)) {
  3554. return false;
  3555. }
  3556. this.next();
  3557. if (!this.tsIsIdentifier()) {
  3558. return false;
  3559. }
  3560. this.next();
  3561. return this.match(types._in);
  3562. }
  3563. tsParseMappedTypeParameter() {
  3564. const node = this.startNode();
  3565. node.name = this.parseIdentifierName(node.start);
  3566. node.constraint = this.tsExpectThenParseType(types._in);
  3567. return this.finishNode(node, "TSTypeParameter");
  3568. }
  3569. tsParseMappedType() {
  3570. const node = this.startNode();
  3571. this.expect(types.braceL);
  3572. if (this.match(types.plusMin)) {
  3573. node.readonly = this.state.value;
  3574. this.next();
  3575. this.expectContextual("readonly");
  3576. } else if (this.eatContextual("readonly")) {
  3577. node.readonly = true;
  3578. }
  3579. this.expect(types.bracketL);
  3580. node.typeParameter = this.tsParseMappedTypeParameter();
  3581. this.expect(types.bracketR);
  3582. if (this.match(types.plusMin)) {
  3583. node.optional = this.state.value;
  3584. this.next();
  3585. this.expect(types.question);
  3586. } else if (this.eat(types.question)) {
  3587. node.optional = true;
  3588. }
  3589. node.typeAnnotation = this.tsTryParseType();
  3590. this.semicolon();
  3591. this.expect(types.braceR);
  3592. return this.finishNode(node, "TSMappedType");
  3593. }
  3594. tsParseTupleType() {
  3595. const node = this.startNode();
  3596. node.elementTypes = this.tsParseBracketedList("TupleElementTypes", this.tsParseTupleElementType.bind(this), true, false);
  3597. let seenOptionalElement = false;
  3598. node.elementTypes.forEach(elementNode => {
  3599. if (elementNode.type === "TSOptionalType") {
  3600. seenOptionalElement = true;
  3601. } else if (seenOptionalElement && elementNode.type !== "TSRestType") {
  3602. this.raise(elementNode.start, "A required element cannot follow an optional element.");
  3603. }
  3604. });
  3605. return this.finishNode(node, "TSTupleType");
  3606. }
  3607. tsParseTupleElementType() {
  3608. if (this.match(types.ellipsis)) {
  3609. const restNode = this.startNode();
  3610. this.next();
  3611. restNode.typeAnnotation = this.tsParseType();
  3612. this.checkCommaAfterRest();
  3613. return this.finishNode(restNode, "TSRestType");
  3614. }
  3615. const type = this.tsParseType();
  3616. if (this.eat(types.question)) {
  3617. const optionalTypeNode = this.startNodeAtNode(type);
  3618. optionalTypeNode.typeAnnotation = type;
  3619. return this.finishNode(optionalTypeNode, "TSOptionalType");
  3620. }
  3621. return type;
  3622. }
  3623. tsParseParenthesizedType() {
  3624. const node = this.startNode();
  3625. this.expect(types.parenL);
  3626. node.typeAnnotation = this.tsParseType();
  3627. this.expect(types.parenR);
  3628. return this.finishNode(node, "TSParenthesizedType");
  3629. }
  3630. tsParseFunctionOrConstructorType(type) {
  3631. const node = this.startNode();
  3632. if (type === "TSConstructorType") {
  3633. this.expect(types._new);
  3634. }
  3635. this.tsFillSignature(types.arrow, node);
  3636. return this.finishNode(node, type);
  3637. }
  3638. tsParseLiteralTypeNode() {
  3639. const node = this.startNode();
  3640. node.literal = (() => {
  3641. switch (this.state.type) {
  3642. case types.num:
  3643. case types.string:
  3644. case types._true:
  3645. case types._false:
  3646. return this.parseExprAtom();
  3647. default:
  3648. throw this.unexpected();
  3649. }
  3650. })();
  3651. return this.finishNode(node, "TSLiteralType");
  3652. }
  3653. tsParseTemplateLiteralType() {
  3654. const node = this.startNode();
  3655. const templateNode = this.parseTemplate(false);
  3656. if (templateNode.expressions.length > 0) {
  3657. throw this.raise(templateNode.expressions[0].start, "Template literal types cannot have any substitution");
  3658. }
  3659. node.literal = templateNode;
  3660. return this.finishNode(node, "TSLiteralType");
  3661. }
  3662. tsParseNonArrayType() {
  3663. switch (this.state.type) {
  3664. case types.name:
  3665. case types._void:
  3666. case types._null:
  3667. {
  3668. const type = this.match(types._void) ? "TSVoidKeyword" : this.match(types._null) ? "TSNullKeyword" : keywordTypeFromName(this.state.value);
  3669. if (type !== undefined && this.lookahead().type !== types.dot) {
  3670. const node = this.startNode();
  3671. this.next();
  3672. return this.finishNode(node, type);
  3673. }
  3674. return this.tsParseTypeReference();
  3675. }
  3676. case types.string:
  3677. case types.num:
  3678. case types._true:
  3679. case types._false:
  3680. return this.tsParseLiteralTypeNode();
  3681. case types.plusMin:
  3682. if (this.state.value === "-") {
  3683. const node = this.startNode();
  3684. if (this.lookahead().type !== types.num) {
  3685. throw this.unexpected();
  3686. }
  3687. node.literal = this.parseMaybeUnary();
  3688. return this.finishNode(node, "TSLiteralType");
  3689. }
  3690. break;
  3691. case types._this:
  3692. {
  3693. const thisKeyword = this.tsParseThisTypeNode();
  3694. if (this.isContextual("is") && !this.hasPrecedingLineBreak()) {
  3695. return this.tsParseThisTypePredicate(thisKeyword);
  3696. } else {
  3697. return thisKeyword;
  3698. }
  3699. }
  3700. case types._typeof:
  3701. return this.tsParseTypeQuery();
  3702. case types._import:
  3703. return this.tsParseImportType();
  3704. case types.braceL:
  3705. return this.tsLookAhead(this.tsIsStartOfMappedType.bind(this)) ? this.tsParseMappedType() : this.tsParseTypeLiteral();
  3706. case types.bracketL:
  3707. return this.tsParseTupleType();
  3708. case types.parenL:
  3709. return this.tsParseParenthesizedType();
  3710. case types.backQuote:
  3711. return this.tsParseTemplateLiteralType();
  3712. }
  3713. throw this.unexpected();
  3714. }
  3715. tsParseArrayTypeOrHigher() {
  3716. let type = this.tsParseNonArrayType();
  3717. while (!this.hasPrecedingLineBreak() && this.eat(types.bracketL)) {
  3718. if (this.match(types.bracketR)) {
  3719. const node = this.startNodeAtNode(type);
  3720. node.elementType = type;
  3721. this.expect(types.bracketR);
  3722. type = this.finishNode(node, "TSArrayType");
  3723. } else {
  3724. const node = this.startNodeAtNode(type);
  3725. node.objectType = type;
  3726. node.indexType = this.tsParseType();
  3727. this.expect(types.bracketR);
  3728. type = this.finishNode(node, "TSIndexedAccessType");
  3729. }
  3730. }
  3731. return type;
  3732. }
  3733. tsParseTypeOperator(operator) {
  3734. const node = this.startNode();
  3735. this.expectContextual(operator);
  3736. node.operator = operator;
  3737. node.typeAnnotation = this.tsParseTypeOperatorOrHigher();
  3738. if (operator === "readonly") {
  3739. this.tsCheckTypeAnnotationForReadOnly(node);
  3740. }
  3741. return this.finishNode(node, "TSTypeOperator");
  3742. }
  3743. tsCheckTypeAnnotationForReadOnly(node) {
  3744. switch (node.typeAnnotation.type) {
  3745. case "TSTupleType":
  3746. case "TSArrayType":
  3747. return;
  3748. default:
  3749. this.raise(node.start, "'readonly' type modifier is only permitted on array and tuple literal types.");
  3750. }
  3751. }
  3752. tsParseInferType() {
  3753. const node = this.startNode();
  3754. this.expectContextual("infer");
  3755. const typeParameter = this.startNode();
  3756. typeParameter.name = this.parseIdentifierName(typeParameter.start);
  3757. node.typeParameter = this.finishNode(typeParameter, "TSTypeParameter");
  3758. return this.finishNode(node, "TSInferType");
  3759. }
  3760. tsParseTypeOperatorOrHigher() {
  3761. const operator = ["keyof", "unique", "readonly"].find(kw => this.isContextual(kw));
  3762. return operator ? this.tsParseTypeOperator(operator) : this.isContextual("infer") ? this.tsParseInferType() : this.tsParseArrayTypeOrHigher();
  3763. }
  3764. tsParseUnionOrIntersectionType(kind, parseConstituentType, operator) {
  3765. this.eat(operator);
  3766. let type = parseConstituentType();
  3767. if (this.match(operator)) {
  3768. const types = [type];
  3769. while (this.eat(operator)) {
  3770. types.push(parseConstituentType());
  3771. }
  3772. const node = this.startNodeAtNode(type);
  3773. node.types = types;
  3774. type = this.finishNode(node, kind);
  3775. }
  3776. return type;
  3777. }
  3778. tsParseIntersectionTypeOrHigher() {
  3779. return this.tsParseUnionOrIntersectionType("TSIntersectionType", this.tsParseTypeOperatorOrHigher.bind(this), types.bitwiseAND);
  3780. }
  3781. tsParseUnionTypeOrHigher() {
  3782. return this.tsParseUnionOrIntersectionType("TSUnionType", this.tsParseIntersectionTypeOrHigher.bind(this), types.bitwiseOR);
  3783. }
  3784. tsIsStartOfFunctionType() {
  3785. if (this.isRelational("<")) {
  3786. return true;
  3787. }
  3788. return this.match(types.parenL) && this.tsLookAhead(this.tsIsUnambiguouslyStartOfFunctionType.bind(this));
  3789. }
  3790. tsSkipParameterStart() {
  3791. if (this.match(types.name) || this.match(types._this)) {
  3792. this.next();
  3793. return true;
  3794. }
  3795. if (this.match(types.braceL)) {
  3796. let braceStackCounter = 1;
  3797. this.next();
  3798. while (braceStackCounter > 0) {
  3799. if (this.match(types.braceL)) {
  3800. ++braceStackCounter;
  3801. } else if (this.match(types.braceR)) {
  3802. --braceStackCounter;
  3803. }
  3804. this.next();
  3805. }
  3806. return true;
  3807. }
  3808. if (this.match(types.bracketL)) {
  3809. let braceStackCounter = 1;
  3810. this.next();
  3811. while (braceStackCounter > 0) {
  3812. if (this.match(types.bracketL)) {
  3813. ++braceStackCounter;
  3814. } else if (this.match(types.bracketR)) {
  3815. --braceStackCounter;
  3816. }
  3817. this.next();
  3818. }
  3819. return true;
  3820. }
  3821. return false;
  3822. }
  3823. tsIsUnambiguouslyStartOfFunctionType() {
  3824. this.next();
  3825. if (this.match(types.parenR) || this.match(types.ellipsis)) {
  3826. return true;
  3827. }
  3828. if (this.tsSkipParameterStart()) {
  3829. if (this.match(types.colon) || this.match(types.comma) || this.match(types.question) || this.match(types.eq)) {
  3830. return true;
  3831. }
  3832. if (this.match(types.parenR)) {
  3833. this.next();
  3834. if (this.match(types.arrow)) {
  3835. return true;
  3836. }
  3837. }
  3838. }
  3839. return false;
  3840. }
  3841. tsParseTypeOrTypePredicateAnnotation(returnToken) {
  3842. return this.tsInType(() => {
  3843. const t = this.startNode();
  3844. this.expect(returnToken);
  3845. const typePredicateVariable = this.tsIsIdentifier() && this.tsTryParse(this.tsParseTypePredicatePrefix.bind(this));
  3846. if (!typePredicateVariable) {
  3847. return this.tsParseTypeAnnotation(false, t);
  3848. }
  3849. const type = this.tsParseTypeAnnotation(false);
  3850. const node = this.startNodeAtNode(typePredicateVariable);
  3851. node.parameterName = typePredicateVariable;
  3852. node.typeAnnotation = type;
  3853. t.typeAnnotation = this.finishNode(node, "TSTypePredicate");
  3854. return this.finishNode(t, "TSTypeAnnotation");
  3855. });
  3856. }
  3857. tsTryParseTypeOrTypePredicateAnnotation() {
  3858. return this.match(types.colon) ? this.tsParseTypeOrTypePredicateAnnotation(types.colon) : undefined;
  3859. }
  3860. tsTryParseTypeAnnotation() {
  3861. return this.match(types.colon) ? this.tsParseTypeAnnotation() : undefined;
  3862. }
  3863. tsTryParseType() {
  3864. return this.tsEatThenParseType(types.colon);
  3865. }
  3866. tsParseTypePredicatePrefix() {
  3867. const id = this.parseIdentifier();
  3868. if (this.isContextual("is") && !this.hasPrecedingLineBreak()) {
  3869. this.next();
  3870. return id;
  3871. }
  3872. }
  3873. tsParseTypeAnnotation(eatColon = true, t = this.startNode()) {
  3874. this.tsInType(() => {
  3875. if (eatColon) this.expect(types.colon);
  3876. t.typeAnnotation = this.tsParseType();
  3877. });
  3878. return this.finishNode(t, "TSTypeAnnotation");
  3879. }
  3880. tsParseType() {
  3881. assert(this.state.inType);
  3882. const type = this.tsParseNonConditionalType();
  3883. if (this.hasPrecedingLineBreak() || !this.eat(types._extends)) {
  3884. return type;
  3885. }
  3886. const node = this.startNodeAtNode(type);
  3887. node.checkType = type;
  3888. node.extendsType = this.tsParseNonConditionalType();
  3889. this.expect(types.question);
  3890. node.trueType = this.tsParseType();
  3891. this.expect(types.colon);
  3892. node.falseType = this.tsParseType();
  3893. return this.finishNode(node, "TSConditionalType");
  3894. }
  3895. tsParseNonConditionalType() {
  3896. if (this.tsIsStartOfFunctionType()) {
  3897. return this.tsParseFunctionOrConstructorType("TSFunctionType");
  3898. }
  3899. if (this.match(types._new)) {
  3900. return this.tsParseFunctionOrConstructorType("TSConstructorType");
  3901. }
  3902. return this.tsParseUnionTypeOrHigher();
  3903. }
  3904. tsParseTypeAssertion() {
  3905. const node = this.startNode();
  3906. const _const = this.tsTryNextParseConstantContext();
  3907. node.typeAnnotation = _const || this.tsNextThenParseType();
  3908. this.expectRelational(">");
  3909. node.expression = this.parseMaybeUnary();
  3910. return this.finishNode(node, "TSTypeAssertion");
  3911. }
  3912. tsParseHeritageClause(descriptor) {
  3913. const originalStart = this.state.start;
  3914. const delimitedList = this.tsParseDelimitedList("HeritageClauseElement", this.tsParseExpressionWithTypeArguments.bind(this));
  3915. if (!delimitedList.length) {
  3916. this.raise(originalStart, `'${descriptor}' list cannot be empty.`);
  3917. }
  3918. return delimitedList;
  3919. }
  3920. tsParseExpressionWithTypeArguments() {
  3921. const node = this.startNode();
  3922. node.expression = this.tsParseEntityName(false);
  3923. if (this.isRelational("<")) {
  3924. node.typeParameters = this.tsParseTypeArguments();
  3925. }
  3926. return this.finishNode(node, "TSExpressionWithTypeArguments");
  3927. }
  3928. tsParseInterfaceDeclaration(node) {
  3929. node.id = this.parseIdentifier();
  3930. this.checkLVal(node.id, BIND_TS_INTERFACE, undefined, "typescript interface declaration");
  3931. node.typeParameters = this.tsTryParseTypeParameters();
  3932. if (this.eat(types._extends)) {
  3933. node.extends = this.tsParseHeritageClause("extends");
  3934. }
  3935. const body = this.startNode();
  3936. body.body = this.tsInType(this.tsParseObjectTypeMembers.bind(this));
  3937. node.body = this.finishNode(body, "TSInterfaceBody");
  3938. return this.finishNode(node, "TSInterfaceDeclaration");
  3939. }
  3940. tsParseTypeAliasDeclaration(node) {
  3941. node.id = this.parseIdentifier();
  3942. this.checkLVal(node.id, BIND_TS_TYPE, undefined, "typescript type alias");
  3943. node.typeParameters = this.tsTryParseTypeParameters();
  3944. node.typeAnnotation = this.tsExpectThenParseType(types.eq);
  3945. this.semicolon();
  3946. return this.finishNode(node, "TSTypeAliasDeclaration");
  3947. }
  3948. tsInNoContext(cb) {
  3949. const oldContext = this.state.context;
  3950. this.state.context = [oldContext[0]];
  3951. try {
  3952. return cb();
  3953. } finally {
  3954. this.state.context = oldContext;
  3955. }
  3956. }
  3957. tsInType(cb) {
  3958. const oldInType = this.state.inType;
  3959. this.state.inType = true;
  3960. try {
  3961. return cb();
  3962. } finally {
  3963. this.state.inType = oldInType;
  3964. }
  3965. }
  3966. tsEatThenParseType(token) {
  3967. return !this.match(token) ? undefined : this.tsNextThenParseType();
  3968. }
  3969. tsExpectThenParseType(token) {
  3970. return this.tsDoThenParseType(() => this.expect(token));
  3971. }
  3972. tsNextThenParseType() {
  3973. return this.tsDoThenParseType(() => this.next());
  3974. }
  3975. tsDoThenParseType(cb) {
  3976. return this.tsInType(() => {
  3977. cb();
  3978. return this.tsParseType();
  3979. });
  3980. }
  3981. tsParseEnumMember() {
  3982. const node = this.startNode();
  3983. node.id = this.match(types.string) ? this.parseExprAtom() : this.parseIdentifier(true);
  3984. if (this.eat(types.eq)) {
  3985. node.initializer = this.parseMaybeAssign();
  3986. }
  3987. return this.finishNode(node, "TSEnumMember");
  3988. }
  3989. tsParseEnumDeclaration(node, isConst) {
  3990. if (isConst) node.const = true;
  3991. node.id = this.parseIdentifier();
  3992. this.checkLVal(node.id, isConst ? BIND_TS_CONST_ENUM : BIND_TS_ENUM, undefined, "typescript enum declaration");
  3993. this.expect(types.braceL);
  3994. node.members = this.tsParseDelimitedList("EnumMembers", this.tsParseEnumMember.bind(this));
  3995. this.expect(types.braceR);
  3996. return this.finishNode(node, "TSEnumDeclaration");
  3997. }
  3998. tsParseModuleBlock() {
  3999. const node = this.startNode();
  4000. this.scope.enter(SCOPE_OTHER);
  4001. this.expect(types.braceL);
  4002. this.parseBlockOrModuleBlockBody(node.body = [], undefined, true, types.braceR);
  4003. this.scope.exit();
  4004. return this.finishNode(node, "TSModuleBlock");
  4005. }
  4006. tsParseModuleOrNamespaceDeclaration(node, nested = false) {
  4007. node.id = this.parseIdentifier();
  4008. if (!nested) {
  4009. this.checkLVal(node.id, BIND_TS_NAMESPACE, null, "module or namespace declaration");
  4010. }
  4011. if (this.eat(types.dot)) {
  4012. const inner = this.startNode();
  4013. this.tsParseModuleOrNamespaceDeclaration(inner, true);
  4014. node.body = inner;
  4015. } else {
  4016. node.body = this.tsParseModuleBlock();
  4017. }
  4018. return this.finishNode(node, "TSModuleDeclaration");
  4019. }
  4020. tsParseAmbientExternalModuleDeclaration(node) {
  4021. if (this.isContextual("global")) {
  4022. node.global = true;
  4023. node.id = this.parseIdentifier();
  4024. } else if (this.match(types.string)) {
  4025. node.id = this.parseExprAtom();
  4026. } else {
  4027. this.unexpected();
  4028. }
  4029. if (this.match(types.braceL)) {
  4030. node.body = this.tsParseModuleBlock();
  4031. } else {
  4032. this.semicolon();
  4033. }
  4034. return this.finishNode(node, "TSModuleDeclaration");
  4035. }
  4036. tsParseImportEqualsDeclaration(node, isExport) {
  4037. node.isExport = isExport || false;
  4038. node.id = this.parseIdentifier();
  4039. this.expect(types.eq);
  4040. node.moduleReference = this.tsParseModuleReference();
  4041. this.semicolon();
  4042. return this.finishNode(node, "TSImportEqualsDeclaration");
  4043. }
  4044. tsIsExternalModuleReference() {
  4045. return this.isContextual("require") && this.lookahead().type === types.parenL;
  4046. }
  4047. tsParseModuleReference() {
  4048. return this.tsIsExternalModuleReference() ? this.tsParseExternalModuleReference() : this.tsParseEntityName(false);
  4049. }
  4050. tsParseExternalModuleReference() {
  4051. const node = this.startNode();
  4052. this.expectContextual("require");
  4053. this.expect(types.parenL);
  4054. if (!this.match(types.string)) {
  4055. throw this.unexpected();
  4056. }
  4057. node.expression = this.parseExprAtom();
  4058. this.expect(types.parenR);
  4059. return this.finishNode(node, "TSExternalModuleReference");
  4060. }
  4061. tsLookAhead(f) {
  4062. const state = this.state.clone();
  4063. const res = f();
  4064. this.state = state;
  4065. return res;
  4066. }
  4067. tsTryParseAndCatch(f) {
  4068. const state = this.state.clone();
  4069. try {
  4070. return f();
  4071. } catch (e) {
  4072. if (e instanceof SyntaxError) {
  4073. this.state = state;
  4074. return undefined;
  4075. }
  4076. throw e;
  4077. }
  4078. }
  4079. tsTryParse(f) {
  4080. const state = this.state.clone();
  4081. const result = f();
  4082. if (result !== undefined && result !== false) {
  4083. return result;
  4084. } else {
  4085. this.state = state;
  4086. return undefined;
  4087. }
  4088. }
  4089. nodeWithSamePosition(original, type) {
  4090. const node = this.startNodeAtNode(original);
  4091. node.type = type;
  4092. node.end = original.end;
  4093. node.loc.end = original.loc.end;
  4094. if (original.leadingComments) {
  4095. node.leadingComments = original.leadingComments;
  4096. }
  4097. if (original.trailingComments) {
  4098. node.trailingComments = original.trailingComments;
  4099. }
  4100. if (original.innerComments) node.innerComments = original.innerComments;
  4101. return node;
  4102. }
  4103. tsTryParseDeclare(nany) {
  4104. if (this.isLineTerminator()) {
  4105. return;
  4106. }
  4107. let starttype = this.state.type;
  4108. let kind;
  4109. if (this.isContextual("let")) {
  4110. starttype = types._var;
  4111. kind = "let";
  4112. }
  4113. switch (starttype) {
  4114. case types._function:
  4115. return this.parseFunctionStatement(nany, false, true);
  4116. case types._class:
  4117. return this.parseClass(nany, true, false);
  4118. case types._const:
  4119. if (this.match(types._const) && this.isLookaheadContextual("enum")) {
  4120. this.expect(types._const);
  4121. this.expectContextual("enum");
  4122. return this.tsParseEnumDeclaration(nany, true);
  4123. }
  4124. case types._var:
  4125. kind = kind || this.state.value;
  4126. return this.parseVarStatement(nany, kind);
  4127. case types.name:
  4128. {
  4129. const value = this.state.value;
  4130. if (value === "global") {
  4131. return this.tsParseAmbientExternalModuleDeclaration(nany);
  4132. } else {
  4133. return this.tsParseDeclaration(nany, value, true);
  4134. }
  4135. }
  4136. }
  4137. }
  4138. tsTryParseExportDeclaration() {
  4139. return this.tsParseDeclaration(this.startNode(), this.state.value, true);
  4140. }
  4141. tsParseExpressionStatement(node, expr) {
  4142. switch (expr.name) {
  4143. case "declare":
  4144. {
  4145. const declaration = this.tsTryParseDeclare(node);
  4146. if (declaration) {
  4147. declaration.declare = true;
  4148. return declaration;
  4149. }
  4150. break;
  4151. }
  4152. case "global":
  4153. if (this.match(types.braceL)) {
  4154. const mod = node;
  4155. mod.global = true;
  4156. mod.id = expr;
  4157. mod.body = this.tsParseModuleBlock();
  4158. return this.finishNode(mod, "TSModuleDeclaration");
  4159. }
  4160. break;
  4161. default:
  4162. return this.tsParseDeclaration(node, expr.name, false);
  4163. }
  4164. }
  4165. tsParseDeclaration(node, value, next) {
  4166. switch (value) {
  4167. case "abstract":
  4168. if (this.tsCheckLineTerminatorAndMatch(types._class, next)) {
  4169. const cls = node;
  4170. cls.abstract = true;
  4171. if (next) {
  4172. this.next();
  4173. if (!this.match(types._class)) {
  4174. this.unexpected(null, types._class);
  4175. }
  4176. }
  4177. return this.parseClass(cls, true, false);
  4178. }
  4179. break;
  4180. case "enum":
  4181. if (next || this.match(types.name)) {
  4182. if (next) this.next();
  4183. return this.tsParseEnumDeclaration(node, false);
  4184. }
  4185. break;
  4186. case "interface":
  4187. if (this.tsCheckLineTerminatorAndMatch(types.name, next)) {
  4188. if (next) this.next();
  4189. return this.tsParseInterfaceDeclaration(node);
  4190. }
  4191. break;
  4192. case "module":
  4193. if (next) this.next();
  4194. if (this.match(types.string)) {
  4195. return this.tsParseAmbientExternalModuleDeclaration(node);
  4196. } else if (this.tsCheckLineTerminatorAndMatch(types.name, next)) {
  4197. return this.tsParseModuleOrNamespaceDeclaration(node);
  4198. }
  4199. break;
  4200. case "namespace":
  4201. if (this.tsCheckLineTerminatorAndMatch(types.name, next)) {
  4202. if (next) this.next();
  4203. return this.tsParseModuleOrNamespaceDeclaration(node);
  4204. }
  4205. break;
  4206. case "type":
  4207. if (this.tsCheckLineTerminatorAndMatch(types.name, next)) {
  4208. if (next) this.next();
  4209. return this.tsParseTypeAliasDeclaration(node);
  4210. }
  4211. break;
  4212. }
  4213. }
  4214. tsCheckLineTerminatorAndMatch(tokenType, next) {
  4215. return (next || this.match(tokenType)) && !this.isLineTerminator();
  4216. }
  4217. tsTryParseGenericAsyncArrowFunction(startPos, startLoc) {
  4218. if (!this.isRelational("<")) {
  4219. return undefined;
  4220. }
  4221. const res = this.tsTryParseAndCatch(() => {
  4222. const node = this.startNodeAt(startPos, startLoc);
  4223. node.typeParameters = this.tsParseTypeParameters();
  4224. super.parseFunctionParams(node);
  4225. node.returnType = this.tsTryParseTypeOrTypePredicateAnnotation();
  4226. this.expect(types.arrow);
  4227. return node;
  4228. });
  4229. if (!res) {
  4230. return undefined;
  4231. }
  4232. return this.parseArrowExpression(res, null, true);
  4233. }
  4234. tsParseTypeArguments() {
  4235. const node = this.startNode();
  4236. node.params = this.tsInType(() => this.tsInNoContext(() => {
  4237. this.expectRelational("<");
  4238. return this.tsParseDelimitedList("TypeParametersOrArguments", this.tsParseType.bind(this));
  4239. }));
  4240. this.state.exprAllowed = false;
  4241. this.expectRelational(">");
  4242. return this.finishNode(node, "TSTypeParameterInstantiation");
  4243. }
  4244. tsIsDeclarationStart() {
  4245. if (this.match(types.name)) {
  4246. switch (this.state.value) {
  4247. case "abstract":
  4248. case "declare":
  4249. case "enum":
  4250. case "interface":
  4251. case "module":
  4252. case "namespace":
  4253. case "type":
  4254. return true;
  4255. }
  4256. }
  4257. return false;
  4258. }
  4259. isExportDefaultSpecifier() {
  4260. if (this.tsIsDeclarationStart()) return false;
  4261. return super.isExportDefaultSpecifier();
  4262. }
  4263. parseAssignableListItem(allowModifiers, decorators) {
  4264. const startPos = this.state.start;
  4265. const startLoc = this.state.startLoc;
  4266. let accessibility;
  4267. let readonly = false;
  4268. if (allowModifiers) {
  4269. accessibility = this.parseAccessModifier();
  4270. readonly = !!this.tsParseModifier(["readonly"]);
  4271. }
  4272. const left = this.parseMaybeDefault();
  4273. this.parseAssignableListItemTypes(left);
  4274. const elt = this.parseMaybeDefault(left.start, left.loc.start, left);
  4275. if (accessibility || readonly) {
  4276. const pp = this.startNodeAt(startPos, startLoc);
  4277. if (decorators.length) {
  4278. pp.decorators = decorators;
  4279. }
  4280. if (accessibility) pp.accessibility = accessibility;
  4281. if (readonly) pp.readonly = readonly;
  4282. if (elt.type !== "Identifier" && elt.type !== "AssignmentPattern") {
  4283. throw this.raise(pp.start, "A parameter property may not be declared using a binding pattern.");
  4284. }
  4285. pp.parameter = elt;
  4286. return this.finishNode(pp, "TSParameterProperty");
  4287. }
  4288. if (decorators.length) {
  4289. left.decorators = decorators;
  4290. }
  4291. return elt;
  4292. }
  4293. parseFunctionBodyAndFinish(node, type, isMethod = false) {
  4294. if (this.match(types.colon)) {
  4295. node.returnType = this.tsParseTypeOrTypePredicateAnnotation(types.colon);
  4296. }
  4297. const bodilessType = type === "FunctionDeclaration" ? "TSDeclareFunction" : type === "ClassMethod" ? "TSDeclareMethod" : undefined;
  4298. if (bodilessType && !this.match(types.braceL) && this.isLineTerminator()) {
  4299. this.finishNode(node, bodilessType);
  4300. return;
  4301. }
  4302. super.parseFunctionBodyAndFinish(node, type, isMethod);
  4303. }
  4304. checkFunctionStatementId(node) {
  4305. if (!node.body && node.id) {
  4306. this.checkLVal(node.id, BIND_TS_FN_TYPE, null, "function name");
  4307. } else {
  4308. super.checkFunctionStatementId(...arguments);
  4309. }
  4310. }
  4311. parseSubscript(base, startPos, startLoc, noCalls, state, maybeAsyncArrow) {
  4312. if (!this.hasPrecedingLineBreak() && this.match(types.bang)) {
  4313. this.state.exprAllowed = false;
  4314. this.next();
  4315. const nonNullExpression = this.startNodeAt(startPos, startLoc);
  4316. nonNullExpression.expression = base;
  4317. return this.finishNode(nonNullExpression, "TSNonNullExpression");
  4318. }
  4319. if (this.isRelational("<")) {
  4320. const result = this.tsTryParseAndCatch(() => {
  4321. if (!noCalls && this.atPossibleAsync(base)) {
  4322. const asyncArrowFn = this.tsTryParseGenericAsyncArrowFunction(startPos, startLoc);
  4323. if (asyncArrowFn) {
  4324. return asyncArrowFn;
  4325. }
  4326. }
  4327. const node = this.startNodeAt(startPos, startLoc);
  4328. node.callee = base;
  4329. const typeArguments = this.tsParseTypeArguments();
  4330. if (typeArguments) {
  4331. if (!noCalls && this.eat(types.parenL)) {
  4332. node.arguments = this.parseCallExpressionArguments(types.parenR, false);
  4333. node.typeParameters = typeArguments;
  4334. return this.finishCallExpression(node);
  4335. } else if (this.match(types.backQuote)) {
  4336. return this.parseTaggedTemplateExpression(startPos, startLoc, base, state, typeArguments);
  4337. }
  4338. }
  4339. this.unexpected();
  4340. });
  4341. if (result) return result;
  4342. }
  4343. return super.parseSubscript(base, startPos, startLoc, noCalls, state, maybeAsyncArrow);
  4344. }
  4345. parseNewArguments(node) {
  4346. if (this.isRelational("<")) {
  4347. const typeParameters = this.tsTryParseAndCatch(() => {
  4348. const args = this.tsParseTypeArguments();
  4349. if (!this.match(types.parenL)) this.unexpected();
  4350. return args;
  4351. });
  4352. if (typeParameters) {
  4353. node.typeParameters = typeParameters;
  4354. }
  4355. }
  4356. super.parseNewArguments(node);
  4357. }
  4358. parseExprOp(left, leftStartPos, leftStartLoc, minPrec, noIn) {
  4359. if (nonNull(types._in.binop) > minPrec && !this.hasPrecedingLineBreak() && this.isContextual("as")) {
  4360. const node = this.startNodeAt(leftStartPos, leftStartLoc);
  4361. node.expression = left;
  4362. const _const = this.tsTryNextParseConstantContext();
  4363. if (_const) {
  4364. node.typeAnnotation = _const;
  4365. } else {
  4366. node.typeAnnotation = this.tsNextThenParseType();
  4367. }
  4368. this.finishNode(node, "TSAsExpression");
  4369. return this.parseExprOp(node, leftStartPos, leftStartLoc, minPrec, noIn);
  4370. }
  4371. return super.parseExprOp(left, leftStartPos, leftStartLoc, minPrec, noIn);
  4372. }
  4373. checkReservedWord(word, startLoc, checkKeywords, isBinding) {}
  4374. checkDuplicateExports() {}
  4375. parseImport(node) {
  4376. if (this.match(types.name) && this.lookahead().type === types.eq) {
  4377. return this.tsParseImportEqualsDeclaration(node);
  4378. }
  4379. return super.parseImport(node);
  4380. }
  4381. parseExport(node) {
  4382. if (this.match(types._import)) {
  4383. this.expect(types._import);
  4384. return this.tsParseImportEqualsDeclaration(node, true);
  4385. } else if (this.eat(types.eq)) {
  4386. const assign = node;
  4387. assign.expression = this.parseExpression();
  4388. this.semicolon();
  4389. return this.finishNode(assign, "TSExportAssignment");
  4390. } else if (this.eatContextual("as")) {
  4391. const decl = node;
  4392. this.expectContextual("namespace");
  4393. decl.id = this.parseIdentifier();
  4394. this.semicolon();
  4395. return this.finishNode(decl, "TSNamespaceExportDeclaration");
  4396. } else {
  4397. return super.parseExport(node);
  4398. }
  4399. }
  4400. isAbstractClass() {
  4401. return this.isContextual("abstract") && this.lookahead().type === types._class;
  4402. }
  4403. parseExportDefaultExpression() {
  4404. if (this.isAbstractClass()) {
  4405. const cls = this.startNode();
  4406. this.next();
  4407. this.parseClass(cls, true, true);
  4408. cls.abstract = true;
  4409. return cls;
  4410. }
  4411. if (this.state.value === "interface") {
  4412. const result = this.tsParseDeclaration(this.startNode(), this.state.value, true);
  4413. if (result) return result;
  4414. }
  4415. return super.parseExportDefaultExpression();
  4416. }
  4417. parseStatementContent(context, topLevel) {
  4418. if (this.state.type === types._const) {
  4419. const ahead = this.lookahead();
  4420. if (ahead.type === types.name && ahead.value === "enum") {
  4421. const node = this.startNode();
  4422. this.expect(types._const);
  4423. this.expectContextual("enum");
  4424. return this.tsParseEnumDeclaration(node, true);
  4425. }
  4426. }
  4427. return super.parseStatementContent(context, topLevel);
  4428. }
  4429. parseAccessModifier() {
  4430. return this.tsParseModifier(["public", "protected", "private"]);
  4431. }
  4432. parseClassMember(classBody, member, state, constructorAllowsSuper) {
  4433. const accessibility = this.parseAccessModifier();
  4434. if (accessibility) member.accessibility = accessibility;
  4435. super.parseClassMember(classBody, member, state, constructorAllowsSuper);
  4436. }
  4437. parseClassMemberWithIsStatic(classBody, member, state, isStatic, constructorAllowsSuper) {
  4438. const methodOrProp = member;
  4439. const prop = member;
  4440. const propOrIdx = member;
  4441. let abstract = false,
  4442. readonly = false;
  4443. const mod = this.tsParseModifier(["abstract", "readonly"]);
  4444. switch (mod) {
  4445. case "readonly":
  4446. readonly = true;
  4447. abstract = !!this.tsParseModifier(["abstract"]);
  4448. break;
  4449. case "abstract":
  4450. abstract = true;
  4451. readonly = !!this.tsParseModifier(["readonly"]);
  4452. break;
  4453. }
  4454. if (abstract) methodOrProp.abstract = true;
  4455. if (readonly) propOrIdx.readonly = true;
  4456. if (!abstract && !isStatic && !methodOrProp.accessibility) {
  4457. const idx = this.tsTryParseIndexSignature(member);
  4458. if (idx) {
  4459. classBody.body.push(idx);
  4460. return;
  4461. }
  4462. }
  4463. if (readonly) {
  4464. methodOrProp.static = isStatic;
  4465. this.parseClassPropertyName(prop);
  4466. this.parsePostMemberNameModifiers(methodOrProp);
  4467. this.pushClassProperty(classBody, prop);
  4468. return;
  4469. }
  4470. super.parseClassMemberWithIsStatic(classBody, member, state, isStatic, constructorAllowsSuper);
  4471. }
  4472. parsePostMemberNameModifiers(methodOrProp) {
  4473. const optional = this.eat(types.question);
  4474. if (optional) methodOrProp.optional = true;
  4475. }
  4476. parseExpressionStatement(node, expr) {
  4477. const decl = expr.type === "Identifier" ? this.tsParseExpressionStatement(node, expr) : undefined;
  4478. return decl || super.parseExpressionStatement(node, expr);
  4479. }
  4480. shouldParseExportDeclaration() {
  4481. if (this.tsIsDeclarationStart()) return true;
  4482. return super.shouldParseExportDeclaration();
  4483. }
  4484. parseConditional(expr, noIn, startPos, startLoc, refNeedsArrowPos) {
  4485. if (!refNeedsArrowPos || !this.match(types.question)) {
  4486. return super.parseConditional(expr, noIn, startPos, startLoc, refNeedsArrowPos);
  4487. }
  4488. const state = this.state.clone();
  4489. try {
  4490. return super.parseConditional(expr, noIn, startPos, startLoc);
  4491. } catch (err) {
  4492. if (!(err instanceof SyntaxError)) {
  4493. throw err;
  4494. }
  4495. this.state = state;
  4496. refNeedsArrowPos.start = err.pos || this.state.start;
  4497. return expr;
  4498. }
  4499. }
  4500. parseParenItem(node, startPos, startLoc) {
  4501. node = super.parseParenItem(node, startPos, startLoc);
  4502. if (this.eat(types.question)) {
  4503. node.optional = true;
  4504. this.resetEndLocation(node);
  4505. }
  4506. if (this.match(types.colon)) {
  4507. const typeCastNode = this.startNodeAt(startPos, startLoc);
  4508. typeCastNode.expression = node;
  4509. typeCastNode.typeAnnotation = this.tsParseTypeAnnotation();
  4510. return this.finishNode(typeCastNode, "TSTypeCastExpression");
  4511. }
  4512. return node;
  4513. }
  4514. parseExportDeclaration(node) {
  4515. const startPos = this.state.start;
  4516. const startLoc = this.state.startLoc;
  4517. const isDeclare = this.eatContextual("declare");
  4518. let declaration;
  4519. if (this.match(types.name)) {
  4520. declaration = this.tsTryParseExportDeclaration();
  4521. }
  4522. if (!declaration) {
  4523. declaration = super.parseExportDeclaration(node);
  4524. }
  4525. if (declaration && isDeclare) {
  4526. this.resetStartLocation(declaration, startPos, startLoc);
  4527. declaration.declare = true;
  4528. }
  4529. return declaration;
  4530. }
  4531. parseClassId(node, isStatement, optionalId) {
  4532. if ((!isStatement || optionalId) && this.isContextual("implements")) {
  4533. return;
  4534. }
  4535. super.parseClassId(...arguments);
  4536. const typeParameters = this.tsTryParseTypeParameters();
  4537. if (typeParameters) node.typeParameters = typeParameters;
  4538. }
  4539. parseClassProperty(node) {
  4540. if (!node.optional && this.eat(types.bang)) {
  4541. node.definite = true;
  4542. }
  4543. const type = this.tsTryParseTypeAnnotation();
  4544. if (type) node.typeAnnotation = type;
  4545. return super.parseClassProperty(node);
  4546. }
  4547. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  4548. const typeParameters = this.tsTryParseTypeParameters();
  4549. if (typeParameters) method.typeParameters = typeParameters;
  4550. super.pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper);
  4551. }
  4552. pushClassPrivateMethod(classBody, method, isGenerator, isAsync) {
  4553. const typeParameters = this.tsTryParseTypeParameters();
  4554. if (typeParameters) method.typeParameters = typeParameters;
  4555. super.pushClassPrivateMethod(classBody, method, isGenerator, isAsync);
  4556. }
  4557. parseClassSuper(node) {
  4558. super.parseClassSuper(node);
  4559. if (node.superClass && this.isRelational("<")) {
  4560. node.superTypeParameters = this.tsParseTypeArguments();
  4561. }
  4562. if (this.eatContextual("implements")) {
  4563. node.implements = this.tsParseHeritageClause("implements");
  4564. }
  4565. }
  4566. parseObjPropValue(prop, ...args) {
  4567. const typeParameters = this.tsTryParseTypeParameters();
  4568. if (typeParameters) prop.typeParameters = typeParameters;
  4569. super.parseObjPropValue(prop, ...args);
  4570. }
  4571. parseFunctionParams(node, allowModifiers) {
  4572. const typeParameters = this.tsTryParseTypeParameters();
  4573. if (typeParameters) node.typeParameters = typeParameters;
  4574. super.parseFunctionParams(node, allowModifiers);
  4575. }
  4576. parseVarId(decl, kind) {
  4577. super.parseVarId(decl, kind);
  4578. if (decl.id.type === "Identifier" && this.eat(types.bang)) {
  4579. decl.definite = true;
  4580. }
  4581. const type = this.tsTryParseTypeAnnotation();
  4582. if (type) {
  4583. decl.id.typeAnnotation = type;
  4584. this.resetEndLocation(decl.id);
  4585. }
  4586. }
  4587. parseAsyncArrowFromCallExpression(node, call) {
  4588. if (this.match(types.colon)) {
  4589. node.returnType = this.tsParseTypeAnnotation();
  4590. }
  4591. return super.parseAsyncArrowFromCallExpression(node, call);
  4592. }
  4593. parseMaybeAssign(...args) {
  4594. let jsxError;
  4595. if (this.match(types.jsxTagStart)) {
  4596. const context = this.curContext();
  4597. assert(context === types$1.j_oTag);
  4598. assert(this.state.context[this.state.context.length - 2] === types$1.j_expr);
  4599. const state = this.state.clone();
  4600. try {
  4601. return super.parseMaybeAssign(...args);
  4602. } catch (err) {
  4603. if (!(err instanceof SyntaxError)) {
  4604. throw err;
  4605. }
  4606. this.state = state;
  4607. assert(this.curContext() === types$1.j_oTag);
  4608. this.state.context.pop();
  4609. assert(this.curContext() === types$1.j_expr);
  4610. this.state.context.pop();
  4611. jsxError = err;
  4612. }
  4613. }
  4614. if (jsxError === undefined && !this.isRelational("<")) {
  4615. return super.parseMaybeAssign(...args);
  4616. }
  4617. let arrowExpression;
  4618. let typeParameters;
  4619. const state = this.state.clone();
  4620. try {
  4621. typeParameters = this.tsParseTypeParameters();
  4622. arrowExpression = super.parseMaybeAssign(...args);
  4623. if (arrowExpression.type !== "ArrowFunctionExpression" || arrowExpression.extra && arrowExpression.extra.parenthesized) {
  4624. this.unexpected();
  4625. }
  4626. } catch (err) {
  4627. if (!(err instanceof SyntaxError)) {
  4628. throw err;
  4629. }
  4630. if (jsxError) {
  4631. throw jsxError;
  4632. }
  4633. assert(!this.hasPlugin("jsx"));
  4634. this.state = state;
  4635. return super.parseMaybeAssign(...args);
  4636. }
  4637. if (typeParameters && typeParameters.params.length !== 0) {
  4638. this.resetStartLocationFromNode(arrowExpression, typeParameters);
  4639. }
  4640. arrowExpression.typeParameters = typeParameters;
  4641. return arrowExpression;
  4642. }
  4643. parseMaybeUnary(refShorthandDefaultPos) {
  4644. if (!this.hasPlugin("jsx") && this.isRelational("<")) {
  4645. return this.tsParseTypeAssertion();
  4646. } else {
  4647. return super.parseMaybeUnary(refShorthandDefaultPos);
  4648. }
  4649. }
  4650. parseArrow(node) {
  4651. if (this.match(types.colon)) {
  4652. const state = this.state.clone();
  4653. try {
  4654. const returnType = this.tsParseTypeOrTypePredicateAnnotation(types.colon);
  4655. if (this.canInsertSemicolon() || !this.match(types.arrow)) {
  4656. this.state = state;
  4657. return undefined;
  4658. }
  4659. node.returnType = returnType;
  4660. } catch (err) {
  4661. if (err instanceof SyntaxError) {
  4662. this.state = state;
  4663. } else {
  4664. throw err;
  4665. }
  4666. }
  4667. }
  4668. return super.parseArrow(node);
  4669. }
  4670. parseAssignableListItemTypes(param) {
  4671. if (this.eat(types.question)) {
  4672. if (param.type !== "Identifier") {
  4673. throw this.raise(param.start, "A binding pattern parameter cannot be optional in an implementation signature.");
  4674. }
  4675. param.optional = true;
  4676. }
  4677. const type = this.tsTryParseTypeAnnotation();
  4678. if (type) param.typeAnnotation = type;
  4679. this.resetEndLocation(param);
  4680. return param;
  4681. }
  4682. toAssignable(node, isBinding, contextDescription) {
  4683. switch (node.type) {
  4684. case "TSTypeCastExpression":
  4685. return super.toAssignable(this.typeCastToParameter(node), isBinding, contextDescription);
  4686. case "TSParameterProperty":
  4687. return super.toAssignable(node, isBinding, contextDescription);
  4688. case "TSAsExpression":
  4689. case "TSNonNullExpression":
  4690. case "TSTypeAssertion":
  4691. node.expression = this.toAssignable(node.expression, isBinding, contextDescription);
  4692. return node;
  4693. default:
  4694. return super.toAssignable(node, isBinding, contextDescription);
  4695. }
  4696. }
  4697. checkLVal(expr, bindingType = BIND_NONE, checkClashes, contextDescription) {
  4698. switch (expr.type) {
  4699. case "TSTypeCastExpression":
  4700. return;
  4701. case "TSParameterProperty":
  4702. this.checkLVal(expr.parameter, bindingType, checkClashes, "parameter property");
  4703. return;
  4704. case "TSAsExpression":
  4705. case "TSNonNullExpression":
  4706. case "TSTypeAssertion":
  4707. this.checkLVal(expr.expression, bindingType, checkClashes, contextDescription);
  4708. return;
  4709. default:
  4710. super.checkLVal(expr, bindingType, checkClashes, contextDescription);
  4711. return;
  4712. }
  4713. }
  4714. parseBindingAtom() {
  4715. switch (this.state.type) {
  4716. case types._this:
  4717. return this.parseIdentifier(true);
  4718. default:
  4719. return super.parseBindingAtom();
  4720. }
  4721. }
  4722. parseMaybeDecoratorArguments(expr) {
  4723. if (this.isRelational("<")) {
  4724. const typeArguments = this.tsParseTypeArguments();
  4725. if (this.match(types.parenL)) {
  4726. const call = super.parseMaybeDecoratorArguments(expr);
  4727. call.typeParameters = typeArguments;
  4728. return call;
  4729. }
  4730. this.unexpected(this.state.start, types.parenL);
  4731. }
  4732. return super.parseMaybeDecoratorArguments(expr);
  4733. }
  4734. isClassMethod() {
  4735. return this.isRelational("<") || super.isClassMethod();
  4736. }
  4737. isClassProperty() {
  4738. return this.match(types.bang) || this.match(types.colon) || super.isClassProperty();
  4739. }
  4740. parseMaybeDefault(...args) {
  4741. const node = super.parseMaybeDefault(...args);
  4742. if (node.type === "AssignmentPattern" && node.typeAnnotation && node.right.start < node.typeAnnotation.start) {
  4743. this.raise(node.typeAnnotation.start, "Type annotations must come before default assignments, " + "e.g. instead of `age = 25: number` use `age: number = 25`");
  4744. }
  4745. return node;
  4746. }
  4747. getTokenFromCode(code) {
  4748. if (this.state.inType && (code === 62 || code === 60)) {
  4749. return this.finishOp(types.relational, 1);
  4750. } else {
  4751. return super.getTokenFromCode(code);
  4752. }
  4753. }
  4754. toAssignableList(exprList, isBinding, contextDescription) {
  4755. for (let i = 0; i < exprList.length; i++) {
  4756. const expr = exprList[i];
  4757. if (!expr) continue;
  4758. switch (expr.type) {
  4759. case "TSTypeCastExpression":
  4760. exprList[i] = this.typeCastToParameter(expr);
  4761. break;
  4762. case "TSAsExpression":
  4763. case "TSTypeAssertion":
  4764. this.raise(expr.start, "Unexpected type cast in parameter position.");
  4765. break;
  4766. }
  4767. }
  4768. return super.toAssignableList(exprList, isBinding, contextDescription);
  4769. }
  4770. typeCastToParameter(node) {
  4771. node.expression.typeAnnotation = node.typeAnnotation;
  4772. this.resetEndLocation(node.expression, node.typeAnnotation.end, node.typeAnnotation.loc.end);
  4773. return node.expression;
  4774. }
  4775. toReferencedList(exprList, isInParens) {
  4776. for (let i = 0; i < exprList.length; i++) {
  4777. const expr = exprList[i];
  4778. if (expr && expr._exprListItem && expr.type === "TsTypeCastExpression") {
  4779. this.raise(expr.start, "Did not expect a type annotation here.");
  4780. }
  4781. }
  4782. return exprList;
  4783. }
  4784. shouldParseArrow() {
  4785. return this.match(types.colon) || super.shouldParseArrow();
  4786. }
  4787. shouldParseAsyncArrow() {
  4788. return this.match(types.colon) || super.shouldParseAsyncArrow();
  4789. }
  4790. canHaveLeadingDecorator() {
  4791. return super.canHaveLeadingDecorator() || this.isAbstractClass();
  4792. }
  4793. jsxParseOpeningElementAfterName(node) {
  4794. if (this.isRelational("<")) {
  4795. const typeArguments = this.tsTryParseAndCatch(() => this.tsParseTypeArguments());
  4796. if (typeArguments) node.typeParameters = typeArguments;
  4797. }
  4798. return super.jsxParseOpeningElementAfterName(node);
  4799. }
  4800. getGetterSetterExpectedParamCount(method) {
  4801. const baseCount = super.getGetterSetterExpectedParamCount(method);
  4802. const firstParam = method.params[0];
  4803. const hasContextParam = firstParam && firstParam.type === "Identifier" && firstParam.name === "this";
  4804. return hasContextParam ? baseCount + 1 : baseCount;
  4805. }
  4806. });
  4807. types.placeholder = new TokenType("%%", {
  4808. startsExpr: true
  4809. });
  4810. var placeholders = (superClass => class extends superClass {
  4811. parsePlaceholder(expectedNode) {
  4812. if (this.match(types.placeholder)) {
  4813. const node = this.startNode();
  4814. this.next();
  4815. this.assertNoSpace("Unexpected space in placeholder.");
  4816. node.name = super.parseIdentifier(true);
  4817. this.assertNoSpace("Unexpected space in placeholder.");
  4818. this.expect(types.placeholder);
  4819. return this.finishPlaceholder(node, expectedNode);
  4820. }
  4821. }
  4822. finishPlaceholder(node, expectedNode) {
  4823. const isFinished = !!(node.expectedNode && node.type === "Placeholder");
  4824. node.expectedNode = expectedNode;
  4825. return isFinished ? node : this.finishNode(node, "Placeholder");
  4826. }
  4827. getTokenFromCode(code) {
  4828. if (code === 37 && this.input.charCodeAt(this.state.pos + 1) === 37) {
  4829. return this.finishOp(types.placeholder, 2);
  4830. }
  4831. return super.getTokenFromCode(...arguments);
  4832. }
  4833. parseExprAtom() {
  4834. return this.parsePlaceholder("Expression") || super.parseExprAtom(...arguments);
  4835. }
  4836. parseIdentifier() {
  4837. return this.parsePlaceholder("Identifier") || super.parseIdentifier(...arguments);
  4838. }
  4839. checkReservedWord(word) {
  4840. if (word !== undefined) super.checkReservedWord(...arguments);
  4841. }
  4842. parseBindingAtom() {
  4843. return this.parsePlaceholder("Pattern") || super.parseBindingAtom(...arguments);
  4844. }
  4845. checkLVal(expr) {
  4846. if (expr.type !== "Placeholder") super.checkLVal(...arguments);
  4847. }
  4848. toAssignable(node) {
  4849. if (node && node.type === "Placeholder" && node.expectedNode === "Expression") {
  4850. node.expectedNode = "Pattern";
  4851. return node;
  4852. }
  4853. return super.toAssignable(...arguments);
  4854. }
  4855. verifyBreakContinue(node) {
  4856. if (node.label && node.label.type === "Placeholder") return;
  4857. super.verifyBreakContinue(...arguments);
  4858. }
  4859. parseExpressionStatement(node, expr) {
  4860. if (expr.type !== "Placeholder" || expr.extra && expr.extra.parenthesized) {
  4861. return super.parseExpressionStatement(...arguments);
  4862. }
  4863. if (this.match(types.colon)) {
  4864. const stmt = node;
  4865. stmt.label = this.finishPlaceholder(expr, "Identifier");
  4866. this.next();
  4867. stmt.body = this.parseStatement("label");
  4868. return this.finishNode(stmt, "LabeledStatement");
  4869. }
  4870. this.semicolon();
  4871. node.name = expr.name;
  4872. return this.finishPlaceholder(node, "Statement");
  4873. }
  4874. parseBlock() {
  4875. return this.parsePlaceholder("BlockStatement") || super.parseBlock(...arguments);
  4876. }
  4877. parseFunctionId() {
  4878. return this.parsePlaceholder("Identifier") || super.parseFunctionId(...arguments);
  4879. }
  4880. parseClass(node, isStatement, optionalId) {
  4881. const type = isStatement ? "ClassDeclaration" : "ClassExpression";
  4882. this.next();
  4883. this.takeDecorators(node);
  4884. const placeholder = this.parsePlaceholder("Identifier");
  4885. if (placeholder) {
  4886. if (this.match(types._extends) || this.match(types.placeholder) || this.match(types.braceL)) {
  4887. node.id = placeholder;
  4888. } else if (optionalId || !isStatement) {
  4889. node.id = null;
  4890. node.body = this.finishPlaceholder(placeholder, "ClassBody");
  4891. return this.finishNode(node, type);
  4892. } else {
  4893. this.unexpected(null, "A class name is required");
  4894. }
  4895. } else {
  4896. this.parseClassId(node, isStatement, optionalId);
  4897. }
  4898. this.parseClassSuper(node);
  4899. node.body = this.parsePlaceholder("ClassBody") || this.parseClassBody(!!node.superClass);
  4900. return this.finishNode(node, type);
  4901. }
  4902. parseExport(node) {
  4903. const placeholder = this.parsePlaceholder("Identifier");
  4904. if (!placeholder) return super.parseExport(...arguments);
  4905. if (!this.isContextual("from") && !this.match(types.comma)) {
  4906. node.specifiers = [];
  4907. node.source = null;
  4908. node.declaration = this.finishPlaceholder(placeholder, "Declaration");
  4909. return this.finishNode(node, "ExportNamedDeclaration");
  4910. }
  4911. this.expectPlugin("exportDefaultFrom");
  4912. const specifier = this.startNode();
  4913. specifier.exported = placeholder;
  4914. node.specifiers = [this.finishNode(specifier, "ExportDefaultSpecifier")];
  4915. return super.parseExport(node);
  4916. }
  4917. maybeParseExportDefaultSpecifier(node) {
  4918. if (node.specifiers && node.specifiers.length > 0) {
  4919. return true;
  4920. }
  4921. return super.maybeParseExportDefaultSpecifier(...arguments);
  4922. }
  4923. checkExport(node) {
  4924. const {
  4925. specifiers
  4926. } = node;
  4927. if (specifiers && specifiers.length) {
  4928. node.specifiers = specifiers.filter(node => node.exported.type === "Placeholder");
  4929. }
  4930. super.checkExport(node);
  4931. node.specifiers = specifiers;
  4932. }
  4933. parseImport(node) {
  4934. const placeholder = this.parsePlaceholder("Identifier");
  4935. if (!placeholder) return super.parseImport(...arguments);
  4936. node.specifiers = [];
  4937. if (!this.isContextual("from") && !this.match(types.comma)) {
  4938. node.source = this.finishPlaceholder(placeholder, "StringLiteral");
  4939. this.semicolon();
  4940. return this.finishNode(node, "ImportDeclaration");
  4941. }
  4942. const specifier = this.startNodeAtNode(placeholder);
  4943. specifier.local = placeholder;
  4944. this.finishNode(specifier, "ImportDefaultSpecifier");
  4945. node.specifiers.push(specifier);
  4946. if (this.eat(types.comma)) {
  4947. const hasStarImport = this.maybeParseStarImportSpecifier(node);
  4948. if (!hasStarImport) this.parseNamedImportSpecifiers(node);
  4949. }
  4950. this.expectContextual("from");
  4951. node.source = this.parseImportSource();
  4952. this.semicolon();
  4953. return this.finishNode(node, "ImportDeclaration");
  4954. }
  4955. parseImportSource() {
  4956. return this.parsePlaceholder("StringLiteral") || super.parseImportSource(...arguments);
  4957. }
  4958. });
  4959. function hasPlugin(plugins, name) {
  4960. return plugins.some(plugin => {
  4961. if (Array.isArray(plugin)) {
  4962. return plugin[0] === name;
  4963. } else {
  4964. return plugin === name;
  4965. }
  4966. });
  4967. }
  4968. function getPluginOption(plugins, name, option) {
  4969. const plugin = plugins.find(plugin => {
  4970. if (Array.isArray(plugin)) {
  4971. return plugin[0] === name;
  4972. } else {
  4973. return plugin === name;
  4974. }
  4975. });
  4976. if (plugin && Array.isArray(plugin)) {
  4977. return plugin[1][option];
  4978. }
  4979. return null;
  4980. }
  4981. const PIPELINE_PROPOSALS = ["minimal", "smart"];
  4982. function validatePlugins(plugins) {
  4983. if (hasPlugin(plugins, "decorators")) {
  4984. if (hasPlugin(plugins, "decorators-legacy")) {
  4985. throw new Error("Cannot use the decorators and decorators-legacy plugin together");
  4986. }
  4987. const decoratorsBeforeExport = getPluginOption(plugins, "decorators", "decoratorsBeforeExport");
  4988. if (decoratorsBeforeExport == null) {
  4989. throw new Error("The 'decorators' plugin requires a 'decoratorsBeforeExport' option," + " whose value must be a boolean. If you are migrating from" + " Babylon/Babel 6 or want to use the old decorators proposal, you" + " should use the 'decorators-legacy' plugin instead of 'decorators'.");
  4990. } else if (typeof decoratorsBeforeExport !== "boolean") {
  4991. throw new Error("'decoratorsBeforeExport' must be a boolean.");
  4992. }
  4993. }
  4994. if (hasPlugin(plugins, "flow") && hasPlugin(plugins, "typescript")) {
  4995. throw new Error("Cannot combine flow and typescript plugins.");
  4996. }
  4997. if (hasPlugin(plugins, "pipelineOperator") && !PIPELINE_PROPOSALS.includes(getPluginOption(plugins, "pipelineOperator", "proposal"))) {
  4998. throw new Error("'pipelineOperator' requires 'proposal' option whose value should be one of: " + PIPELINE_PROPOSALS.map(p => `'${p}'`).join(", "));
  4999. }
  5000. }
  5001. const mixinPlugins = {
  5002. estree,
  5003. jsx,
  5004. flow,
  5005. typescript,
  5006. placeholders
  5007. };
  5008. const mixinPluginNames = Object.keys(mixinPlugins);
  5009. const defaultOptions = {
  5010. sourceType: "script",
  5011. sourceFilename: undefined,
  5012. startLine: 1,
  5013. allowAwaitOutsideFunction: false,
  5014. allowReturnOutsideFunction: false,
  5015. allowImportExportEverywhere: false,
  5016. allowSuperOutsideMethod: false,
  5017. plugins: [],
  5018. strictMode: null,
  5019. ranges: false,
  5020. tokens: false,
  5021. createParenthesizedExpressions: false
  5022. };
  5023. function getOptions(opts) {
  5024. const options = {};
  5025. for (let _i = 0, _Object$keys = Object.keys(defaultOptions); _i < _Object$keys.length; _i++) {
  5026. const key = _Object$keys[_i];
  5027. options[key] = opts && opts[key] != null ? opts[key] : defaultOptions[key];
  5028. }
  5029. return options;
  5030. }
  5031. class Position {
  5032. constructor(line, col) {
  5033. this.line = line;
  5034. this.column = col;
  5035. }
  5036. }
  5037. class SourceLocation {
  5038. constructor(start, end) {
  5039. this.start = start;
  5040. this.end = end;
  5041. }
  5042. }
  5043. function getLineInfo(input, offset) {
  5044. let line = 1;
  5045. let lineStart = 0;
  5046. let match;
  5047. lineBreakG.lastIndex = 0;
  5048. while ((match = lineBreakG.exec(input)) && match.index < offset) {
  5049. line++;
  5050. lineStart = lineBreakG.lastIndex;
  5051. }
  5052. return new Position(line, offset - lineStart);
  5053. }
  5054. class BaseParser {
  5055. constructor() {
  5056. this.sawUnambiguousESM = false;
  5057. }
  5058. hasPlugin(name) {
  5059. return this.plugins.has(name);
  5060. }
  5061. getPluginOption(plugin, name) {
  5062. if (this.hasPlugin(plugin)) return this.plugins.get(plugin)[name];
  5063. }
  5064. }
  5065. function last(stack) {
  5066. return stack[stack.length - 1];
  5067. }
  5068. class CommentsParser extends BaseParser {
  5069. addComment(comment) {
  5070. if (this.filename) comment.loc.filename = this.filename;
  5071. this.state.trailingComments.push(comment);
  5072. this.state.leadingComments.push(comment);
  5073. }
  5074. processComment(node) {
  5075. if (node.type === "Program" && node.body.length > 0) return;
  5076. const stack = this.state.commentStack;
  5077. let firstChild, lastChild, trailingComments, i, j;
  5078. if (this.state.trailingComments.length > 0) {
  5079. if (this.state.trailingComments[0].start >= node.end) {
  5080. trailingComments = this.state.trailingComments;
  5081. this.state.trailingComments = [];
  5082. } else {
  5083. this.state.trailingComments.length = 0;
  5084. }
  5085. } else if (stack.length > 0) {
  5086. const lastInStack = last(stack);
  5087. if (lastInStack.trailingComments && lastInStack.trailingComments[0].start >= node.end) {
  5088. trailingComments = lastInStack.trailingComments;
  5089. delete lastInStack.trailingComments;
  5090. }
  5091. }
  5092. if (stack.length > 0 && last(stack).start >= node.start) {
  5093. firstChild = stack.pop();
  5094. }
  5095. while (stack.length > 0 && last(stack).start >= node.start) {
  5096. lastChild = stack.pop();
  5097. }
  5098. if (!lastChild && firstChild) lastChild = firstChild;
  5099. if (firstChild && this.state.leadingComments.length > 0) {
  5100. const lastComment = last(this.state.leadingComments);
  5101. if (firstChild.type === "ObjectProperty") {
  5102. if (lastComment.start >= node.start) {
  5103. if (this.state.commentPreviousNode) {
  5104. for (j = 0; j < this.state.leadingComments.length; j++) {
  5105. if (this.state.leadingComments[j].end < this.state.commentPreviousNode.end) {
  5106. this.state.leadingComments.splice(j, 1);
  5107. j--;
  5108. }
  5109. }
  5110. if (this.state.leadingComments.length > 0) {
  5111. firstChild.trailingComments = this.state.leadingComments;
  5112. this.state.leadingComments = [];
  5113. }
  5114. }
  5115. }
  5116. } else if (node.type === "CallExpression" && node.arguments && node.arguments.length) {
  5117. const lastArg = last(node.arguments);
  5118. if (lastArg && lastComment.start >= lastArg.start && lastComment.end <= node.end) {
  5119. if (this.state.commentPreviousNode) {
  5120. for (j = 0; j < this.state.leadingComments.length; j++) {
  5121. if (this.state.leadingComments[j].end < this.state.commentPreviousNode.end) {
  5122. this.state.leadingComments.splice(j, 1);
  5123. j--;
  5124. }
  5125. }
  5126. if (this.state.leadingComments.length > 0) {
  5127. lastArg.trailingComments = this.state.leadingComments;
  5128. this.state.leadingComments = [];
  5129. }
  5130. }
  5131. }
  5132. }
  5133. }
  5134. if (lastChild) {
  5135. if (lastChild.leadingComments) {
  5136. if (lastChild !== node && lastChild.leadingComments.length > 0 && last(lastChild.leadingComments).end <= node.start) {
  5137. node.leadingComments = lastChild.leadingComments;
  5138. delete lastChild.leadingComments;
  5139. } else {
  5140. for (i = lastChild.leadingComments.length - 2; i >= 0; --i) {
  5141. if (lastChild.leadingComments[i].end <= node.start) {
  5142. node.leadingComments = lastChild.leadingComments.splice(0, i + 1);
  5143. break;
  5144. }
  5145. }
  5146. }
  5147. }
  5148. } else if (this.state.leadingComments.length > 0) {
  5149. if (last(this.state.leadingComments).end <= node.start) {
  5150. if (this.state.commentPreviousNode) {
  5151. for (j = 0; j < this.state.leadingComments.length; j++) {
  5152. if (this.state.leadingComments[j].end < this.state.commentPreviousNode.end) {
  5153. this.state.leadingComments.splice(j, 1);
  5154. j--;
  5155. }
  5156. }
  5157. }
  5158. if (this.state.leadingComments.length > 0) {
  5159. node.leadingComments = this.state.leadingComments;
  5160. this.state.leadingComments = [];
  5161. }
  5162. } else {
  5163. for (i = 0; i < this.state.leadingComments.length; i++) {
  5164. if (this.state.leadingComments[i].end > node.start) {
  5165. break;
  5166. }
  5167. }
  5168. const leadingComments = this.state.leadingComments.slice(0, i);
  5169. if (leadingComments.length) {
  5170. node.leadingComments = leadingComments;
  5171. }
  5172. trailingComments = this.state.leadingComments.slice(i);
  5173. if (trailingComments.length === 0) {
  5174. trailingComments = null;
  5175. }
  5176. }
  5177. }
  5178. this.state.commentPreviousNode = node;
  5179. if (trailingComments) {
  5180. if (trailingComments.length && trailingComments[0].start >= node.start && last(trailingComments).end <= node.end) {
  5181. node.innerComments = trailingComments;
  5182. } else {
  5183. node.trailingComments = trailingComments;
  5184. }
  5185. }
  5186. stack.push(node);
  5187. }
  5188. }
  5189. class LocationParser extends CommentsParser {
  5190. getLocationForPosition(pos) {
  5191. let loc;
  5192. if (pos === this.state.start) loc = this.state.startLoc;else if (pos === this.state.lastTokStart) loc = this.state.lastTokStartLoc;else if (pos === this.state.end) loc = this.state.endLoc;else if (pos === this.state.lastTokEnd) loc = this.state.lastTokEndLoc;else loc = getLineInfo(this.input, pos);
  5193. return loc;
  5194. }
  5195. raise(pos, message, {
  5196. missingPluginNames,
  5197. code
  5198. } = {}) {
  5199. const loc = this.getLocationForPosition(pos);
  5200. message += ` (${loc.line}:${loc.column})`;
  5201. const err = new SyntaxError(message);
  5202. err.pos = pos;
  5203. err.loc = loc;
  5204. if (missingPluginNames) {
  5205. err.missingPlugin = missingPluginNames;
  5206. }
  5207. if (code !== undefined) {
  5208. err.code = code;
  5209. }
  5210. throw err;
  5211. }
  5212. }
  5213. class State {
  5214. constructor() {
  5215. this.potentialArrowAt = -1;
  5216. this.noArrowAt = [];
  5217. this.noArrowParamsConversionAt = [];
  5218. this.commaAfterSpreadAt = -1;
  5219. this.inParameters = false;
  5220. this.maybeInArrowParameters = false;
  5221. this.inPipeline = false;
  5222. this.inType = false;
  5223. this.noAnonFunctionType = false;
  5224. this.inPropertyName = false;
  5225. this.inClassProperty = false;
  5226. this.hasFlowComment = false;
  5227. this.isIterator = false;
  5228. this.topicContext = {
  5229. maxNumOfResolvableTopics: 0,
  5230. maxTopicIndex: null
  5231. };
  5232. this.classLevel = 0;
  5233. this.labels = [];
  5234. this.decoratorStack = [[]];
  5235. this.yieldPos = 0;
  5236. this.awaitPos = 0;
  5237. this.tokens = [];
  5238. this.comments = [];
  5239. this.trailingComments = [];
  5240. this.leadingComments = [];
  5241. this.commentStack = [];
  5242. this.commentPreviousNode = null;
  5243. this.pos = 0;
  5244. this.lineStart = 0;
  5245. this.type = types.eof;
  5246. this.value = null;
  5247. this.start = 0;
  5248. this.end = 0;
  5249. this.lastTokEndLoc = null;
  5250. this.lastTokStartLoc = null;
  5251. this.lastTokStart = 0;
  5252. this.lastTokEnd = 0;
  5253. this.context = [types$1.braceStatement];
  5254. this.exprAllowed = true;
  5255. this.containsEsc = false;
  5256. this.containsOctal = false;
  5257. this.octalPosition = null;
  5258. this.exportedIdentifiers = [];
  5259. this.invalidTemplateEscapePosition = null;
  5260. }
  5261. init(options) {
  5262. this.strict = options.strictMode === false ? false : options.sourceType === "module";
  5263. this.curLine = options.startLine;
  5264. this.startLoc = this.endLoc = this.curPosition();
  5265. }
  5266. curPosition() {
  5267. return new Position(this.curLine, this.pos - this.lineStart);
  5268. }
  5269. clone(skipArrays) {
  5270. const state = new State();
  5271. const keys = Object.keys(this);
  5272. for (let i = 0, length = keys.length; i < length; i++) {
  5273. const key = keys[i];
  5274. let val = this[key];
  5275. if (!skipArrays && Array.isArray(val)) {
  5276. val = val.slice();
  5277. }
  5278. state[key] = val;
  5279. }
  5280. return state;
  5281. }
  5282. }
  5283. var _isDigit = function isDigit(code) {
  5284. return code >= 48 && code <= 57;
  5285. };
  5286. const VALID_REGEX_FLAGS = new Set(["g", "m", "s", "i", "y", "u"]);
  5287. const forbiddenNumericSeparatorSiblings = {
  5288. decBinOct: [46, 66, 69, 79, 95, 98, 101, 111],
  5289. hex: [46, 88, 95, 120]
  5290. };
  5291. const allowedNumericSeparatorSiblings = {};
  5292. allowedNumericSeparatorSiblings.bin = [48, 49];
  5293. allowedNumericSeparatorSiblings.oct = [...allowedNumericSeparatorSiblings.bin, 50, 51, 52, 53, 54, 55];
  5294. allowedNumericSeparatorSiblings.dec = [...allowedNumericSeparatorSiblings.oct, 56, 57];
  5295. allowedNumericSeparatorSiblings.hex = [...allowedNumericSeparatorSiblings.dec, 65, 66, 67, 68, 69, 70, 97, 98, 99, 100, 101, 102];
  5296. class Token {
  5297. constructor(state) {
  5298. this.type = state.type;
  5299. this.value = state.value;
  5300. this.start = state.start;
  5301. this.end = state.end;
  5302. this.loc = new SourceLocation(state.startLoc, state.endLoc);
  5303. }
  5304. }
  5305. class Tokenizer extends LocationParser {
  5306. constructor(options, input) {
  5307. super();
  5308. this.state = new State();
  5309. this.state.init(options);
  5310. this.input = input;
  5311. this.length = input.length;
  5312. this.isLookahead = false;
  5313. }
  5314. next() {
  5315. if (this.options.tokens && !this.isLookahead) {
  5316. this.state.tokens.push(new Token(this.state));
  5317. }
  5318. this.state.lastTokEnd = this.state.end;
  5319. this.state.lastTokStart = this.state.start;
  5320. this.state.lastTokEndLoc = this.state.endLoc;
  5321. this.state.lastTokStartLoc = this.state.startLoc;
  5322. this.nextToken();
  5323. }
  5324. eat(type) {
  5325. if (this.match(type)) {
  5326. this.next();
  5327. return true;
  5328. } else {
  5329. return false;
  5330. }
  5331. }
  5332. match(type) {
  5333. return this.state.type === type;
  5334. }
  5335. lookahead() {
  5336. const old = this.state;
  5337. this.state = old.clone(true);
  5338. this.isLookahead = true;
  5339. this.next();
  5340. this.isLookahead = false;
  5341. const curr = this.state;
  5342. this.state = old;
  5343. return curr;
  5344. }
  5345. setStrict(strict) {
  5346. this.state.strict = strict;
  5347. if (!this.match(types.num) && !this.match(types.string)) return;
  5348. this.state.pos = this.state.start;
  5349. while (this.state.pos < this.state.lineStart) {
  5350. this.state.lineStart = this.input.lastIndexOf("\n", this.state.lineStart - 2) + 1;
  5351. --this.state.curLine;
  5352. }
  5353. this.nextToken();
  5354. }
  5355. curContext() {
  5356. return this.state.context[this.state.context.length - 1];
  5357. }
  5358. nextToken() {
  5359. const curContext = this.curContext();
  5360. if (!curContext || !curContext.preserveSpace) this.skipSpace();
  5361. this.state.containsOctal = false;
  5362. this.state.octalPosition = null;
  5363. this.state.start = this.state.pos;
  5364. this.state.startLoc = this.state.curPosition();
  5365. if (this.state.pos >= this.length) {
  5366. this.finishToken(types.eof);
  5367. return;
  5368. }
  5369. if (curContext.override) {
  5370. curContext.override(this);
  5371. } else {
  5372. this.getTokenFromCode(this.input.codePointAt(this.state.pos));
  5373. }
  5374. }
  5375. pushComment(block, text, start, end, startLoc, endLoc) {
  5376. const comment = {
  5377. type: block ? "CommentBlock" : "CommentLine",
  5378. value: text,
  5379. start: start,
  5380. end: end,
  5381. loc: new SourceLocation(startLoc, endLoc)
  5382. };
  5383. if (this.options.tokens) this.state.tokens.push(comment);
  5384. this.state.comments.push(comment);
  5385. this.addComment(comment);
  5386. }
  5387. skipBlockComment() {
  5388. const startLoc = this.state.curPosition();
  5389. const start = this.state.pos;
  5390. const end = this.input.indexOf("*/", this.state.pos += 2);
  5391. if (end === -1) this.raise(this.state.pos - 2, "Unterminated comment");
  5392. this.state.pos = end + 2;
  5393. lineBreakG.lastIndex = start;
  5394. let match;
  5395. while ((match = lineBreakG.exec(this.input)) && match.index < this.state.pos) {
  5396. ++this.state.curLine;
  5397. this.state.lineStart = match.index + match[0].length;
  5398. }
  5399. if (this.isLookahead) return;
  5400. this.pushComment(true, this.input.slice(start + 2, end), start, this.state.pos, startLoc, this.state.curPosition());
  5401. }
  5402. skipLineComment(startSkip) {
  5403. const start = this.state.pos;
  5404. const startLoc = this.state.curPosition();
  5405. let ch = this.input.charCodeAt(this.state.pos += startSkip);
  5406. if (this.state.pos < this.length) {
  5407. while (ch !== 10 && ch !== 13 && ch !== 8232 && ch !== 8233 && ++this.state.pos < this.length) {
  5408. ch = this.input.charCodeAt(this.state.pos);
  5409. }
  5410. }
  5411. if (this.isLookahead) return;
  5412. this.pushComment(false, this.input.slice(start + startSkip, this.state.pos), start, this.state.pos, startLoc, this.state.curPosition());
  5413. }
  5414. skipSpace() {
  5415. loop: while (this.state.pos < this.length) {
  5416. const ch = this.input.charCodeAt(this.state.pos);
  5417. switch (ch) {
  5418. case 32:
  5419. case 160:
  5420. case 9:
  5421. ++this.state.pos;
  5422. break;
  5423. case 13:
  5424. if (this.input.charCodeAt(this.state.pos + 1) === 10) {
  5425. ++this.state.pos;
  5426. }
  5427. case 10:
  5428. case 8232:
  5429. case 8233:
  5430. ++this.state.pos;
  5431. ++this.state.curLine;
  5432. this.state.lineStart = this.state.pos;
  5433. break;
  5434. case 47:
  5435. switch (this.input.charCodeAt(this.state.pos + 1)) {
  5436. case 42:
  5437. this.skipBlockComment();
  5438. break;
  5439. case 47:
  5440. this.skipLineComment(2);
  5441. break;
  5442. default:
  5443. break loop;
  5444. }
  5445. break;
  5446. default:
  5447. if (isWhitespace(ch)) {
  5448. ++this.state.pos;
  5449. } else {
  5450. break loop;
  5451. }
  5452. }
  5453. }
  5454. }
  5455. finishToken(type, val) {
  5456. this.state.end = this.state.pos;
  5457. this.state.endLoc = this.state.curPosition();
  5458. const prevType = this.state.type;
  5459. this.state.type = type;
  5460. this.state.value = val;
  5461. if (!this.isLookahead) this.updateContext(prevType);
  5462. }
  5463. readToken_numberSign() {
  5464. if (this.state.pos === 0 && this.readToken_interpreter()) {
  5465. return;
  5466. }
  5467. const nextPos = this.state.pos + 1;
  5468. const next = this.input.charCodeAt(nextPos);
  5469. if (next >= 48 && next <= 57) {
  5470. this.raise(this.state.pos, "Unexpected digit after hash token");
  5471. }
  5472. if ((this.hasPlugin("classPrivateProperties") || this.hasPlugin("classPrivateMethods")) && this.state.classLevel > 0) {
  5473. ++this.state.pos;
  5474. this.finishToken(types.hash);
  5475. return;
  5476. } else if (this.getPluginOption("pipelineOperator", "proposal") === "smart") {
  5477. this.finishOp(types.hash, 1);
  5478. } else {
  5479. this.raise(this.state.pos, "Unexpected character '#'");
  5480. }
  5481. }
  5482. readToken_dot() {
  5483. const next = this.input.charCodeAt(this.state.pos + 1);
  5484. if (next >= 48 && next <= 57) {
  5485. this.readNumber(true);
  5486. return;
  5487. }
  5488. const next2 = this.input.charCodeAt(this.state.pos + 2);
  5489. if (next === 46 && next2 === 46) {
  5490. this.state.pos += 3;
  5491. this.finishToken(types.ellipsis);
  5492. } else {
  5493. ++this.state.pos;
  5494. this.finishToken(types.dot);
  5495. }
  5496. }
  5497. readToken_slash() {
  5498. if (this.state.exprAllowed && !this.state.inType) {
  5499. ++this.state.pos;
  5500. this.readRegexp();
  5501. return;
  5502. }
  5503. const next = this.input.charCodeAt(this.state.pos + 1);
  5504. if (next === 61) {
  5505. this.finishOp(types.assign, 2);
  5506. } else {
  5507. this.finishOp(types.slash, 1);
  5508. }
  5509. }
  5510. readToken_interpreter() {
  5511. if (this.state.pos !== 0 || this.length < 2) return false;
  5512. const start = this.state.pos;
  5513. this.state.pos += 1;
  5514. let ch = this.input.charCodeAt(this.state.pos);
  5515. if (ch !== 33) return false;
  5516. while (ch !== 10 && ch !== 13 && ch !== 8232 && ch !== 8233 && ++this.state.pos < this.length) {
  5517. ch = this.input.charCodeAt(this.state.pos);
  5518. }
  5519. const value = this.input.slice(start + 2, this.state.pos);
  5520. this.finishToken(types.interpreterDirective, value);
  5521. return true;
  5522. }
  5523. readToken_mult_modulo(code) {
  5524. let type = code === 42 ? types.star : types.modulo;
  5525. let width = 1;
  5526. let next = this.input.charCodeAt(this.state.pos + 1);
  5527. const exprAllowed = this.state.exprAllowed;
  5528. if (code === 42 && next === 42) {
  5529. width++;
  5530. next = this.input.charCodeAt(this.state.pos + 2);
  5531. type = types.exponent;
  5532. }
  5533. if (next === 61 && !exprAllowed) {
  5534. width++;
  5535. type = types.assign;
  5536. }
  5537. this.finishOp(type, width);
  5538. }
  5539. readToken_pipe_amp(code) {
  5540. const next = this.input.charCodeAt(this.state.pos + 1);
  5541. if (next === code) {
  5542. if (this.input.charCodeAt(this.state.pos + 2) === 61) {
  5543. this.finishOp(types.assign, 3);
  5544. } else {
  5545. this.finishOp(code === 124 ? types.logicalOR : types.logicalAND, 2);
  5546. }
  5547. return;
  5548. }
  5549. if (code === 124) {
  5550. if (next === 62) {
  5551. this.finishOp(types.pipeline, 2);
  5552. return;
  5553. }
  5554. }
  5555. if (next === 61) {
  5556. this.finishOp(types.assign, 2);
  5557. return;
  5558. }
  5559. this.finishOp(code === 124 ? types.bitwiseOR : types.bitwiseAND, 1);
  5560. }
  5561. readToken_caret() {
  5562. const next = this.input.charCodeAt(this.state.pos + 1);
  5563. if (next === 61) {
  5564. this.finishOp(types.assign, 2);
  5565. } else {
  5566. this.finishOp(types.bitwiseXOR, 1);
  5567. }
  5568. }
  5569. readToken_plus_min(code) {
  5570. const next = this.input.charCodeAt(this.state.pos + 1);
  5571. if (next === code) {
  5572. if (next === 45 && !this.inModule && this.input.charCodeAt(this.state.pos + 2) === 62 && (this.state.lastTokEnd === 0 || lineBreak.test(this.input.slice(this.state.lastTokEnd, this.state.pos)))) {
  5573. this.skipLineComment(3);
  5574. this.skipSpace();
  5575. this.nextToken();
  5576. return;
  5577. }
  5578. this.finishOp(types.incDec, 2);
  5579. return;
  5580. }
  5581. if (next === 61) {
  5582. this.finishOp(types.assign, 2);
  5583. } else {
  5584. this.finishOp(types.plusMin, 1);
  5585. }
  5586. }
  5587. readToken_lt_gt(code) {
  5588. const next = this.input.charCodeAt(this.state.pos + 1);
  5589. let size = 1;
  5590. if (next === code) {
  5591. size = code === 62 && this.input.charCodeAt(this.state.pos + 2) === 62 ? 3 : 2;
  5592. if (this.input.charCodeAt(this.state.pos + size) === 61) {
  5593. this.finishOp(types.assign, size + 1);
  5594. return;
  5595. }
  5596. this.finishOp(types.bitShift, size);
  5597. return;
  5598. }
  5599. if (next === 33 && code === 60 && !this.inModule && this.input.charCodeAt(this.state.pos + 2) === 45 && this.input.charCodeAt(this.state.pos + 3) === 45) {
  5600. this.skipLineComment(4);
  5601. this.skipSpace();
  5602. this.nextToken();
  5603. return;
  5604. }
  5605. if (next === 61) {
  5606. size = 2;
  5607. }
  5608. this.finishOp(types.relational, size);
  5609. }
  5610. readToken_eq_excl(code) {
  5611. const next = this.input.charCodeAt(this.state.pos + 1);
  5612. if (next === 61) {
  5613. this.finishOp(types.equality, this.input.charCodeAt(this.state.pos + 2) === 61 ? 3 : 2);
  5614. return;
  5615. }
  5616. if (code === 61 && next === 62) {
  5617. this.state.pos += 2;
  5618. this.finishToken(types.arrow);
  5619. return;
  5620. }
  5621. this.finishOp(code === 61 ? types.eq : types.bang, 1);
  5622. }
  5623. readToken_question() {
  5624. const next = this.input.charCodeAt(this.state.pos + 1);
  5625. const next2 = this.input.charCodeAt(this.state.pos + 2);
  5626. if (next === 63 && !this.state.inType) {
  5627. if (next2 === 61) {
  5628. this.finishOp(types.assign, 3);
  5629. } else {
  5630. this.finishOp(types.nullishCoalescing, 2);
  5631. }
  5632. } else if (next === 46 && !(next2 >= 48 && next2 <= 57)) {
  5633. this.state.pos += 2;
  5634. this.finishToken(types.questionDot);
  5635. } else {
  5636. ++this.state.pos;
  5637. this.finishToken(types.question);
  5638. }
  5639. }
  5640. getTokenFromCode(code) {
  5641. switch (code) {
  5642. case 46:
  5643. this.readToken_dot();
  5644. return;
  5645. case 40:
  5646. ++this.state.pos;
  5647. this.finishToken(types.parenL);
  5648. return;
  5649. case 41:
  5650. ++this.state.pos;
  5651. this.finishToken(types.parenR);
  5652. return;
  5653. case 59:
  5654. ++this.state.pos;
  5655. this.finishToken(types.semi);
  5656. return;
  5657. case 44:
  5658. ++this.state.pos;
  5659. this.finishToken(types.comma);
  5660. return;
  5661. case 91:
  5662. ++this.state.pos;
  5663. this.finishToken(types.bracketL);
  5664. return;
  5665. case 93:
  5666. ++this.state.pos;
  5667. this.finishToken(types.bracketR);
  5668. return;
  5669. case 123:
  5670. ++this.state.pos;
  5671. this.finishToken(types.braceL);
  5672. return;
  5673. case 125:
  5674. ++this.state.pos;
  5675. this.finishToken(types.braceR);
  5676. return;
  5677. case 58:
  5678. if (this.hasPlugin("functionBind") && this.input.charCodeAt(this.state.pos + 1) === 58) {
  5679. this.finishOp(types.doubleColon, 2);
  5680. } else {
  5681. ++this.state.pos;
  5682. this.finishToken(types.colon);
  5683. }
  5684. return;
  5685. case 63:
  5686. this.readToken_question();
  5687. return;
  5688. case 96:
  5689. ++this.state.pos;
  5690. this.finishToken(types.backQuote);
  5691. return;
  5692. case 48:
  5693. {
  5694. const next = this.input.charCodeAt(this.state.pos + 1);
  5695. if (next === 120 || next === 88) {
  5696. this.readRadixNumber(16);
  5697. return;
  5698. }
  5699. if (next === 111 || next === 79) {
  5700. this.readRadixNumber(8);
  5701. return;
  5702. }
  5703. if (next === 98 || next === 66) {
  5704. this.readRadixNumber(2);
  5705. return;
  5706. }
  5707. }
  5708. case 49:
  5709. case 50:
  5710. case 51:
  5711. case 52:
  5712. case 53:
  5713. case 54:
  5714. case 55:
  5715. case 56:
  5716. case 57:
  5717. this.readNumber(false);
  5718. return;
  5719. case 34:
  5720. case 39:
  5721. this.readString(code);
  5722. return;
  5723. case 47:
  5724. this.readToken_slash();
  5725. return;
  5726. case 37:
  5727. case 42:
  5728. this.readToken_mult_modulo(code);
  5729. return;
  5730. case 124:
  5731. case 38:
  5732. this.readToken_pipe_amp(code);
  5733. return;
  5734. case 94:
  5735. this.readToken_caret();
  5736. return;
  5737. case 43:
  5738. case 45:
  5739. this.readToken_plus_min(code);
  5740. return;
  5741. case 60:
  5742. case 62:
  5743. this.readToken_lt_gt(code);
  5744. return;
  5745. case 61:
  5746. case 33:
  5747. this.readToken_eq_excl(code);
  5748. return;
  5749. case 126:
  5750. this.finishOp(types.tilde, 1);
  5751. return;
  5752. case 64:
  5753. ++this.state.pos;
  5754. this.finishToken(types.at);
  5755. return;
  5756. case 35:
  5757. this.readToken_numberSign();
  5758. return;
  5759. case 92:
  5760. this.readWord();
  5761. return;
  5762. default:
  5763. if (isIdentifierStart(code)) {
  5764. this.readWord();
  5765. return;
  5766. }
  5767. }
  5768. this.raise(this.state.pos, `Unexpected character '${String.fromCodePoint(code)}'`);
  5769. }
  5770. finishOp(type, size) {
  5771. const str = this.input.slice(this.state.pos, this.state.pos + size);
  5772. this.state.pos += size;
  5773. this.finishToken(type, str);
  5774. }
  5775. readRegexp() {
  5776. const start = this.state.pos;
  5777. let escaped, inClass;
  5778. for (;;) {
  5779. if (this.state.pos >= this.length) {
  5780. this.raise(start, "Unterminated regular expression");
  5781. }
  5782. const ch = this.input.charAt(this.state.pos);
  5783. if (lineBreak.test(ch)) {
  5784. this.raise(start, "Unterminated regular expression");
  5785. }
  5786. if (escaped) {
  5787. escaped = false;
  5788. } else {
  5789. if (ch === "[") {
  5790. inClass = true;
  5791. } else if (ch === "]" && inClass) {
  5792. inClass = false;
  5793. } else if (ch === "/" && !inClass) {
  5794. break;
  5795. }
  5796. escaped = ch === "\\";
  5797. }
  5798. ++this.state.pos;
  5799. }
  5800. const content = this.input.slice(start, this.state.pos);
  5801. ++this.state.pos;
  5802. let mods = "";
  5803. while (this.state.pos < this.length) {
  5804. const char = this.input[this.state.pos];
  5805. const charCode = this.input.codePointAt(this.state.pos);
  5806. if (VALID_REGEX_FLAGS.has(char)) {
  5807. if (mods.indexOf(char) > -1) {
  5808. this.raise(this.state.pos + 1, "Duplicate regular expression flag");
  5809. }
  5810. ++this.state.pos;
  5811. mods += char;
  5812. } else if (isIdentifierChar(charCode) || charCode === 92) {
  5813. this.raise(this.state.pos + 1, "Invalid regular expression flag");
  5814. } else {
  5815. break;
  5816. }
  5817. }
  5818. this.finishToken(types.regexp, {
  5819. pattern: content,
  5820. flags: mods
  5821. });
  5822. }
  5823. readInt(radix, len) {
  5824. const start = this.state.pos;
  5825. const forbiddenSiblings = radix === 16 ? forbiddenNumericSeparatorSiblings.hex : forbiddenNumericSeparatorSiblings.decBinOct;
  5826. const allowedSiblings = radix === 16 ? allowedNumericSeparatorSiblings.hex : radix === 10 ? allowedNumericSeparatorSiblings.dec : radix === 8 ? allowedNumericSeparatorSiblings.oct : allowedNumericSeparatorSiblings.bin;
  5827. let total = 0;
  5828. for (let i = 0, e = len == null ? Infinity : len; i < e; ++i) {
  5829. const code = this.input.charCodeAt(this.state.pos);
  5830. let val;
  5831. if (this.hasPlugin("numericSeparator")) {
  5832. const prev = this.input.charCodeAt(this.state.pos - 1);
  5833. const next = this.input.charCodeAt(this.state.pos + 1);
  5834. if (code === 95) {
  5835. if (allowedSiblings.indexOf(next) === -1) {
  5836. this.raise(this.state.pos, "Invalid or unexpected token");
  5837. }
  5838. if (forbiddenSiblings.indexOf(prev) > -1 || forbiddenSiblings.indexOf(next) > -1 || Number.isNaN(next)) {
  5839. this.raise(this.state.pos, "Invalid or unexpected token");
  5840. }
  5841. ++this.state.pos;
  5842. continue;
  5843. }
  5844. }
  5845. if (code >= 97) {
  5846. val = code - 97 + 10;
  5847. } else if (code >= 65) {
  5848. val = code - 65 + 10;
  5849. } else if (_isDigit(code)) {
  5850. val = code - 48;
  5851. } else {
  5852. val = Infinity;
  5853. }
  5854. if (val >= radix) break;
  5855. ++this.state.pos;
  5856. total = total * radix + val;
  5857. }
  5858. if (this.state.pos === start || len != null && this.state.pos - start !== len) {
  5859. return null;
  5860. }
  5861. return total;
  5862. }
  5863. readRadixNumber(radix) {
  5864. const start = this.state.pos;
  5865. let isBigInt = false;
  5866. this.state.pos += 2;
  5867. const val = this.readInt(radix);
  5868. if (val == null) {
  5869. this.raise(this.state.start + 2, "Expected number in radix " + radix);
  5870. }
  5871. if (this.hasPlugin("bigInt")) {
  5872. if (this.input.charCodeAt(this.state.pos) === 110) {
  5873. ++this.state.pos;
  5874. isBigInt = true;
  5875. }
  5876. }
  5877. if (isIdentifierStart(this.input.codePointAt(this.state.pos))) {
  5878. this.raise(this.state.pos, "Identifier directly after number");
  5879. }
  5880. if (isBigInt) {
  5881. const str = this.input.slice(start, this.state.pos).replace(/[_n]/g, "");
  5882. this.finishToken(types.bigint, str);
  5883. return;
  5884. }
  5885. this.finishToken(types.num, val);
  5886. }
  5887. readNumber(startsWithDot) {
  5888. const start = this.state.pos;
  5889. let isFloat = false;
  5890. let isBigInt = false;
  5891. if (!startsWithDot && this.readInt(10) === null) {
  5892. this.raise(start, "Invalid number");
  5893. }
  5894. let octal = this.state.pos - start >= 2 && this.input.charCodeAt(start) === 48;
  5895. if (octal) {
  5896. if (this.state.strict) {
  5897. this.raise(start, "Legacy octal literals are not allowed in strict mode");
  5898. }
  5899. if (/[89]/.test(this.input.slice(start, this.state.pos))) {
  5900. octal = false;
  5901. }
  5902. }
  5903. let next = this.input.charCodeAt(this.state.pos);
  5904. if (next === 46 && !octal) {
  5905. ++this.state.pos;
  5906. this.readInt(10);
  5907. isFloat = true;
  5908. next = this.input.charCodeAt(this.state.pos);
  5909. }
  5910. if ((next === 69 || next === 101) && !octal) {
  5911. next = this.input.charCodeAt(++this.state.pos);
  5912. if (next === 43 || next === 45) {
  5913. ++this.state.pos;
  5914. }
  5915. if (this.readInt(10) === null) this.raise(start, "Invalid number");
  5916. isFloat = true;
  5917. next = this.input.charCodeAt(this.state.pos);
  5918. }
  5919. if (this.hasPlugin("bigInt")) {
  5920. if (next === 110) {
  5921. if (isFloat || octal) this.raise(start, "Invalid BigIntLiteral");
  5922. ++this.state.pos;
  5923. isBigInt = true;
  5924. }
  5925. }
  5926. if (isIdentifierStart(this.input.codePointAt(this.state.pos))) {
  5927. this.raise(this.state.pos, "Identifier directly after number");
  5928. }
  5929. const str = this.input.slice(start, this.state.pos).replace(/[_n]/g, "");
  5930. if (isBigInt) {
  5931. this.finishToken(types.bigint, str);
  5932. return;
  5933. }
  5934. const val = octal ? parseInt(str, 8) : parseFloat(str);
  5935. this.finishToken(types.num, val);
  5936. }
  5937. readCodePoint(throwOnInvalid) {
  5938. const ch = this.input.charCodeAt(this.state.pos);
  5939. let code;
  5940. if (ch === 123) {
  5941. const codePos = ++this.state.pos;
  5942. code = this.readHexChar(this.input.indexOf("}", this.state.pos) - this.state.pos, throwOnInvalid);
  5943. ++this.state.pos;
  5944. if (code === null) {
  5945. --this.state.invalidTemplateEscapePosition;
  5946. } else if (code > 0x10ffff) {
  5947. if (throwOnInvalid) {
  5948. this.raise(codePos, "Code point out of bounds");
  5949. } else {
  5950. this.state.invalidTemplateEscapePosition = codePos - 2;
  5951. return null;
  5952. }
  5953. }
  5954. } else {
  5955. code = this.readHexChar(4, throwOnInvalid);
  5956. }
  5957. return code;
  5958. }
  5959. readString(quote) {
  5960. let out = "",
  5961. chunkStart = ++this.state.pos;
  5962. for (;;) {
  5963. if (this.state.pos >= this.length) {
  5964. this.raise(this.state.start, "Unterminated string constant");
  5965. }
  5966. const ch = this.input.charCodeAt(this.state.pos);
  5967. if (ch === quote) break;
  5968. if (ch === 92) {
  5969. out += this.input.slice(chunkStart, this.state.pos);
  5970. out += this.readEscapedChar(false);
  5971. chunkStart = this.state.pos;
  5972. } else if (ch === 8232 || ch === 8233) {
  5973. ++this.state.pos;
  5974. ++this.state.curLine;
  5975. } else if (isNewLine(ch)) {
  5976. this.raise(this.state.start, "Unterminated string constant");
  5977. } else {
  5978. ++this.state.pos;
  5979. }
  5980. }
  5981. out += this.input.slice(chunkStart, this.state.pos++);
  5982. this.finishToken(types.string, out);
  5983. }
  5984. readTmplToken() {
  5985. let out = "",
  5986. chunkStart = this.state.pos,
  5987. containsInvalid = false;
  5988. for (;;) {
  5989. if (this.state.pos >= this.length) {
  5990. this.raise(this.state.start, "Unterminated template");
  5991. }
  5992. const ch = this.input.charCodeAt(this.state.pos);
  5993. if (ch === 96 || ch === 36 && this.input.charCodeAt(this.state.pos + 1) === 123) {
  5994. if (this.state.pos === this.state.start && this.match(types.template)) {
  5995. if (ch === 36) {
  5996. this.state.pos += 2;
  5997. this.finishToken(types.dollarBraceL);
  5998. return;
  5999. } else {
  6000. ++this.state.pos;
  6001. this.finishToken(types.backQuote);
  6002. return;
  6003. }
  6004. }
  6005. out += this.input.slice(chunkStart, this.state.pos);
  6006. this.finishToken(types.template, containsInvalid ? null : out);
  6007. return;
  6008. }
  6009. if (ch === 92) {
  6010. out += this.input.slice(chunkStart, this.state.pos);
  6011. const escaped = this.readEscapedChar(true);
  6012. if (escaped === null) {
  6013. containsInvalid = true;
  6014. } else {
  6015. out += escaped;
  6016. }
  6017. chunkStart = this.state.pos;
  6018. } else if (isNewLine(ch)) {
  6019. out += this.input.slice(chunkStart, this.state.pos);
  6020. ++this.state.pos;
  6021. switch (ch) {
  6022. case 13:
  6023. if (this.input.charCodeAt(this.state.pos) === 10) {
  6024. ++this.state.pos;
  6025. }
  6026. case 10:
  6027. out += "\n";
  6028. break;
  6029. default:
  6030. out += String.fromCharCode(ch);
  6031. break;
  6032. }
  6033. ++this.state.curLine;
  6034. this.state.lineStart = this.state.pos;
  6035. chunkStart = this.state.pos;
  6036. } else {
  6037. ++this.state.pos;
  6038. }
  6039. }
  6040. }
  6041. readEscapedChar(inTemplate) {
  6042. const throwOnInvalid = !inTemplate;
  6043. const ch = this.input.charCodeAt(++this.state.pos);
  6044. ++this.state.pos;
  6045. switch (ch) {
  6046. case 110:
  6047. return "\n";
  6048. case 114:
  6049. return "\r";
  6050. case 120:
  6051. {
  6052. const code = this.readHexChar(2, throwOnInvalid);
  6053. return code === null ? null : String.fromCharCode(code);
  6054. }
  6055. case 117:
  6056. {
  6057. const code = this.readCodePoint(throwOnInvalid);
  6058. return code === null ? null : String.fromCodePoint(code);
  6059. }
  6060. case 116:
  6061. return "\t";
  6062. case 98:
  6063. return "\b";
  6064. case 118:
  6065. return "\u000b";
  6066. case 102:
  6067. return "\f";
  6068. case 13:
  6069. if (this.input.charCodeAt(this.state.pos) === 10) {
  6070. ++this.state.pos;
  6071. }
  6072. case 10:
  6073. this.state.lineStart = this.state.pos;
  6074. ++this.state.curLine;
  6075. case 8232:
  6076. case 8233:
  6077. return "";
  6078. default:
  6079. if (ch >= 48 && ch <= 55) {
  6080. const codePos = this.state.pos - 1;
  6081. let octalStr = this.input.substr(this.state.pos - 1, 3).match(/^[0-7]+/)[0];
  6082. let octal = parseInt(octalStr, 8);
  6083. if (octal > 255) {
  6084. octalStr = octalStr.slice(0, -1);
  6085. octal = parseInt(octalStr, 8);
  6086. }
  6087. this.state.pos += octalStr.length - 1;
  6088. const next = this.input.charCodeAt(this.state.pos);
  6089. if (octalStr !== "0" || next === 56 || next === 57) {
  6090. if (inTemplate) {
  6091. this.state.invalidTemplateEscapePosition = codePos;
  6092. return null;
  6093. } else if (this.state.strict) {
  6094. this.raise(codePos, "Octal literal in strict mode");
  6095. } else if (!this.state.containsOctal) {
  6096. this.state.containsOctal = true;
  6097. this.state.octalPosition = codePos;
  6098. }
  6099. }
  6100. return String.fromCharCode(octal);
  6101. }
  6102. return String.fromCharCode(ch);
  6103. }
  6104. }
  6105. readHexChar(len, throwOnInvalid) {
  6106. const codePos = this.state.pos;
  6107. const n = this.readInt(16, len);
  6108. if (n === null) {
  6109. if (throwOnInvalid) {
  6110. this.raise(codePos, "Bad character escape sequence");
  6111. } else {
  6112. this.state.pos = codePos - 1;
  6113. this.state.invalidTemplateEscapePosition = codePos - 1;
  6114. }
  6115. }
  6116. return n;
  6117. }
  6118. readWord1() {
  6119. let word = "";
  6120. this.state.containsEsc = false;
  6121. const start = this.state.pos;
  6122. let chunkStart = this.state.pos;
  6123. while (this.state.pos < this.length) {
  6124. const ch = this.input.codePointAt(this.state.pos);
  6125. if (isIdentifierChar(ch)) {
  6126. this.state.pos += ch <= 0xffff ? 1 : 2;
  6127. } else if (this.state.isIterator && ch === 64) {
  6128. ++this.state.pos;
  6129. } else if (ch === 92) {
  6130. this.state.containsEsc = true;
  6131. word += this.input.slice(chunkStart, this.state.pos);
  6132. const escStart = this.state.pos;
  6133. const identifierCheck = this.state.pos === start ? isIdentifierStart : isIdentifierChar;
  6134. if (this.input.charCodeAt(++this.state.pos) !== 117) {
  6135. this.raise(this.state.pos, "Expecting Unicode escape sequence \\uXXXX");
  6136. }
  6137. ++this.state.pos;
  6138. const esc = this.readCodePoint(true);
  6139. if (!identifierCheck(esc, true)) {
  6140. this.raise(escStart, "Invalid Unicode escape");
  6141. }
  6142. word += String.fromCodePoint(esc);
  6143. chunkStart = this.state.pos;
  6144. } else {
  6145. break;
  6146. }
  6147. }
  6148. return word + this.input.slice(chunkStart, this.state.pos);
  6149. }
  6150. isIterator(word) {
  6151. return word === "@@iterator" || word === "@@asyncIterator";
  6152. }
  6153. readWord() {
  6154. const word = this.readWord1();
  6155. const type = keywords.get(word) || types.name;
  6156. if (type.keyword && this.state.containsEsc) {
  6157. this.raise(this.state.pos, `Escape sequence in keyword ${word}`);
  6158. }
  6159. if (this.state.isIterator && (!this.isIterator(word) || !this.state.inType)) {
  6160. this.raise(this.state.pos, `Invalid identifier ${word}`);
  6161. }
  6162. this.finishToken(type, word);
  6163. }
  6164. braceIsBlock(prevType) {
  6165. const parent = this.curContext();
  6166. if (parent === types$1.functionExpression || parent === types$1.functionStatement) {
  6167. return true;
  6168. }
  6169. if (prevType === types.colon && (parent === types$1.braceStatement || parent === types$1.braceExpression)) {
  6170. return !parent.isExpr;
  6171. }
  6172. if (prevType === types._return || prevType === types.name && this.state.exprAllowed) {
  6173. return lineBreak.test(this.input.slice(this.state.lastTokEnd, this.state.start));
  6174. }
  6175. if (prevType === types._else || prevType === types.semi || prevType === types.eof || prevType === types.parenR || prevType === types.arrow) {
  6176. return true;
  6177. }
  6178. if (prevType === types.braceL) {
  6179. return parent === types$1.braceStatement;
  6180. }
  6181. if (prevType === types._var || prevType === types._const || prevType === types.name) {
  6182. return false;
  6183. }
  6184. if (prevType === types.relational) {
  6185. return true;
  6186. }
  6187. return !this.state.exprAllowed;
  6188. }
  6189. updateContext(prevType) {
  6190. const type = this.state.type;
  6191. let update;
  6192. if (type.keyword && (prevType === types.dot || prevType === types.questionDot)) {
  6193. this.state.exprAllowed = false;
  6194. } else if (update = type.updateContext) {
  6195. update.call(this, prevType);
  6196. } else {
  6197. this.state.exprAllowed = type.beforeExpr;
  6198. }
  6199. }
  6200. }
  6201. const literal = /^('|")((?:\\?.)*?)\1/;
  6202. class UtilParser extends Tokenizer {
  6203. addExtra(node, key, val) {
  6204. if (!node) return;
  6205. const extra = node.extra = node.extra || {};
  6206. extra[key] = val;
  6207. }
  6208. isRelational(op) {
  6209. return this.match(types.relational) && this.state.value === op;
  6210. }
  6211. isLookaheadRelational(op) {
  6212. const l = this.lookahead();
  6213. return l.type === types.relational && l.value === op;
  6214. }
  6215. expectRelational(op) {
  6216. if (this.isRelational(op)) {
  6217. this.next();
  6218. } else {
  6219. this.unexpected(null, types.relational);
  6220. }
  6221. }
  6222. eatRelational(op) {
  6223. if (this.isRelational(op)) {
  6224. this.next();
  6225. return true;
  6226. }
  6227. return false;
  6228. }
  6229. isContextual(name) {
  6230. return this.match(types.name) && this.state.value === name && !this.state.containsEsc;
  6231. }
  6232. isLookaheadContextual(name) {
  6233. const l = this.lookahead();
  6234. return l.type === types.name && l.value === name;
  6235. }
  6236. eatContextual(name) {
  6237. return this.isContextual(name) && this.eat(types.name);
  6238. }
  6239. expectContextual(name, message) {
  6240. if (!this.eatContextual(name)) this.unexpected(null, message);
  6241. }
  6242. canInsertSemicolon() {
  6243. return this.match(types.eof) || this.match(types.braceR) || this.hasPrecedingLineBreak();
  6244. }
  6245. hasPrecedingLineBreak() {
  6246. return lineBreak.test(this.input.slice(this.state.lastTokEnd, this.state.start));
  6247. }
  6248. isLineTerminator() {
  6249. return this.eat(types.semi) || this.canInsertSemicolon();
  6250. }
  6251. semicolon() {
  6252. if (!this.isLineTerminator()) this.unexpected(null, types.semi);
  6253. }
  6254. expect(type, pos) {
  6255. this.eat(type) || this.unexpected(pos, type);
  6256. }
  6257. assertNoSpace(message = "Unexpected space.") {
  6258. if (this.state.start > this.state.lastTokEnd) {
  6259. this.raise(this.state.lastTokEnd, message);
  6260. }
  6261. }
  6262. unexpected(pos, messageOrType = "Unexpected token") {
  6263. if (typeof messageOrType !== "string") {
  6264. messageOrType = `Unexpected token, expected "${messageOrType.label}"`;
  6265. }
  6266. throw this.raise(pos != null ? pos : this.state.start, messageOrType);
  6267. }
  6268. expectPlugin(name, pos) {
  6269. if (!this.hasPlugin(name)) {
  6270. throw this.raise(pos != null ? pos : this.state.start, `This experimental syntax requires enabling the parser plugin: '${name}'`, {
  6271. missingPluginNames: [name]
  6272. });
  6273. }
  6274. return true;
  6275. }
  6276. expectOnePlugin(names, pos) {
  6277. if (!names.some(n => this.hasPlugin(n))) {
  6278. throw this.raise(pos != null ? pos : this.state.start, `This experimental syntax requires enabling one of the following parser plugin(s): '${names.join(", ")}'`, {
  6279. missingPluginNames: names
  6280. });
  6281. }
  6282. }
  6283. checkYieldAwaitInDefaultParams() {
  6284. if (this.state.yieldPos && (!this.state.awaitPos || this.state.yieldPos < this.state.awaitPos)) {
  6285. this.raise(this.state.yieldPos, "Yield cannot be used as name inside a generator function");
  6286. }
  6287. if (this.state.awaitPos) {
  6288. this.raise(this.state.awaitPos, "Await cannot be used as name inside an async function");
  6289. }
  6290. }
  6291. strictDirective(start) {
  6292. for (;;) {
  6293. skipWhiteSpace.lastIndex = start;
  6294. start += skipWhiteSpace.exec(this.input)[0].length;
  6295. const match = literal.exec(this.input.slice(start));
  6296. if (!match) break;
  6297. if (match[2] === "use strict") return true;
  6298. start += match[0].length;
  6299. skipWhiteSpace.lastIndex = start;
  6300. start += skipWhiteSpace.exec(this.input)[0].length;
  6301. if (this.input[start] === ";") {
  6302. start++;
  6303. }
  6304. }
  6305. return false;
  6306. }
  6307. }
  6308. class Node {
  6309. constructor(parser, pos, loc) {
  6310. this.type = "";
  6311. this.start = pos;
  6312. this.end = 0;
  6313. this.loc = new SourceLocation(loc);
  6314. if (parser && parser.options.ranges) this.range = [pos, 0];
  6315. if (parser && parser.filename) this.loc.filename = parser.filename;
  6316. }
  6317. __clone() {
  6318. const newNode = new Node();
  6319. const keys = Object.keys(this);
  6320. for (let i = 0, length = keys.length; i < length; i++) {
  6321. const key = keys[i];
  6322. if (key !== "leadingComments" && key !== "trailingComments" && key !== "innerComments") {
  6323. newNode[key] = this[key];
  6324. }
  6325. }
  6326. return newNode;
  6327. }
  6328. }
  6329. class NodeUtils extends UtilParser {
  6330. startNode() {
  6331. return new Node(this, this.state.start, this.state.startLoc);
  6332. }
  6333. startNodeAt(pos, loc) {
  6334. return new Node(this, pos, loc);
  6335. }
  6336. startNodeAtNode(type) {
  6337. return this.startNodeAt(type.start, type.loc.start);
  6338. }
  6339. finishNode(node, type) {
  6340. return this.finishNodeAt(node, type, this.state.lastTokEnd, this.state.lastTokEndLoc);
  6341. }
  6342. finishNodeAt(node, type, pos, loc) {
  6343. node.type = type;
  6344. node.end = pos;
  6345. node.loc.end = loc;
  6346. if (this.options.ranges) node.range[1] = pos;
  6347. this.processComment(node);
  6348. return node;
  6349. }
  6350. resetStartLocation(node, start, startLoc) {
  6351. node.start = start;
  6352. node.loc.start = startLoc;
  6353. if (this.options.ranges) node.range[0] = start;
  6354. }
  6355. resetEndLocation(node, end = this.state.lastTokEnd, endLoc = this.state.lastTokEndLoc) {
  6356. node.end = end;
  6357. node.loc.end = endLoc;
  6358. if (this.options.ranges) node.range[1] = end;
  6359. }
  6360. resetStartLocationFromNode(node, locationNode) {
  6361. this.resetStartLocation(node, locationNode.start, locationNode.loc.start);
  6362. }
  6363. }
  6364. class LValParser extends NodeUtils {
  6365. toAssignable(node, isBinding, contextDescription) {
  6366. if (node) {
  6367. switch (node.type) {
  6368. case "Identifier":
  6369. case "ObjectPattern":
  6370. case "ArrayPattern":
  6371. case "AssignmentPattern":
  6372. break;
  6373. case "ObjectExpression":
  6374. node.type = "ObjectPattern";
  6375. for (let i = 0, length = node.properties.length, last = length - 1; i < length; i++) {
  6376. const prop = node.properties[i];
  6377. const isLast = i === last;
  6378. this.toAssignableObjectExpressionProp(prop, isBinding, isLast);
  6379. }
  6380. break;
  6381. case "ObjectProperty":
  6382. this.toAssignable(node.value, isBinding, contextDescription);
  6383. break;
  6384. case "SpreadElement":
  6385. {
  6386. this.checkToRestConversion(node);
  6387. node.type = "RestElement";
  6388. const arg = node.argument;
  6389. this.toAssignable(arg, isBinding, contextDescription);
  6390. break;
  6391. }
  6392. case "ArrayExpression":
  6393. node.type = "ArrayPattern";
  6394. this.toAssignableList(node.elements, isBinding, contextDescription);
  6395. break;
  6396. case "AssignmentExpression":
  6397. if (node.operator === "=") {
  6398. node.type = "AssignmentPattern";
  6399. delete node.operator;
  6400. } else {
  6401. this.raise(node.left.end, "Only '=' operator can be used for specifying default value.");
  6402. }
  6403. break;
  6404. case "ParenthesizedExpression":
  6405. node.expression = this.toAssignable(node.expression, isBinding, contextDescription);
  6406. break;
  6407. case "MemberExpression":
  6408. if (!isBinding) break;
  6409. default:
  6410. {
  6411. const message = "Invalid left-hand side" + (contextDescription ? " in " + contextDescription : "expression");
  6412. this.raise(node.start, message);
  6413. }
  6414. }
  6415. }
  6416. return node;
  6417. }
  6418. toAssignableObjectExpressionProp(prop, isBinding, isLast) {
  6419. if (prop.type === "ObjectMethod") {
  6420. const error = prop.kind === "get" || prop.kind === "set" ? "Object pattern can't contain getter or setter" : "Object pattern can't contain methods";
  6421. this.raise(prop.key.start, error);
  6422. } else if (prop.type === "SpreadElement" && !isLast) {
  6423. this.raiseRestNotLast(prop.start);
  6424. } else {
  6425. this.toAssignable(prop, isBinding, "object destructuring pattern");
  6426. }
  6427. }
  6428. toAssignableList(exprList, isBinding, contextDescription) {
  6429. let end = exprList.length;
  6430. if (end) {
  6431. const last = exprList[end - 1];
  6432. if (last && last.type === "RestElement") {
  6433. --end;
  6434. } else if (last && last.type === "SpreadElement") {
  6435. last.type = "RestElement";
  6436. const arg = last.argument;
  6437. this.toAssignable(arg, isBinding, contextDescription);
  6438. if (arg.type !== "Identifier" && arg.type !== "MemberExpression" && arg.type !== "ArrayPattern" && arg.type !== "ObjectPattern") {
  6439. this.unexpected(arg.start);
  6440. }
  6441. --end;
  6442. }
  6443. }
  6444. for (let i = 0; i < end; i++) {
  6445. const elt = exprList[i];
  6446. if (elt) {
  6447. this.toAssignable(elt, isBinding, contextDescription);
  6448. if (elt.type === "RestElement") {
  6449. this.raiseRestNotLast(elt.start);
  6450. }
  6451. }
  6452. }
  6453. return exprList;
  6454. }
  6455. toReferencedList(exprList, isParenthesizedExpr) {
  6456. return exprList;
  6457. }
  6458. toReferencedListDeep(exprList, isParenthesizedExpr) {
  6459. this.toReferencedList(exprList, isParenthesizedExpr);
  6460. for (let _i = 0; _i < exprList.length; _i++) {
  6461. const expr = exprList[_i];
  6462. if (expr && expr.type === "ArrayExpression") {
  6463. this.toReferencedListDeep(expr.elements);
  6464. }
  6465. }
  6466. return exprList;
  6467. }
  6468. parseSpread(refShorthandDefaultPos, refNeedsArrowPos) {
  6469. const node = this.startNode();
  6470. this.next();
  6471. node.argument = this.parseMaybeAssign(false, refShorthandDefaultPos, undefined, refNeedsArrowPos);
  6472. if (this.state.commaAfterSpreadAt === -1 && this.match(types.comma)) {
  6473. this.state.commaAfterSpreadAt = this.state.start;
  6474. }
  6475. return this.finishNode(node, "SpreadElement");
  6476. }
  6477. parseRestBinding() {
  6478. const node = this.startNode();
  6479. this.next();
  6480. node.argument = this.parseBindingAtom();
  6481. return this.finishNode(node, "RestElement");
  6482. }
  6483. parseBindingAtom() {
  6484. switch (this.state.type) {
  6485. case types.bracketL:
  6486. {
  6487. const node = this.startNode();
  6488. this.next();
  6489. node.elements = this.parseBindingList(types.bracketR, true);
  6490. return this.finishNode(node, "ArrayPattern");
  6491. }
  6492. case types.braceL:
  6493. return this.parseObj(true);
  6494. }
  6495. return this.parseIdentifier();
  6496. }
  6497. parseBindingList(close, allowEmpty, allowModifiers) {
  6498. const elts = [];
  6499. let first = true;
  6500. while (!this.eat(close)) {
  6501. if (first) {
  6502. first = false;
  6503. } else {
  6504. this.expect(types.comma);
  6505. }
  6506. if (allowEmpty && this.match(types.comma)) {
  6507. elts.push(null);
  6508. } else if (this.eat(close)) {
  6509. break;
  6510. } else if (this.match(types.ellipsis)) {
  6511. elts.push(this.parseAssignableListItemTypes(this.parseRestBinding()));
  6512. this.checkCommaAfterRest();
  6513. this.expect(close);
  6514. break;
  6515. } else {
  6516. const decorators = [];
  6517. if (this.match(types.at) && this.hasPlugin("decorators")) {
  6518. this.raise(this.state.start, "Stage 2 decorators cannot be used to decorate parameters");
  6519. }
  6520. while (this.match(types.at)) {
  6521. decorators.push(this.parseDecorator());
  6522. }
  6523. elts.push(this.parseAssignableListItem(allowModifiers, decorators));
  6524. }
  6525. }
  6526. return elts;
  6527. }
  6528. parseAssignableListItem(allowModifiers, decorators) {
  6529. const left = this.parseMaybeDefault();
  6530. this.parseAssignableListItemTypes(left);
  6531. const elt = this.parseMaybeDefault(left.start, left.loc.start, left);
  6532. if (decorators.length) {
  6533. left.decorators = decorators;
  6534. }
  6535. return elt;
  6536. }
  6537. parseAssignableListItemTypes(param) {
  6538. return param;
  6539. }
  6540. parseMaybeDefault(startPos, startLoc, left) {
  6541. startLoc = startLoc || this.state.startLoc;
  6542. startPos = startPos || this.state.start;
  6543. left = left || this.parseBindingAtom();
  6544. if (!this.eat(types.eq)) return left;
  6545. const node = this.startNodeAt(startPos, startLoc);
  6546. node.left = left;
  6547. node.right = this.parseMaybeAssign();
  6548. return this.finishNode(node, "AssignmentPattern");
  6549. }
  6550. checkLVal(expr, bindingType = BIND_NONE, checkClashes, contextDescription) {
  6551. switch (expr.type) {
  6552. case "Identifier":
  6553. if (this.state.strict && isStrictBindReservedWord(expr.name, this.inModule)) {
  6554. this.raise(expr.start, `${bindingType === BIND_NONE ? "Assigning to" : "Binding"} '${expr.name}' in strict mode`);
  6555. }
  6556. if (checkClashes) {
  6557. const key = `_${expr.name}`;
  6558. if (checkClashes[key]) {
  6559. this.raise(expr.start, "Argument name clash");
  6560. } else {
  6561. checkClashes[key] = true;
  6562. }
  6563. }
  6564. if (!(bindingType & BIND_NONE)) {
  6565. this.scope.declareName(expr.name, bindingType, expr.start);
  6566. }
  6567. break;
  6568. case "MemberExpression":
  6569. if (bindingType !== BIND_NONE) {
  6570. this.raise(expr.start, "Binding member expression");
  6571. }
  6572. break;
  6573. case "ObjectPattern":
  6574. for (let _i2 = 0, _expr$properties = expr.properties; _i2 < _expr$properties.length; _i2++) {
  6575. let prop = _expr$properties[_i2];
  6576. if (prop.type === "ObjectProperty") prop = prop.value;
  6577. this.checkLVal(prop, bindingType, checkClashes, "object destructuring pattern");
  6578. }
  6579. break;
  6580. case "ArrayPattern":
  6581. for (let _i3 = 0, _expr$elements = expr.elements; _i3 < _expr$elements.length; _i3++) {
  6582. const elem = _expr$elements[_i3];
  6583. if (elem) {
  6584. this.checkLVal(elem, bindingType, checkClashes, "array destructuring pattern");
  6585. }
  6586. }
  6587. break;
  6588. case "AssignmentPattern":
  6589. this.checkLVal(expr.left, bindingType, checkClashes, "assignment pattern");
  6590. break;
  6591. case "RestElement":
  6592. this.checkLVal(expr.argument, bindingType, checkClashes, "rest element");
  6593. break;
  6594. case "ParenthesizedExpression":
  6595. this.checkLVal(expr.expression, bindingType, checkClashes, "parenthesized expression");
  6596. break;
  6597. default:
  6598. {
  6599. const message = (bindingType === BIND_NONE ? "Invalid" : "Binding invalid") + " left-hand side" + (contextDescription ? " in " + contextDescription : "expression");
  6600. this.raise(expr.start, message);
  6601. }
  6602. }
  6603. }
  6604. checkToRestConversion(node) {
  6605. if (node.argument.type !== "Identifier" && node.argument.type !== "MemberExpression") {
  6606. this.raise(node.argument.start, "Invalid rest operator's argument");
  6607. }
  6608. }
  6609. checkCommaAfterRest() {
  6610. if (this.match(types.comma)) {
  6611. this.raiseRestNotLast(this.state.start);
  6612. }
  6613. }
  6614. checkCommaAfterRestFromSpread() {
  6615. if (this.state.commaAfterSpreadAt > -1) {
  6616. this.raiseRestNotLast(this.state.commaAfterSpreadAt);
  6617. }
  6618. }
  6619. raiseRestNotLast(pos) {
  6620. this.raise(pos, `Rest element must be last element`);
  6621. }
  6622. }
  6623. const unwrapParenthesizedExpression = node => {
  6624. return node.type === "ParenthesizedExpression" ? unwrapParenthesizedExpression(node.expression) : node;
  6625. };
  6626. class ExpressionParser extends LValParser {
  6627. checkPropClash(prop, propHash) {
  6628. if (prop.type === "SpreadElement" || prop.computed || prop.kind || prop.shorthand) {
  6629. return;
  6630. }
  6631. const key = prop.key;
  6632. const name = key.type === "Identifier" ? key.name : String(key.value);
  6633. if (name === "__proto__") {
  6634. if (propHash.proto) {
  6635. this.raise(key.start, "Redefinition of __proto__ property");
  6636. }
  6637. propHash.proto = true;
  6638. }
  6639. }
  6640. getExpression() {
  6641. this.scope.enter(SCOPE_PROGRAM);
  6642. this.nextToken();
  6643. const expr = this.parseExpression();
  6644. if (!this.match(types.eof)) {
  6645. this.unexpected();
  6646. }
  6647. expr.comments = this.state.comments;
  6648. return expr;
  6649. }
  6650. parseExpression(noIn, refShorthandDefaultPos) {
  6651. const startPos = this.state.start;
  6652. const startLoc = this.state.startLoc;
  6653. const expr = this.parseMaybeAssign(noIn, refShorthandDefaultPos);
  6654. if (this.match(types.comma)) {
  6655. const node = this.startNodeAt(startPos, startLoc);
  6656. node.expressions = [expr];
  6657. while (this.eat(types.comma)) {
  6658. node.expressions.push(this.parseMaybeAssign(noIn, refShorthandDefaultPos));
  6659. }
  6660. this.toReferencedList(node.expressions);
  6661. return this.finishNode(node, "SequenceExpression");
  6662. }
  6663. return expr;
  6664. }
  6665. parseMaybeAssign(noIn, refShorthandDefaultPos, afterLeftParse, refNeedsArrowPos) {
  6666. const startPos = this.state.start;
  6667. const startLoc = this.state.startLoc;
  6668. if (this.isContextual("yield")) {
  6669. if (this.scope.inGenerator) {
  6670. let left = this.parseYield(noIn);
  6671. if (afterLeftParse) {
  6672. left = afterLeftParse.call(this, left, startPos, startLoc);
  6673. }
  6674. return left;
  6675. } else {
  6676. this.state.exprAllowed = false;
  6677. }
  6678. }
  6679. const oldCommaAfterSpreadAt = this.state.commaAfterSpreadAt;
  6680. this.state.commaAfterSpreadAt = -1;
  6681. let failOnShorthandAssign;
  6682. if (refShorthandDefaultPos) {
  6683. failOnShorthandAssign = false;
  6684. } else {
  6685. refShorthandDefaultPos = {
  6686. start: 0
  6687. };
  6688. failOnShorthandAssign = true;
  6689. }
  6690. if (this.match(types.parenL) || this.match(types.name)) {
  6691. this.state.potentialArrowAt = this.state.start;
  6692. }
  6693. let left = this.parseMaybeConditional(noIn, refShorthandDefaultPos, refNeedsArrowPos);
  6694. if (afterLeftParse) {
  6695. left = afterLeftParse.call(this, left, startPos, startLoc);
  6696. }
  6697. if (this.state.type.isAssign) {
  6698. const node = this.startNodeAt(startPos, startLoc);
  6699. const operator = this.state.value;
  6700. node.operator = operator;
  6701. if (operator === "??=") {
  6702. this.expectPlugin("nullishCoalescingOperator");
  6703. this.expectPlugin("logicalAssignment");
  6704. }
  6705. if (operator === "||=" || operator === "&&=") {
  6706. this.expectPlugin("logicalAssignment");
  6707. }
  6708. node.left = this.match(types.eq) ? this.toAssignable(left, undefined, "assignment expression") : left;
  6709. refShorthandDefaultPos.start = 0;
  6710. this.checkLVal(left, undefined, undefined, "assignment expression");
  6711. const maybePattern = unwrapParenthesizedExpression(left);
  6712. let patternErrorMsg;
  6713. if (maybePattern.type === "ObjectPattern") {
  6714. patternErrorMsg = "`({a}) = 0` use `({a} = 0)`";
  6715. } else if (maybePattern.type === "ArrayPattern") {
  6716. patternErrorMsg = "`([a]) = 0` use `([a] = 0)`";
  6717. }
  6718. if (patternErrorMsg && (left.extra && left.extra.parenthesized || left.type === "ParenthesizedExpression")) {
  6719. this.raise(maybePattern.start, `You're trying to assign to a parenthesized expression, eg. instead of ${patternErrorMsg}`);
  6720. }
  6721. if (patternErrorMsg) this.checkCommaAfterRestFromSpread();
  6722. this.state.commaAfterSpreadAt = oldCommaAfterSpreadAt;
  6723. this.next();
  6724. node.right = this.parseMaybeAssign(noIn);
  6725. return this.finishNode(node, "AssignmentExpression");
  6726. } else if (failOnShorthandAssign && refShorthandDefaultPos.start) {
  6727. this.unexpected(refShorthandDefaultPos.start);
  6728. }
  6729. this.state.commaAfterSpreadAt = oldCommaAfterSpreadAt;
  6730. return left;
  6731. }
  6732. parseMaybeConditional(noIn, refShorthandDefaultPos, refNeedsArrowPos) {
  6733. const startPos = this.state.start;
  6734. const startLoc = this.state.startLoc;
  6735. const potentialArrowAt = this.state.potentialArrowAt;
  6736. const expr = this.parseExprOps(noIn, refShorthandDefaultPos);
  6737. if (expr.type === "ArrowFunctionExpression" && expr.start === potentialArrowAt) {
  6738. return expr;
  6739. }
  6740. if (refShorthandDefaultPos && refShorthandDefaultPos.start) return expr;
  6741. return this.parseConditional(expr, noIn, startPos, startLoc, refNeedsArrowPos);
  6742. }
  6743. parseConditional(expr, noIn, startPos, startLoc, refNeedsArrowPos) {
  6744. if (this.eat(types.question)) {
  6745. const node = this.startNodeAt(startPos, startLoc);
  6746. node.test = expr;
  6747. node.consequent = this.parseMaybeAssign();
  6748. this.expect(types.colon);
  6749. node.alternate = this.parseMaybeAssign(noIn);
  6750. return this.finishNode(node, "ConditionalExpression");
  6751. }
  6752. return expr;
  6753. }
  6754. parseExprOps(noIn, refShorthandDefaultPos) {
  6755. const startPos = this.state.start;
  6756. const startLoc = this.state.startLoc;
  6757. const potentialArrowAt = this.state.potentialArrowAt;
  6758. const expr = this.parseMaybeUnary(refShorthandDefaultPos);
  6759. if (expr.type === "ArrowFunctionExpression" && expr.start === potentialArrowAt) {
  6760. return expr;
  6761. }
  6762. if (refShorthandDefaultPos && refShorthandDefaultPos.start) {
  6763. return expr;
  6764. }
  6765. return this.parseExprOp(expr, startPos, startLoc, -1, noIn);
  6766. }
  6767. parseExprOp(left, leftStartPos, leftStartLoc, minPrec, noIn) {
  6768. const prec = this.state.type.binop;
  6769. if (prec != null && (!noIn || !this.match(types._in))) {
  6770. if (prec > minPrec) {
  6771. const node = this.startNodeAt(leftStartPos, leftStartLoc);
  6772. const operator = this.state.value;
  6773. node.left = left;
  6774. node.operator = operator;
  6775. if (operator === "**" && left.type === "UnaryExpression" && (this.options.createParenthesizedExpressions || !(left.extra && left.extra.parenthesized))) {
  6776. this.raise(left.argument.start, "Illegal expression. Wrap left hand side or entire exponentiation in parentheses.");
  6777. }
  6778. const op = this.state.type;
  6779. if (op === types.pipeline) {
  6780. this.expectPlugin("pipelineOperator");
  6781. this.state.inPipeline = true;
  6782. this.checkPipelineAtInfixOperator(left, leftStartPos);
  6783. } else if (op === types.nullishCoalescing) {
  6784. this.expectPlugin("nullishCoalescingOperator");
  6785. }
  6786. this.next();
  6787. if (op === types.pipeline && this.getPluginOption("pipelineOperator", "proposal") === "minimal") {
  6788. if (this.match(types.name) && this.state.value === "await" && this.scope.inAsync) {
  6789. throw this.raise(this.state.start, `Unexpected "await" after pipeline body; await must have parentheses in minimal proposal`);
  6790. }
  6791. }
  6792. node.right = this.parseExprOpRightExpr(op, prec, noIn);
  6793. this.finishNode(node, op === types.logicalOR || op === types.logicalAND || op === types.nullishCoalescing ? "LogicalExpression" : "BinaryExpression");
  6794. return this.parseExprOp(node, leftStartPos, leftStartLoc, minPrec, noIn);
  6795. }
  6796. }
  6797. return left;
  6798. }
  6799. parseExprOpRightExpr(op, prec, noIn) {
  6800. switch (op) {
  6801. case types.pipeline:
  6802. if (this.getPluginOption("pipelineOperator", "proposal") === "smart") {
  6803. const startPos = this.state.start;
  6804. const startLoc = this.state.startLoc;
  6805. return this.withTopicPermittingContext(() => {
  6806. return this.parseSmartPipelineBody(this.parseExprOpBaseRightExpr(op, prec, noIn), startPos, startLoc);
  6807. });
  6808. }
  6809. default:
  6810. return this.parseExprOpBaseRightExpr(op, prec, noIn);
  6811. }
  6812. }
  6813. parseExprOpBaseRightExpr(op, prec, noIn) {
  6814. const startPos = this.state.start;
  6815. const startLoc = this.state.startLoc;
  6816. return this.parseExprOp(this.parseMaybeUnary(), startPos, startLoc, op.rightAssociative ? prec - 1 : prec, noIn);
  6817. }
  6818. parseMaybeUnary(refShorthandDefaultPos) {
  6819. if (this.isContextual("await") && (this.scope.inAsync || !this.scope.inFunction && this.options.allowAwaitOutsideFunction)) {
  6820. return this.parseAwait();
  6821. } else if (this.state.type.prefix) {
  6822. const node = this.startNode();
  6823. const update = this.match(types.incDec);
  6824. node.operator = this.state.value;
  6825. node.prefix = true;
  6826. if (node.operator === "throw") {
  6827. this.expectPlugin("throwExpressions");
  6828. }
  6829. this.next();
  6830. node.argument = this.parseMaybeUnary();
  6831. if (refShorthandDefaultPos && refShorthandDefaultPos.start) {
  6832. this.unexpected(refShorthandDefaultPos.start);
  6833. }
  6834. if (update) {
  6835. this.checkLVal(node.argument, undefined, undefined, "prefix operation");
  6836. } else if (this.state.strict && node.operator === "delete") {
  6837. const arg = node.argument;
  6838. if (arg.type === "Identifier") {
  6839. this.raise(node.start, "Deleting local variable in strict mode");
  6840. } else if (arg.type === "MemberExpression" && arg.property.type === "PrivateName") {
  6841. this.raise(node.start, "Deleting a private field is not allowed");
  6842. }
  6843. }
  6844. return this.finishNode(node, update ? "UpdateExpression" : "UnaryExpression");
  6845. }
  6846. const startPos = this.state.start;
  6847. const startLoc = this.state.startLoc;
  6848. let expr = this.parseExprSubscripts(refShorthandDefaultPos);
  6849. if (refShorthandDefaultPos && refShorthandDefaultPos.start) return expr;
  6850. while (this.state.type.postfix && !this.canInsertSemicolon()) {
  6851. const node = this.startNodeAt(startPos, startLoc);
  6852. node.operator = this.state.value;
  6853. node.prefix = false;
  6854. node.argument = expr;
  6855. this.checkLVal(expr, undefined, undefined, "postfix operation");
  6856. this.next();
  6857. expr = this.finishNode(node, "UpdateExpression");
  6858. }
  6859. return expr;
  6860. }
  6861. parseExprSubscripts(refShorthandDefaultPos) {
  6862. const startPos = this.state.start;
  6863. const startLoc = this.state.startLoc;
  6864. const potentialArrowAt = this.state.potentialArrowAt;
  6865. const expr = this.parseExprAtom(refShorthandDefaultPos);
  6866. if (expr.type === "ArrowFunctionExpression" && expr.start === potentialArrowAt) {
  6867. return expr;
  6868. }
  6869. if (refShorthandDefaultPos && refShorthandDefaultPos.start) {
  6870. return expr;
  6871. }
  6872. return this.parseSubscripts(expr, startPos, startLoc);
  6873. }
  6874. parseSubscripts(base, startPos, startLoc, noCalls) {
  6875. const maybeAsyncArrow = this.atPossibleAsync(base);
  6876. const state = {
  6877. optionalChainMember: false,
  6878. stop: false
  6879. };
  6880. do {
  6881. base = this.parseSubscript(base, startPos, startLoc, noCalls, state, maybeAsyncArrow);
  6882. } while (!state.stop);
  6883. return base;
  6884. }
  6885. parseSubscript(base, startPos, startLoc, noCalls, state, maybeAsyncArrow) {
  6886. if (!noCalls && this.eat(types.doubleColon)) {
  6887. const node = this.startNodeAt(startPos, startLoc);
  6888. node.object = base;
  6889. node.callee = this.parseNoCallExpr();
  6890. state.stop = true;
  6891. return this.parseSubscripts(this.finishNode(node, "BindExpression"), startPos, startLoc, noCalls);
  6892. } else if (this.match(types.questionDot)) {
  6893. this.expectPlugin("optionalChaining");
  6894. state.optionalChainMember = true;
  6895. if (noCalls && this.lookahead().type === types.parenL) {
  6896. state.stop = true;
  6897. return base;
  6898. }
  6899. this.next();
  6900. const node = this.startNodeAt(startPos, startLoc);
  6901. if (this.eat(types.bracketL)) {
  6902. node.object = base;
  6903. node.property = this.parseExpression();
  6904. node.computed = true;
  6905. node.optional = true;
  6906. this.expect(types.bracketR);
  6907. return this.finishNode(node, "OptionalMemberExpression");
  6908. } else if (this.eat(types.parenL)) {
  6909. node.callee = base;
  6910. node.arguments = this.parseCallExpressionArguments(types.parenR, false);
  6911. node.optional = true;
  6912. return this.finishNode(node, "OptionalCallExpression");
  6913. } else {
  6914. node.object = base;
  6915. node.property = this.parseIdentifier(true);
  6916. node.computed = false;
  6917. node.optional = true;
  6918. return this.finishNode(node, "OptionalMemberExpression");
  6919. }
  6920. } else if (this.eat(types.dot)) {
  6921. const node = this.startNodeAt(startPos, startLoc);
  6922. node.object = base;
  6923. node.property = this.parseMaybePrivateName();
  6924. node.computed = false;
  6925. if (state.optionalChainMember) {
  6926. node.optional = false;
  6927. return this.finishNode(node, "OptionalMemberExpression");
  6928. }
  6929. return this.finishNode(node, "MemberExpression");
  6930. } else if (this.eat(types.bracketL)) {
  6931. const node = this.startNodeAt(startPos, startLoc);
  6932. node.object = base;
  6933. node.property = this.parseExpression();
  6934. node.computed = true;
  6935. this.expect(types.bracketR);
  6936. if (state.optionalChainMember) {
  6937. node.optional = false;
  6938. return this.finishNode(node, "OptionalMemberExpression");
  6939. }
  6940. return this.finishNode(node, "MemberExpression");
  6941. } else if (!noCalls && this.match(types.parenL)) {
  6942. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  6943. const oldYieldPos = this.state.yieldPos;
  6944. const oldAwaitPos = this.state.awaitPos;
  6945. this.state.maybeInArrowParameters = true;
  6946. this.state.yieldPos = 0;
  6947. this.state.awaitPos = 0;
  6948. this.next();
  6949. let node = this.startNodeAt(startPos, startLoc);
  6950. node.callee = base;
  6951. const oldCommaAfterSpreadAt = this.state.commaAfterSpreadAt;
  6952. this.state.commaAfterSpreadAt = -1;
  6953. node.arguments = this.parseCallExpressionArguments(types.parenR, maybeAsyncArrow, base.type === "Import", base.type !== "Super");
  6954. if (!state.optionalChainMember) {
  6955. this.finishCallExpression(node);
  6956. } else {
  6957. this.finishOptionalCallExpression(node);
  6958. }
  6959. if (maybeAsyncArrow && this.shouldParseAsyncArrow()) {
  6960. state.stop = true;
  6961. this.checkCommaAfterRestFromSpread();
  6962. node = this.parseAsyncArrowFromCallExpression(this.startNodeAt(startPos, startLoc), node);
  6963. this.checkYieldAwaitInDefaultParams();
  6964. this.state.yieldPos = oldYieldPos;
  6965. this.state.awaitPos = oldAwaitPos;
  6966. } else {
  6967. this.toReferencedListDeep(node.arguments);
  6968. this.state.yieldPos = oldYieldPos || this.state.yieldPos;
  6969. this.state.awaitPos = oldAwaitPos || this.state.awaitPos;
  6970. }
  6971. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  6972. this.state.commaAfterSpreadAt = oldCommaAfterSpreadAt;
  6973. return node;
  6974. } else if (this.match(types.backQuote)) {
  6975. return this.parseTaggedTemplateExpression(startPos, startLoc, base, state);
  6976. } else {
  6977. state.stop = true;
  6978. return base;
  6979. }
  6980. }
  6981. parseTaggedTemplateExpression(startPos, startLoc, base, state, typeArguments) {
  6982. const node = this.startNodeAt(startPos, startLoc);
  6983. node.tag = base;
  6984. node.quasi = this.parseTemplate(true);
  6985. if (typeArguments) node.typeParameters = typeArguments;
  6986. if (state.optionalChainMember) {
  6987. this.raise(startPos, "Tagged Template Literals are not allowed in optionalChain");
  6988. }
  6989. return this.finishNode(node, "TaggedTemplateExpression");
  6990. }
  6991. atPossibleAsync(base) {
  6992. return base.type === "Identifier" && base.name === "async" && this.state.lastTokEnd === base.end && !this.canInsertSemicolon() && this.input.slice(base.start, base.end) === "async";
  6993. }
  6994. finishCallExpression(node) {
  6995. if (node.callee.type === "Import") {
  6996. if (node.arguments.length !== 1) {
  6997. this.raise(node.start, "import() requires exactly one argument");
  6998. }
  6999. const importArg = node.arguments[0];
  7000. if (importArg && importArg.type === "SpreadElement") {
  7001. this.raise(importArg.start, "... is not allowed in import()");
  7002. }
  7003. }
  7004. return this.finishNode(node, "CallExpression");
  7005. }
  7006. finishOptionalCallExpression(node) {
  7007. if (node.callee.type === "Import") {
  7008. if (node.arguments.length !== 1) {
  7009. this.raise(node.start, "import() requires exactly one argument");
  7010. }
  7011. const importArg = node.arguments[0];
  7012. if (importArg && importArg.type === "SpreadElement") {
  7013. this.raise(importArg.start, "... is not allowed in import()");
  7014. }
  7015. }
  7016. return this.finishNode(node, "OptionalCallExpression");
  7017. }
  7018. parseCallExpressionArguments(close, possibleAsyncArrow, dynamicImport, allowPlaceholder) {
  7019. const elts = [];
  7020. let innerParenStart;
  7021. let first = true;
  7022. while (!this.eat(close)) {
  7023. if (first) {
  7024. first = false;
  7025. } else {
  7026. this.expect(types.comma);
  7027. if (this.eat(close)) {
  7028. if (dynamicImport) {
  7029. this.raise(this.state.lastTokStart, "Trailing comma is disallowed inside import(...) arguments");
  7030. }
  7031. break;
  7032. }
  7033. }
  7034. if (this.match(types.parenL) && !innerParenStart) {
  7035. innerParenStart = this.state.start;
  7036. }
  7037. elts.push(this.parseExprListItem(false, possibleAsyncArrow ? {
  7038. start: 0
  7039. } : undefined, possibleAsyncArrow ? {
  7040. start: 0
  7041. } : undefined, allowPlaceholder));
  7042. }
  7043. if (possibleAsyncArrow && innerParenStart && this.shouldParseAsyncArrow()) {
  7044. this.unexpected();
  7045. }
  7046. return elts;
  7047. }
  7048. shouldParseAsyncArrow() {
  7049. return this.match(types.arrow) && !this.canInsertSemicolon();
  7050. }
  7051. parseAsyncArrowFromCallExpression(node, call) {
  7052. this.expect(types.arrow);
  7053. this.parseArrowExpression(node, call.arguments, true);
  7054. return node;
  7055. }
  7056. parseNoCallExpr() {
  7057. const startPos = this.state.start;
  7058. const startLoc = this.state.startLoc;
  7059. return this.parseSubscripts(this.parseExprAtom(), startPos, startLoc, true);
  7060. }
  7061. parseExprAtom(refShorthandDefaultPos) {
  7062. if (this.state.type === types.slash) this.readRegexp();
  7063. const canBeArrow = this.state.potentialArrowAt === this.state.start;
  7064. let node;
  7065. switch (this.state.type) {
  7066. case types._super:
  7067. if (!this.scope.allowSuper && !this.options.allowSuperOutsideMethod) {
  7068. this.raise(this.state.start, "super is only allowed in object methods and classes");
  7069. }
  7070. node = this.startNode();
  7071. this.next();
  7072. if (this.match(types.parenL) && !this.scope.allowDirectSuper && !this.options.allowSuperOutsideMethod) {
  7073. this.raise(node.start, "super() is only valid inside a class constructor of a subclass. " + "Maybe a typo in the method name ('constructor') or not extending another class?");
  7074. }
  7075. if (!this.match(types.parenL) && !this.match(types.bracketL) && !this.match(types.dot)) {
  7076. this.unexpected();
  7077. }
  7078. return this.finishNode(node, "Super");
  7079. case types._import:
  7080. node = this.startNode();
  7081. this.next();
  7082. if (this.match(types.dot)) {
  7083. return this.parseImportMetaProperty(node);
  7084. }
  7085. this.expectPlugin("dynamicImport", node.start);
  7086. if (!this.match(types.parenL)) {
  7087. this.unexpected(null, types.parenL);
  7088. }
  7089. return this.finishNode(node, "Import");
  7090. case types._this:
  7091. node = this.startNode();
  7092. this.next();
  7093. return this.finishNode(node, "ThisExpression");
  7094. case types.name:
  7095. {
  7096. node = this.startNode();
  7097. const containsEsc = this.state.containsEsc;
  7098. const id = this.parseIdentifier();
  7099. if (!containsEsc && id.name === "async" && this.match(types._function) && !this.canInsertSemicolon()) {
  7100. this.next();
  7101. return this.parseFunction(node, undefined, true);
  7102. } else if (canBeArrow && !containsEsc && id.name === "async" && this.match(types.name) && !this.canInsertSemicolon()) {
  7103. const params = [this.parseIdentifier()];
  7104. this.expect(types.arrow);
  7105. this.parseArrowExpression(node, params, true);
  7106. return node;
  7107. }
  7108. if (canBeArrow && this.match(types.arrow) && !this.canInsertSemicolon()) {
  7109. this.next();
  7110. this.parseArrowExpression(node, [id], false);
  7111. return node;
  7112. }
  7113. return id;
  7114. }
  7115. case types._do:
  7116. {
  7117. this.expectPlugin("doExpressions");
  7118. const node = this.startNode();
  7119. this.next();
  7120. const oldLabels = this.state.labels;
  7121. this.state.labels = [];
  7122. node.body = this.parseBlock();
  7123. this.state.labels = oldLabels;
  7124. return this.finishNode(node, "DoExpression");
  7125. }
  7126. case types.regexp:
  7127. {
  7128. const value = this.state.value;
  7129. node = this.parseLiteral(value.value, "RegExpLiteral");
  7130. node.pattern = value.pattern;
  7131. node.flags = value.flags;
  7132. return node;
  7133. }
  7134. case types.num:
  7135. return this.parseLiteral(this.state.value, "NumericLiteral");
  7136. case types.bigint:
  7137. return this.parseLiteral(this.state.value, "BigIntLiteral");
  7138. case types.string:
  7139. return this.parseLiteral(this.state.value, "StringLiteral");
  7140. case types._null:
  7141. node = this.startNode();
  7142. this.next();
  7143. return this.finishNode(node, "NullLiteral");
  7144. case types._true:
  7145. case types._false:
  7146. return this.parseBooleanLiteral();
  7147. case types.parenL:
  7148. return this.parseParenAndDistinguishExpression(canBeArrow);
  7149. case types.bracketL:
  7150. node = this.startNode();
  7151. this.next();
  7152. node.elements = this.parseExprList(types.bracketR, true, refShorthandDefaultPos);
  7153. if (!this.state.maybeInArrowParameters) {
  7154. this.toReferencedList(node.elements);
  7155. }
  7156. return this.finishNode(node, "ArrayExpression");
  7157. case types.braceL:
  7158. return this.parseObj(false, refShorthandDefaultPos);
  7159. case types._function:
  7160. return this.parseFunctionExpression();
  7161. case types.at:
  7162. this.parseDecorators();
  7163. case types._class:
  7164. node = this.startNode();
  7165. this.takeDecorators(node);
  7166. return this.parseClass(node, false);
  7167. case types._new:
  7168. return this.parseNew();
  7169. case types.backQuote:
  7170. return this.parseTemplate(false);
  7171. case types.doubleColon:
  7172. {
  7173. node = this.startNode();
  7174. this.next();
  7175. node.object = null;
  7176. const callee = node.callee = this.parseNoCallExpr();
  7177. if (callee.type === "MemberExpression") {
  7178. return this.finishNode(node, "BindExpression");
  7179. } else {
  7180. throw this.raise(callee.start, "Binding should be performed on object property.");
  7181. }
  7182. }
  7183. case types.hash:
  7184. {
  7185. if (this.state.inPipeline) {
  7186. node = this.startNode();
  7187. if (this.getPluginOption("pipelineOperator", "proposal") !== "smart") {
  7188. this.raise(node.start, "Primary Topic Reference found but pipelineOperator not passed 'smart' for 'proposal' option.");
  7189. }
  7190. this.next();
  7191. if (this.primaryTopicReferenceIsAllowedInCurrentTopicContext()) {
  7192. this.registerTopicReference();
  7193. return this.finishNode(node, "PipelinePrimaryTopicReference");
  7194. } else {
  7195. throw this.raise(node.start, `Topic reference was used in a lexical context without topic binding`);
  7196. }
  7197. }
  7198. }
  7199. default:
  7200. throw this.unexpected();
  7201. }
  7202. }
  7203. parseBooleanLiteral() {
  7204. const node = this.startNode();
  7205. node.value = this.match(types._true);
  7206. this.next();
  7207. return this.finishNode(node, "BooleanLiteral");
  7208. }
  7209. parseMaybePrivateName() {
  7210. const isPrivate = this.match(types.hash);
  7211. if (isPrivate) {
  7212. this.expectOnePlugin(["classPrivateProperties", "classPrivateMethods"]);
  7213. const node = this.startNode();
  7214. this.next();
  7215. this.assertNoSpace("Unexpected space between # and identifier");
  7216. node.id = this.parseIdentifier(true);
  7217. return this.finishNode(node, "PrivateName");
  7218. } else {
  7219. return this.parseIdentifier(true);
  7220. }
  7221. }
  7222. parseFunctionExpression() {
  7223. const node = this.startNode();
  7224. let meta = this.startNode();
  7225. this.next();
  7226. meta = this.createIdentifier(meta, "function");
  7227. if (this.scope.inGenerator && this.eat(types.dot)) {
  7228. return this.parseMetaProperty(node, meta, "sent");
  7229. }
  7230. return this.parseFunction(node);
  7231. }
  7232. parseMetaProperty(node, meta, propertyName) {
  7233. node.meta = meta;
  7234. if (meta.name === "function" && propertyName === "sent") {
  7235. if (this.isContextual(propertyName)) {
  7236. this.expectPlugin("functionSent");
  7237. } else if (!this.hasPlugin("functionSent")) {
  7238. this.unexpected();
  7239. }
  7240. }
  7241. const containsEsc = this.state.containsEsc;
  7242. node.property = this.parseIdentifier(true);
  7243. if (node.property.name !== propertyName || containsEsc) {
  7244. this.raise(node.property.start, `The only valid meta property for ${meta.name} is ${meta.name}.${propertyName}`);
  7245. }
  7246. return this.finishNode(node, "MetaProperty");
  7247. }
  7248. parseImportMetaProperty(node) {
  7249. const id = this.createIdentifier(this.startNodeAtNode(node), "import");
  7250. this.expect(types.dot);
  7251. if (this.isContextual("meta")) {
  7252. this.expectPlugin("importMeta");
  7253. } else if (!this.hasPlugin("importMeta")) {
  7254. this.raise(id.start, `Dynamic imports require a parameter: import('a.js')`);
  7255. }
  7256. if (!this.inModule) {
  7257. this.raise(id.start, `import.meta may appear only with 'sourceType: "module"'`, {
  7258. code: "BABEL_PARSER_SOURCETYPE_MODULE_REQUIRED"
  7259. });
  7260. }
  7261. this.sawUnambiguousESM = true;
  7262. return this.parseMetaProperty(node, id, "meta");
  7263. }
  7264. parseLiteral(value, type, startPos, startLoc) {
  7265. startPos = startPos || this.state.start;
  7266. startLoc = startLoc || this.state.startLoc;
  7267. const node = this.startNodeAt(startPos, startLoc);
  7268. this.addExtra(node, "rawValue", value);
  7269. this.addExtra(node, "raw", this.input.slice(startPos, this.state.end));
  7270. node.value = value;
  7271. this.next();
  7272. return this.finishNode(node, type);
  7273. }
  7274. parseParenAndDistinguishExpression(canBeArrow) {
  7275. const startPos = this.state.start;
  7276. const startLoc = this.state.startLoc;
  7277. let val;
  7278. this.expect(types.parenL);
  7279. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  7280. const oldYieldPos = this.state.yieldPos;
  7281. const oldAwaitPos = this.state.awaitPos;
  7282. this.state.maybeInArrowParameters = true;
  7283. this.state.yieldPos = 0;
  7284. this.state.awaitPos = 0;
  7285. const innerStartPos = this.state.start;
  7286. const innerStartLoc = this.state.startLoc;
  7287. const exprList = [];
  7288. const refShorthandDefaultPos = {
  7289. start: 0
  7290. };
  7291. const refNeedsArrowPos = {
  7292. start: 0
  7293. };
  7294. let first = true;
  7295. let spreadStart;
  7296. let optionalCommaStart;
  7297. while (!this.match(types.parenR)) {
  7298. if (first) {
  7299. first = false;
  7300. } else {
  7301. this.expect(types.comma, refNeedsArrowPos.start || null);
  7302. if (this.match(types.parenR)) {
  7303. optionalCommaStart = this.state.start;
  7304. break;
  7305. }
  7306. }
  7307. if (this.match(types.ellipsis)) {
  7308. const spreadNodeStartPos = this.state.start;
  7309. const spreadNodeStartLoc = this.state.startLoc;
  7310. spreadStart = this.state.start;
  7311. exprList.push(this.parseParenItem(this.parseRestBinding(), spreadNodeStartPos, spreadNodeStartLoc));
  7312. this.checkCommaAfterRest();
  7313. break;
  7314. } else {
  7315. exprList.push(this.parseMaybeAssign(false, refShorthandDefaultPos, this.parseParenItem, refNeedsArrowPos));
  7316. }
  7317. }
  7318. const innerEndPos = this.state.start;
  7319. const innerEndLoc = this.state.startLoc;
  7320. this.expect(types.parenR);
  7321. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  7322. let arrowNode = this.startNodeAt(startPos, startLoc);
  7323. if (canBeArrow && this.shouldParseArrow() && (arrowNode = this.parseArrow(arrowNode))) {
  7324. this.checkYieldAwaitInDefaultParams();
  7325. this.state.yieldPos = oldYieldPos;
  7326. this.state.awaitPos = oldAwaitPos;
  7327. for (let _i = 0; _i < exprList.length; _i++) {
  7328. const param = exprList[_i];
  7329. if (param.extra && param.extra.parenthesized) {
  7330. this.unexpected(param.extra.parenStart);
  7331. }
  7332. }
  7333. this.parseArrowExpression(arrowNode, exprList, false);
  7334. return arrowNode;
  7335. }
  7336. this.state.yieldPos = oldYieldPos || this.state.yieldPos;
  7337. this.state.awaitPos = oldAwaitPos || this.state.awaitPos;
  7338. if (!exprList.length) {
  7339. this.unexpected(this.state.lastTokStart);
  7340. }
  7341. if (optionalCommaStart) this.unexpected(optionalCommaStart);
  7342. if (spreadStart) this.unexpected(spreadStart);
  7343. if (refShorthandDefaultPos.start) {
  7344. this.unexpected(refShorthandDefaultPos.start);
  7345. }
  7346. if (refNeedsArrowPos.start) this.unexpected(refNeedsArrowPos.start);
  7347. this.toReferencedListDeep(exprList, true);
  7348. if (exprList.length > 1) {
  7349. val = this.startNodeAt(innerStartPos, innerStartLoc);
  7350. val.expressions = exprList;
  7351. this.finishNodeAt(val, "SequenceExpression", innerEndPos, innerEndLoc);
  7352. } else {
  7353. val = exprList[0];
  7354. }
  7355. if (!this.options.createParenthesizedExpressions) {
  7356. this.addExtra(val, "parenthesized", true);
  7357. this.addExtra(val, "parenStart", startPos);
  7358. return val;
  7359. }
  7360. const parenExpression = this.startNodeAt(startPos, startLoc);
  7361. parenExpression.expression = val;
  7362. this.finishNode(parenExpression, "ParenthesizedExpression");
  7363. return parenExpression;
  7364. }
  7365. shouldParseArrow() {
  7366. return !this.canInsertSemicolon();
  7367. }
  7368. parseArrow(node) {
  7369. if (this.eat(types.arrow)) {
  7370. return node;
  7371. }
  7372. }
  7373. parseParenItem(node, startPos, startLoc) {
  7374. return node;
  7375. }
  7376. parseNew() {
  7377. const node = this.startNode();
  7378. const meta = this.parseIdentifier(true);
  7379. if (this.eat(types.dot)) {
  7380. const metaProp = this.parseMetaProperty(node, meta, "target");
  7381. if (!this.scope.inNonArrowFunction && !this.state.inClassProperty) {
  7382. let error = "new.target can only be used in functions";
  7383. if (this.hasPlugin("classProperties")) {
  7384. error += " or class properties";
  7385. }
  7386. this.raise(metaProp.start, error);
  7387. }
  7388. return metaProp;
  7389. }
  7390. node.callee = this.parseNoCallExpr();
  7391. if (node.callee.type === "Import") {
  7392. this.raise(node.callee.start, "Cannot use new with import(...)");
  7393. } else if (node.callee.type === "OptionalMemberExpression" || node.callee.type === "OptionalCallExpression") {
  7394. this.raise(this.state.lastTokEnd, "constructors in/after an Optional Chain are not allowed");
  7395. } else if (this.eat(types.questionDot)) {
  7396. this.raise(this.state.start, "constructors in/after an Optional Chain are not allowed");
  7397. }
  7398. this.parseNewArguments(node);
  7399. return this.finishNode(node, "NewExpression");
  7400. }
  7401. parseNewArguments(node) {
  7402. if (this.eat(types.parenL)) {
  7403. const args = this.parseExprList(types.parenR);
  7404. this.toReferencedList(args);
  7405. node.arguments = args;
  7406. } else {
  7407. node.arguments = [];
  7408. }
  7409. }
  7410. parseTemplateElement(isTagged) {
  7411. const elem = this.startNode();
  7412. if (this.state.value === null) {
  7413. if (!isTagged) {
  7414. this.raise(this.state.invalidTemplateEscapePosition || 0, "Invalid escape sequence in template");
  7415. } else {
  7416. this.state.invalidTemplateEscapePosition = null;
  7417. }
  7418. }
  7419. elem.value = {
  7420. raw: this.input.slice(this.state.start, this.state.end).replace(/\r\n?/g, "\n"),
  7421. cooked: this.state.value
  7422. };
  7423. this.next();
  7424. elem.tail = this.match(types.backQuote);
  7425. return this.finishNode(elem, "TemplateElement");
  7426. }
  7427. parseTemplate(isTagged) {
  7428. const node = this.startNode();
  7429. this.next();
  7430. node.expressions = [];
  7431. let curElt = this.parseTemplateElement(isTagged);
  7432. node.quasis = [curElt];
  7433. while (!curElt.tail) {
  7434. this.expect(types.dollarBraceL);
  7435. node.expressions.push(this.parseExpression());
  7436. this.expect(types.braceR);
  7437. node.quasis.push(curElt = this.parseTemplateElement(isTagged));
  7438. }
  7439. this.next();
  7440. return this.finishNode(node, "TemplateLiteral");
  7441. }
  7442. parseObj(isPattern, refShorthandDefaultPos) {
  7443. const propHash = Object.create(null);
  7444. let first = true;
  7445. const node = this.startNode();
  7446. node.properties = [];
  7447. this.next();
  7448. while (!this.eat(types.braceR)) {
  7449. if (first) {
  7450. first = false;
  7451. } else {
  7452. this.expect(types.comma);
  7453. if (this.eat(types.braceR)) break;
  7454. }
  7455. const prop = this.parseObjectMember(isPattern, refShorthandDefaultPos);
  7456. if (!isPattern) this.checkPropClash(prop, propHash);
  7457. if (prop.shorthand) {
  7458. this.addExtra(prop, "shorthand", true);
  7459. }
  7460. node.properties.push(prop);
  7461. }
  7462. return this.finishNode(node, isPattern ? "ObjectPattern" : "ObjectExpression");
  7463. }
  7464. isAsyncProp(prop) {
  7465. return !prop.computed && prop.key.type === "Identifier" && prop.key.name === "async" && (this.match(types.name) || this.match(types.num) || this.match(types.string) || this.match(types.bracketL) || this.state.type.keyword || this.match(types.star)) && !this.hasPrecedingLineBreak();
  7466. }
  7467. parseObjectMember(isPattern, refShorthandDefaultPos) {
  7468. let decorators = [];
  7469. if (this.match(types.at)) {
  7470. if (this.hasPlugin("decorators")) {
  7471. this.raise(this.state.start, "Stage 2 decorators disallow object literal property decorators");
  7472. } else {
  7473. while (this.match(types.at)) {
  7474. decorators.push(this.parseDecorator());
  7475. }
  7476. }
  7477. }
  7478. const prop = this.startNode();
  7479. let isGenerator = false;
  7480. let isAsync = false;
  7481. let startPos;
  7482. let startLoc;
  7483. if (this.match(types.ellipsis)) {
  7484. if (decorators.length) this.unexpected();
  7485. if (isPattern) {
  7486. this.next();
  7487. prop.argument = this.parseIdentifier();
  7488. this.checkCommaAfterRest();
  7489. return this.finishNode(prop, "RestElement");
  7490. }
  7491. return this.parseSpread();
  7492. }
  7493. if (decorators.length) {
  7494. prop.decorators = decorators;
  7495. decorators = [];
  7496. }
  7497. prop.method = false;
  7498. if (isPattern || refShorthandDefaultPos) {
  7499. startPos = this.state.start;
  7500. startLoc = this.state.startLoc;
  7501. }
  7502. if (!isPattern) {
  7503. isGenerator = this.eat(types.star);
  7504. }
  7505. const containsEsc = this.state.containsEsc;
  7506. this.parsePropertyName(prop);
  7507. if (!isPattern && !containsEsc && !isGenerator && this.isAsyncProp(prop)) {
  7508. isAsync = true;
  7509. isGenerator = this.eat(types.star);
  7510. this.parsePropertyName(prop);
  7511. } else {
  7512. isAsync = false;
  7513. }
  7514. this.parseObjPropValue(prop, startPos, startLoc, isGenerator, isAsync, isPattern, refShorthandDefaultPos, containsEsc);
  7515. return prop;
  7516. }
  7517. isGetterOrSetterMethod(prop, isPattern) {
  7518. return !isPattern && !prop.computed && prop.key.type === "Identifier" && (prop.key.name === "get" || prop.key.name === "set") && (this.match(types.string) || this.match(types.num) || this.match(types.bracketL) || this.match(types.name) || !!this.state.type.keyword);
  7519. }
  7520. getGetterSetterExpectedParamCount(method) {
  7521. return method.kind === "get" ? 0 : 1;
  7522. }
  7523. checkGetterSetterParams(method) {
  7524. const paramCount = this.getGetterSetterExpectedParamCount(method);
  7525. const start = method.start;
  7526. if (method.params.length !== paramCount) {
  7527. if (method.kind === "get") {
  7528. this.raise(start, "getter must not have any formal parameters");
  7529. } else {
  7530. this.raise(start, "setter must have exactly one formal parameter");
  7531. }
  7532. }
  7533. if (method.kind === "set" && method.params[method.params.length - 1].type === "RestElement") {
  7534. this.raise(start, "setter function argument must not be a rest parameter");
  7535. }
  7536. }
  7537. parseObjectMethod(prop, isGenerator, isAsync, isPattern, containsEsc) {
  7538. if (isAsync || isGenerator || this.match(types.parenL)) {
  7539. if (isPattern) this.unexpected();
  7540. prop.kind = "method";
  7541. prop.method = true;
  7542. return this.parseMethod(prop, isGenerator, isAsync, false, false, "ObjectMethod");
  7543. }
  7544. if (!containsEsc && this.isGetterOrSetterMethod(prop, isPattern)) {
  7545. if (isGenerator || isAsync) this.unexpected();
  7546. prop.kind = prop.key.name;
  7547. this.parsePropertyName(prop);
  7548. this.parseMethod(prop, false, false, false, false, "ObjectMethod");
  7549. this.checkGetterSetterParams(prop);
  7550. return prop;
  7551. }
  7552. }
  7553. parseObjectProperty(prop, startPos, startLoc, isPattern, refShorthandDefaultPos) {
  7554. prop.shorthand = false;
  7555. if (this.eat(types.colon)) {
  7556. prop.value = isPattern ? this.parseMaybeDefault(this.state.start, this.state.startLoc) : this.parseMaybeAssign(false, refShorthandDefaultPos);
  7557. return this.finishNode(prop, "ObjectProperty");
  7558. }
  7559. if (!prop.computed && prop.key.type === "Identifier") {
  7560. this.checkReservedWord(prop.key.name, prop.key.start, true, true);
  7561. if (isPattern) {
  7562. prop.value = this.parseMaybeDefault(startPos, startLoc, prop.key.__clone());
  7563. } else if (this.match(types.eq) && refShorthandDefaultPos) {
  7564. if (!refShorthandDefaultPos.start) {
  7565. refShorthandDefaultPos.start = this.state.start;
  7566. }
  7567. prop.value = this.parseMaybeDefault(startPos, startLoc, prop.key.__clone());
  7568. } else {
  7569. prop.value = prop.key.__clone();
  7570. }
  7571. prop.shorthand = true;
  7572. return this.finishNode(prop, "ObjectProperty");
  7573. }
  7574. }
  7575. parseObjPropValue(prop, startPos, startLoc, isGenerator, isAsync, isPattern, refShorthandDefaultPos, containsEsc) {
  7576. const node = this.parseObjectMethod(prop, isGenerator, isAsync, isPattern, containsEsc) || this.parseObjectProperty(prop, startPos, startLoc, isPattern, refShorthandDefaultPos);
  7577. if (!node) this.unexpected();
  7578. return node;
  7579. }
  7580. parsePropertyName(prop) {
  7581. if (this.eat(types.bracketL)) {
  7582. prop.computed = true;
  7583. prop.key = this.parseMaybeAssign();
  7584. this.expect(types.bracketR);
  7585. } else {
  7586. const oldInPropertyName = this.state.inPropertyName;
  7587. this.state.inPropertyName = true;
  7588. prop.key = this.match(types.num) || this.match(types.string) ? this.parseExprAtom() : this.parseMaybePrivateName();
  7589. if (prop.key.type !== "PrivateName") {
  7590. prop.computed = false;
  7591. }
  7592. this.state.inPropertyName = oldInPropertyName;
  7593. }
  7594. return prop.key;
  7595. }
  7596. initFunction(node, isAsync) {
  7597. node.id = null;
  7598. node.generator = false;
  7599. node.async = !!isAsync;
  7600. }
  7601. parseMethod(node, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope = false) {
  7602. const oldYieldPos = this.state.yieldPos;
  7603. const oldAwaitPos = this.state.awaitPos;
  7604. this.state.yieldPos = 0;
  7605. this.state.awaitPos = 0;
  7606. this.initFunction(node, isAsync);
  7607. node.generator = !!isGenerator;
  7608. const allowModifiers = isConstructor;
  7609. this.scope.enter(functionFlags(isAsync, node.generator) | SCOPE_SUPER | (inClassScope ? SCOPE_CLASS : 0) | (allowDirectSuper ? SCOPE_DIRECT_SUPER : 0));
  7610. this.parseFunctionParams(node, allowModifiers);
  7611. this.checkYieldAwaitInDefaultParams();
  7612. this.parseFunctionBodyAndFinish(node, type, true);
  7613. this.scope.exit();
  7614. this.state.yieldPos = oldYieldPos;
  7615. this.state.awaitPos = oldAwaitPos;
  7616. return node;
  7617. }
  7618. parseArrowExpression(node, params, isAsync) {
  7619. this.scope.enter(functionFlags(isAsync, false) | SCOPE_ARROW);
  7620. this.initFunction(node, isAsync);
  7621. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  7622. const oldYieldPos = this.state.yieldPos;
  7623. const oldAwaitPos = this.state.awaitPos;
  7624. this.state.maybeInArrowParameters = false;
  7625. this.state.yieldPos = 0;
  7626. this.state.awaitPos = 0;
  7627. if (params) this.setArrowFunctionParameters(node, params);
  7628. this.parseFunctionBody(node, true);
  7629. this.scope.exit();
  7630. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  7631. this.state.yieldPos = oldYieldPos;
  7632. this.state.awaitPos = oldAwaitPos;
  7633. return this.finishNode(node, "ArrowFunctionExpression");
  7634. }
  7635. setArrowFunctionParameters(node, params) {
  7636. node.params = this.toAssignableList(params, true, "arrow function parameters");
  7637. }
  7638. isStrictBody(node) {
  7639. const isBlockStatement = node.body.type === "BlockStatement";
  7640. if (isBlockStatement && node.body.directives.length) {
  7641. for (let _i2 = 0, _node$body$directives = node.body.directives; _i2 < _node$body$directives.length; _i2++) {
  7642. const directive = _node$body$directives[_i2];
  7643. if (directive.value.value === "use strict") {
  7644. return true;
  7645. }
  7646. }
  7647. }
  7648. return false;
  7649. }
  7650. parseFunctionBodyAndFinish(node, type, isMethod = false) {
  7651. this.parseFunctionBody(node, false, isMethod);
  7652. this.finishNode(node, type);
  7653. }
  7654. parseFunctionBody(node, allowExpression, isMethod = false) {
  7655. const isExpression = allowExpression && !this.match(types.braceL);
  7656. const oldStrict = this.state.strict;
  7657. let useStrict = false;
  7658. const oldInParameters = this.state.inParameters;
  7659. this.state.inParameters = false;
  7660. if (isExpression) {
  7661. node.body = this.parseMaybeAssign();
  7662. this.checkParams(node, false, allowExpression);
  7663. } else {
  7664. const nonSimple = !this.isSimpleParamList(node.params);
  7665. if (!oldStrict || nonSimple) {
  7666. useStrict = this.strictDirective(this.state.end);
  7667. if (useStrict && nonSimple) {
  7668. const errorPos = (node.kind === "method" || node.kind === "constructor") && !!node.key ? node.key.end : node.start;
  7669. this.raise(errorPos, "Illegal 'use strict' directive in function with non-simple parameter list");
  7670. }
  7671. }
  7672. const oldLabels = this.state.labels;
  7673. this.state.labels = [];
  7674. if (useStrict) this.state.strict = true;
  7675. this.checkParams(node, !oldStrict && !useStrict && !allowExpression && !isMethod && !nonSimple, allowExpression);
  7676. node.body = this.parseBlock(true, false);
  7677. this.state.labels = oldLabels;
  7678. }
  7679. this.state.inParameters = oldInParameters;
  7680. if (this.state.strict && node.id) {
  7681. this.checkLVal(node.id, BIND_OUTSIDE, undefined, "function name");
  7682. }
  7683. this.state.strict = oldStrict;
  7684. }
  7685. isSimpleParamList(params) {
  7686. for (let i = 0, len = params.length; i < len; i++) {
  7687. if (params[i].type !== "Identifier") return false;
  7688. }
  7689. return true;
  7690. }
  7691. checkParams(node, allowDuplicates, isArrowFunction) {
  7692. const nameHash = Object.create(null);
  7693. for (let i = 0; i < node.params.length; i++) {
  7694. this.checkLVal(node.params[i], BIND_VAR, allowDuplicates ? null : nameHash, "function paramter list");
  7695. }
  7696. }
  7697. parseExprList(close, allowEmpty, refShorthandDefaultPos) {
  7698. const elts = [];
  7699. let first = true;
  7700. while (!this.eat(close)) {
  7701. if (first) {
  7702. first = false;
  7703. } else {
  7704. this.expect(types.comma);
  7705. if (this.eat(close)) break;
  7706. }
  7707. elts.push(this.parseExprListItem(allowEmpty, refShorthandDefaultPos));
  7708. }
  7709. return elts;
  7710. }
  7711. parseExprListItem(allowEmpty, refShorthandDefaultPos, refNeedsArrowPos, allowPlaceholder) {
  7712. let elt;
  7713. if (allowEmpty && this.match(types.comma)) {
  7714. elt = null;
  7715. } else if (this.match(types.ellipsis)) {
  7716. const spreadNodeStartPos = this.state.start;
  7717. const spreadNodeStartLoc = this.state.startLoc;
  7718. elt = this.parseParenItem(this.parseSpread(refShorthandDefaultPos, refNeedsArrowPos), spreadNodeStartPos, spreadNodeStartLoc);
  7719. } else if (this.match(types.question)) {
  7720. this.expectPlugin("partialApplication");
  7721. if (!allowPlaceholder) {
  7722. this.raise(this.state.start, "Unexpected argument placeholder");
  7723. }
  7724. const node = this.startNode();
  7725. this.next();
  7726. elt = this.finishNode(node, "ArgumentPlaceholder");
  7727. } else {
  7728. elt = this.parseMaybeAssign(false, refShorthandDefaultPos, this.parseParenItem, refNeedsArrowPos);
  7729. }
  7730. return elt;
  7731. }
  7732. parseIdentifier(liberal) {
  7733. const node = this.startNode();
  7734. const name = this.parseIdentifierName(node.start, liberal);
  7735. return this.createIdentifier(node, name);
  7736. }
  7737. createIdentifier(node, name) {
  7738. node.name = name;
  7739. node.loc.identifierName = name;
  7740. return this.finishNode(node, "Identifier");
  7741. }
  7742. parseIdentifierName(pos, liberal) {
  7743. let name;
  7744. if (this.match(types.name)) {
  7745. name = this.state.value;
  7746. } else if (this.state.type.keyword) {
  7747. name = this.state.type.keyword;
  7748. if ((name === "class" || name === "function") && (this.state.lastTokEnd !== this.state.lastTokStart + 1 || this.input.charCodeAt(this.state.lastTokStart) !== 46)) {
  7749. this.state.context.pop();
  7750. }
  7751. } else {
  7752. throw this.unexpected();
  7753. }
  7754. if (!liberal) {
  7755. this.checkReservedWord(name, this.state.start, !!this.state.type.keyword, false);
  7756. }
  7757. this.next();
  7758. return name;
  7759. }
  7760. checkReservedWord(word, startLoc, checkKeywords, isBinding) {
  7761. if (this.scope.inGenerator && word === "yield") {
  7762. this.raise(startLoc, "Can not use 'yield' as identifier inside a generator");
  7763. }
  7764. if (this.scope.inAsync && word === "await") {
  7765. this.raise(startLoc, "Can not use 'await' as identifier inside an async function");
  7766. }
  7767. if (this.state.inClassProperty && word === "arguments") {
  7768. this.raise(startLoc, "'arguments' is not allowed in class field initializer");
  7769. }
  7770. if (checkKeywords && isKeyword(word)) {
  7771. this.raise(startLoc, `Unexpected keyword '${word}'`);
  7772. }
  7773. const reservedTest = !this.state.strict ? isReservedWord : isBinding ? isStrictBindReservedWord : isStrictReservedWord;
  7774. if (reservedTest(word, this.inModule)) {
  7775. if (!this.scope.inAsync && word === "await") {
  7776. this.raise(startLoc, "Can not use keyword 'await' outside an async function");
  7777. }
  7778. this.raise(startLoc, `Unexpected reserved word '${word}'`);
  7779. }
  7780. }
  7781. parseAwait() {
  7782. if (!this.state.awaitPos) {
  7783. this.state.awaitPos = this.state.start;
  7784. }
  7785. const node = this.startNode();
  7786. this.next();
  7787. if (this.state.inParameters) {
  7788. this.raise(node.start, "await is not allowed in async function parameters");
  7789. }
  7790. if (this.match(types.star)) {
  7791. this.raise(node.start, "await* has been removed from the async functions proposal. Use Promise.all() instead.");
  7792. }
  7793. node.argument = this.parseMaybeUnary();
  7794. return this.finishNode(node, "AwaitExpression");
  7795. }
  7796. parseYield(noIn) {
  7797. if (!this.state.yieldPos) {
  7798. this.state.yieldPos = this.state.start;
  7799. }
  7800. const node = this.startNode();
  7801. if (this.state.inParameters) {
  7802. this.raise(node.start, "yield is not allowed in generator parameters");
  7803. }
  7804. this.next();
  7805. if (this.match(types.semi) || !this.match(types.star) && !this.state.type.startsExpr || this.canInsertSemicolon()) {
  7806. node.delegate = false;
  7807. node.argument = null;
  7808. } else {
  7809. node.delegate = this.eat(types.star);
  7810. node.argument = this.parseMaybeAssign(noIn);
  7811. }
  7812. return this.finishNode(node, "YieldExpression");
  7813. }
  7814. checkPipelineAtInfixOperator(left, leftStartPos) {
  7815. if (this.getPluginOption("pipelineOperator", "proposal") === "smart") {
  7816. if (left.type === "SequenceExpression") {
  7817. throw this.raise(leftStartPos, `Pipeline head should not be a comma-separated sequence expression`);
  7818. }
  7819. }
  7820. }
  7821. parseSmartPipelineBody(childExpression, startPos, startLoc) {
  7822. const pipelineStyle = this.checkSmartPipelineBodyStyle(childExpression);
  7823. this.checkSmartPipelineBodyEarlyErrors(childExpression, pipelineStyle, startPos);
  7824. return this.parseSmartPipelineBodyInStyle(childExpression, pipelineStyle, startPos, startLoc);
  7825. }
  7826. checkSmartPipelineBodyEarlyErrors(childExpression, pipelineStyle, startPos) {
  7827. if (this.match(types.arrow)) {
  7828. throw this.raise(this.state.start, `Unexpected arrow "=>" after pipeline body; arrow function in pipeline body must be parenthesized`);
  7829. } else if (pipelineStyle === "PipelineTopicExpression" && childExpression.type === "SequenceExpression") {
  7830. throw this.raise(startPos, `Pipeline body may not be a comma-separated sequence expression`);
  7831. }
  7832. }
  7833. parseSmartPipelineBodyInStyle(childExpression, pipelineStyle, startPos, startLoc) {
  7834. const bodyNode = this.startNodeAt(startPos, startLoc);
  7835. switch (pipelineStyle) {
  7836. case "PipelineBareFunction":
  7837. bodyNode.callee = childExpression;
  7838. break;
  7839. case "PipelineBareConstructor":
  7840. bodyNode.callee = childExpression.callee;
  7841. break;
  7842. case "PipelineBareAwaitedFunction":
  7843. bodyNode.callee = childExpression.argument;
  7844. break;
  7845. case "PipelineTopicExpression":
  7846. if (!this.topicReferenceWasUsedInCurrentTopicContext()) {
  7847. throw this.raise(startPos, `Pipeline is in topic style but does not use topic reference`);
  7848. }
  7849. bodyNode.expression = childExpression;
  7850. break;
  7851. default:
  7852. throw this.raise(startPos, `Unknown pipeline style ${pipelineStyle}`);
  7853. }
  7854. return this.finishNode(bodyNode, pipelineStyle);
  7855. }
  7856. checkSmartPipelineBodyStyle(expression) {
  7857. switch (expression.type) {
  7858. default:
  7859. return this.isSimpleReference(expression) ? "PipelineBareFunction" : "PipelineTopicExpression";
  7860. }
  7861. }
  7862. isSimpleReference(expression) {
  7863. switch (expression.type) {
  7864. case "MemberExpression":
  7865. return !expression.computed && this.isSimpleReference(expression.object);
  7866. case "Identifier":
  7867. return true;
  7868. default:
  7869. return false;
  7870. }
  7871. }
  7872. withTopicPermittingContext(callback) {
  7873. const outerContextTopicState = this.state.topicContext;
  7874. this.state.topicContext = {
  7875. maxNumOfResolvableTopics: 1,
  7876. maxTopicIndex: null
  7877. };
  7878. try {
  7879. return callback();
  7880. } finally {
  7881. this.state.topicContext = outerContextTopicState;
  7882. }
  7883. }
  7884. withTopicForbiddingContext(callback) {
  7885. const outerContextTopicState = this.state.topicContext;
  7886. this.state.topicContext = {
  7887. maxNumOfResolvableTopics: 0,
  7888. maxTopicIndex: null
  7889. };
  7890. try {
  7891. return callback();
  7892. } finally {
  7893. this.state.topicContext = outerContextTopicState;
  7894. }
  7895. }
  7896. registerTopicReference() {
  7897. this.state.topicContext.maxTopicIndex = 0;
  7898. }
  7899. primaryTopicReferenceIsAllowedInCurrentTopicContext() {
  7900. return this.state.topicContext.maxNumOfResolvableTopics >= 1;
  7901. }
  7902. topicReferenceWasUsedInCurrentTopicContext() {
  7903. return this.state.topicContext.maxTopicIndex != null && this.state.topicContext.maxTopicIndex >= 0;
  7904. }
  7905. }
  7906. const loopLabel = {
  7907. kind: "loop"
  7908. },
  7909. switchLabel = {
  7910. kind: "switch"
  7911. };
  7912. const FUNC_NO_FLAGS = 0b000,
  7913. FUNC_STATEMENT = 0b001,
  7914. FUNC_HANGING_STATEMENT = 0b010,
  7915. FUNC_NULLABLE_ID = 0b100;
  7916. class StatementParser extends ExpressionParser {
  7917. parseTopLevel(file, program) {
  7918. program.sourceType = this.options.sourceType;
  7919. program.interpreter = this.parseInterpreterDirective();
  7920. this.parseBlockBody(program, true, true, types.eof);
  7921. if (this.inModule && this.scope.undefinedExports.size > 0) {
  7922. for (let _i = 0, _Array$from = Array.from(this.scope.undefinedExports); _i < _Array$from.length; _i++) {
  7923. const [name] = _Array$from[_i];
  7924. const pos = this.scope.undefinedExports.get(name);
  7925. this.raise(pos, `Export '${name}' is not defined`);
  7926. }
  7927. }
  7928. file.program = this.finishNode(program, "Program");
  7929. file.comments = this.state.comments;
  7930. if (this.options.tokens) file.tokens = this.state.tokens;
  7931. return this.finishNode(file, "File");
  7932. }
  7933. stmtToDirective(stmt) {
  7934. const expr = stmt.expression;
  7935. const directiveLiteral = this.startNodeAt(expr.start, expr.loc.start);
  7936. const directive = this.startNodeAt(stmt.start, stmt.loc.start);
  7937. const raw = this.input.slice(expr.start, expr.end);
  7938. const val = directiveLiteral.value = raw.slice(1, -1);
  7939. this.addExtra(directiveLiteral, "raw", raw);
  7940. this.addExtra(directiveLiteral, "rawValue", val);
  7941. directive.value = this.finishNodeAt(directiveLiteral, "DirectiveLiteral", expr.end, expr.loc.end);
  7942. return this.finishNodeAt(directive, "Directive", stmt.end, stmt.loc.end);
  7943. }
  7944. parseInterpreterDirective() {
  7945. if (!this.match(types.interpreterDirective)) {
  7946. return null;
  7947. }
  7948. const node = this.startNode();
  7949. node.value = this.state.value;
  7950. this.next();
  7951. return this.finishNode(node, "InterpreterDirective");
  7952. }
  7953. isLet(context) {
  7954. if (!this.isContextual("let")) {
  7955. return false;
  7956. }
  7957. skipWhiteSpace.lastIndex = this.state.pos;
  7958. const skip = skipWhiteSpace.exec(this.input);
  7959. const next = this.state.pos + skip[0].length;
  7960. const nextCh = this.input.charCodeAt(next);
  7961. if (nextCh === 91) return true;
  7962. if (context) return false;
  7963. if (nextCh === 123) return true;
  7964. if (isIdentifierStart(nextCh)) {
  7965. let pos = next + 1;
  7966. while (isIdentifierChar(this.input.charCodeAt(pos))) {
  7967. ++pos;
  7968. }
  7969. const ident = this.input.slice(next, pos);
  7970. if (!keywordRelationalOperator.test(ident)) return true;
  7971. }
  7972. return false;
  7973. }
  7974. parseStatement(context, topLevel) {
  7975. if (this.match(types.at)) {
  7976. this.parseDecorators(true);
  7977. }
  7978. return this.parseStatementContent(context, topLevel);
  7979. }
  7980. parseStatementContent(context, topLevel) {
  7981. let starttype = this.state.type;
  7982. const node = this.startNode();
  7983. let kind;
  7984. if (this.isLet(context)) {
  7985. starttype = types._var;
  7986. kind = "let";
  7987. }
  7988. switch (starttype) {
  7989. case types._break:
  7990. case types._continue:
  7991. return this.parseBreakContinueStatement(node, starttype.keyword);
  7992. case types._debugger:
  7993. return this.parseDebuggerStatement(node);
  7994. case types._do:
  7995. return this.parseDoStatement(node);
  7996. case types._for:
  7997. return this.parseForStatement(node);
  7998. case types._function:
  7999. if (this.lookahead().type === types.dot) break;
  8000. if (context) {
  8001. if (this.state.strict) {
  8002. this.raise(this.state.start, "In strict mode code, functions can only be declared at top level or inside a block");
  8003. } else if (context !== "if" && context !== "label") {
  8004. this.raise(this.state.start, "In non-strict mode code, functions can only be declared at top level, " + "inside a block, or as the body of an if statement");
  8005. }
  8006. }
  8007. return this.parseFunctionStatement(node, false, !context);
  8008. case types._class:
  8009. if (context) this.unexpected();
  8010. return this.parseClass(node, true);
  8011. case types._if:
  8012. return this.parseIfStatement(node);
  8013. case types._return:
  8014. return this.parseReturnStatement(node);
  8015. case types._switch:
  8016. return this.parseSwitchStatement(node);
  8017. case types._throw:
  8018. return this.parseThrowStatement(node);
  8019. case types._try:
  8020. return this.parseTryStatement(node);
  8021. case types._const:
  8022. case types._var:
  8023. kind = kind || this.state.value;
  8024. if (context && kind !== "var") {
  8025. this.unexpected(this.state.start, "Lexical declaration cannot appear in a single-statement context");
  8026. }
  8027. return this.parseVarStatement(node, kind);
  8028. case types._while:
  8029. return this.parseWhileStatement(node);
  8030. case types._with:
  8031. return this.parseWithStatement(node);
  8032. case types.braceL:
  8033. return this.parseBlock();
  8034. case types.semi:
  8035. return this.parseEmptyStatement(node);
  8036. case types._export:
  8037. case types._import:
  8038. {
  8039. const nextToken = this.lookahead();
  8040. if (nextToken.type === types.parenL || nextToken.type === types.dot) {
  8041. break;
  8042. }
  8043. if (!this.options.allowImportExportEverywhere && !topLevel) {
  8044. this.raise(this.state.start, "'import' and 'export' may only appear at the top level");
  8045. }
  8046. this.next();
  8047. let result;
  8048. if (starttype === types._import) {
  8049. result = this.parseImport(node);
  8050. if (result.type === "ImportDeclaration" && (!result.importKind || result.importKind === "value")) {
  8051. this.sawUnambiguousESM = true;
  8052. }
  8053. } else {
  8054. result = this.parseExport(node);
  8055. if (result.type === "ExportNamedDeclaration" && (!result.exportKind || result.exportKind === "value") || result.type === "ExportAllDeclaration" && (!result.exportKind || result.exportKind === "value") || result.type === "ExportDefaultDeclaration") {
  8056. this.sawUnambiguousESM = true;
  8057. }
  8058. }
  8059. this.assertModuleNodeAllowed(node);
  8060. return result;
  8061. }
  8062. default:
  8063. {
  8064. if (this.isAsyncFunction()) {
  8065. if (context) {
  8066. this.unexpected(null, "Async functions can only be declared at the top level or inside a block");
  8067. }
  8068. this.next();
  8069. return this.parseFunctionStatement(node, true, !context);
  8070. }
  8071. }
  8072. }
  8073. const maybeName = this.state.value;
  8074. const expr = this.parseExpression();
  8075. if (starttype === types.name && expr.type === "Identifier" && this.eat(types.colon)) {
  8076. return this.parseLabeledStatement(node, maybeName, expr, context);
  8077. } else {
  8078. return this.parseExpressionStatement(node, expr);
  8079. }
  8080. }
  8081. assertModuleNodeAllowed(node) {
  8082. if (!this.options.allowImportExportEverywhere && !this.inModule) {
  8083. this.raise(node.start, `'import' and 'export' may appear only with 'sourceType: "module"'`, {
  8084. code: "BABEL_PARSER_SOURCETYPE_MODULE_REQUIRED"
  8085. });
  8086. }
  8087. }
  8088. takeDecorators(node) {
  8089. const decorators = this.state.decoratorStack[this.state.decoratorStack.length - 1];
  8090. if (decorators.length) {
  8091. node.decorators = decorators;
  8092. this.resetStartLocationFromNode(node, decorators[0]);
  8093. this.state.decoratorStack[this.state.decoratorStack.length - 1] = [];
  8094. }
  8095. }
  8096. canHaveLeadingDecorator() {
  8097. return this.match(types._class);
  8098. }
  8099. parseDecorators(allowExport) {
  8100. const currentContextDecorators = this.state.decoratorStack[this.state.decoratorStack.length - 1];
  8101. while (this.match(types.at)) {
  8102. const decorator = this.parseDecorator();
  8103. currentContextDecorators.push(decorator);
  8104. }
  8105. if (this.match(types._export)) {
  8106. if (!allowExport) {
  8107. this.unexpected();
  8108. }
  8109. if (this.hasPlugin("decorators") && !this.getPluginOption("decorators", "decoratorsBeforeExport")) {
  8110. this.raise(this.state.start, "Using the export keyword between a decorator and a class is not allowed. " + "Please use `export @dec class` instead.");
  8111. }
  8112. } else if (!this.canHaveLeadingDecorator()) {
  8113. this.raise(this.state.start, "Leading decorators must be attached to a class declaration");
  8114. }
  8115. }
  8116. parseDecorator() {
  8117. this.expectOnePlugin(["decorators-legacy", "decorators"]);
  8118. const node = this.startNode();
  8119. this.next();
  8120. if (this.hasPlugin("decorators")) {
  8121. this.state.decoratorStack.push([]);
  8122. const startPos = this.state.start;
  8123. const startLoc = this.state.startLoc;
  8124. let expr;
  8125. if (this.eat(types.parenL)) {
  8126. expr = this.parseExpression();
  8127. this.expect(types.parenR);
  8128. } else {
  8129. expr = this.parseIdentifier(false);
  8130. while (this.eat(types.dot)) {
  8131. const node = this.startNodeAt(startPos, startLoc);
  8132. node.object = expr;
  8133. node.property = this.parseIdentifier(true);
  8134. node.computed = false;
  8135. expr = this.finishNode(node, "MemberExpression");
  8136. }
  8137. }
  8138. node.expression = this.parseMaybeDecoratorArguments(expr);
  8139. this.state.decoratorStack.pop();
  8140. } else {
  8141. node.expression = this.parseMaybeAssign();
  8142. }
  8143. return this.finishNode(node, "Decorator");
  8144. }
  8145. parseMaybeDecoratorArguments(expr) {
  8146. if (this.eat(types.parenL)) {
  8147. const node = this.startNodeAtNode(expr);
  8148. node.callee = expr;
  8149. node.arguments = this.parseCallExpressionArguments(types.parenR, false);
  8150. this.toReferencedList(node.arguments);
  8151. return this.finishNode(node, "CallExpression");
  8152. }
  8153. return expr;
  8154. }
  8155. parseBreakContinueStatement(node, keyword) {
  8156. const isBreak = keyword === "break";
  8157. this.next();
  8158. if (this.isLineTerminator()) {
  8159. node.label = null;
  8160. } else {
  8161. node.label = this.parseIdentifier();
  8162. this.semicolon();
  8163. }
  8164. this.verifyBreakContinue(node, keyword);
  8165. return this.finishNode(node, isBreak ? "BreakStatement" : "ContinueStatement");
  8166. }
  8167. verifyBreakContinue(node, keyword) {
  8168. const isBreak = keyword === "break";
  8169. let i;
  8170. for (i = 0; i < this.state.labels.length; ++i) {
  8171. const lab = this.state.labels[i];
  8172. if (node.label == null || lab.name === node.label.name) {
  8173. if (lab.kind != null && (isBreak || lab.kind === "loop")) break;
  8174. if (node.label && isBreak) break;
  8175. }
  8176. }
  8177. if (i === this.state.labels.length) {
  8178. this.raise(node.start, "Unsyntactic " + keyword);
  8179. }
  8180. }
  8181. parseDebuggerStatement(node) {
  8182. this.next();
  8183. this.semicolon();
  8184. return this.finishNode(node, "DebuggerStatement");
  8185. }
  8186. parseHeaderExpression() {
  8187. this.expect(types.parenL);
  8188. const val = this.parseExpression();
  8189. this.expect(types.parenR);
  8190. return val;
  8191. }
  8192. parseDoStatement(node) {
  8193. this.next();
  8194. this.state.labels.push(loopLabel);
  8195. node.body = this.withTopicForbiddingContext(() => this.parseStatement("do"));
  8196. this.state.labels.pop();
  8197. this.expect(types._while);
  8198. node.test = this.parseHeaderExpression();
  8199. this.eat(types.semi);
  8200. return this.finishNode(node, "DoWhileStatement");
  8201. }
  8202. parseForStatement(node) {
  8203. this.next();
  8204. this.state.labels.push(loopLabel);
  8205. let awaitAt = -1;
  8206. if ((this.scope.inAsync || !this.scope.inFunction && this.options.allowAwaitOutsideFunction) && this.eatContextual("await")) {
  8207. awaitAt = this.state.lastTokStart;
  8208. }
  8209. this.scope.enter(SCOPE_OTHER);
  8210. this.expect(types.parenL);
  8211. if (this.match(types.semi)) {
  8212. if (awaitAt > -1) {
  8213. this.unexpected(awaitAt);
  8214. }
  8215. return this.parseFor(node, null);
  8216. }
  8217. const isLet = this.isLet();
  8218. if (this.match(types._var) || this.match(types._const) || isLet) {
  8219. const init = this.startNode();
  8220. const kind = isLet ? "let" : this.state.value;
  8221. this.next();
  8222. this.parseVar(init, true, kind);
  8223. this.finishNode(init, "VariableDeclaration");
  8224. if ((this.match(types._in) || this.isContextual("of")) && init.declarations.length === 1) {
  8225. return this.parseForIn(node, init, awaitAt);
  8226. }
  8227. if (awaitAt > -1) {
  8228. this.unexpected(awaitAt);
  8229. }
  8230. return this.parseFor(node, init);
  8231. }
  8232. const refShorthandDefaultPos = {
  8233. start: 0
  8234. };
  8235. const init = this.parseExpression(true, refShorthandDefaultPos);
  8236. if (this.match(types._in) || this.isContextual("of")) {
  8237. const description = this.isContextual("of") ? "for-of statement" : "for-in statement";
  8238. this.toAssignable(init, undefined, description);
  8239. this.checkLVal(init, undefined, undefined, description);
  8240. return this.parseForIn(node, init, awaitAt);
  8241. } else if (refShorthandDefaultPos.start) {
  8242. this.unexpected(refShorthandDefaultPos.start);
  8243. }
  8244. if (awaitAt > -1) {
  8245. this.unexpected(awaitAt);
  8246. }
  8247. return this.parseFor(node, init);
  8248. }
  8249. parseFunctionStatement(node, isAsync, declarationPosition) {
  8250. this.next();
  8251. return this.parseFunction(node, FUNC_STATEMENT | (declarationPosition ? 0 : FUNC_HANGING_STATEMENT), isAsync);
  8252. }
  8253. parseIfStatement(node) {
  8254. this.next();
  8255. node.test = this.parseHeaderExpression();
  8256. node.consequent = this.parseStatement("if");
  8257. node.alternate = this.eat(types._else) ? this.parseStatement("if") : null;
  8258. return this.finishNode(node, "IfStatement");
  8259. }
  8260. parseReturnStatement(node) {
  8261. if (!this.scope.inFunction && !this.options.allowReturnOutsideFunction) {
  8262. this.raise(this.state.start, "'return' outside of function");
  8263. }
  8264. this.next();
  8265. if (this.isLineTerminator()) {
  8266. node.argument = null;
  8267. } else {
  8268. node.argument = this.parseExpression();
  8269. this.semicolon();
  8270. }
  8271. return this.finishNode(node, "ReturnStatement");
  8272. }
  8273. parseSwitchStatement(node) {
  8274. this.next();
  8275. node.discriminant = this.parseHeaderExpression();
  8276. const cases = node.cases = [];
  8277. this.expect(types.braceL);
  8278. this.state.labels.push(switchLabel);
  8279. this.scope.enter(SCOPE_OTHER);
  8280. let cur;
  8281. for (let sawDefault; !this.match(types.braceR);) {
  8282. if (this.match(types._case) || this.match(types._default)) {
  8283. const isCase = this.match(types._case);
  8284. if (cur) this.finishNode(cur, "SwitchCase");
  8285. cases.push(cur = this.startNode());
  8286. cur.consequent = [];
  8287. this.next();
  8288. if (isCase) {
  8289. cur.test = this.parseExpression();
  8290. } else {
  8291. if (sawDefault) {
  8292. this.raise(this.state.lastTokStart, "Multiple default clauses");
  8293. }
  8294. sawDefault = true;
  8295. cur.test = null;
  8296. }
  8297. this.expect(types.colon);
  8298. } else {
  8299. if (cur) {
  8300. cur.consequent.push(this.parseStatement(null));
  8301. } else {
  8302. this.unexpected();
  8303. }
  8304. }
  8305. }
  8306. this.scope.exit();
  8307. if (cur) this.finishNode(cur, "SwitchCase");
  8308. this.next();
  8309. this.state.labels.pop();
  8310. return this.finishNode(node, "SwitchStatement");
  8311. }
  8312. parseThrowStatement(node) {
  8313. this.next();
  8314. if (lineBreak.test(this.input.slice(this.state.lastTokEnd, this.state.start))) {
  8315. this.raise(this.state.lastTokEnd, "Illegal newline after throw");
  8316. }
  8317. node.argument = this.parseExpression();
  8318. this.semicolon();
  8319. return this.finishNode(node, "ThrowStatement");
  8320. }
  8321. parseTryStatement(node) {
  8322. this.next();
  8323. node.block = this.parseBlock();
  8324. node.handler = null;
  8325. if (this.match(types._catch)) {
  8326. const clause = this.startNode();
  8327. this.next();
  8328. if (this.match(types.parenL)) {
  8329. this.expect(types.parenL);
  8330. clause.param = this.parseBindingAtom();
  8331. const simple = clause.param.type === "Identifier";
  8332. this.scope.enter(simple ? SCOPE_SIMPLE_CATCH : 0);
  8333. this.checkLVal(clause.param, BIND_LEXICAL, null, "catch clause");
  8334. this.expect(types.parenR);
  8335. } else {
  8336. clause.param = null;
  8337. this.scope.enter(SCOPE_OTHER);
  8338. }
  8339. clause.body = this.withTopicForbiddingContext(() => this.parseBlock(false, false));
  8340. this.scope.exit();
  8341. node.handler = this.finishNode(clause, "CatchClause");
  8342. }
  8343. node.finalizer = this.eat(types._finally) ? this.parseBlock() : null;
  8344. if (!node.handler && !node.finalizer) {
  8345. this.raise(node.start, "Missing catch or finally clause");
  8346. }
  8347. return this.finishNode(node, "TryStatement");
  8348. }
  8349. parseVarStatement(node, kind) {
  8350. this.next();
  8351. this.parseVar(node, false, kind);
  8352. this.semicolon();
  8353. return this.finishNode(node, "VariableDeclaration");
  8354. }
  8355. parseWhileStatement(node) {
  8356. this.next();
  8357. node.test = this.parseHeaderExpression();
  8358. this.state.labels.push(loopLabel);
  8359. node.body = this.withTopicForbiddingContext(() => this.parseStatement("while"));
  8360. this.state.labels.pop();
  8361. return this.finishNode(node, "WhileStatement");
  8362. }
  8363. parseWithStatement(node) {
  8364. if (this.state.strict) {
  8365. this.raise(this.state.start, "'with' in strict mode");
  8366. }
  8367. this.next();
  8368. node.object = this.parseHeaderExpression();
  8369. node.body = this.withTopicForbiddingContext(() => this.parseStatement("with"));
  8370. return this.finishNode(node, "WithStatement");
  8371. }
  8372. parseEmptyStatement(node) {
  8373. this.next();
  8374. return this.finishNode(node, "EmptyStatement");
  8375. }
  8376. parseLabeledStatement(node, maybeName, expr, context) {
  8377. for (let _i2 = 0, _this$state$labels = this.state.labels; _i2 < _this$state$labels.length; _i2++) {
  8378. const label = _this$state$labels[_i2];
  8379. if (label.name === maybeName) {
  8380. this.raise(expr.start, `Label '${maybeName}' is already declared`);
  8381. }
  8382. }
  8383. const kind = this.state.type.isLoop ? "loop" : this.match(types._switch) ? "switch" : null;
  8384. for (let i = this.state.labels.length - 1; i >= 0; i--) {
  8385. const label = this.state.labels[i];
  8386. if (label.statementStart === node.start) {
  8387. label.statementStart = this.state.start;
  8388. label.kind = kind;
  8389. } else {
  8390. break;
  8391. }
  8392. }
  8393. this.state.labels.push({
  8394. name: maybeName,
  8395. kind: kind,
  8396. statementStart: this.state.start
  8397. });
  8398. node.body = this.parseStatement(context ? context.indexOf("label") === -1 ? context + "label" : context : "label");
  8399. this.state.labels.pop();
  8400. node.label = expr;
  8401. return this.finishNode(node, "LabeledStatement");
  8402. }
  8403. parseExpressionStatement(node, expr) {
  8404. node.expression = expr;
  8405. this.semicolon();
  8406. return this.finishNode(node, "ExpressionStatement");
  8407. }
  8408. parseBlock(allowDirectives = false, createNewLexicalScope = true) {
  8409. const node = this.startNode();
  8410. this.expect(types.braceL);
  8411. if (createNewLexicalScope) {
  8412. this.scope.enter(SCOPE_OTHER);
  8413. }
  8414. this.parseBlockBody(node, allowDirectives, false, types.braceR);
  8415. if (createNewLexicalScope) {
  8416. this.scope.exit();
  8417. }
  8418. return this.finishNode(node, "BlockStatement");
  8419. }
  8420. isValidDirective(stmt) {
  8421. return stmt.type === "ExpressionStatement" && stmt.expression.type === "StringLiteral" && !stmt.expression.extra.parenthesized;
  8422. }
  8423. parseBlockBody(node, allowDirectives, topLevel, end) {
  8424. const body = node.body = [];
  8425. const directives = node.directives = [];
  8426. this.parseBlockOrModuleBlockBody(body, allowDirectives ? directives : undefined, topLevel, end);
  8427. }
  8428. parseBlockOrModuleBlockBody(body, directives, topLevel, end) {
  8429. let parsedNonDirective = false;
  8430. let oldStrict;
  8431. let octalPosition;
  8432. while (!this.eat(end)) {
  8433. if (!parsedNonDirective && this.state.containsOctal && !octalPosition) {
  8434. octalPosition = this.state.octalPosition;
  8435. }
  8436. const stmt = this.parseStatement(null, topLevel);
  8437. if (directives && !parsedNonDirective && this.isValidDirective(stmt)) {
  8438. const directive = this.stmtToDirective(stmt);
  8439. directives.push(directive);
  8440. if (oldStrict === undefined && directive.value.value === "use strict") {
  8441. oldStrict = this.state.strict;
  8442. this.setStrict(true);
  8443. if (octalPosition) {
  8444. this.raise(octalPosition, "Octal literal in strict mode");
  8445. }
  8446. }
  8447. continue;
  8448. }
  8449. parsedNonDirective = true;
  8450. body.push(stmt);
  8451. }
  8452. if (oldStrict === false) {
  8453. this.setStrict(false);
  8454. }
  8455. }
  8456. parseFor(node, init) {
  8457. node.init = init;
  8458. this.expect(types.semi);
  8459. node.test = this.match(types.semi) ? null : this.parseExpression();
  8460. this.expect(types.semi);
  8461. node.update = this.match(types.parenR) ? null : this.parseExpression();
  8462. this.expect(types.parenR);
  8463. node.body = this.withTopicForbiddingContext(() => this.parseStatement("for"));
  8464. this.scope.exit();
  8465. this.state.labels.pop();
  8466. return this.finishNode(node, "ForStatement");
  8467. }
  8468. parseForIn(node, init, awaitAt) {
  8469. const isForIn = this.match(types._in);
  8470. this.next();
  8471. if (isForIn) {
  8472. if (awaitAt > -1) this.unexpected(awaitAt);
  8473. } else {
  8474. node.await = awaitAt > -1;
  8475. }
  8476. if (init.type === "VariableDeclaration" && init.declarations[0].init != null && (!isForIn || this.state.strict || init.kind !== "var" || init.declarations[0].id.type !== "Identifier")) {
  8477. this.raise(init.start, `${isForIn ? "for-in" : "for-of"} loop variable declaration may not have an initializer`);
  8478. } else if (init.type === "AssignmentPattern") {
  8479. this.raise(init.start, "Invalid left-hand side in for-loop");
  8480. }
  8481. node.left = init;
  8482. node.right = isForIn ? this.parseExpression() : this.parseMaybeAssign();
  8483. this.expect(types.parenR);
  8484. node.body = this.withTopicForbiddingContext(() => this.parseStatement("for"));
  8485. this.scope.exit();
  8486. this.state.labels.pop();
  8487. return this.finishNode(node, isForIn ? "ForInStatement" : "ForOfStatement");
  8488. }
  8489. parseVar(node, isFor, kind) {
  8490. const declarations = node.declarations = [];
  8491. const isTypescript = this.hasPlugin("typescript");
  8492. node.kind = kind;
  8493. for (;;) {
  8494. const decl = this.startNode();
  8495. this.parseVarId(decl, kind);
  8496. if (this.eat(types.eq)) {
  8497. decl.init = this.parseMaybeAssign(isFor);
  8498. } else {
  8499. if (kind === "const" && !(this.match(types._in) || this.isContextual("of"))) {
  8500. if (!isTypescript) {
  8501. this.unexpected();
  8502. }
  8503. } else if (decl.id.type !== "Identifier" && !(isFor && (this.match(types._in) || this.isContextual("of")))) {
  8504. this.raise(this.state.lastTokEnd, "Complex binding patterns require an initialization value");
  8505. }
  8506. decl.init = null;
  8507. }
  8508. declarations.push(this.finishNode(decl, "VariableDeclarator"));
  8509. if (!this.eat(types.comma)) break;
  8510. }
  8511. return node;
  8512. }
  8513. parseVarId(decl, kind) {
  8514. if ((kind === "const" || kind === "let") && this.isContextual("let")) {
  8515. this.unexpected(null, "let is disallowed as a lexically bound name");
  8516. }
  8517. decl.id = this.parseBindingAtom();
  8518. this.checkLVal(decl.id, kind === "var" ? BIND_VAR : BIND_LEXICAL, undefined, "variable declaration");
  8519. }
  8520. parseFunction(node, statement = FUNC_NO_FLAGS, isAsync = false) {
  8521. const isStatement = statement & FUNC_STATEMENT;
  8522. const isHangingStatement = statement & FUNC_HANGING_STATEMENT;
  8523. const requireId = !!isStatement && !(statement & FUNC_NULLABLE_ID);
  8524. this.initFunction(node, isAsync);
  8525. if (this.match(types.star) && isHangingStatement) {
  8526. this.unexpected(this.state.start, "Generators can only be declared at the top level or inside a block");
  8527. }
  8528. node.generator = this.eat(types.star);
  8529. if (isStatement) {
  8530. node.id = this.parseFunctionId(requireId);
  8531. }
  8532. const oldInClassProperty = this.state.inClassProperty;
  8533. const oldYieldPos = this.state.yieldPos;
  8534. const oldAwaitPos = this.state.awaitPos;
  8535. this.state.inClassProperty = false;
  8536. this.state.yieldPos = 0;
  8537. this.state.awaitPos = 0;
  8538. this.scope.enter(functionFlags(node.async, node.generator));
  8539. if (!isStatement) {
  8540. node.id = this.parseFunctionId();
  8541. }
  8542. this.parseFunctionParams(node);
  8543. this.withTopicForbiddingContext(() => {
  8544. this.parseFunctionBodyAndFinish(node, isStatement ? "FunctionDeclaration" : "FunctionExpression");
  8545. });
  8546. this.scope.exit();
  8547. if (isStatement && !isHangingStatement) {
  8548. this.checkFunctionStatementId(node);
  8549. }
  8550. this.state.inClassProperty = oldInClassProperty;
  8551. this.state.yieldPos = oldYieldPos;
  8552. this.state.awaitPos = oldAwaitPos;
  8553. return node;
  8554. }
  8555. parseFunctionId(requireId) {
  8556. return requireId || this.match(types.name) ? this.parseIdentifier() : null;
  8557. }
  8558. parseFunctionParams(node, allowModifiers) {
  8559. const oldInParameters = this.state.inParameters;
  8560. this.state.inParameters = true;
  8561. this.expect(types.parenL);
  8562. node.params = this.parseBindingList(types.parenR, false, allowModifiers);
  8563. this.state.inParameters = oldInParameters;
  8564. this.checkYieldAwaitInDefaultParams();
  8565. }
  8566. checkFunctionStatementId(node) {
  8567. if (!node.id) return;
  8568. this.checkLVal(node.id, this.state.strict || node.generator || node.async ? this.scope.treatFunctionsAsVar ? BIND_VAR : BIND_LEXICAL : BIND_FUNCTION, null, "function name");
  8569. }
  8570. parseClass(node, isStatement, optionalId) {
  8571. this.next();
  8572. this.takeDecorators(node);
  8573. const oldStrict = this.state.strict;
  8574. this.state.strict = true;
  8575. this.parseClassId(node, isStatement, optionalId);
  8576. this.parseClassSuper(node);
  8577. node.body = this.parseClassBody(!!node.superClass);
  8578. this.state.strict = oldStrict;
  8579. return this.finishNode(node, isStatement ? "ClassDeclaration" : "ClassExpression");
  8580. }
  8581. isClassProperty() {
  8582. return this.match(types.eq) || this.match(types.semi) || this.match(types.braceR);
  8583. }
  8584. isClassMethod() {
  8585. return this.match(types.parenL);
  8586. }
  8587. isNonstaticConstructor(method) {
  8588. return !method.computed && !method.static && (method.key.name === "constructor" || method.key.value === "constructor");
  8589. }
  8590. parseClassBody(constructorAllowsSuper) {
  8591. this.state.classLevel++;
  8592. const state = {
  8593. hadConstructor: false
  8594. };
  8595. let decorators = [];
  8596. const classBody = this.startNode();
  8597. classBody.body = [];
  8598. this.expect(types.braceL);
  8599. this.withTopicForbiddingContext(() => {
  8600. while (!this.eat(types.braceR)) {
  8601. if (this.eat(types.semi)) {
  8602. if (decorators.length > 0) {
  8603. this.raise(this.state.lastTokEnd, "Decorators must not be followed by a semicolon");
  8604. }
  8605. continue;
  8606. }
  8607. if (this.match(types.at)) {
  8608. decorators.push(this.parseDecorator());
  8609. continue;
  8610. }
  8611. const member = this.startNode();
  8612. if (decorators.length) {
  8613. member.decorators = decorators;
  8614. this.resetStartLocationFromNode(member, decorators[0]);
  8615. decorators = [];
  8616. }
  8617. this.parseClassMember(classBody, member, state, constructorAllowsSuper);
  8618. if (member.kind === "constructor" && member.decorators && member.decorators.length > 0) {
  8619. this.raise(member.start, "Decorators can't be used with a constructor. Did you mean '@dec class { ... }'?");
  8620. }
  8621. }
  8622. });
  8623. if (decorators.length) {
  8624. this.raise(this.state.start, "You have trailing decorators with no method");
  8625. }
  8626. this.state.classLevel--;
  8627. return this.finishNode(classBody, "ClassBody");
  8628. }
  8629. parseClassMember(classBody, member, state, constructorAllowsSuper) {
  8630. let isStatic = false;
  8631. const containsEsc = this.state.containsEsc;
  8632. if (this.match(types.name) && this.state.value === "static") {
  8633. const key = this.parseIdentifier(true);
  8634. if (this.isClassMethod()) {
  8635. const method = member;
  8636. method.kind = "method";
  8637. method.computed = false;
  8638. method.key = key;
  8639. method.static = false;
  8640. this.pushClassMethod(classBody, method, false, false, false, false);
  8641. return;
  8642. } else if (this.isClassProperty()) {
  8643. const prop = member;
  8644. prop.computed = false;
  8645. prop.key = key;
  8646. prop.static = false;
  8647. classBody.body.push(this.parseClassProperty(prop));
  8648. return;
  8649. } else if (containsEsc) {
  8650. throw this.unexpected();
  8651. }
  8652. isStatic = true;
  8653. }
  8654. this.parseClassMemberWithIsStatic(classBody, member, state, isStatic, constructorAllowsSuper);
  8655. }
  8656. parseClassMemberWithIsStatic(classBody, member, state, isStatic, constructorAllowsSuper) {
  8657. const publicMethod = member;
  8658. const privateMethod = member;
  8659. const publicProp = member;
  8660. const privateProp = member;
  8661. const method = publicMethod;
  8662. const publicMember = publicMethod;
  8663. member.static = isStatic;
  8664. if (this.eat(types.star)) {
  8665. method.kind = "method";
  8666. this.parseClassPropertyName(method);
  8667. if (method.key.type === "PrivateName") {
  8668. this.pushClassPrivateMethod(classBody, privateMethod, true, false);
  8669. return;
  8670. }
  8671. if (this.isNonstaticConstructor(publicMethod)) {
  8672. this.raise(publicMethod.key.start, "Constructor can't be a generator");
  8673. }
  8674. this.pushClassMethod(classBody, publicMethod, true, false, false, false);
  8675. return;
  8676. }
  8677. const containsEsc = this.state.containsEsc;
  8678. const key = this.parseClassPropertyName(member);
  8679. const isPrivate = key.type === "PrivateName";
  8680. const isSimple = key.type === "Identifier";
  8681. this.parsePostMemberNameModifiers(publicMember);
  8682. if (this.isClassMethod()) {
  8683. method.kind = "method";
  8684. if (isPrivate) {
  8685. this.pushClassPrivateMethod(classBody, privateMethod, false, false);
  8686. return;
  8687. }
  8688. const isConstructor = this.isNonstaticConstructor(publicMethod);
  8689. let allowsDirectSuper = false;
  8690. if (isConstructor) {
  8691. publicMethod.kind = "constructor";
  8692. if (publicMethod.decorators) {
  8693. this.raise(publicMethod.start, "You can't attach decorators to a class constructor");
  8694. }
  8695. if (state.hadConstructor && !this.hasPlugin("typescript")) {
  8696. this.raise(key.start, "Duplicate constructor in the same class");
  8697. }
  8698. state.hadConstructor = true;
  8699. allowsDirectSuper = constructorAllowsSuper;
  8700. }
  8701. this.pushClassMethod(classBody, publicMethod, false, false, isConstructor, allowsDirectSuper);
  8702. } else if (this.isClassProperty()) {
  8703. if (isPrivate) {
  8704. this.pushClassPrivateProperty(classBody, privateProp);
  8705. } else {
  8706. this.pushClassProperty(classBody, publicProp);
  8707. }
  8708. } else if (isSimple && key.name === "async" && !containsEsc && !this.isLineTerminator()) {
  8709. const isGenerator = this.eat(types.star);
  8710. method.kind = "method";
  8711. this.parseClassPropertyName(method);
  8712. if (method.key.type === "PrivateName") {
  8713. this.pushClassPrivateMethod(classBody, privateMethod, isGenerator, true);
  8714. } else {
  8715. if (this.isNonstaticConstructor(publicMethod)) {
  8716. this.raise(publicMethod.key.start, "Constructor can't be an async function");
  8717. }
  8718. this.pushClassMethod(classBody, publicMethod, isGenerator, true, false, false);
  8719. }
  8720. } else if (isSimple && (key.name === "get" || key.name === "set") && !containsEsc && !(this.match(types.star) && this.isLineTerminator())) {
  8721. method.kind = key.name;
  8722. this.parseClassPropertyName(publicMethod);
  8723. if (method.key.type === "PrivateName") {
  8724. this.pushClassPrivateMethod(classBody, privateMethod, false, false);
  8725. } else {
  8726. if (this.isNonstaticConstructor(publicMethod)) {
  8727. this.raise(publicMethod.key.start, "Constructor can't have get/set modifier");
  8728. }
  8729. this.pushClassMethod(classBody, publicMethod, false, false, false, false);
  8730. }
  8731. this.checkGetterSetterParams(publicMethod);
  8732. } else if (this.isLineTerminator()) {
  8733. if (isPrivate) {
  8734. this.pushClassPrivateProperty(classBody, privateProp);
  8735. } else {
  8736. this.pushClassProperty(classBody, publicProp);
  8737. }
  8738. } else {
  8739. this.unexpected();
  8740. }
  8741. }
  8742. parseClassPropertyName(member) {
  8743. const key = this.parsePropertyName(member);
  8744. if (!member.computed && member.static && (key.name === "prototype" || key.value === "prototype")) {
  8745. this.raise(key.start, "Classes may not have static property named prototype");
  8746. }
  8747. if (key.type === "PrivateName" && key.id.name === "constructor") {
  8748. this.raise(key.start, "Classes may not have a private field named '#constructor'");
  8749. }
  8750. return key;
  8751. }
  8752. pushClassProperty(classBody, prop) {
  8753. if (this.isNonstaticConstructor(prop)) {
  8754. this.raise(prop.key.start, "Classes may not have a non-static field named 'constructor'");
  8755. }
  8756. classBody.body.push(this.parseClassProperty(prop));
  8757. }
  8758. pushClassPrivateProperty(classBody, prop) {
  8759. this.expectPlugin("classPrivateProperties", prop.key.start);
  8760. classBody.body.push(this.parseClassPrivateProperty(prop));
  8761. }
  8762. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  8763. classBody.body.push(this.parseMethod(method, isGenerator, isAsync, isConstructor, allowsDirectSuper, "ClassMethod", true));
  8764. }
  8765. pushClassPrivateMethod(classBody, method, isGenerator, isAsync) {
  8766. this.expectPlugin("classPrivateMethods", method.key.start);
  8767. classBody.body.push(this.parseMethod(method, isGenerator, isAsync, false, false, "ClassPrivateMethod", true));
  8768. }
  8769. parsePostMemberNameModifiers(methodOrProp) {}
  8770. parseAccessModifier() {
  8771. return undefined;
  8772. }
  8773. parseClassPrivateProperty(node) {
  8774. this.state.inClassProperty = true;
  8775. this.scope.enter(SCOPE_CLASS | SCOPE_SUPER);
  8776. node.value = this.eat(types.eq) ? this.parseMaybeAssign() : null;
  8777. this.semicolon();
  8778. this.state.inClassProperty = false;
  8779. this.scope.exit();
  8780. return this.finishNode(node, "ClassPrivateProperty");
  8781. }
  8782. parseClassProperty(node) {
  8783. if (!node.typeAnnotation) {
  8784. this.expectPlugin("classProperties");
  8785. }
  8786. this.state.inClassProperty = true;
  8787. this.scope.enter(SCOPE_CLASS | SCOPE_SUPER);
  8788. if (this.match(types.eq)) {
  8789. this.expectPlugin("classProperties");
  8790. this.next();
  8791. node.value = this.parseMaybeAssign();
  8792. } else {
  8793. node.value = null;
  8794. }
  8795. this.semicolon();
  8796. this.state.inClassProperty = false;
  8797. this.scope.exit();
  8798. return this.finishNode(node, "ClassProperty");
  8799. }
  8800. parseClassId(node, isStatement, optionalId) {
  8801. if (this.match(types.name)) {
  8802. node.id = this.parseIdentifier();
  8803. if (isStatement) {
  8804. this.checkLVal(node.id, BIND_CLASS, undefined, "class name");
  8805. }
  8806. } else {
  8807. if (optionalId || !isStatement) {
  8808. node.id = null;
  8809. } else {
  8810. this.unexpected(null, "A class name is required");
  8811. }
  8812. }
  8813. }
  8814. parseClassSuper(node) {
  8815. node.superClass = this.eat(types._extends) ? this.parseExprSubscripts() : null;
  8816. }
  8817. parseExport(node) {
  8818. const hasDefault = this.maybeParseExportDefaultSpecifier(node);
  8819. const parseAfterDefault = !hasDefault || this.eat(types.comma);
  8820. const hasStar = parseAfterDefault && this.eatExportStar(node);
  8821. const hasNamespace = hasStar && this.maybeParseExportNamespaceSpecifier(node);
  8822. const parseAfterNamespace = parseAfterDefault && (!hasNamespace || this.eat(types.comma));
  8823. const isFromRequired = hasDefault || hasStar;
  8824. if (hasStar && !hasNamespace) {
  8825. if (hasDefault) this.unexpected();
  8826. this.parseExportFrom(node, true);
  8827. return this.finishNode(node, "ExportAllDeclaration");
  8828. }
  8829. const hasSpecifiers = this.maybeParseExportNamedSpecifiers(node);
  8830. if (hasDefault && parseAfterDefault && !hasStar && !hasSpecifiers || hasNamespace && parseAfterNamespace && !hasSpecifiers) {
  8831. throw this.unexpected(null, types.braceL);
  8832. }
  8833. let hasDeclaration;
  8834. if (isFromRequired || hasSpecifiers) {
  8835. hasDeclaration = false;
  8836. this.parseExportFrom(node, isFromRequired);
  8837. } else {
  8838. hasDeclaration = this.maybeParseExportDeclaration(node);
  8839. }
  8840. if (isFromRequired || hasSpecifiers || hasDeclaration) {
  8841. this.checkExport(node, true, false, !!node.source);
  8842. return this.finishNode(node, "ExportNamedDeclaration");
  8843. }
  8844. if (this.eat(types._default)) {
  8845. node.declaration = this.parseExportDefaultExpression();
  8846. this.checkExport(node, true, true);
  8847. return this.finishNode(node, "ExportDefaultDeclaration");
  8848. }
  8849. throw this.unexpected(null, types.braceL);
  8850. }
  8851. eatExportStar(node) {
  8852. return this.eat(types.star);
  8853. }
  8854. maybeParseExportDefaultSpecifier(node) {
  8855. if (this.isExportDefaultSpecifier()) {
  8856. this.expectPlugin("exportDefaultFrom");
  8857. const specifier = this.startNode();
  8858. specifier.exported = this.parseIdentifier(true);
  8859. node.specifiers = [this.finishNode(specifier, "ExportDefaultSpecifier")];
  8860. return true;
  8861. }
  8862. return false;
  8863. }
  8864. maybeParseExportNamespaceSpecifier(node) {
  8865. if (this.isContextual("as")) {
  8866. if (!node.specifiers) node.specifiers = [];
  8867. this.expectPlugin("exportNamespaceFrom");
  8868. const specifier = this.startNodeAt(this.state.lastTokStart, this.state.lastTokStartLoc);
  8869. this.next();
  8870. specifier.exported = this.parseIdentifier(true);
  8871. node.specifiers.push(this.finishNode(specifier, "ExportNamespaceSpecifier"));
  8872. return true;
  8873. }
  8874. return false;
  8875. }
  8876. maybeParseExportNamedSpecifiers(node) {
  8877. if (this.match(types.braceL)) {
  8878. if (!node.specifiers) node.specifiers = [];
  8879. node.specifiers.push(...this.parseExportSpecifiers());
  8880. node.source = null;
  8881. node.declaration = null;
  8882. return true;
  8883. }
  8884. return false;
  8885. }
  8886. maybeParseExportDeclaration(node) {
  8887. if (this.shouldParseExportDeclaration()) {
  8888. if (this.isContextual("async")) {
  8889. const next = this.lookahead();
  8890. if (next.type !== types._function) {
  8891. this.unexpected(next.start, `Unexpected token, expected "function"`);
  8892. }
  8893. }
  8894. node.specifiers = [];
  8895. node.source = null;
  8896. node.declaration = this.parseExportDeclaration(node);
  8897. return true;
  8898. }
  8899. return false;
  8900. }
  8901. isAsyncFunction() {
  8902. if (!this.isContextual("async")) return false;
  8903. const {
  8904. pos
  8905. } = this.state;
  8906. skipWhiteSpace.lastIndex = pos;
  8907. const skip = skipWhiteSpace.exec(this.input);
  8908. if (!skip || !skip.length) return false;
  8909. const next = pos + skip[0].length;
  8910. return !lineBreak.test(this.input.slice(pos, next)) && this.input.slice(next, next + 8) === "function" && (next + 8 === this.length || !isIdentifierChar(this.input.charCodeAt(next + 8)));
  8911. }
  8912. parseExportDefaultExpression() {
  8913. const expr = this.startNode();
  8914. const isAsync = this.isAsyncFunction();
  8915. if (this.match(types._function) || isAsync) {
  8916. this.next();
  8917. if (isAsync) {
  8918. this.next();
  8919. }
  8920. return this.parseFunction(expr, FUNC_STATEMENT | FUNC_NULLABLE_ID, isAsync);
  8921. } else if (this.match(types._class)) {
  8922. return this.parseClass(expr, true, true);
  8923. } else if (this.match(types.at)) {
  8924. if (this.hasPlugin("decorators") && this.getPluginOption("decorators", "decoratorsBeforeExport")) {
  8925. this.unexpected(this.state.start, "Decorators must be placed *before* the 'export' keyword." + " You can set the 'decoratorsBeforeExport' option to false to use" + " the 'export @decorator class {}' syntax");
  8926. }
  8927. this.parseDecorators(false);
  8928. return this.parseClass(expr, true, true);
  8929. } else if (this.match(types._const) || this.match(types._var) || this.isLet()) {
  8930. return this.raise(this.state.start, "Only expressions, functions or classes are allowed as the `default` export.");
  8931. } else {
  8932. const res = this.parseMaybeAssign();
  8933. this.semicolon();
  8934. return res;
  8935. }
  8936. }
  8937. parseExportDeclaration(node) {
  8938. return this.parseStatement(null);
  8939. }
  8940. isExportDefaultSpecifier() {
  8941. if (this.match(types.name)) {
  8942. return this.state.value !== "async" && this.state.value !== "let";
  8943. }
  8944. if (!this.match(types._default)) {
  8945. return false;
  8946. }
  8947. const lookahead = this.lookahead();
  8948. return lookahead.type === types.comma || lookahead.type === types.name && lookahead.value === "from";
  8949. }
  8950. parseExportFrom(node, expect) {
  8951. if (this.eatContextual("from")) {
  8952. node.source = this.parseImportSource();
  8953. this.checkExport(node);
  8954. } else {
  8955. if (expect) {
  8956. this.unexpected();
  8957. } else {
  8958. node.source = null;
  8959. }
  8960. }
  8961. this.semicolon();
  8962. }
  8963. shouldParseExportDeclaration() {
  8964. if (this.match(types.at)) {
  8965. this.expectOnePlugin(["decorators", "decorators-legacy"]);
  8966. if (this.hasPlugin("decorators")) {
  8967. if (this.getPluginOption("decorators", "decoratorsBeforeExport")) {
  8968. this.unexpected(this.state.start, "Decorators must be placed *before* the 'export' keyword." + " You can set the 'decoratorsBeforeExport' option to false to use" + " the 'export @decorator class {}' syntax");
  8969. } else {
  8970. return true;
  8971. }
  8972. }
  8973. }
  8974. return this.state.type.keyword === "var" || this.state.type.keyword === "const" || this.state.type.keyword === "function" || this.state.type.keyword === "class" || this.isLet() || this.isAsyncFunction();
  8975. }
  8976. checkExport(node, checkNames, isDefault, isFrom) {
  8977. if (checkNames) {
  8978. if (isDefault) {
  8979. this.checkDuplicateExports(node, "default");
  8980. } else if (node.specifiers && node.specifiers.length) {
  8981. for (let _i3 = 0, _node$specifiers = node.specifiers; _i3 < _node$specifiers.length; _i3++) {
  8982. const specifier = _node$specifiers[_i3];
  8983. this.checkDuplicateExports(specifier, specifier.exported.name);
  8984. if (!isFrom && specifier.local) {
  8985. this.checkReservedWord(specifier.local.name, specifier.local.start, true, false);
  8986. this.scope.checkLocalExport(specifier.local);
  8987. }
  8988. }
  8989. } else if (node.declaration) {
  8990. if (node.declaration.type === "FunctionDeclaration" || node.declaration.type === "ClassDeclaration") {
  8991. const id = node.declaration.id;
  8992. if (!id) throw new Error("Assertion failure");
  8993. this.checkDuplicateExports(node, id.name);
  8994. } else if (node.declaration.type === "VariableDeclaration") {
  8995. for (let _i4 = 0, _node$declaration$dec = node.declaration.declarations; _i4 < _node$declaration$dec.length; _i4++) {
  8996. const declaration = _node$declaration$dec[_i4];
  8997. this.checkDeclaration(declaration.id);
  8998. }
  8999. }
  9000. }
  9001. }
  9002. const currentContextDecorators = this.state.decoratorStack[this.state.decoratorStack.length - 1];
  9003. if (currentContextDecorators.length) {
  9004. const isClass = node.declaration && (node.declaration.type === "ClassDeclaration" || node.declaration.type === "ClassExpression");
  9005. if (!node.declaration || !isClass) {
  9006. throw this.raise(node.start, "You can only use decorators on an export when exporting a class");
  9007. }
  9008. this.takeDecorators(node.declaration);
  9009. }
  9010. }
  9011. checkDeclaration(node) {
  9012. if (node.type === "Identifier") {
  9013. this.checkDuplicateExports(node, node.name);
  9014. } else if (node.type === "ObjectPattern") {
  9015. for (let _i5 = 0, _node$properties = node.properties; _i5 < _node$properties.length; _i5++) {
  9016. const prop = _node$properties[_i5];
  9017. this.checkDeclaration(prop);
  9018. }
  9019. } else if (node.type === "ArrayPattern") {
  9020. for (let _i6 = 0, _node$elements = node.elements; _i6 < _node$elements.length; _i6++) {
  9021. const elem = _node$elements[_i6];
  9022. if (elem) {
  9023. this.checkDeclaration(elem);
  9024. }
  9025. }
  9026. } else if (node.type === "ObjectProperty") {
  9027. this.checkDeclaration(node.value);
  9028. } else if (node.type === "RestElement") {
  9029. this.checkDeclaration(node.argument);
  9030. } else if (node.type === "AssignmentPattern") {
  9031. this.checkDeclaration(node.left);
  9032. }
  9033. }
  9034. checkDuplicateExports(node, name) {
  9035. if (this.state.exportedIdentifiers.indexOf(name) > -1) {
  9036. throw this.raise(node.start, name === "default" ? "Only one default export allowed per module." : `\`${name}\` has already been exported. Exported identifiers must be unique.`);
  9037. }
  9038. this.state.exportedIdentifiers.push(name);
  9039. }
  9040. parseExportSpecifiers() {
  9041. const nodes = [];
  9042. let first = true;
  9043. this.expect(types.braceL);
  9044. while (!this.eat(types.braceR)) {
  9045. if (first) {
  9046. first = false;
  9047. } else {
  9048. this.expect(types.comma);
  9049. if (this.eat(types.braceR)) break;
  9050. }
  9051. const node = this.startNode();
  9052. node.local = this.parseIdentifier(true);
  9053. node.exported = this.eatContextual("as") ? this.parseIdentifier(true) : node.local.__clone();
  9054. nodes.push(this.finishNode(node, "ExportSpecifier"));
  9055. }
  9056. return nodes;
  9057. }
  9058. parseImport(node) {
  9059. node.specifiers = [];
  9060. if (!this.match(types.string)) {
  9061. const hasDefault = this.maybeParseDefaultImportSpecifier(node);
  9062. const parseNext = !hasDefault || this.eat(types.comma);
  9063. const hasStar = parseNext && this.maybeParseStarImportSpecifier(node);
  9064. if (parseNext && !hasStar) this.parseNamedImportSpecifiers(node);
  9065. this.expectContextual("from");
  9066. }
  9067. node.source = this.parseImportSource();
  9068. this.semicolon();
  9069. return this.finishNode(node, "ImportDeclaration");
  9070. }
  9071. parseImportSource() {
  9072. if (!this.match(types.string)) this.unexpected();
  9073. return this.parseExprAtom();
  9074. }
  9075. shouldParseDefaultImport(node) {
  9076. return this.match(types.name);
  9077. }
  9078. parseImportSpecifierLocal(node, specifier, type, contextDescription) {
  9079. specifier.local = this.parseIdentifier();
  9080. this.checkLVal(specifier.local, BIND_LEXICAL, undefined, contextDescription);
  9081. node.specifiers.push(this.finishNode(specifier, type));
  9082. }
  9083. maybeParseDefaultImportSpecifier(node) {
  9084. if (this.shouldParseDefaultImport(node)) {
  9085. this.parseImportSpecifierLocal(node, this.startNode(), "ImportDefaultSpecifier", "default import specifier");
  9086. return true;
  9087. }
  9088. return false;
  9089. }
  9090. maybeParseStarImportSpecifier(node) {
  9091. if (this.match(types.star)) {
  9092. const specifier = this.startNode();
  9093. this.next();
  9094. this.expectContextual("as");
  9095. this.parseImportSpecifierLocal(node, specifier, "ImportNamespaceSpecifier", "import namespace specifier");
  9096. return true;
  9097. }
  9098. return false;
  9099. }
  9100. parseNamedImportSpecifiers(node) {
  9101. let first = true;
  9102. this.expect(types.braceL);
  9103. while (!this.eat(types.braceR)) {
  9104. if (first) {
  9105. first = false;
  9106. } else {
  9107. if (this.eat(types.colon)) {
  9108. this.unexpected(null, "ES2015 named imports do not destructure. " + "Use another statement for destructuring after the import.");
  9109. }
  9110. this.expect(types.comma);
  9111. if (this.eat(types.braceR)) break;
  9112. }
  9113. this.parseImportSpecifier(node);
  9114. }
  9115. }
  9116. parseImportSpecifier(node) {
  9117. const specifier = this.startNode();
  9118. specifier.imported = this.parseIdentifier(true);
  9119. if (this.eatContextual("as")) {
  9120. specifier.local = this.parseIdentifier();
  9121. } else {
  9122. this.checkReservedWord(specifier.imported.name, specifier.start, true, true);
  9123. specifier.local = specifier.imported.__clone();
  9124. }
  9125. this.checkLVal(specifier.local, BIND_LEXICAL, undefined, "import specifier");
  9126. node.specifiers.push(this.finishNode(specifier, "ImportSpecifier"));
  9127. }
  9128. }
  9129. class Parser extends StatementParser {
  9130. constructor(options, input) {
  9131. options = getOptions(options);
  9132. super(options, input);
  9133. const ScopeHandler = this.getScopeHandler();
  9134. this.options = options;
  9135. this.inModule = this.options.sourceType === "module";
  9136. this.scope = new ScopeHandler(this.raise.bind(this), this.inModule);
  9137. this.plugins = pluginsMap(this.options.plugins);
  9138. this.filename = options.sourceFilename;
  9139. }
  9140. getScopeHandler() {
  9141. return ScopeHandler;
  9142. }
  9143. parse() {
  9144. this.scope.enter(SCOPE_PROGRAM);
  9145. const file = this.startNode();
  9146. const program = this.startNode();
  9147. this.nextToken();
  9148. return this.parseTopLevel(file, program);
  9149. }
  9150. }
  9151. function pluginsMap(plugins) {
  9152. const pluginMap = new Map();
  9153. for (let _i = 0; _i < plugins.length; _i++) {
  9154. const plugin = plugins[_i];
  9155. const [name, options] = Array.isArray(plugin) ? plugin : [plugin, {}];
  9156. if (!pluginMap.has(name)) pluginMap.set(name, options || {});
  9157. }
  9158. return pluginMap;
  9159. }
  9160. function parse(input, options) {
  9161. if (options && options.sourceType === "unambiguous") {
  9162. options = Object.assign({}, options);
  9163. try {
  9164. options.sourceType = "module";
  9165. const parser = getParser(options, input);
  9166. const ast = parser.parse();
  9167. if (!parser.sawUnambiguousESM) ast.program.sourceType = "script";
  9168. return ast;
  9169. } catch (moduleError) {
  9170. try {
  9171. options.sourceType = "script";
  9172. return getParser(options, input).parse();
  9173. } catch (scriptError) {}
  9174. throw moduleError;
  9175. }
  9176. } else {
  9177. return getParser(options, input).parse();
  9178. }
  9179. }
  9180. function parseExpression(input, options) {
  9181. const parser = getParser(options, input);
  9182. if (parser.options.strictMode) {
  9183. parser.state.strict = true;
  9184. }
  9185. return parser.getExpression();
  9186. }
  9187. function getParser(options, input) {
  9188. let cls = Parser;
  9189. if (options && options.plugins) {
  9190. validatePlugins(options.plugins);
  9191. cls = getParserClass(options.plugins);
  9192. }
  9193. return new cls(options, input);
  9194. }
  9195. const parserClassCache = {};
  9196. function getParserClass(pluginsFromOptions) {
  9197. const pluginList = mixinPluginNames.filter(name => hasPlugin(pluginsFromOptions, name));
  9198. const key = pluginList.join("/");
  9199. let cls = parserClassCache[key];
  9200. if (!cls) {
  9201. cls = Parser;
  9202. for (let _i = 0; _i < pluginList.length; _i++) {
  9203. const plugin = pluginList[_i];
  9204. cls = mixinPlugins[plugin](cls);
  9205. }
  9206. parserClassCache[key] = cls;
  9207. }
  9208. return cls;
  9209. }
  9210. exports.parse = parse;
  9211. exports.parseExpression = parseExpression;
  9212. exports.tokTypes = types;