index.js 475 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. function _objectWithoutPropertiesLoose(r, e) {
  6. if (null == r) return {};
  7. var t = {};
  8. for (var n in r) if ({}.hasOwnProperty.call(r, n)) {
  9. if (e.includes(n)) continue;
  10. t[n] = r[n];
  11. }
  12. return t;
  13. }
  14. class Position {
  15. constructor(line, col, index) {
  16. this.line = void 0;
  17. this.column = void 0;
  18. this.index = void 0;
  19. this.line = line;
  20. this.column = col;
  21. this.index = index;
  22. }
  23. }
  24. class SourceLocation {
  25. constructor(start, end) {
  26. this.start = void 0;
  27. this.end = void 0;
  28. this.filename = void 0;
  29. this.identifierName = void 0;
  30. this.start = start;
  31. this.end = end;
  32. }
  33. }
  34. function createPositionWithColumnOffset(position, columnOffset) {
  35. const {
  36. line,
  37. column,
  38. index
  39. } = position;
  40. return new Position(line, column + columnOffset, index + columnOffset);
  41. }
  42. const code = "BABEL_PARSER_SOURCETYPE_MODULE_REQUIRED";
  43. var ModuleErrors = {
  44. ImportMetaOutsideModule: {
  45. message: `import.meta may appear only with 'sourceType: "module"'`,
  46. code
  47. },
  48. ImportOutsideModule: {
  49. message: `'import' and 'export' may appear only with 'sourceType: "module"'`,
  50. code
  51. }
  52. };
  53. const NodeDescriptions = {
  54. ArrayPattern: "array destructuring pattern",
  55. AssignmentExpression: "assignment expression",
  56. AssignmentPattern: "assignment expression",
  57. ArrowFunctionExpression: "arrow function expression",
  58. ConditionalExpression: "conditional expression",
  59. CatchClause: "catch clause",
  60. ForOfStatement: "for-of statement",
  61. ForInStatement: "for-in statement",
  62. ForStatement: "for-loop",
  63. FormalParameters: "function parameter list",
  64. Identifier: "identifier",
  65. ImportSpecifier: "import specifier",
  66. ImportDefaultSpecifier: "import default specifier",
  67. ImportNamespaceSpecifier: "import namespace specifier",
  68. ObjectPattern: "object destructuring pattern",
  69. ParenthesizedExpression: "parenthesized expression",
  70. RestElement: "rest element",
  71. UpdateExpression: {
  72. true: "prefix operation",
  73. false: "postfix operation"
  74. },
  75. VariableDeclarator: "variable declaration",
  76. YieldExpression: "yield expression"
  77. };
  78. const toNodeDescription = node => node.type === "UpdateExpression" ? NodeDescriptions.UpdateExpression[`${node.prefix}`] : NodeDescriptions[node.type];
  79. var StandardErrors = {
  80. AccessorIsGenerator: ({
  81. kind
  82. }) => `A ${kind}ter cannot be a generator.`,
  83. ArgumentsInClass: "'arguments' is only allowed in functions and class methods.",
  84. AsyncFunctionInSingleStatementContext: "Async functions can only be declared at the top level or inside a block.",
  85. AwaitBindingIdentifier: "Can not use 'await' as identifier inside an async function.",
  86. AwaitBindingIdentifierInStaticBlock: "Can not use 'await' as identifier inside a static block.",
  87. AwaitExpressionFormalParameter: "'await' is not allowed in async function parameters.",
  88. AwaitUsingNotInAsyncContext: "'await using' is only allowed within async functions and at the top levels of modules.",
  89. AwaitNotInAsyncContext: "'await' is only allowed within async functions and at the top levels of modules.",
  90. AwaitNotInAsyncFunction: "'await' is only allowed within async functions.",
  91. BadGetterArity: "A 'get' accessor must not have any formal parameters.",
  92. BadSetterArity: "A 'set' accessor must have exactly one formal parameter.",
  93. BadSetterRestParameter: "A 'set' accessor function argument must not be a rest parameter.",
  94. ConstructorClassField: "Classes may not have a field named 'constructor'.",
  95. ConstructorClassPrivateField: "Classes may not have a private field named '#constructor'.",
  96. ConstructorIsAccessor: "Class constructor may not be an accessor.",
  97. ConstructorIsAsync: "Constructor can't be an async function.",
  98. ConstructorIsGenerator: "Constructor can't be a generator.",
  99. DeclarationMissingInitializer: ({
  100. kind
  101. }) => `Missing initializer in ${kind} declaration.`,
  102. DecoratorArgumentsOutsideParentheses: "Decorator arguments must be moved inside parentheses: use '@(decorator(args))' instead of '@(decorator)(args)'.",
  103. DecoratorBeforeExport: "Decorators must be placed *before* the 'export' keyword. Remove the 'decoratorsBeforeExport: true' option to use the 'export @decorator class {}' syntax.",
  104. DecoratorsBeforeAfterExport: "Decorators can be placed *either* before or after the 'export' keyword, but not in both locations at the same time.",
  105. DecoratorConstructor: "Decorators can't be used with a constructor. Did you mean '@dec class { ... }'?",
  106. DecoratorExportClass: "Decorators must be placed *after* the 'export' keyword. Remove the 'decoratorsBeforeExport: false' option to use the '@decorator export class {}' syntax.",
  107. DecoratorSemicolon: "Decorators must not be followed by a semicolon.",
  108. DecoratorStaticBlock: "Decorators can't be used with a static block.",
  109. DeferImportRequiresNamespace: 'Only `import defer * as x from "./module"` is valid.',
  110. DeletePrivateField: "Deleting a private field is not allowed.",
  111. DestructureNamedImport: "ES2015 named imports do not destructure. Use another statement for destructuring after the import.",
  112. DuplicateConstructor: "Duplicate constructor in the same class.",
  113. DuplicateDefaultExport: "Only one default export allowed per module.",
  114. DuplicateExport: ({
  115. exportName
  116. }) => `\`${exportName}\` has already been exported. Exported identifiers must be unique.`,
  117. DuplicateProto: "Redefinition of __proto__ property.",
  118. DuplicateRegExpFlags: "Duplicate regular expression flag.",
  119. DynamicImportPhaseRequiresImportExpressions: ({
  120. phase
  121. }) => `'import.${phase}(...)' can only be parsed when using the 'createImportExpressions' option.`,
  122. ElementAfterRest: "Rest element must be last element.",
  123. EscapedCharNotAnIdentifier: "Invalid Unicode escape.",
  124. ExportBindingIsString: ({
  125. localName,
  126. exportName
  127. }) => `A string literal cannot be used as an exported binding without \`from\`.\n- Did you mean \`export { '${localName}' as '${exportName}' } from 'some-module'\`?`,
  128. ExportDefaultFromAsIdentifier: "'from' is not allowed as an identifier after 'export default'.",
  129. ForInOfLoopInitializer: ({
  130. type
  131. }) => `'${type === "ForInStatement" ? "for-in" : "for-of"}' loop variable declaration may not have an initializer.`,
  132. ForInUsing: "For-in loop may not start with 'using' declaration.",
  133. ForOfAsync: "The left-hand side of a for-of loop may not be 'async'.",
  134. ForOfLet: "The left-hand side of a for-of loop may not start with 'let'.",
  135. GeneratorInSingleStatementContext: "Generators can only be declared at the top level or inside a block.",
  136. IllegalBreakContinue: ({
  137. type
  138. }) => `Unsyntactic ${type === "BreakStatement" ? "break" : "continue"}.`,
  139. IllegalLanguageModeDirective: "Illegal 'use strict' directive in function with non-simple parameter list.",
  140. IllegalReturn: "'return' outside of function.",
  141. ImportAttributesUseAssert: "The `assert` keyword in import attributes is deprecated and it has been replaced by the `with` keyword. You can enable the `deprecatedAssertSyntax: true` option in the import attributes plugin to suppress this error.",
  142. ImportBindingIsString: ({
  143. importName
  144. }) => `A string literal cannot be used as an imported binding.\n- Did you mean \`import { "${importName}" as foo }\`?`,
  145. ImportCallArgumentTrailingComma: "Trailing comma is disallowed inside import(...) arguments.",
  146. ImportCallArity: ({
  147. maxArgumentCount
  148. }) => `\`import()\` requires exactly ${maxArgumentCount === 1 ? "one argument" : "one or two arguments"}.`,
  149. ImportCallNotNewExpression: "Cannot use new with import(...).",
  150. ImportCallSpreadArgument: "`...` is not allowed in `import()`.",
  151. ImportJSONBindingNotDefault: "A JSON module can only be imported with `default`.",
  152. ImportReflectionHasAssertion: "`import module x` cannot have assertions.",
  153. ImportReflectionNotBinding: 'Only `import module x from "./module"` is valid.',
  154. IncompatibleRegExpUVFlags: "The 'u' and 'v' regular expression flags cannot be enabled at the same time.",
  155. InvalidBigIntLiteral: "Invalid BigIntLiteral.",
  156. InvalidCodePoint: "Code point out of bounds.",
  157. InvalidCoverInitializedName: "Invalid shorthand property initializer.",
  158. InvalidDecimal: "Invalid decimal.",
  159. InvalidDigit: ({
  160. radix
  161. }) => `Expected number in radix ${radix}.`,
  162. InvalidEscapeSequence: "Bad character escape sequence.",
  163. InvalidEscapeSequenceTemplate: "Invalid escape sequence in template.",
  164. InvalidEscapedReservedWord: ({
  165. reservedWord
  166. }) => `Escape sequence in keyword ${reservedWord}.`,
  167. InvalidIdentifier: ({
  168. identifierName
  169. }) => `Invalid identifier ${identifierName}.`,
  170. InvalidLhs: ({
  171. ancestor
  172. }) => `Invalid left-hand side in ${toNodeDescription(ancestor)}.`,
  173. InvalidLhsBinding: ({
  174. ancestor
  175. }) => `Binding invalid left-hand side in ${toNodeDescription(ancestor)}.`,
  176. InvalidLhsOptionalChaining: ({
  177. ancestor
  178. }) => `Invalid optional chaining in the left-hand side of ${toNodeDescription(ancestor)}.`,
  179. InvalidNumber: "Invalid number.",
  180. InvalidOrMissingExponent: "Floating-point numbers require a valid exponent after the 'e'.",
  181. InvalidOrUnexpectedToken: ({
  182. unexpected
  183. }) => `Unexpected character '${unexpected}'.`,
  184. InvalidParenthesizedAssignment: "Invalid parenthesized assignment pattern.",
  185. InvalidPrivateFieldResolution: ({
  186. identifierName
  187. }) => `Private name #${identifierName} is not defined.`,
  188. InvalidPropertyBindingPattern: "Binding member expression.",
  189. InvalidRecordProperty: "Only properties and spread elements are allowed in record definitions.",
  190. InvalidRestAssignmentPattern: "Invalid rest operator's argument.",
  191. LabelRedeclaration: ({
  192. labelName
  193. }) => `Label '${labelName}' is already declared.`,
  194. LetInLexicalBinding: "'let' is disallowed as a lexically bound name.",
  195. LineTerminatorBeforeArrow: "No line break is allowed before '=>'.",
  196. MalformedRegExpFlags: "Invalid regular expression flag.",
  197. MissingClassName: "A class name is required.",
  198. MissingEqInAssignment: "Only '=' operator can be used for specifying default value.",
  199. MissingSemicolon: "Missing semicolon.",
  200. MissingPlugin: ({
  201. missingPlugin
  202. }) => `This experimental syntax requires enabling the parser plugin: ${missingPlugin.map(name => JSON.stringify(name)).join(", ")}.`,
  203. MissingOneOfPlugins: ({
  204. missingPlugin
  205. }) => `This experimental syntax requires enabling one of the following parser plugin(s): ${missingPlugin.map(name => JSON.stringify(name)).join(", ")}.`,
  206. MissingUnicodeEscape: "Expecting Unicode escape sequence \\uXXXX.",
  207. MixingCoalesceWithLogical: "Nullish coalescing operator(??) requires parens when mixing with logical operators.",
  208. ModuleAttributeDifferentFromType: "The only accepted module attribute is `type`.",
  209. ModuleAttributeInvalidValue: "Only string literals are allowed as module attribute values.",
  210. ModuleAttributesWithDuplicateKeys: ({
  211. key
  212. }) => `Duplicate key "${key}" is not allowed in module attributes.`,
  213. ModuleExportNameHasLoneSurrogate: ({
  214. surrogateCharCode
  215. }) => `An export name cannot include a lone surrogate, found '\\u${surrogateCharCode.toString(16)}'.`,
  216. ModuleExportUndefined: ({
  217. localName
  218. }) => `Export '${localName}' is not defined.`,
  219. MultipleDefaultsInSwitch: "Multiple default clauses.",
  220. NewlineAfterThrow: "Illegal newline after throw.",
  221. NoCatchOrFinally: "Missing catch or finally clause.",
  222. NumberIdentifier: "Identifier directly after number.",
  223. NumericSeparatorInEscapeSequence: "Numeric separators are not allowed inside unicode escape sequences or hex escape sequences.",
  224. ObsoleteAwaitStar: "'await*' has been removed from the async functions proposal. Use Promise.all() instead.",
  225. OptionalChainingNoNew: "Constructors in/after an Optional Chain are not allowed.",
  226. OptionalChainingNoTemplate: "Tagged Template Literals are not allowed in optionalChain.",
  227. OverrideOnConstructor: "'override' modifier cannot appear on a constructor declaration.",
  228. ParamDupe: "Argument name clash.",
  229. PatternHasAccessor: "Object pattern can't contain getter or setter.",
  230. PatternHasMethod: "Object pattern can't contain methods.",
  231. PrivateInExpectedIn: ({
  232. identifierName
  233. }) => `Private names are only allowed in property accesses (\`obj.#${identifierName}\`) or in \`in\` expressions (\`#${identifierName} in obj\`).`,
  234. PrivateNameRedeclaration: ({
  235. identifierName
  236. }) => `Duplicate private name #${identifierName}.`,
  237. RecordExpressionBarIncorrectEndSyntaxType: "Record expressions ending with '|}' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'bar'.",
  238. RecordExpressionBarIncorrectStartSyntaxType: "Record expressions starting with '{|' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'bar'.",
  239. RecordExpressionHashIncorrectStartSyntaxType: "Record expressions starting with '#{' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'hash'.",
  240. RecordNoProto: "'__proto__' is not allowed in Record expressions.",
  241. RestTrailingComma: "Unexpected trailing comma after rest element.",
  242. SloppyFunction: "In non-strict mode code, functions can only be declared at top level or inside a block.",
  243. SloppyFunctionAnnexB: "In non-strict mode code, functions can only be declared at top level, inside a block, or as the body of an if statement.",
  244. SourcePhaseImportRequiresDefault: 'Only `import source x from "./module"` is valid.',
  245. StaticPrototype: "Classes may not have static property named prototype.",
  246. SuperNotAllowed: "`super()` is only valid inside a class constructor of a subclass. Maybe a typo in the method name ('constructor') or not extending another class?",
  247. SuperPrivateField: "Private fields can't be accessed on super.",
  248. TrailingDecorator: "Decorators must be attached to a class element.",
  249. TupleExpressionBarIncorrectEndSyntaxType: "Tuple expressions ending with '|]' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'bar'.",
  250. TupleExpressionBarIncorrectStartSyntaxType: "Tuple expressions starting with '[|' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'bar'.",
  251. TupleExpressionHashIncorrectStartSyntaxType: "Tuple expressions starting with '#[' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'hash'.",
  252. UnexpectedArgumentPlaceholder: "Unexpected argument placeholder.",
  253. UnexpectedAwaitAfterPipelineBody: 'Unexpected "await" after pipeline body; await must have parentheses in minimal proposal.',
  254. UnexpectedDigitAfterHash: "Unexpected digit after hash token.",
  255. UnexpectedImportExport: "'import' and 'export' may only appear at the top level.",
  256. UnexpectedKeyword: ({
  257. keyword
  258. }) => `Unexpected keyword '${keyword}'.`,
  259. UnexpectedLeadingDecorator: "Leading decorators must be attached to a class declaration.",
  260. UnexpectedLexicalDeclaration: "Lexical declaration cannot appear in a single-statement context.",
  261. UnexpectedNewTarget: "`new.target` can only be used in functions or class properties.",
  262. UnexpectedNumericSeparator: "A numeric separator is only allowed between two digits.",
  263. UnexpectedPrivateField: "Unexpected private name.",
  264. UnexpectedReservedWord: ({
  265. reservedWord
  266. }) => `Unexpected reserved word '${reservedWord}'.`,
  267. UnexpectedSuper: "'super' is only allowed in object methods and classes.",
  268. UnexpectedToken: ({
  269. expected,
  270. unexpected
  271. }) => `Unexpected token${unexpected ? ` '${unexpected}'.` : ""}${expected ? `, expected "${expected}"` : ""}`,
  272. UnexpectedTokenUnaryExponentiation: "Illegal expression. Wrap left hand side or entire exponentiation in parentheses.",
  273. UnexpectedUsingDeclaration: "Using declaration cannot appear in the top level when source type is `script`.",
  274. UnsupportedBind: "Binding should be performed on object property.",
  275. UnsupportedDecoratorExport: "A decorated export must export a class declaration.",
  276. UnsupportedDefaultExport: "Only expressions, functions or classes are allowed as the `default` export.",
  277. UnsupportedImport: "`import` can only be used in `import()` or `import.meta`.",
  278. UnsupportedMetaProperty: ({
  279. target,
  280. onlyValidPropertyName
  281. }) => `The only valid meta property for ${target} is ${target}.${onlyValidPropertyName}.`,
  282. UnsupportedParameterDecorator: "Decorators cannot be used to decorate parameters.",
  283. UnsupportedPropertyDecorator: "Decorators cannot be used to decorate object literal properties.",
  284. UnsupportedSuper: "'super' can only be used with function calls (i.e. super()) or in property accesses (i.e. super.prop or super[prop]).",
  285. UnterminatedComment: "Unterminated comment.",
  286. UnterminatedRegExp: "Unterminated regular expression.",
  287. UnterminatedString: "Unterminated string constant.",
  288. UnterminatedTemplate: "Unterminated template.",
  289. UsingDeclarationExport: "Using declaration cannot be exported.",
  290. UsingDeclarationHasBindingPattern: "Using declaration cannot have destructuring patterns.",
  291. VarRedeclaration: ({
  292. identifierName
  293. }) => `Identifier '${identifierName}' has already been declared.`,
  294. YieldBindingIdentifier: "Can not use 'yield' as identifier inside a generator.",
  295. YieldInParameter: "Yield expression is not allowed in formal parameters.",
  296. ZeroDigitNumericSeparator: "Numeric separator can not be used after leading 0."
  297. };
  298. var StrictModeErrors = {
  299. StrictDelete: "Deleting local variable in strict mode.",
  300. StrictEvalArguments: ({
  301. referenceName
  302. }) => `Assigning to '${referenceName}' in strict mode.`,
  303. StrictEvalArgumentsBinding: ({
  304. bindingName
  305. }) => `Binding '${bindingName}' in strict mode.`,
  306. StrictFunction: "In strict mode code, functions can only be declared at top level or inside a block.",
  307. StrictNumericEscape: "The only valid numeric escape in strict mode is '\\0'.",
  308. StrictOctalLiteral: "Legacy octal literals are not allowed in strict mode.",
  309. StrictWith: "'with' in strict mode."
  310. };
  311. const UnparenthesizedPipeBodyDescriptions = new Set(["ArrowFunctionExpression", "AssignmentExpression", "ConditionalExpression", "YieldExpression"]);
  312. var PipelineOperatorErrors = {
  313. PipeBodyIsTighter: "Unexpected yield after pipeline body; any yield expression acting as Hack-style pipe body must be parenthesized due to its loose operator precedence.",
  314. PipeTopicRequiresHackPipes: 'Topic reference is used, but the pipelineOperator plugin was not passed a "proposal": "hack" or "smart" option.',
  315. PipeTopicUnbound: "Topic reference is unbound; it must be inside a pipe body.",
  316. PipeTopicUnconfiguredToken: ({
  317. token
  318. }) => `Invalid topic token ${token}. In order to use ${token} as a topic reference, the pipelineOperator plugin must be configured with { "proposal": "hack", "topicToken": "${token}" }.`,
  319. PipeTopicUnused: "Hack-style pipe body does not contain a topic reference; Hack-style pipes must use topic at least once.",
  320. PipeUnparenthesizedBody: ({
  321. type
  322. }) => `Hack-style pipe body cannot be an unparenthesized ${toNodeDescription({
  323. type
  324. })}; please wrap it in parentheses.`,
  325. PipelineBodyNoArrow: 'Unexpected arrow "=>" after pipeline body; arrow function in pipeline body must be parenthesized.',
  326. PipelineBodySequenceExpression: "Pipeline body may not be a comma-separated sequence expression.",
  327. PipelineHeadSequenceExpression: "Pipeline head should not be a comma-separated sequence expression.",
  328. PipelineTopicUnused: "Pipeline is in topic style but does not use topic reference.",
  329. PrimaryTopicNotAllowed: "Topic reference was used in a lexical context without topic binding.",
  330. PrimaryTopicRequiresSmartPipeline: 'Topic reference is used, but the pipelineOperator plugin was not passed a "proposal": "hack" or "smart" option.'
  331. };
  332. const _excluded = ["message"];
  333. function defineHidden(obj, key, value) {
  334. Object.defineProperty(obj, key, {
  335. enumerable: false,
  336. configurable: true,
  337. value
  338. });
  339. }
  340. function toParseErrorConstructor({
  341. toMessage,
  342. code,
  343. reasonCode,
  344. syntaxPlugin
  345. }) {
  346. const hasMissingPlugin = reasonCode === "MissingPlugin" || reasonCode === "MissingOneOfPlugins";
  347. {
  348. const oldReasonCodes = {
  349. AccessorCannotDeclareThisParameter: "AccesorCannotDeclareThisParameter",
  350. AccessorCannotHaveTypeParameters: "AccesorCannotHaveTypeParameters",
  351. ConstInitializerMustBeStringOrNumericLiteralOrLiteralEnumReference: "ConstInitiailizerMustBeStringOrNumericLiteralOrLiteralEnumReference",
  352. SetAccessorCannotHaveOptionalParameter: "SetAccesorCannotHaveOptionalParameter",
  353. SetAccessorCannotHaveRestParameter: "SetAccesorCannotHaveRestParameter",
  354. SetAccessorCannotHaveReturnType: "SetAccesorCannotHaveReturnType"
  355. };
  356. if (oldReasonCodes[reasonCode]) {
  357. reasonCode = oldReasonCodes[reasonCode];
  358. }
  359. }
  360. return function constructor(loc, details) {
  361. const error = new SyntaxError();
  362. error.code = code;
  363. error.reasonCode = reasonCode;
  364. error.loc = loc;
  365. error.pos = loc.index;
  366. error.syntaxPlugin = syntaxPlugin;
  367. if (hasMissingPlugin) {
  368. error.missingPlugin = details.missingPlugin;
  369. }
  370. defineHidden(error, "clone", function clone(overrides = {}) {
  371. var _overrides$loc;
  372. const {
  373. line,
  374. column,
  375. index
  376. } = (_overrides$loc = overrides.loc) != null ? _overrides$loc : loc;
  377. return constructor(new Position(line, column, index), Object.assign({}, details, overrides.details));
  378. });
  379. defineHidden(error, "details", details);
  380. Object.defineProperty(error, "message", {
  381. configurable: true,
  382. get() {
  383. const message = `${toMessage(details)} (${loc.line}:${loc.column})`;
  384. this.message = message;
  385. return message;
  386. },
  387. set(value) {
  388. Object.defineProperty(this, "message", {
  389. value,
  390. writable: true
  391. });
  392. }
  393. });
  394. return error;
  395. };
  396. }
  397. function ParseErrorEnum(argument, syntaxPlugin) {
  398. if (Array.isArray(argument)) {
  399. return parseErrorTemplates => ParseErrorEnum(parseErrorTemplates, argument[0]);
  400. }
  401. const ParseErrorConstructors = {};
  402. for (const reasonCode of Object.keys(argument)) {
  403. const template = argument[reasonCode];
  404. const _ref = typeof template === "string" ? {
  405. message: () => template
  406. } : typeof template === "function" ? {
  407. message: template
  408. } : template,
  409. {
  410. message
  411. } = _ref,
  412. rest = _objectWithoutPropertiesLoose(_ref, _excluded);
  413. const toMessage = typeof message === "string" ? () => message : message;
  414. ParseErrorConstructors[reasonCode] = toParseErrorConstructor(Object.assign({
  415. code: "BABEL_PARSER_SYNTAX_ERROR",
  416. reasonCode,
  417. toMessage
  418. }, syntaxPlugin ? {
  419. syntaxPlugin
  420. } : {}, rest));
  421. }
  422. return ParseErrorConstructors;
  423. }
  424. const Errors = Object.assign({}, ParseErrorEnum(ModuleErrors), ParseErrorEnum(StandardErrors), ParseErrorEnum(StrictModeErrors), ParseErrorEnum`pipelineOperator`(PipelineOperatorErrors));
  425. const {
  426. defineProperty
  427. } = Object;
  428. const toUnenumerable = (object, key) => {
  429. if (object) {
  430. defineProperty(object, key, {
  431. enumerable: false,
  432. value: object[key]
  433. });
  434. }
  435. };
  436. function toESTreeLocation(node) {
  437. toUnenumerable(node.loc.start, "index");
  438. toUnenumerable(node.loc.end, "index");
  439. return node;
  440. }
  441. var estree = superClass => class ESTreeParserMixin extends superClass {
  442. parse() {
  443. const file = toESTreeLocation(super.parse());
  444. if (this.options.tokens) {
  445. file.tokens = file.tokens.map(toESTreeLocation);
  446. }
  447. return file;
  448. }
  449. parseRegExpLiteral({
  450. pattern,
  451. flags
  452. }) {
  453. let regex = null;
  454. try {
  455. regex = new RegExp(pattern, flags);
  456. } catch (_) {}
  457. const node = this.estreeParseLiteral(regex);
  458. node.regex = {
  459. pattern,
  460. flags
  461. };
  462. return node;
  463. }
  464. parseBigIntLiteral(value) {
  465. let bigInt;
  466. try {
  467. bigInt = BigInt(value);
  468. } catch (_unused) {
  469. bigInt = null;
  470. }
  471. const node = this.estreeParseLiteral(bigInt);
  472. node.bigint = String(node.value || value);
  473. return node;
  474. }
  475. parseDecimalLiteral(value) {
  476. const decimal = null;
  477. const node = this.estreeParseLiteral(decimal);
  478. node.decimal = String(node.value || value);
  479. return node;
  480. }
  481. estreeParseLiteral(value) {
  482. return this.parseLiteral(value, "Literal");
  483. }
  484. parseStringLiteral(value) {
  485. return this.estreeParseLiteral(value);
  486. }
  487. parseNumericLiteral(value) {
  488. return this.estreeParseLiteral(value);
  489. }
  490. parseNullLiteral() {
  491. return this.estreeParseLiteral(null);
  492. }
  493. parseBooleanLiteral(value) {
  494. return this.estreeParseLiteral(value);
  495. }
  496. directiveToStmt(directive) {
  497. const expression = directive.value;
  498. delete directive.value;
  499. expression.type = "Literal";
  500. expression.raw = expression.extra.raw;
  501. expression.value = expression.extra.expressionValue;
  502. const stmt = directive;
  503. stmt.type = "ExpressionStatement";
  504. stmt.expression = expression;
  505. stmt.directive = expression.extra.rawValue;
  506. delete expression.extra;
  507. return stmt;
  508. }
  509. initFunction(node, isAsync) {
  510. super.initFunction(node, isAsync);
  511. node.expression = false;
  512. }
  513. checkDeclaration(node) {
  514. if (node != null && this.isObjectProperty(node)) {
  515. this.checkDeclaration(node.value);
  516. } else {
  517. super.checkDeclaration(node);
  518. }
  519. }
  520. getObjectOrClassMethodParams(method) {
  521. return method.value.params;
  522. }
  523. isValidDirective(stmt) {
  524. var _stmt$expression$extr;
  525. return stmt.type === "ExpressionStatement" && stmt.expression.type === "Literal" && typeof stmt.expression.value === "string" && !((_stmt$expression$extr = stmt.expression.extra) != null && _stmt$expression$extr.parenthesized);
  526. }
  527. parseBlockBody(node, allowDirectives, topLevel, end, afterBlockParse) {
  528. super.parseBlockBody(node, allowDirectives, topLevel, end, afterBlockParse);
  529. const directiveStatements = node.directives.map(d => this.directiveToStmt(d));
  530. node.body = directiveStatements.concat(node.body);
  531. delete node.directives;
  532. }
  533. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  534. this.parseMethod(method, isGenerator, isAsync, isConstructor, allowsDirectSuper, "ClassMethod", true);
  535. if (method.typeParameters) {
  536. method.value.typeParameters = method.typeParameters;
  537. delete method.typeParameters;
  538. }
  539. classBody.body.push(method);
  540. }
  541. parsePrivateName() {
  542. const node = super.parsePrivateName();
  543. {
  544. if (!this.getPluginOption("estree", "classFeatures")) {
  545. return node;
  546. }
  547. }
  548. return this.convertPrivateNameToPrivateIdentifier(node);
  549. }
  550. convertPrivateNameToPrivateIdentifier(node) {
  551. const name = super.getPrivateNameSV(node);
  552. node = node;
  553. delete node.id;
  554. node.name = name;
  555. node.type = "PrivateIdentifier";
  556. return node;
  557. }
  558. isPrivateName(node) {
  559. {
  560. if (!this.getPluginOption("estree", "classFeatures")) {
  561. return super.isPrivateName(node);
  562. }
  563. }
  564. return node.type === "PrivateIdentifier";
  565. }
  566. getPrivateNameSV(node) {
  567. {
  568. if (!this.getPluginOption("estree", "classFeatures")) {
  569. return super.getPrivateNameSV(node);
  570. }
  571. }
  572. return node.name;
  573. }
  574. parseLiteral(value, type) {
  575. const node = super.parseLiteral(value, type);
  576. node.raw = node.extra.raw;
  577. delete node.extra;
  578. return node;
  579. }
  580. parseFunctionBody(node, allowExpression, isMethod = false) {
  581. super.parseFunctionBody(node, allowExpression, isMethod);
  582. node.expression = node.body.type !== "BlockStatement";
  583. }
  584. parseMethod(node, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope = false) {
  585. let funcNode = this.startNode();
  586. funcNode.kind = node.kind;
  587. funcNode = super.parseMethod(funcNode, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope);
  588. funcNode.type = "FunctionExpression";
  589. delete funcNode.kind;
  590. node.value = funcNode;
  591. if (type === "ClassPrivateMethod") {
  592. node.computed = false;
  593. }
  594. return this.finishNode(node, "MethodDefinition");
  595. }
  596. nameIsConstructor(key) {
  597. if (key.type === "Literal") return key.value === "constructor";
  598. return super.nameIsConstructor(key);
  599. }
  600. parseClassProperty(...args) {
  601. const propertyNode = super.parseClassProperty(...args);
  602. {
  603. if (!this.getPluginOption("estree", "classFeatures")) {
  604. return propertyNode;
  605. }
  606. }
  607. propertyNode.type = "PropertyDefinition";
  608. return propertyNode;
  609. }
  610. parseClassPrivateProperty(...args) {
  611. const propertyNode = super.parseClassPrivateProperty(...args);
  612. {
  613. if (!this.getPluginOption("estree", "classFeatures")) {
  614. return propertyNode;
  615. }
  616. }
  617. propertyNode.type = "PropertyDefinition";
  618. propertyNode.computed = false;
  619. return propertyNode;
  620. }
  621. parseObjectMethod(prop, isGenerator, isAsync, isPattern, isAccessor) {
  622. const node = super.parseObjectMethod(prop, isGenerator, isAsync, isPattern, isAccessor);
  623. if (node) {
  624. node.type = "Property";
  625. if (node.kind === "method") {
  626. node.kind = "init";
  627. }
  628. node.shorthand = false;
  629. }
  630. return node;
  631. }
  632. parseObjectProperty(prop, startLoc, isPattern, refExpressionErrors) {
  633. const node = super.parseObjectProperty(prop, startLoc, isPattern, refExpressionErrors);
  634. if (node) {
  635. node.kind = "init";
  636. node.type = "Property";
  637. }
  638. return node;
  639. }
  640. isValidLVal(type, isUnparenthesizedInAssign, binding) {
  641. return type === "Property" ? "value" : super.isValidLVal(type, isUnparenthesizedInAssign, binding);
  642. }
  643. isAssignable(node, isBinding) {
  644. if (node != null && this.isObjectProperty(node)) {
  645. return this.isAssignable(node.value, isBinding);
  646. }
  647. return super.isAssignable(node, isBinding);
  648. }
  649. toAssignable(node, isLHS = false) {
  650. if (node != null && this.isObjectProperty(node)) {
  651. const {
  652. key,
  653. value
  654. } = node;
  655. if (this.isPrivateName(key)) {
  656. this.classScope.usePrivateName(this.getPrivateNameSV(key), key.loc.start);
  657. }
  658. this.toAssignable(value, isLHS);
  659. } else {
  660. super.toAssignable(node, isLHS);
  661. }
  662. }
  663. toAssignableObjectExpressionProp(prop, isLast, isLHS) {
  664. if (prop.type === "Property" && (prop.kind === "get" || prop.kind === "set")) {
  665. this.raise(Errors.PatternHasAccessor, prop.key);
  666. } else if (prop.type === "Property" && prop.method) {
  667. this.raise(Errors.PatternHasMethod, prop.key);
  668. } else {
  669. super.toAssignableObjectExpressionProp(prop, isLast, isLHS);
  670. }
  671. }
  672. finishCallExpression(unfinished, optional) {
  673. const node = super.finishCallExpression(unfinished, optional);
  674. if (node.callee.type === "Import") {
  675. node.type = "ImportExpression";
  676. node.source = node.arguments[0];
  677. if (this.hasPlugin("importAttributes") || this.hasPlugin("importAssertions")) {
  678. var _ref, _ref2;
  679. node.options = (_ref = node.arguments[1]) != null ? _ref : null;
  680. node.attributes = (_ref2 = node.arguments[1]) != null ? _ref2 : null;
  681. }
  682. delete node.arguments;
  683. delete node.callee;
  684. }
  685. return node;
  686. }
  687. toReferencedArguments(node) {
  688. if (node.type === "ImportExpression") {
  689. return;
  690. }
  691. super.toReferencedArguments(node);
  692. }
  693. parseExport(unfinished, decorators) {
  694. const exportStartLoc = this.state.lastTokStartLoc;
  695. const node = super.parseExport(unfinished, decorators);
  696. switch (node.type) {
  697. case "ExportAllDeclaration":
  698. node.exported = null;
  699. break;
  700. case "ExportNamedDeclaration":
  701. if (node.specifiers.length === 1 && node.specifiers[0].type === "ExportNamespaceSpecifier") {
  702. node.type = "ExportAllDeclaration";
  703. node.exported = node.specifiers[0].exported;
  704. delete node.specifiers;
  705. }
  706. case "ExportDefaultDeclaration":
  707. {
  708. var _declaration$decorato;
  709. const {
  710. declaration
  711. } = node;
  712. if ((declaration == null ? void 0 : declaration.type) === "ClassDeclaration" && ((_declaration$decorato = declaration.decorators) == null ? void 0 : _declaration$decorato.length) > 0 && declaration.start === node.start) {
  713. this.resetStartLocation(node, exportStartLoc);
  714. }
  715. }
  716. break;
  717. }
  718. return node;
  719. }
  720. parseSubscript(base, startLoc, noCalls, state) {
  721. const node = super.parseSubscript(base, startLoc, noCalls, state);
  722. if (state.optionalChainMember) {
  723. if (node.type === "OptionalMemberExpression" || node.type === "OptionalCallExpression") {
  724. node.type = node.type.substring(8);
  725. }
  726. if (state.stop) {
  727. const chain = this.startNodeAtNode(node);
  728. chain.expression = node;
  729. return this.finishNode(chain, "ChainExpression");
  730. }
  731. } else if (node.type === "MemberExpression" || node.type === "CallExpression") {
  732. node.optional = false;
  733. }
  734. return node;
  735. }
  736. isOptionalMemberExpression(node) {
  737. if (node.type === "ChainExpression") {
  738. return node.expression.type === "MemberExpression";
  739. }
  740. return super.isOptionalMemberExpression(node);
  741. }
  742. hasPropertyAsPrivateName(node) {
  743. if (node.type === "ChainExpression") {
  744. node = node.expression;
  745. }
  746. return super.hasPropertyAsPrivateName(node);
  747. }
  748. isObjectProperty(node) {
  749. return node.type === "Property" && node.kind === "init" && !node.method;
  750. }
  751. isObjectMethod(node) {
  752. return node.type === "Property" && (node.method || node.kind === "get" || node.kind === "set");
  753. }
  754. finishNodeAt(node, type, endLoc) {
  755. return toESTreeLocation(super.finishNodeAt(node, type, endLoc));
  756. }
  757. resetStartLocation(node, startLoc) {
  758. super.resetStartLocation(node, startLoc);
  759. toESTreeLocation(node);
  760. }
  761. resetEndLocation(node, endLoc = this.state.lastTokEndLoc) {
  762. super.resetEndLocation(node, endLoc);
  763. toESTreeLocation(node);
  764. }
  765. };
  766. class TokContext {
  767. constructor(token, preserveSpace) {
  768. this.token = void 0;
  769. this.preserveSpace = void 0;
  770. this.token = token;
  771. this.preserveSpace = !!preserveSpace;
  772. }
  773. }
  774. const types = {
  775. brace: new TokContext("{"),
  776. j_oTag: new TokContext("<tag"),
  777. j_cTag: new TokContext("</tag"),
  778. j_expr: new TokContext("<tag>...</tag>", true)
  779. };
  780. {
  781. types.template = new TokContext("`", true);
  782. }
  783. const beforeExpr = true;
  784. const startsExpr = true;
  785. const isLoop = true;
  786. const isAssign = true;
  787. const prefix = true;
  788. const postfix = true;
  789. class ExportedTokenType {
  790. constructor(label, conf = {}) {
  791. this.label = void 0;
  792. this.keyword = void 0;
  793. this.beforeExpr = void 0;
  794. this.startsExpr = void 0;
  795. this.rightAssociative = void 0;
  796. this.isLoop = void 0;
  797. this.isAssign = void 0;
  798. this.prefix = void 0;
  799. this.postfix = void 0;
  800. this.binop = void 0;
  801. this.label = label;
  802. this.keyword = conf.keyword;
  803. this.beforeExpr = !!conf.beforeExpr;
  804. this.startsExpr = !!conf.startsExpr;
  805. this.rightAssociative = !!conf.rightAssociative;
  806. this.isLoop = !!conf.isLoop;
  807. this.isAssign = !!conf.isAssign;
  808. this.prefix = !!conf.prefix;
  809. this.postfix = !!conf.postfix;
  810. this.binop = conf.binop != null ? conf.binop : null;
  811. {
  812. this.updateContext = null;
  813. }
  814. }
  815. }
  816. const keywords$1 = new Map();
  817. function createKeyword(name, options = {}) {
  818. options.keyword = name;
  819. const token = createToken(name, options);
  820. keywords$1.set(name, token);
  821. return token;
  822. }
  823. function createBinop(name, binop) {
  824. return createToken(name, {
  825. beforeExpr,
  826. binop
  827. });
  828. }
  829. let tokenTypeCounter = -1;
  830. const tokenTypes = [];
  831. const tokenLabels = [];
  832. const tokenBinops = [];
  833. const tokenBeforeExprs = [];
  834. const tokenStartsExprs = [];
  835. const tokenPrefixes = [];
  836. function createToken(name, options = {}) {
  837. var _options$binop, _options$beforeExpr, _options$startsExpr, _options$prefix;
  838. ++tokenTypeCounter;
  839. tokenLabels.push(name);
  840. tokenBinops.push((_options$binop = options.binop) != null ? _options$binop : -1);
  841. tokenBeforeExprs.push((_options$beforeExpr = options.beforeExpr) != null ? _options$beforeExpr : false);
  842. tokenStartsExprs.push((_options$startsExpr = options.startsExpr) != null ? _options$startsExpr : false);
  843. tokenPrefixes.push((_options$prefix = options.prefix) != null ? _options$prefix : false);
  844. tokenTypes.push(new ExportedTokenType(name, options));
  845. return tokenTypeCounter;
  846. }
  847. function createKeywordLike(name, options = {}) {
  848. var _options$binop2, _options$beforeExpr2, _options$startsExpr2, _options$prefix2;
  849. ++tokenTypeCounter;
  850. keywords$1.set(name, tokenTypeCounter);
  851. tokenLabels.push(name);
  852. tokenBinops.push((_options$binop2 = options.binop) != null ? _options$binop2 : -1);
  853. tokenBeforeExprs.push((_options$beforeExpr2 = options.beforeExpr) != null ? _options$beforeExpr2 : false);
  854. tokenStartsExprs.push((_options$startsExpr2 = options.startsExpr) != null ? _options$startsExpr2 : false);
  855. tokenPrefixes.push((_options$prefix2 = options.prefix) != null ? _options$prefix2 : false);
  856. tokenTypes.push(new ExportedTokenType("name", options));
  857. return tokenTypeCounter;
  858. }
  859. const tt = {
  860. bracketL: createToken("[", {
  861. beforeExpr,
  862. startsExpr
  863. }),
  864. bracketHashL: createToken("#[", {
  865. beforeExpr,
  866. startsExpr
  867. }),
  868. bracketBarL: createToken("[|", {
  869. beforeExpr,
  870. startsExpr
  871. }),
  872. bracketR: createToken("]"),
  873. bracketBarR: createToken("|]"),
  874. braceL: createToken("{", {
  875. beforeExpr,
  876. startsExpr
  877. }),
  878. braceBarL: createToken("{|", {
  879. beforeExpr,
  880. startsExpr
  881. }),
  882. braceHashL: createToken("#{", {
  883. beforeExpr,
  884. startsExpr
  885. }),
  886. braceR: createToken("}"),
  887. braceBarR: createToken("|}"),
  888. parenL: createToken("(", {
  889. beforeExpr,
  890. startsExpr
  891. }),
  892. parenR: createToken(")"),
  893. comma: createToken(",", {
  894. beforeExpr
  895. }),
  896. semi: createToken(";", {
  897. beforeExpr
  898. }),
  899. colon: createToken(":", {
  900. beforeExpr
  901. }),
  902. doubleColon: createToken("::", {
  903. beforeExpr
  904. }),
  905. dot: createToken("."),
  906. question: createToken("?", {
  907. beforeExpr
  908. }),
  909. questionDot: createToken("?."),
  910. arrow: createToken("=>", {
  911. beforeExpr
  912. }),
  913. template: createToken("template"),
  914. ellipsis: createToken("...", {
  915. beforeExpr
  916. }),
  917. backQuote: createToken("`", {
  918. startsExpr
  919. }),
  920. dollarBraceL: createToken("${", {
  921. beforeExpr,
  922. startsExpr
  923. }),
  924. templateTail: createToken("...`", {
  925. startsExpr
  926. }),
  927. templateNonTail: createToken("...${", {
  928. beforeExpr,
  929. startsExpr
  930. }),
  931. at: createToken("@"),
  932. hash: createToken("#", {
  933. startsExpr
  934. }),
  935. interpreterDirective: createToken("#!..."),
  936. eq: createToken("=", {
  937. beforeExpr,
  938. isAssign
  939. }),
  940. assign: createToken("_=", {
  941. beforeExpr,
  942. isAssign
  943. }),
  944. slashAssign: createToken("_=", {
  945. beforeExpr,
  946. isAssign
  947. }),
  948. xorAssign: createToken("_=", {
  949. beforeExpr,
  950. isAssign
  951. }),
  952. moduloAssign: createToken("_=", {
  953. beforeExpr,
  954. isAssign
  955. }),
  956. incDec: createToken("++/--", {
  957. prefix,
  958. postfix,
  959. startsExpr
  960. }),
  961. bang: createToken("!", {
  962. beforeExpr,
  963. prefix,
  964. startsExpr
  965. }),
  966. tilde: createToken("~", {
  967. beforeExpr,
  968. prefix,
  969. startsExpr
  970. }),
  971. doubleCaret: createToken("^^", {
  972. startsExpr
  973. }),
  974. doubleAt: createToken("@@", {
  975. startsExpr
  976. }),
  977. pipeline: createBinop("|>", 0),
  978. nullishCoalescing: createBinop("??", 1),
  979. logicalOR: createBinop("||", 1),
  980. logicalAND: createBinop("&&", 2),
  981. bitwiseOR: createBinop("|", 3),
  982. bitwiseXOR: createBinop("^", 4),
  983. bitwiseAND: createBinop("&", 5),
  984. equality: createBinop("==/!=/===/!==", 6),
  985. lt: createBinop("</>/<=/>=", 7),
  986. gt: createBinop("</>/<=/>=", 7),
  987. relational: createBinop("</>/<=/>=", 7),
  988. bitShift: createBinop("<</>>/>>>", 8),
  989. bitShiftL: createBinop("<</>>/>>>", 8),
  990. bitShiftR: createBinop("<</>>/>>>", 8),
  991. plusMin: createToken("+/-", {
  992. beforeExpr,
  993. binop: 9,
  994. prefix,
  995. startsExpr
  996. }),
  997. modulo: createToken("%", {
  998. binop: 10,
  999. startsExpr
  1000. }),
  1001. star: createToken("*", {
  1002. binop: 10
  1003. }),
  1004. slash: createBinop("/", 10),
  1005. exponent: createToken("**", {
  1006. beforeExpr,
  1007. binop: 11,
  1008. rightAssociative: true
  1009. }),
  1010. _in: createKeyword("in", {
  1011. beforeExpr,
  1012. binop: 7
  1013. }),
  1014. _instanceof: createKeyword("instanceof", {
  1015. beforeExpr,
  1016. binop: 7
  1017. }),
  1018. _break: createKeyword("break"),
  1019. _case: createKeyword("case", {
  1020. beforeExpr
  1021. }),
  1022. _catch: createKeyword("catch"),
  1023. _continue: createKeyword("continue"),
  1024. _debugger: createKeyword("debugger"),
  1025. _default: createKeyword("default", {
  1026. beforeExpr
  1027. }),
  1028. _else: createKeyword("else", {
  1029. beforeExpr
  1030. }),
  1031. _finally: createKeyword("finally"),
  1032. _function: createKeyword("function", {
  1033. startsExpr
  1034. }),
  1035. _if: createKeyword("if"),
  1036. _return: createKeyword("return", {
  1037. beforeExpr
  1038. }),
  1039. _switch: createKeyword("switch"),
  1040. _throw: createKeyword("throw", {
  1041. beforeExpr,
  1042. prefix,
  1043. startsExpr
  1044. }),
  1045. _try: createKeyword("try"),
  1046. _var: createKeyword("var"),
  1047. _const: createKeyword("const"),
  1048. _with: createKeyword("with"),
  1049. _new: createKeyword("new", {
  1050. beforeExpr,
  1051. startsExpr
  1052. }),
  1053. _this: createKeyword("this", {
  1054. startsExpr
  1055. }),
  1056. _super: createKeyword("super", {
  1057. startsExpr
  1058. }),
  1059. _class: createKeyword("class", {
  1060. startsExpr
  1061. }),
  1062. _extends: createKeyword("extends", {
  1063. beforeExpr
  1064. }),
  1065. _export: createKeyword("export"),
  1066. _import: createKeyword("import", {
  1067. startsExpr
  1068. }),
  1069. _null: createKeyword("null", {
  1070. startsExpr
  1071. }),
  1072. _true: createKeyword("true", {
  1073. startsExpr
  1074. }),
  1075. _false: createKeyword("false", {
  1076. startsExpr
  1077. }),
  1078. _typeof: createKeyword("typeof", {
  1079. beforeExpr,
  1080. prefix,
  1081. startsExpr
  1082. }),
  1083. _void: createKeyword("void", {
  1084. beforeExpr,
  1085. prefix,
  1086. startsExpr
  1087. }),
  1088. _delete: createKeyword("delete", {
  1089. beforeExpr,
  1090. prefix,
  1091. startsExpr
  1092. }),
  1093. _do: createKeyword("do", {
  1094. isLoop,
  1095. beforeExpr
  1096. }),
  1097. _for: createKeyword("for", {
  1098. isLoop
  1099. }),
  1100. _while: createKeyword("while", {
  1101. isLoop
  1102. }),
  1103. _as: createKeywordLike("as", {
  1104. startsExpr
  1105. }),
  1106. _assert: createKeywordLike("assert", {
  1107. startsExpr
  1108. }),
  1109. _async: createKeywordLike("async", {
  1110. startsExpr
  1111. }),
  1112. _await: createKeywordLike("await", {
  1113. startsExpr
  1114. }),
  1115. _defer: createKeywordLike("defer", {
  1116. startsExpr
  1117. }),
  1118. _from: createKeywordLike("from", {
  1119. startsExpr
  1120. }),
  1121. _get: createKeywordLike("get", {
  1122. startsExpr
  1123. }),
  1124. _let: createKeywordLike("let", {
  1125. startsExpr
  1126. }),
  1127. _meta: createKeywordLike("meta", {
  1128. startsExpr
  1129. }),
  1130. _of: createKeywordLike("of", {
  1131. startsExpr
  1132. }),
  1133. _sent: createKeywordLike("sent", {
  1134. startsExpr
  1135. }),
  1136. _set: createKeywordLike("set", {
  1137. startsExpr
  1138. }),
  1139. _source: createKeywordLike("source", {
  1140. startsExpr
  1141. }),
  1142. _static: createKeywordLike("static", {
  1143. startsExpr
  1144. }),
  1145. _using: createKeywordLike("using", {
  1146. startsExpr
  1147. }),
  1148. _yield: createKeywordLike("yield", {
  1149. startsExpr
  1150. }),
  1151. _asserts: createKeywordLike("asserts", {
  1152. startsExpr
  1153. }),
  1154. _checks: createKeywordLike("checks", {
  1155. startsExpr
  1156. }),
  1157. _exports: createKeywordLike("exports", {
  1158. startsExpr
  1159. }),
  1160. _global: createKeywordLike("global", {
  1161. startsExpr
  1162. }),
  1163. _implements: createKeywordLike("implements", {
  1164. startsExpr
  1165. }),
  1166. _intrinsic: createKeywordLike("intrinsic", {
  1167. startsExpr
  1168. }),
  1169. _infer: createKeywordLike("infer", {
  1170. startsExpr
  1171. }),
  1172. _is: createKeywordLike("is", {
  1173. startsExpr
  1174. }),
  1175. _mixins: createKeywordLike("mixins", {
  1176. startsExpr
  1177. }),
  1178. _proto: createKeywordLike("proto", {
  1179. startsExpr
  1180. }),
  1181. _require: createKeywordLike("require", {
  1182. startsExpr
  1183. }),
  1184. _satisfies: createKeywordLike("satisfies", {
  1185. startsExpr
  1186. }),
  1187. _keyof: createKeywordLike("keyof", {
  1188. startsExpr
  1189. }),
  1190. _readonly: createKeywordLike("readonly", {
  1191. startsExpr
  1192. }),
  1193. _unique: createKeywordLike("unique", {
  1194. startsExpr
  1195. }),
  1196. _abstract: createKeywordLike("abstract", {
  1197. startsExpr
  1198. }),
  1199. _declare: createKeywordLike("declare", {
  1200. startsExpr
  1201. }),
  1202. _enum: createKeywordLike("enum", {
  1203. startsExpr
  1204. }),
  1205. _module: createKeywordLike("module", {
  1206. startsExpr
  1207. }),
  1208. _namespace: createKeywordLike("namespace", {
  1209. startsExpr
  1210. }),
  1211. _interface: createKeywordLike("interface", {
  1212. startsExpr
  1213. }),
  1214. _type: createKeywordLike("type", {
  1215. startsExpr
  1216. }),
  1217. _opaque: createKeywordLike("opaque", {
  1218. startsExpr
  1219. }),
  1220. name: createToken("name", {
  1221. startsExpr
  1222. }),
  1223. string: createToken("string", {
  1224. startsExpr
  1225. }),
  1226. num: createToken("num", {
  1227. startsExpr
  1228. }),
  1229. bigint: createToken("bigint", {
  1230. startsExpr
  1231. }),
  1232. decimal: createToken("decimal", {
  1233. startsExpr
  1234. }),
  1235. regexp: createToken("regexp", {
  1236. startsExpr
  1237. }),
  1238. privateName: createToken("#name", {
  1239. startsExpr
  1240. }),
  1241. eof: createToken("eof"),
  1242. jsxName: createToken("jsxName"),
  1243. jsxText: createToken("jsxText", {
  1244. beforeExpr: true
  1245. }),
  1246. jsxTagStart: createToken("jsxTagStart", {
  1247. startsExpr: true
  1248. }),
  1249. jsxTagEnd: createToken("jsxTagEnd"),
  1250. placeholder: createToken("%%", {
  1251. startsExpr: true
  1252. })
  1253. };
  1254. function tokenIsIdentifier(token) {
  1255. return token >= 93 && token <= 132;
  1256. }
  1257. function tokenKeywordOrIdentifierIsKeyword(token) {
  1258. return token <= 92;
  1259. }
  1260. function tokenIsKeywordOrIdentifier(token) {
  1261. return token >= 58 && token <= 132;
  1262. }
  1263. function tokenIsLiteralPropertyName(token) {
  1264. return token >= 58 && token <= 136;
  1265. }
  1266. function tokenComesBeforeExpression(token) {
  1267. return tokenBeforeExprs[token];
  1268. }
  1269. function tokenCanStartExpression(token) {
  1270. return tokenStartsExprs[token];
  1271. }
  1272. function tokenIsAssignment(token) {
  1273. return token >= 29 && token <= 33;
  1274. }
  1275. function tokenIsFlowInterfaceOrTypeOrOpaque(token) {
  1276. return token >= 129 && token <= 131;
  1277. }
  1278. function tokenIsLoop(token) {
  1279. return token >= 90 && token <= 92;
  1280. }
  1281. function tokenIsKeyword(token) {
  1282. return token >= 58 && token <= 92;
  1283. }
  1284. function tokenIsOperator(token) {
  1285. return token >= 39 && token <= 59;
  1286. }
  1287. function tokenIsPostfix(token) {
  1288. return token === 34;
  1289. }
  1290. function tokenIsPrefix(token) {
  1291. return tokenPrefixes[token];
  1292. }
  1293. function tokenIsTSTypeOperator(token) {
  1294. return token >= 121 && token <= 123;
  1295. }
  1296. function tokenIsTSDeclarationStart(token) {
  1297. return token >= 124 && token <= 130;
  1298. }
  1299. function tokenLabelName(token) {
  1300. return tokenLabels[token];
  1301. }
  1302. function tokenOperatorPrecedence(token) {
  1303. return tokenBinops[token];
  1304. }
  1305. function tokenIsRightAssociative(token) {
  1306. return token === 57;
  1307. }
  1308. function tokenIsTemplate(token) {
  1309. return token >= 24 && token <= 25;
  1310. }
  1311. function getExportedToken(token) {
  1312. return tokenTypes[token];
  1313. }
  1314. {
  1315. tokenTypes[8].updateContext = context => {
  1316. context.pop();
  1317. };
  1318. tokenTypes[5].updateContext = tokenTypes[7].updateContext = tokenTypes[23].updateContext = context => {
  1319. context.push(types.brace);
  1320. };
  1321. tokenTypes[22].updateContext = context => {
  1322. if (context[context.length - 1] === types.template) {
  1323. context.pop();
  1324. } else {
  1325. context.push(types.template);
  1326. }
  1327. };
  1328. tokenTypes[142].updateContext = context => {
  1329. context.push(types.j_expr, types.j_oTag);
  1330. };
  1331. }
  1332. let nonASCIIidentifierStartChars = "\xaa\xb5\xba\xc0-\xd6\xd8-\xf6\xf8-\u02c1\u02c6-\u02d1\u02e0-\u02e4\u02ec\u02ee\u0370-\u0374\u0376\u0377\u037a-\u037d\u037f\u0386\u0388-\u038a\u038c\u038e-\u03a1\u03a3-\u03f5\u03f7-\u0481\u048a-\u052f\u0531-\u0556\u0559\u0560-\u0588\u05d0-\u05ea\u05ef-\u05f2\u0620-\u064a\u066e\u066f\u0671-\u06d3\u06d5\u06e5\u06e6\u06ee\u06ef\u06fa-\u06fc\u06ff\u0710\u0712-\u072f\u074d-\u07a5\u07b1\u07ca-\u07ea\u07f4\u07f5\u07fa\u0800-\u0815\u081a\u0824\u0828\u0840-\u0858\u0860-\u086a\u0870-\u0887\u0889-\u088e\u08a0-\u08c9\u0904-\u0939\u093d\u0950\u0958-\u0961\u0971-\u0980\u0985-\u098c\u098f\u0990\u0993-\u09a8\u09aa-\u09b0\u09b2\u09b6-\u09b9\u09bd\u09ce\u09dc\u09dd\u09df-\u09e1\u09f0\u09f1\u09fc\u0a05-\u0a0a\u0a0f\u0a10\u0a13-\u0a28\u0a2a-\u0a30\u0a32\u0a33\u0a35\u0a36\u0a38\u0a39\u0a59-\u0a5c\u0a5e\u0a72-\u0a74\u0a85-\u0a8d\u0a8f-\u0a91\u0a93-\u0aa8\u0aaa-\u0ab0\u0ab2\u0ab3\u0ab5-\u0ab9\u0abd\u0ad0\u0ae0\u0ae1\u0af9\u0b05-\u0b0c\u0b0f\u0b10\u0b13-\u0b28\u0b2a-\u0b30\u0b32\u0b33\u0b35-\u0b39\u0b3d\u0b5c\u0b5d\u0b5f-\u0b61\u0b71\u0b83\u0b85-\u0b8a\u0b8e-\u0b90\u0b92-\u0b95\u0b99\u0b9a\u0b9c\u0b9e\u0b9f\u0ba3\u0ba4\u0ba8-\u0baa\u0bae-\u0bb9\u0bd0\u0c05-\u0c0c\u0c0e-\u0c10\u0c12-\u0c28\u0c2a-\u0c39\u0c3d\u0c58-\u0c5a\u0c5d\u0c60\u0c61\u0c80\u0c85-\u0c8c\u0c8e-\u0c90\u0c92-\u0ca8\u0caa-\u0cb3\u0cb5-\u0cb9\u0cbd\u0cdd\u0cde\u0ce0\u0ce1\u0cf1\u0cf2\u0d04-\u0d0c\u0d0e-\u0d10\u0d12-\u0d3a\u0d3d\u0d4e\u0d54-\u0d56\u0d5f-\u0d61\u0d7a-\u0d7f\u0d85-\u0d96\u0d9a-\u0db1\u0db3-\u0dbb\u0dbd\u0dc0-\u0dc6\u0e01-\u0e30\u0e32\u0e33\u0e40-\u0e46\u0e81\u0e82\u0e84\u0e86-\u0e8a\u0e8c-\u0ea3\u0ea5\u0ea7-\u0eb0\u0eb2\u0eb3\u0ebd\u0ec0-\u0ec4\u0ec6\u0edc-\u0edf\u0f00\u0f40-\u0f47\u0f49-\u0f6c\u0f88-\u0f8c\u1000-\u102a\u103f\u1050-\u1055\u105a-\u105d\u1061\u1065\u1066\u106e-\u1070\u1075-\u1081\u108e\u10a0-\u10c5\u10c7\u10cd\u10d0-\u10fa\u10fc-\u1248\u124a-\u124d\u1250-\u1256\u1258\u125a-\u125d\u1260-\u1288\u128a-\u128d\u1290-\u12b0\u12b2-\u12b5\u12b8-\u12be\u12c0\u12c2-\u12c5\u12c8-\u12d6\u12d8-\u1310\u1312-\u1315\u1318-\u135a\u1380-\u138f\u13a0-\u13f5\u13f8-\u13fd\u1401-\u166c\u166f-\u167f\u1681-\u169a\u16a0-\u16ea\u16ee-\u16f8\u1700-\u1711\u171f-\u1731\u1740-\u1751\u1760-\u176c\u176e-\u1770\u1780-\u17b3\u17d7\u17dc\u1820-\u1878\u1880-\u18a8\u18aa\u18b0-\u18f5\u1900-\u191e\u1950-\u196d\u1970-\u1974\u1980-\u19ab\u19b0-\u19c9\u1a00-\u1a16\u1a20-\u1a54\u1aa7\u1b05-\u1b33\u1b45-\u1b4c\u1b83-\u1ba0\u1bae\u1baf\u1bba-\u1be5\u1c00-\u1c23\u1c4d-\u1c4f\u1c5a-\u1c7d\u1c80-\u1c88\u1c90-\u1cba\u1cbd-\u1cbf\u1ce9-\u1cec\u1cee-\u1cf3\u1cf5\u1cf6\u1cfa\u1d00-\u1dbf\u1e00-\u1f15\u1f18-\u1f1d\u1f20-\u1f45\u1f48-\u1f4d\u1f50-\u1f57\u1f59\u1f5b\u1f5d\u1f5f-\u1f7d\u1f80-\u1fb4\u1fb6-\u1fbc\u1fbe\u1fc2-\u1fc4\u1fc6-\u1fcc\u1fd0-\u1fd3\u1fd6-\u1fdb\u1fe0-\u1fec\u1ff2-\u1ff4\u1ff6-\u1ffc\u2071\u207f\u2090-\u209c\u2102\u2107\u210a-\u2113\u2115\u2118-\u211d\u2124\u2126\u2128\u212a-\u2139\u213c-\u213f\u2145-\u2149\u214e\u2160-\u2188\u2c00-\u2ce4\u2ceb-\u2cee\u2cf2\u2cf3\u2d00-\u2d25\u2d27\u2d2d\u2d30-\u2d67\u2d6f\u2d80-\u2d96\u2da0-\u2da6\u2da8-\u2dae\u2db0-\u2db6\u2db8-\u2dbe\u2dc0-\u2dc6\u2dc8-\u2dce\u2dd0-\u2dd6\u2dd8-\u2dde\u3005-\u3007\u3021-\u3029\u3031-\u3035\u3038-\u303c\u3041-\u3096\u309b-\u309f\u30a1-\u30fa\u30fc-\u30ff\u3105-\u312f\u3131-\u318e\u31a0-\u31bf\u31f0-\u31ff\u3400-\u4dbf\u4e00-\ua48c\ua4d0-\ua4fd\ua500-\ua60c\ua610-\ua61f\ua62a\ua62b\ua640-\ua66e\ua67f-\ua69d\ua6a0-\ua6ef\ua717-\ua71f\ua722-\ua788\ua78b-\ua7ca\ua7d0\ua7d1\ua7d3\ua7d5-\ua7d9\ua7f2-\ua801\ua803-\ua805\ua807-\ua80a\ua80c-\ua822\ua840-\ua873\ua882-\ua8b3\ua8f2-\ua8f7\ua8fb\ua8fd\ua8fe\ua90a-\ua925\ua930-\ua946\ua960-\ua97c\ua984-\ua9b2\ua9cf\ua9e0-\ua9e4\ua9e6-\ua9ef\ua9fa-\ua9fe\uaa00-\uaa28\uaa40-\uaa42\uaa44-\uaa4b\uaa60-\uaa76\uaa7a\uaa7e-\uaaaf\uaab1\uaab5\uaab6\uaab9-\uaabd\uaac0\uaac2\uaadb-\uaadd\uaae0-\uaaea\uaaf2-\uaaf4\uab01-\uab06\uab09-\uab0e\uab11-\uab16\uab20-\uab26\uab28-\uab2e\uab30-\uab5a\uab5c-\uab69\uab70-\uabe2\uac00-\ud7a3\ud7b0-\ud7c6\ud7cb-\ud7fb\uf900-\ufa6d\ufa70-\ufad9\ufb00-\ufb06\ufb13-\ufb17\ufb1d\ufb1f-\ufb28\ufb2a-\ufb36\ufb38-\ufb3c\ufb3e\ufb40\ufb41\ufb43\ufb44\ufb46-\ufbb1\ufbd3-\ufd3d\ufd50-\ufd8f\ufd92-\ufdc7\ufdf0-\ufdfb\ufe70-\ufe74\ufe76-\ufefc\uff21-\uff3a\uff41-\uff5a\uff66-\uffbe\uffc2-\uffc7\uffca-\uffcf\uffd2-\uffd7\uffda-\uffdc";
  1333. let nonASCIIidentifierChars = "\u200c\u200d\xb7\u0300-\u036f\u0387\u0483-\u0487\u0591-\u05bd\u05bf\u05c1\u05c2\u05c4\u05c5\u05c7\u0610-\u061a\u064b-\u0669\u0670\u06d6-\u06dc\u06df-\u06e4\u06e7\u06e8\u06ea-\u06ed\u06f0-\u06f9\u0711\u0730-\u074a\u07a6-\u07b0\u07c0-\u07c9\u07eb-\u07f3\u07fd\u0816-\u0819\u081b-\u0823\u0825-\u0827\u0829-\u082d\u0859-\u085b\u0898-\u089f\u08ca-\u08e1\u08e3-\u0903\u093a-\u093c\u093e-\u094f\u0951-\u0957\u0962\u0963\u0966-\u096f\u0981-\u0983\u09bc\u09be-\u09c4\u09c7\u09c8\u09cb-\u09cd\u09d7\u09e2\u09e3\u09e6-\u09ef\u09fe\u0a01-\u0a03\u0a3c\u0a3e-\u0a42\u0a47\u0a48\u0a4b-\u0a4d\u0a51\u0a66-\u0a71\u0a75\u0a81-\u0a83\u0abc\u0abe-\u0ac5\u0ac7-\u0ac9\u0acb-\u0acd\u0ae2\u0ae3\u0ae6-\u0aef\u0afa-\u0aff\u0b01-\u0b03\u0b3c\u0b3e-\u0b44\u0b47\u0b48\u0b4b-\u0b4d\u0b55-\u0b57\u0b62\u0b63\u0b66-\u0b6f\u0b82\u0bbe-\u0bc2\u0bc6-\u0bc8\u0bca-\u0bcd\u0bd7\u0be6-\u0bef\u0c00-\u0c04\u0c3c\u0c3e-\u0c44\u0c46-\u0c48\u0c4a-\u0c4d\u0c55\u0c56\u0c62\u0c63\u0c66-\u0c6f\u0c81-\u0c83\u0cbc\u0cbe-\u0cc4\u0cc6-\u0cc8\u0cca-\u0ccd\u0cd5\u0cd6\u0ce2\u0ce3\u0ce6-\u0cef\u0cf3\u0d00-\u0d03\u0d3b\u0d3c\u0d3e-\u0d44\u0d46-\u0d48\u0d4a-\u0d4d\u0d57\u0d62\u0d63\u0d66-\u0d6f\u0d81-\u0d83\u0dca\u0dcf-\u0dd4\u0dd6\u0dd8-\u0ddf\u0de6-\u0def\u0df2\u0df3\u0e31\u0e34-\u0e3a\u0e47-\u0e4e\u0e50-\u0e59\u0eb1\u0eb4-\u0ebc\u0ec8-\u0ece\u0ed0-\u0ed9\u0f18\u0f19\u0f20-\u0f29\u0f35\u0f37\u0f39\u0f3e\u0f3f\u0f71-\u0f84\u0f86\u0f87\u0f8d-\u0f97\u0f99-\u0fbc\u0fc6\u102b-\u103e\u1040-\u1049\u1056-\u1059\u105e-\u1060\u1062-\u1064\u1067-\u106d\u1071-\u1074\u1082-\u108d\u108f-\u109d\u135d-\u135f\u1369-\u1371\u1712-\u1715\u1732-\u1734\u1752\u1753\u1772\u1773\u17b4-\u17d3\u17dd\u17e0-\u17e9\u180b-\u180d\u180f-\u1819\u18a9\u1920-\u192b\u1930-\u193b\u1946-\u194f\u19d0-\u19da\u1a17-\u1a1b\u1a55-\u1a5e\u1a60-\u1a7c\u1a7f-\u1a89\u1a90-\u1a99\u1ab0-\u1abd\u1abf-\u1ace\u1b00-\u1b04\u1b34-\u1b44\u1b50-\u1b59\u1b6b-\u1b73\u1b80-\u1b82\u1ba1-\u1bad\u1bb0-\u1bb9\u1be6-\u1bf3\u1c24-\u1c37\u1c40-\u1c49\u1c50-\u1c59\u1cd0-\u1cd2\u1cd4-\u1ce8\u1ced\u1cf4\u1cf7-\u1cf9\u1dc0-\u1dff\u200c\u200d\u203f\u2040\u2054\u20d0-\u20dc\u20e1\u20e5-\u20f0\u2cef-\u2cf1\u2d7f\u2de0-\u2dff\u302a-\u302f\u3099\u309a\u30fb\ua620-\ua629\ua66f\ua674-\ua67d\ua69e\ua69f\ua6f0\ua6f1\ua802\ua806\ua80b\ua823-\ua827\ua82c\ua880\ua881\ua8b4-\ua8c5\ua8d0-\ua8d9\ua8e0-\ua8f1\ua8ff-\ua909\ua926-\ua92d\ua947-\ua953\ua980-\ua983\ua9b3-\ua9c0\ua9d0-\ua9d9\ua9e5\ua9f0-\ua9f9\uaa29-\uaa36\uaa43\uaa4c\uaa4d\uaa50-\uaa59\uaa7b-\uaa7d\uaab0\uaab2-\uaab4\uaab7\uaab8\uaabe\uaabf\uaac1\uaaeb-\uaaef\uaaf5\uaaf6\uabe3-\uabea\uabec\uabed\uabf0-\uabf9\ufb1e\ufe00-\ufe0f\ufe20-\ufe2f\ufe33\ufe34\ufe4d-\ufe4f\uff10-\uff19\uff3f\uff65";
  1334. const nonASCIIidentifierStart = new RegExp("[" + nonASCIIidentifierStartChars + "]");
  1335. const nonASCIIidentifier = new RegExp("[" + nonASCIIidentifierStartChars + nonASCIIidentifierChars + "]");
  1336. nonASCIIidentifierStartChars = nonASCIIidentifierChars = null;
  1337. const astralIdentifierStartCodes = [0, 11, 2, 25, 2, 18, 2, 1, 2, 14, 3, 13, 35, 122, 70, 52, 268, 28, 4, 48, 48, 31, 14, 29, 6, 37, 11, 29, 3, 35, 5, 7, 2, 4, 43, 157, 19, 35, 5, 35, 5, 39, 9, 51, 13, 10, 2, 14, 2, 6, 2, 1, 2, 10, 2, 14, 2, 6, 2, 1, 68, 310, 10, 21, 11, 7, 25, 5, 2, 41, 2, 8, 70, 5, 3, 0, 2, 43, 2, 1, 4, 0, 3, 22, 11, 22, 10, 30, 66, 18, 2, 1, 11, 21, 11, 25, 71, 55, 7, 1, 65, 0, 16, 3, 2, 2, 2, 28, 43, 28, 4, 28, 36, 7, 2, 27, 28, 53, 11, 21, 11, 18, 14, 17, 111, 72, 56, 50, 14, 50, 14, 35, 349, 41, 7, 1, 79, 28, 11, 0, 9, 21, 43, 17, 47, 20, 28, 22, 13, 52, 58, 1, 3, 0, 14, 44, 33, 24, 27, 35, 30, 0, 3, 0, 9, 34, 4, 0, 13, 47, 15, 3, 22, 0, 2, 0, 36, 17, 2, 24, 20, 1, 64, 6, 2, 0, 2, 3, 2, 14, 2, 9, 8, 46, 39, 7, 3, 1, 3, 21, 2, 6, 2, 1, 2, 4, 4, 0, 19, 0, 13, 4, 159, 52, 19, 3, 21, 2, 31, 47, 21, 1, 2, 0, 185, 46, 42, 3, 37, 47, 21, 0, 60, 42, 14, 0, 72, 26, 38, 6, 186, 43, 117, 63, 32, 7, 3, 0, 3, 7, 2, 1, 2, 23, 16, 0, 2, 0, 95, 7, 3, 38, 17, 0, 2, 0, 29, 0, 11, 39, 8, 0, 22, 0, 12, 45, 20, 0, 19, 72, 264, 8, 2, 36, 18, 0, 50, 29, 113, 6, 2, 1, 2, 37, 22, 0, 26, 5, 2, 1, 2, 31, 15, 0, 328, 18, 16, 0, 2, 12, 2, 33, 125, 0, 80, 921, 103, 110, 18, 195, 2637, 96, 16, 1071, 18, 5, 4026, 582, 8634, 568, 8, 30, 18, 78, 18, 29, 19, 47, 17, 3, 32, 20, 6, 18, 689, 63, 129, 74, 6, 0, 67, 12, 65, 1, 2, 0, 29, 6135, 9, 1237, 43, 8, 8936, 3, 2, 6, 2, 1, 2, 290, 16, 0, 30, 2, 3, 0, 15, 3, 9, 395, 2309, 106, 6, 12, 4, 8, 8, 9, 5991, 84, 2, 70, 2, 1, 3, 0, 3, 1, 3, 3, 2, 11, 2, 0, 2, 6, 2, 64, 2, 3, 3, 7, 2, 6, 2, 27, 2, 3, 2, 4, 2, 0, 4, 6, 2, 339, 3, 24, 2, 24, 2, 30, 2, 24, 2, 30, 2, 24, 2, 30, 2, 24, 2, 30, 2, 24, 2, 7, 1845, 30, 7, 5, 262, 61, 147, 44, 11, 6, 17, 0, 322, 29, 19, 43, 485, 27, 757, 6, 2, 3, 2, 1, 2, 14, 2, 196, 60, 67, 8, 0, 1205, 3, 2, 26, 2, 1, 2, 0, 3, 0, 2, 9, 2, 3, 2, 0, 2, 0, 7, 0, 5, 0, 2, 0, 2, 0, 2, 2, 2, 1, 2, 0, 3, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 1, 2, 0, 3, 3, 2, 6, 2, 3, 2, 3, 2, 0, 2, 9, 2, 16, 6, 2, 2, 4, 2, 16, 4421, 42719, 33, 4153, 7, 221, 3, 5761, 15, 7472, 16, 621, 2467, 541, 1507, 4938, 6, 4191];
  1338. const astralIdentifierCodes = [509, 0, 227, 0, 150, 4, 294, 9, 1368, 2, 2, 1, 6, 3, 41, 2, 5, 0, 166, 1, 574, 3, 9, 9, 370, 1, 81, 2, 71, 10, 50, 3, 123, 2, 54, 14, 32, 10, 3, 1, 11, 3, 46, 10, 8, 0, 46, 9, 7, 2, 37, 13, 2, 9, 6, 1, 45, 0, 13, 2, 49, 13, 9, 3, 2, 11, 83, 11, 7, 0, 3, 0, 158, 11, 6, 9, 7, 3, 56, 1, 2, 6, 3, 1, 3, 2, 10, 0, 11, 1, 3, 6, 4, 4, 193, 17, 10, 9, 5, 0, 82, 19, 13, 9, 214, 6, 3, 8, 28, 1, 83, 16, 16, 9, 82, 12, 9, 9, 84, 14, 5, 9, 243, 14, 166, 9, 71, 5, 2, 1, 3, 3, 2, 0, 2, 1, 13, 9, 120, 6, 3, 6, 4, 0, 29, 9, 41, 6, 2, 3, 9, 0, 10, 10, 47, 15, 406, 7, 2, 7, 17, 9, 57, 21, 2, 13, 123, 5, 4, 0, 2, 1, 2, 6, 2, 0, 9, 9, 49, 4, 2, 1, 2, 4, 9, 9, 330, 3, 10, 1, 2, 0, 49, 6, 4, 4, 14, 9, 5351, 0, 7, 14, 13835, 9, 87, 9, 39, 4, 60, 6, 26, 9, 1014, 0, 2, 54, 8, 3, 82, 0, 12, 1, 19628, 1, 4706, 45, 3, 22, 543, 4, 4, 5, 9, 7, 3, 6, 31, 3, 149, 2, 1418, 49, 513, 54, 5, 49, 9, 0, 15, 0, 23, 4, 2, 14, 1361, 6, 2, 16, 3, 6, 2, 1, 2, 4, 101, 0, 161, 6, 10, 9, 357, 0, 62, 13, 499, 13, 983, 6, 110, 6, 6, 9, 4759, 9, 787719, 239];
  1339. function isInAstralSet(code, set) {
  1340. let pos = 0x10000;
  1341. for (let i = 0, length = set.length; i < length; i += 2) {
  1342. pos += set[i];
  1343. if (pos > code) return false;
  1344. pos += set[i + 1];
  1345. if (pos >= code) return true;
  1346. }
  1347. return false;
  1348. }
  1349. function isIdentifierStart(code) {
  1350. if (code < 65) return code === 36;
  1351. if (code <= 90) return true;
  1352. if (code < 97) return code === 95;
  1353. if (code <= 122) return true;
  1354. if (code <= 0xffff) {
  1355. return code >= 0xaa && nonASCIIidentifierStart.test(String.fromCharCode(code));
  1356. }
  1357. return isInAstralSet(code, astralIdentifierStartCodes);
  1358. }
  1359. function isIdentifierChar(code) {
  1360. if (code < 48) return code === 36;
  1361. if (code < 58) return true;
  1362. if (code < 65) return false;
  1363. if (code <= 90) return true;
  1364. if (code < 97) return code === 95;
  1365. if (code <= 122) return true;
  1366. if (code <= 0xffff) {
  1367. return code >= 0xaa && nonASCIIidentifier.test(String.fromCharCode(code));
  1368. }
  1369. return isInAstralSet(code, astralIdentifierStartCodes) || isInAstralSet(code, astralIdentifierCodes);
  1370. }
  1371. const reservedWords = {
  1372. keyword: ["break", "case", "catch", "continue", "debugger", "default", "do", "else", "finally", "for", "function", "if", "return", "switch", "throw", "try", "var", "const", "while", "with", "new", "this", "super", "class", "extends", "export", "import", "null", "true", "false", "in", "instanceof", "typeof", "void", "delete"],
  1373. strict: ["implements", "interface", "let", "package", "private", "protected", "public", "static", "yield"],
  1374. strictBind: ["eval", "arguments"]
  1375. };
  1376. const keywords = new Set(reservedWords.keyword);
  1377. const reservedWordsStrictSet = new Set(reservedWords.strict);
  1378. const reservedWordsStrictBindSet = new Set(reservedWords.strictBind);
  1379. function isReservedWord(word, inModule) {
  1380. return inModule && word === "await" || word === "enum";
  1381. }
  1382. function isStrictReservedWord(word, inModule) {
  1383. return isReservedWord(word, inModule) || reservedWordsStrictSet.has(word);
  1384. }
  1385. function isStrictBindOnlyReservedWord(word) {
  1386. return reservedWordsStrictBindSet.has(word);
  1387. }
  1388. function isStrictBindReservedWord(word, inModule) {
  1389. return isStrictReservedWord(word, inModule) || isStrictBindOnlyReservedWord(word);
  1390. }
  1391. function isKeyword(word) {
  1392. return keywords.has(word);
  1393. }
  1394. function isIteratorStart(current, next, next2) {
  1395. return current === 64 && next === 64 && isIdentifierStart(next2);
  1396. }
  1397. const reservedWordLikeSet = new Set(["break", "case", "catch", "continue", "debugger", "default", "do", "else", "finally", "for", "function", "if", "return", "switch", "throw", "try", "var", "const", "while", "with", "new", "this", "super", "class", "extends", "export", "import", "null", "true", "false", "in", "instanceof", "typeof", "void", "delete", "implements", "interface", "let", "package", "private", "protected", "public", "static", "yield", "eval", "arguments", "enum", "await"]);
  1398. function canBeReservedWord(word) {
  1399. return reservedWordLikeSet.has(word);
  1400. }
  1401. class Scope {
  1402. constructor(flags) {
  1403. this.flags = 0;
  1404. this.names = new Map();
  1405. this.firstLexicalName = "";
  1406. this.flags = flags;
  1407. }
  1408. }
  1409. class ScopeHandler {
  1410. constructor(parser, inModule) {
  1411. this.parser = void 0;
  1412. this.scopeStack = [];
  1413. this.inModule = void 0;
  1414. this.undefinedExports = new Map();
  1415. this.parser = parser;
  1416. this.inModule = inModule;
  1417. }
  1418. get inTopLevel() {
  1419. return (this.currentScope().flags & 1) > 0;
  1420. }
  1421. get inFunction() {
  1422. return (this.currentVarScopeFlags() & 2) > 0;
  1423. }
  1424. get allowSuper() {
  1425. return (this.currentThisScopeFlags() & 16) > 0;
  1426. }
  1427. get allowDirectSuper() {
  1428. return (this.currentThisScopeFlags() & 32) > 0;
  1429. }
  1430. get inClass() {
  1431. return (this.currentThisScopeFlags() & 64) > 0;
  1432. }
  1433. get inClassAndNotInNonArrowFunction() {
  1434. const flags = this.currentThisScopeFlags();
  1435. return (flags & 64) > 0 && (flags & 2) === 0;
  1436. }
  1437. get inStaticBlock() {
  1438. for (let i = this.scopeStack.length - 1;; i--) {
  1439. const {
  1440. flags
  1441. } = this.scopeStack[i];
  1442. if (flags & 128) {
  1443. return true;
  1444. }
  1445. if (flags & (387 | 64)) {
  1446. return false;
  1447. }
  1448. }
  1449. }
  1450. get inNonArrowFunction() {
  1451. return (this.currentThisScopeFlags() & 2) > 0;
  1452. }
  1453. get treatFunctionsAsVar() {
  1454. return this.treatFunctionsAsVarInScope(this.currentScope());
  1455. }
  1456. createScope(flags) {
  1457. return new Scope(flags);
  1458. }
  1459. enter(flags) {
  1460. this.scopeStack.push(this.createScope(flags));
  1461. }
  1462. exit() {
  1463. const scope = this.scopeStack.pop();
  1464. return scope.flags;
  1465. }
  1466. treatFunctionsAsVarInScope(scope) {
  1467. return !!(scope.flags & (2 | 128) || !this.parser.inModule && scope.flags & 1);
  1468. }
  1469. declareName(name, bindingType, loc) {
  1470. let scope = this.currentScope();
  1471. if (bindingType & 8 || bindingType & 16) {
  1472. this.checkRedeclarationInScope(scope, name, bindingType, loc);
  1473. let type = scope.names.get(name) || 0;
  1474. if (bindingType & 16) {
  1475. type = type | 4;
  1476. } else {
  1477. if (!scope.firstLexicalName) {
  1478. scope.firstLexicalName = name;
  1479. }
  1480. type = type | 2;
  1481. }
  1482. scope.names.set(name, type);
  1483. if (bindingType & 8) {
  1484. this.maybeExportDefined(scope, name);
  1485. }
  1486. } else if (bindingType & 4) {
  1487. for (let i = this.scopeStack.length - 1; i >= 0; --i) {
  1488. scope = this.scopeStack[i];
  1489. this.checkRedeclarationInScope(scope, name, bindingType, loc);
  1490. scope.names.set(name, (scope.names.get(name) || 0) | 1);
  1491. this.maybeExportDefined(scope, name);
  1492. if (scope.flags & 387) break;
  1493. }
  1494. }
  1495. if (this.parser.inModule && scope.flags & 1) {
  1496. this.undefinedExports.delete(name);
  1497. }
  1498. }
  1499. maybeExportDefined(scope, name) {
  1500. if (this.parser.inModule && scope.flags & 1) {
  1501. this.undefinedExports.delete(name);
  1502. }
  1503. }
  1504. checkRedeclarationInScope(scope, name, bindingType, loc) {
  1505. if (this.isRedeclaredInScope(scope, name, bindingType)) {
  1506. this.parser.raise(Errors.VarRedeclaration, loc, {
  1507. identifierName: name
  1508. });
  1509. }
  1510. }
  1511. isRedeclaredInScope(scope, name, bindingType) {
  1512. if (!(bindingType & 1)) return false;
  1513. if (bindingType & 8) {
  1514. return scope.names.has(name);
  1515. }
  1516. const type = scope.names.get(name);
  1517. if (bindingType & 16) {
  1518. return (type & 2) > 0 || !this.treatFunctionsAsVarInScope(scope) && (type & 1) > 0;
  1519. }
  1520. return (type & 2) > 0 && !(scope.flags & 8 && scope.firstLexicalName === name) || !this.treatFunctionsAsVarInScope(scope) && (type & 4) > 0;
  1521. }
  1522. checkLocalExport(id) {
  1523. const {
  1524. name
  1525. } = id;
  1526. const topLevelScope = this.scopeStack[0];
  1527. if (!topLevelScope.names.has(name)) {
  1528. this.undefinedExports.set(name, id.loc.start);
  1529. }
  1530. }
  1531. currentScope() {
  1532. return this.scopeStack[this.scopeStack.length - 1];
  1533. }
  1534. currentVarScopeFlags() {
  1535. for (let i = this.scopeStack.length - 1;; i--) {
  1536. const {
  1537. flags
  1538. } = this.scopeStack[i];
  1539. if (flags & 387) {
  1540. return flags;
  1541. }
  1542. }
  1543. }
  1544. currentThisScopeFlags() {
  1545. for (let i = this.scopeStack.length - 1;; i--) {
  1546. const {
  1547. flags
  1548. } = this.scopeStack[i];
  1549. if (flags & (387 | 64) && !(flags & 4)) {
  1550. return flags;
  1551. }
  1552. }
  1553. }
  1554. }
  1555. class FlowScope extends Scope {
  1556. constructor(...args) {
  1557. super(...args);
  1558. this.declareFunctions = new Set();
  1559. }
  1560. }
  1561. class FlowScopeHandler extends ScopeHandler {
  1562. createScope(flags) {
  1563. return new FlowScope(flags);
  1564. }
  1565. declareName(name, bindingType, loc) {
  1566. const scope = this.currentScope();
  1567. if (bindingType & 2048) {
  1568. this.checkRedeclarationInScope(scope, name, bindingType, loc);
  1569. this.maybeExportDefined(scope, name);
  1570. scope.declareFunctions.add(name);
  1571. return;
  1572. }
  1573. super.declareName(name, bindingType, loc);
  1574. }
  1575. isRedeclaredInScope(scope, name, bindingType) {
  1576. if (super.isRedeclaredInScope(scope, name, bindingType)) return true;
  1577. if (bindingType & 2048 && !scope.declareFunctions.has(name)) {
  1578. const type = scope.names.get(name);
  1579. return (type & 4) > 0 || (type & 2) > 0;
  1580. }
  1581. return false;
  1582. }
  1583. checkLocalExport(id) {
  1584. if (!this.scopeStack[0].declareFunctions.has(id.name)) {
  1585. super.checkLocalExport(id);
  1586. }
  1587. }
  1588. }
  1589. class BaseParser {
  1590. constructor() {
  1591. this.sawUnambiguousESM = false;
  1592. this.ambiguousScriptDifferentAst = false;
  1593. }
  1594. hasPlugin(pluginConfig) {
  1595. if (typeof pluginConfig === "string") {
  1596. return this.plugins.has(pluginConfig);
  1597. } else {
  1598. const [pluginName, pluginOptions] = pluginConfig;
  1599. if (!this.hasPlugin(pluginName)) {
  1600. return false;
  1601. }
  1602. const actualOptions = this.plugins.get(pluginName);
  1603. for (const key of Object.keys(pluginOptions)) {
  1604. if ((actualOptions == null ? void 0 : actualOptions[key]) !== pluginOptions[key]) {
  1605. return false;
  1606. }
  1607. }
  1608. return true;
  1609. }
  1610. }
  1611. getPluginOption(plugin, name) {
  1612. var _this$plugins$get;
  1613. return (_this$plugins$get = this.plugins.get(plugin)) == null ? void 0 : _this$plugins$get[name];
  1614. }
  1615. }
  1616. function setTrailingComments(node, comments) {
  1617. if (node.trailingComments === undefined) {
  1618. node.trailingComments = comments;
  1619. } else {
  1620. node.trailingComments.unshift(...comments);
  1621. }
  1622. }
  1623. function setLeadingComments(node, comments) {
  1624. if (node.leadingComments === undefined) {
  1625. node.leadingComments = comments;
  1626. } else {
  1627. node.leadingComments.unshift(...comments);
  1628. }
  1629. }
  1630. function setInnerComments(node, comments) {
  1631. if (node.innerComments === undefined) {
  1632. node.innerComments = comments;
  1633. } else {
  1634. node.innerComments.unshift(...comments);
  1635. }
  1636. }
  1637. function adjustInnerComments(node, elements, commentWS) {
  1638. let lastElement = null;
  1639. let i = elements.length;
  1640. while (lastElement === null && i > 0) {
  1641. lastElement = elements[--i];
  1642. }
  1643. if (lastElement === null || lastElement.start > commentWS.start) {
  1644. setInnerComments(node, commentWS.comments);
  1645. } else {
  1646. setTrailingComments(lastElement, commentWS.comments);
  1647. }
  1648. }
  1649. class CommentsParser extends BaseParser {
  1650. addComment(comment) {
  1651. if (this.filename) comment.loc.filename = this.filename;
  1652. const {
  1653. commentsLen
  1654. } = this.state;
  1655. if (this.comments.length !== commentsLen) {
  1656. this.comments.length = commentsLen;
  1657. }
  1658. this.comments.push(comment);
  1659. this.state.commentsLen++;
  1660. }
  1661. processComment(node) {
  1662. const {
  1663. commentStack
  1664. } = this.state;
  1665. const commentStackLength = commentStack.length;
  1666. if (commentStackLength === 0) return;
  1667. let i = commentStackLength - 1;
  1668. const lastCommentWS = commentStack[i];
  1669. if (lastCommentWS.start === node.end) {
  1670. lastCommentWS.leadingNode = node;
  1671. i--;
  1672. }
  1673. const {
  1674. start: nodeStart
  1675. } = node;
  1676. for (; i >= 0; i--) {
  1677. const commentWS = commentStack[i];
  1678. const commentEnd = commentWS.end;
  1679. if (commentEnd > nodeStart) {
  1680. commentWS.containingNode = node;
  1681. this.finalizeComment(commentWS);
  1682. commentStack.splice(i, 1);
  1683. } else {
  1684. if (commentEnd === nodeStart) {
  1685. commentWS.trailingNode = node;
  1686. }
  1687. break;
  1688. }
  1689. }
  1690. }
  1691. finalizeComment(commentWS) {
  1692. const {
  1693. comments
  1694. } = commentWS;
  1695. if (commentWS.leadingNode !== null || commentWS.trailingNode !== null) {
  1696. if (commentWS.leadingNode !== null) {
  1697. setTrailingComments(commentWS.leadingNode, comments);
  1698. }
  1699. if (commentWS.trailingNode !== null) {
  1700. setLeadingComments(commentWS.trailingNode, comments);
  1701. }
  1702. } else {
  1703. const {
  1704. containingNode: node,
  1705. start: commentStart
  1706. } = commentWS;
  1707. if (this.input.charCodeAt(commentStart - 1) === 44) {
  1708. switch (node.type) {
  1709. case "ObjectExpression":
  1710. case "ObjectPattern":
  1711. case "RecordExpression":
  1712. adjustInnerComments(node, node.properties, commentWS);
  1713. break;
  1714. case "CallExpression":
  1715. case "OptionalCallExpression":
  1716. adjustInnerComments(node, node.arguments, commentWS);
  1717. break;
  1718. case "FunctionDeclaration":
  1719. case "FunctionExpression":
  1720. case "ArrowFunctionExpression":
  1721. case "ObjectMethod":
  1722. case "ClassMethod":
  1723. case "ClassPrivateMethod":
  1724. adjustInnerComments(node, node.params, commentWS);
  1725. break;
  1726. case "ArrayExpression":
  1727. case "ArrayPattern":
  1728. case "TupleExpression":
  1729. adjustInnerComments(node, node.elements, commentWS);
  1730. break;
  1731. case "ExportNamedDeclaration":
  1732. case "ImportDeclaration":
  1733. adjustInnerComments(node, node.specifiers, commentWS);
  1734. break;
  1735. default:
  1736. {
  1737. setInnerComments(node, comments);
  1738. }
  1739. }
  1740. } else {
  1741. setInnerComments(node, comments);
  1742. }
  1743. }
  1744. }
  1745. finalizeRemainingComments() {
  1746. const {
  1747. commentStack
  1748. } = this.state;
  1749. for (let i = commentStack.length - 1; i >= 0; i--) {
  1750. this.finalizeComment(commentStack[i]);
  1751. }
  1752. this.state.commentStack = [];
  1753. }
  1754. resetPreviousNodeTrailingComments(node) {
  1755. const {
  1756. commentStack
  1757. } = this.state;
  1758. const {
  1759. length
  1760. } = commentStack;
  1761. if (length === 0) return;
  1762. const commentWS = commentStack[length - 1];
  1763. if (commentWS.leadingNode === node) {
  1764. commentWS.leadingNode = null;
  1765. }
  1766. }
  1767. resetPreviousIdentifierLeadingComments(node) {
  1768. const {
  1769. commentStack
  1770. } = this.state;
  1771. const {
  1772. length
  1773. } = commentStack;
  1774. if (length === 0) return;
  1775. if (commentStack[length - 1].trailingNode === node) {
  1776. commentStack[length - 1].trailingNode = null;
  1777. } else if (length >= 2 && commentStack[length - 2].trailingNode === node) {
  1778. commentStack[length - 2].trailingNode = null;
  1779. }
  1780. }
  1781. takeSurroundingComments(node, start, end) {
  1782. const {
  1783. commentStack
  1784. } = this.state;
  1785. const commentStackLength = commentStack.length;
  1786. if (commentStackLength === 0) return;
  1787. let i = commentStackLength - 1;
  1788. for (; i >= 0; i--) {
  1789. const commentWS = commentStack[i];
  1790. const commentEnd = commentWS.end;
  1791. const commentStart = commentWS.start;
  1792. if (commentStart === end) {
  1793. commentWS.leadingNode = node;
  1794. } else if (commentEnd === start) {
  1795. commentWS.trailingNode = node;
  1796. } else if (commentEnd < start) {
  1797. break;
  1798. }
  1799. }
  1800. }
  1801. }
  1802. const lineBreak = /\r\n|[\r\n\u2028\u2029]/;
  1803. const lineBreakG = new RegExp(lineBreak.source, "g");
  1804. function isNewLine(code) {
  1805. switch (code) {
  1806. case 10:
  1807. case 13:
  1808. case 8232:
  1809. case 8233:
  1810. return true;
  1811. default:
  1812. return false;
  1813. }
  1814. }
  1815. function hasNewLine(input, start, end) {
  1816. for (let i = start; i < end; i++) {
  1817. if (isNewLine(input.charCodeAt(i))) {
  1818. return true;
  1819. }
  1820. }
  1821. return false;
  1822. }
  1823. const skipWhiteSpace = /(?:\s|\/\/.*|\/\*[^]*?\*\/)*/g;
  1824. const skipWhiteSpaceInLine = /(?:[^\S\n\r\u2028\u2029]|\/\/.*|\/\*.*?\*\/)*/g;
  1825. function isWhitespace(code) {
  1826. switch (code) {
  1827. case 0x0009:
  1828. case 0x000b:
  1829. case 0x000c:
  1830. case 32:
  1831. case 160:
  1832. case 5760:
  1833. case 0x2000:
  1834. case 0x2001:
  1835. case 0x2002:
  1836. case 0x2003:
  1837. case 0x2004:
  1838. case 0x2005:
  1839. case 0x2006:
  1840. case 0x2007:
  1841. case 0x2008:
  1842. case 0x2009:
  1843. case 0x200a:
  1844. case 0x202f:
  1845. case 0x205f:
  1846. case 0x3000:
  1847. case 0xfeff:
  1848. return true;
  1849. default:
  1850. return false;
  1851. }
  1852. }
  1853. class State {
  1854. constructor() {
  1855. this.flags = 1024;
  1856. this.curLine = void 0;
  1857. this.lineStart = void 0;
  1858. this.startLoc = void 0;
  1859. this.endLoc = void 0;
  1860. this.errors = [];
  1861. this.potentialArrowAt = -1;
  1862. this.noArrowAt = [];
  1863. this.noArrowParamsConversionAt = [];
  1864. this.topicContext = {
  1865. maxNumOfResolvableTopics: 0,
  1866. maxTopicIndex: null
  1867. };
  1868. this.labels = [];
  1869. this.commentsLen = 0;
  1870. this.commentStack = [];
  1871. this.pos = 0;
  1872. this.type = 139;
  1873. this.value = null;
  1874. this.start = 0;
  1875. this.end = 0;
  1876. this.lastTokEndLoc = null;
  1877. this.lastTokStartLoc = null;
  1878. this.context = [types.brace];
  1879. this.firstInvalidTemplateEscapePos = null;
  1880. this.strictErrors = new Map();
  1881. this.tokensLength = 0;
  1882. }
  1883. get strict() {
  1884. return (this.flags & 1) > 0;
  1885. }
  1886. set strict(v) {
  1887. if (v) this.flags |= 1;else this.flags &= -2;
  1888. }
  1889. init({
  1890. strictMode,
  1891. sourceType,
  1892. startLine,
  1893. startColumn
  1894. }) {
  1895. this.strict = strictMode === false ? false : strictMode === true ? true : sourceType === "module";
  1896. this.curLine = startLine;
  1897. this.lineStart = -startColumn;
  1898. this.startLoc = this.endLoc = new Position(startLine, startColumn, 0);
  1899. }
  1900. get maybeInArrowParameters() {
  1901. return (this.flags & 2) > 0;
  1902. }
  1903. set maybeInArrowParameters(v) {
  1904. if (v) this.flags |= 2;else this.flags &= -3;
  1905. }
  1906. get inType() {
  1907. return (this.flags & 4) > 0;
  1908. }
  1909. set inType(v) {
  1910. if (v) this.flags |= 4;else this.flags &= -5;
  1911. }
  1912. get noAnonFunctionType() {
  1913. return (this.flags & 8) > 0;
  1914. }
  1915. set noAnonFunctionType(v) {
  1916. if (v) this.flags |= 8;else this.flags &= -9;
  1917. }
  1918. get hasFlowComment() {
  1919. return (this.flags & 16) > 0;
  1920. }
  1921. set hasFlowComment(v) {
  1922. if (v) this.flags |= 16;else this.flags &= -17;
  1923. }
  1924. get isAmbientContext() {
  1925. return (this.flags & 32) > 0;
  1926. }
  1927. set isAmbientContext(v) {
  1928. if (v) this.flags |= 32;else this.flags &= -33;
  1929. }
  1930. get inAbstractClass() {
  1931. return (this.flags & 64) > 0;
  1932. }
  1933. set inAbstractClass(v) {
  1934. if (v) this.flags |= 64;else this.flags &= -65;
  1935. }
  1936. get inDisallowConditionalTypesContext() {
  1937. return (this.flags & 128) > 0;
  1938. }
  1939. set inDisallowConditionalTypesContext(v) {
  1940. if (v) this.flags |= 128;else this.flags &= -129;
  1941. }
  1942. get soloAwait() {
  1943. return (this.flags & 256) > 0;
  1944. }
  1945. set soloAwait(v) {
  1946. if (v) this.flags |= 256;else this.flags &= -257;
  1947. }
  1948. get inFSharpPipelineDirectBody() {
  1949. return (this.flags & 512) > 0;
  1950. }
  1951. set inFSharpPipelineDirectBody(v) {
  1952. if (v) this.flags |= 512;else this.flags &= -513;
  1953. }
  1954. get canStartJSXElement() {
  1955. return (this.flags & 1024) > 0;
  1956. }
  1957. set canStartJSXElement(v) {
  1958. if (v) this.flags |= 1024;else this.flags &= -1025;
  1959. }
  1960. get containsEsc() {
  1961. return (this.flags & 2048) > 0;
  1962. }
  1963. set containsEsc(v) {
  1964. if (v) this.flags |= 2048;else this.flags &= -2049;
  1965. }
  1966. get hasTopLevelAwait() {
  1967. return (this.flags & 4096) > 0;
  1968. }
  1969. set hasTopLevelAwait(v) {
  1970. if (v) this.flags |= 4096;else this.flags &= -4097;
  1971. }
  1972. curPosition() {
  1973. return new Position(this.curLine, this.pos - this.lineStart, this.pos);
  1974. }
  1975. clone() {
  1976. const state = new State();
  1977. state.flags = this.flags;
  1978. state.curLine = this.curLine;
  1979. state.lineStart = this.lineStart;
  1980. state.startLoc = this.startLoc;
  1981. state.endLoc = this.endLoc;
  1982. state.errors = this.errors.slice();
  1983. state.potentialArrowAt = this.potentialArrowAt;
  1984. state.noArrowAt = this.noArrowAt.slice();
  1985. state.noArrowParamsConversionAt = this.noArrowParamsConversionAt.slice();
  1986. state.topicContext = this.topicContext;
  1987. state.labels = this.labels.slice();
  1988. state.commentsLen = this.commentsLen;
  1989. state.commentStack = this.commentStack.slice();
  1990. state.pos = this.pos;
  1991. state.type = this.type;
  1992. state.value = this.value;
  1993. state.start = this.start;
  1994. state.end = this.end;
  1995. state.lastTokEndLoc = this.lastTokEndLoc;
  1996. state.lastTokStartLoc = this.lastTokStartLoc;
  1997. state.context = this.context.slice();
  1998. state.firstInvalidTemplateEscapePos = this.firstInvalidTemplateEscapePos;
  1999. state.strictErrors = this.strictErrors;
  2000. state.tokensLength = this.tokensLength;
  2001. return state;
  2002. }
  2003. }
  2004. var _isDigit = function isDigit(code) {
  2005. return code >= 48 && code <= 57;
  2006. };
  2007. const forbiddenNumericSeparatorSiblings = {
  2008. decBinOct: new Set([46, 66, 69, 79, 95, 98, 101, 111]),
  2009. hex: new Set([46, 88, 95, 120])
  2010. };
  2011. const isAllowedNumericSeparatorSibling = {
  2012. bin: ch => ch === 48 || ch === 49,
  2013. oct: ch => ch >= 48 && ch <= 55,
  2014. dec: ch => ch >= 48 && ch <= 57,
  2015. hex: ch => ch >= 48 && ch <= 57 || ch >= 65 && ch <= 70 || ch >= 97 && ch <= 102
  2016. };
  2017. function readStringContents(type, input, pos, lineStart, curLine, errors) {
  2018. const initialPos = pos;
  2019. const initialLineStart = lineStart;
  2020. const initialCurLine = curLine;
  2021. let out = "";
  2022. let firstInvalidLoc = null;
  2023. let chunkStart = pos;
  2024. const {
  2025. length
  2026. } = input;
  2027. for (;;) {
  2028. if (pos >= length) {
  2029. errors.unterminated(initialPos, initialLineStart, initialCurLine);
  2030. out += input.slice(chunkStart, pos);
  2031. break;
  2032. }
  2033. const ch = input.charCodeAt(pos);
  2034. if (isStringEnd(type, ch, input, pos)) {
  2035. out += input.slice(chunkStart, pos);
  2036. break;
  2037. }
  2038. if (ch === 92) {
  2039. out += input.slice(chunkStart, pos);
  2040. const res = readEscapedChar(input, pos, lineStart, curLine, type === "template", errors);
  2041. if (res.ch === null && !firstInvalidLoc) {
  2042. firstInvalidLoc = {
  2043. pos,
  2044. lineStart,
  2045. curLine
  2046. };
  2047. } else {
  2048. out += res.ch;
  2049. }
  2050. ({
  2051. pos,
  2052. lineStart,
  2053. curLine
  2054. } = res);
  2055. chunkStart = pos;
  2056. } else if (ch === 8232 || ch === 8233) {
  2057. ++pos;
  2058. ++curLine;
  2059. lineStart = pos;
  2060. } else if (ch === 10 || ch === 13) {
  2061. if (type === "template") {
  2062. out += input.slice(chunkStart, pos) + "\n";
  2063. ++pos;
  2064. if (ch === 13 && input.charCodeAt(pos) === 10) {
  2065. ++pos;
  2066. }
  2067. ++curLine;
  2068. chunkStart = lineStart = pos;
  2069. } else {
  2070. errors.unterminated(initialPos, initialLineStart, initialCurLine);
  2071. }
  2072. } else {
  2073. ++pos;
  2074. }
  2075. }
  2076. return {
  2077. pos,
  2078. str: out,
  2079. firstInvalidLoc,
  2080. lineStart,
  2081. curLine,
  2082. containsInvalid: !!firstInvalidLoc
  2083. };
  2084. }
  2085. function isStringEnd(type, ch, input, pos) {
  2086. if (type === "template") {
  2087. return ch === 96 || ch === 36 && input.charCodeAt(pos + 1) === 123;
  2088. }
  2089. return ch === (type === "double" ? 34 : 39);
  2090. }
  2091. function readEscapedChar(input, pos, lineStart, curLine, inTemplate, errors) {
  2092. const throwOnInvalid = !inTemplate;
  2093. pos++;
  2094. const res = ch => ({
  2095. pos,
  2096. ch,
  2097. lineStart,
  2098. curLine
  2099. });
  2100. const ch = input.charCodeAt(pos++);
  2101. switch (ch) {
  2102. case 110:
  2103. return res("\n");
  2104. case 114:
  2105. return res("\r");
  2106. case 120:
  2107. {
  2108. let code;
  2109. ({
  2110. code,
  2111. pos
  2112. } = readHexChar(input, pos, lineStart, curLine, 2, false, throwOnInvalid, errors));
  2113. return res(code === null ? null : String.fromCharCode(code));
  2114. }
  2115. case 117:
  2116. {
  2117. let code;
  2118. ({
  2119. code,
  2120. pos
  2121. } = readCodePoint(input, pos, lineStart, curLine, throwOnInvalid, errors));
  2122. return res(code === null ? null : String.fromCodePoint(code));
  2123. }
  2124. case 116:
  2125. return res("\t");
  2126. case 98:
  2127. return res("\b");
  2128. case 118:
  2129. return res("\u000b");
  2130. case 102:
  2131. return res("\f");
  2132. case 13:
  2133. if (input.charCodeAt(pos) === 10) {
  2134. ++pos;
  2135. }
  2136. case 10:
  2137. lineStart = pos;
  2138. ++curLine;
  2139. case 8232:
  2140. case 8233:
  2141. return res("");
  2142. case 56:
  2143. case 57:
  2144. if (inTemplate) {
  2145. return res(null);
  2146. } else {
  2147. errors.strictNumericEscape(pos - 1, lineStart, curLine);
  2148. }
  2149. default:
  2150. if (ch >= 48 && ch <= 55) {
  2151. const startPos = pos - 1;
  2152. const match = /^[0-7]+/.exec(input.slice(startPos, pos + 2));
  2153. let octalStr = match[0];
  2154. let octal = parseInt(octalStr, 8);
  2155. if (octal > 255) {
  2156. octalStr = octalStr.slice(0, -1);
  2157. octal = parseInt(octalStr, 8);
  2158. }
  2159. pos += octalStr.length - 1;
  2160. const next = input.charCodeAt(pos);
  2161. if (octalStr !== "0" || next === 56 || next === 57) {
  2162. if (inTemplate) {
  2163. return res(null);
  2164. } else {
  2165. errors.strictNumericEscape(startPos, lineStart, curLine);
  2166. }
  2167. }
  2168. return res(String.fromCharCode(octal));
  2169. }
  2170. return res(String.fromCharCode(ch));
  2171. }
  2172. }
  2173. function readHexChar(input, pos, lineStart, curLine, len, forceLen, throwOnInvalid, errors) {
  2174. const initialPos = pos;
  2175. let n;
  2176. ({
  2177. n,
  2178. pos
  2179. } = readInt(input, pos, lineStart, curLine, 16, len, forceLen, false, errors, !throwOnInvalid));
  2180. if (n === null) {
  2181. if (throwOnInvalid) {
  2182. errors.invalidEscapeSequence(initialPos, lineStart, curLine);
  2183. } else {
  2184. pos = initialPos - 1;
  2185. }
  2186. }
  2187. return {
  2188. code: n,
  2189. pos
  2190. };
  2191. }
  2192. function readInt(input, pos, lineStart, curLine, radix, len, forceLen, allowNumSeparator, errors, bailOnError) {
  2193. const start = pos;
  2194. const forbiddenSiblings = radix === 16 ? forbiddenNumericSeparatorSiblings.hex : forbiddenNumericSeparatorSiblings.decBinOct;
  2195. const isAllowedSibling = radix === 16 ? isAllowedNumericSeparatorSibling.hex : radix === 10 ? isAllowedNumericSeparatorSibling.dec : radix === 8 ? isAllowedNumericSeparatorSibling.oct : isAllowedNumericSeparatorSibling.bin;
  2196. let invalid = false;
  2197. let total = 0;
  2198. for (let i = 0, e = len == null ? Infinity : len; i < e; ++i) {
  2199. const code = input.charCodeAt(pos);
  2200. let val;
  2201. if (code === 95 && allowNumSeparator !== "bail") {
  2202. const prev = input.charCodeAt(pos - 1);
  2203. const next = input.charCodeAt(pos + 1);
  2204. if (!allowNumSeparator) {
  2205. if (bailOnError) return {
  2206. n: null,
  2207. pos
  2208. };
  2209. errors.numericSeparatorInEscapeSequence(pos, lineStart, curLine);
  2210. } else if (Number.isNaN(next) || !isAllowedSibling(next) || forbiddenSiblings.has(prev) || forbiddenSiblings.has(next)) {
  2211. if (bailOnError) return {
  2212. n: null,
  2213. pos
  2214. };
  2215. errors.unexpectedNumericSeparator(pos, lineStart, curLine);
  2216. }
  2217. ++pos;
  2218. continue;
  2219. }
  2220. if (code >= 97) {
  2221. val = code - 97 + 10;
  2222. } else if (code >= 65) {
  2223. val = code - 65 + 10;
  2224. } else if (_isDigit(code)) {
  2225. val = code - 48;
  2226. } else {
  2227. val = Infinity;
  2228. }
  2229. if (val >= radix) {
  2230. if (val <= 9 && bailOnError) {
  2231. return {
  2232. n: null,
  2233. pos
  2234. };
  2235. } else if (val <= 9 && errors.invalidDigit(pos, lineStart, curLine, radix)) {
  2236. val = 0;
  2237. } else if (forceLen) {
  2238. val = 0;
  2239. invalid = true;
  2240. } else {
  2241. break;
  2242. }
  2243. }
  2244. ++pos;
  2245. total = total * radix + val;
  2246. }
  2247. if (pos === start || len != null && pos - start !== len || invalid) {
  2248. return {
  2249. n: null,
  2250. pos
  2251. };
  2252. }
  2253. return {
  2254. n: total,
  2255. pos
  2256. };
  2257. }
  2258. function readCodePoint(input, pos, lineStart, curLine, throwOnInvalid, errors) {
  2259. const ch = input.charCodeAt(pos);
  2260. let code;
  2261. if (ch === 123) {
  2262. ++pos;
  2263. ({
  2264. code,
  2265. pos
  2266. } = readHexChar(input, pos, lineStart, curLine, input.indexOf("}", pos) - pos, true, throwOnInvalid, errors));
  2267. ++pos;
  2268. if (code !== null && code > 0x10ffff) {
  2269. if (throwOnInvalid) {
  2270. errors.invalidCodePoint(pos, lineStart, curLine);
  2271. } else {
  2272. return {
  2273. code: null,
  2274. pos
  2275. };
  2276. }
  2277. }
  2278. } else {
  2279. ({
  2280. code,
  2281. pos
  2282. } = readHexChar(input, pos, lineStart, curLine, 4, false, throwOnInvalid, errors));
  2283. }
  2284. return {
  2285. code,
  2286. pos
  2287. };
  2288. }
  2289. function buildPosition(pos, lineStart, curLine) {
  2290. return new Position(curLine, pos - lineStart, pos);
  2291. }
  2292. const VALID_REGEX_FLAGS = new Set([103, 109, 115, 105, 121, 117, 100, 118]);
  2293. class Token {
  2294. constructor(state) {
  2295. this.type = state.type;
  2296. this.value = state.value;
  2297. this.start = state.start;
  2298. this.end = state.end;
  2299. this.loc = new SourceLocation(state.startLoc, state.endLoc);
  2300. }
  2301. }
  2302. class Tokenizer extends CommentsParser {
  2303. constructor(options, input) {
  2304. super();
  2305. this.isLookahead = void 0;
  2306. this.tokens = [];
  2307. this.errorHandlers_readInt = {
  2308. invalidDigit: (pos, lineStart, curLine, radix) => {
  2309. if (!this.options.errorRecovery) return false;
  2310. this.raise(Errors.InvalidDigit, buildPosition(pos, lineStart, curLine), {
  2311. radix
  2312. });
  2313. return true;
  2314. },
  2315. numericSeparatorInEscapeSequence: this.errorBuilder(Errors.NumericSeparatorInEscapeSequence),
  2316. unexpectedNumericSeparator: this.errorBuilder(Errors.UnexpectedNumericSeparator)
  2317. };
  2318. this.errorHandlers_readCodePoint = Object.assign({}, this.errorHandlers_readInt, {
  2319. invalidEscapeSequence: this.errorBuilder(Errors.InvalidEscapeSequence),
  2320. invalidCodePoint: this.errorBuilder(Errors.InvalidCodePoint)
  2321. });
  2322. this.errorHandlers_readStringContents_string = Object.assign({}, this.errorHandlers_readCodePoint, {
  2323. strictNumericEscape: (pos, lineStart, curLine) => {
  2324. this.recordStrictModeErrors(Errors.StrictNumericEscape, buildPosition(pos, lineStart, curLine));
  2325. },
  2326. unterminated: (pos, lineStart, curLine) => {
  2327. throw this.raise(Errors.UnterminatedString, buildPosition(pos - 1, lineStart, curLine));
  2328. }
  2329. });
  2330. this.errorHandlers_readStringContents_template = Object.assign({}, this.errorHandlers_readCodePoint, {
  2331. strictNumericEscape: this.errorBuilder(Errors.StrictNumericEscape),
  2332. unterminated: (pos, lineStart, curLine) => {
  2333. throw this.raise(Errors.UnterminatedTemplate, buildPosition(pos, lineStart, curLine));
  2334. }
  2335. });
  2336. this.state = new State();
  2337. this.state.init(options);
  2338. this.input = input;
  2339. this.length = input.length;
  2340. this.comments = [];
  2341. this.isLookahead = false;
  2342. }
  2343. pushToken(token) {
  2344. this.tokens.length = this.state.tokensLength;
  2345. this.tokens.push(token);
  2346. ++this.state.tokensLength;
  2347. }
  2348. next() {
  2349. this.checkKeywordEscapes();
  2350. if (this.options.tokens) {
  2351. this.pushToken(new Token(this.state));
  2352. }
  2353. this.state.lastTokEndLoc = this.state.endLoc;
  2354. this.state.lastTokStartLoc = this.state.startLoc;
  2355. this.nextToken();
  2356. }
  2357. eat(type) {
  2358. if (this.match(type)) {
  2359. this.next();
  2360. return true;
  2361. } else {
  2362. return false;
  2363. }
  2364. }
  2365. match(type) {
  2366. return this.state.type === type;
  2367. }
  2368. createLookaheadState(state) {
  2369. return {
  2370. pos: state.pos,
  2371. value: null,
  2372. type: state.type,
  2373. start: state.start,
  2374. end: state.end,
  2375. context: [this.curContext()],
  2376. inType: state.inType,
  2377. startLoc: state.startLoc,
  2378. lastTokEndLoc: state.lastTokEndLoc,
  2379. curLine: state.curLine,
  2380. lineStart: state.lineStart,
  2381. curPosition: state.curPosition
  2382. };
  2383. }
  2384. lookahead() {
  2385. const old = this.state;
  2386. this.state = this.createLookaheadState(old);
  2387. this.isLookahead = true;
  2388. this.nextToken();
  2389. this.isLookahead = false;
  2390. const curr = this.state;
  2391. this.state = old;
  2392. return curr;
  2393. }
  2394. nextTokenStart() {
  2395. return this.nextTokenStartSince(this.state.pos);
  2396. }
  2397. nextTokenStartSince(pos) {
  2398. skipWhiteSpace.lastIndex = pos;
  2399. return skipWhiteSpace.test(this.input) ? skipWhiteSpace.lastIndex : pos;
  2400. }
  2401. lookaheadCharCode() {
  2402. return this.input.charCodeAt(this.nextTokenStart());
  2403. }
  2404. nextTokenInLineStart() {
  2405. return this.nextTokenInLineStartSince(this.state.pos);
  2406. }
  2407. nextTokenInLineStartSince(pos) {
  2408. skipWhiteSpaceInLine.lastIndex = pos;
  2409. return skipWhiteSpaceInLine.test(this.input) ? skipWhiteSpaceInLine.lastIndex : pos;
  2410. }
  2411. lookaheadInLineCharCode() {
  2412. return this.input.charCodeAt(this.nextTokenInLineStart());
  2413. }
  2414. codePointAtPos(pos) {
  2415. let cp = this.input.charCodeAt(pos);
  2416. if ((cp & 0xfc00) === 0xd800 && ++pos < this.input.length) {
  2417. const trail = this.input.charCodeAt(pos);
  2418. if ((trail & 0xfc00) === 0xdc00) {
  2419. cp = 0x10000 + ((cp & 0x3ff) << 10) + (trail & 0x3ff);
  2420. }
  2421. }
  2422. return cp;
  2423. }
  2424. setStrict(strict) {
  2425. this.state.strict = strict;
  2426. if (strict) {
  2427. this.state.strictErrors.forEach(([toParseError, at]) => this.raise(toParseError, at));
  2428. this.state.strictErrors.clear();
  2429. }
  2430. }
  2431. curContext() {
  2432. return this.state.context[this.state.context.length - 1];
  2433. }
  2434. nextToken() {
  2435. this.skipSpace();
  2436. this.state.start = this.state.pos;
  2437. if (!this.isLookahead) this.state.startLoc = this.state.curPosition();
  2438. if (this.state.pos >= this.length) {
  2439. this.finishToken(139);
  2440. return;
  2441. }
  2442. this.getTokenFromCode(this.codePointAtPos(this.state.pos));
  2443. }
  2444. skipBlockComment(commentEnd) {
  2445. let startLoc;
  2446. if (!this.isLookahead) startLoc = this.state.curPosition();
  2447. const start = this.state.pos;
  2448. const end = this.input.indexOf(commentEnd, start + 2);
  2449. if (end === -1) {
  2450. throw this.raise(Errors.UnterminatedComment, this.state.curPosition());
  2451. }
  2452. this.state.pos = end + commentEnd.length;
  2453. lineBreakG.lastIndex = start + 2;
  2454. while (lineBreakG.test(this.input) && lineBreakG.lastIndex <= end) {
  2455. ++this.state.curLine;
  2456. this.state.lineStart = lineBreakG.lastIndex;
  2457. }
  2458. if (this.isLookahead) return;
  2459. const comment = {
  2460. type: "CommentBlock",
  2461. value: this.input.slice(start + 2, end),
  2462. start,
  2463. end: end + commentEnd.length,
  2464. loc: new SourceLocation(startLoc, this.state.curPosition())
  2465. };
  2466. if (this.options.tokens) this.pushToken(comment);
  2467. return comment;
  2468. }
  2469. skipLineComment(startSkip) {
  2470. const start = this.state.pos;
  2471. let startLoc;
  2472. if (!this.isLookahead) startLoc = this.state.curPosition();
  2473. let ch = this.input.charCodeAt(this.state.pos += startSkip);
  2474. if (this.state.pos < this.length) {
  2475. while (!isNewLine(ch) && ++this.state.pos < this.length) {
  2476. ch = this.input.charCodeAt(this.state.pos);
  2477. }
  2478. }
  2479. if (this.isLookahead) return;
  2480. const end = this.state.pos;
  2481. const value = this.input.slice(start + startSkip, end);
  2482. const comment = {
  2483. type: "CommentLine",
  2484. value,
  2485. start,
  2486. end,
  2487. loc: new SourceLocation(startLoc, this.state.curPosition())
  2488. };
  2489. if (this.options.tokens) this.pushToken(comment);
  2490. return comment;
  2491. }
  2492. skipSpace() {
  2493. const spaceStart = this.state.pos;
  2494. const comments = [];
  2495. loop: while (this.state.pos < this.length) {
  2496. const ch = this.input.charCodeAt(this.state.pos);
  2497. switch (ch) {
  2498. case 32:
  2499. case 160:
  2500. case 9:
  2501. ++this.state.pos;
  2502. break;
  2503. case 13:
  2504. if (this.input.charCodeAt(this.state.pos + 1) === 10) {
  2505. ++this.state.pos;
  2506. }
  2507. case 10:
  2508. case 8232:
  2509. case 8233:
  2510. ++this.state.pos;
  2511. ++this.state.curLine;
  2512. this.state.lineStart = this.state.pos;
  2513. break;
  2514. case 47:
  2515. switch (this.input.charCodeAt(this.state.pos + 1)) {
  2516. case 42:
  2517. {
  2518. const comment = this.skipBlockComment("*/");
  2519. if (comment !== undefined) {
  2520. this.addComment(comment);
  2521. if (this.options.attachComment) comments.push(comment);
  2522. }
  2523. break;
  2524. }
  2525. case 47:
  2526. {
  2527. const comment = this.skipLineComment(2);
  2528. if (comment !== undefined) {
  2529. this.addComment(comment);
  2530. if (this.options.attachComment) comments.push(comment);
  2531. }
  2532. break;
  2533. }
  2534. default:
  2535. break loop;
  2536. }
  2537. break;
  2538. default:
  2539. if (isWhitespace(ch)) {
  2540. ++this.state.pos;
  2541. } else if (ch === 45 && !this.inModule && this.options.annexB) {
  2542. const pos = this.state.pos;
  2543. if (this.input.charCodeAt(pos + 1) === 45 && this.input.charCodeAt(pos + 2) === 62 && (spaceStart === 0 || this.state.lineStart > spaceStart)) {
  2544. const comment = this.skipLineComment(3);
  2545. if (comment !== undefined) {
  2546. this.addComment(comment);
  2547. if (this.options.attachComment) comments.push(comment);
  2548. }
  2549. } else {
  2550. break loop;
  2551. }
  2552. } else if (ch === 60 && !this.inModule && this.options.annexB) {
  2553. const pos = this.state.pos;
  2554. if (this.input.charCodeAt(pos + 1) === 33 && this.input.charCodeAt(pos + 2) === 45 && this.input.charCodeAt(pos + 3) === 45) {
  2555. const comment = this.skipLineComment(4);
  2556. if (comment !== undefined) {
  2557. this.addComment(comment);
  2558. if (this.options.attachComment) comments.push(comment);
  2559. }
  2560. } else {
  2561. break loop;
  2562. }
  2563. } else {
  2564. break loop;
  2565. }
  2566. }
  2567. }
  2568. if (comments.length > 0) {
  2569. const end = this.state.pos;
  2570. const commentWhitespace = {
  2571. start: spaceStart,
  2572. end,
  2573. comments,
  2574. leadingNode: null,
  2575. trailingNode: null,
  2576. containingNode: null
  2577. };
  2578. this.state.commentStack.push(commentWhitespace);
  2579. }
  2580. }
  2581. finishToken(type, val) {
  2582. this.state.end = this.state.pos;
  2583. this.state.endLoc = this.state.curPosition();
  2584. const prevType = this.state.type;
  2585. this.state.type = type;
  2586. this.state.value = val;
  2587. if (!this.isLookahead) {
  2588. this.updateContext(prevType);
  2589. }
  2590. }
  2591. replaceToken(type) {
  2592. this.state.type = type;
  2593. this.updateContext();
  2594. }
  2595. readToken_numberSign() {
  2596. if (this.state.pos === 0 && this.readToken_interpreter()) {
  2597. return;
  2598. }
  2599. const nextPos = this.state.pos + 1;
  2600. const next = this.codePointAtPos(nextPos);
  2601. if (next >= 48 && next <= 57) {
  2602. throw this.raise(Errors.UnexpectedDigitAfterHash, this.state.curPosition());
  2603. }
  2604. if (next === 123 || next === 91 && this.hasPlugin("recordAndTuple")) {
  2605. this.expectPlugin("recordAndTuple");
  2606. if (this.getPluginOption("recordAndTuple", "syntaxType") === "bar") {
  2607. throw this.raise(next === 123 ? Errors.RecordExpressionHashIncorrectStartSyntaxType : Errors.TupleExpressionHashIncorrectStartSyntaxType, this.state.curPosition());
  2608. }
  2609. this.state.pos += 2;
  2610. if (next === 123) {
  2611. this.finishToken(7);
  2612. } else {
  2613. this.finishToken(1);
  2614. }
  2615. } else if (isIdentifierStart(next)) {
  2616. ++this.state.pos;
  2617. this.finishToken(138, this.readWord1(next));
  2618. } else if (next === 92) {
  2619. ++this.state.pos;
  2620. this.finishToken(138, this.readWord1());
  2621. } else {
  2622. this.finishOp(27, 1);
  2623. }
  2624. }
  2625. readToken_dot() {
  2626. const next = this.input.charCodeAt(this.state.pos + 1);
  2627. if (next >= 48 && next <= 57) {
  2628. this.readNumber(true);
  2629. return;
  2630. }
  2631. if (next === 46 && this.input.charCodeAt(this.state.pos + 2) === 46) {
  2632. this.state.pos += 3;
  2633. this.finishToken(21);
  2634. } else {
  2635. ++this.state.pos;
  2636. this.finishToken(16);
  2637. }
  2638. }
  2639. readToken_slash() {
  2640. const next = this.input.charCodeAt(this.state.pos + 1);
  2641. if (next === 61) {
  2642. this.finishOp(31, 2);
  2643. } else {
  2644. this.finishOp(56, 1);
  2645. }
  2646. }
  2647. readToken_interpreter() {
  2648. if (this.state.pos !== 0 || this.length < 2) return false;
  2649. let ch = this.input.charCodeAt(this.state.pos + 1);
  2650. if (ch !== 33) return false;
  2651. const start = this.state.pos;
  2652. this.state.pos += 1;
  2653. while (!isNewLine(ch) && ++this.state.pos < this.length) {
  2654. ch = this.input.charCodeAt(this.state.pos);
  2655. }
  2656. const value = this.input.slice(start + 2, this.state.pos);
  2657. this.finishToken(28, value);
  2658. return true;
  2659. }
  2660. readToken_mult_modulo(code) {
  2661. let type = code === 42 ? 55 : 54;
  2662. let width = 1;
  2663. let next = this.input.charCodeAt(this.state.pos + 1);
  2664. if (code === 42 && next === 42) {
  2665. width++;
  2666. next = this.input.charCodeAt(this.state.pos + 2);
  2667. type = 57;
  2668. }
  2669. if (next === 61 && !this.state.inType) {
  2670. width++;
  2671. type = code === 37 ? 33 : 30;
  2672. }
  2673. this.finishOp(type, width);
  2674. }
  2675. readToken_pipe_amp(code) {
  2676. const next = this.input.charCodeAt(this.state.pos + 1);
  2677. if (next === code) {
  2678. if (this.input.charCodeAt(this.state.pos + 2) === 61) {
  2679. this.finishOp(30, 3);
  2680. } else {
  2681. this.finishOp(code === 124 ? 41 : 42, 2);
  2682. }
  2683. return;
  2684. }
  2685. if (code === 124) {
  2686. if (next === 62) {
  2687. this.finishOp(39, 2);
  2688. return;
  2689. }
  2690. if (this.hasPlugin("recordAndTuple") && next === 125) {
  2691. if (this.getPluginOption("recordAndTuple", "syntaxType") !== "bar") {
  2692. throw this.raise(Errors.RecordExpressionBarIncorrectEndSyntaxType, this.state.curPosition());
  2693. }
  2694. this.state.pos += 2;
  2695. this.finishToken(9);
  2696. return;
  2697. }
  2698. if (this.hasPlugin("recordAndTuple") && next === 93) {
  2699. if (this.getPluginOption("recordAndTuple", "syntaxType") !== "bar") {
  2700. throw this.raise(Errors.TupleExpressionBarIncorrectEndSyntaxType, this.state.curPosition());
  2701. }
  2702. this.state.pos += 2;
  2703. this.finishToken(4);
  2704. return;
  2705. }
  2706. }
  2707. if (next === 61) {
  2708. this.finishOp(30, 2);
  2709. return;
  2710. }
  2711. this.finishOp(code === 124 ? 43 : 45, 1);
  2712. }
  2713. readToken_caret() {
  2714. const next = this.input.charCodeAt(this.state.pos + 1);
  2715. if (next === 61 && !this.state.inType) {
  2716. this.finishOp(32, 2);
  2717. } else if (next === 94 && this.hasPlugin(["pipelineOperator", {
  2718. proposal: "hack",
  2719. topicToken: "^^"
  2720. }])) {
  2721. this.finishOp(37, 2);
  2722. const lookaheadCh = this.input.codePointAt(this.state.pos);
  2723. if (lookaheadCh === 94) {
  2724. this.unexpected();
  2725. }
  2726. } else {
  2727. this.finishOp(44, 1);
  2728. }
  2729. }
  2730. readToken_atSign() {
  2731. const next = this.input.charCodeAt(this.state.pos + 1);
  2732. if (next === 64 && this.hasPlugin(["pipelineOperator", {
  2733. proposal: "hack",
  2734. topicToken: "@@"
  2735. }])) {
  2736. this.finishOp(38, 2);
  2737. } else {
  2738. this.finishOp(26, 1);
  2739. }
  2740. }
  2741. readToken_plus_min(code) {
  2742. const next = this.input.charCodeAt(this.state.pos + 1);
  2743. if (next === code) {
  2744. this.finishOp(34, 2);
  2745. return;
  2746. }
  2747. if (next === 61) {
  2748. this.finishOp(30, 2);
  2749. } else {
  2750. this.finishOp(53, 1);
  2751. }
  2752. }
  2753. readToken_lt() {
  2754. const {
  2755. pos
  2756. } = this.state;
  2757. const next = this.input.charCodeAt(pos + 1);
  2758. if (next === 60) {
  2759. if (this.input.charCodeAt(pos + 2) === 61) {
  2760. this.finishOp(30, 3);
  2761. return;
  2762. }
  2763. this.finishOp(51, 2);
  2764. return;
  2765. }
  2766. if (next === 61) {
  2767. this.finishOp(49, 2);
  2768. return;
  2769. }
  2770. this.finishOp(47, 1);
  2771. }
  2772. readToken_gt() {
  2773. const {
  2774. pos
  2775. } = this.state;
  2776. const next = this.input.charCodeAt(pos + 1);
  2777. if (next === 62) {
  2778. const size = this.input.charCodeAt(pos + 2) === 62 ? 3 : 2;
  2779. if (this.input.charCodeAt(pos + size) === 61) {
  2780. this.finishOp(30, size + 1);
  2781. return;
  2782. }
  2783. this.finishOp(52, size);
  2784. return;
  2785. }
  2786. if (next === 61) {
  2787. this.finishOp(49, 2);
  2788. return;
  2789. }
  2790. this.finishOp(48, 1);
  2791. }
  2792. readToken_eq_excl(code) {
  2793. const next = this.input.charCodeAt(this.state.pos + 1);
  2794. if (next === 61) {
  2795. this.finishOp(46, this.input.charCodeAt(this.state.pos + 2) === 61 ? 3 : 2);
  2796. return;
  2797. }
  2798. if (code === 61 && next === 62) {
  2799. this.state.pos += 2;
  2800. this.finishToken(19);
  2801. return;
  2802. }
  2803. this.finishOp(code === 61 ? 29 : 35, 1);
  2804. }
  2805. readToken_question() {
  2806. const next = this.input.charCodeAt(this.state.pos + 1);
  2807. const next2 = this.input.charCodeAt(this.state.pos + 2);
  2808. if (next === 63) {
  2809. if (next2 === 61) {
  2810. this.finishOp(30, 3);
  2811. } else {
  2812. this.finishOp(40, 2);
  2813. }
  2814. } else if (next === 46 && !(next2 >= 48 && next2 <= 57)) {
  2815. this.state.pos += 2;
  2816. this.finishToken(18);
  2817. } else {
  2818. ++this.state.pos;
  2819. this.finishToken(17);
  2820. }
  2821. }
  2822. getTokenFromCode(code) {
  2823. switch (code) {
  2824. case 46:
  2825. this.readToken_dot();
  2826. return;
  2827. case 40:
  2828. ++this.state.pos;
  2829. this.finishToken(10);
  2830. return;
  2831. case 41:
  2832. ++this.state.pos;
  2833. this.finishToken(11);
  2834. return;
  2835. case 59:
  2836. ++this.state.pos;
  2837. this.finishToken(13);
  2838. return;
  2839. case 44:
  2840. ++this.state.pos;
  2841. this.finishToken(12);
  2842. return;
  2843. case 91:
  2844. if (this.hasPlugin("recordAndTuple") && this.input.charCodeAt(this.state.pos + 1) === 124) {
  2845. if (this.getPluginOption("recordAndTuple", "syntaxType") !== "bar") {
  2846. throw this.raise(Errors.TupleExpressionBarIncorrectStartSyntaxType, this.state.curPosition());
  2847. }
  2848. this.state.pos += 2;
  2849. this.finishToken(2);
  2850. } else {
  2851. ++this.state.pos;
  2852. this.finishToken(0);
  2853. }
  2854. return;
  2855. case 93:
  2856. ++this.state.pos;
  2857. this.finishToken(3);
  2858. return;
  2859. case 123:
  2860. if (this.hasPlugin("recordAndTuple") && this.input.charCodeAt(this.state.pos + 1) === 124) {
  2861. if (this.getPluginOption("recordAndTuple", "syntaxType") !== "bar") {
  2862. throw this.raise(Errors.RecordExpressionBarIncorrectStartSyntaxType, this.state.curPosition());
  2863. }
  2864. this.state.pos += 2;
  2865. this.finishToken(6);
  2866. } else {
  2867. ++this.state.pos;
  2868. this.finishToken(5);
  2869. }
  2870. return;
  2871. case 125:
  2872. ++this.state.pos;
  2873. this.finishToken(8);
  2874. return;
  2875. case 58:
  2876. if (this.hasPlugin("functionBind") && this.input.charCodeAt(this.state.pos + 1) === 58) {
  2877. this.finishOp(15, 2);
  2878. } else {
  2879. ++this.state.pos;
  2880. this.finishToken(14);
  2881. }
  2882. return;
  2883. case 63:
  2884. this.readToken_question();
  2885. return;
  2886. case 96:
  2887. this.readTemplateToken();
  2888. return;
  2889. case 48:
  2890. {
  2891. const next = this.input.charCodeAt(this.state.pos + 1);
  2892. if (next === 120 || next === 88) {
  2893. this.readRadixNumber(16);
  2894. return;
  2895. }
  2896. if (next === 111 || next === 79) {
  2897. this.readRadixNumber(8);
  2898. return;
  2899. }
  2900. if (next === 98 || next === 66) {
  2901. this.readRadixNumber(2);
  2902. return;
  2903. }
  2904. }
  2905. case 49:
  2906. case 50:
  2907. case 51:
  2908. case 52:
  2909. case 53:
  2910. case 54:
  2911. case 55:
  2912. case 56:
  2913. case 57:
  2914. this.readNumber(false);
  2915. return;
  2916. case 34:
  2917. case 39:
  2918. this.readString(code);
  2919. return;
  2920. case 47:
  2921. this.readToken_slash();
  2922. return;
  2923. case 37:
  2924. case 42:
  2925. this.readToken_mult_modulo(code);
  2926. return;
  2927. case 124:
  2928. case 38:
  2929. this.readToken_pipe_amp(code);
  2930. return;
  2931. case 94:
  2932. this.readToken_caret();
  2933. return;
  2934. case 43:
  2935. case 45:
  2936. this.readToken_plus_min(code);
  2937. return;
  2938. case 60:
  2939. this.readToken_lt();
  2940. return;
  2941. case 62:
  2942. this.readToken_gt();
  2943. return;
  2944. case 61:
  2945. case 33:
  2946. this.readToken_eq_excl(code);
  2947. return;
  2948. case 126:
  2949. this.finishOp(36, 1);
  2950. return;
  2951. case 64:
  2952. this.readToken_atSign();
  2953. return;
  2954. case 35:
  2955. this.readToken_numberSign();
  2956. return;
  2957. case 92:
  2958. this.readWord();
  2959. return;
  2960. default:
  2961. if (isIdentifierStart(code)) {
  2962. this.readWord(code);
  2963. return;
  2964. }
  2965. }
  2966. throw this.raise(Errors.InvalidOrUnexpectedToken, this.state.curPosition(), {
  2967. unexpected: String.fromCodePoint(code)
  2968. });
  2969. }
  2970. finishOp(type, size) {
  2971. const str = this.input.slice(this.state.pos, this.state.pos + size);
  2972. this.state.pos += size;
  2973. this.finishToken(type, str);
  2974. }
  2975. readRegexp() {
  2976. const startLoc = this.state.startLoc;
  2977. const start = this.state.start + 1;
  2978. let escaped, inClass;
  2979. let {
  2980. pos
  2981. } = this.state;
  2982. for (;; ++pos) {
  2983. if (pos >= this.length) {
  2984. throw this.raise(Errors.UnterminatedRegExp, createPositionWithColumnOffset(startLoc, 1));
  2985. }
  2986. const ch = this.input.charCodeAt(pos);
  2987. if (isNewLine(ch)) {
  2988. throw this.raise(Errors.UnterminatedRegExp, createPositionWithColumnOffset(startLoc, 1));
  2989. }
  2990. if (escaped) {
  2991. escaped = false;
  2992. } else {
  2993. if (ch === 91) {
  2994. inClass = true;
  2995. } else if (ch === 93 && inClass) {
  2996. inClass = false;
  2997. } else if (ch === 47 && !inClass) {
  2998. break;
  2999. }
  3000. escaped = ch === 92;
  3001. }
  3002. }
  3003. const content = this.input.slice(start, pos);
  3004. ++pos;
  3005. let mods = "";
  3006. const nextPos = () => createPositionWithColumnOffset(startLoc, pos + 2 - start);
  3007. while (pos < this.length) {
  3008. const cp = this.codePointAtPos(pos);
  3009. const char = String.fromCharCode(cp);
  3010. if (VALID_REGEX_FLAGS.has(cp)) {
  3011. if (cp === 118) {
  3012. if (mods.includes("u")) {
  3013. this.raise(Errors.IncompatibleRegExpUVFlags, nextPos());
  3014. }
  3015. } else if (cp === 117) {
  3016. if (mods.includes("v")) {
  3017. this.raise(Errors.IncompatibleRegExpUVFlags, nextPos());
  3018. }
  3019. }
  3020. if (mods.includes(char)) {
  3021. this.raise(Errors.DuplicateRegExpFlags, nextPos());
  3022. }
  3023. } else if (isIdentifierChar(cp) || cp === 92) {
  3024. this.raise(Errors.MalformedRegExpFlags, nextPos());
  3025. } else {
  3026. break;
  3027. }
  3028. ++pos;
  3029. mods += char;
  3030. }
  3031. this.state.pos = pos;
  3032. this.finishToken(137, {
  3033. pattern: content,
  3034. flags: mods
  3035. });
  3036. }
  3037. readInt(radix, len, forceLen = false, allowNumSeparator = true) {
  3038. const {
  3039. n,
  3040. pos
  3041. } = readInt(this.input, this.state.pos, this.state.lineStart, this.state.curLine, radix, len, forceLen, allowNumSeparator, this.errorHandlers_readInt, false);
  3042. this.state.pos = pos;
  3043. return n;
  3044. }
  3045. readRadixNumber(radix) {
  3046. const startLoc = this.state.curPosition();
  3047. let isBigInt = false;
  3048. this.state.pos += 2;
  3049. const val = this.readInt(radix);
  3050. if (val == null) {
  3051. this.raise(Errors.InvalidDigit, createPositionWithColumnOffset(startLoc, 2), {
  3052. radix
  3053. });
  3054. }
  3055. const next = this.input.charCodeAt(this.state.pos);
  3056. if (next === 110) {
  3057. ++this.state.pos;
  3058. isBigInt = true;
  3059. } else if (next === 109) {
  3060. throw this.raise(Errors.InvalidDecimal, startLoc);
  3061. }
  3062. if (isIdentifierStart(this.codePointAtPos(this.state.pos))) {
  3063. throw this.raise(Errors.NumberIdentifier, this.state.curPosition());
  3064. }
  3065. if (isBigInt) {
  3066. const str = this.input.slice(startLoc.index, this.state.pos).replace(/[_n]/g, "");
  3067. this.finishToken(135, str);
  3068. return;
  3069. }
  3070. this.finishToken(134, val);
  3071. }
  3072. readNumber(startsWithDot) {
  3073. const start = this.state.pos;
  3074. const startLoc = this.state.curPosition();
  3075. let isFloat = false;
  3076. let isBigInt = false;
  3077. let isDecimal = false;
  3078. let hasExponent = false;
  3079. let isOctal = false;
  3080. if (!startsWithDot && this.readInt(10) === null) {
  3081. this.raise(Errors.InvalidNumber, this.state.curPosition());
  3082. }
  3083. const hasLeadingZero = this.state.pos - start >= 2 && this.input.charCodeAt(start) === 48;
  3084. if (hasLeadingZero) {
  3085. const integer = this.input.slice(start, this.state.pos);
  3086. this.recordStrictModeErrors(Errors.StrictOctalLiteral, startLoc);
  3087. if (!this.state.strict) {
  3088. const underscorePos = integer.indexOf("_");
  3089. if (underscorePos > 0) {
  3090. this.raise(Errors.ZeroDigitNumericSeparator, createPositionWithColumnOffset(startLoc, underscorePos));
  3091. }
  3092. }
  3093. isOctal = hasLeadingZero && !/[89]/.test(integer);
  3094. }
  3095. let next = this.input.charCodeAt(this.state.pos);
  3096. if (next === 46 && !isOctal) {
  3097. ++this.state.pos;
  3098. this.readInt(10);
  3099. isFloat = true;
  3100. next = this.input.charCodeAt(this.state.pos);
  3101. }
  3102. if ((next === 69 || next === 101) && !isOctal) {
  3103. next = this.input.charCodeAt(++this.state.pos);
  3104. if (next === 43 || next === 45) {
  3105. ++this.state.pos;
  3106. }
  3107. if (this.readInt(10) === null) {
  3108. this.raise(Errors.InvalidOrMissingExponent, startLoc);
  3109. }
  3110. isFloat = true;
  3111. hasExponent = true;
  3112. next = this.input.charCodeAt(this.state.pos);
  3113. }
  3114. if (next === 110) {
  3115. if (isFloat || hasLeadingZero) {
  3116. this.raise(Errors.InvalidBigIntLiteral, startLoc);
  3117. }
  3118. ++this.state.pos;
  3119. isBigInt = true;
  3120. }
  3121. if (next === 109) {
  3122. this.expectPlugin("decimal", this.state.curPosition());
  3123. if (hasExponent || hasLeadingZero) {
  3124. this.raise(Errors.InvalidDecimal, startLoc);
  3125. }
  3126. ++this.state.pos;
  3127. isDecimal = true;
  3128. }
  3129. if (isIdentifierStart(this.codePointAtPos(this.state.pos))) {
  3130. throw this.raise(Errors.NumberIdentifier, this.state.curPosition());
  3131. }
  3132. const str = this.input.slice(start, this.state.pos).replace(/[_mn]/g, "");
  3133. if (isBigInt) {
  3134. this.finishToken(135, str);
  3135. return;
  3136. }
  3137. if (isDecimal) {
  3138. this.finishToken(136, str);
  3139. return;
  3140. }
  3141. const val = isOctal ? parseInt(str, 8) : parseFloat(str);
  3142. this.finishToken(134, val);
  3143. }
  3144. readCodePoint(throwOnInvalid) {
  3145. const {
  3146. code,
  3147. pos
  3148. } = readCodePoint(this.input, this.state.pos, this.state.lineStart, this.state.curLine, throwOnInvalid, this.errorHandlers_readCodePoint);
  3149. this.state.pos = pos;
  3150. return code;
  3151. }
  3152. readString(quote) {
  3153. const {
  3154. str,
  3155. pos,
  3156. curLine,
  3157. lineStart
  3158. } = readStringContents(quote === 34 ? "double" : "single", this.input, this.state.pos + 1, this.state.lineStart, this.state.curLine, this.errorHandlers_readStringContents_string);
  3159. this.state.pos = pos + 1;
  3160. this.state.lineStart = lineStart;
  3161. this.state.curLine = curLine;
  3162. this.finishToken(133, str);
  3163. }
  3164. readTemplateContinuation() {
  3165. if (!this.match(8)) {
  3166. this.unexpected(null, 8);
  3167. }
  3168. this.state.pos--;
  3169. this.readTemplateToken();
  3170. }
  3171. readTemplateToken() {
  3172. const opening = this.input[this.state.pos];
  3173. const {
  3174. str,
  3175. firstInvalidLoc,
  3176. pos,
  3177. curLine,
  3178. lineStart
  3179. } = readStringContents("template", this.input, this.state.pos + 1, this.state.lineStart, this.state.curLine, this.errorHandlers_readStringContents_template);
  3180. this.state.pos = pos + 1;
  3181. this.state.lineStart = lineStart;
  3182. this.state.curLine = curLine;
  3183. if (firstInvalidLoc) {
  3184. this.state.firstInvalidTemplateEscapePos = new Position(firstInvalidLoc.curLine, firstInvalidLoc.pos - firstInvalidLoc.lineStart, firstInvalidLoc.pos);
  3185. }
  3186. if (this.input.codePointAt(pos) === 96) {
  3187. this.finishToken(24, firstInvalidLoc ? null : opening + str + "`");
  3188. } else {
  3189. this.state.pos++;
  3190. this.finishToken(25, firstInvalidLoc ? null : opening + str + "${");
  3191. }
  3192. }
  3193. recordStrictModeErrors(toParseError, at) {
  3194. const index = at.index;
  3195. if (this.state.strict && !this.state.strictErrors.has(index)) {
  3196. this.raise(toParseError, at);
  3197. } else {
  3198. this.state.strictErrors.set(index, [toParseError, at]);
  3199. }
  3200. }
  3201. readWord1(firstCode) {
  3202. this.state.containsEsc = false;
  3203. let word = "";
  3204. const start = this.state.pos;
  3205. let chunkStart = this.state.pos;
  3206. if (firstCode !== undefined) {
  3207. this.state.pos += firstCode <= 0xffff ? 1 : 2;
  3208. }
  3209. while (this.state.pos < this.length) {
  3210. const ch = this.codePointAtPos(this.state.pos);
  3211. if (isIdentifierChar(ch)) {
  3212. this.state.pos += ch <= 0xffff ? 1 : 2;
  3213. } else if (ch === 92) {
  3214. this.state.containsEsc = true;
  3215. word += this.input.slice(chunkStart, this.state.pos);
  3216. const escStart = this.state.curPosition();
  3217. const identifierCheck = this.state.pos === start ? isIdentifierStart : isIdentifierChar;
  3218. if (this.input.charCodeAt(++this.state.pos) !== 117) {
  3219. this.raise(Errors.MissingUnicodeEscape, this.state.curPosition());
  3220. chunkStart = this.state.pos - 1;
  3221. continue;
  3222. }
  3223. ++this.state.pos;
  3224. const esc = this.readCodePoint(true);
  3225. if (esc !== null) {
  3226. if (!identifierCheck(esc)) {
  3227. this.raise(Errors.EscapedCharNotAnIdentifier, escStart);
  3228. }
  3229. word += String.fromCodePoint(esc);
  3230. }
  3231. chunkStart = this.state.pos;
  3232. } else {
  3233. break;
  3234. }
  3235. }
  3236. return word + this.input.slice(chunkStart, this.state.pos);
  3237. }
  3238. readWord(firstCode) {
  3239. const word = this.readWord1(firstCode);
  3240. const type = keywords$1.get(word);
  3241. if (type !== undefined) {
  3242. this.finishToken(type, tokenLabelName(type));
  3243. } else {
  3244. this.finishToken(132, word);
  3245. }
  3246. }
  3247. checkKeywordEscapes() {
  3248. const {
  3249. type
  3250. } = this.state;
  3251. if (tokenIsKeyword(type) && this.state.containsEsc) {
  3252. this.raise(Errors.InvalidEscapedReservedWord, this.state.startLoc, {
  3253. reservedWord: tokenLabelName(type)
  3254. });
  3255. }
  3256. }
  3257. raise(toParseError, at, details = {}) {
  3258. const loc = at instanceof Position ? at : at.loc.start;
  3259. const error = toParseError(loc, details);
  3260. if (!this.options.errorRecovery) throw error;
  3261. if (!this.isLookahead) this.state.errors.push(error);
  3262. return error;
  3263. }
  3264. raiseOverwrite(toParseError, at, details = {}) {
  3265. const loc = at instanceof Position ? at : at.loc.start;
  3266. const pos = loc.index;
  3267. const errors = this.state.errors;
  3268. for (let i = errors.length - 1; i >= 0; i--) {
  3269. const error = errors[i];
  3270. if (error.loc.index === pos) {
  3271. return errors[i] = toParseError(loc, details);
  3272. }
  3273. if (error.loc.index < pos) break;
  3274. }
  3275. return this.raise(toParseError, at, details);
  3276. }
  3277. updateContext(prevType) {}
  3278. unexpected(loc, type) {
  3279. throw this.raise(Errors.UnexpectedToken, loc != null ? loc : this.state.startLoc, {
  3280. expected: type ? tokenLabelName(type) : null
  3281. });
  3282. }
  3283. expectPlugin(pluginName, loc) {
  3284. if (this.hasPlugin(pluginName)) {
  3285. return true;
  3286. }
  3287. throw this.raise(Errors.MissingPlugin, loc != null ? loc : this.state.startLoc, {
  3288. missingPlugin: [pluginName]
  3289. });
  3290. }
  3291. expectOnePlugin(pluginNames) {
  3292. if (!pluginNames.some(name => this.hasPlugin(name))) {
  3293. throw this.raise(Errors.MissingOneOfPlugins, this.state.startLoc, {
  3294. missingPlugin: pluginNames
  3295. });
  3296. }
  3297. }
  3298. errorBuilder(error) {
  3299. return (pos, lineStart, curLine) => {
  3300. this.raise(error, buildPosition(pos, lineStart, curLine));
  3301. };
  3302. }
  3303. }
  3304. class ClassScope {
  3305. constructor() {
  3306. this.privateNames = new Set();
  3307. this.loneAccessors = new Map();
  3308. this.undefinedPrivateNames = new Map();
  3309. }
  3310. }
  3311. class ClassScopeHandler {
  3312. constructor(parser) {
  3313. this.parser = void 0;
  3314. this.stack = [];
  3315. this.undefinedPrivateNames = new Map();
  3316. this.parser = parser;
  3317. }
  3318. current() {
  3319. return this.stack[this.stack.length - 1];
  3320. }
  3321. enter() {
  3322. this.stack.push(new ClassScope());
  3323. }
  3324. exit() {
  3325. const oldClassScope = this.stack.pop();
  3326. const current = this.current();
  3327. for (const [name, loc] of Array.from(oldClassScope.undefinedPrivateNames)) {
  3328. if (current) {
  3329. if (!current.undefinedPrivateNames.has(name)) {
  3330. current.undefinedPrivateNames.set(name, loc);
  3331. }
  3332. } else {
  3333. this.parser.raise(Errors.InvalidPrivateFieldResolution, loc, {
  3334. identifierName: name
  3335. });
  3336. }
  3337. }
  3338. }
  3339. declarePrivateName(name, elementType, loc) {
  3340. const {
  3341. privateNames,
  3342. loneAccessors,
  3343. undefinedPrivateNames
  3344. } = this.current();
  3345. let redefined = privateNames.has(name);
  3346. if (elementType & 3) {
  3347. const accessor = redefined && loneAccessors.get(name);
  3348. if (accessor) {
  3349. const oldStatic = accessor & 4;
  3350. const newStatic = elementType & 4;
  3351. const oldKind = accessor & 3;
  3352. const newKind = elementType & 3;
  3353. redefined = oldKind === newKind || oldStatic !== newStatic;
  3354. if (!redefined) loneAccessors.delete(name);
  3355. } else if (!redefined) {
  3356. loneAccessors.set(name, elementType);
  3357. }
  3358. }
  3359. if (redefined) {
  3360. this.parser.raise(Errors.PrivateNameRedeclaration, loc, {
  3361. identifierName: name
  3362. });
  3363. }
  3364. privateNames.add(name);
  3365. undefinedPrivateNames.delete(name);
  3366. }
  3367. usePrivateName(name, loc) {
  3368. let classScope;
  3369. for (classScope of this.stack) {
  3370. if (classScope.privateNames.has(name)) return;
  3371. }
  3372. if (classScope) {
  3373. classScope.undefinedPrivateNames.set(name, loc);
  3374. } else {
  3375. this.parser.raise(Errors.InvalidPrivateFieldResolution, loc, {
  3376. identifierName: name
  3377. });
  3378. }
  3379. }
  3380. }
  3381. class ExpressionScope {
  3382. constructor(type = 0) {
  3383. this.type = type;
  3384. }
  3385. canBeArrowParameterDeclaration() {
  3386. return this.type === 2 || this.type === 1;
  3387. }
  3388. isCertainlyParameterDeclaration() {
  3389. return this.type === 3;
  3390. }
  3391. }
  3392. class ArrowHeadParsingScope extends ExpressionScope {
  3393. constructor(type) {
  3394. super(type);
  3395. this.declarationErrors = new Map();
  3396. }
  3397. recordDeclarationError(ParsingErrorClass, at) {
  3398. const index = at.index;
  3399. this.declarationErrors.set(index, [ParsingErrorClass, at]);
  3400. }
  3401. clearDeclarationError(index) {
  3402. this.declarationErrors.delete(index);
  3403. }
  3404. iterateErrors(iterator) {
  3405. this.declarationErrors.forEach(iterator);
  3406. }
  3407. }
  3408. class ExpressionScopeHandler {
  3409. constructor(parser) {
  3410. this.parser = void 0;
  3411. this.stack = [new ExpressionScope()];
  3412. this.parser = parser;
  3413. }
  3414. enter(scope) {
  3415. this.stack.push(scope);
  3416. }
  3417. exit() {
  3418. this.stack.pop();
  3419. }
  3420. recordParameterInitializerError(toParseError, node) {
  3421. const origin = node.loc.start;
  3422. const {
  3423. stack
  3424. } = this;
  3425. let i = stack.length - 1;
  3426. let scope = stack[i];
  3427. while (!scope.isCertainlyParameterDeclaration()) {
  3428. if (scope.canBeArrowParameterDeclaration()) {
  3429. scope.recordDeclarationError(toParseError, origin);
  3430. } else {
  3431. return;
  3432. }
  3433. scope = stack[--i];
  3434. }
  3435. this.parser.raise(toParseError, origin);
  3436. }
  3437. recordArrowParameterBindingError(error, node) {
  3438. const {
  3439. stack
  3440. } = this;
  3441. const scope = stack[stack.length - 1];
  3442. const origin = node.loc.start;
  3443. if (scope.isCertainlyParameterDeclaration()) {
  3444. this.parser.raise(error, origin);
  3445. } else if (scope.canBeArrowParameterDeclaration()) {
  3446. scope.recordDeclarationError(error, origin);
  3447. } else {
  3448. return;
  3449. }
  3450. }
  3451. recordAsyncArrowParametersError(at) {
  3452. const {
  3453. stack
  3454. } = this;
  3455. let i = stack.length - 1;
  3456. let scope = stack[i];
  3457. while (scope.canBeArrowParameterDeclaration()) {
  3458. if (scope.type === 2) {
  3459. scope.recordDeclarationError(Errors.AwaitBindingIdentifier, at);
  3460. }
  3461. scope = stack[--i];
  3462. }
  3463. }
  3464. validateAsPattern() {
  3465. const {
  3466. stack
  3467. } = this;
  3468. const currentScope = stack[stack.length - 1];
  3469. if (!currentScope.canBeArrowParameterDeclaration()) return;
  3470. currentScope.iterateErrors(([toParseError, loc]) => {
  3471. this.parser.raise(toParseError, loc);
  3472. let i = stack.length - 2;
  3473. let scope = stack[i];
  3474. while (scope.canBeArrowParameterDeclaration()) {
  3475. scope.clearDeclarationError(loc.index);
  3476. scope = stack[--i];
  3477. }
  3478. });
  3479. }
  3480. }
  3481. function newParameterDeclarationScope() {
  3482. return new ExpressionScope(3);
  3483. }
  3484. function newArrowHeadScope() {
  3485. return new ArrowHeadParsingScope(1);
  3486. }
  3487. function newAsyncArrowScope() {
  3488. return new ArrowHeadParsingScope(2);
  3489. }
  3490. function newExpressionScope() {
  3491. return new ExpressionScope();
  3492. }
  3493. class ProductionParameterHandler {
  3494. constructor() {
  3495. this.stacks = [];
  3496. }
  3497. enter(flags) {
  3498. this.stacks.push(flags);
  3499. }
  3500. exit() {
  3501. this.stacks.pop();
  3502. }
  3503. currentFlags() {
  3504. return this.stacks[this.stacks.length - 1];
  3505. }
  3506. get hasAwait() {
  3507. return (this.currentFlags() & 2) > 0;
  3508. }
  3509. get hasYield() {
  3510. return (this.currentFlags() & 1) > 0;
  3511. }
  3512. get hasReturn() {
  3513. return (this.currentFlags() & 4) > 0;
  3514. }
  3515. get hasIn() {
  3516. return (this.currentFlags() & 8) > 0;
  3517. }
  3518. }
  3519. function functionFlags(isAsync, isGenerator) {
  3520. return (isAsync ? 2 : 0) | (isGenerator ? 1 : 0);
  3521. }
  3522. class UtilParser extends Tokenizer {
  3523. addExtra(node, key, value, enumerable = true) {
  3524. if (!node) return;
  3525. let {
  3526. extra
  3527. } = node;
  3528. if (extra == null) {
  3529. extra = {};
  3530. node.extra = extra;
  3531. }
  3532. if (enumerable) {
  3533. extra[key] = value;
  3534. } else {
  3535. Object.defineProperty(extra, key, {
  3536. enumerable,
  3537. value
  3538. });
  3539. }
  3540. }
  3541. isContextual(token) {
  3542. return this.state.type === token && !this.state.containsEsc;
  3543. }
  3544. isUnparsedContextual(nameStart, name) {
  3545. const nameEnd = nameStart + name.length;
  3546. if (this.input.slice(nameStart, nameEnd) === name) {
  3547. const nextCh = this.input.charCodeAt(nameEnd);
  3548. return !(isIdentifierChar(nextCh) || (nextCh & 0xfc00) === 0xd800);
  3549. }
  3550. return false;
  3551. }
  3552. isLookaheadContextual(name) {
  3553. const next = this.nextTokenStart();
  3554. return this.isUnparsedContextual(next, name);
  3555. }
  3556. eatContextual(token) {
  3557. if (this.isContextual(token)) {
  3558. this.next();
  3559. return true;
  3560. }
  3561. return false;
  3562. }
  3563. expectContextual(token, toParseError) {
  3564. if (!this.eatContextual(token)) {
  3565. if (toParseError != null) {
  3566. throw this.raise(toParseError, this.state.startLoc);
  3567. }
  3568. this.unexpected(null, token);
  3569. }
  3570. }
  3571. canInsertSemicolon() {
  3572. return this.match(139) || this.match(8) || this.hasPrecedingLineBreak();
  3573. }
  3574. hasPrecedingLineBreak() {
  3575. return hasNewLine(this.input, this.state.lastTokEndLoc.index, this.state.start);
  3576. }
  3577. hasFollowingLineBreak() {
  3578. return hasNewLine(this.input, this.state.end, this.nextTokenStart());
  3579. }
  3580. isLineTerminator() {
  3581. return this.eat(13) || this.canInsertSemicolon();
  3582. }
  3583. semicolon(allowAsi = true) {
  3584. if (allowAsi ? this.isLineTerminator() : this.eat(13)) return;
  3585. this.raise(Errors.MissingSemicolon, this.state.lastTokEndLoc);
  3586. }
  3587. expect(type, loc) {
  3588. if (!this.eat(type)) {
  3589. this.unexpected(loc, type);
  3590. }
  3591. }
  3592. tryParse(fn, oldState = this.state.clone()) {
  3593. const abortSignal = {
  3594. node: null
  3595. };
  3596. try {
  3597. const node = fn((node = null) => {
  3598. abortSignal.node = node;
  3599. throw abortSignal;
  3600. });
  3601. if (this.state.errors.length > oldState.errors.length) {
  3602. const failState = this.state;
  3603. this.state = oldState;
  3604. this.state.tokensLength = failState.tokensLength;
  3605. return {
  3606. node,
  3607. error: failState.errors[oldState.errors.length],
  3608. thrown: false,
  3609. aborted: false,
  3610. failState
  3611. };
  3612. }
  3613. return {
  3614. node,
  3615. error: null,
  3616. thrown: false,
  3617. aborted: false,
  3618. failState: null
  3619. };
  3620. } catch (error) {
  3621. const failState = this.state;
  3622. this.state = oldState;
  3623. if (error instanceof SyntaxError) {
  3624. return {
  3625. node: null,
  3626. error,
  3627. thrown: true,
  3628. aborted: false,
  3629. failState
  3630. };
  3631. }
  3632. if (error === abortSignal) {
  3633. return {
  3634. node: abortSignal.node,
  3635. error: null,
  3636. thrown: false,
  3637. aborted: true,
  3638. failState
  3639. };
  3640. }
  3641. throw error;
  3642. }
  3643. }
  3644. checkExpressionErrors(refExpressionErrors, andThrow) {
  3645. if (!refExpressionErrors) return false;
  3646. const {
  3647. shorthandAssignLoc,
  3648. doubleProtoLoc,
  3649. privateKeyLoc,
  3650. optionalParametersLoc
  3651. } = refExpressionErrors;
  3652. const hasErrors = !!shorthandAssignLoc || !!doubleProtoLoc || !!optionalParametersLoc || !!privateKeyLoc;
  3653. if (!andThrow) {
  3654. return hasErrors;
  3655. }
  3656. if (shorthandAssignLoc != null) {
  3657. this.raise(Errors.InvalidCoverInitializedName, shorthandAssignLoc);
  3658. }
  3659. if (doubleProtoLoc != null) {
  3660. this.raise(Errors.DuplicateProto, doubleProtoLoc);
  3661. }
  3662. if (privateKeyLoc != null) {
  3663. this.raise(Errors.UnexpectedPrivateField, privateKeyLoc);
  3664. }
  3665. if (optionalParametersLoc != null) {
  3666. this.unexpected(optionalParametersLoc);
  3667. }
  3668. }
  3669. isLiteralPropertyName() {
  3670. return tokenIsLiteralPropertyName(this.state.type);
  3671. }
  3672. isPrivateName(node) {
  3673. return node.type === "PrivateName";
  3674. }
  3675. getPrivateNameSV(node) {
  3676. return node.id.name;
  3677. }
  3678. hasPropertyAsPrivateName(node) {
  3679. return (node.type === "MemberExpression" || node.type === "OptionalMemberExpression") && this.isPrivateName(node.property);
  3680. }
  3681. isObjectProperty(node) {
  3682. return node.type === "ObjectProperty";
  3683. }
  3684. isObjectMethod(node) {
  3685. return node.type === "ObjectMethod";
  3686. }
  3687. initializeScopes(inModule = this.options.sourceType === "module") {
  3688. const oldLabels = this.state.labels;
  3689. this.state.labels = [];
  3690. const oldExportedIdentifiers = this.exportedIdentifiers;
  3691. this.exportedIdentifiers = new Set();
  3692. const oldInModule = this.inModule;
  3693. this.inModule = inModule;
  3694. const oldScope = this.scope;
  3695. const ScopeHandler = this.getScopeHandler();
  3696. this.scope = new ScopeHandler(this, inModule);
  3697. const oldProdParam = this.prodParam;
  3698. this.prodParam = new ProductionParameterHandler();
  3699. const oldClassScope = this.classScope;
  3700. this.classScope = new ClassScopeHandler(this);
  3701. const oldExpressionScope = this.expressionScope;
  3702. this.expressionScope = new ExpressionScopeHandler(this);
  3703. return () => {
  3704. this.state.labels = oldLabels;
  3705. this.exportedIdentifiers = oldExportedIdentifiers;
  3706. this.inModule = oldInModule;
  3707. this.scope = oldScope;
  3708. this.prodParam = oldProdParam;
  3709. this.classScope = oldClassScope;
  3710. this.expressionScope = oldExpressionScope;
  3711. };
  3712. }
  3713. enterInitialScopes() {
  3714. let paramFlags = 0;
  3715. if (this.inModule) {
  3716. paramFlags |= 2;
  3717. }
  3718. this.scope.enter(1);
  3719. this.prodParam.enter(paramFlags);
  3720. }
  3721. checkDestructuringPrivate(refExpressionErrors) {
  3722. const {
  3723. privateKeyLoc
  3724. } = refExpressionErrors;
  3725. if (privateKeyLoc !== null) {
  3726. this.expectPlugin("destructuringPrivate", privateKeyLoc);
  3727. }
  3728. }
  3729. }
  3730. class ExpressionErrors {
  3731. constructor() {
  3732. this.shorthandAssignLoc = null;
  3733. this.doubleProtoLoc = null;
  3734. this.privateKeyLoc = null;
  3735. this.optionalParametersLoc = null;
  3736. }
  3737. }
  3738. class Node {
  3739. constructor(parser, pos, loc) {
  3740. this.type = "";
  3741. this.start = pos;
  3742. this.end = 0;
  3743. this.loc = new SourceLocation(loc);
  3744. if (parser != null && parser.options.ranges) this.range = [pos, 0];
  3745. if (parser != null && parser.filename) this.loc.filename = parser.filename;
  3746. }
  3747. }
  3748. const NodePrototype = Node.prototype;
  3749. {
  3750. NodePrototype.__clone = function () {
  3751. const newNode = new Node(undefined, this.start, this.loc.start);
  3752. const keys = Object.keys(this);
  3753. for (let i = 0, length = keys.length; i < length; i++) {
  3754. const key = keys[i];
  3755. if (key !== "leadingComments" && key !== "trailingComments" && key !== "innerComments") {
  3756. newNode[key] = this[key];
  3757. }
  3758. }
  3759. return newNode;
  3760. };
  3761. }
  3762. function clonePlaceholder(node) {
  3763. return cloneIdentifier(node);
  3764. }
  3765. function cloneIdentifier(node) {
  3766. const {
  3767. type,
  3768. start,
  3769. end,
  3770. loc,
  3771. range,
  3772. extra,
  3773. name
  3774. } = node;
  3775. const cloned = Object.create(NodePrototype);
  3776. cloned.type = type;
  3777. cloned.start = start;
  3778. cloned.end = end;
  3779. cloned.loc = loc;
  3780. cloned.range = range;
  3781. cloned.extra = extra;
  3782. cloned.name = name;
  3783. if (type === "Placeholder") {
  3784. cloned.expectedNode = node.expectedNode;
  3785. }
  3786. return cloned;
  3787. }
  3788. function cloneStringLiteral(node) {
  3789. const {
  3790. type,
  3791. start,
  3792. end,
  3793. loc,
  3794. range,
  3795. extra
  3796. } = node;
  3797. if (type === "Placeholder") {
  3798. return clonePlaceholder(node);
  3799. }
  3800. const cloned = Object.create(NodePrototype);
  3801. cloned.type = type;
  3802. cloned.start = start;
  3803. cloned.end = end;
  3804. cloned.loc = loc;
  3805. cloned.range = range;
  3806. if (node.raw !== undefined) {
  3807. cloned.raw = node.raw;
  3808. } else {
  3809. cloned.extra = extra;
  3810. }
  3811. cloned.value = node.value;
  3812. return cloned;
  3813. }
  3814. class NodeUtils extends UtilParser {
  3815. startNode() {
  3816. const loc = this.state.startLoc;
  3817. return new Node(this, loc.index, loc);
  3818. }
  3819. startNodeAt(loc) {
  3820. return new Node(this, loc.index, loc);
  3821. }
  3822. startNodeAtNode(type) {
  3823. return this.startNodeAt(type.loc.start);
  3824. }
  3825. finishNode(node, type) {
  3826. return this.finishNodeAt(node, type, this.state.lastTokEndLoc);
  3827. }
  3828. finishNodeAt(node, type, endLoc) {
  3829. node.type = type;
  3830. node.end = endLoc.index;
  3831. node.loc.end = endLoc;
  3832. if (this.options.ranges) node.range[1] = endLoc.index;
  3833. if (this.options.attachComment) this.processComment(node);
  3834. return node;
  3835. }
  3836. resetStartLocation(node, startLoc) {
  3837. node.start = startLoc.index;
  3838. node.loc.start = startLoc;
  3839. if (this.options.ranges) node.range[0] = startLoc.index;
  3840. }
  3841. resetEndLocation(node, endLoc = this.state.lastTokEndLoc) {
  3842. node.end = endLoc.index;
  3843. node.loc.end = endLoc;
  3844. if (this.options.ranges) node.range[1] = endLoc.index;
  3845. }
  3846. resetStartLocationFromNode(node, locationNode) {
  3847. this.resetStartLocation(node, locationNode.loc.start);
  3848. }
  3849. }
  3850. const reservedTypes = new Set(["_", "any", "bool", "boolean", "empty", "extends", "false", "interface", "mixed", "null", "number", "static", "string", "true", "typeof", "void"]);
  3851. const FlowErrors = ParseErrorEnum`flow`({
  3852. AmbiguousConditionalArrow: "Ambiguous expression: wrap the arrow functions in parentheses to disambiguate.",
  3853. AmbiguousDeclareModuleKind: "Found both `declare module.exports` and `declare export` in the same module. Modules can only have 1 since they are either an ES module or they are a CommonJS module.",
  3854. AssignReservedType: ({
  3855. reservedType
  3856. }) => `Cannot overwrite reserved type ${reservedType}.`,
  3857. DeclareClassElement: "The `declare` modifier can only appear on class fields.",
  3858. DeclareClassFieldInitializer: "Initializers are not allowed in fields with the `declare` modifier.",
  3859. DuplicateDeclareModuleExports: "Duplicate `declare module.exports` statement.",
  3860. EnumBooleanMemberNotInitialized: ({
  3861. memberName,
  3862. enumName
  3863. }) => `Boolean enum members need to be initialized. Use either \`${memberName} = true,\` or \`${memberName} = false,\` in enum \`${enumName}\`.`,
  3864. EnumDuplicateMemberName: ({
  3865. memberName,
  3866. enumName
  3867. }) => `Enum member names need to be unique, but the name \`${memberName}\` has already been used before in enum \`${enumName}\`.`,
  3868. EnumInconsistentMemberValues: ({
  3869. enumName
  3870. }) => `Enum \`${enumName}\` has inconsistent member initializers. Either use no initializers, or consistently use literals (either booleans, numbers, or strings) for all member initializers.`,
  3871. EnumInvalidExplicitType: ({
  3872. invalidEnumType,
  3873. enumName
  3874. }) => `Enum type \`${invalidEnumType}\` is not valid. Use one of \`boolean\`, \`number\`, \`string\`, or \`symbol\` in enum \`${enumName}\`.`,
  3875. EnumInvalidExplicitTypeUnknownSupplied: ({
  3876. enumName
  3877. }) => `Supplied enum type is not valid. Use one of \`boolean\`, \`number\`, \`string\`, or \`symbol\` in enum \`${enumName}\`.`,
  3878. EnumInvalidMemberInitializerPrimaryType: ({
  3879. enumName,
  3880. memberName,
  3881. explicitType
  3882. }) => `Enum \`${enumName}\` has type \`${explicitType}\`, so the initializer of \`${memberName}\` needs to be a ${explicitType} literal.`,
  3883. EnumInvalidMemberInitializerSymbolType: ({
  3884. enumName,
  3885. memberName
  3886. }) => `Symbol enum members cannot be initialized. Use \`${memberName},\` in enum \`${enumName}\`.`,
  3887. EnumInvalidMemberInitializerUnknownType: ({
  3888. enumName,
  3889. memberName
  3890. }) => `The enum member initializer for \`${memberName}\` needs to be a literal (either a boolean, number, or string) in enum \`${enumName}\`.`,
  3891. EnumInvalidMemberName: ({
  3892. enumName,
  3893. memberName,
  3894. suggestion
  3895. }) => `Enum member names cannot start with lowercase 'a' through 'z'. Instead of using \`${memberName}\`, consider using \`${suggestion}\`, in enum \`${enumName}\`.`,
  3896. EnumNumberMemberNotInitialized: ({
  3897. enumName,
  3898. memberName
  3899. }) => `Number enum members need to be initialized, e.g. \`${memberName} = 1\` in enum \`${enumName}\`.`,
  3900. EnumStringMemberInconsistentlyInitialized: ({
  3901. enumName
  3902. }) => `String enum members need to consistently either all use initializers, or use no initializers, in enum \`${enumName}\`.`,
  3903. GetterMayNotHaveThisParam: "A getter cannot have a `this` parameter.",
  3904. ImportReflectionHasImportType: "An `import module` declaration can not use `type` or `typeof` keyword.",
  3905. ImportTypeShorthandOnlyInPureImport: "The `type` and `typeof` keywords on named imports can only be used on regular `import` statements. It cannot be used with `import type` or `import typeof` statements.",
  3906. InexactInsideExact: "Explicit inexact syntax cannot appear inside an explicit exact object type.",
  3907. InexactInsideNonObject: "Explicit inexact syntax cannot appear in class or interface definitions.",
  3908. InexactVariance: "Explicit inexact syntax cannot have variance.",
  3909. InvalidNonTypeImportInDeclareModule: "Imports within a `declare module` body must always be `import type` or `import typeof`.",
  3910. MissingTypeParamDefault: "Type parameter declaration needs a default, since a preceding type parameter declaration has a default.",
  3911. NestedDeclareModule: "`declare module` cannot be used inside another `declare module`.",
  3912. NestedFlowComment: "Cannot have a flow comment inside another flow comment.",
  3913. PatternIsOptional: Object.assign({
  3914. message: "A binding pattern parameter cannot be optional in an implementation signature."
  3915. }, {
  3916. reasonCode: "OptionalBindingPattern"
  3917. }),
  3918. SetterMayNotHaveThisParam: "A setter cannot have a `this` parameter.",
  3919. SpreadVariance: "Spread properties cannot have variance.",
  3920. ThisParamAnnotationRequired: "A type annotation is required for the `this` parameter.",
  3921. ThisParamBannedInConstructor: "Constructors cannot have a `this` parameter; constructors don't bind `this` like other functions.",
  3922. ThisParamMayNotBeOptional: "The `this` parameter cannot be optional.",
  3923. ThisParamMustBeFirst: "The `this` parameter must be the first function parameter.",
  3924. ThisParamNoDefault: "The `this` parameter may not have a default value.",
  3925. TypeBeforeInitializer: "Type annotations must come before default assignments, e.g. instead of `age = 25: number` use `age: number = 25`.",
  3926. TypeCastInPattern: "The type cast expression is expected to be wrapped with parenthesis.",
  3927. UnexpectedExplicitInexactInObject: "Explicit inexact syntax must appear at the end of an inexact object.",
  3928. UnexpectedReservedType: ({
  3929. reservedType
  3930. }) => `Unexpected reserved type ${reservedType}.`,
  3931. UnexpectedReservedUnderscore: "`_` is only allowed as a type argument to call or new.",
  3932. UnexpectedSpaceBetweenModuloChecks: "Spaces between `%` and `checks` are not allowed here.",
  3933. UnexpectedSpreadType: "Spread operator cannot appear in class or interface definitions.",
  3934. UnexpectedSubtractionOperand: 'Unexpected token, expected "number" or "bigint".',
  3935. UnexpectedTokenAfterTypeParameter: "Expected an arrow function after this type parameter declaration.",
  3936. UnexpectedTypeParameterBeforeAsyncArrowFunction: "Type parameters must come after the async keyword, e.g. instead of `<T> async () => {}`, use `async <T>() => {}`.",
  3937. UnsupportedDeclareExportKind: ({
  3938. unsupportedExportKind,
  3939. suggestion
  3940. }) => `\`declare export ${unsupportedExportKind}\` is not supported. Use \`${suggestion}\` instead.`,
  3941. UnsupportedStatementInDeclareModule: "Only declares and type imports are allowed inside declare module.",
  3942. UnterminatedFlowComment: "Unterminated flow-comment."
  3943. });
  3944. function isEsModuleType(bodyElement) {
  3945. return bodyElement.type === "DeclareExportAllDeclaration" || bodyElement.type === "DeclareExportDeclaration" && (!bodyElement.declaration || bodyElement.declaration.type !== "TypeAlias" && bodyElement.declaration.type !== "InterfaceDeclaration");
  3946. }
  3947. function hasTypeImportKind(node) {
  3948. return node.importKind === "type" || node.importKind === "typeof";
  3949. }
  3950. const exportSuggestions = {
  3951. const: "declare export var",
  3952. let: "declare export var",
  3953. type: "export type",
  3954. interface: "export interface"
  3955. };
  3956. function partition(list, test) {
  3957. const list1 = [];
  3958. const list2 = [];
  3959. for (let i = 0; i < list.length; i++) {
  3960. (test(list[i], i, list) ? list1 : list2).push(list[i]);
  3961. }
  3962. return [list1, list2];
  3963. }
  3964. const FLOW_PRAGMA_REGEX = /\*?\s*@((?:no)?flow)\b/;
  3965. var flow = superClass => class FlowParserMixin extends superClass {
  3966. constructor(...args) {
  3967. super(...args);
  3968. this.flowPragma = undefined;
  3969. }
  3970. getScopeHandler() {
  3971. return FlowScopeHandler;
  3972. }
  3973. shouldParseTypes() {
  3974. return this.getPluginOption("flow", "all") || this.flowPragma === "flow";
  3975. }
  3976. shouldParseEnums() {
  3977. return !!this.getPluginOption("flow", "enums");
  3978. }
  3979. finishToken(type, val) {
  3980. if (type !== 133 && type !== 13 && type !== 28) {
  3981. if (this.flowPragma === undefined) {
  3982. this.flowPragma = null;
  3983. }
  3984. }
  3985. super.finishToken(type, val);
  3986. }
  3987. addComment(comment) {
  3988. if (this.flowPragma === undefined) {
  3989. const matches = FLOW_PRAGMA_REGEX.exec(comment.value);
  3990. if (!matches) ;else if (matches[1] === "flow") {
  3991. this.flowPragma = "flow";
  3992. } else if (matches[1] === "noflow") {
  3993. this.flowPragma = "noflow";
  3994. } else {
  3995. throw new Error("Unexpected flow pragma");
  3996. }
  3997. }
  3998. super.addComment(comment);
  3999. }
  4000. flowParseTypeInitialiser(tok) {
  4001. const oldInType = this.state.inType;
  4002. this.state.inType = true;
  4003. this.expect(tok || 14);
  4004. const type = this.flowParseType();
  4005. this.state.inType = oldInType;
  4006. return type;
  4007. }
  4008. flowParsePredicate() {
  4009. const node = this.startNode();
  4010. const moduloLoc = this.state.startLoc;
  4011. this.next();
  4012. this.expectContextual(110);
  4013. if (this.state.lastTokStartLoc.index > moduloLoc.index + 1) {
  4014. this.raise(FlowErrors.UnexpectedSpaceBetweenModuloChecks, moduloLoc);
  4015. }
  4016. if (this.eat(10)) {
  4017. node.value = super.parseExpression();
  4018. this.expect(11);
  4019. return this.finishNode(node, "DeclaredPredicate");
  4020. } else {
  4021. return this.finishNode(node, "InferredPredicate");
  4022. }
  4023. }
  4024. flowParseTypeAndPredicateInitialiser() {
  4025. const oldInType = this.state.inType;
  4026. this.state.inType = true;
  4027. this.expect(14);
  4028. let type = null;
  4029. let predicate = null;
  4030. if (this.match(54)) {
  4031. this.state.inType = oldInType;
  4032. predicate = this.flowParsePredicate();
  4033. } else {
  4034. type = this.flowParseType();
  4035. this.state.inType = oldInType;
  4036. if (this.match(54)) {
  4037. predicate = this.flowParsePredicate();
  4038. }
  4039. }
  4040. return [type, predicate];
  4041. }
  4042. flowParseDeclareClass(node) {
  4043. this.next();
  4044. this.flowParseInterfaceish(node, true);
  4045. return this.finishNode(node, "DeclareClass");
  4046. }
  4047. flowParseDeclareFunction(node) {
  4048. this.next();
  4049. const id = node.id = this.parseIdentifier();
  4050. const typeNode = this.startNode();
  4051. const typeContainer = this.startNode();
  4052. if (this.match(47)) {
  4053. typeNode.typeParameters = this.flowParseTypeParameterDeclaration();
  4054. } else {
  4055. typeNode.typeParameters = null;
  4056. }
  4057. this.expect(10);
  4058. const tmp = this.flowParseFunctionTypeParams();
  4059. typeNode.params = tmp.params;
  4060. typeNode.rest = tmp.rest;
  4061. typeNode.this = tmp._this;
  4062. this.expect(11);
  4063. [typeNode.returnType, node.predicate] = this.flowParseTypeAndPredicateInitialiser();
  4064. typeContainer.typeAnnotation = this.finishNode(typeNode, "FunctionTypeAnnotation");
  4065. id.typeAnnotation = this.finishNode(typeContainer, "TypeAnnotation");
  4066. this.resetEndLocation(id);
  4067. this.semicolon();
  4068. this.scope.declareName(node.id.name, 2048, node.id.loc.start);
  4069. return this.finishNode(node, "DeclareFunction");
  4070. }
  4071. flowParseDeclare(node, insideModule) {
  4072. if (this.match(80)) {
  4073. return this.flowParseDeclareClass(node);
  4074. } else if (this.match(68)) {
  4075. return this.flowParseDeclareFunction(node);
  4076. } else if (this.match(74)) {
  4077. return this.flowParseDeclareVariable(node);
  4078. } else if (this.eatContextual(127)) {
  4079. if (this.match(16)) {
  4080. return this.flowParseDeclareModuleExports(node);
  4081. } else {
  4082. if (insideModule) {
  4083. this.raise(FlowErrors.NestedDeclareModule, this.state.lastTokStartLoc);
  4084. }
  4085. return this.flowParseDeclareModule(node);
  4086. }
  4087. } else if (this.isContextual(130)) {
  4088. return this.flowParseDeclareTypeAlias(node);
  4089. } else if (this.isContextual(131)) {
  4090. return this.flowParseDeclareOpaqueType(node);
  4091. } else if (this.isContextual(129)) {
  4092. return this.flowParseDeclareInterface(node);
  4093. } else if (this.match(82)) {
  4094. return this.flowParseDeclareExportDeclaration(node, insideModule);
  4095. } else {
  4096. this.unexpected();
  4097. }
  4098. }
  4099. flowParseDeclareVariable(node) {
  4100. this.next();
  4101. node.id = this.flowParseTypeAnnotatableIdentifier(true);
  4102. this.scope.declareName(node.id.name, 5, node.id.loc.start);
  4103. this.semicolon();
  4104. return this.finishNode(node, "DeclareVariable");
  4105. }
  4106. flowParseDeclareModule(node) {
  4107. this.scope.enter(0);
  4108. if (this.match(133)) {
  4109. node.id = super.parseExprAtom();
  4110. } else {
  4111. node.id = this.parseIdentifier();
  4112. }
  4113. const bodyNode = node.body = this.startNode();
  4114. const body = bodyNode.body = [];
  4115. this.expect(5);
  4116. while (!this.match(8)) {
  4117. let bodyNode = this.startNode();
  4118. if (this.match(83)) {
  4119. this.next();
  4120. if (!this.isContextual(130) && !this.match(87)) {
  4121. this.raise(FlowErrors.InvalidNonTypeImportInDeclareModule, this.state.lastTokStartLoc);
  4122. }
  4123. super.parseImport(bodyNode);
  4124. } else {
  4125. this.expectContextual(125, FlowErrors.UnsupportedStatementInDeclareModule);
  4126. bodyNode = this.flowParseDeclare(bodyNode, true);
  4127. }
  4128. body.push(bodyNode);
  4129. }
  4130. this.scope.exit();
  4131. this.expect(8);
  4132. this.finishNode(bodyNode, "BlockStatement");
  4133. let kind = null;
  4134. let hasModuleExport = false;
  4135. body.forEach(bodyElement => {
  4136. if (isEsModuleType(bodyElement)) {
  4137. if (kind === "CommonJS") {
  4138. this.raise(FlowErrors.AmbiguousDeclareModuleKind, bodyElement);
  4139. }
  4140. kind = "ES";
  4141. } else if (bodyElement.type === "DeclareModuleExports") {
  4142. if (hasModuleExport) {
  4143. this.raise(FlowErrors.DuplicateDeclareModuleExports, bodyElement);
  4144. }
  4145. if (kind === "ES") {
  4146. this.raise(FlowErrors.AmbiguousDeclareModuleKind, bodyElement);
  4147. }
  4148. kind = "CommonJS";
  4149. hasModuleExport = true;
  4150. }
  4151. });
  4152. node.kind = kind || "CommonJS";
  4153. return this.finishNode(node, "DeclareModule");
  4154. }
  4155. flowParseDeclareExportDeclaration(node, insideModule) {
  4156. this.expect(82);
  4157. if (this.eat(65)) {
  4158. if (this.match(68) || this.match(80)) {
  4159. node.declaration = this.flowParseDeclare(this.startNode());
  4160. } else {
  4161. node.declaration = this.flowParseType();
  4162. this.semicolon();
  4163. }
  4164. node.default = true;
  4165. return this.finishNode(node, "DeclareExportDeclaration");
  4166. } else {
  4167. if (this.match(75) || this.isLet() || (this.isContextual(130) || this.isContextual(129)) && !insideModule) {
  4168. const label = this.state.value;
  4169. throw this.raise(FlowErrors.UnsupportedDeclareExportKind, this.state.startLoc, {
  4170. unsupportedExportKind: label,
  4171. suggestion: exportSuggestions[label]
  4172. });
  4173. }
  4174. if (this.match(74) || this.match(68) || this.match(80) || this.isContextual(131)) {
  4175. node.declaration = this.flowParseDeclare(this.startNode());
  4176. node.default = false;
  4177. return this.finishNode(node, "DeclareExportDeclaration");
  4178. } else if (this.match(55) || this.match(5) || this.isContextual(129) || this.isContextual(130) || this.isContextual(131)) {
  4179. node = this.parseExport(node, null);
  4180. if (node.type === "ExportNamedDeclaration") {
  4181. node.type = "ExportDeclaration";
  4182. node.default = false;
  4183. delete node.exportKind;
  4184. }
  4185. node.type = "Declare" + node.type;
  4186. return node;
  4187. }
  4188. }
  4189. this.unexpected();
  4190. }
  4191. flowParseDeclareModuleExports(node) {
  4192. this.next();
  4193. this.expectContextual(111);
  4194. node.typeAnnotation = this.flowParseTypeAnnotation();
  4195. this.semicolon();
  4196. return this.finishNode(node, "DeclareModuleExports");
  4197. }
  4198. flowParseDeclareTypeAlias(node) {
  4199. this.next();
  4200. const finished = this.flowParseTypeAlias(node);
  4201. finished.type = "DeclareTypeAlias";
  4202. return finished;
  4203. }
  4204. flowParseDeclareOpaqueType(node) {
  4205. this.next();
  4206. const finished = this.flowParseOpaqueType(node, true);
  4207. finished.type = "DeclareOpaqueType";
  4208. return finished;
  4209. }
  4210. flowParseDeclareInterface(node) {
  4211. this.next();
  4212. this.flowParseInterfaceish(node, false);
  4213. return this.finishNode(node, "DeclareInterface");
  4214. }
  4215. flowParseInterfaceish(node, isClass) {
  4216. node.id = this.flowParseRestrictedIdentifier(!isClass, true);
  4217. this.scope.declareName(node.id.name, isClass ? 17 : 8201, node.id.loc.start);
  4218. if (this.match(47)) {
  4219. node.typeParameters = this.flowParseTypeParameterDeclaration();
  4220. } else {
  4221. node.typeParameters = null;
  4222. }
  4223. node.extends = [];
  4224. if (this.eat(81)) {
  4225. do {
  4226. node.extends.push(this.flowParseInterfaceExtends());
  4227. } while (!isClass && this.eat(12));
  4228. }
  4229. if (isClass) {
  4230. node.implements = [];
  4231. node.mixins = [];
  4232. if (this.eatContextual(117)) {
  4233. do {
  4234. node.mixins.push(this.flowParseInterfaceExtends());
  4235. } while (this.eat(12));
  4236. }
  4237. if (this.eatContextual(113)) {
  4238. do {
  4239. node.implements.push(this.flowParseInterfaceExtends());
  4240. } while (this.eat(12));
  4241. }
  4242. }
  4243. node.body = this.flowParseObjectType({
  4244. allowStatic: isClass,
  4245. allowExact: false,
  4246. allowSpread: false,
  4247. allowProto: isClass,
  4248. allowInexact: false
  4249. });
  4250. }
  4251. flowParseInterfaceExtends() {
  4252. const node = this.startNode();
  4253. node.id = this.flowParseQualifiedTypeIdentifier();
  4254. if (this.match(47)) {
  4255. node.typeParameters = this.flowParseTypeParameterInstantiation();
  4256. } else {
  4257. node.typeParameters = null;
  4258. }
  4259. return this.finishNode(node, "InterfaceExtends");
  4260. }
  4261. flowParseInterface(node) {
  4262. this.flowParseInterfaceish(node, false);
  4263. return this.finishNode(node, "InterfaceDeclaration");
  4264. }
  4265. checkNotUnderscore(word) {
  4266. if (word === "_") {
  4267. this.raise(FlowErrors.UnexpectedReservedUnderscore, this.state.startLoc);
  4268. }
  4269. }
  4270. checkReservedType(word, startLoc, declaration) {
  4271. if (!reservedTypes.has(word)) return;
  4272. this.raise(declaration ? FlowErrors.AssignReservedType : FlowErrors.UnexpectedReservedType, startLoc, {
  4273. reservedType: word
  4274. });
  4275. }
  4276. flowParseRestrictedIdentifier(liberal, declaration) {
  4277. this.checkReservedType(this.state.value, this.state.startLoc, declaration);
  4278. return this.parseIdentifier(liberal);
  4279. }
  4280. flowParseTypeAlias(node) {
  4281. node.id = this.flowParseRestrictedIdentifier(false, true);
  4282. this.scope.declareName(node.id.name, 8201, node.id.loc.start);
  4283. if (this.match(47)) {
  4284. node.typeParameters = this.flowParseTypeParameterDeclaration();
  4285. } else {
  4286. node.typeParameters = null;
  4287. }
  4288. node.right = this.flowParseTypeInitialiser(29);
  4289. this.semicolon();
  4290. return this.finishNode(node, "TypeAlias");
  4291. }
  4292. flowParseOpaqueType(node, declare) {
  4293. this.expectContextual(130);
  4294. node.id = this.flowParseRestrictedIdentifier(true, true);
  4295. this.scope.declareName(node.id.name, 8201, node.id.loc.start);
  4296. if (this.match(47)) {
  4297. node.typeParameters = this.flowParseTypeParameterDeclaration();
  4298. } else {
  4299. node.typeParameters = null;
  4300. }
  4301. node.supertype = null;
  4302. if (this.match(14)) {
  4303. node.supertype = this.flowParseTypeInitialiser(14);
  4304. }
  4305. node.impltype = null;
  4306. if (!declare) {
  4307. node.impltype = this.flowParseTypeInitialiser(29);
  4308. }
  4309. this.semicolon();
  4310. return this.finishNode(node, "OpaqueType");
  4311. }
  4312. flowParseTypeParameter(requireDefault = false) {
  4313. const nodeStartLoc = this.state.startLoc;
  4314. const node = this.startNode();
  4315. const variance = this.flowParseVariance();
  4316. const ident = this.flowParseTypeAnnotatableIdentifier();
  4317. node.name = ident.name;
  4318. node.variance = variance;
  4319. node.bound = ident.typeAnnotation;
  4320. if (this.match(29)) {
  4321. this.eat(29);
  4322. node.default = this.flowParseType();
  4323. } else {
  4324. if (requireDefault) {
  4325. this.raise(FlowErrors.MissingTypeParamDefault, nodeStartLoc);
  4326. }
  4327. }
  4328. return this.finishNode(node, "TypeParameter");
  4329. }
  4330. flowParseTypeParameterDeclaration() {
  4331. const oldInType = this.state.inType;
  4332. const node = this.startNode();
  4333. node.params = [];
  4334. this.state.inType = true;
  4335. if (this.match(47) || this.match(142)) {
  4336. this.next();
  4337. } else {
  4338. this.unexpected();
  4339. }
  4340. let defaultRequired = false;
  4341. do {
  4342. const typeParameter = this.flowParseTypeParameter(defaultRequired);
  4343. node.params.push(typeParameter);
  4344. if (typeParameter.default) {
  4345. defaultRequired = true;
  4346. }
  4347. if (!this.match(48)) {
  4348. this.expect(12);
  4349. }
  4350. } while (!this.match(48));
  4351. this.expect(48);
  4352. this.state.inType = oldInType;
  4353. return this.finishNode(node, "TypeParameterDeclaration");
  4354. }
  4355. flowParseTypeParameterInstantiation() {
  4356. const node = this.startNode();
  4357. const oldInType = this.state.inType;
  4358. node.params = [];
  4359. this.state.inType = true;
  4360. this.expect(47);
  4361. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  4362. this.state.noAnonFunctionType = false;
  4363. while (!this.match(48)) {
  4364. node.params.push(this.flowParseType());
  4365. if (!this.match(48)) {
  4366. this.expect(12);
  4367. }
  4368. }
  4369. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  4370. this.expect(48);
  4371. this.state.inType = oldInType;
  4372. return this.finishNode(node, "TypeParameterInstantiation");
  4373. }
  4374. flowParseTypeParameterInstantiationCallOrNew() {
  4375. const node = this.startNode();
  4376. const oldInType = this.state.inType;
  4377. node.params = [];
  4378. this.state.inType = true;
  4379. this.expect(47);
  4380. while (!this.match(48)) {
  4381. node.params.push(this.flowParseTypeOrImplicitInstantiation());
  4382. if (!this.match(48)) {
  4383. this.expect(12);
  4384. }
  4385. }
  4386. this.expect(48);
  4387. this.state.inType = oldInType;
  4388. return this.finishNode(node, "TypeParameterInstantiation");
  4389. }
  4390. flowParseInterfaceType() {
  4391. const node = this.startNode();
  4392. this.expectContextual(129);
  4393. node.extends = [];
  4394. if (this.eat(81)) {
  4395. do {
  4396. node.extends.push(this.flowParseInterfaceExtends());
  4397. } while (this.eat(12));
  4398. }
  4399. node.body = this.flowParseObjectType({
  4400. allowStatic: false,
  4401. allowExact: false,
  4402. allowSpread: false,
  4403. allowProto: false,
  4404. allowInexact: false
  4405. });
  4406. return this.finishNode(node, "InterfaceTypeAnnotation");
  4407. }
  4408. flowParseObjectPropertyKey() {
  4409. return this.match(134) || this.match(133) ? super.parseExprAtom() : this.parseIdentifier(true);
  4410. }
  4411. flowParseObjectTypeIndexer(node, isStatic, variance) {
  4412. node.static = isStatic;
  4413. if (this.lookahead().type === 14) {
  4414. node.id = this.flowParseObjectPropertyKey();
  4415. node.key = this.flowParseTypeInitialiser();
  4416. } else {
  4417. node.id = null;
  4418. node.key = this.flowParseType();
  4419. }
  4420. this.expect(3);
  4421. node.value = this.flowParseTypeInitialiser();
  4422. node.variance = variance;
  4423. return this.finishNode(node, "ObjectTypeIndexer");
  4424. }
  4425. flowParseObjectTypeInternalSlot(node, isStatic) {
  4426. node.static = isStatic;
  4427. node.id = this.flowParseObjectPropertyKey();
  4428. this.expect(3);
  4429. this.expect(3);
  4430. if (this.match(47) || this.match(10)) {
  4431. node.method = true;
  4432. node.optional = false;
  4433. node.value = this.flowParseObjectTypeMethodish(this.startNodeAt(node.loc.start));
  4434. } else {
  4435. node.method = false;
  4436. if (this.eat(17)) {
  4437. node.optional = true;
  4438. }
  4439. node.value = this.flowParseTypeInitialiser();
  4440. }
  4441. return this.finishNode(node, "ObjectTypeInternalSlot");
  4442. }
  4443. flowParseObjectTypeMethodish(node) {
  4444. node.params = [];
  4445. node.rest = null;
  4446. node.typeParameters = null;
  4447. node.this = null;
  4448. if (this.match(47)) {
  4449. node.typeParameters = this.flowParseTypeParameterDeclaration();
  4450. }
  4451. this.expect(10);
  4452. if (this.match(78)) {
  4453. node.this = this.flowParseFunctionTypeParam(true);
  4454. node.this.name = null;
  4455. if (!this.match(11)) {
  4456. this.expect(12);
  4457. }
  4458. }
  4459. while (!this.match(11) && !this.match(21)) {
  4460. node.params.push(this.flowParseFunctionTypeParam(false));
  4461. if (!this.match(11)) {
  4462. this.expect(12);
  4463. }
  4464. }
  4465. if (this.eat(21)) {
  4466. node.rest = this.flowParseFunctionTypeParam(false);
  4467. }
  4468. this.expect(11);
  4469. node.returnType = this.flowParseTypeInitialiser();
  4470. return this.finishNode(node, "FunctionTypeAnnotation");
  4471. }
  4472. flowParseObjectTypeCallProperty(node, isStatic) {
  4473. const valueNode = this.startNode();
  4474. node.static = isStatic;
  4475. node.value = this.flowParseObjectTypeMethodish(valueNode);
  4476. return this.finishNode(node, "ObjectTypeCallProperty");
  4477. }
  4478. flowParseObjectType({
  4479. allowStatic,
  4480. allowExact,
  4481. allowSpread,
  4482. allowProto,
  4483. allowInexact
  4484. }) {
  4485. const oldInType = this.state.inType;
  4486. this.state.inType = true;
  4487. const nodeStart = this.startNode();
  4488. nodeStart.callProperties = [];
  4489. nodeStart.properties = [];
  4490. nodeStart.indexers = [];
  4491. nodeStart.internalSlots = [];
  4492. let endDelim;
  4493. let exact;
  4494. let inexact = false;
  4495. if (allowExact && this.match(6)) {
  4496. this.expect(6);
  4497. endDelim = 9;
  4498. exact = true;
  4499. } else {
  4500. this.expect(5);
  4501. endDelim = 8;
  4502. exact = false;
  4503. }
  4504. nodeStart.exact = exact;
  4505. while (!this.match(endDelim)) {
  4506. let isStatic = false;
  4507. let protoStartLoc = null;
  4508. let inexactStartLoc = null;
  4509. const node = this.startNode();
  4510. if (allowProto && this.isContextual(118)) {
  4511. const lookahead = this.lookahead();
  4512. if (lookahead.type !== 14 && lookahead.type !== 17) {
  4513. this.next();
  4514. protoStartLoc = this.state.startLoc;
  4515. allowStatic = false;
  4516. }
  4517. }
  4518. if (allowStatic && this.isContextual(106)) {
  4519. const lookahead = this.lookahead();
  4520. if (lookahead.type !== 14 && lookahead.type !== 17) {
  4521. this.next();
  4522. isStatic = true;
  4523. }
  4524. }
  4525. const variance = this.flowParseVariance();
  4526. if (this.eat(0)) {
  4527. if (protoStartLoc != null) {
  4528. this.unexpected(protoStartLoc);
  4529. }
  4530. if (this.eat(0)) {
  4531. if (variance) {
  4532. this.unexpected(variance.loc.start);
  4533. }
  4534. nodeStart.internalSlots.push(this.flowParseObjectTypeInternalSlot(node, isStatic));
  4535. } else {
  4536. nodeStart.indexers.push(this.flowParseObjectTypeIndexer(node, isStatic, variance));
  4537. }
  4538. } else if (this.match(10) || this.match(47)) {
  4539. if (protoStartLoc != null) {
  4540. this.unexpected(protoStartLoc);
  4541. }
  4542. if (variance) {
  4543. this.unexpected(variance.loc.start);
  4544. }
  4545. nodeStart.callProperties.push(this.flowParseObjectTypeCallProperty(node, isStatic));
  4546. } else {
  4547. let kind = "init";
  4548. if (this.isContextual(99) || this.isContextual(104)) {
  4549. const lookahead = this.lookahead();
  4550. if (tokenIsLiteralPropertyName(lookahead.type)) {
  4551. kind = this.state.value;
  4552. this.next();
  4553. }
  4554. }
  4555. const propOrInexact = this.flowParseObjectTypeProperty(node, isStatic, protoStartLoc, variance, kind, allowSpread, allowInexact != null ? allowInexact : !exact);
  4556. if (propOrInexact === null) {
  4557. inexact = true;
  4558. inexactStartLoc = this.state.lastTokStartLoc;
  4559. } else {
  4560. nodeStart.properties.push(propOrInexact);
  4561. }
  4562. }
  4563. this.flowObjectTypeSemicolon();
  4564. if (inexactStartLoc && !this.match(8) && !this.match(9)) {
  4565. this.raise(FlowErrors.UnexpectedExplicitInexactInObject, inexactStartLoc);
  4566. }
  4567. }
  4568. this.expect(endDelim);
  4569. if (allowSpread) {
  4570. nodeStart.inexact = inexact;
  4571. }
  4572. const out = this.finishNode(nodeStart, "ObjectTypeAnnotation");
  4573. this.state.inType = oldInType;
  4574. return out;
  4575. }
  4576. flowParseObjectTypeProperty(node, isStatic, protoStartLoc, variance, kind, allowSpread, allowInexact) {
  4577. if (this.eat(21)) {
  4578. const isInexactToken = this.match(12) || this.match(13) || this.match(8) || this.match(9);
  4579. if (isInexactToken) {
  4580. if (!allowSpread) {
  4581. this.raise(FlowErrors.InexactInsideNonObject, this.state.lastTokStartLoc);
  4582. } else if (!allowInexact) {
  4583. this.raise(FlowErrors.InexactInsideExact, this.state.lastTokStartLoc);
  4584. }
  4585. if (variance) {
  4586. this.raise(FlowErrors.InexactVariance, variance);
  4587. }
  4588. return null;
  4589. }
  4590. if (!allowSpread) {
  4591. this.raise(FlowErrors.UnexpectedSpreadType, this.state.lastTokStartLoc);
  4592. }
  4593. if (protoStartLoc != null) {
  4594. this.unexpected(protoStartLoc);
  4595. }
  4596. if (variance) {
  4597. this.raise(FlowErrors.SpreadVariance, variance);
  4598. }
  4599. node.argument = this.flowParseType();
  4600. return this.finishNode(node, "ObjectTypeSpreadProperty");
  4601. } else {
  4602. node.key = this.flowParseObjectPropertyKey();
  4603. node.static = isStatic;
  4604. node.proto = protoStartLoc != null;
  4605. node.kind = kind;
  4606. let optional = false;
  4607. if (this.match(47) || this.match(10)) {
  4608. node.method = true;
  4609. if (protoStartLoc != null) {
  4610. this.unexpected(protoStartLoc);
  4611. }
  4612. if (variance) {
  4613. this.unexpected(variance.loc.start);
  4614. }
  4615. node.value = this.flowParseObjectTypeMethodish(this.startNodeAt(node.loc.start));
  4616. if (kind === "get" || kind === "set") {
  4617. this.flowCheckGetterSetterParams(node);
  4618. }
  4619. if (!allowSpread && node.key.name === "constructor" && node.value.this) {
  4620. this.raise(FlowErrors.ThisParamBannedInConstructor, node.value.this);
  4621. }
  4622. } else {
  4623. if (kind !== "init") this.unexpected();
  4624. node.method = false;
  4625. if (this.eat(17)) {
  4626. optional = true;
  4627. }
  4628. node.value = this.flowParseTypeInitialiser();
  4629. node.variance = variance;
  4630. }
  4631. node.optional = optional;
  4632. return this.finishNode(node, "ObjectTypeProperty");
  4633. }
  4634. }
  4635. flowCheckGetterSetterParams(property) {
  4636. const paramCount = property.kind === "get" ? 0 : 1;
  4637. const length = property.value.params.length + (property.value.rest ? 1 : 0);
  4638. if (property.value.this) {
  4639. this.raise(property.kind === "get" ? FlowErrors.GetterMayNotHaveThisParam : FlowErrors.SetterMayNotHaveThisParam, property.value.this);
  4640. }
  4641. if (length !== paramCount) {
  4642. this.raise(property.kind === "get" ? Errors.BadGetterArity : Errors.BadSetterArity, property);
  4643. }
  4644. if (property.kind === "set" && property.value.rest) {
  4645. this.raise(Errors.BadSetterRestParameter, property);
  4646. }
  4647. }
  4648. flowObjectTypeSemicolon() {
  4649. if (!this.eat(13) && !this.eat(12) && !this.match(8) && !this.match(9)) {
  4650. this.unexpected();
  4651. }
  4652. }
  4653. flowParseQualifiedTypeIdentifier(startLoc, id) {
  4654. var _startLoc;
  4655. (_startLoc = startLoc) != null ? _startLoc : startLoc = this.state.startLoc;
  4656. let node = id || this.flowParseRestrictedIdentifier(true);
  4657. while (this.eat(16)) {
  4658. const node2 = this.startNodeAt(startLoc);
  4659. node2.qualification = node;
  4660. node2.id = this.flowParseRestrictedIdentifier(true);
  4661. node = this.finishNode(node2, "QualifiedTypeIdentifier");
  4662. }
  4663. return node;
  4664. }
  4665. flowParseGenericType(startLoc, id) {
  4666. const node = this.startNodeAt(startLoc);
  4667. node.typeParameters = null;
  4668. node.id = this.flowParseQualifiedTypeIdentifier(startLoc, id);
  4669. if (this.match(47)) {
  4670. node.typeParameters = this.flowParseTypeParameterInstantiation();
  4671. }
  4672. return this.finishNode(node, "GenericTypeAnnotation");
  4673. }
  4674. flowParseTypeofType() {
  4675. const node = this.startNode();
  4676. this.expect(87);
  4677. node.argument = this.flowParsePrimaryType();
  4678. return this.finishNode(node, "TypeofTypeAnnotation");
  4679. }
  4680. flowParseTupleType() {
  4681. const node = this.startNode();
  4682. node.types = [];
  4683. this.expect(0);
  4684. while (this.state.pos < this.length && !this.match(3)) {
  4685. node.types.push(this.flowParseType());
  4686. if (this.match(3)) break;
  4687. this.expect(12);
  4688. }
  4689. this.expect(3);
  4690. return this.finishNode(node, "TupleTypeAnnotation");
  4691. }
  4692. flowParseFunctionTypeParam(first) {
  4693. let name = null;
  4694. let optional = false;
  4695. let typeAnnotation = null;
  4696. const node = this.startNode();
  4697. const lh = this.lookahead();
  4698. const isThis = this.state.type === 78;
  4699. if (lh.type === 14 || lh.type === 17) {
  4700. if (isThis && !first) {
  4701. this.raise(FlowErrors.ThisParamMustBeFirst, node);
  4702. }
  4703. name = this.parseIdentifier(isThis);
  4704. if (this.eat(17)) {
  4705. optional = true;
  4706. if (isThis) {
  4707. this.raise(FlowErrors.ThisParamMayNotBeOptional, node);
  4708. }
  4709. }
  4710. typeAnnotation = this.flowParseTypeInitialiser();
  4711. } else {
  4712. typeAnnotation = this.flowParseType();
  4713. }
  4714. node.name = name;
  4715. node.optional = optional;
  4716. node.typeAnnotation = typeAnnotation;
  4717. return this.finishNode(node, "FunctionTypeParam");
  4718. }
  4719. reinterpretTypeAsFunctionTypeParam(type) {
  4720. const node = this.startNodeAt(type.loc.start);
  4721. node.name = null;
  4722. node.optional = false;
  4723. node.typeAnnotation = type;
  4724. return this.finishNode(node, "FunctionTypeParam");
  4725. }
  4726. flowParseFunctionTypeParams(params = []) {
  4727. let rest = null;
  4728. let _this = null;
  4729. if (this.match(78)) {
  4730. _this = this.flowParseFunctionTypeParam(true);
  4731. _this.name = null;
  4732. if (!this.match(11)) {
  4733. this.expect(12);
  4734. }
  4735. }
  4736. while (!this.match(11) && !this.match(21)) {
  4737. params.push(this.flowParseFunctionTypeParam(false));
  4738. if (!this.match(11)) {
  4739. this.expect(12);
  4740. }
  4741. }
  4742. if (this.eat(21)) {
  4743. rest = this.flowParseFunctionTypeParam(false);
  4744. }
  4745. return {
  4746. params,
  4747. rest,
  4748. _this
  4749. };
  4750. }
  4751. flowIdentToTypeAnnotation(startLoc, node, id) {
  4752. switch (id.name) {
  4753. case "any":
  4754. return this.finishNode(node, "AnyTypeAnnotation");
  4755. case "bool":
  4756. case "boolean":
  4757. return this.finishNode(node, "BooleanTypeAnnotation");
  4758. case "mixed":
  4759. return this.finishNode(node, "MixedTypeAnnotation");
  4760. case "empty":
  4761. return this.finishNode(node, "EmptyTypeAnnotation");
  4762. case "number":
  4763. return this.finishNode(node, "NumberTypeAnnotation");
  4764. case "string":
  4765. return this.finishNode(node, "StringTypeAnnotation");
  4766. case "symbol":
  4767. return this.finishNode(node, "SymbolTypeAnnotation");
  4768. default:
  4769. this.checkNotUnderscore(id.name);
  4770. return this.flowParseGenericType(startLoc, id);
  4771. }
  4772. }
  4773. flowParsePrimaryType() {
  4774. const startLoc = this.state.startLoc;
  4775. const node = this.startNode();
  4776. let tmp;
  4777. let type;
  4778. let isGroupedType = false;
  4779. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  4780. switch (this.state.type) {
  4781. case 5:
  4782. return this.flowParseObjectType({
  4783. allowStatic: false,
  4784. allowExact: false,
  4785. allowSpread: true,
  4786. allowProto: false,
  4787. allowInexact: true
  4788. });
  4789. case 6:
  4790. return this.flowParseObjectType({
  4791. allowStatic: false,
  4792. allowExact: true,
  4793. allowSpread: true,
  4794. allowProto: false,
  4795. allowInexact: false
  4796. });
  4797. case 0:
  4798. this.state.noAnonFunctionType = false;
  4799. type = this.flowParseTupleType();
  4800. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  4801. return type;
  4802. case 47:
  4803. {
  4804. const node = this.startNode();
  4805. node.typeParameters = this.flowParseTypeParameterDeclaration();
  4806. this.expect(10);
  4807. tmp = this.flowParseFunctionTypeParams();
  4808. node.params = tmp.params;
  4809. node.rest = tmp.rest;
  4810. node.this = tmp._this;
  4811. this.expect(11);
  4812. this.expect(19);
  4813. node.returnType = this.flowParseType();
  4814. return this.finishNode(node, "FunctionTypeAnnotation");
  4815. }
  4816. case 10:
  4817. {
  4818. const node = this.startNode();
  4819. this.next();
  4820. if (!this.match(11) && !this.match(21)) {
  4821. if (tokenIsIdentifier(this.state.type) || this.match(78)) {
  4822. const token = this.lookahead().type;
  4823. isGroupedType = token !== 17 && token !== 14;
  4824. } else {
  4825. isGroupedType = true;
  4826. }
  4827. }
  4828. if (isGroupedType) {
  4829. this.state.noAnonFunctionType = false;
  4830. type = this.flowParseType();
  4831. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  4832. if (this.state.noAnonFunctionType || !(this.match(12) || this.match(11) && this.lookahead().type === 19)) {
  4833. this.expect(11);
  4834. return type;
  4835. } else {
  4836. this.eat(12);
  4837. }
  4838. }
  4839. if (type) {
  4840. tmp = this.flowParseFunctionTypeParams([this.reinterpretTypeAsFunctionTypeParam(type)]);
  4841. } else {
  4842. tmp = this.flowParseFunctionTypeParams();
  4843. }
  4844. node.params = tmp.params;
  4845. node.rest = tmp.rest;
  4846. node.this = tmp._this;
  4847. this.expect(11);
  4848. this.expect(19);
  4849. node.returnType = this.flowParseType();
  4850. node.typeParameters = null;
  4851. return this.finishNode(node, "FunctionTypeAnnotation");
  4852. }
  4853. case 133:
  4854. return this.parseLiteral(this.state.value, "StringLiteralTypeAnnotation");
  4855. case 85:
  4856. case 86:
  4857. node.value = this.match(85);
  4858. this.next();
  4859. return this.finishNode(node, "BooleanLiteralTypeAnnotation");
  4860. case 53:
  4861. if (this.state.value === "-") {
  4862. this.next();
  4863. if (this.match(134)) {
  4864. return this.parseLiteralAtNode(-this.state.value, "NumberLiteralTypeAnnotation", node);
  4865. }
  4866. if (this.match(135)) {
  4867. return this.parseLiteralAtNode(-this.state.value, "BigIntLiteralTypeAnnotation", node);
  4868. }
  4869. throw this.raise(FlowErrors.UnexpectedSubtractionOperand, this.state.startLoc);
  4870. }
  4871. this.unexpected();
  4872. return;
  4873. case 134:
  4874. return this.parseLiteral(this.state.value, "NumberLiteralTypeAnnotation");
  4875. case 135:
  4876. return this.parseLiteral(this.state.value, "BigIntLiteralTypeAnnotation");
  4877. case 88:
  4878. this.next();
  4879. return this.finishNode(node, "VoidTypeAnnotation");
  4880. case 84:
  4881. this.next();
  4882. return this.finishNode(node, "NullLiteralTypeAnnotation");
  4883. case 78:
  4884. this.next();
  4885. return this.finishNode(node, "ThisTypeAnnotation");
  4886. case 55:
  4887. this.next();
  4888. return this.finishNode(node, "ExistsTypeAnnotation");
  4889. case 87:
  4890. return this.flowParseTypeofType();
  4891. default:
  4892. if (tokenIsKeyword(this.state.type)) {
  4893. const label = tokenLabelName(this.state.type);
  4894. this.next();
  4895. return super.createIdentifier(node, label);
  4896. } else if (tokenIsIdentifier(this.state.type)) {
  4897. if (this.isContextual(129)) {
  4898. return this.flowParseInterfaceType();
  4899. }
  4900. return this.flowIdentToTypeAnnotation(startLoc, node, this.parseIdentifier());
  4901. }
  4902. }
  4903. this.unexpected();
  4904. }
  4905. flowParsePostfixType() {
  4906. const startLoc = this.state.startLoc;
  4907. let type = this.flowParsePrimaryType();
  4908. let seenOptionalIndexedAccess = false;
  4909. while ((this.match(0) || this.match(18)) && !this.canInsertSemicolon()) {
  4910. const node = this.startNodeAt(startLoc);
  4911. const optional = this.eat(18);
  4912. seenOptionalIndexedAccess = seenOptionalIndexedAccess || optional;
  4913. this.expect(0);
  4914. if (!optional && this.match(3)) {
  4915. node.elementType = type;
  4916. this.next();
  4917. type = this.finishNode(node, "ArrayTypeAnnotation");
  4918. } else {
  4919. node.objectType = type;
  4920. node.indexType = this.flowParseType();
  4921. this.expect(3);
  4922. if (seenOptionalIndexedAccess) {
  4923. node.optional = optional;
  4924. type = this.finishNode(node, "OptionalIndexedAccessType");
  4925. } else {
  4926. type = this.finishNode(node, "IndexedAccessType");
  4927. }
  4928. }
  4929. }
  4930. return type;
  4931. }
  4932. flowParsePrefixType() {
  4933. const node = this.startNode();
  4934. if (this.eat(17)) {
  4935. node.typeAnnotation = this.flowParsePrefixType();
  4936. return this.finishNode(node, "NullableTypeAnnotation");
  4937. } else {
  4938. return this.flowParsePostfixType();
  4939. }
  4940. }
  4941. flowParseAnonFunctionWithoutParens() {
  4942. const param = this.flowParsePrefixType();
  4943. if (!this.state.noAnonFunctionType && this.eat(19)) {
  4944. const node = this.startNodeAt(param.loc.start);
  4945. node.params = [this.reinterpretTypeAsFunctionTypeParam(param)];
  4946. node.rest = null;
  4947. node.this = null;
  4948. node.returnType = this.flowParseType();
  4949. node.typeParameters = null;
  4950. return this.finishNode(node, "FunctionTypeAnnotation");
  4951. }
  4952. return param;
  4953. }
  4954. flowParseIntersectionType() {
  4955. const node = this.startNode();
  4956. this.eat(45);
  4957. const type = this.flowParseAnonFunctionWithoutParens();
  4958. node.types = [type];
  4959. while (this.eat(45)) {
  4960. node.types.push(this.flowParseAnonFunctionWithoutParens());
  4961. }
  4962. return node.types.length === 1 ? type : this.finishNode(node, "IntersectionTypeAnnotation");
  4963. }
  4964. flowParseUnionType() {
  4965. const node = this.startNode();
  4966. this.eat(43);
  4967. const type = this.flowParseIntersectionType();
  4968. node.types = [type];
  4969. while (this.eat(43)) {
  4970. node.types.push(this.flowParseIntersectionType());
  4971. }
  4972. return node.types.length === 1 ? type : this.finishNode(node, "UnionTypeAnnotation");
  4973. }
  4974. flowParseType() {
  4975. const oldInType = this.state.inType;
  4976. this.state.inType = true;
  4977. const type = this.flowParseUnionType();
  4978. this.state.inType = oldInType;
  4979. return type;
  4980. }
  4981. flowParseTypeOrImplicitInstantiation() {
  4982. if (this.state.type === 132 && this.state.value === "_") {
  4983. const startLoc = this.state.startLoc;
  4984. const node = this.parseIdentifier();
  4985. return this.flowParseGenericType(startLoc, node);
  4986. } else {
  4987. return this.flowParseType();
  4988. }
  4989. }
  4990. flowParseTypeAnnotation() {
  4991. const node = this.startNode();
  4992. node.typeAnnotation = this.flowParseTypeInitialiser();
  4993. return this.finishNode(node, "TypeAnnotation");
  4994. }
  4995. flowParseTypeAnnotatableIdentifier(allowPrimitiveOverride) {
  4996. const ident = allowPrimitiveOverride ? this.parseIdentifier() : this.flowParseRestrictedIdentifier();
  4997. if (this.match(14)) {
  4998. ident.typeAnnotation = this.flowParseTypeAnnotation();
  4999. this.resetEndLocation(ident);
  5000. }
  5001. return ident;
  5002. }
  5003. typeCastToParameter(node) {
  5004. node.expression.typeAnnotation = node.typeAnnotation;
  5005. this.resetEndLocation(node.expression, node.typeAnnotation.loc.end);
  5006. return node.expression;
  5007. }
  5008. flowParseVariance() {
  5009. let variance = null;
  5010. if (this.match(53)) {
  5011. variance = this.startNode();
  5012. if (this.state.value === "+") {
  5013. variance.kind = "plus";
  5014. } else {
  5015. variance.kind = "minus";
  5016. }
  5017. this.next();
  5018. return this.finishNode(variance, "Variance");
  5019. }
  5020. return variance;
  5021. }
  5022. parseFunctionBody(node, allowExpressionBody, isMethod = false) {
  5023. if (allowExpressionBody) {
  5024. this.forwardNoArrowParamsConversionAt(node, () => super.parseFunctionBody(node, true, isMethod));
  5025. return;
  5026. }
  5027. super.parseFunctionBody(node, false, isMethod);
  5028. }
  5029. parseFunctionBodyAndFinish(node, type, isMethod = false) {
  5030. if (this.match(14)) {
  5031. const typeNode = this.startNode();
  5032. [typeNode.typeAnnotation, node.predicate] = this.flowParseTypeAndPredicateInitialiser();
  5033. node.returnType = typeNode.typeAnnotation ? this.finishNode(typeNode, "TypeAnnotation") : null;
  5034. }
  5035. return super.parseFunctionBodyAndFinish(node, type, isMethod);
  5036. }
  5037. parseStatementLike(flags) {
  5038. if (this.state.strict && this.isContextual(129)) {
  5039. const lookahead = this.lookahead();
  5040. if (tokenIsKeywordOrIdentifier(lookahead.type)) {
  5041. const node = this.startNode();
  5042. this.next();
  5043. return this.flowParseInterface(node);
  5044. }
  5045. } else if (this.shouldParseEnums() && this.isContextual(126)) {
  5046. const node = this.startNode();
  5047. this.next();
  5048. return this.flowParseEnumDeclaration(node);
  5049. }
  5050. const stmt = super.parseStatementLike(flags);
  5051. if (this.flowPragma === undefined && !this.isValidDirective(stmt)) {
  5052. this.flowPragma = null;
  5053. }
  5054. return stmt;
  5055. }
  5056. parseExpressionStatement(node, expr, decorators) {
  5057. if (expr.type === "Identifier") {
  5058. if (expr.name === "declare") {
  5059. if (this.match(80) || tokenIsIdentifier(this.state.type) || this.match(68) || this.match(74) || this.match(82)) {
  5060. return this.flowParseDeclare(node);
  5061. }
  5062. } else if (tokenIsIdentifier(this.state.type)) {
  5063. if (expr.name === "interface") {
  5064. return this.flowParseInterface(node);
  5065. } else if (expr.name === "type") {
  5066. return this.flowParseTypeAlias(node);
  5067. } else if (expr.name === "opaque") {
  5068. return this.flowParseOpaqueType(node, false);
  5069. }
  5070. }
  5071. }
  5072. return super.parseExpressionStatement(node, expr, decorators);
  5073. }
  5074. shouldParseExportDeclaration() {
  5075. const {
  5076. type
  5077. } = this.state;
  5078. if (tokenIsFlowInterfaceOrTypeOrOpaque(type) || this.shouldParseEnums() && type === 126) {
  5079. return !this.state.containsEsc;
  5080. }
  5081. return super.shouldParseExportDeclaration();
  5082. }
  5083. isExportDefaultSpecifier() {
  5084. const {
  5085. type
  5086. } = this.state;
  5087. if (tokenIsFlowInterfaceOrTypeOrOpaque(type) || this.shouldParseEnums() && type === 126) {
  5088. return this.state.containsEsc;
  5089. }
  5090. return super.isExportDefaultSpecifier();
  5091. }
  5092. parseExportDefaultExpression() {
  5093. if (this.shouldParseEnums() && this.isContextual(126)) {
  5094. const node = this.startNode();
  5095. this.next();
  5096. return this.flowParseEnumDeclaration(node);
  5097. }
  5098. return super.parseExportDefaultExpression();
  5099. }
  5100. parseConditional(expr, startLoc, refExpressionErrors) {
  5101. if (!this.match(17)) return expr;
  5102. if (this.state.maybeInArrowParameters) {
  5103. const nextCh = this.lookaheadCharCode();
  5104. if (nextCh === 44 || nextCh === 61 || nextCh === 58 || nextCh === 41) {
  5105. this.setOptionalParametersError(refExpressionErrors);
  5106. return expr;
  5107. }
  5108. }
  5109. this.expect(17);
  5110. const state = this.state.clone();
  5111. const originalNoArrowAt = this.state.noArrowAt;
  5112. const node = this.startNodeAt(startLoc);
  5113. let {
  5114. consequent,
  5115. failed
  5116. } = this.tryParseConditionalConsequent();
  5117. let [valid, invalid] = this.getArrowLikeExpressions(consequent);
  5118. if (failed || invalid.length > 0) {
  5119. const noArrowAt = [...originalNoArrowAt];
  5120. if (invalid.length > 0) {
  5121. this.state = state;
  5122. this.state.noArrowAt = noArrowAt;
  5123. for (let i = 0; i < invalid.length; i++) {
  5124. noArrowAt.push(invalid[i].start);
  5125. }
  5126. ({
  5127. consequent,
  5128. failed
  5129. } = this.tryParseConditionalConsequent());
  5130. [valid, invalid] = this.getArrowLikeExpressions(consequent);
  5131. }
  5132. if (failed && valid.length > 1) {
  5133. this.raise(FlowErrors.AmbiguousConditionalArrow, state.startLoc);
  5134. }
  5135. if (failed && valid.length === 1) {
  5136. this.state = state;
  5137. noArrowAt.push(valid[0].start);
  5138. this.state.noArrowAt = noArrowAt;
  5139. ({
  5140. consequent,
  5141. failed
  5142. } = this.tryParseConditionalConsequent());
  5143. }
  5144. }
  5145. this.getArrowLikeExpressions(consequent, true);
  5146. this.state.noArrowAt = originalNoArrowAt;
  5147. this.expect(14);
  5148. node.test = expr;
  5149. node.consequent = consequent;
  5150. node.alternate = this.forwardNoArrowParamsConversionAt(node, () => this.parseMaybeAssign(undefined, undefined));
  5151. return this.finishNode(node, "ConditionalExpression");
  5152. }
  5153. tryParseConditionalConsequent() {
  5154. this.state.noArrowParamsConversionAt.push(this.state.start);
  5155. const consequent = this.parseMaybeAssignAllowIn();
  5156. const failed = !this.match(14);
  5157. this.state.noArrowParamsConversionAt.pop();
  5158. return {
  5159. consequent,
  5160. failed
  5161. };
  5162. }
  5163. getArrowLikeExpressions(node, disallowInvalid) {
  5164. const stack = [node];
  5165. const arrows = [];
  5166. while (stack.length !== 0) {
  5167. const node = stack.pop();
  5168. if (node.type === "ArrowFunctionExpression" && node.body.type !== "BlockStatement") {
  5169. if (node.typeParameters || !node.returnType) {
  5170. this.finishArrowValidation(node);
  5171. } else {
  5172. arrows.push(node);
  5173. }
  5174. stack.push(node.body);
  5175. } else if (node.type === "ConditionalExpression") {
  5176. stack.push(node.consequent);
  5177. stack.push(node.alternate);
  5178. }
  5179. }
  5180. if (disallowInvalid) {
  5181. arrows.forEach(node => this.finishArrowValidation(node));
  5182. return [arrows, []];
  5183. }
  5184. return partition(arrows, node => node.params.every(param => this.isAssignable(param, true)));
  5185. }
  5186. finishArrowValidation(node) {
  5187. var _node$extra;
  5188. this.toAssignableList(node.params, (_node$extra = node.extra) == null ? void 0 : _node$extra.trailingCommaLoc, false);
  5189. this.scope.enter(2 | 4);
  5190. super.checkParams(node, false, true);
  5191. this.scope.exit();
  5192. }
  5193. forwardNoArrowParamsConversionAt(node, parse) {
  5194. let result;
  5195. if (this.state.noArrowParamsConversionAt.includes(node.start)) {
  5196. this.state.noArrowParamsConversionAt.push(this.state.start);
  5197. result = parse();
  5198. this.state.noArrowParamsConversionAt.pop();
  5199. } else {
  5200. result = parse();
  5201. }
  5202. return result;
  5203. }
  5204. parseParenItem(node, startLoc) {
  5205. const newNode = super.parseParenItem(node, startLoc);
  5206. if (this.eat(17)) {
  5207. newNode.optional = true;
  5208. this.resetEndLocation(node);
  5209. }
  5210. if (this.match(14)) {
  5211. const typeCastNode = this.startNodeAt(startLoc);
  5212. typeCastNode.expression = newNode;
  5213. typeCastNode.typeAnnotation = this.flowParseTypeAnnotation();
  5214. return this.finishNode(typeCastNode, "TypeCastExpression");
  5215. }
  5216. return newNode;
  5217. }
  5218. assertModuleNodeAllowed(node) {
  5219. if (node.type === "ImportDeclaration" && (node.importKind === "type" || node.importKind === "typeof") || node.type === "ExportNamedDeclaration" && node.exportKind === "type" || node.type === "ExportAllDeclaration" && node.exportKind === "type") {
  5220. return;
  5221. }
  5222. super.assertModuleNodeAllowed(node);
  5223. }
  5224. parseExportDeclaration(node) {
  5225. if (this.isContextual(130)) {
  5226. node.exportKind = "type";
  5227. const declarationNode = this.startNode();
  5228. this.next();
  5229. if (this.match(5)) {
  5230. node.specifiers = this.parseExportSpecifiers(true);
  5231. super.parseExportFrom(node);
  5232. return null;
  5233. } else {
  5234. return this.flowParseTypeAlias(declarationNode);
  5235. }
  5236. } else if (this.isContextual(131)) {
  5237. node.exportKind = "type";
  5238. const declarationNode = this.startNode();
  5239. this.next();
  5240. return this.flowParseOpaqueType(declarationNode, false);
  5241. } else if (this.isContextual(129)) {
  5242. node.exportKind = "type";
  5243. const declarationNode = this.startNode();
  5244. this.next();
  5245. return this.flowParseInterface(declarationNode);
  5246. } else if (this.shouldParseEnums() && this.isContextual(126)) {
  5247. node.exportKind = "value";
  5248. const declarationNode = this.startNode();
  5249. this.next();
  5250. return this.flowParseEnumDeclaration(declarationNode);
  5251. } else {
  5252. return super.parseExportDeclaration(node);
  5253. }
  5254. }
  5255. eatExportStar(node) {
  5256. if (super.eatExportStar(node)) return true;
  5257. if (this.isContextual(130) && this.lookahead().type === 55) {
  5258. node.exportKind = "type";
  5259. this.next();
  5260. this.next();
  5261. return true;
  5262. }
  5263. return false;
  5264. }
  5265. maybeParseExportNamespaceSpecifier(node) {
  5266. const {
  5267. startLoc
  5268. } = this.state;
  5269. const hasNamespace = super.maybeParseExportNamespaceSpecifier(node);
  5270. if (hasNamespace && node.exportKind === "type") {
  5271. this.unexpected(startLoc);
  5272. }
  5273. return hasNamespace;
  5274. }
  5275. parseClassId(node, isStatement, optionalId) {
  5276. super.parseClassId(node, isStatement, optionalId);
  5277. if (this.match(47)) {
  5278. node.typeParameters = this.flowParseTypeParameterDeclaration();
  5279. }
  5280. }
  5281. parseClassMember(classBody, member, state) {
  5282. const {
  5283. startLoc
  5284. } = this.state;
  5285. if (this.isContextual(125)) {
  5286. if (super.parseClassMemberFromModifier(classBody, member)) {
  5287. return;
  5288. }
  5289. member.declare = true;
  5290. }
  5291. super.parseClassMember(classBody, member, state);
  5292. if (member.declare) {
  5293. if (member.type !== "ClassProperty" && member.type !== "ClassPrivateProperty" && member.type !== "PropertyDefinition") {
  5294. this.raise(FlowErrors.DeclareClassElement, startLoc);
  5295. } else if (member.value) {
  5296. this.raise(FlowErrors.DeclareClassFieldInitializer, member.value);
  5297. }
  5298. }
  5299. }
  5300. isIterator(word) {
  5301. return word === "iterator" || word === "asyncIterator";
  5302. }
  5303. readIterator() {
  5304. const word = super.readWord1();
  5305. const fullWord = "@@" + word;
  5306. if (!this.isIterator(word) || !this.state.inType) {
  5307. this.raise(Errors.InvalidIdentifier, this.state.curPosition(), {
  5308. identifierName: fullWord
  5309. });
  5310. }
  5311. this.finishToken(132, fullWord);
  5312. }
  5313. getTokenFromCode(code) {
  5314. const next = this.input.charCodeAt(this.state.pos + 1);
  5315. if (code === 123 && next === 124) {
  5316. this.finishOp(6, 2);
  5317. } else if (this.state.inType && (code === 62 || code === 60)) {
  5318. this.finishOp(code === 62 ? 48 : 47, 1);
  5319. } else if (this.state.inType && code === 63) {
  5320. if (next === 46) {
  5321. this.finishOp(18, 2);
  5322. } else {
  5323. this.finishOp(17, 1);
  5324. }
  5325. } else if (isIteratorStart(code, next, this.input.charCodeAt(this.state.pos + 2))) {
  5326. this.state.pos += 2;
  5327. this.readIterator();
  5328. } else {
  5329. super.getTokenFromCode(code);
  5330. }
  5331. }
  5332. isAssignable(node, isBinding) {
  5333. if (node.type === "TypeCastExpression") {
  5334. return this.isAssignable(node.expression, isBinding);
  5335. } else {
  5336. return super.isAssignable(node, isBinding);
  5337. }
  5338. }
  5339. toAssignable(node, isLHS = false) {
  5340. if (!isLHS && node.type === "AssignmentExpression" && node.left.type === "TypeCastExpression") {
  5341. node.left = this.typeCastToParameter(node.left);
  5342. }
  5343. super.toAssignable(node, isLHS);
  5344. }
  5345. toAssignableList(exprList, trailingCommaLoc, isLHS) {
  5346. for (let i = 0; i < exprList.length; i++) {
  5347. const expr = exprList[i];
  5348. if ((expr == null ? void 0 : expr.type) === "TypeCastExpression") {
  5349. exprList[i] = this.typeCastToParameter(expr);
  5350. }
  5351. }
  5352. super.toAssignableList(exprList, trailingCommaLoc, isLHS);
  5353. }
  5354. toReferencedList(exprList, isParenthesizedExpr) {
  5355. for (let i = 0; i < exprList.length; i++) {
  5356. var _expr$extra;
  5357. const expr = exprList[i];
  5358. if (expr && expr.type === "TypeCastExpression" && !((_expr$extra = expr.extra) != null && _expr$extra.parenthesized) && (exprList.length > 1 || !isParenthesizedExpr)) {
  5359. this.raise(FlowErrors.TypeCastInPattern, expr.typeAnnotation);
  5360. }
  5361. }
  5362. return exprList;
  5363. }
  5364. parseArrayLike(close, canBePattern, isTuple, refExpressionErrors) {
  5365. const node = super.parseArrayLike(close, canBePattern, isTuple, refExpressionErrors);
  5366. if (canBePattern && !this.state.maybeInArrowParameters) {
  5367. this.toReferencedList(node.elements);
  5368. }
  5369. return node;
  5370. }
  5371. isValidLVal(type, isParenthesized, binding) {
  5372. return type === "TypeCastExpression" || super.isValidLVal(type, isParenthesized, binding);
  5373. }
  5374. parseClassProperty(node) {
  5375. if (this.match(14)) {
  5376. node.typeAnnotation = this.flowParseTypeAnnotation();
  5377. }
  5378. return super.parseClassProperty(node);
  5379. }
  5380. parseClassPrivateProperty(node) {
  5381. if (this.match(14)) {
  5382. node.typeAnnotation = this.flowParseTypeAnnotation();
  5383. }
  5384. return super.parseClassPrivateProperty(node);
  5385. }
  5386. isClassMethod() {
  5387. return this.match(47) || super.isClassMethod();
  5388. }
  5389. isClassProperty() {
  5390. return this.match(14) || super.isClassProperty();
  5391. }
  5392. isNonstaticConstructor(method) {
  5393. return !this.match(14) && super.isNonstaticConstructor(method);
  5394. }
  5395. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  5396. if (method.variance) {
  5397. this.unexpected(method.variance.loc.start);
  5398. }
  5399. delete method.variance;
  5400. if (this.match(47)) {
  5401. method.typeParameters = this.flowParseTypeParameterDeclaration();
  5402. }
  5403. super.pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper);
  5404. if (method.params && isConstructor) {
  5405. const params = method.params;
  5406. if (params.length > 0 && this.isThisParam(params[0])) {
  5407. this.raise(FlowErrors.ThisParamBannedInConstructor, method);
  5408. }
  5409. } else if (method.type === "MethodDefinition" && isConstructor && method.value.params) {
  5410. const params = method.value.params;
  5411. if (params.length > 0 && this.isThisParam(params[0])) {
  5412. this.raise(FlowErrors.ThisParamBannedInConstructor, method);
  5413. }
  5414. }
  5415. }
  5416. pushClassPrivateMethod(classBody, method, isGenerator, isAsync) {
  5417. if (method.variance) {
  5418. this.unexpected(method.variance.loc.start);
  5419. }
  5420. delete method.variance;
  5421. if (this.match(47)) {
  5422. method.typeParameters = this.flowParseTypeParameterDeclaration();
  5423. }
  5424. super.pushClassPrivateMethod(classBody, method, isGenerator, isAsync);
  5425. }
  5426. parseClassSuper(node) {
  5427. super.parseClassSuper(node);
  5428. if (node.superClass && this.match(47)) {
  5429. node.superTypeParameters = this.flowParseTypeParameterInstantiation();
  5430. }
  5431. if (this.isContextual(113)) {
  5432. this.next();
  5433. const implemented = node.implements = [];
  5434. do {
  5435. const node = this.startNode();
  5436. node.id = this.flowParseRestrictedIdentifier(true);
  5437. if (this.match(47)) {
  5438. node.typeParameters = this.flowParseTypeParameterInstantiation();
  5439. } else {
  5440. node.typeParameters = null;
  5441. }
  5442. implemented.push(this.finishNode(node, "ClassImplements"));
  5443. } while (this.eat(12));
  5444. }
  5445. }
  5446. checkGetterSetterParams(method) {
  5447. super.checkGetterSetterParams(method);
  5448. const params = this.getObjectOrClassMethodParams(method);
  5449. if (params.length > 0) {
  5450. const param = params[0];
  5451. if (this.isThisParam(param) && method.kind === "get") {
  5452. this.raise(FlowErrors.GetterMayNotHaveThisParam, param);
  5453. } else if (this.isThisParam(param)) {
  5454. this.raise(FlowErrors.SetterMayNotHaveThisParam, param);
  5455. }
  5456. }
  5457. }
  5458. parsePropertyNamePrefixOperator(node) {
  5459. node.variance = this.flowParseVariance();
  5460. }
  5461. parseObjPropValue(prop, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors) {
  5462. if (prop.variance) {
  5463. this.unexpected(prop.variance.loc.start);
  5464. }
  5465. delete prop.variance;
  5466. let typeParameters;
  5467. if (this.match(47) && !isAccessor) {
  5468. typeParameters = this.flowParseTypeParameterDeclaration();
  5469. if (!this.match(10)) this.unexpected();
  5470. }
  5471. const result = super.parseObjPropValue(prop, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors);
  5472. if (typeParameters) {
  5473. (result.value || result).typeParameters = typeParameters;
  5474. }
  5475. return result;
  5476. }
  5477. parseAssignableListItemTypes(param) {
  5478. if (this.eat(17)) {
  5479. if (param.type !== "Identifier") {
  5480. this.raise(FlowErrors.PatternIsOptional, param);
  5481. }
  5482. if (this.isThisParam(param)) {
  5483. this.raise(FlowErrors.ThisParamMayNotBeOptional, param);
  5484. }
  5485. param.optional = true;
  5486. }
  5487. if (this.match(14)) {
  5488. param.typeAnnotation = this.flowParseTypeAnnotation();
  5489. } else if (this.isThisParam(param)) {
  5490. this.raise(FlowErrors.ThisParamAnnotationRequired, param);
  5491. }
  5492. if (this.match(29) && this.isThisParam(param)) {
  5493. this.raise(FlowErrors.ThisParamNoDefault, param);
  5494. }
  5495. this.resetEndLocation(param);
  5496. return param;
  5497. }
  5498. parseMaybeDefault(startLoc, left) {
  5499. const node = super.parseMaybeDefault(startLoc, left);
  5500. if (node.type === "AssignmentPattern" && node.typeAnnotation && node.right.start < node.typeAnnotation.start) {
  5501. this.raise(FlowErrors.TypeBeforeInitializer, node.typeAnnotation);
  5502. }
  5503. return node;
  5504. }
  5505. checkImportReflection(node) {
  5506. super.checkImportReflection(node);
  5507. if (node.module && node.importKind !== "value") {
  5508. this.raise(FlowErrors.ImportReflectionHasImportType, node.specifiers[0].loc.start);
  5509. }
  5510. }
  5511. parseImportSpecifierLocal(node, specifier, type) {
  5512. specifier.local = hasTypeImportKind(node) ? this.flowParseRestrictedIdentifier(true, true) : this.parseIdentifier();
  5513. node.specifiers.push(this.finishImportSpecifier(specifier, type));
  5514. }
  5515. isPotentialImportPhase(isExport) {
  5516. if (super.isPotentialImportPhase(isExport)) return true;
  5517. if (this.isContextual(130)) {
  5518. if (!isExport) return true;
  5519. const ch = this.lookaheadCharCode();
  5520. return ch === 123 || ch === 42;
  5521. }
  5522. return !isExport && this.isContextual(87);
  5523. }
  5524. applyImportPhase(node, isExport, phase, loc) {
  5525. super.applyImportPhase(node, isExport, phase, loc);
  5526. if (isExport) {
  5527. if (!phase && this.match(65)) {
  5528. return;
  5529. }
  5530. node.exportKind = phase === "type" ? phase : "value";
  5531. } else {
  5532. if (phase === "type" && this.match(55)) this.unexpected();
  5533. node.importKind = phase === "type" || phase === "typeof" ? phase : "value";
  5534. }
  5535. }
  5536. parseImportSpecifier(specifier, importedIsString, isInTypeOnlyImport, isMaybeTypeOnly, bindingType) {
  5537. const firstIdent = specifier.imported;
  5538. let specifierTypeKind = null;
  5539. if (firstIdent.type === "Identifier") {
  5540. if (firstIdent.name === "type") {
  5541. specifierTypeKind = "type";
  5542. } else if (firstIdent.name === "typeof") {
  5543. specifierTypeKind = "typeof";
  5544. }
  5545. }
  5546. let isBinding = false;
  5547. if (this.isContextual(93) && !this.isLookaheadContextual("as")) {
  5548. const as_ident = this.parseIdentifier(true);
  5549. if (specifierTypeKind !== null && !tokenIsKeywordOrIdentifier(this.state.type)) {
  5550. specifier.imported = as_ident;
  5551. specifier.importKind = specifierTypeKind;
  5552. specifier.local = cloneIdentifier(as_ident);
  5553. } else {
  5554. specifier.imported = firstIdent;
  5555. specifier.importKind = null;
  5556. specifier.local = this.parseIdentifier();
  5557. }
  5558. } else {
  5559. if (specifierTypeKind !== null && tokenIsKeywordOrIdentifier(this.state.type)) {
  5560. specifier.imported = this.parseIdentifier(true);
  5561. specifier.importKind = specifierTypeKind;
  5562. } else {
  5563. if (importedIsString) {
  5564. throw this.raise(Errors.ImportBindingIsString, specifier, {
  5565. importName: firstIdent.value
  5566. });
  5567. }
  5568. specifier.imported = firstIdent;
  5569. specifier.importKind = null;
  5570. }
  5571. if (this.eatContextual(93)) {
  5572. specifier.local = this.parseIdentifier();
  5573. } else {
  5574. isBinding = true;
  5575. specifier.local = cloneIdentifier(specifier.imported);
  5576. }
  5577. }
  5578. const specifierIsTypeImport = hasTypeImportKind(specifier);
  5579. if (isInTypeOnlyImport && specifierIsTypeImport) {
  5580. this.raise(FlowErrors.ImportTypeShorthandOnlyInPureImport, specifier);
  5581. }
  5582. if (isInTypeOnlyImport || specifierIsTypeImport) {
  5583. this.checkReservedType(specifier.local.name, specifier.local.loc.start, true);
  5584. }
  5585. if (isBinding && !isInTypeOnlyImport && !specifierIsTypeImport) {
  5586. this.checkReservedWord(specifier.local.name, specifier.loc.start, true, true);
  5587. }
  5588. return this.finishImportSpecifier(specifier, "ImportSpecifier");
  5589. }
  5590. parseBindingAtom() {
  5591. switch (this.state.type) {
  5592. case 78:
  5593. return this.parseIdentifier(true);
  5594. default:
  5595. return super.parseBindingAtom();
  5596. }
  5597. }
  5598. parseFunctionParams(node, isConstructor) {
  5599. const kind = node.kind;
  5600. if (kind !== "get" && kind !== "set" && this.match(47)) {
  5601. node.typeParameters = this.flowParseTypeParameterDeclaration();
  5602. }
  5603. super.parseFunctionParams(node, isConstructor);
  5604. }
  5605. parseVarId(decl, kind) {
  5606. super.parseVarId(decl, kind);
  5607. if (this.match(14)) {
  5608. decl.id.typeAnnotation = this.flowParseTypeAnnotation();
  5609. this.resetEndLocation(decl.id);
  5610. }
  5611. }
  5612. parseAsyncArrowFromCallExpression(node, call) {
  5613. if (this.match(14)) {
  5614. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  5615. this.state.noAnonFunctionType = true;
  5616. node.returnType = this.flowParseTypeAnnotation();
  5617. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  5618. }
  5619. return super.parseAsyncArrowFromCallExpression(node, call);
  5620. }
  5621. shouldParseAsyncArrow() {
  5622. return this.match(14) || super.shouldParseAsyncArrow();
  5623. }
  5624. parseMaybeAssign(refExpressionErrors, afterLeftParse) {
  5625. var _jsx;
  5626. let state = null;
  5627. let jsx;
  5628. if (this.hasPlugin("jsx") && (this.match(142) || this.match(47))) {
  5629. state = this.state.clone();
  5630. jsx = this.tryParse(() => super.parseMaybeAssign(refExpressionErrors, afterLeftParse), state);
  5631. if (!jsx.error) return jsx.node;
  5632. const {
  5633. context
  5634. } = this.state;
  5635. const currentContext = context[context.length - 1];
  5636. if (currentContext === types.j_oTag || currentContext === types.j_expr) {
  5637. context.pop();
  5638. }
  5639. }
  5640. if ((_jsx = jsx) != null && _jsx.error || this.match(47)) {
  5641. var _jsx2, _jsx3;
  5642. state = state || this.state.clone();
  5643. let typeParameters;
  5644. const arrow = this.tryParse(abort => {
  5645. var _arrowExpression$extr;
  5646. typeParameters = this.flowParseTypeParameterDeclaration();
  5647. const arrowExpression = this.forwardNoArrowParamsConversionAt(typeParameters, () => {
  5648. const result = super.parseMaybeAssign(refExpressionErrors, afterLeftParse);
  5649. this.resetStartLocationFromNode(result, typeParameters);
  5650. return result;
  5651. });
  5652. if ((_arrowExpression$extr = arrowExpression.extra) != null && _arrowExpression$extr.parenthesized) abort();
  5653. const expr = this.maybeUnwrapTypeCastExpression(arrowExpression);
  5654. if (expr.type !== "ArrowFunctionExpression") abort();
  5655. expr.typeParameters = typeParameters;
  5656. this.resetStartLocationFromNode(expr, typeParameters);
  5657. return arrowExpression;
  5658. }, state);
  5659. let arrowExpression = null;
  5660. if (arrow.node && this.maybeUnwrapTypeCastExpression(arrow.node).type === "ArrowFunctionExpression") {
  5661. if (!arrow.error && !arrow.aborted) {
  5662. if (arrow.node.async) {
  5663. this.raise(FlowErrors.UnexpectedTypeParameterBeforeAsyncArrowFunction, typeParameters);
  5664. }
  5665. return arrow.node;
  5666. }
  5667. arrowExpression = arrow.node;
  5668. }
  5669. if ((_jsx2 = jsx) != null && _jsx2.node) {
  5670. this.state = jsx.failState;
  5671. return jsx.node;
  5672. }
  5673. if (arrowExpression) {
  5674. this.state = arrow.failState;
  5675. return arrowExpression;
  5676. }
  5677. if ((_jsx3 = jsx) != null && _jsx3.thrown) throw jsx.error;
  5678. if (arrow.thrown) throw arrow.error;
  5679. throw this.raise(FlowErrors.UnexpectedTokenAfterTypeParameter, typeParameters);
  5680. }
  5681. return super.parseMaybeAssign(refExpressionErrors, afterLeftParse);
  5682. }
  5683. parseArrow(node) {
  5684. if (this.match(14)) {
  5685. const result = this.tryParse(() => {
  5686. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  5687. this.state.noAnonFunctionType = true;
  5688. const typeNode = this.startNode();
  5689. [typeNode.typeAnnotation, node.predicate] = this.flowParseTypeAndPredicateInitialiser();
  5690. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  5691. if (this.canInsertSemicolon()) this.unexpected();
  5692. if (!this.match(19)) this.unexpected();
  5693. return typeNode;
  5694. });
  5695. if (result.thrown) return null;
  5696. if (result.error) this.state = result.failState;
  5697. node.returnType = result.node.typeAnnotation ? this.finishNode(result.node, "TypeAnnotation") : null;
  5698. }
  5699. return super.parseArrow(node);
  5700. }
  5701. shouldParseArrow(params) {
  5702. return this.match(14) || super.shouldParseArrow(params);
  5703. }
  5704. setArrowFunctionParameters(node, params) {
  5705. if (this.state.noArrowParamsConversionAt.includes(node.start)) {
  5706. node.params = params;
  5707. } else {
  5708. super.setArrowFunctionParameters(node, params);
  5709. }
  5710. }
  5711. checkParams(node, allowDuplicates, isArrowFunction, strictModeChanged = true) {
  5712. if (isArrowFunction && this.state.noArrowParamsConversionAt.includes(node.start)) {
  5713. return;
  5714. }
  5715. for (let i = 0; i < node.params.length; i++) {
  5716. if (this.isThisParam(node.params[i]) && i > 0) {
  5717. this.raise(FlowErrors.ThisParamMustBeFirst, node.params[i]);
  5718. }
  5719. }
  5720. super.checkParams(node, allowDuplicates, isArrowFunction, strictModeChanged);
  5721. }
  5722. parseParenAndDistinguishExpression(canBeArrow) {
  5723. return super.parseParenAndDistinguishExpression(canBeArrow && !this.state.noArrowAt.includes(this.state.start));
  5724. }
  5725. parseSubscripts(base, startLoc, noCalls) {
  5726. if (base.type === "Identifier" && base.name === "async" && this.state.noArrowAt.includes(startLoc.index)) {
  5727. this.next();
  5728. const node = this.startNodeAt(startLoc);
  5729. node.callee = base;
  5730. node.arguments = super.parseCallExpressionArguments(11, false);
  5731. base = this.finishNode(node, "CallExpression");
  5732. } else if (base.type === "Identifier" && base.name === "async" && this.match(47)) {
  5733. const state = this.state.clone();
  5734. const arrow = this.tryParse(abort => this.parseAsyncArrowWithTypeParameters(startLoc) || abort(), state);
  5735. if (!arrow.error && !arrow.aborted) return arrow.node;
  5736. const result = this.tryParse(() => super.parseSubscripts(base, startLoc, noCalls), state);
  5737. if (result.node && !result.error) return result.node;
  5738. if (arrow.node) {
  5739. this.state = arrow.failState;
  5740. return arrow.node;
  5741. }
  5742. if (result.node) {
  5743. this.state = result.failState;
  5744. return result.node;
  5745. }
  5746. throw arrow.error || result.error;
  5747. }
  5748. return super.parseSubscripts(base, startLoc, noCalls);
  5749. }
  5750. parseSubscript(base, startLoc, noCalls, subscriptState) {
  5751. if (this.match(18) && this.isLookaheadToken_lt()) {
  5752. subscriptState.optionalChainMember = true;
  5753. if (noCalls) {
  5754. subscriptState.stop = true;
  5755. return base;
  5756. }
  5757. this.next();
  5758. const node = this.startNodeAt(startLoc);
  5759. node.callee = base;
  5760. node.typeArguments = this.flowParseTypeParameterInstantiation();
  5761. this.expect(10);
  5762. node.arguments = this.parseCallExpressionArguments(11, false);
  5763. node.optional = true;
  5764. return this.finishCallExpression(node, true);
  5765. } else if (!noCalls && this.shouldParseTypes() && this.match(47)) {
  5766. const node = this.startNodeAt(startLoc);
  5767. node.callee = base;
  5768. const result = this.tryParse(() => {
  5769. node.typeArguments = this.flowParseTypeParameterInstantiationCallOrNew();
  5770. this.expect(10);
  5771. node.arguments = super.parseCallExpressionArguments(11, false);
  5772. if (subscriptState.optionalChainMember) {
  5773. node.optional = false;
  5774. }
  5775. return this.finishCallExpression(node, subscriptState.optionalChainMember);
  5776. });
  5777. if (result.node) {
  5778. if (result.error) this.state = result.failState;
  5779. return result.node;
  5780. }
  5781. }
  5782. return super.parseSubscript(base, startLoc, noCalls, subscriptState);
  5783. }
  5784. parseNewCallee(node) {
  5785. super.parseNewCallee(node);
  5786. let targs = null;
  5787. if (this.shouldParseTypes() && this.match(47)) {
  5788. targs = this.tryParse(() => this.flowParseTypeParameterInstantiationCallOrNew()).node;
  5789. }
  5790. node.typeArguments = targs;
  5791. }
  5792. parseAsyncArrowWithTypeParameters(startLoc) {
  5793. const node = this.startNodeAt(startLoc);
  5794. this.parseFunctionParams(node, false);
  5795. if (!this.parseArrow(node)) return;
  5796. return super.parseArrowExpression(node, undefined, true);
  5797. }
  5798. readToken_mult_modulo(code) {
  5799. const next = this.input.charCodeAt(this.state.pos + 1);
  5800. if (code === 42 && next === 47 && this.state.hasFlowComment) {
  5801. this.state.hasFlowComment = false;
  5802. this.state.pos += 2;
  5803. this.nextToken();
  5804. return;
  5805. }
  5806. super.readToken_mult_modulo(code);
  5807. }
  5808. readToken_pipe_amp(code) {
  5809. const next = this.input.charCodeAt(this.state.pos + 1);
  5810. if (code === 124 && next === 125) {
  5811. this.finishOp(9, 2);
  5812. return;
  5813. }
  5814. super.readToken_pipe_amp(code);
  5815. }
  5816. parseTopLevel(file, program) {
  5817. const fileNode = super.parseTopLevel(file, program);
  5818. if (this.state.hasFlowComment) {
  5819. this.raise(FlowErrors.UnterminatedFlowComment, this.state.curPosition());
  5820. }
  5821. return fileNode;
  5822. }
  5823. skipBlockComment() {
  5824. if (this.hasPlugin("flowComments") && this.skipFlowComment()) {
  5825. if (this.state.hasFlowComment) {
  5826. throw this.raise(FlowErrors.NestedFlowComment, this.state.startLoc);
  5827. }
  5828. this.hasFlowCommentCompletion();
  5829. const commentSkip = this.skipFlowComment();
  5830. if (commentSkip) {
  5831. this.state.pos += commentSkip;
  5832. this.state.hasFlowComment = true;
  5833. }
  5834. return;
  5835. }
  5836. return super.skipBlockComment(this.state.hasFlowComment ? "*-/" : "*/");
  5837. }
  5838. skipFlowComment() {
  5839. const {
  5840. pos
  5841. } = this.state;
  5842. let shiftToFirstNonWhiteSpace = 2;
  5843. while ([32, 9].includes(this.input.charCodeAt(pos + shiftToFirstNonWhiteSpace))) {
  5844. shiftToFirstNonWhiteSpace++;
  5845. }
  5846. const ch2 = this.input.charCodeAt(shiftToFirstNonWhiteSpace + pos);
  5847. const ch3 = this.input.charCodeAt(shiftToFirstNonWhiteSpace + pos + 1);
  5848. if (ch2 === 58 && ch3 === 58) {
  5849. return shiftToFirstNonWhiteSpace + 2;
  5850. }
  5851. if (this.input.slice(shiftToFirstNonWhiteSpace + pos, shiftToFirstNonWhiteSpace + pos + 12) === "flow-include") {
  5852. return shiftToFirstNonWhiteSpace + 12;
  5853. }
  5854. if (ch2 === 58 && ch3 !== 58) {
  5855. return shiftToFirstNonWhiteSpace;
  5856. }
  5857. return false;
  5858. }
  5859. hasFlowCommentCompletion() {
  5860. const end = this.input.indexOf("*/", this.state.pos);
  5861. if (end === -1) {
  5862. throw this.raise(Errors.UnterminatedComment, this.state.curPosition());
  5863. }
  5864. }
  5865. flowEnumErrorBooleanMemberNotInitialized(loc, {
  5866. enumName,
  5867. memberName
  5868. }) {
  5869. this.raise(FlowErrors.EnumBooleanMemberNotInitialized, loc, {
  5870. memberName,
  5871. enumName
  5872. });
  5873. }
  5874. flowEnumErrorInvalidMemberInitializer(loc, enumContext) {
  5875. return this.raise(!enumContext.explicitType ? FlowErrors.EnumInvalidMemberInitializerUnknownType : enumContext.explicitType === "symbol" ? FlowErrors.EnumInvalidMemberInitializerSymbolType : FlowErrors.EnumInvalidMemberInitializerPrimaryType, loc, enumContext);
  5876. }
  5877. flowEnumErrorNumberMemberNotInitialized(loc, details) {
  5878. this.raise(FlowErrors.EnumNumberMemberNotInitialized, loc, details);
  5879. }
  5880. flowEnumErrorStringMemberInconsistentlyInitialized(node, details) {
  5881. this.raise(FlowErrors.EnumStringMemberInconsistentlyInitialized, node, details);
  5882. }
  5883. flowEnumMemberInit() {
  5884. const startLoc = this.state.startLoc;
  5885. const endOfInit = () => this.match(12) || this.match(8);
  5886. switch (this.state.type) {
  5887. case 134:
  5888. {
  5889. const literal = this.parseNumericLiteral(this.state.value);
  5890. if (endOfInit()) {
  5891. return {
  5892. type: "number",
  5893. loc: literal.loc.start,
  5894. value: literal
  5895. };
  5896. }
  5897. return {
  5898. type: "invalid",
  5899. loc: startLoc
  5900. };
  5901. }
  5902. case 133:
  5903. {
  5904. const literal = this.parseStringLiteral(this.state.value);
  5905. if (endOfInit()) {
  5906. return {
  5907. type: "string",
  5908. loc: literal.loc.start,
  5909. value: literal
  5910. };
  5911. }
  5912. return {
  5913. type: "invalid",
  5914. loc: startLoc
  5915. };
  5916. }
  5917. case 85:
  5918. case 86:
  5919. {
  5920. const literal = this.parseBooleanLiteral(this.match(85));
  5921. if (endOfInit()) {
  5922. return {
  5923. type: "boolean",
  5924. loc: literal.loc.start,
  5925. value: literal
  5926. };
  5927. }
  5928. return {
  5929. type: "invalid",
  5930. loc: startLoc
  5931. };
  5932. }
  5933. default:
  5934. return {
  5935. type: "invalid",
  5936. loc: startLoc
  5937. };
  5938. }
  5939. }
  5940. flowEnumMemberRaw() {
  5941. const loc = this.state.startLoc;
  5942. const id = this.parseIdentifier(true);
  5943. const init = this.eat(29) ? this.flowEnumMemberInit() : {
  5944. type: "none",
  5945. loc
  5946. };
  5947. return {
  5948. id,
  5949. init
  5950. };
  5951. }
  5952. flowEnumCheckExplicitTypeMismatch(loc, context, expectedType) {
  5953. const {
  5954. explicitType
  5955. } = context;
  5956. if (explicitType === null) {
  5957. return;
  5958. }
  5959. if (explicitType !== expectedType) {
  5960. this.flowEnumErrorInvalidMemberInitializer(loc, context);
  5961. }
  5962. }
  5963. flowEnumMembers({
  5964. enumName,
  5965. explicitType
  5966. }) {
  5967. const seenNames = new Set();
  5968. const members = {
  5969. booleanMembers: [],
  5970. numberMembers: [],
  5971. stringMembers: [],
  5972. defaultedMembers: []
  5973. };
  5974. let hasUnknownMembers = false;
  5975. while (!this.match(8)) {
  5976. if (this.eat(21)) {
  5977. hasUnknownMembers = true;
  5978. break;
  5979. }
  5980. const memberNode = this.startNode();
  5981. const {
  5982. id,
  5983. init
  5984. } = this.flowEnumMemberRaw();
  5985. const memberName = id.name;
  5986. if (memberName === "") {
  5987. continue;
  5988. }
  5989. if (/^[a-z]/.test(memberName)) {
  5990. this.raise(FlowErrors.EnumInvalidMemberName, id, {
  5991. memberName,
  5992. suggestion: memberName[0].toUpperCase() + memberName.slice(1),
  5993. enumName
  5994. });
  5995. }
  5996. if (seenNames.has(memberName)) {
  5997. this.raise(FlowErrors.EnumDuplicateMemberName, id, {
  5998. memberName,
  5999. enumName
  6000. });
  6001. }
  6002. seenNames.add(memberName);
  6003. const context = {
  6004. enumName,
  6005. explicitType,
  6006. memberName
  6007. };
  6008. memberNode.id = id;
  6009. switch (init.type) {
  6010. case "boolean":
  6011. {
  6012. this.flowEnumCheckExplicitTypeMismatch(init.loc, context, "boolean");
  6013. memberNode.init = init.value;
  6014. members.booleanMembers.push(this.finishNode(memberNode, "EnumBooleanMember"));
  6015. break;
  6016. }
  6017. case "number":
  6018. {
  6019. this.flowEnumCheckExplicitTypeMismatch(init.loc, context, "number");
  6020. memberNode.init = init.value;
  6021. members.numberMembers.push(this.finishNode(memberNode, "EnumNumberMember"));
  6022. break;
  6023. }
  6024. case "string":
  6025. {
  6026. this.flowEnumCheckExplicitTypeMismatch(init.loc, context, "string");
  6027. memberNode.init = init.value;
  6028. members.stringMembers.push(this.finishNode(memberNode, "EnumStringMember"));
  6029. break;
  6030. }
  6031. case "invalid":
  6032. {
  6033. throw this.flowEnumErrorInvalidMemberInitializer(init.loc, context);
  6034. }
  6035. case "none":
  6036. {
  6037. switch (explicitType) {
  6038. case "boolean":
  6039. this.flowEnumErrorBooleanMemberNotInitialized(init.loc, context);
  6040. break;
  6041. case "number":
  6042. this.flowEnumErrorNumberMemberNotInitialized(init.loc, context);
  6043. break;
  6044. default:
  6045. members.defaultedMembers.push(this.finishNode(memberNode, "EnumDefaultedMember"));
  6046. }
  6047. }
  6048. }
  6049. if (!this.match(8)) {
  6050. this.expect(12);
  6051. }
  6052. }
  6053. return {
  6054. members,
  6055. hasUnknownMembers
  6056. };
  6057. }
  6058. flowEnumStringMembers(initializedMembers, defaultedMembers, {
  6059. enumName
  6060. }) {
  6061. if (initializedMembers.length === 0) {
  6062. return defaultedMembers;
  6063. } else if (defaultedMembers.length === 0) {
  6064. return initializedMembers;
  6065. } else if (defaultedMembers.length > initializedMembers.length) {
  6066. for (const member of initializedMembers) {
  6067. this.flowEnumErrorStringMemberInconsistentlyInitialized(member, {
  6068. enumName
  6069. });
  6070. }
  6071. return defaultedMembers;
  6072. } else {
  6073. for (const member of defaultedMembers) {
  6074. this.flowEnumErrorStringMemberInconsistentlyInitialized(member, {
  6075. enumName
  6076. });
  6077. }
  6078. return initializedMembers;
  6079. }
  6080. }
  6081. flowEnumParseExplicitType({
  6082. enumName
  6083. }) {
  6084. if (!this.eatContextual(102)) return null;
  6085. if (!tokenIsIdentifier(this.state.type)) {
  6086. throw this.raise(FlowErrors.EnumInvalidExplicitTypeUnknownSupplied, this.state.startLoc, {
  6087. enumName
  6088. });
  6089. }
  6090. const {
  6091. value
  6092. } = this.state;
  6093. this.next();
  6094. if (value !== "boolean" && value !== "number" && value !== "string" && value !== "symbol") {
  6095. this.raise(FlowErrors.EnumInvalidExplicitType, this.state.startLoc, {
  6096. enumName,
  6097. invalidEnumType: value
  6098. });
  6099. }
  6100. return value;
  6101. }
  6102. flowEnumBody(node, id) {
  6103. const enumName = id.name;
  6104. const nameLoc = id.loc.start;
  6105. const explicitType = this.flowEnumParseExplicitType({
  6106. enumName
  6107. });
  6108. this.expect(5);
  6109. const {
  6110. members,
  6111. hasUnknownMembers
  6112. } = this.flowEnumMembers({
  6113. enumName,
  6114. explicitType
  6115. });
  6116. node.hasUnknownMembers = hasUnknownMembers;
  6117. switch (explicitType) {
  6118. case "boolean":
  6119. node.explicitType = true;
  6120. node.members = members.booleanMembers;
  6121. this.expect(8);
  6122. return this.finishNode(node, "EnumBooleanBody");
  6123. case "number":
  6124. node.explicitType = true;
  6125. node.members = members.numberMembers;
  6126. this.expect(8);
  6127. return this.finishNode(node, "EnumNumberBody");
  6128. case "string":
  6129. node.explicitType = true;
  6130. node.members = this.flowEnumStringMembers(members.stringMembers, members.defaultedMembers, {
  6131. enumName
  6132. });
  6133. this.expect(8);
  6134. return this.finishNode(node, "EnumStringBody");
  6135. case "symbol":
  6136. node.members = members.defaultedMembers;
  6137. this.expect(8);
  6138. return this.finishNode(node, "EnumSymbolBody");
  6139. default:
  6140. {
  6141. const empty = () => {
  6142. node.members = [];
  6143. this.expect(8);
  6144. return this.finishNode(node, "EnumStringBody");
  6145. };
  6146. node.explicitType = false;
  6147. const boolsLen = members.booleanMembers.length;
  6148. const numsLen = members.numberMembers.length;
  6149. const strsLen = members.stringMembers.length;
  6150. const defaultedLen = members.defaultedMembers.length;
  6151. if (!boolsLen && !numsLen && !strsLen && !defaultedLen) {
  6152. return empty();
  6153. } else if (!boolsLen && !numsLen) {
  6154. node.members = this.flowEnumStringMembers(members.stringMembers, members.defaultedMembers, {
  6155. enumName
  6156. });
  6157. this.expect(8);
  6158. return this.finishNode(node, "EnumStringBody");
  6159. } else if (!numsLen && !strsLen && boolsLen >= defaultedLen) {
  6160. for (const member of members.defaultedMembers) {
  6161. this.flowEnumErrorBooleanMemberNotInitialized(member.loc.start, {
  6162. enumName,
  6163. memberName: member.id.name
  6164. });
  6165. }
  6166. node.members = members.booleanMembers;
  6167. this.expect(8);
  6168. return this.finishNode(node, "EnumBooleanBody");
  6169. } else if (!boolsLen && !strsLen && numsLen >= defaultedLen) {
  6170. for (const member of members.defaultedMembers) {
  6171. this.flowEnumErrorNumberMemberNotInitialized(member.loc.start, {
  6172. enumName,
  6173. memberName: member.id.name
  6174. });
  6175. }
  6176. node.members = members.numberMembers;
  6177. this.expect(8);
  6178. return this.finishNode(node, "EnumNumberBody");
  6179. } else {
  6180. this.raise(FlowErrors.EnumInconsistentMemberValues, nameLoc, {
  6181. enumName
  6182. });
  6183. return empty();
  6184. }
  6185. }
  6186. }
  6187. }
  6188. flowParseEnumDeclaration(node) {
  6189. const id = this.parseIdentifier();
  6190. node.id = id;
  6191. node.body = this.flowEnumBody(this.startNode(), id);
  6192. return this.finishNode(node, "EnumDeclaration");
  6193. }
  6194. isLookaheadToken_lt() {
  6195. const next = this.nextTokenStart();
  6196. if (this.input.charCodeAt(next) === 60) {
  6197. const afterNext = this.input.charCodeAt(next + 1);
  6198. return afterNext !== 60 && afterNext !== 61;
  6199. }
  6200. return false;
  6201. }
  6202. maybeUnwrapTypeCastExpression(node) {
  6203. return node.type === "TypeCastExpression" ? node.expression : node;
  6204. }
  6205. };
  6206. const entities = {
  6207. __proto__: null,
  6208. quot: "\u0022",
  6209. amp: "&",
  6210. apos: "\u0027",
  6211. lt: "<",
  6212. gt: ">",
  6213. nbsp: "\u00A0",
  6214. iexcl: "\u00A1",
  6215. cent: "\u00A2",
  6216. pound: "\u00A3",
  6217. curren: "\u00A4",
  6218. yen: "\u00A5",
  6219. brvbar: "\u00A6",
  6220. sect: "\u00A7",
  6221. uml: "\u00A8",
  6222. copy: "\u00A9",
  6223. ordf: "\u00AA",
  6224. laquo: "\u00AB",
  6225. not: "\u00AC",
  6226. shy: "\u00AD",
  6227. reg: "\u00AE",
  6228. macr: "\u00AF",
  6229. deg: "\u00B0",
  6230. plusmn: "\u00B1",
  6231. sup2: "\u00B2",
  6232. sup3: "\u00B3",
  6233. acute: "\u00B4",
  6234. micro: "\u00B5",
  6235. para: "\u00B6",
  6236. middot: "\u00B7",
  6237. cedil: "\u00B8",
  6238. sup1: "\u00B9",
  6239. ordm: "\u00BA",
  6240. raquo: "\u00BB",
  6241. frac14: "\u00BC",
  6242. frac12: "\u00BD",
  6243. frac34: "\u00BE",
  6244. iquest: "\u00BF",
  6245. Agrave: "\u00C0",
  6246. Aacute: "\u00C1",
  6247. Acirc: "\u00C2",
  6248. Atilde: "\u00C3",
  6249. Auml: "\u00C4",
  6250. Aring: "\u00C5",
  6251. AElig: "\u00C6",
  6252. Ccedil: "\u00C7",
  6253. Egrave: "\u00C8",
  6254. Eacute: "\u00C9",
  6255. Ecirc: "\u00CA",
  6256. Euml: "\u00CB",
  6257. Igrave: "\u00CC",
  6258. Iacute: "\u00CD",
  6259. Icirc: "\u00CE",
  6260. Iuml: "\u00CF",
  6261. ETH: "\u00D0",
  6262. Ntilde: "\u00D1",
  6263. Ograve: "\u00D2",
  6264. Oacute: "\u00D3",
  6265. Ocirc: "\u00D4",
  6266. Otilde: "\u00D5",
  6267. Ouml: "\u00D6",
  6268. times: "\u00D7",
  6269. Oslash: "\u00D8",
  6270. Ugrave: "\u00D9",
  6271. Uacute: "\u00DA",
  6272. Ucirc: "\u00DB",
  6273. Uuml: "\u00DC",
  6274. Yacute: "\u00DD",
  6275. THORN: "\u00DE",
  6276. szlig: "\u00DF",
  6277. agrave: "\u00E0",
  6278. aacute: "\u00E1",
  6279. acirc: "\u00E2",
  6280. atilde: "\u00E3",
  6281. auml: "\u00E4",
  6282. aring: "\u00E5",
  6283. aelig: "\u00E6",
  6284. ccedil: "\u00E7",
  6285. egrave: "\u00E8",
  6286. eacute: "\u00E9",
  6287. ecirc: "\u00EA",
  6288. euml: "\u00EB",
  6289. igrave: "\u00EC",
  6290. iacute: "\u00ED",
  6291. icirc: "\u00EE",
  6292. iuml: "\u00EF",
  6293. eth: "\u00F0",
  6294. ntilde: "\u00F1",
  6295. ograve: "\u00F2",
  6296. oacute: "\u00F3",
  6297. ocirc: "\u00F4",
  6298. otilde: "\u00F5",
  6299. ouml: "\u00F6",
  6300. divide: "\u00F7",
  6301. oslash: "\u00F8",
  6302. ugrave: "\u00F9",
  6303. uacute: "\u00FA",
  6304. ucirc: "\u00FB",
  6305. uuml: "\u00FC",
  6306. yacute: "\u00FD",
  6307. thorn: "\u00FE",
  6308. yuml: "\u00FF",
  6309. OElig: "\u0152",
  6310. oelig: "\u0153",
  6311. Scaron: "\u0160",
  6312. scaron: "\u0161",
  6313. Yuml: "\u0178",
  6314. fnof: "\u0192",
  6315. circ: "\u02C6",
  6316. tilde: "\u02DC",
  6317. Alpha: "\u0391",
  6318. Beta: "\u0392",
  6319. Gamma: "\u0393",
  6320. Delta: "\u0394",
  6321. Epsilon: "\u0395",
  6322. Zeta: "\u0396",
  6323. Eta: "\u0397",
  6324. Theta: "\u0398",
  6325. Iota: "\u0399",
  6326. Kappa: "\u039A",
  6327. Lambda: "\u039B",
  6328. Mu: "\u039C",
  6329. Nu: "\u039D",
  6330. Xi: "\u039E",
  6331. Omicron: "\u039F",
  6332. Pi: "\u03A0",
  6333. Rho: "\u03A1",
  6334. Sigma: "\u03A3",
  6335. Tau: "\u03A4",
  6336. Upsilon: "\u03A5",
  6337. Phi: "\u03A6",
  6338. Chi: "\u03A7",
  6339. Psi: "\u03A8",
  6340. Omega: "\u03A9",
  6341. alpha: "\u03B1",
  6342. beta: "\u03B2",
  6343. gamma: "\u03B3",
  6344. delta: "\u03B4",
  6345. epsilon: "\u03B5",
  6346. zeta: "\u03B6",
  6347. eta: "\u03B7",
  6348. theta: "\u03B8",
  6349. iota: "\u03B9",
  6350. kappa: "\u03BA",
  6351. lambda: "\u03BB",
  6352. mu: "\u03BC",
  6353. nu: "\u03BD",
  6354. xi: "\u03BE",
  6355. omicron: "\u03BF",
  6356. pi: "\u03C0",
  6357. rho: "\u03C1",
  6358. sigmaf: "\u03C2",
  6359. sigma: "\u03C3",
  6360. tau: "\u03C4",
  6361. upsilon: "\u03C5",
  6362. phi: "\u03C6",
  6363. chi: "\u03C7",
  6364. psi: "\u03C8",
  6365. omega: "\u03C9",
  6366. thetasym: "\u03D1",
  6367. upsih: "\u03D2",
  6368. piv: "\u03D6",
  6369. ensp: "\u2002",
  6370. emsp: "\u2003",
  6371. thinsp: "\u2009",
  6372. zwnj: "\u200C",
  6373. zwj: "\u200D",
  6374. lrm: "\u200E",
  6375. rlm: "\u200F",
  6376. ndash: "\u2013",
  6377. mdash: "\u2014",
  6378. lsquo: "\u2018",
  6379. rsquo: "\u2019",
  6380. sbquo: "\u201A",
  6381. ldquo: "\u201C",
  6382. rdquo: "\u201D",
  6383. bdquo: "\u201E",
  6384. dagger: "\u2020",
  6385. Dagger: "\u2021",
  6386. bull: "\u2022",
  6387. hellip: "\u2026",
  6388. permil: "\u2030",
  6389. prime: "\u2032",
  6390. Prime: "\u2033",
  6391. lsaquo: "\u2039",
  6392. rsaquo: "\u203A",
  6393. oline: "\u203E",
  6394. frasl: "\u2044",
  6395. euro: "\u20AC",
  6396. image: "\u2111",
  6397. weierp: "\u2118",
  6398. real: "\u211C",
  6399. trade: "\u2122",
  6400. alefsym: "\u2135",
  6401. larr: "\u2190",
  6402. uarr: "\u2191",
  6403. rarr: "\u2192",
  6404. darr: "\u2193",
  6405. harr: "\u2194",
  6406. crarr: "\u21B5",
  6407. lArr: "\u21D0",
  6408. uArr: "\u21D1",
  6409. rArr: "\u21D2",
  6410. dArr: "\u21D3",
  6411. hArr: "\u21D4",
  6412. forall: "\u2200",
  6413. part: "\u2202",
  6414. exist: "\u2203",
  6415. empty: "\u2205",
  6416. nabla: "\u2207",
  6417. isin: "\u2208",
  6418. notin: "\u2209",
  6419. ni: "\u220B",
  6420. prod: "\u220F",
  6421. sum: "\u2211",
  6422. minus: "\u2212",
  6423. lowast: "\u2217",
  6424. radic: "\u221A",
  6425. prop: "\u221D",
  6426. infin: "\u221E",
  6427. ang: "\u2220",
  6428. and: "\u2227",
  6429. or: "\u2228",
  6430. cap: "\u2229",
  6431. cup: "\u222A",
  6432. int: "\u222B",
  6433. there4: "\u2234",
  6434. sim: "\u223C",
  6435. cong: "\u2245",
  6436. asymp: "\u2248",
  6437. ne: "\u2260",
  6438. equiv: "\u2261",
  6439. le: "\u2264",
  6440. ge: "\u2265",
  6441. sub: "\u2282",
  6442. sup: "\u2283",
  6443. nsub: "\u2284",
  6444. sube: "\u2286",
  6445. supe: "\u2287",
  6446. oplus: "\u2295",
  6447. otimes: "\u2297",
  6448. perp: "\u22A5",
  6449. sdot: "\u22C5",
  6450. lceil: "\u2308",
  6451. rceil: "\u2309",
  6452. lfloor: "\u230A",
  6453. rfloor: "\u230B",
  6454. lang: "\u2329",
  6455. rang: "\u232A",
  6456. loz: "\u25CA",
  6457. spades: "\u2660",
  6458. clubs: "\u2663",
  6459. hearts: "\u2665",
  6460. diams: "\u2666"
  6461. };
  6462. const JsxErrors = ParseErrorEnum`jsx`({
  6463. AttributeIsEmpty: "JSX attributes must only be assigned a non-empty expression.",
  6464. MissingClosingTagElement: ({
  6465. openingTagName
  6466. }) => `Expected corresponding JSX closing tag for <${openingTagName}>.`,
  6467. MissingClosingTagFragment: "Expected corresponding JSX closing tag for <>.",
  6468. UnexpectedSequenceExpression: "Sequence expressions cannot be directly nested inside JSX. Did you mean to wrap it in parentheses (...)?",
  6469. UnexpectedToken: ({
  6470. unexpected,
  6471. HTMLEntity
  6472. }) => `Unexpected token \`${unexpected}\`. Did you mean \`${HTMLEntity}\` or \`{'${unexpected}'}\`?`,
  6473. UnsupportedJsxValue: "JSX value should be either an expression or a quoted JSX text.",
  6474. UnterminatedJsxContent: "Unterminated JSX contents.",
  6475. UnwrappedAdjacentJSXElements: "Adjacent JSX elements must be wrapped in an enclosing tag. Did you want a JSX fragment <>...</>?"
  6476. });
  6477. function isFragment(object) {
  6478. return object ? object.type === "JSXOpeningFragment" || object.type === "JSXClosingFragment" : false;
  6479. }
  6480. function getQualifiedJSXName(object) {
  6481. if (object.type === "JSXIdentifier") {
  6482. return object.name;
  6483. }
  6484. if (object.type === "JSXNamespacedName") {
  6485. return object.namespace.name + ":" + object.name.name;
  6486. }
  6487. if (object.type === "JSXMemberExpression") {
  6488. return getQualifiedJSXName(object.object) + "." + getQualifiedJSXName(object.property);
  6489. }
  6490. throw new Error("Node had unexpected type: " + object.type);
  6491. }
  6492. var jsx = superClass => class JSXParserMixin extends superClass {
  6493. jsxReadToken() {
  6494. let out = "";
  6495. let chunkStart = this.state.pos;
  6496. for (;;) {
  6497. if (this.state.pos >= this.length) {
  6498. throw this.raise(JsxErrors.UnterminatedJsxContent, this.state.startLoc);
  6499. }
  6500. const ch = this.input.charCodeAt(this.state.pos);
  6501. switch (ch) {
  6502. case 60:
  6503. case 123:
  6504. if (this.state.pos === this.state.start) {
  6505. if (ch === 60 && this.state.canStartJSXElement) {
  6506. ++this.state.pos;
  6507. this.finishToken(142);
  6508. } else {
  6509. super.getTokenFromCode(ch);
  6510. }
  6511. return;
  6512. }
  6513. out += this.input.slice(chunkStart, this.state.pos);
  6514. this.finishToken(141, out);
  6515. return;
  6516. case 38:
  6517. out += this.input.slice(chunkStart, this.state.pos);
  6518. out += this.jsxReadEntity();
  6519. chunkStart = this.state.pos;
  6520. break;
  6521. case 62:
  6522. case 125:
  6523. default:
  6524. if (isNewLine(ch)) {
  6525. out += this.input.slice(chunkStart, this.state.pos);
  6526. out += this.jsxReadNewLine(true);
  6527. chunkStart = this.state.pos;
  6528. } else {
  6529. ++this.state.pos;
  6530. }
  6531. }
  6532. }
  6533. }
  6534. jsxReadNewLine(normalizeCRLF) {
  6535. const ch = this.input.charCodeAt(this.state.pos);
  6536. let out;
  6537. ++this.state.pos;
  6538. if (ch === 13 && this.input.charCodeAt(this.state.pos) === 10) {
  6539. ++this.state.pos;
  6540. out = normalizeCRLF ? "\n" : "\r\n";
  6541. } else {
  6542. out = String.fromCharCode(ch);
  6543. }
  6544. ++this.state.curLine;
  6545. this.state.lineStart = this.state.pos;
  6546. return out;
  6547. }
  6548. jsxReadString(quote) {
  6549. let out = "";
  6550. let chunkStart = ++this.state.pos;
  6551. for (;;) {
  6552. if (this.state.pos >= this.length) {
  6553. throw this.raise(Errors.UnterminatedString, this.state.startLoc);
  6554. }
  6555. const ch = this.input.charCodeAt(this.state.pos);
  6556. if (ch === quote) break;
  6557. if (ch === 38) {
  6558. out += this.input.slice(chunkStart, this.state.pos);
  6559. out += this.jsxReadEntity();
  6560. chunkStart = this.state.pos;
  6561. } else if (isNewLine(ch)) {
  6562. out += this.input.slice(chunkStart, this.state.pos);
  6563. out += this.jsxReadNewLine(false);
  6564. chunkStart = this.state.pos;
  6565. } else {
  6566. ++this.state.pos;
  6567. }
  6568. }
  6569. out += this.input.slice(chunkStart, this.state.pos++);
  6570. this.finishToken(133, out);
  6571. }
  6572. jsxReadEntity() {
  6573. const startPos = ++this.state.pos;
  6574. if (this.codePointAtPos(this.state.pos) === 35) {
  6575. ++this.state.pos;
  6576. let radix = 10;
  6577. if (this.codePointAtPos(this.state.pos) === 120) {
  6578. radix = 16;
  6579. ++this.state.pos;
  6580. }
  6581. const codePoint = this.readInt(radix, undefined, false, "bail");
  6582. if (codePoint !== null && this.codePointAtPos(this.state.pos) === 59) {
  6583. ++this.state.pos;
  6584. return String.fromCodePoint(codePoint);
  6585. }
  6586. } else {
  6587. let count = 0;
  6588. let semi = false;
  6589. while (count++ < 10 && this.state.pos < this.length && !(semi = this.codePointAtPos(this.state.pos) === 59)) {
  6590. ++this.state.pos;
  6591. }
  6592. if (semi) {
  6593. const desc = this.input.slice(startPos, this.state.pos);
  6594. const entity = entities[desc];
  6595. ++this.state.pos;
  6596. if (entity) {
  6597. return entity;
  6598. }
  6599. }
  6600. }
  6601. this.state.pos = startPos;
  6602. return "&";
  6603. }
  6604. jsxReadWord() {
  6605. let ch;
  6606. const start = this.state.pos;
  6607. do {
  6608. ch = this.input.charCodeAt(++this.state.pos);
  6609. } while (isIdentifierChar(ch) || ch === 45);
  6610. this.finishToken(140, this.input.slice(start, this.state.pos));
  6611. }
  6612. jsxParseIdentifier() {
  6613. const node = this.startNode();
  6614. if (this.match(140)) {
  6615. node.name = this.state.value;
  6616. } else if (tokenIsKeyword(this.state.type)) {
  6617. node.name = tokenLabelName(this.state.type);
  6618. } else {
  6619. this.unexpected();
  6620. }
  6621. this.next();
  6622. return this.finishNode(node, "JSXIdentifier");
  6623. }
  6624. jsxParseNamespacedName() {
  6625. const startLoc = this.state.startLoc;
  6626. const name = this.jsxParseIdentifier();
  6627. if (!this.eat(14)) return name;
  6628. const node = this.startNodeAt(startLoc);
  6629. node.namespace = name;
  6630. node.name = this.jsxParseIdentifier();
  6631. return this.finishNode(node, "JSXNamespacedName");
  6632. }
  6633. jsxParseElementName() {
  6634. const startLoc = this.state.startLoc;
  6635. let node = this.jsxParseNamespacedName();
  6636. if (node.type === "JSXNamespacedName") {
  6637. return node;
  6638. }
  6639. while (this.eat(16)) {
  6640. const newNode = this.startNodeAt(startLoc);
  6641. newNode.object = node;
  6642. newNode.property = this.jsxParseIdentifier();
  6643. node = this.finishNode(newNode, "JSXMemberExpression");
  6644. }
  6645. return node;
  6646. }
  6647. jsxParseAttributeValue() {
  6648. let node;
  6649. switch (this.state.type) {
  6650. case 5:
  6651. node = this.startNode();
  6652. this.setContext(types.brace);
  6653. this.next();
  6654. node = this.jsxParseExpressionContainer(node, types.j_oTag);
  6655. if (node.expression.type === "JSXEmptyExpression") {
  6656. this.raise(JsxErrors.AttributeIsEmpty, node);
  6657. }
  6658. return node;
  6659. case 142:
  6660. case 133:
  6661. return this.parseExprAtom();
  6662. default:
  6663. throw this.raise(JsxErrors.UnsupportedJsxValue, this.state.startLoc);
  6664. }
  6665. }
  6666. jsxParseEmptyExpression() {
  6667. const node = this.startNodeAt(this.state.lastTokEndLoc);
  6668. return this.finishNodeAt(node, "JSXEmptyExpression", this.state.startLoc);
  6669. }
  6670. jsxParseSpreadChild(node) {
  6671. this.next();
  6672. node.expression = this.parseExpression();
  6673. this.setContext(types.j_expr);
  6674. this.state.canStartJSXElement = true;
  6675. this.expect(8);
  6676. return this.finishNode(node, "JSXSpreadChild");
  6677. }
  6678. jsxParseExpressionContainer(node, previousContext) {
  6679. if (this.match(8)) {
  6680. node.expression = this.jsxParseEmptyExpression();
  6681. } else {
  6682. const expression = this.parseExpression();
  6683. node.expression = expression;
  6684. }
  6685. this.setContext(previousContext);
  6686. this.state.canStartJSXElement = true;
  6687. this.expect(8);
  6688. return this.finishNode(node, "JSXExpressionContainer");
  6689. }
  6690. jsxParseAttribute() {
  6691. const node = this.startNode();
  6692. if (this.match(5)) {
  6693. this.setContext(types.brace);
  6694. this.next();
  6695. this.expect(21);
  6696. node.argument = this.parseMaybeAssignAllowIn();
  6697. this.setContext(types.j_oTag);
  6698. this.state.canStartJSXElement = true;
  6699. this.expect(8);
  6700. return this.finishNode(node, "JSXSpreadAttribute");
  6701. }
  6702. node.name = this.jsxParseNamespacedName();
  6703. node.value = this.eat(29) ? this.jsxParseAttributeValue() : null;
  6704. return this.finishNode(node, "JSXAttribute");
  6705. }
  6706. jsxParseOpeningElementAt(startLoc) {
  6707. const node = this.startNodeAt(startLoc);
  6708. if (this.eat(143)) {
  6709. return this.finishNode(node, "JSXOpeningFragment");
  6710. }
  6711. node.name = this.jsxParseElementName();
  6712. return this.jsxParseOpeningElementAfterName(node);
  6713. }
  6714. jsxParseOpeningElementAfterName(node) {
  6715. const attributes = [];
  6716. while (!this.match(56) && !this.match(143)) {
  6717. attributes.push(this.jsxParseAttribute());
  6718. }
  6719. node.attributes = attributes;
  6720. node.selfClosing = this.eat(56);
  6721. this.expect(143);
  6722. return this.finishNode(node, "JSXOpeningElement");
  6723. }
  6724. jsxParseClosingElementAt(startLoc) {
  6725. const node = this.startNodeAt(startLoc);
  6726. if (this.eat(143)) {
  6727. return this.finishNode(node, "JSXClosingFragment");
  6728. }
  6729. node.name = this.jsxParseElementName();
  6730. this.expect(143);
  6731. return this.finishNode(node, "JSXClosingElement");
  6732. }
  6733. jsxParseElementAt(startLoc) {
  6734. const node = this.startNodeAt(startLoc);
  6735. const children = [];
  6736. const openingElement = this.jsxParseOpeningElementAt(startLoc);
  6737. let closingElement = null;
  6738. if (!openingElement.selfClosing) {
  6739. contents: for (;;) {
  6740. switch (this.state.type) {
  6741. case 142:
  6742. startLoc = this.state.startLoc;
  6743. this.next();
  6744. if (this.eat(56)) {
  6745. closingElement = this.jsxParseClosingElementAt(startLoc);
  6746. break contents;
  6747. }
  6748. children.push(this.jsxParseElementAt(startLoc));
  6749. break;
  6750. case 141:
  6751. children.push(this.parseLiteral(this.state.value, "JSXText"));
  6752. break;
  6753. case 5:
  6754. {
  6755. const node = this.startNode();
  6756. this.setContext(types.brace);
  6757. this.next();
  6758. if (this.match(21)) {
  6759. children.push(this.jsxParseSpreadChild(node));
  6760. } else {
  6761. children.push(this.jsxParseExpressionContainer(node, types.j_expr));
  6762. }
  6763. break;
  6764. }
  6765. default:
  6766. this.unexpected();
  6767. }
  6768. }
  6769. if (isFragment(openingElement) && !isFragment(closingElement) && closingElement !== null) {
  6770. this.raise(JsxErrors.MissingClosingTagFragment, closingElement);
  6771. } else if (!isFragment(openingElement) && isFragment(closingElement)) {
  6772. this.raise(JsxErrors.MissingClosingTagElement, closingElement, {
  6773. openingTagName: getQualifiedJSXName(openingElement.name)
  6774. });
  6775. } else if (!isFragment(openingElement) && !isFragment(closingElement)) {
  6776. if (getQualifiedJSXName(closingElement.name) !== getQualifiedJSXName(openingElement.name)) {
  6777. this.raise(JsxErrors.MissingClosingTagElement, closingElement, {
  6778. openingTagName: getQualifiedJSXName(openingElement.name)
  6779. });
  6780. }
  6781. }
  6782. }
  6783. if (isFragment(openingElement)) {
  6784. node.openingFragment = openingElement;
  6785. node.closingFragment = closingElement;
  6786. } else {
  6787. node.openingElement = openingElement;
  6788. node.closingElement = closingElement;
  6789. }
  6790. node.children = children;
  6791. if (this.match(47)) {
  6792. throw this.raise(JsxErrors.UnwrappedAdjacentJSXElements, this.state.startLoc);
  6793. }
  6794. return isFragment(openingElement) ? this.finishNode(node, "JSXFragment") : this.finishNode(node, "JSXElement");
  6795. }
  6796. jsxParseElement() {
  6797. const startLoc = this.state.startLoc;
  6798. this.next();
  6799. return this.jsxParseElementAt(startLoc);
  6800. }
  6801. setContext(newContext) {
  6802. const {
  6803. context
  6804. } = this.state;
  6805. context[context.length - 1] = newContext;
  6806. }
  6807. parseExprAtom(refExpressionErrors) {
  6808. if (this.match(142)) {
  6809. return this.jsxParseElement();
  6810. } else if (this.match(47) && this.input.charCodeAt(this.state.pos) !== 33) {
  6811. this.replaceToken(142);
  6812. return this.jsxParseElement();
  6813. } else {
  6814. return super.parseExprAtom(refExpressionErrors);
  6815. }
  6816. }
  6817. skipSpace() {
  6818. const curContext = this.curContext();
  6819. if (!curContext.preserveSpace) super.skipSpace();
  6820. }
  6821. getTokenFromCode(code) {
  6822. const context = this.curContext();
  6823. if (context === types.j_expr) {
  6824. this.jsxReadToken();
  6825. return;
  6826. }
  6827. if (context === types.j_oTag || context === types.j_cTag) {
  6828. if (isIdentifierStart(code)) {
  6829. this.jsxReadWord();
  6830. return;
  6831. }
  6832. if (code === 62) {
  6833. ++this.state.pos;
  6834. this.finishToken(143);
  6835. return;
  6836. }
  6837. if ((code === 34 || code === 39) && context === types.j_oTag) {
  6838. this.jsxReadString(code);
  6839. return;
  6840. }
  6841. }
  6842. if (code === 60 && this.state.canStartJSXElement && this.input.charCodeAt(this.state.pos + 1) !== 33) {
  6843. ++this.state.pos;
  6844. this.finishToken(142);
  6845. return;
  6846. }
  6847. super.getTokenFromCode(code);
  6848. }
  6849. updateContext(prevType) {
  6850. const {
  6851. context,
  6852. type
  6853. } = this.state;
  6854. if (type === 56 && prevType === 142) {
  6855. context.splice(-2, 2, types.j_cTag);
  6856. this.state.canStartJSXElement = false;
  6857. } else if (type === 142) {
  6858. context.push(types.j_oTag);
  6859. } else if (type === 143) {
  6860. const out = context[context.length - 1];
  6861. if (out === types.j_oTag && prevType === 56 || out === types.j_cTag) {
  6862. context.pop();
  6863. this.state.canStartJSXElement = context[context.length - 1] === types.j_expr;
  6864. } else {
  6865. this.setContext(types.j_expr);
  6866. this.state.canStartJSXElement = true;
  6867. }
  6868. } else {
  6869. this.state.canStartJSXElement = tokenComesBeforeExpression(type);
  6870. }
  6871. }
  6872. };
  6873. class TypeScriptScope extends Scope {
  6874. constructor(...args) {
  6875. super(...args);
  6876. this.tsNames = new Map();
  6877. }
  6878. }
  6879. class TypeScriptScopeHandler extends ScopeHandler {
  6880. constructor(...args) {
  6881. super(...args);
  6882. this.importsStack = [];
  6883. }
  6884. createScope(flags) {
  6885. this.importsStack.push(new Set());
  6886. return new TypeScriptScope(flags);
  6887. }
  6888. enter(flags) {
  6889. if (flags === 256) {
  6890. this.importsStack.push(new Set());
  6891. }
  6892. super.enter(flags);
  6893. }
  6894. exit() {
  6895. const flags = super.exit();
  6896. if (flags === 256) {
  6897. this.importsStack.pop();
  6898. }
  6899. return flags;
  6900. }
  6901. hasImport(name, allowShadow) {
  6902. const len = this.importsStack.length;
  6903. if (this.importsStack[len - 1].has(name)) {
  6904. return true;
  6905. }
  6906. if (!allowShadow && len > 1) {
  6907. for (let i = 0; i < len - 1; i++) {
  6908. if (this.importsStack[i].has(name)) return true;
  6909. }
  6910. }
  6911. return false;
  6912. }
  6913. declareName(name, bindingType, loc) {
  6914. if (bindingType & 4096) {
  6915. if (this.hasImport(name, true)) {
  6916. this.parser.raise(Errors.VarRedeclaration, loc, {
  6917. identifierName: name
  6918. });
  6919. }
  6920. this.importsStack[this.importsStack.length - 1].add(name);
  6921. return;
  6922. }
  6923. const scope = this.currentScope();
  6924. let type = scope.tsNames.get(name) || 0;
  6925. if (bindingType & 1024) {
  6926. this.maybeExportDefined(scope, name);
  6927. scope.tsNames.set(name, type | 16);
  6928. return;
  6929. }
  6930. super.declareName(name, bindingType, loc);
  6931. if (bindingType & 2) {
  6932. if (!(bindingType & 1)) {
  6933. this.checkRedeclarationInScope(scope, name, bindingType, loc);
  6934. this.maybeExportDefined(scope, name);
  6935. }
  6936. type = type | 1;
  6937. }
  6938. if (bindingType & 256) {
  6939. type = type | 2;
  6940. }
  6941. if (bindingType & 512) {
  6942. type = type | 4;
  6943. }
  6944. if (bindingType & 128) {
  6945. type = type | 8;
  6946. }
  6947. if (type) scope.tsNames.set(name, type);
  6948. }
  6949. isRedeclaredInScope(scope, name, bindingType) {
  6950. const type = scope.tsNames.get(name);
  6951. if ((type & 2) > 0) {
  6952. if (bindingType & 256) {
  6953. const isConst = !!(bindingType & 512);
  6954. const wasConst = (type & 4) > 0;
  6955. return isConst !== wasConst;
  6956. }
  6957. return true;
  6958. }
  6959. if (bindingType & 128 && (type & 8) > 0) {
  6960. if (scope.names.get(name) & 2) {
  6961. return !!(bindingType & 1);
  6962. } else {
  6963. return false;
  6964. }
  6965. }
  6966. if (bindingType & 2 && (type & 1) > 0) {
  6967. return true;
  6968. }
  6969. return super.isRedeclaredInScope(scope, name, bindingType);
  6970. }
  6971. checkLocalExport(id) {
  6972. const {
  6973. name
  6974. } = id;
  6975. if (this.hasImport(name)) return;
  6976. const len = this.scopeStack.length;
  6977. for (let i = len - 1; i >= 0; i--) {
  6978. const scope = this.scopeStack[i];
  6979. const type = scope.tsNames.get(name);
  6980. if ((type & 1) > 0 || (type & 16) > 0) {
  6981. return;
  6982. }
  6983. }
  6984. super.checkLocalExport(id);
  6985. }
  6986. }
  6987. const unwrapParenthesizedExpression = node => {
  6988. return node.type === "ParenthesizedExpression" ? unwrapParenthesizedExpression(node.expression) : node;
  6989. };
  6990. class LValParser extends NodeUtils {
  6991. toAssignable(node, isLHS = false) {
  6992. var _node$extra, _node$extra3;
  6993. let parenthesized = undefined;
  6994. if (node.type === "ParenthesizedExpression" || (_node$extra = node.extra) != null && _node$extra.parenthesized) {
  6995. parenthesized = unwrapParenthesizedExpression(node);
  6996. if (isLHS) {
  6997. if (parenthesized.type === "Identifier") {
  6998. this.expressionScope.recordArrowParameterBindingError(Errors.InvalidParenthesizedAssignment, node);
  6999. } else if (parenthesized.type !== "MemberExpression" && !this.isOptionalMemberExpression(parenthesized)) {
  7000. this.raise(Errors.InvalidParenthesizedAssignment, node);
  7001. }
  7002. } else {
  7003. this.raise(Errors.InvalidParenthesizedAssignment, node);
  7004. }
  7005. }
  7006. switch (node.type) {
  7007. case "Identifier":
  7008. case "ObjectPattern":
  7009. case "ArrayPattern":
  7010. case "AssignmentPattern":
  7011. case "RestElement":
  7012. break;
  7013. case "ObjectExpression":
  7014. node.type = "ObjectPattern";
  7015. for (let i = 0, length = node.properties.length, last = length - 1; i < length; i++) {
  7016. var _node$extra2;
  7017. const prop = node.properties[i];
  7018. const isLast = i === last;
  7019. this.toAssignableObjectExpressionProp(prop, isLast, isLHS);
  7020. if (isLast && prop.type === "RestElement" && (_node$extra2 = node.extra) != null && _node$extra2.trailingCommaLoc) {
  7021. this.raise(Errors.RestTrailingComma, node.extra.trailingCommaLoc);
  7022. }
  7023. }
  7024. break;
  7025. case "ObjectProperty":
  7026. {
  7027. const {
  7028. key,
  7029. value
  7030. } = node;
  7031. if (this.isPrivateName(key)) {
  7032. this.classScope.usePrivateName(this.getPrivateNameSV(key), key.loc.start);
  7033. }
  7034. this.toAssignable(value, isLHS);
  7035. break;
  7036. }
  7037. case "SpreadElement":
  7038. {
  7039. throw new Error("Internal @babel/parser error (this is a bug, please report it)." + " SpreadElement should be converted by .toAssignable's caller.");
  7040. }
  7041. case "ArrayExpression":
  7042. node.type = "ArrayPattern";
  7043. this.toAssignableList(node.elements, (_node$extra3 = node.extra) == null ? void 0 : _node$extra3.trailingCommaLoc, isLHS);
  7044. break;
  7045. case "AssignmentExpression":
  7046. if (node.operator !== "=") {
  7047. this.raise(Errors.MissingEqInAssignment, node.left.loc.end);
  7048. }
  7049. node.type = "AssignmentPattern";
  7050. delete node.operator;
  7051. this.toAssignable(node.left, isLHS);
  7052. break;
  7053. case "ParenthesizedExpression":
  7054. this.toAssignable(parenthesized, isLHS);
  7055. break;
  7056. }
  7057. }
  7058. toAssignableObjectExpressionProp(prop, isLast, isLHS) {
  7059. if (prop.type === "ObjectMethod") {
  7060. this.raise(prop.kind === "get" || prop.kind === "set" ? Errors.PatternHasAccessor : Errors.PatternHasMethod, prop.key);
  7061. } else if (prop.type === "SpreadElement") {
  7062. prop.type = "RestElement";
  7063. const arg = prop.argument;
  7064. this.checkToRestConversion(arg, false);
  7065. this.toAssignable(arg, isLHS);
  7066. if (!isLast) {
  7067. this.raise(Errors.RestTrailingComma, prop);
  7068. }
  7069. } else {
  7070. this.toAssignable(prop, isLHS);
  7071. }
  7072. }
  7073. toAssignableList(exprList, trailingCommaLoc, isLHS) {
  7074. const end = exprList.length - 1;
  7075. for (let i = 0; i <= end; i++) {
  7076. const elt = exprList[i];
  7077. if (!elt) continue;
  7078. if (elt.type === "SpreadElement") {
  7079. elt.type = "RestElement";
  7080. const arg = elt.argument;
  7081. this.checkToRestConversion(arg, true);
  7082. this.toAssignable(arg, isLHS);
  7083. } else {
  7084. this.toAssignable(elt, isLHS);
  7085. }
  7086. if (elt.type === "RestElement") {
  7087. if (i < end) {
  7088. this.raise(Errors.RestTrailingComma, elt);
  7089. } else if (trailingCommaLoc) {
  7090. this.raise(Errors.RestTrailingComma, trailingCommaLoc);
  7091. }
  7092. }
  7093. }
  7094. }
  7095. isAssignable(node, isBinding) {
  7096. switch (node.type) {
  7097. case "Identifier":
  7098. case "ObjectPattern":
  7099. case "ArrayPattern":
  7100. case "AssignmentPattern":
  7101. case "RestElement":
  7102. return true;
  7103. case "ObjectExpression":
  7104. {
  7105. const last = node.properties.length - 1;
  7106. return node.properties.every((prop, i) => {
  7107. return prop.type !== "ObjectMethod" && (i === last || prop.type !== "SpreadElement") && this.isAssignable(prop);
  7108. });
  7109. }
  7110. case "ObjectProperty":
  7111. return this.isAssignable(node.value);
  7112. case "SpreadElement":
  7113. return this.isAssignable(node.argument);
  7114. case "ArrayExpression":
  7115. return node.elements.every(element => element === null || this.isAssignable(element));
  7116. case "AssignmentExpression":
  7117. return node.operator === "=";
  7118. case "ParenthesizedExpression":
  7119. return this.isAssignable(node.expression);
  7120. case "MemberExpression":
  7121. case "OptionalMemberExpression":
  7122. return !isBinding;
  7123. default:
  7124. return false;
  7125. }
  7126. }
  7127. toReferencedList(exprList, isParenthesizedExpr) {
  7128. return exprList;
  7129. }
  7130. toReferencedListDeep(exprList, isParenthesizedExpr) {
  7131. this.toReferencedList(exprList, isParenthesizedExpr);
  7132. for (const expr of exprList) {
  7133. if ((expr == null ? void 0 : expr.type) === "ArrayExpression") {
  7134. this.toReferencedListDeep(expr.elements);
  7135. }
  7136. }
  7137. }
  7138. parseSpread(refExpressionErrors) {
  7139. const node = this.startNode();
  7140. this.next();
  7141. node.argument = this.parseMaybeAssignAllowIn(refExpressionErrors, undefined);
  7142. return this.finishNode(node, "SpreadElement");
  7143. }
  7144. parseRestBinding() {
  7145. const node = this.startNode();
  7146. this.next();
  7147. node.argument = this.parseBindingAtom();
  7148. return this.finishNode(node, "RestElement");
  7149. }
  7150. parseBindingAtom() {
  7151. switch (this.state.type) {
  7152. case 0:
  7153. {
  7154. const node = this.startNode();
  7155. this.next();
  7156. node.elements = this.parseBindingList(3, 93, 1);
  7157. return this.finishNode(node, "ArrayPattern");
  7158. }
  7159. case 5:
  7160. return this.parseObjectLike(8, true);
  7161. }
  7162. return this.parseIdentifier();
  7163. }
  7164. parseBindingList(close, closeCharCode, flags) {
  7165. const allowEmpty = flags & 1;
  7166. const elts = [];
  7167. let first = true;
  7168. while (!this.eat(close)) {
  7169. if (first) {
  7170. first = false;
  7171. } else {
  7172. this.expect(12);
  7173. }
  7174. if (allowEmpty && this.match(12)) {
  7175. elts.push(null);
  7176. } else if (this.eat(close)) {
  7177. break;
  7178. } else if (this.match(21)) {
  7179. elts.push(this.parseAssignableListItemTypes(this.parseRestBinding(), flags));
  7180. if (!this.checkCommaAfterRest(closeCharCode)) {
  7181. this.expect(close);
  7182. break;
  7183. }
  7184. } else {
  7185. const decorators = [];
  7186. if (this.match(26) && this.hasPlugin("decorators")) {
  7187. this.raise(Errors.UnsupportedParameterDecorator, this.state.startLoc);
  7188. }
  7189. while (this.match(26)) {
  7190. decorators.push(this.parseDecorator());
  7191. }
  7192. elts.push(this.parseAssignableListItem(flags, decorators));
  7193. }
  7194. }
  7195. return elts;
  7196. }
  7197. parseBindingRestProperty(prop) {
  7198. this.next();
  7199. prop.argument = this.parseIdentifier();
  7200. this.checkCommaAfterRest(125);
  7201. return this.finishNode(prop, "RestElement");
  7202. }
  7203. parseBindingProperty() {
  7204. const {
  7205. type,
  7206. startLoc
  7207. } = this.state;
  7208. if (type === 21) {
  7209. return this.parseBindingRestProperty(this.startNode());
  7210. }
  7211. const prop = this.startNode();
  7212. if (type === 138) {
  7213. this.expectPlugin("destructuringPrivate", startLoc);
  7214. this.classScope.usePrivateName(this.state.value, startLoc);
  7215. prop.key = this.parsePrivateName();
  7216. } else {
  7217. this.parsePropertyName(prop);
  7218. }
  7219. prop.method = false;
  7220. return this.parseObjPropValue(prop, startLoc, false, false, true, false);
  7221. }
  7222. parseAssignableListItem(flags, decorators) {
  7223. const left = this.parseMaybeDefault();
  7224. this.parseAssignableListItemTypes(left, flags);
  7225. const elt = this.parseMaybeDefault(left.loc.start, left);
  7226. if (decorators.length) {
  7227. left.decorators = decorators;
  7228. }
  7229. return elt;
  7230. }
  7231. parseAssignableListItemTypes(param, flags) {
  7232. return param;
  7233. }
  7234. parseMaybeDefault(startLoc, left) {
  7235. var _startLoc, _left;
  7236. (_startLoc = startLoc) != null ? _startLoc : startLoc = this.state.startLoc;
  7237. left = (_left = left) != null ? _left : this.parseBindingAtom();
  7238. if (!this.eat(29)) return left;
  7239. const node = this.startNodeAt(startLoc);
  7240. node.left = left;
  7241. node.right = this.parseMaybeAssignAllowIn();
  7242. return this.finishNode(node, "AssignmentPattern");
  7243. }
  7244. isValidLVal(type, isUnparenthesizedInAssign, binding) {
  7245. switch (type) {
  7246. case "AssignmentPattern":
  7247. return "left";
  7248. case "RestElement":
  7249. return "argument";
  7250. case "ObjectProperty":
  7251. return "value";
  7252. case "ParenthesizedExpression":
  7253. return "expression";
  7254. case "ArrayPattern":
  7255. return "elements";
  7256. case "ObjectPattern":
  7257. return "properties";
  7258. }
  7259. return false;
  7260. }
  7261. isOptionalMemberExpression(expression) {
  7262. return expression.type === "OptionalMemberExpression";
  7263. }
  7264. checkLVal(expression, ancestor, binding = 64, checkClashes = false, strictModeChanged = false, hasParenthesizedAncestor = false) {
  7265. var _expression$extra;
  7266. const type = expression.type;
  7267. if (this.isObjectMethod(expression)) return;
  7268. const isOptionalMemberExpression = this.isOptionalMemberExpression(expression);
  7269. if (isOptionalMemberExpression || type === "MemberExpression") {
  7270. if (isOptionalMemberExpression) {
  7271. this.expectPlugin("optionalChainingAssign", expression.loc.start);
  7272. if (ancestor.type !== "AssignmentExpression") {
  7273. this.raise(Errors.InvalidLhsOptionalChaining, expression, {
  7274. ancestor
  7275. });
  7276. }
  7277. }
  7278. if (binding !== 64) {
  7279. this.raise(Errors.InvalidPropertyBindingPattern, expression);
  7280. }
  7281. return;
  7282. }
  7283. if (type === "Identifier") {
  7284. this.checkIdentifier(expression, binding, strictModeChanged);
  7285. const {
  7286. name
  7287. } = expression;
  7288. if (checkClashes) {
  7289. if (checkClashes.has(name)) {
  7290. this.raise(Errors.ParamDupe, expression);
  7291. } else {
  7292. checkClashes.add(name);
  7293. }
  7294. }
  7295. return;
  7296. }
  7297. const validity = this.isValidLVal(type, !(hasParenthesizedAncestor || (_expression$extra = expression.extra) != null && _expression$extra.parenthesized) && ancestor.type === "AssignmentExpression", binding);
  7298. if (validity === true) return;
  7299. if (validity === false) {
  7300. const ParseErrorClass = binding === 64 ? Errors.InvalidLhs : Errors.InvalidLhsBinding;
  7301. this.raise(ParseErrorClass, expression, {
  7302. ancestor
  7303. });
  7304. return;
  7305. }
  7306. let key, isParenthesizedExpression;
  7307. if (typeof validity === "string") {
  7308. key = validity;
  7309. isParenthesizedExpression = type === "ParenthesizedExpression";
  7310. } else {
  7311. [key, isParenthesizedExpression] = validity;
  7312. }
  7313. const nextAncestor = type === "ArrayPattern" || type === "ObjectPattern" ? {
  7314. type
  7315. } : ancestor;
  7316. const val = expression[key];
  7317. if (Array.isArray(val)) {
  7318. for (const child of val) {
  7319. if (child) {
  7320. this.checkLVal(child, nextAncestor, binding, checkClashes, strictModeChanged, isParenthesizedExpression);
  7321. }
  7322. }
  7323. } else if (val) {
  7324. this.checkLVal(val, nextAncestor, binding, checkClashes, strictModeChanged, isParenthesizedExpression);
  7325. }
  7326. }
  7327. checkIdentifier(at, bindingType, strictModeChanged = false) {
  7328. if (this.state.strict && (strictModeChanged ? isStrictBindReservedWord(at.name, this.inModule) : isStrictBindOnlyReservedWord(at.name))) {
  7329. if (bindingType === 64) {
  7330. this.raise(Errors.StrictEvalArguments, at, {
  7331. referenceName: at.name
  7332. });
  7333. } else {
  7334. this.raise(Errors.StrictEvalArgumentsBinding, at, {
  7335. bindingName: at.name
  7336. });
  7337. }
  7338. }
  7339. if (bindingType & 8192 && at.name === "let") {
  7340. this.raise(Errors.LetInLexicalBinding, at);
  7341. }
  7342. if (!(bindingType & 64)) {
  7343. this.declareNameFromIdentifier(at, bindingType);
  7344. }
  7345. }
  7346. declareNameFromIdentifier(identifier, binding) {
  7347. this.scope.declareName(identifier.name, binding, identifier.loc.start);
  7348. }
  7349. checkToRestConversion(node, allowPattern) {
  7350. switch (node.type) {
  7351. case "ParenthesizedExpression":
  7352. this.checkToRestConversion(node.expression, allowPattern);
  7353. break;
  7354. case "Identifier":
  7355. case "MemberExpression":
  7356. break;
  7357. case "ArrayExpression":
  7358. case "ObjectExpression":
  7359. if (allowPattern) break;
  7360. default:
  7361. this.raise(Errors.InvalidRestAssignmentPattern, node);
  7362. }
  7363. }
  7364. checkCommaAfterRest(close) {
  7365. if (!this.match(12)) {
  7366. return false;
  7367. }
  7368. this.raise(this.lookaheadCharCode() === close ? Errors.RestTrailingComma : Errors.ElementAfterRest, this.state.startLoc);
  7369. return true;
  7370. }
  7371. }
  7372. function nonNull(x) {
  7373. if (x == null) {
  7374. throw new Error(`Unexpected ${x} value.`);
  7375. }
  7376. return x;
  7377. }
  7378. function assert(x) {
  7379. if (!x) {
  7380. throw new Error("Assert fail");
  7381. }
  7382. }
  7383. const TSErrors = ParseErrorEnum`typescript`({
  7384. AbstractMethodHasImplementation: ({
  7385. methodName
  7386. }) => `Method '${methodName}' cannot have an implementation because it is marked abstract.`,
  7387. AbstractPropertyHasInitializer: ({
  7388. propertyName
  7389. }) => `Property '${propertyName}' cannot have an initializer because it is marked abstract.`,
  7390. AccessorCannotBeOptional: "An 'accessor' property cannot be declared optional.",
  7391. AccessorCannotDeclareThisParameter: "'get' and 'set' accessors cannot declare 'this' parameters.",
  7392. AccessorCannotHaveTypeParameters: "An accessor cannot have type parameters.",
  7393. ClassMethodHasDeclare: "Class methods cannot have the 'declare' modifier.",
  7394. ClassMethodHasReadonly: "Class methods cannot have the 'readonly' modifier.",
  7395. ConstInitializerMustBeStringOrNumericLiteralOrLiteralEnumReference: "A 'const' initializer in an ambient context must be a string or numeric literal or literal enum reference.",
  7396. ConstructorHasTypeParameters: "Type parameters cannot appear on a constructor declaration.",
  7397. DeclareAccessor: ({
  7398. kind
  7399. }) => `'declare' is not allowed in ${kind}ters.`,
  7400. DeclareClassFieldHasInitializer: "Initializers are not allowed in ambient contexts.",
  7401. DeclareFunctionHasImplementation: "An implementation cannot be declared in ambient contexts.",
  7402. DuplicateAccessibilityModifier: ({
  7403. modifier
  7404. }) => `Accessibility modifier already seen.`,
  7405. DuplicateModifier: ({
  7406. modifier
  7407. }) => `Duplicate modifier: '${modifier}'.`,
  7408. EmptyHeritageClauseType: ({
  7409. token
  7410. }) => `'${token}' list cannot be empty.`,
  7411. EmptyTypeArguments: "Type argument list cannot be empty.",
  7412. EmptyTypeParameters: "Type parameter list cannot be empty.",
  7413. ExpectedAmbientAfterExportDeclare: "'export declare' must be followed by an ambient declaration.",
  7414. ImportAliasHasImportType: "An import alias can not use 'import type'.",
  7415. ImportReflectionHasImportType: "An `import module` declaration can not use `type` modifier",
  7416. IncompatibleModifiers: ({
  7417. modifiers
  7418. }) => `'${modifiers[0]}' modifier cannot be used with '${modifiers[1]}' modifier.`,
  7419. IndexSignatureHasAbstract: "Index signatures cannot have the 'abstract' modifier.",
  7420. IndexSignatureHasAccessibility: ({
  7421. modifier
  7422. }) => `Index signatures cannot have an accessibility modifier ('${modifier}').`,
  7423. IndexSignatureHasDeclare: "Index signatures cannot have the 'declare' modifier.",
  7424. IndexSignatureHasOverride: "'override' modifier cannot appear on an index signature.",
  7425. IndexSignatureHasStatic: "Index signatures cannot have the 'static' modifier.",
  7426. InitializerNotAllowedInAmbientContext: "Initializers are not allowed in ambient contexts.",
  7427. InvalidModifierOnTypeMember: ({
  7428. modifier
  7429. }) => `'${modifier}' modifier cannot appear on a type member.`,
  7430. InvalidModifierOnTypeParameter: ({
  7431. modifier
  7432. }) => `'${modifier}' modifier cannot appear on a type parameter.`,
  7433. InvalidModifierOnTypeParameterPositions: ({
  7434. modifier
  7435. }) => `'${modifier}' modifier can only appear on a type parameter of a class, interface or type alias.`,
  7436. InvalidModifiersOrder: ({
  7437. orderedModifiers
  7438. }) => `'${orderedModifiers[0]}' modifier must precede '${orderedModifiers[1]}' modifier.`,
  7439. InvalidPropertyAccessAfterInstantiationExpression: "Invalid property access after an instantiation expression. " + "You can either wrap the instantiation expression in parentheses, or delete the type arguments.",
  7440. InvalidTupleMemberLabel: "Tuple members must be labeled with a simple identifier.",
  7441. MissingInterfaceName: "'interface' declarations must be followed by an identifier.",
  7442. NonAbstractClassHasAbstractMethod: "Abstract methods can only appear within an abstract class.",
  7443. NonClassMethodPropertyHasAbstractModifer: "'abstract' modifier can only appear on a class, method, or property declaration.",
  7444. OptionalTypeBeforeRequired: "A required element cannot follow an optional element.",
  7445. OverrideNotInSubClass: "This member cannot have an 'override' modifier because its containing class does not extend another class.",
  7446. PatternIsOptional: "A binding pattern parameter cannot be optional in an implementation signature.",
  7447. PrivateElementHasAbstract: "Private elements cannot have the 'abstract' modifier.",
  7448. PrivateElementHasAccessibility: ({
  7449. modifier
  7450. }) => `Private elements cannot have an accessibility modifier ('${modifier}').`,
  7451. ReadonlyForMethodSignature: "'readonly' modifier can only appear on a property declaration or index signature.",
  7452. ReservedArrowTypeParam: "This syntax is reserved in files with the .mts or .cts extension. Add a trailing comma, as in `<T,>() => ...`.",
  7453. ReservedTypeAssertion: "This syntax is reserved in files with the .mts or .cts extension. Use an `as` expression instead.",
  7454. SetAccessorCannotHaveOptionalParameter: "A 'set' accessor cannot have an optional parameter.",
  7455. SetAccessorCannotHaveRestParameter: "A 'set' accessor cannot have rest parameter.",
  7456. SetAccessorCannotHaveReturnType: "A 'set' accessor cannot have a return type annotation.",
  7457. SingleTypeParameterWithoutTrailingComma: ({
  7458. typeParameterName
  7459. }) => `Single type parameter ${typeParameterName} should have a trailing comma. Example usage: <${typeParameterName},>.`,
  7460. StaticBlockCannotHaveModifier: "Static class blocks cannot have any modifier.",
  7461. TupleOptionalAfterType: "A labeled tuple optional element must be declared using a question mark after the name and before the colon (`name?: type`), rather than after the type (`name: type?`).",
  7462. TypeAnnotationAfterAssign: "Type annotations must come before default assignments, e.g. instead of `age = 25: number` use `age: number = 25`.",
  7463. TypeImportCannotSpecifyDefaultAndNamed: "A type-only import can specify a default import or named bindings, but not both.",
  7464. TypeModifierIsUsedInTypeExports: "The 'type' modifier cannot be used on a named export when 'export type' is used on its export statement.",
  7465. TypeModifierIsUsedInTypeImports: "The 'type' modifier cannot be used on a named import when 'import type' is used on its import statement.",
  7466. UnexpectedParameterModifier: "A parameter property is only allowed in a constructor implementation.",
  7467. UnexpectedReadonly: "'readonly' type modifier is only permitted on array and tuple literal types.",
  7468. UnexpectedTypeAnnotation: "Did not expect a type annotation here.",
  7469. UnexpectedTypeCastInParameter: "Unexpected type cast in parameter position.",
  7470. UnsupportedImportTypeArgument: "Argument in a type import must be a string literal.",
  7471. UnsupportedParameterPropertyKind: "A parameter property may not be declared using a binding pattern.",
  7472. UnsupportedSignatureParameterKind: ({
  7473. type
  7474. }) => `Name in a signature must be an Identifier, ObjectPattern or ArrayPattern, instead got ${type}.`
  7475. });
  7476. function keywordTypeFromName(value) {
  7477. switch (value) {
  7478. case "any":
  7479. return "TSAnyKeyword";
  7480. case "boolean":
  7481. return "TSBooleanKeyword";
  7482. case "bigint":
  7483. return "TSBigIntKeyword";
  7484. case "never":
  7485. return "TSNeverKeyword";
  7486. case "number":
  7487. return "TSNumberKeyword";
  7488. case "object":
  7489. return "TSObjectKeyword";
  7490. case "string":
  7491. return "TSStringKeyword";
  7492. case "symbol":
  7493. return "TSSymbolKeyword";
  7494. case "undefined":
  7495. return "TSUndefinedKeyword";
  7496. case "unknown":
  7497. return "TSUnknownKeyword";
  7498. default:
  7499. return undefined;
  7500. }
  7501. }
  7502. function tsIsAccessModifier(modifier) {
  7503. return modifier === "private" || modifier === "public" || modifier === "protected";
  7504. }
  7505. function tsIsVarianceAnnotations(modifier) {
  7506. return modifier === "in" || modifier === "out";
  7507. }
  7508. var typescript = superClass => class TypeScriptParserMixin extends superClass {
  7509. constructor(...args) {
  7510. super(...args);
  7511. this.tsParseInOutModifiers = this.tsParseModifiers.bind(this, {
  7512. allowedModifiers: ["in", "out"],
  7513. disallowedModifiers: ["const", "public", "private", "protected", "readonly", "declare", "abstract", "override"],
  7514. errorTemplate: TSErrors.InvalidModifierOnTypeParameter
  7515. });
  7516. this.tsParseConstModifier = this.tsParseModifiers.bind(this, {
  7517. allowedModifiers: ["const"],
  7518. disallowedModifiers: ["in", "out"],
  7519. errorTemplate: TSErrors.InvalidModifierOnTypeParameterPositions
  7520. });
  7521. this.tsParseInOutConstModifiers = this.tsParseModifiers.bind(this, {
  7522. allowedModifiers: ["in", "out", "const"],
  7523. disallowedModifiers: ["public", "private", "protected", "readonly", "declare", "abstract", "override"],
  7524. errorTemplate: TSErrors.InvalidModifierOnTypeParameter
  7525. });
  7526. }
  7527. getScopeHandler() {
  7528. return TypeScriptScopeHandler;
  7529. }
  7530. tsIsIdentifier() {
  7531. return tokenIsIdentifier(this.state.type);
  7532. }
  7533. tsTokenCanFollowModifier() {
  7534. return this.match(0) || this.match(5) || this.match(55) || this.match(21) || this.match(138) || this.isLiteralPropertyName();
  7535. }
  7536. tsNextTokenOnSameLineAndCanFollowModifier() {
  7537. this.next();
  7538. if (this.hasPrecedingLineBreak()) {
  7539. return false;
  7540. }
  7541. return this.tsTokenCanFollowModifier();
  7542. }
  7543. tsNextTokenCanFollowModifier() {
  7544. if (this.match(106)) {
  7545. this.next();
  7546. return this.tsTokenCanFollowModifier();
  7547. }
  7548. return this.tsNextTokenOnSameLineAndCanFollowModifier();
  7549. }
  7550. tsParseModifier(allowedModifiers, stopOnStartOfClassStaticBlock) {
  7551. if (!tokenIsIdentifier(this.state.type) && this.state.type !== 58 && this.state.type !== 75) {
  7552. return undefined;
  7553. }
  7554. const modifier = this.state.value;
  7555. if (allowedModifiers.includes(modifier)) {
  7556. if (stopOnStartOfClassStaticBlock && this.tsIsStartOfStaticBlocks()) {
  7557. return undefined;
  7558. }
  7559. if (this.tsTryParse(this.tsNextTokenCanFollowModifier.bind(this))) {
  7560. return modifier;
  7561. }
  7562. }
  7563. return undefined;
  7564. }
  7565. tsParseModifiers({
  7566. allowedModifiers,
  7567. disallowedModifiers,
  7568. stopOnStartOfClassStaticBlock,
  7569. errorTemplate = TSErrors.InvalidModifierOnTypeMember
  7570. }, modified) {
  7571. const enforceOrder = (loc, modifier, before, after) => {
  7572. if (modifier === before && modified[after]) {
  7573. this.raise(TSErrors.InvalidModifiersOrder, loc, {
  7574. orderedModifiers: [before, after]
  7575. });
  7576. }
  7577. };
  7578. const incompatible = (loc, modifier, mod1, mod2) => {
  7579. if (modified[mod1] && modifier === mod2 || modified[mod2] && modifier === mod1) {
  7580. this.raise(TSErrors.IncompatibleModifiers, loc, {
  7581. modifiers: [mod1, mod2]
  7582. });
  7583. }
  7584. };
  7585. for (;;) {
  7586. const {
  7587. startLoc
  7588. } = this.state;
  7589. const modifier = this.tsParseModifier(allowedModifiers.concat(disallowedModifiers != null ? disallowedModifiers : []), stopOnStartOfClassStaticBlock);
  7590. if (!modifier) break;
  7591. if (tsIsAccessModifier(modifier)) {
  7592. if (modified.accessibility) {
  7593. this.raise(TSErrors.DuplicateAccessibilityModifier, startLoc, {
  7594. modifier
  7595. });
  7596. } else {
  7597. enforceOrder(startLoc, modifier, modifier, "override");
  7598. enforceOrder(startLoc, modifier, modifier, "static");
  7599. enforceOrder(startLoc, modifier, modifier, "readonly");
  7600. modified.accessibility = modifier;
  7601. }
  7602. } else if (tsIsVarianceAnnotations(modifier)) {
  7603. if (modified[modifier]) {
  7604. this.raise(TSErrors.DuplicateModifier, startLoc, {
  7605. modifier
  7606. });
  7607. }
  7608. modified[modifier] = true;
  7609. enforceOrder(startLoc, modifier, "in", "out");
  7610. } else {
  7611. if (hasOwnProperty.call(modified, modifier)) {
  7612. this.raise(TSErrors.DuplicateModifier, startLoc, {
  7613. modifier
  7614. });
  7615. } else {
  7616. enforceOrder(startLoc, modifier, "static", "readonly");
  7617. enforceOrder(startLoc, modifier, "static", "override");
  7618. enforceOrder(startLoc, modifier, "override", "readonly");
  7619. enforceOrder(startLoc, modifier, "abstract", "override");
  7620. incompatible(startLoc, modifier, "declare", "override");
  7621. incompatible(startLoc, modifier, "static", "abstract");
  7622. }
  7623. modified[modifier] = true;
  7624. }
  7625. if (disallowedModifiers != null && disallowedModifiers.includes(modifier)) {
  7626. this.raise(errorTemplate, startLoc, {
  7627. modifier
  7628. });
  7629. }
  7630. }
  7631. }
  7632. tsIsListTerminator(kind) {
  7633. switch (kind) {
  7634. case "EnumMembers":
  7635. case "TypeMembers":
  7636. return this.match(8);
  7637. case "HeritageClauseElement":
  7638. return this.match(5);
  7639. case "TupleElementTypes":
  7640. return this.match(3);
  7641. case "TypeParametersOrArguments":
  7642. return this.match(48);
  7643. }
  7644. }
  7645. tsParseList(kind, parseElement) {
  7646. const result = [];
  7647. while (!this.tsIsListTerminator(kind)) {
  7648. result.push(parseElement());
  7649. }
  7650. return result;
  7651. }
  7652. tsParseDelimitedList(kind, parseElement, refTrailingCommaPos) {
  7653. return nonNull(this.tsParseDelimitedListWorker(kind, parseElement, true, refTrailingCommaPos));
  7654. }
  7655. tsParseDelimitedListWorker(kind, parseElement, expectSuccess, refTrailingCommaPos) {
  7656. const result = [];
  7657. let trailingCommaPos = -1;
  7658. for (;;) {
  7659. if (this.tsIsListTerminator(kind)) {
  7660. break;
  7661. }
  7662. trailingCommaPos = -1;
  7663. const element = parseElement();
  7664. if (element == null) {
  7665. return undefined;
  7666. }
  7667. result.push(element);
  7668. if (this.eat(12)) {
  7669. trailingCommaPos = this.state.lastTokStartLoc.index;
  7670. continue;
  7671. }
  7672. if (this.tsIsListTerminator(kind)) {
  7673. break;
  7674. }
  7675. if (expectSuccess) {
  7676. this.expect(12);
  7677. }
  7678. return undefined;
  7679. }
  7680. if (refTrailingCommaPos) {
  7681. refTrailingCommaPos.value = trailingCommaPos;
  7682. }
  7683. return result;
  7684. }
  7685. tsParseBracketedList(kind, parseElement, bracket, skipFirstToken, refTrailingCommaPos) {
  7686. if (!skipFirstToken) {
  7687. if (bracket) {
  7688. this.expect(0);
  7689. } else {
  7690. this.expect(47);
  7691. }
  7692. }
  7693. const result = this.tsParseDelimitedList(kind, parseElement, refTrailingCommaPos);
  7694. if (bracket) {
  7695. this.expect(3);
  7696. } else {
  7697. this.expect(48);
  7698. }
  7699. return result;
  7700. }
  7701. tsParseImportType() {
  7702. const node = this.startNode();
  7703. this.expect(83);
  7704. this.expect(10);
  7705. if (!this.match(133)) {
  7706. this.raise(TSErrors.UnsupportedImportTypeArgument, this.state.startLoc);
  7707. }
  7708. node.argument = super.parseExprAtom();
  7709. if (this.hasPlugin("importAttributes") || this.hasPlugin("importAssertions")) {
  7710. node.options = null;
  7711. }
  7712. if (this.eat(12)) {
  7713. this.expectImportAttributesPlugin();
  7714. if (!this.match(11)) {
  7715. node.options = super.parseMaybeAssignAllowIn();
  7716. this.eat(12);
  7717. }
  7718. }
  7719. this.expect(11);
  7720. if (this.eat(16)) {
  7721. node.qualifier = this.tsParseEntityName();
  7722. }
  7723. if (this.match(47)) {
  7724. node.typeParameters = this.tsParseTypeArguments();
  7725. }
  7726. return this.finishNode(node, "TSImportType");
  7727. }
  7728. tsParseEntityName(allowReservedWords = true) {
  7729. let entity = this.parseIdentifier(allowReservedWords);
  7730. while (this.eat(16)) {
  7731. const node = this.startNodeAtNode(entity);
  7732. node.left = entity;
  7733. node.right = this.parseIdentifier(allowReservedWords);
  7734. entity = this.finishNode(node, "TSQualifiedName");
  7735. }
  7736. return entity;
  7737. }
  7738. tsParseTypeReference() {
  7739. const node = this.startNode();
  7740. node.typeName = this.tsParseEntityName();
  7741. if (!this.hasPrecedingLineBreak() && this.match(47)) {
  7742. node.typeParameters = this.tsParseTypeArguments();
  7743. }
  7744. return this.finishNode(node, "TSTypeReference");
  7745. }
  7746. tsParseThisTypePredicate(lhs) {
  7747. this.next();
  7748. const node = this.startNodeAtNode(lhs);
  7749. node.parameterName = lhs;
  7750. node.typeAnnotation = this.tsParseTypeAnnotation(false);
  7751. node.asserts = false;
  7752. return this.finishNode(node, "TSTypePredicate");
  7753. }
  7754. tsParseThisTypeNode() {
  7755. const node = this.startNode();
  7756. this.next();
  7757. return this.finishNode(node, "TSThisType");
  7758. }
  7759. tsParseTypeQuery() {
  7760. const node = this.startNode();
  7761. this.expect(87);
  7762. if (this.match(83)) {
  7763. node.exprName = this.tsParseImportType();
  7764. } else {
  7765. node.exprName = this.tsParseEntityName();
  7766. }
  7767. if (!this.hasPrecedingLineBreak() && this.match(47)) {
  7768. node.typeParameters = this.tsParseTypeArguments();
  7769. }
  7770. return this.finishNode(node, "TSTypeQuery");
  7771. }
  7772. tsParseTypeParameter(parseModifiers) {
  7773. const node = this.startNode();
  7774. parseModifiers(node);
  7775. node.name = this.tsParseTypeParameterName();
  7776. node.constraint = this.tsEatThenParseType(81);
  7777. node.default = this.tsEatThenParseType(29);
  7778. return this.finishNode(node, "TSTypeParameter");
  7779. }
  7780. tsTryParseTypeParameters(parseModifiers) {
  7781. if (this.match(47)) {
  7782. return this.tsParseTypeParameters(parseModifiers);
  7783. }
  7784. }
  7785. tsParseTypeParameters(parseModifiers) {
  7786. const node = this.startNode();
  7787. if (this.match(47) || this.match(142)) {
  7788. this.next();
  7789. } else {
  7790. this.unexpected();
  7791. }
  7792. const refTrailingCommaPos = {
  7793. value: -1
  7794. };
  7795. node.params = this.tsParseBracketedList("TypeParametersOrArguments", this.tsParseTypeParameter.bind(this, parseModifiers), false, true, refTrailingCommaPos);
  7796. if (node.params.length === 0) {
  7797. this.raise(TSErrors.EmptyTypeParameters, node);
  7798. }
  7799. if (refTrailingCommaPos.value !== -1) {
  7800. this.addExtra(node, "trailingComma", refTrailingCommaPos.value);
  7801. }
  7802. return this.finishNode(node, "TSTypeParameterDeclaration");
  7803. }
  7804. tsFillSignature(returnToken, signature) {
  7805. const returnTokenRequired = returnToken === 19;
  7806. const paramsKey = "parameters";
  7807. const returnTypeKey = "typeAnnotation";
  7808. signature.typeParameters = this.tsTryParseTypeParameters(this.tsParseConstModifier);
  7809. this.expect(10);
  7810. signature[paramsKey] = this.tsParseBindingListForSignature();
  7811. if (returnTokenRequired) {
  7812. signature[returnTypeKey] = this.tsParseTypeOrTypePredicateAnnotation(returnToken);
  7813. } else if (this.match(returnToken)) {
  7814. signature[returnTypeKey] = this.tsParseTypeOrTypePredicateAnnotation(returnToken);
  7815. }
  7816. }
  7817. tsParseBindingListForSignature() {
  7818. const list = super.parseBindingList(11, 41, 2);
  7819. for (const pattern of list) {
  7820. const {
  7821. type
  7822. } = pattern;
  7823. if (type === "AssignmentPattern" || type === "TSParameterProperty") {
  7824. this.raise(TSErrors.UnsupportedSignatureParameterKind, pattern, {
  7825. type
  7826. });
  7827. }
  7828. }
  7829. return list;
  7830. }
  7831. tsParseTypeMemberSemicolon() {
  7832. if (!this.eat(12) && !this.isLineTerminator()) {
  7833. this.expect(13);
  7834. }
  7835. }
  7836. tsParseSignatureMember(kind, node) {
  7837. this.tsFillSignature(14, node);
  7838. this.tsParseTypeMemberSemicolon();
  7839. return this.finishNode(node, kind);
  7840. }
  7841. tsIsUnambiguouslyIndexSignature() {
  7842. this.next();
  7843. if (tokenIsIdentifier(this.state.type)) {
  7844. this.next();
  7845. return this.match(14);
  7846. }
  7847. return false;
  7848. }
  7849. tsTryParseIndexSignature(node) {
  7850. if (!(this.match(0) && this.tsLookAhead(this.tsIsUnambiguouslyIndexSignature.bind(this)))) {
  7851. return;
  7852. }
  7853. this.expect(0);
  7854. const id = this.parseIdentifier();
  7855. id.typeAnnotation = this.tsParseTypeAnnotation();
  7856. this.resetEndLocation(id);
  7857. this.expect(3);
  7858. node.parameters = [id];
  7859. const type = this.tsTryParseTypeAnnotation();
  7860. if (type) node.typeAnnotation = type;
  7861. this.tsParseTypeMemberSemicolon();
  7862. return this.finishNode(node, "TSIndexSignature");
  7863. }
  7864. tsParsePropertyOrMethodSignature(node, readonly) {
  7865. if (this.eat(17)) node.optional = true;
  7866. const nodeAny = node;
  7867. if (this.match(10) || this.match(47)) {
  7868. if (readonly) {
  7869. this.raise(TSErrors.ReadonlyForMethodSignature, node);
  7870. }
  7871. const method = nodeAny;
  7872. if (method.kind && this.match(47)) {
  7873. this.raise(TSErrors.AccessorCannotHaveTypeParameters, this.state.curPosition());
  7874. }
  7875. this.tsFillSignature(14, method);
  7876. this.tsParseTypeMemberSemicolon();
  7877. const paramsKey = "parameters";
  7878. const returnTypeKey = "typeAnnotation";
  7879. if (method.kind === "get") {
  7880. if (method[paramsKey].length > 0) {
  7881. this.raise(Errors.BadGetterArity, this.state.curPosition());
  7882. if (this.isThisParam(method[paramsKey][0])) {
  7883. this.raise(TSErrors.AccessorCannotDeclareThisParameter, this.state.curPosition());
  7884. }
  7885. }
  7886. } else if (method.kind === "set") {
  7887. if (method[paramsKey].length !== 1) {
  7888. this.raise(Errors.BadSetterArity, this.state.curPosition());
  7889. } else {
  7890. const firstParameter = method[paramsKey][0];
  7891. if (this.isThisParam(firstParameter)) {
  7892. this.raise(TSErrors.AccessorCannotDeclareThisParameter, this.state.curPosition());
  7893. }
  7894. if (firstParameter.type === "Identifier" && firstParameter.optional) {
  7895. this.raise(TSErrors.SetAccessorCannotHaveOptionalParameter, this.state.curPosition());
  7896. }
  7897. if (firstParameter.type === "RestElement") {
  7898. this.raise(TSErrors.SetAccessorCannotHaveRestParameter, this.state.curPosition());
  7899. }
  7900. }
  7901. if (method[returnTypeKey]) {
  7902. this.raise(TSErrors.SetAccessorCannotHaveReturnType, method[returnTypeKey]);
  7903. }
  7904. } else {
  7905. method.kind = "method";
  7906. }
  7907. return this.finishNode(method, "TSMethodSignature");
  7908. } else {
  7909. const property = nodeAny;
  7910. if (readonly) property.readonly = true;
  7911. const type = this.tsTryParseTypeAnnotation();
  7912. if (type) property.typeAnnotation = type;
  7913. this.tsParseTypeMemberSemicolon();
  7914. return this.finishNode(property, "TSPropertySignature");
  7915. }
  7916. }
  7917. tsParseTypeMember() {
  7918. const node = this.startNode();
  7919. if (this.match(10) || this.match(47)) {
  7920. return this.tsParseSignatureMember("TSCallSignatureDeclaration", node);
  7921. }
  7922. if (this.match(77)) {
  7923. const id = this.startNode();
  7924. this.next();
  7925. if (this.match(10) || this.match(47)) {
  7926. return this.tsParseSignatureMember("TSConstructSignatureDeclaration", node);
  7927. } else {
  7928. node.key = this.createIdentifier(id, "new");
  7929. return this.tsParsePropertyOrMethodSignature(node, false);
  7930. }
  7931. }
  7932. this.tsParseModifiers({
  7933. allowedModifiers: ["readonly"],
  7934. disallowedModifiers: ["declare", "abstract", "private", "protected", "public", "static", "override"]
  7935. }, node);
  7936. const idx = this.tsTryParseIndexSignature(node);
  7937. if (idx) {
  7938. return idx;
  7939. }
  7940. super.parsePropertyName(node);
  7941. if (!node.computed && node.key.type === "Identifier" && (node.key.name === "get" || node.key.name === "set") && this.tsTokenCanFollowModifier()) {
  7942. node.kind = node.key.name;
  7943. super.parsePropertyName(node);
  7944. }
  7945. return this.tsParsePropertyOrMethodSignature(node, !!node.readonly);
  7946. }
  7947. tsParseTypeLiteral() {
  7948. const node = this.startNode();
  7949. node.members = this.tsParseObjectTypeMembers();
  7950. return this.finishNode(node, "TSTypeLiteral");
  7951. }
  7952. tsParseObjectTypeMembers() {
  7953. this.expect(5);
  7954. const members = this.tsParseList("TypeMembers", this.tsParseTypeMember.bind(this));
  7955. this.expect(8);
  7956. return members;
  7957. }
  7958. tsIsStartOfMappedType() {
  7959. this.next();
  7960. if (this.eat(53)) {
  7961. return this.isContextual(122);
  7962. }
  7963. if (this.isContextual(122)) {
  7964. this.next();
  7965. }
  7966. if (!this.match(0)) {
  7967. return false;
  7968. }
  7969. this.next();
  7970. if (!this.tsIsIdentifier()) {
  7971. return false;
  7972. }
  7973. this.next();
  7974. return this.match(58);
  7975. }
  7976. tsParseMappedType() {
  7977. const node = this.startNode();
  7978. this.expect(5);
  7979. if (this.match(53)) {
  7980. node.readonly = this.state.value;
  7981. this.next();
  7982. this.expectContextual(122);
  7983. } else if (this.eatContextual(122)) {
  7984. node.readonly = true;
  7985. }
  7986. this.expect(0);
  7987. {
  7988. const typeParameter = this.startNode();
  7989. typeParameter.name = this.tsParseTypeParameterName();
  7990. typeParameter.constraint = this.tsExpectThenParseType(58);
  7991. node.typeParameter = this.finishNode(typeParameter, "TSTypeParameter");
  7992. }
  7993. node.nameType = this.eatContextual(93) ? this.tsParseType() : null;
  7994. this.expect(3);
  7995. if (this.match(53)) {
  7996. node.optional = this.state.value;
  7997. this.next();
  7998. this.expect(17);
  7999. } else if (this.eat(17)) {
  8000. node.optional = true;
  8001. }
  8002. node.typeAnnotation = this.tsTryParseType();
  8003. this.semicolon();
  8004. this.expect(8);
  8005. return this.finishNode(node, "TSMappedType");
  8006. }
  8007. tsParseTupleType() {
  8008. const node = this.startNode();
  8009. node.elementTypes = this.tsParseBracketedList("TupleElementTypes", this.tsParseTupleElementType.bind(this), true, false);
  8010. let seenOptionalElement = false;
  8011. node.elementTypes.forEach(elementNode => {
  8012. const {
  8013. type
  8014. } = elementNode;
  8015. if (seenOptionalElement && type !== "TSRestType" && type !== "TSOptionalType" && !(type === "TSNamedTupleMember" && elementNode.optional)) {
  8016. this.raise(TSErrors.OptionalTypeBeforeRequired, elementNode);
  8017. }
  8018. seenOptionalElement || (seenOptionalElement = type === "TSNamedTupleMember" && elementNode.optional || type === "TSOptionalType");
  8019. });
  8020. return this.finishNode(node, "TSTupleType");
  8021. }
  8022. tsParseTupleElementType() {
  8023. const {
  8024. startLoc
  8025. } = this.state;
  8026. const rest = this.eat(21);
  8027. let labeled;
  8028. let label;
  8029. let optional;
  8030. let type;
  8031. const isWord = tokenIsKeywordOrIdentifier(this.state.type);
  8032. const chAfterWord = isWord ? this.lookaheadCharCode() : null;
  8033. if (chAfterWord === 58) {
  8034. labeled = true;
  8035. optional = false;
  8036. label = this.parseIdentifier(true);
  8037. this.expect(14);
  8038. type = this.tsParseType();
  8039. } else if (chAfterWord === 63) {
  8040. optional = true;
  8041. const startLoc = this.state.startLoc;
  8042. const wordName = this.state.value;
  8043. const typeOrLabel = this.tsParseNonArrayType();
  8044. if (this.lookaheadCharCode() === 58) {
  8045. labeled = true;
  8046. label = this.createIdentifier(this.startNodeAt(startLoc), wordName);
  8047. this.expect(17);
  8048. this.expect(14);
  8049. type = this.tsParseType();
  8050. } else {
  8051. labeled = false;
  8052. type = typeOrLabel;
  8053. this.expect(17);
  8054. }
  8055. } else {
  8056. type = this.tsParseType();
  8057. optional = this.eat(17);
  8058. labeled = this.eat(14);
  8059. }
  8060. if (labeled) {
  8061. let labeledNode;
  8062. if (label) {
  8063. labeledNode = this.startNodeAtNode(label);
  8064. labeledNode.optional = optional;
  8065. labeledNode.label = label;
  8066. labeledNode.elementType = type;
  8067. if (this.eat(17)) {
  8068. labeledNode.optional = true;
  8069. this.raise(TSErrors.TupleOptionalAfterType, this.state.lastTokStartLoc);
  8070. }
  8071. } else {
  8072. labeledNode = this.startNodeAtNode(type);
  8073. labeledNode.optional = optional;
  8074. this.raise(TSErrors.InvalidTupleMemberLabel, type);
  8075. labeledNode.label = type;
  8076. labeledNode.elementType = this.tsParseType();
  8077. }
  8078. type = this.finishNode(labeledNode, "TSNamedTupleMember");
  8079. } else if (optional) {
  8080. const optionalTypeNode = this.startNodeAtNode(type);
  8081. optionalTypeNode.typeAnnotation = type;
  8082. type = this.finishNode(optionalTypeNode, "TSOptionalType");
  8083. }
  8084. if (rest) {
  8085. const restNode = this.startNodeAt(startLoc);
  8086. restNode.typeAnnotation = type;
  8087. type = this.finishNode(restNode, "TSRestType");
  8088. }
  8089. return type;
  8090. }
  8091. tsParseParenthesizedType() {
  8092. const node = this.startNode();
  8093. this.expect(10);
  8094. node.typeAnnotation = this.tsParseType();
  8095. this.expect(11);
  8096. return this.finishNode(node, "TSParenthesizedType");
  8097. }
  8098. tsParseFunctionOrConstructorType(type, abstract) {
  8099. const node = this.startNode();
  8100. if (type === "TSConstructorType") {
  8101. node.abstract = !!abstract;
  8102. if (abstract) this.next();
  8103. this.next();
  8104. }
  8105. this.tsInAllowConditionalTypesContext(() => this.tsFillSignature(19, node));
  8106. return this.finishNode(node, type);
  8107. }
  8108. tsParseLiteralTypeNode() {
  8109. const node = this.startNode();
  8110. switch (this.state.type) {
  8111. case 134:
  8112. case 135:
  8113. case 133:
  8114. case 85:
  8115. case 86:
  8116. node.literal = super.parseExprAtom();
  8117. break;
  8118. default:
  8119. this.unexpected();
  8120. }
  8121. return this.finishNode(node, "TSLiteralType");
  8122. }
  8123. tsParseTemplateLiteralType() {
  8124. const node = this.startNode();
  8125. node.literal = super.parseTemplate(false);
  8126. return this.finishNode(node, "TSLiteralType");
  8127. }
  8128. parseTemplateSubstitution() {
  8129. if (this.state.inType) return this.tsParseType();
  8130. return super.parseTemplateSubstitution();
  8131. }
  8132. tsParseThisTypeOrThisTypePredicate() {
  8133. const thisKeyword = this.tsParseThisTypeNode();
  8134. if (this.isContextual(116) && !this.hasPrecedingLineBreak()) {
  8135. return this.tsParseThisTypePredicate(thisKeyword);
  8136. } else {
  8137. return thisKeyword;
  8138. }
  8139. }
  8140. tsParseNonArrayType() {
  8141. switch (this.state.type) {
  8142. case 133:
  8143. case 134:
  8144. case 135:
  8145. case 85:
  8146. case 86:
  8147. return this.tsParseLiteralTypeNode();
  8148. case 53:
  8149. if (this.state.value === "-") {
  8150. const node = this.startNode();
  8151. const nextToken = this.lookahead();
  8152. if (nextToken.type !== 134 && nextToken.type !== 135) {
  8153. this.unexpected();
  8154. }
  8155. node.literal = this.parseMaybeUnary();
  8156. return this.finishNode(node, "TSLiteralType");
  8157. }
  8158. break;
  8159. case 78:
  8160. return this.tsParseThisTypeOrThisTypePredicate();
  8161. case 87:
  8162. return this.tsParseTypeQuery();
  8163. case 83:
  8164. return this.tsParseImportType();
  8165. case 5:
  8166. return this.tsLookAhead(this.tsIsStartOfMappedType.bind(this)) ? this.tsParseMappedType() : this.tsParseTypeLiteral();
  8167. case 0:
  8168. return this.tsParseTupleType();
  8169. case 10:
  8170. return this.tsParseParenthesizedType();
  8171. case 25:
  8172. case 24:
  8173. return this.tsParseTemplateLiteralType();
  8174. default:
  8175. {
  8176. const {
  8177. type
  8178. } = this.state;
  8179. if (tokenIsIdentifier(type) || type === 88 || type === 84) {
  8180. const nodeType = type === 88 ? "TSVoidKeyword" : type === 84 ? "TSNullKeyword" : keywordTypeFromName(this.state.value);
  8181. if (nodeType !== undefined && this.lookaheadCharCode() !== 46) {
  8182. const node = this.startNode();
  8183. this.next();
  8184. return this.finishNode(node, nodeType);
  8185. }
  8186. return this.tsParseTypeReference();
  8187. }
  8188. }
  8189. }
  8190. this.unexpected();
  8191. }
  8192. tsParseArrayTypeOrHigher() {
  8193. let type = this.tsParseNonArrayType();
  8194. while (!this.hasPrecedingLineBreak() && this.eat(0)) {
  8195. if (this.match(3)) {
  8196. const node = this.startNodeAtNode(type);
  8197. node.elementType = type;
  8198. this.expect(3);
  8199. type = this.finishNode(node, "TSArrayType");
  8200. } else {
  8201. const node = this.startNodeAtNode(type);
  8202. node.objectType = type;
  8203. node.indexType = this.tsParseType();
  8204. this.expect(3);
  8205. type = this.finishNode(node, "TSIndexedAccessType");
  8206. }
  8207. }
  8208. return type;
  8209. }
  8210. tsParseTypeOperator() {
  8211. const node = this.startNode();
  8212. const operator = this.state.value;
  8213. this.next();
  8214. node.operator = operator;
  8215. node.typeAnnotation = this.tsParseTypeOperatorOrHigher();
  8216. if (operator === "readonly") {
  8217. this.tsCheckTypeAnnotationForReadOnly(node);
  8218. }
  8219. return this.finishNode(node, "TSTypeOperator");
  8220. }
  8221. tsCheckTypeAnnotationForReadOnly(node) {
  8222. switch (node.typeAnnotation.type) {
  8223. case "TSTupleType":
  8224. case "TSArrayType":
  8225. return;
  8226. default:
  8227. this.raise(TSErrors.UnexpectedReadonly, node);
  8228. }
  8229. }
  8230. tsParseInferType() {
  8231. const node = this.startNode();
  8232. this.expectContextual(115);
  8233. const typeParameter = this.startNode();
  8234. typeParameter.name = this.tsParseTypeParameterName();
  8235. typeParameter.constraint = this.tsTryParse(() => this.tsParseConstraintForInferType());
  8236. node.typeParameter = this.finishNode(typeParameter, "TSTypeParameter");
  8237. return this.finishNode(node, "TSInferType");
  8238. }
  8239. tsParseConstraintForInferType() {
  8240. if (this.eat(81)) {
  8241. const constraint = this.tsInDisallowConditionalTypesContext(() => this.tsParseType());
  8242. if (this.state.inDisallowConditionalTypesContext || !this.match(17)) {
  8243. return constraint;
  8244. }
  8245. }
  8246. }
  8247. tsParseTypeOperatorOrHigher() {
  8248. const isTypeOperator = tokenIsTSTypeOperator(this.state.type) && !this.state.containsEsc;
  8249. return isTypeOperator ? this.tsParseTypeOperator() : this.isContextual(115) ? this.tsParseInferType() : this.tsInAllowConditionalTypesContext(() => this.tsParseArrayTypeOrHigher());
  8250. }
  8251. tsParseUnionOrIntersectionType(kind, parseConstituentType, operator) {
  8252. const node = this.startNode();
  8253. const hasLeadingOperator = this.eat(operator);
  8254. const types = [];
  8255. do {
  8256. types.push(parseConstituentType());
  8257. } while (this.eat(operator));
  8258. if (types.length === 1 && !hasLeadingOperator) {
  8259. return types[0];
  8260. }
  8261. node.types = types;
  8262. return this.finishNode(node, kind);
  8263. }
  8264. tsParseIntersectionTypeOrHigher() {
  8265. return this.tsParseUnionOrIntersectionType("TSIntersectionType", this.tsParseTypeOperatorOrHigher.bind(this), 45);
  8266. }
  8267. tsParseUnionTypeOrHigher() {
  8268. return this.tsParseUnionOrIntersectionType("TSUnionType", this.tsParseIntersectionTypeOrHigher.bind(this), 43);
  8269. }
  8270. tsIsStartOfFunctionType() {
  8271. if (this.match(47)) {
  8272. return true;
  8273. }
  8274. return this.match(10) && this.tsLookAhead(this.tsIsUnambiguouslyStartOfFunctionType.bind(this));
  8275. }
  8276. tsSkipParameterStart() {
  8277. if (tokenIsIdentifier(this.state.type) || this.match(78)) {
  8278. this.next();
  8279. return true;
  8280. }
  8281. if (this.match(5)) {
  8282. const {
  8283. errors
  8284. } = this.state;
  8285. const previousErrorCount = errors.length;
  8286. try {
  8287. this.parseObjectLike(8, true);
  8288. return errors.length === previousErrorCount;
  8289. } catch (_unused) {
  8290. return false;
  8291. }
  8292. }
  8293. if (this.match(0)) {
  8294. this.next();
  8295. const {
  8296. errors
  8297. } = this.state;
  8298. const previousErrorCount = errors.length;
  8299. try {
  8300. super.parseBindingList(3, 93, 1);
  8301. return errors.length === previousErrorCount;
  8302. } catch (_unused2) {
  8303. return false;
  8304. }
  8305. }
  8306. return false;
  8307. }
  8308. tsIsUnambiguouslyStartOfFunctionType() {
  8309. this.next();
  8310. if (this.match(11) || this.match(21)) {
  8311. return true;
  8312. }
  8313. if (this.tsSkipParameterStart()) {
  8314. if (this.match(14) || this.match(12) || this.match(17) || this.match(29)) {
  8315. return true;
  8316. }
  8317. if (this.match(11)) {
  8318. this.next();
  8319. if (this.match(19)) {
  8320. return true;
  8321. }
  8322. }
  8323. }
  8324. return false;
  8325. }
  8326. tsParseTypeOrTypePredicateAnnotation(returnToken) {
  8327. return this.tsInType(() => {
  8328. const t = this.startNode();
  8329. this.expect(returnToken);
  8330. const node = this.startNode();
  8331. const asserts = !!this.tsTryParse(this.tsParseTypePredicateAsserts.bind(this));
  8332. if (asserts && this.match(78)) {
  8333. let thisTypePredicate = this.tsParseThisTypeOrThisTypePredicate();
  8334. if (thisTypePredicate.type === "TSThisType") {
  8335. node.parameterName = thisTypePredicate;
  8336. node.asserts = true;
  8337. node.typeAnnotation = null;
  8338. thisTypePredicate = this.finishNode(node, "TSTypePredicate");
  8339. } else {
  8340. this.resetStartLocationFromNode(thisTypePredicate, node);
  8341. thisTypePredicate.asserts = true;
  8342. }
  8343. t.typeAnnotation = thisTypePredicate;
  8344. return this.finishNode(t, "TSTypeAnnotation");
  8345. }
  8346. const typePredicateVariable = this.tsIsIdentifier() && this.tsTryParse(this.tsParseTypePredicatePrefix.bind(this));
  8347. if (!typePredicateVariable) {
  8348. if (!asserts) {
  8349. return this.tsParseTypeAnnotation(false, t);
  8350. }
  8351. node.parameterName = this.parseIdentifier();
  8352. node.asserts = asserts;
  8353. node.typeAnnotation = null;
  8354. t.typeAnnotation = this.finishNode(node, "TSTypePredicate");
  8355. return this.finishNode(t, "TSTypeAnnotation");
  8356. }
  8357. const type = this.tsParseTypeAnnotation(false);
  8358. node.parameterName = typePredicateVariable;
  8359. node.typeAnnotation = type;
  8360. node.asserts = asserts;
  8361. t.typeAnnotation = this.finishNode(node, "TSTypePredicate");
  8362. return this.finishNode(t, "TSTypeAnnotation");
  8363. });
  8364. }
  8365. tsTryParseTypeOrTypePredicateAnnotation() {
  8366. if (this.match(14)) {
  8367. return this.tsParseTypeOrTypePredicateAnnotation(14);
  8368. }
  8369. }
  8370. tsTryParseTypeAnnotation() {
  8371. if (this.match(14)) {
  8372. return this.tsParseTypeAnnotation();
  8373. }
  8374. }
  8375. tsTryParseType() {
  8376. return this.tsEatThenParseType(14);
  8377. }
  8378. tsParseTypePredicatePrefix() {
  8379. const id = this.parseIdentifier();
  8380. if (this.isContextual(116) && !this.hasPrecedingLineBreak()) {
  8381. this.next();
  8382. return id;
  8383. }
  8384. }
  8385. tsParseTypePredicateAsserts() {
  8386. if (this.state.type !== 109) {
  8387. return false;
  8388. }
  8389. const containsEsc = this.state.containsEsc;
  8390. this.next();
  8391. if (!tokenIsIdentifier(this.state.type) && !this.match(78)) {
  8392. return false;
  8393. }
  8394. if (containsEsc) {
  8395. this.raise(Errors.InvalidEscapedReservedWord, this.state.lastTokStartLoc, {
  8396. reservedWord: "asserts"
  8397. });
  8398. }
  8399. return true;
  8400. }
  8401. tsParseTypeAnnotation(eatColon = true, t = this.startNode()) {
  8402. this.tsInType(() => {
  8403. if (eatColon) this.expect(14);
  8404. t.typeAnnotation = this.tsParseType();
  8405. });
  8406. return this.finishNode(t, "TSTypeAnnotation");
  8407. }
  8408. tsParseType() {
  8409. assert(this.state.inType);
  8410. const type = this.tsParseNonConditionalType();
  8411. if (this.state.inDisallowConditionalTypesContext || this.hasPrecedingLineBreak() || !this.eat(81)) {
  8412. return type;
  8413. }
  8414. const node = this.startNodeAtNode(type);
  8415. node.checkType = type;
  8416. node.extendsType = this.tsInDisallowConditionalTypesContext(() => this.tsParseNonConditionalType());
  8417. this.expect(17);
  8418. node.trueType = this.tsInAllowConditionalTypesContext(() => this.tsParseType());
  8419. this.expect(14);
  8420. node.falseType = this.tsInAllowConditionalTypesContext(() => this.tsParseType());
  8421. return this.finishNode(node, "TSConditionalType");
  8422. }
  8423. isAbstractConstructorSignature() {
  8424. return this.isContextual(124) && this.lookahead().type === 77;
  8425. }
  8426. tsParseNonConditionalType() {
  8427. if (this.tsIsStartOfFunctionType()) {
  8428. return this.tsParseFunctionOrConstructorType("TSFunctionType");
  8429. }
  8430. if (this.match(77)) {
  8431. return this.tsParseFunctionOrConstructorType("TSConstructorType");
  8432. } else if (this.isAbstractConstructorSignature()) {
  8433. return this.tsParseFunctionOrConstructorType("TSConstructorType", true);
  8434. }
  8435. return this.tsParseUnionTypeOrHigher();
  8436. }
  8437. tsParseTypeAssertion() {
  8438. if (this.getPluginOption("typescript", "disallowAmbiguousJSXLike")) {
  8439. this.raise(TSErrors.ReservedTypeAssertion, this.state.startLoc);
  8440. }
  8441. const node = this.startNode();
  8442. node.typeAnnotation = this.tsInType(() => {
  8443. this.next();
  8444. return this.match(75) ? this.tsParseTypeReference() : this.tsParseType();
  8445. });
  8446. this.expect(48);
  8447. node.expression = this.parseMaybeUnary();
  8448. return this.finishNode(node, "TSTypeAssertion");
  8449. }
  8450. tsParseHeritageClause(token) {
  8451. const originalStartLoc = this.state.startLoc;
  8452. const delimitedList = this.tsParseDelimitedList("HeritageClauseElement", () => {
  8453. const node = this.startNode();
  8454. node.expression = this.tsParseEntityName();
  8455. if (this.match(47)) {
  8456. node.typeParameters = this.tsParseTypeArguments();
  8457. }
  8458. return this.finishNode(node, "TSExpressionWithTypeArguments");
  8459. });
  8460. if (!delimitedList.length) {
  8461. this.raise(TSErrors.EmptyHeritageClauseType, originalStartLoc, {
  8462. token
  8463. });
  8464. }
  8465. return delimitedList;
  8466. }
  8467. tsParseInterfaceDeclaration(node, properties = {}) {
  8468. if (this.hasFollowingLineBreak()) return null;
  8469. this.expectContextual(129);
  8470. if (properties.declare) node.declare = true;
  8471. if (tokenIsIdentifier(this.state.type)) {
  8472. node.id = this.parseIdentifier();
  8473. this.checkIdentifier(node.id, 130);
  8474. } else {
  8475. node.id = null;
  8476. this.raise(TSErrors.MissingInterfaceName, this.state.startLoc);
  8477. }
  8478. node.typeParameters = this.tsTryParseTypeParameters(this.tsParseInOutConstModifiers);
  8479. if (this.eat(81)) {
  8480. node.extends = this.tsParseHeritageClause("extends");
  8481. }
  8482. const body = this.startNode();
  8483. body.body = this.tsInType(this.tsParseObjectTypeMembers.bind(this));
  8484. node.body = this.finishNode(body, "TSInterfaceBody");
  8485. return this.finishNode(node, "TSInterfaceDeclaration");
  8486. }
  8487. tsParseTypeAliasDeclaration(node) {
  8488. node.id = this.parseIdentifier();
  8489. this.checkIdentifier(node.id, 2);
  8490. node.typeAnnotation = this.tsInType(() => {
  8491. node.typeParameters = this.tsTryParseTypeParameters(this.tsParseInOutModifiers);
  8492. this.expect(29);
  8493. if (this.isContextual(114) && this.lookahead().type !== 16) {
  8494. const node = this.startNode();
  8495. this.next();
  8496. return this.finishNode(node, "TSIntrinsicKeyword");
  8497. }
  8498. return this.tsParseType();
  8499. });
  8500. this.semicolon();
  8501. return this.finishNode(node, "TSTypeAliasDeclaration");
  8502. }
  8503. tsInNoContext(cb) {
  8504. const oldContext = this.state.context;
  8505. this.state.context = [oldContext[0]];
  8506. try {
  8507. return cb();
  8508. } finally {
  8509. this.state.context = oldContext;
  8510. }
  8511. }
  8512. tsInType(cb) {
  8513. const oldInType = this.state.inType;
  8514. this.state.inType = true;
  8515. try {
  8516. return cb();
  8517. } finally {
  8518. this.state.inType = oldInType;
  8519. }
  8520. }
  8521. tsInDisallowConditionalTypesContext(cb) {
  8522. const oldInDisallowConditionalTypesContext = this.state.inDisallowConditionalTypesContext;
  8523. this.state.inDisallowConditionalTypesContext = true;
  8524. try {
  8525. return cb();
  8526. } finally {
  8527. this.state.inDisallowConditionalTypesContext = oldInDisallowConditionalTypesContext;
  8528. }
  8529. }
  8530. tsInAllowConditionalTypesContext(cb) {
  8531. const oldInDisallowConditionalTypesContext = this.state.inDisallowConditionalTypesContext;
  8532. this.state.inDisallowConditionalTypesContext = false;
  8533. try {
  8534. return cb();
  8535. } finally {
  8536. this.state.inDisallowConditionalTypesContext = oldInDisallowConditionalTypesContext;
  8537. }
  8538. }
  8539. tsEatThenParseType(token) {
  8540. if (this.match(token)) {
  8541. return this.tsNextThenParseType();
  8542. }
  8543. }
  8544. tsExpectThenParseType(token) {
  8545. return this.tsInType(() => {
  8546. this.expect(token);
  8547. return this.tsParseType();
  8548. });
  8549. }
  8550. tsNextThenParseType() {
  8551. return this.tsInType(() => {
  8552. this.next();
  8553. return this.tsParseType();
  8554. });
  8555. }
  8556. tsParseEnumMember() {
  8557. const node = this.startNode();
  8558. node.id = this.match(133) ? super.parseStringLiteral(this.state.value) : this.parseIdentifier(true);
  8559. if (this.eat(29)) {
  8560. node.initializer = super.parseMaybeAssignAllowIn();
  8561. }
  8562. return this.finishNode(node, "TSEnumMember");
  8563. }
  8564. tsParseEnumDeclaration(node, properties = {}) {
  8565. if (properties.const) node.const = true;
  8566. if (properties.declare) node.declare = true;
  8567. this.expectContextual(126);
  8568. node.id = this.parseIdentifier();
  8569. this.checkIdentifier(node.id, node.const ? 8971 : 8459);
  8570. this.expect(5);
  8571. node.members = this.tsParseDelimitedList("EnumMembers", this.tsParseEnumMember.bind(this));
  8572. this.expect(8);
  8573. return this.finishNode(node, "TSEnumDeclaration");
  8574. }
  8575. tsParseModuleBlock() {
  8576. const node = this.startNode();
  8577. this.scope.enter(0);
  8578. this.expect(5);
  8579. super.parseBlockOrModuleBlockBody(node.body = [], undefined, true, 8);
  8580. this.scope.exit();
  8581. return this.finishNode(node, "TSModuleBlock");
  8582. }
  8583. tsParseModuleOrNamespaceDeclaration(node, nested = false) {
  8584. node.id = this.parseIdentifier();
  8585. if (!nested) {
  8586. this.checkIdentifier(node.id, 1024);
  8587. }
  8588. if (this.eat(16)) {
  8589. const inner = this.startNode();
  8590. this.tsParseModuleOrNamespaceDeclaration(inner, true);
  8591. node.body = inner;
  8592. } else {
  8593. this.scope.enter(256);
  8594. this.prodParam.enter(0);
  8595. node.body = this.tsParseModuleBlock();
  8596. this.prodParam.exit();
  8597. this.scope.exit();
  8598. }
  8599. return this.finishNode(node, "TSModuleDeclaration");
  8600. }
  8601. tsParseAmbientExternalModuleDeclaration(node) {
  8602. if (this.isContextual(112)) {
  8603. node.global = true;
  8604. node.id = this.parseIdentifier();
  8605. } else if (this.match(133)) {
  8606. node.id = super.parseStringLiteral(this.state.value);
  8607. } else {
  8608. this.unexpected();
  8609. }
  8610. if (this.match(5)) {
  8611. this.scope.enter(256);
  8612. this.prodParam.enter(0);
  8613. node.body = this.tsParseModuleBlock();
  8614. this.prodParam.exit();
  8615. this.scope.exit();
  8616. } else {
  8617. this.semicolon();
  8618. }
  8619. return this.finishNode(node, "TSModuleDeclaration");
  8620. }
  8621. tsParseImportEqualsDeclaration(node, maybeDefaultIdentifier, isExport) {
  8622. node.isExport = isExport || false;
  8623. node.id = maybeDefaultIdentifier || this.parseIdentifier();
  8624. this.checkIdentifier(node.id, 4096);
  8625. this.expect(29);
  8626. const moduleReference = this.tsParseModuleReference();
  8627. if (node.importKind === "type" && moduleReference.type !== "TSExternalModuleReference") {
  8628. this.raise(TSErrors.ImportAliasHasImportType, moduleReference);
  8629. }
  8630. node.moduleReference = moduleReference;
  8631. this.semicolon();
  8632. return this.finishNode(node, "TSImportEqualsDeclaration");
  8633. }
  8634. tsIsExternalModuleReference() {
  8635. return this.isContextual(119) && this.lookaheadCharCode() === 40;
  8636. }
  8637. tsParseModuleReference() {
  8638. return this.tsIsExternalModuleReference() ? this.tsParseExternalModuleReference() : this.tsParseEntityName(false);
  8639. }
  8640. tsParseExternalModuleReference() {
  8641. const node = this.startNode();
  8642. this.expectContextual(119);
  8643. this.expect(10);
  8644. if (!this.match(133)) {
  8645. this.unexpected();
  8646. }
  8647. node.expression = super.parseExprAtom();
  8648. this.expect(11);
  8649. this.sawUnambiguousESM = true;
  8650. return this.finishNode(node, "TSExternalModuleReference");
  8651. }
  8652. tsLookAhead(f) {
  8653. const state = this.state.clone();
  8654. const res = f();
  8655. this.state = state;
  8656. return res;
  8657. }
  8658. tsTryParseAndCatch(f) {
  8659. const result = this.tryParse(abort => f() || abort());
  8660. if (result.aborted || !result.node) return;
  8661. if (result.error) this.state = result.failState;
  8662. return result.node;
  8663. }
  8664. tsTryParse(f) {
  8665. const state = this.state.clone();
  8666. const result = f();
  8667. if (result !== undefined && result !== false) {
  8668. return result;
  8669. }
  8670. this.state = state;
  8671. }
  8672. tsTryParseDeclare(nany) {
  8673. if (this.isLineTerminator()) {
  8674. return;
  8675. }
  8676. let startType = this.state.type;
  8677. let kind;
  8678. if (this.isContextual(100)) {
  8679. startType = 74;
  8680. kind = "let";
  8681. }
  8682. return this.tsInAmbientContext(() => {
  8683. switch (startType) {
  8684. case 68:
  8685. nany.declare = true;
  8686. return super.parseFunctionStatement(nany, false, false);
  8687. case 80:
  8688. nany.declare = true;
  8689. return this.parseClass(nany, true, false);
  8690. case 126:
  8691. return this.tsParseEnumDeclaration(nany, {
  8692. declare: true
  8693. });
  8694. case 112:
  8695. return this.tsParseAmbientExternalModuleDeclaration(nany);
  8696. case 75:
  8697. case 74:
  8698. if (!this.match(75) || !this.isLookaheadContextual("enum")) {
  8699. nany.declare = true;
  8700. return this.parseVarStatement(nany, kind || this.state.value, true);
  8701. }
  8702. this.expect(75);
  8703. return this.tsParseEnumDeclaration(nany, {
  8704. const: true,
  8705. declare: true
  8706. });
  8707. case 129:
  8708. {
  8709. const result = this.tsParseInterfaceDeclaration(nany, {
  8710. declare: true
  8711. });
  8712. if (result) return result;
  8713. }
  8714. default:
  8715. if (tokenIsIdentifier(startType)) {
  8716. return this.tsParseDeclaration(nany, this.state.value, true, null);
  8717. }
  8718. }
  8719. });
  8720. }
  8721. tsTryParseExportDeclaration() {
  8722. return this.tsParseDeclaration(this.startNode(), this.state.value, true, null);
  8723. }
  8724. tsParseExpressionStatement(node, expr, decorators) {
  8725. switch (expr.name) {
  8726. case "declare":
  8727. {
  8728. const declaration = this.tsTryParseDeclare(node);
  8729. if (declaration) {
  8730. declaration.declare = true;
  8731. }
  8732. return declaration;
  8733. }
  8734. case "global":
  8735. if (this.match(5)) {
  8736. this.scope.enter(256);
  8737. this.prodParam.enter(0);
  8738. const mod = node;
  8739. mod.global = true;
  8740. mod.id = expr;
  8741. mod.body = this.tsParseModuleBlock();
  8742. this.scope.exit();
  8743. this.prodParam.exit();
  8744. return this.finishNode(mod, "TSModuleDeclaration");
  8745. }
  8746. break;
  8747. default:
  8748. return this.tsParseDeclaration(node, expr.name, false, decorators);
  8749. }
  8750. }
  8751. tsParseDeclaration(node, value, next, decorators) {
  8752. switch (value) {
  8753. case "abstract":
  8754. if (this.tsCheckLineTerminator(next) && (this.match(80) || tokenIsIdentifier(this.state.type))) {
  8755. return this.tsParseAbstractDeclaration(node, decorators);
  8756. }
  8757. break;
  8758. case "module":
  8759. if (this.tsCheckLineTerminator(next)) {
  8760. if (this.match(133)) {
  8761. return this.tsParseAmbientExternalModuleDeclaration(node);
  8762. } else if (tokenIsIdentifier(this.state.type)) {
  8763. return this.tsParseModuleOrNamespaceDeclaration(node);
  8764. }
  8765. }
  8766. break;
  8767. case "namespace":
  8768. if (this.tsCheckLineTerminator(next) && tokenIsIdentifier(this.state.type)) {
  8769. return this.tsParseModuleOrNamespaceDeclaration(node);
  8770. }
  8771. break;
  8772. case "type":
  8773. if (this.tsCheckLineTerminator(next) && tokenIsIdentifier(this.state.type)) {
  8774. return this.tsParseTypeAliasDeclaration(node);
  8775. }
  8776. break;
  8777. }
  8778. }
  8779. tsCheckLineTerminator(next) {
  8780. if (next) {
  8781. if (this.hasFollowingLineBreak()) return false;
  8782. this.next();
  8783. return true;
  8784. }
  8785. return !this.isLineTerminator();
  8786. }
  8787. tsTryParseGenericAsyncArrowFunction(startLoc) {
  8788. if (!this.match(47)) return;
  8789. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  8790. this.state.maybeInArrowParameters = true;
  8791. const res = this.tsTryParseAndCatch(() => {
  8792. const node = this.startNodeAt(startLoc);
  8793. node.typeParameters = this.tsParseTypeParameters(this.tsParseConstModifier);
  8794. super.parseFunctionParams(node);
  8795. node.returnType = this.tsTryParseTypeOrTypePredicateAnnotation();
  8796. this.expect(19);
  8797. return node;
  8798. });
  8799. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  8800. if (!res) return;
  8801. return super.parseArrowExpression(res, null, true);
  8802. }
  8803. tsParseTypeArgumentsInExpression() {
  8804. if (this.reScan_lt() !== 47) return;
  8805. return this.tsParseTypeArguments();
  8806. }
  8807. tsParseTypeArguments() {
  8808. const node = this.startNode();
  8809. node.params = this.tsInType(() => this.tsInNoContext(() => {
  8810. this.expect(47);
  8811. return this.tsParseDelimitedList("TypeParametersOrArguments", this.tsParseType.bind(this));
  8812. }));
  8813. if (node.params.length === 0) {
  8814. this.raise(TSErrors.EmptyTypeArguments, node);
  8815. } else if (!this.state.inType && this.curContext() === types.brace) {
  8816. this.reScan_lt_gt();
  8817. }
  8818. this.expect(48);
  8819. return this.finishNode(node, "TSTypeParameterInstantiation");
  8820. }
  8821. tsIsDeclarationStart() {
  8822. return tokenIsTSDeclarationStart(this.state.type);
  8823. }
  8824. isExportDefaultSpecifier() {
  8825. if (this.tsIsDeclarationStart()) return false;
  8826. return super.isExportDefaultSpecifier();
  8827. }
  8828. parseAssignableListItem(flags, decorators) {
  8829. const startLoc = this.state.startLoc;
  8830. const modified = {};
  8831. this.tsParseModifiers({
  8832. allowedModifiers: ["public", "private", "protected", "override", "readonly"]
  8833. }, modified);
  8834. const accessibility = modified.accessibility;
  8835. const override = modified.override;
  8836. const readonly = modified.readonly;
  8837. if (!(flags & 4) && (accessibility || readonly || override)) {
  8838. this.raise(TSErrors.UnexpectedParameterModifier, startLoc);
  8839. }
  8840. const left = this.parseMaybeDefault();
  8841. this.parseAssignableListItemTypes(left, flags);
  8842. const elt = this.parseMaybeDefault(left.loc.start, left);
  8843. if (accessibility || readonly || override) {
  8844. const pp = this.startNodeAt(startLoc);
  8845. if (decorators.length) {
  8846. pp.decorators = decorators;
  8847. }
  8848. if (accessibility) pp.accessibility = accessibility;
  8849. if (readonly) pp.readonly = readonly;
  8850. if (override) pp.override = override;
  8851. if (elt.type !== "Identifier" && elt.type !== "AssignmentPattern") {
  8852. this.raise(TSErrors.UnsupportedParameterPropertyKind, pp);
  8853. }
  8854. pp.parameter = elt;
  8855. return this.finishNode(pp, "TSParameterProperty");
  8856. }
  8857. if (decorators.length) {
  8858. left.decorators = decorators;
  8859. }
  8860. return elt;
  8861. }
  8862. isSimpleParameter(node) {
  8863. return node.type === "TSParameterProperty" && super.isSimpleParameter(node.parameter) || super.isSimpleParameter(node);
  8864. }
  8865. tsDisallowOptionalPattern(node) {
  8866. for (const param of node.params) {
  8867. if (param.type !== "Identifier" && param.optional && !this.state.isAmbientContext) {
  8868. this.raise(TSErrors.PatternIsOptional, param);
  8869. }
  8870. }
  8871. }
  8872. setArrowFunctionParameters(node, params, trailingCommaLoc) {
  8873. super.setArrowFunctionParameters(node, params, trailingCommaLoc);
  8874. this.tsDisallowOptionalPattern(node);
  8875. }
  8876. parseFunctionBodyAndFinish(node, type, isMethod = false) {
  8877. if (this.match(14)) {
  8878. node.returnType = this.tsParseTypeOrTypePredicateAnnotation(14);
  8879. }
  8880. const bodilessType = type === "FunctionDeclaration" ? "TSDeclareFunction" : type === "ClassMethod" || type === "ClassPrivateMethod" ? "TSDeclareMethod" : undefined;
  8881. if (bodilessType && !this.match(5) && this.isLineTerminator()) {
  8882. return this.finishNode(node, bodilessType);
  8883. }
  8884. if (bodilessType === "TSDeclareFunction" && this.state.isAmbientContext) {
  8885. this.raise(TSErrors.DeclareFunctionHasImplementation, node);
  8886. if (node.declare) {
  8887. return super.parseFunctionBodyAndFinish(node, bodilessType, isMethod);
  8888. }
  8889. }
  8890. this.tsDisallowOptionalPattern(node);
  8891. return super.parseFunctionBodyAndFinish(node, type, isMethod);
  8892. }
  8893. registerFunctionStatementId(node) {
  8894. if (!node.body && node.id) {
  8895. this.checkIdentifier(node.id, 1024);
  8896. } else {
  8897. super.registerFunctionStatementId(node);
  8898. }
  8899. }
  8900. tsCheckForInvalidTypeCasts(items) {
  8901. items.forEach(node => {
  8902. if ((node == null ? void 0 : node.type) === "TSTypeCastExpression") {
  8903. this.raise(TSErrors.UnexpectedTypeAnnotation, node.typeAnnotation);
  8904. }
  8905. });
  8906. }
  8907. toReferencedList(exprList, isInParens) {
  8908. this.tsCheckForInvalidTypeCasts(exprList);
  8909. return exprList;
  8910. }
  8911. parseArrayLike(close, canBePattern, isTuple, refExpressionErrors) {
  8912. const node = super.parseArrayLike(close, canBePattern, isTuple, refExpressionErrors);
  8913. if (node.type === "ArrayExpression") {
  8914. this.tsCheckForInvalidTypeCasts(node.elements);
  8915. }
  8916. return node;
  8917. }
  8918. parseSubscript(base, startLoc, noCalls, state) {
  8919. if (!this.hasPrecedingLineBreak() && this.match(35)) {
  8920. this.state.canStartJSXElement = false;
  8921. this.next();
  8922. const nonNullExpression = this.startNodeAt(startLoc);
  8923. nonNullExpression.expression = base;
  8924. return this.finishNode(nonNullExpression, "TSNonNullExpression");
  8925. }
  8926. let isOptionalCall = false;
  8927. if (this.match(18) && this.lookaheadCharCode() === 60) {
  8928. if (noCalls) {
  8929. state.stop = true;
  8930. return base;
  8931. }
  8932. state.optionalChainMember = isOptionalCall = true;
  8933. this.next();
  8934. }
  8935. if (this.match(47) || this.match(51)) {
  8936. let missingParenErrorLoc;
  8937. const result = this.tsTryParseAndCatch(() => {
  8938. if (!noCalls && this.atPossibleAsyncArrow(base)) {
  8939. const asyncArrowFn = this.tsTryParseGenericAsyncArrowFunction(startLoc);
  8940. if (asyncArrowFn) {
  8941. return asyncArrowFn;
  8942. }
  8943. }
  8944. const typeArguments = this.tsParseTypeArgumentsInExpression();
  8945. if (!typeArguments) return;
  8946. if (isOptionalCall && !this.match(10)) {
  8947. missingParenErrorLoc = this.state.curPosition();
  8948. return;
  8949. }
  8950. if (tokenIsTemplate(this.state.type)) {
  8951. const result = super.parseTaggedTemplateExpression(base, startLoc, state);
  8952. result.typeParameters = typeArguments;
  8953. return result;
  8954. }
  8955. if (!noCalls && this.eat(10)) {
  8956. const node = this.startNodeAt(startLoc);
  8957. node.callee = base;
  8958. node.arguments = this.parseCallExpressionArguments(11, false);
  8959. this.tsCheckForInvalidTypeCasts(node.arguments);
  8960. node.typeParameters = typeArguments;
  8961. if (state.optionalChainMember) {
  8962. node.optional = isOptionalCall;
  8963. }
  8964. return this.finishCallExpression(node, state.optionalChainMember);
  8965. }
  8966. const tokenType = this.state.type;
  8967. if (tokenType === 48 || tokenType === 52 || tokenType !== 10 && tokenCanStartExpression(tokenType) && !this.hasPrecedingLineBreak()) {
  8968. return;
  8969. }
  8970. const node = this.startNodeAt(startLoc);
  8971. node.expression = base;
  8972. node.typeParameters = typeArguments;
  8973. return this.finishNode(node, "TSInstantiationExpression");
  8974. });
  8975. if (missingParenErrorLoc) {
  8976. this.unexpected(missingParenErrorLoc, 10);
  8977. }
  8978. if (result) {
  8979. if (result.type === "TSInstantiationExpression" && (this.match(16) || this.match(18) && this.lookaheadCharCode() !== 40)) {
  8980. this.raise(TSErrors.InvalidPropertyAccessAfterInstantiationExpression, this.state.startLoc);
  8981. }
  8982. return result;
  8983. }
  8984. }
  8985. return super.parseSubscript(base, startLoc, noCalls, state);
  8986. }
  8987. parseNewCallee(node) {
  8988. var _callee$extra;
  8989. super.parseNewCallee(node);
  8990. const {
  8991. callee
  8992. } = node;
  8993. if (callee.type === "TSInstantiationExpression" && !((_callee$extra = callee.extra) != null && _callee$extra.parenthesized)) {
  8994. node.typeParameters = callee.typeParameters;
  8995. node.callee = callee.expression;
  8996. }
  8997. }
  8998. parseExprOp(left, leftStartLoc, minPrec) {
  8999. let isSatisfies;
  9000. if (tokenOperatorPrecedence(58) > minPrec && !this.hasPrecedingLineBreak() && (this.isContextual(93) || (isSatisfies = this.isContextual(120)))) {
  9001. const node = this.startNodeAt(leftStartLoc);
  9002. node.expression = left;
  9003. node.typeAnnotation = this.tsInType(() => {
  9004. this.next();
  9005. if (this.match(75)) {
  9006. if (isSatisfies) {
  9007. this.raise(Errors.UnexpectedKeyword, this.state.startLoc, {
  9008. keyword: "const"
  9009. });
  9010. }
  9011. return this.tsParseTypeReference();
  9012. }
  9013. return this.tsParseType();
  9014. });
  9015. this.finishNode(node, isSatisfies ? "TSSatisfiesExpression" : "TSAsExpression");
  9016. this.reScan_lt_gt();
  9017. return this.parseExprOp(node, leftStartLoc, minPrec);
  9018. }
  9019. return super.parseExprOp(left, leftStartLoc, minPrec);
  9020. }
  9021. checkReservedWord(word, startLoc, checkKeywords, isBinding) {
  9022. if (!this.state.isAmbientContext) {
  9023. super.checkReservedWord(word, startLoc, checkKeywords, isBinding);
  9024. }
  9025. }
  9026. checkImportReflection(node) {
  9027. super.checkImportReflection(node);
  9028. if (node.module && node.importKind !== "value") {
  9029. this.raise(TSErrors.ImportReflectionHasImportType, node.specifiers[0].loc.start);
  9030. }
  9031. }
  9032. checkDuplicateExports() {}
  9033. isPotentialImportPhase(isExport) {
  9034. if (super.isPotentialImportPhase(isExport)) return true;
  9035. if (this.isContextual(130)) {
  9036. const ch = this.lookaheadCharCode();
  9037. return isExport ? ch === 123 || ch === 42 : ch !== 61;
  9038. }
  9039. return !isExport && this.isContextual(87);
  9040. }
  9041. applyImportPhase(node, isExport, phase, loc) {
  9042. super.applyImportPhase(node, isExport, phase, loc);
  9043. if (isExport) {
  9044. node.exportKind = phase === "type" ? "type" : "value";
  9045. } else {
  9046. node.importKind = phase === "type" || phase === "typeof" ? phase : "value";
  9047. }
  9048. }
  9049. parseImport(node) {
  9050. if (this.match(133)) {
  9051. node.importKind = "value";
  9052. return super.parseImport(node);
  9053. }
  9054. let importNode;
  9055. if (tokenIsIdentifier(this.state.type) && this.lookaheadCharCode() === 61) {
  9056. node.importKind = "value";
  9057. return this.tsParseImportEqualsDeclaration(node);
  9058. } else if (this.isContextual(130)) {
  9059. const maybeDefaultIdentifier = this.parseMaybeImportPhase(node, false);
  9060. if (this.lookaheadCharCode() === 61) {
  9061. return this.tsParseImportEqualsDeclaration(node, maybeDefaultIdentifier);
  9062. } else {
  9063. importNode = super.parseImportSpecifiersAndAfter(node, maybeDefaultIdentifier);
  9064. }
  9065. } else {
  9066. importNode = super.parseImport(node);
  9067. }
  9068. if (importNode.importKind === "type" && importNode.specifiers.length > 1 && importNode.specifiers[0].type === "ImportDefaultSpecifier") {
  9069. this.raise(TSErrors.TypeImportCannotSpecifyDefaultAndNamed, importNode);
  9070. }
  9071. return importNode;
  9072. }
  9073. parseExport(node, decorators) {
  9074. if (this.match(83)) {
  9075. this.next();
  9076. const nodeImportEquals = node;
  9077. let maybeDefaultIdentifier = null;
  9078. if (this.isContextual(130) && this.isPotentialImportPhase(false)) {
  9079. maybeDefaultIdentifier = this.parseMaybeImportPhase(nodeImportEquals, false);
  9080. } else {
  9081. nodeImportEquals.importKind = "value";
  9082. }
  9083. return this.tsParseImportEqualsDeclaration(nodeImportEquals, maybeDefaultIdentifier, true);
  9084. } else if (this.eat(29)) {
  9085. const assign = node;
  9086. assign.expression = super.parseExpression();
  9087. this.semicolon();
  9088. this.sawUnambiguousESM = true;
  9089. return this.finishNode(assign, "TSExportAssignment");
  9090. } else if (this.eatContextual(93)) {
  9091. const decl = node;
  9092. this.expectContextual(128);
  9093. decl.id = this.parseIdentifier();
  9094. this.semicolon();
  9095. return this.finishNode(decl, "TSNamespaceExportDeclaration");
  9096. } else {
  9097. return super.parseExport(node, decorators);
  9098. }
  9099. }
  9100. isAbstractClass() {
  9101. return this.isContextual(124) && this.lookahead().type === 80;
  9102. }
  9103. parseExportDefaultExpression() {
  9104. if (this.isAbstractClass()) {
  9105. const cls = this.startNode();
  9106. this.next();
  9107. cls.abstract = true;
  9108. return this.parseClass(cls, true, true);
  9109. }
  9110. if (this.match(129)) {
  9111. const result = this.tsParseInterfaceDeclaration(this.startNode());
  9112. if (result) return result;
  9113. }
  9114. return super.parseExportDefaultExpression();
  9115. }
  9116. parseVarStatement(node, kind, allowMissingInitializer = false) {
  9117. const {
  9118. isAmbientContext
  9119. } = this.state;
  9120. const declaration = super.parseVarStatement(node, kind, allowMissingInitializer || isAmbientContext);
  9121. if (!isAmbientContext) return declaration;
  9122. for (const {
  9123. id,
  9124. init
  9125. } of declaration.declarations) {
  9126. if (!init) continue;
  9127. if (kind !== "const" || !!id.typeAnnotation) {
  9128. this.raise(TSErrors.InitializerNotAllowedInAmbientContext, init);
  9129. } else if (!isValidAmbientConstInitializer(init, this.hasPlugin("estree"))) {
  9130. this.raise(TSErrors.ConstInitializerMustBeStringOrNumericLiteralOrLiteralEnumReference, init);
  9131. }
  9132. }
  9133. return declaration;
  9134. }
  9135. parseStatementContent(flags, decorators) {
  9136. if (this.match(75) && this.isLookaheadContextual("enum")) {
  9137. const node = this.startNode();
  9138. this.expect(75);
  9139. return this.tsParseEnumDeclaration(node, {
  9140. const: true
  9141. });
  9142. }
  9143. if (this.isContextual(126)) {
  9144. return this.tsParseEnumDeclaration(this.startNode());
  9145. }
  9146. if (this.isContextual(129)) {
  9147. const result = this.tsParseInterfaceDeclaration(this.startNode());
  9148. if (result) return result;
  9149. }
  9150. return super.parseStatementContent(flags, decorators);
  9151. }
  9152. parseAccessModifier() {
  9153. return this.tsParseModifier(["public", "protected", "private"]);
  9154. }
  9155. tsHasSomeModifiers(member, modifiers) {
  9156. return modifiers.some(modifier => {
  9157. if (tsIsAccessModifier(modifier)) {
  9158. return member.accessibility === modifier;
  9159. }
  9160. return !!member[modifier];
  9161. });
  9162. }
  9163. tsIsStartOfStaticBlocks() {
  9164. return this.isContextual(106) && this.lookaheadCharCode() === 123;
  9165. }
  9166. parseClassMember(classBody, member, state) {
  9167. const modifiers = ["declare", "private", "public", "protected", "override", "abstract", "readonly", "static"];
  9168. this.tsParseModifiers({
  9169. allowedModifiers: modifiers,
  9170. disallowedModifiers: ["in", "out"],
  9171. stopOnStartOfClassStaticBlock: true,
  9172. errorTemplate: TSErrors.InvalidModifierOnTypeParameterPositions
  9173. }, member);
  9174. const callParseClassMemberWithIsStatic = () => {
  9175. if (this.tsIsStartOfStaticBlocks()) {
  9176. this.next();
  9177. this.next();
  9178. if (this.tsHasSomeModifiers(member, modifiers)) {
  9179. this.raise(TSErrors.StaticBlockCannotHaveModifier, this.state.curPosition());
  9180. }
  9181. super.parseClassStaticBlock(classBody, member);
  9182. } else {
  9183. this.parseClassMemberWithIsStatic(classBody, member, state, !!member.static);
  9184. }
  9185. };
  9186. if (member.declare) {
  9187. this.tsInAmbientContext(callParseClassMemberWithIsStatic);
  9188. } else {
  9189. callParseClassMemberWithIsStatic();
  9190. }
  9191. }
  9192. parseClassMemberWithIsStatic(classBody, member, state, isStatic) {
  9193. const idx = this.tsTryParseIndexSignature(member);
  9194. if (idx) {
  9195. classBody.body.push(idx);
  9196. if (member.abstract) {
  9197. this.raise(TSErrors.IndexSignatureHasAbstract, member);
  9198. }
  9199. if (member.accessibility) {
  9200. this.raise(TSErrors.IndexSignatureHasAccessibility, member, {
  9201. modifier: member.accessibility
  9202. });
  9203. }
  9204. if (member.declare) {
  9205. this.raise(TSErrors.IndexSignatureHasDeclare, member);
  9206. }
  9207. if (member.override) {
  9208. this.raise(TSErrors.IndexSignatureHasOverride, member);
  9209. }
  9210. return;
  9211. }
  9212. if (!this.state.inAbstractClass && member.abstract) {
  9213. this.raise(TSErrors.NonAbstractClassHasAbstractMethod, member);
  9214. }
  9215. if (member.override) {
  9216. if (!state.hadSuperClass) {
  9217. this.raise(TSErrors.OverrideNotInSubClass, member);
  9218. }
  9219. }
  9220. super.parseClassMemberWithIsStatic(classBody, member, state, isStatic);
  9221. }
  9222. parsePostMemberNameModifiers(methodOrProp) {
  9223. const optional = this.eat(17);
  9224. if (optional) methodOrProp.optional = true;
  9225. if (methodOrProp.readonly && this.match(10)) {
  9226. this.raise(TSErrors.ClassMethodHasReadonly, methodOrProp);
  9227. }
  9228. if (methodOrProp.declare && this.match(10)) {
  9229. this.raise(TSErrors.ClassMethodHasDeclare, methodOrProp);
  9230. }
  9231. }
  9232. parseExpressionStatement(node, expr, decorators) {
  9233. const decl = expr.type === "Identifier" ? this.tsParseExpressionStatement(node, expr, decorators) : undefined;
  9234. return decl || super.parseExpressionStatement(node, expr, decorators);
  9235. }
  9236. shouldParseExportDeclaration() {
  9237. if (this.tsIsDeclarationStart()) return true;
  9238. return super.shouldParseExportDeclaration();
  9239. }
  9240. parseConditional(expr, startLoc, refExpressionErrors) {
  9241. if (!this.state.maybeInArrowParameters || !this.match(17)) {
  9242. return super.parseConditional(expr, startLoc, refExpressionErrors);
  9243. }
  9244. const result = this.tryParse(() => super.parseConditional(expr, startLoc));
  9245. if (!result.node) {
  9246. if (result.error) {
  9247. super.setOptionalParametersError(refExpressionErrors, result.error);
  9248. }
  9249. return expr;
  9250. }
  9251. if (result.error) this.state = result.failState;
  9252. return result.node;
  9253. }
  9254. parseParenItem(node, startLoc) {
  9255. const newNode = super.parseParenItem(node, startLoc);
  9256. if (this.eat(17)) {
  9257. newNode.optional = true;
  9258. this.resetEndLocation(node);
  9259. }
  9260. if (this.match(14)) {
  9261. const typeCastNode = this.startNodeAt(startLoc);
  9262. typeCastNode.expression = node;
  9263. typeCastNode.typeAnnotation = this.tsParseTypeAnnotation();
  9264. return this.finishNode(typeCastNode, "TSTypeCastExpression");
  9265. }
  9266. return node;
  9267. }
  9268. parseExportDeclaration(node) {
  9269. if (!this.state.isAmbientContext && this.isContextual(125)) {
  9270. return this.tsInAmbientContext(() => this.parseExportDeclaration(node));
  9271. }
  9272. const startLoc = this.state.startLoc;
  9273. const isDeclare = this.eatContextual(125);
  9274. if (isDeclare && (this.isContextual(125) || !this.shouldParseExportDeclaration())) {
  9275. throw this.raise(TSErrors.ExpectedAmbientAfterExportDeclare, this.state.startLoc);
  9276. }
  9277. const isIdentifier = tokenIsIdentifier(this.state.type);
  9278. const declaration = isIdentifier && this.tsTryParseExportDeclaration() || super.parseExportDeclaration(node);
  9279. if (!declaration) return null;
  9280. if (declaration.type === "TSInterfaceDeclaration" || declaration.type === "TSTypeAliasDeclaration" || isDeclare) {
  9281. node.exportKind = "type";
  9282. }
  9283. if (isDeclare) {
  9284. this.resetStartLocation(declaration, startLoc);
  9285. declaration.declare = true;
  9286. }
  9287. return declaration;
  9288. }
  9289. parseClassId(node, isStatement, optionalId, bindingType) {
  9290. if ((!isStatement || optionalId) && this.isContextual(113)) {
  9291. return;
  9292. }
  9293. super.parseClassId(node, isStatement, optionalId, node.declare ? 1024 : 8331);
  9294. const typeParameters = this.tsTryParseTypeParameters(this.tsParseInOutConstModifiers);
  9295. if (typeParameters) node.typeParameters = typeParameters;
  9296. }
  9297. parseClassPropertyAnnotation(node) {
  9298. if (!node.optional) {
  9299. if (this.eat(35)) {
  9300. node.definite = true;
  9301. } else if (this.eat(17)) {
  9302. node.optional = true;
  9303. }
  9304. }
  9305. const type = this.tsTryParseTypeAnnotation();
  9306. if (type) node.typeAnnotation = type;
  9307. }
  9308. parseClassProperty(node) {
  9309. this.parseClassPropertyAnnotation(node);
  9310. if (this.state.isAmbientContext && !(node.readonly && !node.typeAnnotation) && this.match(29)) {
  9311. this.raise(TSErrors.DeclareClassFieldHasInitializer, this.state.startLoc);
  9312. }
  9313. if (node.abstract && this.match(29)) {
  9314. const {
  9315. key
  9316. } = node;
  9317. this.raise(TSErrors.AbstractPropertyHasInitializer, this.state.startLoc, {
  9318. propertyName: key.type === "Identifier" && !node.computed ? key.name : `[${this.input.slice(key.start, key.end)}]`
  9319. });
  9320. }
  9321. return super.parseClassProperty(node);
  9322. }
  9323. parseClassPrivateProperty(node) {
  9324. if (node.abstract) {
  9325. this.raise(TSErrors.PrivateElementHasAbstract, node);
  9326. }
  9327. if (node.accessibility) {
  9328. this.raise(TSErrors.PrivateElementHasAccessibility, node, {
  9329. modifier: node.accessibility
  9330. });
  9331. }
  9332. this.parseClassPropertyAnnotation(node);
  9333. return super.parseClassPrivateProperty(node);
  9334. }
  9335. parseClassAccessorProperty(node) {
  9336. this.parseClassPropertyAnnotation(node);
  9337. if (node.optional) {
  9338. this.raise(TSErrors.AccessorCannotBeOptional, node);
  9339. }
  9340. return super.parseClassAccessorProperty(node);
  9341. }
  9342. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  9343. const typeParameters = this.tsTryParseTypeParameters(this.tsParseConstModifier);
  9344. if (typeParameters && isConstructor) {
  9345. this.raise(TSErrors.ConstructorHasTypeParameters, typeParameters);
  9346. }
  9347. const {
  9348. declare = false,
  9349. kind
  9350. } = method;
  9351. if (declare && (kind === "get" || kind === "set")) {
  9352. this.raise(TSErrors.DeclareAccessor, method, {
  9353. kind
  9354. });
  9355. }
  9356. if (typeParameters) method.typeParameters = typeParameters;
  9357. super.pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper);
  9358. }
  9359. pushClassPrivateMethod(classBody, method, isGenerator, isAsync) {
  9360. const typeParameters = this.tsTryParseTypeParameters(this.tsParseConstModifier);
  9361. if (typeParameters) method.typeParameters = typeParameters;
  9362. super.pushClassPrivateMethod(classBody, method, isGenerator, isAsync);
  9363. }
  9364. declareClassPrivateMethodInScope(node, kind) {
  9365. if (node.type === "TSDeclareMethod") return;
  9366. if (node.type === "MethodDefinition" && !hasOwnProperty.call(node.value, "body")) {
  9367. return;
  9368. }
  9369. super.declareClassPrivateMethodInScope(node, kind);
  9370. }
  9371. parseClassSuper(node) {
  9372. super.parseClassSuper(node);
  9373. if (node.superClass && (this.match(47) || this.match(51))) {
  9374. node.superTypeParameters = this.tsParseTypeArgumentsInExpression();
  9375. }
  9376. if (this.eatContextual(113)) {
  9377. node.implements = this.tsParseHeritageClause("implements");
  9378. }
  9379. }
  9380. parseObjPropValue(prop, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors) {
  9381. const typeParameters = this.tsTryParseTypeParameters(this.tsParseConstModifier);
  9382. if (typeParameters) prop.typeParameters = typeParameters;
  9383. return super.parseObjPropValue(prop, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors);
  9384. }
  9385. parseFunctionParams(node, isConstructor) {
  9386. const typeParameters = this.tsTryParseTypeParameters(this.tsParseConstModifier);
  9387. if (typeParameters) node.typeParameters = typeParameters;
  9388. super.parseFunctionParams(node, isConstructor);
  9389. }
  9390. parseVarId(decl, kind) {
  9391. super.parseVarId(decl, kind);
  9392. if (decl.id.type === "Identifier" && !this.hasPrecedingLineBreak() && this.eat(35)) {
  9393. decl.definite = true;
  9394. }
  9395. const type = this.tsTryParseTypeAnnotation();
  9396. if (type) {
  9397. decl.id.typeAnnotation = type;
  9398. this.resetEndLocation(decl.id);
  9399. }
  9400. }
  9401. parseAsyncArrowFromCallExpression(node, call) {
  9402. if (this.match(14)) {
  9403. node.returnType = this.tsParseTypeAnnotation();
  9404. }
  9405. return super.parseAsyncArrowFromCallExpression(node, call);
  9406. }
  9407. parseMaybeAssign(refExpressionErrors, afterLeftParse) {
  9408. var _jsx, _jsx2, _typeCast, _jsx3, _typeCast2;
  9409. let state;
  9410. let jsx;
  9411. let typeCast;
  9412. if (this.hasPlugin("jsx") && (this.match(142) || this.match(47))) {
  9413. state = this.state.clone();
  9414. jsx = this.tryParse(() => super.parseMaybeAssign(refExpressionErrors, afterLeftParse), state);
  9415. if (!jsx.error) return jsx.node;
  9416. const {
  9417. context
  9418. } = this.state;
  9419. const currentContext = context[context.length - 1];
  9420. if (currentContext === types.j_oTag || currentContext === types.j_expr) {
  9421. context.pop();
  9422. }
  9423. }
  9424. if (!((_jsx = jsx) != null && _jsx.error) && !this.match(47)) {
  9425. return super.parseMaybeAssign(refExpressionErrors, afterLeftParse);
  9426. }
  9427. if (!state || state === this.state) state = this.state.clone();
  9428. let typeParameters;
  9429. const arrow = this.tryParse(abort => {
  9430. var _expr$extra, _typeParameters;
  9431. typeParameters = this.tsParseTypeParameters(this.tsParseConstModifier);
  9432. const expr = super.parseMaybeAssign(refExpressionErrors, afterLeftParse);
  9433. if (expr.type !== "ArrowFunctionExpression" || (_expr$extra = expr.extra) != null && _expr$extra.parenthesized) {
  9434. abort();
  9435. }
  9436. if (((_typeParameters = typeParameters) == null ? void 0 : _typeParameters.params.length) !== 0) {
  9437. this.resetStartLocationFromNode(expr, typeParameters);
  9438. }
  9439. expr.typeParameters = typeParameters;
  9440. return expr;
  9441. }, state);
  9442. if (!arrow.error && !arrow.aborted) {
  9443. if (typeParameters) this.reportReservedArrowTypeParam(typeParameters);
  9444. return arrow.node;
  9445. }
  9446. if (!jsx) {
  9447. assert(!this.hasPlugin("jsx"));
  9448. typeCast = this.tryParse(() => super.parseMaybeAssign(refExpressionErrors, afterLeftParse), state);
  9449. if (!typeCast.error) return typeCast.node;
  9450. }
  9451. if ((_jsx2 = jsx) != null && _jsx2.node) {
  9452. this.state = jsx.failState;
  9453. return jsx.node;
  9454. }
  9455. if (arrow.node) {
  9456. this.state = arrow.failState;
  9457. if (typeParameters) this.reportReservedArrowTypeParam(typeParameters);
  9458. return arrow.node;
  9459. }
  9460. if ((_typeCast = typeCast) != null && _typeCast.node) {
  9461. this.state = typeCast.failState;
  9462. return typeCast.node;
  9463. }
  9464. throw ((_jsx3 = jsx) == null ? void 0 : _jsx3.error) || arrow.error || ((_typeCast2 = typeCast) == null ? void 0 : _typeCast2.error);
  9465. }
  9466. reportReservedArrowTypeParam(node) {
  9467. var _node$extra;
  9468. if (node.params.length === 1 && !node.params[0].constraint && !((_node$extra = node.extra) != null && _node$extra.trailingComma) && this.getPluginOption("typescript", "disallowAmbiguousJSXLike")) {
  9469. this.raise(TSErrors.ReservedArrowTypeParam, node);
  9470. }
  9471. }
  9472. parseMaybeUnary(refExpressionErrors, sawUnary) {
  9473. if (!this.hasPlugin("jsx") && this.match(47)) {
  9474. return this.tsParseTypeAssertion();
  9475. }
  9476. return super.parseMaybeUnary(refExpressionErrors, sawUnary);
  9477. }
  9478. parseArrow(node) {
  9479. if (this.match(14)) {
  9480. const result = this.tryParse(abort => {
  9481. const returnType = this.tsParseTypeOrTypePredicateAnnotation(14);
  9482. if (this.canInsertSemicolon() || !this.match(19)) abort();
  9483. return returnType;
  9484. });
  9485. if (result.aborted) return;
  9486. if (!result.thrown) {
  9487. if (result.error) this.state = result.failState;
  9488. node.returnType = result.node;
  9489. }
  9490. }
  9491. return super.parseArrow(node);
  9492. }
  9493. parseAssignableListItemTypes(param, flags) {
  9494. if (!(flags & 2)) return param;
  9495. if (this.eat(17)) {
  9496. param.optional = true;
  9497. }
  9498. const type = this.tsTryParseTypeAnnotation();
  9499. if (type) param.typeAnnotation = type;
  9500. this.resetEndLocation(param);
  9501. return param;
  9502. }
  9503. isAssignable(node, isBinding) {
  9504. switch (node.type) {
  9505. case "TSTypeCastExpression":
  9506. return this.isAssignable(node.expression, isBinding);
  9507. case "TSParameterProperty":
  9508. return true;
  9509. default:
  9510. return super.isAssignable(node, isBinding);
  9511. }
  9512. }
  9513. toAssignable(node, isLHS = false) {
  9514. switch (node.type) {
  9515. case "ParenthesizedExpression":
  9516. this.toAssignableParenthesizedExpression(node, isLHS);
  9517. break;
  9518. case "TSAsExpression":
  9519. case "TSSatisfiesExpression":
  9520. case "TSNonNullExpression":
  9521. case "TSTypeAssertion":
  9522. if (isLHS) {
  9523. this.expressionScope.recordArrowParameterBindingError(TSErrors.UnexpectedTypeCastInParameter, node);
  9524. } else {
  9525. this.raise(TSErrors.UnexpectedTypeCastInParameter, node);
  9526. }
  9527. this.toAssignable(node.expression, isLHS);
  9528. break;
  9529. case "AssignmentExpression":
  9530. if (!isLHS && node.left.type === "TSTypeCastExpression") {
  9531. node.left = this.typeCastToParameter(node.left);
  9532. }
  9533. default:
  9534. super.toAssignable(node, isLHS);
  9535. }
  9536. }
  9537. toAssignableParenthesizedExpression(node, isLHS) {
  9538. switch (node.expression.type) {
  9539. case "TSAsExpression":
  9540. case "TSSatisfiesExpression":
  9541. case "TSNonNullExpression":
  9542. case "TSTypeAssertion":
  9543. case "ParenthesizedExpression":
  9544. this.toAssignable(node.expression, isLHS);
  9545. break;
  9546. default:
  9547. super.toAssignable(node, isLHS);
  9548. }
  9549. }
  9550. checkToRestConversion(node, allowPattern) {
  9551. switch (node.type) {
  9552. case "TSAsExpression":
  9553. case "TSSatisfiesExpression":
  9554. case "TSTypeAssertion":
  9555. case "TSNonNullExpression":
  9556. this.checkToRestConversion(node.expression, false);
  9557. break;
  9558. default:
  9559. super.checkToRestConversion(node, allowPattern);
  9560. }
  9561. }
  9562. isValidLVal(type, isUnparenthesizedInAssign, binding) {
  9563. switch (type) {
  9564. case "TSTypeCastExpression":
  9565. return true;
  9566. case "TSParameterProperty":
  9567. return "parameter";
  9568. case "TSNonNullExpression":
  9569. case "TSInstantiationExpression":
  9570. return "expression";
  9571. case "TSAsExpression":
  9572. case "TSSatisfiesExpression":
  9573. case "TSTypeAssertion":
  9574. return (binding !== 64 || !isUnparenthesizedInAssign) && ["expression", true];
  9575. default:
  9576. return super.isValidLVal(type, isUnparenthesizedInAssign, binding);
  9577. }
  9578. }
  9579. parseBindingAtom() {
  9580. if (this.state.type === 78) {
  9581. return this.parseIdentifier(true);
  9582. }
  9583. return super.parseBindingAtom();
  9584. }
  9585. parseMaybeDecoratorArguments(expr) {
  9586. if (this.match(47) || this.match(51)) {
  9587. const typeArguments = this.tsParseTypeArgumentsInExpression();
  9588. if (this.match(10)) {
  9589. const call = super.parseMaybeDecoratorArguments(expr);
  9590. call.typeParameters = typeArguments;
  9591. return call;
  9592. }
  9593. this.unexpected(null, 10);
  9594. }
  9595. return super.parseMaybeDecoratorArguments(expr);
  9596. }
  9597. checkCommaAfterRest(close) {
  9598. if (this.state.isAmbientContext && this.match(12) && this.lookaheadCharCode() === close) {
  9599. this.next();
  9600. return false;
  9601. }
  9602. return super.checkCommaAfterRest(close);
  9603. }
  9604. isClassMethod() {
  9605. return this.match(47) || super.isClassMethod();
  9606. }
  9607. isClassProperty() {
  9608. return this.match(35) || this.match(14) || super.isClassProperty();
  9609. }
  9610. parseMaybeDefault(startLoc, left) {
  9611. const node = super.parseMaybeDefault(startLoc, left);
  9612. if (node.type === "AssignmentPattern" && node.typeAnnotation && node.right.start < node.typeAnnotation.start) {
  9613. this.raise(TSErrors.TypeAnnotationAfterAssign, node.typeAnnotation);
  9614. }
  9615. return node;
  9616. }
  9617. getTokenFromCode(code) {
  9618. if (this.state.inType) {
  9619. if (code === 62) {
  9620. this.finishOp(48, 1);
  9621. return;
  9622. }
  9623. if (code === 60) {
  9624. this.finishOp(47, 1);
  9625. return;
  9626. }
  9627. }
  9628. super.getTokenFromCode(code);
  9629. }
  9630. reScan_lt_gt() {
  9631. const {
  9632. type
  9633. } = this.state;
  9634. if (type === 47) {
  9635. this.state.pos -= 1;
  9636. this.readToken_lt();
  9637. } else if (type === 48) {
  9638. this.state.pos -= 1;
  9639. this.readToken_gt();
  9640. }
  9641. }
  9642. reScan_lt() {
  9643. const {
  9644. type
  9645. } = this.state;
  9646. if (type === 51) {
  9647. this.state.pos -= 2;
  9648. this.finishOp(47, 1);
  9649. return 47;
  9650. }
  9651. return type;
  9652. }
  9653. toAssignableList(exprList, trailingCommaLoc, isLHS) {
  9654. for (let i = 0; i < exprList.length; i++) {
  9655. const expr = exprList[i];
  9656. if ((expr == null ? void 0 : expr.type) === "TSTypeCastExpression") {
  9657. exprList[i] = this.typeCastToParameter(expr);
  9658. }
  9659. }
  9660. super.toAssignableList(exprList, trailingCommaLoc, isLHS);
  9661. }
  9662. typeCastToParameter(node) {
  9663. node.expression.typeAnnotation = node.typeAnnotation;
  9664. this.resetEndLocation(node.expression, node.typeAnnotation.loc.end);
  9665. return node.expression;
  9666. }
  9667. shouldParseArrow(params) {
  9668. if (this.match(14)) {
  9669. return params.every(expr => this.isAssignable(expr, true));
  9670. }
  9671. return super.shouldParseArrow(params);
  9672. }
  9673. shouldParseAsyncArrow() {
  9674. return this.match(14) || super.shouldParseAsyncArrow();
  9675. }
  9676. canHaveLeadingDecorator() {
  9677. return super.canHaveLeadingDecorator() || this.isAbstractClass();
  9678. }
  9679. jsxParseOpeningElementAfterName(node) {
  9680. if (this.match(47) || this.match(51)) {
  9681. const typeArguments = this.tsTryParseAndCatch(() => this.tsParseTypeArgumentsInExpression());
  9682. if (typeArguments) node.typeParameters = typeArguments;
  9683. }
  9684. return super.jsxParseOpeningElementAfterName(node);
  9685. }
  9686. getGetterSetterExpectedParamCount(method) {
  9687. const baseCount = super.getGetterSetterExpectedParamCount(method);
  9688. const params = this.getObjectOrClassMethodParams(method);
  9689. const firstParam = params[0];
  9690. const hasContextParam = firstParam && this.isThisParam(firstParam);
  9691. return hasContextParam ? baseCount + 1 : baseCount;
  9692. }
  9693. parseCatchClauseParam() {
  9694. const param = super.parseCatchClauseParam();
  9695. const type = this.tsTryParseTypeAnnotation();
  9696. if (type) {
  9697. param.typeAnnotation = type;
  9698. this.resetEndLocation(param);
  9699. }
  9700. return param;
  9701. }
  9702. tsInAmbientContext(cb) {
  9703. const {
  9704. isAmbientContext: oldIsAmbientContext,
  9705. strict: oldStrict
  9706. } = this.state;
  9707. this.state.isAmbientContext = true;
  9708. this.state.strict = false;
  9709. try {
  9710. return cb();
  9711. } finally {
  9712. this.state.isAmbientContext = oldIsAmbientContext;
  9713. this.state.strict = oldStrict;
  9714. }
  9715. }
  9716. parseClass(node, isStatement, optionalId) {
  9717. const oldInAbstractClass = this.state.inAbstractClass;
  9718. this.state.inAbstractClass = !!node.abstract;
  9719. try {
  9720. return super.parseClass(node, isStatement, optionalId);
  9721. } finally {
  9722. this.state.inAbstractClass = oldInAbstractClass;
  9723. }
  9724. }
  9725. tsParseAbstractDeclaration(node, decorators) {
  9726. if (this.match(80)) {
  9727. node.abstract = true;
  9728. return this.maybeTakeDecorators(decorators, this.parseClass(node, true, false));
  9729. } else if (this.isContextual(129)) {
  9730. if (!this.hasFollowingLineBreak()) {
  9731. node.abstract = true;
  9732. this.raise(TSErrors.NonClassMethodPropertyHasAbstractModifer, node);
  9733. return this.tsParseInterfaceDeclaration(node);
  9734. }
  9735. } else {
  9736. this.unexpected(null, 80);
  9737. }
  9738. }
  9739. parseMethod(node, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope) {
  9740. const method = super.parseMethod(node, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope);
  9741. if (method.abstract) {
  9742. const hasBody = this.hasPlugin("estree") ? !!method.value.body : !!method.body;
  9743. if (hasBody) {
  9744. const {
  9745. key
  9746. } = method;
  9747. this.raise(TSErrors.AbstractMethodHasImplementation, method, {
  9748. methodName: key.type === "Identifier" && !method.computed ? key.name : `[${this.input.slice(key.start, key.end)}]`
  9749. });
  9750. }
  9751. }
  9752. return method;
  9753. }
  9754. tsParseTypeParameterName() {
  9755. const typeName = this.parseIdentifier();
  9756. return typeName.name;
  9757. }
  9758. shouldParseAsAmbientContext() {
  9759. return !!this.getPluginOption("typescript", "dts");
  9760. }
  9761. parse() {
  9762. if (this.shouldParseAsAmbientContext()) {
  9763. this.state.isAmbientContext = true;
  9764. }
  9765. return super.parse();
  9766. }
  9767. getExpression() {
  9768. if (this.shouldParseAsAmbientContext()) {
  9769. this.state.isAmbientContext = true;
  9770. }
  9771. return super.getExpression();
  9772. }
  9773. parseExportSpecifier(node, isString, isInTypeExport, isMaybeTypeOnly) {
  9774. if (!isString && isMaybeTypeOnly) {
  9775. this.parseTypeOnlyImportExportSpecifier(node, false, isInTypeExport);
  9776. return this.finishNode(node, "ExportSpecifier");
  9777. }
  9778. node.exportKind = "value";
  9779. return super.parseExportSpecifier(node, isString, isInTypeExport, isMaybeTypeOnly);
  9780. }
  9781. parseImportSpecifier(specifier, importedIsString, isInTypeOnlyImport, isMaybeTypeOnly, bindingType) {
  9782. if (!importedIsString && isMaybeTypeOnly) {
  9783. this.parseTypeOnlyImportExportSpecifier(specifier, true, isInTypeOnlyImport);
  9784. return this.finishNode(specifier, "ImportSpecifier");
  9785. }
  9786. specifier.importKind = "value";
  9787. return super.parseImportSpecifier(specifier, importedIsString, isInTypeOnlyImport, isMaybeTypeOnly, isInTypeOnlyImport ? 4098 : 4096);
  9788. }
  9789. parseTypeOnlyImportExportSpecifier(node, isImport, isInTypeOnlyImportExport) {
  9790. const leftOfAsKey = isImport ? "imported" : "local";
  9791. const rightOfAsKey = isImport ? "local" : "exported";
  9792. let leftOfAs = node[leftOfAsKey];
  9793. let rightOfAs;
  9794. let hasTypeSpecifier = false;
  9795. let canParseAsKeyword = true;
  9796. const loc = leftOfAs.loc.start;
  9797. if (this.isContextual(93)) {
  9798. const firstAs = this.parseIdentifier();
  9799. if (this.isContextual(93)) {
  9800. const secondAs = this.parseIdentifier();
  9801. if (tokenIsKeywordOrIdentifier(this.state.type)) {
  9802. hasTypeSpecifier = true;
  9803. leftOfAs = firstAs;
  9804. rightOfAs = isImport ? this.parseIdentifier() : this.parseModuleExportName();
  9805. canParseAsKeyword = false;
  9806. } else {
  9807. rightOfAs = secondAs;
  9808. canParseAsKeyword = false;
  9809. }
  9810. } else if (tokenIsKeywordOrIdentifier(this.state.type)) {
  9811. canParseAsKeyword = false;
  9812. rightOfAs = isImport ? this.parseIdentifier() : this.parseModuleExportName();
  9813. } else {
  9814. hasTypeSpecifier = true;
  9815. leftOfAs = firstAs;
  9816. }
  9817. } else if (tokenIsKeywordOrIdentifier(this.state.type)) {
  9818. hasTypeSpecifier = true;
  9819. if (isImport) {
  9820. leftOfAs = this.parseIdentifier(true);
  9821. if (!this.isContextual(93)) {
  9822. this.checkReservedWord(leftOfAs.name, leftOfAs.loc.start, true, true);
  9823. }
  9824. } else {
  9825. leftOfAs = this.parseModuleExportName();
  9826. }
  9827. }
  9828. if (hasTypeSpecifier && isInTypeOnlyImportExport) {
  9829. this.raise(isImport ? TSErrors.TypeModifierIsUsedInTypeImports : TSErrors.TypeModifierIsUsedInTypeExports, loc);
  9830. }
  9831. node[leftOfAsKey] = leftOfAs;
  9832. node[rightOfAsKey] = rightOfAs;
  9833. const kindKey = isImport ? "importKind" : "exportKind";
  9834. node[kindKey] = hasTypeSpecifier ? "type" : "value";
  9835. if (canParseAsKeyword && this.eatContextual(93)) {
  9836. node[rightOfAsKey] = isImport ? this.parseIdentifier() : this.parseModuleExportName();
  9837. }
  9838. if (!node[rightOfAsKey]) {
  9839. node[rightOfAsKey] = cloneIdentifier(node[leftOfAsKey]);
  9840. }
  9841. if (isImport) {
  9842. this.checkIdentifier(node[rightOfAsKey], hasTypeSpecifier ? 4098 : 4096);
  9843. }
  9844. }
  9845. };
  9846. function isPossiblyLiteralEnum(expression) {
  9847. if (expression.type !== "MemberExpression") return false;
  9848. const {
  9849. computed,
  9850. property
  9851. } = expression;
  9852. if (computed && property.type !== "StringLiteral" && (property.type !== "TemplateLiteral" || property.expressions.length > 0)) {
  9853. return false;
  9854. }
  9855. return isUncomputedMemberExpressionChain(expression.object);
  9856. }
  9857. function isValidAmbientConstInitializer(expression, estree) {
  9858. var _expression$extra;
  9859. const {
  9860. type
  9861. } = expression;
  9862. if ((_expression$extra = expression.extra) != null && _expression$extra.parenthesized) {
  9863. return false;
  9864. }
  9865. if (estree) {
  9866. if (type === "Literal") {
  9867. const {
  9868. value
  9869. } = expression;
  9870. if (typeof value === "string" || typeof value === "boolean") {
  9871. return true;
  9872. }
  9873. }
  9874. } else {
  9875. if (type === "StringLiteral" || type === "BooleanLiteral") {
  9876. return true;
  9877. }
  9878. }
  9879. if (isNumber(expression, estree) || isNegativeNumber(expression, estree)) {
  9880. return true;
  9881. }
  9882. if (type === "TemplateLiteral" && expression.expressions.length === 0) {
  9883. return true;
  9884. }
  9885. if (isPossiblyLiteralEnum(expression)) {
  9886. return true;
  9887. }
  9888. return false;
  9889. }
  9890. function isNumber(expression, estree) {
  9891. if (estree) {
  9892. return expression.type === "Literal" && (typeof expression.value === "number" || "bigint" in expression);
  9893. }
  9894. return expression.type === "NumericLiteral" || expression.type === "BigIntLiteral";
  9895. }
  9896. function isNegativeNumber(expression, estree) {
  9897. if (expression.type === "UnaryExpression") {
  9898. const {
  9899. operator,
  9900. argument
  9901. } = expression;
  9902. if (operator === "-" && isNumber(argument, estree)) {
  9903. return true;
  9904. }
  9905. }
  9906. return false;
  9907. }
  9908. function isUncomputedMemberExpressionChain(expression) {
  9909. if (expression.type === "Identifier") return true;
  9910. if (expression.type !== "MemberExpression" || expression.computed) {
  9911. return false;
  9912. }
  9913. return isUncomputedMemberExpressionChain(expression.object);
  9914. }
  9915. const PlaceholderErrors = ParseErrorEnum`placeholders`({
  9916. ClassNameIsRequired: "A class name is required.",
  9917. UnexpectedSpace: "Unexpected space in placeholder."
  9918. });
  9919. var placeholders = superClass => class PlaceholdersParserMixin extends superClass {
  9920. parsePlaceholder(expectedNode) {
  9921. if (this.match(144)) {
  9922. const node = this.startNode();
  9923. this.next();
  9924. this.assertNoSpace();
  9925. node.name = super.parseIdentifier(true);
  9926. this.assertNoSpace();
  9927. this.expect(144);
  9928. return this.finishPlaceholder(node, expectedNode);
  9929. }
  9930. }
  9931. finishPlaceholder(node, expectedNode) {
  9932. let placeholder = node;
  9933. if (!placeholder.expectedNode || !placeholder.type) {
  9934. placeholder = this.finishNode(placeholder, "Placeholder");
  9935. }
  9936. placeholder.expectedNode = expectedNode;
  9937. return placeholder;
  9938. }
  9939. getTokenFromCode(code) {
  9940. if (code === 37 && this.input.charCodeAt(this.state.pos + 1) === 37) {
  9941. this.finishOp(144, 2);
  9942. } else {
  9943. super.getTokenFromCode(code);
  9944. }
  9945. }
  9946. parseExprAtom(refExpressionErrors) {
  9947. return this.parsePlaceholder("Expression") || super.parseExprAtom(refExpressionErrors);
  9948. }
  9949. parseIdentifier(liberal) {
  9950. return this.parsePlaceholder("Identifier") || super.parseIdentifier(liberal);
  9951. }
  9952. checkReservedWord(word, startLoc, checkKeywords, isBinding) {
  9953. if (word !== undefined) {
  9954. super.checkReservedWord(word, startLoc, checkKeywords, isBinding);
  9955. }
  9956. }
  9957. parseBindingAtom() {
  9958. return this.parsePlaceholder("Pattern") || super.parseBindingAtom();
  9959. }
  9960. isValidLVal(type, isParenthesized, binding) {
  9961. return type === "Placeholder" || super.isValidLVal(type, isParenthesized, binding);
  9962. }
  9963. toAssignable(node, isLHS) {
  9964. if (node && node.type === "Placeholder" && node.expectedNode === "Expression") {
  9965. node.expectedNode = "Pattern";
  9966. } else {
  9967. super.toAssignable(node, isLHS);
  9968. }
  9969. }
  9970. chStartsBindingIdentifier(ch, pos) {
  9971. if (super.chStartsBindingIdentifier(ch, pos)) {
  9972. return true;
  9973. }
  9974. const nextToken = this.lookahead();
  9975. if (nextToken.type === 144) {
  9976. return true;
  9977. }
  9978. return false;
  9979. }
  9980. verifyBreakContinue(node, isBreak) {
  9981. if (node.label && node.label.type === "Placeholder") return;
  9982. super.verifyBreakContinue(node, isBreak);
  9983. }
  9984. parseExpressionStatement(node, expr) {
  9985. var _expr$extra;
  9986. if (expr.type !== "Placeholder" || (_expr$extra = expr.extra) != null && _expr$extra.parenthesized) {
  9987. return super.parseExpressionStatement(node, expr);
  9988. }
  9989. if (this.match(14)) {
  9990. const stmt = node;
  9991. stmt.label = this.finishPlaceholder(expr, "Identifier");
  9992. this.next();
  9993. stmt.body = super.parseStatementOrSloppyAnnexBFunctionDeclaration();
  9994. return this.finishNode(stmt, "LabeledStatement");
  9995. }
  9996. this.semicolon();
  9997. const stmtPlaceholder = node;
  9998. stmtPlaceholder.name = expr.name;
  9999. return this.finishPlaceholder(stmtPlaceholder, "Statement");
  10000. }
  10001. parseBlock(allowDirectives, createNewLexicalScope, afterBlockParse) {
  10002. return this.parsePlaceholder("BlockStatement") || super.parseBlock(allowDirectives, createNewLexicalScope, afterBlockParse);
  10003. }
  10004. parseFunctionId(requireId) {
  10005. return this.parsePlaceholder("Identifier") || super.parseFunctionId(requireId);
  10006. }
  10007. parseClass(node, isStatement, optionalId) {
  10008. const type = isStatement ? "ClassDeclaration" : "ClassExpression";
  10009. this.next();
  10010. const oldStrict = this.state.strict;
  10011. const placeholder = this.parsePlaceholder("Identifier");
  10012. if (placeholder) {
  10013. if (this.match(81) || this.match(144) || this.match(5)) {
  10014. node.id = placeholder;
  10015. } else if (optionalId || !isStatement) {
  10016. node.id = null;
  10017. node.body = this.finishPlaceholder(placeholder, "ClassBody");
  10018. return this.finishNode(node, type);
  10019. } else {
  10020. throw this.raise(PlaceholderErrors.ClassNameIsRequired, this.state.startLoc);
  10021. }
  10022. } else {
  10023. this.parseClassId(node, isStatement, optionalId);
  10024. }
  10025. super.parseClassSuper(node);
  10026. node.body = this.parsePlaceholder("ClassBody") || super.parseClassBody(!!node.superClass, oldStrict);
  10027. return this.finishNode(node, type);
  10028. }
  10029. parseExport(node, decorators) {
  10030. const placeholder = this.parsePlaceholder("Identifier");
  10031. if (!placeholder) return super.parseExport(node, decorators);
  10032. const node2 = node;
  10033. if (!this.isContextual(98) && !this.match(12)) {
  10034. node2.specifiers = [];
  10035. node2.source = null;
  10036. node2.declaration = this.finishPlaceholder(placeholder, "Declaration");
  10037. return this.finishNode(node2, "ExportNamedDeclaration");
  10038. }
  10039. this.expectPlugin("exportDefaultFrom");
  10040. const specifier = this.startNode();
  10041. specifier.exported = placeholder;
  10042. node2.specifiers = [this.finishNode(specifier, "ExportDefaultSpecifier")];
  10043. return super.parseExport(node2, decorators);
  10044. }
  10045. isExportDefaultSpecifier() {
  10046. if (this.match(65)) {
  10047. const next = this.nextTokenStart();
  10048. if (this.isUnparsedContextual(next, "from")) {
  10049. if (this.input.startsWith(tokenLabelName(144), this.nextTokenStartSince(next + 4))) {
  10050. return true;
  10051. }
  10052. }
  10053. }
  10054. return super.isExportDefaultSpecifier();
  10055. }
  10056. maybeParseExportDefaultSpecifier(node, maybeDefaultIdentifier) {
  10057. var _specifiers;
  10058. if ((_specifiers = node.specifiers) != null && _specifiers.length) {
  10059. return true;
  10060. }
  10061. return super.maybeParseExportDefaultSpecifier(node, maybeDefaultIdentifier);
  10062. }
  10063. checkExport(node) {
  10064. const {
  10065. specifiers
  10066. } = node;
  10067. if (specifiers != null && specifiers.length) {
  10068. node.specifiers = specifiers.filter(node => node.exported.type === "Placeholder");
  10069. }
  10070. super.checkExport(node);
  10071. node.specifiers = specifiers;
  10072. }
  10073. parseImport(node) {
  10074. const placeholder = this.parsePlaceholder("Identifier");
  10075. if (!placeholder) return super.parseImport(node);
  10076. node.specifiers = [];
  10077. if (!this.isContextual(98) && !this.match(12)) {
  10078. node.source = this.finishPlaceholder(placeholder, "StringLiteral");
  10079. this.semicolon();
  10080. return this.finishNode(node, "ImportDeclaration");
  10081. }
  10082. const specifier = this.startNodeAtNode(placeholder);
  10083. specifier.local = placeholder;
  10084. node.specifiers.push(this.finishNode(specifier, "ImportDefaultSpecifier"));
  10085. if (this.eat(12)) {
  10086. const hasStarImport = this.maybeParseStarImportSpecifier(node);
  10087. if (!hasStarImport) this.parseNamedImportSpecifiers(node);
  10088. }
  10089. this.expectContextual(98);
  10090. node.source = this.parseImportSource();
  10091. this.semicolon();
  10092. return this.finishNode(node, "ImportDeclaration");
  10093. }
  10094. parseImportSource() {
  10095. return this.parsePlaceholder("StringLiteral") || super.parseImportSource();
  10096. }
  10097. assertNoSpace() {
  10098. if (this.state.start > this.state.lastTokEndLoc.index) {
  10099. this.raise(PlaceholderErrors.UnexpectedSpace, this.state.lastTokEndLoc);
  10100. }
  10101. }
  10102. };
  10103. var v8intrinsic = superClass => class V8IntrinsicMixin extends superClass {
  10104. parseV8Intrinsic() {
  10105. if (this.match(54)) {
  10106. const v8IntrinsicStartLoc = this.state.startLoc;
  10107. const node = this.startNode();
  10108. this.next();
  10109. if (tokenIsIdentifier(this.state.type)) {
  10110. const name = this.parseIdentifierName();
  10111. const identifier = this.createIdentifier(node, name);
  10112. identifier.type = "V8IntrinsicIdentifier";
  10113. if (this.match(10)) {
  10114. return identifier;
  10115. }
  10116. }
  10117. this.unexpected(v8IntrinsicStartLoc);
  10118. }
  10119. }
  10120. parseExprAtom(refExpressionErrors) {
  10121. return this.parseV8Intrinsic() || super.parseExprAtom(refExpressionErrors);
  10122. }
  10123. };
  10124. const PIPELINE_PROPOSALS = ["minimal", "fsharp", "hack", "smart"];
  10125. const TOPIC_TOKENS = ["^^", "@@", "^", "%", "#"];
  10126. function validatePlugins(pluginsMap) {
  10127. if (pluginsMap.has("decorators")) {
  10128. if (pluginsMap.has("decorators-legacy")) {
  10129. throw new Error("Cannot use the decorators and decorators-legacy plugin together");
  10130. }
  10131. const decoratorsBeforeExport = pluginsMap.get("decorators").decoratorsBeforeExport;
  10132. if (decoratorsBeforeExport != null && typeof decoratorsBeforeExport !== "boolean") {
  10133. throw new Error("'decoratorsBeforeExport' must be a boolean, if specified.");
  10134. }
  10135. const allowCallParenthesized = pluginsMap.get("decorators").allowCallParenthesized;
  10136. if (allowCallParenthesized != null && typeof allowCallParenthesized !== "boolean") {
  10137. throw new Error("'allowCallParenthesized' must be a boolean.");
  10138. }
  10139. }
  10140. if (pluginsMap.has("flow") && pluginsMap.has("typescript")) {
  10141. throw new Error("Cannot combine flow and typescript plugins.");
  10142. }
  10143. if (pluginsMap.has("placeholders") && pluginsMap.has("v8intrinsic")) {
  10144. throw new Error("Cannot combine placeholders and v8intrinsic plugins.");
  10145. }
  10146. if (pluginsMap.has("pipelineOperator")) {
  10147. var _pluginsMap$get;
  10148. const proposal = pluginsMap.get("pipelineOperator").proposal;
  10149. if (!PIPELINE_PROPOSALS.includes(proposal)) {
  10150. const proposalList = PIPELINE_PROPOSALS.map(p => `"${p}"`).join(", ");
  10151. throw new Error(`"pipelineOperator" requires "proposal" option whose value must be one of: ${proposalList}.`);
  10152. }
  10153. const tupleSyntaxIsHash = ((_pluginsMap$get = pluginsMap.get("recordAndTuple")) == null ? void 0 : _pluginsMap$get.syntaxType) === "hash";
  10154. if (proposal === "hack") {
  10155. if (pluginsMap.has("placeholders")) {
  10156. throw new Error("Cannot combine placeholders plugin and Hack-style pipes.");
  10157. }
  10158. if (pluginsMap.has("v8intrinsic")) {
  10159. throw new Error("Cannot combine v8intrinsic plugin and Hack-style pipes.");
  10160. }
  10161. const topicToken = pluginsMap.get("pipelineOperator").topicToken;
  10162. if (!TOPIC_TOKENS.includes(topicToken)) {
  10163. const tokenList = TOPIC_TOKENS.map(t => `"${t}"`).join(", ");
  10164. throw new Error(`"pipelineOperator" in "proposal": "hack" mode also requires a "topicToken" option whose value must be one of: ${tokenList}.`);
  10165. }
  10166. if (topicToken === "#" && tupleSyntaxIsHash) {
  10167. throw new Error(`Plugin conflict between \`["pipelineOperator", { proposal: "hack", topicToken: "#" }]\` and \`${JSON.stringify(["recordAndTuple", pluginsMap.get("recordAndTuple")])}\`.`);
  10168. }
  10169. } else if (proposal === "smart" && tupleSyntaxIsHash) {
  10170. throw new Error(`Plugin conflict between \`["pipelineOperator", { proposal: "smart" }]\` and \`${JSON.stringify(["recordAndTuple", pluginsMap.get("recordAndTuple")])}\`.`);
  10171. }
  10172. }
  10173. if (pluginsMap.has("moduleAttributes")) {
  10174. {
  10175. if (pluginsMap.has("importAttributes") || pluginsMap.has("importAssertions")) {
  10176. throw new Error("Cannot combine importAssertions, importAttributes and moduleAttributes plugins.");
  10177. }
  10178. const moduleAttributesVersionPluginOption = pluginsMap.get("moduleAttributes").version;
  10179. if (moduleAttributesVersionPluginOption !== "may-2020") {
  10180. throw new Error("The 'moduleAttributes' plugin requires a 'version' option," + " representing the last proposal update. Currently, the" + " only supported value is 'may-2020'.");
  10181. }
  10182. }
  10183. }
  10184. if (pluginsMap.has("importAttributes") && pluginsMap.has("importAssertions")) {
  10185. throw new Error("Cannot combine importAssertions and importAttributes plugins.");
  10186. }
  10187. if (pluginsMap.has("recordAndTuple")) {
  10188. const syntaxType = pluginsMap.get("recordAndTuple").syntaxType;
  10189. if (syntaxType != null) {
  10190. {
  10191. const RECORD_AND_TUPLE_SYNTAX_TYPES = ["hash", "bar"];
  10192. if (!RECORD_AND_TUPLE_SYNTAX_TYPES.includes(syntaxType)) {
  10193. throw new Error("The 'syntaxType' option of the 'recordAndTuple' plugin must be one of: " + RECORD_AND_TUPLE_SYNTAX_TYPES.map(p => `'${p}'`).join(", "));
  10194. }
  10195. }
  10196. }
  10197. }
  10198. if (pluginsMap.has("asyncDoExpressions") && !pluginsMap.has("doExpressions")) {
  10199. const error = new Error("'asyncDoExpressions' requires 'doExpressions', please add 'doExpressions' to parser plugins.");
  10200. error.missingPlugins = "doExpressions";
  10201. throw error;
  10202. }
  10203. if (pluginsMap.has("optionalChainingAssign") && pluginsMap.get("optionalChainingAssign").version !== "2023-07") {
  10204. throw new Error("The 'optionalChainingAssign' plugin requires a 'version' option," + " representing the last proposal update. Currently, the" + " only supported value is '2023-07'.");
  10205. }
  10206. }
  10207. const mixinPlugins = {
  10208. estree,
  10209. jsx,
  10210. flow,
  10211. typescript,
  10212. v8intrinsic,
  10213. placeholders
  10214. };
  10215. const mixinPluginNames = Object.keys(mixinPlugins);
  10216. const defaultOptions = {
  10217. sourceType: "script",
  10218. sourceFilename: undefined,
  10219. startColumn: 0,
  10220. startLine: 1,
  10221. allowAwaitOutsideFunction: false,
  10222. allowReturnOutsideFunction: false,
  10223. allowNewTargetOutsideFunction: false,
  10224. allowImportExportEverywhere: false,
  10225. allowSuperOutsideMethod: false,
  10226. allowUndeclaredExports: false,
  10227. plugins: [],
  10228. strictMode: null,
  10229. ranges: false,
  10230. tokens: false,
  10231. createImportExpressions: false,
  10232. createParenthesizedExpressions: false,
  10233. errorRecovery: false,
  10234. attachComment: true,
  10235. annexB: true
  10236. };
  10237. function getOptions(opts) {
  10238. if (opts == null) {
  10239. return Object.assign({}, defaultOptions);
  10240. }
  10241. if (opts.annexB != null && opts.annexB !== false) {
  10242. throw new Error("The `annexB` option can only be set to `false`.");
  10243. }
  10244. const options = {};
  10245. for (const key of Object.keys(defaultOptions)) {
  10246. var _opts$key;
  10247. options[key] = (_opts$key = opts[key]) != null ? _opts$key : defaultOptions[key];
  10248. }
  10249. return options;
  10250. }
  10251. class ExpressionParser extends LValParser {
  10252. checkProto(prop, isRecord, protoRef, refExpressionErrors) {
  10253. if (prop.type === "SpreadElement" || this.isObjectMethod(prop) || prop.computed || prop.shorthand) {
  10254. return;
  10255. }
  10256. const key = prop.key;
  10257. const name = key.type === "Identifier" ? key.name : key.value;
  10258. if (name === "__proto__") {
  10259. if (isRecord) {
  10260. this.raise(Errors.RecordNoProto, key);
  10261. return;
  10262. }
  10263. if (protoRef.used) {
  10264. if (refExpressionErrors) {
  10265. if (refExpressionErrors.doubleProtoLoc === null) {
  10266. refExpressionErrors.doubleProtoLoc = key.loc.start;
  10267. }
  10268. } else {
  10269. this.raise(Errors.DuplicateProto, key);
  10270. }
  10271. }
  10272. protoRef.used = true;
  10273. }
  10274. }
  10275. shouldExitDescending(expr, potentialArrowAt) {
  10276. return expr.type === "ArrowFunctionExpression" && expr.start === potentialArrowAt;
  10277. }
  10278. getExpression() {
  10279. this.enterInitialScopes();
  10280. this.nextToken();
  10281. const expr = this.parseExpression();
  10282. if (!this.match(139)) {
  10283. this.unexpected();
  10284. }
  10285. this.finalizeRemainingComments();
  10286. expr.comments = this.comments;
  10287. expr.errors = this.state.errors;
  10288. if (this.options.tokens) {
  10289. expr.tokens = this.tokens;
  10290. }
  10291. return expr;
  10292. }
  10293. parseExpression(disallowIn, refExpressionErrors) {
  10294. if (disallowIn) {
  10295. return this.disallowInAnd(() => this.parseExpressionBase(refExpressionErrors));
  10296. }
  10297. return this.allowInAnd(() => this.parseExpressionBase(refExpressionErrors));
  10298. }
  10299. parseExpressionBase(refExpressionErrors) {
  10300. const startLoc = this.state.startLoc;
  10301. const expr = this.parseMaybeAssign(refExpressionErrors);
  10302. if (this.match(12)) {
  10303. const node = this.startNodeAt(startLoc);
  10304. node.expressions = [expr];
  10305. while (this.eat(12)) {
  10306. node.expressions.push(this.parseMaybeAssign(refExpressionErrors));
  10307. }
  10308. this.toReferencedList(node.expressions);
  10309. return this.finishNode(node, "SequenceExpression");
  10310. }
  10311. return expr;
  10312. }
  10313. parseMaybeAssignDisallowIn(refExpressionErrors, afterLeftParse) {
  10314. return this.disallowInAnd(() => this.parseMaybeAssign(refExpressionErrors, afterLeftParse));
  10315. }
  10316. parseMaybeAssignAllowIn(refExpressionErrors, afterLeftParse) {
  10317. return this.allowInAnd(() => this.parseMaybeAssign(refExpressionErrors, afterLeftParse));
  10318. }
  10319. setOptionalParametersError(refExpressionErrors, resultError) {
  10320. var _resultError$loc;
  10321. refExpressionErrors.optionalParametersLoc = (_resultError$loc = resultError == null ? void 0 : resultError.loc) != null ? _resultError$loc : this.state.startLoc;
  10322. }
  10323. parseMaybeAssign(refExpressionErrors, afterLeftParse) {
  10324. const startLoc = this.state.startLoc;
  10325. if (this.isContextual(108)) {
  10326. if (this.prodParam.hasYield) {
  10327. let left = this.parseYield();
  10328. if (afterLeftParse) {
  10329. left = afterLeftParse.call(this, left, startLoc);
  10330. }
  10331. return left;
  10332. }
  10333. }
  10334. let ownExpressionErrors;
  10335. if (refExpressionErrors) {
  10336. ownExpressionErrors = false;
  10337. } else {
  10338. refExpressionErrors = new ExpressionErrors();
  10339. ownExpressionErrors = true;
  10340. }
  10341. const {
  10342. type
  10343. } = this.state;
  10344. if (type === 10 || tokenIsIdentifier(type)) {
  10345. this.state.potentialArrowAt = this.state.start;
  10346. }
  10347. let left = this.parseMaybeConditional(refExpressionErrors);
  10348. if (afterLeftParse) {
  10349. left = afterLeftParse.call(this, left, startLoc);
  10350. }
  10351. if (tokenIsAssignment(this.state.type)) {
  10352. const node = this.startNodeAt(startLoc);
  10353. const operator = this.state.value;
  10354. node.operator = operator;
  10355. if (this.match(29)) {
  10356. this.toAssignable(left, true);
  10357. node.left = left;
  10358. const startIndex = startLoc.index;
  10359. if (refExpressionErrors.doubleProtoLoc != null && refExpressionErrors.doubleProtoLoc.index >= startIndex) {
  10360. refExpressionErrors.doubleProtoLoc = null;
  10361. }
  10362. if (refExpressionErrors.shorthandAssignLoc != null && refExpressionErrors.shorthandAssignLoc.index >= startIndex) {
  10363. refExpressionErrors.shorthandAssignLoc = null;
  10364. }
  10365. if (refExpressionErrors.privateKeyLoc != null && refExpressionErrors.privateKeyLoc.index >= startIndex) {
  10366. this.checkDestructuringPrivate(refExpressionErrors);
  10367. refExpressionErrors.privateKeyLoc = null;
  10368. }
  10369. } else {
  10370. node.left = left;
  10371. }
  10372. this.next();
  10373. node.right = this.parseMaybeAssign();
  10374. this.checkLVal(left, this.finishNode(node, "AssignmentExpression"));
  10375. return node;
  10376. } else if (ownExpressionErrors) {
  10377. this.checkExpressionErrors(refExpressionErrors, true);
  10378. }
  10379. return left;
  10380. }
  10381. parseMaybeConditional(refExpressionErrors) {
  10382. const startLoc = this.state.startLoc;
  10383. const potentialArrowAt = this.state.potentialArrowAt;
  10384. const expr = this.parseExprOps(refExpressionErrors);
  10385. if (this.shouldExitDescending(expr, potentialArrowAt)) {
  10386. return expr;
  10387. }
  10388. return this.parseConditional(expr, startLoc, refExpressionErrors);
  10389. }
  10390. parseConditional(expr, startLoc, refExpressionErrors) {
  10391. if (this.eat(17)) {
  10392. const node = this.startNodeAt(startLoc);
  10393. node.test = expr;
  10394. node.consequent = this.parseMaybeAssignAllowIn();
  10395. this.expect(14);
  10396. node.alternate = this.parseMaybeAssign();
  10397. return this.finishNode(node, "ConditionalExpression");
  10398. }
  10399. return expr;
  10400. }
  10401. parseMaybeUnaryOrPrivate(refExpressionErrors) {
  10402. return this.match(138) ? this.parsePrivateName() : this.parseMaybeUnary(refExpressionErrors);
  10403. }
  10404. parseExprOps(refExpressionErrors) {
  10405. const startLoc = this.state.startLoc;
  10406. const potentialArrowAt = this.state.potentialArrowAt;
  10407. const expr = this.parseMaybeUnaryOrPrivate(refExpressionErrors);
  10408. if (this.shouldExitDescending(expr, potentialArrowAt)) {
  10409. return expr;
  10410. }
  10411. return this.parseExprOp(expr, startLoc, -1);
  10412. }
  10413. parseExprOp(left, leftStartLoc, minPrec) {
  10414. if (this.isPrivateName(left)) {
  10415. const value = this.getPrivateNameSV(left);
  10416. if (minPrec >= tokenOperatorPrecedence(58) || !this.prodParam.hasIn || !this.match(58)) {
  10417. this.raise(Errors.PrivateInExpectedIn, left, {
  10418. identifierName: value
  10419. });
  10420. }
  10421. this.classScope.usePrivateName(value, left.loc.start);
  10422. }
  10423. const op = this.state.type;
  10424. if (tokenIsOperator(op) && (this.prodParam.hasIn || !this.match(58))) {
  10425. let prec = tokenOperatorPrecedence(op);
  10426. if (prec > minPrec) {
  10427. if (op === 39) {
  10428. this.expectPlugin("pipelineOperator");
  10429. if (this.state.inFSharpPipelineDirectBody) {
  10430. return left;
  10431. }
  10432. this.checkPipelineAtInfixOperator(left, leftStartLoc);
  10433. }
  10434. const node = this.startNodeAt(leftStartLoc);
  10435. node.left = left;
  10436. node.operator = this.state.value;
  10437. const logical = op === 41 || op === 42;
  10438. const coalesce = op === 40;
  10439. if (coalesce) {
  10440. prec = tokenOperatorPrecedence(42);
  10441. }
  10442. this.next();
  10443. if (op === 39 && this.hasPlugin(["pipelineOperator", {
  10444. proposal: "minimal"
  10445. }])) {
  10446. if (this.state.type === 96 && this.prodParam.hasAwait) {
  10447. throw this.raise(Errors.UnexpectedAwaitAfterPipelineBody, this.state.startLoc);
  10448. }
  10449. }
  10450. node.right = this.parseExprOpRightExpr(op, prec);
  10451. const finishedNode = this.finishNode(node, logical || coalesce ? "LogicalExpression" : "BinaryExpression");
  10452. const nextOp = this.state.type;
  10453. if (coalesce && (nextOp === 41 || nextOp === 42) || logical && nextOp === 40) {
  10454. throw this.raise(Errors.MixingCoalesceWithLogical, this.state.startLoc);
  10455. }
  10456. return this.parseExprOp(finishedNode, leftStartLoc, minPrec);
  10457. }
  10458. }
  10459. return left;
  10460. }
  10461. parseExprOpRightExpr(op, prec) {
  10462. const startLoc = this.state.startLoc;
  10463. switch (op) {
  10464. case 39:
  10465. switch (this.getPluginOption("pipelineOperator", "proposal")) {
  10466. case "hack":
  10467. return this.withTopicBindingContext(() => {
  10468. return this.parseHackPipeBody();
  10469. });
  10470. case "smart":
  10471. return this.withTopicBindingContext(() => {
  10472. if (this.prodParam.hasYield && this.isContextual(108)) {
  10473. throw this.raise(Errors.PipeBodyIsTighter, this.state.startLoc);
  10474. }
  10475. return this.parseSmartPipelineBodyInStyle(this.parseExprOpBaseRightExpr(op, prec), startLoc);
  10476. });
  10477. case "fsharp":
  10478. return this.withSoloAwaitPermittingContext(() => {
  10479. return this.parseFSharpPipelineBody(prec);
  10480. });
  10481. }
  10482. default:
  10483. return this.parseExprOpBaseRightExpr(op, prec);
  10484. }
  10485. }
  10486. parseExprOpBaseRightExpr(op, prec) {
  10487. const startLoc = this.state.startLoc;
  10488. return this.parseExprOp(this.parseMaybeUnaryOrPrivate(), startLoc, tokenIsRightAssociative(op) ? prec - 1 : prec);
  10489. }
  10490. parseHackPipeBody() {
  10491. var _body$extra;
  10492. const {
  10493. startLoc
  10494. } = this.state;
  10495. const body = this.parseMaybeAssign();
  10496. const requiredParentheses = UnparenthesizedPipeBodyDescriptions.has(body.type);
  10497. if (requiredParentheses && !((_body$extra = body.extra) != null && _body$extra.parenthesized)) {
  10498. this.raise(Errors.PipeUnparenthesizedBody, startLoc, {
  10499. type: body.type
  10500. });
  10501. }
  10502. if (!this.topicReferenceWasUsedInCurrentContext()) {
  10503. this.raise(Errors.PipeTopicUnused, startLoc);
  10504. }
  10505. return body;
  10506. }
  10507. checkExponentialAfterUnary(node) {
  10508. if (this.match(57)) {
  10509. this.raise(Errors.UnexpectedTokenUnaryExponentiation, node.argument);
  10510. }
  10511. }
  10512. parseMaybeUnary(refExpressionErrors, sawUnary) {
  10513. const startLoc = this.state.startLoc;
  10514. const isAwait = this.isContextual(96);
  10515. if (isAwait && this.recordAwaitIfAllowed()) {
  10516. this.next();
  10517. const expr = this.parseAwait(startLoc);
  10518. if (!sawUnary) this.checkExponentialAfterUnary(expr);
  10519. return expr;
  10520. }
  10521. const update = this.match(34);
  10522. const node = this.startNode();
  10523. if (tokenIsPrefix(this.state.type)) {
  10524. node.operator = this.state.value;
  10525. node.prefix = true;
  10526. if (this.match(72)) {
  10527. this.expectPlugin("throwExpressions");
  10528. }
  10529. const isDelete = this.match(89);
  10530. this.next();
  10531. node.argument = this.parseMaybeUnary(null, true);
  10532. this.checkExpressionErrors(refExpressionErrors, true);
  10533. if (this.state.strict && isDelete) {
  10534. const arg = node.argument;
  10535. if (arg.type === "Identifier") {
  10536. this.raise(Errors.StrictDelete, node);
  10537. } else if (this.hasPropertyAsPrivateName(arg)) {
  10538. this.raise(Errors.DeletePrivateField, node);
  10539. }
  10540. }
  10541. if (!update) {
  10542. if (!sawUnary) {
  10543. this.checkExponentialAfterUnary(node);
  10544. }
  10545. return this.finishNode(node, "UnaryExpression");
  10546. }
  10547. }
  10548. const expr = this.parseUpdate(node, update, refExpressionErrors);
  10549. if (isAwait) {
  10550. const {
  10551. type
  10552. } = this.state;
  10553. const startsExpr = this.hasPlugin("v8intrinsic") ? tokenCanStartExpression(type) : tokenCanStartExpression(type) && !this.match(54);
  10554. if (startsExpr && !this.isAmbiguousAwait()) {
  10555. this.raiseOverwrite(Errors.AwaitNotInAsyncContext, startLoc);
  10556. return this.parseAwait(startLoc);
  10557. }
  10558. }
  10559. return expr;
  10560. }
  10561. parseUpdate(node, update, refExpressionErrors) {
  10562. if (update) {
  10563. const updateExpressionNode = node;
  10564. this.checkLVal(updateExpressionNode.argument, this.finishNode(updateExpressionNode, "UpdateExpression"));
  10565. return node;
  10566. }
  10567. const startLoc = this.state.startLoc;
  10568. let expr = this.parseExprSubscripts(refExpressionErrors);
  10569. if (this.checkExpressionErrors(refExpressionErrors, false)) return expr;
  10570. while (tokenIsPostfix(this.state.type) && !this.canInsertSemicolon()) {
  10571. const node = this.startNodeAt(startLoc);
  10572. node.operator = this.state.value;
  10573. node.prefix = false;
  10574. node.argument = expr;
  10575. this.next();
  10576. this.checkLVal(expr, expr = this.finishNode(node, "UpdateExpression"));
  10577. }
  10578. return expr;
  10579. }
  10580. parseExprSubscripts(refExpressionErrors) {
  10581. const startLoc = this.state.startLoc;
  10582. const potentialArrowAt = this.state.potentialArrowAt;
  10583. const expr = this.parseExprAtom(refExpressionErrors);
  10584. if (this.shouldExitDescending(expr, potentialArrowAt)) {
  10585. return expr;
  10586. }
  10587. return this.parseSubscripts(expr, startLoc);
  10588. }
  10589. parseSubscripts(base, startLoc, noCalls) {
  10590. const state = {
  10591. optionalChainMember: false,
  10592. maybeAsyncArrow: this.atPossibleAsyncArrow(base),
  10593. stop: false
  10594. };
  10595. do {
  10596. base = this.parseSubscript(base, startLoc, noCalls, state);
  10597. state.maybeAsyncArrow = false;
  10598. } while (!state.stop);
  10599. return base;
  10600. }
  10601. parseSubscript(base, startLoc, noCalls, state) {
  10602. const {
  10603. type
  10604. } = this.state;
  10605. if (!noCalls && type === 15) {
  10606. return this.parseBind(base, startLoc, noCalls, state);
  10607. } else if (tokenIsTemplate(type)) {
  10608. return this.parseTaggedTemplateExpression(base, startLoc, state);
  10609. }
  10610. let optional = false;
  10611. if (type === 18) {
  10612. if (noCalls) {
  10613. this.raise(Errors.OptionalChainingNoNew, this.state.startLoc);
  10614. if (this.lookaheadCharCode() === 40) {
  10615. state.stop = true;
  10616. return base;
  10617. }
  10618. }
  10619. state.optionalChainMember = optional = true;
  10620. this.next();
  10621. }
  10622. if (!noCalls && this.match(10)) {
  10623. return this.parseCoverCallAndAsyncArrowHead(base, startLoc, state, optional);
  10624. } else {
  10625. const computed = this.eat(0);
  10626. if (computed || optional || this.eat(16)) {
  10627. return this.parseMember(base, startLoc, state, computed, optional);
  10628. } else {
  10629. state.stop = true;
  10630. return base;
  10631. }
  10632. }
  10633. }
  10634. parseMember(base, startLoc, state, computed, optional) {
  10635. const node = this.startNodeAt(startLoc);
  10636. node.object = base;
  10637. node.computed = computed;
  10638. if (computed) {
  10639. node.property = this.parseExpression();
  10640. this.expect(3);
  10641. } else if (this.match(138)) {
  10642. if (base.type === "Super") {
  10643. this.raise(Errors.SuperPrivateField, startLoc);
  10644. }
  10645. this.classScope.usePrivateName(this.state.value, this.state.startLoc);
  10646. node.property = this.parsePrivateName();
  10647. } else {
  10648. node.property = this.parseIdentifier(true);
  10649. }
  10650. if (state.optionalChainMember) {
  10651. node.optional = optional;
  10652. return this.finishNode(node, "OptionalMemberExpression");
  10653. } else {
  10654. return this.finishNode(node, "MemberExpression");
  10655. }
  10656. }
  10657. parseBind(base, startLoc, noCalls, state) {
  10658. const node = this.startNodeAt(startLoc);
  10659. node.object = base;
  10660. this.next();
  10661. node.callee = this.parseNoCallExpr();
  10662. state.stop = true;
  10663. return this.parseSubscripts(this.finishNode(node, "BindExpression"), startLoc, noCalls);
  10664. }
  10665. parseCoverCallAndAsyncArrowHead(base, startLoc, state, optional) {
  10666. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  10667. let refExpressionErrors = null;
  10668. this.state.maybeInArrowParameters = true;
  10669. this.next();
  10670. const node = this.startNodeAt(startLoc);
  10671. node.callee = base;
  10672. const {
  10673. maybeAsyncArrow,
  10674. optionalChainMember
  10675. } = state;
  10676. if (maybeAsyncArrow) {
  10677. this.expressionScope.enter(newAsyncArrowScope());
  10678. refExpressionErrors = new ExpressionErrors();
  10679. }
  10680. if (optionalChainMember) {
  10681. node.optional = optional;
  10682. }
  10683. if (optional) {
  10684. node.arguments = this.parseCallExpressionArguments(11);
  10685. } else {
  10686. node.arguments = this.parseCallExpressionArguments(11, base.type === "Import", base.type !== "Super", node, refExpressionErrors);
  10687. }
  10688. let finishedNode = this.finishCallExpression(node, optionalChainMember);
  10689. if (maybeAsyncArrow && this.shouldParseAsyncArrow() && !optional) {
  10690. state.stop = true;
  10691. this.checkDestructuringPrivate(refExpressionErrors);
  10692. this.expressionScope.validateAsPattern();
  10693. this.expressionScope.exit();
  10694. finishedNode = this.parseAsyncArrowFromCallExpression(this.startNodeAt(startLoc), finishedNode);
  10695. } else {
  10696. if (maybeAsyncArrow) {
  10697. this.checkExpressionErrors(refExpressionErrors, true);
  10698. this.expressionScope.exit();
  10699. }
  10700. this.toReferencedArguments(finishedNode);
  10701. }
  10702. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  10703. return finishedNode;
  10704. }
  10705. toReferencedArguments(node, isParenthesizedExpr) {
  10706. this.toReferencedListDeep(node.arguments, isParenthesizedExpr);
  10707. }
  10708. parseTaggedTemplateExpression(base, startLoc, state) {
  10709. const node = this.startNodeAt(startLoc);
  10710. node.tag = base;
  10711. node.quasi = this.parseTemplate(true);
  10712. if (state.optionalChainMember) {
  10713. this.raise(Errors.OptionalChainingNoTemplate, startLoc);
  10714. }
  10715. return this.finishNode(node, "TaggedTemplateExpression");
  10716. }
  10717. atPossibleAsyncArrow(base) {
  10718. return base.type === "Identifier" && base.name === "async" && this.state.lastTokEndLoc.index === base.end && !this.canInsertSemicolon() && base.end - base.start === 5 && base.start === this.state.potentialArrowAt;
  10719. }
  10720. expectImportAttributesPlugin() {
  10721. if (!this.hasPlugin("importAssertions")) {
  10722. this.expectPlugin("importAttributes");
  10723. }
  10724. }
  10725. finishCallExpression(node, optional) {
  10726. if (node.callee.type === "Import") {
  10727. if (node.arguments.length === 2) {
  10728. {
  10729. if (!this.hasPlugin("moduleAttributes")) {
  10730. this.expectImportAttributesPlugin();
  10731. }
  10732. }
  10733. }
  10734. if (node.arguments.length === 0 || node.arguments.length > 2) {
  10735. this.raise(Errors.ImportCallArity, node, {
  10736. maxArgumentCount: this.hasPlugin("importAttributes") || this.hasPlugin("importAssertions") || this.hasPlugin("moduleAttributes") ? 2 : 1
  10737. });
  10738. } else {
  10739. for (const arg of node.arguments) {
  10740. if (arg.type === "SpreadElement") {
  10741. this.raise(Errors.ImportCallSpreadArgument, arg);
  10742. }
  10743. }
  10744. }
  10745. }
  10746. return this.finishNode(node, optional ? "OptionalCallExpression" : "CallExpression");
  10747. }
  10748. parseCallExpressionArguments(close, dynamicImport, allowPlaceholder, nodeForExtra, refExpressionErrors) {
  10749. const elts = [];
  10750. let first = true;
  10751. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  10752. this.state.inFSharpPipelineDirectBody = false;
  10753. while (!this.eat(close)) {
  10754. if (first) {
  10755. first = false;
  10756. } else {
  10757. this.expect(12);
  10758. if (this.match(close)) {
  10759. if (dynamicImport && !this.hasPlugin("importAttributes") && !this.hasPlugin("importAssertions") && !this.hasPlugin("moduleAttributes")) {
  10760. this.raise(Errors.ImportCallArgumentTrailingComma, this.state.lastTokStartLoc);
  10761. }
  10762. if (nodeForExtra) {
  10763. this.addTrailingCommaExtraToNode(nodeForExtra);
  10764. }
  10765. this.next();
  10766. break;
  10767. }
  10768. }
  10769. elts.push(this.parseExprListItem(false, refExpressionErrors, allowPlaceholder));
  10770. }
  10771. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  10772. return elts;
  10773. }
  10774. shouldParseAsyncArrow() {
  10775. return this.match(19) && !this.canInsertSemicolon();
  10776. }
  10777. parseAsyncArrowFromCallExpression(node, call) {
  10778. var _call$extra;
  10779. this.resetPreviousNodeTrailingComments(call);
  10780. this.expect(19);
  10781. this.parseArrowExpression(node, call.arguments, true, (_call$extra = call.extra) == null ? void 0 : _call$extra.trailingCommaLoc);
  10782. if (call.innerComments) {
  10783. setInnerComments(node, call.innerComments);
  10784. }
  10785. if (call.callee.trailingComments) {
  10786. setInnerComments(node, call.callee.trailingComments);
  10787. }
  10788. return node;
  10789. }
  10790. parseNoCallExpr() {
  10791. const startLoc = this.state.startLoc;
  10792. return this.parseSubscripts(this.parseExprAtom(), startLoc, true);
  10793. }
  10794. parseExprAtom(refExpressionErrors) {
  10795. let node;
  10796. let decorators = null;
  10797. const {
  10798. type
  10799. } = this.state;
  10800. switch (type) {
  10801. case 79:
  10802. return this.parseSuper();
  10803. case 83:
  10804. node = this.startNode();
  10805. this.next();
  10806. if (this.match(16)) {
  10807. return this.parseImportMetaProperty(node);
  10808. }
  10809. if (this.match(10)) {
  10810. if (this.options.createImportExpressions) {
  10811. return this.parseImportCall(node);
  10812. } else {
  10813. return this.finishNode(node, "Import");
  10814. }
  10815. } else {
  10816. this.raise(Errors.UnsupportedImport, this.state.lastTokStartLoc);
  10817. return this.finishNode(node, "Import");
  10818. }
  10819. case 78:
  10820. node = this.startNode();
  10821. this.next();
  10822. return this.finishNode(node, "ThisExpression");
  10823. case 90:
  10824. {
  10825. return this.parseDo(this.startNode(), false);
  10826. }
  10827. case 56:
  10828. case 31:
  10829. {
  10830. this.readRegexp();
  10831. return this.parseRegExpLiteral(this.state.value);
  10832. }
  10833. case 134:
  10834. return this.parseNumericLiteral(this.state.value);
  10835. case 135:
  10836. return this.parseBigIntLiteral(this.state.value);
  10837. case 136:
  10838. return this.parseDecimalLiteral(this.state.value);
  10839. case 133:
  10840. return this.parseStringLiteral(this.state.value);
  10841. case 84:
  10842. return this.parseNullLiteral();
  10843. case 85:
  10844. return this.parseBooleanLiteral(true);
  10845. case 86:
  10846. return this.parseBooleanLiteral(false);
  10847. case 10:
  10848. {
  10849. const canBeArrow = this.state.potentialArrowAt === this.state.start;
  10850. return this.parseParenAndDistinguishExpression(canBeArrow);
  10851. }
  10852. case 2:
  10853. case 1:
  10854. {
  10855. return this.parseArrayLike(this.state.type === 2 ? 4 : 3, false, true);
  10856. }
  10857. case 0:
  10858. {
  10859. return this.parseArrayLike(3, true, false, refExpressionErrors);
  10860. }
  10861. case 6:
  10862. case 7:
  10863. {
  10864. return this.parseObjectLike(this.state.type === 6 ? 9 : 8, false, true);
  10865. }
  10866. case 5:
  10867. {
  10868. return this.parseObjectLike(8, false, false, refExpressionErrors);
  10869. }
  10870. case 68:
  10871. return this.parseFunctionOrFunctionSent();
  10872. case 26:
  10873. decorators = this.parseDecorators();
  10874. case 80:
  10875. return this.parseClass(this.maybeTakeDecorators(decorators, this.startNode()), false);
  10876. case 77:
  10877. return this.parseNewOrNewTarget();
  10878. case 25:
  10879. case 24:
  10880. return this.parseTemplate(false);
  10881. case 15:
  10882. {
  10883. node = this.startNode();
  10884. this.next();
  10885. node.object = null;
  10886. const callee = node.callee = this.parseNoCallExpr();
  10887. if (callee.type === "MemberExpression") {
  10888. return this.finishNode(node, "BindExpression");
  10889. } else {
  10890. throw this.raise(Errors.UnsupportedBind, callee);
  10891. }
  10892. }
  10893. case 138:
  10894. {
  10895. this.raise(Errors.PrivateInExpectedIn, this.state.startLoc, {
  10896. identifierName: this.state.value
  10897. });
  10898. return this.parsePrivateName();
  10899. }
  10900. case 33:
  10901. {
  10902. return this.parseTopicReferenceThenEqualsSign(54, "%");
  10903. }
  10904. case 32:
  10905. {
  10906. return this.parseTopicReferenceThenEqualsSign(44, "^");
  10907. }
  10908. case 37:
  10909. case 38:
  10910. {
  10911. return this.parseTopicReference("hack");
  10912. }
  10913. case 44:
  10914. case 54:
  10915. case 27:
  10916. {
  10917. const pipeProposal = this.getPluginOption("pipelineOperator", "proposal");
  10918. if (pipeProposal) {
  10919. return this.parseTopicReference(pipeProposal);
  10920. }
  10921. this.unexpected();
  10922. break;
  10923. }
  10924. case 47:
  10925. {
  10926. const lookaheadCh = this.input.codePointAt(this.nextTokenStart());
  10927. if (isIdentifierStart(lookaheadCh) || lookaheadCh === 62) {
  10928. this.expectOnePlugin(["jsx", "flow", "typescript"]);
  10929. } else {
  10930. this.unexpected();
  10931. }
  10932. break;
  10933. }
  10934. default:
  10935. if (tokenIsIdentifier(type)) {
  10936. if (this.isContextual(127) && this.lookaheadInLineCharCode() === 123) {
  10937. return this.parseModuleExpression();
  10938. }
  10939. const canBeArrow = this.state.potentialArrowAt === this.state.start;
  10940. const containsEsc = this.state.containsEsc;
  10941. const id = this.parseIdentifier();
  10942. if (!containsEsc && id.name === "async" && !this.canInsertSemicolon()) {
  10943. const {
  10944. type
  10945. } = this.state;
  10946. if (type === 68) {
  10947. this.resetPreviousNodeTrailingComments(id);
  10948. this.next();
  10949. return this.parseAsyncFunctionExpression(this.startNodeAtNode(id));
  10950. } else if (tokenIsIdentifier(type)) {
  10951. if (this.lookaheadCharCode() === 61) {
  10952. return this.parseAsyncArrowUnaryFunction(this.startNodeAtNode(id));
  10953. } else {
  10954. return id;
  10955. }
  10956. } else if (type === 90) {
  10957. this.resetPreviousNodeTrailingComments(id);
  10958. return this.parseDo(this.startNodeAtNode(id), true);
  10959. }
  10960. }
  10961. if (canBeArrow && this.match(19) && !this.canInsertSemicolon()) {
  10962. this.next();
  10963. return this.parseArrowExpression(this.startNodeAtNode(id), [id], false);
  10964. }
  10965. return id;
  10966. } else {
  10967. this.unexpected();
  10968. }
  10969. }
  10970. }
  10971. parseTopicReferenceThenEqualsSign(topicTokenType, topicTokenValue) {
  10972. const pipeProposal = this.getPluginOption("pipelineOperator", "proposal");
  10973. if (pipeProposal) {
  10974. this.state.type = topicTokenType;
  10975. this.state.value = topicTokenValue;
  10976. this.state.pos--;
  10977. this.state.end--;
  10978. this.state.endLoc = createPositionWithColumnOffset(this.state.endLoc, -1);
  10979. return this.parseTopicReference(pipeProposal);
  10980. } else {
  10981. this.unexpected();
  10982. }
  10983. }
  10984. parseTopicReference(pipeProposal) {
  10985. const node = this.startNode();
  10986. const startLoc = this.state.startLoc;
  10987. const tokenType = this.state.type;
  10988. this.next();
  10989. return this.finishTopicReference(node, startLoc, pipeProposal, tokenType);
  10990. }
  10991. finishTopicReference(node, startLoc, pipeProposal, tokenType) {
  10992. if (this.testTopicReferenceConfiguration(pipeProposal, startLoc, tokenType)) {
  10993. const nodeType = pipeProposal === "smart" ? "PipelinePrimaryTopicReference" : "TopicReference";
  10994. if (!this.topicReferenceIsAllowedInCurrentContext()) {
  10995. this.raise(pipeProposal === "smart" ? Errors.PrimaryTopicNotAllowed : Errors.PipeTopicUnbound, startLoc);
  10996. }
  10997. this.registerTopicReference();
  10998. return this.finishNode(node, nodeType);
  10999. } else {
  11000. throw this.raise(Errors.PipeTopicUnconfiguredToken, startLoc, {
  11001. token: tokenLabelName(tokenType)
  11002. });
  11003. }
  11004. }
  11005. testTopicReferenceConfiguration(pipeProposal, startLoc, tokenType) {
  11006. switch (pipeProposal) {
  11007. case "hack":
  11008. {
  11009. return this.hasPlugin(["pipelineOperator", {
  11010. topicToken: tokenLabelName(tokenType)
  11011. }]);
  11012. }
  11013. case "smart":
  11014. return tokenType === 27;
  11015. default:
  11016. throw this.raise(Errors.PipeTopicRequiresHackPipes, startLoc);
  11017. }
  11018. }
  11019. parseAsyncArrowUnaryFunction(node) {
  11020. this.prodParam.enter(functionFlags(true, this.prodParam.hasYield));
  11021. const params = [this.parseIdentifier()];
  11022. this.prodParam.exit();
  11023. if (this.hasPrecedingLineBreak()) {
  11024. this.raise(Errors.LineTerminatorBeforeArrow, this.state.curPosition());
  11025. }
  11026. this.expect(19);
  11027. return this.parseArrowExpression(node, params, true);
  11028. }
  11029. parseDo(node, isAsync) {
  11030. this.expectPlugin("doExpressions");
  11031. if (isAsync) {
  11032. this.expectPlugin("asyncDoExpressions");
  11033. }
  11034. node.async = isAsync;
  11035. this.next();
  11036. const oldLabels = this.state.labels;
  11037. this.state.labels = [];
  11038. if (isAsync) {
  11039. this.prodParam.enter(2);
  11040. node.body = this.parseBlock();
  11041. this.prodParam.exit();
  11042. } else {
  11043. node.body = this.parseBlock();
  11044. }
  11045. this.state.labels = oldLabels;
  11046. return this.finishNode(node, "DoExpression");
  11047. }
  11048. parseSuper() {
  11049. const node = this.startNode();
  11050. this.next();
  11051. if (this.match(10) && !this.scope.allowDirectSuper && !this.options.allowSuperOutsideMethod) {
  11052. this.raise(Errors.SuperNotAllowed, node);
  11053. } else if (!this.scope.allowSuper && !this.options.allowSuperOutsideMethod) {
  11054. this.raise(Errors.UnexpectedSuper, node);
  11055. }
  11056. if (!this.match(10) && !this.match(0) && !this.match(16)) {
  11057. this.raise(Errors.UnsupportedSuper, node);
  11058. }
  11059. return this.finishNode(node, "Super");
  11060. }
  11061. parsePrivateName() {
  11062. const node = this.startNode();
  11063. const id = this.startNodeAt(createPositionWithColumnOffset(this.state.startLoc, 1));
  11064. const name = this.state.value;
  11065. this.next();
  11066. node.id = this.createIdentifier(id, name);
  11067. return this.finishNode(node, "PrivateName");
  11068. }
  11069. parseFunctionOrFunctionSent() {
  11070. const node = this.startNode();
  11071. this.next();
  11072. if (this.prodParam.hasYield && this.match(16)) {
  11073. const meta = this.createIdentifier(this.startNodeAtNode(node), "function");
  11074. this.next();
  11075. if (this.match(103)) {
  11076. this.expectPlugin("functionSent");
  11077. } else if (!this.hasPlugin("functionSent")) {
  11078. this.unexpected();
  11079. }
  11080. return this.parseMetaProperty(node, meta, "sent");
  11081. }
  11082. return this.parseFunction(node);
  11083. }
  11084. parseMetaProperty(node, meta, propertyName) {
  11085. node.meta = meta;
  11086. const containsEsc = this.state.containsEsc;
  11087. node.property = this.parseIdentifier(true);
  11088. if (node.property.name !== propertyName || containsEsc) {
  11089. this.raise(Errors.UnsupportedMetaProperty, node.property, {
  11090. target: meta.name,
  11091. onlyValidPropertyName: propertyName
  11092. });
  11093. }
  11094. return this.finishNode(node, "MetaProperty");
  11095. }
  11096. parseImportMetaProperty(node) {
  11097. const id = this.createIdentifier(this.startNodeAtNode(node), "import");
  11098. this.next();
  11099. if (this.isContextual(101)) {
  11100. if (!this.inModule) {
  11101. this.raise(Errors.ImportMetaOutsideModule, id);
  11102. }
  11103. this.sawUnambiguousESM = true;
  11104. } else if (this.isContextual(105) || this.isContextual(97)) {
  11105. const isSource = this.isContextual(105);
  11106. if (!isSource) this.unexpected();
  11107. this.expectPlugin(isSource ? "sourcePhaseImports" : "deferredImportEvaluation");
  11108. if (!this.options.createImportExpressions) {
  11109. throw this.raise(Errors.DynamicImportPhaseRequiresImportExpressions, this.state.startLoc, {
  11110. phase: this.state.value
  11111. });
  11112. }
  11113. this.next();
  11114. node.phase = isSource ? "source" : "defer";
  11115. return this.parseImportCall(node);
  11116. }
  11117. return this.parseMetaProperty(node, id, "meta");
  11118. }
  11119. parseLiteralAtNode(value, type, node) {
  11120. this.addExtra(node, "rawValue", value);
  11121. this.addExtra(node, "raw", this.input.slice(node.start, this.state.end));
  11122. node.value = value;
  11123. this.next();
  11124. return this.finishNode(node, type);
  11125. }
  11126. parseLiteral(value, type) {
  11127. const node = this.startNode();
  11128. return this.parseLiteralAtNode(value, type, node);
  11129. }
  11130. parseStringLiteral(value) {
  11131. return this.parseLiteral(value, "StringLiteral");
  11132. }
  11133. parseNumericLiteral(value) {
  11134. return this.parseLiteral(value, "NumericLiteral");
  11135. }
  11136. parseBigIntLiteral(value) {
  11137. return this.parseLiteral(value, "BigIntLiteral");
  11138. }
  11139. parseDecimalLiteral(value) {
  11140. return this.parseLiteral(value, "DecimalLiteral");
  11141. }
  11142. parseRegExpLiteral(value) {
  11143. const node = this.startNode();
  11144. this.addExtra(node, "raw", this.input.slice(node.start, this.state.end));
  11145. node.pattern = value.pattern;
  11146. node.flags = value.flags;
  11147. this.next();
  11148. return this.finishNode(node, "RegExpLiteral");
  11149. }
  11150. parseBooleanLiteral(value) {
  11151. const node = this.startNode();
  11152. node.value = value;
  11153. this.next();
  11154. return this.finishNode(node, "BooleanLiteral");
  11155. }
  11156. parseNullLiteral() {
  11157. const node = this.startNode();
  11158. this.next();
  11159. return this.finishNode(node, "NullLiteral");
  11160. }
  11161. parseParenAndDistinguishExpression(canBeArrow) {
  11162. const startLoc = this.state.startLoc;
  11163. let val;
  11164. this.next();
  11165. this.expressionScope.enter(newArrowHeadScope());
  11166. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  11167. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  11168. this.state.maybeInArrowParameters = true;
  11169. this.state.inFSharpPipelineDirectBody = false;
  11170. const innerStartLoc = this.state.startLoc;
  11171. const exprList = [];
  11172. const refExpressionErrors = new ExpressionErrors();
  11173. let first = true;
  11174. let spreadStartLoc;
  11175. let optionalCommaStartLoc;
  11176. while (!this.match(11)) {
  11177. if (first) {
  11178. first = false;
  11179. } else {
  11180. this.expect(12, refExpressionErrors.optionalParametersLoc === null ? null : refExpressionErrors.optionalParametersLoc);
  11181. if (this.match(11)) {
  11182. optionalCommaStartLoc = this.state.startLoc;
  11183. break;
  11184. }
  11185. }
  11186. if (this.match(21)) {
  11187. const spreadNodeStartLoc = this.state.startLoc;
  11188. spreadStartLoc = this.state.startLoc;
  11189. exprList.push(this.parseParenItem(this.parseRestBinding(), spreadNodeStartLoc));
  11190. if (!this.checkCommaAfterRest(41)) {
  11191. break;
  11192. }
  11193. } else {
  11194. exprList.push(this.parseMaybeAssignAllowIn(refExpressionErrors, this.parseParenItem));
  11195. }
  11196. }
  11197. const innerEndLoc = this.state.lastTokEndLoc;
  11198. this.expect(11);
  11199. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  11200. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  11201. let arrowNode = this.startNodeAt(startLoc);
  11202. if (canBeArrow && this.shouldParseArrow(exprList) && (arrowNode = this.parseArrow(arrowNode))) {
  11203. this.checkDestructuringPrivate(refExpressionErrors);
  11204. this.expressionScope.validateAsPattern();
  11205. this.expressionScope.exit();
  11206. this.parseArrowExpression(arrowNode, exprList, false);
  11207. return arrowNode;
  11208. }
  11209. this.expressionScope.exit();
  11210. if (!exprList.length) {
  11211. this.unexpected(this.state.lastTokStartLoc);
  11212. }
  11213. if (optionalCommaStartLoc) this.unexpected(optionalCommaStartLoc);
  11214. if (spreadStartLoc) this.unexpected(spreadStartLoc);
  11215. this.checkExpressionErrors(refExpressionErrors, true);
  11216. this.toReferencedListDeep(exprList, true);
  11217. if (exprList.length > 1) {
  11218. val = this.startNodeAt(innerStartLoc);
  11219. val.expressions = exprList;
  11220. this.finishNode(val, "SequenceExpression");
  11221. this.resetEndLocation(val, innerEndLoc);
  11222. } else {
  11223. val = exprList[0];
  11224. }
  11225. return this.wrapParenthesis(startLoc, val);
  11226. }
  11227. wrapParenthesis(startLoc, expression) {
  11228. if (!this.options.createParenthesizedExpressions) {
  11229. this.addExtra(expression, "parenthesized", true);
  11230. this.addExtra(expression, "parenStart", startLoc.index);
  11231. this.takeSurroundingComments(expression, startLoc.index, this.state.lastTokEndLoc.index);
  11232. return expression;
  11233. }
  11234. const parenExpression = this.startNodeAt(startLoc);
  11235. parenExpression.expression = expression;
  11236. return this.finishNode(parenExpression, "ParenthesizedExpression");
  11237. }
  11238. shouldParseArrow(params) {
  11239. return !this.canInsertSemicolon();
  11240. }
  11241. parseArrow(node) {
  11242. if (this.eat(19)) {
  11243. return node;
  11244. }
  11245. }
  11246. parseParenItem(node, startLoc) {
  11247. return node;
  11248. }
  11249. parseNewOrNewTarget() {
  11250. const node = this.startNode();
  11251. this.next();
  11252. if (this.match(16)) {
  11253. const meta = this.createIdentifier(this.startNodeAtNode(node), "new");
  11254. this.next();
  11255. const metaProp = this.parseMetaProperty(node, meta, "target");
  11256. if (!this.scope.inNonArrowFunction && !this.scope.inClass && !this.options.allowNewTargetOutsideFunction) {
  11257. this.raise(Errors.UnexpectedNewTarget, metaProp);
  11258. }
  11259. return metaProp;
  11260. }
  11261. return this.parseNew(node);
  11262. }
  11263. parseNew(node) {
  11264. this.parseNewCallee(node);
  11265. if (this.eat(10)) {
  11266. const args = this.parseExprList(11);
  11267. this.toReferencedList(args);
  11268. node.arguments = args;
  11269. } else {
  11270. node.arguments = [];
  11271. }
  11272. return this.finishNode(node, "NewExpression");
  11273. }
  11274. parseNewCallee(node) {
  11275. const isImport = this.match(83);
  11276. const callee = this.parseNoCallExpr();
  11277. node.callee = callee;
  11278. if (isImport && (callee.type === "Import" || callee.type === "ImportExpression")) {
  11279. this.raise(Errors.ImportCallNotNewExpression, callee);
  11280. }
  11281. }
  11282. parseTemplateElement(isTagged) {
  11283. const {
  11284. start,
  11285. startLoc,
  11286. end,
  11287. value
  11288. } = this.state;
  11289. const elemStart = start + 1;
  11290. const elem = this.startNodeAt(createPositionWithColumnOffset(startLoc, 1));
  11291. if (value === null) {
  11292. if (!isTagged) {
  11293. this.raise(Errors.InvalidEscapeSequenceTemplate, createPositionWithColumnOffset(this.state.firstInvalidTemplateEscapePos, 1));
  11294. }
  11295. }
  11296. const isTail = this.match(24);
  11297. const endOffset = isTail ? -1 : -2;
  11298. const elemEnd = end + endOffset;
  11299. elem.value = {
  11300. raw: this.input.slice(elemStart, elemEnd).replace(/\r\n?/g, "\n"),
  11301. cooked: value === null ? null : value.slice(1, endOffset)
  11302. };
  11303. elem.tail = isTail;
  11304. this.next();
  11305. const finishedNode = this.finishNode(elem, "TemplateElement");
  11306. this.resetEndLocation(finishedNode, createPositionWithColumnOffset(this.state.lastTokEndLoc, endOffset));
  11307. return finishedNode;
  11308. }
  11309. parseTemplate(isTagged) {
  11310. const node = this.startNode();
  11311. let curElt = this.parseTemplateElement(isTagged);
  11312. const quasis = [curElt];
  11313. const substitutions = [];
  11314. while (!curElt.tail) {
  11315. substitutions.push(this.parseTemplateSubstitution());
  11316. this.readTemplateContinuation();
  11317. quasis.push(curElt = this.parseTemplateElement(isTagged));
  11318. }
  11319. node.expressions = substitutions;
  11320. node.quasis = quasis;
  11321. return this.finishNode(node, "TemplateLiteral");
  11322. }
  11323. parseTemplateSubstitution() {
  11324. return this.parseExpression();
  11325. }
  11326. parseObjectLike(close, isPattern, isRecord, refExpressionErrors) {
  11327. if (isRecord) {
  11328. this.expectPlugin("recordAndTuple");
  11329. }
  11330. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  11331. this.state.inFSharpPipelineDirectBody = false;
  11332. const propHash = Object.create(null);
  11333. let first = true;
  11334. const node = this.startNode();
  11335. node.properties = [];
  11336. this.next();
  11337. while (!this.match(close)) {
  11338. if (first) {
  11339. first = false;
  11340. } else {
  11341. this.expect(12);
  11342. if (this.match(close)) {
  11343. this.addTrailingCommaExtraToNode(node);
  11344. break;
  11345. }
  11346. }
  11347. let prop;
  11348. if (isPattern) {
  11349. prop = this.parseBindingProperty();
  11350. } else {
  11351. prop = this.parsePropertyDefinition(refExpressionErrors);
  11352. this.checkProto(prop, isRecord, propHash, refExpressionErrors);
  11353. }
  11354. if (isRecord && !this.isObjectProperty(prop) && prop.type !== "SpreadElement") {
  11355. this.raise(Errors.InvalidRecordProperty, prop);
  11356. }
  11357. {
  11358. if (prop.shorthand) {
  11359. this.addExtra(prop, "shorthand", true);
  11360. }
  11361. }
  11362. node.properties.push(prop);
  11363. }
  11364. this.next();
  11365. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  11366. let type = "ObjectExpression";
  11367. if (isPattern) {
  11368. type = "ObjectPattern";
  11369. } else if (isRecord) {
  11370. type = "RecordExpression";
  11371. }
  11372. return this.finishNode(node, type);
  11373. }
  11374. addTrailingCommaExtraToNode(node) {
  11375. this.addExtra(node, "trailingComma", this.state.lastTokStartLoc.index);
  11376. this.addExtra(node, "trailingCommaLoc", this.state.lastTokStartLoc, false);
  11377. }
  11378. maybeAsyncOrAccessorProp(prop) {
  11379. return !prop.computed && prop.key.type === "Identifier" && (this.isLiteralPropertyName() || this.match(0) || this.match(55));
  11380. }
  11381. parsePropertyDefinition(refExpressionErrors) {
  11382. let decorators = [];
  11383. if (this.match(26)) {
  11384. if (this.hasPlugin("decorators")) {
  11385. this.raise(Errors.UnsupportedPropertyDecorator, this.state.startLoc);
  11386. }
  11387. while (this.match(26)) {
  11388. decorators.push(this.parseDecorator());
  11389. }
  11390. }
  11391. const prop = this.startNode();
  11392. let isAsync = false;
  11393. let isAccessor = false;
  11394. let startLoc;
  11395. if (this.match(21)) {
  11396. if (decorators.length) this.unexpected();
  11397. return this.parseSpread();
  11398. }
  11399. if (decorators.length) {
  11400. prop.decorators = decorators;
  11401. decorators = [];
  11402. }
  11403. prop.method = false;
  11404. if (refExpressionErrors) {
  11405. startLoc = this.state.startLoc;
  11406. }
  11407. let isGenerator = this.eat(55);
  11408. this.parsePropertyNamePrefixOperator(prop);
  11409. const containsEsc = this.state.containsEsc;
  11410. this.parsePropertyName(prop, refExpressionErrors);
  11411. if (!isGenerator && !containsEsc && this.maybeAsyncOrAccessorProp(prop)) {
  11412. const {
  11413. key
  11414. } = prop;
  11415. const keyName = key.name;
  11416. if (keyName === "async" && !this.hasPrecedingLineBreak()) {
  11417. isAsync = true;
  11418. this.resetPreviousNodeTrailingComments(key);
  11419. isGenerator = this.eat(55);
  11420. this.parsePropertyName(prop);
  11421. }
  11422. if (keyName === "get" || keyName === "set") {
  11423. isAccessor = true;
  11424. this.resetPreviousNodeTrailingComments(key);
  11425. prop.kind = keyName;
  11426. if (this.match(55)) {
  11427. isGenerator = true;
  11428. this.raise(Errors.AccessorIsGenerator, this.state.curPosition(), {
  11429. kind: keyName
  11430. });
  11431. this.next();
  11432. }
  11433. this.parsePropertyName(prop);
  11434. }
  11435. }
  11436. return this.parseObjPropValue(prop, startLoc, isGenerator, isAsync, false, isAccessor, refExpressionErrors);
  11437. }
  11438. getGetterSetterExpectedParamCount(method) {
  11439. return method.kind === "get" ? 0 : 1;
  11440. }
  11441. getObjectOrClassMethodParams(method) {
  11442. return method.params;
  11443. }
  11444. checkGetterSetterParams(method) {
  11445. var _params;
  11446. const paramCount = this.getGetterSetterExpectedParamCount(method);
  11447. const params = this.getObjectOrClassMethodParams(method);
  11448. if (params.length !== paramCount) {
  11449. this.raise(method.kind === "get" ? Errors.BadGetterArity : Errors.BadSetterArity, method);
  11450. }
  11451. if (method.kind === "set" && ((_params = params[params.length - 1]) == null ? void 0 : _params.type) === "RestElement") {
  11452. this.raise(Errors.BadSetterRestParameter, method);
  11453. }
  11454. }
  11455. parseObjectMethod(prop, isGenerator, isAsync, isPattern, isAccessor) {
  11456. if (isAccessor) {
  11457. const finishedProp = this.parseMethod(prop, isGenerator, false, false, false, "ObjectMethod");
  11458. this.checkGetterSetterParams(finishedProp);
  11459. return finishedProp;
  11460. }
  11461. if (isAsync || isGenerator || this.match(10)) {
  11462. if (isPattern) this.unexpected();
  11463. prop.kind = "method";
  11464. prop.method = true;
  11465. return this.parseMethod(prop, isGenerator, isAsync, false, false, "ObjectMethod");
  11466. }
  11467. }
  11468. parseObjectProperty(prop, startLoc, isPattern, refExpressionErrors) {
  11469. prop.shorthand = false;
  11470. if (this.eat(14)) {
  11471. prop.value = isPattern ? this.parseMaybeDefault(this.state.startLoc) : this.parseMaybeAssignAllowIn(refExpressionErrors);
  11472. return this.finishNode(prop, "ObjectProperty");
  11473. }
  11474. if (!prop.computed && prop.key.type === "Identifier") {
  11475. this.checkReservedWord(prop.key.name, prop.key.loc.start, true, false);
  11476. if (isPattern) {
  11477. prop.value = this.parseMaybeDefault(startLoc, cloneIdentifier(prop.key));
  11478. } else if (this.match(29)) {
  11479. const shorthandAssignLoc = this.state.startLoc;
  11480. if (refExpressionErrors != null) {
  11481. if (refExpressionErrors.shorthandAssignLoc === null) {
  11482. refExpressionErrors.shorthandAssignLoc = shorthandAssignLoc;
  11483. }
  11484. } else {
  11485. this.raise(Errors.InvalidCoverInitializedName, shorthandAssignLoc);
  11486. }
  11487. prop.value = this.parseMaybeDefault(startLoc, cloneIdentifier(prop.key));
  11488. } else {
  11489. prop.value = cloneIdentifier(prop.key);
  11490. }
  11491. prop.shorthand = true;
  11492. return this.finishNode(prop, "ObjectProperty");
  11493. }
  11494. }
  11495. parseObjPropValue(prop, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors) {
  11496. const node = this.parseObjectMethod(prop, isGenerator, isAsync, isPattern, isAccessor) || this.parseObjectProperty(prop, startLoc, isPattern, refExpressionErrors);
  11497. if (!node) this.unexpected();
  11498. return node;
  11499. }
  11500. parsePropertyName(prop, refExpressionErrors) {
  11501. if (this.eat(0)) {
  11502. prop.computed = true;
  11503. prop.key = this.parseMaybeAssignAllowIn();
  11504. this.expect(3);
  11505. } else {
  11506. const {
  11507. type,
  11508. value
  11509. } = this.state;
  11510. let key;
  11511. if (tokenIsKeywordOrIdentifier(type)) {
  11512. key = this.parseIdentifier(true);
  11513. } else {
  11514. switch (type) {
  11515. case 134:
  11516. key = this.parseNumericLiteral(value);
  11517. break;
  11518. case 133:
  11519. key = this.parseStringLiteral(value);
  11520. break;
  11521. case 135:
  11522. key = this.parseBigIntLiteral(value);
  11523. break;
  11524. case 136:
  11525. key = this.parseDecimalLiteral(value);
  11526. break;
  11527. case 138:
  11528. {
  11529. const privateKeyLoc = this.state.startLoc;
  11530. if (refExpressionErrors != null) {
  11531. if (refExpressionErrors.privateKeyLoc === null) {
  11532. refExpressionErrors.privateKeyLoc = privateKeyLoc;
  11533. }
  11534. } else {
  11535. this.raise(Errors.UnexpectedPrivateField, privateKeyLoc);
  11536. }
  11537. key = this.parsePrivateName();
  11538. break;
  11539. }
  11540. default:
  11541. this.unexpected();
  11542. }
  11543. }
  11544. prop.key = key;
  11545. if (type !== 138) {
  11546. prop.computed = false;
  11547. }
  11548. }
  11549. }
  11550. initFunction(node, isAsync) {
  11551. node.id = null;
  11552. node.generator = false;
  11553. node.async = isAsync;
  11554. }
  11555. parseMethod(node, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope = false) {
  11556. this.initFunction(node, isAsync);
  11557. node.generator = isGenerator;
  11558. this.scope.enter(2 | 16 | (inClassScope ? 64 : 0) | (allowDirectSuper ? 32 : 0));
  11559. this.prodParam.enter(functionFlags(isAsync, node.generator));
  11560. this.parseFunctionParams(node, isConstructor);
  11561. const finishedNode = this.parseFunctionBodyAndFinish(node, type, true);
  11562. this.prodParam.exit();
  11563. this.scope.exit();
  11564. return finishedNode;
  11565. }
  11566. parseArrayLike(close, canBePattern, isTuple, refExpressionErrors) {
  11567. if (isTuple) {
  11568. this.expectPlugin("recordAndTuple");
  11569. }
  11570. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  11571. this.state.inFSharpPipelineDirectBody = false;
  11572. const node = this.startNode();
  11573. this.next();
  11574. node.elements = this.parseExprList(close, !isTuple, refExpressionErrors, node);
  11575. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  11576. return this.finishNode(node, isTuple ? "TupleExpression" : "ArrayExpression");
  11577. }
  11578. parseArrowExpression(node, params, isAsync, trailingCommaLoc) {
  11579. this.scope.enter(2 | 4);
  11580. let flags = functionFlags(isAsync, false);
  11581. if (!this.match(5) && this.prodParam.hasIn) {
  11582. flags |= 8;
  11583. }
  11584. this.prodParam.enter(flags);
  11585. this.initFunction(node, isAsync);
  11586. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  11587. if (params) {
  11588. this.state.maybeInArrowParameters = true;
  11589. this.setArrowFunctionParameters(node, params, trailingCommaLoc);
  11590. }
  11591. this.state.maybeInArrowParameters = false;
  11592. this.parseFunctionBody(node, true);
  11593. this.prodParam.exit();
  11594. this.scope.exit();
  11595. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  11596. return this.finishNode(node, "ArrowFunctionExpression");
  11597. }
  11598. setArrowFunctionParameters(node, params, trailingCommaLoc) {
  11599. this.toAssignableList(params, trailingCommaLoc, false);
  11600. node.params = params;
  11601. }
  11602. parseFunctionBodyAndFinish(node, type, isMethod = false) {
  11603. this.parseFunctionBody(node, false, isMethod);
  11604. return this.finishNode(node, type);
  11605. }
  11606. parseFunctionBody(node, allowExpression, isMethod = false) {
  11607. const isExpression = allowExpression && !this.match(5);
  11608. this.expressionScope.enter(newExpressionScope());
  11609. if (isExpression) {
  11610. node.body = this.parseMaybeAssign();
  11611. this.checkParams(node, false, allowExpression, false);
  11612. } else {
  11613. const oldStrict = this.state.strict;
  11614. const oldLabels = this.state.labels;
  11615. this.state.labels = [];
  11616. this.prodParam.enter(this.prodParam.currentFlags() | 4);
  11617. node.body = this.parseBlock(true, false, hasStrictModeDirective => {
  11618. const nonSimple = !this.isSimpleParamList(node.params);
  11619. if (hasStrictModeDirective && nonSimple) {
  11620. this.raise(Errors.IllegalLanguageModeDirective, (node.kind === "method" || node.kind === "constructor") && !!node.key ? node.key.loc.end : node);
  11621. }
  11622. const strictModeChanged = !oldStrict && this.state.strict;
  11623. this.checkParams(node, !this.state.strict && !allowExpression && !isMethod && !nonSimple, allowExpression, strictModeChanged);
  11624. if (this.state.strict && node.id) {
  11625. this.checkIdentifier(node.id, 65, strictModeChanged);
  11626. }
  11627. });
  11628. this.prodParam.exit();
  11629. this.state.labels = oldLabels;
  11630. }
  11631. this.expressionScope.exit();
  11632. }
  11633. isSimpleParameter(node) {
  11634. return node.type === "Identifier";
  11635. }
  11636. isSimpleParamList(params) {
  11637. for (let i = 0, len = params.length; i < len; i++) {
  11638. if (!this.isSimpleParameter(params[i])) return false;
  11639. }
  11640. return true;
  11641. }
  11642. checkParams(node, allowDuplicates, isArrowFunction, strictModeChanged = true) {
  11643. const checkClashes = !allowDuplicates && new Set();
  11644. const formalParameters = {
  11645. type: "FormalParameters"
  11646. };
  11647. for (const param of node.params) {
  11648. this.checkLVal(param, formalParameters, 5, checkClashes, strictModeChanged);
  11649. }
  11650. }
  11651. parseExprList(close, allowEmpty, refExpressionErrors, nodeForExtra) {
  11652. const elts = [];
  11653. let first = true;
  11654. while (!this.eat(close)) {
  11655. if (first) {
  11656. first = false;
  11657. } else {
  11658. this.expect(12);
  11659. if (this.match(close)) {
  11660. if (nodeForExtra) {
  11661. this.addTrailingCommaExtraToNode(nodeForExtra);
  11662. }
  11663. this.next();
  11664. break;
  11665. }
  11666. }
  11667. elts.push(this.parseExprListItem(allowEmpty, refExpressionErrors));
  11668. }
  11669. return elts;
  11670. }
  11671. parseExprListItem(allowEmpty, refExpressionErrors, allowPlaceholder) {
  11672. let elt;
  11673. if (this.match(12)) {
  11674. if (!allowEmpty) {
  11675. this.raise(Errors.UnexpectedToken, this.state.curPosition(), {
  11676. unexpected: ","
  11677. });
  11678. }
  11679. elt = null;
  11680. } else if (this.match(21)) {
  11681. const spreadNodeStartLoc = this.state.startLoc;
  11682. elt = this.parseParenItem(this.parseSpread(refExpressionErrors), spreadNodeStartLoc);
  11683. } else if (this.match(17)) {
  11684. this.expectPlugin("partialApplication");
  11685. if (!allowPlaceholder) {
  11686. this.raise(Errors.UnexpectedArgumentPlaceholder, this.state.startLoc);
  11687. }
  11688. const node = this.startNode();
  11689. this.next();
  11690. elt = this.finishNode(node, "ArgumentPlaceholder");
  11691. } else {
  11692. elt = this.parseMaybeAssignAllowIn(refExpressionErrors, this.parseParenItem);
  11693. }
  11694. return elt;
  11695. }
  11696. parseIdentifier(liberal) {
  11697. const node = this.startNode();
  11698. const name = this.parseIdentifierName(liberal);
  11699. return this.createIdentifier(node, name);
  11700. }
  11701. createIdentifier(node, name) {
  11702. node.name = name;
  11703. node.loc.identifierName = name;
  11704. return this.finishNode(node, "Identifier");
  11705. }
  11706. parseIdentifierName(liberal) {
  11707. let name;
  11708. const {
  11709. startLoc,
  11710. type
  11711. } = this.state;
  11712. if (tokenIsKeywordOrIdentifier(type)) {
  11713. name = this.state.value;
  11714. } else {
  11715. this.unexpected();
  11716. }
  11717. const tokenIsKeyword = tokenKeywordOrIdentifierIsKeyword(type);
  11718. if (liberal) {
  11719. if (tokenIsKeyword) {
  11720. this.replaceToken(132);
  11721. }
  11722. } else {
  11723. this.checkReservedWord(name, startLoc, tokenIsKeyword, false);
  11724. }
  11725. this.next();
  11726. return name;
  11727. }
  11728. checkReservedWord(word, startLoc, checkKeywords, isBinding) {
  11729. if (word.length > 10) {
  11730. return;
  11731. }
  11732. if (!canBeReservedWord(word)) {
  11733. return;
  11734. }
  11735. if (checkKeywords && isKeyword(word)) {
  11736. this.raise(Errors.UnexpectedKeyword, startLoc, {
  11737. keyword: word
  11738. });
  11739. return;
  11740. }
  11741. const reservedTest = !this.state.strict ? isReservedWord : isBinding ? isStrictBindReservedWord : isStrictReservedWord;
  11742. if (reservedTest(word, this.inModule)) {
  11743. this.raise(Errors.UnexpectedReservedWord, startLoc, {
  11744. reservedWord: word
  11745. });
  11746. return;
  11747. } else if (word === "yield") {
  11748. if (this.prodParam.hasYield) {
  11749. this.raise(Errors.YieldBindingIdentifier, startLoc);
  11750. return;
  11751. }
  11752. } else if (word === "await") {
  11753. if (this.prodParam.hasAwait) {
  11754. this.raise(Errors.AwaitBindingIdentifier, startLoc);
  11755. return;
  11756. }
  11757. if (this.scope.inStaticBlock) {
  11758. this.raise(Errors.AwaitBindingIdentifierInStaticBlock, startLoc);
  11759. return;
  11760. }
  11761. this.expressionScope.recordAsyncArrowParametersError(startLoc);
  11762. } else if (word === "arguments") {
  11763. if (this.scope.inClassAndNotInNonArrowFunction) {
  11764. this.raise(Errors.ArgumentsInClass, startLoc);
  11765. return;
  11766. }
  11767. }
  11768. }
  11769. recordAwaitIfAllowed() {
  11770. const isAwaitAllowed = this.prodParam.hasAwait || this.options.allowAwaitOutsideFunction && !this.scope.inFunction;
  11771. if (isAwaitAllowed && !this.scope.inFunction) {
  11772. this.state.hasTopLevelAwait = true;
  11773. }
  11774. return isAwaitAllowed;
  11775. }
  11776. parseAwait(startLoc) {
  11777. const node = this.startNodeAt(startLoc);
  11778. this.expressionScope.recordParameterInitializerError(Errors.AwaitExpressionFormalParameter, node);
  11779. if (this.eat(55)) {
  11780. this.raise(Errors.ObsoleteAwaitStar, node);
  11781. }
  11782. if (!this.scope.inFunction && !this.options.allowAwaitOutsideFunction) {
  11783. if (this.isAmbiguousAwait()) {
  11784. this.ambiguousScriptDifferentAst = true;
  11785. } else {
  11786. this.sawUnambiguousESM = true;
  11787. }
  11788. }
  11789. if (!this.state.soloAwait) {
  11790. node.argument = this.parseMaybeUnary(null, true);
  11791. }
  11792. return this.finishNode(node, "AwaitExpression");
  11793. }
  11794. isAmbiguousAwait() {
  11795. if (this.hasPrecedingLineBreak()) return true;
  11796. const {
  11797. type
  11798. } = this.state;
  11799. return type === 53 || type === 10 || type === 0 || tokenIsTemplate(type) || type === 102 && !this.state.containsEsc || type === 137 || type === 56 || this.hasPlugin("v8intrinsic") && type === 54;
  11800. }
  11801. parseYield() {
  11802. const node = this.startNode();
  11803. this.expressionScope.recordParameterInitializerError(Errors.YieldInParameter, node);
  11804. this.next();
  11805. let delegating = false;
  11806. let argument = null;
  11807. if (!this.hasPrecedingLineBreak()) {
  11808. delegating = this.eat(55);
  11809. switch (this.state.type) {
  11810. case 13:
  11811. case 139:
  11812. case 8:
  11813. case 11:
  11814. case 3:
  11815. case 9:
  11816. case 14:
  11817. case 12:
  11818. if (!delegating) break;
  11819. default:
  11820. argument = this.parseMaybeAssign();
  11821. }
  11822. }
  11823. node.delegate = delegating;
  11824. node.argument = argument;
  11825. return this.finishNode(node, "YieldExpression");
  11826. }
  11827. parseImportCall(node) {
  11828. this.next();
  11829. node.source = this.parseMaybeAssignAllowIn();
  11830. if (this.hasPlugin("importAttributes") || this.hasPlugin("importAssertions")) {
  11831. node.options = null;
  11832. }
  11833. if (this.eat(12)) {
  11834. this.expectImportAttributesPlugin();
  11835. if (!this.match(11)) {
  11836. node.options = this.parseMaybeAssignAllowIn();
  11837. this.eat(12);
  11838. }
  11839. }
  11840. this.expect(11);
  11841. return this.finishNode(node, "ImportExpression");
  11842. }
  11843. checkPipelineAtInfixOperator(left, leftStartLoc) {
  11844. if (this.hasPlugin(["pipelineOperator", {
  11845. proposal: "smart"
  11846. }])) {
  11847. if (left.type === "SequenceExpression") {
  11848. this.raise(Errors.PipelineHeadSequenceExpression, leftStartLoc);
  11849. }
  11850. }
  11851. }
  11852. parseSmartPipelineBodyInStyle(childExpr, startLoc) {
  11853. if (this.isSimpleReference(childExpr)) {
  11854. const bodyNode = this.startNodeAt(startLoc);
  11855. bodyNode.callee = childExpr;
  11856. return this.finishNode(bodyNode, "PipelineBareFunction");
  11857. } else {
  11858. const bodyNode = this.startNodeAt(startLoc);
  11859. this.checkSmartPipeTopicBodyEarlyErrors(startLoc);
  11860. bodyNode.expression = childExpr;
  11861. return this.finishNode(bodyNode, "PipelineTopicExpression");
  11862. }
  11863. }
  11864. isSimpleReference(expression) {
  11865. switch (expression.type) {
  11866. case "MemberExpression":
  11867. return !expression.computed && this.isSimpleReference(expression.object);
  11868. case "Identifier":
  11869. return true;
  11870. default:
  11871. return false;
  11872. }
  11873. }
  11874. checkSmartPipeTopicBodyEarlyErrors(startLoc) {
  11875. if (this.match(19)) {
  11876. throw this.raise(Errors.PipelineBodyNoArrow, this.state.startLoc);
  11877. }
  11878. if (!this.topicReferenceWasUsedInCurrentContext()) {
  11879. this.raise(Errors.PipelineTopicUnused, startLoc);
  11880. }
  11881. }
  11882. withTopicBindingContext(callback) {
  11883. const outerContextTopicState = this.state.topicContext;
  11884. this.state.topicContext = {
  11885. maxNumOfResolvableTopics: 1,
  11886. maxTopicIndex: null
  11887. };
  11888. try {
  11889. return callback();
  11890. } finally {
  11891. this.state.topicContext = outerContextTopicState;
  11892. }
  11893. }
  11894. withSmartMixTopicForbiddingContext(callback) {
  11895. if (this.hasPlugin(["pipelineOperator", {
  11896. proposal: "smart"
  11897. }])) {
  11898. const outerContextTopicState = this.state.topicContext;
  11899. this.state.topicContext = {
  11900. maxNumOfResolvableTopics: 0,
  11901. maxTopicIndex: null
  11902. };
  11903. try {
  11904. return callback();
  11905. } finally {
  11906. this.state.topicContext = outerContextTopicState;
  11907. }
  11908. } else {
  11909. return callback();
  11910. }
  11911. }
  11912. withSoloAwaitPermittingContext(callback) {
  11913. const outerContextSoloAwaitState = this.state.soloAwait;
  11914. this.state.soloAwait = true;
  11915. try {
  11916. return callback();
  11917. } finally {
  11918. this.state.soloAwait = outerContextSoloAwaitState;
  11919. }
  11920. }
  11921. allowInAnd(callback) {
  11922. const flags = this.prodParam.currentFlags();
  11923. const prodParamToSet = 8 & ~flags;
  11924. if (prodParamToSet) {
  11925. this.prodParam.enter(flags | 8);
  11926. try {
  11927. return callback();
  11928. } finally {
  11929. this.prodParam.exit();
  11930. }
  11931. }
  11932. return callback();
  11933. }
  11934. disallowInAnd(callback) {
  11935. const flags = this.prodParam.currentFlags();
  11936. const prodParamToClear = 8 & flags;
  11937. if (prodParamToClear) {
  11938. this.prodParam.enter(flags & ~8);
  11939. try {
  11940. return callback();
  11941. } finally {
  11942. this.prodParam.exit();
  11943. }
  11944. }
  11945. return callback();
  11946. }
  11947. registerTopicReference() {
  11948. this.state.topicContext.maxTopicIndex = 0;
  11949. }
  11950. topicReferenceIsAllowedInCurrentContext() {
  11951. return this.state.topicContext.maxNumOfResolvableTopics >= 1;
  11952. }
  11953. topicReferenceWasUsedInCurrentContext() {
  11954. return this.state.topicContext.maxTopicIndex != null && this.state.topicContext.maxTopicIndex >= 0;
  11955. }
  11956. parseFSharpPipelineBody(prec) {
  11957. const startLoc = this.state.startLoc;
  11958. this.state.potentialArrowAt = this.state.start;
  11959. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  11960. this.state.inFSharpPipelineDirectBody = true;
  11961. const ret = this.parseExprOp(this.parseMaybeUnaryOrPrivate(), startLoc, prec);
  11962. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  11963. return ret;
  11964. }
  11965. parseModuleExpression() {
  11966. this.expectPlugin("moduleBlocks");
  11967. const node = this.startNode();
  11968. this.next();
  11969. if (!this.match(5)) {
  11970. this.unexpected(null, 5);
  11971. }
  11972. const program = this.startNodeAt(this.state.endLoc);
  11973. this.next();
  11974. const revertScopes = this.initializeScopes(true);
  11975. this.enterInitialScopes();
  11976. try {
  11977. node.body = this.parseProgram(program, 8, "module");
  11978. } finally {
  11979. revertScopes();
  11980. }
  11981. return this.finishNode(node, "ModuleExpression");
  11982. }
  11983. parsePropertyNamePrefixOperator(prop) {}
  11984. }
  11985. const loopLabel = {
  11986. kind: 1
  11987. },
  11988. switchLabel = {
  11989. kind: 2
  11990. };
  11991. const loneSurrogate = /[\uD800-\uDFFF]/u;
  11992. const keywordRelationalOperator = /in(?:stanceof)?/y;
  11993. function babel7CompatTokens(tokens, input) {
  11994. for (let i = 0; i < tokens.length; i++) {
  11995. const token = tokens[i];
  11996. const {
  11997. type
  11998. } = token;
  11999. if (typeof type === "number") {
  12000. {
  12001. if (type === 138) {
  12002. const {
  12003. loc,
  12004. start,
  12005. value,
  12006. end
  12007. } = token;
  12008. const hashEndPos = start + 1;
  12009. const hashEndLoc = createPositionWithColumnOffset(loc.start, 1);
  12010. tokens.splice(i, 1, new Token({
  12011. type: getExportedToken(27),
  12012. value: "#",
  12013. start: start,
  12014. end: hashEndPos,
  12015. startLoc: loc.start,
  12016. endLoc: hashEndLoc
  12017. }), new Token({
  12018. type: getExportedToken(132),
  12019. value: value,
  12020. start: hashEndPos,
  12021. end: end,
  12022. startLoc: hashEndLoc,
  12023. endLoc: loc.end
  12024. }));
  12025. i++;
  12026. continue;
  12027. }
  12028. if (tokenIsTemplate(type)) {
  12029. const {
  12030. loc,
  12031. start,
  12032. value,
  12033. end
  12034. } = token;
  12035. const backquoteEnd = start + 1;
  12036. const backquoteEndLoc = createPositionWithColumnOffset(loc.start, 1);
  12037. let startToken;
  12038. if (input.charCodeAt(start) === 96) {
  12039. startToken = new Token({
  12040. type: getExportedToken(22),
  12041. value: "`",
  12042. start: start,
  12043. end: backquoteEnd,
  12044. startLoc: loc.start,
  12045. endLoc: backquoteEndLoc
  12046. });
  12047. } else {
  12048. startToken = new Token({
  12049. type: getExportedToken(8),
  12050. value: "}",
  12051. start: start,
  12052. end: backquoteEnd,
  12053. startLoc: loc.start,
  12054. endLoc: backquoteEndLoc
  12055. });
  12056. }
  12057. let templateValue, templateElementEnd, templateElementEndLoc, endToken;
  12058. if (type === 24) {
  12059. templateElementEnd = end - 1;
  12060. templateElementEndLoc = createPositionWithColumnOffset(loc.end, -1);
  12061. templateValue = value === null ? null : value.slice(1, -1);
  12062. endToken = new Token({
  12063. type: getExportedToken(22),
  12064. value: "`",
  12065. start: templateElementEnd,
  12066. end: end,
  12067. startLoc: templateElementEndLoc,
  12068. endLoc: loc.end
  12069. });
  12070. } else {
  12071. templateElementEnd = end - 2;
  12072. templateElementEndLoc = createPositionWithColumnOffset(loc.end, -2);
  12073. templateValue = value === null ? null : value.slice(1, -2);
  12074. endToken = new Token({
  12075. type: getExportedToken(23),
  12076. value: "${",
  12077. start: templateElementEnd,
  12078. end: end,
  12079. startLoc: templateElementEndLoc,
  12080. endLoc: loc.end
  12081. });
  12082. }
  12083. tokens.splice(i, 1, startToken, new Token({
  12084. type: getExportedToken(20),
  12085. value: templateValue,
  12086. start: backquoteEnd,
  12087. end: templateElementEnd,
  12088. startLoc: backquoteEndLoc,
  12089. endLoc: templateElementEndLoc
  12090. }), endToken);
  12091. i += 2;
  12092. continue;
  12093. }
  12094. }
  12095. token.type = getExportedToken(type);
  12096. }
  12097. }
  12098. return tokens;
  12099. }
  12100. class StatementParser extends ExpressionParser {
  12101. parseTopLevel(file, program) {
  12102. file.program = this.parseProgram(program);
  12103. file.comments = this.comments;
  12104. if (this.options.tokens) {
  12105. file.tokens = babel7CompatTokens(this.tokens, this.input);
  12106. }
  12107. return this.finishNode(file, "File");
  12108. }
  12109. parseProgram(program, end = 139, sourceType = this.options.sourceType) {
  12110. program.sourceType = sourceType;
  12111. program.interpreter = this.parseInterpreterDirective();
  12112. this.parseBlockBody(program, true, true, end);
  12113. if (this.inModule) {
  12114. if (!this.options.allowUndeclaredExports && this.scope.undefinedExports.size > 0) {
  12115. for (const [localName, at] of Array.from(this.scope.undefinedExports)) {
  12116. this.raise(Errors.ModuleExportUndefined, at, {
  12117. localName
  12118. });
  12119. }
  12120. }
  12121. this.addExtra(program, "topLevelAwait", this.state.hasTopLevelAwait);
  12122. }
  12123. let finishedProgram;
  12124. if (end === 139) {
  12125. finishedProgram = this.finishNode(program, "Program");
  12126. } else {
  12127. finishedProgram = this.finishNodeAt(program, "Program", createPositionWithColumnOffset(this.state.startLoc, -1));
  12128. }
  12129. return finishedProgram;
  12130. }
  12131. stmtToDirective(stmt) {
  12132. const directive = stmt;
  12133. directive.type = "Directive";
  12134. directive.value = directive.expression;
  12135. delete directive.expression;
  12136. const directiveLiteral = directive.value;
  12137. const expressionValue = directiveLiteral.value;
  12138. const raw = this.input.slice(directiveLiteral.start, directiveLiteral.end);
  12139. const val = directiveLiteral.value = raw.slice(1, -1);
  12140. this.addExtra(directiveLiteral, "raw", raw);
  12141. this.addExtra(directiveLiteral, "rawValue", val);
  12142. this.addExtra(directiveLiteral, "expressionValue", expressionValue);
  12143. directiveLiteral.type = "DirectiveLiteral";
  12144. return directive;
  12145. }
  12146. parseInterpreterDirective() {
  12147. if (!this.match(28)) {
  12148. return null;
  12149. }
  12150. const node = this.startNode();
  12151. node.value = this.state.value;
  12152. this.next();
  12153. return this.finishNode(node, "InterpreterDirective");
  12154. }
  12155. isLet() {
  12156. if (!this.isContextual(100)) {
  12157. return false;
  12158. }
  12159. return this.hasFollowingBindingAtom();
  12160. }
  12161. chStartsBindingIdentifier(ch, pos) {
  12162. if (isIdentifierStart(ch)) {
  12163. keywordRelationalOperator.lastIndex = pos;
  12164. if (keywordRelationalOperator.test(this.input)) {
  12165. const endCh = this.codePointAtPos(keywordRelationalOperator.lastIndex);
  12166. if (!isIdentifierChar(endCh) && endCh !== 92) {
  12167. return false;
  12168. }
  12169. }
  12170. return true;
  12171. } else if (ch === 92) {
  12172. return true;
  12173. } else {
  12174. return false;
  12175. }
  12176. }
  12177. chStartsBindingPattern(ch) {
  12178. return ch === 91 || ch === 123;
  12179. }
  12180. hasFollowingBindingAtom() {
  12181. const next = this.nextTokenStart();
  12182. const nextCh = this.codePointAtPos(next);
  12183. return this.chStartsBindingPattern(nextCh) || this.chStartsBindingIdentifier(nextCh, next);
  12184. }
  12185. hasInLineFollowingBindingIdentifierOrBrace() {
  12186. const next = this.nextTokenInLineStart();
  12187. const nextCh = this.codePointAtPos(next);
  12188. return nextCh === 123 || this.chStartsBindingIdentifier(nextCh, next);
  12189. }
  12190. startsUsingForOf() {
  12191. const {
  12192. type,
  12193. containsEsc
  12194. } = this.lookahead();
  12195. if (type === 102 && !containsEsc) {
  12196. return false;
  12197. } else if (tokenIsIdentifier(type) && !this.hasFollowingLineBreak()) {
  12198. this.expectPlugin("explicitResourceManagement");
  12199. return true;
  12200. }
  12201. }
  12202. startsAwaitUsing() {
  12203. let next = this.nextTokenInLineStart();
  12204. if (this.isUnparsedContextual(next, "using")) {
  12205. next = this.nextTokenInLineStartSince(next + 5);
  12206. const nextCh = this.codePointAtPos(next);
  12207. if (this.chStartsBindingIdentifier(nextCh, next)) {
  12208. this.expectPlugin("explicitResourceManagement");
  12209. return true;
  12210. }
  12211. }
  12212. return false;
  12213. }
  12214. parseModuleItem() {
  12215. return this.parseStatementLike(1 | 2 | 4 | 8);
  12216. }
  12217. parseStatementListItem() {
  12218. return this.parseStatementLike(2 | 4 | (!this.options.annexB || this.state.strict ? 0 : 8));
  12219. }
  12220. parseStatementOrSloppyAnnexBFunctionDeclaration(allowLabeledFunction = false) {
  12221. let flags = 0;
  12222. if (this.options.annexB && !this.state.strict) {
  12223. flags |= 4;
  12224. if (allowLabeledFunction) {
  12225. flags |= 8;
  12226. }
  12227. }
  12228. return this.parseStatementLike(flags);
  12229. }
  12230. parseStatement() {
  12231. return this.parseStatementLike(0);
  12232. }
  12233. parseStatementLike(flags) {
  12234. let decorators = null;
  12235. if (this.match(26)) {
  12236. decorators = this.parseDecorators(true);
  12237. }
  12238. return this.parseStatementContent(flags, decorators);
  12239. }
  12240. parseStatementContent(flags, decorators) {
  12241. const startType = this.state.type;
  12242. const node = this.startNode();
  12243. const allowDeclaration = !!(flags & 2);
  12244. const allowFunctionDeclaration = !!(flags & 4);
  12245. const topLevel = flags & 1;
  12246. switch (startType) {
  12247. case 60:
  12248. return this.parseBreakContinueStatement(node, true);
  12249. case 63:
  12250. return this.parseBreakContinueStatement(node, false);
  12251. case 64:
  12252. return this.parseDebuggerStatement(node);
  12253. case 90:
  12254. return this.parseDoWhileStatement(node);
  12255. case 91:
  12256. return this.parseForStatement(node);
  12257. case 68:
  12258. if (this.lookaheadCharCode() === 46) break;
  12259. if (!allowFunctionDeclaration) {
  12260. this.raise(this.state.strict ? Errors.StrictFunction : this.options.annexB ? Errors.SloppyFunctionAnnexB : Errors.SloppyFunction, this.state.startLoc);
  12261. }
  12262. return this.parseFunctionStatement(node, false, !allowDeclaration && allowFunctionDeclaration);
  12263. case 80:
  12264. if (!allowDeclaration) this.unexpected();
  12265. return this.parseClass(this.maybeTakeDecorators(decorators, node), true);
  12266. case 69:
  12267. return this.parseIfStatement(node);
  12268. case 70:
  12269. return this.parseReturnStatement(node);
  12270. case 71:
  12271. return this.parseSwitchStatement(node);
  12272. case 72:
  12273. return this.parseThrowStatement(node);
  12274. case 73:
  12275. return this.parseTryStatement(node);
  12276. case 96:
  12277. if (!this.state.containsEsc && this.startsAwaitUsing()) {
  12278. if (!this.recordAwaitIfAllowed()) {
  12279. this.raise(Errors.AwaitUsingNotInAsyncContext, node);
  12280. } else if (!allowDeclaration) {
  12281. this.raise(Errors.UnexpectedLexicalDeclaration, node);
  12282. }
  12283. this.next();
  12284. return this.parseVarStatement(node, "await using");
  12285. }
  12286. break;
  12287. case 107:
  12288. if (this.state.containsEsc || !this.hasInLineFollowingBindingIdentifierOrBrace()) {
  12289. break;
  12290. }
  12291. this.expectPlugin("explicitResourceManagement");
  12292. if (!this.scope.inModule && this.scope.inTopLevel) {
  12293. this.raise(Errors.UnexpectedUsingDeclaration, this.state.startLoc);
  12294. } else if (!allowDeclaration) {
  12295. this.raise(Errors.UnexpectedLexicalDeclaration, this.state.startLoc);
  12296. }
  12297. return this.parseVarStatement(node, "using");
  12298. case 100:
  12299. {
  12300. if (this.state.containsEsc) {
  12301. break;
  12302. }
  12303. const next = this.nextTokenStart();
  12304. const nextCh = this.codePointAtPos(next);
  12305. if (nextCh !== 91) {
  12306. if (!allowDeclaration && this.hasFollowingLineBreak()) break;
  12307. if (!this.chStartsBindingIdentifier(nextCh, next) && nextCh !== 123) {
  12308. break;
  12309. }
  12310. }
  12311. }
  12312. case 75:
  12313. {
  12314. if (!allowDeclaration) {
  12315. this.raise(Errors.UnexpectedLexicalDeclaration, this.state.startLoc);
  12316. }
  12317. }
  12318. case 74:
  12319. {
  12320. const kind = this.state.value;
  12321. return this.parseVarStatement(node, kind);
  12322. }
  12323. case 92:
  12324. return this.parseWhileStatement(node);
  12325. case 76:
  12326. return this.parseWithStatement(node);
  12327. case 5:
  12328. return this.parseBlock();
  12329. case 13:
  12330. return this.parseEmptyStatement(node);
  12331. case 83:
  12332. {
  12333. const nextTokenCharCode = this.lookaheadCharCode();
  12334. if (nextTokenCharCode === 40 || nextTokenCharCode === 46) {
  12335. break;
  12336. }
  12337. }
  12338. case 82:
  12339. {
  12340. if (!this.options.allowImportExportEverywhere && !topLevel) {
  12341. this.raise(Errors.UnexpectedImportExport, this.state.startLoc);
  12342. }
  12343. this.next();
  12344. let result;
  12345. if (startType === 83) {
  12346. result = this.parseImport(node);
  12347. if (result.type === "ImportDeclaration" && (!result.importKind || result.importKind === "value")) {
  12348. this.sawUnambiguousESM = true;
  12349. }
  12350. } else {
  12351. result = this.parseExport(node, decorators);
  12352. if (result.type === "ExportNamedDeclaration" && (!result.exportKind || result.exportKind === "value") || result.type === "ExportAllDeclaration" && (!result.exportKind || result.exportKind === "value") || result.type === "ExportDefaultDeclaration") {
  12353. this.sawUnambiguousESM = true;
  12354. }
  12355. }
  12356. this.assertModuleNodeAllowed(result);
  12357. return result;
  12358. }
  12359. default:
  12360. {
  12361. if (this.isAsyncFunction()) {
  12362. if (!allowDeclaration) {
  12363. this.raise(Errors.AsyncFunctionInSingleStatementContext, this.state.startLoc);
  12364. }
  12365. this.next();
  12366. return this.parseFunctionStatement(node, true, !allowDeclaration && allowFunctionDeclaration);
  12367. }
  12368. }
  12369. }
  12370. const maybeName = this.state.value;
  12371. const expr = this.parseExpression();
  12372. if (tokenIsIdentifier(startType) && expr.type === "Identifier" && this.eat(14)) {
  12373. return this.parseLabeledStatement(node, maybeName, expr, flags);
  12374. } else {
  12375. return this.parseExpressionStatement(node, expr, decorators);
  12376. }
  12377. }
  12378. assertModuleNodeAllowed(node) {
  12379. if (!this.options.allowImportExportEverywhere && !this.inModule) {
  12380. this.raise(Errors.ImportOutsideModule, node);
  12381. }
  12382. }
  12383. decoratorsEnabledBeforeExport() {
  12384. if (this.hasPlugin("decorators-legacy")) return true;
  12385. return this.hasPlugin("decorators") && this.getPluginOption("decorators", "decoratorsBeforeExport") !== false;
  12386. }
  12387. maybeTakeDecorators(maybeDecorators, classNode, exportNode) {
  12388. if (maybeDecorators) {
  12389. if (classNode.decorators && classNode.decorators.length > 0) {
  12390. if (typeof this.getPluginOption("decorators", "decoratorsBeforeExport") !== "boolean") {
  12391. this.raise(Errors.DecoratorsBeforeAfterExport, classNode.decorators[0]);
  12392. }
  12393. classNode.decorators.unshift(...maybeDecorators);
  12394. } else {
  12395. classNode.decorators = maybeDecorators;
  12396. }
  12397. this.resetStartLocationFromNode(classNode, maybeDecorators[0]);
  12398. if (exportNode) this.resetStartLocationFromNode(exportNode, classNode);
  12399. }
  12400. return classNode;
  12401. }
  12402. canHaveLeadingDecorator() {
  12403. return this.match(80);
  12404. }
  12405. parseDecorators(allowExport) {
  12406. const decorators = [];
  12407. do {
  12408. decorators.push(this.parseDecorator());
  12409. } while (this.match(26));
  12410. if (this.match(82)) {
  12411. if (!allowExport) {
  12412. this.unexpected();
  12413. }
  12414. if (!this.decoratorsEnabledBeforeExport()) {
  12415. this.raise(Errors.DecoratorExportClass, this.state.startLoc);
  12416. }
  12417. } else if (!this.canHaveLeadingDecorator()) {
  12418. throw this.raise(Errors.UnexpectedLeadingDecorator, this.state.startLoc);
  12419. }
  12420. return decorators;
  12421. }
  12422. parseDecorator() {
  12423. this.expectOnePlugin(["decorators", "decorators-legacy"]);
  12424. const node = this.startNode();
  12425. this.next();
  12426. if (this.hasPlugin("decorators")) {
  12427. const startLoc = this.state.startLoc;
  12428. let expr;
  12429. if (this.match(10)) {
  12430. const startLoc = this.state.startLoc;
  12431. this.next();
  12432. expr = this.parseExpression();
  12433. this.expect(11);
  12434. expr = this.wrapParenthesis(startLoc, expr);
  12435. const paramsStartLoc = this.state.startLoc;
  12436. node.expression = this.parseMaybeDecoratorArguments(expr);
  12437. if (this.getPluginOption("decorators", "allowCallParenthesized") === false && node.expression !== expr) {
  12438. this.raise(Errors.DecoratorArgumentsOutsideParentheses, paramsStartLoc);
  12439. }
  12440. } else {
  12441. expr = this.parseIdentifier(false);
  12442. while (this.eat(16)) {
  12443. const node = this.startNodeAt(startLoc);
  12444. node.object = expr;
  12445. if (this.match(138)) {
  12446. this.classScope.usePrivateName(this.state.value, this.state.startLoc);
  12447. node.property = this.parsePrivateName();
  12448. } else {
  12449. node.property = this.parseIdentifier(true);
  12450. }
  12451. node.computed = false;
  12452. expr = this.finishNode(node, "MemberExpression");
  12453. }
  12454. node.expression = this.parseMaybeDecoratorArguments(expr);
  12455. }
  12456. } else {
  12457. node.expression = this.parseExprSubscripts();
  12458. }
  12459. return this.finishNode(node, "Decorator");
  12460. }
  12461. parseMaybeDecoratorArguments(expr) {
  12462. if (this.eat(10)) {
  12463. const node = this.startNodeAtNode(expr);
  12464. node.callee = expr;
  12465. node.arguments = this.parseCallExpressionArguments(11, false);
  12466. this.toReferencedList(node.arguments);
  12467. return this.finishNode(node, "CallExpression");
  12468. }
  12469. return expr;
  12470. }
  12471. parseBreakContinueStatement(node, isBreak) {
  12472. this.next();
  12473. if (this.isLineTerminator()) {
  12474. node.label = null;
  12475. } else {
  12476. node.label = this.parseIdentifier();
  12477. this.semicolon();
  12478. }
  12479. this.verifyBreakContinue(node, isBreak);
  12480. return this.finishNode(node, isBreak ? "BreakStatement" : "ContinueStatement");
  12481. }
  12482. verifyBreakContinue(node, isBreak) {
  12483. let i;
  12484. for (i = 0; i < this.state.labels.length; ++i) {
  12485. const lab = this.state.labels[i];
  12486. if (node.label == null || lab.name === node.label.name) {
  12487. if (lab.kind != null && (isBreak || lab.kind === 1)) {
  12488. break;
  12489. }
  12490. if (node.label && isBreak) break;
  12491. }
  12492. }
  12493. if (i === this.state.labels.length) {
  12494. const type = isBreak ? "BreakStatement" : "ContinueStatement";
  12495. this.raise(Errors.IllegalBreakContinue, node, {
  12496. type
  12497. });
  12498. }
  12499. }
  12500. parseDebuggerStatement(node) {
  12501. this.next();
  12502. this.semicolon();
  12503. return this.finishNode(node, "DebuggerStatement");
  12504. }
  12505. parseHeaderExpression() {
  12506. this.expect(10);
  12507. const val = this.parseExpression();
  12508. this.expect(11);
  12509. return val;
  12510. }
  12511. parseDoWhileStatement(node) {
  12512. this.next();
  12513. this.state.labels.push(loopLabel);
  12514. node.body = this.withSmartMixTopicForbiddingContext(() => this.parseStatement());
  12515. this.state.labels.pop();
  12516. this.expect(92);
  12517. node.test = this.parseHeaderExpression();
  12518. this.eat(13);
  12519. return this.finishNode(node, "DoWhileStatement");
  12520. }
  12521. parseForStatement(node) {
  12522. this.next();
  12523. this.state.labels.push(loopLabel);
  12524. let awaitAt = null;
  12525. if (this.isContextual(96) && this.recordAwaitIfAllowed()) {
  12526. awaitAt = this.state.startLoc;
  12527. this.next();
  12528. }
  12529. this.scope.enter(0);
  12530. this.expect(10);
  12531. if (this.match(13)) {
  12532. if (awaitAt !== null) {
  12533. this.unexpected(awaitAt);
  12534. }
  12535. return this.parseFor(node, null);
  12536. }
  12537. const startsWithLet = this.isContextual(100);
  12538. {
  12539. const startsWithAwaitUsing = this.isContextual(96) && this.startsAwaitUsing();
  12540. const starsWithUsingDeclaration = startsWithAwaitUsing || this.isContextual(107) && this.startsUsingForOf();
  12541. const isLetOrUsing = startsWithLet && this.hasFollowingBindingAtom() || starsWithUsingDeclaration;
  12542. if (this.match(74) || this.match(75) || isLetOrUsing) {
  12543. const initNode = this.startNode();
  12544. let kind;
  12545. if (startsWithAwaitUsing) {
  12546. kind = "await using";
  12547. if (!this.recordAwaitIfAllowed()) {
  12548. this.raise(Errors.AwaitUsingNotInAsyncContext, this.state.startLoc);
  12549. }
  12550. this.next();
  12551. } else {
  12552. kind = this.state.value;
  12553. }
  12554. this.next();
  12555. this.parseVar(initNode, true, kind);
  12556. const init = this.finishNode(initNode, "VariableDeclaration");
  12557. const isForIn = this.match(58);
  12558. if (isForIn && starsWithUsingDeclaration) {
  12559. this.raise(Errors.ForInUsing, init);
  12560. }
  12561. if ((isForIn || this.isContextual(102)) && init.declarations.length === 1) {
  12562. return this.parseForIn(node, init, awaitAt);
  12563. }
  12564. if (awaitAt !== null) {
  12565. this.unexpected(awaitAt);
  12566. }
  12567. return this.parseFor(node, init);
  12568. }
  12569. }
  12570. const startsWithAsync = this.isContextual(95);
  12571. const refExpressionErrors = new ExpressionErrors();
  12572. const init = this.parseExpression(true, refExpressionErrors);
  12573. const isForOf = this.isContextual(102);
  12574. if (isForOf) {
  12575. if (startsWithLet) {
  12576. this.raise(Errors.ForOfLet, init);
  12577. }
  12578. if (awaitAt === null && startsWithAsync && init.type === "Identifier") {
  12579. this.raise(Errors.ForOfAsync, init);
  12580. }
  12581. }
  12582. if (isForOf || this.match(58)) {
  12583. this.checkDestructuringPrivate(refExpressionErrors);
  12584. this.toAssignable(init, true);
  12585. const type = isForOf ? "ForOfStatement" : "ForInStatement";
  12586. this.checkLVal(init, {
  12587. type
  12588. });
  12589. return this.parseForIn(node, init, awaitAt);
  12590. } else {
  12591. this.checkExpressionErrors(refExpressionErrors, true);
  12592. }
  12593. if (awaitAt !== null) {
  12594. this.unexpected(awaitAt);
  12595. }
  12596. return this.parseFor(node, init);
  12597. }
  12598. parseFunctionStatement(node, isAsync, isHangingDeclaration) {
  12599. this.next();
  12600. return this.parseFunction(node, 1 | (isHangingDeclaration ? 2 : 0) | (isAsync ? 8 : 0));
  12601. }
  12602. parseIfStatement(node) {
  12603. this.next();
  12604. node.test = this.parseHeaderExpression();
  12605. node.consequent = this.parseStatementOrSloppyAnnexBFunctionDeclaration();
  12606. node.alternate = this.eat(66) ? this.parseStatementOrSloppyAnnexBFunctionDeclaration() : null;
  12607. return this.finishNode(node, "IfStatement");
  12608. }
  12609. parseReturnStatement(node) {
  12610. if (!this.prodParam.hasReturn && !this.options.allowReturnOutsideFunction) {
  12611. this.raise(Errors.IllegalReturn, this.state.startLoc);
  12612. }
  12613. this.next();
  12614. if (this.isLineTerminator()) {
  12615. node.argument = null;
  12616. } else {
  12617. node.argument = this.parseExpression();
  12618. this.semicolon();
  12619. }
  12620. return this.finishNode(node, "ReturnStatement");
  12621. }
  12622. parseSwitchStatement(node) {
  12623. this.next();
  12624. node.discriminant = this.parseHeaderExpression();
  12625. const cases = node.cases = [];
  12626. this.expect(5);
  12627. this.state.labels.push(switchLabel);
  12628. this.scope.enter(0);
  12629. let cur;
  12630. for (let sawDefault; !this.match(8);) {
  12631. if (this.match(61) || this.match(65)) {
  12632. const isCase = this.match(61);
  12633. if (cur) this.finishNode(cur, "SwitchCase");
  12634. cases.push(cur = this.startNode());
  12635. cur.consequent = [];
  12636. this.next();
  12637. if (isCase) {
  12638. cur.test = this.parseExpression();
  12639. } else {
  12640. if (sawDefault) {
  12641. this.raise(Errors.MultipleDefaultsInSwitch, this.state.lastTokStartLoc);
  12642. }
  12643. sawDefault = true;
  12644. cur.test = null;
  12645. }
  12646. this.expect(14);
  12647. } else {
  12648. if (cur) {
  12649. cur.consequent.push(this.parseStatementListItem());
  12650. } else {
  12651. this.unexpected();
  12652. }
  12653. }
  12654. }
  12655. this.scope.exit();
  12656. if (cur) this.finishNode(cur, "SwitchCase");
  12657. this.next();
  12658. this.state.labels.pop();
  12659. return this.finishNode(node, "SwitchStatement");
  12660. }
  12661. parseThrowStatement(node) {
  12662. this.next();
  12663. if (this.hasPrecedingLineBreak()) {
  12664. this.raise(Errors.NewlineAfterThrow, this.state.lastTokEndLoc);
  12665. }
  12666. node.argument = this.parseExpression();
  12667. this.semicolon();
  12668. return this.finishNode(node, "ThrowStatement");
  12669. }
  12670. parseCatchClauseParam() {
  12671. const param = this.parseBindingAtom();
  12672. this.scope.enter(this.options.annexB && param.type === "Identifier" ? 8 : 0);
  12673. this.checkLVal(param, {
  12674. type: "CatchClause"
  12675. }, 9);
  12676. return param;
  12677. }
  12678. parseTryStatement(node) {
  12679. this.next();
  12680. node.block = this.parseBlock();
  12681. node.handler = null;
  12682. if (this.match(62)) {
  12683. const clause = this.startNode();
  12684. this.next();
  12685. if (this.match(10)) {
  12686. this.expect(10);
  12687. clause.param = this.parseCatchClauseParam();
  12688. this.expect(11);
  12689. } else {
  12690. clause.param = null;
  12691. this.scope.enter(0);
  12692. }
  12693. clause.body = this.withSmartMixTopicForbiddingContext(() => this.parseBlock(false, false));
  12694. this.scope.exit();
  12695. node.handler = this.finishNode(clause, "CatchClause");
  12696. }
  12697. node.finalizer = this.eat(67) ? this.parseBlock() : null;
  12698. if (!node.handler && !node.finalizer) {
  12699. this.raise(Errors.NoCatchOrFinally, node);
  12700. }
  12701. return this.finishNode(node, "TryStatement");
  12702. }
  12703. parseVarStatement(node, kind, allowMissingInitializer = false) {
  12704. this.next();
  12705. this.parseVar(node, false, kind, allowMissingInitializer);
  12706. this.semicolon();
  12707. return this.finishNode(node, "VariableDeclaration");
  12708. }
  12709. parseWhileStatement(node) {
  12710. this.next();
  12711. node.test = this.parseHeaderExpression();
  12712. this.state.labels.push(loopLabel);
  12713. node.body = this.withSmartMixTopicForbiddingContext(() => this.parseStatement());
  12714. this.state.labels.pop();
  12715. return this.finishNode(node, "WhileStatement");
  12716. }
  12717. parseWithStatement(node) {
  12718. if (this.state.strict) {
  12719. this.raise(Errors.StrictWith, this.state.startLoc);
  12720. }
  12721. this.next();
  12722. node.object = this.parseHeaderExpression();
  12723. node.body = this.withSmartMixTopicForbiddingContext(() => this.parseStatement());
  12724. return this.finishNode(node, "WithStatement");
  12725. }
  12726. parseEmptyStatement(node) {
  12727. this.next();
  12728. return this.finishNode(node, "EmptyStatement");
  12729. }
  12730. parseLabeledStatement(node, maybeName, expr, flags) {
  12731. for (const label of this.state.labels) {
  12732. if (label.name === maybeName) {
  12733. this.raise(Errors.LabelRedeclaration, expr, {
  12734. labelName: maybeName
  12735. });
  12736. }
  12737. }
  12738. const kind = tokenIsLoop(this.state.type) ? 1 : this.match(71) ? 2 : null;
  12739. for (let i = this.state.labels.length - 1; i >= 0; i--) {
  12740. const label = this.state.labels[i];
  12741. if (label.statementStart === node.start) {
  12742. label.statementStart = this.state.start;
  12743. label.kind = kind;
  12744. } else {
  12745. break;
  12746. }
  12747. }
  12748. this.state.labels.push({
  12749. name: maybeName,
  12750. kind: kind,
  12751. statementStart: this.state.start
  12752. });
  12753. node.body = flags & 8 ? this.parseStatementOrSloppyAnnexBFunctionDeclaration(true) : this.parseStatement();
  12754. this.state.labels.pop();
  12755. node.label = expr;
  12756. return this.finishNode(node, "LabeledStatement");
  12757. }
  12758. parseExpressionStatement(node, expr, decorators) {
  12759. node.expression = expr;
  12760. this.semicolon();
  12761. return this.finishNode(node, "ExpressionStatement");
  12762. }
  12763. parseBlock(allowDirectives = false, createNewLexicalScope = true, afterBlockParse) {
  12764. const node = this.startNode();
  12765. if (allowDirectives) {
  12766. this.state.strictErrors.clear();
  12767. }
  12768. this.expect(5);
  12769. if (createNewLexicalScope) {
  12770. this.scope.enter(0);
  12771. }
  12772. this.parseBlockBody(node, allowDirectives, false, 8, afterBlockParse);
  12773. if (createNewLexicalScope) {
  12774. this.scope.exit();
  12775. }
  12776. return this.finishNode(node, "BlockStatement");
  12777. }
  12778. isValidDirective(stmt) {
  12779. return stmt.type === "ExpressionStatement" && stmt.expression.type === "StringLiteral" && !stmt.expression.extra.parenthesized;
  12780. }
  12781. parseBlockBody(node, allowDirectives, topLevel, end, afterBlockParse) {
  12782. const body = node.body = [];
  12783. const directives = node.directives = [];
  12784. this.parseBlockOrModuleBlockBody(body, allowDirectives ? directives : undefined, topLevel, end, afterBlockParse);
  12785. }
  12786. parseBlockOrModuleBlockBody(body, directives, topLevel, end, afterBlockParse) {
  12787. const oldStrict = this.state.strict;
  12788. let hasStrictModeDirective = false;
  12789. let parsedNonDirective = false;
  12790. while (!this.match(end)) {
  12791. const stmt = topLevel ? this.parseModuleItem() : this.parseStatementListItem();
  12792. if (directives && !parsedNonDirective) {
  12793. if (this.isValidDirective(stmt)) {
  12794. const directive = this.stmtToDirective(stmt);
  12795. directives.push(directive);
  12796. if (!hasStrictModeDirective && directive.value.value === "use strict") {
  12797. hasStrictModeDirective = true;
  12798. this.setStrict(true);
  12799. }
  12800. continue;
  12801. }
  12802. parsedNonDirective = true;
  12803. this.state.strictErrors.clear();
  12804. }
  12805. body.push(stmt);
  12806. }
  12807. afterBlockParse == null || afterBlockParse.call(this, hasStrictModeDirective);
  12808. if (!oldStrict) {
  12809. this.setStrict(false);
  12810. }
  12811. this.next();
  12812. }
  12813. parseFor(node, init) {
  12814. node.init = init;
  12815. this.semicolon(false);
  12816. node.test = this.match(13) ? null : this.parseExpression();
  12817. this.semicolon(false);
  12818. node.update = this.match(11) ? null : this.parseExpression();
  12819. this.expect(11);
  12820. node.body = this.withSmartMixTopicForbiddingContext(() => this.parseStatement());
  12821. this.scope.exit();
  12822. this.state.labels.pop();
  12823. return this.finishNode(node, "ForStatement");
  12824. }
  12825. parseForIn(node, init, awaitAt) {
  12826. const isForIn = this.match(58);
  12827. this.next();
  12828. if (isForIn) {
  12829. if (awaitAt !== null) this.unexpected(awaitAt);
  12830. } else {
  12831. node.await = awaitAt !== null;
  12832. }
  12833. if (init.type === "VariableDeclaration" && init.declarations[0].init != null && (!isForIn || !this.options.annexB || this.state.strict || init.kind !== "var" || init.declarations[0].id.type !== "Identifier")) {
  12834. this.raise(Errors.ForInOfLoopInitializer, init, {
  12835. type: isForIn ? "ForInStatement" : "ForOfStatement"
  12836. });
  12837. }
  12838. if (init.type === "AssignmentPattern") {
  12839. this.raise(Errors.InvalidLhs, init, {
  12840. ancestor: {
  12841. type: "ForStatement"
  12842. }
  12843. });
  12844. }
  12845. node.left = init;
  12846. node.right = isForIn ? this.parseExpression() : this.parseMaybeAssignAllowIn();
  12847. this.expect(11);
  12848. node.body = this.withSmartMixTopicForbiddingContext(() => this.parseStatement());
  12849. this.scope.exit();
  12850. this.state.labels.pop();
  12851. return this.finishNode(node, isForIn ? "ForInStatement" : "ForOfStatement");
  12852. }
  12853. parseVar(node, isFor, kind, allowMissingInitializer = false) {
  12854. const declarations = node.declarations = [];
  12855. node.kind = kind;
  12856. for (;;) {
  12857. const decl = this.startNode();
  12858. this.parseVarId(decl, kind);
  12859. decl.init = !this.eat(29) ? null : isFor ? this.parseMaybeAssignDisallowIn() : this.parseMaybeAssignAllowIn();
  12860. if (decl.init === null && !allowMissingInitializer) {
  12861. if (decl.id.type !== "Identifier" && !(isFor && (this.match(58) || this.isContextual(102)))) {
  12862. this.raise(Errors.DeclarationMissingInitializer, this.state.lastTokEndLoc, {
  12863. kind: "destructuring"
  12864. });
  12865. } else if ((kind === "const" || kind === "using" || kind === "await using") && !(this.match(58) || this.isContextual(102))) {
  12866. this.raise(Errors.DeclarationMissingInitializer, this.state.lastTokEndLoc, {
  12867. kind
  12868. });
  12869. }
  12870. }
  12871. declarations.push(this.finishNode(decl, "VariableDeclarator"));
  12872. if (!this.eat(12)) break;
  12873. }
  12874. return node;
  12875. }
  12876. parseVarId(decl, kind) {
  12877. const id = this.parseBindingAtom();
  12878. if (kind === "using" || kind === "await using") {
  12879. if (id.type === "ArrayPattern" || id.type === "ObjectPattern") {
  12880. this.raise(Errors.UsingDeclarationHasBindingPattern, id.loc.start);
  12881. }
  12882. }
  12883. this.checkLVal(id, {
  12884. type: "VariableDeclarator"
  12885. }, kind === "var" ? 5 : 8201);
  12886. decl.id = id;
  12887. }
  12888. parseAsyncFunctionExpression(node) {
  12889. return this.parseFunction(node, 8);
  12890. }
  12891. parseFunction(node, flags = 0) {
  12892. const hangingDeclaration = flags & 2;
  12893. const isDeclaration = !!(flags & 1);
  12894. const requireId = isDeclaration && !(flags & 4);
  12895. const isAsync = !!(flags & 8);
  12896. this.initFunction(node, isAsync);
  12897. if (this.match(55)) {
  12898. if (hangingDeclaration) {
  12899. this.raise(Errors.GeneratorInSingleStatementContext, this.state.startLoc);
  12900. }
  12901. this.next();
  12902. node.generator = true;
  12903. }
  12904. if (isDeclaration) {
  12905. node.id = this.parseFunctionId(requireId);
  12906. }
  12907. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  12908. this.state.maybeInArrowParameters = false;
  12909. this.scope.enter(2);
  12910. this.prodParam.enter(functionFlags(isAsync, node.generator));
  12911. if (!isDeclaration) {
  12912. node.id = this.parseFunctionId();
  12913. }
  12914. this.parseFunctionParams(node, false);
  12915. this.withSmartMixTopicForbiddingContext(() => {
  12916. this.parseFunctionBodyAndFinish(node, isDeclaration ? "FunctionDeclaration" : "FunctionExpression");
  12917. });
  12918. this.prodParam.exit();
  12919. this.scope.exit();
  12920. if (isDeclaration && !hangingDeclaration) {
  12921. this.registerFunctionStatementId(node);
  12922. }
  12923. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  12924. return node;
  12925. }
  12926. parseFunctionId(requireId) {
  12927. return requireId || tokenIsIdentifier(this.state.type) ? this.parseIdentifier() : null;
  12928. }
  12929. parseFunctionParams(node, isConstructor) {
  12930. this.expect(10);
  12931. this.expressionScope.enter(newParameterDeclarationScope());
  12932. node.params = this.parseBindingList(11, 41, 2 | (isConstructor ? 4 : 0));
  12933. this.expressionScope.exit();
  12934. }
  12935. registerFunctionStatementId(node) {
  12936. if (!node.id) return;
  12937. this.scope.declareName(node.id.name, !this.options.annexB || this.state.strict || node.generator || node.async ? this.scope.treatFunctionsAsVar ? 5 : 8201 : 17, node.id.loc.start);
  12938. }
  12939. parseClass(node, isStatement, optionalId) {
  12940. this.next();
  12941. const oldStrict = this.state.strict;
  12942. this.state.strict = true;
  12943. this.parseClassId(node, isStatement, optionalId);
  12944. this.parseClassSuper(node);
  12945. node.body = this.parseClassBody(!!node.superClass, oldStrict);
  12946. return this.finishNode(node, isStatement ? "ClassDeclaration" : "ClassExpression");
  12947. }
  12948. isClassProperty() {
  12949. return this.match(29) || this.match(13) || this.match(8);
  12950. }
  12951. isClassMethod() {
  12952. return this.match(10);
  12953. }
  12954. nameIsConstructor(key) {
  12955. return key.type === "Identifier" && key.name === "constructor" || key.type === "StringLiteral" && key.value === "constructor";
  12956. }
  12957. isNonstaticConstructor(method) {
  12958. return !method.computed && !method.static && this.nameIsConstructor(method.key);
  12959. }
  12960. parseClassBody(hadSuperClass, oldStrict) {
  12961. this.classScope.enter();
  12962. const state = {
  12963. hadConstructor: false,
  12964. hadSuperClass
  12965. };
  12966. let decorators = [];
  12967. const classBody = this.startNode();
  12968. classBody.body = [];
  12969. this.expect(5);
  12970. this.withSmartMixTopicForbiddingContext(() => {
  12971. while (!this.match(8)) {
  12972. if (this.eat(13)) {
  12973. if (decorators.length > 0) {
  12974. throw this.raise(Errors.DecoratorSemicolon, this.state.lastTokEndLoc);
  12975. }
  12976. continue;
  12977. }
  12978. if (this.match(26)) {
  12979. decorators.push(this.parseDecorator());
  12980. continue;
  12981. }
  12982. const member = this.startNode();
  12983. if (decorators.length) {
  12984. member.decorators = decorators;
  12985. this.resetStartLocationFromNode(member, decorators[0]);
  12986. decorators = [];
  12987. }
  12988. this.parseClassMember(classBody, member, state);
  12989. if (member.kind === "constructor" && member.decorators && member.decorators.length > 0) {
  12990. this.raise(Errors.DecoratorConstructor, member);
  12991. }
  12992. }
  12993. });
  12994. this.state.strict = oldStrict;
  12995. this.next();
  12996. if (decorators.length) {
  12997. throw this.raise(Errors.TrailingDecorator, this.state.startLoc);
  12998. }
  12999. this.classScope.exit();
  13000. return this.finishNode(classBody, "ClassBody");
  13001. }
  13002. parseClassMemberFromModifier(classBody, member) {
  13003. const key = this.parseIdentifier(true);
  13004. if (this.isClassMethod()) {
  13005. const method = member;
  13006. method.kind = "method";
  13007. method.computed = false;
  13008. method.key = key;
  13009. method.static = false;
  13010. this.pushClassMethod(classBody, method, false, false, false, false);
  13011. return true;
  13012. } else if (this.isClassProperty()) {
  13013. const prop = member;
  13014. prop.computed = false;
  13015. prop.key = key;
  13016. prop.static = false;
  13017. classBody.body.push(this.parseClassProperty(prop));
  13018. return true;
  13019. }
  13020. this.resetPreviousNodeTrailingComments(key);
  13021. return false;
  13022. }
  13023. parseClassMember(classBody, member, state) {
  13024. const isStatic = this.isContextual(106);
  13025. if (isStatic) {
  13026. if (this.parseClassMemberFromModifier(classBody, member)) {
  13027. return;
  13028. }
  13029. if (this.eat(5)) {
  13030. this.parseClassStaticBlock(classBody, member);
  13031. return;
  13032. }
  13033. }
  13034. this.parseClassMemberWithIsStatic(classBody, member, state, isStatic);
  13035. }
  13036. parseClassMemberWithIsStatic(classBody, member, state, isStatic) {
  13037. const publicMethod = member;
  13038. const privateMethod = member;
  13039. const publicProp = member;
  13040. const privateProp = member;
  13041. const accessorProp = member;
  13042. const method = publicMethod;
  13043. const publicMember = publicMethod;
  13044. member.static = isStatic;
  13045. this.parsePropertyNamePrefixOperator(member);
  13046. if (this.eat(55)) {
  13047. method.kind = "method";
  13048. const isPrivateName = this.match(138);
  13049. this.parseClassElementName(method);
  13050. if (isPrivateName) {
  13051. this.pushClassPrivateMethod(classBody, privateMethod, true, false);
  13052. return;
  13053. }
  13054. if (this.isNonstaticConstructor(publicMethod)) {
  13055. this.raise(Errors.ConstructorIsGenerator, publicMethod.key);
  13056. }
  13057. this.pushClassMethod(classBody, publicMethod, true, false, false, false);
  13058. return;
  13059. }
  13060. const isContextual = !this.state.containsEsc && tokenIsIdentifier(this.state.type);
  13061. const key = this.parseClassElementName(member);
  13062. const maybeContextualKw = isContextual ? key.name : null;
  13063. const isPrivate = this.isPrivateName(key);
  13064. const maybeQuestionTokenStartLoc = this.state.startLoc;
  13065. this.parsePostMemberNameModifiers(publicMember);
  13066. if (this.isClassMethod()) {
  13067. method.kind = "method";
  13068. if (isPrivate) {
  13069. this.pushClassPrivateMethod(classBody, privateMethod, false, false);
  13070. return;
  13071. }
  13072. const isConstructor = this.isNonstaticConstructor(publicMethod);
  13073. let allowsDirectSuper = false;
  13074. if (isConstructor) {
  13075. publicMethod.kind = "constructor";
  13076. if (state.hadConstructor && !this.hasPlugin("typescript")) {
  13077. this.raise(Errors.DuplicateConstructor, key);
  13078. }
  13079. if (isConstructor && this.hasPlugin("typescript") && member.override) {
  13080. this.raise(Errors.OverrideOnConstructor, key);
  13081. }
  13082. state.hadConstructor = true;
  13083. allowsDirectSuper = state.hadSuperClass;
  13084. }
  13085. this.pushClassMethod(classBody, publicMethod, false, false, isConstructor, allowsDirectSuper);
  13086. } else if (this.isClassProperty()) {
  13087. if (isPrivate) {
  13088. this.pushClassPrivateProperty(classBody, privateProp);
  13089. } else {
  13090. this.pushClassProperty(classBody, publicProp);
  13091. }
  13092. } else if (maybeContextualKw === "async" && !this.isLineTerminator()) {
  13093. this.resetPreviousNodeTrailingComments(key);
  13094. const isGenerator = this.eat(55);
  13095. if (publicMember.optional) {
  13096. this.unexpected(maybeQuestionTokenStartLoc);
  13097. }
  13098. method.kind = "method";
  13099. const isPrivate = this.match(138);
  13100. this.parseClassElementName(method);
  13101. this.parsePostMemberNameModifiers(publicMember);
  13102. if (isPrivate) {
  13103. this.pushClassPrivateMethod(classBody, privateMethod, isGenerator, true);
  13104. } else {
  13105. if (this.isNonstaticConstructor(publicMethod)) {
  13106. this.raise(Errors.ConstructorIsAsync, publicMethod.key);
  13107. }
  13108. this.pushClassMethod(classBody, publicMethod, isGenerator, true, false, false);
  13109. }
  13110. } else if ((maybeContextualKw === "get" || maybeContextualKw === "set") && !(this.match(55) && this.isLineTerminator())) {
  13111. this.resetPreviousNodeTrailingComments(key);
  13112. method.kind = maybeContextualKw;
  13113. const isPrivate = this.match(138);
  13114. this.parseClassElementName(publicMethod);
  13115. if (isPrivate) {
  13116. this.pushClassPrivateMethod(classBody, privateMethod, false, false);
  13117. } else {
  13118. if (this.isNonstaticConstructor(publicMethod)) {
  13119. this.raise(Errors.ConstructorIsAccessor, publicMethod.key);
  13120. }
  13121. this.pushClassMethod(classBody, publicMethod, false, false, false, false);
  13122. }
  13123. this.checkGetterSetterParams(publicMethod);
  13124. } else if (maybeContextualKw === "accessor" && !this.isLineTerminator()) {
  13125. this.expectPlugin("decoratorAutoAccessors");
  13126. this.resetPreviousNodeTrailingComments(key);
  13127. const isPrivate = this.match(138);
  13128. this.parseClassElementName(publicProp);
  13129. this.pushClassAccessorProperty(classBody, accessorProp, isPrivate);
  13130. } else if (this.isLineTerminator()) {
  13131. if (isPrivate) {
  13132. this.pushClassPrivateProperty(classBody, privateProp);
  13133. } else {
  13134. this.pushClassProperty(classBody, publicProp);
  13135. }
  13136. } else {
  13137. this.unexpected();
  13138. }
  13139. }
  13140. parseClassElementName(member) {
  13141. const {
  13142. type,
  13143. value
  13144. } = this.state;
  13145. if ((type === 132 || type === 133) && member.static && value === "prototype") {
  13146. this.raise(Errors.StaticPrototype, this.state.startLoc);
  13147. }
  13148. if (type === 138) {
  13149. if (value === "constructor") {
  13150. this.raise(Errors.ConstructorClassPrivateField, this.state.startLoc);
  13151. }
  13152. const key = this.parsePrivateName();
  13153. member.key = key;
  13154. return key;
  13155. }
  13156. this.parsePropertyName(member);
  13157. return member.key;
  13158. }
  13159. parseClassStaticBlock(classBody, member) {
  13160. var _member$decorators;
  13161. this.scope.enter(64 | 128 | 16);
  13162. const oldLabels = this.state.labels;
  13163. this.state.labels = [];
  13164. this.prodParam.enter(0);
  13165. const body = member.body = [];
  13166. this.parseBlockOrModuleBlockBody(body, undefined, false, 8);
  13167. this.prodParam.exit();
  13168. this.scope.exit();
  13169. this.state.labels = oldLabels;
  13170. classBody.body.push(this.finishNode(member, "StaticBlock"));
  13171. if ((_member$decorators = member.decorators) != null && _member$decorators.length) {
  13172. this.raise(Errors.DecoratorStaticBlock, member);
  13173. }
  13174. }
  13175. pushClassProperty(classBody, prop) {
  13176. if (!prop.computed && this.nameIsConstructor(prop.key)) {
  13177. this.raise(Errors.ConstructorClassField, prop.key);
  13178. }
  13179. classBody.body.push(this.parseClassProperty(prop));
  13180. }
  13181. pushClassPrivateProperty(classBody, prop) {
  13182. const node = this.parseClassPrivateProperty(prop);
  13183. classBody.body.push(node);
  13184. this.classScope.declarePrivateName(this.getPrivateNameSV(node.key), 0, node.key.loc.start);
  13185. }
  13186. pushClassAccessorProperty(classBody, prop, isPrivate) {
  13187. if (!isPrivate && !prop.computed && this.nameIsConstructor(prop.key)) {
  13188. this.raise(Errors.ConstructorClassField, prop.key);
  13189. }
  13190. const node = this.parseClassAccessorProperty(prop);
  13191. classBody.body.push(node);
  13192. if (isPrivate) {
  13193. this.classScope.declarePrivateName(this.getPrivateNameSV(node.key), 0, node.key.loc.start);
  13194. }
  13195. }
  13196. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  13197. classBody.body.push(this.parseMethod(method, isGenerator, isAsync, isConstructor, allowsDirectSuper, "ClassMethod", true));
  13198. }
  13199. pushClassPrivateMethod(classBody, method, isGenerator, isAsync) {
  13200. const node = this.parseMethod(method, isGenerator, isAsync, false, false, "ClassPrivateMethod", true);
  13201. classBody.body.push(node);
  13202. const kind = node.kind === "get" ? node.static ? 6 : 2 : node.kind === "set" ? node.static ? 5 : 1 : 0;
  13203. this.declareClassPrivateMethodInScope(node, kind);
  13204. }
  13205. declareClassPrivateMethodInScope(node, kind) {
  13206. this.classScope.declarePrivateName(this.getPrivateNameSV(node.key), kind, node.key.loc.start);
  13207. }
  13208. parsePostMemberNameModifiers(methodOrProp) {}
  13209. parseClassPrivateProperty(node) {
  13210. this.parseInitializer(node);
  13211. this.semicolon();
  13212. return this.finishNode(node, "ClassPrivateProperty");
  13213. }
  13214. parseClassProperty(node) {
  13215. this.parseInitializer(node);
  13216. this.semicolon();
  13217. return this.finishNode(node, "ClassProperty");
  13218. }
  13219. parseClassAccessorProperty(node) {
  13220. this.parseInitializer(node);
  13221. this.semicolon();
  13222. return this.finishNode(node, "ClassAccessorProperty");
  13223. }
  13224. parseInitializer(node) {
  13225. this.scope.enter(64 | 16);
  13226. this.expressionScope.enter(newExpressionScope());
  13227. this.prodParam.enter(0);
  13228. node.value = this.eat(29) ? this.parseMaybeAssignAllowIn() : null;
  13229. this.expressionScope.exit();
  13230. this.prodParam.exit();
  13231. this.scope.exit();
  13232. }
  13233. parseClassId(node, isStatement, optionalId, bindingType = 8331) {
  13234. if (tokenIsIdentifier(this.state.type)) {
  13235. node.id = this.parseIdentifier();
  13236. if (isStatement) {
  13237. this.declareNameFromIdentifier(node.id, bindingType);
  13238. }
  13239. } else {
  13240. if (optionalId || !isStatement) {
  13241. node.id = null;
  13242. } else {
  13243. throw this.raise(Errors.MissingClassName, this.state.startLoc);
  13244. }
  13245. }
  13246. }
  13247. parseClassSuper(node) {
  13248. node.superClass = this.eat(81) ? this.parseExprSubscripts() : null;
  13249. }
  13250. parseExport(node, decorators) {
  13251. const maybeDefaultIdentifier = this.parseMaybeImportPhase(node, true);
  13252. const hasDefault = this.maybeParseExportDefaultSpecifier(node, maybeDefaultIdentifier);
  13253. const parseAfterDefault = !hasDefault || this.eat(12);
  13254. const hasStar = parseAfterDefault && this.eatExportStar(node);
  13255. const hasNamespace = hasStar && this.maybeParseExportNamespaceSpecifier(node);
  13256. const parseAfterNamespace = parseAfterDefault && (!hasNamespace || this.eat(12));
  13257. const isFromRequired = hasDefault || hasStar;
  13258. if (hasStar && !hasNamespace) {
  13259. if (hasDefault) this.unexpected();
  13260. if (decorators) {
  13261. throw this.raise(Errors.UnsupportedDecoratorExport, node);
  13262. }
  13263. this.parseExportFrom(node, true);
  13264. return this.finishNode(node, "ExportAllDeclaration");
  13265. }
  13266. const hasSpecifiers = this.maybeParseExportNamedSpecifiers(node);
  13267. if (hasDefault && parseAfterDefault && !hasStar && !hasSpecifiers) {
  13268. this.unexpected(null, 5);
  13269. }
  13270. if (hasNamespace && parseAfterNamespace) {
  13271. this.unexpected(null, 98);
  13272. }
  13273. let hasDeclaration;
  13274. if (isFromRequired || hasSpecifiers) {
  13275. hasDeclaration = false;
  13276. if (decorators) {
  13277. throw this.raise(Errors.UnsupportedDecoratorExport, node);
  13278. }
  13279. this.parseExportFrom(node, isFromRequired);
  13280. } else {
  13281. hasDeclaration = this.maybeParseExportDeclaration(node);
  13282. }
  13283. if (isFromRequired || hasSpecifiers || hasDeclaration) {
  13284. var _node2$declaration;
  13285. const node2 = node;
  13286. this.checkExport(node2, true, false, !!node2.source);
  13287. if (((_node2$declaration = node2.declaration) == null ? void 0 : _node2$declaration.type) === "ClassDeclaration") {
  13288. this.maybeTakeDecorators(decorators, node2.declaration, node2);
  13289. } else if (decorators) {
  13290. throw this.raise(Errors.UnsupportedDecoratorExport, node);
  13291. }
  13292. return this.finishNode(node2, "ExportNamedDeclaration");
  13293. }
  13294. if (this.eat(65)) {
  13295. const node2 = node;
  13296. const decl = this.parseExportDefaultExpression();
  13297. node2.declaration = decl;
  13298. if (decl.type === "ClassDeclaration") {
  13299. this.maybeTakeDecorators(decorators, decl, node2);
  13300. } else if (decorators) {
  13301. throw this.raise(Errors.UnsupportedDecoratorExport, node);
  13302. }
  13303. this.checkExport(node2, true, true);
  13304. return this.finishNode(node2, "ExportDefaultDeclaration");
  13305. }
  13306. this.unexpected(null, 5);
  13307. }
  13308. eatExportStar(node) {
  13309. return this.eat(55);
  13310. }
  13311. maybeParseExportDefaultSpecifier(node, maybeDefaultIdentifier) {
  13312. if (maybeDefaultIdentifier || this.isExportDefaultSpecifier()) {
  13313. this.expectPlugin("exportDefaultFrom", maybeDefaultIdentifier == null ? void 0 : maybeDefaultIdentifier.loc.start);
  13314. const id = maybeDefaultIdentifier || this.parseIdentifier(true);
  13315. const specifier = this.startNodeAtNode(id);
  13316. specifier.exported = id;
  13317. node.specifiers = [this.finishNode(specifier, "ExportDefaultSpecifier")];
  13318. return true;
  13319. }
  13320. return false;
  13321. }
  13322. maybeParseExportNamespaceSpecifier(node) {
  13323. if (this.isContextual(93)) {
  13324. var _ref, _ref$specifiers;
  13325. (_ref$specifiers = (_ref = node).specifiers) != null ? _ref$specifiers : _ref.specifiers = [];
  13326. const specifier = this.startNodeAt(this.state.lastTokStartLoc);
  13327. this.next();
  13328. specifier.exported = this.parseModuleExportName();
  13329. node.specifiers.push(this.finishNode(specifier, "ExportNamespaceSpecifier"));
  13330. return true;
  13331. }
  13332. return false;
  13333. }
  13334. maybeParseExportNamedSpecifiers(node) {
  13335. if (this.match(5)) {
  13336. const node2 = node;
  13337. if (!node2.specifiers) node2.specifiers = [];
  13338. const isTypeExport = node2.exportKind === "type";
  13339. node2.specifiers.push(...this.parseExportSpecifiers(isTypeExport));
  13340. node2.source = null;
  13341. node2.declaration = null;
  13342. if (this.hasPlugin("importAssertions")) {
  13343. node2.assertions = [];
  13344. }
  13345. return true;
  13346. }
  13347. return false;
  13348. }
  13349. maybeParseExportDeclaration(node) {
  13350. if (this.shouldParseExportDeclaration()) {
  13351. node.specifiers = [];
  13352. node.source = null;
  13353. if (this.hasPlugin("importAssertions")) {
  13354. node.assertions = [];
  13355. }
  13356. node.declaration = this.parseExportDeclaration(node);
  13357. return true;
  13358. }
  13359. return false;
  13360. }
  13361. isAsyncFunction() {
  13362. if (!this.isContextual(95)) return false;
  13363. const next = this.nextTokenInLineStart();
  13364. return this.isUnparsedContextual(next, "function");
  13365. }
  13366. parseExportDefaultExpression() {
  13367. const expr = this.startNode();
  13368. if (this.match(68)) {
  13369. this.next();
  13370. return this.parseFunction(expr, 1 | 4);
  13371. } else if (this.isAsyncFunction()) {
  13372. this.next();
  13373. this.next();
  13374. return this.parseFunction(expr, 1 | 4 | 8);
  13375. }
  13376. if (this.match(80)) {
  13377. return this.parseClass(expr, true, true);
  13378. }
  13379. if (this.match(26)) {
  13380. if (this.hasPlugin("decorators") && this.getPluginOption("decorators", "decoratorsBeforeExport") === true) {
  13381. this.raise(Errors.DecoratorBeforeExport, this.state.startLoc);
  13382. }
  13383. return this.parseClass(this.maybeTakeDecorators(this.parseDecorators(false), this.startNode()), true, true);
  13384. }
  13385. if (this.match(75) || this.match(74) || this.isLet()) {
  13386. throw this.raise(Errors.UnsupportedDefaultExport, this.state.startLoc);
  13387. }
  13388. const res = this.parseMaybeAssignAllowIn();
  13389. this.semicolon();
  13390. return res;
  13391. }
  13392. parseExportDeclaration(node) {
  13393. if (this.match(80)) {
  13394. const node = this.parseClass(this.startNode(), true, false);
  13395. return node;
  13396. }
  13397. return this.parseStatementListItem();
  13398. }
  13399. isExportDefaultSpecifier() {
  13400. const {
  13401. type
  13402. } = this.state;
  13403. if (tokenIsIdentifier(type)) {
  13404. if (type === 95 && !this.state.containsEsc || type === 100) {
  13405. return false;
  13406. }
  13407. if ((type === 130 || type === 129) && !this.state.containsEsc) {
  13408. const {
  13409. type: nextType
  13410. } = this.lookahead();
  13411. if (tokenIsIdentifier(nextType) && nextType !== 98 || nextType === 5) {
  13412. this.expectOnePlugin(["flow", "typescript"]);
  13413. return false;
  13414. }
  13415. }
  13416. } else if (!this.match(65)) {
  13417. return false;
  13418. }
  13419. const next = this.nextTokenStart();
  13420. const hasFrom = this.isUnparsedContextual(next, "from");
  13421. if (this.input.charCodeAt(next) === 44 || tokenIsIdentifier(this.state.type) && hasFrom) {
  13422. return true;
  13423. }
  13424. if (this.match(65) && hasFrom) {
  13425. const nextAfterFrom = this.input.charCodeAt(this.nextTokenStartSince(next + 4));
  13426. return nextAfterFrom === 34 || nextAfterFrom === 39;
  13427. }
  13428. return false;
  13429. }
  13430. parseExportFrom(node, expect) {
  13431. if (this.eatContextual(98)) {
  13432. node.source = this.parseImportSource();
  13433. this.checkExport(node);
  13434. this.maybeParseImportAttributes(node);
  13435. this.checkJSONModuleImport(node);
  13436. } else if (expect) {
  13437. this.unexpected();
  13438. }
  13439. this.semicolon();
  13440. }
  13441. shouldParseExportDeclaration() {
  13442. const {
  13443. type
  13444. } = this.state;
  13445. if (type === 26) {
  13446. this.expectOnePlugin(["decorators", "decorators-legacy"]);
  13447. if (this.hasPlugin("decorators")) {
  13448. if (this.getPluginOption("decorators", "decoratorsBeforeExport") === true) {
  13449. this.raise(Errors.DecoratorBeforeExport, this.state.startLoc);
  13450. }
  13451. return true;
  13452. }
  13453. }
  13454. if (this.isContextual(107)) {
  13455. this.raise(Errors.UsingDeclarationExport, this.state.startLoc);
  13456. return true;
  13457. }
  13458. if (this.isContextual(96) && this.startsAwaitUsing()) {
  13459. this.raise(Errors.UsingDeclarationExport, this.state.startLoc);
  13460. return true;
  13461. }
  13462. return type === 74 || type === 75 || type === 68 || type === 80 || this.isLet() || this.isAsyncFunction();
  13463. }
  13464. checkExport(node, checkNames, isDefault, isFrom) {
  13465. if (checkNames) {
  13466. var _node$specifiers;
  13467. if (isDefault) {
  13468. this.checkDuplicateExports(node, "default");
  13469. if (this.hasPlugin("exportDefaultFrom")) {
  13470. var _declaration$extra;
  13471. const declaration = node.declaration;
  13472. if (declaration.type === "Identifier" && declaration.name === "from" && declaration.end - declaration.start === 4 && !((_declaration$extra = declaration.extra) != null && _declaration$extra.parenthesized)) {
  13473. this.raise(Errors.ExportDefaultFromAsIdentifier, declaration);
  13474. }
  13475. }
  13476. } else if ((_node$specifiers = node.specifiers) != null && _node$specifiers.length) {
  13477. for (const specifier of node.specifiers) {
  13478. const {
  13479. exported
  13480. } = specifier;
  13481. const exportName = exported.type === "Identifier" ? exported.name : exported.value;
  13482. this.checkDuplicateExports(specifier, exportName);
  13483. if (!isFrom && specifier.local) {
  13484. const {
  13485. local
  13486. } = specifier;
  13487. if (local.type !== "Identifier") {
  13488. this.raise(Errors.ExportBindingIsString, specifier, {
  13489. localName: local.value,
  13490. exportName
  13491. });
  13492. } else {
  13493. this.checkReservedWord(local.name, local.loc.start, true, false);
  13494. this.scope.checkLocalExport(local);
  13495. }
  13496. }
  13497. }
  13498. } else if (node.declaration) {
  13499. const decl = node.declaration;
  13500. if (decl.type === "FunctionDeclaration" || decl.type === "ClassDeclaration") {
  13501. const {
  13502. id
  13503. } = decl;
  13504. if (!id) throw new Error("Assertion failure");
  13505. this.checkDuplicateExports(node, id.name);
  13506. } else if (decl.type === "VariableDeclaration") {
  13507. for (const declaration of decl.declarations) {
  13508. this.checkDeclaration(declaration.id);
  13509. }
  13510. }
  13511. }
  13512. }
  13513. }
  13514. checkDeclaration(node) {
  13515. if (node.type === "Identifier") {
  13516. this.checkDuplicateExports(node, node.name);
  13517. } else if (node.type === "ObjectPattern") {
  13518. for (const prop of node.properties) {
  13519. this.checkDeclaration(prop);
  13520. }
  13521. } else if (node.type === "ArrayPattern") {
  13522. for (const elem of node.elements) {
  13523. if (elem) {
  13524. this.checkDeclaration(elem);
  13525. }
  13526. }
  13527. } else if (node.type === "ObjectProperty") {
  13528. this.checkDeclaration(node.value);
  13529. } else if (node.type === "RestElement") {
  13530. this.checkDeclaration(node.argument);
  13531. } else if (node.type === "AssignmentPattern") {
  13532. this.checkDeclaration(node.left);
  13533. }
  13534. }
  13535. checkDuplicateExports(node, exportName) {
  13536. if (this.exportedIdentifiers.has(exportName)) {
  13537. if (exportName === "default") {
  13538. this.raise(Errors.DuplicateDefaultExport, node);
  13539. } else {
  13540. this.raise(Errors.DuplicateExport, node, {
  13541. exportName
  13542. });
  13543. }
  13544. }
  13545. this.exportedIdentifiers.add(exportName);
  13546. }
  13547. parseExportSpecifiers(isInTypeExport) {
  13548. const nodes = [];
  13549. let first = true;
  13550. this.expect(5);
  13551. while (!this.eat(8)) {
  13552. if (first) {
  13553. first = false;
  13554. } else {
  13555. this.expect(12);
  13556. if (this.eat(8)) break;
  13557. }
  13558. const isMaybeTypeOnly = this.isContextual(130);
  13559. const isString = this.match(133);
  13560. const node = this.startNode();
  13561. node.local = this.parseModuleExportName();
  13562. nodes.push(this.parseExportSpecifier(node, isString, isInTypeExport, isMaybeTypeOnly));
  13563. }
  13564. return nodes;
  13565. }
  13566. parseExportSpecifier(node, isString, isInTypeExport, isMaybeTypeOnly) {
  13567. if (this.eatContextual(93)) {
  13568. node.exported = this.parseModuleExportName();
  13569. } else if (isString) {
  13570. node.exported = cloneStringLiteral(node.local);
  13571. } else if (!node.exported) {
  13572. node.exported = cloneIdentifier(node.local);
  13573. }
  13574. return this.finishNode(node, "ExportSpecifier");
  13575. }
  13576. parseModuleExportName() {
  13577. if (this.match(133)) {
  13578. const result = this.parseStringLiteral(this.state.value);
  13579. const surrogate = loneSurrogate.exec(result.value);
  13580. if (surrogate) {
  13581. this.raise(Errors.ModuleExportNameHasLoneSurrogate, result, {
  13582. surrogateCharCode: surrogate[0].charCodeAt(0)
  13583. });
  13584. }
  13585. return result;
  13586. }
  13587. return this.parseIdentifier(true);
  13588. }
  13589. isJSONModuleImport(node) {
  13590. if (node.assertions != null) {
  13591. return node.assertions.some(({
  13592. key,
  13593. value
  13594. }) => {
  13595. return value.value === "json" && (key.type === "Identifier" ? key.name === "type" : key.value === "type");
  13596. });
  13597. }
  13598. return false;
  13599. }
  13600. checkImportReflection(node) {
  13601. const {
  13602. specifiers
  13603. } = node;
  13604. const singleBindingType = specifiers.length === 1 ? specifiers[0].type : null;
  13605. if (node.phase === "source") {
  13606. if (singleBindingType !== "ImportDefaultSpecifier") {
  13607. this.raise(Errors.SourcePhaseImportRequiresDefault, specifiers[0].loc.start);
  13608. }
  13609. } else if (node.phase === "defer") {
  13610. if (singleBindingType !== "ImportNamespaceSpecifier") {
  13611. this.raise(Errors.DeferImportRequiresNamespace, specifiers[0].loc.start);
  13612. }
  13613. } else if (node.module) {
  13614. var _node$assertions;
  13615. if (singleBindingType !== "ImportDefaultSpecifier") {
  13616. this.raise(Errors.ImportReflectionNotBinding, specifiers[0].loc.start);
  13617. }
  13618. if (((_node$assertions = node.assertions) == null ? void 0 : _node$assertions.length) > 0) {
  13619. this.raise(Errors.ImportReflectionHasAssertion, specifiers[0].loc.start);
  13620. }
  13621. }
  13622. }
  13623. checkJSONModuleImport(node) {
  13624. if (this.isJSONModuleImport(node) && node.type !== "ExportAllDeclaration") {
  13625. const {
  13626. specifiers
  13627. } = node;
  13628. if (specifiers != null) {
  13629. const nonDefaultNamedSpecifier = specifiers.find(specifier => {
  13630. let imported;
  13631. if (specifier.type === "ExportSpecifier") {
  13632. imported = specifier.local;
  13633. } else if (specifier.type === "ImportSpecifier") {
  13634. imported = specifier.imported;
  13635. }
  13636. if (imported !== undefined) {
  13637. return imported.type === "Identifier" ? imported.name !== "default" : imported.value !== "default";
  13638. }
  13639. });
  13640. if (nonDefaultNamedSpecifier !== undefined) {
  13641. this.raise(Errors.ImportJSONBindingNotDefault, nonDefaultNamedSpecifier.loc.start);
  13642. }
  13643. }
  13644. }
  13645. }
  13646. isPotentialImportPhase(isExport) {
  13647. if (isExport) return false;
  13648. return this.isContextual(105) || this.isContextual(97) || this.isContextual(127);
  13649. }
  13650. applyImportPhase(node, isExport, phase, loc) {
  13651. if (isExport) {
  13652. return;
  13653. }
  13654. if (phase === "module") {
  13655. this.expectPlugin("importReflection", loc);
  13656. node.module = true;
  13657. } else if (this.hasPlugin("importReflection")) {
  13658. node.module = false;
  13659. }
  13660. if (phase === "source") {
  13661. this.expectPlugin("sourcePhaseImports", loc);
  13662. node.phase = "source";
  13663. } else if (phase === "defer") {
  13664. this.expectPlugin("deferredImportEvaluation", loc);
  13665. node.phase = "defer";
  13666. } else if (this.hasPlugin("sourcePhaseImports")) {
  13667. node.phase = null;
  13668. }
  13669. }
  13670. parseMaybeImportPhase(node, isExport) {
  13671. if (!this.isPotentialImportPhase(isExport)) {
  13672. this.applyImportPhase(node, isExport, null);
  13673. return null;
  13674. }
  13675. const phaseIdentifier = this.parseIdentifier(true);
  13676. const {
  13677. type
  13678. } = this.state;
  13679. const isImportPhase = tokenIsKeywordOrIdentifier(type) ? type !== 98 || this.lookaheadCharCode() === 102 : type !== 12;
  13680. if (isImportPhase) {
  13681. this.resetPreviousIdentifierLeadingComments(phaseIdentifier);
  13682. this.applyImportPhase(node, isExport, phaseIdentifier.name, phaseIdentifier.loc.start);
  13683. return null;
  13684. } else {
  13685. this.applyImportPhase(node, isExport, null);
  13686. return phaseIdentifier;
  13687. }
  13688. }
  13689. isPrecedingIdImportPhase(phase) {
  13690. const {
  13691. type
  13692. } = this.state;
  13693. return tokenIsIdentifier(type) ? type !== 98 || this.lookaheadCharCode() === 102 : type !== 12;
  13694. }
  13695. parseImport(node) {
  13696. if (this.match(133)) {
  13697. return this.parseImportSourceAndAttributes(node);
  13698. }
  13699. return this.parseImportSpecifiersAndAfter(node, this.parseMaybeImportPhase(node, false));
  13700. }
  13701. parseImportSpecifiersAndAfter(node, maybeDefaultIdentifier) {
  13702. node.specifiers = [];
  13703. const hasDefault = this.maybeParseDefaultImportSpecifier(node, maybeDefaultIdentifier);
  13704. const parseNext = !hasDefault || this.eat(12);
  13705. const hasStar = parseNext && this.maybeParseStarImportSpecifier(node);
  13706. if (parseNext && !hasStar) this.parseNamedImportSpecifiers(node);
  13707. this.expectContextual(98);
  13708. return this.parseImportSourceAndAttributes(node);
  13709. }
  13710. parseImportSourceAndAttributes(node) {
  13711. var _node$specifiers2;
  13712. (_node$specifiers2 = node.specifiers) != null ? _node$specifiers2 : node.specifiers = [];
  13713. node.source = this.parseImportSource();
  13714. this.maybeParseImportAttributes(node);
  13715. this.checkImportReflection(node);
  13716. this.checkJSONModuleImport(node);
  13717. this.semicolon();
  13718. return this.finishNode(node, "ImportDeclaration");
  13719. }
  13720. parseImportSource() {
  13721. if (!this.match(133)) this.unexpected();
  13722. return this.parseExprAtom();
  13723. }
  13724. parseImportSpecifierLocal(node, specifier, type) {
  13725. specifier.local = this.parseIdentifier();
  13726. node.specifiers.push(this.finishImportSpecifier(specifier, type));
  13727. }
  13728. finishImportSpecifier(specifier, type, bindingType = 8201) {
  13729. this.checkLVal(specifier.local, {
  13730. type
  13731. }, bindingType);
  13732. return this.finishNode(specifier, type);
  13733. }
  13734. parseImportAttributes() {
  13735. this.expect(5);
  13736. const attrs = [];
  13737. const attrNames = new Set();
  13738. do {
  13739. if (this.match(8)) {
  13740. break;
  13741. }
  13742. const node = this.startNode();
  13743. const keyName = this.state.value;
  13744. if (attrNames.has(keyName)) {
  13745. this.raise(Errors.ModuleAttributesWithDuplicateKeys, this.state.startLoc, {
  13746. key: keyName
  13747. });
  13748. }
  13749. attrNames.add(keyName);
  13750. if (this.match(133)) {
  13751. node.key = this.parseStringLiteral(keyName);
  13752. } else {
  13753. node.key = this.parseIdentifier(true);
  13754. }
  13755. this.expect(14);
  13756. if (!this.match(133)) {
  13757. throw this.raise(Errors.ModuleAttributeInvalidValue, this.state.startLoc);
  13758. }
  13759. node.value = this.parseStringLiteral(this.state.value);
  13760. attrs.push(this.finishNode(node, "ImportAttribute"));
  13761. } while (this.eat(12));
  13762. this.expect(8);
  13763. return attrs;
  13764. }
  13765. parseModuleAttributes() {
  13766. const attrs = [];
  13767. const attributes = new Set();
  13768. do {
  13769. const node = this.startNode();
  13770. node.key = this.parseIdentifier(true);
  13771. if (node.key.name !== "type") {
  13772. this.raise(Errors.ModuleAttributeDifferentFromType, node.key);
  13773. }
  13774. if (attributes.has(node.key.name)) {
  13775. this.raise(Errors.ModuleAttributesWithDuplicateKeys, node.key, {
  13776. key: node.key.name
  13777. });
  13778. }
  13779. attributes.add(node.key.name);
  13780. this.expect(14);
  13781. if (!this.match(133)) {
  13782. throw this.raise(Errors.ModuleAttributeInvalidValue, this.state.startLoc);
  13783. }
  13784. node.value = this.parseStringLiteral(this.state.value);
  13785. attrs.push(this.finishNode(node, "ImportAttribute"));
  13786. } while (this.eat(12));
  13787. return attrs;
  13788. }
  13789. maybeParseImportAttributes(node) {
  13790. let attributes;
  13791. let useWith = false;
  13792. if (this.match(76)) {
  13793. if (this.hasPrecedingLineBreak() && this.lookaheadCharCode() === 40) {
  13794. return;
  13795. }
  13796. this.next();
  13797. {
  13798. if (this.hasPlugin("moduleAttributes")) {
  13799. attributes = this.parseModuleAttributes();
  13800. } else {
  13801. this.expectImportAttributesPlugin();
  13802. attributes = this.parseImportAttributes();
  13803. }
  13804. }
  13805. useWith = true;
  13806. } else if (this.isContextual(94) && !this.hasPrecedingLineBreak()) {
  13807. if (this.hasPlugin("importAttributes")) {
  13808. if (this.getPluginOption("importAttributes", "deprecatedAssertSyntax") !== true) {
  13809. this.raise(Errors.ImportAttributesUseAssert, this.state.startLoc);
  13810. }
  13811. this.addExtra(node, "deprecatedAssertSyntax", true);
  13812. } else {
  13813. this.expectOnePlugin(["importAttributes", "importAssertions"]);
  13814. }
  13815. this.next();
  13816. attributes = this.parseImportAttributes();
  13817. } else if (this.hasPlugin("importAttributes") || this.hasPlugin("importAssertions")) {
  13818. attributes = [];
  13819. } else {
  13820. if (this.hasPlugin("moduleAttributes")) {
  13821. attributes = [];
  13822. } else return;
  13823. }
  13824. if (!useWith && this.hasPlugin("importAssertions")) {
  13825. node.assertions = attributes;
  13826. } else {
  13827. node.attributes = attributes;
  13828. }
  13829. }
  13830. maybeParseDefaultImportSpecifier(node, maybeDefaultIdentifier) {
  13831. if (maybeDefaultIdentifier) {
  13832. const specifier = this.startNodeAtNode(maybeDefaultIdentifier);
  13833. specifier.local = maybeDefaultIdentifier;
  13834. node.specifiers.push(this.finishImportSpecifier(specifier, "ImportDefaultSpecifier"));
  13835. return true;
  13836. } else if (tokenIsKeywordOrIdentifier(this.state.type)) {
  13837. this.parseImportSpecifierLocal(node, this.startNode(), "ImportDefaultSpecifier");
  13838. return true;
  13839. }
  13840. return false;
  13841. }
  13842. maybeParseStarImportSpecifier(node) {
  13843. if (this.match(55)) {
  13844. const specifier = this.startNode();
  13845. this.next();
  13846. this.expectContextual(93);
  13847. this.parseImportSpecifierLocal(node, specifier, "ImportNamespaceSpecifier");
  13848. return true;
  13849. }
  13850. return false;
  13851. }
  13852. parseNamedImportSpecifiers(node) {
  13853. let first = true;
  13854. this.expect(5);
  13855. while (!this.eat(8)) {
  13856. if (first) {
  13857. first = false;
  13858. } else {
  13859. if (this.eat(14)) {
  13860. throw this.raise(Errors.DestructureNamedImport, this.state.startLoc);
  13861. }
  13862. this.expect(12);
  13863. if (this.eat(8)) break;
  13864. }
  13865. const specifier = this.startNode();
  13866. const importedIsString = this.match(133);
  13867. const isMaybeTypeOnly = this.isContextual(130);
  13868. specifier.imported = this.parseModuleExportName();
  13869. const importSpecifier = this.parseImportSpecifier(specifier, importedIsString, node.importKind === "type" || node.importKind === "typeof", isMaybeTypeOnly, undefined);
  13870. node.specifiers.push(importSpecifier);
  13871. }
  13872. }
  13873. parseImportSpecifier(specifier, importedIsString, isInTypeOnlyImport, isMaybeTypeOnly, bindingType) {
  13874. if (this.eatContextual(93)) {
  13875. specifier.local = this.parseIdentifier();
  13876. } else {
  13877. const {
  13878. imported
  13879. } = specifier;
  13880. if (importedIsString) {
  13881. throw this.raise(Errors.ImportBindingIsString, specifier, {
  13882. importName: imported.value
  13883. });
  13884. }
  13885. this.checkReservedWord(imported.name, specifier.loc.start, true, true);
  13886. if (!specifier.local) {
  13887. specifier.local = cloneIdentifier(imported);
  13888. }
  13889. }
  13890. return this.finishImportSpecifier(specifier, "ImportSpecifier", bindingType);
  13891. }
  13892. isThisParam(param) {
  13893. return param.type === "Identifier" && param.name === "this";
  13894. }
  13895. }
  13896. class Parser extends StatementParser {
  13897. constructor(options, input, pluginsMap) {
  13898. options = getOptions(options);
  13899. super(options, input);
  13900. this.options = options;
  13901. this.initializeScopes();
  13902. this.plugins = pluginsMap;
  13903. this.filename = options.sourceFilename;
  13904. }
  13905. getScopeHandler() {
  13906. return ScopeHandler;
  13907. }
  13908. parse() {
  13909. this.enterInitialScopes();
  13910. const file = this.startNode();
  13911. const program = this.startNode();
  13912. this.nextToken();
  13913. file.errors = null;
  13914. this.parseTopLevel(file, program);
  13915. file.errors = this.state.errors;
  13916. file.comments.length = this.state.commentsLen;
  13917. return file;
  13918. }
  13919. }
  13920. function parse(input, options) {
  13921. var _options;
  13922. if (((_options = options) == null ? void 0 : _options.sourceType) === "unambiguous") {
  13923. options = Object.assign({}, options);
  13924. try {
  13925. options.sourceType = "module";
  13926. const parser = getParser(options, input);
  13927. const ast = parser.parse();
  13928. if (parser.sawUnambiguousESM) {
  13929. return ast;
  13930. }
  13931. if (parser.ambiguousScriptDifferentAst) {
  13932. try {
  13933. options.sourceType = "script";
  13934. return getParser(options, input).parse();
  13935. } catch (_unused) {}
  13936. } else {
  13937. ast.program.sourceType = "script";
  13938. }
  13939. return ast;
  13940. } catch (moduleError) {
  13941. try {
  13942. options.sourceType = "script";
  13943. return getParser(options, input).parse();
  13944. } catch (_unused2) {}
  13945. throw moduleError;
  13946. }
  13947. } else {
  13948. return getParser(options, input).parse();
  13949. }
  13950. }
  13951. function parseExpression(input, options) {
  13952. const parser = getParser(options, input);
  13953. if (parser.options.strictMode) {
  13954. parser.state.strict = true;
  13955. }
  13956. return parser.getExpression();
  13957. }
  13958. function generateExportedTokenTypes(internalTokenTypes) {
  13959. const tokenTypes = {};
  13960. for (const typeName of Object.keys(internalTokenTypes)) {
  13961. tokenTypes[typeName] = getExportedToken(internalTokenTypes[typeName]);
  13962. }
  13963. return tokenTypes;
  13964. }
  13965. const tokTypes = generateExportedTokenTypes(tt);
  13966. function getParser(options, input) {
  13967. let cls = Parser;
  13968. const pluginsMap = new Map();
  13969. if (options != null && options.plugins) {
  13970. for (const plugin of options.plugins) {
  13971. let name, opts;
  13972. if (typeof plugin === "string") {
  13973. name = plugin;
  13974. } else {
  13975. [name, opts] = plugin;
  13976. }
  13977. if (!pluginsMap.has(name)) {
  13978. pluginsMap.set(name, opts || {});
  13979. }
  13980. }
  13981. validatePlugins(pluginsMap);
  13982. cls = getParserClass(pluginsMap);
  13983. }
  13984. return new cls(options, input, pluginsMap);
  13985. }
  13986. const parserClassCache = new Map();
  13987. function getParserClass(pluginsMap) {
  13988. const pluginList = [];
  13989. for (const name of mixinPluginNames) {
  13990. if (pluginsMap.has(name)) {
  13991. pluginList.push(name);
  13992. }
  13993. }
  13994. const key = pluginList.join("|");
  13995. let cls = parserClassCache.get(key);
  13996. if (!cls) {
  13997. cls = Parser;
  13998. for (const plugin of pluginList) {
  13999. cls = mixinPlugins[plugin](cls);
  14000. }
  14001. parserClassCache.set(key, cls);
  14002. }
  14003. return cls;
  14004. }
  14005. exports.parse = parse;
  14006. exports.parseExpression = parseExpression;
  14007. exports.tokTypes = tokTypes;
  14008. //# sourceMappingURL=index.js.map