index.js 493 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504
  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 (-1 !== e.indexOf(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. BadGetterArity: "A 'get' accessor must not have any formal parameters.",
  91. BadSetterArity: "A 'set' accessor must have exactly one formal parameter.",
  92. BadSetterRestParameter: "A 'set' accessor function argument must not be a rest parameter.",
  93. ConstructorClassField: "Classes may not have a field named 'constructor'.",
  94. ConstructorClassPrivateField: "Classes may not have a private field named '#constructor'.",
  95. ConstructorIsAccessor: "Class constructor may not be an accessor.",
  96. ConstructorIsAsync: "Constructor can't be an async function.",
  97. ConstructorIsGenerator: "Constructor can't be a generator.",
  98. DeclarationMissingInitializer: ({
  99. kind
  100. }) => `Missing initializer in ${kind} declaration.`,
  101. DecoratorArgumentsOutsideParentheses: "Decorator arguments must be moved inside parentheses: use '@(decorator(args))' instead of '@(decorator)(args)'.",
  102. DecoratorBeforeExport: "Decorators must be placed *before* the 'export' keyword. Remove the 'decoratorsBeforeExport: true' option to use the 'export @decorator class {}' syntax.",
  103. DecoratorsBeforeAfterExport: "Decorators can be placed *either* before or after the 'export' keyword, but not in both locations at the same time.",
  104. DecoratorConstructor: "Decorators can't be used with a constructor. Did you mean '@dec class { ... }'?",
  105. DecoratorExportClass: "Decorators must be placed *after* the 'export' keyword. Remove the 'decoratorsBeforeExport: false' option to use the '@decorator export class {}' syntax.",
  106. DecoratorSemicolon: "Decorators must not be followed by a semicolon.",
  107. DecoratorStaticBlock: "Decorators can't be used with a static block.",
  108. DeferImportRequiresNamespace: 'Only `import defer * as x from "./module"` is valid.',
  109. DeletePrivateField: "Deleting a private field is not allowed.",
  110. DestructureNamedImport: "ES2015 named imports do not destructure. Use another statement for destructuring after the import.",
  111. DuplicateConstructor: "Duplicate constructor in the same class.",
  112. DuplicateDefaultExport: "Only one default export allowed per module.",
  113. DuplicateExport: ({
  114. exportName
  115. }) => `\`${exportName}\` has already been exported. Exported identifiers must be unique.`,
  116. DuplicateProto: "Redefinition of __proto__ property.",
  117. DuplicateRegExpFlags: "Duplicate regular expression flag.",
  118. ElementAfterRest: "Rest element must be last element.",
  119. EscapedCharNotAnIdentifier: "Invalid Unicode escape.",
  120. ExportBindingIsString: ({
  121. localName,
  122. exportName
  123. }) => `A string literal cannot be used as an exported binding without \`from\`.\n- Did you mean \`export { '${localName}' as '${exportName}' } from 'some-module'\`?`,
  124. ExportDefaultFromAsIdentifier: "'from' is not allowed as an identifier after 'export default'.",
  125. ForInOfLoopInitializer: ({
  126. type
  127. }) => `'${type === "ForInStatement" ? "for-in" : "for-of"}' loop variable declaration may not have an initializer.`,
  128. ForInUsing: "For-in loop may not start with 'using' declaration.",
  129. ForOfAsync: "The left-hand side of a for-of loop may not be 'async'.",
  130. ForOfLet: "The left-hand side of a for-of loop may not start with 'let'.",
  131. GeneratorInSingleStatementContext: "Generators can only be declared at the top level or inside a block.",
  132. IllegalBreakContinue: ({
  133. type
  134. }) => `Unsyntactic ${type === "BreakStatement" ? "break" : "continue"}.`,
  135. IllegalLanguageModeDirective: "Illegal 'use strict' directive in function with non-simple parameter list.",
  136. IllegalReturn: "'return' outside of function.",
  137. ImportAttributesUseAssert: "The `assert` keyword in import attributes is deprecated and it has been replaced by the `with` keyword. You can enable the `deprecatedImportAssert` parser plugin to suppress this error.",
  138. ImportBindingIsString: ({
  139. importName
  140. }) => `A string literal cannot be used as an imported binding.\n- Did you mean \`import { "${importName}" as foo }\`?`,
  141. ImportCallArity: `\`import()\` requires exactly one or two arguments.`,
  142. ImportCallNotNewExpression: "Cannot use new with import(...).",
  143. ImportCallSpreadArgument: "`...` is not allowed in `import()`.",
  144. ImportJSONBindingNotDefault: "A JSON module can only be imported with `default`.",
  145. ImportReflectionHasAssertion: "`import module x` cannot have assertions.",
  146. ImportReflectionNotBinding: 'Only `import module x from "./module"` is valid.',
  147. IncompatibleRegExpUVFlags: "The 'u' and 'v' regular expression flags cannot be enabled at the same time.",
  148. InvalidBigIntLiteral: "Invalid BigIntLiteral.",
  149. InvalidCodePoint: "Code point out of bounds.",
  150. InvalidCoverInitializedName: "Invalid shorthand property initializer.",
  151. InvalidDecimal: "Invalid decimal.",
  152. InvalidDigit: ({
  153. radix
  154. }) => `Expected number in radix ${radix}.`,
  155. InvalidEscapeSequence: "Bad character escape sequence.",
  156. InvalidEscapeSequenceTemplate: "Invalid escape sequence in template.",
  157. InvalidEscapedReservedWord: ({
  158. reservedWord
  159. }) => `Escape sequence in keyword ${reservedWord}.`,
  160. InvalidIdentifier: ({
  161. identifierName
  162. }) => `Invalid identifier ${identifierName}.`,
  163. InvalidLhs: ({
  164. ancestor
  165. }) => `Invalid left-hand side in ${toNodeDescription(ancestor)}.`,
  166. InvalidLhsBinding: ({
  167. ancestor
  168. }) => `Binding invalid left-hand side in ${toNodeDescription(ancestor)}.`,
  169. InvalidLhsOptionalChaining: ({
  170. ancestor
  171. }) => `Invalid optional chaining in the left-hand side of ${toNodeDescription(ancestor)}.`,
  172. InvalidNumber: "Invalid number.",
  173. InvalidOrMissingExponent: "Floating-point numbers require a valid exponent after the 'e'.",
  174. InvalidOrUnexpectedToken: ({
  175. unexpected
  176. }) => `Unexpected character '${unexpected}'.`,
  177. InvalidParenthesizedAssignment: "Invalid parenthesized assignment pattern.",
  178. InvalidPrivateFieldResolution: ({
  179. identifierName
  180. }) => `Private name #${identifierName} is not defined.`,
  181. InvalidPropertyBindingPattern: "Binding member expression.",
  182. InvalidRecordProperty: "Only properties and spread elements are allowed in record definitions.",
  183. InvalidRestAssignmentPattern: "Invalid rest operator's argument.",
  184. LabelRedeclaration: ({
  185. labelName
  186. }) => `Label '${labelName}' is already declared.`,
  187. LetInLexicalBinding: "'let' is disallowed as a lexically bound name.",
  188. LineTerminatorBeforeArrow: "No line break is allowed before '=>'.",
  189. MalformedRegExpFlags: "Invalid regular expression flag.",
  190. MissingClassName: "A class name is required.",
  191. MissingEqInAssignment: "Only '=' operator can be used for specifying default value.",
  192. MissingSemicolon: "Missing semicolon.",
  193. MissingPlugin: ({
  194. missingPlugin
  195. }) => `This experimental syntax requires enabling the parser plugin: ${missingPlugin.map(name => JSON.stringify(name)).join(", ")}.`,
  196. MissingOneOfPlugins: ({
  197. missingPlugin
  198. }) => `This experimental syntax requires enabling one of the following parser plugin(s): ${missingPlugin.map(name => JSON.stringify(name)).join(", ")}.`,
  199. MissingUnicodeEscape: "Expecting Unicode escape sequence \\uXXXX.",
  200. MixingCoalesceWithLogical: "Nullish coalescing operator(??) requires parens when mixing with logical operators.",
  201. ModuleAttributeDifferentFromType: "The only accepted module attribute is `type`.",
  202. ModuleAttributeInvalidValue: "Only string literals are allowed as module attribute values.",
  203. ModuleAttributesWithDuplicateKeys: ({
  204. key
  205. }) => `Duplicate key "${key}" is not allowed in module attributes.`,
  206. ModuleExportNameHasLoneSurrogate: ({
  207. surrogateCharCode
  208. }) => `An export name cannot include a lone surrogate, found '\\u${surrogateCharCode.toString(16)}'.`,
  209. ModuleExportUndefined: ({
  210. localName
  211. }) => `Export '${localName}' is not defined.`,
  212. MultipleDefaultsInSwitch: "Multiple default clauses.",
  213. NewlineAfterThrow: "Illegal newline after throw.",
  214. NoCatchOrFinally: "Missing catch or finally clause.",
  215. NumberIdentifier: "Identifier directly after number.",
  216. NumericSeparatorInEscapeSequence: "Numeric separators are not allowed inside unicode escape sequences or hex escape sequences.",
  217. ObsoleteAwaitStar: "'await*' has been removed from the async functions proposal. Use Promise.all() instead.",
  218. OptionalChainingNoNew: "Constructors in/after an Optional Chain are not allowed.",
  219. OptionalChainingNoTemplate: "Tagged Template Literals are not allowed in optionalChain.",
  220. OverrideOnConstructor: "'override' modifier cannot appear on a constructor declaration.",
  221. ParamDupe: "Argument name clash.",
  222. PatternHasAccessor: "Object pattern can't contain getter or setter.",
  223. PatternHasMethod: "Object pattern can't contain methods.",
  224. PrivateInExpectedIn: ({
  225. identifierName
  226. }) => `Private names are only allowed in property accesses (\`obj.#${identifierName}\`) or in \`in\` expressions (\`#${identifierName} in obj\`).`,
  227. PrivateNameRedeclaration: ({
  228. identifierName
  229. }) => `Duplicate private name #${identifierName}.`,
  230. RecordExpressionBarIncorrectEndSyntaxType: "Record expressions ending with '|}' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'bar'.",
  231. RecordExpressionBarIncorrectStartSyntaxType: "Record expressions starting with '{|' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'bar'.",
  232. RecordExpressionHashIncorrectStartSyntaxType: "Record expressions starting with '#{' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'hash'.",
  233. RecordNoProto: "'__proto__' is not allowed in Record expressions.",
  234. RestTrailingComma: "Unexpected trailing comma after rest element.",
  235. SloppyFunction: "In non-strict mode code, functions can only be declared at top level or inside a block.",
  236. 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.",
  237. SourcePhaseImportRequiresDefault: 'Only `import source x from "./module"` is valid.',
  238. StaticPrototype: "Classes may not have static property named prototype.",
  239. 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?",
  240. SuperPrivateField: "Private fields can't be accessed on super.",
  241. TrailingDecorator: "Decorators must be attached to a class element.",
  242. TupleExpressionBarIncorrectEndSyntaxType: "Tuple expressions ending with '|]' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'bar'.",
  243. TupleExpressionBarIncorrectStartSyntaxType: "Tuple expressions starting with '[|' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'bar'.",
  244. TupleExpressionHashIncorrectStartSyntaxType: "Tuple expressions starting with '#[' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'hash'.",
  245. UnexpectedArgumentPlaceholder: "Unexpected argument placeholder.",
  246. UnexpectedAwaitAfterPipelineBody: 'Unexpected "await" after pipeline body; await must have parentheses in minimal proposal.',
  247. UnexpectedDigitAfterHash: "Unexpected digit after hash token.",
  248. UnexpectedImportExport: "'import' and 'export' may only appear at the top level.",
  249. UnexpectedKeyword: ({
  250. keyword
  251. }) => `Unexpected keyword '${keyword}'.`,
  252. UnexpectedLeadingDecorator: "Leading decorators must be attached to a class declaration.",
  253. UnexpectedLexicalDeclaration: "Lexical declaration cannot appear in a single-statement context.",
  254. UnexpectedNewTarget: "`new.target` can only be used in functions or class properties.",
  255. UnexpectedNumericSeparator: "A numeric separator is only allowed between two digits.",
  256. UnexpectedPrivateField: "Unexpected private name.",
  257. UnexpectedReservedWord: ({
  258. reservedWord
  259. }) => `Unexpected reserved word '${reservedWord}'.`,
  260. UnexpectedSuper: "'super' is only allowed in object methods and classes.",
  261. UnexpectedToken: ({
  262. expected,
  263. unexpected
  264. }) => `Unexpected token${unexpected ? ` '${unexpected}'.` : ""}${expected ? `, expected "${expected}"` : ""}`,
  265. UnexpectedTokenUnaryExponentiation: "Illegal expression. Wrap left hand side or entire exponentiation in parentheses.",
  266. UnexpectedUsingDeclaration: "Using declaration cannot appear in the top level when source type is `script` or in the bare case statement.",
  267. UnsupportedBind: "Binding should be performed on object property.",
  268. UnsupportedDecoratorExport: "A decorated export must export a class declaration.",
  269. UnsupportedDefaultExport: "Only expressions, functions or classes are allowed as the `default` export.",
  270. UnsupportedImport: "`import` can only be used in `import()` or `import.meta`.",
  271. UnsupportedMetaProperty: ({
  272. target,
  273. onlyValidPropertyName
  274. }) => `The only valid meta property for ${target} is ${target}.${onlyValidPropertyName}.`,
  275. UnsupportedParameterDecorator: "Decorators cannot be used to decorate parameters.",
  276. UnsupportedPropertyDecorator: "Decorators cannot be used to decorate object literal properties.",
  277. UnsupportedSuper: "'super' can only be used with function calls (i.e. super()) or in property accesses (i.e. super.prop or super[prop]).",
  278. UnterminatedComment: "Unterminated comment.",
  279. UnterminatedRegExp: "Unterminated regular expression.",
  280. UnterminatedString: "Unterminated string constant.",
  281. UnterminatedTemplate: "Unterminated template.",
  282. UsingDeclarationExport: "Using declaration cannot be exported.",
  283. UsingDeclarationHasBindingPattern: "Using declaration cannot have destructuring patterns.",
  284. VarRedeclaration: ({
  285. identifierName
  286. }) => `Identifier '${identifierName}' has already been declared.`,
  287. YieldBindingIdentifier: "Can not use 'yield' as identifier inside a generator.",
  288. YieldInParameter: "Yield expression is not allowed in formal parameters.",
  289. YieldNotInGeneratorFunction: "'yield' is only allowed within generator functions.",
  290. ZeroDigitNumericSeparator: "Numeric separator can not be used after leading 0."
  291. };
  292. var StrictModeErrors = {
  293. StrictDelete: "Deleting local variable in strict mode.",
  294. StrictEvalArguments: ({
  295. referenceName
  296. }) => `Assigning to '${referenceName}' in strict mode.`,
  297. StrictEvalArgumentsBinding: ({
  298. bindingName
  299. }) => `Binding '${bindingName}' in strict mode.`,
  300. StrictFunction: "In strict mode code, functions can only be declared at top level or inside a block.",
  301. StrictNumericEscape: "The only valid numeric escape in strict mode is '\\0'.",
  302. StrictOctalLiteral: "Legacy octal literals are not allowed in strict mode.",
  303. StrictWith: "'with' in strict mode."
  304. };
  305. var ParseExpressionErrors = {
  306. ParseExpressionEmptyInput: "Unexpected parseExpression() input: The input is empty or contains only comments.",
  307. ParseExpressionExpectsEOF: ({
  308. unexpected
  309. }) => `Unexpected parseExpression() input: The input should contain exactly one expression, but the first expression is followed by the unexpected character \`${String.fromCodePoint(unexpected)}\`.`
  310. };
  311. const UnparenthesizedPipeBodyDescriptions = new Set(["ArrowFunctionExpression", "AssignmentExpression", "ConditionalExpression", "YieldExpression"]);
  312. var PipelineOperatorErrors = Object.assign({
  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. }, {
  326. PipelineBodyNoArrow: 'Unexpected arrow "=>" after pipeline body; arrow function in pipeline body must be parenthesized.',
  327. PipelineBodySequenceExpression: "Pipeline body may not be a comma-separated sequence expression.",
  328. PipelineHeadSequenceExpression: "Pipeline head should not be a comma-separated sequence expression.",
  329. PipelineTopicUnused: "Pipeline is in topic style but does not use topic reference.",
  330. PrimaryTopicNotAllowed: "Topic reference was used in a lexical context without topic binding.",
  331. PrimaryTopicRequiresSmartPipeline: 'Topic reference is used, but the pipelineOperator plugin was not passed a "proposal": "hack" or "smart" option.'
  332. });
  333. const _excluded = ["message"];
  334. function defineHidden(obj, key, value) {
  335. Object.defineProperty(obj, key, {
  336. enumerable: false,
  337. configurable: true,
  338. value
  339. });
  340. }
  341. function toParseErrorConstructor({
  342. toMessage,
  343. code,
  344. reasonCode,
  345. syntaxPlugin
  346. }) {
  347. const hasMissingPlugin = reasonCode === "MissingPlugin" || reasonCode === "MissingOneOfPlugins";
  348. {
  349. const oldReasonCodes = {
  350. AccessorCannotDeclareThisParameter: "AccesorCannotDeclareThisParameter",
  351. AccessorCannotHaveTypeParameters: "AccesorCannotHaveTypeParameters",
  352. ConstInitializerMustBeStringOrNumericLiteralOrLiteralEnumReference: "ConstInitiailizerMustBeStringOrNumericLiteralOrLiteralEnumReference",
  353. SetAccessorCannotHaveOptionalParameter: "SetAccesorCannotHaveOptionalParameter",
  354. SetAccessorCannotHaveRestParameter: "SetAccesorCannotHaveRestParameter",
  355. SetAccessorCannotHaveReturnType: "SetAccesorCannotHaveReturnType"
  356. };
  357. if (oldReasonCodes[reasonCode]) {
  358. reasonCode = oldReasonCodes[reasonCode];
  359. }
  360. }
  361. return function constructor(loc, details) {
  362. const error = new SyntaxError();
  363. error.code = code;
  364. error.reasonCode = reasonCode;
  365. error.loc = loc;
  366. error.pos = loc.index;
  367. error.syntaxPlugin = syntaxPlugin;
  368. if (hasMissingPlugin) {
  369. error.missingPlugin = details.missingPlugin;
  370. }
  371. defineHidden(error, "clone", function clone(overrides = {}) {
  372. var _overrides$loc;
  373. const {
  374. line,
  375. column,
  376. index
  377. } = (_overrides$loc = overrides.loc) != null ? _overrides$loc : loc;
  378. return constructor(new Position(line, column, index), Object.assign({}, details, overrides.details));
  379. });
  380. defineHidden(error, "details", details);
  381. Object.defineProperty(error, "message", {
  382. configurable: true,
  383. get() {
  384. const message = `${toMessage(details)} (${loc.line}:${loc.column})`;
  385. this.message = message;
  386. return message;
  387. },
  388. set(value) {
  389. Object.defineProperty(this, "message", {
  390. value,
  391. writable: true
  392. });
  393. }
  394. });
  395. return error;
  396. };
  397. }
  398. function ParseErrorEnum(argument, syntaxPlugin) {
  399. if (Array.isArray(argument)) {
  400. return parseErrorTemplates => ParseErrorEnum(parseErrorTemplates, argument[0]);
  401. }
  402. const ParseErrorConstructors = {};
  403. for (const reasonCode of Object.keys(argument)) {
  404. const template = argument[reasonCode];
  405. const _ref = typeof template === "string" ? {
  406. message: () => template
  407. } : typeof template === "function" ? {
  408. message: template
  409. } : template,
  410. {
  411. message
  412. } = _ref,
  413. rest = _objectWithoutPropertiesLoose(_ref, _excluded);
  414. const toMessage = typeof message === "string" ? () => message : message;
  415. ParseErrorConstructors[reasonCode] = toParseErrorConstructor(Object.assign({
  416. code: "BABEL_PARSER_SYNTAX_ERROR",
  417. reasonCode,
  418. toMessage
  419. }, syntaxPlugin ? {
  420. syntaxPlugin
  421. } : {}, rest));
  422. }
  423. return ParseErrorConstructors;
  424. }
  425. const Errors = Object.assign({}, ParseErrorEnum(ModuleErrors), ParseErrorEnum(StandardErrors), ParseErrorEnum(StrictModeErrors), ParseErrorEnum(ParseExpressionErrors), ParseErrorEnum`pipelineOperator`(PipelineOperatorErrors));
  426. function createDefaultOptions() {
  427. return {
  428. sourceType: "script",
  429. sourceFilename: undefined,
  430. startIndex: 0,
  431. startColumn: 0,
  432. startLine: 1,
  433. allowAwaitOutsideFunction: false,
  434. allowReturnOutsideFunction: false,
  435. allowNewTargetOutsideFunction: false,
  436. allowImportExportEverywhere: false,
  437. allowSuperOutsideMethod: false,
  438. allowUndeclaredExports: false,
  439. allowYieldOutsideFunction: false,
  440. plugins: [],
  441. strictMode: null,
  442. ranges: false,
  443. tokens: false,
  444. createImportExpressions: false,
  445. createParenthesizedExpressions: false,
  446. errorRecovery: false,
  447. attachComment: true,
  448. annexB: true
  449. };
  450. }
  451. function getOptions(opts) {
  452. const options = createDefaultOptions();
  453. if (opts == null) {
  454. return options;
  455. }
  456. if (opts.annexB != null && opts.annexB !== false) {
  457. throw new Error("The `annexB` option can only be set to `false`.");
  458. }
  459. for (const key of Object.keys(options)) {
  460. if (opts[key] != null) options[key] = opts[key];
  461. }
  462. if (options.startLine === 1) {
  463. if (opts.startIndex == null && options.startColumn > 0) {
  464. options.startIndex = options.startColumn;
  465. } else if (opts.startColumn == null && options.startIndex > 0) {
  466. options.startColumn = options.startIndex;
  467. }
  468. } else if (opts.startColumn == null || opts.startIndex == null) {
  469. if (opts.startIndex != null) {
  470. throw new Error("With a `startLine > 1` you must also specify `startIndex` and `startColumn`.");
  471. }
  472. }
  473. return options;
  474. }
  475. const {
  476. defineProperty
  477. } = Object;
  478. const toUnenumerable = (object, key) => {
  479. if (object) {
  480. defineProperty(object, key, {
  481. enumerable: false,
  482. value: object[key]
  483. });
  484. }
  485. };
  486. function toESTreeLocation(node) {
  487. toUnenumerable(node.loc.start, "index");
  488. toUnenumerable(node.loc.end, "index");
  489. return node;
  490. }
  491. var estree = superClass => class ESTreeParserMixin extends superClass {
  492. parse() {
  493. const file = toESTreeLocation(super.parse());
  494. if (this.optionFlags & 256) {
  495. file.tokens = file.tokens.map(toESTreeLocation);
  496. }
  497. return file;
  498. }
  499. parseRegExpLiteral({
  500. pattern,
  501. flags
  502. }) {
  503. let regex = null;
  504. try {
  505. regex = new RegExp(pattern, flags);
  506. } catch (_) {}
  507. const node = this.estreeParseLiteral(regex);
  508. node.regex = {
  509. pattern,
  510. flags
  511. };
  512. return node;
  513. }
  514. parseBigIntLiteral(value) {
  515. let bigInt;
  516. try {
  517. bigInt = BigInt(value);
  518. } catch (_unused) {
  519. bigInt = null;
  520. }
  521. const node = this.estreeParseLiteral(bigInt);
  522. node.bigint = String(node.value || value);
  523. return node;
  524. }
  525. parseDecimalLiteral(value) {
  526. const decimal = null;
  527. const node = this.estreeParseLiteral(decimal);
  528. node.decimal = String(node.value || value);
  529. return node;
  530. }
  531. estreeParseLiteral(value) {
  532. return this.parseLiteral(value, "Literal");
  533. }
  534. parseStringLiteral(value) {
  535. return this.estreeParseLiteral(value);
  536. }
  537. parseNumericLiteral(value) {
  538. return this.estreeParseLiteral(value);
  539. }
  540. parseNullLiteral() {
  541. return this.estreeParseLiteral(null);
  542. }
  543. parseBooleanLiteral(value) {
  544. return this.estreeParseLiteral(value);
  545. }
  546. estreeParseChainExpression(node, endLoc) {
  547. const chain = this.startNodeAtNode(node);
  548. chain.expression = node;
  549. return this.finishNodeAt(chain, "ChainExpression", endLoc);
  550. }
  551. directiveToStmt(directive) {
  552. const expression = directive.value;
  553. delete directive.value;
  554. this.castNodeTo(expression, "Literal");
  555. expression.raw = expression.extra.raw;
  556. expression.value = expression.extra.expressionValue;
  557. const stmt = this.castNodeTo(directive, "ExpressionStatement");
  558. stmt.expression = expression;
  559. stmt.directive = expression.extra.rawValue;
  560. delete expression.extra;
  561. return stmt;
  562. }
  563. fillOptionalPropertiesForTSESLint(node) {}
  564. cloneEstreeStringLiteral(node) {
  565. const {
  566. start,
  567. end,
  568. loc,
  569. range,
  570. raw,
  571. value
  572. } = node;
  573. const cloned = Object.create(node.constructor.prototype);
  574. cloned.type = "Literal";
  575. cloned.start = start;
  576. cloned.end = end;
  577. cloned.loc = loc;
  578. cloned.range = range;
  579. cloned.raw = raw;
  580. cloned.value = value;
  581. return cloned;
  582. }
  583. initFunction(node, isAsync) {
  584. super.initFunction(node, isAsync);
  585. node.expression = false;
  586. }
  587. checkDeclaration(node) {
  588. if (node != null && this.isObjectProperty(node)) {
  589. this.checkDeclaration(node.value);
  590. } else {
  591. super.checkDeclaration(node);
  592. }
  593. }
  594. getObjectOrClassMethodParams(method) {
  595. return method.value.params;
  596. }
  597. isValidDirective(stmt) {
  598. var _stmt$expression$extr;
  599. return stmt.type === "ExpressionStatement" && stmt.expression.type === "Literal" && typeof stmt.expression.value === "string" && !((_stmt$expression$extr = stmt.expression.extra) != null && _stmt$expression$extr.parenthesized);
  600. }
  601. parseBlockBody(node, allowDirectives, topLevel, end, afterBlockParse) {
  602. super.parseBlockBody(node, allowDirectives, topLevel, end, afterBlockParse);
  603. const directiveStatements = node.directives.map(d => this.directiveToStmt(d));
  604. node.body = directiveStatements.concat(node.body);
  605. delete node.directives;
  606. }
  607. parsePrivateName() {
  608. const node = super.parsePrivateName();
  609. {
  610. if (!this.getPluginOption("estree", "classFeatures")) {
  611. return node;
  612. }
  613. }
  614. return this.convertPrivateNameToPrivateIdentifier(node);
  615. }
  616. convertPrivateNameToPrivateIdentifier(node) {
  617. const name = super.getPrivateNameSV(node);
  618. node = node;
  619. delete node.id;
  620. node.name = name;
  621. return this.castNodeTo(node, "PrivateIdentifier");
  622. }
  623. isPrivateName(node) {
  624. {
  625. if (!this.getPluginOption("estree", "classFeatures")) {
  626. return super.isPrivateName(node);
  627. }
  628. }
  629. return node.type === "PrivateIdentifier";
  630. }
  631. getPrivateNameSV(node) {
  632. {
  633. if (!this.getPluginOption("estree", "classFeatures")) {
  634. return super.getPrivateNameSV(node);
  635. }
  636. }
  637. return node.name;
  638. }
  639. parseLiteral(value, type) {
  640. const node = super.parseLiteral(value, type);
  641. node.raw = node.extra.raw;
  642. delete node.extra;
  643. return node;
  644. }
  645. parseFunctionBody(node, allowExpression, isMethod = false) {
  646. super.parseFunctionBody(node, allowExpression, isMethod);
  647. node.expression = node.body.type !== "BlockStatement";
  648. }
  649. parseMethod(node, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope = false) {
  650. let funcNode = this.startNode();
  651. funcNode.kind = node.kind;
  652. funcNode = super.parseMethod(funcNode, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope);
  653. delete funcNode.kind;
  654. const {
  655. typeParameters
  656. } = node;
  657. if (typeParameters) {
  658. delete node.typeParameters;
  659. funcNode.typeParameters = typeParameters;
  660. this.resetStartLocationFromNode(funcNode, typeParameters);
  661. }
  662. const valueNode = this.castNodeTo(funcNode, "FunctionExpression");
  663. node.value = valueNode;
  664. if (type === "ClassPrivateMethod") {
  665. node.computed = false;
  666. }
  667. if (type === "ObjectMethod") {
  668. if (node.kind === "method") {
  669. node.kind = "init";
  670. }
  671. node.shorthand = false;
  672. return this.finishNode(node, "Property");
  673. } else {
  674. return this.finishNode(node, "MethodDefinition");
  675. }
  676. }
  677. nameIsConstructor(key) {
  678. if (key.type === "Literal") return key.value === "constructor";
  679. return super.nameIsConstructor(key);
  680. }
  681. parseClassProperty(...args) {
  682. const propertyNode = super.parseClassProperty(...args);
  683. {
  684. if (!this.getPluginOption("estree", "classFeatures")) {
  685. return propertyNode;
  686. }
  687. }
  688. {
  689. this.castNodeTo(propertyNode, "PropertyDefinition");
  690. }
  691. return propertyNode;
  692. }
  693. parseClassPrivateProperty(...args) {
  694. const propertyNode = super.parseClassPrivateProperty(...args);
  695. {
  696. if (!this.getPluginOption("estree", "classFeatures")) {
  697. return propertyNode;
  698. }
  699. }
  700. {
  701. this.castNodeTo(propertyNode, "PropertyDefinition");
  702. }
  703. propertyNode.computed = false;
  704. return propertyNode;
  705. }
  706. parseClassAccessorProperty(node) {
  707. const accessorPropertyNode = super.parseClassAccessorProperty(node);
  708. {
  709. if (!this.getPluginOption("estree", "classFeatures")) {
  710. return accessorPropertyNode;
  711. }
  712. }
  713. if (accessorPropertyNode.abstract && this.hasPlugin("typescript")) {
  714. delete accessorPropertyNode.abstract;
  715. this.castNodeTo(accessorPropertyNode, "TSAbstractAccessorProperty");
  716. } else {
  717. this.castNodeTo(accessorPropertyNode, "AccessorProperty");
  718. }
  719. return accessorPropertyNode;
  720. }
  721. parseObjectProperty(prop, startLoc, isPattern, refExpressionErrors) {
  722. const node = super.parseObjectProperty(prop, startLoc, isPattern, refExpressionErrors);
  723. if (node) {
  724. node.kind = "init";
  725. this.castNodeTo(node, "Property");
  726. }
  727. return node;
  728. }
  729. finishObjectProperty(node) {
  730. node.kind = "init";
  731. return this.finishNode(node, "Property");
  732. }
  733. isValidLVal(type, isUnparenthesizedInAssign, binding) {
  734. return type === "Property" ? "value" : super.isValidLVal(type, isUnparenthesizedInAssign, binding);
  735. }
  736. isAssignable(node, isBinding) {
  737. if (node != null && this.isObjectProperty(node)) {
  738. return this.isAssignable(node.value, isBinding);
  739. }
  740. return super.isAssignable(node, isBinding);
  741. }
  742. toAssignable(node, isLHS = false) {
  743. if (node != null && this.isObjectProperty(node)) {
  744. const {
  745. key,
  746. value
  747. } = node;
  748. if (this.isPrivateName(key)) {
  749. this.classScope.usePrivateName(this.getPrivateNameSV(key), key.loc.start);
  750. }
  751. this.toAssignable(value, isLHS);
  752. } else {
  753. super.toAssignable(node, isLHS);
  754. }
  755. }
  756. toAssignableObjectExpressionProp(prop, isLast, isLHS) {
  757. if (prop.type === "Property" && (prop.kind === "get" || prop.kind === "set")) {
  758. this.raise(Errors.PatternHasAccessor, prop.key);
  759. } else if (prop.type === "Property" && prop.method) {
  760. this.raise(Errors.PatternHasMethod, prop.key);
  761. } else {
  762. super.toAssignableObjectExpressionProp(prop, isLast, isLHS);
  763. }
  764. }
  765. finishCallExpression(unfinished, optional) {
  766. const node = super.finishCallExpression(unfinished, optional);
  767. if (node.callee.type === "Import") {
  768. var _ref, _ref2;
  769. this.castNodeTo(node, "ImportExpression");
  770. node.source = node.arguments[0];
  771. node.options = (_ref = node.arguments[1]) != null ? _ref : null;
  772. node.attributes = (_ref2 = node.arguments[1]) != null ? _ref2 : null;
  773. delete node.arguments;
  774. delete node.callee;
  775. } else if (node.type === "OptionalCallExpression") {
  776. this.castNodeTo(node, "CallExpression");
  777. } else {
  778. node.optional = false;
  779. }
  780. return node;
  781. }
  782. toReferencedArguments(node) {
  783. if (node.type === "ImportExpression") {
  784. return;
  785. }
  786. super.toReferencedArguments(node);
  787. }
  788. parseExport(unfinished, decorators) {
  789. const exportStartLoc = this.state.lastTokStartLoc;
  790. const node = super.parseExport(unfinished, decorators);
  791. switch (node.type) {
  792. case "ExportAllDeclaration":
  793. node.exported = null;
  794. break;
  795. case "ExportNamedDeclaration":
  796. if (node.specifiers.length === 1 && node.specifiers[0].type === "ExportNamespaceSpecifier") {
  797. this.castNodeTo(node, "ExportAllDeclaration");
  798. node.exported = node.specifiers[0].exported;
  799. delete node.specifiers;
  800. }
  801. case "ExportDefaultDeclaration":
  802. {
  803. var _declaration$decorato;
  804. const {
  805. declaration
  806. } = node;
  807. if ((declaration == null ? void 0 : declaration.type) === "ClassDeclaration" && ((_declaration$decorato = declaration.decorators) == null ? void 0 : _declaration$decorato.length) > 0 && declaration.start === node.start) {
  808. this.resetStartLocation(node, exportStartLoc);
  809. }
  810. }
  811. break;
  812. }
  813. return node;
  814. }
  815. stopParseSubscript(base, state) {
  816. const node = super.stopParseSubscript(base, state);
  817. if (state.optionalChainMember) {
  818. return this.estreeParseChainExpression(node, base.loc.end);
  819. }
  820. return node;
  821. }
  822. parseMember(base, startLoc, state, computed, optional) {
  823. const node = super.parseMember(base, startLoc, state, computed, optional);
  824. if (node.type === "OptionalMemberExpression") {
  825. this.castNodeTo(node, "MemberExpression");
  826. } else {
  827. node.optional = false;
  828. }
  829. return node;
  830. }
  831. isOptionalMemberExpression(node) {
  832. if (node.type === "ChainExpression") {
  833. return node.expression.type === "MemberExpression";
  834. }
  835. return super.isOptionalMemberExpression(node);
  836. }
  837. hasPropertyAsPrivateName(node) {
  838. if (node.type === "ChainExpression") {
  839. node = node.expression;
  840. }
  841. return super.hasPropertyAsPrivateName(node);
  842. }
  843. isObjectProperty(node) {
  844. return node.type === "Property" && node.kind === "init" && !node.method;
  845. }
  846. isObjectMethod(node) {
  847. return node.type === "Property" && (node.method || node.kind === "get" || node.kind === "set");
  848. }
  849. castNodeTo(node, type) {
  850. const result = super.castNodeTo(node, type);
  851. this.fillOptionalPropertiesForTSESLint(result);
  852. return result;
  853. }
  854. cloneIdentifier(node) {
  855. const cloned = super.cloneIdentifier(node);
  856. this.fillOptionalPropertiesForTSESLint(cloned);
  857. return cloned;
  858. }
  859. cloneStringLiteral(node) {
  860. if (node.type === "Literal") {
  861. return this.cloneEstreeStringLiteral(node);
  862. }
  863. return super.cloneStringLiteral(node);
  864. }
  865. finishNodeAt(node, type, endLoc) {
  866. return toESTreeLocation(super.finishNodeAt(node, type, endLoc));
  867. }
  868. finishNode(node, type) {
  869. const result = super.finishNode(node, type);
  870. this.fillOptionalPropertiesForTSESLint(result);
  871. return result;
  872. }
  873. resetStartLocation(node, startLoc) {
  874. super.resetStartLocation(node, startLoc);
  875. toESTreeLocation(node);
  876. }
  877. resetEndLocation(node, endLoc = this.state.lastTokEndLoc) {
  878. super.resetEndLocation(node, endLoc);
  879. toESTreeLocation(node);
  880. }
  881. };
  882. class TokContext {
  883. constructor(token, preserveSpace) {
  884. this.token = void 0;
  885. this.preserveSpace = void 0;
  886. this.token = token;
  887. this.preserveSpace = !!preserveSpace;
  888. }
  889. }
  890. const types = {
  891. brace: new TokContext("{"),
  892. j_oTag: new TokContext("<tag"),
  893. j_cTag: new TokContext("</tag"),
  894. j_expr: new TokContext("<tag>...</tag>", true)
  895. };
  896. {
  897. types.template = new TokContext("`", true);
  898. }
  899. const beforeExpr = true;
  900. const startsExpr = true;
  901. const isLoop = true;
  902. const isAssign = true;
  903. const prefix = true;
  904. const postfix = true;
  905. class ExportedTokenType {
  906. constructor(label, conf = {}) {
  907. this.label = void 0;
  908. this.keyword = void 0;
  909. this.beforeExpr = void 0;
  910. this.startsExpr = void 0;
  911. this.rightAssociative = void 0;
  912. this.isLoop = void 0;
  913. this.isAssign = void 0;
  914. this.prefix = void 0;
  915. this.postfix = void 0;
  916. this.binop = void 0;
  917. this.label = label;
  918. this.keyword = conf.keyword;
  919. this.beforeExpr = !!conf.beforeExpr;
  920. this.startsExpr = !!conf.startsExpr;
  921. this.rightAssociative = !!conf.rightAssociative;
  922. this.isLoop = !!conf.isLoop;
  923. this.isAssign = !!conf.isAssign;
  924. this.prefix = !!conf.prefix;
  925. this.postfix = !!conf.postfix;
  926. this.binop = conf.binop != null ? conf.binop : null;
  927. {
  928. this.updateContext = null;
  929. }
  930. }
  931. }
  932. const keywords$1 = new Map();
  933. function createKeyword(name, options = {}) {
  934. options.keyword = name;
  935. const token = createToken(name, options);
  936. keywords$1.set(name, token);
  937. return token;
  938. }
  939. function createBinop(name, binop) {
  940. return createToken(name, {
  941. beforeExpr,
  942. binop
  943. });
  944. }
  945. let tokenTypeCounter = -1;
  946. const tokenTypes = [];
  947. const tokenLabels = [];
  948. const tokenBinops = [];
  949. const tokenBeforeExprs = [];
  950. const tokenStartsExprs = [];
  951. const tokenPrefixes = [];
  952. function createToken(name, options = {}) {
  953. var _options$binop, _options$beforeExpr, _options$startsExpr, _options$prefix;
  954. ++tokenTypeCounter;
  955. tokenLabels.push(name);
  956. tokenBinops.push((_options$binop = options.binop) != null ? _options$binop : -1);
  957. tokenBeforeExprs.push((_options$beforeExpr = options.beforeExpr) != null ? _options$beforeExpr : false);
  958. tokenStartsExprs.push((_options$startsExpr = options.startsExpr) != null ? _options$startsExpr : false);
  959. tokenPrefixes.push((_options$prefix = options.prefix) != null ? _options$prefix : false);
  960. tokenTypes.push(new ExportedTokenType(name, options));
  961. return tokenTypeCounter;
  962. }
  963. function createKeywordLike(name, options = {}) {
  964. var _options$binop2, _options$beforeExpr2, _options$startsExpr2, _options$prefix2;
  965. ++tokenTypeCounter;
  966. keywords$1.set(name, tokenTypeCounter);
  967. tokenLabels.push(name);
  968. tokenBinops.push((_options$binop2 = options.binop) != null ? _options$binop2 : -1);
  969. tokenBeforeExprs.push((_options$beforeExpr2 = options.beforeExpr) != null ? _options$beforeExpr2 : false);
  970. tokenStartsExprs.push((_options$startsExpr2 = options.startsExpr) != null ? _options$startsExpr2 : false);
  971. tokenPrefixes.push((_options$prefix2 = options.prefix) != null ? _options$prefix2 : false);
  972. tokenTypes.push(new ExportedTokenType("name", options));
  973. return tokenTypeCounter;
  974. }
  975. const tt = {
  976. bracketL: createToken("[", {
  977. beforeExpr,
  978. startsExpr
  979. }),
  980. bracketHashL: createToken("#[", {
  981. beforeExpr,
  982. startsExpr
  983. }),
  984. bracketBarL: createToken("[|", {
  985. beforeExpr,
  986. startsExpr
  987. }),
  988. bracketR: createToken("]"),
  989. bracketBarR: createToken("|]"),
  990. braceL: createToken("{", {
  991. beforeExpr,
  992. startsExpr
  993. }),
  994. braceBarL: createToken("{|", {
  995. beforeExpr,
  996. startsExpr
  997. }),
  998. braceHashL: createToken("#{", {
  999. beforeExpr,
  1000. startsExpr
  1001. }),
  1002. braceR: createToken("}"),
  1003. braceBarR: createToken("|}"),
  1004. parenL: createToken("(", {
  1005. beforeExpr,
  1006. startsExpr
  1007. }),
  1008. parenR: createToken(")"),
  1009. comma: createToken(",", {
  1010. beforeExpr
  1011. }),
  1012. semi: createToken(";", {
  1013. beforeExpr
  1014. }),
  1015. colon: createToken(":", {
  1016. beforeExpr
  1017. }),
  1018. doubleColon: createToken("::", {
  1019. beforeExpr
  1020. }),
  1021. dot: createToken("."),
  1022. question: createToken("?", {
  1023. beforeExpr
  1024. }),
  1025. questionDot: createToken("?."),
  1026. arrow: createToken("=>", {
  1027. beforeExpr
  1028. }),
  1029. template: createToken("template"),
  1030. ellipsis: createToken("...", {
  1031. beforeExpr
  1032. }),
  1033. backQuote: createToken("`", {
  1034. startsExpr
  1035. }),
  1036. dollarBraceL: createToken("${", {
  1037. beforeExpr,
  1038. startsExpr
  1039. }),
  1040. templateTail: createToken("...`", {
  1041. startsExpr
  1042. }),
  1043. templateNonTail: createToken("...${", {
  1044. beforeExpr,
  1045. startsExpr
  1046. }),
  1047. at: createToken("@"),
  1048. hash: createToken("#", {
  1049. startsExpr
  1050. }),
  1051. interpreterDirective: createToken("#!..."),
  1052. eq: createToken("=", {
  1053. beforeExpr,
  1054. isAssign
  1055. }),
  1056. assign: createToken("_=", {
  1057. beforeExpr,
  1058. isAssign
  1059. }),
  1060. slashAssign: createToken("_=", {
  1061. beforeExpr,
  1062. isAssign
  1063. }),
  1064. xorAssign: createToken("_=", {
  1065. beforeExpr,
  1066. isAssign
  1067. }),
  1068. moduloAssign: createToken("_=", {
  1069. beforeExpr,
  1070. isAssign
  1071. }),
  1072. incDec: createToken("++/--", {
  1073. prefix,
  1074. postfix,
  1075. startsExpr
  1076. }),
  1077. bang: createToken("!", {
  1078. beforeExpr,
  1079. prefix,
  1080. startsExpr
  1081. }),
  1082. tilde: createToken("~", {
  1083. beforeExpr,
  1084. prefix,
  1085. startsExpr
  1086. }),
  1087. doubleCaret: createToken("^^", {
  1088. startsExpr
  1089. }),
  1090. doubleAt: createToken("@@", {
  1091. startsExpr
  1092. }),
  1093. pipeline: createBinop("|>", 0),
  1094. nullishCoalescing: createBinop("??", 1),
  1095. logicalOR: createBinop("||", 1),
  1096. logicalAND: createBinop("&&", 2),
  1097. bitwiseOR: createBinop("|", 3),
  1098. bitwiseXOR: createBinop("^", 4),
  1099. bitwiseAND: createBinop("&", 5),
  1100. equality: createBinop("==/!=/===/!==", 6),
  1101. lt: createBinop("</>/<=/>=", 7),
  1102. gt: createBinop("</>/<=/>=", 7),
  1103. relational: createBinop("</>/<=/>=", 7),
  1104. bitShift: createBinop("<</>>/>>>", 8),
  1105. bitShiftL: createBinop("<</>>/>>>", 8),
  1106. bitShiftR: createBinop("<</>>/>>>", 8),
  1107. plusMin: createToken("+/-", {
  1108. beforeExpr,
  1109. binop: 9,
  1110. prefix,
  1111. startsExpr
  1112. }),
  1113. modulo: createToken("%", {
  1114. binop: 10,
  1115. startsExpr
  1116. }),
  1117. star: createToken("*", {
  1118. binop: 10
  1119. }),
  1120. slash: createBinop("/", 10),
  1121. exponent: createToken("**", {
  1122. beforeExpr,
  1123. binop: 11,
  1124. rightAssociative: true
  1125. }),
  1126. _in: createKeyword("in", {
  1127. beforeExpr,
  1128. binop: 7
  1129. }),
  1130. _instanceof: createKeyword("instanceof", {
  1131. beforeExpr,
  1132. binop: 7
  1133. }),
  1134. _break: createKeyword("break"),
  1135. _case: createKeyword("case", {
  1136. beforeExpr
  1137. }),
  1138. _catch: createKeyword("catch"),
  1139. _continue: createKeyword("continue"),
  1140. _debugger: createKeyword("debugger"),
  1141. _default: createKeyword("default", {
  1142. beforeExpr
  1143. }),
  1144. _else: createKeyword("else", {
  1145. beforeExpr
  1146. }),
  1147. _finally: createKeyword("finally"),
  1148. _function: createKeyword("function", {
  1149. startsExpr
  1150. }),
  1151. _if: createKeyword("if"),
  1152. _return: createKeyword("return", {
  1153. beforeExpr
  1154. }),
  1155. _switch: createKeyword("switch"),
  1156. _throw: createKeyword("throw", {
  1157. beforeExpr,
  1158. prefix,
  1159. startsExpr
  1160. }),
  1161. _try: createKeyword("try"),
  1162. _var: createKeyword("var"),
  1163. _const: createKeyword("const"),
  1164. _with: createKeyword("with"),
  1165. _new: createKeyword("new", {
  1166. beforeExpr,
  1167. startsExpr
  1168. }),
  1169. _this: createKeyword("this", {
  1170. startsExpr
  1171. }),
  1172. _super: createKeyword("super", {
  1173. startsExpr
  1174. }),
  1175. _class: createKeyword("class", {
  1176. startsExpr
  1177. }),
  1178. _extends: createKeyword("extends", {
  1179. beforeExpr
  1180. }),
  1181. _export: createKeyword("export"),
  1182. _import: createKeyword("import", {
  1183. startsExpr
  1184. }),
  1185. _null: createKeyword("null", {
  1186. startsExpr
  1187. }),
  1188. _true: createKeyword("true", {
  1189. startsExpr
  1190. }),
  1191. _false: createKeyword("false", {
  1192. startsExpr
  1193. }),
  1194. _typeof: createKeyword("typeof", {
  1195. beforeExpr,
  1196. prefix,
  1197. startsExpr
  1198. }),
  1199. _void: createKeyword("void", {
  1200. beforeExpr,
  1201. prefix,
  1202. startsExpr
  1203. }),
  1204. _delete: createKeyword("delete", {
  1205. beforeExpr,
  1206. prefix,
  1207. startsExpr
  1208. }),
  1209. _do: createKeyword("do", {
  1210. isLoop,
  1211. beforeExpr
  1212. }),
  1213. _for: createKeyword("for", {
  1214. isLoop
  1215. }),
  1216. _while: createKeyword("while", {
  1217. isLoop
  1218. }),
  1219. _as: createKeywordLike("as", {
  1220. startsExpr
  1221. }),
  1222. _assert: createKeywordLike("assert", {
  1223. startsExpr
  1224. }),
  1225. _async: createKeywordLike("async", {
  1226. startsExpr
  1227. }),
  1228. _await: createKeywordLike("await", {
  1229. startsExpr
  1230. }),
  1231. _defer: createKeywordLike("defer", {
  1232. startsExpr
  1233. }),
  1234. _from: createKeywordLike("from", {
  1235. startsExpr
  1236. }),
  1237. _get: createKeywordLike("get", {
  1238. startsExpr
  1239. }),
  1240. _let: createKeywordLike("let", {
  1241. startsExpr
  1242. }),
  1243. _meta: createKeywordLike("meta", {
  1244. startsExpr
  1245. }),
  1246. _of: createKeywordLike("of", {
  1247. startsExpr
  1248. }),
  1249. _sent: createKeywordLike("sent", {
  1250. startsExpr
  1251. }),
  1252. _set: createKeywordLike("set", {
  1253. startsExpr
  1254. }),
  1255. _source: createKeywordLike("source", {
  1256. startsExpr
  1257. }),
  1258. _static: createKeywordLike("static", {
  1259. startsExpr
  1260. }),
  1261. _using: createKeywordLike("using", {
  1262. startsExpr
  1263. }),
  1264. _yield: createKeywordLike("yield", {
  1265. startsExpr
  1266. }),
  1267. _asserts: createKeywordLike("asserts", {
  1268. startsExpr
  1269. }),
  1270. _checks: createKeywordLike("checks", {
  1271. startsExpr
  1272. }),
  1273. _exports: createKeywordLike("exports", {
  1274. startsExpr
  1275. }),
  1276. _global: createKeywordLike("global", {
  1277. startsExpr
  1278. }),
  1279. _implements: createKeywordLike("implements", {
  1280. startsExpr
  1281. }),
  1282. _intrinsic: createKeywordLike("intrinsic", {
  1283. startsExpr
  1284. }),
  1285. _infer: createKeywordLike("infer", {
  1286. startsExpr
  1287. }),
  1288. _is: createKeywordLike("is", {
  1289. startsExpr
  1290. }),
  1291. _mixins: createKeywordLike("mixins", {
  1292. startsExpr
  1293. }),
  1294. _proto: createKeywordLike("proto", {
  1295. startsExpr
  1296. }),
  1297. _require: createKeywordLike("require", {
  1298. startsExpr
  1299. }),
  1300. _satisfies: createKeywordLike("satisfies", {
  1301. startsExpr
  1302. }),
  1303. _keyof: createKeywordLike("keyof", {
  1304. startsExpr
  1305. }),
  1306. _readonly: createKeywordLike("readonly", {
  1307. startsExpr
  1308. }),
  1309. _unique: createKeywordLike("unique", {
  1310. startsExpr
  1311. }),
  1312. _abstract: createKeywordLike("abstract", {
  1313. startsExpr
  1314. }),
  1315. _declare: createKeywordLike("declare", {
  1316. startsExpr
  1317. }),
  1318. _enum: createKeywordLike("enum", {
  1319. startsExpr
  1320. }),
  1321. _module: createKeywordLike("module", {
  1322. startsExpr
  1323. }),
  1324. _namespace: createKeywordLike("namespace", {
  1325. startsExpr
  1326. }),
  1327. _interface: createKeywordLike("interface", {
  1328. startsExpr
  1329. }),
  1330. _type: createKeywordLike("type", {
  1331. startsExpr
  1332. }),
  1333. _opaque: createKeywordLike("opaque", {
  1334. startsExpr
  1335. }),
  1336. name: createToken("name", {
  1337. startsExpr
  1338. }),
  1339. placeholder: createToken("%%", {
  1340. startsExpr: true
  1341. }),
  1342. string: createToken("string", {
  1343. startsExpr
  1344. }),
  1345. num: createToken("num", {
  1346. startsExpr
  1347. }),
  1348. bigint: createToken("bigint", {
  1349. startsExpr
  1350. }),
  1351. decimal: createToken("decimal", {
  1352. startsExpr
  1353. }),
  1354. regexp: createToken("regexp", {
  1355. startsExpr
  1356. }),
  1357. privateName: createToken("#name", {
  1358. startsExpr
  1359. }),
  1360. eof: createToken("eof"),
  1361. jsxName: createToken("jsxName"),
  1362. jsxText: createToken("jsxText", {
  1363. beforeExpr: true
  1364. }),
  1365. jsxTagStart: createToken("jsxTagStart", {
  1366. startsExpr: true
  1367. }),
  1368. jsxTagEnd: createToken("jsxTagEnd")
  1369. };
  1370. function tokenIsIdentifier(token) {
  1371. return token >= 93 && token <= 133;
  1372. }
  1373. function tokenKeywordOrIdentifierIsKeyword(token) {
  1374. return token <= 92;
  1375. }
  1376. function tokenIsKeywordOrIdentifier(token) {
  1377. return token >= 58 && token <= 133;
  1378. }
  1379. function tokenIsLiteralPropertyName(token) {
  1380. return token >= 58 && token <= 137;
  1381. }
  1382. function tokenComesBeforeExpression(token) {
  1383. return tokenBeforeExprs[token];
  1384. }
  1385. function tokenCanStartExpression(token) {
  1386. return tokenStartsExprs[token];
  1387. }
  1388. function tokenIsAssignment(token) {
  1389. return token >= 29 && token <= 33;
  1390. }
  1391. function tokenIsFlowInterfaceOrTypeOrOpaque(token) {
  1392. return token >= 129 && token <= 131;
  1393. }
  1394. function tokenIsLoop(token) {
  1395. return token >= 90 && token <= 92;
  1396. }
  1397. function tokenIsKeyword(token) {
  1398. return token >= 58 && token <= 92;
  1399. }
  1400. function tokenIsOperator(token) {
  1401. return token >= 39 && token <= 59;
  1402. }
  1403. function tokenIsPostfix(token) {
  1404. return token === 34;
  1405. }
  1406. function tokenIsPrefix(token) {
  1407. return tokenPrefixes[token];
  1408. }
  1409. function tokenIsTSTypeOperator(token) {
  1410. return token >= 121 && token <= 123;
  1411. }
  1412. function tokenIsTSDeclarationStart(token) {
  1413. return token >= 124 && token <= 130;
  1414. }
  1415. function tokenLabelName(token) {
  1416. return tokenLabels[token];
  1417. }
  1418. function tokenOperatorPrecedence(token) {
  1419. return tokenBinops[token];
  1420. }
  1421. function tokenIsRightAssociative(token) {
  1422. return token === 57;
  1423. }
  1424. function tokenIsTemplate(token) {
  1425. return token >= 24 && token <= 25;
  1426. }
  1427. function getExportedToken(token) {
  1428. return tokenTypes[token];
  1429. }
  1430. {
  1431. tokenTypes[8].updateContext = context => {
  1432. context.pop();
  1433. };
  1434. tokenTypes[5].updateContext = tokenTypes[7].updateContext = tokenTypes[23].updateContext = context => {
  1435. context.push(types.brace);
  1436. };
  1437. tokenTypes[22].updateContext = context => {
  1438. if (context[context.length - 1] === types.template) {
  1439. context.pop();
  1440. } else {
  1441. context.push(types.template);
  1442. }
  1443. };
  1444. tokenTypes[143].updateContext = context => {
  1445. context.push(types.j_expr, types.j_oTag);
  1446. };
  1447. }
  1448. 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-\u1c8a\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-\ua7cd\ua7d0\ua7d1\ua7d3\ua7d5-\ua7dc\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";
  1449. let nonASCIIidentifierChars = "\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\u0897-\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";
  1450. const nonASCIIidentifierStart = new RegExp("[" + nonASCIIidentifierStartChars + "]");
  1451. const nonASCIIidentifier = new RegExp("[" + nonASCIIidentifierStartChars + nonASCIIidentifierChars + "]");
  1452. nonASCIIidentifierStartChars = nonASCIIidentifierChars = null;
  1453. 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, 4, 51, 13, 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, 39, 27, 10, 22, 251, 41, 7, 1, 17, 2, 60, 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, 31, 9, 2, 0, 3, 0, 2, 37, 2, 0, 26, 0, 2, 0, 45, 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, 200, 32, 32, 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, 26, 3994, 6, 582, 6842, 29, 1763, 568, 8, 30, 18, 78, 18, 29, 19, 47, 17, 3, 32, 20, 6, 18, 433, 44, 212, 63, 129, 74, 6, 0, 67, 12, 65, 1, 2, 0, 29, 6135, 9, 1237, 42, 9, 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, 229, 29, 3, 0, 496, 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];
  1454. 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, 7, 9, 32, 4, 318, 1, 80, 3, 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, 68, 8, 2, 0, 3, 0, 2, 3, 2, 4, 2, 0, 15, 1, 83, 17, 10, 9, 5, 0, 82, 19, 13, 9, 214, 6, 3, 8, 28, 1, 83, 16, 16, 9, 82, 12, 9, 9, 7, 19, 58, 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, 343, 9, 54, 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, 10, 5350, 0, 7, 14, 11465, 27, 2343, 9, 87, 9, 39, 4, 60, 6, 26, 9, 535, 9, 470, 0, 2, 54, 8, 3, 82, 0, 12, 1, 19628, 1, 4178, 9, 519, 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, 245, 1, 2, 9, 726, 6, 110, 6, 6, 9, 4759, 9, 787719, 239];
  1455. function isInAstralSet(code, set) {
  1456. let pos = 0x10000;
  1457. for (let i = 0, length = set.length; i < length; i += 2) {
  1458. pos += set[i];
  1459. if (pos > code) return false;
  1460. pos += set[i + 1];
  1461. if (pos >= code) return true;
  1462. }
  1463. return false;
  1464. }
  1465. function isIdentifierStart(code) {
  1466. if (code < 65) return code === 36;
  1467. if (code <= 90) return true;
  1468. if (code < 97) return code === 95;
  1469. if (code <= 122) return true;
  1470. if (code <= 0xffff) {
  1471. return code >= 0xaa && nonASCIIidentifierStart.test(String.fromCharCode(code));
  1472. }
  1473. return isInAstralSet(code, astralIdentifierStartCodes);
  1474. }
  1475. function isIdentifierChar(code) {
  1476. if (code < 48) return code === 36;
  1477. if (code < 58) return true;
  1478. if (code < 65) return false;
  1479. if (code <= 90) return true;
  1480. if (code < 97) return code === 95;
  1481. if (code <= 122) return true;
  1482. if (code <= 0xffff) {
  1483. return code >= 0xaa && nonASCIIidentifier.test(String.fromCharCode(code));
  1484. }
  1485. return isInAstralSet(code, astralIdentifierStartCodes) || isInAstralSet(code, astralIdentifierCodes);
  1486. }
  1487. const reservedWords = {
  1488. 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"],
  1489. strict: ["implements", "interface", "let", "package", "private", "protected", "public", "static", "yield"],
  1490. strictBind: ["eval", "arguments"]
  1491. };
  1492. const keywords = new Set(reservedWords.keyword);
  1493. const reservedWordsStrictSet = new Set(reservedWords.strict);
  1494. const reservedWordsStrictBindSet = new Set(reservedWords.strictBind);
  1495. function isReservedWord(word, inModule) {
  1496. return inModule && word === "await" || word === "enum";
  1497. }
  1498. function isStrictReservedWord(word, inModule) {
  1499. return isReservedWord(word, inModule) || reservedWordsStrictSet.has(word);
  1500. }
  1501. function isStrictBindOnlyReservedWord(word) {
  1502. return reservedWordsStrictBindSet.has(word);
  1503. }
  1504. function isStrictBindReservedWord(word, inModule) {
  1505. return isStrictReservedWord(word, inModule) || isStrictBindOnlyReservedWord(word);
  1506. }
  1507. function isKeyword(word) {
  1508. return keywords.has(word);
  1509. }
  1510. function isIteratorStart(current, next, next2) {
  1511. return current === 64 && next === 64 && isIdentifierStart(next2);
  1512. }
  1513. 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"]);
  1514. function canBeReservedWord(word) {
  1515. return reservedWordLikeSet.has(word);
  1516. }
  1517. class Scope {
  1518. constructor(flags) {
  1519. this.flags = 0;
  1520. this.names = new Map();
  1521. this.firstLexicalName = "";
  1522. this.flags = flags;
  1523. }
  1524. }
  1525. class ScopeHandler {
  1526. constructor(parser, inModule) {
  1527. this.parser = void 0;
  1528. this.scopeStack = [];
  1529. this.inModule = void 0;
  1530. this.undefinedExports = new Map();
  1531. this.parser = parser;
  1532. this.inModule = inModule;
  1533. }
  1534. get inTopLevel() {
  1535. return (this.currentScope().flags & 1) > 0;
  1536. }
  1537. get inFunction() {
  1538. return (this.currentVarScopeFlags() & 2) > 0;
  1539. }
  1540. get allowSuper() {
  1541. return (this.currentThisScopeFlags() & 16) > 0;
  1542. }
  1543. get allowDirectSuper() {
  1544. return (this.currentThisScopeFlags() & 32) > 0;
  1545. }
  1546. get inClass() {
  1547. return (this.currentThisScopeFlags() & 64) > 0;
  1548. }
  1549. get inClassAndNotInNonArrowFunction() {
  1550. const flags = this.currentThisScopeFlags();
  1551. return (flags & 64) > 0 && (flags & 2) === 0;
  1552. }
  1553. get inStaticBlock() {
  1554. for (let i = this.scopeStack.length - 1;; i--) {
  1555. const {
  1556. flags
  1557. } = this.scopeStack[i];
  1558. if (flags & 128) {
  1559. return true;
  1560. }
  1561. if (flags & (643 | 64)) {
  1562. return false;
  1563. }
  1564. }
  1565. }
  1566. get inNonArrowFunction() {
  1567. return (this.currentThisScopeFlags() & 2) > 0;
  1568. }
  1569. get inBareCaseStatement() {
  1570. return (this.currentScope().flags & 256) > 0;
  1571. }
  1572. get treatFunctionsAsVar() {
  1573. return this.treatFunctionsAsVarInScope(this.currentScope());
  1574. }
  1575. createScope(flags) {
  1576. return new Scope(flags);
  1577. }
  1578. enter(flags) {
  1579. this.scopeStack.push(this.createScope(flags));
  1580. }
  1581. exit() {
  1582. const scope = this.scopeStack.pop();
  1583. return scope.flags;
  1584. }
  1585. treatFunctionsAsVarInScope(scope) {
  1586. return !!(scope.flags & (2 | 128) || !this.parser.inModule && scope.flags & 1);
  1587. }
  1588. declareName(name, bindingType, loc) {
  1589. let scope = this.currentScope();
  1590. if (bindingType & 8 || bindingType & 16) {
  1591. this.checkRedeclarationInScope(scope, name, bindingType, loc);
  1592. let type = scope.names.get(name) || 0;
  1593. if (bindingType & 16) {
  1594. type = type | 4;
  1595. } else {
  1596. if (!scope.firstLexicalName) {
  1597. scope.firstLexicalName = name;
  1598. }
  1599. type = type | 2;
  1600. }
  1601. scope.names.set(name, type);
  1602. if (bindingType & 8) {
  1603. this.maybeExportDefined(scope, name);
  1604. }
  1605. } else if (bindingType & 4) {
  1606. for (let i = this.scopeStack.length - 1; i >= 0; --i) {
  1607. scope = this.scopeStack[i];
  1608. this.checkRedeclarationInScope(scope, name, bindingType, loc);
  1609. scope.names.set(name, (scope.names.get(name) || 0) | 1);
  1610. this.maybeExportDefined(scope, name);
  1611. if (scope.flags & 643) break;
  1612. }
  1613. }
  1614. if (this.parser.inModule && scope.flags & 1) {
  1615. this.undefinedExports.delete(name);
  1616. }
  1617. }
  1618. maybeExportDefined(scope, name) {
  1619. if (this.parser.inModule && scope.flags & 1) {
  1620. this.undefinedExports.delete(name);
  1621. }
  1622. }
  1623. checkRedeclarationInScope(scope, name, bindingType, loc) {
  1624. if (this.isRedeclaredInScope(scope, name, bindingType)) {
  1625. this.parser.raise(Errors.VarRedeclaration, loc, {
  1626. identifierName: name
  1627. });
  1628. }
  1629. }
  1630. isRedeclaredInScope(scope, name, bindingType) {
  1631. if (!(bindingType & 1)) return false;
  1632. if (bindingType & 8) {
  1633. return scope.names.has(name);
  1634. }
  1635. const type = scope.names.get(name);
  1636. if (bindingType & 16) {
  1637. return (type & 2) > 0 || !this.treatFunctionsAsVarInScope(scope) && (type & 1) > 0;
  1638. }
  1639. return (type & 2) > 0 && !(scope.flags & 8 && scope.firstLexicalName === name) || !this.treatFunctionsAsVarInScope(scope) && (type & 4) > 0;
  1640. }
  1641. checkLocalExport(id) {
  1642. const {
  1643. name
  1644. } = id;
  1645. const topLevelScope = this.scopeStack[0];
  1646. if (!topLevelScope.names.has(name)) {
  1647. this.undefinedExports.set(name, id.loc.start);
  1648. }
  1649. }
  1650. currentScope() {
  1651. return this.scopeStack[this.scopeStack.length - 1];
  1652. }
  1653. currentVarScopeFlags() {
  1654. for (let i = this.scopeStack.length - 1;; i--) {
  1655. const {
  1656. flags
  1657. } = this.scopeStack[i];
  1658. if (flags & 643) {
  1659. return flags;
  1660. }
  1661. }
  1662. }
  1663. currentThisScopeFlags() {
  1664. for (let i = this.scopeStack.length - 1;; i--) {
  1665. const {
  1666. flags
  1667. } = this.scopeStack[i];
  1668. if (flags & (643 | 64) && !(flags & 4)) {
  1669. return flags;
  1670. }
  1671. }
  1672. }
  1673. }
  1674. class FlowScope extends Scope {
  1675. constructor(...args) {
  1676. super(...args);
  1677. this.declareFunctions = new Set();
  1678. }
  1679. }
  1680. class FlowScopeHandler extends ScopeHandler {
  1681. createScope(flags) {
  1682. return new FlowScope(flags);
  1683. }
  1684. declareName(name, bindingType, loc) {
  1685. const scope = this.currentScope();
  1686. if (bindingType & 2048) {
  1687. this.checkRedeclarationInScope(scope, name, bindingType, loc);
  1688. this.maybeExportDefined(scope, name);
  1689. scope.declareFunctions.add(name);
  1690. return;
  1691. }
  1692. super.declareName(name, bindingType, loc);
  1693. }
  1694. isRedeclaredInScope(scope, name, bindingType) {
  1695. if (super.isRedeclaredInScope(scope, name, bindingType)) return true;
  1696. if (bindingType & 2048 && !scope.declareFunctions.has(name)) {
  1697. const type = scope.names.get(name);
  1698. return (type & 4) > 0 || (type & 2) > 0;
  1699. }
  1700. return false;
  1701. }
  1702. checkLocalExport(id) {
  1703. if (!this.scopeStack[0].declareFunctions.has(id.name)) {
  1704. super.checkLocalExport(id);
  1705. }
  1706. }
  1707. }
  1708. const reservedTypes = new Set(["_", "any", "bool", "boolean", "empty", "extends", "false", "interface", "mixed", "null", "number", "static", "string", "true", "typeof", "void"]);
  1709. const FlowErrors = ParseErrorEnum`flow`({
  1710. AmbiguousConditionalArrow: "Ambiguous expression: wrap the arrow functions in parentheses to disambiguate.",
  1711. 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.",
  1712. AssignReservedType: ({
  1713. reservedType
  1714. }) => `Cannot overwrite reserved type ${reservedType}.`,
  1715. DeclareClassElement: "The `declare` modifier can only appear on class fields.",
  1716. DeclareClassFieldInitializer: "Initializers are not allowed in fields with the `declare` modifier.",
  1717. DuplicateDeclareModuleExports: "Duplicate `declare module.exports` statement.",
  1718. EnumBooleanMemberNotInitialized: ({
  1719. memberName,
  1720. enumName
  1721. }) => `Boolean enum members need to be initialized. Use either \`${memberName} = true,\` or \`${memberName} = false,\` in enum \`${enumName}\`.`,
  1722. EnumDuplicateMemberName: ({
  1723. memberName,
  1724. enumName
  1725. }) => `Enum member names need to be unique, but the name \`${memberName}\` has already been used before in enum \`${enumName}\`.`,
  1726. EnumInconsistentMemberValues: ({
  1727. enumName
  1728. }) => `Enum \`${enumName}\` has inconsistent member initializers. Either use no initializers, or consistently use literals (either booleans, numbers, or strings) for all member initializers.`,
  1729. EnumInvalidExplicitType: ({
  1730. invalidEnumType,
  1731. enumName
  1732. }) => `Enum type \`${invalidEnumType}\` is not valid. Use one of \`boolean\`, \`number\`, \`string\`, or \`symbol\` in enum \`${enumName}\`.`,
  1733. EnumInvalidExplicitTypeUnknownSupplied: ({
  1734. enumName
  1735. }) => `Supplied enum type is not valid. Use one of \`boolean\`, \`number\`, \`string\`, or \`symbol\` in enum \`${enumName}\`.`,
  1736. EnumInvalidMemberInitializerPrimaryType: ({
  1737. enumName,
  1738. memberName,
  1739. explicitType
  1740. }) => `Enum \`${enumName}\` has type \`${explicitType}\`, so the initializer of \`${memberName}\` needs to be a ${explicitType} literal.`,
  1741. EnumInvalidMemberInitializerSymbolType: ({
  1742. enumName,
  1743. memberName
  1744. }) => `Symbol enum members cannot be initialized. Use \`${memberName},\` in enum \`${enumName}\`.`,
  1745. EnumInvalidMemberInitializerUnknownType: ({
  1746. enumName,
  1747. memberName
  1748. }) => `The enum member initializer for \`${memberName}\` needs to be a literal (either a boolean, number, or string) in enum \`${enumName}\`.`,
  1749. EnumInvalidMemberName: ({
  1750. enumName,
  1751. memberName,
  1752. suggestion
  1753. }) => `Enum member names cannot start with lowercase 'a' through 'z'. Instead of using \`${memberName}\`, consider using \`${suggestion}\`, in enum \`${enumName}\`.`,
  1754. EnumNumberMemberNotInitialized: ({
  1755. enumName,
  1756. memberName
  1757. }) => `Number enum members need to be initialized, e.g. \`${memberName} = 1\` in enum \`${enumName}\`.`,
  1758. EnumStringMemberInconsistentlyInitialized: ({
  1759. enumName
  1760. }) => `String enum members need to consistently either all use initializers, or use no initializers, in enum \`${enumName}\`.`,
  1761. GetterMayNotHaveThisParam: "A getter cannot have a `this` parameter.",
  1762. ImportReflectionHasImportType: "An `import module` declaration can not use `type` or `typeof` keyword.",
  1763. 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.",
  1764. InexactInsideExact: "Explicit inexact syntax cannot appear inside an explicit exact object type.",
  1765. InexactInsideNonObject: "Explicit inexact syntax cannot appear in class or interface definitions.",
  1766. InexactVariance: "Explicit inexact syntax cannot have variance.",
  1767. InvalidNonTypeImportInDeclareModule: "Imports within a `declare module` body must always be `import type` or `import typeof`.",
  1768. MissingTypeParamDefault: "Type parameter declaration needs a default, since a preceding type parameter declaration has a default.",
  1769. NestedDeclareModule: "`declare module` cannot be used inside another `declare module`.",
  1770. NestedFlowComment: "Cannot have a flow comment inside another flow comment.",
  1771. PatternIsOptional: Object.assign({
  1772. message: "A binding pattern parameter cannot be optional in an implementation signature."
  1773. }, {
  1774. reasonCode: "OptionalBindingPattern"
  1775. }),
  1776. SetterMayNotHaveThisParam: "A setter cannot have a `this` parameter.",
  1777. SpreadVariance: "Spread properties cannot have variance.",
  1778. ThisParamAnnotationRequired: "A type annotation is required for the `this` parameter.",
  1779. ThisParamBannedInConstructor: "Constructors cannot have a `this` parameter; constructors don't bind `this` like other functions.",
  1780. ThisParamMayNotBeOptional: "The `this` parameter cannot be optional.",
  1781. ThisParamMustBeFirst: "The `this` parameter must be the first function parameter.",
  1782. ThisParamNoDefault: "The `this` parameter may not have a default value.",
  1783. TypeBeforeInitializer: "Type annotations must come before default assignments, e.g. instead of `age = 25: number` use `age: number = 25`.",
  1784. TypeCastInPattern: "The type cast expression is expected to be wrapped with parenthesis.",
  1785. UnexpectedExplicitInexactInObject: "Explicit inexact syntax must appear at the end of an inexact object.",
  1786. UnexpectedReservedType: ({
  1787. reservedType
  1788. }) => `Unexpected reserved type ${reservedType}.`,
  1789. UnexpectedReservedUnderscore: "`_` is only allowed as a type argument to call or new.",
  1790. UnexpectedSpaceBetweenModuloChecks: "Spaces between `%` and `checks` are not allowed here.",
  1791. UnexpectedSpreadType: "Spread operator cannot appear in class or interface definitions.",
  1792. UnexpectedSubtractionOperand: 'Unexpected token, expected "number" or "bigint".',
  1793. UnexpectedTokenAfterTypeParameter: "Expected an arrow function after this type parameter declaration.",
  1794. UnexpectedTypeParameterBeforeAsyncArrowFunction: "Type parameters must come after the async keyword, e.g. instead of `<T> async () => {}`, use `async <T>() => {}`.",
  1795. UnsupportedDeclareExportKind: ({
  1796. unsupportedExportKind,
  1797. suggestion
  1798. }) => `\`declare export ${unsupportedExportKind}\` is not supported. Use \`${suggestion}\` instead.`,
  1799. UnsupportedStatementInDeclareModule: "Only declares and type imports are allowed inside declare module.",
  1800. UnterminatedFlowComment: "Unterminated flow-comment."
  1801. });
  1802. function isEsModuleType(bodyElement) {
  1803. return bodyElement.type === "DeclareExportAllDeclaration" || bodyElement.type === "DeclareExportDeclaration" && (!bodyElement.declaration || bodyElement.declaration.type !== "TypeAlias" && bodyElement.declaration.type !== "InterfaceDeclaration");
  1804. }
  1805. function hasTypeImportKind(node) {
  1806. return node.importKind === "type" || node.importKind === "typeof";
  1807. }
  1808. const exportSuggestions = {
  1809. const: "declare export var",
  1810. let: "declare export var",
  1811. type: "export type",
  1812. interface: "export interface"
  1813. };
  1814. function partition(list, test) {
  1815. const list1 = [];
  1816. const list2 = [];
  1817. for (let i = 0; i < list.length; i++) {
  1818. (test(list[i], i, list) ? list1 : list2).push(list[i]);
  1819. }
  1820. return [list1, list2];
  1821. }
  1822. const FLOW_PRAGMA_REGEX = /\*?\s*@((?:no)?flow)\b/;
  1823. var flow = superClass => class FlowParserMixin extends superClass {
  1824. constructor(...args) {
  1825. super(...args);
  1826. this.flowPragma = undefined;
  1827. }
  1828. getScopeHandler() {
  1829. return FlowScopeHandler;
  1830. }
  1831. shouldParseTypes() {
  1832. return this.getPluginOption("flow", "all") || this.flowPragma === "flow";
  1833. }
  1834. finishToken(type, val) {
  1835. if (type !== 134 && type !== 13 && type !== 28) {
  1836. if (this.flowPragma === undefined) {
  1837. this.flowPragma = null;
  1838. }
  1839. }
  1840. super.finishToken(type, val);
  1841. }
  1842. addComment(comment) {
  1843. if (this.flowPragma === undefined) {
  1844. const matches = FLOW_PRAGMA_REGEX.exec(comment.value);
  1845. if (!matches) ;else if (matches[1] === "flow") {
  1846. this.flowPragma = "flow";
  1847. } else if (matches[1] === "noflow") {
  1848. this.flowPragma = "noflow";
  1849. } else {
  1850. throw new Error("Unexpected flow pragma");
  1851. }
  1852. }
  1853. super.addComment(comment);
  1854. }
  1855. flowParseTypeInitialiser(tok) {
  1856. const oldInType = this.state.inType;
  1857. this.state.inType = true;
  1858. this.expect(tok || 14);
  1859. const type = this.flowParseType();
  1860. this.state.inType = oldInType;
  1861. return type;
  1862. }
  1863. flowParsePredicate() {
  1864. const node = this.startNode();
  1865. const moduloLoc = this.state.startLoc;
  1866. this.next();
  1867. this.expectContextual(110);
  1868. if (this.state.lastTokStartLoc.index > moduloLoc.index + 1) {
  1869. this.raise(FlowErrors.UnexpectedSpaceBetweenModuloChecks, moduloLoc);
  1870. }
  1871. if (this.eat(10)) {
  1872. node.value = super.parseExpression();
  1873. this.expect(11);
  1874. return this.finishNode(node, "DeclaredPredicate");
  1875. } else {
  1876. return this.finishNode(node, "InferredPredicate");
  1877. }
  1878. }
  1879. flowParseTypeAndPredicateInitialiser() {
  1880. const oldInType = this.state.inType;
  1881. this.state.inType = true;
  1882. this.expect(14);
  1883. let type = null;
  1884. let predicate = null;
  1885. if (this.match(54)) {
  1886. this.state.inType = oldInType;
  1887. predicate = this.flowParsePredicate();
  1888. } else {
  1889. type = this.flowParseType();
  1890. this.state.inType = oldInType;
  1891. if (this.match(54)) {
  1892. predicate = this.flowParsePredicate();
  1893. }
  1894. }
  1895. return [type, predicate];
  1896. }
  1897. flowParseDeclareClass(node) {
  1898. this.next();
  1899. this.flowParseInterfaceish(node, true);
  1900. return this.finishNode(node, "DeclareClass");
  1901. }
  1902. flowParseDeclareFunction(node) {
  1903. this.next();
  1904. const id = node.id = this.parseIdentifier();
  1905. const typeNode = this.startNode();
  1906. const typeContainer = this.startNode();
  1907. if (this.match(47)) {
  1908. typeNode.typeParameters = this.flowParseTypeParameterDeclaration();
  1909. } else {
  1910. typeNode.typeParameters = null;
  1911. }
  1912. this.expect(10);
  1913. const tmp = this.flowParseFunctionTypeParams();
  1914. typeNode.params = tmp.params;
  1915. typeNode.rest = tmp.rest;
  1916. typeNode.this = tmp._this;
  1917. this.expect(11);
  1918. [typeNode.returnType, node.predicate] = this.flowParseTypeAndPredicateInitialiser();
  1919. typeContainer.typeAnnotation = this.finishNode(typeNode, "FunctionTypeAnnotation");
  1920. id.typeAnnotation = this.finishNode(typeContainer, "TypeAnnotation");
  1921. this.resetEndLocation(id);
  1922. this.semicolon();
  1923. this.scope.declareName(node.id.name, 2048, node.id.loc.start);
  1924. return this.finishNode(node, "DeclareFunction");
  1925. }
  1926. flowParseDeclare(node, insideModule) {
  1927. if (this.match(80)) {
  1928. return this.flowParseDeclareClass(node);
  1929. } else if (this.match(68)) {
  1930. return this.flowParseDeclareFunction(node);
  1931. } else if (this.match(74)) {
  1932. return this.flowParseDeclareVariable(node);
  1933. } else if (this.eatContextual(127)) {
  1934. if (this.match(16)) {
  1935. return this.flowParseDeclareModuleExports(node);
  1936. } else {
  1937. if (insideModule) {
  1938. this.raise(FlowErrors.NestedDeclareModule, this.state.lastTokStartLoc);
  1939. }
  1940. return this.flowParseDeclareModule(node);
  1941. }
  1942. } else if (this.isContextual(130)) {
  1943. return this.flowParseDeclareTypeAlias(node);
  1944. } else if (this.isContextual(131)) {
  1945. return this.flowParseDeclareOpaqueType(node);
  1946. } else if (this.isContextual(129)) {
  1947. return this.flowParseDeclareInterface(node);
  1948. } else if (this.match(82)) {
  1949. return this.flowParseDeclareExportDeclaration(node, insideModule);
  1950. } else {
  1951. this.unexpected();
  1952. }
  1953. }
  1954. flowParseDeclareVariable(node) {
  1955. this.next();
  1956. node.id = this.flowParseTypeAnnotatableIdentifier(true);
  1957. this.scope.declareName(node.id.name, 5, node.id.loc.start);
  1958. this.semicolon();
  1959. return this.finishNode(node, "DeclareVariable");
  1960. }
  1961. flowParseDeclareModule(node) {
  1962. this.scope.enter(0);
  1963. if (this.match(134)) {
  1964. node.id = super.parseExprAtom();
  1965. } else {
  1966. node.id = this.parseIdentifier();
  1967. }
  1968. const bodyNode = node.body = this.startNode();
  1969. const body = bodyNode.body = [];
  1970. this.expect(5);
  1971. while (!this.match(8)) {
  1972. let bodyNode = this.startNode();
  1973. if (this.match(83)) {
  1974. this.next();
  1975. if (!this.isContextual(130) && !this.match(87)) {
  1976. this.raise(FlowErrors.InvalidNonTypeImportInDeclareModule, this.state.lastTokStartLoc);
  1977. }
  1978. super.parseImport(bodyNode);
  1979. } else {
  1980. this.expectContextual(125, FlowErrors.UnsupportedStatementInDeclareModule);
  1981. bodyNode = this.flowParseDeclare(bodyNode, true);
  1982. }
  1983. body.push(bodyNode);
  1984. }
  1985. this.scope.exit();
  1986. this.expect(8);
  1987. this.finishNode(bodyNode, "BlockStatement");
  1988. let kind = null;
  1989. let hasModuleExport = false;
  1990. body.forEach(bodyElement => {
  1991. if (isEsModuleType(bodyElement)) {
  1992. if (kind === "CommonJS") {
  1993. this.raise(FlowErrors.AmbiguousDeclareModuleKind, bodyElement);
  1994. }
  1995. kind = "ES";
  1996. } else if (bodyElement.type === "DeclareModuleExports") {
  1997. if (hasModuleExport) {
  1998. this.raise(FlowErrors.DuplicateDeclareModuleExports, bodyElement);
  1999. }
  2000. if (kind === "ES") {
  2001. this.raise(FlowErrors.AmbiguousDeclareModuleKind, bodyElement);
  2002. }
  2003. kind = "CommonJS";
  2004. hasModuleExport = true;
  2005. }
  2006. });
  2007. node.kind = kind || "CommonJS";
  2008. return this.finishNode(node, "DeclareModule");
  2009. }
  2010. flowParseDeclareExportDeclaration(node, insideModule) {
  2011. this.expect(82);
  2012. if (this.eat(65)) {
  2013. if (this.match(68) || this.match(80)) {
  2014. node.declaration = this.flowParseDeclare(this.startNode());
  2015. } else {
  2016. node.declaration = this.flowParseType();
  2017. this.semicolon();
  2018. }
  2019. node.default = true;
  2020. return this.finishNode(node, "DeclareExportDeclaration");
  2021. } else {
  2022. if (this.match(75) || this.isLet() || (this.isContextual(130) || this.isContextual(129)) && !insideModule) {
  2023. const label = this.state.value;
  2024. throw this.raise(FlowErrors.UnsupportedDeclareExportKind, this.state.startLoc, {
  2025. unsupportedExportKind: label,
  2026. suggestion: exportSuggestions[label]
  2027. });
  2028. }
  2029. if (this.match(74) || this.match(68) || this.match(80) || this.isContextual(131)) {
  2030. node.declaration = this.flowParseDeclare(this.startNode());
  2031. node.default = false;
  2032. return this.finishNode(node, "DeclareExportDeclaration");
  2033. } else if (this.match(55) || this.match(5) || this.isContextual(129) || this.isContextual(130) || this.isContextual(131)) {
  2034. node = this.parseExport(node, null);
  2035. if (node.type === "ExportNamedDeclaration") {
  2036. node.default = false;
  2037. delete node.exportKind;
  2038. return this.castNodeTo(node, "DeclareExportDeclaration");
  2039. } else {
  2040. return this.castNodeTo(node, "DeclareExportAllDeclaration");
  2041. }
  2042. }
  2043. }
  2044. this.unexpected();
  2045. }
  2046. flowParseDeclareModuleExports(node) {
  2047. this.next();
  2048. this.expectContextual(111);
  2049. node.typeAnnotation = this.flowParseTypeAnnotation();
  2050. this.semicolon();
  2051. return this.finishNode(node, "DeclareModuleExports");
  2052. }
  2053. flowParseDeclareTypeAlias(node) {
  2054. this.next();
  2055. const finished = this.flowParseTypeAlias(node);
  2056. this.castNodeTo(finished, "DeclareTypeAlias");
  2057. return finished;
  2058. }
  2059. flowParseDeclareOpaqueType(node) {
  2060. this.next();
  2061. const finished = this.flowParseOpaqueType(node, true);
  2062. this.castNodeTo(finished, "DeclareOpaqueType");
  2063. return finished;
  2064. }
  2065. flowParseDeclareInterface(node) {
  2066. this.next();
  2067. this.flowParseInterfaceish(node, false);
  2068. return this.finishNode(node, "DeclareInterface");
  2069. }
  2070. flowParseInterfaceish(node, isClass) {
  2071. node.id = this.flowParseRestrictedIdentifier(!isClass, true);
  2072. this.scope.declareName(node.id.name, isClass ? 17 : 8201, node.id.loc.start);
  2073. if (this.match(47)) {
  2074. node.typeParameters = this.flowParseTypeParameterDeclaration();
  2075. } else {
  2076. node.typeParameters = null;
  2077. }
  2078. node.extends = [];
  2079. if (this.eat(81)) {
  2080. do {
  2081. node.extends.push(this.flowParseInterfaceExtends());
  2082. } while (!isClass && this.eat(12));
  2083. }
  2084. if (isClass) {
  2085. node.implements = [];
  2086. node.mixins = [];
  2087. if (this.eatContextual(117)) {
  2088. do {
  2089. node.mixins.push(this.flowParseInterfaceExtends());
  2090. } while (this.eat(12));
  2091. }
  2092. if (this.eatContextual(113)) {
  2093. do {
  2094. node.implements.push(this.flowParseInterfaceExtends());
  2095. } while (this.eat(12));
  2096. }
  2097. }
  2098. node.body = this.flowParseObjectType({
  2099. allowStatic: isClass,
  2100. allowExact: false,
  2101. allowSpread: false,
  2102. allowProto: isClass,
  2103. allowInexact: false
  2104. });
  2105. }
  2106. flowParseInterfaceExtends() {
  2107. const node = this.startNode();
  2108. node.id = this.flowParseQualifiedTypeIdentifier();
  2109. if (this.match(47)) {
  2110. node.typeParameters = this.flowParseTypeParameterInstantiation();
  2111. } else {
  2112. node.typeParameters = null;
  2113. }
  2114. return this.finishNode(node, "InterfaceExtends");
  2115. }
  2116. flowParseInterface(node) {
  2117. this.flowParseInterfaceish(node, false);
  2118. return this.finishNode(node, "InterfaceDeclaration");
  2119. }
  2120. checkNotUnderscore(word) {
  2121. if (word === "_") {
  2122. this.raise(FlowErrors.UnexpectedReservedUnderscore, this.state.startLoc);
  2123. }
  2124. }
  2125. checkReservedType(word, startLoc, declaration) {
  2126. if (!reservedTypes.has(word)) return;
  2127. this.raise(declaration ? FlowErrors.AssignReservedType : FlowErrors.UnexpectedReservedType, startLoc, {
  2128. reservedType: word
  2129. });
  2130. }
  2131. flowParseRestrictedIdentifier(liberal, declaration) {
  2132. this.checkReservedType(this.state.value, this.state.startLoc, declaration);
  2133. return this.parseIdentifier(liberal);
  2134. }
  2135. flowParseTypeAlias(node) {
  2136. node.id = this.flowParseRestrictedIdentifier(false, true);
  2137. this.scope.declareName(node.id.name, 8201, node.id.loc.start);
  2138. if (this.match(47)) {
  2139. node.typeParameters = this.flowParseTypeParameterDeclaration();
  2140. } else {
  2141. node.typeParameters = null;
  2142. }
  2143. node.right = this.flowParseTypeInitialiser(29);
  2144. this.semicolon();
  2145. return this.finishNode(node, "TypeAlias");
  2146. }
  2147. flowParseOpaqueType(node, declare) {
  2148. this.expectContextual(130);
  2149. node.id = this.flowParseRestrictedIdentifier(true, true);
  2150. this.scope.declareName(node.id.name, 8201, node.id.loc.start);
  2151. if (this.match(47)) {
  2152. node.typeParameters = this.flowParseTypeParameterDeclaration();
  2153. } else {
  2154. node.typeParameters = null;
  2155. }
  2156. node.supertype = null;
  2157. if (this.match(14)) {
  2158. node.supertype = this.flowParseTypeInitialiser(14);
  2159. }
  2160. node.impltype = null;
  2161. if (!declare) {
  2162. node.impltype = this.flowParseTypeInitialiser(29);
  2163. }
  2164. this.semicolon();
  2165. return this.finishNode(node, "OpaqueType");
  2166. }
  2167. flowParseTypeParameter(requireDefault = false) {
  2168. const nodeStartLoc = this.state.startLoc;
  2169. const node = this.startNode();
  2170. const variance = this.flowParseVariance();
  2171. const ident = this.flowParseTypeAnnotatableIdentifier();
  2172. node.name = ident.name;
  2173. node.variance = variance;
  2174. node.bound = ident.typeAnnotation;
  2175. if (this.match(29)) {
  2176. this.eat(29);
  2177. node.default = this.flowParseType();
  2178. } else {
  2179. if (requireDefault) {
  2180. this.raise(FlowErrors.MissingTypeParamDefault, nodeStartLoc);
  2181. }
  2182. }
  2183. return this.finishNode(node, "TypeParameter");
  2184. }
  2185. flowParseTypeParameterDeclaration() {
  2186. const oldInType = this.state.inType;
  2187. const node = this.startNode();
  2188. node.params = [];
  2189. this.state.inType = true;
  2190. if (this.match(47) || this.match(143)) {
  2191. this.next();
  2192. } else {
  2193. this.unexpected();
  2194. }
  2195. let defaultRequired = false;
  2196. do {
  2197. const typeParameter = this.flowParseTypeParameter(defaultRequired);
  2198. node.params.push(typeParameter);
  2199. if (typeParameter.default) {
  2200. defaultRequired = true;
  2201. }
  2202. if (!this.match(48)) {
  2203. this.expect(12);
  2204. }
  2205. } while (!this.match(48));
  2206. this.expect(48);
  2207. this.state.inType = oldInType;
  2208. return this.finishNode(node, "TypeParameterDeclaration");
  2209. }
  2210. flowInTopLevelContext(cb) {
  2211. if (this.curContext() !== types.brace) {
  2212. const oldContext = this.state.context;
  2213. this.state.context = [oldContext[0]];
  2214. try {
  2215. return cb();
  2216. } finally {
  2217. this.state.context = oldContext;
  2218. }
  2219. } else {
  2220. return cb();
  2221. }
  2222. }
  2223. flowParseTypeParameterInstantiationInExpression() {
  2224. if (this.reScan_lt() !== 47) return;
  2225. return this.flowParseTypeParameterInstantiation();
  2226. }
  2227. flowParseTypeParameterInstantiation() {
  2228. const node = this.startNode();
  2229. const oldInType = this.state.inType;
  2230. this.state.inType = true;
  2231. node.params = [];
  2232. this.flowInTopLevelContext(() => {
  2233. this.expect(47);
  2234. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  2235. this.state.noAnonFunctionType = false;
  2236. while (!this.match(48)) {
  2237. node.params.push(this.flowParseType());
  2238. if (!this.match(48)) {
  2239. this.expect(12);
  2240. }
  2241. }
  2242. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  2243. });
  2244. this.state.inType = oldInType;
  2245. if (!this.state.inType && this.curContext() === types.brace) {
  2246. this.reScan_lt_gt();
  2247. }
  2248. this.expect(48);
  2249. return this.finishNode(node, "TypeParameterInstantiation");
  2250. }
  2251. flowParseTypeParameterInstantiationCallOrNew() {
  2252. if (this.reScan_lt() !== 47) return;
  2253. const node = this.startNode();
  2254. const oldInType = this.state.inType;
  2255. node.params = [];
  2256. this.state.inType = true;
  2257. this.expect(47);
  2258. while (!this.match(48)) {
  2259. node.params.push(this.flowParseTypeOrImplicitInstantiation());
  2260. if (!this.match(48)) {
  2261. this.expect(12);
  2262. }
  2263. }
  2264. this.expect(48);
  2265. this.state.inType = oldInType;
  2266. return this.finishNode(node, "TypeParameterInstantiation");
  2267. }
  2268. flowParseInterfaceType() {
  2269. const node = this.startNode();
  2270. this.expectContextual(129);
  2271. node.extends = [];
  2272. if (this.eat(81)) {
  2273. do {
  2274. node.extends.push(this.flowParseInterfaceExtends());
  2275. } while (this.eat(12));
  2276. }
  2277. node.body = this.flowParseObjectType({
  2278. allowStatic: false,
  2279. allowExact: false,
  2280. allowSpread: false,
  2281. allowProto: false,
  2282. allowInexact: false
  2283. });
  2284. return this.finishNode(node, "InterfaceTypeAnnotation");
  2285. }
  2286. flowParseObjectPropertyKey() {
  2287. return this.match(135) || this.match(134) ? super.parseExprAtom() : this.parseIdentifier(true);
  2288. }
  2289. flowParseObjectTypeIndexer(node, isStatic, variance) {
  2290. node.static = isStatic;
  2291. if (this.lookahead().type === 14) {
  2292. node.id = this.flowParseObjectPropertyKey();
  2293. node.key = this.flowParseTypeInitialiser();
  2294. } else {
  2295. node.id = null;
  2296. node.key = this.flowParseType();
  2297. }
  2298. this.expect(3);
  2299. node.value = this.flowParseTypeInitialiser();
  2300. node.variance = variance;
  2301. return this.finishNode(node, "ObjectTypeIndexer");
  2302. }
  2303. flowParseObjectTypeInternalSlot(node, isStatic) {
  2304. node.static = isStatic;
  2305. node.id = this.flowParseObjectPropertyKey();
  2306. this.expect(3);
  2307. this.expect(3);
  2308. if (this.match(47) || this.match(10)) {
  2309. node.method = true;
  2310. node.optional = false;
  2311. node.value = this.flowParseObjectTypeMethodish(this.startNodeAt(node.loc.start));
  2312. } else {
  2313. node.method = false;
  2314. if (this.eat(17)) {
  2315. node.optional = true;
  2316. }
  2317. node.value = this.flowParseTypeInitialiser();
  2318. }
  2319. return this.finishNode(node, "ObjectTypeInternalSlot");
  2320. }
  2321. flowParseObjectTypeMethodish(node) {
  2322. node.params = [];
  2323. node.rest = null;
  2324. node.typeParameters = null;
  2325. node.this = null;
  2326. if (this.match(47)) {
  2327. node.typeParameters = this.flowParseTypeParameterDeclaration();
  2328. }
  2329. this.expect(10);
  2330. if (this.match(78)) {
  2331. node.this = this.flowParseFunctionTypeParam(true);
  2332. node.this.name = null;
  2333. if (!this.match(11)) {
  2334. this.expect(12);
  2335. }
  2336. }
  2337. while (!this.match(11) && !this.match(21)) {
  2338. node.params.push(this.flowParseFunctionTypeParam(false));
  2339. if (!this.match(11)) {
  2340. this.expect(12);
  2341. }
  2342. }
  2343. if (this.eat(21)) {
  2344. node.rest = this.flowParseFunctionTypeParam(false);
  2345. }
  2346. this.expect(11);
  2347. node.returnType = this.flowParseTypeInitialiser();
  2348. return this.finishNode(node, "FunctionTypeAnnotation");
  2349. }
  2350. flowParseObjectTypeCallProperty(node, isStatic) {
  2351. const valueNode = this.startNode();
  2352. node.static = isStatic;
  2353. node.value = this.flowParseObjectTypeMethodish(valueNode);
  2354. return this.finishNode(node, "ObjectTypeCallProperty");
  2355. }
  2356. flowParseObjectType({
  2357. allowStatic,
  2358. allowExact,
  2359. allowSpread,
  2360. allowProto,
  2361. allowInexact
  2362. }) {
  2363. const oldInType = this.state.inType;
  2364. this.state.inType = true;
  2365. const nodeStart = this.startNode();
  2366. nodeStart.callProperties = [];
  2367. nodeStart.properties = [];
  2368. nodeStart.indexers = [];
  2369. nodeStart.internalSlots = [];
  2370. let endDelim;
  2371. let exact;
  2372. let inexact = false;
  2373. if (allowExact && this.match(6)) {
  2374. this.expect(6);
  2375. endDelim = 9;
  2376. exact = true;
  2377. } else {
  2378. this.expect(5);
  2379. endDelim = 8;
  2380. exact = false;
  2381. }
  2382. nodeStart.exact = exact;
  2383. while (!this.match(endDelim)) {
  2384. let isStatic = false;
  2385. let protoStartLoc = null;
  2386. let inexactStartLoc = null;
  2387. const node = this.startNode();
  2388. if (allowProto && this.isContextual(118)) {
  2389. const lookahead = this.lookahead();
  2390. if (lookahead.type !== 14 && lookahead.type !== 17) {
  2391. this.next();
  2392. protoStartLoc = this.state.startLoc;
  2393. allowStatic = false;
  2394. }
  2395. }
  2396. if (allowStatic && this.isContextual(106)) {
  2397. const lookahead = this.lookahead();
  2398. if (lookahead.type !== 14 && lookahead.type !== 17) {
  2399. this.next();
  2400. isStatic = true;
  2401. }
  2402. }
  2403. const variance = this.flowParseVariance();
  2404. if (this.eat(0)) {
  2405. if (protoStartLoc != null) {
  2406. this.unexpected(protoStartLoc);
  2407. }
  2408. if (this.eat(0)) {
  2409. if (variance) {
  2410. this.unexpected(variance.loc.start);
  2411. }
  2412. nodeStart.internalSlots.push(this.flowParseObjectTypeInternalSlot(node, isStatic));
  2413. } else {
  2414. nodeStart.indexers.push(this.flowParseObjectTypeIndexer(node, isStatic, variance));
  2415. }
  2416. } else if (this.match(10) || this.match(47)) {
  2417. if (protoStartLoc != null) {
  2418. this.unexpected(protoStartLoc);
  2419. }
  2420. if (variance) {
  2421. this.unexpected(variance.loc.start);
  2422. }
  2423. nodeStart.callProperties.push(this.flowParseObjectTypeCallProperty(node, isStatic));
  2424. } else {
  2425. let kind = "init";
  2426. if (this.isContextual(99) || this.isContextual(104)) {
  2427. const lookahead = this.lookahead();
  2428. if (tokenIsLiteralPropertyName(lookahead.type)) {
  2429. kind = this.state.value;
  2430. this.next();
  2431. }
  2432. }
  2433. const propOrInexact = this.flowParseObjectTypeProperty(node, isStatic, protoStartLoc, variance, kind, allowSpread, allowInexact != null ? allowInexact : !exact);
  2434. if (propOrInexact === null) {
  2435. inexact = true;
  2436. inexactStartLoc = this.state.lastTokStartLoc;
  2437. } else {
  2438. nodeStart.properties.push(propOrInexact);
  2439. }
  2440. }
  2441. this.flowObjectTypeSemicolon();
  2442. if (inexactStartLoc && !this.match(8) && !this.match(9)) {
  2443. this.raise(FlowErrors.UnexpectedExplicitInexactInObject, inexactStartLoc);
  2444. }
  2445. }
  2446. this.expect(endDelim);
  2447. if (allowSpread) {
  2448. nodeStart.inexact = inexact;
  2449. }
  2450. const out = this.finishNode(nodeStart, "ObjectTypeAnnotation");
  2451. this.state.inType = oldInType;
  2452. return out;
  2453. }
  2454. flowParseObjectTypeProperty(node, isStatic, protoStartLoc, variance, kind, allowSpread, allowInexact) {
  2455. if (this.eat(21)) {
  2456. const isInexactToken = this.match(12) || this.match(13) || this.match(8) || this.match(9);
  2457. if (isInexactToken) {
  2458. if (!allowSpread) {
  2459. this.raise(FlowErrors.InexactInsideNonObject, this.state.lastTokStartLoc);
  2460. } else if (!allowInexact) {
  2461. this.raise(FlowErrors.InexactInsideExact, this.state.lastTokStartLoc);
  2462. }
  2463. if (variance) {
  2464. this.raise(FlowErrors.InexactVariance, variance);
  2465. }
  2466. return null;
  2467. }
  2468. if (!allowSpread) {
  2469. this.raise(FlowErrors.UnexpectedSpreadType, this.state.lastTokStartLoc);
  2470. }
  2471. if (protoStartLoc != null) {
  2472. this.unexpected(protoStartLoc);
  2473. }
  2474. if (variance) {
  2475. this.raise(FlowErrors.SpreadVariance, variance);
  2476. }
  2477. node.argument = this.flowParseType();
  2478. return this.finishNode(node, "ObjectTypeSpreadProperty");
  2479. } else {
  2480. node.key = this.flowParseObjectPropertyKey();
  2481. node.static = isStatic;
  2482. node.proto = protoStartLoc != null;
  2483. node.kind = kind;
  2484. let optional = false;
  2485. if (this.match(47) || this.match(10)) {
  2486. node.method = true;
  2487. if (protoStartLoc != null) {
  2488. this.unexpected(protoStartLoc);
  2489. }
  2490. if (variance) {
  2491. this.unexpected(variance.loc.start);
  2492. }
  2493. node.value = this.flowParseObjectTypeMethodish(this.startNodeAt(node.loc.start));
  2494. if (kind === "get" || kind === "set") {
  2495. this.flowCheckGetterSetterParams(node);
  2496. }
  2497. if (!allowSpread && node.key.name === "constructor" && node.value.this) {
  2498. this.raise(FlowErrors.ThisParamBannedInConstructor, node.value.this);
  2499. }
  2500. } else {
  2501. if (kind !== "init") this.unexpected();
  2502. node.method = false;
  2503. if (this.eat(17)) {
  2504. optional = true;
  2505. }
  2506. node.value = this.flowParseTypeInitialiser();
  2507. node.variance = variance;
  2508. }
  2509. node.optional = optional;
  2510. return this.finishNode(node, "ObjectTypeProperty");
  2511. }
  2512. }
  2513. flowCheckGetterSetterParams(property) {
  2514. const paramCount = property.kind === "get" ? 0 : 1;
  2515. const length = property.value.params.length + (property.value.rest ? 1 : 0);
  2516. if (property.value.this) {
  2517. this.raise(property.kind === "get" ? FlowErrors.GetterMayNotHaveThisParam : FlowErrors.SetterMayNotHaveThisParam, property.value.this);
  2518. }
  2519. if (length !== paramCount) {
  2520. this.raise(property.kind === "get" ? Errors.BadGetterArity : Errors.BadSetterArity, property);
  2521. }
  2522. if (property.kind === "set" && property.value.rest) {
  2523. this.raise(Errors.BadSetterRestParameter, property);
  2524. }
  2525. }
  2526. flowObjectTypeSemicolon() {
  2527. if (!this.eat(13) && !this.eat(12) && !this.match(8) && !this.match(9)) {
  2528. this.unexpected();
  2529. }
  2530. }
  2531. flowParseQualifiedTypeIdentifier(startLoc, id) {
  2532. startLoc != null ? startLoc : startLoc = this.state.startLoc;
  2533. let node = id || this.flowParseRestrictedIdentifier(true);
  2534. while (this.eat(16)) {
  2535. const node2 = this.startNodeAt(startLoc);
  2536. node2.qualification = node;
  2537. node2.id = this.flowParseRestrictedIdentifier(true);
  2538. node = this.finishNode(node2, "QualifiedTypeIdentifier");
  2539. }
  2540. return node;
  2541. }
  2542. flowParseGenericType(startLoc, id) {
  2543. const node = this.startNodeAt(startLoc);
  2544. node.typeParameters = null;
  2545. node.id = this.flowParseQualifiedTypeIdentifier(startLoc, id);
  2546. if (this.match(47)) {
  2547. node.typeParameters = this.flowParseTypeParameterInstantiation();
  2548. }
  2549. return this.finishNode(node, "GenericTypeAnnotation");
  2550. }
  2551. flowParseTypeofType() {
  2552. const node = this.startNode();
  2553. this.expect(87);
  2554. node.argument = this.flowParsePrimaryType();
  2555. return this.finishNode(node, "TypeofTypeAnnotation");
  2556. }
  2557. flowParseTupleType() {
  2558. const node = this.startNode();
  2559. node.types = [];
  2560. this.expect(0);
  2561. while (this.state.pos < this.length && !this.match(3)) {
  2562. node.types.push(this.flowParseType());
  2563. if (this.match(3)) break;
  2564. this.expect(12);
  2565. }
  2566. this.expect(3);
  2567. return this.finishNode(node, "TupleTypeAnnotation");
  2568. }
  2569. flowParseFunctionTypeParam(first) {
  2570. let name = null;
  2571. let optional = false;
  2572. let typeAnnotation = null;
  2573. const node = this.startNode();
  2574. const lh = this.lookahead();
  2575. const isThis = this.state.type === 78;
  2576. if (lh.type === 14 || lh.type === 17) {
  2577. if (isThis && !first) {
  2578. this.raise(FlowErrors.ThisParamMustBeFirst, node);
  2579. }
  2580. name = this.parseIdentifier(isThis);
  2581. if (this.eat(17)) {
  2582. optional = true;
  2583. if (isThis) {
  2584. this.raise(FlowErrors.ThisParamMayNotBeOptional, node);
  2585. }
  2586. }
  2587. typeAnnotation = this.flowParseTypeInitialiser();
  2588. } else {
  2589. typeAnnotation = this.flowParseType();
  2590. }
  2591. node.name = name;
  2592. node.optional = optional;
  2593. node.typeAnnotation = typeAnnotation;
  2594. return this.finishNode(node, "FunctionTypeParam");
  2595. }
  2596. reinterpretTypeAsFunctionTypeParam(type) {
  2597. const node = this.startNodeAt(type.loc.start);
  2598. node.name = null;
  2599. node.optional = false;
  2600. node.typeAnnotation = type;
  2601. return this.finishNode(node, "FunctionTypeParam");
  2602. }
  2603. flowParseFunctionTypeParams(params = []) {
  2604. let rest = null;
  2605. let _this = null;
  2606. if (this.match(78)) {
  2607. _this = this.flowParseFunctionTypeParam(true);
  2608. _this.name = null;
  2609. if (!this.match(11)) {
  2610. this.expect(12);
  2611. }
  2612. }
  2613. while (!this.match(11) && !this.match(21)) {
  2614. params.push(this.flowParseFunctionTypeParam(false));
  2615. if (!this.match(11)) {
  2616. this.expect(12);
  2617. }
  2618. }
  2619. if (this.eat(21)) {
  2620. rest = this.flowParseFunctionTypeParam(false);
  2621. }
  2622. return {
  2623. params,
  2624. rest,
  2625. _this
  2626. };
  2627. }
  2628. flowIdentToTypeAnnotation(startLoc, node, id) {
  2629. switch (id.name) {
  2630. case "any":
  2631. return this.finishNode(node, "AnyTypeAnnotation");
  2632. case "bool":
  2633. case "boolean":
  2634. return this.finishNode(node, "BooleanTypeAnnotation");
  2635. case "mixed":
  2636. return this.finishNode(node, "MixedTypeAnnotation");
  2637. case "empty":
  2638. return this.finishNode(node, "EmptyTypeAnnotation");
  2639. case "number":
  2640. return this.finishNode(node, "NumberTypeAnnotation");
  2641. case "string":
  2642. return this.finishNode(node, "StringTypeAnnotation");
  2643. case "symbol":
  2644. return this.finishNode(node, "SymbolTypeAnnotation");
  2645. default:
  2646. this.checkNotUnderscore(id.name);
  2647. return this.flowParseGenericType(startLoc, id);
  2648. }
  2649. }
  2650. flowParsePrimaryType() {
  2651. const startLoc = this.state.startLoc;
  2652. const node = this.startNode();
  2653. let tmp;
  2654. let type;
  2655. let isGroupedType = false;
  2656. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  2657. switch (this.state.type) {
  2658. case 5:
  2659. return this.flowParseObjectType({
  2660. allowStatic: false,
  2661. allowExact: false,
  2662. allowSpread: true,
  2663. allowProto: false,
  2664. allowInexact: true
  2665. });
  2666. case 6:
  2667. return this.flowParseObjectType({
  2668. allowStatic: false,
  2669. allowExact: true,
  2670. allowSpread: true,
  2671. allowProto: false,
  2672. allowInexact: false
  2673. });
  2674. case 0:
  2675. this.state.noAnonFunctionType = false;
  2676. type = this.flowParseTupleType();
  2677. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  2678. return type;
  2679. case 47:
  2680. {
  2681. const node = this.startNode();
  2682. node.typeParameters = this.flowParseTypeParameterDeclaration();
  2683. this.expect(10);
  2684. tmp = this.flowParseFunctionTypeParams();
  2685. node.params = tmp.params;
  2686. node.rest = tmp.rest;
  2687. node.this = tmp._this;
  2688. this.expect(11);
  2689. this.expect(19);
  2690. node.returnType = this.flowParseType();
  2691. return this.finishNode(node, "FunctionTypeAnnotation");
  2692. }
  2693. case 10:
  2694. {
  2695. const node = this.startNode();
  2696. this.next();
  2697. if (!this.match(11) && !this.match(21)) {
  2698. if (tokenIsIdentifier(this.state.type) || this.match(78)) {
  2699. const token = this.lookahead().type;
  2700. isGroupedType = token !== 17 && token !== 14;
  2701. } else {
  2702. isGroupedType = true;
  2703. }
  2704. }
  2705. if (isGroupedType) {
  2706. this.state.noAnonFunctionType = false;
  2707. type = this.flowParseType();
  2708. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  2709. if (this.state.noAnonFunctionType || !(this.match(12) || this.match(11) && this.lookahead().type === 19)) {
  2710. this.expect(11);
  2711. return type;
  2712. } else {
  2713. this.eat(12);
  2714. }
  2715. }
  2716. if (type) {
  2717. tmp = this.flowParseFunctionTypeParams([this.reinterpretTypeAsFunctionTypeParam(type)]);
  2718. } else {
  2719. tmp = this.flowParseFunctionTypeParams();
  2720. }
  2721. node.params = tmp.params;
  2722. node.rest = tmp.rest;
  2723. node.this = tmp._this;
  2724. this.expect(11);
  2725. this.expect(19);
  2726. node.returnType = this.flowParseType();
  2727. node.typeParameters = null;
  2728. return this.finishNode(node, "FunctionTypeAnnotation");
  2729. }
  2730. case 134:
  2731. return this.parseLiteral(this.state.value, "StringLiteralTypeAnnotation");
  2732. case 85:
  2733. case 86:
  2734. node.value = this.match(85);
  2735. this.next();
  2736. return this.finishNode(node, "BooleanLiteralTypeAnnotation");
  2737. case 53:
  2738. if (this.state.value === "-") {
  2739. this.next();
  2740. if (this.match(135)) {
  2741. return this.parseLiteralAtNode(-this.state.value, "NumberLiteralTypeAnnotation", node);
  2742. }
  2743. if (this.match(136)) {
  2744. return this.parseLiteralAtNode(-this.state.value, "BigIntLiteralTypeAnnotation", node);
  2745. }
  2746. throw this.raise(FlowErrors.UnexpectedSubtractionOperand, this.state.startLoc);
  2747. }
  2748. this.unexpected();
  2749. return;
  2750. case 135:
  2751. return this.parseLiteral(this.state.value, "NumberLiteralTypeAnnotation");
  2752. case 136:
  2753. return this.parseLiteral(this.state.value, "BigIntLiteralTypeAnnotation");
  2754. case 88:
  2755. this.next();
  2756. return this.finishNode(node, "VoidTypeAnnotation");
  2757. case 84:
  2758. this.next();
  2759. return this.finishNode(node, "NullLiteralTypeAnnotation");
  2760. case 78:
  2761. this.next();
  2762. return this.finishNode(node, "ThisTypeAnnotation");
  2763. case 55:
  2764. this.next();
  2765. return this.finishNode(node, "ExistsTypeAnnotation");
  2766. case 87:
  2767. return this.flowParseTypeofType();
  2768. default:
  2769. if (tokenIsKeyword(this.state.type)) {
  2770. const label = tokenLabelName(this.state.type);
  2771. this.next();
  2772. return super.createIdentifier(node, label);
  2773. } else if (tokenIsIdentifier(this.state.type)) {
  2774. if (this.isContextual(129)) {
  2775. return this.flowParseInterfaceType();
  2776. }
  2777. return this.flowIdentToTypeAnnotation(startLoc, node, this.parseIdentifier());
  2778. }
  2779. }
  2780. this.unexpected();
  2781. }
  2782. flowParsePostfixType() {
  2783. const startLoc = this.state.startLoc;
  2784. let type = this.flowParsePrimaryType();
  2785. let seenOptionalIndexedAccess = false;
  2786. while ((this.match(0) || this.match(18)) && !this.canInsertSemicolon()) {
  2787. const node = this.startNodeAt(startLoc);
  2788. const optional = this.eat(18);
  2789. seenOptionalIndexedAccess = seenOptionalIndexedAccess || optional;
  2790. this.expect(0);
  2791. if (!optional && this.match(3)) {
  2792. node.elementType = type;
  2793. this.next();
  2794. type = this.finishNode(node, "ArrayTypeAnnotation");
  2795. } else {
  2796. node.objectType = type;
  2797. node.indexType = this.flowParseType();
  2798. this.expect(3);
  2799. if (seenOptionalIndexedAccess) {
  2800. node.optional = optional;
  2801. type = this.finishNode(node, "OptionalIndexedAccessType");
  2802. } else {
  2803. type = this.finishNode(node, "IndexedAccessType");
  2804. }
  2805. }
  2806. }
  2807. return type;
  2808. }
  2809. flowParsePrefixType() {
  2810. const node = this.startNode();
  2811. if (this.eat(17)) {
  2812. node.typeAnnotation = this.flowParsePrefixType();
  2813. return this.finishNode(node, "NullableTypeAnnotation");
  2814. } else {
  2815. return this.flowParsePostfixType();
  2816. }
  2817. }
  2818. flowParseAnonFunctionWithoutParens() {
  2819. const param = this.flowParsePrefixType();
  2820. if (!this.state.noAnonFunctionType && this.eat(19)) {
  2821. const node = this.startNodeAt(param.loc.start);
  2822. node.params = [this.reinterpretTypeAsFunctionTypeParam(param)];
  2823. node.rest = null;
  2824. node.this = null;
  2825. node.returnType = this.flowParseType();
  2826. node.typeParameters = null;
  2827. return this.finishNode(node, "FunctionTypeAnnotation");
  2828. }
  2829. return param;
  2830. }
  2831. flowParseIntersectionType() {
  2832. const node = this.startNode();
  2833. this.eat(45);
  2834. const type = this.flowParseAnonFunctionWithoutParens();
  2835. node.types = [type];
  2836. while (this.eat(45)) {
  2837. node.types.push(this.flowParseAnonFunctionWithoutParens());
  2838. }
  2839. return node.types.length === 1 ? type : this.finishNode(node, "IntersectionTypeAnnotation");
  2840. }
  2841. flowParseUnionType() {
  2842. const node = this.startNode();
  2843. this.eat(43);
  2844. const type = this.flowParseIntersectionType();
  2845. node.types = [type];
  2846. while (this.eat(43)) {
  2847. node.types.push(this.flowParseIntersectionType());
  2848. }
  2849. return node.types.length === 1 ? type : this.finishNode(node, "UnionTypeAnnotation");
  2850. }
  2851. flowParseType() {
  2852. const oldInType = this.state.inType;
  2853. this.state.inType = true;
  2854. const type = this.flowParseUnionType();
  2855. this.state.inType = oldInType;
  2856. return type;
  2857. }
  2858. flowParseTypeOrImplicitInstantiation() {
  2859. if (this.state.type === 132 && this.state.value === "_") {
  2860. const startLoc = this.state.startLoc;
  2861. const node = this.parseIdentifier();
  2862. return this.flowParseGenericType(startLoc, node);
  2863. } else {
  2864. return this.flowParseType();
  2865. }
  2866. }
  2867. flowParseTypeAnnotation() {
  2868. const node = this.startNode();
  2869. node.typeAnnotation = this.flowParseTypeInitialiser();
  2870. return this.finishNode(node, "TypeAnnotation");
  2871. }
  2872. flowParseTypeAnnotatableIdentifier(allowPrimitiveOverride) {
  2873. const ident = allowPrimitiveOverride ? this.parseIdentifier() : this.flowParseRestrictedIdentifier();
  2874. if (this.match(14)) {
  2875. ident.typeAnnotation = this.flowParseTypeAnnotation();
  2876. this.resetEndLocation(ident);
  2877. }
  2878. return ident;
  2879. }
  2880. typeCastToParameter(node) {
  2881. node.expression.typeAnnotation = node.typeAnnotation;
  2882. this.resetEndLocation(node.expression, node.typeAnnotation.loc.end);
  2883. return node.expression;
  2884. }
  2885. flowParseVariance() {
  2886. let variance = null;
  2887. if (this.match(53)) {
  2888. variance = this.startNode();
  2889. if (this.state.value === "+") {
  2890. variance.kind = "plus";
  2891. } else {
  2892. variance.kind = "minus";
  2893. }
  2894. this.next();
  2895. return this.finishNode(variance, "Variance");
  2896. }
  2897. return variance;
  2898. }
  2899. parseFunctionBody(node, allowExpressionBody, isMethod = false) {
  2900. if (allowExpressionBody) {
  2901. this.forwardNoArrowParamsConversionAt(node, () => super.parseFunctionBody(node, true, isMethod));
  2902. return;
  2903. }
  2904. super.parseFunctionBody(node, false, isMethod);
  2905. }
  2906. parseFunctionBodyAndFinish(node, type, isMethod = false) {
  2907. if (this.match(14)) {
  2908. const typeNode = this.startNode();
  2909. [typeNode.typeAnnotation, node.predicate] = this.flowParseTypeAndPredicateInitialiser();
  2910. node.returnType = typeNode.typeAnnotation ? this.finishNode(typeNode, "TypeAnnotation") : null;
  2911. }
  2912. return super.parseFunctionBodyAndFinish(node, type, isMethod);
  2913. }
  2914. parseStatementLike(flags) {
  2915. if (this.state.strict && this.isContextual(129)) {
  2916. const lookahead = this.lookahead();
  2917. if (tokenIsKeywordOrIdentifier(lookahead.type)) {
  2918. const node = this.startNode();
  2919. this.next();
  2920. return this.flowParseInterface(node);
  2921. }
  2922. } else if (this.isContextual(126)) {
  2923. const node = this.startNode();
  2924. this.next();
  2925. return this.flowParseEnumDeclaration(node);
  2926. }
  2927. const stmt = super.parseStatementLike(flags);
  2928. if (this.flowPragma === undefined && !this.isValidDirective(stmt)) {
  2929. this.flowPragma = null;
  2930. }
  2931. return stmt;
  2932. }
  2933. parseExpressionStatement(node, expr, decorators) {
  2934. if (expr.type === "Identifier") {
  2935. if (expr.name === "declare") {
  2936. if (this.match(80) || tokenIsIdentifier(this.state.type) || this.match(68) || this.match(74) || this.match(82)) {
  2937. return this.flowParseDeclare(node);
  2938. }
  2939. } else if (tokenIsIdentifier(this.state.type)) {
  2940. if (expr.name === "interface") {
  2941. return this.flowParseInterface(node);
  2942. } else if (expr.name === "type") {
  2943. return this.flowParseTypeAlias(node);
  2944. } else if (expr.name === "opaque") {
  2945. return this.flowParseOpaqueType(node, false);
  2946. }
  2947. }
  2948. }
  2949. return super.parseExpressionStatement(node, expr, decorators);
  2950. }
  2951. shouldParseExportDeclaration() {
  2952. const {
  2953. type
  2954. } = this.state;
  2955. if (type === 126 || tokenIsFlowInterfaceOrTypeOrOpaque(type)) {
  2956. return !this.state.containsEsc;
  2957. }
  2958. return super.shouldParseExportDeclaration();
  2959. }
  2960. isExportDefaultSpecifier() {
  2961. const {
  2962. type
  2963. } = this.state;
  2964. if (type === 126 || tokenIsFlowInterfaceOrTypeOrOpaque(type)) {
  2965. return this.state.containsEsc;
  2966. }
  2967. return super.isExportDefaultSpecifier();
  2968. }
  2969. parseExportDefaultExpression() {
  2970. if (this.isContextual(126)) {
  2971. const node = this.startNode();
  2972. this.next();
  2973. return this.flowParseEnumDeclaration(node);
  2974. }
  2975. return super.parseExportDefaultExpression();
  2976. }
  2977. parseConditional(expr, startLoc, refExpressionErrors) {
  2978. if (!this.match(17)) return expr;
  2979. if (this.state.maybeInArrowParameters) {
  2980. const nextCh = this.lookaheadCharCode();
  2981. if (nextCh === 44 || nextCh === 61 || nextCh === 58 || nextCh === 41) {
  2982. this.setOptionalParametersError(refExpressionErrors);
  2983. return expr;
  2984. }
  2985. }
  2986. this.expect(17);
  2987. const state = this.state.clone();
  2988. const originalNoArrowAt = this.state.noArrowAt;
  2989. const node = this.startNodeAt(startLoc);
  2990. let {
  2991. consequent,
  2992. failed
  2993. } = this.tryParseConditionalConsequent();
  2994. let [valid, invalid] = this.getArrowLikeExpressions(consequent);
  2995. if (failed || invalid.length > 0) {
  2996. const noArrowAt = [...originalNoArrowAt];
  2997. if (invalid.length > 0) {
  2998. this.state = state;
  2999. this.state.noArrowAt = noArrowAt;
  3000. for (let i = 0; i < invalid.length; i++) {
  3001. noArrowAt.push(invalid[i].start);
  3002. }
  3003. ({
  3004. consequent,
  3005. failed
  3006. } = this.tryParseConditionalConsequent());
  3007. [valid, invalid] = this.getArrowLikeExpressions(consequent);
  3008. }
  3009. if (failed && valid.length > 1) {
  3010. this.raise(FlowErrors.AmbiguousConditionalArrow, state.startLoc);
  3011. }
  3012. if (failed && valid.length === 1) {
  3013. this.state = state;
  3014. noArrowAt.push(valid[0].start);
  3015. this.state.noArrowAt = noArrowAt;
  3016. ({
  3017. consequent,
  3018. failed
  3019. } = this.tryParseConditionalConsequent());
  3020. }
  3021. }
  3022. this.getArrowLikeExpressions(consequent, true);
  3023. this.state.noArrowAt = originalNoArrowAt;
  3024. this.expect(14);
  3025. node.test = expr;
  3026. node.consequent = consequent;
  3027. node.alternate = this.forwardNoArrowParamsConversionAt(node, () => this.parseMaybeAssign(undefined, undefined));
  3028. return this.finishNode(node, "ConditionalExpression");
  3029. }
  3030. tryParseConditionalConsequent() {
  3031. this.state.noArrowParamsConversionAt.push(this.state.start);
  3032. const consequent = this.parseMaybeAssignAllowIn();
  3033. const failed = !this.match(14);
  3034. this.state.noArrowParamsConversionAt.pop();
  3035. return {
  3036. consequent,
  3037. failed
  3038. };
  3039. }
  3040. getArrowLikeExpressions(node, disallowInvalid) {
  3041. const stack = [node];
  3042. const arrows = [];
  3043. while (stack.length !== 0) {
  3044. const node = stack.pop();
  3045. if (node.type === "ArrowFunctionExpression" && node.body.type !== "BlockStatement") {
  3046. if (node.typeParameters || !node.returnType) {
  3047. this.finishArrowValidation(node);
  3048. } else {
  3049. arrows.push(node);
  3050. }
  3051. stack.push(node.body);
  3052. } else if (node.type === "ConditionalExpression") {
  3053. stack.push(node.consequent);
  3054. stack.push(node.alternate);
  3055. }
  3056. }
  3057. if (disallowInvalid) {
  3058. arrows.forEach(node => this.finishArrowValidation(node));
  3059. return [arrows, []];
  3060. }
  3061. return partition(arrows, node => node.params.every(param => this.isAssignable(param, true)));
  3062. }
  3063. finishArrowValidation(node) {
  3064. var _node$extra;
  3065. this.toAssignableList(node.params, (_node$extra = node.extra) == null ? void 0 : _node$extra.trailingCommaLoc, false);
  3066. this.scope.enter(2 | 4);
  3067. super.checkParams(node, false, true);
  3068. this.scope.exit();
  3069. }
  3070. forwardNoArrowParamsConversionAt(node, parse) {
  3071. let result;
  3072. if (this.state.noArrowParamsConversionAt.includes(this.offsetToSourcePos(node.start))) {
  3073. this.state.noArrowParamsConversionAt.push(this.state.start);
  3074. result = parse();
  3075. this.state.noArrowParamsConversionAt.pop();
  3076. } else {
  3077. result = parse();
  3078. }
  3079. return result;
  3080. }
  3081. parseParenItem(node, startLoc) {
  3082. const newNode = super.parseParenItem(node, startLoc);
  3083. if (this.eat(17)) {
  3084. newNode.optional = true;
  3085. this.resetEndLocation(node);
  3086. }
  3087. if (this.match(14)) {
  3088. const typeCastNode = this.startNodeAt(startLoc);
  3089. typeCastNode.expression = newNode;
  3090. typeCastNode.typeAnnotation = this.flowParseTypeAnnotation();
  3091. return this.finishNode(typeCastNode, "TypeCastExpression");
  3092. }
  3093. return newNode;
  3094. }
  3095. assertModuleNodeAllowed(node) {
  3096. if (node.type === "ImportDeclaration" && (node.importKind === "type" || node.importKind === "typeof") || node.type === "ExportNamedDeclaration" && node.exportKind === "type" || node.type === "ExportAllDeclaration" && node.exportKind === "type") {
  3097. return;
  3098. }
  3099. super.assertModuleNodeAllowed(node);
  3100. }
  3101. parseExportDeclaration(node) {
  3102. if (this.isContextual(130)) {
  3103. node.exportKind = "type";
  3104. const declarationNode = this.startNode();
  3105. this.next();
  3106. if (this.match(5)) {
  3107. node.specifiers = this.parseExportSpecifiers(true);
  3108. super.parseExportFrom(node);
  3109. return null;
  3110. } else {
  3111. return this.flowParseTypeAlias(declarationNode);
  3112. }
  3113. } else if (this.isContextual(131)) {
  3114. node.exportKind = "type";
  3115. const declarationNode = this.startNode();
  3116. this.next();
  3117. return this.flowParseOpaqueType(declarationNode, false);
  3118. } else if (this.isContextual(129)) {
  3119. node.exportKind = "type";
  3120. const declarationNode = this.startNode();
  3121. this.next();
  3122. return this.flowParseInterface(declarationNode);
  3123. } else if (this.isContextual(126)) {
  3124. node.exportKind = "value";
  3125. const declarationNode = this.startNode();
  3126. this.next();
  3127. return this.flowParseEnumDeclaration(declarationNode);
  3128. } else {
  3129. return super.parseExportDeclaration(node);
  3130. }
  3131. }
  3132. eatExportStar(node) {
  3133. if (super.eatExportStar(node)) return true;
  3134. if (this.isContextual(130) && this.lookahead().type === 55) {
  3135. node.exportKind = "type";
  3136. this.next();
  3137. this.next();
  3138. return true;
  3139. }
  3140. return false;
  3141. }
  3142. maybeParseExportNamespaceSpecifier(node) {
  3143. const {
  3144. startLoc
  3145. } = this.state;
  3146. const hasNamespace = super.maybeParseExportNamespaceSpecifier(node);
  3147. if (hasNamespace && node.exportKind === "type") {
  3148. this.unexpected(startLoc);
  3149. }
  3150. return hasNamespace;
  3151. }
  3152. parseClassId(node, isStatement, optionalId) {
  3153. super.parseClassId(node, isStatement, optionalId);
  3154. if (this.match(47)) {
  3155. node.typeParameters = this.flowParseTypeParameterDeclaration();
  3156. }
  3157. }
  3158. parseClassMember(classBody, member, state) {
  3159. const {
  3160. startLoc
  3161. } = this.state;
  3162. if (this.isContextual(125)) {
  3163. if (super.parseClassMemberFromModifier(classBody, member)) {
  3164. return;
  3165. }
  3166. member.declare = true;
  3167. }
  3168. super.parseClassMember(classBody, member, state);
  3169. if (member.declare) {
  3170. if (member.type !== "ClassProperty" && member.type !== "ClassPrivateProperty" && member.type !== "PropertyDefinition") {
  3171. this.raise(FlowErrors.DeclareClassElement, startLoc);
  3172. } else if (member.value) {
  3173. this.raise(FlowErrors.DeclareClassFieldInitializer, member.value);
  3174. }
  3175. }
  3176. }
  3177. isIterator(word) {
  3178. return word === "iterator" || word === "asyncIterator";
  3179. }
  3180. readIterator() {
  3181. const word = super.readWord1();
  3182. const fullWord = "@@" + word;
  3183. if (!this.isIterator(word) || !this.state.inType) {
  3184. this.raise(Errors.InvalidIdentifier, this.state.curPosition(), {
  3185. identifierName: fullWord
  3186. });
  3187. }
  3188. this.finishToken(132, fullWord);
  3189. }
  3190. getTokenFromCode(code) {
  3191. const next = this.input.charCodeAt(this.state.pos + 1);
  3192. if (code === 123 && next === 124) {
  3193. this.finishOp(6, 2);
  3194. } else if (this.state.inType && (code === 62 || code === 60)) {
  3195. this.finishOp(code === 62 ? 48 : 47, 1);
  3196. } else if (this.state.inType && code === 63) {
  3197. if (next === 46) {
  3198. this.finishOp(18, 2);
  3199. } else {
  3200. this.finishOp(17, 1);
  3201. }
  3202. } else if (isIteratorStart(code, next, this.input.charCodeAt(this.state.pos + 2))) {
  3203. this.state.pos += 2;
  3204. this.readIterator();
  3205. } else {
  3206. super.getTokenFromCode(code);
  3207. }
  3208. }
  3209. isAssignable(node, isBinding) {
  3210. if (node.type === "TypeCastExpression") {
  3211. return this.isAssignable(node.expression, isBinding);
  3212. } else {
  3213. return super.isAssignable(node, isBinding);
  3214. }
  3215. }
  3216. toAssignable(node, isLHS = false) {
  3217. if (!isLHS && node.type === "AssignmentExpression" && node.left.type === "TypeCastExpression") {
  3218. node.left = this.typeCastToParameter(node.left);
  3219. }
  3220. super.toAssignable(node, isLHS);
  3221. }
  3222. toAssignableList(exprList, trailingCommaLoc, isLHS) {
  3223. for (let i = 0; i < exprList.length; i++) {
  3224. const expr = exprList[i];
  3225. if ((expr == null ? void 0 : expr.type) === "TypeCastExpression") {
  3226. exprList[i] = this.typeCastToParameter(expr);
  3227. }
  3228. }
  3229. super.toAssignableList(exprList, trailingCommaLoc, isLHS);
  3230. }
  3231. toReferencedList(exprList, isParenthesizedExpr) {
  3232. for (let i = 0; i < exprList.length; i++) {
  3233. var _expr$extra;
  3234. const expr = exprList[i];
  3235. if (expr && expr.type === "TypeCastExpression" && !((_expr$extra = expr.extra) != null && _expr$extra.parenthesized) && (exprList.length > 1 || !isParenthesizedExpr)) {
  3236. this.raise(FlowErrors.TypeCastInPattern, expr.typeAnnotation);
  3237. }
  3238. }
  3239. return exprList;
  3240. }
  3241. parseArrayLike(close, canBePattern, isTuple, refExpressionErrors) {
  3242. const node = super.parseArrayLike(close, canBePattern, isTuple, refExpressionErrors);
  3243. if (canBePattern && !this.state.maybeInArrowParameters) {
  3244. this.toReferencedList(node.elements);
  3245. }
  3246. return node;
  3247. }
  3248. isValidLVal(type, isParenthesized, binding) {
  3249. return type === "TypeCastExpression" || super.isValidLVal(type, isParenthesized, binding);
  3250. }
  3251. parseClassProperty(node) {
  3252. if (this.match(14)) {
  3253. node.typeAnnotation = this.flowParseTypeAnnotation();
  3254. }
  3255. return super.parseClassProperty(node);
  3256. }
  3257. parseClassPrivateProperty(node) {
  3258. if (this.match(14)) {
  3259. node.typeAnnotation = this.flowParseTypeAnnotation();
  3260. }
  3261. return super.parseClassPrivateProperty(node);
  3262. }
  3263. isClassMethod() {
  3264. return this.match(47) || super.isClassMethod();
  3265. }
  3266. isClassProperty() {
  3267. return this.match(14) || super.isClassProperty();
  3268. }
  3269. isNonstaticConstructor(method) {
  3270. return !this.match(14) && super.isNonstaticConstructor(method);
  3271. }
  3272. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  3273. if (method.variance) {
  3274. this.unexpected(method.variance.loc.start);
  3275. }
  3276. delete method.variance;
  3277. if (this.match(47)) {
  3278. method.typeParameters = this.flowParseTypeParameterDeclaration();
  3279. }
  3280. super.pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper);
  3281. if (method.params && isConstructor) {
  3282. const params = method.params;
  3283. if (params.length > 0 && this.isThisParam(params[0])) {
  3284. this.raise(FlowErrors.ThisParamBannedInConstructor, method);
  3285. }
  3286. } else if (method.type === "MethodDefinition" && isConstructor && method.value.params) {
  3287. const params = method.value.params;
  3288. if (params.length > 0 && this.isThisParam(params[0])) {
  3289. this.raise(FlowErrors.ThisParamBannedInConstructor, method);
  3290. }
  3291. }
  3292. }
  3293. pushClassPrivateMethod(classBody, method, isGenerator, isAsync) {
  3294. if (method.variance) {
  3295. this.unexpected(method.variance.loc.start);
  3296. }
  3297. delete method.variance;
  3298. if (this.match(47)) {
  3299. method.typeParameters = this.flowParseTypeParameterDeclaration();
  3300. }
  3301. super.pushClassPrivateMethod(classBody, method, isGenerator, isAsync);
  3302. }
  3303. parseClassSuper(node) {
  3304. super.parseClassSuper(node);
  3305. if (node.superClass && (this.match(47) || this.match(51))) {
  3306. {
  3307. node.superTypeParameters = this.flowParseTypeParameterInstantiationInExpression();
  3308. }
  3309. }
  3310. if (this.isContextual(113)) {
  3311. this.next();
  3312. const implemented = node.implements = [];
  3313. do {
  3314. const node = this.startNode();
  3315. node.id = this.flowParseRestrictedIdentifier(true);
  3316. if (this.match(47)) {
  3317. node.typeParameters = this.flowParseTypeParameterInstantiation();
  3318. } else {
  3319. node.typeParameters = null;
  3320. }
  3321. implemented.push(this.finishNode(node, "ClassImplements"));
  3322. } while (this.eat(12));
  3323. }
  3324. }
  3325. checkGetterSetterParams(method) {
  3326. super.checkGetterSetterParams(method);
  3327. const params = this.getObjectOrClassMethodParams(method);
  3328. if (params.length > 0) {
  3329. const param = params[0];
  3330. if (this.isThisParam(param) && method.kind === "get") {
  3331. this.raise(FlowErrors.GetterMayNotHaveThisParam, param);
  3332. } else if (this.isThisParam(param)) {
  3333. this.raise(FlowErrors.SetterMayNotHaveThisParam, param);
  3334. }
  3335. }
  3336. }
  3337. parsePropertyNamePrefixOperator(node) {
  3338. node.variance = this.flowParseVariance();
  3339. }
  3340. parseObjPropValue(prop, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors) {
  3341. if (prop.variance) {
  3342. this.unexpected(prop.variance.loc.start);
  3343. }
  3344. delete prop.variance;
  3345. let typeParameters;
  3346. if (this.match(47) && !isAccessor) {
  3347. typeParameters = this.flowParseTypeParameterDeclaration();
  3348. if (!this.match(10)) this.unexpected();
  3349. }
  3350. const result = super.parseObjPropValue(prop, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors);
  3351. if (typeParameters) {
  3352. (result.value || result).typeParameters = typeParameters;
  3353. }
  3354. return result;
  3355. }
  3356. parseFunctionParamType(param) {
  3357. if (this.eat(17)) {
  3358. if (param.type !== "Identifier") {
  3359. this.raise(FlowErrors.PatternIsOptional, param);
  3360. }
  3361. if (this.isThisParam(param)) {
  3362. this.raise(FlowErrors.ThisParamMayNotBeOptional, param);
  3363. }
  3364. param.optional = true;
  3365. }
  3366. if (this.match(14)) {
  3367. param.typeAnnotation = this.flowParseTypeAnnotation();
  3368. } else if (this.isThisParam(param)) {
  3369. this.raise(FlowErrors.ThisParamAnnotationRequired, param);
  3370. }
  3371. if (this.match(29) && this.isThisParam(param)) {
  3372. this.raise(FlowErrors.ThisParamNoDefault, param);
  3373. }
  3374. this.resetEndLocation(param);
  3375. return param;
  3376. }
  3377. parseMaybeDefault(startLoc, left) {
  3378. const node = super.parseMaybeDefault(startLoc, left);
  3379. if (node.type === "AssignmentPattern" && node.typeAnnotation && node.right.start < node.typeAnnotation.start) {
  3380. this.raise(FlowErrors.TypeBeforeInitializer, node.typeAnnotation);
  3381. }
  3382. return node;
  3383. }
  3384. checkImportReflection(node) {
  3385. super.checkImportReflection(node);
  3386. if (node.module && node.importKind !== "value") {
  3387. this.raise(FlowErrors.ImportReflectionHasImportType, node.specifiers[0].loc.start);
  3388. }
  3389. }
  3390. parseImportSpecifierLocal(node, specifier, type) {
  3391. specifier.local = hasTypeImportKind(node) ? this.flowParseRestrictedIdentifier(true, true) : this.parseIdentifier();
  3392. node.specifiers.push(this.finishImportSpecifier(specifier, type));
  3393. }
  3394. isPotentialImportPhase(isExport) {
  3395. if (super.isPotentialImportPhase(isExport)) return true;
  3396. if (this.isContextual(130)) {
  3397. if (!isExport) return true;
  3398. const ch = this.lookaheadCharCode();
  3399. return ch === 123 || ch === 42;
  3400. }
  3401. return !isExport && this.isContextual(87);
  3402. }
  3403. applyImportPhase(node, isExport, phase, loc) {
  3404. super.applyImportPhase(node, isExport, phase, loc);
  3405. if (isExport) {
  3406. if (!phase && this.match(65)) {
  3407. return;
  3408. }
  3409. node.exportKind = phase === "type" ? phase : "value";
  3410. } else {
  3411. if (phase === "type" && this.match(55)) this.unexpected();
  3412. node.importKind = phase === "type" || phase === "typeof" ? phase : "value";
  3413. }
  3414. }
  3415. parseImportSpecifier(specifier, importedIsString, isInTypeOnlyImport, isMaybeTypeOnly, bindingType) {
  3416. const firstIdent = specifier.imported;
  3417. let specifierTypeKind = null;
  3418. if (firstIdent.type === "Identifier") {
  3419. if (firstIdent.name === "type") {
  3420. specifierTypeKind = "type";
  3421. } else if (firstIdent.name === "typeof") {
  3422. specifierTypeKind = "typeof";
  3423. }
  3424. }
  3425. let isBinding = false;
  3426. if (this.isContextual(93) && !this.isLookaheadContextual("as")) {
  3427. const as_ident = this.parseIdentifier(true);
  3428. if (specifierTypeKind !== null && !tokenIsKeywordOrIdentifier(this.state.type)) {
  3429. specifier.imported = as_ident;
  3430. specifier.importKind = specifierTypeKind;
  3431. specifier.local = this.cloneIdentifier(as_ident);
  3432. } else {
  3433. specifier.imported = firstIdent;
  3434. specifier.importKind = null;
  3435. specifier.local = this.parseIdentifier();
  3436. }
  3437. } else {
  3438. if (specifierTypeKind !== null && tokenIsKeywordOrIdentifier(this.state.type)) {
  3439. specifier.imported = this.parseIdentifier(true);
  3440. specifier.importKind = specifierTypeKind;
  3441. } else {
  3442. if (importedIsString) {
  3443. throw this.raise(Errors.ImportBindingIsString, specifier, {
  3444. importName: firstIdent.value
  3445. });
  3446. }
  3447. specifier.imported = firstIdent;
  3448. specifier.importKind = null;
  3449. }
  3450. if (this.eatContextual(93)) {
  3451. specifier.local = this.parseIdentifier();
  3452. } else {
  3453. isBinding = true;
  3454. specifier.local = this.cloneIdentifier(specifier.imported);
  3455. }
  3456. }
  3457. const specifierIsTypeImport = hasTypeImportKind(specifier);
  3458. if (isInTypeOnlyImport && specifierIsTypeImport) {
  3459. this.raise(FlowErrors.ImportTypeShorthandOnlyInPureImport, specifier);
  3460. }
  3461. if (isInTypeOnlyImport || specifierIsTypeImport) {
  3462. this.checkReservedType(specifier.local.name, specifier.local.loc.start, true);
  3463. }
  3464. if (isBinding && !isInTypeOnlyImport && !specifierIsTypeImport) {
  3465. this.checkReservedWord(specifier.local.name, specifier.loc.start, true, true);
  3466. }
  3467. return this.finishImportSpecifier(specifier, "ImportSpecifier");
  3468. }
  3469. parseBindingAtom() {
  3470. switch (this.state.type) {
  3471. case 78:
  3472. return this.parseIdentifier(true);
  3473. default:
  3474. return super.parseBindingAtom();
  3475. }
  3476. }
  3477. parseFunctionParams(node, isConstructor) {
  3478. const kind = node.kind;
  3479. if (kind !== "get" && kind !== "set" && this.match(47)) {
  3480. node.typeParameters = this.flowParseTypeParameterDeclaration();
  3481. }
  3482. super.parseFunctionParams(node, isConstructor);
  3483. }
  3484. parseVarId(decl, kind) {
  3485. super.parseVarId(decl, kind);
  3486. if (this.match(14)) {
  3487. decl.id.typeAnnotation = this.flowParseTypeAnnotation();
  3488. this.resetEndLocation(decl.id);
  3489. }
  3490. }
  3491. parseAsyncArrowFromCallExpression(node, call) {
  3492. if (this.match(14)) {
  3493. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  3494. this.state.noAnonFunctionType = true;
  3495. node.returnType = this.flowParseTypeAnnotation();
  3496. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  3497. }
  3498. return super.parseAsyncArrowFromCallExpression(node, call);
  3499. }
  3500. shouldParseAsyncArrow() {
  3501. return this.match(14) || super.shouldParseAsyncArrow();
  3502. }
  3503. parseMaybeAssign(refExpressionErrors, afterLeftParse) {
  3504. var _jsx;
  3505. let state = null;
  3506. let jsx;
  3507. if (this.hasPlugin("jsx") && (this.match(143) || this.match(47))) {
  3508. state = this.state.clone();
  3509. jsx = this.tryParse(() => super.parseMaybeAssign(refExpressionErrors, afterLeftParse), state);
  3510. if (!jsx.error) return jsx.node;
  3511. const {
  3512. context
  3513. } = this.state;
  3514. const currentContext = context[context.length - 1];
  3515. if (currentContext === types.j_oTag || currentContext === types.j_expr) {
  3516. context.pop();
  3517. }
  3518. }
  3519. if ((_jsx = jsx) != null && _jsx.error || this.match(47)) {
  3520. var _jsx2, _jsx3;
  3521. state = state || this.state.clone();
  3522. let typeParameters;
  3523. const arrow = this.tryParse(abort => {
  3524. var _arrowExpression$extr;
  3525. typeParameters = this.flowParseTypeParameterDeclaration();
  3526. const arrowExpression = this.forwardNoArrowParamsConversionAt(typeParameters, () => {
  3527. const result = super.parseMaybeAssign(refExpressionErrors, afterLeftParse);
  3528. this.resetStartLocationFromNode(result, typeParameters);
  3529. return result;
  3530. });
  3531. if ((_arrowExpression$extr = arrowExpression.extra) != null && _arrowExpression$extr.parenthesized) abort();
  3532. const expr = this.maybeUnwrapTypeCastExpression(arrowExpression);
  3533. if (expr.type !== "ArrowFunctionExpression") abort();
  3534. expr.typeParameters = typeParameters;
  3535. this.resetStartLocationFromNode(expr, typeParameters);
  3536. return arrowExpression;
  3537. }, state);
  3538. let arrowExpression = null;
  3539. if (arrow.node && this.maybeUnwrapTypeCastExpression(arrow.node).type === "ArrowFunctionExpression") {
  3540. if (!arrow.error && !arrow.aborted) {
  3541. if (arrow.node.async) {
  3542. this.raise(FlowErrors.UnexpectedTypeParameterBeforeAsyncArrowFunction, typeParameters);
  3543. }
  3544. return arrow.node;
  3545. }
  3546. arrowExpression = arrow.node;
  3547. }
  3548. if ((_jsx2 = jsx) != null && _jsx2.node) {
  3549. this.state = jsx.failState;
  3550. return jsx.node;
  3551. }
  3552. if (arrowExpression) {
  3553. this.state = arrow.failState;
  3554. return arrowExpression;
  3555. }
  3556. if ((_jsx3 = jsx) != null && _jsx3.thrown) throw jsx.error;
  3557. if (arrow.thrown) throw arrow.error;
  3558. throw this.raise(FlowErrors.UnexpectedTokenAfterTypeParameter, typeParameters);
  3559. }
  3560. return super.parseMaybeAssign(refExpressionErrors, afterLeftParse);
  3561. }
  3562. parseArrow(node) {
  3563. if (this.match(14)) {
  3564. const result = this.tryParse(() => {
  3565. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  3566. this.state.noAnonFunctionType = true;
  3567. const typeNode = this.startNode();
  3568. [typeNode.typeAnnotation, node.predicate] = this.flowParseTypeAndPredicateInitialiser();
  3569. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  3570. if (this.canInsertSemicolon()) this.unexpected();
  3571. if (!this.match(19)) this.unexpected();
  3572. return typeNode;
  3573. });
  3574. if (result.thrown) return null;
  3575. if (result.error) this.state = result.failState;
  3576. node.returnType = result.node.typeAnnotation ? this.finishNode(result.node, "TypeAnnotation") : null;
  3577. }
  3578. return super.parseArrow(node);
  3579. }
  3580. shouldParseArrow(params) {
  3581. return this.match(14) || super.shouldParseArrow(params);
  3582. }
  3583. setArrowFunctionParameters(node, params) {
  3584. if (this.state.noArrowParamsConversionAt.includes(this.offsetToSourcePos(node.start))) {
  3585. node.params = params;
  3586. } else {
  3587. super.setArrowFunctionParameters(node, params);
  3588. }
  3589. }
  3590. checkParams(node, allowDuplicates, isArrowFunction, strictModeChanged = true) {
  3591. if (isArrowFunction && this.state.noArrowParamsConversionAt.includes(this.offsetToSourcePos(node.start))) {
  3592. return;
  3593. }
  3594. for (let i = 0; i < node.params.length; i++) {
  3595. if (this.isThisParam(node.params[i]) && i > 0) {
  3596. this.raise(FlowErrors.ThisParamMustBeFirst, node.params[i]);
  3597. }
  3598. }
  3599. super.checkParams(node, allowDuplicates, isArrowFunction, strictModeChanged);
  3600. }
  3601. parseParenAndDistinguishExpression(canBeArrow) {
  3602. return super.parseParenAndDistinguishExpression(canBeArrow && !this.state.noArrowAt.includes(this.sourceToOffsetPos(this.state.start)));
  3603. }
  3604. parseSubscripts(base, startLoc, noCalls) {
  3605. if (base.type === "Identifier" && base.name === "async" && this.state.noArrowAt.includes(startLoc.index)) {
  3606. this.next();
  3607. const node = this.startNodeAt(startLoc);
  3608. node.callee = base;
  3609. node.arguments = super.parseCallExpressionArguments(11);
  3610. base = this.finishNode(node, "CallExpression");
  3611. } else if (base.type === "Identifier" && base.name === "async" && this.match(47)) {
  3612. const state = this.state.clone();
  3613. const arrow = this.tryParse(abort => this.parseAsyncArrowWithTypeParameters(startLoc) || abort(), state);
  3614. if (!arrow.error && !arrow.aborted) return arrow.node;
  3615. const result = this.tryParse(() => super.parseSubscripts(base, startLoc, noCalls), state);
  3616. if (result.node && !result.error) return result.node;
  3617. if (arrow.node) {
  3618. this.state = arrow.failState;
  3619. return arrow.node;
  3620. }
  3621. if (result.node) {
  3622. this.state = result.failState;
  3623. return result.node;
  3624. }
  3625. throw arrow.error || result.error;
  3626. }
  3627. return super.parseSubscripts(base, startLoc, noCalls);
  3628. }
  3629. parseSubscript(base, startLoc, noCalls, subscriptState) {
  3630. if (this.match(18) && this.isLookaheadToken_lt()) {
  3631. subscriptState.optionalChainMember = true;
  3632. if (noCalls) {
  3633. subscriptState.stop = true;
  3634. return base;
  3635. }
  3636. this.next();
  3637. const node = this.startNodeAt(startLoc);
  3638. node.callee = base;
  3639. node.typeArguments = this.flowParseTypeParameterInstantiationInExpression();
  3640. this.expect(10);
  3641. node.arguments = this.parseCallExpressionArguments(11);
  3642. node.optional = true;
  3643. return this.finishCallExpression(node, true);
  3644. } else if (!noCalls && this.shouldParseTypes() && (this.match(47) || this.match(51))) {
  3645. const node = this.startNodeAt(startLoc);
  3646. node.callee = base;
  3647. const result = this.tryParse(() => {
  3648. node.typeArguments = this.flowParseTypeParameterInstantiationCallOrNew();
  3649. this.expect(10);
  3650. node.arguments = super.parseCallExpressionArguments(11);
  3651. if (subscriptState.optionalChainMember) {
  3652. node.optional = false;
  3653. }
  3654. return this.finishCallExpression(node, subscriptState.optionalChainMember);
  3655. });
  3656. if (result.node) {
  3657. if (result.error) this.state = result.failState;
  3658. return result.node;
  3659. }
  3660. }
  3661. return super.parseSubscript(base, startLoc, noCalls, subscriptState);
  3662. }
  3663. parseNewCallee(node) {
  3664. super.parseNewCallee(node);
  3665. let targs = null;
  3666. if (this.shouldParseTypes() && this.match(47)) {
  3667. targs = this.tryParse(() => this.flowParseTypeParameterInstantiationCallOrNew()).node;
  3668. }
  3669. node.typeArguments = targs;
  3670. }
  3671. parseAsyncArrowWithTypeParameters(startLoc) {
  3672. const node = this.startNodeAt(startLoc);
  3673. this.parseFunctionParams(node, false);
  3674. if (!this.parseArrow(node)) return;
  3675. return super.parseArrowExpression(node, undefined, true);
  3676. }
  3677. readToken_mult_modulo(code) {
  3678. const next = this.input.charCodeAt(this.state.pos + 1);
  3679. if (code === 42 && next === 47 && this.state.hasFlowComment) {
  3680. this.state.hasFlowComment = false;
  3681. this.state.pos += 2;
  3682. this.nextToken();
  3683. return;
  3684. }
  3685. super.readToken_mult_modulo(code);
  3686. }
  3687. readToken_pipe_amp(code) {
  3688. const next = this.input.charCodeAt(this.state.pos + 1);
  3689. if (code === 124 && next === 125) {
  3690. this.finishOp(9, 2);
  3691. return;
  3692. }
  3693. super.readToken_pipe_amp(code);
  3694. }
  3695. parseTopLevel(file, program) {
  3696. const fileNode = super.parseTopLevel(file, program);
  3697. if (this.state.hasFlowComment) {
  3698. this.raise(FlowErrors.UnterminatedFlowComment, this.state.curPosition());
  3699. }
  3700. return fileNode;
  3701. }
  3702. skipBlockComment() {
  3703. if (this.hasPlugin("flowComments") && this.skipFlowComment()) {
  3704. if (this.state.hasFlowComment) {
  3705. throw this.raise(FlowErrors.NestedFlowComment, this.state.startLoc);
  3706. }
  3707. this.hasFlowCommentCompletion();
  3708. const commentSkip = this.skipFlowComment();
  3709. if (commentSkip) {
  3710. this.state.pos += commentSkip;
  3711. this.state.hasFlowComment = true;
  3712. }
  3713. return;
  3714. }
  3715. return super.skipBlockComment(this.state.hasFlowComment ? "*-/" : "*/");
  3716. }
  3717. skipFlowComment() {
  3718. const {
  3719. pos
  3720. } = this.state;
  3721. let shiftToFirstNonWhiteSpace = 2;
  3722. while ([32, 9].includes(this.input.charCodeAt(pos + shiftToFirstNonWhiteSpace))) {
  3723. shiftToFirstNonWhiteSpace++;
  3724. }
  3725. const ch2 = this.input.charCodeAt(shiftToFirstNonWhiteSpace + pos);
  3726. const ch3 = this.input.charCodeAt(shiftToFirstNonWhiteSpace + pos + 1);
  3727. if (ch2 === 58 && ch3 === 58) {
  3728. return shiftToFirstNonWhiteSpace + 2;
  3729. }
  3730. if (this.input.slice(shiftToFirstNonWhiteSpace + pos, shiftToFirstNonWhiteSpace + pos + 12) === "flow-include") {
  3731. return shiftToFirstNonWhiteSpace + 12;
  3732. }
  3733. if (ch2 === 58 && ch3 !== 58) {
  3734. return shiftToFirstNonWhiteSpace;
  3735. }
  3736. return false;
  3737. }
  3738. hasFlowCommentCompletion() {
  3739. const end = this.input.indexOf("*/", this.state.pos);
  3740. if (end === -1) {
  3741. throw this.raise(Errors.UnterminatedComment, this.state.curPosition());
  3742. }
  3743. }
  3744. flowEnumErrorBooleanMemberNotInitialized(loc, {
  3745. enumName,
  3746. memberName
  3747. }) {
  3748. this.raise(FlowErrors.EnumBooleanMemberNotInitialized, loc, {
  3749. memberName,
  3750. enumName
  3751. });
  3752. }
  3753. flowEnumErrorInvalidMemberInitializer(loc, enumContext) {
  3754. return this.raise(!enumContext.explicitType ? FlowErrors.EnumInvalidMemberInitializerUnknownType : enumContext.explicitType === "symbol" ? FlowErrors.EnumInvalidMemberInitializerSymbolType : FlowErrors.EnumInvalidMemberInitializerPrimaryType, loc, enumContext);
  3755. }
  3756. flowEnumErrorNumberMemberNotInitialized(loc, details) {
  3757. this.raise(FlowErrors.EnumNumberMemberNotInitialized, loc, details);
  3758. }
  3759. flowEnumErrorStringMemberInconsistentlyInitialized(node, details) {
  3760. this.raise(FlowErrors.EnumStringMemberInconsistentlyInitialized, node, details);
  3761. }
  3762. flowEnumMemberInit() {
  3763. const startLoc = this.state.startLoc;
  3764. const endOfInit = () => this.match(12) || this.match(8);
  3765. switch (this.state.type) {
  3766. case 135:
  3767. {
  3768. const literal = this.parseNumericLiteral(this.state.value);
  3769. if (endOfInit()) {
  3770. return {
  3771. type: "number",
  3772. loc: literal.loc.start,
  3773. value: literal
  3774. };
  3775. }
  3776. return {
  3777. type: "invalid",
  3778. loc: startLoc
  3779. };
  3780. }
  3781. case 134:
  3782. {
  3783. const literal = this.parseStringLiteral(this.state.value);
  3784. if (endOfInit()) {
  3785. return {
  3786. type: "string",
  3787. loc: literal.loc.start,
  3788. value: literal
  3789. };
  3790. }
  3791. return {
  3792. type: "invalid",
  3793. loc: startLoc
  3794. };
  3795. }
  3796. case 85:
  3797. case 86:
  3798. {
  3799. const literal = this.parseBooleanLiteral(this.match(85));
  3800. if (endOfInit()) {
  3801. return {
  3802. type: "boolean",
  3803. loc: literal.loc.start,
  3804. value: literal
  3805. };
  3806. }
  3807. return {
  3808. type: "invalid",
  3809. loc: startLoc
  3810. };
  3811. }
  3812. default:
  3813. return {
  3814. type: "invalid",
  3815. loc: startLoc
  3816. };
  3817. }
  3818. }
  3819. flowEnumMemberRaw() {
  3820. const loc = this.state.startLoc;
  3821. const id = this.parseIdentifier(true);
  3822. const init = this.eat(29) ? this.flowEnumMemberInit() : {
  3823. type: "none",
  3824. loc
  3825. };
  3826. return {
  3827. id,
  3828. init
  3829. };
  3830. }
  3831. flowEnumCheckExplicitTypeMismatch(loc, context, expectedType) {
  3832. const {
  3833. explicitType
  3834. } = context;
  3835. if (explicitType === null) {
  3836. return;
  3837. }
  3838. if (explicitType !== expectedType) {
  3839. this.flowEnumErrorInvalidMemberInitializer(loc, context);
  3840. }
  3841. }
  3842. flowEnumMembers({
  3843. enumName,
  3844. explicitType
  3845. }) {
  3846. const seenNames = new Set();
  3847. const members = {
  3848. booleanMembers: [],
  3849. numberMembers: [],
  3850. stringMembers: [],
  3851. defaultedMembers: []
  3852. };
  3853. let hasUnknownMembers = false;
  3854. while (!this.match(8)) {
  3855. if (this.eat(21)) {
  3856. hasUnknownMembers = true;
  3857. break;
  3858. }
  3859. const memberNode = this.startNode();
  3860. const {
  3861. id,
  3862. init
  3863. } = this.flowEnumMemberRaw();
  3864. const memberName = id.name;
  3865. if (memberName === "") {
  3866. continue;
  3867. }
  3868. if (/^[a-z]/.test(memberName)) {
  3869. this.raise(FlowErrors.EnumInvalidMemberName, id, {
  3870. memberName,
  3871. suggestion: memberName[0].toUpperCase() + memberName.slice(1),
  3872. enumName
  3873. });
  3874. }
  3875. if (seenNames.has(memberName)) {
  3876. this.raise(FlowErrors.EnumDuplicateMemberName, id, {
  3877. memberName,
  3878. enumName
  3879. });
  3880. }
  3881. seenNames.add(memberName);
  3882. const context = {
  3883. enumName,
  3884. explicitType,
  3885. memberName
  3886. };
  3887. memberNode.id = id;
  3888. switch (init.type) {
  3889. case "boolean":
  3890. {
  3891. this.flowEnumCheckExplicitTypeMismatch(init.loc, context, "boolean");
  3892. memberNode.init = init.value;
  3893. members.booleanMembers.push(this.finishNode(memberNode, "EnumBooleanMember"));
  3894. break;
  3895. }
  3896. case "number":
  3897. {
  3898. this.flowEnumCheckExplicitTypeMismatch(init.loc, context, "number");
  3899. memberNode.init = init.value;
  3900. members.numberMembers.push(this.finishNode(memberNode, "EnumNumberMember"));
  3901. break;
  3902. }
  3903. case "string":
  3904. {
  3905. this.flowEnumCheckExplicitTypeMismatch(init.loc, context, "string");
  3906. memberNode.init = init.value;
  3907. members.stringMembers.push(this.finishNode(memberNode, "EnumStringMember"));
  3908. break;
  3909. }
  3910. case "invalid":
  3911. {
  3912. throw this.flowEnumErrorInvalidMemberInitializer(init.loc, context);
  3913. }
  3914. case "none":
  3915. {
  3916. switch (explicitType) {
  3917. case "boolean":
  3918. this.flowEnumErrorBooleanMemberNotInitialized(init.loc, context);
  3919. break;
  3920. case "number":
  3921. this.flowEnumErrorNumberMemberNotInitialized(init.loc, context);
  3922. break;
  3923. default:
  3924. members.defaultedMembers.push(this.finishNode(memberNode, "EnumDefaultedMember"));
  3925. }
  3926. }
  3927. }
  3928. if (!this.match(8)) {
  3929. this.expect(12);
  3930. }
  3931. }
  3932. return {
  3933. members,
  3934. hasUnknownMembers
  3935. };
  3936. }
  3937. flowEnumStringMembers(initializedMembers, defaultedMembers, {
  3938. enumName
  3939. }) {
  3940. if (initializedMembers.length === 0) {
  3941. return defaultedMembers;
  3942. } else if (defaultedMembers.length === 0) {
  3943. return initializedMembers;
  3944. } else if (defaultedMembers.length > initializedMembers.length) {
  3945. for (const member of initializedMembers) {
  3946. this.flowEnumErrorStringMemberInconsistentlyInitialized(member, {
  3947. enumName
  3948. });
  3949. }
  3950. return defaultedMembers;
  3951. } else {
  3952. for (const member of defaultedMembers) {
  3953. this.flowEnumErrorStringMemberInconsistentlyInitialized(member, {
  3954. enumName
  3955. });
  3956. }
  3957. return initializedMembers;
  3958. }
  3959. }
  3960. flowEnumParseExplicitType({
  3961. enumName
  3962. }) {
  3963. if (!this.eatContextual(102)) return null;
  3964. if (!tokenIsIdentifier(this.state.type)) {
  3965. throw this.raise(FlowErrors.EnumInvalidExplicitTypeUnknownSupplied, this.state.startLoc, {
  3966. enumName
  3967. });
  3968. }
  3969. const {
  3970. value
  3971. } = this.state;
  3972. this.next();
  3973. if (value !== "boolean" && value !== "number" && value !== "string" && value !== "symbol") {
  3974. this.raise(FlowErrors.EnumInvalidExplicitType, this.state.startLoc, {
  3975. enumName,
  3976. invalidEnumType: value
  3977. });
  3978. }
  3979. return value;
  3980. }
  3981. flowEnumBody(node, id) {
  3982. const enumName = id.name;
  3983. const nameLoc = id.loc.start;
  3984. const explicitType = this.flowEnumParseExplicitType({
  3985. enumName
  3986. });
  3987. this.expect(5);
  3988. const {
  3989. members,
  3990. hasUnknownMembers
  3991. } = this.flowEnumMembers({
  3992. enumName,
  3993. explicitType
  3994. });
  3995. node.hasUnknownMembers = hasUnknownMembers;
  3996. switch (explicitType) {
  3997. case "boolean":
  3998. node.explicitType = true;
  3999. node.members = members.booleanMembers;
  4000. this.expect(8);
  4001. return this.finishNode(node, "EnumBooleanBody");
  4002. case "number":
  4003. node.explicitType = true;
  4004. node.members = members.numberMembers;
  4005. this.expect(8);
  4006. return this.finishNode(node, "EnumNumberBody");
  4007. case "string":
  4008. node.explicitType = true;
  4009. node.members = this.flowEnumStringMembers(members.stringMembers, members.defaultedMembers, {
  4010. enumName
  4011. });
  4012. this.expect(8);
  4013. return this.finishNode(node, "EnumStringBody");
  4014. case "symbol":
  4015. node.members = members.defaultedMembers;
  4016. this.expect(8);
  4017. return this.finishNode(node, "EnumSymbolBody");
  4018. default:
  4019. {
  4020. const empty = () => {
  4021. node.members = [];
  4022. this.expect(8);
  4023. return this.finishNode(node, "EnumStringBody");
  4024. };
  4025. node.explicitType = false;
  4026. const boolsLen = members.booleanMembers.length;
  4027. const numsLen = members.numberMembers.length;
  4028. const strsLen = members.stringMembers.length;
  4029. const defaultedLen = members.defaultedMembers.length;
  4030. if (!boolsLen && !numsLen && !strsLen && !defaultedLen) {
  4031. return empty();
  4032. } else if (!boolsLen && !numsLen) {
  4033. node.members = this.flowEnumStringMembers(members.stringMembers, members.defaultedMembers, {
  4034. enumName
  4035. });
  4036. this.expect(8);
  4037. return this.finishNode(node, "EnumStringBody");
  4038. } else if (!numsLen && !strsLen && boolsLen >= defaultedLen) {
  4039. for (const member of members.defaultedMembers) {
  4040. this.flowEnumErrorBooleanMemberNotInitialized(member.loc.start, {
  4041. enumName,
  4042. memberName: member.id.name
  4043. });
  4044. }
  4045. node.members = members.booleanMembers;
  4046. this.expect(8);
  4047. return this.finishNode(node, "EnumBooleanBody");
  4048. } else if (!boolsLen && !strsLen && numsLen >= defaultedLen) {
  4049. for (const member of members.defaultedMembers) {
  4050. this.flowEnumErrorNumberMemberNotInitialized(member.loc.start, {
  4051. enumName,
  4052. memberName: member.id.name
  4053. });
  4054. }
  4055. node.members = members.numberMembers;
  4056. this.expect(8);
  4057. return this.finishNode(node, "EnumNumberBody");
  4058. } else {
  4059. this.raise(FlowErrors.EnumInconsistentMemberValues, nameLoc, {
  4060. enumName
  4061. });
  4062. return empty();
  4063. }
  4064. }
  4065. }
  4066. }
  4067. flowParseEnumDeclaration(node) {
  4068. const id = this.parseIdentifier();
  4069. node.id = id;
  4070. node.body = this.flowEnumBody(this.startNode(), id);
  4071. return this.finishNode(node, "EnumDeclaration");
  4072. }
  4073. jsxParseOpeningElementAfterName(node) {
  4074. if (this.shouldParseTypes()) {
  4075. if (this.match(47) || this.match(51)) {
  4076. node.typeArguments = this.flowParseTypeParameterInstantiationInExpression();
  4077. }
  4078. }
  4079. return super.jsxParseOpeningElementAfterName(node);
  4080. }
  4081. isLookaheadToken_lt() {
  4082. const next = this.nextTokenStart();
  4083. if (this.input.charCodeAt(next) === 60) {
  4084. const afterNext = this.input.charCodeAt(next + 1);
  4085. return afterNext !== 60 && afterNext !== 61;
  4086. }
  4087. return false;
  4088. }
  4089. reScan_lt_gt() {
  4090. const {
  4091. type
  4092. } = this.state;
  4093. if (type === 47) {
  4094. this.state.pos -= 1;
  4095. this.readToken_lt();
  4096. } else if (type === 48) {
  4097. this.state.pos -= 1;
  4098. this.readToken_gt();
  4099. }
  4100. }
  4101. reScan_lt() {
  4102. const {
  4103. type
  4104. } = this.state;
  4105. if (type === 51) {
  4106. this.state.pos -= 2;
  4107. this.finishOp(47, 1);
  4108. return 47;
  4109. }
  4110. return type;
  4111. }
  4112. maybeUnwrapTypeCastExpression(node) {
  4113. return node.type === "TypeCastExpression" ? node.expression : node;
  4114. }
  4115. };
  4116. const entities = {
  4117. __proto__: null,
  4118. quot: "\u0022",
  4119. amp: "&",
  4120. apos: "\u0027",
  4121. lt: "<",
  4122. gt: ">",
  4123. nbsp: "\u00A0",
  4124. iexcl: "\u00A1",
  4125. cent: "\u00A2",
  4126. pound: "\u00A3",
  4127. curren: "\u00A4",
  4128. yen: "\u00A5",
  4129. brvbar: "\u00A6",
  4130. sect: "\u00A7",
  4131. uml: "\u00A8",
  4132. copy: "\u00A9",
  4133. ordf: "\u00AA",
  4134. laquo: "\u00AB",
  4135. not: "\u00AC",
  4136. shy: "\u00AD",
  4137. reg: "\u00AE",
  4138. macr: "\u00AF",
  4139. deg: "\u00B0",
  4140. plusmn: "\u00B1",
  4141. sup2: "\u00B2",
  4142. sup3: "\u00B3",
  4143. acute: "\u00B4",
  4144. micro: "\u00B5",
  4145. para: "\u00B6",
  4146. middot: "\u00B7",
  4147. cedil: "\u00B8",
  4148. sup1: "\u00B9",
  4149. ordm: "\u00BA",
  4150. raquo: "\u00BB",
  4151. frac14: "\u00BC",
  4152. frac12: "\u00BD",
  4153. frac34: "\u00BE",
  4154. iquest: "\u00BF",
  4155. Agrave: "\u00C0",
  4156. Aacute: "\u00C1",
  4157. Acirc: "\u00C2",
  4158. Atilde: "\u00C3",
  4159. Auml: "\u00C4",
  4160. Aring: "\u00C5",
  4161. AElig: "\u00C6",
  4162. Ccedil: "\u00C7",
  4163. Egrave: "\u00C8",
  4164. Eacute: "\u00C9",
  4165. Ecirc: "\u00CA",
  4166. Euml: "\u00CB",
  4167. Igrave: "\u00CC",
  4168. Iacute: "\u00CD",
  4169. Icirc: "\u00CE",
  4170. Iuml: "\u00CF",
  4171. ETH: "\u00D0",
  4172. Ntilde: "\u00D1",
  4173. Ograve: "\u00D2",
  4174. Oacute: "\u00D3",
  4175. Ocirc: "\u00D4",
  4176. Otilde: "\u00D5",
  4177. Ouml: "\u00D6",
  4178. times: "\u00D7",
  4179. Oslash: "\u00D8",
  4180. Ugrave: "\u00D9",
  4181. Uacute: "\u00DA",
  4182. Ucirc: "\u00DB",
  4183. Uuml: "\u00DC",
  4184. Yacute: "\u00DD",
  4185. THORN: "\u00DE",
  4186. szlig: "\u00DF",
  4187. agrave: "\u00E0",
  4188. aacute: "\u00E1",
  4189. acirc: "\u00E2",
  4190. atilde: "\u00E3",
  4191. auml: "\u00E4",
  4192. aring: "\u00E5",
  4193. aelig: "\u00E6",
  4194. ccedil: "\u00E7",
  4195. egrave: "\u00E8",
  4196. eacute: "\u00E9",
  4197. ecirc: "\u00EA",
  4198. euml: "\u00EB",
  4199. igrave: "\u00EC",
  4200. iacute: "\u00ED",
  4201. icirc: "\u00EE",
  4202. iuml: "\u00EF",
  4203. eth: "\u00F0",
  4204. ntilde: "\u00F1",
  4205. ograve: "\u00F2",
  4206. oacute: "\u00F3",
  4207. ocirc: "\u00F4",
  4208. otilde: "\u00F5",
  4209. ouml: "\u00F6",
  4210. divide: "\u00F7",
  4211. oslash: "\u00F8",
  4212. ugrave: "\u00F9",
  4213. uacute: "\u00FA",
  4214. ucirc: "\u00FB",
  4215. uuml: "\u00FC",
  4216. yacute: "\u00FD",
  4217. thorn: "\u00FE",
  4218. yuml: "\u00FF",
  4219. OElig: "\u0152",
  4220. oelig: "\u0153",
  4221. Scaron: "\u0160",
  4222. scaron: "\u0161",
  4223. Yuml: "\u0178",
  4224. fnof: "\u0192",
  4225. circ: "\u02C6",
  4226. tilde: "\u02DC",
  4227. Alpha: "\u0391",
  4228. Beta: "\u0392",
  4229. Gamma: "\u0393",
  4230. Delta: "\u0394",
  4231. Epsilon: "\u0395",
  4232. Zeta: "\u0396",
  4233. Eta: "\u0397",
  4234. Theta: "\u0398",
  4235. Iota: "\u0399",
  4236. Kappa: "\u039A",
  4237. Lambda: "\u039B",
  4238. Mu: "\u039C",
  4239. Nu: "\u039D",
  4240. Xi: "\u039E",
  4241. Omicron: "\u039F",
  4242. Pi: "\u03A0",
  4243. Rho: "\u03A1",
  4244. Sigma: "\u03A3",
  4245. Tau: "\u03A4",
  4246. Upsilon: "\u03A5",
  4247. Phi: "\u03A6",
  4248. Chi: "\u03A7",
  4249. Psi: "\u03A8",
  4250. Omega: "\u03A9",
  4251. alpha: "\u03B1",
  4252. beta: "\u03B2",
  4253. gamma: "\u03B3",
  4254. delta: "\u03B4",
  4255. epsilon: "\u03B5",
  4256. zeta: "\u03B6",
  4257. eta: "\u03B7",
  4258. theta: "\u03B8",
  4259. iota: "\u03B9",
  4260. kappa: "\u03BA",
  4261. lambda: "\u03BB",
  4262. mu: "\u03BC",
  4263. nu: "\u03BD",
  4264. xi: "\u03BE",
  4265. omicron: "\u03BF",
  4266. pi: "\u03C0",
  4267. rho: "\u03C1",
  4268. sigmaf: "\u03C2",
  4269. sigma: "\u03C3",
  4270. tau: "\u03C4",
  4271. upsilon: "\u03C5",
  4272. phi: "\u03C6",
  4273. chi: "\u03C7",
  4274. psi: "\u03C8",
  4275. omega: "\u03C9",
  4276. thetasym: "\u03D1",
  4277. upsih: "\u03D2",
  4278. piv: "\u03D6",
  4279. ensp: "\u2002",
  4280. emsp: "\u2003",
  4281. thinsp: "\u2009",
  4282. zwnj: "\u200C",
  4283. zwj: "\u200D",
  4284. lrm: "\u200E",
  4285. rlm: "\u200F",
  4286. ndash: "\u2013",
  4287. mdash: "\u2014",
  4288. lsquo: "\u2018",
  4289. rsquo: "\u2019",
  4290. sbquo: "\u201A",
  4291. ldquo: "\u201C",
  4292. rdquo: "\u201D",
  4293. bdquo: "\u201E",
  4294. dagger: "\u2020",
  4295. Dagger: "\u2021",
  4296. bull: "\u2022",
  4297. hellip: "\u2026",
  4298. permil: "\u2030",
  4299. prime: "\u2032",
  4300. Prime: "\u2033",
  4301. lsaquo: "\u2039",
  4302. rsaquo: "\u203A",
  4303. oline: "\u203E",
  4304. frasl: "\u2044",
  4305. euro: "\u20AC",
  4306. image: "\u2111",
  4307. weierp: "\u2118",
  4308. real: "\u211C",
  4309. trade: "\u2122",
  4310. alefsym: "\u2135",
  4311. larr: "\u2190",
  4312. uarr: "\u2191",
  4313. rarr: "\u2192",
  4314. darr: "\u2193",
  4315. harr: "\u2194",
  4316. crarr: "\u21B5",
  4317. lArr: "\u21D0",
  4318. uArr: "\u21D1",
  4319. rArr: "\u21D2",
  4320. dArr: "\u21D3",
  4321. hArr: "\u21D4",
  4322. forall: "\u2200",
  4323. part: "\u2202",
  4324. exist: "\u2203",
  4325. empty: "\u2205",
  4326. nabla: "\u2207",
  4327. isin: "\u2208",
  4328. notin: "\u2209",
  4329. ni: "\u220B",
  4330. prod: "\u220F",
  4331. sum: "\u2211",
  4332. minus: "\u2212",
  4333. lowast: "\u2217",
  4334. radic: "\u221A",
  4335. prop: "\u221D",
  4336. infin: "\u221E",
  4337. ang: "\u2220",
  4338. and: "\u2227",
  4339. or: "\u2228",
  4340. cap: "\u2229",
  4341. cup: "\u222A",
  4342. int: "\u222B",
  4343. there4: "\u2234",
  4344. sim: "\u223C",
  4345. cong: "\u2245",
  4346. asymp: "\u2248",
  4347. ne: "\u2260",
  4348. equiv: "\u2261",
  4349. le: "\u2264",
  4350. ge: "\u2265",
  4351. sub: "\u2282",
  4352. sup: "\u2283",
  4353. nsub: "\u2284",
  4354. sube: "\u2286",
  4355. supe: "\u2287",
  4356. oplus: "\u2295",
  4357. otimes: "\u2297",
  4358. perp: "\u22A5",
  4359. sdot: "\u22C5",
  4360. lceil: "\u2308",
  4361. rceil: "\u2309",
  4362. lfloor: "\u230A",
  4363. rfloor: "\u230B",
  4364. lang: "\u2329",
  4365. rang: "\u232A",
  4366. loz: "\u25CA",
  4367. spades: "\u2660",
  4368. clubs: "\u2663",
  4369. hearts: "\u2665",
  4370. diams: "\u2666"
  4371. };
  4372. const lineBreak = /\r\n|[\r\n\u2028\u2029]/;
  4373. const lineBreakG = new RegExp(lineBreak.source, "g");
  4374. function isNewLine(code) {
  4375. switch (code) {
  4376. case 10:
  4377. case 13:
  4378. case 8232:
  4379. case 8233:
  4380. return true;
  4381. default:
  4382. return false;
  4383. }
  4384. }
  4385. function hasNewLine(input, start, end) {
  4386. for (let i = start; i < end; i++) {
  4387. if (isNewLine(input.charCodeAt(i))) {
  4388. return true;
  4389. }
  4390. }
  4391. return false;
  4392. }
  4393. const skipWhiteSpace = /(?:\s|\/\/.*|\/\*[^]*?\*\/)*/g;
  4394. const skipWhiteSpaceInLine = /(?:[^\S\n\r\u2028\u2029]|\/\/.*|\/\*.*?\*\/)*/g;
  4395. function isWhitespace(code) {
  4396. switch (code) {
  4397. case 0x0009:
  4398. case 0x000b:
  4399. case 0x000c:
  4400. case 32:
  4401. case 160:
  4402. case 5760:
  4403. case 0x2000:
  4404. case 0x2001:
  4405. case 0x2002:
  4406. case 0x2003:
  4407. case 0x2004:
  4408. case 0x2005:
  4409. case 0x2006:
  4410. case 0x2007:
  4411. case 0x2008:
  4412. case 0x2009:
  4413. case 0x200a:
  4414. case 0x202f:
  4415. case 0x205f:
  4416. case 0x3000:
  4417. case 0xfeff:
  4418. return true;
  4419. default:
  4420. return false;
  4421. }
  4422. }
  4423. const JsxErrors = ParseErrorEnum`jsx`({
  4424. AttributeIsEmpty: "JSX attributes must only be assigned a non-empty expression.",
  4425. MissingClosingTagElement: ({
  4426. openingTagName
  4427. }) => `Expected corresponding JSX closing tag for <${openingTagName}>.`,
  4428. MissingClosingTagFragment: "Expected corresponding JSX closing tag for <>.",
  4429. UnexpectedSequenceExpression: "Sequence expressions cannot be directly nested inside JSX. Did you mean to wrap it in parentheses (...)?",
  4430. UnexpectedToken: ({
  4431. unexpected,
  4432. HTMLEntity
  4433. }) => `Unexpected token \`${unexpected}\`. Did you mean \`${HTMLEntity}\` or \`{'${unexpected}'}\`?`,
  4434. UnsupportedJsxValue: "JSX value should be either an expression or a quoted JSX text.",
  4435. UnterminatedJsxContent: "Unterminated JSX contents.",
  4436. UnwrappedAdjacentJSXElements: "Adjacent JSX elements must be wrapped in an enclosing tag. Did you want a JSX fragment <>...</>?"
  4437. });
  4438. function isFragment(object) {
  4439. return object ? object.type === "JSXOpeningFragment" || object.type === "JSXClosingFragment" : false;
  4440. }
  4441. function getQualifiedJSXName(object) {
  4442. if (object.type === "JSXIdentifier") {
  4443. return object.name;
  4444. }
  4445. if (object.type === "JSXNamespacedName") {
  4446. return object.namespace.name + ":" + object.name.name;
  4447. }
  4448. if (object.type === "JSXMemberExpression") {
  4449. return getQualifiedJSXName(object.object) + "." + getQualifiedJSXName(object.property);
  4450. }
  4451. throw new Error("Node had unexpected type: " + object.type);
  4452. }
  4453. var jsx = superClass => class JSXParserMixin extends superClass {
  4454. jsxReadToken() {
  4455. let out = "";
  4456. let chunkStart = this.state.pos;
  4457. for (;;) {
  4458. if (this.state.pos >= this.length) {
  4459. throw this.raise(JsxErrors.UnterminatedJsxContent, this.state.startLoc);
  4460. }
  4461. const ch = this.input.charCodeAt(this.state.pos);
  4462. switch (ch) {
  4463. case 60:
  4464. case 123:
  4465. if (this.state.pos === this.state.start) {
  4466. if (ch === 60 && this.state.canStartJSXElement) {
  4467. ++this.state.pos;
  4468. this.finishToken(143);
  4469. } else {
  4470. super.getTokenFromCode(ch);
  4471. }
  4472. return;
  4473. }
  4474. out += this.input.slice(chunkStart, this.state.pos);
  4475. this.finishToken(142, out);
  4476. return;
  4477. case 38:
  4478. out += this.input.slice(chunkStart, this.state.pos);
  4479. out += this.jsxReadEntity();
  4480. chunkStart = this.state.pos;
  4481. break;
  4482. case 62:
  4483. case 125:
  4484. default:
  4485. if (isNewLine(ch)) {
  4486. out += this.input.slice(chunkStart, this.state.pos);
  4487. out += this.jsxReadNewLine(true);
  4488. chunkStart = this.state.pos;
  4489. } else {
  4490. ++this.state.pos;
  4491. }
  4492. }
  4493. }
  4494. }
  4495. jsxReadNewLine(normalizeCRLF) {
  4496. const ch = this.input.charCodeAt(this.state.pos);
  4497. let out;
  4498. ++this.state.pos;
  4499. if (ch === 13 && this.input.charCodeAt(this.state.pos) === 10) {
  4500. ++this.state.pos;
  4501. out = normalizeCRLF ? "\n" : "\r\n";
  4502. } else {
  4503. out = String.fromCharCode(ch);
  4504. }
  4505. ++this.state.curLine;
  4506. this.state.lineStart = this.state.pos;
  4507. return out;
  4508. }
  4509. jsxReadString(quote) {
  4510. let out = "";
  4511. let chunkStart = ++this.state.pos;
  4512. for (;;) {
  4513. if (this.state.pos >= this.length) {
  4514. throw this.raise(Errors.UnterminatedString, this.state.startLoc);
  4515. }
  4516. const ch = this.input.charCodeAt(this.state.pos);
  4517. if (ch === quote) break;
  4518. if (ch === 38) {
  4519. out += this.input.slice(chunkStart, this.state.pos);
  4520. out += this.jsxReadEntity();
  4521. chunkStart = this.state.pos;
  4522. } else if (isNewLine(ch)) {
  4523. out += this.input.slice(chunkStart, this.state.pos);
  4524. out += this.jsxReadNewLine(false);
  4525. chunkStart = this.state.pos;
  4526. } else {
  4527. ++this.state.pos;
  4528. }
  4529. }
  4530. out += this.input.slice(chunkStart, this.state.pos++);
  4531. this.finishToken(134, out);
  4532. }
  4533. jsxReadEntity() {
  4534. const startPos = ++this.state.pos;
  4535. if (this.codePointAtPos(this.state.pos) === 35) {
  4536. ++this.state.pos;
  4537. let radix = 10;
  4538. if (this.codePointAtPos(this.state.pos) === 120) {
  4539. radix = 16;
  4540. ++this.state.pos;
  4541. }
  4542. const codePoint = this.readInt(radix, undefined, false, "bail");
  4543. if (codePoint !== null && this.codePointAtPos(this.state.pos) === 59) {
  4544. ++this.state.pos;
  4545. return String.fromCodePoint(codePoint);
  4546. }
  4547. } else {
  4548. let count = 0;
  4549. let semi = false;
  4550. while (count++ < 10 && this.state.pos < this.length && !(semi = this.codePointAtPos(this.state.pos) === 59)) {
  4551. ++this.state.pos;
  4552. }
  4553. if (semi) {
  4554. const desc = this.input.slice(startPos, this.state.pos);
  4555. const entity = entities[desc];
  4556. ++this.state.pos;
  4557. if (entity) {
  4558. return entity;
  4559. }
  4560. }
  4561. }
  4562. this.state.pos = startPos;
  4563. return "&";
  4564. }
  4565. jsxReadWord() {
  4566. let ch;
  4567. const start = this.state.pos;
  4568. do {
  4569. ch = this.input.charCodeAt(++this.state.pos);
  4570. } while (isIdentifierChar(ch) || ch === 45);
  4571. this.finishToken(141, this.input.slice(start, this.state.pos));
  4572. }
  4573. jsxParseIdentifier() {
  4574. const node = this.startNode();
  4575. if (this.match(141)) {
  4576. node.name = this.state.value;
  4577. } else if (tokenIsKeyword(this.state.type)) {
  4578. node.name = tokenLabelName(this.state.type);
  4579. } else {
  4580. this.unexpected();
  4581. }
  4582. this.next();
  4583. return this.finishNode(node, "JSXIdentifier");
  4584. }
  4585. jsxParseNamespacedName() {
  4586. const startLoc = this.state.startLoc;
  4587. const name = this.jsxParseIdentifier();
  4588. if (!this.eat(14)) return name;
  4589. const node = this.startNodeAt(startLoc);
  4590. node.namespace = name;
  4591. node.name = this.jsxParseIdentifier();
  4592. return this.finishNode(node, "JSXNamespacedName");
  4593. }
  4594. jsxParseElementName() {
  4595. const startLoc = this.state.startLoc;
  4596. let node = this.jsxParseNamespacedName();
  4597. if (node.type === "JSXNamespacedName") {
  4598. return node;
  4599. }
  4600. while (this.eat(16)) {
  4601. const newNode = this.startNodeAt(startLoc);
  4602. newNode.object = node;
  4603. newNode.property = this.jsxParseIdentifier();
  4604. node = this.finishNode(newNode, "JSXMemberExpression");
  4605. }
  4606. return node;
  4607. }
  4608. jsxParseAttributeValue() {
  4609. let node;
  4610. switch (this.state.type) {
  4611. case 5:
  4612. node = this.startNode();
  4613. this.setContext(types.brace);
  4614. this.next();
  4615. node = this.jsxParseExpressionContainer(node, types.j_oTag);
  4616. if (node.expression.type === "JSXEmptyExpression") {
  4617. this.raise(JsxErrors.AttributeIsEmpty, node);
  4618. }
  4619. return node;
  4620. case 143:
  4621. case 134:
  4622. return this.parseExprAtom();
  4623. default:
  4624. throw this.raise(JsxErrors.UnsupportedJsxValue, this.state.startLoc);
  4625. }
  4626. }
  4627. jsxParseEmptyExpression() {
  4628. const node = this.startNodeAt(this.state.lastTokEndLoc);
  4629. return this.finishNodeAt(node, "JSXEmptyExpression", this.state.startLoc);
  4630. }
  4631. jsxParseSpreadChild(node) {
  4632. this.next();
  4633. node.expression = this.parseExpression();
  4634. this.setContext(types.j_expr);
  4635. this.state.canStartJSXElement = true;
  4636. this.expect(8);
  4637. return this.finishNode(node, "JSXSpreadChild");
  4638. }
  4639. jsxParseExpressionContainer(node, previousContext) {
  4640. if (this.match(8)) {
  4641. node.expression = this.jsxParseEmptyExpression();
  4642. } else {
  4643. const expression = this.parseExpression();
  4644. node.expression = expression;
  4645. }
  4646. this.setContext(previousContext);
  4647. this.state.canStartJSXElement = true;
  4648. this.expect(8);
  4649. return this.finishNode(node, "JSXExpressionContainer");
  4650. }
  4651. jsxParseAttribute() {
  4652. const node = this.startNode();
  4653. if (this.match(5)) {
  4654. this.setContext(types.brace);
  4655. this.next();
  4656. this.expect(21);
  4657. node.argument = this.parseMaybeAssignAllowIn();
  4658. this.setContext(types.j_oTag);
  4659. this.state.canStartJSXElement = true;
  4660. this.expect(8);
  4661. return this.finishNode(node, "JSXSpreadAttribute");
  4662. }
  4663. node.name = this.jsxParseNamespacedName();
  4664. node.value = this.eat(29) ? this.jsxParseAttributeValue() : null;
  4665. return this.finishNode(node, "JSXAttribute");
  4666. }
  4667. jsxParseOpeningElementAt(startLoc) {
  4668. const node = this.startNodeAt(startLoc);
  4669. if (this.eat(144)) {
  4670. return this.finishNode(node, "JSXOpeningFragment");
  4671. }
  4672. node.name = this.jsxParseElementName();
  4673. return this.jsxParseOpeningElementAfterName(node);
  4674. }
  4675. jsxParseOpeningElementAfterName(node) {
  4676. const attributes = [];
  4677. while (!this.match(56) && !this.match(144)) {
  4678. attributes.push(this.jsxParseAttribute());
  4679. }
  4680. node.attributes = attributes;
  4681. node.selfClosing = this.eat(56);
  4682. this.expect(144);
  4683. return this.finishNode(node, "JSXOpeningElement");
  4684. }
  4685. jsxParseClosingElementAt(startLoc) {
  4686. const node = this.startNodeAt(startLoc);
  4687. if (this.eat(144)) {
  4688. return this.finishNode(node, "JSXClosingFragment");
  4689. }
  4690. node.name = this.jsxParseElementName();
  4691. this.expect(144);
  4692. return this.finishNode(node, "JSXClosingElement");
  4693. }
  4694. jsxParseElementAt(startLoc) {
  4695. const node = this.startNodeAt(startLoc);
  4696. const children = [];
  4697. const openingElement = this.jsxParseOpeningElementAt(startLoc);
  4698. let closingElement = null;
  4699. if (!openingElement.selfClosing) {
  4700. contents: for (;;) {
  4701. switch (this.state.type) {
  4702. case 143:
  4703. startLoc = this.state.startLoc;
  4704. this.next();
  4705. if (this.eat(56)) {
  4706. closingElement = this.jsxParseClosingElementAt(startLoc);
  4707. break contents;
  4708. }
  4709. children.push(this.jsxParseElementAt(startLoc));
  4710. break;
  4711. case 142:
  4712. children.push(this.parseLiteral(this.state.value, "JSXText"));
  4713. break;
  4714. case 5:
  4715. {
  4716. const node = this.startNode();
  4717. this.setContext(types.brace);
  4718. this.next();
  4719. if (this.match(21)) {
  4720. children.push(this.jsxParseSpreadChild(node));
  4721. } else {
  4722. children.push(this.jsxParseExpressionContainer(node, types.j_expr));
  4723. }
  4724. break;
  4725. }
  4726. default:
  4727. this.unexpected();
  4728. }
  4729. }
  4730. if (isFragment(openingElement) && !isFragment(closingElement) && closingElement !== null) {
  4731. this.raise(JsxErrors.MissingClosingTagFragment, closingElement);
  4732. } else if (!isFragment(openingElement) && isFragment(closingElement)) {
  4733. this.raise(JsxErrors.MissingClosingTagElement, closingElement, {
  4734. openingTagName: getQualifiedJSXName(openingElement.name)
  4735. });
  4736. } else if (!isFragment(openingElement) && !isFragment(closingElement)) {
  4737. if (getQualifiedJSXName(closingElement.name) !== getQualifiedJSXName(openingElement.name)) {
  4738. this.raise(JsxErrors.MissingClosingTagElement, closingElement, {
  4739. openingTagName: getQualifiedJSXName(openingElement.name)
  4740. });
  4741. }
  4742. }
  4743. }
  4744. if (isFragment(openingElement)) {
  4745. node.openingFragment = openingElement;
  4746. node.closingFragment = closingElement;
  4747. } else {
  4748. node.openingElement = openingElement;
  4749. node.closingElement = closingElement;
  4750. }
  4751. node.children = children;
  4752. if (this.match(47)) {
  4753. throw this.raise(JsxErrors.UnwrappedAdjacentJSXElements, this.state.startLoc);
  4754. }
  4755. return isFragment(openingElement) ? this.finishNode(node, "JSXFragment") : this.finishNode(node, "JSXElement");
  4756. }
  4757. jsxParseElement() {
  4758. const startLoc = this.state.startLoc;
  4759. this.next();
  4760. return this.jsxParseElementAt(startLoc);
  4761. }
  4762. setContext(newContext) {
  4763. const {
  4764. context
  4765. } = this.state;
  4766. context[context.length - 1] = newContext;
  4767. }
  4768. parseExprAtom(refExpressionErrors) {
  4769. if (this.match(143)) {
  4770. return this.jsxParseElement();
  4771. } else if (this.match(47) && this.input.charCodeAt(this.state.pos) !== 33) {
  4772. this.replaceToken(143);
  4773. return this.jsxParseElement();
  4774. } else {
  4775. return super.parseExprAtom(refExpressionErrors);
  4776. }
  4777. }
  4778. skipSpace() {
  4779. const curContext = this.curContext();
  4780. if (!curContext.preserveSpace) super.skipSpace();
  4781. }
  4782. getTokenFromCode(code) {
  4783. const context = this.curContext();
  4784. if (context === types.j_expr) {
  4785. this.jsxReadToken();
  4786. return;
  4787. }
  4788. if (context === types.j_oTag || context === types.j_cTag) {
  4789. if (isIdentifierStart(code)) {
  4790. this.jsxReadWord();
  4791. return;
  4792. }
  4793. if (code === 62) {
  4794. ++this.state.pos;
  4795. this.finishToken(144);
  4796. return;
  4797. }
  4798. if ((code === 34 || code === 39) && context === types.j_oTag) {
  4799. this.jsxReadString(code);
  4800. return;
  4801. }
  4802. }
  4803. if (code === 60 && this.state.canStartJSXElement && this.input.charCodeAt(this.state.pos + 1) !== 33) {
  4804. ++this.state.pos;
  4805. this.finishToken(143);
  4806. return;
  4807. }
  4808. super.getTokenFromCode(code);
  4809. }
  4810. updateContext(prevType) {
  4811. const {
  4812. context,
  4813. type
  4814. } = this.state;
  4815. if (type === 56 && prevType === 143) {
  4816. context.splice(-2, 2, types.j_cTag);
  4817. this.state.canStartJSXElement = false;
  4818. } else if (type === 143) {
  4819. context.push(types.j_oTag);
  4820. } else if (type === 144) {
  4821. const out = context[context.length - 1];
  4822. if (out === types.j_oTag && prevType === 56 || out === types.j_cTag) {
  4823. context.pop();
  4824. this.state.canStartJSXElement = context[context.length - 1] === types.j_expr;
  4825. } else {
  4826. this.setContext(types.j_expr);
  4827. this.state.canStartJSXElement = true;
  4828. }
  4829. } else {
  4830. this.state.canStartJSXElement = tokenComesBeforeExpression(type);
  4831. }
  4832. }
  4833. };
  4834. class TypeScriptScope extends Scope {
  4835. constructor(...args) {
  4836. super(...args);
  4837. this.tsNames = new Map();
  4838. }
  4839. }
  4840. class TypeScriptScopeHandler extends ScopeHandler {
  4841. constructor(...args) {
  4842. super(...args);
  4843. this.importsStack = [];
  4844. }
  4845. createScope(flags) {
  4846. this.importsStack.push(new Set());
  4847. return new TypeScriptScope(flags);
  4848. }
  4849. enter(flags) {
  4850. if (flags === 512) {
  4851. this.importsStack.push(new Set());
  4852. }
  4853. super.enter(flags);
  4854. }
  4855. exit() {
  4856. const flags = super.exit();
  4857. if (flags === 512) {
  4858. this.importsStack.pop();
  4859. }
  4860. return flags;
  4861. }
  4862. hasImport(name, allowShadow) {
  4863. const len = this.importsStack.length;
  4864. if (this.importsStack[len - 1].has(name)) {
  4865. return true;
  4866. }
  4867. if (!allowShadow && len > 1) {
  4868. for (let i = 0; i < len - 1; i++) {
  4869. if (this.importsStack[i].has(name)) return true;
  4870. }
  4871. }
  4872. return false;
  4873. }
  4874. declareName(name, bindingType, loc) {
  4875. if (bindingType & 4096) {
  4876. if (this.hasImport(name, true)) {
  4877. this.parser.raise(Errors.VarRedeclaration, loc, {
  4878. identifierName: name
  4879. });
  4880. }
  4881. this.importsStack[this.importsStack.length - 1].add(name);
  4882. return;
  4883. }
  4884. const scope = this.currentScope();
  4885. let type = scope.tsNames.get(name) || 0;
  4886. if (bindingType & 1024) {
  4887. this.maybeExportDefined(scope, name);
  4888. scope.tsNames.set(name, type | 16);
  4889. return;
  4890. }
  4891. super.declareName(name, bindingType, loc);
  4892. if (bindingType & 2) {
  4893. if (!(bindingType & 1)) {
  4894. this.checkRedeclarationInScope(scope, name, bindingType, loc);
  4895. this.maybeExportDefined(scope, name);
  4896. }
  4897. type = type | 1;
  4898. }
  4899. if (bindingType & 256) {
  4900. type = type | 2;
  4901. }
  4902. if (bindingType & 512) {
  4903. type = type | 4;
  4904. }
  4905. if (bindingType & 128) {
  4906. type = type | 8;
  4907. }
  4908. if (type) scope.tsNames.set(name, type);
  4909. }
  4910. isRedeclaredInScope(scope, name, bindingType) {
  4911. const type = scope.tsNames.get(name);
  4912. if ((type & 2) > 0) {
  4913. if (bindingType & 256) {
  4914. const isConst = !!(bindingType & 512);
  4915. const wasConst = (type & 4) > 0;
  4916. return isConst !== wasConst;
  4917. }
  4918. return true;
  4919. }
  4920. if (bindingType & 128 && (type & 8) > 0) {
  4921. if (scope.names.get(name) & 2) {
  4922. return !!(bindingType & 1);
  4923. } else {
  4924. return false;
  4925. }
  4926. }
  4927. if (bindingType & 2 && (type & 1) > 0) {
  4928. return true;
  4929. }
  4930. return super.isRedeclaredInScope(scope, name, bindingType);
  4931. }
  4932. checkLocalExport(id) {
  4933. const {
  4934. name
  4935. } = id;
  4936. if (this.hasImport(name)) return;
  4937. const len = this.scopeStack.length;
  4938. for (let i = len - 1; i >= 0; i--) {
  4939. const scope = this.scopeStack[i];
  4940. const type = scope.tsNames.get(name);
  4941. if ((type & 1) > 0 || (type & 16) > 0) {
  4942. return;
  4943. }
  4944. }
  4945. super.checkLocalExport(id);
  4946. }
  4947. }
  4948. class ProductionParameterHandler {
  4949. constructor() {
  4950. this.stacks = [];
  4951. }
  4952. enter(flags) {
  4953. this.stacks.push(flags);
  4954. }
  4955. exit() {
  4956. this.stacks.pop();
  4957. }
  4958. currentFlags() {
  4959. return this.stacks[this.stacks.length - 1];
  4960. }
  4961. get hasAwait() {
  4962. return (this.currentFlags() & 2) > 0;
  4963. }
  4964. get hasYield() {
  4965. return (this.currentFlags() & 1) > 0;
  4966. }
  4967. get hasReturn() {
  4968. return (this.currentFlags() & 4) > 0;
  4969. }
  4970. get hasIn() {
  4971. return (this.currentFlags() & 8) > 0;
  4972. }
  4973. }
  4974. function functionFlags(isAsync, isGenerator) {
  4975. return (isAsync ? 2 : 0) | (isGenerator ? 1 : 0);
  4976. }
  4977. class BaseParser {
  4978. constructor() {
  4979. this.sawUnambiguousESM = false;
  4980. this.ambiguousScriptDifferentAst = false;
  4981. }
  4982. sourceToOffsetPos(sourcePos) {
  4983. return sourcePos + this.startIndex;
  4984. }
  4985. offsetToSourcePos(offsetPos) {
  4986. return offsetPos - this.startIndex;
  4987. }
  4988. hasPlugin(pluginConfig) {
  4989. if (typeof pluginConfig === "string") {
  4990. return this.plugins.has(pluginConfig);
  4991. } else {
  4992. const [pluginName, pluginOptions] = pluginConfig;
  4993. if (!this.hasPlugin(pluginName)) {
  4994. return false;
  4995. }
  4996. const actualOptions = this.plugins.get(pluginName);
  4997. for (const key of Object.keys(pluginOptions)) {
  4998. if ((actualOptions == null ? void 0 : actualOptions[key]) !== pluginOptions[key]) {
  4999. return false;
  5000. }
  5001. }
  5002. return true;
  5003. }
  5004. }
  5005. getPluginOption(plugin, name) {
  5006. var _this$plugins$get;
  5007. return (_this$plugins$get = this.plugins.get(plugin)) == null ? void 0 : _this$plugins$get[name];
  5008. }
  5009. }
  5010. function setTrailingComments(node, comments) {
  5011. if (node.trailingComments === undefined) {
  5012. node.trailingComments = comments;
  5013. } else {
  5014. node.trailingComments.unshift(...comments);
  5015. }
  5016. }
  5017. function setLeadingComments(node, comments) {
  5018. if (node.leadingComments === undefined) {
  5019. node.leadingComments = comments;
  5020. } else {
  5021. node.leadingComments.unshift(...comments);
  5022. }
  5023. }
  5024. function setInnerComments(node, comments) {
  5025. if (node.innerComments === undefined) {
  5026. node.innerComments = comments;
  5027. } else {
  5028. node.innerComments.unshift(...comments);
  5029. }
  5030. }
  5031. function adjustInnerComments(node, elements, commentWS) {
  5032. let lastElement = null;
  5033. let i = elements.length;
  5034. while (lastElement === null && i > 0) {
  5035. lastElement = elements[--i];
  5036. }
  5037. if (lastElement === null || lastElement.start > commentWS.start) {
  5038. setInnerComments(node, commentWS.comments);
  5039. } else {
  5040. setTrailingComments(lastElement, commentWS.comments);
  5041. }
  5042. }
  5043. class CommentsParser extends BaseParser {
  5044. addComment(comment) {
  5045. if (this.filename) comment.loc.filename = this.filename;
  5046. const {
  5047. commentsLen
  5048. } = this.state;
  5049. if (this.comments.length !== commentsLen) {
  5050. this.comments.length = commentsLen;
  5051. }
  5052. this.comments.push(comment);
  5053. this.state.commentsLen++;
  5054. }
  5055. processComment(node) {
  5056. const {
  5057. commentStack
  5058. } = this.state;
  5059. const commentStackLength = commentStack.length;
  5060. if (commentStackLength === 0) return;
  5061. let i = commentStackLength - 1;
  5062. const lastCommentWS = commentStack[i];
  5063. if (lastCommentWS.start === node.end) {
  5064. lastCommentWS.leadingNode = node;
  5065. i--;
  5066. }
  5067. const {
  5068. start: nodeStart
  5069. } = node;
  5070. for (; i >= 0; i--) {
  5071. const commentWS = commentStack[i];
  5072. const commentEnd = commentWS.end;
  5073. if (commentEnd > nodeStart) {
  5074. commentWS.containingNode = node;
  5075. this.finalizeComment(commentWS);
  5076. commentStack.splice(i, 1);
  5077. } else {
  5078. if (commentEnd === nodeStart) {
  5079. commentWS.trailingNode = node;
  5080. }
  5081. break;
  5082. }
  5083. }
  5084. }
  5085. finalizeComment(commentWS) {
  5086. var _node$options;
  5087. const {
  5088. comments
  5089. } = commentWS;
  5090. if (commentWS.leadingNode !== null || commentWS.trailingNode !== null) {
  5091. if (commentWS.leadingNode !== null) {
  5092. setTrailingComments(commentWS.leadingNode, comments);
  5093. }
  5094. if (commentWS.trailingNode !== null) {
  5095. setLeadingComments(commentWS.trailingNode, comments);
  5096. }
  5097. } else {
  5098. const {
  5099. containingNode: node,
  5100. start: commentStart
  5101. } = commentWS;
  5102. if (this.input.charCodeAt(this.offsetToSourcePos(commentStart) - 1) === 44) {
  5103. switch (node.type) {
  5104. case "ObjectExpression":
  5105. case "ObjectPattern":
  5106. case "RecordExpression":
  5107. adjustInnerComments(node, node.properties, commentWS);
  5108. break;
  5109. case "CallExpression":
  5110. case "OptionalCallExpression":
  5111. adjustInnerComments(node, node.arguments, commentWS);
  5112. break;
  5113. case "ImportExpression":
  5114. adjustInnerComments(node, [node.source, (_node$options = node.options) != null ? _node$options : null], commentWS);
  5115. break;
  5116. case "FunctionDeclaration":
  5117. case "FunctionExpression":
  5118. case "ArrowFunctionExpression":
  5119. case "ObjectMethod":
  5120. case "ClassMethod":
  5121. case "ClassPrivateMethod":
  5122. adjustInnerComments(node, node.params, commentWS);
  5123. break;
  5124. case "ArrayExpression":
  5125. case "ArrayPattern":
  5126. case "TupleExpression":
  5127. adjustInnerComments(node, node.elements, commentWS);
  5128. break;
  5129. case "ExportNamedDeclaration":
  5130. case "ImportDeclaration":
  5131. adjustInnerComments(node, node.specifiers, commentWS);
  5132. break;
  5133. case "TSEnumDeclaration":
  5134. {
  5135. adjustInnerComments(node, node.members, commentWS);
  5136. }
  5137. break;
  5138. case "TSEnumBody":
  5139. adjustInnerComments(node, node.members, commentWS);
  5140. break;
  5141. default:
  5142. {
  5143. setInnerComments(node, comments);
  5144. }
  5145. }
  5146. } else {
  5147. setInnerComments(node, comments);
  5148. }
  5149. }
  5150. }
  5151. finalizeRemainingComments() {
  5152. const {
  5153. commentStack
  5154. } = this.state;
  5155. for (let i = commentStack.length - 1; i >= 0; i--) {
  5156. this.finalizeComment(commentStack[i]);
  5157. }
  5158. this.state.commentStack = [];
  5159. }
  5160. resetPreviousNodeTrailingComments(node) {
  5161. const {
  5162. commentStack
  5163. } = this.state;
  5164. const {
  5165. length
  5166. } = commentStack;
  5167. if (length === 0) return;
  5168. const commentWS = commentStack[length - 1];
  5169. if (commentWS.leadingNode === node) {
  5170. commentWS.leadingNode = null;
  5171. }
  5172. }
  5173. takeSurroundingComments(node, start, end) {
  5174. const {
  5175. commentStack
  5176. } = this.state;
  5177. const commentStackLength = commentStack.length;
  5178. if (commentStackLength === 0) return;
  5179. let i = commentStackLength - 1;
  5180. for (; i >= 0; i--) {
  5181. const commentWS = commentStack[i];
  5182. const commentEnd = commentWS.end;
  5183. const commentStart = commentWS.start;
  5184. if (commentStart === end) {
  5185. commentWS.leadingNode = node;
  5186. } else if (commentEnd === start) {
  5187. commentWS.trailingNode = node;
  5188. } else if (commentEnd < start) {
  5189. break;
  5190. }
  5191. }
  5192. }
  5193. }
  5194. class State {
  5195. constructor() {
  5196. this.flags = 1024;
  5197. this.startIndex = void 0;
  5198. this.curLine = void 0;
  5199. this.lineStart = void 0;
  5200. this.startLoc = void 0;
  5201. this.endLoc = void 0;
  5202. this.errors = [];
  5203. this.potentialArrowAt = -1;
  5204. this.noArrowAt = [];
  5205. this.noArrowParamsConversionAt = [];
  5206. this.topicContext = {
  5207. maxNumOfResolvableTopics: 0,
  5208. maxTopicIndex: null
  5209. };
  5210. this.labels = [];
  5211. this.commentsLen = 0;
  5212. this.commentStack = [];
  5213. this.pos = 0;
  5214. this.type = 140;
  5215. this.value = null;
  5216. this.start = 0;
  5217. this.end = 0;
  5218. this.lastTokEndLoc = null;
  5219. this.lastTokStartLoc = null;
  5220. this.context = [types.brace];
  5221. this.firstInvalidTemplateEscapePos = null;
  5222. this.strictErrors = new Map();
  5223. this.tokensLength = 0;
  5224. }
  5225. get strict() {
  5226. return (this.flags & 1) > 0;
  5227. }
  5228. set strict(v) {
  5229. if (v) this.flags |= 1;else this.flags &= -2;
  5230. }
  5231. init({
  5232. strictMode,
  5233. sourceType,
  5234. startIndex,
  5235. startLine,
  5236. startColumn
  5237. }) {
  5238. this.strict = strictMode === false ? false : strictMode === true ? true : sourceType === "module";
  5239. this.startIndex = startIndex;
  5240. this.curLine = startLine;
  5241. this.lineStart = -startColumn;
  5242. this.startLoc = this.endLoc = new Position(startLine, startColumn, startIndex);
  5243. }
  5244. get maybeInArrowParameters() {
  5245. return (this.flags & 2) > 0;
  5246. }
  5247. set maybeInArrowParameters(v) {
  5248. if (v) this.flags |= 2;else this.flags &= -3;
  5249. }
  5250. get inType() {
  5251. return (this.flags & 4) > 0;
  5252. }
  5253. set inType(v) {
  5254. if (v) this.flags |= 4;else this.flags &= -5;
  5255. }
  5256. get noAnonFunctionType() {
  5257. return (this.flags & 8) > 0;
  5258. }
  5259. set noAnonFunctionType(v) {
  5260. if (v) this.flags |= 8;else this.flags &= -9;
  5261. }
  5262. get hasFlowComment() {
  5263. return (this.flags & 16) > 0;
  5264. }
  5265. set hasFlowComment(v) {
  5266. if (v) this.flags |= 16;else this.flags &= -17;
  5267. }
  5268. get isAmbientContext() {
  5269. return (this.flags & 32) > 0;
  5270. }
  5271. set isAmbientContext(v) {
  5272. if (v) this.flags |= 32;else this.flags &= -33;
  5273. }
  5274. get inAbstractClass() {
  5275. return (this.flags & 64) > 0;
  5276. }
  5277. set inAbstractClass(v) {
  5278. if (v) this.flags |= 64;else this.flags &= -65;
  5279. }
  5280. get inDisallowConditionalTypesContext() {
  5281. return (this.flags & 128) > 0;
  5282. }
  5283. set inDisallowConditionalTypesContext(v) {
  5284. if (v) this.flags |= 128;else this.flags &= -129;
  5285. }
  5286. get soloAwait() {
  5287. return (this.flags & 256) > 0;
  5288. }
  5289. set soloAwait(v) {
  5290. if (v) this.flags |= 256;else this.flags &= -257;
  5291. }
  5292. get inFSharpPipelineDirectBody() {
  5293. return (this.flags & 512) > 0;
  5294. }
  5295. set inFSharpPipelineDirectBody(v) {
  5296. if (v) this.flags |= 512;else this.flags &= -513;
  5297. }
  5298. get canStartJSXElement() {
  5299. return (this.flags & 1024) > 0;
  5300. }
  5301. set canStartJSXElement(v) {
  5302. if (v) this.flags |= 1024;else this.flags &= -1025;
  5303. }
  5304. get containsEsc() {
  5305. return (this.flags & 2048) > 0;
  5306. }
  5307. set containsEsc(v) {
  5308. if (v) this.flags |= 2048;else this.flags &= -2049;
  5309. }
  5310. get hasTopLevelAwait() {
  5311. return (this.flags & 4096) > 0;
  5312. }
  5313. set hasTopLevelAwait(v) {
  5314. if (v) this.flags |= 4096;else this.flags &= -4097;
  5315. }
  5316. curPosition() {
  5317. return new Position(this.curLine, this.pos - this.lineStart, this.pos + this.startIndex);
  5318. }
  5319. clone() {
  5320. const state = new State();
  5321. state.flags = this.flags;
  5322. state.startIndex = this.startIndex;
  5323. state.curLine = this.curLine;
  5324. state.lineStart = this.lineStart;
  5325. state.startLoc = this.startLoc;
  5326. state.endLoc = this.endLoc;
  5327. state.errors = this.errors.slice();
  5328. state.potentialArrowAt = this.potentialArrowAt;
  5329. state.noArrowAt = this.noArrowAt.slice();
  5330. state.noArrowParamsConversionAt = this.noArrowParamsConversionAt.slice();
  5331. state.topicContext = this.topicContext;
  5332. state.labels = this.labels.slice();
  5333. state.commentsLen = this.commentsLen;
  5334. state.commentStack = this.commentStack.slice();
  5335. state.pos = this.pos;
  5336. state.type = this.type;
  5337. state.value = this.value;
  5338. state.start = this.start;
  5339. state.end = this.end;
  5340. state.lastTokEndLoc = this.lastTokEndLoc;
  5341. state.lastTokStartLoc = this.lastTokStartLoc;
  5342. state.context = this.context.slice();
  5343. state.firstInvalidTemplateEscapePos = this.firstInvalidTemplateEscapePos;
  5344. state.strictErrors = this.strictErrors;
  5345. state.tokensLength = this.tokensLength;
  5346. return state;
  5347. }
  5348. }
  5349. var _isDigit = function isDigit(code) {
  5350. return code >= 48 && code <= 57;
  5351. };
  5352. const forbiddenNumericSeparatorSiblings = {
  5353. decBinOct: new Set([46, 66, 69, 79, 95, 98, 101, 111]),
  5354. hex: new Set([46, 88, 95, 120])
  5355. };
  5356. const isAllowedNumericSeparatorSibling = {
  5357. bin: ch => ch === 48 || ch === 49,
  5358. oct: ch => ch >= 48 && ch <= 55,
  5359. dec: ch => ch >= 48 && ch <= 57,
  5360. hex: ch => ch >= 48 && ch <= 57 || ch >= 65 && ch <= 70 || ch >= 97 && ch <= 102
  5361. };
  5362. function readStringContents(type, input, pos, lineStart, curLine, errors) {
  5363. const initialPos = pos;
  5364. const initialLineStart = lineStart;
  5365. const initialCurLine = curLine;
  5366. let out = "";
  5367. let firstInvalidLoc = null;
  5368. let chunkStart = pos;
  5369. const {
  5370. length
  5371. } = input;
  5372. for (;;) {
  5373. if (pos >= length) {
  5374. errors.unterminated(initialPos, initialLineStart, initialCurLine);
  5375. out += input.slice(chunkStart, pos);
  5376. break;
  5377. }
  5378. const ch = input.charCodeAt(pos);
  5379. if (isStringEnd(type, ch, input, pos)) {
  5380. out += input.slice(chunkStart, pos);
  5381. break;
  5382. }
  5383. if (ch === 92) {
  5384. out += input.slice(chunkStart, pos);
  5385. const res = readEscapedChar(input, pos, lineStart, curLine, type === "template", errors);
  5386. if (res.ch === null && !firstInvalidLoc) {
  5387. firstInvalidLoc = {
  5388. pos,
  5389. lineStart,
  5390. curLine
  5391. };
  5392. } else {
  5393. out += res.ch;
  5394. }
  5395. ({
  5396. pos,
  5397. lineStart,
  5398. curLine
  5399. } = res);
  5400. chunkStart = pos;
  5401. } else if (ch === 8232 || ch === 8233) {
  5402. ++pos;
  5403. ++curLine;
  5404. lineStart = pos;
  5405. } else if (ch === 10 || ch === 13) {
  5406. if (type === "template") {
  5407. out += input.slice(chunkStart, pos) + "\n";
  5408. ++pos;
  5409. if (ch === 13 && input.charCodeAt(pos) === 10) {
  5410. ++pos;
  5411. }
  5412. ++curLine;
  5413. chunkStart = lineStart = pos;
  5414. } else {
  5415. errors.unterminated(initialPos, initialLineStart, initialCurLine);
  5416. }
  5417. } else {
  5418. ++pos;
  5419. }
  5420. }
  5421. return {
  5422. pos,
  5423. str: out,
  5424. firstInvalidLoc,
  5425. lineStart,
  5426. curLine,
  5427. containsInvalid: !!firstInvalidLoc
  5428. };
  5429. }
  5430. function isStringEnd(type, ch, input, pos) {
  5431. if (type === "template") {
  5432. return ch === 96 || ch === 36 && input.charCodeAt(pos + 1) === 123;
  5433. }
  5434. return ch === (type === "double" ? 34 : 39);
  5435. }
  5436. function readEscapedChar(input, pos, lineStart, curLine, inTemplate, errors) {
  5437. const throwOnInvalid = !inTemplate;
  5438. pos++;
  5439. const res = ch => ({
  5440. pos,
  5441. ch,
  5442. lineStart,
  5443. curLine
  5444. });
  5445. const ch = input.charCodeAt(pos++);
  5446. switch (ch) {
  5447. case 110:
  5448. return res("\n");
  5449. case 114:
  5450. return res("\r");
  5451. case 120:
  5452. {
  5453. let code;
  5454. ({
  5455. code,
  5456. pos
  5457. } = readHexChar(input, pos, lineStart, curLine, 2, false, throwOnInvalid, errors));
  5458. return res(code === null ? null : String.fromCharCode(code));
  5459. }
  5460. case 117:
  5461. {
  5462. let code;
  5463. ({
  5464. code,
  5465. pos
  5466. } = readCodePoint(input, pos, lineStart, curLine, throwOnInvalid, errors));
  5467. return res(code === null ? null : String.fromCodePoint(code));
  5468. }
  5469. case 116:
  5470. return res("\t");
  5471. case 98:
  5472. return res("\b");
  5473. case 118:
  5474. return res("\u000b");
  5475. case 102:
  5476. return res("\f");
  5477. case 13:
  5478. if (input.charCodeAt(pos) === 10) {
  5479. ++pos;
  5480. }
  5481. case 10:
  5482. lineStart = pos;
  5483. ++curLine;
  5484. case 8232:
  5485. case 8233:
  5486. return res("");
  5487. case 56:
  5488. case 57:
  5489. if (inTemplate) {
  5490. return res(null);
  5491. } else {
  5492. errors.strictNumericEscape(pos - 1, lineStart, curLine);
  5493. }
  5494. default:
  5495. if (ch >= 48 && ch <= 55) {
  5496. const startPos = pos - 1;
  5497. const match = /^[0-7]+/.exec(input.slice(startPos, pos + 2));
  5498. let octalStr = match[0];
  5499. let octal = parseInt(octalStr, 8);
  5500. if (octal > 255) {
  5501. octalStr = octalStr.slice(0, -1);
  5502. octal = parseInt(octalStr, 8);
  5503. }
  5504. pos += octalStr.length - 1;
  5505. const next = input.charCodeAt(pos);
  5506. if (octalStr !== "0" || next === 56 || next === 57) {
  5507. if (inTemplate) {
  5508. return res(null);
  5509. } else {
  5510. errors.strictNumericEscape(startPos, lineStart, curLine);
  5511. }
  5512. }
  5513. return res(String.fromCharCode(octal));
  5514. }
  5515. return res(String.fromCharCode(ch));
  5516. }
  5517. }
  5518. function readHexChar(input, pos, lineStart, curLine, len, forceLen, throwOnInvalid, errors) {
  5519. const initialPos = pos;
  5520. let n;
  5521. ({
  5522. n,
  5523. pos
  5524. } = readInt(input, pos, lineStart, curLine, 16, len, forceLen, false, errors, !throwOnInvalid));
  5525. if (n === null) {
  5526. if (throwOnInvalid) {
  5527. errors.invalidEscapeSequence(initialPos, lineStart, curLine);
  5528. } else {
  5529. pos = initialPos - 1;
  5530. }
  5531. }
  5532. return {
  5533. code: n,
  5534. pos
  5535. };
  5536. }
  5537. function readInt(input, pos, lineStart, curLine, radix, len, forceLen, allowNumSeparator, errors, bailOnError) {
  5538. const start = pos;
  5539. const forbiddenSiblings = radix === 16 ? forbiddenNumericSeparatorSiblings.hex : forbiddenNumericSeparatorSiblings.decBinOct;
  5540. const isAllowedSibling = radix === 16 ? isAllowedNumericSeparatorSibling.hex : radix === 10 ? isAllowedNumericSeparatorSibling.dec : radix === 8 ? isAllowedNumericSeparatorSibling.oct : isAllowedNumericSeparatorSibling.bin;
  5541. let invalid = false;
  5542. let total = 0;
  5543. for (let i = 0, e = len == null ? Infinity : len; i < e; ++i) {
  5544. const code = input.charCodeAt(pos);
  5545. let val;
  5546. if (code === 95 && allowNumSeparator !== "bail") {
  5547. const prev = input.charCodeAt(pos - 1);
  5548. const next = input.charCodeAt(pos + 1);
  5549. if (!allowNumSeparator) {
  5550. if (bailOnError) return {
  5551. n: null,
  5552. pos
  5553. };
  5554. errors.numericSeparatorInEscapeSequence(pos, lineStart, curLine);
  5555. } else if (Number.isNaN(next) || !isAllowedSibling(next) || forbiddenSiblings.has(prev) || forbiddenSiblings.has(next)) {
  5556. if (bailOnError) return {
  5557. n: null,
  5558. pos
  5559. };
  5560. errors.unexpectedNumericSeparator(pos, lineStart, curLine);
  5561. }
  5562. ++pos;
  5563. continue;
  5564. }
  5565. if (code >= 97) {
  5566. val = code - 97 + 10;
  5567. } else if (code >= 65) {
  5568. val = code - 65 + 10;
  5569. } else if (_isDigit(code)) {
  5570. val = code - 48;
  5571. } else {
  5572. val = Infinity;
  5573. }
  5574. if (val >= radix) {
  5575. if (val <= 9 && bailOnError) {
  5576. return {
  5577. n: null,
  5578. pos
  5579. };
  5580. } else if (val <= 9 && errors.invalidDigit(pos, lineStart, curLine, radix)) {
  5581. val = 0;
  5582. } else if (forceLen) {
  5583. val = 0;
  5584. invalid = true;
  5585. } else {
  5586. break;
  5587. }
  5588. }
  5589. ++pos;
  5590. total = total * radix + val;
  5591. }
  5592. if (pos === start || len != null && pos - start !== len || invalid) {
  5593. return {
  5594. n: null,
  5595. pos
  5596. };
  5597. }
  5598. return {
  5599. n: total,
  5600. pos
  5601. };
  5602. }
  5603. function readCodePoint(input, pos, lineStart, curLine, throwOnInvalid, errors) {
  5604. const ch = input.charCodeAt(pos);
  5605. let code;
  5606. if (ch === 123) {
  5607. ++pos;
  5608. ({
  5609. code,
  5610. pos
  5611. } = readHexChar(input, pos, lineStart, curLine, input.indexOf("}", pos) - pos, true, throwOnInvalid, errors));
  5612. ++pos;
  5613. if (code !== null && code > 0x10ffff) {
  5614. if (throwOnInvalid) {
  5615. errors.invalidCodePoint(pos, lineStart, curLine);
  5616. } else {
  5617. return {
  5618. code: null,
  5619. pos
  5620. };
  5621. }
  5622. }
  5623. } else {
  5624. ({
  5625. code,
  5626. pos
  5627. } = readHexChar(input, pos, lineStart, curLine, 4, false, throwOnInvalid, errors));
  5628. }
  5629. return {
  5630. code,
  5631. pos
  5632. };
  5633. }
  5634. function buildPosition(pos, lineStart, curLine) {
  5635. return new Position(curLine, pos - lineStart, pos);
  5636. }
  5637. const VALID_REGEX_FLAGS = new Set([103, 109, 115, 105, 121, 117, 100, 118]);
  5638. class Token {
  5639. constructor(state) {
  5640. const startIndex = state.startIndex || 0;
  5641. this.type = state.type;
  5642. this.value = state.value;
  5643. this.start = startIndex + state.start;
  5644. this.end = startIndex + state.end;
  5645. this.loc = new SourceLocation(state.startLoc, state.endLoc);
  5646. }
  5647. }
  5648. class Tokenizer extends CommentsParser {
  5649. constructor(options, input) {
  5650. super();
  5651. this.isLookahead = void 0;
  5652. this.tokens = [];
  5653. this.errorHandlers_readInt = {
  5654. invalidDigit: (pos, lineStart, curLine, radix) => {
  5655. if (!(this.optionFlags & 2048)) return false;
  5656. this.raise(Errors.InvalidDigit, buildPosition(pos, lineStart, curLine), {
  5657. radix
  5658. });
  5659. return true;
  5660. },
  5661. numericSeparatorInEscapeSequence: this.errorBuilder(Errors.NumericSeparatorInEscapeSequence),
  5662. unexpectedNumericSeparator: this.errorBuilder(Errors.UnexpectedNumericSeparator)
  5663. };
  5664. this.errorHandlers_readCodePoint = Object.assign({}, this.errorHandlers_readInt, {
  5665. invalidEscapeSequence: this.errorBuilder(Errors.InvalidEscapeSequence),
  5666. invalidCodePoint: this.errorBuilder(Errors.InvalidCodePoint)
  5667. });
  5668. this.errorHandlers_readStringContents_string = Object.assign({}, this.errorHandlers_readCodePoint, {
  5669. strictNumericEscape: (pos, lineStart, curLine) => {
  5670. this.recordStrictModeErrors(Errors.StrictNumericEscape, buildPosition(pos, lineStart, curLine));
  5671. },
  5672. unterminated: (pos, lineStart, curLine) => {
  5673. throw this.raise(Errors.UnterminatedString, buildPosition(pos - 1, lineStart, curLine));
  5674. }
  5675. });
  5676. this.errorHandlers_readStringContents_template = Object.assign({}, this.errorHandlers_readCodePoint, {
  5677. strictNumericEscape: this.errorBuilder(Errors.StrictNumericEscape),
  5678. unterminated: (pos, lineStart, curLine) => {
  5679. throw this.raise(Errors.UnterminatedTemplate, buildPosition(pos, lineStart, curLine));
  5680. }
  5681. });
  5682. this.state = new State();
  5683. this.state.init(options);
  5684. this.input = input;
  5685. this.length = input.length;
  5686. this.comments = [];
  5687. this.isLookahead = false;
  5688. }
  5689. pushToken(token) {
  5690. this.tokens.length = this.state.tokensLength;
  5691. this.tokens.push(token);
  5692. ++this.state.tokensLength;
  5693. }
  5694. next() {
  5695. this.checkKeywordEscapes();
  5696. if (this.optionFlags & 256) {
  5697. this.pushToken(new Token(this.state));
  5698. }
  5699. this.state.lastTokEndLoc = this.state.endLoc;
  5700. this.state.lastTokStartLoc = this.state.startLoc;
  5701. this.nextToken();
  5702. }
  5703. eat(type) {
  5704. if (this.match(type)) {
  5705. this.next();
  5706. return true;
  5707. } else {
  5708. return false;
  5709. }
  5710. }
  5711. match(type) {
  5712. return this.state.type === type;
  5713. }
  5714. createLookaheadState(state) {
  5715. return {
  5716. pos: state.pos,
  5717. value: null,
  5718. type: state.type,
  5719. start: state.start,
  5720. end: state.end,
  5721. context: [this.curContext()],
  5722. inType: state.inType,
  5723. startLoc: state.startLoc,
  5724. lastTokEndLoc: state.lastTokEndLoc,
  5725. curLine: state.curLine,
  5726. lineStart: state.lineStart,
  5727. curPosition: state.curPosition
  5728. };
  5729. }
  5730. lookahead() {
  5731. const old = this.state;
  5732. this.state = this.createLookaheadState(old);
  5733. this.isLookahead = true;
  5734. this.nextToken();
  5735. this.isLookahead = false;
  5736. const curr = this.state;
  5737. this.state = old;
  5738. return curr;
  5739. }
  5740. nextTokenStart() {
  5741. return this.nextTokenStartSince(this.state.pos);
  5742. }
  5743. nextTokenStartSince(pos) {
  5744. skipWhiteSpace.lastIndex = pos;
  5745. return skipWhiteSpace.test(this.input) ? skipWhiteSpace.lastIndex : pos;
  5746. }
  5747. lookaheadCharCode() {
  5748. return this.lookaheadCharCodeSince(this.state.pos);
  5749. }
  5750. lookaheadCharCodeSince(pos) {
  5751. return this.input.charCodeAt(this.nextTokenStartSince(pos));
  5752. }
  5753. nextTokenInLineStart() {
  5754. return this.nextTokenInLineStartSince(this.state.pos);
  5755. }
  5756. nextTokenInLineStartSince(pos) {
  5757. skipWhiteSpaceInLine.lastIndex = pos;
  5758. return skipWhiteSpaceInLine.test(this.input) ? skipWhiteSpaceInLine.lastIndex : pos;
  5759. }
  5760. lookaheadInLineCharCode() {
  5761. return this.input.charCodeAt(this.nextTokenInLineStart());
  5762. }
  5763. codePointAtPos(pos) {
  5764. let cp = this.input.charCodeAt(pos);
  5765. if ((cp & 0xfc00) === 0xd800 && ++pos < this.input.length) {
  5766. const trail = this.input.charCodeAt(pos);
  5767. if ((trail & 0xfc00) === 0xdc00) {
  5768. cp = 0x10000 + ((cp & 0x3ff) << 10) + (trail & 0x3ff);
  5769. }
  5770. }
  5771. return cp;
  5772. }
  5773. setStrict(strict) {
  5774. this.state.strict = strict;
  5775. if (strict) {
  5776. this.state.strictErrors.forEach(([toParseError, at]) => this.raise(toParseError, at));
  5777. this.state.strictErrors.clear();
  5778. }
  5779. }
  5780. curContext() {
  5781. return this.state.context[this.state.context.length - 1];
  5782. }
  5783. nextToken() {
  5784. this.skipSpace();
  5785. this.state.start = this.state.pos;
  5786. if (!this.isLookahead) this.state.startLoc = this.state.curPosition();
  5787. if (this.state.pos >= this.length) {
  5788. this.finishToken(140);
  5789. return;
  5790. }
  5791. this.getTokenFromCode(this.codePointAtPos(this.state.pos));
  5792. }
  5793. skipBlockComment(commentEnd) {
  5794. let startLoc;
  5795. if (!this.isLookahead) startLoc = this.state.curPosition();
  5796. const start = this.state.pos;
  5797. const end = this.input.indexOf(commentEnd, start + 2);
  5798. if (end === -1) {
  5799. throw this.raise(Errors.UnterminatedComment, this.state.curPosition());
  5800. }
  5801. this.state.pos = end + commentEnd.length;
  5802. lineBreakG.lastIndex = start + 2;
  5803. while (lineBreakG.test(this.input) && lineBreakG.lastIndex <= end) {
  5804. ++this.state.curLine;
  5805. this.state.lineStart = lineBreakG.lastIndex;
  5806. }
  5807. if (this.isLookahead) return;
  5808. const comment = {
  5809. type: "CommentBlock",
  5810. value: this.input.slice(start + 2, end),
  5811. start: this.sourceToOffsetPos(start),
  5812. end: this.sourceToOffsetPos(end + commentEnd.length),
  5813. loc: new SourceLocation(startLoc, this.state.curPosition())
  5814. };
  5815. if (this.optionFlags & 256) this.pushToken(comment);
  5816. return comment;
  5817. }
  5818. skipLineComment(startSkip) {
  5819. const start = this.state.pos;
  5820. let startLoc;
  5821. if (!this.isLookahead) startLoc = this.state.curPosition();
  5822. let ch = this.input.charCodeAt(this.state.pos += startSkip);
  5823. if (this.state.pos < this.length) {
  5824. while (!isNewLine(ch) && ++this.state.pos < this.length) {
  5825. ch = this.input.charCodeAt(this.state.pos);
  5826. }
  5827. }
  5828. if (this.isLookahead) return;
  5829. const end = this.state.pos;
  5830. const value = this.input.slice(start + startSkip, end);
  5831. const comment = {
  5832. type: "CommentLine",
  5833. value,
  5834. start: this.sourceToOffsetPos(start),
  5835. end: this.sourceToOffsetPos(end),
  5836. loc: new SourceLocation(startLoc, this.state.curPosition())
  5837. };
  5838. if (this.optionFlags & 256) this.pushToken(comment);
  5839. return comment;
  5840. }
  5841. skipSpace() {
  5842. const spaceStart = this.state.pos;
  5843. const comments = this.optionFlags & 4096 ? [] : null;
  5844. loop: while (this.state.pos < this.length) {
  5845. const ch = this.input.charCodeAt(this.state.pos);
  5846. switch (ch) {
  5847. case 32:
  5848. case 160:
  5849. case 9:
  5850. ++this.state.pos;
  5851. break;
  5852. case 13:
  5853. if (this.input.charCodeAt(this.state.pos + 1) === 10) {
  5854. ++this.state.pos;
  5855. }
  5856. case 10:
  5857. case 8232:
  5858. case 8233:
  5859. ++this.state.pos;
  5860. ++this.state.curLine;
  5861. this.state.lineStart = this.state.pos;
  5862. break;
  5863. case 47:
  5864. switch (this.input.charCodeAt(this.state.pos + 1)) {
  5865. case 42:
  5866. {
  5867. const comment = this.skipBlockComment("*/");
  5868. if (comment !== undefined) {
  5869. this.addComment(comment);
  5870. comments == null || comments.push(comment);
  5871. }
  5872. break;
  5873. }
  5874. case 47:
  5875. {
  5876. const comment = this.skipLineComment(2);
  5877. if (comment !== undefined) {
  5878. this.addComment(comment);
  5879. comments == null || comments.push(comment);
  5880. }
  5881. break;
  5882. }
  5883. default:
  5884. break loop;
  5885. }
  5886. break;
  5887. default:
  5888. if (isWhitespace(ch)) {
  5889. ++this.state.pos;
  5890. } else if (ch === 45 && !this.inModule && this.optionFlags & 8192) {
  5891. const pos = this.state.pos;
  5892. if (this.input.charCodeAt(pos + 1) === 45 && this.input.charCodeAt(pos + 2) === 62 && (spaceStart === 0 || this.state.lineStart > spaceStart)) {
  5893. const comment = this.skipLineComment(3);
  5894. if (comment !== undefined) {
  5895. this.addComment(comment);
  5896. comments == null || comments.push(comment);
  5897. }
  5898. } else {
  5899. break loop;
  5900. }
  5901. } else if (ch === 60 && !this.inModule && this.optionFlags & 8192) {
  5902. const pos = this.state.pos;
  5903. if (this.input.charCodeAt(pos + 1) === 33 && this.input.charCodeAt(pos + 2) === 45 && this.input.charCodeAt(pos + 3) === 45) {
  5904. const comment = this.skipLineComment(4);
  5905. if (comment !== undefined) {
  5906. this.addComment(comment);
  5907. comments == null || comments.push(comment);
  5908. }
  5909. } else {
  5910. break loop;
  5911. }
  5912. } else {
  5913. break loop;
  5914. }
  5915. }
  5916. }
  5917. if ((comments == null ? void 0 : comments.length) > 0) {
  5918. const end = this.state.pos;
  5919. const commentWhitespace = {
  5920. start: this.sourceToOffsetPos(spaceStart),
  5921. end: this.sourceToOffsetPos(end),
  5922. comments,
  5923. leadingNode: null,
  5924. trailingNode: null,
  5925. containingNode: null
  5926. };
  5927. this.state.commentStack.push(commentWhitespace);
  5928. }
  5929. }
  5930. finishToken(type, val) {
  5931. this.state.end = this.state.pos;
  5932. this.state.endLoc = this.state.curPosition();
  5933. const prevType = this.state.type;
  5934. this.state.type = type;
  5935. this.state.value = val;
  5936. if (!this.isLookahead) {
  5937. this.updateContext(prevType);
  5938. }
  5939. }
  5940. replaceToken(type) {
  5941. this.state.type = type;
  5942. this.updateContext();
  5943. }
  5944. readToken_numberSign() {
  5945. if (this.state.pos === 0 && this.readToken_interpreter()) {
  5946. return;
  5947. }
  5948. const nextPos = this.state.pos + 1;
  5949. const next = this.codePointAtPos(nextPos);
  5950. if (next >= 48 && next <= 57) {
  5951. throw this.raise(Errors.UnexpectedDigitAfterHash, this.state.curPosition());
  5952. }
  5953. if (next === 123 || next === 91 && this.hasPlugin("recordAndTuple")) {
  5954. this.expectPlugin("recordAndTuple");
  5955. if (this.getPluginOption("recordAndTuple", "syntaxType") === "bar") {
  5956. throw this.raise(next === 123 ? Errors.RecordExpressionHashIncorrectStartSyntaxType : Errors.TupleExpressionHashIncorrectStartSyntaxType, this.state.curPosition());
  5957. }
  5958. this.state.pos += 2;
  5959. if (next === 123) {
  5960. this.finishToken(7);
  5961. } else {
  5962. this.finishToken(1);
  5963. }
  5964. } else if (isIdentifierStart(next)) {
  5965. ++this.state.pos;
  5966. this.finishToken(139, this.readWord1(next));
  5967. } else if (next === 92) {
  5968. ++this.state.pos;
  5969. this.finishToken(139, this.readWord1());
  5970. } else {
  5971. this.finishOp(27, 1);
  5972. }
  5973. }
  5974. readToken_dot() {
  5975. const next = this.input.charCodeAt(this.state.pos + 1);
  5976. if (next >= 48 && next <= 57) {
  5977. this.readNumber(true);
  5978. return;
  5979. }
  5980. if (next === 46 && this.input.charCodeAt(this.state.pos + 2) === 46) {
  5981. this.state.pos += 3;
  5982. this.finishToken(21);
  5983. } else {
  5984. ++this.state.pos;
  5985. this.finishToken(16);
  5986. }
  5987. }
  5988. readToken_slash() {
  5989. const next = this.input.charCodeAt(this.state.pos + 1);
  5990. if (next === 61) {
  5991. this.finishOp(31, 2);
  5992. } else {
  5993. this.finishOp(56, 1);
  5994. }
  5995. }
  5996. readToken_interpreter() {
  5997. if (this.state.pos !== 0 || this.length < 2) return false;
  5998. let ch = this.input.charCodeAt(this.state.pos + 1);
  5999. if (ch !== 33) return false;
  6000. const start = this.state.pos;
  6001. this.state.pos += 1;
  6002. while (!isNewLine(ch) && ++this.state.pos < this.length) {
  6003. ch = this.input.charCodeAt(this.state.pos);
  6004. }
  6005. const value = this.input.slice(start + 2, this.state.pos);
  6006. this.finishToken(28, value);
  6007. return true;
  6008. }
  6009. readToken_mult_modulo(code) {
  6010. let type = code === 42 ? 55 : 54;
  6011. let width = 1;
  6012. let next = this.input.charCodeAt(this.state.pos + 1);
  6013. if (code === 42 && next === 42) {
  6014. width++;
  6015. next = this.input.charCodeAt(this.state.pos + 2);
  6016. type = 57;
  6017. }
  6018. if (next === 61 && !this.state.inType) {
  6019. width++;
  6020. type = code === 37 ? 33 : 30;
  6021. }
  6022. this.finishOp(type, width);
  6023. }
  6024. readToken_pipe_amp(code) {
  6025. const next = this.input.charCodeAt(this.state.pos + 1);
  6026. if (next === code) {
  6027. if (this.input.charCodeAt(this.state.pos + 2) === 61) {
  6028. this.finishOp(30, 3);
  6029. } else {
  6030. this.finishOp(code === 124 ? 41 : 42, 2);
  6031. }
  6032. return;
  6033. }
  6034. if (code === 124) {
  6035. if (next === 62) {
  6036. this.finishOp(39, 2);
  6037. return;
  6038. }
  6039. if (this.hasPlugin("recordAndTuple") && next === 125) {
  6040. if (this.getPluginOption("recordAndTuple", "syntaxType") !== "bar") {
  6041. throw this.raise(Errors.RecordExpressionBarIncorrectEndSyntaxType, this.state.curPosition());
  6042. }
  6043. this.state.pos += 2;
  6044. this.finishToken(9);
  6045. return;
  6046. }
  6047. if (this.hasPlugin("recordAndTuple") && next === 93) {
  6048. if (this.getPluginOption("recordAndTuple", "syntaxType") !== "bar") {
  6049. throw this.raise(Errors.TupleExpressionBarIncorrectEndSyntaxType, this.state.curPosition());
  6050. }
  6051. this.state.pos += 2;
  6052. this.finishToken(4);
  6053. return;
  6054. }
  6055. }
  6056. if (next === 61) {
  6057. this.finishOp(30, 2);
  6058. return;
  6059. }
  6060. this.finishOp(code === 124 ? 43 : 45, 1);
  6061. }
  6062. readToken_caret() {
  6063. const next = this.input.charCodeAt(this.state.pos + 1);
  6064. if (next === 61 && !this.state.inType) {
  6065. this.finishOp(32, 2);
  6066. } else if (next === 94 && this.hasPlugin(["pipelineOperator", {
  6067. proposal: "hack",
  6068. topicToken: "^^"
  6069. }])) {
  6070. this.finishOp(37, 2);
  6071. const lookaheadCh = this.input.codePointAt(this.state.pos);
  6072. if (lookaheadCh === 94) {
  6073. this.unexpected();
  6074. }
  6075. } else {
  6076. this.finishOp(44, 1);
  6077. }
  6078. }
  6079. readToken_atSign() {
  6080. const next = this.input.charCodeAt(this.state.pos + 1);
  6081. if (next === 64 && this.hasPlugin(["pipelineOperator", {
  6082. proposal: "hack",
  6083. topicToken: "@@"
  6084. }])) {
  6085. this.finishOp(38, 2);
  6086. } else {
  6087. this.finishOp(26, 1);
  6088. }
  6089. }
  6090. readToken_plus_min(code) {
  6091. const next = this.input.charCodeAt(this.state.pos + 1);
  6092. if (next === code) {
  6093. this.finishOp(34, 2);
  6094. return;
  6095. }
  6096. if (next === 61) {
  6097. this.finishOp(30, 2);
  6098. } else {
  6099. this.finishOp(53, 1);
  6100. }
  6101. }
  6102. readToken_lt() {
  6103. const {
  6104. pos
  6105. } = this.state;
  6106. const next = this.input.charCodeAt(pos + 1);
  6107. if (next === 60) {
  6108. if (this.input.charCodeAt(pos + 2) === 61) {
  6109. this.finishOp(30, 3);
  6110. return;
  6111. }
  6112. this.finishOp(51, 2);
  6113. return;
  6114. }
  6115. if (next === 61) {
  6116. this.finishOp(49, 2);
  6117. return;
  6118. }
  6119. this.finishOp(47, 1);
  6120. }
  6121. readToken_gt() {
  6122. const {
  6123. pos
  6124. } = this.state;
  6125. const next = this.input.charCodeAt(pos + 1);
  6126. if (next === 62) {
  6127. const size = this.input.charCodeAt(pos + 2) === 62 ? 3 : 2;
  6128. if (this.input.charCodeAt(pos + size) === 61) {
  6129. this.finishOp(30, size + 1);
  6130. return;
  6131. }
  6132. this.finishOp(52, size);
  6133. return;
  6134. }
  6135. if (next === 61) {
  6136. this.finishOp(49, 2);
  6137. return;
  6138. }
  6139. this.finishOp(48, 1);
  6140. }
  6141. readToken_eq_excl(code) {
  6142. const next = this.input.charCodeAt(this.state.pos + 1);
  6143. if (next === 61) {
  6144. this.finishOp(46, this.input.charCodeAt(this.state.pos + 2) === 61 ? 3 : 2);
  6145. return;
  6146. }
  6147. if (code === 61 && next === 62) {
  6148. this.state.pos += 2;
  6149. this.finishToken(19);
  6150. return;
  6151. }
  6152. this.finishOp(code === 61 ? 29 : 35, 1);
  6153. }
  6154. readToken_question() {
  6155. const next = this.input.charCodeAt(this.state.pos + 1);
  6156. const next2 = this.input.charCodeAt(this.state.pos + 2);
  6157. if (next === 63) {
  6158. if (next2 === 61) {
  6159. this.finishOp(30, 3);
  6160. } else {
  6161. this.finishOp(40, 2);
  6162. }
  6163. } else if (next === 46 && !(next2 >= 48 && next2 <= 57)) {
  6164. this.state.pos += 2;
  6165. this.finishToken(18);
  6166. } else {
  6167. ++this.state.pos;
  6168. this.finishToken(17);
  6169. }
  6170. }
  6171. getTokenFromCode(code) {
  6172. switch (code) {
  6173. case 46:
  6174. this.readToken_dot();
  6175. return;
  6176. case 40:
  6177. ++this.state.pos;
  6178. this.finishToken(10);
  6179. return;
  6180. case 41:
  6181. ++this.state.pos;
  6182. this.finishToken(11);
  6183. return;
  6184. case 59:
  6185. ++this.state.pos;
  6186. this.finishToken(13);
  6187. return;
  6188. case 44:
  6189. ++this.state.pos;
  6190. this.finishToken(12);
  6191. return;
  6192. case 91:
  6193. if (this.hasPlugin("recordAndTuple") && this.input.charCodeAt(this.state.pos + 1) === 124) {
  6194. if (this.getPluginOption("recordAndTuple", "syntaxType") !== "bar") {
  6195. throw this.raise(Errors.TupleExpressionBarIncorrectStartSyntaxType, this.state.curPosition());
  6196. }
  6197. this.state.pos += 2;
  6198. this.finishToken(2);
  6199. } else {
  6200. ++this.state.pos;
  6201. this.finishToken(0);
  6202. }
  6203. return;
  6204. case 93:
  6205. ++this.state.pos;
  6206. this.finishToken(3);
  6207. return;
  6208. case 123:
  6209. if (this.hasPlugin("recordAndTuple") && this.input.charCodeAt(this.state.pos + 1) === 124) {
  6210. if (this.getPluginOption("recordAndTuple", "syntaxType") !== "bar") {
  6211. throw this.raise(Errors.RecordExpressionBarIncorrectStartSyntaxType, this.state.curPosition());
  6212. }
  6213. this.state.pos += 2;
  6214. this.finishToken(6);
  6215. } else {
  6216. ++this.state.pos;
  6217. this.finishToken(5);
  6218. }
  6219. return;
  6220. case 125:
  6221. ++this.state.pos;
  6222. this.finishToken(8);
  6223. return;
  6224. case 58:
  6225. if (this.hasPlugin("functionBind") && this.input.charCodeAt(this.state.pos + 1) === 58) {
  6226. this.finishOp(15, 2);
  6227. } else {
  6228. ++this.state.pos;
  6229. this.finishToken(14);
  6230. }
  6231. return;
  6232. case 63:
  6233. this.readToken_question();
  6234. return;
  6235. case 96:
  6236. this.readTemplateToken();
  6237. return;
  6238. case 48:
  6239. {
  6240. const next = this.input.charCodeAt(this.state.pos + 1);
  6241. if (next === 120 || next === 88) {
  6242. this.readRadixNumber(16);
  6243. return;
  6244. }
  6245. if (next === 111 || next === 79) {
  6246. this.readRadixNumber(8);
  6247. return;
  6248. }
  6249. if (next === 98 || next === 66) {
  6250. this.readRadixNumber(2);
  6251. return;
  6252. }
  6253. }
  6254. case 49:
  6255. case 50:
  6256. case 51:
  6257. case 52:
  6258. case 53:
  6259. case 54:
  6260. case 55:
  6261. case 56:
  6262. case 57:
  6263. this.readNumber(false);
  6264. return;
  6265. case 34:
  6266. case 39:
  6267. this.readString(code);
  6268. return;
  6269. case 47:
  6270. this.readToken_slash();
  6271. return;
  6272. case 37:
  6273. case 42:
  6274. this.readToken_mult_modulo(code);
  6275. return;
  6276. case 124:
  6277. case 38:
  6278. this.readToken_pipe_amp(code);
  6279. return;
  6280. case 94:
  6281. this.readToken_caret();
  6282. return;
  6283. case 43:
  6284. case 45:
  6285. this.readToken_plus_min(code);
  6286. return;
  6287. case 60:
  6288. this.readToken_lt();
  6289. return;
  6290. case 62:
  6291. this.readToken_gt();
  6292. return;
  6293. case 61:
  6294. case 33:
  6295. this.readToken_eq_excl(code);
  6296. return;
  6297. case 126:
  6298. this.finishOp(36, 1);
  6299. return;
  6300. case 64:
  6301. this.readToken_atSign();
  6302. return;
  6303. case 35:
  6304. this.readToken_numberSign();
  6305. return;
  6306. case 92:
  6307. this.readWord();
  6308. return;
  6309. default:
  6310. if (isIdentifierStart(code)) {
  6311. this.readWord(code);
  6312. return;
  6313. }
  6314. }
  6315. throw this.raise(Errors.InvalidOrUnexpectedToken, this.state.curPosition(), {
  6316. unexpected: String.fromCodePoint(code)
  6317. });
  6318. }
  6319. finishOp(type, size) {
  6320. const str = this.input.slice(this.state.pos, this.state.pos + size);
  6321. this.state.pos += size;
  6322. this.finishToken(type, str);
  6323. }
  6324. readRegexp() {
  6325. const startLoc = this.state.startLoc;
  6326. const start = this.state.start + 1;
  6327. let escaped, inClass;
  6328. let {
  6329. pos
  6330. } = this.state;
  6331. for (;; ++pos) {
  6332. if (pos >= this.length) {
  6333. throw this.raise(Errors.UnterminatedRegExp, createPositionWithColumnOffset(startLoc, 1));
  6334. }
  6335. const ch = this.input.charCodeAt(pos);
  6336. if (isNewLine(ch)) {
  6337. throw this.raise(Errors.UnterminatedRegExp, createPositionWithColumnOffset(startLoc, 1));
  6338. }
  6339. if (escaped) {
  6340. escaped = false;
  6341. } else {
  6342. if (ch === 91) {
  6343. inClass = true;
  6344. } else if (ch === 93 && inClass) {
  6345. inClass = false;
  6346. } else if (ch === 47 && !inClass) {
  6347. break;
  6348. }
  6349. escaped = ch === 92;
  6350. }
  6351. }
  6352. const content = this.input.slice(start, pos);
  6353. ++pos;
  6354. let mods = "";
  6355. const nextPos = () => createPositionWithColumnOffset(startLoc, pos + 2 - start);
  6356. while (pos < this.length) {
  6357. const cp = this.codePointAtPos(pos);
  6358. const char = String.fromCharCode(cp);
  6359. if (VALID_REGEX_FLAGS.has(cp)) {
  6360. if (cp === 118) {
  6361. if (mods.includes("u")) {
  6362. this.raise(Errors.IncompatibleRegExpUVFlags, nextPos());
  6363. }
  6364. } else if (cp === 117) {
  6365. if (mods.includes("v")) {
  6366. this.raise(Errors.IncompatibleRegExpUVFlags, nextPos());
  6367. }
  6368. }
  6369. if (mods.includes(char)) {
  6370. this.raise(Errors.DuplicateRegExpFlags, nextPos());
  6371. }
  6372. } else if (isIdentifierChar(cp) || cp === 92) {
  6373. this.raise(Errors.MalformedRegExpFlags, nextPos());
  6374. } else {
  6375. break;
  6376. }
  6377. ++pos;
  6378. mods += char;
  6379. }
  6380. this.state.pos = pos;
  6381. this.finishToken(138, {
  6382. pattern: content,
  6383. flags: mods
  6384. });
  6385. }
  6386. readInt(radix, len, forceLen = false, allowNumSeparator = true) {
  6387. const {
  6388. n,
  6389. pos
  6390. } = readInt(this.input, this.state.pos, this.state.lineStart, this.state.curLine, radix, len, forceLen, allowNumSeparator, this.errorHandlers_readInt, false);
  6391. this.state.pos = pos;
  6392. return n;
  6393. }
  6394. readRadixNumber(radix) {
  6395. const start = this.state.pos;
  6396. const startLoc = this.state.curPosition();
  6397. let isBigInt = false;
  6398. this.state.pos += 2;
  6399. const val = this.readInt(radix);
  6400. if (val == null) {
  6401. this.raise(Errors.InvalidDigit, createPositionWithColumnOffset(startLoc, 2), {
  6402. radix
  6403. });
  6404. }
  6405. const next = this.input.charCodeAt(this.state.pos);
  6406. if (next === 110) {
  6407. ++this.state.pos;
  6408. isBigInt = true;
  6409. } else if (next === 109) {
  6410. throw this.raise(Errors.InvalidDecimal, startLoc);
  6411. }
  6412. if (isIdentifierStart(this.codePointAtPos(this.state.pos))) {
  6413. throw this.raise(Errors.NumberIdentifier, this.state.curPosition());
  6414. }
  6415. if (isBigInt) {
  6416. const str = this.input.slice(start, this.state.pos).replace(/[_n]/g, "");
  6417. this.finishToken(136, str);
  6418. return;
  6419. }
  6420. this.finishToken(135, val);
  6421. }
  6422. readNumber(startsWithDot) {
  6423. const start = this.state.pos;
  6424. const startLoc = this.state.curPosition();
  6425. let isFloat = false;
  6426. let isBigInt = false;
  6427. let hasExponent = false;
  6428. let isOctal = false;
  6429. if (!startsWithDot && this.readInt(10) === null) {
  6430. this.raise(Errors.InvalidNumber, this.state.curPosition());
  6431. }
  6432. const hasLeadingZero = this.state.pos - start >= 2 && this.input.charCodeAt(start) === 48;
  6433. if (hasLeadingZero) {
  6434. const integer = this.input.slice(start, this.state.pos);
  6435. this.recordStrictModeErrors(Errors.StrictOctalLiteral, startLoc);
  6436. if (!this.state.strict) {
  6437. const underscorePos = integer.indexOf("_");
  6438. if (underscorePos > 0) {
  6439. this.raise(Errors.ZeroDigitNumericSeparator, createPositionWithColumnOffset(startLoc, underscorePos));
  6440. }
  6441. }
  6442. isOctal = hasLeadingZero && !/[89]/.test(integer);
  6443. }
  6444. let next = this.input.charCodeAt(this.state.pos);
  6445. if (next === 46 && !isOctal) {
  6446. ++this.state.pos;
  6447. this.readInt(10);
  6448. isFloat = true;
  6449. next = this.input.charCodeAt(this.state.pos);
  6450. }
  6451. if ((next === 69 || next === 101) && !isOctal) {
  6452. next = this.input.charCodeAt(++this.state.pos);
  6453. if (next === 43 || next === 45) {
  6454. ++this.state.pos;
  6455. }
  6456. if (this.readInt(10) === null) {
  6457. this.raise(Errors.InvalidOrMissingExponent, startLoc);
  6458. }
  6459. isFloat = true;
  6460. hasExponent = true;
  6461. next = this.input.charCodeAt(this.state.pos);
  6462. }
  6463. if (next === 110) {
  6464. if (isFloat || hasLeadingZero) {
  6465. this.raise(Errors.InvalidBigIntLiteral, startLoc);
  6466. }
  6467. ++this.state.pos;
  6468. isBigInt = true;
  6469. }
  6470. if (next === 109) {
  6471. this.expectPlugin("decimal", this.state.curPosition());
  6472. if (hasExponent || hasLeadingZero) {
  6473. this.raise(Errors.InvalidDecimal, startLoc);
  6474. }
  6475. ++this.state.pos;
  6476. var isDecimal = true;
  6477. }
  6478. if (isIdentifierStart(this.codePointAtPos(this.state.pos))) {
  6479. throw this.raise(Errors.NumberIdentifier, this.state.curPosition());
  6480. }
  6481. const str = this.input.slice(start, this.state.pos).replace(/[_mn]/g, "");
  6482. if (isBigInt) {
  6483. this.finishToken(136, str);
  6484. return;
  6485. }
  6486. if (isDecimal) {
  6487. this.finishToken(137, str);
  6488. return;
  6489. }
  6490. const val = isOctal ? parseInt(str, 8) : parseFloat(str);
  6491. this.finishToken(135, val);
  6492. }
  6493. readCodePoint(throwOnInvalid) {
  6494. const {
  6495. code,
  6496. pos
  6497. } = readCodePoint(this.input, this.state.pos, this.state.lineStart, this.state.curLine, throwOnInvalid, this.errorHandlers_readCodePoint);
  6498. this.state.pos = pos;
  6499. return code;
  6500. }
  6501. readString(quote) {
  6502. const {
  6503. str,
  6504. pos,
  6505. curLine,
  6506. lineStart
  6507. } = readStringContents(quote === 34 ? "double" : "single", this.input, this.state.pos + 1, this.state.lineStart, this.state.curLine, this.errorHandlers_readStringContents_string);
  6508. this.state.pos = pos + 1;
  6509. this.state.lineStart = lineStart;
  6510. this.state.curLine = curLine;
  6511. this.finishToken(134, str);
  6512. }
  6513. readTemplateContinuation() {
  6514. if (!this.match(8)) {
  6515. this.unexpected(null, 8);
  6516. }
  6517. this.state.pos--;
  6518. this.readTemplateToken();
  6519. }
  6520. readTemplateToken() {
  6521. const opening = this.input[this.state.pos];
  6522. const {
  6523. str,
  6524. firstInvalidLoc,
  6525. pos,
  6526. curLine,
  6527. lineStart
  6528. } = readStringContents("template", this.input, this.state.pos + 1, this.state.lineStart, this.state.curLine, this.errorHandlers_readStringContents_template);
  6529. this.state.pos = pos + 1;
  6530. this.state.lineStart = lineStart;
  6531. this.state.curLine = curLine;
  6532. if (firstInvalidLoc) {
  6533. this.state.firstInvalidTemplateEscapePos = new Position(firstInvalidLoc.curLine, firstInvalidLoc.pos - firstInvalidLoc.lineStart, this.sourceToOffsetPos(firstInvalidLoc.pos));
  6534. }
  6535. if (this.input.codePointAt(pos) === 96) {
  6536. this.finishToken(24, firstInvalidLoc ? null : opening + str + "`");
  6537. } else {
  6538. this.state.pos++;
  6539. this.finishToken(25, firstInvalidLoc ? null : opening + str + "${");
  6540. }
  6541. }
  6542. recordStrictModeErrors(toParseError, at) {
  6543. const index = at.index;
  6544. if (this.state.strict && !this.state.strictErrors.has(index)) {
  6545. this.raise(toParseError, at);
  6546. } else {
  6547. this.state.strictErrors.set(index, [toParseError, at]);
  6548. }
  6549. }
  6550. readWord1(firstCode) {
  6551. this.state.containsEsc = false;
  6552. let word = "";
  6553. const start = this.state.pos;
  6554. let chunkStart = this.state.pos;
  6555. if (firstCode !== undefined) {
  6556. this.state.pos += firstCode <= 0xffff ? 1 : 2;
  6557. }
  6558. while (this.state.pos < this.length) {
  6559. const ch = this.codePointAtPos(this.state.pos);
  6560. if (isIdentifierChar(ch)) {
  6561. this.state.pos += ch <= 0xffff ? 1 : 2;
  6562. } else if (ch === 92) {
  6563. this.state.containsEsc = true;
  6564. word += this.input.slice(chunkStart, this.state.pos);
  6565. const escStart = this.state.curPosition();
  6566. const identifierCheck = this.state.pos === start ? isIdentifierStart : isIdentifierChar;
  6567. if (this.input.charCodeAt(++this.state.pos) !== 117) {
  6568. this.raise(Errors.MissingUnicodeEscape, this.state.curPosition());
  6569. chunkStart = this.state.pos - 1;
  6570. continue;
  6571. }
  6572. ++this.state.pos;
  6573. const esc = this.readCodePoint(true);
  6574. if (esc !== null) {
  6575. if (!identifierCheck(esc)) {
  6576. this.raise(Errors.EscapedCharNotAnIdentifier, escStart);
  6577. }
  6578. word += String.fromCodePoint(esc);
  6579. }
  6580. chunkStart = this.state.pos;
  6581. } else {
  6582. break;
  6583. }
  6584. }
  6585. return word + this.input.slice(chunkStart, this.state.pos);
  6586. }
  6587. readWord(firstCode) {
  6588. const word = this.readWord1(firstCode);
  6589. const type = keywords$1.get(word);
  6590. if (type !== undefined) {
  6591. this.finishToken(type, tokenLabelName(type));
  6592. } else {
  6593. this.finishToken(132, word);
  6594. }
  6595. }
  6596. checkKeywordEscapes() {
  6597. const {
  6598. type
  6599. } = this.state;
  6600. if (tokenIsKeyword(type) && this.state.containsEsc) {
  6601. this.raise(Errors.InvalidEscapedReservedWord, this.state.startLoc, {
  6602. reservedWord: tokenLabelName(type)
  6603. });
  6604. }
  6605. }
  6606. raise(toParseError, at, details = {}) {
  6607. const loc = at instanceof Position ? at : at.loc.start;
  6608. const error = toParseError(loc, details);
  6609. if (!(this.optionFlags & 2048)) throw error;
  6610. if (!this.isLookahead) this.state.errors.push(error);
  6611. return error;
  6612. }
  6613. raiseOverwrite(toParseError, at, details = {}) {
  6614. const loc = at instanceof Position ? at : at.loc.start;
  6615. const pos = loc.index;
  6616. const errors = this.state.errors;
  6617. for (let i = errors.length - 1; i >= 0; i--) {
  6618. const error = errors[i];
  6619. if (error.loc.index === pos) {
  6620. return errors[i] = toParseError(loc, details);
  6621. }
  6622. if (error.loc.index < pos) break;
  6623. }
  6624. return this.raise(toParseError, at, details);
  6625. }
  6626. updateContext(prevType) {}
  6627. unexpected(loc, type) {
  6628. throw this.raise(Errors.UnexpectedToken, loc != null ? loc : this.state.startLoc, {
  6629. expected: type ? tokenLabelName(type) : null
  6630. });
  6631. }
  6632. expectPlugin(pluginName, loc) {
  6633. if (this.hasPlugin(pluginName)) {
  6634. return true;
  6635. }
  6636. throw this.raise(Errors.MissingPlugin, loc != null ? loc : this.state.startLoc, {
  6637. missingPlugin: [pluginName]
  6638. });
  6639. }
  6640. expectOnePlugin(pluginNames) {
  6641. if (!pluginNames.some(name => this.hasPlugin(name))) {
  6642. throw this.raise(Errors.MissingOneOfPlugins, this.state.startLoc, {
  6643. missingPlugin: pluginNames
  6644. });
  6645. }
  6646. }
  6647. errorBuilder(error) {
  6648. return (pos, lineStart, curLine) => {
  6649. this.raise(error, buildPosition(pos, lineStart, curLine));
  6650. };
  6651. }
  6652. }
  6653. class ClassScope {
  6654. constructor() {
  6655. this.privateNames = new Set();
  6656. this.loneAccessors = new Map();
  6657. this.undefinedPrivateNames = new Map();
  6658. }
  6659. }
  6660. class ClassScopeHandler {
  6661. constructor(parser) {
  6662. this.parser = void 0;
  6663. this.stack = [];
  6664. this.undefinedPrivateNames = new Map();
  6665. this.parser = parser;
  6666. }
  6667. current() {
  6668. return this.stack[this.stack.length - 1];
  6669. }
  6670. enter() {
  6671. this.stack.push(new ClassScope());
  6672. }
  6673. exit() {
  6674. const oldClassScope = this.stack.pop();
  6675. const current = this.current();
  6676. for (const [name, loc] of Array.from(oldClassScope.undefinedPrivateNames)) {
  6677. if (current) {
  6678. if (!current.undefinedPrivateNames.has(name)) {
  6679. current.undefinedPrivateNames.set(name, loc);
  6680. }
  6681. } else {
  6682. this.parser.raise(Errors.InvalidPrivateFieldResolution, loc, {
  6683. identifierName: name
  6684. });
  6685. }
  6686. }
  6687. }
  6688. declarePrivateName(name, elementType, loc) {
  6689. const {
  6690. privateNames,
  6691. loneAccessors,
  6692. undefinedPrivateNames
  6693. } = this.current();
  6694. let redefined = privateNames.has(name);
  6695. if (elementType & 3) {
  6696. const accessor = redefined && loneAccessors.get(name);
  6697. if (accessor) {
  6698. const oldStatic = accessor & 4;
  6699. const newStatic = elementType & 4;
  6700. const oldKind = accessor & 3;
  6701. const newKind = elementType & 3;
  6702. redefined = oldKind === newKind || oldStatic !== newStatic;
  6703. if (!redefined) loneAccessors.delete(name);
  6704. } else if (!redefined) {
  6705. loneAccessors.set(name, elementType);
  6706. }
  6707. }
  6708. if (redefined) {
  6709. this.parser.raise(Errors.PrivateNameRedeclaration, loc, {
  6710. identifierName: name
  6711. });
  6712. }
  6713. privateNames.add(name);
  6714. undefinedPrivateNames.delete(name);
  6715. }
  6716. usePrivateName(name, loc) {
  6717. let classScope;
  6718. for (classScope of this.stack) {
  6719. if (classScope.privateNames.has(name)) return;
  6720. }
  6721. if (classScope) {
  6722. classScope.undefinedPrivateNames.set(name, loc);
  6723. } else {
  6724. this.parser.raise(Errors.InvalidPrivateFieldResolution, loc, {
  6725. identifierName: name
  6726. });
  6727. }
  6728. }
  6729. }
  6730. class ExpressionScope {
  6731. constructor(type = 0) {
  6732. this.type = type;
  6733. }
  6734. canBeArrowParameterDeclaration() {
  6735. return this.type === 2 || this.type === 1;
  6736. }
  6737. isCertainlyParameterDeclaration() {
  6738. return this.type === 3;
  6739. }
  6740. }
  6741. class ArrowHeadParsingScope extends ExpressionScope {
  6742. constructor(type) {
  6743. super(type);
  6744. this.declarationErrors = new Map();
  6745. }
  6746. recordDeclarationError(ParsingErrorClass, at) {
  6747. const index = at.index;
  6748. this.declarationErrors.set(index, [ParsingErrorClass, at]);
  6749. }
  6750. clearDeclarationError(index) {
  6751. this.declarationErrors.delete(index);
  6752. }
  6753. iterateErrors(iterator) {
  6754. this.declarationErrors.forEach(iterator);
  6755. }
  6756. }
  6757. class ExpressionScopeHandler {
  6758. constructor(parser) {
  6759. this.parser = void 0;
  6760. this.stack = [new ExpressionScope()];
  6761. this.parser = parser;
  6762. }
  6763. enter(scope) {
  6764. this.stack.push(scope);
  6765. }
  6766. exit() {
  6767. this.stack.pop();
  6768. }
  6769. recordParameterInitializerError(toParseError, node) {
  6770. const origin = node.loc.start;
  6771. const {
  6772. stack
  6773. } = this;
  6774. let i = stack.length - 1;
  6775. let scope = stack[i];
  6776. while (!scope.isCertainlyParameterDeclaration()) {
  6777. if (scope.canBeArrowParameterDeclaration()) {
  6778. scope.recordDeclarationError(toParseError, origin);
  6779. } else {
  6780. return;
  6781. }
  6782. scope = stack[--i];
  6783. }
  6784. this.parser.raise(toParseError, origin);
  6785. }
  6786. recordArrowParameterBindingError(error, node) {
  6787. const {
  6788. stack
  6789. } = this;
  6790. const scope = stack[stack.length - 1];
  6791. const origin = node.loc.start;
  6792. if (scope.isCertainlyParameterDeclaration()) {
  6793. this.parser.raise(error, origin);
  6794. } else if (scope.canBeArrowParameterDeclaration()) {
  6795. scope.recordDeclarationError(error, origin);
  6796. } else {
  6797. return;
  6798. }
  6799. }
  6800. recordAsyncArrowParametersError(at) {
  6801. const {
  6802. stack
  6803. } = this;
  6804. let i = stack.length - 1;
  6805. let scope = stack[i];
  6806. while (scope.canBeArrowParameterDeclaration()) {
  6807. if (scope.type === 2) {
  6808. scope.recordDeclarationError(Errors.AwaitBindingIdentifier, at);
  6809. }
  6810. scope = stack[--i];
  6811. }
  6812. }
  6813. validateAsPattern() {
  6814. const {
  6815. stack
  6816. } = this;
  6817. const currentScope = stack[stack.length - 1];
  6818. if (!currentScope.canBeArrowParameterDeclaration()) return;
  6819. currentScope.iterateErrors(([toParseError, loc]) => {
  6820. this.parser.raise(toParseError, loc);
  6821. let i = stack.length - 2;
  6822. let scope = stack[i];
  6823. while (scope.canBeArrowParameterDeclaration()) {
  6824. scope.clearDeclarationError(loc.index);
  6825. scope = stack[--i];
  6826. }
  6827. });
  6828. }
  6829. }
  6830. function newParameterDeclarationScope() {
  6831. return new ExpressionScope(3);
  6832. }
  6833. function newArrowHeadScope() {
  6834. return new ArrowHeadParsingScope(1);
  6835. }
  6836. function newAsyncArrowScope() {
  6837. return new ArrowHeadParsingScope(2);
  6838. }
  6839. function newExpressionScope() {
  6840. return new ExpressionScope();
  6841. }
  6842. class UtilParser extends Tokenizer {
  6843. addExtra(node, key, value, enumerable = true) {
  6844. if (!node) return;
  6845. let {
  6846. extra
  6847. } = node;
  6848. if (extra == null) {
  6849. extra = {};
  6850. node.extra = extra;
  6851. }
  6852. if (enumerable) {
  6853. extra[key] = value;
  6854. } else {
  6855. Object.defineProperty(extra, key, {
  6856. enumerable,
  6857. value
  6858. });
  6859. }
  6860. }
  6861. isContextual(token) {
  6862. return this.state.type === token && !this.state.containsEsc;
  6863. }
  6864. isUnparsedContextual(nameStart, name) {
  6865. const nameEnd = nameStart + name.length;
  6866. if (this.input.slice(nameStart, nameEnd) === name) {
  6867. const nextCh = this.input.charCodeAt(nameEnd);
  6868. return !(isIdentifierChar(nextCh) || (nextCh & 0xfc00) === 0xd800);
  6869. }
  6870. return false;
  6871. }
  6872. isLookaheadContextual(name) {
  6873. const next = this.nextTokenStart();
  6874. return this.isUnparsedContextual(next, name);
  6875. }
  6876. eatContextual(token) {
  6877. if (this.isContextual(token)) {
  6878. this.next();
  6879. return true;
  6880. }
  6881. return false;
  6882. }
  6883. expectContextual(token, toParseError) {
  6884. if (!this.eatContextual(token)) {
  6885. if (toParseError != null) {
  6886. throw this.raise(toParseError, this.state.startLoc);
  6887. }
  6888. this.unexpected(null, token);
  6889. }
  6890. }
  6891. canInsertSemicolon() {
  6892. return this.match(140) || this.match(8) || this.hasPrecedingLineBreak();
  6893. }
  6894. hasPrecedingLineBreak() {
  6895. return hasNewLine(this.input, this.offsetToSourcePos(this.state.lastTokEndLoc.index), this.state.start);
  6896. }
  6897. hasFollowingLineBreak() {
  6898. return hasNewLine(this.input, this.state.end, this.nextTokenStart());
  6899. }
  6900. isLineTerminator() {
  6901. return this.eat(13) || this.canInsertSemicolon();
  6902. }
  6903. semicolon(allowAsi = true) {
  6904. if (allowAsi ? this.isLineTerminator() : this.eat(13)) return;
  6905. this.raise(Errors.MissingSemicolon, this.state.lastTokEndLoc);
  6906. }
  6907. expect(type, loc) {
  6908. if (!this.eat(type)) {
  6909. this.unexpected(loc, type);
  6910. }
  6911. }
  6912. tryParse(fn, oldState = this.state.clone()) {
  6913. const abortSignal = {
  6914. node: null
  6915. };
  6916. try {
  6917. const node = fn((node = null) => {
  6918. abortSignal.node = node;
  6919. throw abortSignal;
  6920. });
  6921. if (this.state.errors.length > oldState.errors.length) {
  6922. const failState = this.state;
  6923. this.state = oldState;
  6924. this.state.tokensLength = failState.tokensLength;
  6925. return {
  6926. node,
  6927. error: failState.errors[oldState.errors.length],
  6928. thrown: false,
  6929. aborted: false,
  6930. failState
  6931. };
  6932. }
  6933. return {
  6934. node,
  6935. error: null,
  6936. thrown: false,
  6937. aborted: false,
  6938. failState: null
  6939. };
  6940. } catch (error) {
  6941. const failState = this.state;
  6942. this.state = oldState;
  6943. if (error instanceof SyntaxError) {
  6944. return {
  6945. node: null,
  6946. error,
  6947. thrown: true,
  6948. aborted: false,
  6949. failState
  6950. };
  6951. }
  6952. if (error === abortSignal) {
  6953. return {
  6954. node: abortSignal.node,
  6955. error: null,
  6956. thrown: false,
  6957. aborted: true,
  6958. failState
  6959. };
  6960. }
  6961. throw error;
  6962. }
  6963. }
  6964. checkExpressionErrors(refExpressionErrors, andThrow) {
  6965. if (!refExpressionErrors) return false;
  6966. const {
  6967. shorthandAssignLoc,
  6968. doubleProtoLoc,
  6969. privateKeyLoc,
  6970. optionalParametersLoc
  6971. } = refExpressionErrors;
  6972. const hasErrors = !!shorthandAssignLoc || !!doubleProtoLoc || !!optionalParametersLoc || !!privateKeyLoc;
  6973. if (!andThrow) {
  6974. return hasErrors;
  6975. }
  6976. if (shorthandAssignLoc != null) {
  6977. this.raise(Errors.InvalidCoverInitializedName, shorthandAssignLoc);
  6978. }
  6979. if (doubleProtoLoc != null) {
  6980. this.raise(Errors.DuplicateProto, doubleProtoLoc);
  6981. }
  6982. if (privateKeyLoc != null) {
  6983. this.raise(Errors.UnexpectedPrivateField, privateKeyLoc);
  6984. }
  6985. if (optionalParametersLoc != null) {
  6986. this.unexpected(optionalParametersLoc);
  6987. }
  6988. }
  6989. isLiteralPropertyName() {
  6990. return tokenIsLiteralPropertyName(this.state.type);
  6991. }
  6992. isPrivateName(node) {
  6993. return node.type === "PrivateName";
  6994. }
  6995. getPrivateNameSV(node) {
  6996. return node.id.name;
  6997. }
  6998. hasPropertyAsPrivateName(node) {
  6999. return (node.type === "MemberExpression" || node.type === "OptionalMemberExpression") && this.isPrivateName(node.property);
  7000. }
  7001. isObjectProperty(node) {
  7002. return node.type === "ObjectProperty";
  7003. }
  7004. isObjectMethod(node) {
  7005. return node.type === "ObjectMethod";
  7006. }
  7007. initializeScopes(inModule = this.options.sourceType === "module") {
  7008. const oldLabels = this.state.labels;
  7009. this.state.labels = [];
  7010. const oldExportedIdentifiers = this.exportedIdentifiers;
  7011. this.exportedIdentifiers = new Set();
  7012. const oldInModule = this.inModule;
  7013. this.inModule = inModule;
  7014. const oldScope = this.scope;
  7015. const ScopeHandler = this.getScopeHandler();
  7016. this.scope = new ScopeHandler(this, inModule);
  7017. const oldProdParam = this.prodParam;
  7018. this.prodParam = new ProductionParameterHandler();
  7019. const oldClassScope = this.classScope;
  7020. this.classScope = new ClassScopeHandler(this);
  7021. const oldExpressionScope = this.expressionScope;
  7022. this.expressionScope = new ExpressionScopeHandler(this);
  7023. return () => {
  7024. this.state.labels = oldLabels;
  7025. this.exportedIdentifiers = oldExportedIdentifiers;
  7026. this.inModule = oldInModule;
  7027. this.scope = oldScope;
  7028. this.prodParam = oldProdParam;
  7029. this.classScope = oldClassScope;
  7030. this.expressionScope = oldExpressionScope;
  7031. };
  7032. }
  7033. enterInitialScopes() {
  7034. let paramFlags = 0;
  7035. if (this.inModule) {
  7036. paramFlags |= 2;
  7037. }
  7038. if (this.optionFlags & 32) {
  7039. paramFlags |= 1;
  7040. }
  7041. this.scope.enter(1);
  7042. this.prodParam.enter(paramFlags);
  7043. }
  7044. checkDestructuringPrivate(refExpressionErrors) {
  7045. const {
  7046. privateKeyLoc
  7047. } = refExpressionErrors;
  7048. if (privateKeyLoc !== null) {
  7049. this.expectPlugin("destructuringPrivate", privateKeyLoc);
  7050. }
  7051. }
  7052. }
  7053. class ExpressionErrors {
  7054. constructor() {
  7055. this.shorthandAssignLoc = null;
  7056. this.doubleProtoLoc = null;
  7057. this.privateKeyLoc = null;
  7058. this.optionalParametersLoc = null;
  7059. }
  7060. }
  7061. class Node {
  7062. constructor(parser, pos, loc) {
  7063. this.type = "";
  7064. this.start = pos;
  7065. this.end = 0;
  7066. this.loc = new SourceLocation(loc);
  7067. if ((parser == null ? void 0 : parser.optionFlags) & 128) this.range = [pos, 0];
  7068. if (parser != null && parser.filename) this.loc.filename = parser.filename;
  7069. }
  7070. }
  7071. const NodePrototype = Node.prototype;
  7072. {
  7073. NodePrototype.__clone = function () {
  7074. const newNode = new Node(undefined, this.start, this.loc.start);
  7075. const keys = Object.keys(this);
  7076. for (let i = 0, length = keys.length; i < length; i++) {
  7077. const key = keys[i];
  7078. if (key !== "leadingComments" && key !== "trailingComments" && key !== "innerComments") {
  7079. newNode[key] = this[key];
  7080. }
  7081. }
  7082. return newNode;
  7083. };
  7084. }
  7085. class NodeUtils extends UtilParser {
  7086. startNode() {
  7087. const loc = this.state.startLoc;
  7088. return new Node(this, loc.index, loc);
  7089. }
  7090. startNodeAt(loc) {
  7091. return new Node(this, loc.index, loc);
  7092. }
  7093. startNodeAtNode(type) {
  7094. return this.startNodeAt(type.loc.start);
  7095. }
  7096. finishNode(node, type) {
  7097. return this.finishNodeAt(node, type, this.state.lastTokEndLoc);
  7098. }
  7099. finishNodeAt(node, type, endLoc) {
  7100. node.type = type;
  7101. node.end = endLoc.index;
  7102. node.loc.end = endLoc;
  7103. if (this.optionFlags & 128) node.range[1] = endLoc.index;
  7104. if (this.optionFlags & 4096) {
  7105. this.processComment(node);
  7106. }
  7107. return node;
  7108. }
  7109. resetStartLocation(node, startLoc) {
  7110. node.start = startLoc.index;
  7111. node.loc.start = startLoc;
  7112. if (this.optionFlags & 128) node.range[0] = startLoc.index;
  7113. }
  7114. resetEndLocation(node, endLoc = this.state.lastTokEndLoc) {
  7115. node.end = endLoc.index;
  7116. node.loc.end = endLoc;
  7117. if (this.optionFlags & 128) node.range[1] = endLoc.index;
  7118. }
  7119. resetStartLocationFromNode(node, locationNode) {
  7120. this.resetStartLocation(node, locationNode.loc.start);
  7121. }
  7122. castNodeTo(node, type) {
  7123. node.type = type;
  7124. return node;
  7125. }
  7126. cloneIdentifier(node) {
  7127. const {
  7128. type,
  7129. start,
  7130. end,
  7131. loc,
  7132. range,
  7133. name
  7134. } = node;
  7135. const cloned = Object.create(NodePrototype);
  7136. cloned.type = type;
  7137. cloned.start = start;
  7138. cloned.end = end;
  7139. cloned.loc = loc;
  7140. cloned.range = range;
  7141. cloned.name = name;
  7142. if (node.extra) cloned.extra = node.extra;
  7143. return cloned;
  7144. }
  7145. cloneStringLiteral(node) {
  7146. const {
  7147. type,
  7148. start,
  7149. end,
  7150. loc,
  7151. range,
  7152. extra
  7153. } = node;
  7154. const cloned = Object.create(NodePrototype);
  7155. cloned.type = type;
  7156. cloned.start = start;
  7157. cloned.end = end;
  7158. cloned.loc = loc;
  7159. cloned.range = range;
  7160. cloned.extra = extra;
  7161. cloned.value = node.value;
  7162. return cloned;
  7163. }
  7164. }
  7165. const unwrapParenthesizedExpression = node => {
  7166. return node.type === "ParenthesizedExpression" ? unwrapParenthesizedExpression(node.expression) : node;
  7167. };
  7168. class LValParser extends NodeUtils {
  7169. toAssignable(node, isLHS = false) {
  7170. var _node$extra, _node$extra3;
  7171. let parenthesized = undefined;
  7172. if (node.type === "ParenthesizedExpression" || (_node$extra = node.extra) != null && _node$extra.parenthesized) {
  7173. parenthesized = unwrapParenthesizedExpression(node);
  7174. if (isLHS) {
  7175. if (parenthesized.type === "Identifier") {
  7176. this.expressionScope.recordArrowParameterBindingError(Errors.InvalidParenthesizedAssignment, node);
  7177. } else if (parenthesized.type !== "MemberExpression" && !this.isOptionalMemberExpression(parenthesized)) {
  7178. this.raise(Errors.InvalidParenthesizedAssignment, node);
  7179. }
  7180. } else {
  7181. this.raise(Errors.InvalidParenthesizedAssignment, node);
  7182. }
  7183. }
  7184. switch (node.type) {
  7185. case "Identifier":
  7186. case "ObjectPattern":
  7187. case "ArrayPattern":
  7188. case "AssignmentPattern":
  7189. case "RestElement":
  7190. break;
  7191. case "ObjectExpression":
  7192. this.castNodeTo(node, "ObjectPattern");
  7193. for (let i = 0, length = node.properties.length, last = length - 1; i < length; i++) {
  7194. var _node$extra2;
  7195. const prop = node.properties[i];
  7196. const isLast = i === last;
  7197. this.toAssignableObjectExpressionProp(prop, isLast, isLHS);
  7198. if (isLast && prop.type === "RestElement" && (_node$extra2 = node.extra) != null && _node$extra2.trailingCommaLoc) {
  7199. this.raise(Errors.RestTrailingComma, node.extra.trailingCommaLoc);
  7200. }
  7201. }
  7202. break;
  7203. case "ObjectProperty":
  7204. {
  7205. const {
  7206. key,
  7207. value
  7208. } = node;
  7209. if (this.isPrivateName(key)) {
  7210. this.classScope.usePrivateName(this.getPrivateNameSV(key), key.loc.start);
  7211. }
  7212. this.toAssignable(value, isLHS);
  7213. break;
  7214. }
  7215. case "SpreadElement":
  7216. {
  7217. throw new Error("Internal @babel/parser error (this is a bug, please report it)." + " SpreadElement should be converted by .toAssignable's caller.");
  7218. }
  7219. case "ArrayExpression":
  7220. this.castNodeTo(node, "ArrayPattern");
  7221. this.toAssignableList(node.elements, (_node$extra3 = node.extra) == null ? void 0 : _node$extra3.trailingCommaLoc, isLHS);
  7222. break;
  7223. case "AssignmentExpression":
  7224. if (node.operator !== "=") {
  7225. this.raise(Errors.MissingEqInAssignment, node.left.loc.end);
  7226. }
  7227. this.castNodeTo(node, "AssignmentPattern");
  7228. delete node.operator;
  7229. this.toAssignable(node.left, isLHS);
  7230. break;
  7231. case "ParenthesizedExpression":
  7232. this.toAssignable(parenthesized, isLHS);
  7233. break;
  7234. }
  7235. }
  7236. toAssignableObjectExpressionProp(prop, isLast, isLHS) {
  7237. if (prop.type === "ObjectMethod") {
  7238. this.raise(prop.kind === "get" || prop.kind === "set" ? Errors.PatternHasAccessor : Errors.PatternHasMethod, prop.key);
  7239. } else if (prop.type === "SpreadElement") {
  7240. this.castNodeTo(prop, "RestElement");
  7241. const arg = prop.argument;
  7242. this.checkToRestConversion(arg, false);
  7243. this.toAssignable(arg, isLHS);
  7244. if (!isLast) {
  7245. this.raise(Errors.RestTrailingComma, prop);
  7246. }
  7247. } else {
  7248. this.toAssignable(prop, isLHS);
  7249. }
  7250. }
  7251. toAssignableList(exprList, trailingCommaLoc, isLHS) {
  7252. const end = exprList.length - 1;
  7253. for (let i = 0; i <= end; i++) {
  7254. const elt = exprList[i];
  7255. if (!elt) continue;
  7256. this.toAssignableListItem(exprList, i, isLHS);
  7257. if (elt.type === "RestElement") {
  7258. if (i < end) {
  7259. this.raise(Errors.RestTrailingComma, elt);
  7260. } else if (trailingCommaLoc) {
  7261. this.raise(Errors.RestTrailingComma, trailingCommaLoc);
  7262. }
  7263. }
  7264. }
  7265. }
  7266. toAssignableListItem(exprList, index, isLHS) {
  7267. const node = exprList[index];
  7268. if (node.type === "SpreadElement") {
  7269. this.castNodeTo(node, "RestElement");
  7270. const arg = node.argument;
  7271. this.checkToRestConversion(arg, true);
  7272. this.toAssignable(arg, isLHS);
  7273. } else {
  7274. this.toAssignable(node, isLHS);
  7275. }
  7276. }
  7277. isAssignable(node, isBinding) {
  7278. switch (node.type) {
  7279. case "Identifier":
  7280. case "ObjectPattern":
  7281. case "ArrayPattern":
  7282. case "AssignmentPattern":
  7283. case "RestElement":
  7284. return true;
  7285. case "ObjectExpression":
  7286. {
  7287. const last = node.properties.length - 1;
  7288. return node.properties.every((prop, i) => {
  7289. return prop.type !== "ObjectMethod" && (i === last || prop.type !== "SpreadElement") && this.isAssignable(prop);
  7290. });
  7291. }
  7292. case "ObjectProperty":
  7293. return this.isAssignable(node.value);
  7294. case "SpreadElement":
  7295. return this.isAssignable(node.argument);
  7296. case "ArrayExpression":
  7297. return node.elements.every(element => element === null || this.isAssignable(element));
  7298. case "AssignmentExpression":
  7299. return node.operator === "=";
  7300. case "ParenthesizedExpression":
  7301. return this.isAssignable(node.expression);
  7302. case "MemberExpression":
  7303. case "OptionalMemberExpression":
  7304. return !isBinding;
  7305. default:
  7306. return false;
  7307. }
  7308. }
  7309. toReferencedList(exprList, isParenthesizedExpr) {
  7310. return exprList;
  7311. }
  7312. toReferencedListDeep(exprList, isParenthesizedExpr) {
  7313. this.toReferencedList(exprList, isParenthesizedExpr);
  7314. for (const expr of exprList) {
  7315. if ((expr == null ? void 0 : expr.type) === "ArrayExpression") {
  7316. this.toReferencedListDeep(expr.elements);
  7317. }
  7318. }
  7319. }
  7320. parseSpread(refExpressionErrors) {
  7321. const node = this.startNode();
  7322. this.next();
  7323. node.argument = this.parseMaybeAssignAllowIn(refExpressionErrors, undefined);
  7324. return this.finishNode(node, "SpreadElement");
  7325. }
  7326. parseRestBinding() {
  7327. const node = this.startNode();
  7328. this.next();
  7329. node.argument = this.parseBindingAtom();
  7330. return this.finishNode(node, "RestElement");
  7331. }
  7332. parseBindingAtom() {
  7333. switch (this.state.type) {
  7334. case 0:
  7335. {
  7336. const node = this.startNode();
  7337. this.next();
  7338. node.elements = this.parseBindingList(3, 93, 1);
  7339. return this.finishNode(node, "ArrayPattern");
  7340. }
  7341. case 5:
  7342. return this.parseObjectLike(8, true);
  7343. }
  7344. return this.parseIdentifier();
  7345. }
  7346. parseBindingList(close, closeCharCode, flags) {
  7347. const allowEmpty = flags & 1;
  7348. const elts = [];
  7349. let first = true;
  7350. while (!this.eat(close)) {
  7351. if (first) {
  7352. first = false;
  7353. } else {
  7354. this.expect(12);
  7355. }
  7356. if (allowEmpty && this.match(12)) {
  7357. elts.push(null);
  7358. } else if (this.eat(close)) {
  7359. break;
  7360. } else if (this.match(21)) {
  7361. let rest = this.parseRestBinding();
  7362. if (this.hasPlugin("flow") || flags & 2) {
  7363. rest = this.parseFunctionParamType(rest);
  7364. }
  7365. elts.push(rest);
  7366. if (!this.checkCommaAfterRest(closeCharCode)) {
  7367. this.expect(close);
  7368. break;
  7369. }
  7370. } else {
  7371. const decorators = [];
  7372. if (flags & 2) {
  7373. if (this.match(26) && this.hasPlugin("decorators")) {
  7374. this.raise(Errors.UnsupportedParameterDecorator, this.state.startLoc);
  7375. }
  7376. while (this.match(26)) {
  7377. decorators.push(this.parseDecorator());
  7378. }
  7379. }
  7380. elts.push(this.parseBindingElement(flags, decorators));
  7381. }
  7382. }
  7383. return elts;
  7384. }
  7385. parseBindingRestProperty(prop) {
  7386. this.next();
  7387. prop.argument = this.parseIdentifier();
  7388. this.checkCommaAfterRest(125);
  7389. return this.finishNode(prop, "RestElement");
  7390. }
  7391. parseBindingProperty() {
  7392. const {
  7393. type,
  7394. startLoc
  7395. } = this.state;
  7396. if (type === 21) {
  7397. return this.parseBindingRestProperty(this.startNode());
  7398. }
  7399. const prop = this.startNode();
  7400. if (type === 139) {
  7401. this.expectPlugin("destructuringPrivate", startLoc);
  7402. this.classScope.usePrivateName(this.state.value, startLoc);
  7403. prop.key = this.parsePrivateName();
  7404. } else {
  7405. this.parsePropertyName(prop);
  7406. }
  7407. prop.method = false;
  7408. return this.parseObjPropValue(prop, startLoc, false, false, true, false);
  7409. }
  7410. parseBindingElement(flags, decorators) {
  7411. const left = this.parseMaybeDefault();
  7412. if (this.hasPlugin("flow") || flags & 2) {
  7413. this.parseFunctionParamType(left);
  7414. }
  7415. if (decorators.length) {
  7416. left.decorators = decorators;
  7417. this.resetStartLocationFromNode(left, decorators[0]);
  7418. }
  7419. const elt = this.parseMaybeDefault(left.loc.start, left);
  7420. return elt;
  7421. }
  7422. parseFunctionParamType(param) {
  7423. return param;
  7424. }
  7425. parseMaybeDefault(startLoc, left) {
  7426. startLoc != null ? startLoc : startLoc = this.state.startLoc;
  7427. left = left != null ? left : this.parseBindingAtom();
  7428. if (!this.eat(29)) return left;
  7429. const node = this.startNodeAt(startLoc);
  7430. node.left = left;
  7431. node.right = this.parseMaybeAssignAllowIn();
  7432. return this.finishNode(node, "AssignmentPattern");
  7433. }
  7434. isValidLVal(type, isUnparenthesizedInAssign, binding) {
  7435. switch (type) {
  7436. case "AssignmentPattern":
  7437. return "left";
  7438. case "RestElement":
  7439. return "argument";
  7440. case "ObjectProperty":
  7441. return "value";
  7442. case "ParenthesizedExpression":
  7443. return "expression";
  7444. case "ArrayPattern":
  7445. return "elements";
  7446. case "ObjectPattern":
  7447. return "properties";
  7448. }
  7449. return false;
  7450. }
  7451. isOptionalMemberExpression(expression) {
  7452. return expression.type === "OptionalMemberExpression";
  7453. }
  7454. checkLVal(expression, ancestor, binding = 64, checkClashes = false, strictModeChanged = false, hasParenthesizedAncestor = false) {
  7455. var _expression$extra;
  7456. const type = expression.type;
  7457. if (this.isObjectMethod(expression)) return;
  7458. const isOptionalMemberExpression = this.isOptionalMemberExpression(expression);
  7459. if (isOptionalMemberExpression || type === "MemberExpression") {
  7460. if (isOptionalMemberExpression) {
  7461. this.expectPlugin("optionalChainingAssign", expression.loc.start);
  7462. if (ancestor.type !== "AssignmentExpression") {
  7463. this.raise(Errors.InvalidLhsOptionalChaining, expression, {
  7464. ancestor
  7465. });
  7466. }
  7467. }
  7468. if (binding !== 64) {
  7469. this.raise(Errors.InvalidPropertyBindingPattern, expression);
  7470. }
  7471. return;
  7472. }
  7473. if (type === "Identifier") {
  7474. this.checkIdentifier(expression, binding, strictModeChanged);
  7475. const {
  7476. name
  7477. } = expression;
  7478. if (checkClashes) {
  7479. if (checkClashes.has(name)) {
  7480. this.raise(Errors.ParamDupe, expression);
  7481. } else {
  7482. checkClashes.add(name);
  7483. }
  7484. }
  7485. return;
  7486. }
  7487. const validity = this.isValidLVal(type, !(hasParenthesizedAncestor || (_expression$extra = expression.extra) != null && _expression$extra.parenthesized) && ancestor.type === "AssignmentExpression", binding);
  7488. if (validity === true) return;
  7489. if (validity === false) {
  7490. const ParseErrorClass = binding === 64 ? Errors.InvalidLhs : Errors.InvalidLhsBinding;
  7491. this.raise(ParseErrorClass, expression, {
  7492. ancestor
  7493. });
  7494. return;
  7495. }
  7496. let key, isParenthesizedExpression;
  7497. if (typeof validity === "string") {
  7498. key = validity;
  7499. isParenthesizedExpression = type === "ParenthesizedExpression";
  7500. } else {
  7501. [key, isParenthesizedExpression] = validity;
  7502. }
  7503. const nextAncestor = type === "ArrayPattern" || type === "ObjectPattern" ? {
  7504. type
  7505. } : ancestor;
  7506. const val = expression[key];
  7507. if (Array.isArray(val)) {
  7508. for (const child of val) {
  7509. if (child) {
  7510. this.checkLVal(child, nextAncestor, binding, checkClashes, strictModeChanged, isParenthesizedExpression);
  7511. }
  7512. }
  7513. } else if (val) {
  7514. this.checkLVal(val, nextAncestor, binding, checkClashes, strictModeChanged, isParenthesizedExpression);
  7515. }
  7516. }
  7517. checkIdentifier(at, bindingType, strictModeChanged = false) {
  7518. if (this.state.strict && (strictModeChanged ? isStrictBindReservedWord(at.name, this.inModule) : isStrictBindOnlyReservedWord(at.name))) {
  7519. if (bindingType === 64) {
  7520. this.raise(Errors.StrictEvalArguments, at, {
  7521. referenceName: at.name
  7522. });
  7523. } else {
  7524. this.raise(Errors.StrictEvalArgumentsBinding, at, {
  7525. bindingName: at.name
  7526. });
  7527. }
  7528. }
  7529. if (bindingType & 8192 && at.name === "let") {
  7530. this.raise(Errors.LetInLexicalBinding, at);
  7531. }
  7532. if (!(bindingType & 64)) {
  7533. this.declareNameFromIdentifier(at, bindingType);
  7534. }
  7535. }
  7536. declareNameFromIdentifier(identifier, binding) {
  7537. this.scope.declareName(identifier.name, binding, identifier.loc.start);
  7538. }
  7539. checkToRestConversion(node, allowPattern) {
  7540. switch (node.type) {
  7541. case "ParenthesizedExpression":
  7542. this.checkToRestConversion(node.expression, allowPattern);
  7543. break;
  7544. case "Identifier":
  7545. case "MemberExpression":
  7546. break;
  7547. case "ArrayExpression":
  7548. case "ObjectExpression":
  7549. if (allowPattern) break;
  7550. default:
  7551. this.raise(Errors.InvalidRestAssignmentPattern, node);
  7552. }
  7553. }
  7554. checkCommaAfterRest(close) {
  7555. if (!this.match(12)) {
  7556. return false;
  7557. }
  7558. this.raise(this.lookaheadCharCode() === close ? Errors.RestTrailingComma : Errors.ElementAfterRest, this.state.startLoc);
  7559. return true;
  7560. }
  7561. }
  7562. function nonNull(x) {
  7563. if (x == null) {
  7564. throw new Error(`Unexpected ${x} value.`);
  7565. }
  7566. return x;
  7567. }
  7568. function assert(x) {
  7569. if (!x) {
  7570. throw new Error("Assert fail");
  7571. }
  7572. }
  7573. const TSErrors = ParseErrorEnum`typescript`({
  7574. AbstractMethodHasImplementation: ({
  7575. methodName
  7576. }) => `Method '${methodName}' cannot have an implementation because it is marked abstract.`,
  7577. AbstractPropertyHasInitializer: ({
  7578. propertyName
  7579. }) => `Property '${propertyName}' cannot have an initializer because it is marked abstract.`,
  7580. AccessorCannotBeOptional: "An 'accessor' property cannot be declared optional.",
  7581. AccessorCannotDeclareThisParameter: "'get' and 'set' accessors cannot declare 'this' parameters.",
  7582. AccessorCannotHaveTypeParameters: "An accessor cannot have type parameters.",
  7583. ClassMethodHasDeclare: "Class methods cannot have the 'declare' modifier.",
  7584. ClassMethodHasReadonly: "Class methods cannot have the 'readonly' modifier.",
  7585. ConstInitializerMustBeStringOrNumericLiteralOrLiteralEnumReference: "A 'const' initializer in an ambient context must be a string or numeric literal or literal enum reference.",
  7586. ConstructorHasTypeParameters: "Type parameters cannot appear on a constructor declaration.",
  7587. DeclareAccessor: ({
  7588. kind
  7589. }) => `'declare' is not allowed in ${kind}ters.`,
  7590. DeclareClassFieldHasInitializer: "Initializers are not allowed in ambient contexts.",
  7591. DeclareFunctionHasImplementation: "An implementation cannot be declared in ambient contexts.",
  7592. DuplicateAccessibilityModifier: ({
  7593. modifier
  7594. }) => `Accessibility modifier already seen: '${modifier}'.`,
  7595. DuplicateModifier: ({
  7596. modifier
  7597. }) => `Duplicate modifier: '${modifier}'.`,
  7598. EmptyHeritageClauseType: ({
  7599. token
  7600. }) => `'${token}' list cannot be empty.`,
  7601. EmptyTypeArguments: "Type argument list cannot be empty.",
  7602. EmptyTypeParameters: "Type parameter list cannot be empty.",
  7603. ExpectedAmbientAfterExportDeclare: "'export declare' must be followed by an ambient declaration.",
  7604. ImportAliasHasImportType: "An import alias can not use 'import type'.",
  7605. ImportReflectionHasImportType: "An `import module` declaration can not use `type` modifier",
  7606. IncompatibleModifiers: ({
  7607. modifiers
  7608. }) => `'${modifiers[0]}' modifier cannot be used with '${modifiers[1]}' modifier.`,
  7609. IndexSignatureHasAbstract: "Index signatures cannot have the 'abstract' modifier.",
  7610. IndexSignatureHasAccessibility: ({
  7611. modifier
  7612. }) => `Index signatures cannot have an accessibility modifier ('${modifier}').`,
  7613. IndexSignatureHasDeclare: "Index signatures cannot have the 'declare' modifier.",
  7614. IndexSignatureHasOverride: "'override' modifier cannot appear on an index signature.",
  7615. IndexSignatureHasStatic: "Index signatures cannot have the 'static' modifier.",
  7616. InitializerNotAllowedInAmbientContext: "Initializers are not allowed in ambient contexts.",
  7617. InvalidHeritageClauseType: ({
  7618. token
  7619. }) => `'${token}' list can only include identifiers or qualified-names with optional type arguments.`,
  7620. InvalidModifierOnAwaitUsingDeclaration: modifier => `'${modifier}' modifier cannot appear on an await using declaration.`,
  7621. InvalidModifierOnTypeMember: ({
  7622. modifier
  7623. }) => `'${modifier}' modifier cannot appear on a type member.`,
  7624. InvalidModifierOnTypeParameter: ({
  7625. modifier
  7626. }) => `'${modifier}' modifier cannot appear on a type parameter.`,
  7627. InvalidModifierOnTypeParameterPositions: ({
  7628. modifier
  7629. }) => `'${modifier}' modifier can only appear on a type parameter of a class, interface or type alias.`,
  7630. InvalidModifierOnUsingDeclaration: modifier => `'${modifier}' modifier cannot appear on a using declaration.`,
  7631. InvalidModifiersOrder: ({
  7632. orderedModifiers
  7633. }) => `'${orderedModifiers[0]}' modifier must precede '${orderedModifiers[1]}' modifier.`,
  7634. InvalidPropertyAccessAfterInstantiationExpression: "Invalid property access after an instantiation expression. " + "You can either wrap the instantiation expression in parentheses, or delete the type arguments.",
  7635. InvalidTupleMemberLabel: "Tuple members must be labeled with a simple identifier.",
  7636. MissingInterfaceName: "'interface' declarations must be followed by an identifier.",
  7637. NonAbstractClassHasAbstractMethod: "Abstract methods can only appear within an abstract class.",
  7638. NonClassMethodPropertyHasAbstractModifier: "'abstract' modifier can only appear on a class, method, or property declaration.",
  7639. OptionalTypeBeforeRequired: "A required element cannot follow an optional element.",
  7640. OverrideNotInSubClass: "This member cannot have an 'override' modifier because its containing class does not extend another class.",
  7641. PatternIsOptional: "A binding pattern parameter cannot be optional in an implementation signature.",
  7642. PrivateElementHasAbstract: "Private elements cannot have the 'abstract' modifier.",
  7643. PrivateElementHasAccessibility: ({
  7644. modifier
  7645. }) => `Private elements cannot have an accessibility modifier ('${modifier}').`,
  7646. ReadonlyForMethodSignature: "'readonly' modifier can only appear on a property declaration or index signature.",
  7647. ReservedArrowTypeParam: "This syntax is reserved in files with the .mts or .cts extension. Add a trailing comma, as in `<T,>() => ...`.",
  7648. ReservedTypeAssertion: "This syntax is reserved in files with the .mts or .cts extension. Use an `as` expression instead.",
  7649. SetAccessorCannotHaveOptionalParameter: "A 'set' accessor cannot have an optional parameter.",
  7650. SetAccessorCannotHaveRestParameter: "A 'set' accessor cannot have rest parameter.",
  7651. SetAccessorCannotHaveReturnType: "A 'set' accessor cannot have a return type annotation.",
  7652. SingleTypeParameterWithoutTrailingComma: ({
  7653. typeParameterName
  7654. }) => `Single type parameter ${typeParameterName} should have a trailing comma. Example usage: <${typeParameterName},>.`,
  7655. StaticBlockCannotHaveModifier: "Static class blocks cannot have any modifier.",
  7656. 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?`).",
  7657. TypeAnnotationAfterAssign: "Type annotations must come before default assignments, e.g. instead of `age = 25: number` use `age: number = 25`.",
  7658. TypeImportCannotSpecifyDefaultAndNamed: "A type-only import can specify a default import or named bindings, but not both.",
  7659. TypeModifierIsUsedInTypeExports: "The 'type' modifier cannot be used on a named export when 'export type' is used on its export statement.",
  7660. TypeModifierIsUsedInTypeImports: "The 'type' modifier cannot be used on a named import when 'import type' is used on its import statement.",
  7661. UnexpectedParameterModifier: "A parameter property is only allowed in a constructor implementation.",
  7662. UnexpectedReadonly: "'readonly' type modifier is only permitted on array and tuple literal types.",
  7663. UnexpectedTypeAnnotation: "Did not expect a type annotation here.",
  7664. UnexpectedTypeCastInParameter: "Unexpected type cast in parameter position.",
  7665. UnsupportedImportTypeArgument: "Argument in a type import must be a string literal.",
  7666. UnsupportedParameterPropertyKind: "A parameter property may not be declared using a binding pattern.",
  7667. UnsupportedSignatureParameterKind: ({
  7668. type
  7669. }) => `Name in a signature must be an Identifier, ObjectPattern or ArrayPattern, instead got ${type}.`
  7670. });
  7671. function keywordTypeFromName(value) {
  7672. switch (value) {
  7673. case "any":
  7674. return "TSAnyKeyword";
  7675. case "boolean":
  7676. return "TSBooleanKeyword";
  7677. case "bigint":
  7678. return "TSBigIntKeyword";
  7679. case "never":
  7680. return "TSNeverKeyword";
  7681. case "number":
  7682. return "TSNumberKeyword";
  7683. case "object":
  7684. return "TSObjectKeyword";
  7685. case "string":
  7686. return "TSStringKeyword";
  7687. case "symbol":
  7688. return "TSSymbolKeyword";
  7689. case "undefined":
  7690. return "TSUndefinedKeyword";
  7691. case "unknown":
  7692. return "TSUnknownKeyword";
  7693. default:
  7694. return undefined;
  7695. }
  7696. }
  7697. function tsIsAccessModifier(modifier) {
  7698. return modifier === "private" || modifier === "public" || modifier === "protected";
  7699. }
  7700. function tsIsVarianceAnnotations(modifier) {
  7701. return modifier === "in" || modifier === "out";
  7702. }
  7703. var typescript = superClass => class TypeScriptParserMixin extends superClass {
  7704. constructor(...args) {
  7705. super(...args);
  7706. this.tsParseInOutModifiers = this.tsParseModifiers.bind(this, {
  7707. allowedModifiers: ["in", "out"],
  7708. disallowedModifiers: ["const", "public", "private", "protected", "readonly", "declare", "abstract", "override"],
  7709. errorTemplate: TSErrors.InvalidModifierOnTypeParameter
  7710. });
  7711. this.tsParseConstModifier = this.tsParseModifiers.bind(this, {
  7712. allowedModifiers: ["const"],
  7713. disallowedModifiers: ["in", "out"],
  7714. errorTemplate: TSErrors.InvalidModifierOnTypeParameterPositions
  7715. });
  7716. this.tsParseInOutConstModifiers = this.tsParseModifiers.bind(this, {
  7717. allowedModifiers: ["in", "out", "const"],
  7718. disallowedModifiers: ["public", "private", "protected", "readonly", "declare", "abstract", "override"],
  7719. errorTemplate: TSErrors.InvalidModifierOnTypeParameter
  7720. });
  7721. }
  7722. getScopeHandler() {
  7723. return TypeScriptScopeHandler;
  7724. }
  7725. tsIsIdentifier() {
  7726. return tokenIsIdentifier(this.state.type);
  7727. }
  7728. tsTokenCanFollowModifier() {
  7729. return this.match(0) || this.match(5) || this.match(55) || this.match(21) || this.match(139) || this.isLiteralPropertyName();
  7730. }
  7731. tsNextTokenOnSameLineAndCanFollowModifier() {
  7732. this.next();
  7733. if (this.hasPrecedingLineBreak()) {
  7734. return false;
  7735. }
  7736. return this.tsTokenCanFollowModifier();
  7737. }
  7738. tsNextTokenCanFollowModifier() {
  7739. if (this.match(106)) {
  7740. this.next();
  7741. return this.tsTokenCanFollowModifier();
  7742. }
  7743. return this.tsNextTokenOnSameLineAndCanFollowModifier();
  7744. }
  7745. tsParseModifier(allowedModifiers, stopOnStartOfClassStaticBlock, hasSeenStaticModifier) {
  7746. if (!tokenIsIdentifier(this.state.type) && this.state.type !== 58 && this.state.type !== 75) {
  7747. return undefined;
  7748. }
  7749. const modifier = this.state.value;
  7750. if (allowedModifiers.includes(modifier)) {
  7751. if (hasSeenStaticModifier && this.match(106)) {
  7752. return undefined;
  7753. }
  7754. if (stopOnStartOfClassStaticBlock && this.tsIsStartOfStaticBlocks()) {
  7755. return undefined;
  7756. }
  7757. if (this.tsTryParse(this.tsNextTokenCanFollowModifier.bind(this))) {
  7758. return modifier;
  7759. }
  7760. }
  7761. return undefined;
  7762. }
  7763. tsParseModifiers({
  7764. allowedModifiers,
  7765. disallowedModifiers,
  7766. stopOnStartOfClassStaticBlock,
  7767. errorTemplate = TSErrors.InvalidModifierOnTypeMember
  7768. }, modified) {
  7769. const enforceOrder = (loc, modifier, before, after) => {
  7770. if (modifier === before && modified[after]) {
  7771. this.raise(TSErrors.InvalidModifiersOrder, loc, {
  7772. orderedModifiers: [before, after]
  7773. });
  7774. }
  7775. };
  7776. const incompatible = (loc, modifier, mod1, mod2) => {
  7777. if (modified[mod1] && modifier === mod2 || modified[mod2] && modifier === mod1) {
  7778. this.raise(TSErrors.IncompatibleModifiers, loc, {
  7779. modifiers: [mod1, mod2]
  7780. });
  7781. }
  7782. };
  7783. for (;;) {
  7784. const {
  7785. startLoc
  7786. } = this.state;
  7787. const modifier = this.tsParseModifier(allowedModifiers.concat(disallowedModifiers != null ? disallowedModifiers : []), stopOnStartOfClassStaticBlock, modified.static);
  7788. if (!modifier) break;
  7789. if (tsIsAccessModifier(modifier)) {
  7790. if (modified.accessibility) {
  7791. this.raise(TSErrors.DuplicateAccessibilityModifier, startLoc, {
  7792. modifier
  7793. });
  7794. } else {
  7795. enforceOrder(startLoc, modifier, modifier, "override");
  7796. enforceOrder(startLoc, modifier, modifier, "static");
  7797. enforceOrder(startLoc, modifier, modifier, "readonly");
  7798. modified.accessibility = modifier;
  7799. }
  7800. } else if (tsIsVarianceAnnotations(modifier)) {
  7801. if (modified[modifier]) {
  7802. this.raise(TSErrors.DuplicateModifier, startLoc, {
  7803. modifier
  7804. });
  7805. }
  7806. modified[modifier] = true;
  7807. enforceOrder(startLoc, modifier, "in", "out");
  7808. } else {
  7809. if (hasOwnProperty.call(modified, modifier)) {
  7810. this.raise(TSErrors.DuplicateModifier, startLoc, {
  7811. modifier
  7812. });
  7813. } else {
  7814. enforceOrder(startLoc, modifier, "static", "readonly");
  7815. enforceOrder(startLoc, modifier, "static", "override");
  7816. enforceOrder(startLoc, modifier, "override", "readonly");
  7817. enforceOrder(startLoc, modifier, "abstract", "override");
  7818. incompatible(startLoc, modifier, "declare", "override");
  7819. incompatible(startLoc, modifier, "static", "abstract");
  7820. }
  7821. modified[modifier] = true;
  7822. }
  7823. if (disallowedModifiers != null && disallowedModifiers.includes(modifier)) {
  7824. this.raise(errorTemplate, startLoc, {
  7825. modifier
  7826. });
  7827. }
  7828. }
  7829. }
  7830. tsIsListTerminator(kind) {
  7831. switch (kind) {
  7832. case "EnumMembers":
  7833. case "TypeMembers":
  7834. return this.match(8);
  7835. case "HeritageClauseElement":
  7836. return this.match(5);
  7837. case "TupleElementTypes":
  7838. return this.match(3);
  7839. case "TypeParametersOrArguments":
  7840. return this.match(48);
  7841. }
  7842. }
  7843. tsParseList(kind, parseElement) {
  7844. const result = [];
  7845. while (!this.tsIsListTerminator(kind)) {
  7846. result.push(parseElement());
  7847. }
  7848. return result;
  7849. }
  7850. tsParseDelimitedList(kind, parseElement, refTrailingCommaPos) {
  7851. return nonNull(this.tsParseDelimitedListWorker(kind, parseElement, true, refTrailingCommaPos));
  7852. }
  7853. tsParseDelimitedListWorker(kind, parseElement, expectSuccess, refTrailingCommaPos) {
  7854. const result = [];
  7855. let trailingCommaPos = -1;
  7856. for (;;) {
  7857. if (this.tsIsListTerminator(kind)) {
  7858. break;
  7859. }
  7860. trailingCommaPos = -1;
  7861. const element = parseElement();
  7862. if (element == null) {
  7863. return undefined;
  7864. }
  7865. result.push(element);
  7866. if (this.eat(12)) {
  7867. trailingCommaPos = this.state.lastTokStartLoc.index;
  7868. continue;
  7869. }
  7870. if (this.tsIsListTerminator(kind)) {
  7871. break;
  7872. }
  7873. if (expectSuccess) {
  7874. this.expect(12);
  7875. }
  7876. return undefined;
  7877. }
  7878. if (refTrailingCommaPos) {
  7879. refTrailingCommaPos.value = trailingCommaPos;
  7880. }
  7881. return result;
  7882. }
  7883. tsParseBracketedList(kind, parseElement, bracket, skipFirstToken, refTrailingCommaPos) {
  7884. if (!skipFirstToken) {
  7885. if (bracket) {
  7886. this.expect(0);
  7887. } else {
  7888. this.expect(47);
  7889. }
  7890. }
  7891. const result = this.tsParseDelimitedList(kind, parseElement, refTrailingCommaPos);
  7892. if (bracket) {
  7893. this.expect(3);
  7894. } else {
  7895. this.expect(48);
  7896. }
  7897. return result;
  7898. }
  7899. tsParseImportType() {
  7900. const node = this.startNode();
  7901. this.expect(83);
  7902. this.expect(10);
  7903. if (!this.match(134)) {
  7904. this.raise(TSErrors.UnsupportedImportTypeArgument, this.state.startLoc);
  7905. {
  7906. node.argument = super.parseExprAtom();
  7907. }
  7908. } else {
  7909. {
  7910. node.argument = this.parseStringLiteral(this.state.value);
  7911. }
  7912. }
  7913. if (this.eat(12)) {
  7914. node.options = this.tsParseImportTypeOptions();
  7915. } else {
  7916. node.options = null;
  7917. }
  7918. this.expect(11);
  7919. if (this.eat(16)) {
  7920. node.qualifier = this.tsParseEntityName(1 | 2);
  7921. }
  7922. if (this.match(47)) {
  7923. {
  7924. node.typeParameters = this.tsParseTypeArguments();
  7925. }
  7926. }
  7927. return this.finishNode(node, "TSImportType");
  7928. }
  7929. tsParseImportTypeOptions() {
  7930. const node = this.startNode();
  7931. this.expect(5);
  7932. const withProperty = this.startNode();
  7933. if (this.isContextual(76)) {
  7934. withProperty.method = false;
  7935. withProperty.key = this.parseIdentifier(true);
  7936. withProperty.computed = false;
  7937. withProperty.shorthand = false;
  7938. } else {
  7939. this.unexpected(null, 76);
  7940. }
  7941. this.expect(14);
  7942. withProperty.value = this.tsParseImportTypeWithPropertyValue();
  7943. node.properties = [this.finishObjectProperty(withProperty)];
  7944. this.expect(8);
  7945. return this.finishNode(node, "ObjectExpression");
  7946. }
  7947. tsParseImportTypeWithPropertyValue() {
  7948. const node = this.startNode();
  7949. const properties = [];
  7950. this.expect(5);
  7951. while (!this.match(8)) {
  7952. const type = this.state.type;
  7953. if (tokenIsIdentifier(type) || type === 134) {
  7954. properties.push(super.parsePropertyDefinition(null));
  7955. } else {
  7956. this.unexpected();
  7957. }
  7958. this.eat(12);
  7959. }
  7960. node.properties = properties;
  7961. this.next();
  7962. return this.finishNode(node, "ObjectExpression");
  7963. }
  7964. tsParseEntityName(flags) {
  7965. let entity;
  7966. if (flags & 1 && this.match(78)) {
  7967. if (flags & 2) {
  7968. entity = this.parseIdentifier(true);
  7969. } else {
  7970. const node = this.startNode();
  7971. this.next();
  7972. entity = this.finishNode(node, "ThisExpression");
  7973. }
  7974. } else {
  7975. entity = this.parseIdentifier(!!(flags & 1));
  7976. }
  7977. while (this.eat(16)) {
  7978. const node = this.startNodeAtNode(entity);
  7979. node.left = entity;
  7980. node.right = this.parseIdentifier(!!(flags & 1));
  7981. entity = this.finishNode(node, "TSQualifiedName");
  7982. }
  7983. return entity;
  7984. }
  7985. tsParseTypeReference() {
  7986. const node = this.startNode();
  7987. node.typeName = this.tsParseEntityName(1);
  7988. if (!this.hasPrecedingLineBreak() && this.match(47)) {
  7989. {
  7990. node.typeParameters = this.tsParseTypeArguments();
  7991. }
  7992. }
  7993. return this.finishNode(node, "TSTypeReference");
  7994. }
  7995. tsParseThisTypePredicate(lhs) {
  7996. this.next();
  7997. const node = this.startNodeAtNode(lhs);
  7998. node.parameterName = lhs;
  7999. node.typeAnnotation = this.tsParseTypeAnnotation(false);
  8000. node.asserts = false;
  8001. return this.finishNode(node, "TSTypePredicate");
  8002. }
  8003. tsParseThisTypeNode() {
  8004. const node = this.startNode();
  8005. this.next();
  8006. return this.finishNode(node, "TSThisType");
  8007. }
  8008. tsParseTypeQuery() {
  8009. const node = this.startNode();
  8010. this.expect(87);
  8011. if (this.match(83)) {
  8012. node.exprName = this.tsParseImportType();
  8013. } else {
  8014. {
  8015. node.exprName = this.tsParseEntityName(1 | 2);
  8016. }
  8017. }
  8018. if (!this.hasPrecedingLineBreak() && this.match(47)) {
  8019. {
  8020. node.typeParameters = this.tsParseTypeArguments();
  8021. }
  8022. }
  8023. return this.finishNode(node, "TSTypeQuery");
  8024. }
  8025. tsParseTypeParameter(parseModifiers) {
  8026. const node = this.startNode();
  8027. parseModifiers(node);
  8028. node.name = this.tsParseTypeParameterName();
  8029. node.constraint = this.tsEatThenParseType(81);
  8030. node.default = this.tsEatThenParseType(29);
  8031. return this.finishNode(node, "TSTypeParameter");
  8032. }
  8033. tsTryParseTypeParameters(parseModifiers) {
  8034. if (this.match(47)) {
  8035. return this.tsParseTypeParameters(parseModifiers);
  8036. }
  8037. }
  8038. tsParseTypeParameters(parseModifiers) {
  8039. const node = this.startNode();
  8040. if (this.match(47) || this.match(143)) {
  8041. this.next();
  8042. } else {
  8043. this.unexpected();
  8044. }
  8045. const refTrailingCommaPos = {
  8046. value: -1
  8047. };
  8048. node.params = this.tsParseBracketedList("TypeParametersOrArguments", this.tsParseTypeParameter.bind(this, parseModifiers), false, true, refTrailingCommaPos);
  8049. if (node.params.length === 0) {
  8050. this.raise(TSErrors.EmptyTypeParameters, node);
  8051. }
  8052. if (refTrailingCommaPos.value !== -1) {
  8053. this.addExtra(node, "trailingComma", refTrailingCommaPos.value);
  8054. }
  8055. return this.finishNode(node, "TSTypeParameterDeclaration");
  8056. }
  8057. tsFillSignature(returnToken, signature) {
  8058. const returnTokenRequired = returnToken === 19;
  8059. const paramsKey = "parameters";
  8060. const returnTypeKey = "typeAnnotation";
  8061. signature.typeParameters = this.tsTryParseTypeParameters(this.tsParseConstModifier);
  8062. this.expect(10);
  8063. signature[paramsKey] = this.tsParseBindingListForSignature();
  8064. if (returnTokenRequired) {
  8065. signature[returnTypeKey] = this.tsParseTypeOrTypePredicateAnnotation(returnToken);
  8066. } else if (this.match(returnToken)) {
  8067. signature[returnTypeKey] = this.tsParseTypeOrTypePredicateAnnotation(returnToken);
  8068. }
  8069. }
  8070. tsParseBindingListForSignature() {
  8071. const list = super.parseBindingList(11, 41, 2);
  8072. for (const pattern of list) {
  8073. const {
  8074. type
  8075. } = pattern;
  8076. if (type === "AssignmentPattern" || type === "TSParameterProperty") {
  8077. this.raise(TSErrors.UnsupportedSignatureParameterKind, pattern, {
  8078. type
  8079. });
  8080. }
  8081. }
  8082. return list;
  8083. }
  8084. tsParseTypeMemberSemicolon() {
  8085. if (!this.eat(12) && !this.isLineTerminator()) {
  8086. this.expect(13);
  8087. }
  8088. }
  8089. tsParseSignatureMember(kind, node) {
  8090. this.tsFillSignature(14, node);
  8091. this.tsParseTypeMemberSemicolon();
  8092. return this.finishNode(node, kind);
  8093. }
  8094. tsIsUnambiguouslyIndexSignature() {
  8095. this.next();
  8096. if (tokenIsIdentifier(this.state.type)) {
  8097. this.next();
  8098. return this.match(14);
  8099. }
  8100. return false;
  8101. }
  8102. tsTryParseIndexSignature(node) {
  8103. if (!(this.match(0) && this.tsLookAhead(this.tsIsUnambiguouslyIndexSignature.bind(this)))) {
  8104. return;
  8105. }
  8106. this.expect(0);
  8107. const id = this.parseIdentifier();
  8108. id.typeAnnotation = this.tsParseTypeAnnotation();
  8109. this.resetEndLocation(id);
  8110. this.expect(3);
  8111. node.parameters = [id];
  8112. const type = this.tsTryParseTypeAnnotation();
  8113. if (type) node.typeAnnotation = type;
  8114. this.tsParseTypeMemberSemicolon();
  8115. return this.finishNode(node, "TSIndexSignature");
  8116. }
  8117. tsParsePropertyOrMethodSignature(node, readonly) {
  8118. if (this.eat(17)) node.optional = true;
  8119. if (this.match(10) || this.match(47)) {
  8120. if (readonly) {
  8121. this.raise(TSErrors.ReadonlyForMethodSignature, node);
  8122. }
  8123. const method = node;
  8124. if (method.kind && this.match(47)) {
  8125. this.raise(TSErrors.AccessorCannotHaveTypeParameters, this.state.curPosition());
  8126. }
  8127. this.tsFillSignature(14, method);
  8128. this.tsParseTypeMemberSemicolon();
  8129. const paramsKey = "parameters";
  8130. const returnTypeKey = "typeAnnotation";
  8131. if (method.kind === "get") {
  8132. if (method[paramsKey].length > 0) {
  8133. this.raise(Errors.BadGetterArity, this.state.curPosition());
  8134. if (this.isThisParam(method[paramsKey][0])) {
  8135. this.raise(TSErrors.AccessorCannotDeclareThisParameter, this.state.curPosition());
  8136. }
  8137. }
  8138. } else if (method.kind === "set") {
  8139. if (method[paramsKey].length !== 1) {
  8140. this.raise(Errors.BadSetterArity, this.state.curPosition());
  8141. } else {
  8142. const firstParameter = method[paramsKey][0];
  8143. if (this.isThisParam(firstParameter)) {
  8144. this.raise(TSErrors.AccessorCannotDeclareThisParameter, this.state.curPosition());
  8145. }
  8146. if (firstParameter.type === "Identifier" && firstParameter.optional) {
  8147. this.raise(TSErrors.SetAccessorCannotHaveOptionalParameter, this.state.curPosition());
  8148. }
  8149. if (firstParameter.type === "RestElement") {
  8150. this.raise(TSErrors.SetAccessorCannotHaveRestParameter, this.state.curPosition());
  8151. }
  8152. }
  8153. if (method[returnTypeKey]) {
  8154. this.raise(TSErrors.SetAccessorCannotHaveReturnType, method[returnTypeKey]);
  8155. }
  8156. } else {
  8157. method.kind = "method";
  8158. }
  8159. return this.finishNode(method, "TSMethodSignature");
  8160. } else {
  8161. const property = node;
  8162. if (readonly) property.readonly = true;
  8163. const type = this.tsTryParseTypeAnnotation();
  8164. if (type) property.typeAnnotation = type;
  8165. this.tsParseTypeMemberSemicolon();
  8166. return this.finishNode(property, "TSPropertySignature");
  8167. }
  8168. }
  8169. tsParseTypeMember() {
  8170. const node = this.startNode();
  8171. if (this.match(10) || this.match(47)) {
  8172. return this.tsParseSignatureMember("TSCallSignatureDeclaration", node);
  8173. }
  8174. if (this.match(77)) {
  8175. const id = this.startNode();
  8176. this.next();
  8177. if (this.match(10) || this.match(47)) {
  8178. return this.tsParseSignatureMember("TSConstructSignatureDeclaration", node);
  8179. } else {
  8180. node.key = this.createIdentifier(id, "new");
  8181. return this.tsParsePropertyOrMethodSignature(node, false);
  8182. }
  8183. }
  8184. this.tsParseModifiers({
  8185. allowedModifiers: ["readonly"],
  8186. disallowedModifiers: ["declare", "abstract", "private", "protected", "public", "static", "override"]
  8187. }, node);
  8188. const idx = this.tsTryParseIndexSignature(node);
  8189. if (idx) {
  8190. return idx;
  8191. }
  8192. super.parsePropertyName(node);
  8193. if (!node.computed && node.key.type === "Identifier" && (node.key.name === "get" || node.key.name === "set") && this.tsTokenCanFollowModifier()) {
  8194. node.kind = node.key.name;
  8195. super.parsePropertyName(node);
  8196. if (!this.match(10) && !this.match(47)) {
  8197. this.unexpected(null, 10);
  8198. }
  8199. }
  8200. return this.tsParsePropertyOrMethodSignature(node, !!node.readonly);
  8201. }
  8202. tsParseTypeLiteral() {
  8203. const node = this.startNode();
  8204. node.members = this.tsParseObjectTypeMembers();
  8205. return this.finishNode(node, "TSTypeLiteral");
  8206. }
  8207. tsParseObjectTypeMembers() {
  8208. this.expect(5);
  8209. const members = this.tsParseList("TypeMembers", this.tsParseTypeMember.bind(this));
  8210. this.expect(8);
  8211. return members;
  8212. }
  8213. tsIsStartOfMappedType() {
  8214. this.next();
  8215. if (this.eat(53)) {
  8216. return this.isContextual(122);
  8217. }
  8218. if (this.isContextual(122)) {
  8219. this.next();
  8220. }
  8221. if (!this.match(0)) {
  8222. return false;
  8223. }
  8224. this.next();
  8225. if (!this.tsIsIdentifier()) {
  8226. return false;
  8227. }
  8228. this.next();
  8229. return this.match(58);
  8230. }
  8231. tsParseMappedType() {
  8232. const node = this.startNode();
  8233. this.expect(5);
  8234. if (this.match(53)) {
  8235. node.readonly = this.state.value;
  8236. this.next();
  8237. this.expectContextual(122);
  8238. } else if (this.eatContextual(122)) {
  8239. node.readonly = true;
  8240. }
  8241. this.expect(0);
  8242. {
  8243. const typeParameter = this.startNode();
  8244. typeParameter.name = this.tsParseTypeParameterName();
  8245. typeParameter.constraint = this.tsExpectThenParseType(58);
  8246. node.typeParameter = this.finishNode(typeParameter, "TSTypeParameter");
  8247. }
  8248. node.nameType = this.eatContextual(93) ? this.tsParseType() : null;
  8249. this.expect(3);
  8250. if (this.match(53)) {
  8251. node.optional = this.state.value;
  8252. this.next();
  8253. this.expect(17);
  8254. } else if (this.eat(17)) {
  8255. node.optional = true;
  8256. }
  8257. node.typeAnnotation = this.tsTryParseType();
  8258. this.semicolon();
  8259. this.expect(8);
  8260. return this.finishNode(node, "TSMappedType");
  8261. }
  8262. tsParseTupleType() {
  8263. const node = this.startNode();
  8264. node.elementTypes = this.tsParseBracketedList("TupleElementTypes", this.tsParseTupleElementType.bind(this), true, false);
  8265. let seenOptionalElement = false;
  8266. node.elementTypes.forEach(elementNode => {
  8267. const {
  8268. type
  8269. } = elementNode;
  8270. if (seenOptionalElement && type !== "TSRestType" && type !== "TSOptionalType" && !(type === "TSNamedTupleMember" && elementNode.optional)) {
  8271. this.raise(TSErrors.OptionalTypeBeforeRequired, elementNode);
  8272. }
  8273. seenOptionalElement || (seenOptionalElement = type === "TSNamedTupleMember" && elementNode.optional || type === "TSOptionalType");
  8274. });
  8275. return this.finishNode(node, "TSTupleType");
  8276. }
  8277. tsParseTupleElementType() {
  8278. const restStartLoc = this.state.startLoc;
  8279. const rest = this.eat(21);
  8280. const {
  8281. startLoc
  8282. } = this.state;
  8283. let labeled;
  8284. let label;
  8285. let optional;
  8286. let type;
  8287. const isWord = tokenIsKeywordOrIdentifier(this.state.type);
  8288. const chAfterWord = isWord ? this.lookaheadCharCode() : null;
  8289. if (chAfterWord === 58) {
  8290. labeled = true;
  8291. optional = false;
  8292. label = this.parseIdentifier(true);
  8293. this.expect(14);
  8294. type = this.tsParseType();
  8295. } else if (chAfterWord === 63) {
  8296. optional = true;
  8297. const wordName = this.state.value;
  8298. const typeOrLabel = this.tsParseNonArrayType();
  8299. if (this.lookaheadCharCode() === 58) {
  8300. labeled = true;
  8301. label = this.createIdentifier(this.startNodeAt(startLoc), wordName);
  8302. this.expect(17);
  8303. this.expect(14);
  8304. type = this.tsParseType();
  8305. } else {
  8306. labeled = false;
  8307. type = typeOrLabel;
  8308. this.expect(17);
  8309. }
  8310. } else {
  8311. type = this.tsParseType();
  8312. optional = this.eat(17);
  8313. labeled = this.eat(14);
  8314. }
  8315. if (labeled) {
  8316. let labeledNode;
  8317. if (label) {
  8318. labeledNode = this.startNodeAt(startLoc);
  8319. labeledNode.optional = optional;
  8320. labeledNode.label = label;
  8321. labeledNode.elementType = type;
  8322. if (this.eat(17)) {
  8323. labeledNode.optional = true;
  8324. this.raise(TSErrors.TupleOptionalAfterType, this.state.lastTokStartLoc);
  8325. }
  8326. } else {
  8327. labeledNode = this.startNodeAt(startLoc);
  8328. labeledNode.optional = optional;
  8329. this.raise(TSErrors.InvalidTupleMemberLabel, type);
  8330. labeledNode.label = type;
  8331. labeledNode.elementType = this.tsParseType();
  8332. }
  8333. type = this.finishNode(labeledNode, "TSNamedTupleMember");
  8334. } else if (optional) {
  8335. const optionalTypeNode = this.startNodeAt(startLoc);
  8336. optionalTypeNode.typeAnnotation = type;
  8337. type = this.finishNode(optionalTypeNode, "TSOptionalType");
  8338. }
  8339. if (rest) {
  8340. const restNode = this.startNodeAt(restStartLoc);
  8341. restNode.typeAnnotation = type;
  8342. type = this.finishNode(restNode, "TSRestType");
  8343. }
  8344. return type;
  8345. }
  8346. tsParseParenthesizedType() {
  8347. const node = this.startNode();
  8348. this.expect(10);
  8349. node.typeAnnotation = this.tsParseType();
  8350. this.expect(11);
  8351. return this.finishNode(node, "TSParenthesizedType");
  8352. }
  8353. tsParseFunctionOrConstructorType(type, abstract) {
  8354. const node = this.startNode();
  8355. if (type === "TSConstructorType") {
  8356. node.abstract = !!abstract;
  8357. if (abstract) this.next();
  8358. this.next();
  8359. }
  8360. this.tsInAllowConditionalTypesContext(() => this.tsFillSignature(19, node));
  8361. return this.finishNode(node, type);
  8362. }
  8363. tsParseLiteralTypeNode() {
  8364. const node = this.startNode();
  8365. switch (this.state.type) {
  8366. case 135:
  8367. case 136:
  8368. case 134:
  8369. case 85:
  8370. case 86:
  8371. node.literal = super.parseExprAtom();
  8372. break;
  8373. default:
  8374. this.unexpected();
  8375. }
  8376. return this.finishNode(node, "TSLiteralType");
  8377. }
  8378. tsParseTemplateLiteralType() {
  8379. {
  8380. const node = this.startNode();
  8381. node.literal = super.parseTemplate(false);
  8382. return this.finishNode(node, "TSLiteralType");
  8383. }
  8384. }
  8385. parseTemplateSubstitution() {
  8386. if (this.state.inType) return this.tsParseType();
  8387. return super.parseTemplateSubstitution();
  8388. }
  8389. tsParseThisTypeOrThisTypePredicate() {
  8390. const thisKeyword = this.tsParseThisTypeNode();
  8391. if (this.isContextual(116) && !this.hasPrecedingLineBreak()) {
  8392. return this.tsParseThisTypePredicate(thisKeyword);
  8393. } else {
  8394. return thisKeyword;
  8395. }
  8396. }
  8397. tsParseNonArrayType() {
  8398. switch (this.state.type) {
  8399. case 134:
  8400. case 135:
  8401. case 136:
  8402. case 85:
  8403. case 86:
  8404. return this.tsParseLiteralTypeNode();
  8405. case 53:
  8406. if (this.state.value === "-") {
  8407. const node = this.startNode();
  8408. const nextToken = this.lookahead();
  8409. if (nextToken.type !== 135 && nextToken.type !== 136) {
  8410. this.unexpected();
  8411. }
  8412. node.literal = this.parseMaybeUnary();
  8413. return this.finishNode(node, "TSLiteralType");
  8414. }
  8415. break;
  8416. case 78:
  8417. return this.tsParseThisTypeOrThisTypePredicate();
  8418. case 87:
  8419. return this.tsParseTypeQuery();
  8420. case 83:
  8421. return this.tsParseImportType();
  8422. case 5:
  8423. return this.tsLookAhead(this.tsIsStartOfMappedType.bind(this)) ? this.tsParseMappedType() : this.tsParseTypeLiteral();
  8424. case 0:
  8425. return this.tsParseTupleType();
  8426. case 10:
  8427. return this.tsParseParenthesizedType();
  8428. case 25:
  8429. case 24:
  8430. return this.tsParseTemplateLiteralType();
  8431. default:
  8432. {
  8433. const {
  8434. type
  8435. } = this.state;
  8436. if (tokenIsIdentifier(type) || type === 88 || type === 84) {
  8437. const nodeType = type === 88 ? "TSVoidKeyword" : type === 84 ? "TSNullKeyword" : keywordTypeFromName(this.state.value);
  8438. if (nodeType !== undefined && this.lookaheadCharCode() !== 46) {
  8439. const node = this.startNode();
  8440. this.next();
  8441. return this.finishNode(node, nodeType);
  8442. }
  8443. return this.tsParseTypeReference();
  8444. }
  8445. }
  8446. }
  8447. this.unexpected();
  8448. }
  8449. tsParseArrayTypeOrHigher() {
  8450. const {
  8451. startLoc
  8452. } = this.state;
  8453. let type = this.tsParseNonArrayType();
  8454. while (!this.hasPrecedingLineBreak() && this.eat(0)) {
  8455. if (this.match(3)) {
  8456. const node = this.startNodeAt(startLoc);
  8457. node.elementType = type;
  8458. this.expect(3);
  8459. type = this.finishNode(node, "TSArrayType");
  8460. } else {
  8461. const node = this.startNodeAt(startLoc);
  8462. node.objectType = type;
  8463. node.indexType = this.tsParseType();
  8464. this.expect(3);
  8465. type = this.finishNode(node, "TSIndexedAccessType");
  8466. }
  8467. }
  8468. return type;
  8469. }
  8470. tsParseTypeOperator() {
  8471. const node = this.startNode();
  8472. const operator = this.state.value;
  8473. this.next();
  8474. node.operator = operator;
  8475. node.typeAnnotation = this.tsParseTypeOperatorOrHigher();
  8476. if (operator === "readonly") {
  8477. this.tsCheckTypeAnnotationForReadOnly(node);
  8478. }
  8479. return this.finishNode(node, "TSTypeOperator");
  8480. }
  8481. tsCheckTypeAnnotationForReadOnly(node) {
  8482. switch (node.typeAnnotation.type) {
  8483. case "TSTupleType":
  8484. case "TSArrayType":
  8485. return;
  8486. default:
  8487. this.raise(TSErrors.UnexpectedReadonly, node);
  8488. }
  8489. }
  8490. tsParseInferType() {
  8491. const node = this.startNode();
  8492. this.expectContextual(115);
  8493. const typeParameter = this.startNode();
  8494. typeParameter.name = this.tsParseTypeParameterName();
  8495. typeParameter.constraint = this.tsTryParse(() => this.tsParseConstraintForInferType());
  8496. node.typeParameter = this.finishNode(typeParameter, "TSTypeParameter");
  8497. return this.finishNode(node, "TSInferType");
  8498. }
  8499. tsParseConstraintForInferType() {
  8500. if (this.eat(81)) {
  8501. const constraint = this.tsInDisallowConditionalTypesContext(() => this.tsParseType());
  8502. if (this.state.inDisallowConditionalTypesContext || !this.match(17)) {
  8503. return constraint;
  8504. }
  8505. }
  8506. }
  8507. tsParseTypeOperatorOrHigher() {
  8508. const isTypeOperator = tokenIsTSTypeOperator(this.state.type) && !this.state.containsEsc;
  8509. return isTypeOperator ? this.tsParseTypeOperator() : this.isContextual(115) ? this.tsParseInferType() : this.tsInAllowConditionalTypesContext(() => this.tsParseArrayTypeOrHigher());
  8510. }
  8511. tsParseUnionOrIntersectionType(kind, parseConstituentType, operator) {
  8512. const node = this.startNode();
  8513. const hasLeadingOperator = this.eat(operator);
  8514. const types = [];
  8515. do {
  8516. types.push(parseConstituentType());
  8517. } while (this.eat(operator));
  8518. if (types.length === 1 && !hasLeadingOperator) {
  8519. return types[0];
  8520. }
  8521. node.types = types;
  8522. return this.finishNode(node, kind);
  8523. }
  8524. tsParseIntersectionTypeOrHigher() {
  8525. return this.tsParseUnionOrIntersectionType("TSIntersectionType", this.tsParseTypeOperatorOrHigher.bind(this), 45);
  8526. }
  8527. tsParseUnionTypeOrHigher() {
  8528. return this.tsParseUnionOrIntersectionType("TSUnionType", this.tsParseIntersectionTypeOrHigher.bind(this), 43);
  8529. }
  8530. tsIsStartOfFunctionType() {
  8531. if (this.match(47)) {
  8532. return true;
  8533. }
  8534. return this.match(10) && this.tsLookAhead(this.tsIsUnambiguouslyStartOfFunctionType.bind(this));
  8535. }
  8536. tsSkipParameterStart() {
  8537. if (tokenIsIdentifier(this.state.type) || this.match(78)) {
  8538. this.next();
  8539. return true;
  8540. }
  8541. if (this.match(5)) {
  8542. const {
  8543. errors
  8544. } = this.state;
  8545. const previousErrorCount = errors.length;
  8546. try {
  8547. this.parseObjectLike(8, true);
  8548. return errors.length === previousErrorCount;
  8549. } catch (_unused) {
  8550. return false;
  8551. }
  8552. }
  8553. if (this.match(0)) {
  8554. this.next();
  8555. const {
  8556. errors
  8557. } = this.state;
  8558. const previousErrorCount = errors.length;
  8559. try {
  8560. super.parseBindingList(3, 93, 1);
  8561. return errors.length === previousErrorCount;
  8562. } catch (_unused2) {
  8563. return false;
  8564. }
  8565. }
  8566. return false;
  8567. }
  8568. tsIsUnambiguouslyStartOfFunctionType() {
  8569. this.next();
  8570. if (this.match(11) || this.match(21)) {
  8571. return true;
  8572. }
  8573. if (this.tsSkipParameterStart()) {
  8574. if (this.match(14) || this.match(12) || this.match(17) || this.match(29)) {
  8575. return true;
  8576. }
  8577. if (this.match(11)) {
  8578. this.next();
  8579. if (this.match(19)) {
  8580. return true;
  8581. }
  8582. }
  8583. }
  8584. return false;
  8585. }
  8586. tsParseTypeOrTypePredicateAnnotation(returnToken) {
  8587. return this.tsInType(() => {
  8588. const t = this.startNode();
  8589. this.expect(returnToken);
  8590. const node = this.startNode();
  8591. const asserts = !!this.tsTryParse(this.tsParseTypePredicateAsserts.bind(this));
  8592. if (asserts && this.match(78)) {
  8593. let thisTypePredicate = this.tsParseThisTypeOrThisTypePredicate();
  8594. if (thisTypePredicate.type === "TSThisType") {
  8595. node.parameterName = thisTypePredicate;
  8596. node.asserts = true;
  8597. node.typeAnnotation = null;
  8598. thisTypePredicate = this.finishNode(node, "TSTypePredicate");
  8599. } else {
  8600. this.resetStartLocationFromNode(thisTypePredicate, node);
  8601. thisTypePredicate.asserts = true;
  8602. }
  8603. t.typeAnnotation = thisTypePredicate;
  8604. return this.finishNode(t, "TSTypeAnnotation");
  8605. }
  8606. const typePredicateVariable = this.tsIsIdentifier() && this.tsTryParse(this.tsParseTypePredicatePrefix.bind(this));
  8607. if (!typePredicateVariable) {
  8608. if (!asserts) {
  8609. return this.tsParseTypeAnnotation(false, t);
  8610. }
  8611. node.parameterName = this.parseIdentifier();
  8612. node.asserts = asserts;
  8613. node.typeAnnotation = null;
  8614. t.typeAnnotation = this.finishNode(node, "TSTypePredicate");
  8615. return this.finishNode(t, "TSTypeAnnotation");
  8616. }
  8617. const type = this.tsParseTypeAnnotation(false);
  8618. node.parameterName = typePredicateVariable;
  8619. node.typeAnnotation = type;
  8620. node.asserts = asserts;
  8621. t.typeAnnotation = this.finishNode(node, "TSTypePredicate");
  8622. return this.finishNode(t, "TSTypeAnnotation");
  8623. });
  8624. }
  8625. tsTryParseTypeOrTypePredicateAnnotation() {
  8626. if (this.match(14)) {
  8627. return this.tsParseTypeOrTypePredicateAnnotation(14);
  8628. }
  8629. }
  8630. tsTryParseTypeAnnotation() {
  8631. if (this.match(14)) {
  8632. return this.tsParseTypeAnnotation();
  8633. }
  8634. }
  8635. tsTryParseType() {
  8636. return this.tsEatThenParseType(14);
  8637. }
  8638. tsParseTypePredicatePrefix() {
  8639. const id = this.parseIdentifier();
  8640. if (this.isContextual(116) && !this.hasPrecedingLineBreak()) {
  8641. this.next();
  8642. return id;
  8643. }
  8644. }
  8645. tsParseTypePredicateAsserts() {
  8646. if (this.state.type !== 109) {
  8647. return false;
  8648. }
  8649. const containsEsc = this.state.containsEsc;
  8650. this.next();
  8651. if (!tokenIsIdentifier(this.state.type) && !this.match(78)) {
  8652. return false;
  8653. }
  8654. if (containsEsc) {
  8655. this.raise(Errors.InvalidEscapedReservedWord, this.state.lastTokStartLoc, {
  8656. reservedWord: "asserts"
  8657. });
  8658. }
  8659. return true;
  8660. }
  8661. tsParseTypeAnnotation(eatColon = true, t = this.startNode()) {
  8662. this.tsInType(() => {
  8663. if (eatColon) this.expect(14);
  8664. t.typeAnnotation = this.tsParseType();
  8665. });
  8666. return this.finishNode(t, "TSTypeAnnotation");
  8667. }
  8668. tsParseType() {
  8669. assert(this.state.inType);
  8670. const type = this.tsParseNonConditionalType();
  8671. if (this.state.inDisallowConditionalTypesContext || this.hasPrecedingLineBreak() || !this.eat(81)) {
  8672. return type;
  8673. }
  8674. const node = this.startNodeAtNode(type);
  8675. node.checkType = type;
  8676. node.extendsType = this.tsInDisallowConditionalTypesContext(() => this.tsParseNonConditionalType());
  8677. this.expect(17);
  8678. node.trueType = this.tsInAllowConditionalTypesContext(() => this.tsParseType());
  8679. this.expect(14);
  8680. node.falseType = this.tsInAllowConditionalTypesContext(() => this.tsParseType());
  8681. return this.finishNode(node, "TSConditionalType");
  8682. }
  8683. isAbstractConstructorSignature() {
  8684. return this.isContextual(124) && this.lookahead().type === 77;
  8685. }
  8686. tsParseNonConditionalType() {
  8687. if (this.tsIsStartOfFunctionType()) {
  8688. return this.tsParseFunctionOrConstructorType("TSFunctionType");
  8689. }
  8690. if (this.match(77)) {
  8691. return this.tsParseFunctionOrConstructorType("TSConstructorType");
  8692. } else if (this.isAbstractConstructorSignature()) {
  8693. return this.tsParseFunctionOrConstructorType("TSConstructorType", true);
  8694. }
  8695. return this.tsParseUnionTypeOrHigher();
  8696. }
  8697. tsParseTypeAssertion() {
  8698. if (this.getPluginOption("typescript", "disallowAmbiguousJSXLike")) {
  8699. this.raise(TSErrors.ReservedTypeAssertion, this.state.startLoc);
  8700. }
  8701. const node = this.startNode();
  8702. node.typeAnnotation = this.tsInType(() => {
  8703. this.next();
  8704. return this.match(75) ? this.tsParseTypeReference() : this.tsParseType();
  8705. });
  8706. this.expect(48);
  8707. node.expression = this.parseMaybeUnary();
  8708. return this.finishNode(node, "TSTypeAssertion");
  8709. }
  8710. tsParseHeritageClause(token) {
  8711. const originalStartLoc = this.state.startLoc;
  8712. const delimitedList = this.tsParseDelimitedList("HeritageClauseElement", () => {
  8713. {
  8714. const node = this.startNode();
  8715. node.expression = this.tsParseEntityName(1 | 2);
  8716. if (this.match(47)) {
  8717. node.typeParameters = this.tsParseTypeArguments();
  8718. }
  8719. return this.finishNode(node, "TSExpressionWithTypeArguments");
  8720. }
  8721. });
  8722. if (!delimitedList.length) {
  8723. this.raise(TSErrors.EmptyHeritageClauseType, originalStartLoc, {
  8724. token
  8725. });
  8726. }
  8727. return delimitedList;
  8728. }
  8729. tsParseInterfaceDeclaration(node, properties = {}) {
  8730. if (this.hasFollowingLineBreak()) return null;
  8731. this.expectContextual(129);
  8732. if (properties.declare) node.declare = true;
  8733. if (tokenIsIdentifier(this.state.type)) {
  8734. node.id = this.parseIdentifier();
  8735. this.checkIdentifier(node.id, 130);
  8736. } else {
  8737. node.id = null;
  8738. this.raise(TSErrors.MissingInterfaceName, this.state.startLoc);
  8739. }
  8740. node.typeParameters = this.tsTryParseTypeParameters(this.tsParseInOutConstModifiers);
  8741. if (this.eat(81)) {
  8742. node.extends = this.tsParseHeritageClause("extends");
  8743. }
  8744. const body = this.startNode();
  8745. body.body = this.tsInType(this.tsParseObjectTypeMembers.bind(this));
  8746. node.body = this.finishNode(body, "TSInterfaceBody");
  8747. return this.finishNode(node, "TSInterfaceDeclaration");
  8748. }
  8749. tsParseTypeAliasDeclaration(node) {
  8750. node.id = this.parseIdentifier();
  8751. this.checkIdentifier(node.id, 2);
  8752. node.typeAnnotation = this.tsInType(() => {
  8753. node.typeParameters = this.tsTryParseTypeParameters(this.tsParseInOutModifiers);
  8754. this.expect(29);
  8755. if (this.isContextual(114) && this.lookahead().type !== 16) {
  8756. const node = this.startNode();
  8757. this.next();
  8758. return this.finishNode(node, "TSIntrinsicKeyword");
  8759. }
  8760. return this.tsParseType();
  8761. });
  8762. this.semicolon();
  8763. return this.finishNode(node, "TSTypeAliasDeclaration");
  8764. }
  8765. tsInTopLevelContext(cb) {
  8766. if (this.curContext() !== types.brace) {
  8767. const oldContext = this.state.context;
  8768. this.state.context = [oldContext[0]];
  8769. try {
  8770. return cb();
  8771. } finally {
  8772. this.state.context = oldContext;
  8773. }
  8774. } else {
  8775. return cb();
  8776. }
  8777. }
  8778. tsInType(cb) {
  8779. const oldInType = this.state.inType;
  8780. this.state.inType = true;
  8781. try {
  8782. return cb();
  8783. } finally {
  8784. this.state.inType = oldInType;
  8785. }
  8786. }
  8787. tsInDisallowConditionalTypesContext(cb) {
  8788. const oldInDisallowConditionalTypesContext = this.state.inDisallowConditionalTypesContext;
  8789. this.state.inDisallowConditionalTypesContext = true;
  8790. try {
  8791. return cb();
  8792. } finally {
  8793. this.state.inDisallowConditionalTypesContext = oldInDisallowConditionalTypesContext;
  8794. }
  8795. }
  8796. tsInAllowConditionalTypesContext(cb) {
  8797. const oldInDisallowConditionalTypesContext = this.state.inDisallowConditionalTypesContext;
  8798. this.state.inDisallowConditionalTypesContext = false;
  8799. try {
  8800. return cb();
  8801. } finally {
  8802. this.state.inDisallowConditionalTypesContext = oldInDisallowConditionalTypesContext;
  8803. }
  8804. }
  8805. tsEatThenParseType(token) {
  8806. if (this.match(token)) {
  8807. return this.tsNextThenParseType();
  8808. }
  8809. }
  8810. tsExpectThenParseType(token) {
  8811. return this.tsInType(() => {
  8812. this.expect(token);
  8813. return this.tsParseType();
  8814. });
  8815. }
  8816. tsNextThenParseType() {
  8817. return this.tsInType(() => {
  8818. this.next();
  8819. return this.tsParseType();
  8820. });
  8821. }
  8822. tsParseEnumMember() {
  8823. const node = this.startNode();
  8824. node.id = this.match(134) ? super.parseStringLiteral(this.state.value) : this.parseIdentifier(true);
  8825. if (this.eat(29)) {
  8826. node.initializer = super.parseMaybeAssignAllowIn();
  8827. }
  8828. return this.finishNode(node, "TSEnumMember");
  8829. }
  8830. tsParseEnumDeclaration(node, properties = {}) {
  8831. if (properties.const) node.const = true;
  8832. if (properties.declare) node.declare = true;
  8833. this.expectContextual(126);
  8834. node.id = this.parseIdentifier();
  8835. this.checkIdentifier(node.id, node.const ? 8971 : 8459);
  8836. {
  8837. this.expect(5);
  8838. node.members = this.tsParseDelimitedList("EnumMembers", this.tsParseEnumMember.bind(this));
  8839. this.expect(8);
  8840. }
  8841. return this.finishNode(node, "TSEnumDeclaration");
  8842. }
  8843. tsParseEnumBody() {
  8844. const node = this.startNode();
  8845. this.expect(5);
  8846. node.members = this.tsParseDelimitedList("EnumMembers", this.tsParseEnumMember.bind(this));
  8847. this.expect(8);
  8848. return this.finishNode(node, "TSEnumBody");
  8849. }
  8850. tsParseModuleBlock() {
  8851. const node = this.startNode();
  8852. this.scope.enter(0);
  8853. this.expect(5);
  8854. super.parseBlockOrModuleBlockBody(node.body = [], undefined, true, 8);
  8855. this.scope.exit();
  8856. return this.finishNode(node, "TSModuleBlock");
  8857. }
  8858. tsParseModuleOrNamespaceDeclaration(node, nested = false) {
  8859. node.id = this.parseIdentifier();
  8860. if (!nested) {
  8861. this.checkIdentifier(node.id, 1024);
  8862. }
  8863. if (this.eat(16)) {
  8864. const inner = this.startNode();
  8865. this.tsParseModuleOrNamespaceDeclaration(inner, true);
  8866. node.body = inner;
  8867. } else {
  8868. this.scope.enter(512);
  8869. this.prodParam.enter(0);
  8870. node.body = this.tsParseModuleBlock();
  8871. this.prodParam.exit();
  8872. this.scope.exit();
  8873. }
  8874. return this.finishNode(node, "TSModuleDeclaration");
  8875. }
  8876. tsParseAmbientExternalModuleDeclaration(node) {
  8877. if (this.isContextual(112)) {
  8878. node.kind = "global";
  8879. {
  8880. node.global = true;
  8881. }
  8882. node.id = this.parseIdentifier();
  8883. } else if (this.match(134)) {
  8884. node.kind = "module";
  8885. node.id = super.parseStringLiteral(this.state.value);
  8886. } else {
  8887. this.unexpected();
  8888. }
  8889. if (this.match(5)) {
  8890. this.scope.enter(512);
  8891. this.prodParam.enter(0);
  8892. node.body = this.tsParseModuleBlock();
  8893. this.prodParam.exit();
  8894. this.scope.exit();
  8895. } else {
  8896. this.semicolon();
  8897. }
  8898. return this.finishNode(node, "TSModuleDeclaration");
  8899. }
  8900. tsParseImportEqualsDeclaration(node, maybeDefaultIdentifier, isExport) {
  8901. {
  8902. node.isExport = isExport || false;
  8903. }
  8904. node.id = maybeDefaultIdentifier || this.parseIdentifier();
  8905. this.checkIdentifier(node.id, 4096);
  8906. this.expect(29);
  8907. const moduleReference = this.tsParseModuleReference();
  8908. if (node.importKind === "type" && moduleReference.type !== "TSExternalModuleReference") {
  8909. this.raise(TSErrors.ImportAliasHasImportType, moduleReference);
  8910. }
  8911. node.moduleReference = moduleReference;
  8912. this.semicolon();
  8913. return this.finishNode(node, "TSImportEqualsDeclaration");
  8914. }
  8915. tsIsExternalModuleReference() {
  8916. return this.isContextual(119) && this.lookaheadCharCode() === 40;
  8917. }
  8918. tsParseModuleReference() {
  8919. return this.tsIsExternalModuleReference() ? this.tsParseExternalModuleReference() : this.tsParseEntityName(0);
  8920. }
  8921. tsParseExternalModuleReference() {
  8922. const node = this.startNode();
  8923. this.expectContextual(119);
  8924. this.expect(10);
  8925. if (!this.match(134)) {
  8926. this.unexpected();
  8927. }
  8928. node.expression = super.parseExprAtom();
  8929. this.expect(11);
  8930. this.sawUnambiguousESM = true;
  8931. return this.finishNode(node, "TSExternalModuleReference");
  8932. }
  8933. tsLookAhead(f) {
  8934. const state = this.state.clone();
  8935. const res = f();
  8936. this.state = state;
  8937. return res;
  8938. }
  8939. tsTryParseAndCatch(f) {
  8940. const result = this.tryParse(abort => f() || abort());
  8941. if (result.aborted || !result.node) return;
  8942. if (result.error) this.state = result.failState;
  8943. return result.node;
  8944. }
  8945. tsTryParse(f) {
  8946. const state = this.state.clone();
  8947. const result = f();
  8948. if (result !== undefined && result !== false) {
  8949. return result;
  8950. }
  8951. this.state = state;
  8952. }
  8953. tsTryParseDeclare(node) {
  8954. if (this.isLineTerminator()) {
  8955. return;
  8956. }
  8957. const startType = this.state.type;
  8958. return this.tsInAmbientContext(() => {
  8959. switch (startType) {
  8960. case 68:
  8961. node.declare = true;
  8962. return super.parseFunctionStatement(node, false, false);
  8963. case 80:
  8964. node.declare = true;
  8965. return this.parseClass(node, true, false);
  8966. case 126:
  8967. return this.tsParseEnumDeclaration(node, {
  8968. declare: true
  8969. });
  8970. case 112:
  8971. return this.tsParseAmbientExternalModuleDeclaration(node);
  8972. case 100:
  8973. if (this.state.containsEsc) {
  8974. return;
  8975. }
  8976. case 75:
  8977. case 74:
  8978. if (!this.match(75) || !this.isLookaheadContextual("enum")) {
  8979. node.declare = true;
  8980. return this.parseVarStatement(node, this.state.value, true);
  8981. }
  8982. this.expect(75);
  8983. return this.tsParseEnumDeclaration(node, {
  8984. const: true,
  8985. declare: true
  8986. });
  8987. case 107:
  8988. if (this.hasPlugin("explicitResourceManagement") && this.isUsing()) {
  8989. this.raise(TSErrors.InvalidModifierOnUsingDeclaration, this.state.startLoc, "declare");
  8990. node.declare = true;
  8991. return this.parseVarStatement(node, "using", true);
  8992. }
  8993. break;
  8994. case 96:
  8995. if (this.hasPlugin("explicitResourceManagement") && this.isAwaitUsing()) {
  8996. this.raise(TSErrors.InvalidModifierOnAwaitUsingDeclaration, this.state.startLoc, "declare");
  8997. node.declare = true;
  8998. this.next();
  8999. return this.parseVarStatement(node, "await using", true);
  9000. }
  9001. break;
  9002. case 129:
  9003. {
  9004. const result = this.tsParseInterfaceDeclaration(node, {
  9005. declare: true
  9006. });
  9007. if (result) return result;
  9008. }
  9009. default:
  9010. if (tokenIsIdentifier(startType)) {
  9011. return this.tsParseDeclaration(node, this.state.value, true, null);
  9012. }
  9013. }
  9014. });
  9015. }
  9016. tsTryParseExportDeclaration() {
  9017. return this.tsParseDeclaration(this.startNode(), this.state.value, true, null);
  9018. }
  9019. tsParseExpressionStatement(node, expr, decorators) {
  9020. switch (expr.name) {
  9021. case "declare":
  9022. {
  9023. const declaration = this.tsTryParseDeclare(node);
  9024. if (declaration) {
  9025. declaration.declare = true;
  9026. }
  9027. return declaration;
  9028. }
  9029. case "global":
  9030. if (this.match(5)) {
  9031. this.scope.enter(512);
  9032. this.prodParam.enter(0);
  9033. const mod = node;
  9034. mod.kind = "global";
  9035. {
  9036. node.global = true;
  9037. }
  9038. mod.id = expr;
  9039. mod.body = this.tsParseModuleBlock();
  9040. this.scope.exit();
  9041. this.prodParam.exit();
  9042. return this.finishNode(mod, "TSModuleDeclaration");
  9043. }
  9044. break;
  9045. default:
  9046. return this.tsParseDeclaration(node, expr.name, false, decorators);
  9047. }
  9048. }
  9049. tsParseDeclaration(node, value, next, decorators) {
  9050. switch (value) {
  9051. case "abstract":
  9052. if (this.tsCheckLineTerminator(next) && (this.match(80) || tokenIsIdentifier(this.state.type))) {
  9053. return this.tsParseAbstractDeclaration(node, decorators);
  9054. }
  9055. break;
  9056. case "module":
  9057. if (this.tsCheckLineTerminator(next)) {
  9058. if (this.match(134)) {
  9059. return this.tsParseAmbientExternalModuleDeclaration(node);
  9060. } else if (tokenIsIdentifier(this.state.type)) {
  9061. node.kind = "module";
  9062. return this.tsParseModuleOrNamespaceDeclaration(node);
  9063. }
  9064. }
  9065. break;
  9066. case "namespace":
  9067. if (this.tsCheckLineTerminator(next) && tokenIsIdentifier(this.state.type)) {
  9068. node.kind = "namespace";
  9069. return this.tsParseModuleOrNamespaceDeclaration(node);
  9070. }
  9071. break;
  9072. case "type":
  9073. if (this.tsCheckLineTerminator(next) && tokenIsIdentifier(this.state.type)) {
  9074. return this.tsParseTypeAliasDeclaration(node);
  9075. }
  9076. break;
  9077. }
  9078. }
  9079. tsCheckLineTerminator(next) {
  9080. if (next) {
  9081. if (this.hasFollowingLineBreak()) return false;
  9082. this.next();
  9083. return true;
  9084. }
  9085. return !this.isLineTerminator();
  9086. }
  9087. tsTryParseGenericAsyncArrowFunction(startLoc) {
  9088. if (!this.match(47)) return;
  9089. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  9090. this.state.maybeInArrowParameters = true;
  9091. const res = this.tsTryParseAndCatch(() => {
  9092. const node = this.startNodeAt(startLoc);
  9093. node.typeParameters = this.tsParseTypeParameters(this.tsParseConstModifier);
  9094. super.parseFunctionParams(node);
  9095. node.returnType = this.tsTryParseTypeOrTypePredicateAnnotation();
  9096. this.expect(19);
  9097. return node;
  9098. });
  9099. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  9100. if (!res) return;
  9101. return super.parseArrowExpression(res, null, true);
  9102. }
  9103. tsParseTypeArgumentsInExpression() {
  9104. if (this.reScan_lt() !== 47) return;
  9105. return this.tsParseTypeArguments();
  9106. }
  9107. tsParseTypeArguments() {
  9108. const node = this.startNode();
  9109. node.params = this.tsInType(() => this.tsInTopLevelContext(() => {
  9110. this.expect(47);
  9111. return this.tsParseDelimitedList("TypeParametersOrArguments", this.tsParseType.bind(this));
  9112. }));
  9113. if (node.params.length === 0) {
  9114. this.raise(TSErrors.EmptyTypeArguments, node);
  9115. } else if (!this.state.inType && this.curContext() === types.brace) {
  9116. this.reScan_lt_gt();
  9117. }
  9118. this.expect(48);
  9119. return this.finishNode(node, "TSTypeParameterInstantiation");
  9120. }
  9121. tsIsDeclarationStart() {
  9122. return tokenIsTSDeclarationStart(this.state.type);
  9123. }
  9124. isExportDefaultSpecifier() {
  9125. if (this.tsIsDeclarationStart()) return false;
  9126. return super.isExportDefaultSpecifier();
  9127. }
  9128. parseBindingElement(flags, decorators) {
  9129. const startLoc = decorators.length ? decorators[0].loc.start : this.state.startLoc;
  9130. const modified = {};
  9131. this.tsParseModifiers({
  9132. allowedModifiers: ["public", "private", "protected", "override", "readonly"]
  9133. }, modified);
  9134. const accessibility = modified.accessibility;
  9135. const override = modified.override;
  9136. const readonly = modified.readonly;
  9137. if (!(flags & 4) && (accessibility || readonly || override)) {
  9138. this.raise(TSErrors.UnexpectedParameterModifier, startLoc);
  9139. }
  9140. const left = this.parseMaybeDefault();
  9141. if (flags & 2) {
  9142. this.parseFunctionParamType(left);
  9143. }
  9144. const elt = this.parseMaybeDefault(left.loc.start, left);
  9145. if (accessibility || readonly || override) {
  9146. const pp = this.startNodeAt(startLoc);
  9147. if (decorators.length) {
  9148. pp.decorators = decorators;
  9149. }
  9150. if (accessibility) pp.accessibility = accessibility;
  9151. if (readonly) pp.readonly = readonly;
  9152. if (override) pp.override = override;
  9153. if (elt.type !== "Identifier" && elt.type !== "AssignmentPattern") {
  9154. this.raise(TSErrors.UnsupportedParameterPropertyKind, pp);
  9155. }
  9156. pp.parameter = elt;
  9157. return this.finishNode(pp, "TSParameterProperty");
  9158. }
  9159. if (decorators.length) {
  9160. left.decorators = decorators;
  9161. }
  9162. return elt;
  9163. }
  9164. isSimpleParameter(node) {
  9165. return node.type === "TSParameterProperty" && super.isSimpleParameter(node.parameter) || super.isSimpleParameter(node);
  9166. }
  9167. tsDisallowOptionalPattern(node) {
  9168. for (const param of node.params) {
  9169. if (param.type !== "Identifier" && param.optional && !this.state.isAmbientContext) {
  9170. this.raise(TSErrors.PatternIsOptional, param);
  9171. }
  9172. }
  9173. }
  9174. setArrowFunctionParameters(node, params, trailingCommaLoc) {
  9175. super.setArrowFunctionParameters(node, params, trailingCommaLoc);
  9176. this.tsDisallowOptionalPattern(node);
  9177. }
  9178. parseFunctionBodyAndFinish(node, type, isMethod = false) {
  9179. if (this.match(14)) {
  9180. node.returnType = this.tsParseTypeOrTypePredicateAnnotation(14);
  9181. }
  9182. const bodilessType = type === "FunctionDeclaration" ? "TSDeclareFunction" : type === "ClassMethod" || type === "ClassPrivateMethod" ? "TSDeclareMethod" : undefined;
  9183. if (bodilessType && !this.match(5) && this.isLineTerminator()) {
  9184. return this.finishNode(node, bodilessType);
  9185. }
  9186. if (bodilessType === "TSDeclareFunction" && this.state.isAmbientContext) {
  9187. this.raise(TSErrors.DeclareFunctionHasImplementation, node);
  9188. if (node.declare) {
  9189. return super.parseFunctionBodyAndFinish(node, bodilessType, isMethod);
  9190. }
  9191. }
  9192. this.tsDisallowOptionalPattern(node);
  9193. return super.parseFunctionBodyAndFinish(node, type, isMethod);
  9194. }
  9195. registerFunctionStatementId(node) {
  9196. if (!node.body && node.id) {
  9197. this.checkIdentifier(node.id, 1024);
  9198. } else {
  9199. super.registerFunctionStatementId(node);
  9200. }
  9201. }
  9202. tsCheckForInvalidTypeCasts(items) {
  9203. items.forEach(node => {
  9204. if ((node == null ? void 0 : node.type) === "TSTypeCastExpression") {
  9205. this.raise(TSErrors.UnexpectedTypeAnnotation, node.typeAnnotation);
  9206. }
  9207. });
  9208. }
  9209. toReferencedList(exprList, isInParens) {
  9210. this.tsCheckForInvalidTypeCasts(exprList);
  9211. return exprList;
  9212. }
  9213. parseArrayLike(close, canBePattern, isTuple, refExpressionErrors) {
  9214. const node = super.parseArrayLike(close, canBePattern, isTuple, refExpressionErrors);
  9215. if (node.type === "ArrayExpression") {
  9216. this.tsCheckForInvalidTypeCasts(node.elements);
  9217. }
  9218. return node;
  9219. }
  9220. parseSubscript(base, startLoc, noCalls, state) {
  9221. if (!this.hasPrecedingLineBreak() && this.match(35)) {
  9222. this.state.canStartJSXElement = false;
  9223. this.next();
  9224. const nonNullExpression = this.startNodeAt(startLoc);
  9225. nonNullExpression.expression = base;
  9226. return this.finishNode(nonNullExpression, "TSNonNullExpression");
  9227. }
  9228. let isOptionalCall = false;
  9229. if (this.match(18) && this.lookaheadCharCode() === 60) {
  9230. if (noCalls) {
  9231. state.stop = true;
  9232. return base;
  9233. }
  9234. state.optionalChainMember = isOptionalCall = true;
  9235. this.next();
  9236. }
  9237. if (this.match(47) || this.match(51)) {
  9238. let missingParenErrorLoc;
  9239. const result = this.tsTryParseAndCatch(() => {
  9240. if (!noCalls && this.atPossibleAsyncArrow(base)) {
  9241. const asyncArrowFn = this.tsTryParseGenericAsyncArrowFunction(startLoc);
  9242. if (asyncArrowFn) {
  9243. return asyncArrowFn;
  9244. }
  9245. }
  9246. const typeArguments = this.tsParseTypeArgumentsInExpression();
  9247. if (!typeArguments) return;
  9248. if (isOptionalCall && !this.match(10)) {
  9249. missingParenErrorLoc = this.state.curPosition();
  9250. return;
  9251. }
  9252. if (tokenIsTemplate(this.state.type)) {
  9253. const result = super.parseTaggedTemplateExpression(base, startLoc, state);
  9254. {
  9255. result.typeParameters = typeArguments;
  9256. }
  9257. return result;
  9258. }
  9259. if (!noCalls && this.eat(10)) {
  9260. const node = this.startNodeAt(startLoc);
  9261. node.callee = base;
  9262. node.arguments = this.parseCallExpressionArguments(11);
  9263. this.tsCheckForInvalidTypeCasts(node.arguments);
  9264. {
  9265. node.typeParameters = typeArguments;
  9266. }
  9267. if (state.optionalChainMember) {
  9268. node.optional = isOptionalCall;
  9269. }
  9270. return this.finishCallExpression(node, state.optionalChainMember);
  9271. }
  9272. const tokenType = this.state.type;
  9273. if (tokenType === 48 || tokenType === 52 || tokenType !== 10 && tokenCanStartExpression(tokenType) && !this.hasPrecedingLineBreak()) {
  9274. return;
  9275. }
  9276. const node = this.startNodeAt(startLoc);
  9277. node.expression = base;
  9278. {
  9279. node.typeParameters = typeArguments;
  9280. }
  9281. return this.finishNode(node, "TSInstantiationExpression");
  9282. });
  9283. if (missingParenErrorLoc) {
  9284. this.unexpected(missingParenErrorLoc, 10);
  9285. }
  9286. if (result) {
  9287. if (result.type === "TSInstantiationExpression") {
  9288. if (this.match(16) || this.match(18) && this.lookaheadCharCode() !== 40) {
  9289. this.raise(TSErrors.InvalidPropertyAccessAfterInstantiationExpression, this.state.startLoc);
  9290. }
  9291. if (!this.match(16) && !this.match(18)) {
  9292. result.expression = super.stopParseSubscript(base, state);
  9293. }
  9294. }
  9295. return result;
  9296. }
  9297. }
  9298. return super.parseSubscript(base, startLoc, noCalls, state);
  9299. }
  9300. parseNewCallee(node) {
  9301. var _callee$extra;
  9302. super.parseNewCallee(node);
  9303. const {
  9304. callee
  9305. } = node;
  9306. if (callee.type === "TSInstantiationExpression" && !((_callee$extra = callee.extra) != null && _callee$extra.parenthesized)) {
  9307. {
  9308. node.typeParameters = callee.typeParameters;
  9309. }
  9310. node.callee = callee.expression;
  9311. }
  9312. }
  9313. parseExprOp(left, leftStartLoc, minPrec) {
  9314. let isSatisfies;
  9315. if (tokenOperatorPrecedence(58) > minPrec && !this.hasPrecedingLineBreak() && (this.isContextual(93) || (isSatisfies = this.isContextual(120)))) {
  9316. const node = this.startNodeAt(leftStartLoc);
  9317. node.expression = left;
  9318. node.typeAnnotation = this.tsInType(() => {
  9319. this.next();
  9320. if (this.match(75)) {
  9321. if (isSatisfies) {
  9322. this.raise(Errors.UnexpectedKeyword, this.state.startLoc, {
  9323. keyword: "const"
  9324. });
  9325. }
  9326. return this.tsParseTypeReference();
  9327. }
  9328. return this.tsParseType();
  9329. });
  9330. this.finishNode(node, isSatisfies ? "TSSatisfiesExpression" : "TSAsExpression");
  9331. this.reScan_lt_gt();
  9332. return this.parseExprOp(node, leftStartLoc, minPrec);
  9333. }
  9334. return super.parseExprOp(left, leftStartLoc, minPrec);
  9335. }
  9336. checkReservedWord(word, startLoc, checkKeywords, isBinding) {
  9337. if (!this.state.isAmbientContext) {
  9338. super.checkReservedWord(word, startLoc, checkKeywords, isBinding);
  9339. }
  9340. }
  9341. checkImportReflection(node) {
  9342. super.checkImportReflection(node);
  9343. if (node.module && node.importKind !== "value") {
  9344. this.raise(TSErrors.ImportReflectionHasImportType, node.specifiers[0].loc.start);
  9345. }
  9346. }
  9347. checkDuplicateExports() {}
  9348. isPotentialImportPhase(isExport) {
  9349. if (super.isPotentialImportPhase(isExport)) return true;
  9350. if (this.isContextual(130)) {
  9351. const ch = this.lookaheadCharCode();
  9352. return isExport ? ch === 123 || ch === 42 : ch !== 61;
  9353. }
  9354. return !isExport && this.isContextual(87);
  9355. }
  9356. applyImportPhase(node, isExport, phase, loc) {
  9357. super.applyImportPhase(node, isExport, phase, loc);
  9358. if (isExport) {
  9359. node.exportKind = phase === "type" ? "type" : "value";
  9360. } else {
  9361. node.importKind = phase === "type" || phase === "typeof" ? phase : "value";
  9362. }
  9363. }
  9364. parseImport(node) {
  9365. if (this.match(134)) {
  9366. node.importKind = "value";
  9367. return super.parseImport(node);
  9368. }
  9369. let importNode;
  9370. if (tokenIsIdentifier(this.state.type) && this.lookaheadCharCode() === 61) {
  9371. node.importKind = "value";
  9372. return this.tsParseImportEqualsDeclaration(node);
  9373. } else if (this.isContextual(130)) {
  9374. const maybeDefaultIdentifier = this.parseMaybeImportPhase(node, false);
  9375. if (this.lookaheadCharCode() === 61) {
  9376. return this.tsParseImportEqualsDeclaration(node, maybeDefaultIdentifier);
  9377. } else {
  9378. importNode = super.parseImportSpecifiersAndAfter(node, maybeDefaultIdentifier);
  9379. }
  9380. } else {
  9381. importNode = super.parseImport(node);
  9382. }
  9383. if (importNode.importKind === "type" && importNode.specifiers.length > 1 && importNode.specifiers[0].type === "ImportDefaultSpecifier") {
  9384. this.raise(TSErrors.TypeImportCannotSpecifyDefaultAndNamed, importNode);
  9385. }
  9386. return importNode;
  9387. }
  9388. parseExport(node, decorators) {
  9389. if (this.match(83)) {
  9390. const nodeImportEquals = node;
  9391. this.next();
  9392. let maybeDefaultIdentifier = null;
  9393. if (this.isContextual(130) && this.isPotentialImportPhase(false)) {
  9394. maybeDefaultIdentifier = this.parseMaybeImportPhase(nodeImportEquals, false);
  9395. } else {
  9396. nodeImportEquals.importKind = "value";
  9397. }
  9398. const declaration = this.tsParseImportEqualsDeclaration(nodeImportEquals, maybeDefaultIdentifier, true);
  9399. {
  9400. return declaration;
  9401. }
  9402. } else if (this.eat(29)) {
  9403. const assign = node;
  9404. assign.expression = super.parseExpression();
  9405. this.semicolon();
  9406. this.sawUnambiguousESM = true;
  9407. return this.finishNode(assign, "TSExportAssignment");
  9408. } else if (this.eatContextual(93)) {
  9409. const decl = node;
  9410. this.expectContextual(128);
  9411. decl.id = this.parseIdentifier();
  9412. this.semicolon();
  9413. return this.finishNode(decl, "TSNamespaceExportDeclaration");
  9414. } else {
  9415. return super.parseExport(node, decorators);
  9416. }
  9417. }
  9418. isAbstractClass() {
  9419. return this.isContextual(124) && this.lookahead().type === 80;
  9420. }
  9421. parseExportDefaultExpression() {
  9422. if (this.isAbstractClass()) {
  9423. const cls = this.startNode();
  9424. this.next();
  9425. cls.abstract = true;
  9426. return this.parseClass(cls, true, true);
  9427. }
  9428. if (this.match(129)) {
  9429. const result = this.tsParseInterfaceDeclaration(this.startNode());
  9430. if (result) return result;
  9431. }
  9432. return super.parseExportDefaultExpression();
  9433. }
  9434. parseVarStatement(node, kind, allowMissingInitializer = false) {
  9435. const {
  9436. isAmbientContext
  9437. } = this.state;
  9438. const declaration = super.parseVarStatement(node, kind, allowMissingInitializer || isAmbientContext);
  9439. if (!isAmbientContext) return declaration;
  9440. for (const {
  9441. id,
  9442. init
  9443. } of declaration.declarations) {
  9444. if (!init) continue;
  9445. if (kind === "var" || kind === "let" || !!id.typeAnnotation) {
  9446. this.raise(TSErrors.InitializerNotAllowedInAmbientContext, init);
  9447. } else if (!isValidAmbientConstInitializer(init, this.hasPlugin("estree"))) {
  9448. this.raise(TSErrors.ConstInitializerMustBeStringOrNumericLiteralOrLiteralEnumReference, init);
  9449. }
  9450. }
  9451. return declaration;
  9452. }
  9453. parseStatementContent(flags, decorators) {
  9454. if (this.match(75) && this.isLookaheadContextual("enum")) {
  9455. const node = this.startNode();
  9456. this.expect(75);
  9457. return this.tsParseEnumDeclaration(node, {
  9458. const: true
  9459. });
  9460. }
  9461. if (this.isContextual(126)) {
  9462. return this.tsParseEnumDeclaration(this.startNode());
  9463. }
  9464. if (this.isContextual(129)) {
  9465. const result = this.tsParseInterfaceDeclaration(this.startNode());
  9466. if (result) return result;
  9467. }
  9468. return super.parseStatementContent(flags, decorators);
  9469. }
  9470. parseAccessModifier() {
  9471. return this.tsParseModifier(["public", "protected", "private"]);
  9472. }
  9473. tsHasSomeModifiers(member, modifiers) {
  9474. return modifiers.some(modifier => {
  9475. if (tsIsAccessModifier(modifier)) {
  9476. return member.accessibility === modifier;
  9477. }
  9478. return !!member[modifier];
  9479. });
  9480. }
  9481. tsIsStartOfStaticBlocks() {
  9482. return this.isContextual(106) && this.lookaheadCharCode() === 123;
  9483. }
  9484. parseClassMember(classBody, member, state) {
  9485. const modifiers = ["declare", "private", "public", "protected", "override", "abstract", "readonly", "static"];
  9486. this.tsParseModifiers({
  9487. allowedModifiers: modifiers,
  9488. disallowedModifiers: ["in", "out"],
  9489. stopOnStartOfClassStaticBlock: true,
  9490. errorTemplate: TSErrors.InvalidModifierOnTypeParameterPositions
  9491. }, member);
  9492. const callParseClassMemberWithIsStatic = () => {
  9493. if (this.tsIsStartOfStaticBlocks()) {
  9494. this.next();
  9495. this.next();
  9496. if (this.tsHasSomeModifiers(member, modifiers)) {
  9497. this.raise(TSErrors.StaticBlockCannotHaveModifier, this.state.curPosition());
  9498. }
  9499. super.parseClassStaticBlock(classBody, member);
  9500. } else {
  9501. this.parseClassMemberWithIsStatic(classBody, member, state, !!member.static);
  9502. }
  9503. };
  9504. if (member.declare) {
  9505. this.tsInAmbientContext(callParseClassMemberWithIsStatic);
  9506. } else {
  9507. callParseClassMemberWithIsStatic();
  9508. }
  9509. }
  9510. parseClassMemberWithIsStatic(classBody, member, state, isStatic) {
  9511. const idx = this.tsTryParseIndexSignature(member);
  9512. if (idx) {
  9513. classBody.body.push(idx);
  9514. if (member.abstract) {
  9515. this.raise(TSErrors.IndexSignatureHasAbstract, member);
  9516. }
  9517. if (member.accessibility) {
  9518. this.raise(TSErrors.IndexSignatureHasAccessibility, member, {
  9519. modifier: member.accessibility
  9520. });
  9521. }
  9522. if (member.declare) {
  9523. this.raise(TSErrors.IndexSignatureHasDeclare, member);
  9524. }
  9525. if (member.override) {
  9526. this.raise(TSErrors.IndexSignatureHasOverride, member);
  9527. }
  9528. return;
  9529. }
  9530. if (!this.state.inAbstractClass && member.abstract) {
  9531. this.raise(TSErrors.NonAbstractClassHasAbstractMethod, member);
  9532. }
  9533. if (member.override) {
  9534. if (!state.hadSuperClass) {
  9535. this.raise(TSErrors.OverrideNotInSubClass, member);
  9536. }
  9537. }
  9538. super.parseClassMemberWithIsStatic(classBody, member, state, isStatic);
  9539. }
  9540. parsePostMemberNameModifiers(methodOrProp) {
  9541. const optional = this.eat(17);
  9542. if (optional) methodOrProp.optional = true;
  9543. if (methodOrProp.readonly && this.match(10)) {
  9544. this.raise(TSErrors.ClassMethodHasReadonly, methodOrProp);
  9545. }
  9546. if (methodOrProp.declare && this.match(10)) {
  9547. this.raise(TSErrors.ClassMethodHasDeclare, methodOrProp);
  9548. }
  9549. }
  9550. parseExpressionStatement(node, expr, decorators) {
  9551. const decl = expr.type === "Identifier" ? this.tsParseExpressionStatement(node, expr, decorators) : undefined;
  9552. return decl || super.parseExpressionStatement(node, expr, decorators);
  9553. }
  9554. shouldParseExportDeclaration() {
  9555. if (this.tsIsDeclarationStart()) return true;
  9556. return super.shouldParseExportDeclaration();
  9557. }
  9558. parseConditional(expr, startLoc, refExpressionErrors) {
  9559. if (!this.match(17)) return expr;
  9560. if (this.state.maybeInArrowParameters) {
  9561. const nextCh = this.lookaheadCharCode();
  9562. if (nextCh === 44 || nextCh === 61 || nextCh === 58 || nextCh === 41) {
  9563. this.setOptionalParametersError(refExpressionErrors);
  9564. return expr;
  9565. }
  9566. }
  9567. return super.parseConditional(expr, startLoc, refExpressionErrors);
  9568. }
  9569. parseParenItem(node, startLoc) {
  9570. const newNode = super.parseParenItem(node, startLoc);
  9571. if (this.eat(17)) {
  9572. newNode.optional = true;
  9573. this.resetEndLocation(node);
  9574. }
  9575. if (this.match(14)) {
  9576. const typeCastNode = this.startNodeAt(startLoc);
  9577. typeCastNode.expression = node;
  9578. typeCastNode.typeAnnotation = this.tsParseTypeAnnotation();
  9579. return this.finishNode(typeCastNode, "TSTypeCastExpression");
  9580. }
  9581. return node;
  9582. }
  9583. parseExportDeclaration(node) {
  9584. if (!this.state.isAmbientContext && this.isContextual(125)) {
  9585. return this.tsInAmbientContext(() => this.parseExportDeclaration(node));
  9586. }
  9587. const startLoc = this.state.startLoc;
  9588. const isDeclare = this.eatContextual(125);
  9589. if (isDeclare && (this.isContextual(125) || !this.shouldParseExportDeclaration())) {
  9590. throw this.raise(TSErrors.ExpectedAmbientAfterExportDeclare, this.state.startLoc);
  9591. }
  9592. const isIdentifier = tokenIsIdentifier(this.state.type);
  9593. const declaration = isIdentifier && this.tsTryParseExportDeclaration() || super.parseExportDeclaration(node);
  9594. if (!declaration) return null;
  9595. if (declaration.type === "TSInterfaceDeclaration" || declaration.type === "TSTypeAliasDeclaration" || isDeclare) {
  9596. node.exportKind = "type";
  9597. }
  9598. if (isDeclare && declaration.type !== "TSImportEqualsDeclaration") {
  9599. this.resetStartLocation(declaration, startLoc);
  9600. declaration.declare = true;
  9601. }
  9602. return declaration;
  9603. }
  9604. parseClassId(node, isStatement, optionalId, bindingType) {
  9605. if ((!isStatement || optionalId) && this.isContextual(113)) {
  9606. return;
  9607. }
  9608. super.parseClassId(node, isStatement, optionalId, node.declare ? 1024 : 8331);
  9609. const typeParameters = this.tsTryParseTypeParameters(this.tsParseInOutConstModifiers);
  9610. if (typeParameters) node.typeParameters = typeParameters;
  9611. }
  9612. parseClassPropertyAnnotation(node) {
  9613. if (!node.optional) {
  9614. if (this.eat(35)) {
  9615. node.definite = true;
  9616. } else if (this.eat(17)) {
  9617. node.optional = true;
  9618. }
  9619. }
  9620. const type = this.tsTryParseTypeAnnotation();
  9621. if (type) node.typeAnnotation = type;
  9622. }
  9623. parseClassProperty(node) {
  9624. this.parseClassPropertyAnnotation(node);
  9625. if (this.state.isAmbientContext && !(node.readonly && !node.typeAnnotation) && this.match(29)) {
  9626. this.raise(TSErrors.DeclareClassFieldHasInitializer, this.state.startLoc);
  9627. }
  9628. if (node.abstract && this.match(29)) {
  9629. const {
  9630. key
  9631. } = node;
  9632. this.raise(TSErrors.AbstractPropertyHasInitializer, this.state.startLoc, {
  9633. propertyName: key.type === "Identifier" && !node.computed ? key.name : `[${this.input.slice(this.offsetToSourcePos(key.start), this.offsetToSourcePos(key.end))}]`
  9634. });
  9635. }
  9636. return super.parseClassProperty(node);
  9637. }
  9638. parseClassPrivateProperty(node) {
  9639. if (node.abstract) {
  9640. this.raise(TSErrors.PrivateElementHasAbstract, node);
  9641. }
  9642. if (node.accessibility) {
  9643. this.raise(TSErrors.PrivateElementHasAccessibility, node, {
  9644. modifier: node.accessibility
  9645. });
  9646. }
  9647. this.parseClassPropertyAnnotation(node);
  9648. return super.parseClassPrivateProperty(node);
  9649. }
  9650. parseClassAccessorProperty(node) {
  9651. this.parseClassPropertyAnnotation(node);
  9652. if (node.optional) {
  9653. this.raise(TSErrors.AccessorCannotBeOptional, node);
  9654. }
  9655. return super.parseClassAccessorProperty(node);
  9656. }
  9657. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  9658. const typeParameters = this.tsTryParseTypeParameters(this.tsParseConstModifier);
  9659. if (typeParameters && isConstructor) {
  9660. this.raise(TSErrors.ConstructorHasTypeParameters, typeParameters);
  9661. }
  9662. const {
  9663. declare = false,
  9664. kind
  9665. } = method;
  9666. if (declare && (kind === "get" || kind === "set")) {
  9667. this.raise(TSErrors.DeclareAccessor, method, {
  9668. kind
  9669. });
  9670. }
  9671. if (typeParameters) method.typeParameters = typeParameters;
  9672. super.pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper);
  9673. }
  9674. pushClassPrivateMethod(classBody, method, isGenerator, isAsync) {
  9675. const typeParameters = this.tsTryParseTypeParameters(this.tsParseConstModifier);
  9676. if (typeParameters) method.typeParameters = typeParameters;
  9677. super.pushClassPrivateMethod(classBody, method, isGenerator, isAsync);
  9678. }
  9679. declareClassPrivateMethodInScope(node, kind) {
  9680. if (node.type === "TSDeclareMethod") return;
  9681. if (node.type === "MethodDefinition" && node.value.body == null) {
  9682. return;
  9683. }
  9684. super.declareClassPrivateMethodInScope(node, kind);
  9685. }
  9686. parseClassSuper(node) {
  9687. super.parseClassSuper(node);
  9688. if (node.superClass && (this.match(47) || this.match(51))) {
  9689. {
  9690. node.superTypeParameters = this.tsParseTypeArgumentsInExpression();
  9691. }
  9692. }
  9693. if (this.eatContextual(113)) {
  9694. node.implements = this.tsParseHeritageClause("implements");
  9695. }
  9696. }
  9697. parseObjPropValue(prop, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors) {
  9698. const typeParameters = this.tsTryParseTypeParameters(this.tsParseConstModifier);
  9699. if (typeParameters) prop.typeParameters = typeParameters;
  9700. return super.parseObjPropValue(prop, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors);
  9701. }
  9702. parseFunctionParams(node, isConstructor) {
  9703. const typeParameters = this.tsTryParseTypeParameters(this.tsParseConstModifier);
  9704. if (typeParameters) node.typeParameters = typeParameters;
  9705. super.parseFunctionParams(node, isConstructor);
  9706. }
  9707. parseVarId(decl, kind) {
  9708. super.parseVarId(decl, kind);
  9709. if (decl.id.type === "Identifier" && !this.hasPrecedingLineBreak() && this.eat(35)) {
  9710. decl.definite = true;
  9711. }
  9712. const type = this.tsTryParseTypeAnnotation();
  9713. if (type) {
  9714. decl.id.typeAnnotation = type;
  9715. this.resetEndLocation(decl.id);
  9716. }
  9717. }
  9718. parseAsyncArrowFromCallExpression(node, call) {
  9719. if (this.match(14)) {
  9720. node.returnType = this.tsParseTypeAnnotation();
  9721. }
  9722. return super.parseAsyncArrowFromCallExpression(node, call);
  9723. }
  9724. parseMaybeAssign(refExpressionErrors, afterLeftParse) {
  9725. var _jsx, _jsx2, _typeCast, _jsx3, _typeCast2;
  9726. let state;
  9727. let jsx;
  9728. let typeCast;
  9729. if (this.hasPlugin("jsx") && (this.match(143) || this.match(47))) {
  9730. state = this.state.clone();
  9731. jsx = this.tryParse(() => super.parseMaybeAssign(refExpressionErrors, afterLeftParse), state);
  9732. if (!jsx.error) return jsx.node;
  9733. const {
  9734. context
  9735. } = this.state;
  9736. const currentContext = context[context.length - 1];
  9737. if (currentContext === types.j_oTag || currentContext === types.j_expr) {
  9738. context.pop();
  9739. }
  9740. }
  9741. if (!((_jsx = jsx) != null && _jsx.error) && !this.match(47)) {
  9742. return super.parseMaybeAssign(refExpressionErrors, afterLeftParse);
  9743. }
  9744. if (!state || state === this.state) state = this.state.clone();
  9745. let typeParameters;
  9746. const arrow = this.tryParse(abort => {
  9747. var _expr$extra, _typeParameters;
  9748. typeParameters = this.tsParseTypeParameters(this.tsParseConstModifier);
  9749. const expr = super.parseMaybeAssign(refExpressionErrors, afterLeftParse);
  9750. if (expr.type !== "ArrowFunctionExpression" || (_expr$extra = expr.extra) != null && _expr$extra.parenthesized) {
  9751. abort();
  9752. }
  9753. if (((_typeParameters = typeParameters) == null ? void 0 : _typeParameters.params.length) !== 0) {
  9754. this.resetStartLocationFromNode(expr, typeParameters);
  9755. }
  9756. expr.typeParameters = typeParameters;
  9757. return expr;
  9758. }, state);
  9759. if (!arrow.error && !arrow.aborted) {
  9760. if (typeParameters) this.reportReservedArrowTypeParam(typeParameters);
  9761. return arrow.node;
  9762. }
  9763. if (!jsx) {
  9764. assert(!this.hasPlugin("jsx"));
  9765. typeCast = this.tryParse(() => super.parseMaybeAssign(refExpressionErrors, afterLeftParse), state);
  9766. if (!typeCast.error) return typeCast.node;
  9767. }
  9768. if ((_jsx2 = jsx) != null && _jsx2.node) {
  9769. this.state = jsx.failState;
  9770. return jsx.node;
  9771. }
  9772. if (arrow.node) {
  9773. this.state = arrow.failState;
  9774. if (typeParameters) this.reportReservedArrowTypeParam(typeParameters);
  9775. return arrow.node;
  9776. }
  9777. if ((_typeCast = typeCast) != null && _typeCast.node) {
  9778. this.state = typeCast.failState;
  9779. return typeCast.node;
  9780. }
  9781. throw ((_jsx3 = jsx) == null ? void 0 : _jsx3.error) || arrow.error || ((_typeCast2 = typeCast) == null ? void 0 : _typeCast2.error);
  9782. }
  9783. reportReservedArrowTypeParam(node) {
  9784. var _node$extra2;
  9785. if (node.params.length === 1 && !node.params[0].constraint && !((_node$extra2 = node.extra) != null && _node$extra2.trailingComma) && this.getPluginOption("typescript", "disallowAmbiguousJSXLike")) {
  9786. this.raise(TSErrors.ReservedArrowTypeParam, node);
  9787. }
  9788. }
  9789. parseMaybeUnary(refExpressionErrors, sawUnary) {
  9790. if (!this.hasPlugin("jsx") && this.match(47)) {
  9791. return this.tsParseTypeAssertion();
  9792. }
  9793. return super.parseMaybeUnary(refExpressionErrors, sawUnary);
  9794. }
  9795. parseArrow(node) {
  9796. if (this.match(14)) {
  9797. const result = this.tryParse(abort => {
  9798. const returnType = this.tsParseTypeOrTypePredicateAnnotation(14);
  9799. if (this.canInsertSemicolon() || !this.match(19)) abort();
  9800. return returnType;
  9801. });
  9802. if (result.aborted) return;
  9803. if (!result.thrown) {
  9804. if (result.error) this.state = result.failState;
  9805. node.returnType = result.node;
  9806. }
  9807. }
  9808. return super.parseArrow(node);
  9809. }
  9810. parseFunctionParamType(param) {
  9811. if (this.eat(17)) {
  9812. param.optional = true;
  9813. }
  9814. const type = this.tsTryParseTypeAnnotation();
  9815. if (type) param.typeAnnotation = type;
  9816. this.resetEndLocation(param);
  9817. return param;
  9818. }
  9819. isAssignable(node, isBinding) {
  9820. switch (node.type) {
  9821. case "TSTypeCastExpression":
  9822. return this.isAssignable(node.expression, isBinding);
  9823. case "TSParameterProperty":
  9824. return true;
  9825. default:
  9826. return super.isAssignable(node, isBinding);
  9827. }
  9828. }
  9829. toAssignable(node, isLHS = false) {
  9830. switch (node.type) {
  9831. case "ParenthesizedExpression":
  9832. this.toAssignableParenthesizedExpression(node, isLHS);
  9833. break;
  9834. case "TSAsExpression":
  9835. case "TSSatisfiesExpression":
  9836. case "TSNonNullExpression":
  9837. case "TSTypeAssertion":
  9838. if (isLHS) {
  9839. this.expressionScope.recordArrowParameterBindingError(TSErrors.UnexpectedTypeCastInParameter, node);
  9840. } else {
  9841. this.raise(TSErrors.UnexpectedTypeCastInParameter, node);
  9842. }
  9843. this.toAssignable(node.expression, isLHS);
  9844. break;
  9845. case "AssignmentExpression":
  9846. if (!isLHS && node.left.type === "TSTypeCastExpression") {
  9847. node.left = this.typeCastToParameter(node.left);
  9848. }
  9849. default:
  9850. super.toAssignable(node, isLHS);
  9851. }
  9852. }
  9853. toAssignableParenthesizedExpression(node, isLHS) {
  9854. switch (node.expression.type) {
  9855. case "TSAsExpression":
  9856. case "TSSatisfiesExpression":
  9857. case "TSNonNullExpression":
  9858. case "TSTypeAssertion":
  9859. case "ParenthesizedExpression":
  9860. this.toAssignable(node.expression, isLHS);
  9861. break;
  9862. default:
  9863. super.toAssignable(node, isLHS);
  9864. }
  9865. }
  9866. checkToRestConversion(node, allowPattern) {
  9867. switch (node.type) {
  9868. case "TSAsExpression":
  9869. case "TSSatisfiesExpression":
  9870. case "TSTypeAssertion":
  9871. case "TSNonNullExpression":
  9872. this.checkToRestConversion(node.expression, false);
  9873. break;
  9874. default:
  9875. super.checkToRestConversion(node, allowPattern);
  9876. }
  9877. }
  9878. isValidLVal(type, isUnparenthesizedInAssign, binding) {
  9879. switch (type) {
  9880. case "TSTypeCastExpression":
  9881. return true;
  9882. case "TSParameterProperty":
  9883. return "parameter";
  9884. case "TSNonNullExpression":
  9885. return "expression";
  9886. case "TSAsExpression":
  9887. case "TSSatisfiesExpression":
  9888. case "TSTypeAssertion":
  9889. return (binding !== 64 || !isUnparenthesizedInAssign) && ["expression", true];
  9890. default:
  9891. return super.isValidLVal(type, isUnparenthesizedInAssign, binding);
  9892. }
  9893. }
  9894. parseBindingAtom() {
  9895. if (this.state.type === 78) {
  9896. return this.parseIdentifier(true);
  9897. }
  9898. return super.parseBindingAtom();
  9899. }
  9900. parseMaybeDecoratorArguments(expr, startLoc) {
  9901. if (this.match(47) || this.match(51)) {
  9902. const typeArguments = this.tsParseTypeArgumentsInExpression();
  9903. if (this.match(10)) {
  9904. const call = super.parseMaybeDecoratorArguments(expr, startLoc);
  9905. {
  9906. call.typeParameters = typeArguments;
  9907. }
  9908. return call;
  9909. }
  9910. this.unexpected(null, 10);
  9911. }
  9912. return super.parseMaybeDecoratorArguments(expr, startLoc);
  9913. }
  9914. checkCommaAfterRest(close) {
  9915. if (this.state.isAmbientContext && this.match(12) && this.lookaheadCharCode() === close) {
  9916. this.next();
  9917. return false;
  9918. }
  9919. return super.checkCommaAfterRest(close);
  9920. }
  9921. isClassMethod() {
  9922. return this.match(47) || super.isClassMethod();
  9923. }
  9924. isClassProperty() {
  9925. return this.match(35) || this.match(14) || super.isClassProperty();
  9926. }
  9927. parseMaybeDefault(startLoc, left) {
  9928. const node = super.parseMaybeDefault(startLoc, left);
  9929. if (node.type === "AssignmentPattern" && node.typeAnnotation && node.right.start < node.typeAnnotation.start) {
  9930. this.raise(TSErrors.TypeAnnotationAfterAssign, node.typeAnnotation);
  9931. }
  9932. return node;
  9933. }
  9934. getTokenFromCode(code) {
  9935. if (this.state.inType) {
  9936. if (code === 62) {
  9937. this.finishOp(48, 1);
  9938. return;
  9939. }
  9940. if (code === 60) {
  9941. this.finishOp(47, 1);
  9942. return;
  9943. }
  9944. }
  9945. super.getTokenFromCode(code);
  9946. }
  9947. reScan_lt_gt() {
  9948. const {
  9949. type
  9950. } = this.state;
  9951. if (type === 47) {
  9952. this.state.pos -= 1;
  9953. this.readToken_lt();
  9954. } else if (type === 48) {
  9955. this.state.pos -= 1;
  9956. this.readToken_gt();
  9957. }
  9958. }
  9959. reScan_lt() {
  9960. const {
  9961. type
  9962. } = this.state;
  9963. if (type === 51) {
  9964. this.state.pos -= 2;
  9965. this.finishOp(47, 1);
  9966. return 47;
  9967. }
  9968. return type;
  9969. }
  9970. toAssignableListItem(exprList, index, isLHS) {
  9971. const node = exprList[index];
  9972. if (node.type === "TSTypeCastExpression") {
  9973. exprList[index] = this.typeCastToParameter(node);
  9974. }
  9975. super.toAssignableListItem(exprList, index, isLHS);
  9976. }
  9977. typeCastToParameter(node) {
  9978. node.expression.typeAnnotation = node.typeAnnotation;
  9979. this.resetEndLocation(node.expression, node.typeAnnotation.loc.end);
  9980. return node.expression;
  9981. }
  9982. shouldParseArrow(params) {
  9983. if (this.match(14)) {
  9984. return params.every(expr => this.isAssignable(expr, true));
  9985. }
  9986. return super.shouldParseArrow(params);
  9987. }
  9988. shouldParseAsyncArrow() {
  9989. return this.match(14) || super.shouldParseAsyncArrow();
  9990. }
  9991. canHaveLeadingDecorator() {
  9992. return super.canHaveLeadingDecorator() || this.isAbstractClass();
  9993. }
  9994. jsxParseOpeningElementAfterName(node) {
  9995. if (this.match(47) || this.match(51)) {
  9996. const typeArguments = this.tsTryParseAndCatch(() => this.tsParseTypeArgumentsInExpression());
  9997. if (typeArguments) {
  9998. {
  9999. node.typeParameters = typeArguments;
  10000. }
  10001. }
  10002. }
  10003. return super.jsxParseOpeningElementAfterName(node);
  10004. }
  10005. getGetterSetterExpectedParamCount(method) {
  10006. const baseCount = super.getGetterSetterExpectedParamCount(method);
  10007. const params = this.getObjectOrClassMethodParams(method);
  10008. const firstParam = params[0];
  10009. const hasContextParam = firstParam && this.isThisParam(firstParam);
  10010. return hasContextParam ? baseCount + 1 : baseCount;
  10011. }
  10012. parseCatchClauseParam() {
  10013. const param = super.parseCatchClauseParam();
  10014. const type = this.tsTryParseTypeAnnotation();
  10015. if (type) {
  10016. param.typeAnnotation = type;
  10017. this.resetEndLocation(param);
  10018. }
  10019. return param;
  10020. }
  10021. tsInAmbientContext(cb) {
  10022. const {
  10023. isAmbientContext: oldIsAmbientContext,
  10024. strict: oldStrict
  10025. } = this.state;
  10026. this.state.isAmbientContext = true;
  10027. this.state.strict = false;
  10028. try {
  10029. return cb();
  10030. } finally {
  10031. this.state.isAmbientContext = oldIsAmbientContext;
  10032. this.state.strict = oldStrict;
  10033. }
  10034. }
  10035. parseClass(node, isStatement, optionalId) {
  10036. const oldInAbstractClass = this.state.inAbstractClass;
  10037. this.state.inAbstractClass = !!node.abstract;
  10038. try {
  10039. return super.parseClass(node, isStatement, optionalId);
  10040. } finally {
  10041. this.state.inAbstractClass = oldInAbstractClass;
  10042. }
  10043. }
  10044. tsParseAbstractDeclaration(node, decorators) {
  10045. if (this.match(80)) {
  10046. node.abstract = true;
  10047. return this.maybeTakeDecorators(decorators, this.parseClass(node, true, false));
  10048. } else if (this.isContextual(129)) {
  10049. if (!this.hasFollowingLineBreak()) {
  10050. node.abstract = true;
  10051. this.raise(TSErrors.NonClassMethodPropertyHasAbstractModifier, node);
  10052. return this.tsParseInterfaceDeclaration(node);
  10053. }
  10054. } else {
  10055. this.unexpected(null, 80);
  10056. }
  10057. }
  10058. parseMethod(node, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope) {
  10059. const method = super.parseMethod(node, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope);
  10060. if (method.abstract || method.type === "TSAbstractMethodDefinition") {
  10061. const hasEstreePlugin = this.hasPlugin("estree");
  10062. const methodFn = hasEstreePlugin ? method.value : method;
  10063. if (methodFn.body) {
  10064. const {
  10065. key
  10066. } = method;
  10067. this.raise(TSErrors.AbstractMethodHasImplementation, method, {
  10068. methodName: key.type === "Identifier" && !method.computed ? key.name : `[${this.input.slice(this.offsetToSourcePos(key.start), this.offsetToSourcePos(key.end))}]`
  10069. });
  10070. }
  10071. }
  10072. return method;
  10073. }
  10074. tsParseTypeParameterName() {
  10075. const typeName = this.parseIdentifier();
  10076. return typeName.name;
  10077. }
  10078. shouldParseAsAmbientContext() {
  10079. return !!this.getPluginOption("typescript", "dts");
  10080. }
  10081. parse() {
  10082. if (this.shouldParseAsAmbientContext()) {
  10083. this.state.isAmbientContext = true;
  10084. }
  10085. return super.parse();
  10086. }
  10087. getExpression() {
  10088. if (this.shouldParseAsAmbientContext()) {
  10089. this.state.isAmbientContext = true;
  10090. }
  10091. return super.getExpression();
  10092. }
  10093. parseExportSpecifier(node, isString, isInTypeExport, isMaybeTypeOnly) {
  10094. if (!isString && isMaybeTypeOnly) {
  10095. this.parseTypeOnlyImportExportSpecifier(node, false, isInTypeExport);
  10096. return this.finishNode(node, "ExportSpecifier");
  10097. }
  10098. node.exportKind = "value";
  10099. return super.parseExportSpecifier(node, isString, isInTypeExport, isMaybeTypeOnly);
  10100. }
  10101. parseImportSpecifier(specifier, importedIsString, isInTypeOnlyImport, isMaybeTypeOnly, bindingType) {
  10102. if (!importedIsString && isMaybeTypeOnly) {
  10103. this.parseTypeOnlyImportExportSpecifier(specifier, true, isInTypeOnlyImport);
  10104. return this.finishNode(specifier, "ImportSpecifier");
  10105. }
  10106. specifier.importKind = "value";
  10107. return super.parseImportSpecifier(specifier, importedIsString, isInTypeOnlyImport, isMaybeTypeOnly, isInTypeOnlyImport ? 4098 : 4096);
  10108. }
  10109. parseTypeOnlyImportExportSpecifier(node, isImport, isInTypeOnlyImportExport) {
  10110. const leftOfAsKey = isImport ? "imported" : "local";
  10111. const rightOfAsKey = isImport ? "local" : "exported";
  10112. let leftOfAs = node[leftOfAsKey];
  10113. let rightOfAs;
  10114. let hasTypeSpecifier = false;
  10115. let canParseAsKeyword = true;
  10116. const loc = leftOfAs.loc.start;
  10117. if (this.isContextual(93)) {
  10118. const firstAs = this.parseIdentifier();
  10119. if (this.isContextual(93)) {
  10120. const secondAs = this.parseIdentifier();
  10121. if (tokenIsKeywordOrIdentifier(this.state.type)) {
  10122. hasTypeSpecifier = true;
  10123. leftOfAs = firstAs;
  10124. rightOfAs = isImport ? this.parseIdentifier() : this.parseModuleExportName();
  10125. canParseAsKeyword = false;
  10126. } else {
  10127. rightOfAs = secondAs;
  10128. canParseAsKeyword = false;
  10129. }
  10130. } else if (tokenIsKeywordOrIdentifier(this.state.type)) {
  10131. canParseAsKeyword = false;
  10132. rightOfAs = isImport ? this.parseIdentifier() : this.parseModuleExportName();
  10133. } else {
  10134. hasTypeSpecifier = true;
  10135. leftOfAs = firstAs;
  10136. }
  10137. } else if (tokenIsKeywordOrIdentifier(this.state.type)) {
  10138. hasTypeSpecifier = true;
  10139. if (isImport) {
  10140. leftOfAs = this.parseIdentifier(true);
  10141. if (!this.isContextual(93)) {
  10142. this.checkReservedWord(leftOfAs.name, leftOfAs.loc.start, true, true);
  10143. }
  10144. } else {
  10145. leftOfAs = this.parseModuleExportName();
  10146. }
  10147. }
  10148. if (hasTypeSpecifier && isInTypeOnlyImportExport) {
  10149. this.raise(isImport ? TSErrors.TypeModifierIsUsedInTypeImports : TSErrors.TypeModifierIsUsedInTypeExports, loc);
  10150. }
  10151. node[leftOfAsKey] = leftOfAs;
  10152. node[rightOfAsKey] = rightOfAs;
  10153. const kindKey = isImport ? "importKind" : "exportKind";
  10154. node[kindKey] = hasTypeSpecifier ? "type" : "value";
  10155. if (canParseAsKeyword && this.eatContextual(93)) {
  10156. node[rightOfAsKey] = isImport ? this.parseIdentifier() : this.parseModuleExportName();
  10157. }
  10158. if (!node[rightOfAsKey]) {
  10159. node[rightOfAsKey] = this.cloneIdentifier(node[leftOfAsKey]);
  10160. }
  10161. if (isImport) {
  10162. this.checkIdentifier(node[rightOfAsKey], hasTypeSpecifier ? 4098 : 4096);
  10163. }
  10164. }
  10165. fillOptionalPropertiesForTSESLint(node) {
  10166. var _node$directive, _node$decorators, _node$optional, _node$typeAnnotation, _node$accessibility, _node$decorators2, _node$override, _node$readonly, _node$static, _node$declare, _node$returnType, _node$typeParameters, _node$optional2, _node$optional3, _node$accessibility2, _node$readonly2, _node$static2, _node$declare2, _node$definite, _node$readonly3, _node$typeAnnotation2, _node$accessibility3, _node$decorators3, _node$override2, _node$optional4, _node$id, _node$abstract, _node$declare3, _node$decorators4, _node$implements, _node$superTypeArgume, _node$typeParameters2, _node$declare4, _node$definite2, _node$const, _node$declare5, _node$computed, _node$qualifier, _node$options, _node$declare6, _node$extends, _node$declare7, _node$global, _node$const2, _node$in, _node$out;
  10167. switch (node.type) {
  10168. case "ExpressionStatement":
  10169. (_node$directive = node.directive) != null ? _node$directive : node.directive = undefined;
  10170. return;
  10171. case "RestElement":
  10172. node.value = undefined;
  10173. case "Identifier":
  10174. case "ArrayPattern":
  10175. case "AssignmentPattern":
  10176. case "ObjectPattern":
  10177. (_node$decorators = node.decorators) != null ? _node$decorators : node.decorators = [];
  10178. (_node$optional = node.optional) != null ? _node$optional : node.optional = false;
  10179. (_node$typeAnnotation = node.typeAnnotation) != null ? _node$typeAnnotation : node.typeAnnotation = undefined;
  10180. return;
  10181. case "TSParameterProperty":
  10182. (_node$accessibility = node.accessibility) != null ? _node$accessibility : node.accessibility = undefined;
  10183. (_node$decorators2 = node.decorators) != null ? _node$decorators2 : node.decorators = [];
  10184. (_node$override = node.override) != null ? _node$override : node.override = false;
  10185. (_node$readonly = node.readonly) != null ? _node$readonly : node.readonly = false;
  10186. (_node$static = node.static) != null ? _node$static : node.static = false;
  10187. return;
  10188. case "TSEmptyBodyFunctionExpression":
  10189. node.body = null;
  10190. case "TSDeclareFunction":
  10191. case "FunctionDeclaration":
  10192. case "FunctionExpression":
  10193. case "ClassMethod":
  10194. case "ClassPrivateMethod":
  10195. (_node$declare = node.declare) != null ? _node$declare : node.declare = false;
  10196. (_node$returnType = node.returnType) != null ? _node$returnType : node.returnType = undefined;
  10197. (_node$typeParameters = node.typeParameters) != null ? _node$typeParameters : node.typeParameters = undefined;
  10198. return;
  10199. case "Property":
  10200. (_node$optional2 = node.optional) != null ? _node$optional2 : node.optional = false;
  10201. return;
  10202. case "TSMethodSignature":
  10203. case "TSPropertySignature":
  10204. (_node$optional3 = node.optional) != null ? _node$optional3 : node.optional = false;
  10205. case "TSIndexSignature":
  10206. (_node$accessibility2 = node.accessibility) != null ? _node$accessibility2 : node.accessibility = undefined;
  10207. (_node$readonly2 = node.readonly) != null ? _node$readonly2 : node.readonly = false;
  10208. (_node$static2 = node.static) != null ? _node$static2 : node.static = false;
  10209. return;
  10210. case "TSAbstractPropertyDefinition":
  10211. case "PropertyDefinition":
  10212. case "TSAbstractAccessorProperty":
  10213. case "AccessorProperty":
  10214. (_node$declare2 = node.declare) != null ? _node$declare2 : node.declare = false;
  10215. (_node$definite = node.definite) != null ? _node$definite : node.definite = false;
  10216. (_node$readonly3 = node.readonly) != null ? _node$readonly3 : node.readonly = false;
  10217. (_node$typeAnnotation2 = node.typeAnnotation) != null ? _node$typeAnnotation2 : node.typeAnnotation = undefined;
  10218. case "TSAbstractMethodDefinition":
  10219. case "MethodDefinition":
  10220. (_node$accessibility3 = node.accessibility) != null ? _node$accessibility3 : node.accessibility = undefined;
  10221. (_node$decorators3 = node.decorators) != null ? _node$decorators3 : node.decorators = [];
  10222. (_node$override2 = node.override) != null ? _node$override2 : node.override = false;
  10223. (_node$optional4 = node.optional) != null ? _node$optional4 : node.optional = false;
  10224. return;
  10225. case "ClassExpression":
  10226. (_node$id = node.id) != null ? _node$id : node.id = null;
  10227. case "ClassDeclaration":
  10228. (_node$abstract = node.abstract) != null ? _node$abstract : node.abstract = false;
  10229. (_node$declare3 = node.declare) != null ? _node$declare3 : node.declare = false;
  10230. (_node$decorators4 = node.decorators) != null ? _node$decorators4 : node.decorators = [];
  10231. (_node$implements = node.implements) != null ? _node$implements : node.implements = [];
  10232. (_node$superTypeArgume = node.superTypeArguments) != null ? _node$superTypeArgume : node.superTypeArguments = undefined;
  10233. (_node$typeParameters2 = node.typeParameters) != null ? _node$typeParameters2 : node.typeParameters = undefined;
  10234. return;
  10235. case "TSTypeAliasDeclaration":
  10236. case "VariableDeclaration":
  10237. (_node$declare4 = node.declare) != null ? _node$declare4 : node.declare = false;
  10238. return;
  10239. case "VariableDeclarator":
  10240. (_node$definite2 = node.definite) != null ? _node$definite2 : node.definite = false;
  10241. return;
  10242. case "TSEnumDeclaration":
  10243. (_node$const = node.const) != null ? _node$const : node.const = false;
  10244. (_node$declare5 = node.declare) != null ? _node$declare5 : node.declare = false;
  10245. return;
  10246. case "TSEnumMember":
  10247. (_node$computed = node.computed) != null ? _node$computed : node.computed = false;
  10248. return;
  10249. case "TSImportType":
  10250. (_node$qualifier = node.qualifier) != null ? _node$qualifier : node.qualifier = null;
  10251. (_node$options = node.options) != null ? _node$options : node.options = null;
  10252. return;
  10253. case "TSInterfaceDeclaration":
  10254. (_node$declare6 = node.declare) != null ? _node$declare6 : node.declare = false;
  10255. (_node$extends = node.extends) != null ? _node$extends : node.extends = [];
  10256. return;
  10257. case "TSModuleDeclaration":
  10258. (_node$declare7 = node.declare) != null ? _node$declare7 : node.declare = false;
  10259. (_node$global = node.global) != null ? _node$global : node.global = node.kind === "global";
  10260. return;
  10261. case "TSTypeParameter":
  10262. (_node$const2 = node.const) != null ? _node$const2 : node.const = false;
  10263. (_node$in = node.in) != null ? _node$in : node.in = false;
  10264. (_node$out = node.out) != null ? _node$out : node.out = false;
  10265. return;
  10266. }
  10267. }
  10268. };
  10269. function isPossiblyLiteralEnum(expression) {
  10270. if (expression.type !== "MemberExpression") return false;
  10271. const {
  10272. computed,
  10273. property
  10274. } = expression;
  10275. if (computed && property.type !== "StringLiteral" && (property.type !== "TemplateLiteral" || property.expressions.length > 0)) {
  10276. return false;
  10277. }
  10278. return isUncomputedMemberExpressionChain(expression.object);
  10279. }
  10280. function isValidAmbientConstInitializer(expression, estree) {
  10281. var _expression$extra;
  10282. const {
  10283. type
  10284. } = expression;
  10285. if ((_expression$extra = expression.extra) != null && _expression$extra.parenthesized) {
  10286. return false;
  10287. }
  10288. if (estree) {
  10289. if (type === "Literal") {
  10290. const {
  10291. value
  10292. } = expression;
  10293. if (typeof value === "string" || typeof value === "boolean") {
  10294. return true;
  10295. }
  10296. }
  10297. } else {
  10298. if (type === "StringLiteral" || type === "BooleanLiteral") {
  10299. return true;
  10300. }
  10301. }
  10302. if (isNumber(expression, estree) || isNegativeNumber(expression, estree)) {
  10303. return true;
  10304. }
  10305. if (type === "TemplateLiteral" && expression.expressions.length === 0) {
  10306. return true;
  10307. }
  10308. if (isPossiblyLiteralEnum(expression)) {
  10309. return true;
  10310. }
  10311. return false;
  10312. }
  10313. function isNumber(expression, estree) {
  10314. if (estree) {
  10315. return expression.type === "Literal" && (typeof expression.value === "number" || "bigint" in expression);
  10316. }
  10317. return expression.type === "NumericLiteral" || expression.type === "BigIntLiteral";
  10318. }
  10319. function isNegativeNumber(expression, estree) {
  10320. if (expression.type === "UnaryExpression") {
  10321. const {
  10322. operator,
  10323. argument
  10324. } = expression;
  10325. if (operator === "-" && isNumber(argument, estree)) {
  10326. return true;
  10327. }
  10328. }
  10329. return false;
  10330. }
  10331. function isUncomputedMemberExpressionChain(expression) {
  10332. if (expression.type === "Identifier") return true;
  10333. if (expression.type !== "MemberExpression" || expression.computed) {
  10334. return false;
  10335. }
  10336. return isUncomputedMemberExpressionChain(expression.object);
  10337. }
  10338. const PlaceholderErrors = ParseErrorEnum`placeholders`({
  10339. ClassNameIsRequired: "A class name is required.",
  10340. UnexpectedSpace: "Unexpected space in placeholder."
  10341. });
  10342. var placeholders = superClass => class PlaceholdersParserMixin extends superClass {
  10343. parsePlaceholder(expectedNode) {
  10344. if (this.match(133)) {
  10345. const node = this.startNode();
  10346. this.next();
  10347. this.assertNoSpace();
  10348. node.name = super.parseIdentifier(true);
  10349. this.assertNoSpace();
  10350. this.expect(133);
  10351. return this.finishPlaceholder(node, expectedNode);
  10352. }
  10353. }
  10354. finishPlaceholder(node, expectedNode) {
  10355. let placeholder = node;
  10356. if (!placeholder.expectedNode || !placeholder.type) {
  10357. placeholder = this.finishNode(placeholder, "Placeholder");
  10358. }
  10359. placeholder.expectedNode = expectedNode;
  10360. return placeholder;
  10361. }
  10362. getTokenFromCode(code) {
  10363. if (code === 37 && this.input.charCodeAt(this.state.pos + 1) === 37) {
  10364. this.finishOp(133, 2);
  10365. } else {
  10366. super.getTokenFromCode(code);
  10367. }
  10368. }
  10369. parseExprAtom(refExpressionErrors) {
  10370. return this.parsePlaceholder("Expression") || super.parseExprAtom(refExpressionErrors);
  10371. }
  10372. parseIdentifier(liberal) {
  10373. return this.parsePlaceholder("Identifier") || super.parseIdentifier(liberal);
  10374. }
  10375. checkReservedWord(word, startLoc, checkKeywords, isBinding) {
  10376. if (word !== undefined) {
  10377. super.checkReservedWord(word, startLoc, checkKeywords, isBinding);
  10378. }
  10379. }
  10380. cloneIdentifier(node) {
  10381. const cloned = super.cloneIdentifier(node);
  10382. if (cloned.type === "Placeholder") {
  10383. cloned.expectedNode = node.expectedNode;
  10384. }
  10385. return cloned;
  10386. }
  10387. cloneStringLiteral(node) {
  10388. if (node.type === "Placeholder") {
  10389. return this.cloneIdentifier(node);
  10390. }
  10391. return super.cloneStringLiteral(node);
  10392. }
  10393. parseBindingAtom() {
  10394. return this.parsePlaceholder("Pattern") || super.parseBindingAtom();
  10395. }
  10396. isValidLVal(type, isParenthesized, binding) {
  10397. return type === "Placeholder" || super.isValidLVal(type, isParenthesized, binding);
  10398. }
  10399. toAssignable(node, isLHS) {
  10400. if (node && node.type === "Placeholder" && node.expectedNode === "Expression") {
  10401. node.expectedNode = "Pattern";
  10402. } else {
  10403. super.toAssignable(node, isLHS);
  10404. }
  10405. }
  10406. chStartsBindingIdentifier(ch, pos) {
  10407. if (super.chStartsBindingIdentifier(ch, pos)) {
  10408. return true;
  10409. }
  10410. const nextToken = this.lookahead();
  10411. if (nextToken.type === 133) {
  10412. return true;
  10413. }
  10414. return false;
  10415. }
  10416. verifyBreakContinue(node, isBreak) {
  10417. if (node.label && node.label.type === "Placeholder") return;
  10418. super.verifyBreakContinue(node, isBreak);
  10419. }
  10420. parseExpressionStatement(node, expr) {
  10421. var _expr$extra;
  10422. if (expr.type !== "Placeholder" || (_expr$extra = expr.extra) != null && _expr$extra.parenthesized) {
  10423. return super.parseExpressionStatement(node, expr);
  10424. }
  10425. if (this.match(14)) {
  10426. const stmt = node;
  10427. stmt.label = this.finishPlaceholder(expr, "Identifier");
  10428. this.next();
  10429. stmt.body = super.parseStatementOrSloppyAnnexBFunctionDeclaration();
  10430. return this.finishNode(stmt, "LabeledStatement");
  10431. }
  10432. this.semicolon();
  10433. const stmtPlaceholder = node;
  10434. stmtPlaceholder.name = expr.name;
  10435. return this.finishPlaceholder(stmtPlaceholder, "Statement");
  10436. }
  10437. parseBlock(allowDirectives, createNewLexicalScope, afterBlockParse) {
  10438. return this.parsePlaceholder("BlockStatement") || super.parseBlock(allowDirectives, createNewLexicalScope, afterBlockParse);
  10439. }
  10440. parseFunctionId(requireId) {
  10441. return this.parsePlaceholder("Identifier") || super.parseFunctionId(requireId);
  10442. }
  10443. parseClass(node, isStatement, optionalId) {
  10444. const type = isStatement ? "ClassDeclaration" : "ClassExpression";
  10445. this.next();
  10446. const oldStrict = this.state.strict;
  10447. const placeholder = this.parsePlaceholder("Identifier");
  10448. if (placeholder) {
  10449. if (this.match(81) || this.match(133) || this.match(5)) {
  10450. node.id = placeholder;
  10451. } else if (optionalId || !isStatement) {
  10452. node.id = null;
  10453. node.body = this.finishPlaceholder(placeholder, "ClassBody");
  10454. return this.finishNode(node, type);
  10455. } else {
  10456. throw this.raise(PlaceholderErrors.ClassNameIsRequired, this.state.startLoc);
  10457. }
  10458. } else {
  10459. this.parseClassId(node, isStatement, optionalId);
  10460. }
  10461. super.parseClassSuper(node);
  10462. node.body = this.parsePlaceholder("ClassBody") || super.parseClassBody(!!node.superClass, oldStrict);
  10463. return this.finishNode(node, type);
  10464. }
  10465. parseExport(node, decorators) {
  10466. const placeholder = this.parsePlaceholder("Identifier");
  10467. if (!placeholder) return super.parseExport(node, decorators);
  10468. const node2 = node;
  10469. if (!this.isContextual(98) && !this.match(12)) {
  10470. node2.specifiers = [];
  10471. node2.source = null;
  10472. node2.declaration = this.finishPlaceholder(placeholder, "Declaration");
  10473. return this.finishNode(node2, "ExportNamedDeclaration");
  10474. }
  10475. this.expectPlugin("exportDefaultFrom");
  10476. const specifier = this.startNode();
  10477. specifier.exported = placeholder;
  10478. node2.specifiers = [this.finishNode(specifier, "ExportDefaultSpecifier")];
  10479. return super.parseExport(node2, decorators);
  10480. }
  10481. isExportDefaultSpecifier() {
  10482. if (this.match(65)) {
  10483. const next = this.nextTokenStart();
  10484. if (this.isUnparsedContextual(next, "from")) {
  10485. if (this.input.startsWith(tokenLabelName(133), this.nextTokenStartSince(next + 4))) {
  10486. return true;
  10487. }
  10488. }
  10489. }
  10490. return super.isExportDefaultSpecifier();
  10491. }
  10492. maybeParseExportDefaultSpecifier(node, maybeDefaultIdentifier) {
  10493. var _specifiers;
  10494. if ((_specifiers = node.specifiers) != null && _specifiers.length) {
  10495. return true;
  10496. }
  10497. return super.maybeParseExportDefaultSpecifier(node, maybeDefaultIdentifier);
  10498. }
  10499. checkExport(node) {
  10500. const {
  10501. specifiers
  10502. } = node;
  10503. if (specifiers != null && specifiers.length) {
  10504. node.specifiers = specifiers.filter(node => node.exported.type === "Placeholder");
  10505. }
  10506. super.checkExport(node);
  10507. node.specifiers = specifiers;
  10508. }
  10509. parseImport(node) {
  10510. const placeholder = this.parsePlaceholder("Identifier");
  10511. if (!placeholder) return super.parseImport(node);
  10512. node.specifiers = [];
  10513. if (!this.isContextual(98) && !this.match(12)) {
  10514. node.source = this.finishPlaceholder(placeholder, "StringLiteral");
  10515. this.semicolon();
  10516. return this.finishNode(node, "ImportDeclaration");
  10517. }
  10518. const specifier = this.startNodeAtNode(placeholder);
  10519. specifier.local = placeholder;
  10520. node.specifiers.push(this.finishNode(specifier, "ImportDefaultSpecifier"));
  10521. if (this.eat(12)) {
  10522. const hasStarImport = this.maybeParseStarImportSpecifier(node);
  10523. if (!hasStarImport) this.parseNamedImportSpecifiers(node);
  10524. }
  10525. this.expectContextual(98);
  10526. node.source = this.parseImportSource();
  10527. this.semicolon();
  10528. return this.finishNode(node, "ImportDeclaration");
  10529. }
  10530. parseImportSource() {
  10531. return this.parsePlaceholder("StringLiteral") || super.parseImportSource();
  10532. }
  10533. assertNoSpace() {
  10534. if (this.state.start > this.offsetToSourcePos(this.state.lastTokEndLoc.index)) {
  10535. this.raise(PlaceholderErrors.UnexpectedSpace, this.state.lastTokEndLoc);
  10536. }
  10537. }
  10538. };
  10539. var v8intrinsic = superClass => class V8IntrinsicMixin extends superClass {
  10540. parseV8Intrinsic() {
  10541. if (this.match(54)) {
  10542. const v8IntrinsicStartLoc = this.state.startLoc;
  10543. const node = this.startNode();
  10544. this.next();
  10545. if (tokenIsIdentifier(this.state.type)) {
  10546. const name = this.parseIdentifierName();
  10547. const identifier = this.createIdentifier(node, name);
  10548. this.castNodeTo(identifier, "V8IntrinsicIdentifier");
  10549. if (this.match(10)) {
  10550. return identifier;
  10551. }
  10552. }
  10553. this.unexpected(v8IntrinsicStartLoc);
  10554. }
  10555. }
  10556. parseExprAtom(refExpressionErrors) {
  10557. return this.parseV8Intrinsic() || super.parseExprAtom(refExpressionErrors);
  10558. }
  10559. };
  10560. const PIPELINE_PROPOSALS = ["minimal", "fsharp", "hack", "smart"];
  10561. const TOPIC_TOKENS = ["^^", "@@", "^", "%", "#"];
  10562. function validatePlugins(pluginsMap) {
  10563. if (pluginsMap.has("decorators")) {
  10564. if (pluginsMap.has("decorators-legacy")) {
  10565. throw new Error("Cannot use the decorators and decorators-legacy plugin together");
  10566. }
  10567. const decoratorsBeforeExport = pluginsMap.get("decorators").decoratorsBeforeExport;
  10568. if (decoratorsBeforeExport != null && typeof decoratorsBeforeExport !== "boolean") {
  10569. throw new Error("'decoratorsBeforeExport' must be a boolean, if specified.");
  10570. }
  10571. const allowCallParenthesized = pluginsMap.get("decorators").allowCallParenthesized;
  10572. if (allowCallParenthesized != null && typeof allowCallParenthesized !== "boolean") {
  10573. throw new Error("'allowCallParenthesized' must be a boolean.");
  10574. }
  10575. }
  10576. if (pluginsMap.has("flow") && pluginsMap.has("typescript")) {
  10577. throw new Error("Cannot combine flow and typescript plugins.");
  10578. }
  10579. if (pluginsMap.has("placeholders") && pluginsMap.has("v8intrinsic")) {
  10580. throw new Error("Cannot combine placeholders and v8intrinsic plugins.");
  10581. }
  10582. if (pluginsMap.has("pipelineOperator")) {
  10583. var _pluginsMap$get2;
  10584. const proposal = pluginsMap.get("pipelineOperator").proposal;
  10585. if (!PIPELINE_PROPOSALS.includes(proposal)) {
  10586. const proposalList = PIPELINE_PROPOSALS.map(p => `"${p}"`).join(", ");
  10587. throw new Error(`"pipelineOperator" requires "proposal" option whose value must be one of: ${proposalList}.`);
  10588. }
  10589. if (proposal === "hack") {
  10590. if (pluginsMap.has("placeholders")) {
  10591. throw new Error("Cannot combine placeholders plugin and Hack-style pipes.");
  10592. }
  10593. if (pluginsMap.has("v8intrinsic")) {
  10594. throw new Error("Cannot combine v8intrinsic plugin and Hack-style pipes.");
  10595. }
  10596. const topicToken = pluginsMap.get("pipelineOperator").topicToken;
  10597. if (!TOPIC_TOKENS.includes(topicToken)) {
  10598. const tokenList = TOPIC_TOKENS.map(t => `"${t}"`).join(", ");
  10599. throw new Error(`"pipelineOperator" in "proposal": "hack" mode also requires a "topicToken" option whose value must be one of: ${tokenList}.`);
  10600. }
  10601. {
  10602. var _pluginsMap$get;
  10603. if (topicToken === "#" && ((_pluginsMap$get = pluginsMap.get("recordAndTuple")) == null ? void 0 : _pluginsMap$get.syntaxType) === "hash") {
  10604. throw new Error(`Plugin conflict between \`["pipelineOperator", { proposal: "hack", topicToken: "#" }]\` and \`${JSON.stringify(["recordAndTuple", pluginsMap.get("recordAndTuple")])}\`.`);
  10605. }
  10606. }
  10607. } else if (proposal === "smart" && ((_pluginsMap$get2 = pluginsMap.get("recordAndTuple")) == null ? void 0 : _pluginsMap$get2.syntaxType) === "hash") {
  10608. throw new Error(`Plugin conflict between \`["pipelineOperator", { proposal: "smart" }]\` and \`${JSON.stringify(["recordAndTuple", pluginsMap.get("recordAndTuple")])}\`.`);
  10609. }
  10610. }
  10611. if (pluginsMap.has("moduleAttributes")) {
  10612. {
  10613. if (pluginsMap.has("deprecatedImportAssert") || pluginsMap.has("importAssertions")) {
  10614. throw new Error("Cannot combine importAssertions, deprecatedImportAssert and moduleAttributes plugins.");
  10615. }
  10616. const moduleAttributesVersionPluginOption = pluginsMap.get("moduleAttributes").version;
  10617. if (moduleAttributesVersionPluginOption !== "may-2020") {
  10618. throw new Error("The 'moduleAttributes' plugin requires a 'version' option," + " representing the last proposal update. Currently, the" + " only supported value is 'may-2020'.");
  10619. }
  10620. }
  10621. }
  10622. if (pluginsMap.has("importAssertions")) {
  10623. if (pluginsMap.has("deprecatedImportAssert")) {
  10624. throw new Error("Cannot combine importAssertions and deprecatedImportAssert plugins.");
  10625. }
  10626. }
  10627. if (!pluginsMap.has("deprecatedImportAssert") && pluginsMap.has("importAttributes") && pluginsMap.get("importAttributes").deprecatedAssertSyntax) {
  10628. {
  10629. pluginsMap.set("deprecatedImportAssert", {});
  10630. }
  10631. }
  10632. if (pluginsMap.has("recordAndTuple")) {
  10633. {
  10634. const syntaxType = pluginsMap.get("recordAndTuple").syntaxType;
  10635. if (syntaxType != null) {
  10636. const RECORD_AND_TUPLE_SYNTAX_TYPES = ["hash", "bar"];
  10637. if (!RECORD_AND_TUPLE_SYNTAX_TYPES.includes(syntaxType)) {
  10638. throw new Error("The 'syntaxType' option of the 'recordAndTuple' plugin must be one of: " + RECORD_AND_TUPLE_SYNTAX_TYPES.map(p => `'${p}'`).join(", "));
  10639. }
  10640. }
  10641. }
  10642. }
  10643. if (pluginsMap.has("asyncDoExpressions") && !pluginsMap.has("doExpressions")) {
  10644. const error = new Error("'asyncDoExpressions' requires 'doExpressions', please add 'doExpressions' to parser plugins.");
  10645. error.missingPlugins = "doExpressions";
  10646. throw error;
  10647. }
  10648. if (pluginsMap.has("optionalChainingAssign") && pluginsMap.get("optionalChainingAssign").version !== "2023-07") {
  10649. throw new Error("The 'optionalChainingAssign' plugin requires a 'version' option," + " representing the last proposal update. Currently, the" + " only supported value is '2023-07'.");
  10650. }
  10651. }
  10652. const mixinPlugins = {
  10653. estree,
  10654. jsx,
  10655. flow,
  10656. typescript,
  10657. v8intrinsic,
  10658. placeholders
  10659. };
  10660. const mixinPluginNames = Object.keys(mixinPlugins);
  10661. class ExpressionParser extends LValParser {
  10662. checkProto(prop, isRecord, sawProto, refExpressionErrors) {
  10663. if (prop.type === "SpreadElement" || this.isObjectMethod(prop) || prop.computed || prop.shorthand) {
  10664. return sawProto;
  10665. }
  10666. const key = prop.key;
  10667. const name = key.type === "Identifier" ? key.name : key.value;
  10668. if (name === "__proto__") {
  10669. if (isRecord) {
  10670. this.raise(Errors.RecordNoProto, key);
  10671. return true;
  10672. }
  10673. if (sawProto) {
  10674. if (refExpressionErrors) {
  10675. if (refExpressionErrors.doubleProtoLoc === null) {
  10676. refExpressionErrors.doubleProtoLoc = key.loc.start;
  10677. }
  10678. } else {
  10679. this.raise(Errors.DuplicateProto, key);
  10680. }
  10681. }
  10682. return true;
  10683. }
  10684. return sawProto;
  10685. }
  10686. shouldExitDescending(expr, potentialArrowAt) {
  10687. return expr.type === "ArrowFunctionExpression" && this.offsetToSourcePos(expr.start) === potentialArrowAt;
  10688. }
  10689. getExpression() {
  10690. this.enterInitialScopes();
  10691. this.nextToken();
  10692. if (this.match(140)) {
  10693. throw this.raise(Errors.ParseExpressionEmptyInput, this.state.startLoc);
  10694. }
  10695. const expr = this.parseExpression();
  10696. if (!this.match(140)) {
  10697. throw this.raise(Errors.ParseExpressionExpectsEOF, this.state.startLoc, {
  10698. unexpected: this.input.codePointAt(this.state.start)
  10699. });
  10700. }
  10701. this.finalizeRemainingComments();
  10702. expr.comments = this.comments;
  10703. expr.errors = this.state.errors;
  10704. if (this.optionFlags & 256) {
  10705. expr.tokens = this.tokens;
  10706. }
  10707. return expr;
  10708. }
  10709. parseExpression(disallowIn, refExpressionErrors) {
  10710. if (disallowIn) {
  10711. return this.disallowInAnd(() => this.parseExpressionBase(refExpressionErrors));
  10712. }
  10713. return this.allowInAnd(() => this.parseExpressionBase(refExpressionErrors));
  10714. }
  10715. parseExpressionBase(refExpressionErrors) {
  10716. const startLoc = this.state.startLoc;
  10717. const expr = this.parseMaybeAssign(refExpressionErrors);
  10718. if (this.match(12)) {
  10719. const node = this.startNodeAt(startLoc);
  10720. node.expressions = [expr];
  10721. while (this.eat(12)) {
  10722. node.expressions.push(this.parseMaybeAssign(refExpressionErrors));
  10723. }
  10724. this.toReferencedList(node.expressions);
  10725. return this.finishNode(node, "SequenceExpression");
  10726. }
  10727. return expr;
  10728. }
  10729. parseMaybeAssignDisallowIn(refExpressionErrors, afterLeftParse) {
  10730. return this.disallowInAnd(() => this.parseMaybeAssign(refExpressionErrors, afterLeftParse));
  10731. }
  10732. parseMaybeAssignAllowIn(refExpressionErrors, afterLeftParse) {
  10733. return this.allowInAnd(() => this.parseMaybeAssign(refExpressionErrors, afterLeftParse));
  10734. }
  10735. setOptionalParametersError(refExpressionErrors) {
  10736. refExpressionErrors.optionalParametersLoc = this.state.startLoc;
  10737. }
  10738. parseMaybeAssign(refExpressionErrors, afterLeftParse) {
  10739. const startLoc = this.state.startLoc;
  10740. const isYield = this.isContextual(108);
  10741. if (isYield) {
  10742. if (this.prodParam.hasYield) {
  10743. this.next();
  10744. let left = this.parseYield(startLoc);
  10745. if (afterLeftParse) {
  10746. left = afterLeftParse.call(this, left, startLoc);
  10747. }
  10748. return left;
  10749. }
  10750. }
  10751. let ownExpressionErrors;
  10752. if (refExpressionErrors) {
  10753. ownExpressionErrors = false;
  10754. } else {
  10755. refExpressionErrors = new ExpressionErrors();
  10756. ownExpressionErrors = true;
  10757. }
  10758. const {
  10759. type
  10760. } = this.state;
  10761. if (type === 10 || tokenIsIdentifier(type)) {
  10762. this.state.potentialArrowAt = this.state.start;
  10763. }
  10764. let left = this.parseMaybeConditional(refExpressionErrors);
  10765. if (afterLeftParse) {
  10766. left = afterLeftParse.call(this, left, startLoc);
  10767. }
  10768. if (tokenIsAssignment(this.state.type)) {
  10769. const node = this.startNodeAt(startLoc);
  10770. const operator = this.state.value;
  10771. node.operator = operator;
  10772. if (this.match(29)) {
  10773. this.toAssignable(left, true);
  10774. node.left = left;
  10775. const startIndex = startLoc.index;
  10776. if (refExpressionErrors.doubleProtoLoc != null && refExpressionErrors.doubleProtoLoc.index >= startIndex) {
  10777. refExpressionErrors.doubleProtoLoc = null;
  10778. }
  10779. if (refExpressionErrors.shorthandAssignLoc != null && refExpressionErrors.shorthandAssignLoc.index >= startIndex) {
  10780. refExpressionErrors.shorthandAssignLoc = null;
  10781. }
  10782. if (refExpressionErrors.privateKeyLoc != null && refExpressionErrors.privateKeyLoc.index >= startIndex) {
  10783. this.checkDestructuringPrivate(refExpressionErrors);
  10784. refExpressionErrors.privateKeyLoc = null;
  10785. }
  10786. } else {
  10787. node.left = left;
  10788. }
  10789. this.next();
  10790. node.right = this.parseMaybeAssign();
  10791. this.checkLVal(left, this.finishNode(node, "AssignmentExpression"));
  10792. return node;
  10793. } else if (ownExpressionErrors) {
  10794. this.checkExpressionErrors(refExpressionErrors, true);
  10795. }
  10796. if (isYield) {
  10797. const {
  10798. type
  10799. } = this.state;
  10800. const startsExpr = this.hasPlugin("v8intrinsic") ? tokenCanStartExpression(type) : tokenCanStartExpression(type) && !this.match(54);
  10801. if (startsExpr && !this.isAmbiguousPrefixOrIdentifier()) {
  10802. this.raiseOverwrite(Errors.YieldNotInGeneratorFunction, startLoc);
  10803. return this.parseYield(startLoc);
  10804. }
  10805. }
  10806. return left;
  10807. }
  10808. parseMaybeConditional(refExpressionErrors) {
  10809. const startLoc = this.state.startLoc;
  10810. const potentialArrowAt = this.state.potentialArrowAt;
  10811. const expr = this.parseExprOps(refExpressionErrors);
  10812. if (this.shouldExitDescending(expr, potentialArrowAt)) {
  10813. return expr;
  10814. }
  10815. return this.parseConditional(expr, startLoc, refExpressionErrors);
  10816. }
  10817. parseConditional(expr, startLoc, refExpressionErrors) {
  10818. if (this.eat(17)) {
  10819. const node = this.startNodeAt(startLoc);
  10820. node.test = expr;
  10821. node.consequent = this.parseMaybeAssignAllowIn();
  10822. this.expect(14);
  10823. node.alternate = this.parseMaybeAssign();
  10824. return this.finishNode(node, "ConditionalExpression");
  10825. }
  10826. return expr;
  10827. }
  10828. parseMaybeUnaryOrPrivate(refExpressionErrors) {
  10829. return this.match(139) ? this.parsePrivateName() : this.parseMaybeUnary(refExpressionErrors);
  10830. }
  10831. parseExprOps(refExpressionErrors) {
  10832. const startLoc = this.state.startLoc;
  10833. const potentialArrowAt = this.state.potentialArrowAt;
  10834. const expr = this.parseMaybeUnaryOrPrivate(refExpressionErrors);
  10835. if (this.shouldExitDescending(expr, potentialArrowAt)) {
  10836. return expr;
  10837. }
  10838. return this.parseExprOp(expr, startLoc, -1);
  10839. }
  10840. parseExprOp(left, leftStartLoc, minPrec) {
  10841. if (this.isPrivateName(left)) {
  10842. const value = this.getPrivateNameSV(left);
  10843. if (minPrec >= tokenOperatorPrecedence(58) || !this.prodParam.hasIn || !this.match(58)) {
  10844. this.raise(Errors.PrivateInExpectedIn, left, {
  10845. identifierName: value
  10846. });
  10847. }
  10848. this.classScope.usePrivateName(value, left.loc.start);
  10849. }
  10850. const op = this.state.type;
  10851. if (tokenIsOperator(op) && (this.prodParam.hasIn || !this.match(58))) {
  10852. let prec = tokenOperatorPrecedence(op);
  10853. if (prec > minPrec) {
  10854. if (op === 39) {
  10855. this.expectPlugin("pipelineOperator");
  10856. if (this.state.inFSharpPipelineDirectBody) {
  10857. return left;
  10858. }
  10859. this.checkPipelineAtInfixOperator(left, leftStartLoc);
  10860. }
  10861. const node = this.startNodeAt(leftStartLoc);
  10862. node.left = left;
  10863. node.operator = this.state.value;
  10864. const logical = op === 41 || op === 42;
  10865. const coalesce = op === 40;
  10866. if (coalesce) {
  10867. prec = tokenOperatorPrecedence(42);
  10868. }
  10869. this.next();
  10870. if (op === 39 && this.hasPlugin(["pipelineOperator", {
  10871. proposal: "minimal"
  10872. }])) {
  10873. if (this.state.type === 96 && this.prodParam.hasAwait) {
  10874. throw this.raise(Errors.UnexpectedAwaitAfterPipelineBody, this.state.startLoc);
  10875. }
  10876. }
  10877. node.right = this.parseExprOpRightExpr(op, prec);
  10878. const finishedNode = this.finishNode(node, logical || coalesce ? "LogicalExpression" : "BinaryExpression");
  10879. const nextOp = this.state.type;
  10880. if (coalesce && (nextOp === 41 || nextOp === 42) || logical && nextOp === 40) {
  10881. throw this.raise(Errors.MixingCoalesceWithLogical, this.state.startLoc);
  10882. }
  10883. return this.parseExprOp(finishedNode, leftStartLoc, minPrec);
  10884. }
  10885. }
  10886. return left;
  10887. }
  10888. parseExprOpRightExpr(op, prec) {
  10889. const startLoc = this.state.startLoc;
  10890. switch (op) {
  10891. case 39:
  10892. switch (this.getPluginOption("pipelineOperator", "proposal")) {
  10893. case "hack":
  10894. return this.withTopicBindingContext(() => {
  10895. return this.parseHackPipeBody();
  10896. });
  10897. case "fsharp":
  10898. return this.withSoloAwaitPermittingContext(() => {
  10899. return this.parseFSharpPipelineBody(prec);
  10900. });
  10901. }
  10902. if (this.getPluginOption("pipelineOperator", "proposal") === "smart") {
  10903. return this.withTopicBindingContext(() => {
  10904. if (this.prodParam.hasYield && this.isContextual(108)) {
  10905. throw this.raise(Errors.PipeBodyIsTighter, this.state.startLoc);
  10906. }
  10907. return this.parseSmartPipelineBodyInStyle(this.parseExprOpBaseRightExpr(op, prec), startLoc);
  10908. });
  10909. }
  10910. default:
  10911. return this.parseExprOpBaseRightExpr(op, prec);
  10912. }
  10913. }
  10914. parseExprOpBaseRightExpr(op, prec) {
  10915. const startLoc = this.state.startLoc;
  10916. return this.parseExprOp(this.parseMaybeUnaryOrPrivate(), startLoc, tokenIsRightAssociative(op) ? prec - 1 : prec);
  10917. }
  10918. parseHackPipeBody() {
  10919. var _body$extra;
  10920. const {
  10921. startLoc
  10922. } = this.state;
  10923. const body = this.parseMaybeAssign();
  10924. const requiredParentheses = UnparenthesizedPipeBodyDescriptions.has(body.type);
  10925. if (requiredParentheses && !((_body$extra = body.extra) != null && _body$extra.parenthesized)) {
  10926. this.raise(Errors.PipeUnparenthesizedBody, startLoc, {
  10927. type: body.type
  10928. });
  10929. }
  10930. if (!this.topicReferenceWasUsedInCurrentContext()) {
  10931. this.raise(Errors.PipeTopicUnused, startLoc);
  10932. }
  10933. return body;
  10934. }
  10935. checkExponentialAfterUnary(node) {
  10936. if (this.match(57)) {
  10937. this.raise(Errors.UnexpectedTokenUnaryExponentiation, node.argument);
  10938. }
  10939. }
  10940. parseMaybeUnary(refExpressionErrors, sawUnary) {
  10941. const startLoc = this.state.startLoc;
  10942. const isAwait = this.isContextual(96);
  10943. if (isAwait && this.recordAwaitIfAllowed()) {
  10944. this.next();
  10945. const expr = this.parseAwait(startLoc);
  10946. if (!sawUnary) this.checkExponentialAfterUnary(expr);
  10947. return expr;
  10948. }
  10949. const update = this.match(34);
  10950. const node = this.startNode();
  10951. if (tokenIsPrefix(this.state.type)) {
  10952. node.operator = this.state.value;
  10953. node.prefix = true;
  10954. if (this.match(72)) {
  10955. this.expectPlugin("throwExpressions");
  10956. }
  10957. const isDelete = this.match(89);
  10958. this.next();
  10959. node.argument = this.parseMaybeUnary(null, true);
  10960. this.checkExpressionErrors(refExpressionErrors, true);
  10961. if (this.state.strict && isDelete) {
  10962. const arg = node.argument;
  10963. if (arg.type === "Identifier") {
  10964. this.raise(Errors.StrictDelete, node);
  10965. } else if (this.hasPropertyAsPrivateName(arg)) {
  10966. this.raise(Errors.DeletePrivateField, node);
  10967. }
  10968. }
  10969. if (!update) {
  10970. if (!sawUnary) {
  10971. this.checkExponentialAfterUnary(node);
  10972. }
  10973. return this.finishNode(node, "UnaryExpression");
  10974. }
  10975. }
  10976. const expr = this.parseUpdate(node, update, refExpressionErrors);
  10977. if (isAwait) {
  10978. const {
  10979. type
  10980. } = this.state;
  10981. const startsExpr = this.hasPlugin("v8intrinsic") ? tokenCanStartExpression(type) : tokenCanStartExpression(type) && !this.match(54);
  10982. if (startsExpr && !this.isAmbiguousPrefixOrIdentifier()) {
  10983. this.raiseOverwrite(Errors.AwaitNotInAsyncContext, startLoc);
  10984. return this.parseAwait(startLoc);
  10985. }
  10986. }
  10987. return expr;
  10988. }
  10989. parseUpdate(node, update, refExpressionErrors) {
  10990. if (update) {
  10991. const updateExpressionNode = node;
  10992. this.checkLVal(updateExpressionNode.argument, this.finishNode(updateExpressionNode, "UpdateExpression"));
  10993. return node;
  10994. }
  10995. const startLoc = this.state.startLoc;
  10996. let expr = this.parseExprSubscripts(refExpressionErrors);
  10997. if (this.checkExpressionErrors(refExpressionErrors, false)) return expr;
  10998. while (tokenIsPostfix(this.state.type) && !this.canInsertSemicolon()) {
  10999. const node = this.startNodeAt(startLoc);
  11000. node.operator = this.state.value;
  11001. node.prefix = false;
  11002. node.argument = expr;
  11003. this.next();
  11004. this.checkLVal(expr, expr = this.finishNode(node, "UpdateExpression"));
  11005. }
  11006. return expr;
  11007. }
  11008. parseExprSubscripts(refExpressionErrors) {
  11009. const startLoc = this.state.startLoc;
  11010. const potentialArrowAt = this.state.potentialArrowAt;
  11011. const expr = this.parseExprAtom(refExpressionErrors);
  11012. if (this.shouldExitDescending(expr, potentialArrowAt)) {
  11013. return expr;
  11014. }
  11015. return this.parseSubscripts(expr, startLoc);
  11016. }
  11017. parseSubscripts(base, startLoc, noCalls) {
  11018. const state = {
  11019. optionalChainMember: false,
  11020. maybeAsyncArrow: this.atPossibleAsyncArrow(base),
  11021. stop: false
  11022. };
  11023. do {
  11024. base = this.parseSubscript(base, startLoc, noCalls, state);
  11025. state.maybeAsyncArrow = false;
  11026. } while (!state.stop);
  11027. return base;
  11028. }
  11029. parseSubscript(base, startLoc, noCalls, state) {
  11030. const {
  11031. type
  11032. } = this.state;
  11033. if (!noCalls && type === 15) {
  11034. return this.parseBind(base, startLoc, noCalls, state);
  11035. } else if (tokenIsTemplate(type)) {
  11036. return this.parseTaggedTemplateExpression(base, startLoc, state);
  11037. }
  11038. let optional = false;
  11039. if (type === 18) {
  11040. if (noCalls) {
  11041. this.raise(Errors.OptionalChainingNoNew, this.state.startLoc);
  11042. if (this.lookaheadCharCode() === 40) {
  11043. return this.stopParseSubscript(base, state);
  11044. }
  11045. }
  11046. state.optionalChainMember = optional = true;
  11047. this.next();
  11048. }
  11049. if (!noCalls && this.match(10)) {
  11050. return this.parseCoverCallAndAsyncArrowHead(base, startLoc, state, optional);
  11051. } else {
  11052. const computed = this.eat(0);
  11053. if (computed || optional || this.eat(16)) {
  11054. return this.parseMember(base, startLoc, state, computed, optional);
  11055. } else {
  11056. return this.stopParseSubscript(base, state);
  11057. }
  11058. }
  11059. }
  11060. stopParseSubscript(base, state) {
  11061. state.stop = true;
  11062. return base;
  11063. }
  11064. parseMember(base, startLoc, state, computed, optional) {
  11065. const node = this.startNodeAt(startLoc);
  11066. node.object = base;
  11067. node.computed = computed;
  11068. if (computed) {
  11069. node.property = this.parseExpression();
  11070. this.expect(3);
  11071. } else if (this.match(139)) {
  11072. if (base.type === "Super") {
  11073. this.raise(Errors.SuperPrivateField, startLoc);
  11074. }
  11075. this.classScope.usePrivateName(this.state.value, this.state.startLoc);
  11076. node.property = this.parsePrivateName();
  11077. } else {
  11078. node.property = this.parseIdentifier(true);
  11079. }
  11080. if (state.optionalChainMember) {
  11081. node.optional = optional;
  11082. return this.finishNode(node, "OptionalMemberExpression");
  11083. } else {
  11084. return this.finishNode(node, "MemberExpression");
  11085. }
  11086. }
  11087. parseBind(base, startLoc, noCalls, state) {
  11088. const node = this.startNodeAt(startLoc);
  11089. node.object = base;
  11090. this.next();
  11091. node.callee = this.parseNoCallExpr();
  11092. state.stop = true;
  11093. return this.parseSubscripts(this.finishNode(node, "BindExpression"), startLoc, noCalls);
  11094. }
  11095. parseCoverCallAndAsyncArrowHead(base, startLoc, state, optional) {
  11096. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  11097. let refExpressionErrors = null;
  11098. this.state.maybeInArrowParameters = true;
  11099. this.next();
  11100. const node = this.startNodeAt(startLoc);
  11101. node.callee = base;
  11102. const {
  11103. maybeAsyncArrow,
  11104. optionalChainMember
  11105. } = state;
  11106. if (maybeAsyncArrow) {
  11107. this.expressionScope.enter(newAsyncArrowScope());
  11108. refExpressionErrors = new ExpressionErrors();
  11109. }
  11110. if (optionalChainMember) {
  11111. node.optional = optional;
  11112. }
  11113. if (optional) {
  11114. node.arguments = this.parseCallExpressionArguments(11);
  11115. } else {
  11116. node.arguments = this.parseCallExpressionArguments(11, base.type !== "Super", node, refExpressionErrors);
  11117. }
  11118. let finishedNode = this.finishCallExpression(node, optionalChainMember);
  11119. if (maybeAsyncArrow && this.shouldParseAsyncArrow() && !optional) {
  11120. state.stop = true;
  11121. this.checkDestructuringPrivate(refExpressionErrors);
  11122. this.expressionScope.validateAsPattern();
  11123. this.expressionScope.exit();
  11124. finishedNode = this.parseAsyncArrowFromCallExpression(this.startNodeAt(startLoc), finishedNode);
  11125. } else {
  11126. if (maybeAsyncArrow) {
  11127. this.checkExpressionErrors(refExpressionErrors, true);
  11128. this.expressionScope.exit();
  11129. }
  11130. this.toReferencedArguments(finishedNode);
  11131. }
  11132. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  11133. return finishedNode;
  11134. }
  11135. toReferencedArguments(node, isParenthesizedExpr) {
  11136. this.toReferencedListDeep(node.arguments, isParenthesizedExpr);
  11137. }
  11138. parseTaggedTemplateExpression(base, startLoc, state) {
  11139. const node = this.startNodeAt(startLoc);
  11140. node.tag = base;
  11141. node.quasi = this.parseTemplate(true);
  11142. if (state.optionalChainMember) {
  11143. this.raise(Errors.OptionalChainingNoTemplate, startLoc);
  11144. }
  11145. return this.finishNode(node, "TaggedTemplateExpression");
  11146. }
  11147. atPossibleAsyncArrow(base) {
  11148. return base.type === "Identifier" && base.name === "async" && this.state.lastTokEndLoc.index === base.end && !this.canInsertSemicolon() && base.end - base.start === 5 && this.offsetToSourcePos(base.start) === this.state.potentialArrowAt;
  11149. }
  11150. finishCallExpression(node, optional) {
  11151. if (node.callee.type === "Import") {
  11152. if (node.arguments.length === 0 || node.arguments.length > 2) {
  11153. this.raise(Errors.ImportCallArity, node);
  11154. } else {
  11155. for (const arg of node.arguments) {
  11156. if (arg.type === "SpreadElement") {
  11157. this.raise(Errors.ImportCallSpreadArgument, arg);
  11158. }
  11159. }
  11160. }
  11161. }
  11162. return this.finishNode(node, optional ? "OptionalCallExpression" : "CallExpression");
  11163. }
  11164. parseCallExpressionArguments(close, allowPlaceholder, nodeForExtra, refExpressionErrors) {
  11165. const elts = [];
  11166. let first = true;
  11167. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  11168. this.state.inFSharpPipelineDirectBody = false;
  11169. while (!this.eat(close)) {
  11170. if (first) {
  11171. first = false;
  11172. } else {
  11173. this.expect(12);
  11174. if (this.match(close)) {
  11175. if (nodeForExtra) {
  11176. this.addTrailingCommaExtraToNode(nodeForExtra);
  11177. }
  11178. this.next();
  11179. break;
  11180. }
  11181. }
  11182. elts.push(this.parseExprListItem(false, refExpressionErrors, allowPlaceholder));
  11183. }
  11184. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  11185. return elts;
  11186. }
  11187. shouldParseAsyncArrow() {
  11188. return this.match(19) && !this.canInsertSemicolon();
  11189. }
  11190. parseAsyncArrowFromCallExpression(node, call) {
  11191. var _call$extra;
  11192. this.resetPreviousNodeTrailingComments(call);
  11193. this.expect(19);
  11194. this.parseArrowExpression(node, call.arguments, true, (_call$extra = call.extra) == null ? void 0 : _call$extra.trailingCommaLoc);
  11195. if (call.innerComments) {
  11196. setInnerComments(node, call.innerComments);
  11197. }
  11198. if (call.callee.trailingComments) {
  11199. setInnerComments(node, call.callee.trailingComments);
  11200. }
  11201. return node;
  11202. }
  11203. parseNoCallExpr() {
  11204. const startLoc = this.state.startLoc;
  11205. return this.parseSubscripts(this.parseExprAtom(), startLoc, true);
  11206. }
  11207. parseExprAtom(refExpressionErrors) {
  11208. let node;
  11209. let decorators = null;
  11210. const {
  11211. type
  11212. } = this.state;
  11213. switch (type) {
  11214. case 79:
  11215. return this.parseSuper();
  11216. case 83:
  11217. node = this.startNode();
  11218. this.next();
  11219. if (this.match(16)) {
  11220. return this.parseImportMetaPropertyOrPhaseCall(node);
  11221. }
  11222. if (this.match(10)) {
  11223. if (this.optionFlags & 512) {
  11224. return this.parseImportCall(node);
  11225. } else {
  11226. return this.finishNode(node, "Import");
  11227. }
  11228. } else {
  11229. this.raise(Errors.UnsupportedImport, this.state.lastTokStartLoc);
  11230. return this.finishNode(node, "Import");
  11231. }
  11232. case 78:
  11233. node = this.startNode();
  11234. this.next();
  11235. return this.finishNode(node, "ThisExpression");
  11236. case 90:
  11237. {
  11238. return this.parseDo(this.startNode(), false);
  11239. }
  11240. case 56:
  11241. case 31:
  11242. {
  11243. this.readRegexp();
  11244. return this.parseRegExpLiteral(this.state.value);
  11245. }
  11246. case 135:
  11247. return this.parseNumericLiteral(this.state.value);
  11248. case 136:
  11249. return this.parseBigIntLiteral(this.state.value);
  11250. case 134:
  11251. return this.parseStringLiteral(this.state.value);
  11252. case 84:
  11253. return this.parseNullLiteral();
  11254. case 85:
  11255. return this.parseBooleanLiteral(true);
  11256. case 86:
  11257. return this.parseBooleanLiteral(false);
  11258. case 10:
  11259. {
  11260. const canBeArrow = this.state.potentialArrowAt === this.state.start;
  11261. return this.parseParenAndDistinguishExpression(canBeArrow);
  11262. }
  11263. case 0:
  11264. {
  11265. return this.parseArrayLike(3, true, false, refExpressionErrors);
  11266. }
  11267. case 5:
  11268. {
  11269. return this.parseObjectLike(8, false, false, refExpressionErrors);
  11270. }
  11271. case 68:
  11272. return this.parseFunctionOrFunctionSent();
  11273. case 26:
  11274. decorators = this.parseDecorators();
  11275. case 80:
  11276. return this.parseClass(this.maybeTakeDecorators(decorators, this.startNode()), false);
  11277. case 77:
  11278. return this.parseNewOrNewTarget();
  11279. case 25:
  11280. case 24:
  11281. return this.parseTemplate(false);
  11282. case 15:
  11283. {
  11284. node = this.startNode();
  11285. this.next();
  11286. node.object = null;
  11287. const callee = node.callee = this.parseNoCallExpr();
  11288. if (callee.type === "MemberExpression") {
  11289. return this.finishNode(node, "BindExpression");
  11290. } else {
  11291. throw this.raise(Errors.UnsupportedBind, callee);
  11292. }
  11293. }
  11294. case 139:
  11295. {
  11296. this.raise(Errors.PrivateInExpectedIn, this.state.startLoc, {
  11297. identifierName: this.state.value
  11298. });
  11299. return this.parsePrivateName();
  11300. }
  11301. case 33:
  11302. {
  11303. return this.parseTopicReferenceThenEqualsSign(54, "%");
  11304. }
  11305. case 32:
  11306. {
  11307. return this.parseTopicReferenceThenEqualsSign(44, "^");
  11308. }
  11309. case 37:
  11310. case 38:
  11311. {
  11312. return this.parseTopicReference("hack");
  11313. }
  11314. case 44:
  11315. case 54:
  11316. case 27:
  11317. {
  11318. const pipeProposal = this.getPluginOption("pipelineOperator", "proposal");
  11319. if (pipeProposal) {
  11320. return this.parseTopicReference(pipeProposal);
  11321. }
  11322. this.unexpected();
  11323. break;
  11324. }
  11325. case 47:
  11326. {
  11327. const lookaheadCh = this.input.codePointAt(this.nextTokenStart());
  11328. if (isIdentifierStart(lookaheadCh) || lookaheadCh === 62) {
  11329. this.expectOnePlugin(["jsx", "flow", "typescript"]);
  11330. } else {
  11331. this.unexpected();
  11332. }
  11333. break;
  11334. }
  11335. default:
  11336. {
  11337. if (type === 137) {
  11338. return this.parseDecimalLiteral(this.state.value);
  11339. } else if (type === 2 || type === 1) {
  11340. return this.parseArrayLike(this.state.type === 2 ? 4 : 3, false, true);
  11341. } else if (type === 6 || type === 7) {
  11342. return this.parseObjectLike(this.state.type === 6 ? 9 : 8, false, true);
  11343. }
  11344. }
  11345. if (tokenIsIdentifier(type)) {
  11346. if (this.isContextual(127) && this.lookaheadInLineCharCode() === 123) {
  11347. return this.parseModuleExpression();
  11348. }
  11349. const canBeArrow = this.state.potentialArrowAt === this.state.start;
  11350. const containsEsc = this.state.containsEsc;
  11351. const id = this.parseIdentifier();
  11352. if (!containsEsc && id.name === "async" && !this.canInsertSemicolon()) {
  11353. const {
  11354. type
  11355. } = this.state;
  11356. if (type === 68) {
  11357. this.resetPreviousNodeTrailingComments(id);
  11358. this.next();
  11359. return this.parseAsyncFunctionExpression(this.startNodeAtNode(id));
  11360. } else if (tokenIsIdentifier(type)) {
  11361. if (this.lookaheadCharCode() === 61) {
  11362. return this.parseAsyncArrowUnaryFunction(this.startNodeAtNode(id));
  11363. } else {
  11364. return id;
  11365. }
  11366. } else if (type === 90) {
  11367. this.resetPreviousNodeTrailingComments(id);
  11368. return this.parseDo(this.startNodeAtNode(id), true);
  11369. }
  11370. }
  11371. if (canBeArrow && this.match(19) && !this.canInsertSemicolon()) {
  11372. this.next();
  11373. return this.parseArrowExpression(this.startNodeAtNode(id), [id], false);
  11374. }
  11375. return id;
  11376. } else {
  11377. this.unexpected();
  11378. }
  11379. }
  11380. }
  11381. parseTopicReferenceThenEqualsSign(topicTokenType, topicTokenValue) {
  11382. const pipeProposal = this.getPluginOption("pipelineOperator", "proposal");
  11383. if (pipeProposal) {
  11384. this.state.type = topicTokenType;
  11385. this.state.value = topicTokenValue;
  11386. this.state.pos--;
  11387. this.state.end--;
  11388. this.state.endLoc = createPositionWithColumnOffset(this.state.endLoc, -1);
  11389. return this.parseTopicReference(pipeProposal);
  11390. } else {
  11391. this.unexpected();
  11392. }
  11393. }
  11394. parseTopicReference(pipeProposal) {
  11395. const node = this.startNode();
  11396. const startLoc = this.state.startLoc;
  11397. const tokenType = this.state.type;
  11398. this.next();
  11399. return this.finishTopicReference(node, startLoc, pipeProposal, tokenType);
  11400. }
  11401. finishTopicReference(node, startLoc, pipeProposal, tokenType) {
  11402. if (this.testTopicReferenceConfiguration(pipeProposal, startLoc, tokenType)) {
  11403. if (pipeProposal === "hack") {
  11404. if (!this.topicReferenceIsAllowedInCurrentContext()) {
  11405. this.raise(Errors.PipeTopicUnbound, startLoc);
  11406. }
  11407. this.registerTopicReference();
  11408. return this.finishNode(node, "TopicReference");
  11409. } else {
  11410. if (!this.topicReferenceIsAllowedInCurrentContext()) {
  11411. this.raise(Errors.PrimaryTopicNotAllowed, startLoc);
  11412. }
  11413. this.registerTopicReference();
  11414. return this.finishNode(node, "PipelinePrimaryTopicReference");
  11415. }
  11416. } else {
  11417. throw this.raise(Errors.PipeTopicUnconfiguredToken, startLoc, {
  11418. token: tokenLabelName(tokenType)
  11419. });
  11420. }
  11421. }
  11422. testTopicReferenceConfiguration(pipeProposal, startLoc, tokenType) {
  11423. switch (pipeProposal) {
  11424. case "hack":
  11425. {
  11426. return this.hasPlugin(["pipelineOperator", {
  11427. topicToken: tokenLabelName(tokenType)
  11428. }]);
  11429. }
  11430. case "smart":
  11431. return tokenType === 27;
  11432. default:
  11433. throw this.raise(Errors.PipeTopicRequiresHackPipes, startLoc);
  11434. }
  11435. }
  11436. parseAsyncArrowUnaryFunction(node) {
  11437. this.prodParam.enter(functionFlags(true, this.prodParam.hasYield));
  11438. const params = [this.parseIdentifier()];
  11439. this.prodParam.exit();
  11440. if (this.hasPrecedingLineBreak()) {
  11441. this.raise(Errors.LineTerminatorBeforeArrow, this.state.curPosition());
  11442. }
  11443. this.expect(19);
  11444. return this.parseArrowExpression(node, params, true);
  11445. }
  11446. parseDo(node, isAsync) {
  11447. this.expectPlugin("doExpressions");
  11448. if (isAsync) {
  11449. this.expectPlugin("asyncDoExpressions");
  11450. }
  11451. node.async = isAsync;
  11452. this.next();
  11453. const oldLabels = this.state.labels;
  11454. this.state.labels = [];
  11455. if (isAsync) {
  11456. this.prodParam.enter(2);
  11457. node.body = this.parseBlock();
  11458. this.prodParam.exit();
  11459. } else {
  11460. node.body = this.parseBlock();
  11461. }
  11462. this.state.labels = oldLabels;
  11463. return this.finishNode(node, "DoExpression");
  11464. }
  11465. parseSuper() {
  11466. const node = this.startNode();
  11467. this.next();
  11468. if (this.match(10) && !this.scope.allowDirectSuper && !(this.optionFlags & 16)) {
  11469. this.raise(Errors.SuperNotAllowed, node);
  11470. } else if (!this.scope.allowSuper && !(this.optionFlags & 16)) {
  11471. this.raise(Errors.UnexpectedSuper, node);
  11472. }
  11473. if (!this.match(10) && !this.match(0) && !this.match(16)) {
  11474. this.raise(Errors.UnsupportedSuper, node);
  11475. }
  11476. return this.finishNode(node, "Super");
  11477. }
  11478. parsePrivateName() {
  11479. const node = this.startNode();
  11480. const id = this.startNodeAt(createPositionWithColumnOffset(this.state.startLoc, 1));
  11481. const name = this.state.value;
  11482. this.next();
  11483. node.id = this.createIdentifier(id, name);
  11484. return this.finishNode(node, "PrivateName");
  11485. }
  11486. parseFunctionOrFunctionSent() {
  11487. const node = this.startNode();
  11488. this.next();
  11489. if (this.prodParam.hasYield && this.match(16)) {
  11490. const meta = this.createIdentifier(this.startNodeAtNode(node), "function");
  11491. this.next();
  11492. if (this.match(103)) {
  11493. this.expectPlugin("functionSent");
  11494. } else if (!this.hasPlugin("functionSent")) {
  11495. this.unexpected();
  11496. }
  11497. return this.parseMetaProperty(node, meta, "sent");
  11498. }
  11499. return this.parseFunction(node);
  11500. }
  11501. parseMetaProperty(node, meta, propertyName) {
  11502. node.meta = meta;
  11503. const containsEsc = this.state.containsEsc;
  11504. node.property = this.parseIdentifier(true);
  11505. if (node.property.name !== propertyName || containsEsc) {
  11506. this.raise(Errors.UnsupportedMetaProperty, node.property, {
  11507. target: meta.name,
  11508. onlyValidPropertyName: propertyName
  11509. });
  11510. }
  11511. return this.finishNode(node, "MetaProperty");
  11512. }
  11513. parseImportMetaPropertyOrPhaseCall(node) {
  11514. this.next();
  11515. if (this.isContextual(105) || this.isContextual(97)) {
  11516. const isSource = this.isContextual(105);
  11517. this.expectPlugin(isSource ? "sourcePhaseImports" : "deferredImportEvaluation");
  11518. this.next();
  11519. node.phase = isSource ? "source" : "defer";
  11520. return this.parseImportCall(node);
  11521. } else {
  11522. const id = this.createIdentifierAt(this.startNodeAtNode(node), "import", this.state.lastTokStartLoc);
  11523. if (this.isContextual(101)) {
  11524. if (!this.inModule) {
  11525. this.raise(Errors.ImportMetaOutsideModule, id);
  11526. }
  11527. this.sawUnambiguousESM = true;
  11528. }
  11529. return this.parseMetaProperty(node, id, "meta");
  11530. }
  11531. }
  11532. parseLiteralAtNode(value, type, node) {
  11533. this.addExtra(node, "rawValue", value);
  11534. this.addExtra(node, "raw", this.input.slice(this.offsetToSourcePos(node.start), this.state.end));
  11535. node.value = value;
  11536. this.next();
  11537. return this.finishNode(node, type);
  11538. }
  11539. parseLiteral(value, type) {
  11540. const node = this.startNode();
  11541. return this.parseLiteralAtNode(value, type, node);
  11542. }
  11543. parseStringLiteral(value) {
  11544. return this.parseLiteral(value, "StringLiteral");
  11545. }
  11546. parseNumericLiteral(value) {
  11547. return this.parseLiteral(value, "NumericLiteral");
  11548. }
  11549. parseBigIntLiteral(value) {
  11550. return this.parseLiteral(value, "BigIntLiteral");
  11551. }
  11552. parseDecimalLiteral(value) {
  11553. return this.parseLiteral(value, "DecimalLiteral");
  11554. }
  11555. parseRegExpLiteral(value) {
  11556. const node = this.startNode();
  11557. this.addExtra(node, "raw", this.input.slice(this.offsetToSourcePos(node.start), this.state.end));
  11558. node.pattern = value.pattern;
  11559. node.flags = value.flags;
  11560. this.next();
  11561. return this.finishNode(node, "RegExpLiteral");
  11562. }
  11563. parseBooleanLiteral(value) {
  11564. const node = this.startNode();
  11565. node.value = value;
  11566. this.next();
  11567. return this.finishNode(node, "BooleanLiteral");
  11568. }
  11569. parseNullLiteral() {
  11570. const node = this.startNode();
  11571. this.next();
  11572. return this.finishNode(node, "NullLiteral");
  11573. }
  11574. parseParenAndDistinguishExpression(canBeArrow) {
  11575. const startLoc = this.state.startLoc;
  11576. let val;
  11577. this.next();
  11578. this.expressionScope.enter(newArrowHeadScope());
  11579. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  11580. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  11581. this.state.maybeInArrowParameters = true;
  11582. this.state.inFSharpPipelineDirectBody = false;
  11583. const innerStartLoc = this.state.startLoc;
  11584. const exprList = [];
  11585. const refExpressionErrors = new ExpressionErrors();
  11586. let first = true;
  11587. let spreadStartLoc;
  11588. let optionalCommaStartLoc;
  11589. while (!this.match(11)) {
  11590. if (first) {
  11591. first = false;
  11592. } else {
  11593. this.expect(12, refExpressionErrors.optionalParametersLoc === null ? null : refExpressionErrors.optionalParametersLoc);
  11594. if (this.match(11)) {
  11595. optionalCommaStartLoc = this.state.startLoc;
  11596. break;
  11597. }
  11598. }
  11599. if (this.match(21)) {
  11600. const spreadNodeStartLoc = this.state.startLoc;
  11601. spreadStartLoc = this.state.startLoc;
  11602. exprList.push(this.parseParenItem(this.parseRestBinding(), spreadNodeStartLoc));
  11603. if (!this.checkCommaAfterRest(41)) {
  11604. break;
  11605. }
  11606. } else {
  11607. exprList.push(this.parseMaybeAssignAllowIn(refExpressionErrors, this.parseParenItem));
  11608. }
  11609. }
  11610. const innerEndLoc = this.state.lastTokEndLoc;
  11611. this.expect(11);
  11612. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  11613. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  11614. let arrowNode = this.startNodeAt(startLoc);
  11615. if (canBeArrow && this.shouldParseArrow(exprList) && (arrowNode = this.parseArrow(arrowNode))) {
  11616. this.checkDestructuringPrivate(refExpressionErrors);
  11617. this.expressionScope.validateAsPattern();
  11618. this.expressionScope.exit();
  11619. this.parseArrowExpression(arrowNode, exprList, false);
  11620. return arrowNode;
  11621. }
  11622. this.expressionScope.exit();
  11623. if (!exprList.length) {
  11624. this.unexpected(this.state.lastTokStartLoc);
  11625. }
  11626. if (optionalCommaStartLoc) this.unexpected(optionalCommaStartLoc);
  11627. if (spreadStartLoc) this.unexpected(spreadStartLoc);
  11628. this.checkExpressionErrors(refExpressionErrors, true);
  11629. this.toReferencedListDeep(exprList, true);
  11630. if (exprList.length > 1) {
  11631. val = this.startNodeAt(innerStartLoc);
  11632. val.expressions = exprList;
  11633. this.finishNode(val, "SequenceExpression");
  11634. this.resetEndLocation(val, innerEndLoc);
  11635. } else {
  11636. val = exprList[0];
  11637. }
  11638. return this.wrapParenthesis(startLoc, val);
  11639. }
  11640. wrapParenthesis(startLoc, expression) {
  11641. if (!(this.optionFlags & 1024)) {
  11642. this.addExtra(expression, "parenthesized", true);
  11643. this.addExtra(expression, "parenStart", startLoc.index);
  11644. this.takeSurroundingComments(expression, startLoc.index, this.state.lastTokEndLoc.index);
  11645. return expression;
  11646. }
  11647. const parenExpression = this.startNodeAt(startLoc);
  11648. parenExpression.expression = expression;
  11649. return this.finishNode(parenExpression, "ParenthesizedExpression");
  11650. }
  11651. shouldParseArrow(params) {
  11652. return !this.canInsertSemicolon();
  11653. }
  11654. parseArrow(node) {
  11655. if (this.eat(19)) {
  11656. return node;
  11657. }
  11658. }
  11659. parseParenItem(node, startLoc) {
  11660. return node;
  11661. }
  11662. parseNewOrNewTarget() {
  11663. const node = this.startNode();
  11664. this.next();
  11665. if (this.match(16)) {
  11666. const meta = this.createIdentifier(this.startNodeAtNode(node), "new");
  11667. this.next();
  11668. const metaProp = this.parseMetaProperty(node, meta, "target");
  11669. if (!this.scope.inNonArrowFunction && !this.scope.inClass && !(this.optionFlags & 4)) {
  11670. this.raise(Errors.UnexpectedNewTarget, metaProp);
  11671. }
  11672. return metaProp;
  11673. }
  11674. return this.parseNew(node);
  11675. }
  11676. parseNew(node) {
  11677. this.parseNewCallee(node);
  11678. if (this.eat(10)) {
  11679. const args = this.parseExprList(11);
  11680. this.toReferencedList(args);
  11681. node.arguments = args;
  11682. } else {
  11683. node.arguments = [];
  11684. }
  11685. return this.finishNode(node, "NewExpression");
  11686. }
  11687. parseNewCallee(node) {
  11688. const isImport = this.match(83);
  11689. const callee = this.parseNoCallExpr();
  11690. node.callee = callee;
  11691. if (isImport && (callee.type === "Import" || callee.type === "ImportExpression")) {
  11692. this.raise(Errors.ImportCallNotNewExpression, callee);
  11693. }
  11694. }
  11695. parseTemplateElement(isTagged) {
  11696. const {
  11697. start,
  11698. startLoc,
  11699. end,
  11700. value
  11701. } = this.state;
  11702. const elemStart = start + 1;
  11703. const elem = this.startNodeAt(createPositionWithColumnOffset(startLoc, 1));
  11704. if (value === null) {
  11705. if (!isTagged) {
  11706. this.raise(Errors.InvalidEscapeSequenceTemplate, createPositionWithColumnOffset(this.state.firstInvalidTemplateEscapePos, 1));
  11707. }
  11708. }
  11709. const isTail = this.match(24);
  11710. const endOffset = isTail ? -1 : -2;
  11711. const elemEnd = end + endOffset;
  11712. elem.value = {
  11713. raw: this.input.slice(elemStart, elemEnd).replace(/\r\n?/g, "\n"),
  11714. cooked: value === null ? null : value.slice(1, endOffset)
  11715. };
  11716. elem.tail = isTail;
  11717. this.next();
  11718. const finishedNode = this.finishNode(elem, "TemplateElement");
  11719. this.resetEndLocation(finishedNode, createPositionWithColumnOffset(this.state.lastTokEndLoc, endOffset));
  11720. return finishedNode;
  11721. }
  11722. parseTemplate(isTagged) {
  11723. const node = this.startNode();
  11724. let curElt = this.parseTemplateElement(isTagged);
  11725. const quasis = [curElt];
  11726. const substitutions = [];
  11727. while (!curElt.tail) {
  11728. substitutions.push(this.parseTemplateSubstitution());
  11729. this.readTemplateContinuation();
  11730. quasis.push(curElt = this.parseTemplateElement(isTagged));
  11731. }
  11732. node.expressions = substitutions;
  11733. node.quasis = quasis;
  11734. return this.finishNode(node, "TemplateLiteral");
  11735. }
  11736. parseTemplateSubstitution() {
  11737. return this.parseExpression();
  11738. }
  11739. parseObjectLike(close, isPattern, isRecord, refExpressionErrors) {
  11740. if (isRecord) {
  11741. this.expectPlugin("recordAndTuple");
  11742. }
  11743. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  11744. this.state.inFSharpPipelineDirectBody = false;
  11745. let sawProto = false;
  11746. let first = true;
  11747. const node = this.startNode();
  11748. node.properties = [];
  11749. this.next();
  11750. while (!this.match(close)) {
  11751. if (first) {
  11752. first = false;
  11753. } else {
  11754. this.expect(12);
  11755. if (this.match(close)) {
  11756. this.addTrailingCommaExtraToNode(node);
  11757. break;
  11758. }
  11759. }
  11760. let prop;
  11761. if (isPattern) {
  11762. prop = this.parseBindingProperty();
  11763. } else {
  11764. prop = this.parsePropertyDefinition(refExpressionErrors);
  11765. sawProto = this.checkProto(prop, isRecord, sawProto, refExpressionErrors);
  11766. }
  11767. if (isRecord && !this.isObjectProperty(prop) && prop.type !== "SpreadElement") {
  11768. this.raise(Errors.InvalidRecordProperty, prop);
  11769. }
  11770. {
  11771. if (prop.shorthand) {
  11772. this.addExtra(prop, "shorthand", true);
  11773. }
  11774. }
  11775. node.properties.push(prop);
  11776. }
  11777. this.next();
  11778. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  11779. let type = "ObjectExpression";
  11780. if (isPattern) {
  11781. type = "ObjectPattern";
  11782. } else if (isRecord) {
  11783. type = "RecordExpression";
  11784. }
  11785. return this.finishNode(node, type);
  11786. }
  11787. addTrailingCommaExtraToNode(node) {
  11788. this.addExtra(node, "trailingComma", this.state.lastTokStartLoc.index);
  11789. this.addExtra(node, "trailingCommaLoc", this.state.lastTokStartLoc, false);
  11790. }
  11791. maybeAsyncOrAccessorProp(prop) {
  11792. return !prop.computed && prop.key.type === "Identifier" && (this.isLiteralPropertyName() || this.match(0) || this.match(55));
  11793. }
  11794. parsePropertyDefinition(refExpressionErrors) {
  11795. let decorators = [];
  11796. if (this.match(26)) {
  11797. if (this.hasPlugin("decorators")) {
  11798. this.raise(Errors.UnsupportedPropertyDecorator, this.state.startLoc);
  11799. }
  11800. while (this.match(26)) {
  11801. decorators.push(this.parseDecorator());
  11802. }
  11803. }
  11804. const prop = this.startNode();
  11805. let isAsync = false;
  11806. let isAccessor = false;
  11807. let startLoc;
  11808. if (this.match(21)) {
  11809. if (decorators.length) this.unexpected();
  11810. return this.parseSpread();
  11811. }
  11812. if (decorators.length) {
  11813. prop.decorators = decorators;
  11814. decorators = [];
  11815. }
  11816. prop.method = false;
  11817. if (refExpressionErrors) {
  11818. startLoc = this.state.startLoc;
  11819. }
  11820. let isGenerator = this.eat(55);
  11821. this.parsePropertyNamePrefixOperator(prop);
  11822. const containsEsc = this.state.containsEsc;
  11823. this.parsePropertyName(prop, refExpressionErrors);
  11824. if (!isGenerator && !containsEsc && this.maybeAsyncOrAccessorProp(prop)) {
  11825. const {
  11826. key
  11827. } = prop;
  11828. const keyName = key.name;
  11829. if (keyName === "async" && !this.hasPrecedingLineBreak()) {
  11830. isAsync = true;
  11831. this.resetPreviousNodeTrailingComments(key);
  11832. isGenerator = this.eat(55);
  11833. this.parsePropertyName(prop);
  11834. }
  11835. if (keyName === "get" || keyName === "set") {
  11836. isAccessor = true;
  11837. this.resetPreviousNodeTrailingComments(key);
  11838. prop.kind = keyName;
  11839. if (this.match(55)) {
  11840. isGenerator = true;
  11841. this.raise(Errors.AccessorIsGenerator, this.state.curPosition(), {
  11842. kind: keyName
  11843. });
  11844. this.next();
  11845. }
  11846. this.parsePropertyName(prop);
  11847. }
  11848. }
  11849. return this.parseObjPropValue(prop, startLoc, isGenerator, isAsync, false, isAccessor, refExpressionErrors);
  11850. }
  11851. getGetterSetterExpectedParamCount(method) {
  11852. return method.kind === "get" ? 0 : 1;
  11853. }
  11854. getObjectOrClassMethodParams(method) {
  11855. return method.params;
  11856. }
  11857. checkGetterSetterParams(method) {
  11858. var _params;
  11859. const paramCount = this.getGetterSetterExpectedParamCount(method);
  11860. const params = this.getObjectOrClassMethodParams(method);
  11861. if (params.length !== paramCount) {
  11862. this.raise(method.kind === "get" ? Errors.BadGetterArity : Errors.BadSetterArity, method);
  11863. }
  11864. if (method.kind === "set" && ((_params = params[params.length - 1]) == null ? void 0 : _params.type) === "RestElement") {
  11865. this.raise(Errors.BadSetterRestParameter, method);
  11866. }
  11867. }
  11868. parseObjectMethod(prop, isGenerator, isAsync, isPattern, isAccessor) {
  11869. if (isAccessor) {
  11870. const finishedProp = this.parseMethod(prop, isGenerator, false, false, false, "ObjectMethod");
  11871. this.checkGetterSetterParams(finishedProp);
  11872. return finishedProp;
  11873. }
  11874. if (isAsync || isGenerator || this.match(10)) {
  11875. if (isPattern) this.unexpected();
  11876. prop.kind = "method";
  11877. prop.method = true;
  11878. return this.parseMethod(prop, isGenerator, isAsync, false, false, "ObjectMethod");
  11879. }
  11880. }
  11881. parseObjectProperty(prop, startLoc, isPattern, refExpressionErrors) {
  11882. prop.shorthand = false;
  11883. if (this.eat(14)) {
  11884. prop.value = isPattern ? this.parseMaybeDefault(this.state.startLoc) : this.parseMaybeAssignAllowIn(refExpressionErrors);
  11885. return this.finishObjectProperty(prop);
  11886. }
  11887. if (!prop.computed && prop.key.type === "Identifier") {
  11888. this.checkReservedWord(prop.key.name, prop.key.loc.start, true, false);
  11889. if (isPattern) {
  11890. prop.value = this.parseMaybeDefault(startLoc, this.cloneIdentifier(prop.key));
  11891. } else if (this.match(29)) {
  11892. const shorthandAssignLoc = this.state.startLoc;
  11893. if (refExpressionErrors != null) {
  11894. if (refExpressionErrors.shorthandAssignLoc === null) {
  11895. refExpressionErrors.shorthandAssignLoc = shorthandAssignLoc;
  11896. }
  11897. } else {
  11898. this.raise(Errors.InvalidCoverInitializedName, shorthandAssignLoc);
  11899. }
  11900. prop.value = this.parseMaybeDefault(startLoc, this.cloneIdentifier(prop.key));
  11901. } else {
  11902. prop.value = this.cloneIdentifier(prop.key);
  11903. }
  11904. prop.shorthand = true;
  11905. return this.finishObjectProperty(prop);
  11906. }
  11907. }
  11908. finishObjectProperty(node) {
  11909. return this.finishNode(node, "ObjectProperty");
  11910. }
  11911. parseObjPropValue(prop, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors) {
  11912. const node = this.parseObjectMethod(prop, isGenerator, isAsync, isPattern, isAccessor) || this.parseObjectProperty(prop, startLoc, isPattern, refExpressionErrors);
  11913. if (!node) this.unexpected();
  11914. return node;
  11915. }
  11916. parsePropertyName(prop, refExpressionErrors) {
  11917. if (this.eat(0)) {
  11918. prop.computed = true;
  11919. prop.key = this.parseMaybeAssignAllowIn();
  11920. this.expect(3);
  11921. } else {
  11922. const {
  11923. type,
  11924. value
  11925. } = this.state;
  11926. let key;
  11927. if (tokenIsKeywordOrIdentifier(type)) {
  11928. key = this.parseIdentifier(true);
  11929. } else {
  11930. switch (type) {
  11931. case 135:
  11932. key = this.parseNumericLiteral(value);
  11933. break;
  11934. case 134:
  11935. key = this.parseStringLiteral(value);
  11936. break;
  11937. case 136:
  11938. key = this.parseBigIntLiteral(value);
  11939. break;
  11940. case 139:
  11941. {
  11942. const privateKeyLoc = this.state.startLoc;
  11943. if (refExpressionErrors != null) {
  11944. if (refExpressionErrors.privateKeyLoc === null) {
  11945. refExpressionErrors.privateKeyLoc = privateKeyLoc;
  11946. }
  11947. } else {
  11948. this.raise(Errors.UnexpectedPrivateField, privateKeyLoc);
  11949. }
  11950. key = this.parsePrivateName();
  11951. break;
  11952. }
  11953. default:
  11954. if (type === 137) {
  11955. key = this.parseDecimalLiteral(value);
  11956. break;
  11957. }
  11958. this.unexpected();
  11959. }
  11960. }
  11961. prop.key = key;
  11962. if (type !== 139) {
  11963. prop.computed = false;
  11964. }
  11965. }
  11966. }
  11967. initFunction(node, isAsync) {
  11968. node.id = null;
  11969. node.generator = false;
  11970. node.async = isAsync;
  11971. }
  11972. parseMethod(node, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope = false) {
  11973. this.initFunction(node, isAsync);
  11974. node.generator = isGenerator;
  11975. this.scope.enter(2 | 16 | (inClassScope ? 64 : 0) | (allowDirectSuper ? 32 : 0));
  11976. this.prodParam.enter(functionFlags(isAsync, node.generator));
  11977. this.parseFunctionParams(node, isConstructor);
  11978. const finishedNode = this.parseFunctionBodyAndFinish(node, type, true);
  11979. this.prodParam.exit();
  11980. this.scope.exit();
  11981. return finishedNode;
  11982. }
  11983. parseArrayLike(close, canBePattern, isTuple, refExpressionErrors) {
  11984. if (isTuple) {
  11985. this.expectPlugin("recordAndTuple");
  11986. }
  11987. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  11988. this.state.inFSharpPipelineDirectBody = false;
  11989. const node = this.startNode();
  11990. this.next();
  11991. node.elements = this.parseExprList(close, !isTuple, refExpressionErrors, node);
  11992. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  11993. return this.finishNode(node, isTuple ? "TupleExpression" : "ArrayExpression");
  11994. }
  11995. parseArrowExpression(node, params, isAsync, trailingCommaLoc) {
  11996. this.scope.enter(2 | 4);
  11997. let flags = functionFlags(isAsync, false);
  11998. if (!this.match(5) && this.prodParam.hasIn) {
  11999. flags |= 8;
  12000. }
  12001. this.prodParam.enter(flags);
  12002. this.initFunction(node, isAsync);
  12003. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  12004. if (params) {
  12005. this.state.maybeInArrowParameters = true;
  12006. this.setArrowFunctionParameters(node, params, trailingCommaLoc);
  12007. }
  12008. this.state.maybeInArrowParameters = false;
  12009. this.parseFunctionBody(node, true);
  12010. this.prodParam.exit();
  12011. this.scope.exit();
  12012. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  12013. return this.finishNode(node, "ArrowFunctionExpression");
  12014. }
  12015. setArrowFunctionParameters(node, params, trailingCommaLoc) {
  12016. this.toAssignableList(params, trailingCommaLoc, false);
  12017. node.params = params;
  12018. }
  12019. parseFunctionBodyAndFinish(node, type, isMethod = false) {
  12020. this.parseFunctionBody(node, false, isMethod);
  12021. return this.finishNode(node, type);
  12022. }
  12023. parseFunctionBody(node, allowExpression, isMethod = false) {
  12024. const isExpression = allowExpression && !this.match(5);
  12025. this.expressionScope.enter(newExpressionScope());
  12026. if (isExpression) {
  12027. node.body = this.parseMaybeAssign();
  12028. this.checkParams(node, false, allowExpression, false);
  12029. } else {
  12030. const oldStrict = this.state.strict;
  12031. const oldLabels = this.state.labels;
  12032. this.state.labels = [];
  12033. this.prodParam.enter(this.prodParam.currentFlags() | 4);
  12034. node.body = this.parseBlock(true, false, hasStrictModeDirective => {
  12035. const nonSimple = !this.isSimpleParamList(node.params);
  12036. if (hasStrictModeDirective && nonSimple) {
  12037. this.raise(Errors.IllegalLanguageModeDirective, (node.kind === "method" || node.kind === "constructor") && !!node.key ? node.key.loc.end : node);
  12038. }
  12039. const strictModeChanged = !oldStrict && this.state.strict;
  12040. this.checkParams(node, !this.state.strict && !allowExpression && !isMethod && !nonSimple, allowExpression, strictModeChanged);
  12041. if (this.state.strict && node.id) {
  12042. this.checkIdentifier(node.id, 65, strictModeChanged);
  12043. }
  12044. });
  12045. this.prodParam.exit();
  12046. this.state.labels = oldLabels;
  12047. }
  12048. this.expressionScope.exit();
  12049. }
  12050. isSimpleParameter(node) {
  12051. return node.type === "Identifier";
  12052. }
  12053. isSimpleParamList(params) {
  12054. for (let i = 0, len = params.length; i < len; i++) {
  12055. if (!this.isSimpleParameter(params[i])) return false;
  12056. }
  12057. return true;
  12058. }
  12059. checkParams(node, allowDuplicates, isArrowFunction, strictModeChanged = true) {
  12060. const checkClashes = !allowDuplicates && new Set();
  12061. const formalParameters = {
  12062. type: "FormalParameters"
  12063. };
  12064. for (const param of node.params) {
  12065. this.checkLVal(param, formalParameters, 5, checkClashes, strictModeChanged);
  12066. }
  12067. }
  12068. parseExprList(close, allowEmpty, refExpressionErrors, nodeForExtra) {
  12069. const elts = [];
  12070. let first = true;
  12071. while (!this.eat(close)) {
  12072. if (first) {
  12073. first = false;
  12074. } else {
  12075. this.expect(12);
  12076. if (this.match(close)) {
  12077. if (nodeForExtra) {
  12078. this.addTrailingCommaExtraToNode(nodeForExtra);
  12079. }
  12080. this.next();
  12081. break;
  12082. }
  12083. }
  12084. elts.push(this.parseExprListItem(allowEmpty, refExpressionErrors));
  12085. }
  12086. return elts;
  12087. }
  12088. parseExprListItem(allowEmpty, refExpressionErrors, allowPlaceholder) {
  12089. let elt;
  12090. if (this.match(12)) {
  12091. if (!allowEmpty) {
  12092. this.raise(Errors.UnexpectedToken, this.state.curPosition(), {
  12093. unexpected: ","
  12094. });
  12095. }
  12096. elt = null;
  12097. } else if (this.match(21)) {
  12098. const spreadNodeStartLoc = this.state.startLoc;
  12099. elt = this.parseParenItem(this.parseSpread(refExpressionErrors), spreadNodeStartLoc);
  12100. } else if (this.match(17)) {
  12101. this.expectPlugin("partialApplication");
  12102. if (!allowPlaceholder) {
  12103. this.raise(Errors.UnexpectedArgumentPlaceholder, this.state.startLoc);
  12104. }
  12105. const node = this.startNode();
  12106. this.next();
  12107. elt = this.finishNode(node, "ArgumentPlaceholder");
  12108. } else {
  12109. elt = this.parseMaybeAssignAllowIn(refExpressionErrors, this.parseParenItem);
  12110. }
  12111. return elt;
  12112. }
  12113. parseIdentifier(liberal) {
  12114. const node = this.startNode();
  12115. const name = this.parseIdentifierName(liberal);
  12116. return this.createIdentifier(node, name);
  12117. }
  12118. createIdentifier(node, name) {
  12119. node.name = name;
  12120. node.loc.identifierName = name;
  12121. return this.finishNode(node, "Identifier");
  12122. }
  12123. createIdentifierAt(node, name, endLoc) {
  12124. node.name = name;
  12125. node.loc.identifierName = name;
  12126. return this.finishNodeAt(node, "Identifier", endLoc);
  12127. }
  12128. parseIdentifierName(liberal) {
  12129. let name;
  12130. const {
  12131. startLoc,
  12132. type
  12133. } = this.state;
  12134. if (tokenIsKeywordOrIdentifier(type)) {
  12135. name = this.state.value;
  12136. } else {
  12137. this.unexpected();
  12138. }
  12139. const tokenIsKeyword = tokenKeywordOrIdentifierIsKeyword(type);
  12140. if (liberal) {
  12141. if (tokenIsKeyword) {
  12142. this.replaceToken(132);
  12143. }
  12144. } else {
  12145. this.checkReservedWord(name, startLoc, tokenIsKeyword, false);
  12146. }
  12147. this.next();
  12148. return name;
  12149. }
  12150. checkReservedWord(word, startLoc, checkKeywords, isBinding) {
  12151. if (word.length > 10) {
  12152. return;
  12153. }
  12154. if (!canBeReservedWord(word)) {
  12155. return;
  12156. }
  12157. if (checkKeywords && isKeyword(word)) {
  12158. this.raise(Errors.UnexpectedKeyword, startLoc, {
  12159. keyword: word
  12160. });
  12161. return;
  12162. }
  12163. const reservedTest = !this.state.strict ? isReservedWord : isBinding ? isStrictBindReservedWord : isStrictReservedWord;
  12164. if (reservedTest(word, this.inModule)) {
  12165. this.raise(Errors.UnexpectedReservedWord, startLoc, {
  12166. reservedWord: word
  12167. });
  12168. return;
  12169. } else if (word === "yield") {
  12170. if (this.prodParam.hasYield) {
  12171. this.raise(Errors.YieldBindingIdentifier, startLoc);
  12172. return;
  12173. }
  12174. } else if (word === "await") {
  12175. if (this.prodParam.hasAwait) {
  12176. this.raise(Errors.AwaitBindingIdentifier, startLoc);
  12177. return;
  12178. }
  12179. if (this.scope.inStaticBlock) {
  12180. this.raise(Errors.AwaitBindingIdentifierInStaticBlock, startLoc);
  12181. return;
  12182. }
  12183. this.expressionScope.recordAsyncArrowParametersError(startLoc);
  12184. } else if (word === "arguments") {
  12185. if (this.scope.inClassAndNotInNonArrowFunction) {
  12186. this.raise(Errors.ArgumentsInClass, startLoc);
  12187. return;
  12188. }
  12189. }
  12190. }
  12191. recordAwaitIfAllowed() {
  12192. const isAwaitAllowed = this.prodParam.hasAwait || this.optionFlags & 1 && !this.scope.inFunction;
  12193. if (isAwaitAllowed && !this.scope.inFunction) {
  12194. this.state.hasTopLevelAwait = true;
  12195. }
  12196. return isAwaitAllowed;
  12197. }
  12198. parseAwait(startLoc) {
  12199. const node = this.startNodeAt(startLoc);
  12200. this.expressionScope.recordParameterInitializerError(Errors.AwaitExpressionFormalParameter, node);
  12201. if (this.eat(55)) {
  12202. this.raise(Errors.ObsoleteAwaitStar, node);
  12203. }
  12204. if (!this.scope.inFunction && !(this.optionFlags & 1)) {
  12205. if (this.isAmbiguousPrefixOrIdentifier()) {
  12206. this.ambiguousScriptDifferentAst = true;
  12207. } else {
  12208. this.sawUnambiguousESM = true;
  12209. }
  12210. }
  12211. if (!this.state.soloAwait) {
  12212. node.argument = this.parseMaybeUnary(null, true);
  12213. }
  12214. return this.finishNode(node, "AwaitExpression");
  12215. }
  12216. isAmbiguousPrefixOrIdentifier() {
  12217. if (this.hasPrecedingLineBreak()) return true;
  12218. const {
  12219. type
  12220. } = this.state;
  12221. return type === 53 || type === 10 || type === 0 || tokenIsTemplate(type) || type === 102 && !this.state.containsEsc || type === 138 || type === 56 || this.hasPlugin("v8intrinsic") && type === 54;
  12222. }
  12223. parseYield(startLoc) {
  12224. const node = this.startNodeAt(startLoc);
  12225. this.expressionScope.recordParameterInitializerError(Errors.YieldInParameter, node);
  12226. let delegating = false;
  12227. let argument = null;
  12228. if (!this.hasPrecedingLineBreak()) {
  12229. delegating = this.eat(55);
  12230. switch (this.state.type) {
  12231. case 13:
  12232. case 140:
  12233. case 8:
  12234. case 11:
  12235. case 3:
  12236. case 9:
  12237. case 14:
  12238. case 12:
  12239. if (!delegating) break;
  12240. default:
  12241. argument = this.parseMaybeAssign();
  12242. }
  12243. }
  12244. node.delegate = delegating;
  12245. node.argument = argument;
  12246. return this.finishNode(node, "YieldExpression");
  12247. }
  12248. parseImportCall(node) {
  12249. this.next();
  12250. node.source = this.parseMaybeAssignAllowIn();
  12251. node.options = null;
  12252. if (this.eat(12)) {
  12253. if (!this.match(11)) {
  12254. node.options = this.parseMaybeAssignAllowIn();
  12255. if (this.eat(12)) {
  12256. this.addTrailingCommaExtraToNode(node.options);
  12257. if (!this.match(11)) {
  12258. do {
  12259. this.parseMaybeAssignAllowIn();
  12260. } while (this.eat(12) && !this.match(11));
  12261. this.raise(Errors.ImportCallArity, node);
  12262. }
  12263. }
  12264. } else {
  12265. this.addTrailingCommaExtraToNode(node.source);
  12266. }
  12267. }
  12268. this.expect(11);
  12269. return this.finishNode(node, "ImportExpression");
  12270. }
  12271. checkPipelineAtInfixOperator(left, leftStartLoc) {
  12272. if (this.hasPlugin(["pipelineOperator", {
  12273. proposal: "smart"
  12274. }])) {
  12275. if (left.type === "SequenceExpression") {
  12276. this.raise(Errors.PipelineHeadSequenceExpression, leftStartLoc);
  12277. }
  12278. }
  12279. }
  12280. parseSmartPipelineBodyInStyle(childExpr, startLoc) {
  12281. if (this.isSimpleReference(childExpr)) {
  12282. const bodyNode = this.startNodeAt(startLoc);
  12283. bodyNode.callee = childExpr;
  12284. return this.finishNode(bodyNode, "PipelineBareFunction");
  12285. } else {
  12286. const bodyNode = this.startNodeAt(startLoc);
  12287. this.checkSmartPipeTopicBodyEarlyErrors(startLoc);
  12288. bodyNode.expression = childExpr;
  12289. return this.finishNode(bodyNode, "PipelineTopicExpression");
  12290. }
  12291. }
  12292. isSimpleReference(expression) {
  12293. switch (expression.type) {
  12294. case "MemberExpression":
  12295. return !expression.computed && this.isSimpleReference(expression.object);
  12296. case "Identifier":
  12297. return true;
  12298. default:
  12299. return false;
  12300. }
  12301. }
  12302. checkSmartPipeTopicBodyEarlyErrors(startLoc) {
  12303. if (this.match(19)) {
  12304. throw this.raise(Errors.PipelineBodyNoArrow, this.state.startLoc);
  12305. }
  12306. if (!this.topicReferenceWasUsedInCurrentContext()) {
  12307. this.raise(Errors.PipelineTopicUnused, startLoc);
  12308. }
  12309. }
  12310. withTopicBindingContext(callback) {
  12311. const outerContextTopicState = this.state.topicContext;
  12312. this.state.topicContext = {
  12313. maxNumOfResolvableTopics: 1,
  12314. maxTopicIndex: null
  12315. };
  12316. try {
  12317. return callback();
  12318. } finally {
  12319. this.state.topicContext = outerContextTopicState;
  12320. }
  12321. }
  12322. withSmartMixTopicForbiddingContext(callback) {
  12323. if (this.hasPlugin(["pipelineOperator", {
  12324. proposal: "smart"
  12325. }])) {
  12326. const outerContextTopicState = this.state.topicContext;
  12327. this.state.topicContext = {
  12328. maxNumOfResolvableTopics: 0,
  12329. maxTopicIndex: null
  12330. };
  12331. try {
  12332. return callback();
  12333. } finally {
  12334. this.state.topicContext = outerContextTopicState;
  12335. }
  12336. } else {
  12337. return callback();
  12338. }
  12339. }
  12340. withSoloAwaitPermittingContext(callback) {
  12341. const outerContextSoloAwaitState = this.state.soloAwait;
  12342. this.state.soloAwait = true;
  12343. try {
  12344. return callback();
  12345. } finally {
  12346. this.state.soloAwait = outerContextSoloAwaitState;
  12347. }
  12348. }
  12349. allowInAnd(callback) {
  12350. const flags = this.prodParam.currentFlags();
  12351. const prodParamToSet = 8 & ~flags;
  12352. if (prodParamToSet) {
  12353. this.prodParam.enter(flags | 8);
  12354. try {
  12355. return callback();
  12356. } finally {
  12357. this.prodParam.exit();
  12358. }
  12359. }
  12360. return callback();
  12361. }
  12362. disallowInAnd(callback) {
  12363. const flags = this.prodParam.currentFlags();
  12364. const prodParamToClear = 8 & flags;
  12365. if (prodParamToClear) {
  12366. this.prodParam.enter(flags & ~8);
  12367. try {
  12368. return callback();
  12369. } finally {
  12370. this.prodParam.exit();
  12371. }
  12372. }
  12373. return callback();
  12374. }
  12375. registerTopicReference() {
  12376. this.state.topicContext.maxTopicIndex = 0;
  12377. }
  12378. topicReferenceIsAllowedInCurrentContext() {
  12379. return this.state.topicContext.maxNumOfResolvableTopics >= 1;
  12380. }
  12381. topicReferenceWasUsedInCurrentContext() {
  12382. return this.state.topicContext.maxTopicIndex != null && this.state.topicContext.maxTopicIndex >= 0;
  12383. }
  12384. parseFSharpPipelineBody(prec) {
  12385. const startLoc = this.state.startLoc;
  12386. this.state.potentialArrowAt = this.state.start;
  12387. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  12388. this.state.inFSharpPipelineDirectBody = true;
  12389. const ret = this.parseExprOp(this.parseMaybeUnaryOrPrivate(), startLoc, prec);
  12390. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  12391. return ret;
  12392. }
  12393. parseModuleExpression() {
  12394. this.expectPlugin("moduleBlocks");
  12395. const node = this.startNode();
  12396. this.next();
  12397. if (!this.match(5)) {
  12398. this.unexpected(null, 5);
  12399. }
  12400. const program = this.startNodeAt(this.state.endLoc);
  12401. this.next();
  12402. const revertScopes = this.initializeScopes(true);
  12403. this.enterInitialScopes();
  12404. try {
  12405. node.body = this.parseProgram(program, 8, "module");
  12406. } finally {
  12407. revertScopes();
  12408. }
  12409. return this.finishNode(node, "ModuleExpression");
  12410. }
  12411. parsePropertyNamePrefixOperator(prop) {}
  12412. }
  12413. const loopLabel = {
  12414. kind: 1
  12415. },
  12416. switchLabel = {
  12417. kind: 2
  12418. };
  12419. const loneSurrogate = /[\uD800-\uDFFF]/u;
  12420. const keywordRelationalOperator = /in(?:stanceof)?/y;
  12421. function babel7CompatTokens(tokens, input, startIndex) {
  12422. for (let i = 0; i < tokens.length; i++) {
  12423. const token = tokens[i];
  12424. const {
  12425. type
  12426. } = token;
  12427. if (typeof type === "number") {
  12428. {
  12429. if (type === 139) {
  12430. const {
  12431. loc,
  12432. start,
  12433. value,
  12434. end
  12435. } = token;
  12436. const hashEndPos = start + 1;
  12437. const hashEndLoc = createPositionWithColumnOffset(loc.start, 1);
  12438. tokens.splice(i, 1, new Token({
  12439. type: getExportedToken(27),
  12440. value: "#",
  12441. start: start,
  12442. end: hashEndPos,
  12443. startLoc: loc.start,
  12444. endLoc: hashEndLoc
  12445. }), new Token({
  12446. type: getExportedToken(132),
  12447. value: value,
  12448. start: hashEndPos,
  12449. end: end,
  12450. startLoc: hashEndLoc,
  12451. endLoc: loc.end
  12452. }));
  12453. i++;
  12454. continue;
  12455. }
  12456. if (tokenIsTemplate(type)) {
  12457. const {
  12458. loc,
  12459. start,
  12460. value,
  12461. end
  12462. } = token;
  12463. const backquoteEnd = start + 1;
  12464. const backquoteEndLoc = createPositionWithColumnOffset(loc.start, 1);
  12465. let startToken;
  12466. if (input.charCodeAt(start - startIndex) === 96) {
  12467. startToken = new Token({
  12468. type: getExportedToken(22),
  12469. value: "`",
  12470. start: start,
  12471. end: backquoteEnd,
  12472. startLoc: loc.start,
  12473. endLoc: backquoteEndLoc
  12474. });
  12475. } else {
  12476. startToken = new Token({
  12477. type: getExportedToken(8),
  12478. value: "}",
  12479. start: start,
  12480. end: backquoteEnd,
  12481. startLoc: loc.start,
  12482. endLoc: backquoteEndLoc
  12483. });
  12484. }
  12485. let templateValue, templateElementEnd, templateElementEndLoc, endToken;
  12486. if (type === 24) {
  12487. templateElementEnd = end - 1;
  12488. templateElementEndLoc = createPositionWithColumnOffset(loc.end, -1);
  12489. templateValue = value === null ? null : value.slice(1, -1);
  12490. endToken = new Token({
  12491. type: getExportedToken(22),
  12492. value: "`",
  12493. start: templateElementEnd,
  12494. end: end,
  12495. startLoc: templateElementEndLoc,
  12496. endLoc: loc.end
  12497. });
  12498. } else {
  12499. templateElementEnd = end - 2;
  12500. templateElementEndLoc = createPositionWithColumnOffset(loc.end, -2);
  12501. templateValue = value === null ? null : value.slice(1, -2);
  12502. endToken = new Token({
  12503. type: getExportedToken(23),
  12504. value: "${",
  12505. start: templateElementEnd,
  12506. end: end,
  12507. startLoc: templateElementEndLoc,
  12508. endLoc: loc.end
  12509. });
  12510. }
  12511. tokens.splice(i, 1, startToken, new Token({
  12512. type: getExportedToken(20),
  12513. value: templateValue,
  12514. start: backquoteEnd,
  12515. end: templateElementEnd,
  12516. startLoc: backquoteEndLoc,
  12517. endLoc: templateElementEndLoc
  12518. }), endToken);
  12519. i += 2;
  12520. continue;
  12521. }
  12522. }
  12523. token.type = getExportedToken(type);
  12524. }
  12525. }
  12526. return tokens;
  12527. }
  12528. class StatementParser extends ExpressionParser {
  12529. parseTopLevel(file, program) {
  12530. file.program = this.parseProgram(program);
  12531. file.comments = this.comments;
  12532. if (this.optionFlags & 256) {
  12533. file.tokens = babel7CompatTokens(this.tokens, this.input, this.startIndex);
  12534. }
  12535. return this.finishNode(file, "File");
  12536. }
  12537. parseProgram(program, end = 140, sourceType = this.options.sourceType) {
  12538. program.sourceType = sourceType;
  12539. program.interpreter = this.parseInterpreterDirective();
  12540. this.parseBlockBody(program, true, true, end);
  12541. if (this.inModule) {
  12542. if (!(this.optionFlags & 64) && this.scope.undefinedExports.size > 0) {
  12543. for (const [localName, at] of Array.from(this.scope.undefinedExports)) {
  12544. this.raise(Errors.ModuleExportUndefined, at, {
  12545. localName
  12546. });
  12547. }
  12548. }
  12549. this.addExtra(program, "topLevelAwait", this.state.hasTopLevelAwait);
  12550. }
  12551. let finishedProgram;
  12552. if (end === 140) {
  12553. finishedProgram = this.finishNode(program, "Program");
  12554. } else {
  12555. finishedProgram = this.finishNodeAt(program, "Program", createPositionWithColumnOffset(this.state.startLoc, -1));
  12556. }
  12557. return finishedProgram;
  12558. }
  12559. stmtToDirective(stmt) {
  12560. const directive = this.castNodeTo(stmt, "Directive");
  12561. const directiveLiteral = this.castNodeTo(stmt.expression, "DirectiveLiteral");
  12562. const expressionValue = directiveLiteral.value;
  12563. const raw = this.input.slice(this.offsetToSourcePos(directiveLiteral.start), this.offsetToSourcePos(directiveLiteral.end));
  12564. const val = directiveLiteral.value = raw.slice(1, -1);
  12565. this.addExtra(directiveLiteral, "raw", raw);
  12566. this.addExtra(directiveLiteral, "rawValue", val);
  12567. this.addExtra(directiveLiteral, "expressionValue", expressionValue);
  12568. directive.value = directiveLiteral;
  12569. delete stmt.expression;
  12570. return directive;
  12571. }
  12572. parseInterpreterDirective() {
  12573. if (!this.match(28)) {
  12574. return null;
  12575. }
  12576. const node = this.startNode();
  12577. node.value = this.state.value;
  12578. this.next();
  12579. return this.finishNode(node, "InterpreterDirective");
  12580. }
  12581. isLet() {
  12582. if (!this.isContextual(100)) {
  12583. return false;
  12584. }
  12585. return this.hasFollowingBindingAtom();
  12586. }
  12587. isUsing() {
  12588. if (!this.isContextual(107)) {
  12589. return false;
  12590. }
  12591. const next = this.nextTokenInLineStart();
  12592. const nextCh = this.codePointAtPos(next);
  12593. return this.chStartsBindingIdentifier(nextCh, next);
  12594. }
  12595. isAwaitUsing() {
  12596. if (!this.isContextual(96)) {
  12597. return false;
  12598. }
  12599. let next = this.nextTokenInLineStart();
  12600. if (this.isUnparsedContextual(next, "using")) {
  12601. next = this.nextTokenInLineStartSince(next + 5);
  12602. const nextCh = this.codePointAtPos(next);
  12603. if (this.chStartsBindingIdentifier(nextCh, next)) {
  12604. this.expectPlugin("explicitResourceManagement");
  12605. return true;
  12606. }
  12607. }
  12608. return false;
  12609. }
  12610. chStartsBindingIdentifier(ch, pos) {
  12611. if (isIdentifierStart(ch)) {
  12612. keywordRelationalOperator.lastIndex = pos;
  12613. if (keywordRelationalOperator.test(this.input)) {
  12614. const endCh = this.codePointAtPos(keywordRelationalOperator.lastIndex);
  12615. if (!isIdentifierChar(endCh) && endCh !== 92) {
  12616. return false;
  12617. }
  12618. }
  12619. return true;
  12620. } else if (ch === 92) {
  12621. return true;
  12622. } else {
  12623. return false;
  12624. }
  12625. }
  12626. chStartsBindingPattern(ch) {
  12627. return ch === 91 || ch === 123;
  12628. }
  12629. hasFollowingBindingAtom() {
  12630. const next = this.nextTokenStart();
  12631. const nextCh = this.codePointAtPos(next);
  12632. return this.chStartsBindingPattern(nextCh) || this.chStartsBindingIdentifier(nextCh, next);
  12633. }
  12634. hasInLineFollowingBindingIdentifierOrBrace() {
  12635. const next = this.nextTokenInLineStart();
  12636. const nextCh = this.codePointAtPos(next);
  12637. return nextCh === 123 || this.chStartsBindingIdentifier(nextCh, next);
  12638. }
  12639. allowsForUsing() {
  12640. const {
  12641. type,
  12642. containsEsc,
  12643. end
  12644. } = this.lookahead();
  12645. if (type === 102 && !containsEsc) {
  12646. const nextCharAfterOf = this.lookaheadCharCodeSince(end);
  12647. if (nextCharAfterOf !== 61 && nextCharAfterOf !== 58 && nextCharAfterOf !== 59) {
  12648. return false;
  12649. }
  12650. }
  12651. if (tokenIsIdentifier(type) && !this.hasFollowingLineBreak()) {
  12652. this.expectPlugin("explicitResourceManagement");
  12653. return true;
  12654. }
  12655. return false;
  12656. }
  12657. allowsUsing() {
  12658. return (this.scope.inModule || !this.scope.inTopLevel) && !this.scope.inBareCaseStatement;
  12659. }
  12660. parseModuleItem() {
  12661. return this.parseStatementLike(1 | 2 | 4 | 8);
  12662. }
  12663. parseStatementListItem() {
  12664. return this.parseStatementLike(2 | 4 | (!this.options.annexB || this.state.strict ? 0 : 8));
  12665. }
  12666. parseStatementOrSloppyAnnexBFunctionDeclaration(allowLabeledFunction = false) {
  12667. let flags = 0;
  12668. if (this.options.annexB && !this.state.strict) {
  12669. flags |= 4;
  12670. if (allowLabeledFunction) {
  12671. flags |= 8;
  12672. }
  12673. }
  12674. return this.parseStatementLike(flags);
  12675. }
  12676. parseStatement() {
  12677. return this.parseStatementLike(0);
  12678. }
  12679. parseStatementLike(flags) {
  12680. let decorators = null;
  12681. if (this.match(26)) {
  12682. decorators = this.parseDecorators(true);
  12683. }
  12684. return this.parseStatementContent(flags, decorators);
  12685. }
  12686. parseStatementContent(flags, decorators) {
  12687. const startType = this.state.type;
  12688. const node = this.startNode();
  12689. const allowDeclaration = !!(flags & 2);
  12690. const allowFunctionDeclaration = !!(flags & 4);
  12691. const topLevel = flags & 1;
  12692. switch (startType) {
  12693. case 60:
  12694. return this.parseBreakContinueStatement(node, true);
  12695. case 63:
  12696. return this.parseBreakContinueStatement(node, false);
  12697. case 64:
  12698. return this.parseDebuggerStatement(node);
  12699. case 90:
  12700. return this.parseDoWhileStatement(node);
  12701. case 91:
  12702. return this.parseForStatement(node);
  12703. case 68:
  12704. if (this.lookaheadCharCode() === 46) break;
  12705. if (!allowFunctionDeclaration) {
  12706. this.raise(this.state.strict ? Errors.StrictFunction : this.options.annexB ? Errors.SloppyFunctionAnnexB : Errors.SloppyFunction, this.state.startLoc);
  12707. }
  12708. return this.parseFunctionStatement(node, false, !allowDeclaration && allowFunctionDeclaration);
  12709. case 80:
  12710. if (!allowDeclaration) this.unexpected();
  12711. return this.parseClass(this.maybeTakeDecorators(decorators, node), true);
  12712. case 69:
  12713. return this.parseIfStatement(node);
  12714. case 70:
  12715. return this.parseReturnStatement(node);
  12716. case 71:
  12717. return this.parseSwitchStatement(node);
  12718. case 72:
  12719. return this.parseThrowStatement(node);
  12720. case 73:
  12721. return this.parseTryStatement(node);
  12722. case 96:
  12723. if (this.isAwaitUsing()) {
  12724. if (!this.allowsUsing()) {
  12725. this.raise(Errors.UnexpectedUsingDeclaration, node);
  12726. } else if (!allowDeclaration) {
  12727. this.raise(Errors.UnexpectedLexicalDeclaration, node);
  12728. } else if (!this.recordAwaitIfAllowed()) {
  12729. this.raise(Errors.AwaitUsingNotInAsyncContext, node);
  12730. }
  12731. this.next();
  12732. return this.parseVarStatement(node, "await using");
  12733. }
  12734. break;
  12735. case 107:
  12736. if (this.state.containsEsc || !this.hasInLineFollowingBindingIdentifierOrBrace()) {
  12737. break;
  12738. }
  12739. this.expectPlugin("explicitResourceManagement");
  12740. if (!this.allowsUsing()) {
  12741. this.raise(Errors.UnexpectedUsingDeclaration, this.state.startLoc);
  12742. } else if (!allowDeclaration) {
  12743. this.raise(Errors.UnexpectedLexicalDeclaration, this.state.startLoc);
  12744. }
  12745. return this.parseVarStatement(node, "using");
  12746. case 100:
  12747. {
  12748. if (this.state.containsEsc) {
  12749. break;
  12750. }
  12751. const next = this.nextTokenStart();
  12752. const nextCh = this.codePointAtPos(next);
  12753. if (nextCh !== 91) {
  12754. if (!allowDeclaration && this.hasFollowingLineBreak()) break;
  12755. if (!this.chStartsBindingIdentifier(nextCh, next) && nextCh !== 123) {
  12756. break;
  12757. }
  12758. }
  12759. }
  12760. case 75:
  12761. {
  12762. if (!allowDeclaration) {
  12763. this.raise(Errors.UnexpectedLexicalDeclaration, this.state.startLoc);
  12764. }
  12765. }
  12766. case 74:
  12767. {
  12768. const kind = this.state.value;
  12769. return this.parseVarStatement(node, kind);
  12770. }
  12771. case 92:
  12772. return this.parseWhileStatement(node);
  12773. case 76:
  12774. return this.parseWithStatement(node);
  12775. case 5:
  12776. return this.parseBlock();
  12777. case 13:
  12778. return this.parseEmptyStatement(node);
  12779. case 83:
  12780. {
  12781. const nextTokenCharCode = this.lookaheadCharCode();
  12782. if (nextTokenCharCode === 40 || nextTokenCharCode === 46) {
  12783. break;
  12784. }
  12785. }
  12786. case 82:
  12787. {
  12788. if (!(this.optionFlags & 8) && !topLevel) {
  12789. this.raise(Errors.UnexpectedImportExport, this.state.startLoc);
  12790. }
  12791. this.next();
  12792. let result;
  12793. if (startType === 83) {
  12794. result = this.parseImport(node);
  12795. } else {
  12796. result = this.parseExport(node, decorators);
  12797. }
  12798. this.assertModuleNodeAllowed(result);
  12799. return result;
  12800. }
  12801. default:
  12802. {
  12803. if (this.isAsyncFunction()) {
  12804. if (!allowDeclaration) {
  12805. this.raise(Errors.AsyncFunctionInSingleStatementContext, this.state.startLoc);
  12806. }
  12807. this.next();
  12808. return this.parseFunctionStatement(node, true, !allowDeclaration && allowFunctionDeclaration);
  12809. }
  12810. }
  12811. }
  12812. const maybeName = this.state.value;
  12813. const expr = this.parseExpression();
  12814. if (tokenIsIdentifier(startType) && expr.type === "Identifier" && this.eat(14)) {
  12815. return this.parseLabeledStatement(node, maybeName, expr, flags);
  12816. } else {
  12817. return this.parseExpressionStatement(node, expr, decorators);
  12818. }
  12819. }
  12820. assertModuleNodeAllowed(node) {
  12821. if (!(this.optionFlags & 8) && !this.inModule) {
  12822. this.raise(Errors.ImportOutsideModule, node);
  12823. }
  12824. }
  12825. decoratorsEnabledBeforeExport() {
  12826. if (this.hasPlugin("decorators-legacy")) return true;
  12827. return this.hasPlugin("decorators") && this.getPluginOption("decorators", "decoratorsBeforeExport") !== false;
  12828. }
  12829. maybeTakeDecorators(maybeDecorators, classNode, exportNode) {
  12830. if (maybeDecorators) {
  12831. var _classNode$decorators;
  12832. if ((_classNode$decorators = classNode.decorators) != null && _classNode$decorators.length) {
  12833. if (typeof this.getPluginOption("decorators", "decoratorsBeforeExport") !== "boolean") {
  12834. this.raise(Errors.DecoratorsBeforeAfterExport, classNode.decorators[0]);
  12835. }
  12836. classNode.decorators.unshift(...maybeDecorators);
  12837. } else {
  12838. classNode.decorators = maybeDecorators;
  12839. }
  12840. this.resetStartLocationFromNode(classNode, maybeDecorators[0]);
  12841. if (exportNode) this.resetStartLocationFromNode(exportNode, classNode);
  12842. }
  12843. return classNode;
  12844. }
  12845. canHaveLeadingDecorator() {
  12846. return this.match(80);
  12847. }
  12848. parseDecorators(allowExport) {
  12849. const decorators = [];
  12850. do {
  12851. decorators.push(this.parseDecorator());
  12852. } while (this.match(26));
  12853. if (this.match(82)) {
  12854. if (!allowExport) {
  12855. this.unexpected();
  12856. }
  12857. if (!this.decoratorsEnabledBeforeExport()) {
  12858. this.raise(Errors.DecoratorExportClass, this.state.startLoc);
  12859. }
  12860. } else if (!this.canHaveLeadingDecorator()) {
  12861. throw this.raise(Errors.UnexpectedLeadingDecorator, this.state.startLoc);
  12862. }
  12863. return decorators;
  12864. }
  12865. parseDecorator() {
  12866. this.expectOnePlugin(["decorators", "decorators-legacy"]);
  12867. const node = this.startNode();
  12868. this.next();
  12869. if (this.hasPlugin("decorators")) {
  12870. const startLoc = this.state.startLoc;
  12871. let expr;
  12872. if (this.match(10)) {
  12873. const startLoc = this.state.startLoc;
  12874. this.next();
  12875. expr = this.parseExpression();
  12876. this.expect(11);
  12877. expr = this.wrapParenthesis(startLoc, expr);
  12878. const paramsStartLoc = this.state.startLoc;
  12879. node.expression = this.parseMaybeDecoratorArguments(expr, startLoc);
  12880. if (this.getPluginOption("decorators", "allowCallParenthesized") === false && node.expression !== expr) {
  12881. this.raise(Errors.DecoratorArgumentsOutsideParentheses, paramsStartLoc);
  12882. }
  12883. } else {
  12884. expr = this.parseIdentifier(false);
  12885. while (this.eat(16)) {
  12886. const node = this.startNodeAt(startLoc);
  12887. node.object = expr;
  12888. if (this.match(139)) {
  12889. this.classScope.usePrivateName(this.state.value, this.state.startLoc);
  12890. node.property = this.parsePrivateName();
  12891. } else {
  12892. node.property = this.parseIdentifier(true);
  12893. }
  12894. node.computed = false;
  12895. expr = this.finishNode(node, "MemberExpression");
  12896. }
  12897. node.expression = this.parseMaybeDecoratorArguments(expr, startLoc);
  12898. }
  12899. } else {
  12900. node.expression = this.parseExprSubscripts();
  12901. }
  12902. return this.finishNode(node, "Decorator");
  12903. }
  12904. parseMaybeDecoratorArguments(expr, startLoc) {
  12905. if (this.eat(10)) {
  12906. const node = this.startNodeAt(startLoc);
  12907. node.callee = expr;
  12908. node.arguments = this.parseCallExpressionArguments(11);
  12909. this.toReferencedList(node.arguments);
  12910. return this.finishNode(node, "CallExpression");
  12911. }
  12912. return expr;
  12913. }
  12914. parseBreakContinueStatement(node, isBreak) {
  12915. this.next();
  12916. if (this.isLineTerminator()) {
  12917. node.label = null;
  12918. } else {
  12919. node.label = this.parseIdentifier();
  12920. this.semicolon();
  12921. }
  12922. this.verifyBreakContinue(node, isBreak);
  12923. return this.finishNode(node, isBreak ? "BreakStatement" : "ContinueStatement");
  12924. }
  12925. verifyBreakContinue(node, isBreak) {
  12926. let i;
  12927. for (i = 0; i < this.state.labels.length; ++i) {
  12928. const lab = this.state.labels[i];
  12929. if (node.label == null || lab.name === node.label.name) {
  12930. if (lab.kind != null && (isBreak || lab.kind === 1)) {
  12931. break;
  12932. }
  12933. if (node.label && isBreak) break;
  12934. }
  12935. }
  12936. if (i === this.state.labels.length) {
  12937. const type = isBreak ? "BreakStatement" : "ContinueStatement";
  12938. this.raise(Errors.IllegalBreakContinue, node, {
  12939. type
  12940. });
  12941. }
  12942. }
  12943. parseDebuggerStatement(node) {
  12944. this.next();
  12945. this.semicolon();
  12946. return this.finishNode(node, "DebuggerStatement");
  12947. }
  12948. parseHeaderExpression() {
  12949. this.expect(10);
  12950. const val = this.parseExpression();
  12951. this.expect(11);
  12952. return val;
  12953. }
  12954. parseDoWhileStatement(node) {
  12955. this.next();
  12956. this.state.labels.push(loopLabel);
  12957. node.body = this.withSmartMixTopicForbiddingContext(() => this.parseStatement());
  12958. this.state.labels.pop();
  12959. this.expect(92);
  12960. node.test = this.parseHeaderExpression();
  12961. this.eat(13);
  12962. return this.finishNode(node, "DoWhileStatement");
  12963. }
  12964. parseForStatement(node) {
  12965. this.next();
  12966. this.state.labels.push(loopLabel);
  12967. let awaitAt = null;
  12968. if (this.isContextual(96) && this.recordAwaitIfAllowed()) {
  12969. awaitAt = this.state.startLoc;
  12970. this.next();
  12971. }
  12972. this.scope.enter(0);
  12973. this.expect(10);
  12974. if (this.match(13)) {
  12975. if (awaitAt !== null) {
  12976. this.unexpected(awaitAt);
  12977. }
  12978. return this.parseFor(node, null);
  12979. }
  12980. const startsWithLet = this.isContextual(100);
  12981. {
  12982. const startsWithAwaitUsing = this.isAwaitUsing();
  12983. const starsWithUsingDeclaration = startsWithAwaitUsing || this.isContextual(107) && this.allowsForUsing();
  12984. const isLetOrUsing = startsWithLet && this.hasFollowingBindingAtom() || starsWithUsingDeclaration;
  12985. if (this.match(74) || this.match(75) || isLetOrUsing) {
  12986. const initNode = this.startNode();
  12987. let kind;
  12988. if (startsWithAwaitUsing) {
  12989. kind = "await using";
  12990. if (!this.recordAwaitIfAllowed()) {
  12991. this.raise(Errors.AwaitUsingNotInAsyncContext, this.state.startLoc);
  12992. }
  12993. this.next();
  12994. } else {
  12995. kind = this.state.value;
  12996. }
  12997. this.next();
  12998. this.parseVar(initNode, true, kind);
  12999. const init = this.finishNode(initNode, "VariableDeclaration");
  13000. const isForIn = this.match(58);
  13001. if (isForIn && starsWithUsingDeclaration) {
  13002. this.raise(Errors.ForInUsing, init);
  13003. }
  13004. if ((isForIn || this.isContextual(102)) && init.declarations.length === 1) {
  13005. return this.parseForIn(node, init, awaitAt);
  13006. }
  13007. if (awaitAt !== null) {
  13008. this.unexpected(awaitAt);
  13009. }
  13010. return this.parseFor(node, init);
  13011. }
  13012. }
  13013. const startsWithAsync = this.isContextual(95);
  13014. const refExpressionErrors = new ExpressionErrors();
  13015. const init = this.parseExpression(true, refExpressionErrors);
  13016. const isForOf = this.isContextual(102);
  13017. if (isForOf) {
  13018. if (startsWithLet) {
  13019. this.raise(Errors.ForOfLet, init);
  13020. }
  13021. if (awaitAt === null && startsWithAsync && init.type === "Identifier") {
  13022. this.raise(Errors.ForOfAsync, init);
  13023. }
  13024. }
  13025. if (isForOf || this.match(58)) {
  13026. this.checkDestructuringPrivate(refExpressionErrors);
  13027. this.toAssignable(init, true);
  13028. const type = isForOf ? "ForOfStatement" : "ForInStatement";
  13029. this.checkLVal(init, {
  13030. type
  13031. });
  13032. return this.parseForIn(node, init, awaitAt);
  13033. } else {
  13034. this.checkExpressionErrors(refExpressionErrors, true);
  13035. }
  13036. if (awaitAt !== null) {
  13037. this.unexpected(awaitAt);
  13038. }
  13039. return this.parseFor(node, init);
  13040. }
  13041. parseFunctionStatement(node, isAsync, isHangingDeclaration) {
  13042. this.next();
  13043. return this.parseFunction(node, 1 | (isHangingDeclaration ? 2 : 0) | (isAsync ? 8 : 0));
  13044. }
  13045. parseIfStatement(node) {
  13046. this.next();
  13047. node.test = this.parseHeaderExpression();
  13048. node.consequent = this.parseStatementOrSloppyAnnexBFunctionDeclaration();
  13049. node.alternate = this.eat(66) ? this.parseStatementOrSloppyAnnexBFunctionDeclaration() : null;
  13050. return this.finishNode(node, "IfStatement");
  13051. }
  13052. parseReturnStatement(node) {
  13053. if (!this.prodParam.hasReturn && !(this.optionFlags & 2)) {
  13054. this.raise(Errors.IllegalReturn, this.state.startLoc);
  13055. }
  13056. this.next();
  13057. if (this.isLineTerminator()) {
  13058. node.argument = null;
  13059. } else {
  13060. node.argument = this.parseExpression();
  13061. this.semicolon();
  13062. }
  13063. return this.finishNode(node, "ReturnStatement");
  13064. }
  13065. parseSwitchStatement(node) {
  13066. this.next();
  13067. node.discriminant = this.parseHeaderExpression();
  13068. const cases = node.cases = [];
  13069. this.expect(5);
  13070. this.state.labels.push(switchLabel);
  13071. this.scope.enter(256);
  13072. let cur;
  13073. for (let sawDefault; !this.match(8);) {
  13074. if (this.match(61) || this.match(65)) {
  13075. const isCase = this.match(61);
  13076. if (cur) this.finishNode(cur, "SwitchCase");
  13077. cases.push(cur = this.startNode());
  13078. cur.consequent = [];
  13079. this.next();
  13080. if (isCase) {
  13081. cur.test = this.parseExpression();
  13082. } else {
  13083. if (sawDefault) {
  13084. this.raise(Errors.MultipleDefaultsInSwitch, this.state.lastTokStartLoc);
  13085. }
  13086. sawDefault = true;
  13087. cur.test = null;
  13088. }
  13089. this.expect(14);
  13090. } else {
  13091. if (cur) {
  13092. cur.consequent.push(this.parseStatementListItem());
  13093. } else {
  13094. this.unexpected();
  13095. }
  13096. }
  13097. }
  13098. this.scope.exit();
  13099. if (cur) this.finishNode(cur, "SwitchCase");
  13100. this.next();
  13101. this.state.labels.pop();
  13102. return this.finishNode(node, "SwitchStatement");
  13103. }
  13104. parseThrowStatement(node) {
  13105. this.next();
  13106. if (this.hasPrecedingLineBreak()) {
  13107. this.raise(Errors.NewlineAfterThrow, this.state.lastTokEndLoc);
  13108. }
  13109. node.argument = this.parseExpression();
  13110. this.semicolon();
  13111. return this.finishNode(node, "ThrowStatement");
  13112. }
  13113. parseCatchClauseParam() {
  13114. const param = this.parseBindingAtom();
  13115. this.scope.enter(this.options.annexB && param.type === "Identifier" ? 8 : 0);
  13116. this.checkLVal(param, {
  13117. type: "CatchClause"
  13118. }, 9);
  13119. return param;
  13120. }
  13121. parseTryStatement(node) {
  13122. this.next();
  13123. node.block = this.parseBlock();
  13124. node.handler = null;
  13125. if (this.match(62)) {
  13126. const clause = this.startNode();
  13127. this.next();
  13128. if (this.match(10)) {
  13129. this.expect(10);
  13130. clause.param = this.parseCatchClauseParam();
  13131. this.expect(11);
  13132. } else {
  13133. clause.param = null;
  13134. this.scope.enter(0);
  13135. }
  13136. clause.body = this.withSmartMixTopicForbiddingContext(() => this.parseBlock(false, false));
  13137. this.scope.exit();
  13138. node.handler = this.finishNode(clause, "CatchClause");
  13139. }
  13140. node.finalizer = this.eat(67) ? this.parseBlock() : null;
  13141. if (!node.handler && !node.finalizer) {
  13142. this.raise(Errors.NoCatchOrFinally, node);
  13143. }
  13144. return this.finishNode(node, "TryStatement");
  13145. }
  13146. parseVarStatement(node, kind, allowMissingInitializer = false) {
  13147. this.next();
  13148. this.parseVar(node, false, kind, allowMissingInitializer);
  13149. this.semicolon();
  13150. return this.finishNode(node, "VariableDeclaration");
  13151. }
  13152. parseWhileStatement(node) {
  13153. this.next();
  13154. node.test = this.parseHeaderExpression();
  13155. this.state.labels.push(loopLabel);
  13156. node.body = this.withSmartMixTopicForbiddingContext(() => this.parseStatement());
  13157. this.state.labels.pop();
  13158. return this.finishNode(node, "WhileStatement");
  13159. }
  13160. parseWithStatement(node) {
  13161. if (this.state.strict) {
  13162. this.raise(Errors.StrictWith, this.state.startLoc);
  13163. }
  13164. this.next();
  13165. node.object = this.parseHeaderExpression();
  13166. node.body = this.withSmartMixTopicForbiddingContext(() => this.parseStatement());
  13167. return this.finishNode(node, "WithStatement");
  13168. }
  13169. parseEmptyStatement(node) {
  13170. this.next();
  13171. return this.finishNode(node, "EmptyStatement");
  13172. }
  13173. parseLabeledStatement(node, maybeName, expr, flags) {
  13174. for (const label of this.state.labels) {
  13175. if (label.name === maybeName) {
  13176. this.raise(Errors.LabelRedeclaration, expr, {
  13177. labelName: maybeName
  13178. });
  13179. }
  13180. }
  13181. const kind = tokenIsLoop(this.state.type) ? 1 : this.match(71) ? 2 : null;
  13182. for (let i = this.state.labels.length - 1; i >= 0; i--) {
  13183. const label = this.state.labels[i];
  13184. if (label.statementStart === node.start) {
  13185. label.statementStart = this.sourceToOffsetPos(this.state.start);
  13186. label.kind = kind;
  13187. } else {
  13188. break;
  13189. }
  13190. }
  13191. this.state.labels.push({
  13192. name: maybeName,
  13193. kind: kind,
  13194. statementStart: this.sourceToOffsetPos(this.state.start)
  13195. });
  13196. node.body = flags & 8 ? this.parseStatementOrSloppyAnnexBFunctionDeclaration(true) : this.parseStatement();
  13197. this.state.labels.pop();
  13198. node.label = expr;
  13199. return this.finishNode(node, "LabeledStatement");
  13200. }
  13201. parseExpressionStatement(node, expr, decorators) {
  13202. node.expression = expr;
  13203. this.semicolon();
  13204. return this.finishNode(node, "ExpressionStatement");
  13205. }
  13206. parseBlock(allowDirectives = false, createNewLexicalScope = true, afterBlockParse) {
  13207. const node = this.startNode();
  13208. if (allowDirectives) {
  13209. this.state.strictErrors.clear();
  13210. }
  13211. this.expect(5);
  13212. if (createNewLexicalScope) {
  13213. this.scope.enter(0);
  13214. }
  13215. this.parseBlockBody(node, allowDirectives, false, 8, afterBlockParse);
  13216. if (createNewLexicalScope) {
  13217. this.scope.exit();
  13218. }
  13219. return this.finishNode(node, "BlockStatement");
  13220. }
  13221. isValidDirective(stmt) {
  13222. return stmt.type === "ExpressionStatement" && stmt.expression.type === "StringLiteral" && !stmt.expression.extra.parenthesized;
  13223. }
  13224. parseBlockBody(node, allowDirectives, topLevel, end, afterBlockParse) {
  13225. const body = node.body = [];
  13226. const directives = node.directives = [];
  13227. this.parseBlockOrModuleBlockBody(body, allowDirectives ? directives : undefined, topLevel, end, afterBlockParse);
  13228. }
  13229. parseBlockOrModuleBlockBody(body, directives, topLevel, end, afterBlockParse) {
  13230. const oldStrict = this.state.strict;
  13231. let hasStrictModeDirective = false;
  13232. let parsedNonDirective = false;
  13233. while (!this.match(end)) {
  13234. const stmt = topLevel ? this.parseModuleItem() : this.parseStatementListItem();
  13235. if (directives && !parsedNonDirective) {
  13236. if (this.isValidDirective(stmt)) {
  13237. const directive = this.stmtToDirective(stmt);
  13238. directives.push(directive);
  13239. if (!hasStrictModeDirective && directive.value.value === "use strict") {
  13240. hasStrictModeDirective = true;
  13241. this.setStrict(true);
  13242. }
  13243. continue;
  13244. }
  13245. parsedNonDirective = true;
  13246. this.state.strictErrors.clear();
  13247. }
  13248. body.push(stmt);
  13249. }
  13250. afterBlockParse == null || afterBlockParse.call(this, hasStrictModeDirective);
  13251. if (!oldStrict) {
  13252. this.setStrict(false);
  13253. }
  13254. this.next();
  13255. }
  13256. parseFor(node, init) {
  13257. node.init = init;
  13258. this.semicolon(false);
  13259. node.test = this.match(13) ? null : this.parseExpression();
  13260. this.semicolon(false);
  13261. node.update = this.match(11) ? null : this.parseExpression();
  13262. this.expect(11);
  13263. node.body = this.withSmartMixTopicForbiddingContext(() => this.parseStatement());
  13264. this.scope.exit();
  13265. this.state.labels.pop();
  13266. return this.finishNode(node, "ForStatement");
  13267. }
  13268. parseForIn(node, init, awaitAt) {
  13269. const isForIn = this.match(58);
  13270. this.next();
  13271. if (isForIn) {
  13272. if (awaitAt !== null) this.unexpected(awaitAt);
  13273. } else {
  13274. node.await = awaitAt !== null;
  13275. }
  13276. 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")) {
  13277. this.raise(Errors.ForInOfLoopInitializer, init, {
  13278. type: isForIn ? "ForInStatement" : "ForOfStatement"
  13279. });
  13280. }
  13281. if (init.type === "AssignmentPattern") {
  13282. this.raise(Errors.InvalidLhs, init, {
  13283. ancestor: {
  13284. type: "ForStatement"
  13285. }
  13286. });
  13287. }
  13288. node.left = init;
  13289. node.right = isForIn ? this.parseExpression() : this.parseMaybeAssignAllowIn();
  13290. this.expect(11);
  13291. node.body = this.withSmartMixTopicForbiddingContext(() => this.parseStatement());
  13292. this.scope.exit();
  13293. this.state.labels.pop();
  13294. return this.finishNode(node, isForIn ? "ForInStatement" : "ForOfStatement");
  13295. }
  13296. parseVar(node, isFor, kind, allowMissingInitializer = false) {
  13297. const declarations = node.declarations = [];
  13298. node.kind = kind;
  13299. for (;;) {
  13300. const decl = this.startNode();
  13301. this.parseVarId(decl, kind);
  13302. decl.init = !this.eat(29) ? null : isFor ? this.parseMaybeAssignDisallowIn() : this.parseMaybeAssignAllowIn();
  13303. if (decl.init === null && !allowMissingInitializer) {
  13304. if (decl.id.type !== "Identifier" && !(isFor && (this.match(58) || this.isContextual(102)))) {
  13305. this.raise(Errors.DeclarationMissingInitializer, this.state.lastTokEndLoc, {
  13306. kind: "destructuring"
  13307. });
  13308. } else if ((kind === "const" || kind === "using" || kind === "await using") && !(this.match(58) || this.isContextual(102))) {
  13309. this.raise(Errors.DeclarationMissingInitializer, this.state.lastTokEndLoc, {
  13310. kind
  13311. });
  13312. }
  13313. }
  13314. declarations.push(this.finishNode(decl, "VariableDeclarator"));
  13315. if (!this.eat(12)) break;
  13316. }
  13317. return node;
  13318. }
  13319. parseVarId(decl, kind) {
  13320. const id = this.parseBindingAtom();
  13321. if (kind === "using" || kind === "await using") {
  13322. if (id.type === "ArrayPattern" || id.type === "ObjectPattern") {
  13323. this.raise(Errors.UsingDeclarationHasBindingPattern, id.loc.start);
  13324. }
  13325. }
  13326. this.checkLVal(id, {
  13327. type: "VariableDeclarator"
  13328. }, kind === "var" ? 5 : 8201);
  13329. decl.id = id;
  13330. }
  13331. parseAsyncFunctionExpression(node) {
  13332. return this.parseFunction(node, 8);
  13333. }
  13334. parseFunction(node, flags = 0) {
  13335. const hangingDeclaration = flags & 2;
  13336. const isDeclaration = !!(flags & 1);
  13337. const requireId = isDeclaration && !(flags & 4);
  13338. const isAsync = !!(flags & 8);
  13339. this.initFunction(node, isAsync);
  13340. if (this.match(55)) {
  13341. if (hangingDeclaration) {
  13342. this.raise(Errors.GeneratorInSingleStatementContext, this.state.startLoc);
  13343. }
  13344. this.next();
  13345. node.generator = true;
  13346. }
  13347. if (isDeclaration) {
  13348. node.id = this.parseFunctionId(requireId);
  13349. }
  13350. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  13351. this.state.maybeInArrowParameters = false;
  13352. this.scope.enter(2);
  13353. this.prodParam.enter(functionFlags(isAsync, node.generator));
  13354. if (!isDeclaration) {
  13355. node.id = this.parseFunctionId();
  13356. }
  13357. this.parseFunctionParams(node, false);
  13358. this.withSmartMixTopicForbiddingContext(() => {
  13359. this.parseFunctionBodyAndFinish(node, isDeclaration ? "FunctionDeclaration" : "FunctionExpression");
  13360. });
  13361. this.prodParam.exit();
  13362. this.scope.exit();
  13363. if (isDeclaration && !hangingDeclaration) {
  13364. this.registerFunctionStatementId(node);
  13365. }
  13366. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  13367. return node;
  13368. }
  13369. parseFunctionId(requireId) {
  13370. return requireId || tokenIsIdentifier(this.state.type) ? this.parseIdentifier() : null;
  13371. }
  13372. parseFunctionParams(node, isConstructor) {
  13373. this.expect(10);
  13374. this.expressionScope.enter(newParameterDeclarationScope());
  13375. node.params = this.parseBindingList(11, 41, 2 | (isConstructor ? 4 : 0));
  13376. this.expressionScope.exit();
  13377. }
  13378. registerFunctionStatementId(node) {
  13379. if (!node.id) return;
  13380. 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);
  13381. }
  13382. parseClass(node, isStatement, optionalId) {
  13383. this.next();
  13384. const oldStrict = this.state.strict;
  13385. this.state.strict = true;
  13386. this.parseClassId(node, isStatement, optionalId);
  13387. this.parseClassSuper(node);
  13388. node.body = this.parseClassBody(!!node.superClass, oldStrict);
  13389. return this.finishNode(node, isStatement ? "ClassDeclaration" : "ClassExpression");
  13390. }
  13391. isClassProperty() {
  13392. return this.match(29) || this.match(13) || this.match(8);
  13393. }
  13394. isClassMethod() {
  13395. return this.match(10);
  13396. }
  13397. nameIsConstructor(key) {
  13398. return key.type === "Identifier" && key.name === "constructor" || key.type === "StringLiteral" && key.value === "constructor";
  13399. }
  13400. isNonstaticConstructor(method) {
  13401. return !method.computed && !method.static && this.nameIsConstructor(method.key);
  13402. }
  13403. parseClassBody(hadSuperClass, oldStrict) {
  13404. this.classScope.enter();
  13405. const state = {
  13406. hadConstructor: false,
  13407. hadSuperClass
  13408. };
  13409. let decorators = [];
  13410. const classBody = this.startNode();
  13411. classBody.body = [];
  13412. this.expect(5);
  13413. this.withSmartMixTopicForbiddingContext(() => {
  13414. while (!this.match(8)) {
  13415. if (this.eat(13)) {
  13416. if (decorators.length > 0) {
  13417. throw this.raise(Errors.DecoratorSemicolon, this.state.lastTokEndLoc);
  13418. }
  13419. continue;
  13420. }
  13421. if (this.match(26)) {
  13422. decorators.push(this.parseDecorator());
  13423. continue;
  13424. }
  13425. const member = this.startNode();
  13426. if (decorators.length) {
  13427. member.decorators = decorators;
  13428. this.resetStartLocationFromNode(member, decorators[0]);
  13429. decorators = [];
  13430. }
  13431. this.parseClassMember(classBody, member, state);
  13432. if (member.kind === "constructor" && member.decorators && member.decorators.length > 0) {
  13433. this.raise(Errors.DecoratorConstructor, member);
  13434. }
  13435. }
  13436. });
  13437. this.state.strict = oldStrict;
  13438. this.next();
  13439. if (decorators.length) {
  13440. throw this.raise(Errors.TrailingDecorator, this.state.startLoc);
  13441. }
  13442. this.classScope.exit();
  13443. return this.finishNode(classBody, "ClassBody");
  13444. }
  13445. parseClassMemberFromModifier(classBody, member) {
  13446. const key = this.parseIdentifier(true);
  13447. if (this.isClassMethod()) {
  13448. const method = member;
  13449. method.kind = "method";
  13450. method.computed = false;
  13451. method.key = key;
  13452. method.static = false;
  13453. this.pushClassMethod(classBody, method, false, false, false, false);
  13454. return true;
  13455. } else if (this.isClassProperty()) {
  13456. const prop = member;
  13457. prop.computed = false;
  13458. prop.key = key;
  13459. prop.static = false;
  13460. classBody.body.push(this.parseClassProperty(prop));
  13461. return true;
  13462. }
  13463. this.resetPreviousNodeTrailingComments(key);
  13464. return false;
  13465. }
  13466. parseClassMember(classBody, member, state) {
  13467. const isStatic = this.isContextual(106);
  13468. if (isStatic) {
  13469. if (this.parseClassMemberFromModifier(classBody, member)) {
  13470. return;
  13471. }
  13472. if (this.eat(5)) {
  13473. this.parseClassStaticBlock(classBody, member);
  13474. return;
  13475. }
  13476. }
  13477. this.parseClassMemberWithIsStatic(classBody, member, state, isStatic);
  13478. }
  13479. parseClassMemberWithIsStatic(classBody, member, state, isStatic) {
  13480. const publicMethod = member;
  13481. const privateMethod = member;
  13482. const publicProp = member;
  13483. const privateProp = member;
  13484. const accessorProp = member;
  13485. const method = publicMethod;
  13486. const publicMember = publicMethod;
  13487. member.static = isStatic;
  13488. this.parsePropertyNamePrefixOperator(member);
  13489. if (this.eat(55)) {
  13490. method.kind = "method";
  13491. const isPrivateName = this.match(139);
  13492. this.parseClassElementName(method);
  13493. this.parsePostMemberNameModifiers(method);
  13494. if (isPrivateName) {
  13495. this.pushClassPrivateMethod(classBody, privateMethod, true, false);
  13496. return;
  13497. }
  13498. if (this.isNonstaticConstructor(publicMethod)) {
  13499. this.raise(Errors.ConstructorIsGenerator, publicMethod.key);
  13500. }
  13501. this.pushClassMethod(classBody, publicMethod, true, false, false, false);
  13502. return;
  13503. }
  13504. const isContextual = !this.state.containsEsc && tokenIsIdentifier(this.state.type);
  13505. const key = this.parseClassElementName(member);
  13506. const maybeContextualKw = isContextual ? key.name : null;
  13507. const isPrivate = this.isPrivateName(key);
  13508. const maybeQuestionTokenStartLoc = this.state.startLoc;
  13509. this.parsePostMemberNameModifiers(publicMember);
  13510. if (this.isClassMethod()) {
  13511. method.kind = "method";
  13512. if (isPrivate) {
  13513. this.pushClassPrivateMethod(classBody, privateMethod, false, false);
  13514. return;
  13515. }
  13516. const isConstructor = this.isNonstaticConstructor(publicMethod);
  13517. let allowsDirectSuper = false;
  13518. if (isConstructor) {
  13519. publicMethod.kind = "constructor";
  13520. if (state.hadConstructor && !this.hasPlugin("typescript")) {
  13521. this.raise(Errors.DuplicateConstructor, key);
  13522. }
  13523. if (isConstructor && this.hasPlugin("typescript") && member.override) {
  13524. this.raise(Errors.OverrideOnConstructor, key);
  13525. }
  13526. state.hadConstructor = true;
  13527. allowsDirectSuper = state.hadSuperClass;
  13528. }
  13529. this.pushClassMethod(classBody, publicMethod, false, false, isConstructor, allowsDirectSuper);
  13530. } else if (this.isClassProperty()) {
  13531. if (isPrivate) {
  13532. this.pushClassPrivateProperty(classBody, privateProp);
  13533. } else {
  13534. this.pushClassProperty(classBody, publicProp);
  13535. }
  13536. } else if (maybeContextualKw === "async" && !this.isLineTerminator()) {
  13537. this.resetPreviousNodeTrailingComments(key);
  13538. const isGenerator = this.eat(55);
  13539. if (publicMember.optional) {
  13540. this.unexpected(maybeQuestionTokenStartLoc);
  13541. }
  13542. method.kind = "method";
  13543. const isPrivate = this.match(139);
  13544. this.parseClassElementName(method);
  13545. this.parsePostMemberNameModifiers(publicMember);
  13546. if (isPrivate) {
  13547. this.pushClassPrivateMethod(classBody, privateMethod, isGenerator, true);
  13548. } else {
  13549. if (this.isNonstaticConstructor(publicMethod)) {
  13550. this.raise(Errors.ConstructorIsAsync, publicMethod.key);
  13551. }
  13552. this.pushClassMethod(classBody, publicMethod, isGenerator, true, false, false);
  13553. }
  13554. } else if ((maybeContextualKw === "get" || maybeContextualKw === "set") && !(this.match(55) && this.isLineTerminator())) {
  13555. this.resetPreviousNodeTrailingComments(key);
  13556. method.kind = maybeContextualKw;
  13557. const isPrivate = this.match(139);
  13558. this.parseClassElementName(publicMethod);
  13559. if (isPrivate) {
  13560. this.pushClassPrivateMethod(classBody, privateMethod, false, false);
  13561. } else {
  13562. if (this.isNonstaticConstructor(publicMethod)) {
  13563. this.raise(Errors.ConstructorIsAccessor, publicMethod.key);
  13564. }
  13565. this.pushClassMethod(classBody, publicMethod, false, false, false, false);
  13566. }
  13567. this.checkGetterSetterParams(publicMethod);
  13568. } else if (maybeContextualKw === "accessor" && !this.isLineTerminator()) {
  13569. this.expectPlugin("decoratorAutoAccessors");
  13570. this.resetPreviousNodeTrailingComments(key);
  13571. const isPrivate = this.match(139);
  13572. this.parseClassElementName(publicProp);
  13573. this.pushClassAccessorProperty(classBody, accessorProp, isPrivate);
  13574. } else if (this.isLineTerminator()) {
  13575. if (isPrivate) {
  13576. this.pushClassPrivateProperty(classBody, privateProp);
  13577. } else {
  13578. this.pushClassProperty(classBody, publicProp);
  13579. }
  13580. } else {
  13581. this.unexpected();
  13582. }
  13583. }
  13584. parseClassElementName(member) {
  13585. const {
  13586. type,
  13587. value
  13588. } = this.state;
  13589. if ((type === 132 || type === 134) && member.static && value === "prototype") {
  13590. this.raise(Errors.StaticPrototype, this.state.startLoc);
  13591. }
  13592. if (type === 139) {
  13593. if (value === "constructor") {
  13594. this.raise(Errors.ConstructorClassPrivateField, this.state.startLoc);
  13595. }
  13596. const key = this.parsePrivateName();
  13597. member.key = key;
  13598. return key;
  13599. }
  13600. this.parsePropertyName(member);
  13601. return member.key;
  13602. }
  13603. parseClassStaticBlock(classBody, member) {
  13604. var _member$decorators;
  13605. this.scope.enter(64 | 128 | 16);
  13606. const oldLabels = this.state.labels;
  13607. this.state.labels = [];
  13608. this.prodParam.enter(0);
  13609. const body = member.body = [];
  13610. this.parseBlockOrModuleBlockBody(body, undefined, false, 8);
  13611. this.prodParam.exit();
  13612. this.scope.exit();
  13613. this.state.labels = oldLabels;
  13614. classBody.body.push(this.finishNode(member, "StaticBlock"));
  13615. if ((_member$decorators = member.decorators) != null && _member$decorators.length) {
  13616. this.raise(Errors.DecoratorStaticBlock, member);
  13617. }
  13618. }
  13619. pushClassProperty(classBody, prop) {
  13620. if (!prop.computed && this.nameIsConstructor(prop.key)) {
  13621. this.raise(Errors.ConstructorClassField, prop.key);
  13622. }
  13623. classBody.body.push(this.parseClassProperty(prop));
  13624. }
  13625. pushClassPrivateProperty(classBody, prop) {
  13626. const node = this.parseClassPrivateProperty(prop);
  13627. classBody.body.push(node);
  13628. this.classScope.declarePrivateName(this.getPrivateNameSV(node.key), 0, node.key.loc.start);
  13629. }
  13630. pushClassAccessorProperty(classBody, prop, isPrivate) {
  13631. if (!isPrivate && !prop.computed && this.nameIsConstructor(prop.key)) {
  13632. this.raise(Errors.ConstructorClassField, prop.key);
  13633. }
  13634. const node = this.parseClassAccessorProperty(prop);
  13635. classBody.body.push(node);
  13636. if (isPrivate) {
  13637. this.classScope.declarePrivateName(this.getPrivateNameSV(node.key), 0, node.key.loc.start);
  13638. }
  13639. }
  13640. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  13641. classBody.body.push(this.parseMethod(method, isGenerator, isAsync, isConstructor, allowsDirectSuper, "ClassMethod", true));
  13642. }
  13643. pushClassPrivateMethod(classBody, method, isGenerator, isAsync) {
  13644. const node = this.parseMethod(method, isGenerator, isAsync, false, false, "ClassPrivateMethod", true);
  13645. classBody.body.push(node);
  13646. const kind = node.kind === "get" ? node.static ? 6 : 2 : node.kind === "set" ? node.static ? 5 : 1 : 0;
  13647. this.declareClassPrivateMethodInScope(node, kind);
  13648. }
  13649. declareClassPrivateMethodInScope(node, kind) {
  13650. this.classScope.declarePrivateName(this.getPrivateNameSV(node.key), kind, node.key.loc.start);
  13651. }
  13652. parsePostMemberNameModifiers(methodOrProp) {}
  13653. parseClassPrivateProperty(node) {
  13654. this.parseInitializer(node);
  13655. this.semicolon();
  13656. return this.finishNode(node, "ClassPrivateProperty");
  13657. }
  13658. parseClassProperty(node) {
  13659. this.parseInitializer(node);
  13660. this.semicolon();
  13661. return this.finishNode(node, "ClassProperty");
  13662. }
  13663. parseClassAccessorProperty(node) {
  13664. this.parseInitializer(node);
  13665. this.semicolon();
  13666. return this.finishNode(node, "ClassAccessorProperty");
  13667. }
  13668. parseInitializer(node) {
  13669. this.scope.enter(64 | 16);
  13670. this.expressionScope.enter(newExpressionScope());
  13671. this.prodParam.enter(0);
  13672. node.value = this.eat(29) ? this.parseMaybeAssignAllowIn() : null;
  13673. this.expressionScope.exit();
  13674. this.prodParam.exit();
  13675. this.scope.exit();
  13676. }
  13677. parseClassId(node, isStatement, optionalId, bindingType = 8331) {
  13678. if (tokenIsIdentifier(this.state.type)) {
  13679. node.id = this.parseIdentifier();
  13680. if (isStatement) {
  13681. this.declareNameFromIdentifier(node.id, bindingType);
  13682. }
  13683. } else {
  13684. if (optionalId || !isStatement) {
  13685. node.id = null;
  13686. } else {
  13687. throw this.raise(Errors.MissingClassName, this.state.startLoc);
  13688. }
  13689. }
  13690. }
  13691. parseClassSuper(node) {
  13692. node.superClass = this.eat(81) ? this.parseExprSubscripts() : null;
  13693. }
  13694. parseExport(node, decorators) {
  13695. const maybeDefaultIdentifier = this.parseMaybeImportPhase(node, true);
  13696. const hasDefault = this.maybeParseExportDefaultSpecifier(node, maybeDefaultIdentifier);
  13697. const parseAfterDefault = !hasDefault || this.eat(12);
  13698. const hasStar = parseAfterDefault && this.eatExportStar(node);
  13699. const hasNamespace = hasStar && this.maybeParseExportNamespaceSpecifier(node);
  13700. const parseAfterNamespace = parseAfterDefault && (!hasNamespace || this.eat(12));
  13701. const isFromRequired = hasDefault || hasStar;
  13702. if (hasStar && !hasNamespace) {
  13703. if (hasDefault) this.unexpected();
  13704. if (decorators) {
  13705. throw this.raise(Errors.UnsupportedDecoratorExport, node);
  13706. }
  13707. this.parseExportFrom(node, true);
  13708. this.sawUnambiguousESM = true;
  13709. return this.finishNode(node, "ExportAllDeclaration");
  13710. }
  13711. const hasSpecifiers = this.maybeParseExportNamedSpecifiers(node);
  13712. if (hasDefault && parseAfterDefault && !hasStar && !hasSpecifiers) {
  13713. this.unexpected(null, 5);
  13714. }
  13715. if (hasNamespace && parseAfterNamespace) {
  13716. this.unexpected(null, 98);
  13717. }
  13718. let hasDeclaration;
  13719. if (isFromRequired || hasSpecifiers) {
  13720. hasDeclaration = false;
  13721. if (decorators) {
  13722. throw this.raise(Errors.UnsupportedDecoratorExport, node);
  13723. }
  13724. this.parseExportFrom(node, isFromRequired);
  13725. } else {
  13726. hasDeclaration = this.maybeParseExportDeclaration(node);
  13727. }
  13728. if (isFromRequired || hasSpecifiers || hasDeclaration) {
  13729. var _node2$declaration;
  13730. const node2 = node;
  13731. this.checkExport(node2, true, false, !!node2.source);
  13732. if (((_node2$declaration = node2.declaration) == null ? void 0 : _node2$declaration.type) === "ClassDeclaration") {
  13733. this.maybeTakeDecorators(decorators, node2.declaration, node2);
  13734. } else if (decorators) {
  13735. throw this.raise(Errors.UnsupportedDecoratorExport, node);
  13736. }
  13737. this.sawUnambiguousESM = true;
  13738. return this.finishNode(node2, "ExportNamedDeclaration");
  13739. }
  13740. if (this.eat(65)) {
  13741. const node2 = node;
  13742. const decl = this.parseExportDefaultExpression();
  13743. node2.declaration = decl;
  13744. if (decl.type === "ClassDeclaration") {
  13745. this.maybeTakeDecorators(decorators, decl, node2);
  13746. } else if (decorators) {
  13747. throw this.raise(Errors.UnsupportedDecoratorExport, node);
  13748. }
  13749. this.checkExport(node2, true, true);
  13750. this.sawUnambiguousESM = true;
  13751. return this.finishNode(node2, "ExportDefaultDeclaration");
  13752. }
  13753. this.unexpected(null, 5);
  13754. }
  13755. eatExportStar(node) {
  13756. return this.eat(55);
  13757. }
  13758. maybeParseExportDefaultSpecifier(node, maybeDefaultIdentifier) {
  13759. if (maybeDefaultIdentifier || this.isExportDefaultSpecifier()) {
  13760. this.expectPlugin("exportDefaultFrom", maybeDefaultIdentifier == null ? void 0 : maybeDefaultIdentifier.loc.start);
  13761. const id = maybeDefaultIdentifier || this.parseIdentifier(true);
  13762. const specifier = this.startNodeAtNode(id);
  13763. specifier.exported = id;
  13764. node.specifiers = [this.finishNode(specifier, "ExportDefaultSpecifier")];
  13765. return true;
  13766. }
  13767. return false;
  13768. }
  13769. maybeParseExportNamespaceSpecifier(node) {
  13770. if (this.isContextual(93)) {
  13771. var _ref, _ref$specifiers;
  13772. (_ref$specifiers = (_ref = node).specifiers) != null ? _ref$specifiers : _ref.specifiers = [];
  13773. const specifier = this.startNodeAt(this.state.lastTokStartLoc);
  13774. this.next();
  13775. specifier.exported = this.parseModuleExportName();
  13776. node.specifiers.push(this.finishNode(specifier, "ExportNamespaceSpecifier"));
  13777. return true;
  13778. }
  13779. return false;
  13780. }
  13781. maybeParseExportNamedSpecifiers(node) {
  13782. if (this.match(5)) {
  13783. const node2 = node;
  13784. if (!node2.specifiers) node2.specifiers = [];
  13785. const isTypeExport = node2.exportKind === "type";
  13786. node2.specifiers.push(...this.parseExportSpecifiers(isTypeExport));
  13787. node2.source = null;
  13788. if (this.hasPlugin("importAssertions")) {
  13789. node2.assertions = [];
  13790. } else {
  13791. node2.attributes = [];
  13792. }
  13793. node2.declaration = null;
  13794. return true;
  13795. }
  13796. return false;
  13797. }
  13798. maybeParseExportDeclaration(node) {
  13799. if (this.shouldParseExportDeclaration()) {
  13800. node.specifiers = [];
  13801. node.source = null;
  13802. if (this.hasPlugin("importAssertions")) {
  13803. node.assertions = [];
  13804. } else {
  13805. node.attributes = [];
  13806. }
  13807. node.declaration = this.parseExportDeclaration(node);
  13808. return true;
  13809. }
  13810. return false;
  13811. }
  13812. isAsyncFunction() {
  13813. if (!this.isContextual(95)) return false;
  13814. const next = this.nextTokenInLineStart();
  13815. return this.isUnparsedContextual(next, "function");
  13816. }
  13817. parseExportDefaultExpression() {
  13818. const expr = this.startNode();
  13819. if (this.match(68)) {
  13820. this.next();
  13821. return this.parseFunction(expr, 1 | 4);
  13822. } else if (this.isAsyncFunction()) {
  13823. this.next();
  13824. this.next();
  13825. return this.parseFunction(expr, 1 | 4 | 8);
  13826. }
  13827. if (this.match(80)) {
  13828. return this.parseClass(expr, true, true);
  13829. }
  13830. if (this.match(26)) {
  13831. if (this.hasPlugin("decorators") && this.getPluginOption("decorators", "decoratorsBeforeExport") === true) {
  13832. this.raise(Errors.DecoratorBeforeExport, this.state.startLoc);
  13833. }
  13834. return this.parseClass(this.maybeTakeDecorators(this.parseDecorators(false), this.startNode()), true, true);
  13835. }
  13836. if (this.match(75) || this.match(74) || this.isLet() || this.hasPlugin("explicitResourceManagement") && (this.isUsing() || this.isAwaitUsing())) {
  13837. throw this.raise(Errors.UnsupportedDefaultExport, this.state.startLoc);
  13838. }
  13839. const res = this.parseMaybeAssignAllowIn();
  13840. this.semicolon();
  13841. return res;
  13842. }
  13843. parseExportDeclaration(node) {
  13844. if (this.match(80)) {
  13845. const node = this.parseClass(this.startNode(), true, false);
  13846. return node;
  13847. }
  13848. return this.parseStatementListItem();
  13849. }
  13850. isExportDefaultSpecifier() {
  13851. const {
  13852. type
  13853. } = this.state;
  13854. if (tokenIsIdentifier(type)) {
  13855. if (type === 95 && !this.state.containsEsc || type === 100) {
  13856. return false;
  13857. }
  13858. if ((type === 130 || type === 129) && !this.state.containsEsc) {
  13859. const {
  13860. type: nextType
  13861. } = this.lookahead();
  13862. if (tokenIsIdentifier(nextType) && nextType !== 98 || nextType === 5) {
  13863. this.expectOnePlugin(["flow", "typescript"]);
  13864. return false;
  13865. }
  13866. }
  13867. } else if (!this.match(65)) {
  13868. return false;
  13869. }
  13870. const next = this.nextTokenStart();
  13871. const hasFrom = this.isUnparsedContextual(next, "from");
  13872. if (this.input.charCodeAt(next) === 44 || tokenIsIdentifier(this.state.type) && hasFrom) {
  13873. return true;
  13874. }
  13875. if (this.match(65) && hasFrom) {
  13876. const nextAfterFrom = this.input.charCodeAt(this.nextTokenStartSince(next + 4));
  13877. return nextAfterFrom === 34 || nextAfterFrom === 39;
  13878. }
  13879. return false;
  13880. }
  13881. parseExportFrom(node, expect) {
  13882. if (this.eatContextual(98)) {
  13883. node.source = this.parseImportSource();
  13884. this.checkExport(node);
  13885. this.maybeParseImportAttributes(node);
  13886. this.checkJSONModuleImport(node);
  13887. } else if (expect) {
  13888. this.unexpected();
  13889. }
  13890. this.semicolon();
  13891. }
  13892. shouldParseExportDeclaration() {
  13893. const {
  13894. type
  13895. } = this.state;
  13896. if (type === 26) {
  13897. this.expectOnePlugin(["decorators", "decorators-legacy"]);
  13898. if (this.hasPlugin("decorators")) {
  13899. if (this.getPluginOption("decorators", "decoratorsBeforeExport") === true) {
  13900. this.raise(Errors.DecoratorBeforeExport, this.state.startLoc);
  13901. }
  13902. return true;
  13903. }
  13904. }
  13905. if (this.hasPlugin("explicitResourceManagement")) {
  13906. if (this.isUsing()) {
  13907. this.raise(Errors.UsingDeclarationExport, this.state.startLoc);
  13908. return true;
  13909. }
  13910. if (this.isAwaitUsing()) {
  13911. this.raise(Errors.UsingDeclarationExport, this.state.startLoc);
  13912. return true;
  13913. }
  13914. }
  13915. return type === 74 || type === 75 || type === 68 || type === 80 || this.isLet() || this.isAsyncFunction();
  13916. }
  13917. checkExport(node, checkNames, isDefault, isFrom) {
  13918. if (checkNames) {
  13919. var _node$specifiers;
  13920. if (isDefault) {
  13921. this.checkDuplicateExports(node, "default");
  13922. if (this.hasPlugin("exportDefaultFrom")) {
  13923. var _declaration$extra;
  13924. const declaration = node.declaration;
  13925. if (declaration.type === "Identifier" && declaration.name === "from" && declaration.end - declaration.start === 4 && !((_declaration$extra = declaration.extra) != null && _declaration$extra.parenthesized)) {
  13926. this.raise(Errors.ExportDefaultFromAsIdentifier, declaration);
  13927. }
  13928. }
  13929. } else if ((_node$specifiers = node.specifiers) != null && _node$specifiers.length) {
  13930. for (const specifier of node.specifiers) {
  13931. const {
  13932. exported
  13933. } = specifier;
  13934. const exportName = exported.type === "Identifier" ? exported.name : exported.value;
  13935. this.checkDuplicateExports(specifier, exportName);
  13936. if (!isFrom && specifier.local) {
  13937. const {
  13938. local
  13939. } = specifier;
  13940. if (local.type !== "Identifier") {
  13941. this.raise(Errors.ExportBindingIsString, specifier, {
  13942. localName: local.value,
  13943. exportName
  13944. });
  13945. } else {
  13946. this.checkReservedWord(local.name, local.loc.start, true, false);
  13947. this.scope.checkLocalExport(local);
  13948. }
  13949. }
  13950. }
  13951. } else if (node.declaration) {
  13952. const decl = node.declaration;
  13953. if (decl.type === "FunctionDeclaration" || decl.type === "ClassDeclaration") {
  13954. const {
  13955. id
  13956. } = decl;
  13957. if (!id) throw new Error("Assertion failure");
  13958. this.checkDuplicateExports(node, id.name);
  13959. } else if (decl.type === "VariableDeclaration") {
  13960. for (const declaration of decl.declarations) {
  13961. this.checkDeclaration(declaration.id);
  13962. }
  13963. }
  13964. }
  13965. }
  13966. }
  13967. checkDeclaration(node) {
  13968. if (node.type === "Identifier") {
  13969. this.checkDuplicateExports(node, node.name);
  13970. } else if (node.type === "ObjectPattern") {
  13971. for (const prop of node.properties) {
  13972. this.checkDeclaration(prop);
  13973. }
  13974. } else if (node.type === "ArrayPattern") {
  13975. for (const elem of node.elements) {
  13976. if (elem) {
  13977. this.checkDeclaration(elem);
  13978. }
  13979. }
  13980. } else if (node.type === "ObjectProperty") {
  13981. this.checkDeclaration(node.value);
  13982. } else if (node.type === "RestElement") {
  13983. this.checkDeclaration(node.argument);
  13984. } else if (node.type === "AssignmentPattern") {
  13985. this.checkDeclaration(node.left);
  13986. }
  13987. }
  13988. checkDuplicateExports(node, exportName) {
  13989. if (this.exportedIdentifiers.has(exportName)) {
  13990. if (exportName === "default") {
  13991. this.raise(Errors.DuplicateDefaultExport, node);
  13992. } else {
  13993. this.raise(Errors.DuplicateExport, node, {
  13994. exportName
  13995. });
  13996. }
  13997. }
  13998. this.exportedIdentifiers.add(exportName);
  13999. }
  14000. parseExportSpecifiers(isInTypeExport) {
  14001. const nodes = [];
  14002. let first = true;
  14003. this.expect(5);
  14004. while (!this.eat(8)) {
  14005. if (first) {
  14006. first = false;
  14007. } else {
  14008. this.expect(12);
  14009. if (this.eat(8)) break;
  14010. }
  14011. const isMaybeTypeOnly = this.isContextual(130);
  14012. const isString = this.match(134);
  14013. const node = this.startNode();
  14014. node.local = this.parseModuleExportName();
  14015. nodes.push(this.parseExportSpecifier(node, isString, isInTypeExport, isMaybeTypeOnly));
  14016. }
  14017. return nodes;
  14018. }
  14019. parseExportSpecifier(node, isString, isInTypeExport, isMaybeTypeOnly) {
  14020. if (this.eatContextual(93)) {
  14021. node.exported = this.parseModuleExportName();
  14022. } else if (isString) {
  14023. node.exported = this.cloneStringLiteral(node.local);
  14024. } else if (!node.exported) {
  14025. node.exported = this.cloneIdentifier(node.local);
  14026. }
  14027. return this.finishNode(node, "ExportSpecifier");
  14028. }
  14029. parseModuleExportName() {
  14030. if (this.match(134)) {
  14031. const result = this.parseStringLiteral(this.state.value);
  14032. const surrogate = loneSurrogate.exec(result.value);
  14033. if (surrogate) {
  14034. this.raise(Errors.ModuleExportNameHasLoneSurrogate, result, {
  14035. surrogateCharCode: surrogate[0].charCodeAt(0)
  14036. });
  14037. }
  14038. return result;
  14039. }
  14040. return this.parseIdentifier(true);
  14041. }
  14042. isJSONModuleImport(node) {
  14043. if (node.assertions != null) {
  14044. return node.assertions.some(({
  14045. key,
  14046. value
  14047. }) => {
  14048. return value.value === "json" && (key.type === "Identifier" ? key.name === "type" : key.value === "type");
  14049. });
  14050. }
  14051. return false;
  14052. }
  14053. checkImportReflection(node) {
  14054. const {
  14055. specifiers
  14056. } = node;
  14057. const singleBindingType = specifiers.length === 1 ? specifiers[0].type : null;
  14058. if (node.phase === "source") {
  14059. if (singleBindingType !== "ImportDefaultSpecifier") {
  14060. this.raise(Errors.SourcePhaseImportRequiresDefault, specifiers[0].loc.start);
  14061. }
  14062. } else if (node.phase === "defer") {
  14063. if (singleBindingType !== "ImportNamespaceSpecifier") {
  14064. this.raise(Errors.DeferImportRequiresNamespace, specifiers[0].loc.start);
  14065. }
  14066. } else if (node.module) {
  14067. var _node$assertions;
  14068. if (singleBindingType !== "ImportDefaultSpecifier") {
  14069. this.raise(Errors.ImportReflectionNotBinding, specifiers[0].loc.start);
  14070. }
  14071. if (((_node$assertions = node.assertions) == null ? void 0 : _node$assertions.length) > 0) {
  14072. this.raise(Errors.ImportReflectionHasAssertion, specifiers[0].loc.start);
  14073. }
  14074. }
  14075. }
  14076. checkJSONModuleImport(node) {
  14077. if (this.isJSONModuleImport(node) && node.type !== "ExportAllDeclaration") {
  14078. const {
  14079. specifiers
  14080. } = node;
  14081. if (specifiers != null) {
  14082. const nonDefaultNamedSpecifier = specifiers.find(specifier => {
  14083. let imported;
  14084. if (specifier.type === "ExportSpecifier") {
  14085. imported = specifier.local;
  14086. } else if (specifier.type === "ImportSpecifier") {
  14087. imported = specifier.imported;
  14088. }
  14089. if (imported !== undefined) {
  14090. return imported.type === "Identifier" ? imported.name !== "default" : imported.value !== "default";
  14091. }
  14092. });
  14093. if (nonDefaultNamedSpecifier !== undefined) {
  14094. this.raise(Errors.ImportJSONBindingNotDefault, nonDefaultNamedSpecifier.loc.start);
  14095. }
  14096. }
  14097. }
  14098. }
  14099. isPotentialImportPhase(isExport) {
  14100. if (isExport) return false;
  14101. return this.isContextual(105) || this.isContextual(97) || this.isContextual(127);
  14102. }
  14103. applyImportPhase(node, isExport, phase, loc) {
  14104. if (isExport) {
  14105. return;
  14106. }
  14107. if (phase === "module") {
  14108. this.expectPlugin("importReflection", loc);
  14109. node.module = true;
  14110. } else if (this.hasPlugin("importReflection")) {
  14111. node.module = false;
  14112. }
  14113. if (phase === "source") {
  14114. this.expectPlugin("sourcePhaseImports", loc);
  14115. node.phase = "source";
  14116. } else if (phase === "defer") {
  14117. this.expectPlugin("deferredImportEvaluation", loc);
  14118. node.phase = "defer";
  14119. } else if (this.hasPlugin("sourcePhaseImports")) {
  14120. node.phase = null;
  14121. }
  14122. }
  14123. parseMaybeImportPhase(node, isExport) {
  14124. if (!this.isPotentialImportPhase(isExport)) {
  14125. this.applyImportPhase(node, isExport, null);
  14126. return null;
  14127. }
  14128. const phaseIdentifier = this.startNode();
  14129. const phaseIdentifierName = this.parseIdentifierName(true);
  14130. const {
  14131. type
  14132. } = this.state;
  14133. const isImportPhase = tokenIsKeywordOrIdentifier(type) ? type !== 98 || this.lookaheadCharCode() === 102 : type !== 12;
  14134. if (isImportPhase) {
  14135. this.applyImportPhase(node, isExport, phaseIdentifierName, phaseIdentifier.loc.start);
  14136. return null;
  14137. } else {
  14138. this.applyImportPhase(node, isExport, null);
  14139. return this.createIdentifier(phaseIdentifier, phaseIdentifierName);
  14140. }
  14141. }
  14142. isPrecedingIdImportPhase(phase) {
  14143. const {
  14144. type
  14145. } = this.state;
  14146. return tokenIsIdentifier(type) ? type !== 98 || this.lookaheadCharCode() === 102 : type !== 12;
  14147. }
  14148. parseImport(node) {
  14149. if (this.match(134)) {
  14150. return this.parseImportSourceAndAttributes(node);
  14151. }
  14152. return this.parseImportSpecifiersAndAfter(node, this.parseMaybeImportPhase(node, false));
  14153. }
  14154. parseImportSpecifiersAndAfter(node, maybeDefaultIdentifier) {
  14155. node.specifiers = [];
  14156. const hasDefault = this.maybeParseDefaultImportSpecifier(node, maybeDefaultIdentifier);
  14157. const parseNext = !hasDefault || this.eat(12);
  14158. const hasStar = parseNext && this.maybeParseStarImportSpecifier(node);
  14159. if (parseNext && !hasStar) this.parseNamedImportSpecifiers(node);
  14160. this.expectContextual(98);
  14161. return this.parseImportSourceAndAttributes(node);
  14162. }
  14163. parseImportSourceAndAttributes(node) {
  14164. var _node$specifiers2;
  14165. (_node$specifiers2 = node.specifiers) != null ? _node$specifiers2 : node.specifiers = [];
  14166. node.source = this.parseImportSource();
  14167. this.maybeParseImportAttributes(node);
  14168. this.checkImportReflection(node);
  14169. this.checkJSONModuleImport(node);
  14170. this.semicolon();
  14171. this.sawUnambiguousESM = true;
  14172. return this.finishNode(node, "ImportDeclaration");
  14173. }
  14174. parseImportSource() {
  14175. if (!this.match(134)) this.unexpected();
  14176. return this.parseExprAtom();
  14177. }
  14178. parseImportSpecifierLocal(node, specifier, type) {
  14179. specifier.local = this.parseIdentifier();
  14180. node.specifiers.push(this.finishImportSpecifier(specifier, type));
  14181. }
  14182. finishImportSpecifier(specifier, type, bindingType = 8201) {
  14183. this.checkLVal(specifier.local, {
  14184. type
  14185. }, bindingType);
  14186. return this.finishNode(specifier, type);
  14187. }
  14188. parseImportAttributes() {
  14189. this.expect(5);
  14190. const attrs = [];
  14191. const attrNames = new Set();
  14192. do {
  14193. if (this.match(8)) {
  14194. break;
  14195. }
  14196. const node = this.startNode();
  14197. const keyName = this.state.value;
  14198. if (attrNames.has(keyName)) {
  14199. this.raise(Errors.ModuleAttributesWithDuplicateKeys, this.state.startLoc, {
  14200. key: keyName
  14201. });
  14202. }
  14203. attrNames.add(keyName);
  14204. if (this.match(134)) {
  14205. node.key = this.parseStringLiteral(keyName);
  14206. } else {
  14207. node.key = this.parseIdentifier(true);
  14208. }
  14209. this.expect(14);
  14210. if (!this.match(134)) {
  14211. throw this.raise(Errors.ModuleAttributeInvalidValue, this.state.startLoc);
  14212. }
  14213. node.value = this.parseStringLiteral(this.state.value);
  14214. attrs.push(this.finishNode(node, "ImportAttribute"));
  14215. } while (this.eat(12));
  14216. this.expect(8);
  14217. return attrs;
  14218. }
  14219. parseModuleAttributes() {
  14220. const attrs = [];
  14221. const attributes = new Set();
  14222. do {
  14223. const node = this.startNode();
  14224. node.key = this.parseIdentifier(true);
  14225. if (node.key.name !== "type") {
  14226. this.raise(Errors.ModuleAttributeDifferentFromType, node.key);
  14227. }
  14228. if (attributes.has(node.key.name)) {
  14229. this.raise(Errors.ModuleAttributesWithDuplicateKeys, node.key, {
  14230. key: node.key.name
  14231. });
  14232. }
  14233. attributes.add(node.key.name);
  14234. this.expect(14);
  14235. if (!this.match(134)) {
  14236. throw this.raise(Errors.ModuleAttributeInvalidValue, this.state.startLoc);
  14237. }
  14238. node.value = this.parseStringLiteral(this.state.value);
  14239. attrs.push(this.finishNode(node, "ImportAttribute"));
  14240. } while (this.eat(12));
  14241. return attrs;
  14242. }
  14243. maybeParseImportAttributes(node) {
  14244. let attributes;
  14245. {
  14246. var useWith = false;
  14247. }
  14248. if (this.match(76)) {
  14249. if (this.hasPrecedingLineBreak() && this.lookaheadCharCode() === 40) {
  14250. return;
  14251. }
  14252. this.next();
  14253. if (this.hasPlugin("moduleAttributes")) {
  14254. attributes = this.parseModuleAttributes();
  14255. this.addExtra(node, "deprecatedWithLegacySyntax", true);
  14256. } else {
  14257. attributes = this.parseImportAttributes();
  14258. }
  14259. {
  14260. useWith = true;
  14261. }
  14262. } else if (this.isContextual(94) && !this.hasPrecedingLineBreak()) {
  14263. if (!this.hasPlugin("deprecatedImportAssert") && !this.hasPlugin("importAssertions")) {
  14264. this.raise(Errors.ImportAttributesUseAssert, this.state.startLoc);
  14265. }
  14266. if (!this.hasPlugin("importAssertions")) {
  14267. this.addExtra(node, "deprecatedAssertSyntax", true);
  14268. }
  14269. this.next();
  14270. attributes = this.parseImportAttributes();
  14271. } else {
  14272. attributes = [];
  14273. }
  14274. if (!useWith && this.hasPlugin("importAssertions")) {
  14275. node.assertions = attributes;
  14276. } else {
  14277. node.attributes = attributes;
  14278. }
  14279. }
  14280. maybeParseDefaultImportSpecifier(node, maybeDefaultIdentifier) {
  14281. if (maybeDefaultIdentifier) {
  14282. const specifier = this.startNodeAtNode(maybeDefaultIdentifier);
  14283. specifier.local = maybeDefaultIdentifier;
  14284. node.specifiers.push(this.finishImportSpecifier(specifier, "ImportDefaultSpecifier"));
  14285. return true;
  14286. } else if (tokenIsKeywordOrIdentifier(this.state.type)) {
  14287. this.parseImportSpecifierLocal(node, this.startNode(), "ImportDefaultSpecifier");
  14288. return true;
  14289. }
  14290. return false;
  14291. }
  14292. maybeParseStarImportSpecifier(node) {
  14293. if (this.match(55)) {
  14294. const specifier = this.startNode();
  14295. this.next();
  14296. this.expectContextual(93);
  14297. this.parseImportSpecifierLocal(node, specifier, "ImportNamespaceSpecifier");
  14298. return true;
  14299. }
  14300. return false;
  14301. }
  14302. parseNamedImportSpecifiers(node) {
  14303. let first = true;
  14304. this.expect(5);
  14305. while (!this.eat(8)) {
  14306. if (first) {
  14307. first = false;
  14308. } else {
  14309. if (this.eat(14)) {
  14310. throw this.raise(Errors.DestructureNamedImport, this.state.startLoc);
  14311. }
  14312. this.expect(12);
  14313. if (this.eat(8)) break;
  14314. }
  14315. const specifier = this.startNode();
  14316. const importedIsString = this.match(134);
  14317. const isMaybeTypeOnly = this.isContextual(130);
  14318. specifier.imported = this.parseModuleExportName();
  14319. const importSpecifier = this.parseImportSpecifier(specifier, importedIsString, node.importKind === "type" || node.importKind === "typeof", isMaybeTypeOnly, undefined);
  14320. node.specifiers.push(importSpecifier);
  14321. }
  14322. }
  14323. parseImportSpecifier(specifier, importedIsString, isInTypeOnlyImport, isMaybeTypeOnly, bindingType) {
  14324. if (this.eatContextual(93)) {
  14325. specifier.local = this.parseIdentifier();
  14326. } else {
  14327. const {
  14328. imported
  14329. } = specifier;
  14330. if (importedIsString) {
  14331. throw this.raise(Errors.ImportBindingIsString, specifier, {
  14332. importName: imported.value
  14333. });
  14334. }
  14335. this.checkReservedWord(imported.name, specifier.loc.start, true, true);
  14336. if (!specifier.local) {
  14337. specifier.local = this.cloneIdentifier(imported);
  14338. }
  14339. }
  14340. return this.finishImportSpecifier(specifier, "ImportSpecifier", bindingType);
  14341. }
  14342. isThisParam(param) {
  14343. return param.type === "Identifier" && param.name === "this";
  14344. }
  14345. }
  14346. class Parser extends StatementParser {
  14347. constructor(options, input, pluginsMap) {
  14348. options = getOptions(options);
  14349. super(options, input);
  14350. this.options = options;
  14351. this.initializeScopes();
  14352. this.plugins = pluginsMap;
  14353. this.filename = options.sourceFilename;
  14354. this.startIndex = options.startIndex;
  14355. let optionFlags = 0;
  14356. if (options.allowAwaitOutsideFunction) {
  14357. optionFlags |= 1;
  14358. }
  14359. if (options.allowReturnOutsideFunction) {
  14360. optionFlags |= 2;
  14361. }
  14362. if (options.allowImportExportEverywhere) {
  14363. optionFlags |= 8;
  14364. }
  14365. if (options.allowSuperOutsideMethod) {
  14366. optionFlags |= 16;
  14367. }
  14368. if (options.allowUndeclaredExports) {
  14369. optionFlags |= 64;
  14370. }
  14371. if (options.allowNewTargetOutsideFunction) {
  14372. optionFlags |= 4;
  14373. }
  14374. if (options.allowYieldOutsideFunction) {
  14375. optionFlags |= 32;
  14376. }
  14377. if (options.ranges) {
  14378. optionFlags |= 128;
  14379. }
  14380. if (options.tokens) {
  14381. optionFlags |= 256;
  14382. }
  14383. if (options.createImportExpressions) {
  14384. optionFlags |= 512;
  14385. }
  14386. if (options.createParenthesizedExpressions) {
  14387. optionFlags |= 1024;
  14388. }
  14389. if (options.errorRecovery) {
  14390. optionFlags |= 2048;
  14391. }
  14392. if (options.attachComment) {
  14393. optionFlags |= 4096;
  14394. }
  14395. if (options.annexB) {
  14396. optionFlags |= 8192;
  14397. }
  14398. this.optionFlags = optionFlags;
  14399. }
  14400. getScopeHandler() {
  14401. return ScopeHandler;
  14402. }
  14403. parse() {
  14404. this.enterInitialScopes();
  14405. const file = this.startNode();
  14406. const program = this.startNode();
  14407. this.nextToken();
  14408. file.errors = null;
  14409. this.parseTopLevel(file, program);
  14410. file.errors = this.state.errors;
  14411. file.comments.length = this.state.commentsLen;
  14412. return file;
  14413. }
  14414. }
  14415. function parse(input, options) {
  14416. var _options;
  14417. if (((_options = options) == null ? void 0 : _options.sourceType) === "unambiguous") {
  14418. options = Object.assign({}, options);
  14419. try {
  14420. options.sourceType = "module";
  14421. const parser = getParser(options, input);
  14422. const ast = parser.parse();
  14423. if (parser.sawUnambiguousESM) {
  14424. return ast;
  14425. }
  14426. if (parser.ambiguousScriptDifferentAst) {
  14427. try {
  14428. options.sourceType = "script";
  14429. return getParser(options, input).parse();
  14430. } catch (_unused) {}
  14431. } else {
  14432. ast.program.sourceType = "script";
  14433. }
  14434. return ast;
  14435. } catch (moduleError) {
  14436. try {
  14437. options.sourceType = "script";
  14438. return getParser(options, input).parse();
  14439. } catch (_unused2) {}
  14440. throw moduleError;
  14441. }
  14442. } else {
  14443. return getParser(options, input).parse();
  14444. }
  14445. }
  14446. function parseExpression(input, options) {
  14447. const parser = getParser(options, input);
  14448. if (parser.options.strictMode) {
  14449. parser.state.strict = true;
  14450. }
  14451. return parser.getExpression();
  14452. }
  14453. function generateExportedTokenTypes(internalTokenTypes) {
  14454. const tokenTypes = {};
  14455. for (const typeName of Object.keys(internalTokenTypes)) {
  14456. tokenTypes[typeName] = getExportedToken(internalTokenTypes[typeName]);
  14457. }
  14458. return tokenTypes;
  14459. }
  14460. const tokTypes = generateExportedTokenTypes(tt);
  14461. function getParser(options, input) {
  14462. let cls = Parser;
  14463. const pluginsMap = new Map();
  14464. if (options != null && options.plugins) {
  14465. for (const plugin of options.plugins) {
  14466. let name, opts;
  14467. if (typeof plugin === "string") {
  14468. name = plugin;
  14469. } else {
  14470. [name, opts] = plugin;
  14471. }
  14472. if (!pluginsMap.has(name)) {
  14473. pluginsMap.set(name, opts || {});
  14474. }
  14475. }
  14476. validatePlugins(pluginsMap);
  14477. cls = getParserClass(pluginsMap);
  14478. }
  14479. return new cls(options, input, pluginsMap);
  14480. }
  14481. const parserClassCache = new Map();
  14482. function getParserClass(pluginsMap) {
  14483. const pluginList = [];
  14484. for (const name of mixinPluginNames) {
  14485. if (pluginsMap.has(name)) {
  14486. pluginList.push(name);
  14487. }
  14488. }
  14489. const key = pluginList.join("|");
  14490. let cls = parserClassCache.get(key);
  14491. if (!cls) {
  14492. cls = Parser;
  14493. for (const plugin of pluginList) {
  14494. cls = mixinPlugins[plugin](cls);
  14495. }
  14496. parserClassCache.set(key, cls);
  14497. }
  14498. return cls;
  14499. }
  14500. exports.parse = parse;
  14501. exports.parseExpression = parseExpression;
  14502. exports.tokTypes = tokTypes;
  14503. //# sourceMappingURL=index.js.map